gtkthemes.c 4.79 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
2 3 4 5 6
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * Themes added by The Rasterman <raster@redhat.com>
 * 
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8 9 10 11 12 13
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * 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 18 19
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
20 21

/*
22
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
23 24 25 26 27
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

28
#include "config.h"
29

30 31 32 33 34
#include <stdio.h>
#include <stdlib.h>
#include <gmodule.h>
#include "gtkthemes.h"
#include "gtkrc.h"
35
#include "gtkintl.h"
36

37

Owen Taylor's avatar
Owen Taylor committed
38
typedef struct _GtkThemeEngineClass GtkThemeEngineClass;
39

40 41
struct _GtkThemeEngine
{
Owen Taylor's avatar
Owen Taylor committed
42 43
  GTypeModule parent_instance;
  
44 45
  GModule *library;

Owen Taylor's avatar
Owen Taylor committed
46
  void (*init) (GTypeModule *);
47
  void (*exit) (void);
48
  GtkRcStyle *(*create_rc_style) ();
49

50
  gchar *name;
Owen Taylor's avatar
Owen Taylor committed
51
};
52

Owen Taylor's avatar
Owen Taylor committed
53 54 55
struct _GtkThemeEngineClass
{
  GTypeModuleClass parent_class;
56 57 58 59
};

static GHashTable *engine_hash = NULL;

Owen Taylor's avatar
Owen Taylor committed
60 61
static gboolean
gtk_theme_engine_load (GTypeModule *module)
62
{
Owen Taylor's avatar
Owen Taylor committed
63
  GtkThemeEngine *engine = GTK_THEME_ENGINE (module);
64
  
Owen Taylor's avatar
Owen Taylor committed
65 66
  gchar *engine_path;
      
67
  engine_path = gtk_rc_find_module_in_path (engine->name);
68
  
Owen Taylor's avatar
Owen Taylor committed
69
  if (!engine_path)
70
    {
71
      g_warning (_("Unable to locate theme engine in module_path: \"%s\","),
72
		 engine->name);
Owen Taylor's avatar
Owen Taylor committed
73 74 75 76 77 78
      return FALSE;
    }
    
  /* load the lib */
  
  GTK_NOTE (MISC, g_message ("Loading Theme %s\n", engine_path));
79
       
80
  engine->library = g_module_open (engine_path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
Owen Taylor's avatar
Owen Taylor committed
81 82 83
  g_free(engine_path);
  if (!engine->library)
    {
84
      g_warning ("%s", g_module_error());
Owen Taylor's avatar
Owen Taylor committed
85
      return FALSE;
86 87
    }
  
Owen Taylor's avatar
Owen Taylor committed
88 89 90 91 92 93 94
  /* extract symbols from the lib */
  if (!g_module_symbol (engine->library, "theme_init",
			(gpointer *)&engine->init) ||
      !g_module_symbol (engine->library, "theme_exit", 
			(gpointer *)&engine->exit) ||
      !g_module_symbol (engine->library, "theme_create_rc_style", 
			(gpointer *)&engine->create_rc_style))
95
    {
96
      g_warning ("%s", g_module_error());
97
      g_module_close (engine->library);
Owen Taylor's avatar
Owen Taylor committed
98 99
      
      return FALSE;
100
    }
Owen Taylor's avatar
Owen Taylor committed
101 102 103 104
	    
  /* call the theme's init (theme_init) function to let it */
  /* setup anything it needs to set up. */
  engine->init (module);
105

Owen Taylor's avatar
Owen Taylor committed
106
  return TRUE;
107 108 109
}

static void
Owen Taylor's avatar
Owen Taylor committed
110
gtk_theme_engine_unload (GTypeModule *module)
111
{
Owen Taylor's avatar
Owen Taylor committed
112
  GtkThemeEngine *engine = GTK_THEME_ENGINE (module);
113

Owen Taylor's avatar
Owen Taylor committed
114
  engine->exit();
115

Owen Taylor's avatar
Owen Taylor committed
116 117
  g_module_close (engine->library);
  engine->library = NULL;
118

Owen Taylor's avatar
Owen Taylor committed
119 120 121
  engine->init = NULL;
  engine->exit = NULL;
  engine->create_rc_style = NULL;
122
}
Owen Taylor's avatar
Owen Taylor committed
123

124
static void
Owen Taylor's avatar
Owen Taylor committed
125
gtk_theme_engine_class_init (GtkThemeEngineClass *class)
126
{
Owen Taylor's avatar
Owen Taylor committed
127
  GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (class);
128

Owen Taylor's avatar
Owen Taylor committed
129 130
  module_class->load = gtk_theme_engine_load;
  module_class->unload = gtk_theme_engine_unload;
131 132 133
}

GType
Owen Taylor's avatar
Owen Taylor committed
134
gtk_theme_engine_get_type (void)
135
{
Owen Taylor's avatar
Owen Taylor committed
136
  static GType theme_engine_type = 0;
137

Owen Taylor's avatar
Owen Taylor committed
138
  if (!theme_engine_type)
139
    {
140
      const GTypeInfo theme_engine_info = {
Owen Taylor's avatar
Owen Taylor committed
141 142 143 144 145 146 147 148 149 150
        sizeof (GtkThemeEngineClass),
        NULL,           /* base_init */
        NULL,           /* base_finalize */
        (GClassInitFunc) gtk_theme_engine_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data */
        sizeof (GtkThemeEngine),
        0,              /* n_preallocs */
        NULL,           /* instance_init */
      };
151

Manish Singh's avatar
Manish Singh committed
152
      theme_engine_type =
153
	g_type_register_static (G_TYPE_TYPE_MODULE, I_("GtkThemeEngine"),
Manish Singh's avatar
Manish Singh committed
154
				&theme_engine_info, 0);
155 156
    }
  
Owen Taylor's avatar
Owen Taylor committed
157 158
  return theme_engine_type;
}
159

Owen Taylor's avatar
Owen Taylor committed
160 161 162 163 164 165 166
GtkThemeEngine*
gtk_theme_engine_get (const gchar *name)
{
  GtkThemeEngine *result;
  
  if (!engine_hash)
    engine_hash = g_hash_table_new (g_str_hash, g_str_equal);
167

Owen Taylor's avatar
Owen Taylor committed
168 169 170
  /* get the library name for the theme
   */
  result = g_hash_table_lookup (engine_hash, name);
171

Owen Taylor's avatar
Owen Taylor committed
172 173
  if (!result)
    {
Manish Singh's avatar
Manish Singh committed
174
      result = g_object_new (GTK_TYPE_THEME_ENGINE, NULL);
Owen Taylor's avatar
Owen Taylor committed
175
      g_type_module_set_name (G_TYPE_MODULE (result), name);
176
      result->name = g_strdup (name);
Owen Taylor's avatar
Owen Taylor committed
177 178

      g_hash_table_insert (engine_hash, result->name, result);
179
    }
180

Owen Taylor's avatar
Owen Taylor committed
181 182 183 184
  if (!g_type_module_use (G_TYPE_MODULE (result)))
    return NULL;

  return result;
185
}
186

Owen Taylor's avatar
Owen Taylor committed
187 188 189 190 191 192 193
GtkRcStyle *
gtk_theme_engine_create_rc_style (GtkThemeEngine *engine)
{
  g_return_val_if_fail (engine != NULL, NULL);

  return engine->create_rc_style ();
}