]> Pileus Git - ~andy/gtk/blob - gtk/gtkthemes.c
New static function to set the background of all windows.
[~andy/gtk] / gtk / gtkthemes.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * Themes added by The Rasterman <raster@redhat.com>
5  * 
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 /*
22  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
23  * file for a list of people on the GTK+ Team.  See the ChangeLog
24  * files for a list of changes.  These files are distributed with
25  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
26  */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <gmodule.h>
31 #include "gtkthemes.h"
32 #include "gtkrc.h"
33 #include "config.h"
34 #include "gtkintl.h"
35
36 typedef struct _GtkThemeEngineClass GtkThemeEngineClass;
37
38 struct _GtkThemeEngine
39 {
40   GTypeModule parent_instance;
41   
42   GModule *library;
43
44   void (*init) (GTypeModule *);
45   void (*exit) (void);
46   GtkRcStyle *(*create_rc_style) ();
47
48   gchar *name;
49 };
50
51 struct _GtkThemeEngineClass
52 {
53   GTypeModuleClass parent_class;
54 };
55
56 static GHashTable *engine_hash = NULL;
57
58 static gboolean
59 gtk_theme_engine_load (GTypeModule *module)
60 {
61   GtkThemeEngine *engine = GTK_THEME_ENGINE (module);
62   
63   gchar *engine_path;
64       
65   engine_path = gtk_rc_find_module_in_path (engine->name);
66   
67   if (!engine_path)
68     {
69       g_warning (_("Unable to locate theme engine in module_path: \"%s\","),
70                  engine->name);
71       return FALSE;
72     }
73     
74   /* load the lib */
75   
76   GTK_NOTE (MISC, g_message ("Loading Theme %s\n", engine_path));
77        
78   engine->library = g_module_open (engine_path, 0);
79   g_free(engine_path);
80   if (!engine->library)
81     {
82       g_warning (g_module_error());
83       return FALSE;
84     }
85   
86   /* extract symbols from the lib */
87   if (!g_module_symbol (engine->library, "theme_init",
88                         (gpointer *)&engine->init) ||
89       !g_module_symbol (engine->library, "theme_exit", 
90                         (gpointer *)&engine->exit) ||
91       !g_module_symbol (engine->library, "theme_create_rc_style", 
92                         (gpointer *)&engine->create_rc_style))
93     {
94       g_warning (g_module_error());
95       g_module_close (engine->library);
96       
97       return FALSE;
98     }
99             
100   /* call the theme's init (theme_init) function to let it */
101   /* setup anything it needs to set up. */
102   engine->init (module);
103
104   return TRUE;
105 }
106
107 static void
108 gtk_theme_engine_unload (GTypeModule *module)
109 {
110   GtkThemeEngine *engine = GTK_THEME_ENGINE (module);
111
112   engine->exit();
113
114   g_module_close (engine->library);
115   engine->library = NULL;
116
117   engine->init = NULL;
118   engine->exit = NULL;
119   engine->create_rc_style = NULL;
120 }
121
122 static void
123 gtk_theme_engine_class_init (GtkThemeEngineClass *class)
124 {
125   GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (class);
126
127   module_class->load = gtk_theme_engine_load;
128   module_class->unload = gtk_theme_engine_unload;
129 }
130
131 GType
132 gtk_theme_engine_get_type (void)
133 {
134   static GType theme_engine_type = 0;
135
136   if (!theme_engine_type)
137     {
138       static const GTypeInfo theme_engine_info = {
139         sizeof (GtkThemeEngineClass),
140         NULL,           /* base_init */
141         NULL,           /* base_finalize */
142         (GClassInitFunc) gtk_theme_engine_class_init,
143         NULL,           /* class_finalize */
144         NULL,           /* class_data */
145         sizeof (GtkThemeEngine),
146         0,              /* n_preallocs */
147         NULL,           /* instance_init */
148       };
149
150       theme_engine_type =
151         g_type_register_static (G_TYPE_TYPE_MODULE, "GtkThemeEngine",
152                                 &theme_engine_info, 0);
153     }
154   
155   return theme_engine_type;
156 }
157
158 GtkThemeEngine*
159 gtk_theme_engine_get (const gchar *name)
160 {
161   GtkThemeEngine *result;
162   
163   if (!engine_hash)
164     engine_hash = g_hash_table_new (g_str_hash, g_str_equal);
165
166   /* get the library name for the theme
167    */
168   result = g_hash_table_lookup (engine_hash, name);
169
170   if (!result)
171     {
172       result = g_object_new (GTK_TYPE_THEME_ENGINE, NULL);
173       g_type_module_set_name (G_TYPE_MODULE (result), name);
174       result->name = g_strdup (name);
175
176       g_hash_table_insert (engine_hash, result->name, result);
177     }
178
179   if (!g_type_module_use (G_TYPE_MODULE (result)))
180     return NULL;
181
182   return result;
183 }
184
185 GtkRcStyle *
186 gtk_theme_engine_create_rc_style (GtkThemeEngine *engine)
187 {
188   g_return_val_if_fail (engine != NULL, NULL);
189
190   return engine->create_rc_style ();
191 }