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),
185 gdk_pixbuf_get_has_alpha (pixbuf) ? 3 : 4,
190 g_warning ("Unable to set the pixbuf: %s", error->message);
191 g_error_free (error);
198 * gtk_clutter_texture_set_from_pixbuf:
199 * @texture: a #ClutterTexture
200 * @pixbuf: a #GdkPixbuf
202 * Sets the contents of @texture with a copy of @pixbuf.
207 gtk_clutter_texture_set_from_pixbuf (ClutterTexture *texture,
212 g_return_if_fail (CLUTTER_IS_TEXTURE (texture));
213 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
216 clutter_texture_set_from_rgb_data (texture,
217 gdk_pixbuf_get_pixels (pixbuf),
218 gdk_pixbuf_get_has_alpha (pixbuf),
219 gdk_pixbuf_get_width (pixbuf),
220 gdk_pixbuf_get_height (pixbuf),
221 gdk_pixbuf_get_rowstride (pixbuf),
222 gdk_pixbuf_get_has_alpha (pixbuf) ? 3 : 4,
227 g_warning ("Unable to set the pixbuf: %s", error->message);
228 g_error_free (error);
233 * gtk_clutter_texture_new_from_stock:
234 * @widget: a #GtkWidget
235 * @stock_id: the stock id of the icon
236 * @size: the size of the icon, or -1
238 * Creates a new #ClutterTexture and sets its contents using the stock
239 * icon @stock_id as rendered by @widget.
241 * Return value: the newly created #ClutterTexture
246 gtk_clutter_texture_new_from_stock (GtkWidget *widget,
247 const gchar *stock_id,
251 ClutterActor *retval;
253 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
254 g_return_val_if_fail (stock_id != NULL, NULL);
255 g_return_val_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1, NULL);
257 pixbuf = gtk_widget_render_icon (widget, stock_id, size, NULL);
259 pixbuf = gtk_widget_render_icon (widget,
260 GTK_STOCK_MISSING_IMAGE, size,
263 retval = gtk_clutter_texture_new_from_pixbuf (pixbuf);
264 g_object_unref (pixbuf);
270 * gtk_clutter_texture_set_from_stock:
271 * @texture: a #ClutterTexture
272 * @widget: a #GtkWidget
273 * @stock_id: the stock id of the icon
274 * @size: the size of the icon, or -1
276 * Sets the contents of @texture using the stock icon @stock_id, as
277 * rendered by @widget.
282 gtk_clutter_texture_set_from_stock (ClutterTexture *texture,
284 const gchar *stock_id,
289 g_return_if_fail (CLUTTER_IS_TEXTURE (texture));
290 g_return_if_fail (GTK_IS_WIDGET (widget));
291 g_return_if_fail (stock_id != NULL);
292 g_return_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1);
294 pixbuf = gtk_widget_render_icon (widget, stock_id, size, NULL);
296 pixbuf = gtk_widget_render_icon (widget,
297 GTK_STOCK_MISSING_IMAGE, size,
300 gtk_clutter_texture_set_from_pixbuf (texture, pixbuf);
301 g_object_unref (pixbuf);
305 * gtk_clutter_texture_new_from_icon_name:
306 * @widget: a #GtkWidget or %NULL
307 * @icon_name: the name of the icon
308 * @size: the size of the icon, or -1
310 * Creates a new #ClutterTexture and sets its contents to be
311 * the @icon_name from the current icon theme.
313 * Return value: the newly created texture, or %NULL if @widget
314 * was %NULL and @icon_name was not found.
319 gtk_clutter_texture_new_from_icon_name (GtkWidget *widget,
320 const gchar *icon_name,
323 GtkSettings *settings;
324 GtkIconTheme *icon_theme;
328 ClutterActor *retval;
330 g_return_val_if_fail (widget == NULL || GTK_IS_WIDGET (widget), NULL);
331 g_return_val_if_fail (icon_name != NULL, NULL);
332 g_return_val_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1, NULL);
334 if (widget && gtk_widget_has_screen (widget))
338 screen = gtk_widget_get_screen (widget);
339 settings = gtk_settings_get_for_screen (screen);
340 icon_theme = gtk_icon_theme_get_for_screen (screen);
344 settings = gtk_settings_get_default ();
345 icon_theme = gtk_icon_theme_get_default ();
349 !gtk_icon_size_lookup_for_settings (settings, size, &width, &height))
355 pixbuf = gtk_icon_theme_load_icon (icon_theme,
357 MIN (width, height), 0,
361 g_warning ("Unable to load the icon `%s' from the theme: %s",
365 g_error_free (error);
368 return gtk_clutter_texture_new_from_stock (widget,
369 GTK_STOCK_MISSING_IMAGE,
375 retval = gtk_clutter_texture_new_from_pixbuf (pixbuf);
376 g_object_unref (pixbuf);
382 * gtk_clutter_texture_set_from_icon_name:
383 * @texture: a #ClutterTexture
384 * @widget: a #GtkWidget or %NULL
385 * @icon_name: the name of the icon
386 * @size: the icon size or -1
388 * Sets the contents of @texture using the @icon_name from the
389 * current icon theme.
394 gtk_clutter_texture_set_from_icon_name (ClutterTexture *texture,
396 const gchar *icon_name,
399 GtkSettings *settings;
400 GtkIconTheme *icon_theme;
405 g_return_if_fail (CLUTTER_IS_TEXTURE (texture));
406 g_return_if_fail (widget == NULL || GTK_IS_WIDGET (widget));
407 g_return_if_fail (icon_name != NULL);
408 g_return_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1);
410 if (widget && gtk_widget_has_screen (widget))
414 screen = gtk_widget_get_screen (widget);
415 settings = gtk_settings_get_for_screen (screen);
416 icon_theme = gtk_icon_theme_get_for_screen (screen);
420 settings = gtk_settings_get_default ();
421 icon_theme = gtk_icon_theme_get_default ();
425 !gtk_icon_size_lookup_for_settings (settings, size, &width, &height))
431 pixbuf = gtk_icon_theme_load_icon (icon_theme,
433 MIN (width, height), 0,
437 g_warning ("Unable to load the icon `%s' from the theme: %s",
441 g_error_free (error);
444 gtk_clutter_texture_set_from_stock (texture,
446 GTK_STOCK_MISSING_IMAGE,
452 gtk_clutter_texture_set_from_pixbuf (texture, pixbuf);
453 g_object_unref (pixbuf);