]> Pileus Git - ~andy/gtk/blob - gtk/gtkthemingengine.c
GtkThemingEngine: Add vmethod to render arrows.
[~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 static void gtk_theming_engine_render_arrow  (GtkThemingEngine *engine,
52                                               cairo_t          *cr,
53                                               gdouble           angle,
54                                               gdouble           x,
55                                               gdouble           y,
56                                               gdouble           size);
57
58 G_DEFINE_TYPE (GtkThemingEngine, gtk_theming_engine, G_TYPE_OBJECT)
59
60
61 typedef struct GtkThemingModule GtkThemingModule;
62 typedef struct GtkThemingModuleClass GtkThemingModuleClass;
63
64 struct GtkThemingModule
65 {
66   GTypeModule parent_instance;
67   gchar *name;
68
69   GtkThemingEngine * (*create_engine) (void);
70 };
71
72 struct GtkThemingModuleClass
73 {
74   GTypeModuleClass parent_class;
75 };
76
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))
80
81 G_DEFINE_TYPE (GtkThemingModule, gtk_theming_module, G_TYPE_TYPE_MODULE);
82
83 static void
84 gtk_theming_engine_class_init (GtkThemingEngineClass *klass)
85 {
86   GObjectClass *object_class = G_OBJECT_CLASS (klass);
87
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;
91
92   g_type_class_add_private (object_class, sizeof (GtkThemingEnginePrivate));
93 }
94
95 static void
96 gtk_theming_engine_init (GtkThemingEngine *engine)
97 {
98   engine->priv = GTK_THEMING_ENGINE_GET_PRIVATE (engine);
99 }
100
101 void
102 _gtk_theming_engine_set_context (GtkThemingEngine *engine,
103                                  GtkStyleContext  *context)
104 {
105   GtkThemingEnginePrivate *priv;
106
107   g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
108   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
109
110   priv = engine->priv;
111   priv->context = context;
112 }
113
114 void
115 gtk_theming_engine_get_property (GtkThemingEngine *engine,
116                                  const gchar      *property,
117                                  GtkStateType      state,
118                                  GValue           *value)
119 {
120   GtkThemingEnginePrivate *priv;
121
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);
126
127   priv = engine->priv;
128   gtk_style_context_get_property (priv->context, property, state, value);
129 }
130
131 void
132 gtk_theming_engine_get_valist (GtkThemingEngine *engine,
133                                GtkStateType      state,
134                                va_list           args)
135 {
136   GtkThemingEnginePrivate *priv;
137
138   g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
139   g_return_if_fail (state < GTK_STATE_LAST);
140
141   priv = engine->priv;
142   gtk_style_context_get_valist (priv->context, state, args);
143 }
144
145 void
146 gtk_theming_engine_get (GtkThemingEngine *engine,
147                         GtkStateType      state,
148                         ...)
149 {
150   GtkThemingEnginePrivate *priv;
151   va_list args;
152
153   g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
154   g_return_if_fail (state < GTK_STATE_LAST);
155
156   priv = engine->priv;
157
158   va_start (args, state);
159   gtk_style_context_get_valist (priv->context, state, args);
160   va_end (args);
161 }
162
163 GtkStateFlags
164 gtk_theming_engine_get_state (GtkThemingEngine *engine)
165 {
166   GtkThemingEnginePrivate *priv;
167
168   g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), 0);
169
170   priv = engine->priv;
171   return gtk_style_context_get_state (priv->context);
172 }
173
174 gboolean
175 gtk_theming_engine_is_state_set (GtkThemingEngine *engine,
176                                  GtkStateType      state)
177 {
178   GtkThemingEnginePrivate *priv;
179
180   g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), 0);
181
182   priv = engine->priv;
183   return gtk_style_context_is_state_set (priv->context, state);
184 }
185
186 G_CONST_RETURN GtkWidgetPath *
187 gtk_theming_engine_get_path (GtkThemingEngine *engine)
188 {
189   GtkThemingEnginePrivate *priv;
190
191   g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);
192
193   priv = engine->priv;
194   return gtk_style_context_get_path (priv->context);
195 }
196
197 gboolean
198 gtk_theming_engine_has_class (GtkThemingEngine *engine,
199                               const gchar      *style_class)
200 {
201   GtkThemingEnginePrivate *priv;
202
203   g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
204
205   priv = engine->priv;
206   return gtk_style_context_has_class (priv->context, style_class);
207 }
208
209 gboolean
210 gtk_theming_engine_has_child_class (GtkThemingEngine   *engine,
211                                     const gchar        *style_class,
212                                     GtkChildClassFlags *flags)
213 {
214   GtkThemingEnginePrivate *priv;
215
216   if (flags)
217     *flags = 0;
218
219   g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
220
221   priv = engine->priv;
222   return gtk_style_context_has_child_class (priv->context, style_class, flags);
223 }
224
225 /* GtkThemingModule */
226
227 static gboolean
228 gtk_theming_module_load (GTypeModule *type_module)
229 {
230   GtkThemingModule *theming_module;
231   GModule *module;
232   gchar *name, *module_path;
233
234   theming_module = GTK_THEMING_MODULE (type_module);
235   name = theming_module->name;
236   module_path = _gtk_find_module (name, "theming-engines");
237
238   if (!module_path)
239     {
240       g_warning (_("Unable to locate theme engine in module path: \"%s\","), name);
241       return FALSE;
242     }
243
244   module = g_module_open (module_path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
245   g_free (module_path);
246
247   if (!module)
248     {
249       g_warning ("%s", g_module_error ());
250       return FALSE;
251     }
252
253   if (!g_module_symbol (module, "create_engine",
254                         (gpointer *) &theming_module->create_engine))
255     {
256       g_warning ("%s", g_module_error());
257       g_module_close (module);
258
259       return FALSE;
260     }
261
262   g_module_make_resident (module);
263
264   return TRUE;
265 }
266
267 static void
268 gtk_theming_module_class_init (GtkThemingModuleClass *klass)
269 {
270   GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (klass);
271
272   module_class->load = gtk_theming_module_load;
273 }
274
275 static void
276 gtk_theming_module_init (GtkThemingModule *module)
277 {
278 }
279
280 G_CONST_RETURN GtkThemingEngine *
281 gtk_theming_engine_load (const gchar *name)
282 {
283   static GHashTable *engines = NULL;
284   static GtkThemingEngine *default_engine;
285   GtkThemingEngine *engine = NULL;
286
287   if (name)
288     {
289       if (!engines)
290         engines = g_hash_table_new (g_str_hash, g_str_equal);
291
292       engine = g_hash_table_lookup (engines, name);
293
294       if (!engine)
295         {
296           GtkThemingModule *module;
297
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);
301
302           if (module && g_type_module_use (G_TYPE_MODULE (module)))
303             {
304               engine = (module->create_engine) ();
305
306               if (engine)
307                 g_hash_table_insert (engines, module->name, engine);
308             }
309         }
310     }
311
312   if (!engine)
313     {
314       if (!default_engine)
315         default_engine = g_object_new (GTK_TYPE_THEMING_ENGINE, NULL);
316
317       engine = default_engine;
318     }
319
320   return engine;
321 }
322
323 /* Paint method implementations */
324 static void
325 gtk_theming_engine_render_check (GtkThemingEngine *engine,
326                                  cairo_t          *cr,
327                                  gdouble           x,
328                                  gdouble           y,
329                                  gdouble           width,
330                                  gdouble           height)
331 {
332   GdkColor *fg_color, *base_color, *text_color;
333   const GtkWidgetPath *path;
334   GtkStateFlags flags;
335   GtkStateType state;
336
337   flags = gtk_theming_engine_get_state (engine);
338   path = gtk_theming_engine_get_path (engine);
339   cairo_save (cr);
340
341   if (flags & GTK_STATE_FLAG_PRELIGHT)
342     state = GTK_STATE_PRELIGHT;
343   else
344     state = GTK_STATE_NORMAL;
345
346   gtk_theming_engine_get (engine, state,
347                           "foreground-color", &fg_color,
348                           "base-color", &base_color,
349                           "text-color", &text_color,
350                           NULL);
351
352   if (!gtk_widget_path_has_parent (path, GTK_TYPE_MENU))
353     {
354       cairo_set_line_width (cr, 1);
355
356       cairo_rectangle (cr,
357                        x + 0.5, y + 0.5,
358                        width - 1, height - 1);
359
360       gdk_cairo_set_source_color (cr, base_color);
361       cairo_fill_preserve (cr);
362
363       if (gtk_widget_path_has_parent (path, GTK_TYPE_TREE_VIEW))
364         gdk_cairo_set_source_color (cr, text_color);
365       else
366         gdk_cairo_set_source_color (cr, fg_color);
367
368       cairo_stroke (cr);
369     }
370
371   cairo_set_line_width (cr, 1.5);
372   gdk_cairo_set_source_color (cr, text_color);
373
374   if (gtk_theming_engine_is_state_set (engine, GTK_STATE_INCONSISTENT))
375     {
376       cairo_move_to (cr, x + (width * 0.2), y + (height / 2));
377       cairo_line_to (cr, x + (width * 0.8), y + (height / 2));
378     }
379   else if (gtk_theming_engine_is_state_set (engine, GTK_STATE_ACTIVE))
380     {
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));
384     }
385
386   cairo_stroke (cr);
387
388   cairo_restore (cr);
389
390   gdk_color_free (fg_color);
391   gdk_color_free (base_color);
392   gdk_color_free (text_color);
393 }
394
395 static void
396 gtk_theming_engine_render_option (GtkThemingEngine *engine,
397                                   cairo_t          *cr,
398                                   gdouble           x,
399                                   gdouble           y,
400                                   gdouble           width,
401                                   gdouble           height)
402 {
403   GtkStateFlags flags;
404   GdkColor *base_color, *fg_color, *text_color;
405   const GtkWidgetPath *path;
406   GtkStateType state;
407   gdouble radius;
408
409   flags = gtk_theming_engine_get_state (engine);
410   path = gtk_theming_engine_get_path (engine);
411   radius = MIN (width, height) / 2 - 0.5;
412
413   cairo_save (cr);
414   cairo_set_line_width (cr, 1);
415
416   if (flags & GTK_STATE_FLAG_PRELIGHT)
417     state = GTK_STATE_PRELIGHT;
418   else
419     state = GTK_STATE_NORMAL;
420
421   gtk_theming_engine_get (engine, state,
422                           "foreground-color", &fg_color,
423                           "base-color", &base_color,
424                           "text-color", &text_color,
425                           NULL);
426
427   if (!gtk_widget_path_has_parent (path, GTK_TYPE_MENU))
428     {
429       cairo_arc (cr,
430                  x + (width / 2),
431                  y + (height / 2),
432                  radius,
433                  0, 2 * G_PI);
434
435       gdk_cairo_set_source_color (cr, base_color);
436       cairo_fill_preserve (cr);
437
438       if (gtk_widget_path_has_parent (path, GTK_TYPE_TREE_VIEW))
439         gdk_cairo_set_source_color (cr, text_color);
440       else
441         gdk_cairo_set_source_color (cr, fg_color);
442
443       cairo_stroke (cr);
444     }
445
446   if (gtk_widget_path_has_parent (path, GTK_TYPE_MENU))
447     gdk_cairo_set_source_color (cr, fg_color);
448   else
449     gdk_cairo_set_source_color (cr, text_color);
450
451   if (gtk_theming_engine_is_state_set (engine, GTK_STATE_INCONSISTENT))
452     {
453       cairo_move_to (cr, x + (width * 0.2), y + (height / 2));
454       cairo_line_to (cr, x + (width * 0.8), y + (height / 2));
455       cairo_stroke (cr);
456     }
457   if (gtk_theming_engine_is_state_set (engine, GTK_STATE_ACTIVE))
458     {
459       cairo_arc (cr,
460                  x + (width / 2),
461                  y + (height / 2),
462                  radius / 2,
463                  0, 2 * G_PI);
464
465       cairo_fill (cr);
466     }
467
468   cairo_restore (cr);
469 }
470
471 static void
472 add_path_arrow (cairo_t *cr,
473                 gdouble  angle,
474                 gdouble  x,
475                 gdouble  y,
476                 gdouble  size)
477 {
478   cairo_save (cr);
479
480   cairo_translate (cr, x + (size / 2), y + (size / 2));
481   cairo_rotate (cr, angle);
482
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);
487
488   cairo_restore (cr);
489 }
490
491 static void
492 gtk_theming_engine_render_arrow (GtkThemingEngine *engine,
493                                  cairo_t          *cr,
494                                  gdouble           angle,
495                                  gdouble           x,
496                                  gdouble           y,
497                                  gdouble           size)
498 {
499   GtkStateFlags flags;
500   GtkStateType state;
501   GdkColor *fg_color;
502
503   cairo_save (cr);
504
505   flags = gtk_theming_engine_get_state (engine);
506
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;
511   else
512     state = GTK_STATE_NORMAL;
513
514   gtk_theming_engine_get (engine, state,
515                           "foreground-color", &fg_color,
516                           NULL);
517
518   if (flags & GTK_STATE_FLAG_INSENSITIVE)
519     {
520       add_path_arrow (cr, angle, x + 1, y + 1, size);
521       cairo_set_source_rgb (cr, 1, 1, 1);
522       cairo_fill (cr);
523     }
524
525   add_path_arrow (cr, angle, x, y, size);
526   gdk_cairo_set_source_color (cr, fg_color);
527   cairo_fill (cr);
528
529   cairo_restore (cr);
530
531   gdk_color_free (fg_color);
532 }
533
534 #define __GTK_THEMING_ENGINE_C__
535 #include "gtkaliasdef.c"