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.
24 gtk_clutter_get_component (GtkWidget *widget,
29 GtkStyle *style = gtk_widget_get_style (widget);
30 GdkColor gtk_color = { 0, };
35 gtk_color = style->fg[state];
39 gtk_color = style->bg[state];
43 gtk_color = style->text[state];
47 gtk_color = style->base[state];
51 g_assert_not_reached ();
55 color->red = CLAMP (((gtk_color.red / 65535.0) * 255), 0, 255);
56 color->green = CLAMP (((gtk_color.green / 65535.0) * 255), 0, 255);
57 color->blue = CLAMP (((gtk_color.blue / 65535.0) * 255), 0, 255);
62 * gtk_clutter_get_fg_color:
63 * @widget: a #GtkWidget
65 * @color: return location for a #ClutterColor
67 * Retrieves the foreground color of @widget for the given @state and copies
73 gtk_clutter_get_fg_color (GtkWidget *widget,
77 g_return_if_fail (GTK_IS_WIDGET (widget));
78 g_return_if_fail (state >= GTK_STATE_NORMAL &&
79 state <= GTK_STATE_INSENSITIVE);
80 g_return_if_fail (color != NULL);
82 gtk_clutter_get_component (widget, GTK_RC_FG, state, color);
86 * gtk_clutter_get_bg_color:
87 * @widget: a #GtkWidget
89 * @color: return location for a #ClutterColor
91 * Retrieves the background color of @widget for the given @state and copies
97 gtk_clutter_get_bg_color (GtkWidget *widget,
101 g_return_if_fail (GTK_IS_WIDGET (widget));
102 g_return_if_fail (state >= GTK_STATE_NORMAL &&
103 state <= GTK_STATE_INSENSITIVE);
104 g_return_if_fail (color != NULL);
106 gtk_clutter_get_component (widget, GTK_RC_BG, state, color);
110 * gtk_clutter_get_text_color:
111 * @widget: a #GtkWidget
113 * @color: return location for a #ClutterColor
115 * Retrieves the text color of @widget for the given @state and copies it
121 gtk_clutter_get_text_color (GtkWidget *widget,
125 g_return_if_fail (GTK_IS_WIDGET (widget));
126 g_return_if_fail (state >= GTK_STATE_NORMAL &&
127 state <= GTK_STATE_INSENSITIVE);
128 g_return_if_fail (color != NULL);
130 gtk_clutter_get_component (widget, GTK_RC_TEXT, state, color);
134 * gtk_clutter_get_base_color:
135 * @widget: a #GtkWidget
137 * @color: return location for a #ClutterColor
139 * Retrieves the base color of @widget for the given @state and copies it
145 gtk_clutter_get_base_color (GtkWidget *widget,
149 g_return_if_fail (GTK_IS_WIDGET (widget));
150 g_return_if_fail (state >= GTK_STATE_NORMAL &&
151 state <= GTK_STATE_INSENSITIVE);
152 g_return_if_fail (color != NULL);
154 gtk_clutter_get_component (widget, GTK_RC_BASE, state, color);
158 * gtk_clutter_texture_new_from_pixbuf:
159 * @pixbuf: a #GdkPixbuf
161 * Creates a new #ClutterTexture and sets its contents with a copy
164 * Return value: the newly created #ClutterTexture
169 gtk_clutter_texture_new_from_pixbuf (GdkPixbuf *pixbuf)
171 ClutterActor *retval;
174 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
176 retval = clutter_texture_new ();
179 clutter_texture_set_from_rgb_data (CLUTTER_TEXTURE (retval),
180 gdk_pixbuf_get_pixels (pixbuf),
181 gdk_pixbuf_get_has_alpha (pixbuf),
182 gdk_pixbuf_get_width (pixbuf),
183 gdk_pixbuf_get_height (pixbuf),
184 gdk_pixbuf_get_rowstride (pixbuf),
189 g_warning ("Unable to set the pixbuf: %s", error->message);
190 g_error_free (error);
197 * gtk_clutter_texture_set_from_pixbuf:
198 * @texture: a #ClutterTexture
199 * @pixbuf: a #GdkPixbuf
201 * Sets the contents of @texture with a copy of @pixbuf.
206 gtk_clutter_texture_set_from_pixbuf (ClutterTexture *texture,
211 g_return_if_fail (CLUTTER_IS_TEXTURE (texture));
212 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
215 clutter_texture_set_from_rgb_data (texture,
216 gdk_pixbuf_get_pixels (pixbuf),
217 gdk_pixbuf_get_has_alpha (pixbuf),
218 gdk_pixbuf_get_width (pixbuf),
219 gdk_pixbuf_get_height (pixbuf),
220 gdk_pixbuf_get_rowstride (pixbuf),
225 g_warning ("Unable to set the pixbuf: %s", error->message);
226 g_error_free (error);
231 * gtk_clutter_texture_new_from_stock:
232 * @widget: a #GtkWidget
233 * @stock_id: the stock id of the icon
234 * @size: the size of the icon, or -1
236 * Creates a new #ClutterTexture and sets its contents using the stock
237 * icon @stock_id as rendered by @widget.
239 * Return value: the newly created #ClutterTexture
244 gtk_clutter_texture_new_from_stock (GtkWidget *widget,
245 const gchar *stock_id,
249 ClutterActor *retval;
251 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
252 g_return_val_if_fail (stock_id != NULL, NULL);
253 g_return_val_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1, NULL);
255 pixbuf = gtk_widget_render_icon (widget, stock_id, size, NULL);
257 pixbuf = gtk_widget_render_icon (widget,
258 GTK_STOCK_MISSING_IMAGE, size,
261 retval = gtk_clutter_texture_new_from_pixbuf (pixbuf);
262 g_object_unref (pixbuf);
268 * gtk_clutter_texture_set_from_stock:
269 * @texture: a #ClutterTexture
270 * @widget: a #GtkWidget
271 * @stock_id: the stock id of the icon
272 * @size: the size of the icon, or -1
274 * Sets the contents of @texture using the stock icon @stock_id, as
275 * rendered by @widget.
280 gtk_clutter_texture_set_from_stock (ClutterTexture *texture,
282 const gchar *stock_id,
287 g_return_if_fail (CLUTTER_IS_TEXTURE (texture));
288 g_return_if_fail (GTK_IS_WIDGET (widget));
289 g_return_if_fail (stock_id != NULL);
290 g_return_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1);
292 pixbuf = gtk_widget_render_icon (widget, stock_id, size, NULL);
294 pixbuf = gtk_widget_render_icon (widget,
295 GTK_STOCK_MISSING_IMAGE, size,
298 gtk_clutter_texture_set_from_pixbuf (texture, pixbuf);
299 g_object_unref (pixbuf);
303 * gtk_clutter_texture_new_from_icon_name:
304 * @widget: a #GtkWidget or %NULL
305 * @icon_name: the name of the icon
306 * @size: the size of the icon, or -1
308 * Creates a new #ClutterTexture and sets its contents to be
309 * the @icon_name from the current icon theme.
311 * Return value: the newly created texture, or %NULL if @widget
312 * was %NULL and @icon_name was not found.
317 gtk_clutter_texture_new_from_icon_name (GtkWidget *widget,
318 const gchar *icon_name,
321 GtkSettings *settings;
322 GtkIconTheme *icon_theme;
326 ClutterActor *retval;
328 g_return_val_if_fail (widget == NULL || GTK_IS_WIDGET (widget), NULL);
329 g_return_val_if_fail (icon_name != NULL, NULL);
330 g_return_val_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1, NULL);
332 if (widget && gtk_widget_has_screen (widget))
336 screen = gtk_widget_get_screen (widget);
337 settings = gtk_settings_get_for_screen (screen);
338 icon_theme = gtk_icon_theme_get_for_screen (screen);
342 settings = gtk_settings_get_default ();
343 icon_theme = gtk_icon_theme_get_default ();
347 !gtk_icon_size_lookup_for_settings (settings, size, &width, &height))
353 pixbuf = gtk_icon_theme_load_icon (icon_theme,
355 MIN (width, height), 0,
359 g_warning ("Unable to load the icon `%s' from the theme: %s",
363 g_error_free (error);
366 return gtk_clutter_texture_new_from_stock (widget,
367 GTK_STOCK_MISSING_IMAGE,
373 retval = gtk_clutter_texture_new_from_pixbuf (pixbuf);
374 g_object_unref (pixbuf);
380 * gtk_clutter_texture_set_from_icon_name:
381 * @texture: a #ClutterTexture
382 * @widget: a #GtkWidget or %NULL
383 * @icon_name: the name of the icon
384 * @size: the icon size or -1
386 * Sets the contents of @texture using the @icon_name from the
387 * current icon theme.
392 gtk_clutter_texture_set_from_icon_name (ClutterTexture *texture,
394 const gchar *icon_name,
397 GtkSettings *settings;
398 GtkIconTheme *icon_theme;
403 g_return_if_fail (CLUTTER_IS_TEXTURE (texture));
404 g_return_if_fail (widget == NULL || GTK_IS_WIDGET (widget));
405 g_return_if_fail (icon_name != NULL);
406 g_return_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1);
408 if (widget && gtk_widget_has_screen (widget))
412 screen = gtk_widget_get_screen (widget);
413 settings = gtk_settings_get_for_screen (screen);
414 icon_theme = gtk_icon_theme_get_for_screen (screen);
418 settings = gtk_settings_get_default ();
419 icon_theme = gtk_icon_theme_get_default ();
423 !gtk_icon_size_lookup_for_settings (settings, size, &width, &height))
429 pixbuf = gtk_icon_theme_load_icon (icon_theme,
431 MIN (width, height), 0,
435 g_warning ("Unable to load the icon `%s' from the theme: %s",
439 g_error_free (error);
442 gtk_clutter_texture_set_from_stock (texture,
444 GTK_STOCK_MISSING_IMAGE,
450 gtk_clutter_texture_set_from_pixbuf (texture, pixbuf);
451 g_object_unref (pixbuf);