[monkey-bubble: 46/753] made gtt use gnome-stock.[ch]



commit a6040d835c7e92eef4915f14facb268da5ac0260
Author: MET  Eckehard Berns <eb berns prima de>
Date:   Mon Jan 19 15:09:19 1998 +0000

    made gtt use gnome-stock.[ch]
    
    Mon Jan 19 15:52:46 1998 MET  Eckehard Berns  <eb berns prima de>
    
    	* programs/gtt: made gtt use gnome-stock.[ch]
    
    	* libgnomeui/libgnomeui.h: include gnome-stock.h
    
    	* libgnomeui/gnome-stock.[ch]: added. They provide stock icons for
     	  toolbars etc. Short description in gnome-stock.h. Not everything
     	  is implemented, espacially theme support. But they're usable.

 libgnomeui/Makefile.am   |    4 +
 libgnomeui/gnome-stock.c |  301 ++++++++++++++++++++++++++++++++++++++++++++++
 libgnomeui/gnome-stock.h |  163 +++++++++++++++++++++++++
 libgnomeui/libgnomeui.h  |    1 +
 4 files changed, 469 insertions(+), 0 deletions(-)
---
diff --git a/libgnomeui/Makefile.am b/libgnomeui/Makefile.am
index 2df788a..9e03c9a 100644
--- a/libgnomeui/Makefile.am
+++ b/libgnomeui/Makefile.am
@@ -1,5 +1,7 @@
 ## Process this file with automake to produce Makefile.in
 
+DEFS += -Wall
+
 libgnomeuiincludedir = $(includedir)/libgnomeui
 
 INCLUDES = -I.. -I$(srcdir)/.. -I$(includedir)
@@ -18,6 +20,7 @@ libgnomeui_la_SOURCES = \
 	gnome-pixmap.c			\
 	gnome-properties.c		\
 	gnome-session.c			\
+	gnome-stock.c			\
 	gtk-clock.c
 
 libgnomeuiinclude_HEADERS = 		\
@@ -31,6 +34,7 @@ libgnomeuiinclude_HEADERS = 		\
 	gnome-properties.h     		\
 	gnome-pixmap.h			\
 	gnome-session.h			\
+	gnome-stock.h			\
 	gtk-clock.h			\
 	libgnomeui.h
 
