5 #include <gdk-pixbuf/gdk-pixbuf.h>
7 #include <clutter/clutter.h>
9 #include "gtk-clutter-util.h"
12 * SECTION:gtk-clutter-util
13 * @short_description: Utility functions for integrating Clutter in GTK+
15 * In order to properly integrate a Clutter scene into a GTK+ applications
16 * a certain degree of state must be retrieved from GTK+ itself.
18 * Clutter-GTK provides API for easing the process of synchronizing colors
19 * with the current GTK+ theme and for loading image sources from #GdkPixbuf,
20 * GTK+ stock items and icon themes.
25 /* base symbols from GtkRcStyle */
26 FG_COMPONENT = GTK_RC_FG,
27 BG_COMPONENT = GTK_RC_BG,
28 TEXT_COMPONENT = GTK_RC_TEXT,
29 BASE_COMPONENT = GTK_RC_BASE,
31 /* symbols used by GtkStyle */
39 gtk_clutter_get_component (GtkWidget *widget,
44 GtkStyle *style = gtk_widget_get_style (widget);
45 GdkColor gtk_color = { 0, };
50 gtk_color = style->fg[state];
54 gtk_color = style->bg[state];
58 gtk_color = style->text[state];
62 gtk_color = style->base[state];
66 gtk_color = style->light[state];
70 gtk_color = style->mid[state];
74 gtk_color = style->dark[state];
77 case TEXT_AA_COMPONENT:
78 gtk_color = style->text_aa[state];
82 g_assert_not_reached ();
86 color->red = CLAMP (((gtk_color.red / 65535.0) * 255), 0, 255);
87 color->green = CLAMP (((gtk_color.green / 65535.0) * 255), 0, 255);
88 color->blue = CLAMP (((gtk_color.blue / 65535.0) * 255), 0, 255);
93 * gtk_clutter_get_fg_color:
94 * @widget: a #GtkWidget
96 * @color: return location for a #ClutterColor
98 * Retrieves the foreground color of @widget for the given @state and copies
104 gtk_clutter_get_fg_color (GtkWidget *widget,
108 g_return_if_fail (GTK_IS_WIDGET (widget));
109 g_return_if_fail (state >= GTK_STATE_NORMAL &&
110 state <= GTK_STATE_INSENSITIVE);
111 g_return_if_fail (color != NULL);
113 gtk_clutter_get_component (widget, FG_COMPONENT, state, color);
117 * gtk_clutter_get_bg_color:
118 * @widget: a #GtkWidget
120 * @color: return location for a #ClutterColor
122 * Retrieves the background color of @widget for the given @state and copies
128 gtk_clutter_get_bg_color (GtkWidget *widget,
132 g_return_if_fail (GTK_IS_WIDGET (widget));
133 g_return_if_fail (state >= GTK_STATE_NORMAL &&
134 state <= GTK_STATE_INSENSITIVE);
135 g_return_if_fail (color != NULL);
137 gtk_clutter_get_component (widget, BG_COMPONENT, state, color);
141 * gtk_clutter_get_text_color:
142 * @widget: a #GtkWidget
144 * @color: return location for a #ClutterColor
146 * Retrieves the text color of @widget for the given @state and copies it
152 gtk_clutter_get_text_color (GtkWidget *widget,
156 g_return_if_fail (GTK_IS_WIDGET (widget));
157 g_return_if_fail (state >= GTK_STATE_NORMAL &&
158 state <= GTK_STATE_INSENSITIVE);
159 g_return_if_fail (color != NULL);
161 gtk_clutter_get_component (widget, TEXT_COMPONENT, state, color);
165 * gtk_clutter_get_base_color:
166 * @widget: a #GtkWidget
168 * @color: return location for a #ClutterColor
170 * Retrieves the base color of @widget for the given @state and copies it
176 gtk_clutter_get_base_color (GtkWidget *widget,
180 g_return_if_fail (GTK_IS_WIDGET (widget));
181 g_return_if_fail (state >= GTK_STATE_NORMAL &&
182 state <= GTK_STATE_INSENSITIVE);
183 g_return_if_fail (color != NULL);
185 gtk_clutter_get_component (widget, BASE_COMPONENT, state, color);
189 * gtk_clutter_get_light_color:
190 * @widget: a #GtkWidget
192 * @color: return location for a #ClutterColor
194 * Retrieves the light color of @widget for the given @state and copies it
200 gtk_clutter_get_light_color (GtkWidget *widget,
204 g_return_if_fail (GTK_IS_WIDGET (widget));
205 g_return_if_fail (state >= GTK_STATE_NORMAL &&
206 state <= GTK_STATE_INSENSITIVE);
207 g_return_if_fail (color != NULL);
209 gtk_clutter_get_component (widget, LIGHT_COMPONENT, state, color);
213 * gtk_clutter_get_mid_color:
214 * @widget: a #GtkWidget
216 * @color: return location for a #ClutterColor
218 * Retrieves the mid color of @widget for the given @state and copies it
224 gtk_clutter_get_mid_color (GtkWidget *widget,
228 g_return_if_fail (GTK_IS_WIDGET (widget));
229 g_return_if_fail (state >= GTK_STATE_NORMAL &&
230 state <= GTK_STATE_INSENSITIVE);
231 g_return_if_fail (color != NULL);
233 gtk_clutter_get_component (widget, MID_COMPONENT, state, color);
237 * gtk_clutter_get_dark_color:
238 * @widget: a #GtkWidget
240 * @color: return location for a #ClutterColor
242 * Retrieves the dark color of @widget for the given @state and copies it
248 gtk_clutter_get_dark_color (GtkWidget *widget,
252 g_return_if_fail (GTK_IS_WIDGET (widget));
253 g_return_if_fail (state >= GTK_STATE_NORMAL &&
254 state <= GTK_STATE_INSENSITIVE);
255 g_return_if_fail (color != NULL);
257 gtk_clutter_get_component (widget, DARK_COMPONENT, state, color);
261 * gtk_clutter_get_text_aa_color:
262 * @widget: a #GtkWidget
264 * @color: return location for a #ClutterColor
266 * Retrieves the text-aa color of @widget for the given @state and copies it
272 gtk_clutter_get_text_aa_color (GtkWidget *widget,
276 g_return_if_fail (GTK_IS_WIDGET (widget));
277 g_return_if_fail (state >= GTK_STATE_NORMAL &&
278 state <= GTK_STATE_INSENSITIVE);
279 g_return_if_fail (color != NULL);
281 gtk_clutter_get_component (widget, TEXT_AA_COMPONENT, state, color);
285 * gtk_clutter_texture_new_from_pixbuf:
286 * @pixbuf: a #GdkPixbuf
288 * Creates a new #ClutterTexture and sets its contents with a copy
291 * Return value: the newly created #ClutterTexture
296 gtk_clutter_texture_new_from_pixbuf (GdkPixbuf *pixbuf)
298 ClutterActor *retval;
301 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
303 retval = clutter_texture_new ();
306 clutter_texture_set_from_rgb_data (CLUTTER_TEXTURE (retval),
307 gdk_pixbuf_get_pixels (pixbuf),
308 gdk_pixbuf_get_has_alpha (pixbuf),
309 gdk_pixbuf_get_width (pixbuf),
310 gdk_pixbuf_get_height (pixbuf),
311 gdk_pixbuf_get_rowstride (pixbuf),
312 gdk_pixbuf_get_has_alpha (pixbuf) ? 4 : 3,
317 g_warning ("Unable to set the pixbuf: %s", error->message);
318 g_error_free (error);
325 * gtk_clutter_texture_set_from_pixbuf:
326 * @texture: a #ClutterTexture
327 * @pixbuf: a #GdkPixbuf
329 * Sets the contents of @texture with a copy of @pixbuf.
334 gtk_clutter_texture_set_from_pixbuf (ClutterTexture *texture,
339 g_return_if_fail (CLUTTER_IS_TEXTURE (texture));
340 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
343 clutter_texture_set_from_rgb_data (texture,
344 gdk_pixbuf_get_pixels (pixbuf),
345 gdk_pixbuf_get_has_alpha (pixbuf),
346 gdk_pixbuf_get_width (pixbuf),
347 gdk_pixbuf_get_height (pixbuf),
348 gdk_pixbuf_get_rowstride (pixbuf),
349 gdk_pixbuf_get_has_alpha (pixbuf) ? 4 : 3,
354 g_warning ("Unable to set the pixbuf: %s", error->message);
355 g_error_free (error);
360 * gtk_clutter_texture_new_from_stock:
361 * @widget: a #GtkWidget
362 * @stock_id: the stock id of the icon
363 * @size: the size of the icon, or -1
365 * Creates a new #ClutterTexture and sets its contents using the stock
366 * icon @stock_id as rendered by @widget.
368 * Return value: the newly created #ClutterTexture
373 gtk_clutter_texture_new_from_stock (GtkWidget *widget,
374 const gchar *stock_id,
378 ClutterActor *retval;
380 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
381 g_return_val_if_fail (stock_id != NULL, NULL);
382 g_return_val_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1, NULL);
384 pixbuf = gtk_widget_render_icon (widget, stock_id, size, NULL);
386 pixbuf = gtk_widget_render_icon (widget,
387 GTK_STOCK_MISSING_IMAGE, size,
390 retval = gtk_clutter_texture_new_from_pixbuf (pixbuf);
391 g_object_unref (pixbuf);
397 * gtk_clutter_texture_set_from_stock:
398 * @texture: a #ClutterTexture
399 * @widget: a #GtkWidget
400 * @stock_id: the stock id of the icon
401 * @size: the size of the icon, or -1
403 * Sets the contents of @texture using the stock icon @stock_id, as
404 * rendered by @widget.
409 gtk_clutter_texture_set_from_stock (ClutterTexture *texture,
411 const gchar *stock_id,
416 g_return_if_fail (CLUTTER_IS_TEXTURE (texture));
417 g_return_if_fail (GTK_IS_WIDGET (widget));
418 g_return_if_fail (stock_id != NULL);
419 g_return_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1);
421 pixbuf = gtk_widget_render_icon (widget, stock_id, size, NULL);
423 pixbuf = gtk_widget_render_icon (widget,
424 GTK_STOCK_MISSING_IMAGE, size,
427 gtk_clutter_texture_set_from_pixbuf (texture, pixbuf);
428 g_object_unref (pixbuf);
432 * gtk_clutter_texture_new_from_icon_name:
433 * @widget: a #GtkWidget or %NULL
434 * @icon_name: the name of the icon
435 * @size: the size of the icon, or -1
437 * Creates a new #ClutterTexture and sets its contents to be
438 * the @icon_name from the current icon theme.
440 * Return value: the newly created texture, or %NULL if @widget
441 * was %NULL and @icon_name was not found.
446 gtk_clutter_texture_new_from_icon_name (GtkWidget *widget,
447 const gchar *icon_name,
450 GtkSettings *settings;
451 GtkIconTheme *icon_theme;
455 ClutterActor *retval;
457 g_return_val_if_fail (widget == NULL || GTK_IS_WIDGET (widget), NULL);
458 g_return_val_if_fail (icon_name != NULL, NULL);
459 g_return_val_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1, NULL);
461 if (widget && gtk_widget_has_screen (widget))
465 screen = gtk_widget_get_screen (widget);
466 settings = gtk_settings_get_for_screen (screen);
467 icon_theme = gtk_icon_theme_get_for_screen (screen);
471 settings = gtk_settings_get_default ();
472 icon_theme = gtk_icon_theme_get_default ();
476 !gtk_icon_size_lookup_for_settings (settings, size, &width, &height))
482 pixbuf = gtk_icon_theme_load_icon (icon_theme,
484 MIN (width, height), 0,
488 g_warning ("Unable to load the icon `%s' from the theme: %s",
492 g_error_free (error);
495 return gtk_clutter_texture_new_from_stock (widget,
496 GTK_STOCK_MISSING_IMAGE,
502 retval = gtk_clutter_texture_new_from_pixbuf (pixbuf);
503 g_object_unref (pixbuf);
509 * gtk_clutter_texture_set_from_icon_name:
510 * @texture: a #ClutterTexture
511 * @widget: a #GtkWidget or %NULL
512 * @icon_name: the name of the icon
513 * @size: the icon size or -1
515 * Sets the contents of @texture using the @icon_name from the
516 * current icon theme.
521 gtk_clutter_texture_set_from_icon_name (ClutterTexture *texture,
523 const gchar *icon_name,
526 GtkSettings *settings;
527 GtkIconTheme *icon_theme;
532 g_return_if_fail (CLUTTER_IS_TEXTURE (texture));
533 g_return_if_fail (widget == NULL || GTK_IS_WIDGET (widget));
534 g_return_if_fail (icon_name != NULL);
535 g_return_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1);
537 if (widget && gtk_widget_has_screen (widget))
541 screen = gtk_widget_get_screen (widget);
542 settings = gtk_settings_get_for_screen (screen);
543 icon_theme = gtk_icon_theme_get_for_screen (screen);
547 settings = gtk_settings_get_default ();
548 icon_theme = gtk_icon_theme_get_default ();
552 !gtk_icon_size_lookup_for_settings (settings, size, &width, &height))
558 pixbuf = gtk_icon_theme_load_icon (icon_theme,
560 MIN (width, height), 0,
564 g_warning ("Unable to load the icon `%s' from the theme: %s",
568 g_error_free (error);
571 gtk_clutter_texture_set_from_stock (texture,
573 GTK_STOCK_MISSING_IMAGE,
579 gtk_clutter_texture_set_from_pixbuf (texture, pixbuf);
580 g_object_unref (pixbuf);