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