]> Pileus Git - ~andy/gtk/blob - gtk/gtkthemingengine.c
2962f522232fb78a0aeb6e80b0f5c511aa172301
[~andy/gtk] / gtk / gtkthemingengine.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include "config.h"
21
22 #include <gtk/gtk.h>
23
24 #include <gtk/gtkthemingengine.h>
25 #include <gtk/gtkstylecontext.h>
26 #include <gtk/gtkintl.h>
27
28 #include "gtkalias.h"
29
30 typedef struct GtkThemingEnginePrivate GtkThemingEnginePrivate;
31
32 struct GtkThemingEnginePrivate
33 {
34   GtkStyleContext *context;
35 };
36
37 #define GTK_THEMING_ENGINE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_THEMING_ENGINE, GtkThemingEnginePrivate))
38
39 static void gtk_theming_engine_render_check (GtkThemingEngine *engine,
40                                              cairo_t          *cr,
41                                              gdouble           x,
42                                              gdouble           y,
43                                              gdouble           width,
44                                              gdouble           height);
45 static void gtk_theming_engine_render_option (GtkThemingEngine *engine,
46                                               cairo_t          *cr,
47                                               gdouble           x,
48                                               gdouble           y,
49                                               gdouble           width,
50                                               gdouble           height);
51
52 G_DEFINE_TYPE (GtkThemingEngine, gtk_theming_engine, G_TYPE_OBJECT)
53
54
55 typedef struct GtkThemingModule GtkThemingModule;
56 typedef struct GtkThemingModuleClass GtkThemingModuleClass;
57
58 struct GtkThemingModule
59 {
60   GTypeModule parent_instance;
61   gchar *name;
62
63   GtkThemingEngine * (*create_engine) (void);
64 };
65
66 struct GtkThemingModuleClass
67 {
68   GTypeModuleClass parent_class;
69 };
70
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))
74
75 G_DEFINE_TYPE (GtkThemingModule, gtk_theming_module, G_TYPE_TYPE_MODULE);
76
77 static void
78 gtk_theming_engine_class_init (GtkThemingEngineClass *klass)
79 {
80   GObjectClass *object_class = G_OBJECT_CLASS (klass);
81
82   klass->render_check = gtk_theming_engine_render_check;
83   klass->render_option = gtk_theming_engine_render_option;
84
85   g_type_class_add_private (object_class, sizeof (GtkThemingEnginePrivate));
86 }
87
88 static void
89 gtk_theming_engine_init (GtkThemingEngine *engine)
90 {
91   engine->priv = GTK_THEMING_ENGINE_GET_PRIVATE (engine);
92 }
93
94 void
95 _gtk_theming_engine_set_context (GtkThemingEngine *engine,
96                                  GtkStyleContext  *context)
97 {
98   GtkThemingEnginePrivate *priv;
99
100   g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
101   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
102
103   priv = engine->priv;
104   priv->context = context;
105 }
106
107 void
108 gtk_theming_engine_get_property (GtkThemingEngine *engine,
109                                  const gchar      *property,
110                                  GtkStateType      state,
111                                  GValue           *value)
112 {
113   GtkThemingEnginePrivate *priv;
114
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);
119
120   priv = engine->priv;
121   gtk_style_context_get_property (priv->context, property, state, value);
122 }
123
124 void
125 gtk_theming_engine_get_valist (GtkThemingEngine *engine,
126                                GtkStateType      state,
127                                va_list           args)
128 {
129   GtkThemingEnginePrivate *priv;
130
131   g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
132   g_return_if_fail (state < GTK_STATE_LAST);
133
134   priv = engine->priv;
135   gtk_style_context_get_valist (priv->context, state, args);
136 }
137
138 void
139 gtk_theming_engine_get (GtkThemingEngine *engine,
140                         GtkStateType      state,
141                         ...)
142 {
143   GtkThemingEnginePrivate *priv;
144   va_list args;
145
146   g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
147   g_return_if_fail (state < GTK_STATE_LAST);
148
149   priv = engine->priv;
150
151   va_start (args, state);
152   gtk_style_context_get_valist (priv->context, state, args);
153   va_end (args);
154 }
155
156 GtkStateFlags
157 gtk_theming_engine_get_state (GtkThemingEngine *engine)
158 {
159   GtkThemingEnginePrivate *priv;
160
161   g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), 0);
162
163   priv = engine->priv;
164   return gtk_style_context_get_state (priv->context);
165 }
166
167 gboolean
168 gtk_theming_engine_is_state_set (GtkThemingEngine *engine,
169                                  GtkStateType      state)
170 {
171   GtkThemingEnginePrivate *priv;
172
173   g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), 0);
174
175   priv = engine->priv;
176   return gtk_style_context_is_state_set (priv->context, state);
177 }
178
179 G_CONST_RETURN GtkWidgetPath *
180 gtk_theming_engine_get_path (GtkThemingEngine *engine)
181 {
182   GtkThemingEnginePrivate *priv;
183
184   g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);
185
186   priv = engine->priv;
187   return gtk_style_context_get_path (priv->context);
188 }
189
190 gboolean
191 gtk_theming_engine_has_class (GtkThemingEngine *engine,
192                               const gchar      *style_class)
193 {
194   GtkThemingEnginePrivate *priv;
195
196   g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
197
198   priv = engine->priv;
199   return gtk_style_context_has_class (priv->context, style_class);
200 }
201
202 gboolean
203 gtk_theming_engine_has_child_class (GtkThemingEngine   *engine,
204                                     const gchar        *style_class,
205                                     GtkChildClassFlags *flags)
206 {
207   GtkThemingEnginePrivate *priv;
208
209   if (flags)
210     *flags = 0;
211
212   g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
213
214   priv = engine->priv;
215   return gtk_style_context_has_child_class (priv->context, style_class, flags);
216 }
217
218 /* GtkThemingModule */
219
220 static gboolean
221 gtk_theming_module_load (GTypeModule *type_module)
222 {
223   GtkThemingModule *theming_module;
224   GModule *module;
225   gchar *name, *module_path;
226
227   theming_module = GTK_THEMING_MODULE (type_module);
228   name = theming_module->name;
229   module_path = _gtk_find_module (name, "theming-engines");
230
231   if (!module_path)
232     {
233       g_warning (_("Unable to locate theme engine in module path: \"%s\","), name);
234       return FALSE;
235     }
236
237   module = g_module_open (module_path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
238   g_free (module_path);
239
240   if (!module)
241     {
242       g_warning ("%s", g_module_error ());
243       return FALSE;
244     }
245
246   if (!g_module_symbol (module, "create_engine",
247                         (gpointer *) &theming_module->create_engine))
248     {
249       g_warning ("%s", g_module_error());
250       g_module_close (module);
251
252       return FALSE;
253     }
254
255   g_module_make_resident (module);
256
257   return TRUE;
258 }
259
260 static void
261 gtk_theming_module_class_init (GtkThemingModuleClass *klass)
262 {
263   GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (klass);
264
265   module_class->load = gtk_theming_module_load;
266 }
267
268 static void
269 gtk_theming_module_init (GtkThemingModule *module)
270 {
271 }
272
273 G_CONST_RETURN GtkThemingEngine *
274 gtk_theming_engine_load (const gchar *name)
275 {
276   static GHashTable *engines = NULL;
277   static GtkThemingEngine *default_engine;
278   GtkThemingEngine *engine = NULL;
279
280   if (name)
281     {
282       if (!engines)
283         engines = g_hash_table_new (g_str_hash, g_str_equal);
284
285       engine = g_hash_table_lookup (engines, name);
286
287       if (!engine)
288         {
289           GtkThemingModule *module;
290
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);
294
295           if (module && g_type_module_use (G_TYPE_MODULE (module)))
296             {
297               engine = (module->create_engine) ();
298
299               if (engine)
300                 g_hash_table_insert (engines, module->name, engine);
301             }
302         }
303     }
304
305   if (!engine)
306     {
307       if (!default_engine)
308         default_engine = g_object_new (GTK_TYPE_THEMING_ENGINE, NULL);
309
310       engine = default_engine;
311     }
312
313   return engine;
314 }
315
316 /* Paint method implementations */
317 static void
318 gtk_theming_engine_render_check (GtkThemingEngine *engine,
319                                  cairo_t          *cr,
320                                  gdouble           x,
321                                  gdouble           y,
322                                  gdouble           width,
323                                  gdouble           height)
324 {
325   GdkColor *fg_color, *base_color, *text_color;
326   const GtkWidgetPath *path;
327   GtkStateFlags flags;
328   GtkStateType state;
329
330   flags = gtk_theming_engine_get_state (engine);
331   path = gtk_theming_engine_get_path (engine);
332   cairo_save (cr);
333
334   if (flags & GTK_STATE_FLAG_PRELIGHT)
335     state = GTK_STATE_PRELIGHT;
336   else
337     state = GTK_STATE_NORMAL;
338
339   gtk_theming_engine_get (engine, state,
340                           "foreground-color", &fg_color,
341                           "base-color", &base_color,
342                           "text-color", &text_color,
343                           NULL);
344
345   if (!gtk_widget_path_has_parent (path, GTK_TYPE_MENU))
346     {
347       cairo_set_line_width (cr, 1);
348
349       cairo_rectangle (cr,
350                        x + 0.5, y + 0.5,
351                        width - 1, height - 1);
352
353       gdk_cairo_set_source_color (cr, base_color);
354       cairo_fill_preserve (cr);
355
356       if (gtk_widget_path_has_parent (path, GTK_TYPE_TREE_VIEW))
357         gdk_cairo_set_source_color (cr, text_color);
358       else
359         gdk_cairo_set_source_color (cr, fg_color);
360
361       cairo_stroke (cr);
362     }
363
364   cairo_set_line_width (cr, 1.5);
365   gdk_cairo_set_source_color (cr, text_color);
366
367   if (gtk_theming_engine_is_state_set (engine, GTK_STATE_INCONSISTENT))
368     {
369       cairo_move_to (cr, x + (width * 0.2), y + (height / 2));
370       cairo_line_to (cr, x + (width * 0.8), y + (height / 2));
371     }
372   else if (gtk_theming_engine_is_state_set (engine, GTK_STATE_ACTIVE))
373     {
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));
377     }
378
379   cairo_stroke (cr);
380
381   cairo_restore (cr);
382
383   gdk_color_free (fg_color);
384   gdk_color_free (base_color);
385   gdk_color_free (text_color);
386 }
387
388 static void
389 gtk_theming_engine_render_option (GtkThemingEngine *engine,
390                                   cairo_t          *cr,
391                                   gdouble           x,
392                                   gdouble           y,
393                                   gdouble           width,
394                                   gdouble           height)
395 {
396   GtkStateFlags flags;
397   GdkColor *base_color, *fg_color, *text_color;
398   const GtkWidgetPath *path;
399   GtkStateType state;
400   gdouble radius;
401
402   flags = gtk_theming_engine_get_state (engine);
403   path = gtk_theming_engine_get_path (engine);
404   radius = MIN (width, height) / 2 - 0.5;
405
406   cairo_save (cr);
407   cairo_set_line_width (cr, 1);
408
409   if (flags & GTK_STATE_FLAG_PRELIGHT)
410     state = GTK_STATE_PRELIGHT;
411   else
412     state = GTK_STATE_NORMAL;
413
414   gtk_theming_engine_get (engine, state,
415                           "foreground-color", &fg_color,
416                           "base-color", &base_color,
417                           "text-color", &text_color,
418                           NULL);
419
420   if (!gtk_widget_path_has_parent (path, GTK_TYPE_MENU))
421     {
422       cairo_arc (cr,
423                  x + (width / 2),
424                  y + (height / 2),
425                  radius,
426                  0, 2 * G_PI);
427
428       gdk_cairo_set_source_color (cr, base_color);
429       cairo_fill_preserve (cr);
430
431       if (gtk_widget_path_has_parent (path, GTK_TYPE_TREE_VIEW))
432         gdk_cairo_set_source_color (cr, text_color);
433       else
434         gdk_cairo_set_source_color (cr, fg_color);
435
436       cairo_stroke (cr);
437     }
438
439   if (gtk_widget_path_has_parent (path, GTK_TYPE_MENU))
440     gdk_cairo_set_source_color (cr, fg_color);
441   else
442     gdk_cairo_set_source_color (cr, text_color);
443
444   if (gtk_theming_engine_is_state_set (engine, GTK_STATE_INCONSISTENT))
445     {
446       cairo_move_to (cr, x + (width * 0.2), y + (height / 2));
447       cairo_line_to (cr, x + (width * 0.8), y + (height / 2));
448       cairo_stroke (cr);
449     }
450   if (gtk_theming_engine_is_state_set (engine, GTK_STATE_ACTIVE))
451     {
452       cairo_arc (cr,
453                  x + (width / 2),
454                  y + (height / 2),
455                  radius / 2,
456                  0, 2 * G_PI);
457
458       cairo_fill (cr);
459     }
460
461   cairo_restore (cr);
462 }
463
464 #define __GTK_THEMING_ENGINE_C__
465 #include "gtkaliasdef.c"