Debian Codegraph @

demos/gtk-demo/foreigndrawing.c

raw
1 /* Foreign drawing
2 *
3 * Many applications can't use GTK+ widgets, for a variety of reasons,
4 * but still want their user interface to appear integrated with the
5 * rest of the desktop, and follow GTK+ themes. This demo shows how to
6 * use GtkStyleContext and the gtk_render_ APIs to achieve this.
7 *
8 * Note that this is a very simple, non-interactive example.
9 */
10
11 #include <gtk/gtk.h>
12 #include <string.h>
13
14 static void
15 append_element (GtkWidgetPath *path,
16 const char *selector)
17 {
18 static const struct {
19 const char *name;
20 GtkStateFlags state_flag;
21 } pseudo_classes[] = {
22 { "active", GTK_STATE_FLAG_ACTIVE },
23 { "hover", GTK_STATE_FLAG_PRELIGHT },
24 { "selected", GTK_STATE_FLAG_SELECTED },
25 { "disabled", GTK_STATE_FLAG_INSENSITIVE },
26 { "indeterminate", GTK_STATE_FLAG_INCONSISTENT },
27 { "focus", GTK_STATE_FLAG_FOCUSED },
28 { "backdrop", GTK_STATE_FLAG_BACKDROP },
29 { "dir(ltr)", GTK_STATE_FLAG_DIR_LTR },
30 { "dir(rtl)", GTK_STATE_FLAG_DIR_RTL },
31 { "link", GTK_STATE_FLAG_LINK },
32 { "visited", GTK_STATE_FLAG_VISITED },
33 { "checked", GTK_STATE_FLAG_CHECKED },
34 { "drop(active)", GTK_STATE_FLAG_DROP_ACTIVE }
35 };
36 const char *next;
37 char *name;
38 char type;
39 guint i;
40
41 next = strpbrk (selector, "#.:");
42 if (next == NULL)
43 next = selector + strlen (selector);
44
45 name = g_strndup (selector, next - selector);
46 if (g_ascii_isupper (selector[0]))
47 {
48 GType gtype;
49 gtype = g_type_from_name (name);
50 if (gtype == G_TYPE_INVALID)
51 {
52 g_critical ("Unknown type name `%s'", name);
53 g_free (name);
54 return;
55 }
56 gtk_widget_path_append_type (path, gtype);
57 }
58 else
59 {
60 /* Omit type, we're using name */
61 gtk_widget_path_append_type (path, G_TYPE_NONE);
62 gtk_widget_path_iter_set_object_name (path, -1, name);
63 }
64 g_free (name);
65
66 while (*next != '\0')
67 {
68 type = *next;
69 selector = next + 1;
70 next = strpbrk (selector, "#.:");
71 if (next == NULL)
72 next = selector + strlen (selector);
73 name = g_strndup (selector, next - selector);
74
75 switch (type)
76 {
77 case '#':
78 gtk_widget_path_iter_set_name (path, -1, name);
79 break;
80
81 case '.':
82 gtk_widget_path_iter_add_class (path, -1, name);
83 break;
84
85 case ':':
86 for (i = 0; i < G_N_ELEMENTS (pseudo_classes); i++)
87 {
88 if (g_str_equal (pseudo_classes[i].name, name))
89 {
90 gtk_widget_path_iter_set_state (path,
91 -1,
92 gtk_widget_path_iter_get_state (path, -1)
93 | pseudo_classes[i].state_flag);
94 break;
95 }
96 }
97 if (i == G_N_ELEMENTS (pseudo_classes))
98 g_critical ("Unknown pseudo-class :%s", name);
99 break;
100
101 default:
102 g_assert_not_reached ();
103 break;
104 }
105
106 g_free (name);
107 }
108 }
109
110 static GtkStyleContext *
111 create_context_for_path (GtkWidgetPath *path,
112 GtkStyleContext *parent)
113 {
114 GtkStyleContext *context;
115
116 context = gtk_style_context_new ();
117 gtk_style_context_set_path (context, path);
118 gtk_style_context_set_parent (context, parent);
119 /* Unfortunately, we have to explicitly set the state again here
120 * for it to take effect
121 */
122 gtk_style_context_set_state (context, gtk_widget_path_iter_get_state (path, -1));
123 gtk_widget_path_unref (path);
124
125 return context;
126 }
127
128 static GtkStyleContext *
129 get_style (GtkStyleContext *parent,
130 const char *selector)
131 {
132 GtkWidgetPath *path;
133
134 if (parent)
135 path = gtk_widget_path_copy (gtk_style_context_get_path (parent));
136 else
137 path = gtk_widget_path_new ();
138
139 append_element (path, selector);
140
141 return create_context_for_path (path, parent);
142 }
143
144 static GtkStyleContext *
145 get_style_with_siblings (GtkStyleContext *parent,
146 const char *selector,
147 const char **siblings,
148 gint position)
149 {
150 GtkWidgetPath *path, *siblings_path;
151 guint i;
152
153 if (parent)
154 path = gtk_widget_path_copy (gtk_style_context_get_path (parent));
155 else
156 path = gtk_widget_path_new ();
157
158 siblings_path = gtk_widget_path_new ();
159 for (i = 0; siblings[i]; i++)
160 append_element (siblings_path, siblings[i]);
161
162 gtk_widget_path_append_with_siblings (path, siblings_path, position);
163 gtk_widget_path_unref (siblings_path);
164
165 return create_context_for_path (path, parent);
166 }
167
168 static void
169 draw_style_common (GtkStyleContext *context,
170 cairo_t *cr,
171 gint x,
172 gint y,
173 gint width,
174 gint height,
175 gint *contents_x,
176 gint *contents_y,
177 gint *contents_width,
178 gint *contents_height)
179 {
180 GtkBorder margin, border, padding;
181 int min_width, min_height;
182
183 gtk_style_context_get_margin (context, gtk_style_context_get_state (context), &margin);
184 gtk_style_context_get_border (context, gtk_style_context_get_state (context), &border);
185 gtk_style_context_get_padding (context, gtk_style_context_get_state (context), &padding);
186
187 gtk_style_context_get (context, gtk_style_context_get_state (context),
188 "min-width", &min_width,
189 "min-height", &min_height,
190 NULL);
191 x += margin.left;
192 y += margin.top;
193 width -= margin.left + margin.right;
194 height -= margin.top + margin.bottom;
195
196 width = MAX (width, min_width);
197 height = MAX (height, min_height);
198
199 gtk_render_background (context, cr, x, y, width, height);
200 gtk_render_frame (context, cr, x, y, width, height);
201
202 if (contents_x)
203 *contents_x = x + border.left + padding.left;
204 if (contents_y)
205 *contents_y = y + border.top + padding.top;
206 if (contents_width)
207 *contents_width = width - border.left - border.right - padding.left - padding.right;
208 if (contents_height)
209 *contents_height = height - border.top - border.bottom - padding.top - padding.bottom;
210 }
211
212 static void
213 query_size (GtkStyleContext *context,
214 gint *width,
215 gint *height)
216 {
217 GtkBorder margin, border, padding;
218 int min_width, min_height;
219
220 gtk_style_context_get_margin (context, gtk_style_context_get_state (context), &margin);
221 gtk_style_context_get_border (context, gtk_style_context_get_state (context), &border);
222 gtk_style_context_get_padding (context, gtk_style_context_get_state (context), &padding);
223
224 gtk_style_context_get (context, gtk_style_context_get_state (context),
225 "min-width", &min_width,
226 "min-height", &min_height,
227 NULL);
228
229 min_width += margin.left + margin.right + border.left + border.right + padding.left + padding.right;
230 min_height += margin.top + margin.bottom + border.top + border.bottom + padding.top + padding.bottom;
231
232 if (width)
233 *width = MAX (*width, min_width);
234 if (height)
235 *height = MAX (*height, min_height);
236 }
237
238 static void
239 draw_menu (GtkWidget *widget,
240 cairo_t *cr,
241 gint x,
242 gint y,
243 gint width,
244 gint *height)
245 {
246 GtkStyleContext *menu_context;
247 GtkStyleContext *menuitem_context;
248 GtkStyleContext *hovermenuitem_context;
249 GtkStyleContext *hoveredarrowmenuitem_context;
250 GtkStyleContext *arrowmenuitem_context;
251 GtkStyleContext *checkmenuitem_context;
252 GtkStyleContext *disabledarrowmenuitem_context;
253 GtkStyleContext *disabledcheckmenuitem_context;
254 GtkStyleContext *radiomenuitem_context;
255 GtkStyleContext *disablemenuitem_context;
256 GtkStyleContext *disabledradiomenuitem_context;
257 GtkStyleContext *separatormenuitem_context;
258 gint menuitem1_height, menuitem2_height, menuitem3_height, menuitem4_height, menuitem5_height;
259 gint contents_x, contents_y, contents_width, contents_height;
260 gint menu_x, menu_y, menu_width, menu_height;
261 gint arrow_width, arrow_height, arrow_size;
262 gint toggle_x, toggle_y, toggle_width, toggle_height;
263
264 /* This information is taken from the GtkMenu docs, see "CSS nodes" */
265 menu_context = get_style (gtk_widget_get_style_context(widget), "menu");
266 hovermenuitem_context = get_style (menu_context, "menuitem:hover");
267 hoveredarrowmenuitem_context = get_style (hovermenuitem_context, "arrow.right:dir(ltr)");
268 menuitem_context = get_style (menu_context, "menuitem");
269 arrowmenuitem_context = get_style (menuitem_context, "arrow:dir(rtl)");
270 disablemenuitem_context = get_style (menu_context, "menuitem:disabled");
271 disabledarrowmenuitem_context = get_style (disablemenuitem_context, "arrow:dir(rtl)");
272 checkmenuitem_context = get_style (menuitem_context, "check:checked");
273 disabledcheckmenuitem_context = get_style (disablemenuitem_context, "check");
274 separatormenuitem_context = get_style (menu_context, "separator:disabled");
275 radiomenuitem_context = get_style (menuitem_context, "radio:checked");
276 disabledradiomenuitem_context = get_style (disablemenuitem_context, "radio");
277
278 *height = 0;
279 query_size (menu_context, NULL, height);
280 menuitem1_height = 0;
281 query_size (hovermenuitem_context, NULL, &menuitem1_height);
282 query_size (hoveredarrowmenuitem_context, NULL, &menuitem1_height);
283 *height += menuitem1_height;
284 menuitem2_height = 0;
285 query_size (menu_context, NULL, &menuitem5_height);
286 query_size (menuitem_context, NULL, &menuitem2_height);
287 query_size (arrowmenuitem_context, NULL, &menuitem2_height);
288 query_size (disabledarrowmenuitem_context, NULL, &menuitem2_height);
289 *height += menuitem2_height;
290 menuitem3_height = 0;
291 query_size (menu_context, NULL, &menuitem5_height);
292 query_size (menuitem_context, NULL, &menuitem3_height);
293 query_size (checkmenuitem_context, NULL, &menuitem3_height);
294 query_size (disabledcheckmenuitem_context, NULL, &menuitem3_height);
295 *height += menuitem3_height;
296 menuitem4_height = 0;
297 query_size (menu_context, NULL, &menuitem5_height);
298 query_size (separatormenuitem_context, NULL, &menuitem4_height);
299 *height += menuitem4_height;
300 menuitem5_height = 0;
301 query_size (menu_context, NULL, &menuitem5_height);
302 query_size (menuitem_context, NULL, &menuitem5_height);
303 query_size (radiomenuitem_context, NULL, &menuitem5_height);
304 query_size (disabledradiomenuitem_context, NULL, &menuitem5_height);
305 *height += menuitem5_height;
306
307 draw_style_common (menu_context, cr, x, y, width, *height,
308 &menu_x, &menu_y, &menu_width, &menu_height);
309
310 /* Hovered with right arrow */
311 gtk_style_context_get (hoveredarrowmenuitem_context, gtk_style_context_get_state (hoveredarrowmenuitem_context),
312 "min-width", &arrow_width, "min-height", &arrow_height, NULL);
313 arrow_size = MIN (arrow_width, arrow_height);
314 draw_style_common (hovermenuitem_context, cr, menu_x, menu_y, menu_width, menuitem1_height,
315 &contents_x, &contents_y, &contents_width, &contents_height);
316 gtk_render_arrow (hoveredarrowmenuitem_context, cr, G_PI / 2,
317 contents_x + contents_width - arrow_size,
318 contents_y + (contents_height - arrow_size) / 2, arrow_size);
319
320 /* Left arrow sensitive, and right arrow insensitive */
321 draw_style_common (menuitem_context, cr, menu_x, menu_y + menuitem1_height, menu_width, menuitem2_height,
322 &contents_x, &contents_y, &contents_width, &contents_height);
323 gtk_style_context_get (arrowmenuitem_context, gtk_style_context_get_state (arrowmenuitem_context),
324 "min-width", &arrow_width, "min-height", &arrow_height, NULL);
325 arrow_size = MIN (arrow_width, arrow_height);
326 gtk_render_arrow (arrowmenuitem_context, cr, G_PI / 2,
327 contents_x,
328 contents_y + (contents_height - arrow_size) / 2, arrow_size);
329 gtk_style_context_get (disabledarrowmenuitem_context, gtk_style_context_get_state (disabledarrowmenuitem_context),
330 "min-width", &arrow_width, "min-height", &arrow_height, NULL);
331 arrow_size = MIN (arrow_width, arrow_height);
332 gtk_render_arrow (disabledarrowmenuitem_context, cr, G_PI / 2,
333 contents_x + contents_width - arrow_size,
334 contents_y + (contents_height - arrow_size) / 2, arrow_size);
335
336
337 /* Left check enabled, sensitive, and right check unchecked, insensitive */
338 draw_style_common (menuitem_context, cr, menu_x, menu_y + menuitem1_height + menuitem2_height, menu_width, menuitem3_height,
339 &contents_x, &contents_y, &contents_width, &contents_height);
340 gtk_style_context_get (checkmenuitem_context, gtk_style_context_get_state (checkmenuitem_context),
341 "min-width", &toggle_width, "min-height", &toggle_height, NULL);
342 draw_style_common (checkmenuitem_context, cr,
343 contents_x,
344 contents_y,
345 toggle_width, toggle_height,
346 &toggle_x, &toggle_y, &toggle_width, &toggle_height);
347 gtk_render_check (checkmenuitem_context, cr, toggle_x, toggle_y, toggle_width, toggle_height);
348 gtk_style_context_get (disabledcheckmenuitem_context, gtk_style_context_get_state (disabledcheckmenuitem_context),
349 "min-width", &toggle_width, "min-height", &toggle_height, NULL);
350 draw_style_common (disabledcheckmenuitem_context, cr,
351 contents_x + contents_width - toggle_width,
352 contents_y,
353 toggle_width, toggle_height,
354 &toggle_x, &toggle_y, &toggle_width, &toggle_height);
355 gtk_render_check (disabledcheckmenuitem_context, cr, toggle_x, toggle_y, toggle_width, toggle_height);
356
357 /* Separator */
358 draw_style_common (separatormenuitem_context, cr, menu_x, menu_y + menuitem1_height + menuitem2_height + menuitem3_height,
359 menu_width, menuitem4_height,
360 NULL, NULL, NULL, NULL);
361
362 /* Left check enabled, sensitive, and right check unchecked, insensitive */
363 draw_style_common (menuitem_context, cr, menu_x, menu_y + menuitem1_height + menuitem2_height + menuitem3_height + menuitem4_height,
364 menu_width, menuitem5_height,
365 &contents_x, &contents_y, &contents_width, &contents_height);
366 gtk_style_context_get (radiomenuitem_context, gtk_style_context_get_state (radiomenuitem_context),
367 "min-width", &toggle_width, "min-height", &toggle_height, NULL);
368 draw_style_common (radiomenuitem_context, cr,
369 contents_x,
370 contents_y,
371 toggle_width, toggle_height,
372 &toggle_x, &toggle_y, &toggle_width, &toggle_height);
373 gtk_render_check (radiomenuitem_context, cr, toggle_x, toggle_y, toggle_width, toggle_height);
374 gtk_style_context_get (disabledradiomenuitem_context, gtk_style_context_get_state (disabledradiomenuitem_context),
375 "min-width", &toggle_width, "min-height", &toggle_height, NULL);
376 draw_style_common (disabledradiomenuitem_context, cr,
377 contents_x + contents_width - toggle_width,
378 contents_y,
379 toggle_width, toggle_height,
380 &toggle_x, &toggle_y, &toggle_width, &toggle_height);
381 gtk_render_check (disabledradiomenuitem_context, cr, toggle_x, toggle_y, toggle_width, toggle_height);
382
383 g_object_unref (menu_context);
384 g_object_unref (menuitem_context);
385 g_object_unref (hovermenuitem_context);
386 g_object_unref (hoveredarrowmenuitem_context);
387 g_object_unref (arrowmenuitem_context);
388 g_object_unref (checkmenuitem_context);
389 g_object_unref (disabledarrowmenuitem_context);
390 g_object_unref (disabledcheckmenuitem_context);
391 g_object_unref (radiomenuitem_context);
392 g_object_unref (disablemenuitem_context);
393 g_object_unref (disabledradiomenuitem_context);
394 g_object_unref (separatormenuitem_context);
395 }
396
397 static void
398 draw_menubar (GtkWidget *widget,
399 cairo_t *cr,
400 gint x,
401 gint y,
402 gint width,
403 gint *height)
404 {
405 GtkStyleContext *frame_context;
406 GtkStyleContext *border_context;
407 GtkStyleContext *menubar_context;
408 GtkStyleContext *hovered_menuitem_context;
409 GtkStyleContext *menuitem_context;
410 gint contents_x, contents_y, contents_width, contents_height;
411 gint item_width;
412
413 /* Menubar background is the same color as our base background, so use a frame */
414 frame_context = get_style (NULL, "frame");
415 border_context = get_style (frame_context, "border");
416
417 /* This information is taken from the GtkMenuBar docs, see "CSS nodes" */
418 menubar_context = get_style (NULL, "menubar");
419 hovered_menuitem_context = get_style (menubar_context, "menuitem:hover");
420 menuitem_context = get_style (menubar_context, "menuitem");
421
422 *height = 0;
423 query_size (frame_context, NULL, height);
424 query_size (border_context, NULL, height);
425 query_size (menubar_context, NULL, height);
426 query_size (hovered_menuitem_context, NULL, height);
427 query_size (menuitem_context, NULL, height);
428
429 draw_style_common (frame_context, cr, x, y, width, *height,
430 NULL, NULL, NULL, NULL);
431 draw_style_common (border_context, cr, x, y, width, *height,
432 &contents_x, &contents_y, &contents_width, &contents_height);
433 draw_style_common (menubar_context, cr, contents_x, contents_y, contents_width, contents_height,
434 NULL, NULL, NULL, NULL);
435 item_width = contents_width / 3;
436 draw_style_common (hovered_menuitem_context, cr, contents_x, contents_y, item_width, contents_height,
437 NULL, NULL, NULL, NULL);
438 draw_style_common (menuitem_context, cr, contents_x + item_width * 2, contents_y, item_width, contents_height,
439 NULL, NULL, NULL, NULL);
440
441 g_object_unref (menuitem_context);
442 g_object_unref (hovered_menuitem_context);
443 g_object_unref (menubar_context);
444 g_object_unref (border_context);
445 g_object_unref (frame_context);
446 }
447
448 static void
449 draw_notebook (GtkWidget *widget,
450 cairo_t *cr,
451 gint x,
452 gint y,
453 gint width,
454 gint height)
455 {
456 GtkStyleContext *notebook_context;
457 GtkStyleContext *header_context;
458 GtkStyleContext *tabs_context;
459 GtkStyleContext *tab1_context, *tab2_context;
460 GtkStyleContext *stack_context;
461 gint header_height;
462 gint contents_x, contents_y, contents_width, contents_height;
463
464 /* This information is taken from the GtkNotebook docs, see "CSS nodes" */
465 notebook_context = get_style (NULL, "notebook.frame");
466 header_context = get_style (notebook_context, "header.top");
467 tabs_context = get_style (header_context, "tabs");
468 tab1_context = get_style (tabs_context, "tab:checked");
469 tab2_context = get_style (tabs_context, "tab:hover");
470 stack_context = get_style (notebook_context, "stack");
471
472 header_height = 0;
473 query_size (notebook_context, NULL, &header_height);
474 query_size (header_context, NULL, &header_height);
475 query_size (tabs_context, NULL, &header_height);
476 query_size (tab1_context, NULL, &header_height);
477 query_size (tab2_context, NULL, &header_height);
478
479 draw_style_common (notebook_context, cr, x, y, width, height, NULL, NULL, NULL, NULL);
480 draw_style_common (header_context, cr, x, y, width, header_height, NULL, NULL, NULL, NULL);
481 draw_style_common (tabs_context, cr, x, y, width, header_height, NULL, NULL, NULL, NULL);
482 draw_style_common (tab1_context, cr, x, y, width / 2, header_height,
483 &contents_x, &contents_y, &contents_width, &contents_height);
484 draw_style_common (tab2_context, cr, x + width / 2, y, width / 2, header_height,
485 NULL, NULL, NULL, NULL);
486 draw_style_common (stack_context, cr, x, y + header_height, width,height - header_height,
487 NULL, NULL, NULL, NULL);
488
489 g_object_unref (stack_context);
490 g_object_unref (tabs_context);
491 g_object_unref (tab1_context);
492 g_object_unref (tab2_context);
493 g_object_unref (header_context);
494 g_object_unref (notebook_context);
495 }
496
497 static void
498 draw_horizontal_scrollbar (GtkWidget *widget,
499 cairo_t *cr,
500 gint x,
501 gint y,
502 gint width,
503 gint position,
504 GtkStateFlags state,
505 gint *height)
506 {
507 GtkStyleContext *scrollbar_context;
508 GtkStyleContext *contents_context;
509 GtkStyleContext *trough_context;
510 GtkStyleContext *slider_context;
511 gint slider_width;
512
513 /* This information is taken from the GtkScrollbar docs, see "CSS nodes" */
514 scrollbar_context = get_style (NULL, "scrollbar.horizontal.bottom");
515 contents_context = get_style (scrollbar_context, "contents");
516 trough_context = get_style (contents_context, "trough");
517 slider_context = get_style (trough_context, "slider");
518
519 gtk_style_context_set_state (scrollbar_context, state);
520 gtk_style_context_set_state (contents_context, state);
521 gtk_style_context_set_state (trough_context, state);
522 gtk_style_context_set_state (slider_context, state);
523
524 *height = 0;
525 query_size (scrollbar_context, NULL, height);
526 query_size (contents_context, NULL, height);
527 query_size (trough_context, NULL, height);
528 query_size (slider_context, NULL, height);
529
530 gtk_style_context_get (slider_context, gtk_style_context_get_state (slider_context),
531 "min-width", &slider_width, NULL);
532
533 draw_style_common (scrollbar_context, cr, x, y, width, *height, NULL, NULL, NULL, NULL);
534 draw_style_common (contents_context, cr, x, y, width, *height, NULL, NULL, NULL, NULL);
535 draw_style_common (trough_context, cr, x, y, width, *height, NULL, NULL, NULL, NULL);
536 draw_style_common (slider_context, cr, x + position, y, slider_width, *height, NULL, NULL, NULL, NULL);
537
538 g_object_unref (slider_context);
539 g_object_unref (trough_context);
540 g_object_unref (contents_context);
541 g_object_unref (scrollbar_context);
542 }
543
544 static void
545 draw_text (GtkWidget *widget,
546 cairo_t *cr,
547 gint x,
548 gint y,
549 gint width,
550 gint height,
551 const gchar *text,
552 GtkStateFlags state)
553 {
554 GtkStyleContext *label_context;
555 GtkStyleContext *selection_context;
556 GtkStyleContext *context;
557 PangoLayout *layout;
558
559 /* This information is taken from the GtkLabel docs, see "CSS nodes" */
560 label_context = get_style (NULL, "label.view");
561 selection_context = get_style (label_context, "selection");
562
563 gtk_style_context_set_state (label_context, state);
564
565 if (state & GTK_STATE_FLAG_SELECTED)
566 context = selection_context;
567 else
568 context = label_context;
569
570 layout = gtk_widget_create_pango_layout (widget, text);
571
572 gtk_render_background (context, cr, x, y, width, height);
573 gtk_render_frame (context, cr, x, y, width, height);
574 gtk_render_layout (context, cr, x, y, layout);
575
576 g_object_unref (layout);
577
578 g_object_unref (selection_context);
579 g_object_unref (label_context);
580 }
581
582 static void
583 draw_check (GtkWidget *widget,
584 cairo_t *cr,
585 gint x,
586 gint y,
587 GtkStateFlags state,
588 gint *width,
589 gint *height)
590 {
591 GtkStyleContext *button_context;
592 GtkStyleContext *check_context;
593 gint contents_x, contents_y, contents_width, contents_height;
594
595 /* This information is taken from the GtkCheckButton docs, see "CSS nodes" */
596 button_context = get_style (NULL, "checkbutton");
597 check_context = get_style (button_context, "check");
598
599 gtk_style_context_set_state (check_context, state);
600
601 *width = *height = 0;
602 query_size (button_context, width, height);
603 query_size (check_context, width, height);
604
605 draw_style_common (button_context, cr, x, y, *width, *height, NULL, NULL, NULL, NULL);
606 draw_style_common (check_context, cr, x, y, *width, *height,
607 &contents_x, &contents_y, &contents_width, &contents_height);
608 gtk_render_check (check_context, cr, contents_x, contents_y, contents_width, contents_height);
609
610 g_object_unref (check_context);
611 g_object_unref (button_context);
612
613 }
614
615 static void
616 draw_radio (GtkWidget *widget,
617 cairo_t *cr,
618 gint x,
619 gint y,
620 GtkStateFlags state,
621 gint *width,
622 gint *height)
623 {
624 GtkStyleContext *button_context;
625 GtkStyleContext *check_context;
626 gint contents_x, contents_y, contents_width, contents_height;
627
628 /* This information is taken from the GtkRadioButton docs, see "CSS nodes" */
629 button_context = get_style (NULL, "radiobutton");
630 check_context = get_style (button_context, "radio");
631
632 gtk_style_context_set_state (check_context, state);
633
634 *width = *height = 0;
635 query_size (button_context, width, height);
636 query_size (check_context, width, height);
637
638 draw_style_common (button_context, cr, x, y, *width, *height, NULL, NULL, NULL, NULL);
639 draw_style_common (check_context, cr, x, y, *width, *height,
640 &contents_x, &contents_y, &contents_width, &contents_height);
641 gtk_render_check (check_context, cr, contents_x, contents_y, contents_width, contents_height);
642
643 g_object_unref (check_context);
644 g_object_unref (button_context);
645
646 }
647
648 static void
649 draw_progress (GtkWidget *widget,
650 cairo_t *cr,
651 gint x,
652 gint y,
653 gint width,
654 gint position,
655 gint *height)
656 {
657 GtkStyleContext *bar_context;
658 GtkStyleContext *trough_context;
659 GtkStyleContext *progress_context;
660
661 /* This information is taken from the GtkProgressBar docs, see "CSS nodes" */
662 bar_context = get_style (NULL, "progressbar.horizontal");
663 trough_context = get_style (bar_context, "trough");
664 progress_context = get_style (trough_context, "progress.left");
665
666 *height = 0;
667 query_size (bar_context, NULL, height);
668 query_size (trough_context, NULL, height);
669 query_size (progress_context, NULL, height);
670
671 draw_style_common (bar_context, cr, x, y, width, *height, NULL, NULL, NULL, NULL);
672 draw_style_common (trough_context, cr, x, y, width, *height, NULL, NULL, NULL, NULL);
673 draw_style_common (progress_context, cr, x, y, position, *height, NULL, NULL, NULL, NULL);
674
675 g_object_unref (progress_context);
676 g_object_unref (trough_context);
677 g_object_unref (bar_context);
678 }
679
680 static void
681 draw_scale (GtkWidget *widget,
682 cairo_t *cr,
683 gint x,
684 gint y,
685 gint width,
686 gint position,
687 gint *height)
688 {
689 GtkStyleContext *scale_context;
690 GtkStyleContext *contents_context;
691 GtkStyleContext *trough_context;
692 GtkStyleContext *slider_context;
693 GtkStyleContext *highlight_context;
694 gint contents_x, contents_y, contents_width, contents_height;
695 gint trough_height, slider_height;
696
697 scale_context = get_style (NULL, "scale.horizontal");
698 contents_context = get_style (scale_context, "contents");
699 trough_context = get_style (contents_context, "trough");
700 slider_context = get_style (trough_context, "slider");
701 highlight_context = get_style (trough_context, "highlight.top");
702
703 *height = 0;
704 query_size (scale_context, NULL, height);
705 query_size (contents_context, NULL, height);
706 query_size (trough_context, NULL, height);
707 query_size (slider_context, NULL, height);
708 query_size (highlight_context, NULL, height);
709
710 draw_style_common (scale_context, cr, x, y, width, *height,
711 &contents_x, &contents_y, &contents_width, &contents_height);
712 draw_style_common (contents_context, cr, contents_x, contents_y, contents_width, contents_height,
713 &contents_x, &contents_y, &contents_width, &contents_height);
714 /* Scale trough defines its size querying slider and highlight */
715 trough_height = 0;
716 query_size (trough_context, NULL, &trough_height);
717 slider_height = 0;
718 query_size (slider_context, NULL, &slider_height);
719 query_size (highlight_context, NULL, &slider_height);
720 trough_height += slider_height;
721 draw_style_common (trough_context, cr, contents_x, contents_y, contents_width, trough_height,
722 &contents_x, &contents_y, &contents_width, &contents_height);
723 draw_style_common (highlight_context, cr, contents_x, contents_y,
724 contents_width / 2, contents_height,
725 NULL, NULL, NULL, NULL);
726 draw_style_common (slider_context, cr, contents_x + position, contents_y, contents_height, contents_height,
727 NULL, NULL, NULL, NULL);
728
729 g_object_unref (scale_context);
730 g_object_unref (contents_context);
731 g_object_unref (trough_context);
732 g_object_unref (slider_context);
733 g_object_unref (highlight_context);
734 }
735
736 static void
737 draw_combobox (GtkWidget *widget,
738 cairo_t *cr,
739 gint x,
740 gint y,
741 gint width,
742 gboolean has_entry,
743 gint *height)
744 {
745 GtkStyleContext *combo_context;
746 GtkStyleContext *box_context;
747 GtkStyleContext *button_context;
748 GtkStyleContext *button_box_context;
749 GtkStyleContext *entry_context;
750 GtkStyleContext *arrow_context;
751 gint contents_x, contents_y, contents_width, contents_height;
752 gint button_width;
753 gint arrow_width, arrow_height, arrow_size;
754
755 /* This information is taken from the GtkComboBox docs, see "CSS nodes" */
756 combo_context = get_style (NULL, "combobox:focus");
757 box_context = get_style (combo_context, "box.horizontal.linked");
758 if (has_entry)
759 {
760 const char *siblings[3] = { "entry.combo:focus", "button.combo" , NULL };
761
762 entry_context = get_style_with_siblings (box_context, "entry.combo:focus", siblings, 0);
763 button_context = get_style_with_siblings (box_context, "button.combo", siblings, 1);
764 }
765 else
766 {
767 const char *siblings[2] = { "button.combo" , NULL };
768
769 button_context = get_style_with_siblings (box_context, "button.combo", siblings, 0);
770 }
771 button_box_context = get_style (button_context, "box.horizontal");
772 arrow_context = get_style (button_box_context, "arrow");
773
774 *height = 0;
775 query_size (combo_context, NULL, height);
776 query_size (box_context, NULL, height);
777 if (has_entry)
778 query_size (entry_context, NULL, height);
779 query_size (button_context, NULL, height);
780 query_size (button_box_context, NULL, height);
781 query_size (arrow_context, NULL, height);
782
783 gtk_style_context_get (arrow_context, gtk_style_context_get_state (arrow_context),
784 "min-width", &arrow_width, "min-height", &arrow_height, NULL);
785 arrow_size = MIN (arrow_width, arrow_height);
786
787 draw_style_common (combo_context, cr, x, y, width, *height, NULL, NULL, NULL, NULL);
788 draw_style_common (box_context, cr, x, y, width, *height, NULL, NULL, NULL, NULL);
789 if (has_entry)
790 {
791 button_width = *height;
792 draw_style_common (entry_context, cr, x, y, width - button_width, *height, NULL, NULL, NULL, NULL);
793 draw_style_common (button_context, cr, x + width - button_width, y, button_width, *height,
794 &contents_x, &contents_y, &contents_width, &contents_height);
795 }
796 else
797 {
798 button_width = width;
799 draw_style_common (button_context, cr, x, y, width, *height,
800 &contents_x, &contents_y, &contents_width, &contents_height);
801 }
802
803 draw_style_common (button_box_context, cr, contents_x, contents_y, contents_width, contents_height,
804 NULL, NULL, NULL, NULL);
805 draw_style_common (arrow_context, cr, contents_x, contents_y, contents_width, contents_height,
806 NULL, NULL, NULL, NULL);
807 gtk_render_arrow (arrow_context, cr, G_PI / 2,
808 contents_x + contents_width - arrow_size,
809 contents_y + (contents_height - arrow_size) / 2, arrow_size);
810
811 g_object_unref (arrow_context);
812 if (has_entry)
813 g_object_unref (entry_context);
814 g_object_unref (button_context);
815 g_object_unref (combo_context);
816 }
817
818 static void
819 draw_spinbutton (GtkWidget *widget,
820 cairo_t *cr,
821 gint x,
822 gint y,
823 gint width,
824 gint *height)
825 {
826 GtkStyleContext *spin_context;
827 GtkStyleContext *entry_context;
828 GtkStyleContext *up_context;
829 GtkStyleContext *down_context;
830 GtkIconTheme *icon_theme;
831 GtkIconInfo *icon_info;
832 GdkPixbuf *pixbuf;
833 gint icon_width, icon_height, icon_size;
834 gint button_width;
835 gint contents_x, contents_y, contents_width, contents_height;
836
837 /* This information is taken from the GtkSpinButton docs, see "CSS nodes" */
838 spin_context = get_style (NULL, "spinbutton.horizontal:focus");
839 entry_context = get_style (spin_context, "entry:focus");
840 up_context = get_style (spin_context, "button.up:focus:active");
841 down_context = get_style (spin_context, "button.down:focus");
842
843 *height = 0;
844 query_size (spin_context, NULL, height);
845 query_size (entry_context, NULL, height);
846 query_size (up_context, NULL, height);
847 query_size (down_context, NULL, height);
848 button_width = *height;
849
850 draw_style_common (spin_context, cr, x, y, width, *height, NULL, NULL, NULL, NULL);
851 draw_style_common (entry_context, cr, x, y, width, *height, NULL, NULL, NULL, NULL);
852
853 icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (widget));
854
855 gtk_style_context_get (up_context, gtk_style_context_get_state (up_context),
856 "min-width", &icon_width, "min-height", &icon_height, NULL);
857 icon_size = MIN (icon_width, icon_height);
858 icon_info = gtk_icon_theme_lookup_icon (icon_theme, "list-add-symbolic", icon_size, 0);
859 pixbuf = gtk_icon_info_load_symbolic_for_context (icon_info, up_context, NULL, NULL);
860 g_object_unref (icon_info);
861 draw_style_common (up_context, cr, x + width - button_width, y, button_width, *height,
862 &contents_x, &contents_y, &contents_width, &contents_height);
863 gtk_render_icon (up_context, cr, pixbuf, contents_x, contents_y + (contents_height - icon_size) / 2);
864 g_object_unref (pixbuf);
865
866
867 gtk_style_context_get (down_context, gtk_style_context_get_state (down_context),
868 "min-width", &icon_width, "min-height", &icon_height, NULL);
869 icon_size = MIN (icon_width, icon_height);
870 icon_info = gtk_icon_theme_lookup_icon (icon_theme, "list-remove-symbolic", icon_size, 0);
871 pixbuf = gtk_icon_info_load_symbolic_for_context (icon_info, down_context, NULL, NULL);
872 g_object_unref (icon_info);
873 draw_style_common (down_context, cr, x + width - 2 * button_width, y, button_width, *height,
874 &contents_x, &contents_y, &contents_width, &contents_height);
875 gtk_render_icon (down_context, cr, pixbuf, contents_x, contents_y + (contents_height - icon_size) / 2);
876 g_object_unref (pixbuf);
877
878 g_object_unref (down_context);
879 g_object_unref (up_context);
880 g_object_unref (entry_context);
881 g_object_unref (spin_context);
882 }
883
884 static gboolean
885 draw_cb (GtkWidget *widget,
886 cairo_t *cr)
887 {
888 gint panewidth, width, height;
889 gint x, y;
890
891 width = gtk_widget_get_allocated_width (widget);
892 panewidth = width / 2;
893 height = gtk_widget_get_allocated_height (widget);
894
895 cairo_rectangle (cr, 0, 0, width, height);
896 cairo_set_source_rgb (cr, 0.9, 0.9, 0.9);
897 cairo_fill (cr);
898
899 x = y = 10;
900 draw_horizontal_scrollbar (widget, cr, x, y, panewidth - 20, 30, GTK_STATE_FLAG_NORMAL, &height);
901 y += height + 8;
902 draw_horizontal_scrollbar (widget, cr, x, y, panewidth - 20, 40, GTK_STATE_FLAG_PRELIGHT, &height);
903 y += height + 8;
904 draw_horizontal_scrollbar (widget, cr, x, y, panewidth - 20, 50, GTK_STATE_FLAG_ACTIVE|GTK_STATE_FLAG_PRELIGHT, &height);
905
906 y += height + 8;
907 draw_text (widget, cr, x, y, panewidth - 20, 20, "Not selected", GTK_STATE_FLAG_NORMAL);
908 y += 20 + 10;
909 draw_text (widget, cr, x, y, panewidth - 20, 20, "Selected", GTK_STATE_FLAG_SELECTED);
910
911 x = 10;
912 y += 20 + 10;
913 draw_check (widget, cr, x, y, GTK_STATE_FLAG_NORMAL, &width, &height);
914 x += width + 10;
915 draw_check (widget, cr, x, y, GTK_STATE_FLAG_CHECKED, &width, &height);
916 x += width + 10;
917 draw_radio (widget, cr, x, y, GTK_STATE_FLAG_NORMAL, &width, &height);
918 x += width + 10;
919 draw_radio (widget, cr, x, y, GTK_STATE_FLAG_CHECKED, &width, &height);
920 x = 10;
921
922 y += height + 10;
923 draw_progress (widget, cr, x, y, panewidth - 20, 50, &height);
924
925 y += height + 10;
926 draw_scale (widget, cr, x, y, panewidth - 20, 75, &height);
927
928 y += height + 20;
929 draw_notebook (widget, cr, x, y, panewidth - 20, 160);
930
931 /* Second column */
932 x += panewidth;
933 y = 10;
934 draw_menu (widget, cr, x, y, panewidth - 20, &height);
935
936 y += height + 10;
937 draw_menubar (widget, cr, x, y, panewidth - 20, &height);
938
939 y += height + 20;
940 draw_spinbutton (widget, cr, x, y, panewidth - 20, &height);
941
942 y += height + 30;
943 draw_combobox (widget, cr, x, y, panewidth - 20, FALSE, &height);
944
945 y += height + 10;
946 draw_combobox (widget, cr, 10 + panewidth, y, panewidth - 20, TRUE, &height);
947
948 return FALSE;
949 }
950
951 GtkWidget *
952 do_foreigndrawing (GtkWidget *do_widget)
953 {
954 static GtkWidget *window = NULL;
955
956 if (!window)
957 {
958 GtkWidget *box;
959 GtkWidget *da;
960
961 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
962 gtk_window_set_title (GTK_WINDOW (window), "Foreign drawing");
963 gtk_window_set_screen (GTK_WINDOW (window),
964 gtk_widget_get_screen (do_widget));
965 g_signal_connect (window, "destroy",
966 G_CALLBACK (gtk_widget_destroyed), &window);
967
968 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
969 gtk_container_add (GTK_CONTAINER (window), box);
970 da = gtk_drawing_area_new ();
971 gtk_widget_set_size_request (da, 400, 400);
972 gtk_widget_set_hexpand (da, TRUE);
973 gtk_widget_set_vexpand (da, TRUE);
974 gtk_widget_set_app_paintable (da, TRUE);
975 gtk_container_add (GTK_CONTAINER (box), da);
976
977 g_signal_connect (da, "draw", G_CALLBACK (draw_cb), NULL);
978 }
979
980 if (!gtk_widget_get_visible (window))
981 gtk_widget_show_all (window);
982 else
983 gtk_widget_destroy (window);
984
985 return window;
986 }