5 #include "gtk-clutter-util.h"
7 #include <gdk-pixbuf/gdk-pixbuf.h>
10 #include <clutter/clutter.h>
12 #if defined(HAVE_CLUTTER_GTK_X11)
14 #include <clutter/x11/clutter-x11.h>
17 #elif defined(HAVE_CLUTTER_GTK_WIN32)
19 #include <clutter/clutter-win32.h>
20 #include <gdk/gdkwin32.h>
22 #endif /* HAVE_CLUTTER_GTK_{X11,WIN32} */
25 * SECTION:gtk-clutter-util
26 * @short_description: Utility functions for integrating Clutter in GTK+
28 * In order to properly integrate a Clutter scene into a GTK+ applications
29 * a certain degree of state must be retrieved from GTK+ itself.
31 * Clutter-GTK provides API for easing the process of synchronizing colors
32 * with the current GTK+ theme and for loading image sources from #GdkPixbuf,
33 * GTK+ stock items and icon themes.
38 /* base symbols from GtkRcStyle */
39 FG_COMPONENT = GTK_RC_FG,
40 BG_COMPONENT = GTK_RC_BG,
41 TEXT_COMPONENT = GTK_RC_TEXT,
42 BASE_COMPONENT = GTK_RC_BASE,
44 /* symbols used by GtkStyle */
52 gtk_clutter_get_component (GtkWidget *widget,
57 GtkStyle *style = gtk_widget_get_style (widget);
58 GdkColor gtk_color = { 0, };
63 gtk_color = style->fg[state];
67 gtk_color = style->bg[state];
71 gtk_color = style->text[state];
75 gtk_color = style->base[state];
79 gtk_color = style->light[state];
83 gtk_color = style->mid[state];
87 gtk_color = style->dark[state];
90 case TEXT_AA_COMPONENT:
91 gtk_color = style->text_aa[state];
95 g_assert_not_reached ();
99 color->red = CLAMP (((gtk_color.red / 65535.0) * 255), 0, 255);
100 color->green = CLAMP (((gtk_color.green / 65535.0) * 255), 0, 255);
101 color->blue = CLAMP (((gtk_color.blue / 65535.0) * 255), 0, 255);
106 * gtk_clutter_get_fg_color:
107 * @widget: a #GtkWidget
109 * @color: return location for a #ClutterColor
111 * Retrieves the foreground color of @widget for the given @state and copies
117 gtk_clutter_get_fg_color (GtkWidget *widget,
121 g_return_if_fail (GTK_IS_WIDGET (widget));
122 g_return_if_fail (state >= GTK_STATE_NORMAL &&
123 state <= GTK_STATE_INSENSITIVE);
124 g_return_if_fail (color != NULL);
126 gtk_clutter_get_component (widget, FG_COMPONENT, state, color);
130 * gtk_clutter_get_bg_color:
131 * @widget: a #GtkWidget
133 * @color: return location for a #ClutterColor
135 * Retrieves the background color of @widget for the given @state and copies
141 gtk_clutter_get_bg_color (GtkWidget *widget,
145 g_return_if_fail (GTK_IS_WIDGET (widget));
146 g_return_if_fail (state >= GTK_STATE_NORMAL &&
147 state <= GTK_STATE_INSENSITIVE);
148 g_return_if_fail (color != NULL);
150 gtk_clutter_get_component (widget, BG_COMPONENT, state, color);
154 * gtk_clutter_get_text_color:
155 * @widget: a #GtkWidget
157 * @color: return location for a #ClutterColor
159 * Retrieves the text color of @widget for the given @state and copies it
165 gtk_clutter_get_text_color (GtkWidget *widget,
169 g_return_if_fail (GTK_IS_WIDGET (widget));
170 g_return_if_fail (state >= GTK_STATE_NORMAL &&
171 state <= GTK_STATE_INSENSITIVE);
172 g_return_if_fail (color != NULL);
174 gtk_clutter_get_component (widget, TEXT_COMPONENT, state, color);
178 * gtk_clutter_get_base_color:
179 * @widget: a #GtkWidget
181 * @color: return location for a #ClutterColor
183 * Retrieves the base color of @widget for the given @state and copies it
189 gtk_clutter_get_base_color (GtkWidget *widget,
193 g_return_if_fail (GTK_IS_WIDGET (widget));
194 g_return_if_fail (state >= GTK_STATE_NORMAL &&
195 state <= GTK_STATE_INSENSITIVE);
196 g_return_if_fail (color != NULL);
198 gtk_clutter_get_component (widget, BASE_COMPONENT, state, color);
202 * gtk_clutter_get_light_color:
203 * @widget: a #GtkWidget
205 * @color: return location for a #ClutterColor
207 * Retrieves the light color of @widget for the given @state and copies it
213 gtk_clutter_get_light_color (GtkWidget *widget,
217 g_return_if_fail (GTK_IS_WIDGET (widget));
218 g_return_if_fail (state >= GTK_STATE_NORMAL &&
219 state <= GTK_STATE_INSENSITIVE);
220 g_return_if_fail (color != NULL);
222 gtk_clutter_get_component (widget, LIGHT_COMPONENT, state, color);
226 * gtk_clutter_get_mid_color:
227 * @widget: a #GtkWidget
229 * @color: return location for a #ClutterColor
231 * Retrieves the mid color of @widget for the given @state and copies it
237 gtk_clutter_get_mid_color (GtkWidget *widget,
241 g_return_if_fail (GTK_IS_WIDGET (widget));
242 g_return_if_fail (state >= GTK_STATE_NORMAL &&
243 state <= GTK_STATE_INSENSITIVE);
244 g_return_if_fail (color != NULL);
246 gtk_clutter_get_component (widget, MID_COMPONENT, state, color);
250 * gtk_clutter_get_dark_color:
251 * @widget: a #GtkWidget
253 * @color: return location for a #ClutterColor
255 * Retrieves the dark color of @widget for the given @state and copies it
261 gtk_clutter_get_dark_color (GtkWidget *widget,
265 g_return_if_fail (GTK_IS_WIDGET (widget));
266 g_return_if_fail (state >= GTK_STATE_NORMAL &&
267 state <= GTK_STATE_INSENSITIVE);
268 g_return_if_fail (color != NULL);
270 gtk_clutter_get_component (widget, DARK_COMPONENT, state, color);
274 * gtk_clutter_get_text_aa_color:
275 * @widget: a #GtkWidget
277 * @color: return location for a #ClutterColor
279 * Retrieves the text-aa color of @widget for the given @state and copies it
285 gtk_clutter_get_text_aa_color (GtkWidget *widget,
289 g_return_if_fail (GTK_IS_WIDGET (widget));
290 g_return_if_fail (state >= GTK_STATE_NORMAL &&
291 state <= GTK_STATE_INSENSITIVE);
292 g_return_if_fail (color != NULL);
294 gtk_clutter_get_component (widget, TEXT_AA_COMPONENT, state, color);
298 * gtk_clutter_texture_new_from_pixbuf:
299 * @pixbuf: a #GdkPixbuf
301 * Creates a new #ClutterTexture and sets its contents with a copy
304 * Return value: the newly created #ClutterTexture
309 gtk_clutter_texture_new_from_pixbuf (GdkPixbuf *pixbuf)
311 ClutterActor *retval;
314 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
316 retval = clutter_texture_new ();
319 clutter_texture_set_from_rgb_data (CLUTTER_TEXTURE (retval),
320 gdk_pixbuf_get_pixels (pixbuf),
321 gdk_pixbuf_get_has_alpha (pixbuf),
322 gdk_pixbuf_get_width (pixbuf),
323 gdk_pixbuf_get_height (pixbuf),
324 gdk_pixbuf_get_rowstride (pixbuf),
325 gdk_pixbuf_get_has_alpha (pixbuf) ? 4 : 3,
330 g_warning ("Unable to set the pixbuf: %s", error->message);
331 g_error_free (error);
338 * gtk_clutter_texture_set_from_pixbuf:
339 * @texture: a #ClutterTexture
340 * @pixbuf: a #GdkPixbuf
342 * Sets the contents of @texture with a copy of @pixbuf.
347 gtk_clutter_texture_set_from_pixbuf (ClutterTexture *texture,
352 g_return_if_fail (CLUTTER_IS_TEXTURE (texture));
353 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
356 clutter_texture_set_from_rgb_data (texture,
357 gdk_pixbuf_get_pixels (pixbuf),
358 gdk_pixbuf_get_has_alpha (pixbuf),
359 gdk_pixbuf_get_width (pixbuf),
360 gdk_pixbuf_get_height (pixbuf),
361 gdk_pixbuf_get_rowstride (pixbuf),
362 gdk_pixbuf_get_has_alpha (pixbuf) ? 4 : 3,
367 g_warning ("Unable to set the pixbuf: %s", error->message);
368 g_error_free (error);
373 * gtk_clutter_texture_new_from_stock:
374 * @widget: a #GtkWidget
375 * @stock_id: the stock id of the icon
376 * @size: the size of the icon, or -1
378 * Creates a new #ClutterTexture and sets its contents using the stock
379 * icon @stock_id as rendered by @widget.
381 * Return value: the newly created #ClutterTexture
386 gtk_clutter_texture_new_from_stock (GtkWidget *widget,
387 const gchar *stock_id,
391 ClutterActor *retval;
393 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
394 g_return_val_if_fail (stock_id != NULL, NULL);
395 g_return_val_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1, NULL);
397 pixbuf = gtk_widget_render_icon (widget, stock_id, size, NULL);
399 pixbuf = gtk_widget_render_icon (widget,
400 GTK_STOCK_MISSING_IMAGE, size,
403 retval = gtk_clutter_texture_new_from_pixbuf (pixbuf);
404 g_object_unref (pixbuf);
410 * gtk_clutter_texture_set_from_stock:
411 * @texture: a #ClutterTexture
412 * @widget: a #GtkWidget
413 * @stock_id: the stock id of the icon
414 * @size: the size of the icon, or -1
416 * Sets the contents of @texture using the stock icon @stock_id, as
417 * rendered by @widget.
422 gtk_clutter_texture_set_from_stock (ClutterTexture *texture,
424 const gchar *stock_id,
429 g_return_if_fail (CLUTTER_IS_TEXTURE (texture));
430 g_return_if_fail (GTK_IS_WIDGET (widget));
431 g_return_if_fail (stock_id != NULL);
432 g_return_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1);
434 pixbuf = gtk_widget_render_icon (widget, stock_id, size, NULL);
436 pixbuf = gtk_widget_render_icon (widget,
437 GTK_STOCK_MISSING_IMAGE, size,
440 gtk_clutter_texture_set_from_pixbuf (texture, pixbuf);
441 g_object_unref (pixbuf);
445 * gtk_clutter_texture_new_from_icon_name:
446 * @widget: a #GtkWidget or %NULL
447 * @icon_name: the name of the icon
448 * @size: the size of the icon, or -1
450 * Creates a new #ClutterTexture and sets its contents to be
451 * the @icon_name from the current icon theme.
453 * Return value: the newly created texture, or %NULL if @widget
454 * was %NULL and @icon_name was not found.
459 gtk_clutter_texture_new_from_icon_name (GtkWidget *widget,
460 const gchar *icon_name,
463 GtkSettings *settings;
464 GtkIconTheme *icon_theme;
468 ClutterActor *retval;
470 g_return_val_if_fail (widget == NULL || GTK_IS_WIDGET (widget), NULL);
471 g_return_val_if_fail (icon_name != NULL, NULL);
472 g_return_val_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1, NULL);
474 if (widget && gtk_widget_has_screen (widget))
478 screen = gtk_widget_get_screen (widget);
479 settings = gtk_settings_get_for_screen (screen);
480 icon_theme = gtk_icon_theme_get_for_screen (screen);
484 settings = gtk_settings_get_default ();
485 icon_theme = gtk_icon_theme_get_default ();
489 !gtk_icon_size_lookup_for_settings (settings, size, &width, &height))
495 pixbuf = gtk_icon_theme_load_icon (icon_theme,
497 MIN (width, height), 0,
501 g_warning ("Unable to load the icon `%s' from the theme: %s",
505 g_error_free (error);
508 return gtk_clutter_texture_new_from_stock (widget,
509 GTK_STOCK_MISSING_IMAGE,
515 retval = gtk_clutter_texture_new_from_pixbuf (pixbuf);
516 g_object_unref (pixbuf);
522 * gtk_clutter_texture_set_from_icon_name:
523 * @texture: a #ClutterTexture
524 * @widget: a #GtkWidget or %NULL
525 * @icon_name: the name of the icon
526 * @size: the icon size or -1
528 * Sets the contents of @texture using the @icon_name from the
529 * current icon theme.
534 gtk_clutter_texture_set_from_icon_name (ClutterTexture *texture,
536 const gchar *icon_name,
539 GtkSettings *settings;
540 GtkIconTheme *icon_theme;
545 g_return_if_fail (CLUTTER_IS_TEXTURE (texture));
546 g_return_if_fail (widget == NULL || GTK_IS_WIDGET (widget));
547 g_return_if_fail (icon_name != NULL);
548 g_return_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1);
550 if (widget && gtk_widget_has_screen (widget))
554 screen = gtk_widget_get_screen (widget);
555 settings = gtk_settings_get_for_screen (screen);
556 icon_theme = gtk_icon_theme_get_for_screen (screen);
560 settings = gtk_settings_get_default ();
561 icon_theme = gtk_icon_theme_get_default ();
565 !gtk_icon_size_lookup_for_settings (settings, size, &width, &height))
571 pixbuf = gtk_icon_theme_load_icon (icon_theme,
573 MIN (width, height), 0,
577 g_warning ("Unable to load the icon `%s' from the theme: %s",
581 g_error_free (error);
584 gtk_clutter_texture_set_from_stock (texture,
586 GTK_STOCK_MISSING_IMAGE,
592 gtk_clutter_texture_set_from_pixbuf (texture, pixbuf);
593 g_object_unref (pixbuf);
598 * @argc: pointer to the arguments count, or %NULL
599 * @argv: pointer to the arguments vector, or %NULL
601 * This function should be called instead of clutter_init() and
604 * Return value: %CLUTTER_INIT_SUCCESS on success, a negative integer
610 gtk_clutter_init (int *argc,
613 if (!gtk_init_check (argc, argv))
614 return CLUTTER_INIT_ERROR_GTK;
616 #if defined(HAVE_CLUTTER_GTK_X11)
617 clutter_x11_set_display (GDK_DISPLAY());
618 clutter_x11_disable_event_retrieval ();
619 #elif defined(HAVE_CLUTTER_GTK_WIN32)
620 clutter_win32_disable_event_retrieval ();
621 #endif /* HAVE_CLUTTER_GTK_{X11,WIN32} */
623 return clutter_init (argc, argv);
627 * gtk_clutter_init_with_args:
628 * @argc: a pointer to the number of command line arguments.
629 * @argv: a pointer to the array of command line arguments.
630 * @parameter_string: a string which is displayed in
631 * the first line of <option>--help</option> output, after
632 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
633 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
634 * describing the options of your program
635 * @translation_domain: a translation domain to use for translating
636 * the <option>--help</option> output for the options in @entries
637 * with gettext(), or %NULL
638 * @error: a return location for errors
640 * This function should be called instead of clutter_init() and
641 * gtk_init_with_args().
643 * Return value: %CLUTTER_INIT_SUCCESS on success, a negative integer
649 gtk_clutter_init_with_args (int *argc,
651 const char *parameter_string,
652 GOptionEntry *entries,
653 const char *translation_domain,
658 res = gtk_init_with_args (argc, argv,
659 (char*) parameter_string,
661 (char*) translation_domain,
665 return CLUTTER_INIT_ERROR_GTK;
667 #if defined(GDK_WINDOWING_X11)
668 clutter_x11_set_display (GDK_DISPLAY());
669 clutter_x11_disable_event_retrieval ();
670 #elif defined(GDK_WINDOWING_WIN32)
671 clutter_win32_disable_event_retrieval ();
672 #endif /* GDK_WINDOWING_{X11,WIN32} */
674 return clutter_init_with_args (argc, argv,