]> Pileus Git - ~andy/gtk/blob - gtk/gtkthemingengine.c
GtkThemingEngine: Add vmethod to render checkboxes.
[~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
46 G_DEFINE_TYPE (GtkThemingEngine, gtk_theming_engine, G_TYPE_OBJECT)
47
48
49 typedef struct GtkThemingModule GtkThemingModule;
50 typedef struct GtkThemingModuleClass GtkThemingModuleClass;
51
52 struct GtkThemingModule
53 {
54   GTypeModule parent_instance;
55   gchar *name;
56
57   GtkThemingEngine * (*create_engine) (void);
58 };
59
60 struct GtkThemingModuleClass
61 {
62   GTypeModuleClass parent_class;
63 };
64
65 #define GTK_TYPE_THEMING_MODULE  (gtk_theming_module_get_type ())
66 #define GTK_THEMING_MODULE(o)    (G_TYPE_CHECK_INSTANCE_CAST ((o), GTK_TYPE_THEMING_MODULE, GtkThemingModule))
67 #define GTK_IS_THEMING_MODULE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GTK_TYPE_THEMING_MODULE))
68
69 G_DEFINE_TYPE (GtkThemingModule, gtk_theming_module, G_TYPE_TYPE_MODULE);
70
71 static void
72 gtk_theming_engine_class_init (GtkThemingEngineClass *klass)
73 {
74   GObjectClass *object_class = G_OBJECT_CLASS (klass);
75
76   klass->render_check = gtk_theming_engine_render_check;
77
78   g_type_class_add_private (object_class, sizeof (GtkThemingEnginePrivate));
79 }
80
81 static void
82 gtk_theming_engine_init (GtkThemingEngine *engine)
83 {
84   engine->priv = GTK_THEMING_ENGINE_GET_PRIVATE (engine);
85 }
86
87 void
88 _gtk_theming_engine_set_context (GtkThemingEngine *engine,
89                                  GtkStyleContext  *context)
90 {
91   GtkThemingEnginePrivate *priv;
92
93   g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
94   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
95
96   priv = engine->priv;
97   priv->context = context;
98 }
99
100 void
101 gtk_theming_engine_get_property (GtkThemingEngine *engine,
102                                  const gchar      *property,
103                                  GtkStateType      state,
104                                  GValue           *value)
105 {
106   GtkThemingEnginePrivate *priv;
107
108   g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
109   g_return_if_fail (property != NULL);
110   g_return_if_fail (state < GTK_STATE_LAST);
111   g_return_if_fail (value != NULL);
112
113   priv = engine->priv;
114   gtk_style_context_get_property (priv->context, property, state, value);
115 }
116
117 void
118 gtk_theming_engine_get_valist (GtkThemingEngine *engine,
119                                GtkStateType      state,
120                                va_list           args)
121 {
122   GtkThemingEnginePrivate *priv;
123
124   g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
125   g_return_if_fail (state < GTK_STATE_LAST);
126
127   priv = engine->priv;
128   gtk_style_context_get_valist (priv->context, state, args);
129 }
130
131 void
132 gtk_theming_engine_get (GtkThemingEngine *engine,
133                         GtkStateType      state,
134                         ...)
135 {
136   GtkThemingEnginePrivate *priv;
137   va_list args;
138
139   g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
140   g_return_if_fail (state < GTK_STATE_LAST);
141
142   priv = engine->priv;
143
144   va_start (args, state);
145   gtk_style_context_get_valist (priv->context, state, args);
146   va_end (args);
147 }
148
149 GtkStateFlags
150 gtk_theming_engine_get_state (GtkThemingEngine *engine)
151 {
152   GtkThemingEnginePrivate *priv;
153
154   g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), 0);
155
156   priv = engine->priv;
157   return gtk_style_context_get_state (priv->context);
158 }
159
160 gboolean
161 gtk_theming_engine_is_state_set (GtkThemingEngine *engine,
162                                  GtkStateType      state)
163 {
164   GtkThemingEnginePrivate *priv;
165
166   g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), 0);
167
168   priv = engine->priv;
169   return gtk_style_context_is_state_set (priv->context, state);
170 }
171
172 G_CONST_RETURN GtkWidgetPath *
173 gtk_theming_engine_get_path (GtkThemingEngine *engine)
174 {
175   GtkThemingEnginePrivate *priv;
176
177   g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);
178
179   priv = engine->priv;
180   return gtk_style_context_get_path (priv->context);
181 }
182
183 gboolean
184 gtk_theming_engine_has_class (GtkThemingEngine *engine,
185                               const gchar      *style_class)
186 {
187   GtkThemingEnginePrivate *priv;
188
189   g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
190
191   priv = engine->priv;
192   return gtk_style_context_has_class (priv->context, style_class);
193 }
194
195 gboolean
196 gtk_theming_engine_has_child_class (GtkThemingEngine   *engine,
197                                     const gchar        *style_class,
198                                     GtkChildClassFlags *flags)
199 {
200   GtkThemingEnginePrivate *priv;
201
202   if (flags)
203     *flags = 0;
204
205   g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
206
207   priv = engine->priv;
208   return gtk_style_context_has_child_class (priv->context, style_class, flags);
209 }
210
211 /* GtkThemingModule */
212
213 static gboolean
214 gtk_theming_module_load (GTypeModule *type_module)
215 {
216   GtkThemingModule *theming_module;
217   GModule *module;
218   gchar *name, *module_path;
219
220   theming_module = GTK_THEMING_MODULE (type_module);
221   name = theming_module->name;
222   module_path = _gtk_find_module (name, "theming-engines");
223
224   if (!module_path)
225     {
226       g_warning (_("Unable to locate theme engine in module path: \"%s\","), name);
227       return FALSE;
228     }
229
230   module = g_module_open (module_path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
231   g_free (module_path);
232
233   if (!module)
234     {
235       g_warning ("%s", g_module_error ());
236       return FALSE;
237     }
238
239   if (!g_module_symbol (module, "create_engine",
240                         (gpointer *) &theming_module->create_engine))
241     {
242       g_warning ("%s", g_module_error());
243       g_module_close (module);
244
245       return FALSE;
246     }
247
248   g_module_make_resident (module);
249
250   return TRUE;
251 }
252
253 static void
254 gtk_theming_module_class_init (GtkThemingModuleClass *klass)
255 {
256   GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (klass);
257
258   module_class->load = gtk_theming_module_load;
259 }
260
261 static void
262 gtk_theming_module_init (GtkThemingModule *module)
263 {
264 }
265
266 G_CONST_RETURN GtkThemingEngine *
267 gtk_theming_engine_load (const gchar *name)
268 {
269   static GHashTable *engines = NULL;
270   static GtkThemingEngine *default_engine;
271   GtkThemingEngine *engine = NULL;
272
273   if (name)
274     {
275       if (!engines)
276         engines = g_hash_table_new (g_str_hash, g_str_equal);
277
278       engine = g_hash_table_lookup (engines, name);
279
280       if (!engine)
281         {
282           GtkThemingModule *module;
283
284           module = g_object_new (GTK_TYPE_THEMING_MODULE, NULL);
285           g_type_module_set_name (G_TYPE_MODULE (module), name);
286           module->name = g_strdup (name);
287
288           if (module && g_type_module_use (G_TYPE_MODULE (module)))
289             {
290               engine = (module->create_engine) ();
291
292               if (engine)
293                 g_hash_table_insert (engines, module->name, engine);
294             }
295         }
296     }
297
298   if (!engine)
299     {
300       if (!default_engine)
301         default_engine = g_object_new (GTK_TYPE_THEMING_ENGINE, NULL);
302
303       engine = default_engine;
304     }
305
306   return engine;
307 }
308
309 /* Paint method implementations */
310 static void
311 gtk_theming_engine_render_check (GtkThemingEngine *engine,
312                                  cairo_t          *cr,
313                                  gdouble           x,
314                                  gdouble           y,
315                                  gdouble           width,
316                                  gdouble           height)
317 {
318   GdkColor *fg_color, *base_color, *text_color;
319   const GtkWidgetPath *path;
320   GtkStateFlags flags;
321   GtkStateType state;
322
323   flags = gtk_theming_engine_get_state (engine);
324   path = gtk_theming_engine_get_path (engine);
325   cairo_save (cr);
326
327   if (flags & GTK_STATE_FLAG_PRELIGHT)
328     state = GTK_STATE_PRELIGHT;
329   else
330     state = GTK_STATE_NORMAL;
331
332   gtk_theming_engine_get (engine, state,
333                           "foreground-color", &fg_color,
334                           "base-color", &base_color,
335                           "text-color", &text_color,
336                           NULL);
337
338   if (!gtk_widget_path_has_parent (path, GTK_TYPE_MENU))
339     {
340       cairo_set_line_width (cr, 1);
341
342       cairo_rectangle (cr,
343                        x + 0.5, y + 0.5,
344                        width - 1, height - 1);
345
346       gdk_cairo_set_source_color (cr, base_color);
347       cairo_fill_preserve (cr);
348
349       if (gtk_widget_path_has_parent (path, GTK_TYPE_TREE_VIEW))
350         gdk_cairo_set_source_color (cr, text_color);
351       else
352         gdk_cairo_set_source_color (cr, fg_color);
353
354       cairo_stroke (cr);
355     }
356
357   cairo_set_line_width (cr, 1.5);
358   gdk_cairo_set_source_color (cr, text_color);
359
360   if (gtk_theming_engine_is_state_set (engine, GTK_STATE_INCONSISTENT))
361     {
362       cairo_move_to (cr, x + (width * 0.2), y + (height / 2));
363       cairo_line_to (cr, x + (width * 0.8), y + (height / 2));
364     }
365   else if (gtk_theming_engine_is_state_set (engine, GTK_STATE_ACTIVE))
366     {
367       cairo_move_to (cr, x + (width * 0.2), y + (height / 2));
368       cairo_line_to (cr, x + (width * 0.4), y + (height * 0.8));
369       cairo_line_to (cr, x + (width * 0.8), y + (height * 0.2));
370     }
371
372   cairo_stroke (cr);
373
374   cairo_restore (cr);
375
376   gdk_color_free (fg_color);
377   gdk_color_free (base_color);
378   gdk_color_free (text_color);
379 }
380
381 #define __GTK_THEMING_ENGINE_C__
382 #include "gtkaliasdef.c"