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_error_quark (void)
340 return g_quark_from_static_string ("clutter-gtk-texture-error-quark");
344 * gtk_clutter_texture_set_from_pixbuf:
345 * @texture: a #ClutterTexture
346 * @pixbuf: a #GdkPixbuf
347 * @error: a return location for errors
349 * Sets the contents of @texture with a copy of @pixbuf.
351 * Return value: %TRUE on success, %FALSE on failure.
356 gtk_clutter_texture_set_from_pixbuf (ClutterTexture *texture,
360 g_return_val_if_fail (CLUTTER_IS_TEXTURE (texture), FALSE);
361 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), FALSE);
363 return clutter_texture_set_from_rgb_data (texture,
364 gdk_pixbuf_get_pixels (pixbuf),
365 gdk_pixbuf_get_has_alpha (pixbuf),
366 gdk_pixbuf_get_width (pixbuf),
367 gdk_pixbuf_get_height (pixbuf),
368 gdk_pixbuf_get_rowstride (pixbuf),
369 gdk_pixbuf_get_has_alpha (pixbuf) ? 4 : 3,
375 * gtk_clutter_texture_new_from_stock:
376 * @widget: a #GtkWidget
377 * @stock_id: the stock id of the icon
378 * @size: the size of the icon, or -1
380 * Creates a new #ClutterTexture and sets its contents using the stock
381 * icon @stock_id as rendered by @widget.
383 * Return value: the newly created #ClutterTexture
388 gtk_clutter_texture_new_from_stock (GtkWidget *widget,
389 const gchar *stock_id,
393 ClutterActor *retval;
395 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
396 g_return_val_if_fail (stock_id != NULL, NULL);
397 g_return_val_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1, NULL);
399 pixbuf = gtk_widget_render_icon (widget, stock_id, size, NULL);
401 pixbuf = gtk_widget_render_icon (widget,
402 GTK_STOCK_MISSING_IMAGE, size,
405 retval = gtk_clutter_texture_new_from_pixbuf (pixbuf);
406 g_object_unref (pixbuf);
412 * gtk_clutter_texture_set_from_stock:
413 * @texture: a #ClutterTexture
414 * @widget: a #GtkWidget
415 * @stock_id: the stock id of the icon
416 * @size: the size of the icon, or -1
417 * @error: a return location for errors
419 * Sets the contents of @texture using the stock icon @stock_id, as
420 * rendered by @widget.
422 * Return value: %TRUE on success, %FALSE on failure.
427 gtk_clutter_texture_set_from_stock (ClutterTexture *texture,
429 const gchar *stock_id,
436 g_return_val_if_fail (CLUTTER_IS_TEXTURE (texture), FALSE);
437 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
438 g_return_val_if_fail (stock_id != NULL, FALSE);
439 g_return_val_if_fail ((size > GTK_ICON_SIZE_INVALID) || (size == -1), FALSE);
441 pixbuf = gtk_widget_render_icon (widget, stock_id, size, NULL);
445 CLUTTER_GTK_TEXTURE_ERROR,
446 CLUTTER_GTK_TEXTURE_INVALID_STOCK_ID,
447 "Stock ID '%s' not found", stock_id);
451 returnval = gtk_clutter_texture_set_from_pixbuf (texture, pixbuf, error);
452 g_object_unref (pixbuf);
458 * gtk_clutter_texture_new_from_icon_name:
459 * @widget: a #GtkWidget or %NULL
460 * @icon_name: the name of the icon
461 * @size: the size of the icon, or -1
463 * Creates a new #ClutterTexture and sets its contents to be
464 * the @icon_name from the current icon theme.
466 * Return value: the newly created texture, or %NULL if @widget
467 * was %NULL and @icon_name was not found.
472 gtk_clutter_texture_new_from_icon_name (GtkWidget *widget,
473 const gchar *icon_name,
476 GtkSettings *settings;
477 GtkIconTheme *icon_theme;
481 ClutterActor *retval;
483 g_return_val_if_fail (widget == NULL || GTK_IS_WIDGET (widget), NULL);
484 g_return_val_if_fail (icon_name != NULL, NULL);
485 g_return_val_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1, NULL);
487 if (widget && gtk_widget_has_screen (widget))
491 screen = gtk_widget_get_screen (widget);
492 settings = gtk_settings_get_for_screen (screen);
493 icon_theme = gtk_icon_theme_get_for_screen (screen);
497 settings = gtk_settings_get_default ();
498 icon_theme = gtk_icon_theme_get_default ();
502 !gtk_icon_size_lookup_for_settings (settings, size, &width, &height))
508 pixbuf = gtk_icon_theme_load_icon (icon_theme,
510 MIN (width, height), 0,
514 g_warning ("Unable to load the icon `%s' from the theme: %s",
518 g_error_free (error);
521 return gtk_clutter_texture_new_from_stock (widget,
522 GTK_STOCK_MISSING_IMAGE,
528 retval = gtk_clutter_texture_new_from_pixbuf (pixbuf);
529 g_object_unref (pixbuf);
535 * gtk_clutter_texture_set_from_icon_name:
536 * @texture: a #ClutterTexture
537 * @widget: a #GtkWidget or %NULL
538 * @icon_name: the name of the icon
539 * @size: the icon size or -1
540 * @error: a return location for errors
542 * Sets the contents of @texture using the @icon_name from the
543 * current icon theme.
545 * Return value: %TRUE on success, %FALSE on failure.
550 gtk_clutter_texture_set_from_icon_name (ClutterTexture *texture,
552 const gchar *icon_name,
556 GError *local_error = NULL;
557 GtkSettings *settings;
558 GtkIconTheme *icon_theme;
563 g_return_val_if_fail (CLUTTER_IS_TEXTURE (texture), FALSE);
564 g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
565 g_return_val_if_fail (icon_name != NULL, FALSE);
566 g_return_val_if_fail ((size > GTK_ICON_SIZE_INVALID) || (size == -1), FALSE);
568 if (widget && gtk_widget_has_screen (widget))
572 screen = gtk_widget_get_screen (widget);
573 settings = gtk_settings_get_for_screen (screen);
574 icon_theme = gtk_icon_theme_get_for_screen (screen);
578 settings = gtk_settings_get_default ();
579 icon_theme = gtk_icon_theme_get_default ();
583 !gtk_icon_size_lookup_for_settings (settings, size, &width, &height))
588 pixbuf = gtk_icon_theme_load_icon (icon_theme,
590 MIN (width, height), 0,
594 g_propagate_error (error, local_error);
598 returnval = gtk_clutter_texture_set_from_pixbuf (texture, pixbuf, error);
599 g_object_unref (pixbuf);
606 * @argc: pointer to the arguments count, or %NULL
607 * @argv: pointer to the arguments vector, or %NULL
609 * This function should be called instead of clutter_init() and
612 * Return value: %CLUTTER_INIT_SUCCESS on success, a negative integer
618 gtk_clutter_init (int *argc,
621 if (!gtk_init_check (argc, argv))
622 return CLUTTER_INIT_ERROR_GTK;
624 #if defined(HAVE_CLUTTER_GTK_X11)
625 clutter_x11_set_display (GDK_DISPLAY());
626 clutter_x11_disable_event_retrieval ();
627 #elif defined(HAVE_CLUTTER_GTK_WIN32)
628 clutter_win32_disable_event_retrieval ();
629 #endif /* HAVE_CLUTTER_GTK_{X11,WIN32} */
631 return clutter_init (argc, argv);
635 * gtk_clutter_init_with_args:
636 * @argc: a pointer to the number of command line arguments.
637 * @argv: a pointer to the array of command line arguments.
638 * @parameter_string: a string which is displayed in
639 * the first line of <option>--help</option> output, after
640 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
641 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
642 * describing the options of your program
643 * @translation_domain: a translation domain to use for translating
644 * the <option>--help</option> output for the options in @entries
645 * with gettext(), or %NULL
646 * @error: a return location for errors
648 * This function should be called instead of clutter_init() and
649 * gtk_init_with_args().
651 * Return value: %CLUTTER_INIT_SUCCESS on success, a negative integer
657 gtk_clutter_init_with_args (int *argc,
659 const char *parameter_string,
660 GOptionEntry *entries,
661 const char *translation_domain,
666 res = gtk_init_with_args (argc, argv,
667 (char*) parameter_string,
669 (char*) translation_domain,
673 return CLUTTER_INIT_ERROR_GTK;
675 #if defined(GDK_WINDOWING_X11)
676 clutter_x11_set_display (GDK_DISPLAY());
677 clutter_x11_disable_event_retrieval ();
678 #elif defined(GDK_WINDOWING_WIN32)
679 clutter_win32_disable_event_retrieval ();
680 #endif /* GDK_WINDOWING_{X11,WIN32} */
682 return clutter_init_with_args (argc, argv,