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,
52 G_DEFINE_TYPE (GtkThemingEngine, gtk_theming_engine, G_TYPE_OBJECT)
55 typedef struct GtkThemingModule GtkThemingModule;
56 typedef struct GtkThemingModuleClass GtkThemingModuleClass;
58 struct GtkThemingModule
60 GTypeModule parent_instance;
63 GtkThemingEngine * (*create_engine) (void);
66 struct GtkThemingModuleClass
68 GTypeModuleClass parent_class;
71 #define GTK_TYPE_THEMING_MODULE (gtk_theming_module_get_type ())
72 #define GTK_THEMING_MODULE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GTK_TYPE_THEMING_MODULE, GtkThemingModule))
73 #define GTK_IS_THEMING_MODULE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GTK_TYPE_THEMING_MODULE))
75 G_DEFINE_TYPE (GtkThemingModule, gtk_theming_module, G_TYPE_TYPE_MODULE);
78 gtk_theming_engine_class_init (GtkThemingEngineClass *klass)
80 GObjectClass *object_class = G_OBJECT_CLASS (klass);
82 klass->render_check = gtk_theming_engine_render_check;
83 klass->render_option = gtk_theming_engine_render_option;
85 g_type_class_add_private (object_class, sizeof (GtkThemingEnginePrivate));
89 gtk_theming_engine_init (GtkThemingEngine *engine)
91 engine->priv = GTK_THEMING_ENGINE_GET_PRIVATE (engine);
95 _gtk_theming_engine_set_context (GtkThemingEngine *engine,
96 GtkStyleContext *context)
98 GtkThemingEnginePrivate *priv;
100 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
101 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
104 priv->context = context;
108 gtk_theming_engine_get_property (GtkThemingEngine *engine,
109 const gchar *property,
113 GtkThemingEnginePrivate *priv;
115 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
116 g_return_if_fail (property != NULL);
117 g_return_if_fail (state < GTK_STATE_LAST);
118 g_return_if_fail (value != NULL);
121 gtk_style_context_get_property (priv->context, property, state, value);
125 gtk_theming_engine_get_valist (GtkThemingEngine *engine,
129 GtkThemingEnginePrivate *priv;
131 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
132 g_return_if_fail (state < GTK_STATE_LAST);
135 gtk_style_context_get_valist (priv->context, state, args);
139 gtk_theming_engine_get (GtkThemingEngine *engine,
143 GtkThemingEnginePrivate *priv;
146 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
147 g_return_if_fail (state < GTK_STATE_LAST);
151 va_start (args, state);
152 gtk_style_context_get_valist (priv->context, state, args);
157 gtk_theming_engine_get_state (GtkThemingEngine *engine)
159 GtkThemingEnginePrivate *priv;
161 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), 0);
164 return gtk_style_context_get_state (priv->context);
168 gtk_theming_engine_is_state_set (GtkThemingEngine *engine,
171 GtkThemingEnginePrivate *priv;
173 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), 0);
176 return gtk_style_context_is_state_set (priv->context, state);
179 G_CONST_RETURN GtkWidgetPath *
180 gtk_theming_engine_get_path (GtkThemingEngine *engine)
182 GtkThemingEnginePrivate *priv;
184 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);
187 return gtk_style_context_get_path (priv->context);
191 gtk_theming_engine_has_class (GtkThemingEngine *engine,
192 const gchar *style_class)
194 GtkThemingEnginePrivate *priv;
196 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
199 return gtk_style_context_has_class (priv->context, style_class);
203 gtk_theming_engine_has_child_class (GtkThemingEngine *engine,
204 const gchar *style_class,
205 GtkChildClassFlags *flags)
207 GtkThemingEnginePrivate *priv;
212 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
215 return gtk_style_context_has_child_class (priv->context, style_class, flags);
218 /* GtkThemingModule */
221 gtk_theming_module_load (GTypeModule *type_module)
223 GtkThemingModule *theming_module;
225 gchar *name, *module_path;
227 theming_module = GTK_THEMING_MODULE (type_module);
228 name = theming_module->name;
229 module_path = _gtk_find_module (name, "theming-engines");
233 g_warning (_("Unable to locate theme engine in module path: \"%s\","), name);
237 module = g_module_open (module_path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
238 g_free (module_path);
242 g_warning ("%s", g_module_error ());
246 if (!g_module_symbol (module, "create_engine",
247 (gpointer *) &theming_module->create_engine))
249 g_warning ("%s", g_module_error());
250 g_module_close (module);
255 g_module_make_resident (module);
261 gtk_theming_module_class_init (GtkThemingModuleClass *klass)
263 GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (klass);
265 module_class->load = gtk_theming_module_load;
269 gtk_theming_module_init (GtkThemingModule *module)
273 G_CONST_RETURN GtkThemingEngine *
274 gtk_theming_engine_load (const gchar *name)
276 static GHashTable *engines = NULL;
277 static GtkThemingEngine *default_engine;
278 GtkThemingEngine *engine = NULL;
283 engines = g_hash_table_new (g_str_hash, g_str_equal);
285 engine = g_hash_table_lookup (engines, name);
289 GtkThemingModule *module;
291 module = g_object_new (GTK_TYPE_THEMING_MODULE, NULL);
292 g_type_module_set_name (G_TYPE_MODULE (module), name);
293 module->name = g_strdup (name);
295 if (module && g_type_module_use (G_TYPE_MODULE (module)))
297 engine = (module->create_engine) ();
300 g_hash_table_insert (engines, module->name, engine);
308 default_engine = g_object_new (GTK_TYPE_THEMING_ENGINE, NULL);
310 engine = default_engine;
316 /* Paint method implementations */
318 gtk_theming_engine_render_check (GtkThemingEngine *engine,
325 GdkColor *fg_color, *base_color, *text_color;
326 const GtkWidgetPath *path;
330 flags = gtk_theming_engine_get_state (engine);
331 path = gtk_theming_engine_get_path (engine);
334 if (flags & GTK_STATE_FLAG_PRELIGHT)
335 state = GTK_STATE_PRELIGHT;
337 state = GTK_STATE_NORMAL;
339 gtk_theming_engine_get (engine, state,
340 "foreground-color", &fg_color,
341 "base-color", &base_color,
342 "text-color", &text_color,
345 if (!gtk_widget_path_has_parent (path, GTK_TYPE_MENU))
347 cairo_set_line_width (cr, 1);
351 width - 1, height - 1);
353 gdk_cairo_set_source_color (cr, base_color);
354 cairo_fill_preserve (cr);
356 if (gtk_widget_path_has_parent (path, GTK_TYPE_TREE_VIEW))
357 gdk_cairo_set_source_color (cr, text_color);
359 gdk_cairo_set_source_color (cr, fg_color);
364 cairo_set_line_width (cr, 1.5);
365 gdk_cairo_set_source_color (cr, text_color);
367 if (gtk_theming_engine_is_state_set (engine, GTK_STATE_INCONSISTENT))
369 cairo_move_to (cr, x + (width * 0.2), y + (height / 2));
370 cairo_line_to (cr, x + (width * 0.8), y + (height / 2));
372 else if (gtk_theming_engine_is_state_set (engine, GTK_STATE_ACTIVE))
374 cairo_move_to (cr, x + (width * 0.2), y + (height / 2));
375 cairo_line_to (cr, x + (width * 0.4), y + (height * 0.8));
376 cairo_line_to (cr, x + (width * 0.8), y + (height * 0.2));
383 gdk_color_free (fg_color);
384 gdk_color_free (base_color);
385 gdk_color_free (text_color);
389 gtk_theming_engine_render_option (GtkThemingEngine *engine,
397 GdkColor *base_color, *fg_color, *text_color;
398 const GtkWidgetPath *path;
402 flags = gtk_theming_engine_get_state (engine);
403 path = gtk_theming_engine_get_path (engine);
404 radius = MIN (width, height) / 2 - 0.5;
407 cairo_set_line_width (cr, 1);
409 if (flags & GTK_STATE_FLAG_PRELIGHT)
410 state = GTK_STATE_PRELIGHT;
412 state = GTK_STATE_NORMAL;
414 gtk_theming_engine_get (engine, state,
415 "foreground-color", &fg_color,
416 "base-color", &base_color,
417 "text-color", &text_color,
420 if (!gtk_widget_path_has_parent (path, GTK_TYPE_MENU))
428 gdk_cairo_set_source_color (cr, base_color);
429 cairo_fill_preserve (cr);
431 if (gtk_widget_path_has_parent (path, GTK_TYPE_TREE_VIEW))
432 gdk_cairo_set_source_color (cr, text_color);
434 gdk_cairo_set_source_color (cr, fg_color);
439 if (gtk_widget_path_has_parent (path, GTK_TYPE_MENU))
440 gdk_cairo_set_source_color (cr, fg_color);
442 gdk_cairo_set_source_color (cr, text_color);
444 if (gtk_theming_engine_is_state_set (engine, GTK_STATE_INCONSISTENT))
446 cairo_move_to (cr, x + (width * 0.2), y + (height / 2));
447 cairo_line_to (cr, x + (width * 0.8), y + (height / 2));
450 if (gtk_theming_engine_is_state_set (engine, GTK_STATE_ACTIVE))
464 #define __GTK_THEMING_ENGINE_C__
465 #include "gtkaliasdef.c"