]> Pileus Git - ~andy/gtk/blob - gtk/gtkthemes.c
1362552c68f389065407eda28cb423cac6253c4d
[~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 <config.h>
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <gmodule.h>
33 #include "gtkthemes.h"
34 #include "gtkrc.h"
35 #include "gtkintl.h"
36 #include "gtkalias.h"
37
38 typedef struct _GtkThemeEngineClass GtkThemeEngineClass;
39
40 struct _GtkThemeEngine
41 {
42   GTypeModule parent_instance;
43   
44   GModule *library;
45
46   void (*init) (GTypeModule *);
47   void (*exit) (void);
48   GtkRcStyle *(*create_rc_style) ();
49
50   gchar *name;
51 };
52
53 struct _GtkThemeEngineClass
54 {
55   GTypeModuleClass parent_class;
56 };
57
58 static GHashTable *engine_hash = NULL;
59
60 static gboolean
61 gtk_theme_engine_load (GTypeModule *module)
62 {
63   GtkThemeEngine *engine = GTK_THEME_ENGINE (module);
64   
65   gchar *engine_path;
66       
67   engine_path = gtk_rc_find_module_in_path (engine->name);
68   
69   if (!engine_path)
70     {
71       g_warning (_("Unable to locate theme engine in module_path: \"%s\","),
72                  engine->name);
73       return FALSE;
74     }
75     
76   /* load the lib */
77   
78   GTK_NOTE (MISC, g_message ("Loading Theme %s\n", engine_path));
79        
80   engine->library = g_module_open (engine_path, G_MODULE_BIND_LAZY);
81   g_free(engine_path);
82   if (!engine->library)
83     {
84       g_warning (g_module_error());
85       return FALSE;
86     }
87   
88   /* extract symbols from the lib */
89   if (!g_module_symbol (engine->library, "theme_init",
90                         (gpointer *)&engine->init) ||
91       !g_module_symbol (engine->library, "theme_exit", 
92                         (gpointer *)&engine->exit) ||
93       !g_module_symbol (engine->library, "theme_create_rc_style", 
94                         (gpointer *)&engine->create_rc_style))
95     {
96       g_warning (g_module_error());
97       g_module_close (engine->library);
98       
99       return FALSE;
100     }
101             
102   /* call the theme's init (theme_init) function to let it */
103   /* setup anything it needs to set up. */
104   engine->init (module);
105
106   return TRUE;
107 }
108
109 static void
110 gtk_theme_engine_unload (GTypeModule *module)
111 {
112   GtkThemeEngine *engine = GTK_THEME_ENGINE (module);
113
114   engine->exit();
115
116   g_module_close (engine->library);
117   engine->library = NULL;
118
119   engine->init = NULL;
120   engine->exit = NULL;
121   engine->create_rc_style = NULL;
122 }
123
124 static void
125 gtk_theme_engine_class_init (GtkThemeEngineClass *class)
126 {
127   GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (class);
128
129   module_class->load = gtk_theme_engine_load;
130   module_class->unload = gtk_theme_engine_unload;
131 }
132
133 GType
134 gtk_theme_engine_get_type (void)
135 {
136   static GType theme_engine_type = 0;
137
138   if (!theme_engine_type)
139     {
140       static const GTypeInfo theme_engine_info = {
141         sizeof (GtkThemeEngineClass),
142         NULL,           /* base_init */
143         NULL,           /* base_finalize */
144         (GClassInitFunc) gtk_theme_engine_class_init,
145         NULL,           /* class_finalize */
146         NULL,           /* class_data */
147         sizeof (GtkThemeEngine),
148         0,              /* n_preallocs */
149         NULL,           /* instance_init */
150       };
151
152       theme_engine_type =
153         g_type_register_static (G_TYPE_TYPE_MODULE, I_("GtkThemeEngine"),
154                                 &theme_engine_info, 0);
155     }
156   
157   return theme_engine_type;
158 }
159
160 GtkThemeEngine*
161 gtk_theme_engine_get (const gchar *name)
162 {
163   GtkThemeEngine *result;
164   
165   if (!engine_hash)
166     engine_hash = g_hash_table_new (g_str_hash, g_str_equal);
167
168   /* get the library name for the theme
169    */
170   result = g_hash_table_lookup (engine_hash, name);
171
172   if (!result)
173     {
174       result = g_object_new (GTK_TYPE_THEME_ENGINE, NULL);
175       g_type_module_set_name (G_TYPE_MODULE (result), name);
176       result->name = g_strdup (name);
177
178       g_hash_table_insert (engine_hash, result->name, result);
179     }
180
181   if (!g_type_module_use (G_TYPE_MODULE (result)))
182     return NULL;
183
184   return result;
185 }
186
187 GtkRcStyle *
188 gtk_theme_engine_create_rc_style (GtkThemeEngine *engine)
189 {
190   g_return_val_if_fail (engine != NULL, NULL);
191
192   return engine->create_rc_style ();
193 }
194
195 #define __GTK_THEMES_C__
196 #include "gtkaliasdef.c"