diff --git a/libgnomeui/gnome-stock.c b/libgnomeui/gnome-stock.c
new file mode 100644
index 0000000..ecc22f4
--- /dev/null
+++ b/libgnomeui/gnome-stock.c
@@ -0,0 +1,301 @@
+#include <config.h>
+#include <stdlib.h>
+#include <string.h>
+#include <glib.h>
+#include <gdk/gdk.h>
+#include "libgnome/gnome-defs.h"
+#include "gnome-stock.h"
+
+#include "../programs/gtt/tb_new.xpm"
+#include "../programs/gtt/tb_save.xpm"
+#include "../programs/gtt/tb_open.xpm"
+#include "../programs/gtt/tb_cut.xpm"
+#include "../programs/gtt/tb_copy.xpm"
+#include "../programs/gtt/tb_paste.xpm"
+#include "../programs/gtt/tb_properties.xpm"
+#include "../programs/gtt/tb_prop_dis.xpm"
+
+
+#define STOCK_SEP '.'
+#define STOCK_SEP_STR "."
+
+
+/**************************/
+/* GnomeStockPixmapWidget */
+/**************************/
+
+static GtkVBoxClass *parent_class = NULL;
+
+static void
+gnome_stock_pixmap_widget_destroy(GtkObject *object)
+{
+	GnomeStockPixmapWidget *w;
+
+	g_return_if_fail (object != NULL);
+	g_return_if_fail (GNOME_IS_STOCK_PIXMAP_WIDGET (object));
+
+	w = GNOME_STOCK_PIXMAP_WIDGET (object);
+	
+	/* free resources */
+	if (w->pixmap) gtk_widget_destroy(GTK_WIDGET(w->pixmap));
+        if (w->icon) g_free(w->icon);
+
+	if (GTK_OBJECT_CLASS(parent_class)->destroy)
+		(* GTK_OBJECT_CLASS(parent_class)->destroy)(object);
+}
+
+
+
+static void
+gnome_stock_pixmap_widget_class_init(GnomeStockPixmapWidgetClass *klass)
+{
+	GtkObjectClass *object_class = GTK_OBJECT_CLASS(klass);
+	object_class->destroy = gnome_stock_pixmap_widget_destroy;
+}
+
+
+
+static void
+gnome_stock_pixmap_widget_init(GtkObject *obj)
+{
+        GnomeStockPixmapWidget *w;
+
+        g_return_if_fail(obj != NULL);
+        g_return_if_fail(GNOME_IS_STOCK_PIXMAP_WIDGET(obj));
+
+        w = GNOME_STOCK_PIXMAP_WIDGET(obj);
+        w->icon = NULL;
+        w->pixmap = NULL;
+        w->window = NULL;
+}
+
+
+
+guint
+gnome_stock_pixmap_widget_get_type(void)
+{
+	static guint new_type = 0;
+	if (!new_type) {
+		GtkTypeInfo type_info = {
+			"GnomeStockPixmapWidget",
+			sizeof(GnomeStockPixmapWidget),
+			sizeof(GnomeStockPixmapWidgetClass),
+			(GtkClassInitFunc)gnome_stock_pixmap_widget_class_init,
+			(GtkObjectInitFunc)gnome_stock_pixmap_widget_init,
+			(GtkArgSetFunc) NULL,
+			(GtkArgGetFunc) NULL
+		};
+		new_type = gtk_type_unique(gtk_vbox_get_type(), &type_info);
+		parent_class = gtk_type_class(gtk_vbox_get_type());
+	}
+	return new_type;
+}
+
+
+
+GtkWidget *
+gnome_stock_pixmap_widget_new(GtkWidget *window, char *icon)
+{
+	GtkWidget *w;
+        GnomeStockPixmapWidget *p;
+
+        g_return_val_if_fail(icon != NULL, NULL);
+        g_return_val_if_fail(gnome_stock_pixmap_checkfor(icon, GNOME_STOCK_PIXMAP_REGULAR), NULL);
+
+	w = gtk_type_new(gnome_stock_pixmap_widget_get_type());
+        p = GNOME_STOCK_PIXMAP_WIDGET(w);
+        p->icon = g_strdup(icon);
+        p->window = window;
+
+        /* TODO: for now I just show the regular pixmap at init time
+           and that's it */
+        p->pixmap = gnome_stock_pixmap(window, icon,
+                                       GNOME_STOCK_PIXMAP_REGULAR);
+        gtk_widget_show(GTK_WIDGET(p->pixmap));
+        gtk_container_add(GTK_CONTAINER(w), GTK_WIDGET(p->pixmap));
+
+	return w;
+}
+
+
+
+/****************/
+/* some helpers */
+/****************/
+
+
+struct _default_entries_data {
+        char *icon, *subtype;
+        gchar **xpm_data;
+};
+
+struct _default_entries_data entries_data[] = {
+        {GNOME_STOCK_PIXMAP_NEW, GNOME_STOCK_PIXMAP_REGULAR, tb_new_xpm},
+        {GNOME_STOCK_PIXMAP_SAVE, GNOME_STOCK_PIXMAP_REGULAR, tb_save_xpm},
+        {GNOME_STOCK_PIXMAP_OPEN, GNOME_STOCK_PIXMAP_REGULAR, tb_open_xpm},
+        {GNOME_STOCK_PIXMAP_CUT, GNOME_STOCK_PIXMAP_REGULAR, tb_cut_xpm},
+        {GNOME_STOCK_PIXMAP_COPY, GNOME_STOCK_PIXMAP_REGULAR, tb_copy_xpm},
+        {GNOME_STOCK_PIXMAP_PASTE, GNOME_STOCK_PIXMAP_REGULAR, tb_paste_xpm},
+        {GNOME_STOCK_PIXMAP_PROPERTIES, GNOME_STOCK_PIXMAP_REGULAR, tb_properties_xpm},
+        {GNOME_STOCK_PIXMAP_PROPERTIES, GNOME_STOCK_PIXMAP_DISABLED, tb_prop_dis_xpm},
+};
+static int entries_data_num = sizeof(entries_data) / sizeof(entries_data[0]);
+
+
+static char *
+build_hash_key(char *icon, char *subtype)
+{
+        char *s;
+
+        s = g_malloc(strlen(icon) + strlen(subtype) + 2);
+        strcpy(s, icon);
+        strcat(s, STOCK_SEP_STR);
+        if (subtype)
+                strcat(s, subtype);
+        else
+                strcat(s, GNOME_STOCK_PIXMAP_REGULAR);
+        return s;
+}
+
+
+
+static GHashTable *
+stock_pixmaps(void)
+{
+        static GHashTable *hash = NULL;
+        GnomeStockPixmapEntry *entry;
+        int i;
+
+        if (hash) return hash;
+
+        hash = g_hash_table_new(g_str_hash, g_str_equal);
+
+        for (i = 0; i < entries_data_num; i++) {
+                entry = g_malloc(sizeof(GnomeStockPixmapEntry));
+                entry->type = GNOME_STOCK_PIXMAP_TYPE_DATA;
+                entry->data.xpm_data = entries_data[i].xpm_data;
+                g_hash_table_insert(hash,
+                                    build_hash_key(entries_data[i].icon,
+                                                   entries_data[i].subtype),
+                                    entry);
+        }
+
+        return hash;
+}
+
+
+static GnomeStockPixmapEntry *
+lookup(char *icon, char *subtype)
+{
+        char *s;
+        GHashTable *hash = stock_pixmaps();
+        GnomeStockPixmapEntry *entry;
+
+        s = build_hash_key(icon, subtype);
+        entry = (GnomeStockPixmapEntry *)g_hash_table_lookup(hash, s);
+        if (!entry) {
+                g_free(s);
+                s = build_hash_key(icon, GNOME_STOCK_PIXMAP_REGULAR);
+                entry = (GnomeStockPixmapEntry *)
+                        g_hash_table_lookup(hash, s);
+        }
+        g_free(s);
+        return entry;
+}
+
+
+
+static GtkPixmap *
+create_pixmap_from_data(GtkWidget *window, GnomeStockPixmapEntryData *data)
+{
+        GtkPixmap *pixmap;
+        GdkPixmap *pmap;
+        GdkBitmap *bmap;
+        GtkStyle *style;
+        GdkWindow *gwin;
+
+        if (window) {
+                style = gtk_widget_get_style(window);
+                gwin = window->window;
+        } else {
+                style = gtk_widget_get_default_style();
+                gwin = NULL;
+        }
+        pmap = gdk_pixmap_create_from_xpm_d(gwin, &bmap,
+                                            &style->bg[GTK_STATE_NORMAL],
+                                            data->xpm_data);
+	pixmap = (GtkPixmap *)gtk_pixmap_new(pmap, bmap);
+        return pixmap;
+}
+
+
+
+/**********************/
+/* utitlity functions */
+/**********************/
+
+
+
+GtkPixmap *
+gnome_stock_pixmap(GtkWidget *window, char *icon, char *subtype)
+{
+        GnomeStockPixmapEntry *entry;
+        GtkPixmap *pixmap;
+
+        g_return_val_if_fail(icon != NULL, NULL);
+        /* subtype can be NULL, so not checked */
+        /* window can be NULL, but if not, window has to be GtkWidget */
+        if (window)
+                g_return_val_if_fail(GTK_IS_WIDGET(window), NULL);
+
+        entry = lookup(icon, subtype);
+        if (!entry) return NULL;
+        pixmap = NULL;
+        switch (entry->type) {
+        case GNOME_STOCK_PIXMAP_TYPE_DATA:
+                pixmap = create_pixmap_from_data(window, &(entry->data));
+                break;
+        default:
+                g_assert_not_reached();
+                break;
+        }
+        return pixmap;
+}
+
+
+
+GtkWidget *
+gnome_stock_pixmap_widget(GtkWidget *window, char *icon)
+{
+        GtkWidget *w;
+
+        w = gnome_stock_pixmap_widget_new(window, icon);
+        return w;
+}
+
+
+gint
+gnome_stock_pixmap_register(char *icon, char *subtype,
+                            GnomeStockPixmapEntry *entry)
+{
+        g_print(__FILE__ "gnome_stock_pixmap_register: not implemented yet\n");
+        return 0;
+}
+
+
+
+gint
+gnome_stock_pixmap_change(char *icon, char *subtype,
+                          GnomeStockPixmapEntry *entry)
+{
+        g_print(__FILE__ "gnome_stock_pixmap_change: not implemented yet\n");
+        return 0;
+}
+
+
+
+GnomeStockPixmapEntry *
+gnome_stock_pixmap_checkfor(char *icon, char *subtype)
+{
+        return lookup(icon, subtype);
+}
diff --git a/libgnomeui/gnome-stock.h b/libgnomeui/gnome-stock.h
new file mode 100644
index 0000000..2b0614c
--- /dev/null
+++ b/libgnomeui/gnome-stock.h
@@ -0,0 +1,163 @@
+#ifndef __GNOME_PIXMAP_H__
+#define __GNOME_PIXMAP_H__
+
+
+#include <libgnome/gnome-defs.h>
+#include <gtk/gtkwidget.h>
+#include <gtk/gtkpixmap.h>
+#include <gtk/gtkvbox.h>
+
+
+/* A short description:
+
+   These functions provide an applications programmer with default
+   icons for toolbars, menu pixmaps, etc. One such `icon' should have
+   at least three pixmaps to reflect it's state. There is a `regular'
+   pixmap, a `disabled' pixmap and a `focused' pixmap. You can get
+   either each of these pixmaps by calling gnome_stock_pixmap or you
+   can get a widget by calling gnome_stock_pixmap_widget. This widget
+   is a container which gtk_widget_shows the pixmap, that is
+   reflecting the current state of the widget. If for example you
+   gtk_container_add this widget to a button, which is currently not
+   sensitive, the widget will just show the `disabled' pixmap. If the
+   state of the button changes to sensitive, the widget will change to
+   the `regular' pixmap. The `focused' pixmap will be shown, when the
+   mouse pointer enters the widget.
+
+   To support themability, we use (char *) to call those functions. A
+   new theme might register new icons by calling
+   gnome_stock_pixmap_register, or may change existing icons by
+   calling gnome_stock_pixmap_change. An application should check (by
+   calling gnome_stock_pixmap_checkfor), if the current theme supports
+   an uncommon icon, before using it. The only icons an app can rely
+   on, are those defined in this haeder file. */
+
+
+BEGIN_GNOME_DECLS
+
+/* The names of `well known' icons. I define these strings basically
+   to prevent errors due to typos. */
+
+#define GNOME_STOCK_PIXMAP_NEW         "New"
+#define GNOME_STOCK_PIXMAP_OPEN        "Open"
+#define GNOME_STOCK_PIXMAP_SAVE        "Save"
+#define GNOME_STOCK_PIXMAP_CUT         "Cut"
+#define GNOME_STOCK_PIXMAP_COPY        "Copy"
+#define GNOME_STOCK_PIXMAP_PASTE       "Paste"
+#define GNOME_STOCK_PIXMAP_PROPERTIES  "Properties"
+
+
+/* The basic pixmap version of an icon. */
+
+#define GNOME_STOCK_PIXMAP_REGULAR     "regular"
+#define GNOME_STOCK_PIXMAP_DISABLED    "disabled"
+#define GNOME_STOCK_PIXMAP_FOCUSED     "focused"
+
+
+
+/* some internal definitions */
+
+typedef struct _GnomeStockPixmapEntryData    GnomeStockPixmapEntryData;
+typedef struct _GnomeStockPixmapEntryFile    GnomeStockPixmapEntryFile;
+typedef struct _GnomeStockPixmapEntryPath    GnomeStockPixmapEntryPath;
+typedef struct _GnomeStockPixmapEntryWidget  GnomeStockPixmapEntryWidget;
+typedef union  _GnomeStockPixmapEntry        GnomeStockPixmapEntry;
+
+typedef enum {
+        GNOME_STOCK_PIXMAP_TYPE_NONE,
+        GNOME_STOCK_PIXMAP_TYPE_DATA,
+        GNOME_STOCK_PIXMAP_TYPE_FILE,
+        GNOME_STOCK_PIXMAP_TYPE_PATH,
+        GNOME_STOCK_PIXMAP_TYPE_WIDGET
+} GnomeStockPixmapType;
+
+
+/* a data entry holds a hardcoded pixmap */
+struct _GnomeStockPixmapEntryData {
+        GnomeStockPixmapType type;
+        gchar **xpm_data;
+};
+
+/* a file entry holds a filename (no path) to the pixamp. this pixmap
+   will be seached for using gnome_pixmap_file */
+struct _GnomeStockPixmapEntryFile {
+        GnomeStockPixmapType type;
+        gchar *filename;
+};
+
+/* a path entry holds the complete (absolut) path to the pixmap file */
+struct _GnomeStockPixmapEntryPath {
+        GnomeStockPixmapType type;
+        gchar *pathname;
+};
+
+/* a widget entry holds a GnomeStockPixmapWidget. This kind of icon
+   can be used by a theme to completely change the handling of a stock
+   icon. */
+struct _GnomeStockPixmapEntryWidget {
+        GnomeStockPixmapType type;
+        GtkWidget *widget;
+};
+
+union _GnomeStockPixmapEntry {
+        GnomeStockPixmapType type;
+        GnomeStockPixmapEntryData data;
+        GnomeStockPixmapEntryFile file;
+        GnomeStockPixmapEntryPath path;
+        GnomeStockPixmapEntryWidget widget;
+};
+
+
+
+/* the GnomeStockPixmapWidget */
+
+#define GNOME_STOCK_PIXMAP_WIDGET(obj)         GTK_CHECK_CAST(obj, gnome_stock_pixmap_widget_get_type(), GnomeStockPixmapWidget)
+#define GNOME_STOCK_PIXMAP_WIDGET_CLASS(klass) GTK_CHECK_CAST_CLASS(obj, gnome_stock_pixmap_widget_get_type(), GnomeStockPixmapWidget)
+#define GNOME_IS_STOCK_PIXMAP_WIDGET(obj)      GTK_CHECK_TYPE(obj, gnome_stock_pixmap_widget_get_type())
+
+typedef struct _GnomeStockPixmapWidget         GnomeStockPixmapWidget;
+typedef struct _GnomeStockPixmapWidgetClass    GnomeStockPixmapWidgetClass;
+
+struct _GnomeStockPixmapWidget {
+	GtkVBox parent_object;
+
+        char *icon;
+        GtkWidget *window;    /* needed for style and gdk_pixmap_create... */
+        GtkPixmap *pixmap;    /* the pixmap currently shown */
+};
+
+struct _GnomeStockPixmapWidgetClass {
+	GtkVBoxClass parent_class;
+};
+
+guint gnome_stock_pixmap_widget_get_type(void);
+GtkWidget *gnome_stock_pixmap_widget_new(GtkWidget *window, char *icon);
+
+
+
+/* the utility functions */
+
+/* just fetch a pixmap */
+GtkPixmap             *gnome_stock_pixmap          (GtkWidget *window,
+                                                    char *icon,
+                                                    char *subtype);
+
+/* just fetch a GnomeStockPixmapWidget */
+GtkWidget             *gnome_stock_pixmap_widget   (GtkWidget *window,
+                                                    char *icon);
+
+/* register a pixmap. returns non-zero, if successfull */
+gint                   gnome_stock_pixmap_register (char *icon, char *subtype,
+                                                    GnomeStockPixmapEntry *entry);
+
+/* change an existing entry. returns non-zero on success */
+gint                   gnome_stock_pixmap_change   (char *icon, char *subtype,
+                                                    GnomeStockPixmapEntry *entry);
+
+/* check for the existance of an entry. returns the entry if it
+   exists, or NULL otherwise */
+GnomeStockPixmapEntry *gnome_stock_pixmap_checkfor (char *icon, char *subtype);
+
+END_GNOME_DECLS
+
+#endif
diff --git a/libgnomeui/libgnomeui.h b/libgnomeui/libgnomeui.h
index e0d9a2b..c93b9eb 100644
--- a/libgnomeui/libgnomeui.h
+++ b/libgnomeui/libgnomeui.h
@@ -10,6 +10,7 @@
 #include "libgnomeui/gnome-messagebox.h"
 #include "libgnomeui/gnome-pixmap.h"
 #include "libgnomeui/gnome-actionarea.h"
+#include "libgnomeui/gnome-stock.h"
 #include "libgnomeui/gtk-clock.h"
 
 BEGIN_GNOME_DECLS



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]