]> Pileus Git - ~andy/gtk/blob - gtk/gtkthemes.c
c0510d0461bce8badaeebc4a9a027c720ab55ae4
[~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 Library 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  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library 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 #include <stdio.h>
21 #include <stdlib.h>
22 #include <gmodule.h>
23 #include "gtkthemes.h"
24 #include "gtkmain.h"
25 #include "gtkrc.h"
26 #include "gtkselection.h"
27 #include "gtksignal.h"
28 #include "gtkwidget.h"
29 #include "gtkprivate.h"
30 #include "config.h"
31
32 typedef struct _GtkThemeEnginePrivate GtkThemeEnginePrivate;
33
34 struct _GtkThemeEnginePrivate {
35   GtkThemeEngine engine;
36   
37   GModule *library;
38   void *name;
39
40   void (*init) (GtkThemeEngine *);
41   void (*exit) (void);
42
43   guint refcount;
44 };
45
46 static GHashTable *engine_hash = NULL;
47
48 GtkThemeEngine*
49 gtk_theme_engine_get (gchar *name)
50 {
51   GtkThemeEnginePrivate *result;
52   
53   if (!engine_hash)
54     engine_hash = g_hash_table_new (g_str_hash, g_str_equal);
55
56   /* get the library name for the theme */
57   
58   result = g_hash_table_lookup (engine_hash, name);
59
60   if (!result)
61     {
62        gchar fullname[1024];
63        gchar *engine_path;
64        GModule *library;
65       
66        g_snprintf (fullname, 1024, "lib%s.so", name);
67        engine_path = gtk_rc_find_module_in_path (fullname);
68
69        if (!engine_path)
70          {
71            g_warning ("Unable to locate loadable module in module_path: \"%s\",",
72                       fullname);
73            
74            return NULL;
75          }
76        
77        /* load the lib */
78        
79        printf ("Loading Theme %s\n", engine_path);
80        
81        library = g_module_open (engine_path, 0);
82        g_free(engine_path);
83        if (!library)
84          g_error(g_module_error());
85        else
86          {
87             result = g_new (GtkThemeEnginePrivate, 1);
88             
89             result->refcount = 1;
90             result->name = g_strdup (name);
91             result->library = library;
92             
93             /* extract symbols from the lib */
94             if (!g_module_symbol (library, "theme_init",
95                                   (gpointer *)&result->init) ||
96                 !g_module_symbol (library, "theme_exit", 
97                                   (gpointer *)&result->exit))
98               {
99                 g_error (g_module_error());
100                 g_free (result);
101                 return NULL;
102               }
103             
104             /* call the theme's init (theme_init) function to let it */
105             /* setup anything it needs to set up. */
106             result->init((GtkThemeEngine *)result);
107             
108             g_hash_table_insert (engine_hash, result->name, result);
109          }
110     }
111   else
112     result->refcount++;
113
114   return (GtkThemeEngine *)result;
115 }
116
117 void
118 gtk_theme_engine_ref (GtkThemeEngine *engine)
119 {
120   g_return_if_fail (engine != NULL);
121   
122   ((GtkThemeEnginePrivate *)engine)->refcount++;
123 }
124
125 void
126 gtk_theme_engine_unref (GtkThemeEngine *engine)
127 {
128   GtkThemeEnginePrivate *private;
129
130   g_return_if_fail (engine != NULL);
131
132   private = (GtkThemeEnginePrivate *)engine;
133   private->refcount--;
134
135   if (private->refcount == 0)
136     {
137       g_hash_table_remove (engine_hash, private->name);
138       
139       g_module_close (private->library);
140       g_free (private->name);
141       g_free (private);
142     }
143 }