]> Pileus Git - ~andy/gtk/blob - gtk/gtkthemes.c
add gen_8_3_dll_name()
[~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[1024];
82        gchar *engine_path;
83        GModule *library;
84       
85 #ifndef __EMX__
86        g_snprintf (fullname, 1024, "lib%s.so", name);
87 #else
88        gen_8_3_dll_name(name, fullname);
89 #endif
90        engine_path = gtk_rc_find_module_in_path (fullname);
91 #ifdef __EMX__
92        if (!engine_path)
93          {
94            /* try theme name without prefix '_' */
95            memmove(fullname, fullname + 1, strlen(fullname));
96            engine_path = gtk_rc_find_module_in_path (fullname);
97          }
98 #endif
99
100        if (!engine_path)
101          {
102            g_warning (_("Unable to locate loadable module in module_path: \"%s\","),
103                       fullname);
104            
105            return NULL;
106          }
107        
108        /* load the lib */
109
110        GTK_NOTE (MISC, g_message ("Loading Theme %s\n", engine_path));
111        
112        library = g_module_open (engine_path, 0);
113        g_free(engine_path);
114        if (!library)
115          {
116            g_warning (g_module_error());
117            return NULL;
118          }
119        else
120          {
121             result = g_new (GtkThemeEnginePrivate, 1);
122             
123             result->refcount = 1;
124             result->name = g_strdup (name);
125             result->library = library;
126             
127             /* extract symbols from the lib */
128             if (!g_module_symbol (library, "theme_init",
129                                   (gpointer *)&result->init) ||
130                 !g_module_symbol (library, "theme_exit", 
131                                   (gpointer *)&result->exit))
132               {
133                 g_warning (g_module_error());
134                 g_free (result);
135                 return NULL;
136               }
137             
138             /* call the theme's init (theme_init) function to let it */
139             /* setup anything it needs to set up. */
140             result->init((GtkThemeEngine *)result);
141             
142             g_hash_table_insert (engine_hash, result->name, result);
143          }
144     }
145   else
146     result->refcount++;
147
148   return (GtkThemeEngine *)result;
149 }
150
151 void
152 gtk_theme_engine_ref (GtkThemeEngine *engine)
153 {
154   g_return_if_fail (engine != NULL);
155   
156   ((GtkThemeEnginePrivate *)engine)->refcount++;
157 }
158
159 void
160 gtk_theme_engine_unref (GtkThemeEngine *engine)
161 {
162   GtkThemeEnginePrivate *private;
163
164   g_return_if_fail (engine != NULL);
165
166   private = (GtkThemeEnginePrivate *)engine;
167   private->refcount--;
168
169   if (private->refcount == 0)
170     {
171       g_hash_table_remove (engine_hash, private->name);
172       
173       g_module_close (private->library);
174       g_free (private->name);
175       g_free (private);
176     }
177 }