gtkthemes.c 8.54 KB
Newer Older
1 2 3 4 5 6
/* GTK - The GIMP Toolkit
 * 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 29 30 31 32 33 34 35 36 37
#include <stdio.h>
#include <stdlib.h>
#include <gmodule.h>
#include "gtkthemes.h"
#include "gtkmain.h"
#include "gtkrc.h"
#include "gtkselection.h"
#include "gtksignal.h"
#include "gtkwidget.h"
#include "config.h"
38
#include "gtkintl.h"
39

40
typedef struct _GtkThemeEnginePlugin GtkThemeEnginePlugin;
41

42 43
struct _GtkThemeEngine
{
44 45 46 47
  GModule *library;

  void (*init) (GtkThemeEngine *);
  void (*exit) (void);
48
  GtkRcStyle *(*create_rc_style) ();
49

50 51 52 53
  gchar *name;

  GSList *plugins;		/* TypePlugins for this engine */
  
54 55 56
  guint refcount;
};

57 58 59 60 61 62 63 64 65 66 67
struct _GtkThemeEnginePlugin
{
  GTypePlugin plugin;

  GtkThemeEngine *engine;
  gchar *engine_name;
  GTypeInfo info;
  GType type;
  GType parent_type;
};

68 69
static GHashTable *engine_hash = NULL;

Asbjørn Pettersen's avatar
Asbjørn Pettersen committed
70 71 72 73 74 75 76 77 78 79 80
#ifdef __EMX__
static void gen_8_3_dll_name(gchar *name, gchar *fullname)
{
    /* 8.3 dll filename restriction */
    fullname[0] = '_';
    strncpy (fullname + 1, name, 7);
    fullname[8] = '\0';
    strcat (fullname, ".dll");
}                                                	
#endif

81
GtkThemeEngine*
Owen Taylor's avatar
Owen Taylor committed
82
gtk_theme_engine_get (const gchar *name)
83
{
84
  GtkThemeEngine *result;
85 86 87 88 89 90 91 92 93 94
  
  if (!engine_hash)
    engine_hash = g_hash_table_new (g_str_hash, g_str_equal);

  /* get the library name for the theme */
  
  result = g_hash_table_lookup (engine_hash, name);

  if (!result)
    {
95
       gchar *fullname;
96 97 98
       gchar *engine_path;
       GModule *library;
      
Asbjørn Pettersen's avatar
Asbjørn Pettersen committed
99
#ifndef __EMX__
100
       fullname = g_module_build_path (NULL, name);
Asbjørn Pettersen's avatar
Asbjørn Pettersen committed
101
#else
102
       fullname = g_malloc (13);
Asbjørn Pettersen's avatar
Asbjørn Pettersen committed
103 104
       gen_8_3_dll_name(name, fullname);
#endif
105
       engine_path = gtk_rc_find_module_in_path (fullname);
Asbjørn Pettersen's avatar
Asbjørn Pettersen committed
106 107 108 109 110 111 112 113
#ifdef __EMX__
       if (!engine_path)
	 {
	   /* try theme name without prefix '_' */
	   memmove(fullname, fullname + 1, strlen(fullname));
	   engine_path = gtk_rc_find_module_in_path (fullname);
	 }
#endif
114 115

       if (!engine_path)
116
	 {
117
	   g_warning (_("Unable to locate loadable module in module_path: \"%s\","),
118 119
		      fullname);
	   
120
	   g_free (fullname);
121 122
	   return NULL;
	 }
123
       g_free (fullname);
124 125
       
       /* load the lib */
126 127

       GTK_NOTE (MISC, g_message ("Loading Theme %s\n", engine_path));
128 129 130 131
       
       library = g_module_open (engine_path, 0);
       g_free(engine_path);
       if (!library)
132 133 134 135
	 {
	   g_warning (g_module_error());
	   return NULL;
	 }
136 137
       else
	 {
138
	    result = g_new (GtkThemeEngine, 1);
139 140 141 142
	    
	    result->refcount = 1;
	    result->name = g_strdup (name);
	    result->library = library;
143
	    result->plugins = NULL;
144 145 146 147 148
	    
	    /* extract symbols from the lib */
	    if (!g_module_symbol (library, "theme_init",
				  (gpointer *)&result->init) ||
		!g_module_symbol (library, "theme_exit", 
149 150 151
				  (gpointer *)&result->exit) ||
		!g_module_symbol (library, "theme_create_rc_style", 
				  (gpointer *)&result->create_rc_style))
152
	      {
153
		g_warning (g_module_error());
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
		g_free (result);
		return NULL;
	      }
	    
	    /* call the theme's init (theme_init) function to let it */
	    /* setup anything it needs to set up. */
	    result->init((GtkThemeEngine *)result);
	    
	    g_hash_table_insert (engine_hash, result->name, result);
	 }
    }
  else
    result->refcount++;

  return (GtkThemeEngine *)result;
}

void
gtk_theme_engine_ref (GtkThemeEngine *engine)
{
  g_return_if_fail (engine != NULL);
  
176
  engine->refcount++;
177 178 179 180 181
}

