]> Pileus Git - ~andy/gtk/blob - gtk/gtkthemes.c
4a5d484b06ca8c3f8c79b3da12aa92b8dac9078d
[~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
21 /*
22  * Modified by the GTK+ Team and others 1997-1999.  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 "gtkmain.h"
33 #include "gtkrc.h"
34 #include "gtkselection.h"
35 #include "gtksignal.h"
36 #include "gtkwidget.h"
37 #include "config.h"
38 #include "gtkintl.h"
39
40 typedef struct _GtkThemeEnginePrivate GtkThemeEnginePrivate;
41
42 struct _GtkThemeEnginePrivate {
43   GtkThemeEngine engine;
44   
45   GModule *library;
46   void *name;
47
48   void (*init) (GtkThemeEngine *);
49   void (*exit) (void);
50
51   guint refcount;
52 };
53
54 static GHashTable *engine_hash = NULL;
55
56 #ifdef __EMX__
57 static void gen_8_3_dll_name(gchar *name, gchar *fullname)
58 {
59     /* 8.3 dll filename restriction */
60     fullname[0] = '_';
61     strncpy (fullname + 1, name, 7);
62     fullname[8] = '\0';
63     strcat (fullname, ".dll");
64 }                                                       
65 #endif
66
67 GtkThemeEngine*
68 gtk_theme_engine_get (gchar *name)
69 {
70   GtkThemeEnginePrivate *result;
71   
72   if (!engine_hash)
73     engine_hash = g_hash_table_new (g_str_hash, g_str_equal);
74
75   /* get the library name for the theme */
76   
77   result = g_hash_table_lookup (engine_hash, name);
78
79   if (!result)
80     {
81        gchar *fullname;
82        gchar *engine_path;
83        GModule *library;
84       
85 #ifndef __EMX__
86        fullname = g_module_build_path (NULL, name);
87 #else
88        fullname = g_malloc (13);
89        gen_8_3_dll_name(name, fullname);
90 #endif
91        engine_path = gtk_rc_find_module_in_path (fullname);
92 #ifdef __EMX__
93        if (!engine_path)
94          {
95            /* try theme name without prefix '_' */
96            memmove(fullname, fullname + 1, strlen(fullname));
97            engine_path = gtk_rc_find_module_in_path (fullname);
98          }
99 #endif
100
101        if (!engine_path)
102          {
103            g_warning (_("Unable to locate loadable module in module_path: \"%s\","),
104                       fullname);
105            
106            g_free (fullname);
107            return NULL;
108          }
109        g_free (fullname);
110        
111        /* load the lib */
112
113        GTK_NOTE (MISC, g_message ("Loading Theme %s\n", engine_path));
114        
115        library = g_module_open (engine_path, 0);
116        g_free(engine_path);
117        if (!library)
118          {
119            g_warning (g_module_error());
120            return NULL;
121          }
122        else
123          {
124             result = g_new (GtkThemeEnginePrivate, 1);
125             
126             result->refcount = 1;
127             result->name = g_strdup (name);
128             result->library = library;
129             
130             /* extract symbols from the lib */
131             if (!g_module_symbol (library, "theme_init",
132                                   (gpointer *)&result->init) ||
133                 !g_module_symbol (library, "theme_exit", 
134                                   (gpointer *)&result->exit))
135               {
136                 g_warning (g_module_error());
137                 g_free (result);
138                 return NULL;
139               }
140             
141             /* call the theme's init (theme_init) function to let it */
142             /* setup anything it needs to set up. */
143             result->init((GtkThemeEngine *)result);
144             
145             g_hash_table_insert (engine_hash, result->name, result);
146          }
147     }
148   else
149     result->refcount++;
150
151   return (GtkThemeEngine *)result;
152 }
153
154 void
155 gtk_theme_engine_ref (GtkThemeEngine *engine)
156 {
157   g_return_if_fail (engine != NULL);
158   
159   ((GtkThemeEnginePrivate *)engine)->refcount++;
160 }
161
162 void
163 gtk_theme_engine_unref (GtkThemeEngine *engine)
164 {
165   GtkThemeEnginePrivate *private;
166   private = (GtkThemeEnginePrivate *)engine;
167
168   g_return_if_fail (engine != NULL);
169   g_return_if_fail (private->refcount > 0);
170
171   private->refcount--;
172
173   if (private->refcount == 0)
174     {
175       private->exit();
176       
177       g_hash_table_remove (engine_hash, private->name);
178       
179       g_module_close (private->library);
180       g_free (private->name);
181       g_free (private);
182     }
183 }