[14481] | 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 | GtkThemeEngine* |
---|
| 57 | gtk_theme_engine_get (const gchar *name) |
---|
| 58 | { |
---|
| 59 | GtkThemeEnginePrivate *result; |
---|
| 60 | |
---|
| 61 | if (!engine_hash) |
---|
| 62 | engine_hash = g_hash_table_new (g_str_hash, g_str_equal); |
---|
| 63 | |
---|
| 64 | /* get the library name for the theme */ |
---|
| 65 | |
---|
| 66 | result = g_hash_table_lookup (engine_hash, name); |
---|
| 67 | |
---|
| 68 | if (!result) |
---|
| 69 | { |
---|
| 70 | gchar fullname[1024]; |
---|
| 71 | gchar *engine_path; |
---|
| 72 | GModule *library; |
---|
| 73 | |
---|
| 74 | g_snprintf (fullname, 1024, "lib%s.so", name); |
---|
| 75 | engine_path = gtk_rc_find_module_in_path (fullname); |
---|
| 76 | |
---|
| 77 | if (!engine_path) |
---|
| 78 | { |
---|
| 79 | g_warning (_("Unable to locate loadable module in module_path: \"%s\","), |
---|
| 80 | fullname); |
---|
| 81 | |
---|
| 82 | return NULL; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | /* load the lib */ |
---|
| 86 | |
---|
| 87 | GTK_NOTE (MISC, g_message ("Loading Theme %s\n", engine_path)); |
---|
| 88 | |
---|
| 89 | library = g_module_open (engine_path, 0); |
---|
| 90 | g_free(engine_path); |
---|
| 91 | if (!library) |
---|
| 92 | { |
---|
| 93 | g_warning (g_module_error()); |
---|
| 94 | return NULL; |
---|
| 95 | } |
---|
| 96 | else |
---|
| 97 | { |
---|
| 98 | result = g_new (GtkThemeEnginePrivate, 1); |
---|
| 99 | |
---|
| 100 | result->refcount = 1; |
---|
| 101 | result->name = g_strdup (name); |
---|
| 102 | result->library = library; |
---|
| 103 | |
---|
| 104 | /* extract symbols from the lib */ |
---|
| 105 | if (!g_module_symbol (library, "theme_init", |
---|
| 106 | (gpointer *)&result->init) || |
---|
| 107 | !g_module_symbol (library, "theme_exit", |
---|
| 108 | (gpointer *)&result->exit)) |
---|
| 109 | { |
---|
| 110 | g_warning (g_module_error()); |
---|
| 111 | g_free (result); |
---|
| 112 | return NULL; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | /* call the theme's init (theme_init) function to let it */ |
---|
| 116 | /* setup anything it needs to set up. */ |
---|
| 117 | result->init((GtkThemeEngine *)result); |
---|
| 118 | |
---|
| 119 | g_hash_table_insert (engine_hash, result->name, result); |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | else |
---|
| 123 | result->refcount++; |
---|
| 124 | |
---|
| 125 | return (GtkThemeEngine *)result; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | void |
---|
| 129 | gtk_theme_engine_ref (GtkThemeEngine *engine) |
---|
| 130 | { |
---|
| 131 | g_return_if_fail (engine != NULL); |
---|
| 132 | |
---|
| 133 | ((GtkThemeEnginePrivate *)engine)->refcount++; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | void |
---|
| 137 | gtk_theme_engine_unref (GtkThemeEngine *engine) |
---|
| 138 | { |
---|
| 139 | GtkThemeEnginePrivate *private; |
---|
| 140 | private = (GtkThemeEnginePrivate *)engine; |
---|
| 141 | |
---|
| 142 | g_return_if_fail (engine != NULL); |
---|
| 143 | g_return_if_fail (private->refcount > 0); |
---|
| 144 | |
---|
| 145 | private->refcount--; |
---|
| 146 | |
---|
| 147 | if (private->refcount == 0) |
---|
| 148 | { |
---|
| 149 | private->exit(); |
---|
| 150 | |
---|
| 151 | g_hash_table_remove (engine_hash, private->name); |
---|
| 152 | |
---|
| 153 | g_module_close (private->library); |
---|
| 154 | g_free (private->name); |
---|
| 155 | g_free (private); |
---|
| 156 | } |
---|
| 157 | } |
---|