void
gtk_theme_engine_unref (GtkThemeEngine *engine)
{
182
  GSList *tmp_list;
183 184

  g_return_if_fail (engine != NULL);
185
  g_return_if_fail (engine->refcount > 0);
186

187
  engine->refcount--;
188

189
  if (engine->refcount == 0)
190
    {
191 192 193 194 195 196 197 198 199 200 201 202 203
      engine->exit();

      g_hash_table_remove (engine_hash, engine->name);

      tmp_list = engine->plugins;
      while (tmp_list)
	{
	  GtkThemeEnginePlugin *plugin = tmp_list->data;
	  plugin->engine = NULL;

	  tmp_list = tmp_list->next;
	}
      g_slist_free (engine->plugins);
204
      
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
      g_module_close (engine->library);
      g_free (engine->name);
      g_free (engine);
    }
}

GtkRcStyle *
gtk_theme_engine_create_rc_style (GtkThemeEngine *engine)
{
  g_return_val_if_fail (engine != NULL, NULL);
  
  return engine->create_rc_style ();
}

static void
gtk_theme_engine_plugin_ref (GTypePlugin *plugin)
{
  GtkThemeEnginePlugin *theme_plugin = (GtkThemeEnginePlugin *)plugin;

  if (theme_plugin->engine == NULL)
    {
      gtk_theme_engine_get (theme_plugin->engine_name);
      if (!theme_plugin->engine)
	{
	  g_warning ("An attempt to create an instance of a type from\n"
		     "a previously loaded theme engine was made after the engine\n"
		     "was unloaded, but the engine could not be reloaded or no longer\n"
		     "implements the type. Bad things will happen.\n");
	}
    }
  else
    gtk_theme_engine_ref (theme_plugin->engine);
}

static void
gtk_theme_engine_plugin_unref (GTypePlugin *plugin)
{
  GtkThemeEnginePlugin *theme_plugin = (GtkThemeEnginePlugin *)plugin;

  g_return_if_fail (theme_plugin->engine != NULL);
  
  gtk_theme_engine_unref (theme_plugin->engine);
}
			       
static void
gtk_theme_engine_complete_type_info (GTypePlugin     *plugin,
				     GType            g_type,
				     GTypeInfo       *info,
				     GTypeValueTable *value_table)
{
  GtkThemeEnginePlugin *theme_plugin = (GtkThemeEnginePlugin *)plugin;

  *info = theme_plugin->info;
}

static GTypePluginVTable gtk_theme_engine_plugin_vtable = {
  gtk_theme_engine_plugin_ref,
  gtk_theme_engine_plugin_unref,
  gtk_theme_engine_complete_type_info,
  NULL
};

/**
 * gtk_theme_engine_register_type:
 * @engine:      a #GtkThemeEngine
 * @parent_type: the type for the parent class
 * @type_name:   name for the type
 * @type_info:   type information structure
 * 
 * Looks up or registers a type that is implemented with a particular
 * theme engine. If a type with name @type_name is already registered,
 * the #GType identifier for the type is returned, otherwise the type
 * is newly registered, and the resulting #GType identifier returned.
 *
 * As long as any instances of the type exist, the a reference will be
 * held to the theme engine and the theme engine will not be unloaded.
 *
 * Return value: the type identifier for the class.
 **/
GType
gtk_theme_engine_register_type (GtkThemeEngine  *engine,
				GType            parent_type,
				const gchar     *type_name,
				const GTypeInfo *type_info)
{
  GtkThemeEnginePlugin *plugin;
  GType type;
  
  g_return_val_if_fail (engine != NULL, 0);
  g_return_val_if_fail (type_name != NULL, 0);
  g_return_val_if_fail (type_info != NULL, 0);

  type = g_type_from_name (type_name);
  if (type)
    plugin = (GtkThemeEnginePlugin *)g_type_get_plugin (type);
  else
    {
      plugin = g_new (GtkThemeEnginePlugin, 1);

      plugin->plugin.vtable = &gtk_theme_engine_plugin_vtable;
      plugin->engine = NULL;
      plugin->engine_name = NULL;
      plugin->parent_type = parent_type;
      plugin->type = g_type_register_dynamic (parent_type, type_name, (GTypePlugin *)plugin);
    }
  
  if (plugin->engine)
    {
      if (plugin->engine != engine)
	{
	  g_warning ("Two different theme engines tried to register '%s'.", type_name);
	  return 0;
	}

      if (plugin->parent_type != parent_type)
	{
	  g_warning ("Type '%s' recreated with different parent type.\n"
		     "(was '%s', now '%s')", type_name,
		     g_type_name (plugin->parent_type),
		     g_type_name (parent_type));
	  return 0;
	}
    }
  else
    {
      plugin->engine = engine;
      if (plugin->engine_name)
	g_free (plugin->engine_name);

      plugin->engine_name = g_strdup (engine->name);
335
      
336 337 338
      plugin->info = *type_info;

      engine->plugins = g_slist_prepend (engine->plugins, plugin);
339
    }
340 341

  return plugin->type;
342
}
343