summaryrefslogtreecommitdiff
path: root/tests/testwidgetfocus.c
blob: e28d4aa79451e1671099cd354f557a650ed414a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include <gtk/gtk.h>



typedef struct _GtkFocusWidget      GtkFocusWidget;
typedef struct _GtkFocusWidgetClass GtkFocusWidgetClass;

#define GTK_TYPE_FOCUS_WIDGET           (gtk_focus_widget_get_type ())
#define GTK_FOCUS_WIDGET(obj)           (G_TYPE_CHECK_INSTANCE_CAST(obj, GTK_TYPE_FOCUS_WIDGET, GtkFocusWidget))
#define GTK_FOCUS_WIDGET_CLASS(cls)     (G_TYPE_CHECK_CLASS_CAST(cls, GTK_TYPE_FOCUS_WIDGET, GtkFocusWidgetClass))
#define GTK_IS_FOCUS_WIDGET(obj)        (G_TYPE_CHECK_INSTANCE_TYPE(obj, GTK_TYPE_FOCUS_WIDGET))
#define GTK_IS_FOCUS_WIDGET_CLASS(cls)   (G_TYPE_CHECK_CLASS_TYPE(cls, GTK_TYPE_FOCUS_WIDGET))
#define GTK_FOCUS_WIDGET_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS(obj, GTK_TYPE_FOCUS_WIDGET, GtkFocusWidgetClass))

const char *css =
"* {"
"  transition: none; "
"}"
"focuswidget {"
"  padding: 30px;"
"  font-size: 70%;"
"}"
"focuswidget button:nth-child(1) {"
"  margin-right: 15px;"
"  margin-bottom: 15px;"
"}"
"focuswidget button:nth-child(2) {"
"  margin-left: 15px;"
"  margin-bottom: 15px;"
"}"
"focuswidget button:nth-child(3) {"
"  margin-right: 15px;"
"  margin-top: 15px;"
"}"
"focuswidget button:nth-child(4) {"
"  margin-left: 15px;"
"  margin-top: 15px;"
"}"
"focuswidget button {"
"  min-width: 80px;"
"  min-height: 80px;"
"  margin: 0px;"
"  border: 5px solid green;"
"  border-radius: 0px;"
"  padding: 10px;"
"  background-image: none;"
"  background-color: white;"
"  box-shadow: none;"
"}"
"focuswidget button:focus-visible {"
"  outline-width: 4px;"
"  outline-color: yellow;"
"}"
"focuswidget button:hover {"
"  background-color: black;"
"  color: white;"
"}"
"focuswidget button label:hover {"
"  background-color: green;"
"}"
;

struct _GtkFocusWidget
{
  GtkWidget parent_instance;
  double mouse_x;
  double mouse_y;

  union {
    struct {
      GtkWidget *child1;
      GtkWidget *child2;
      GtkWidget *child3;
      GtkWidget *child4;
    };
    GtkWidget* children[4];
  };
};

struct _GtkFocusWidgetClass
{
  GtkWidgetClass parent_class;
};

GType gtk_focus_widget_get_type (void) G_GNUC_CONST;


G_DEFINE_TYPE(GtkFocusWidget, gtk_focus_widget, GTK_TYPE_WIDGET)

static void
gtk_focus_widget_size_allocate (GtkWidget *widget,
                                int        width,
                                int        height,
                                int        baseline)
{
  GtkFocusWidget *self = GTK_FOCUS_WIDGET (widget);
  int child_width  = width  / 2;
  int child_height = height / 2;
  GtkAllocation child_alloc;

  child_alloc.x = 0;
  child_alloc.y = 0;
  child_alloc.width = child_width;
  child_alloc.height = child_height;

  gtk_widget_size_allocate (self->child1, &child_alloc, -1);

  child_alloc.x += child_width;

  gtk_widget_size_allocate (self->child2, &child_alloc, -1);

  child_alloc.y += child_height;

  gtk_widget_size_allocate (self->child4, &child_alloc, -1);

  child_alloc.x -= child_width;

  gtk_widget_size_allocate (self->child3, &child_alloc, -1);
}

static void
gtk_focus_widget_measure (GtkWidget      *widget,
                          GtkOrientation  orientation,
                          int             for_size,
                          int            *minimum,
                          int            *natural,
                          int            *minimum_baseline,
                          int            *natural_baseline)
{
  GtkFocusWidget *self = GTK_FOCUS_WIDGET (widget);
  int min, nat;
  int i;

  *minimum = 0;
  *natural = 0;

  for (i = 0; i < 4; i ++)
    {
      gtk_widget_measure (self->children[i], orientation, for_size,
                          &min, &nat, NULL, NULL);

      *minimum = MAX (*minimum, min);
      *natural = MAX (*natural, nat);
    }

  *minimum *= 2;
  *natural *= 2;
}

