1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
24 #include <gtk/gtkthemingengine.h>
25 #include <gtk/gtkstylecontext.h>
26 #include <gtk/gtkintl.h>
30 typedef struct GtkThemingEnginePrivate GtkThemingEnginePrivate;
32 struct GtkThemingEnginePrivate
34 GtkStyleContext *context;
37 #define GTK_THEMING_ENGINE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_THEMING_ENGINE, GtkThemingEnginePrivate))
39 static void gtk_theming_engine_render_check (GtkThemingEngine *engine,
45 static void gtk_theming_engine_render_option (GtkThemingEngine *engine,
51 static void gtk_theming_engine_render_arrow (GtkThemingEngine *engine,
58 G_DEFINE_TYPE (GtkThemingEngine, gtk_theming_engine, G_TYPE_OBJECT)
61 typedef struct GtkThemingModule GtkThemingModule;
62 typedef struct GtkThemingModuleClass GtkThemingModuleClass;
64 struct GtkThemingModule
66 GTypeModule parent_instance;
69 GtkThemingEngine * (*create_engine) (void);
72 struct GtkThemingModuleClass
74 GTypeModuleClass parent_class;
77 #define GTK_TYPE_THEMING_MODULE (gtk_theming_module_get_type ())
78 #define GTK_THEMING_MODULE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GTK_TYPE_THEMING_MODULE, GtkThemingModule))
79 #define GTK_IS_THEMING_MODULE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GTK_TYPE_THEMING_MODULE))
81 G_DEFINE_TYPE (GtkThemingModule, gtk_theming_module, G_TYPE_TYPE_MODULE);
84 gtk_theming_engine_class_init (GtkThemingEngineClass *klass)
86 GObjectClass *object_class = G_OBJECT_CLASS (klass);
88 klass->render_check = gtk_theming_engine_render_check;
89 klass->render_option = gtk_theming_engine_render_option;
90 klass->render_arrow = gtk_theming_engine_render_arrow;
92 g_type_class_add_private (object_class, sizeof (GtkThemingEnginePrivate));
96 gtk_theming_engine_init (GtkThemingEngine *engine)
98 engine->priv = GTK_THEMING_ENGINE_GET_PRIVATE (engine);
102 _gtk_theming_engine_set_context (GtkThemingEngine *engine,
103 GtkStyleContext *context)
105 GtkThemingEnginePrivate *priv;
107 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
108 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
111 priv->context = context;
115 gtk_theming_engine_get_property (GtkThemingEngine *engine,
116 const gchar *property,
120 GtkThemingEnginePrivate *priv;
122 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
123 g_return_if_fail (property != NULL);
124 g_return_if_fail (state < GTK_STATE_LAST);
125 g_return_if_fail (value != NULL);
128 gtk_style_context_get_property (priv->context, property, state, value);
132 gtk_theming_engine_get_valist (GtkThemingEngine *engine,
136 GtkThemingEnginePrivate *priv;
138 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
139 g_return_if_fail (state < GTK_STATE_LAST);
142 gtk_style_context_get_valist (priv->context, state, args);
146 gtk_theming_engine_get (GtkThemingEngine *engine,
150 GtkThemingEnginePrivate *priv;
153 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
154 g_return_if_fail (state < GTK_STATE_LAST);
158 va_start (args, state);
159 gtk_style_context_get_valist (priv->context, state, args);
164 gtk_theming_engine_get_state (GtkThemingEngine *engine)
166 GtkThemingEnginePrivate *priv;
168 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), 0);
171 return gtk_style_context_get_state (priv->context);
175 gtk_theming_engine_is_state_set (GtkThemingEngine *engine,
178 GtkThemingEnginePrivate *priv;
180 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), 0);
183 return gtk_style_context_is_state_set (priv->context, state);
186 G_CONST_RETURN GtkWidgetPath *
187 gtk_theming_engine_get_path (GtkThemingEngine *engine)
189 GtkThemingEnginePrivate *priv;
191 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);
194 return gtk_style_context_get_path (priv->context);
198 gtk_theming_engine_has_class (GtkThemingEngine *engine,
199 const gchar *style_class)
201 GtkThemingEnginePrivate *priv;
203 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
206 return gtk_style_context_has_class (priv->context, style_class);
210 gtk_theming_engine_has_child_class (GtkThemingEngine *engine,
211 const gchar *style_class,
212 GtkChildClassFlags *flags)
214 GtkThemingEnginePrivate *priv;
219 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
222 return gtk_style_context_has_child_class (priv->context, style_class, flags);
225 /* GtkThemingModule */
228 gtk_theming_module_load (GTypeModule *type_module)
230 GtkThemingModule *theming_module;
232 gchar *name, *module_path;
234 theming_module = GTK_THEMING_MODULE (type_module);
235 name = theming_module->name;
236 module_path = _gtk_find_module (name, "theming-engines");
240 g_warning (_("Unable to locate theme engine in module path: \"%s\","), name);
244 module = g_module_open (module_path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
245 g_free (module_path);
249 g_warning ("%s", g_module_error ());
253 if (!g_module_symbol (module, "create_engine",
254 (gpointer *) &theming_module->create_engine))
256 g_warning ("%s", g_module_error());
257 g_module_close (module);
262 g_module_make_resident (module);
268 gtk_theming_module_class_init (GtkThemingModuleClass *klass)
270 GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (klass);
272 module_class->load = gtk_theming_module_load;
276 gtk_theming_module_init (GtkThemingModule *module)
280 G_CONST_RETURN GtkThemingEngine *
281 gtk_theming_engine_load (const gchar *name)
283 static GHashTable *engines = NULL;
284 static GtkThemingEngine *default_engine;
285 GtkThemingEngine *engine = NULL;
290 engines = g_hash_table_new (g_str_hash, g_str_equal);
292 engine = g_hash_table_lookup (engines, name);
296 GtkThemingModule *module;
298 module = g_object_new (GTK_TYPE_THEMING_MODULE, NULL);
299 g_type_module_set_name (G_TYPE_MODULE (module), name);
300 module->name = g_strdup (name);
302 if (module && g_type_module_use (G_TYPE_MODULE (module)))
304 engine = (module->create_engine) ();
307 g_hash_table_insert (engines, module->name, engine);
315 default_engine = g_object_new (GTK_TYPE_THEMING_ENGINE, NULL);
317 engine = default_engine;
323 /* Paint method implementations */
325 gtk_theming_engine_render_check (GtkThemingEngine *engine,
332 GdkColor *fg_color, *base_color, *text_color;
333 const GtkWidgetPath *path;
337 flags = gtk_theming_engine_get_state (engine);
338 path = gtk_theming_engine_get_path (engine);
341 if (flags & GTK_STATE_FLAG_PRELIGHT)
342 state = GTK_STATE_PRELIGHT;
344 state = GTK_STATE_NORMAL;
346 gtk_theming_engine_get (engine, state,
347 "foreground-color", &fg_color,
348 "base-color", &base_color,
349 "text-color", &text_color,
352 if (!gtk_widget_path_has_parent (path, GTK_TYPE_MENU))
354 cairo_set_line_width (cr, 1);
358 width - 1, height - 1);
360 gdk_cairo_set_source_color (cr, base_color);
361 cairo_fill_preserve (cr);
363 if (gtk_widget_path_has_parent (path, GTK_TYPE_TREE_VIEW))
364 gdk_cairo_set_source_color (cr, text_color);
366 gdk_cairo_set_source_color (cr, fg_color);
371 cairo_set_line_width (cr, 1.5);
372 gdk_cairo_set_source_color (cr, text_color);
374 if (gtk_theming_engine_is_state_set (engine, GTK_STATE_INCONSISTENT))
376 cairo_move_to (cr, x + (width * 0.2), y + (height / 2));
377 cairo_line_to (cr, x + (width * 0.8), y + (height / 2));
379 else if (gtk_theming_engine_is_state_set (engine, GTK_STATE_ACTIVE))
381 cairo_move_to (cr, x + (width * 0.2), y + (height / 2));
382 cairo_line_to (cr, x + (width * 0.4), y + (height * 0.8));
383 cairo_line_to (cr, x + (width * 0.8), y + (height * 0.2));
390 gdk_color_free (fg_color);
391 gdk_color_free (base_color);
392 gdk_color_free (text_color);
396 gtk_theming_engine_render_option (GtkThemingEngine *engine,
404 GdkColor *base_color, *fg_color, *text_color;
405 const GtkWidgetPath *path;
409 flags = gtk_theming_engine_get_state (engine);
410 path = gtk_theming_engine_get_path (engine);
411 radius = MIN (width, height) / 2 - 0.5;
414 cairo_set_line_width (cr, 1);
416 if (flags & GTK_STATE_FLAG_PRELIGHT)
417 state = GTK_STATE_PRELIGHT;
419 state = GTK_STATE_NORMAL;
421 gtk_theming_engine_get (engine, state,
422 "foreground-color", &fg_color,
423 "base-color", &base_color,
424 "text-color", &text_color,
427 if (!gtk_widget_path_has_parent (path, GTK_TYPE_MENU))
435 gdk_cairo_set_source_color (cr, base_color);
436 cairo_fill_preserve (cr);
438 if (gtk_widget_path_has_parent (path, GTK_TYPE_TREE_VIEW))
439 gdk_cairo_set_source_color (cr, text_color);
441 gdk_cairo_set_source_color (cr, fg_color);
446 if (gtk_widget_path_has_parent (path, GTK_TYPE_MENU))
447 gdk_cairo_set_source_color (cr, fg_color);
449 gdk_cairo_set_source_color (cr, text_color);
451 if (gtk_theming_engine_is_state_set (engine, GTK_STATE_INCONSISTENT))
453 cairo_move_to (cr, x + (width * 0.2), y + (height / 2));
454 cairo_line_to (cr, x + (width * 0.8), y + (height / 2));
457 if (gtk_theming_engine_is_state_set (engine, GTK_STATE_ACTIVE))
472 add_path_arrow (cairo_t *cr,
480 cairo_translate (cr, x + (size / 2), y + (size / 2));
481 cairo_rotate (cr, angle);
483 cairo_move_to (cr, 0, - (size / 4) + 0.5);
484 cairo_line_to (cr, - (size / 2), (size / 4) + 0.5);
485 cairo_line_to (cr, (size / 2), (size / 4) + 0.5);
486 cairo_close_path (cr);
492 gtk_theming_engine_render_arrow (GtkThemingEngine *engine,
505 flags = gtk_theming_engine_get_state (engine);
507 if (flags & GTK_STATE_FLAG_PRELIGHT)
508 state = GTK_STATE_PRELIGHT;
509 else if (flags & GTK_STATE_FLAG_INSENSITIVE)
510 state = GTK_STATE_INSENSITIVE;
512 state = GTK_STATE_NORMAL;
514 gtk_theming_engine_get (engine, state,
515 "foreground-color", &fg_color,
518 if (flags & GTK_STATE_FLAG_INSENSITIVE)
520 add_path_arrow (cr, angle, x + 1, y + 1, size);
521 cairo_set_source_rgb (cr, 1, 1, 1);
525 add_path_arrow (cr, angle, x, y, size);
526 gdk_cairo_set_source_color (cr, fg_color);
531 gdk_color_free (fg_color);
534 #define __GTK_THEMING_ENGINE_C__
535 #include "gtkaliasdef.c"