static void
gtk_focus_widget_snapshot (GtkWidget *widget, GtkSnapshot *snapshot)
{
  GtkFocusWidget *self = GTK_FOCUS_WIDGET (widget);

  gtk_widget_snapshot_child (widget, self->child1, snapshot);
  gtk_widget_snapshot_child (widget, self->child2, snapshot);
  gtk_widget_snapshot_child (widget, self->child3, snapshot);
  gtk_widget_snapshot_child (widget, self->child4, snapshot);

  if (self->mouse_x != G_MININT && self->mouse_y != G_MININT)
    {
      PangoLayout *layout;
      char *text;
      GtkAllocation alloc;
      graphene_rect_t bounds;
      GdkRGBA black = {0, 0, 0, 1};

      gtk_widget_get_allocation (widget, &alloc);

      /* Since event coordinates and drawing is supposed to happen in the
       * same coordinates space, this should all work out just fine. */
      bounds.origin.x = self->mouse_x;
      bounds.origin.y = -30;
      bounds.size.width = 1;
      bounds.size.height = alloc.height;
      gtk_snapshot_append_color (snapshot,
                                 &black,
                                 &bounds);

      bounds.origin.x = -30;
      bounds.origin.y = self->mouse_y;
      bounds.size.width = alloc.width;
      bounds.size.height = 1;
      gtk_snapshot_append_color (snapshot,
                                 &black,
                                 &bounds);

      layout = gtk_widget_create_pango_layout (widget, NULL);
      text = g_strdup_printf ("%.2f×%.2f", self->mouse_x, self->mouse_y);
      pango_layout_set_text (layout, text, -1);

      gtk_snapshot_render_layout (snapshot,
                                  gtk_widget_get_style_context (widget),
                                  self->mouse_x + 2,
                                  self->mouse_y - 15, /* *shrug* */
                                  layout);

      g_free (text);
      g_object_unref (layout);
    }
}

static void
motion_cb (GtkEventControllerMotion *controller,
           double                    x,
           double                    y,
           GtkWidget                *widget)
{
  GtkFocusWidget *self = GTK_FOCUS_WIDGET (widget);

  self->mouse_x = x;
  self->mouse_y = y;

  gtk_widget_queue_draw (widget);
}

static void
gtk_focus_widget_finalize (GObject *object)
{
  GtkFocusWidget *self = GTK_FOCUS_WIDGET (object);

  gtk_widget_unparent (self->child1);
  gtk_widget_unparent (self->child2);
  gtk_widget_unparent (self->child3);
  gtk_widget_unparent (self->child4);

  G_OBJECT_CLASS (gtk_focus_widget_parent_class)->finalize (object);
}

static void
gtk_focus_widget_init (GtkFocusWidget *self)
{
  GtkEventController *controller;

  self->child1 = gtk_button_new_with_label ("1");
  gtk_widget_set_parent (self->child1, GTK_WIDGET (self));
  self->child2 = gtk_button_new_with_label ("2");
  gtk_widget_set_parent (self->child2, GTK_WIDGET (self));
  self->child3 = gtk_button_new_with_label ("3");
  gtk_widget_set_parent (self->child3, GTK_WIDGET (self));
  self->child4 = gtk_button_new_with_label ("4");
  gtk_widget_set_parent (self->child4, GTK_WIDGET (self));

  self->mouse_x = G_MININT;
  self->mouse_y = G_MININT;

  controller = gtk_event_controller_motion_new ();
  g_signal_connect (controller, "motion",
		    G_CALLBACK (motion_cb), self);
  gtk_widget_add_controller (GTK_WIDGET (self), controller);
}

static void
gtk_focus_widget_class_init (GtkFocusWidgetClass *klass)
{
  GObjectClass   *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  object_class->finalize = gtk_focus_widget_finalize;

  widget_class->snapshot = gtk_focus_widget_snapshot;
  widget_class->measure = gtk_focus_widget_measure;
  widget_class->size_allocate = gtk_focus_widget_size_allocate;

  gtk_widget_class_set_css_name (widget_class, "focuswidget");
}

static void
quit_cb (GtkWidget *widget,
         gpointer   user_data)
{
  gboolean *is_done = user_data;

  *is_done = TRUE;

  g_main_context_wakeup (NULL);
}

int
main(int argc, char **argv)
{
  GtkWidget *window;
  GtkWidget *widget;
  GtkCssProvider *provider;
  gboolean done = FALSE;

  gtk_init ();

  provider = gtk_css_provider_new ();
  gtk_css_provider_load_from_data (provider, css, -1);
  gtk_style_context_add_provider_for_display (gdk_display_get_default (),
                                              GTK_STYLE_PROVIDER (provider),
                                              GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

  window = gtk_window_new ();
  widget = g_object_new (GTK_TYPE_FOCUS_WIDGET, NULL);

  gtk_window_set_decorated (GTK_WINDOW (window), FALSE);

  gtk_window_set_child (GTK_WINDOW (window), widget);
  g_signal_connect (window, "destroy", G_CALLBACK (quit_cb), &done);

  gtk_widget_show (window);

  while (!done)
    g_main_context_iteration (NULL, TRUE);
}