From 946bd11112f6947e490d40444ec6b0685bf93a8e Mon Sep 17 00:00:00 2001
From: Edward Hennessy <[EMAIL PROTECTED]>
Date: Sat, 26 Jul 2008 09:44:29 -0700
Subject: [PATCH] Modified toolbar to use GtkIconFactory for icons.
---
gschem/include/prototype.h | 3 +
gschem/include/x_icons.h | 8 +++
gschem/src/Makefile.am | 2 +-
gschem/src/x_icons.c | 117 ++++++++++++++++++++++++++++++++++++++++++++
gschem/src/x_window.c | 71 +++++----------------------
5 files changed, 142 insertions(+), 59 deletions(-)
create mode 100644 gschem/include/x_icons.h
create mode 100644 gschem/src/x_icons.c
diff --git a/gschem/include/prototype.h b/gschem/include/prototype.h
index 89ecf24..f0086e2 100644
--- a/gschem/include/prototype.h
+++ b/gschem/include/prototype.h
@@ -805,6 +805,9 @@ int x_fileselect_load_backup(TOPLEVEL *toplevel, GString
*message);
/* x_grid.c */
void x_grid_draw(GSCHEM_TOPLEVEL *w_current);
void x_draw_tiles(GSCHEM_TOPLEVEL *w_current);
+/* x_icons.c */
+void x_icons_init(GSCHEM_TOPLEVEL *w_current);
+GtkWidget *x_icons_stock_pixmap(const gchar *stock_id);
/* x_image.c */
void x_image_lowlevel(GSCHEM_TOPLEVEL *w_current, const char* filename,
int desired_width, int desired_height, char *filetype);
diff --git a/gschem/include/x_icons.h b/gschem/include/x_icons.h
new file mode 100644
index 0000000..8379213
--- /dev/null
+++ b/gschem/include/x_icons.h
@@ -0,0 +1,8 @@
+/* stock ids for gschem's icons */
+
+#define GSCHEM_STOCK_BUS "gschem-bus"
+#define GSCHEM_STOCK_COMPONENT "gschem-component"
+#define GSCHEM_STOCK_NET "gschem-net"
+#define GSCHEM_STOCK_SELECT "gschem-select"
+#define GSCHEM_STOCK_TEXT "gschem-text"
+
diff --git a/gschem/src/Makefile.am b/gschem/src/Makefile.am
index 0a01fa1..c58d70f 100644
--- a/gschem/src/Makefile.am
+++ b/gschem/src/Makefile.am
@@ -21,7 +21,7 @@ gschem_SOURCES = \
x_event.c x_grid.c x_log.c x_menus.c x_script.c \
x_pagesel.c x_print.c x_window.c x_stroke.c x_image.c x_color.c \
x_compselect.c x_fileselect.c x_preview.c x_attribedit.c \
- x_multiattrib.c \
+ x_multiattrib.c x_icons.c \
parsecmd.c o_cue.c \
gschem_dialog.c \
gschem_toplevel.c
diff --git a/gschem/src/x_icons.c b/gschem/src/x_icons.c
new file mode 100644
index 0000000..a476096
--- /dev/null
+++ b/gschem/src/x_icons.c
@@ -0,0 +1,117 @@
+/* gEDA - GPL Electronic Design Automation
+ * gschem - gEDA Schematic Capture
+ * RCopyright (C) 1998-2008 Ales Hvezda
+ * Copyright (C) 1998-2008 gEDA Contributors (see ChangeLog for details)
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
+ */
+#include <config.h>
+#include <glib-object.h>
+#include <stdio.h>
+
+#include <libgeda/libgeda.h>
+
+#include "../include/gschem_struct.h"
+#include "../include/globals.h"
+#include "../include/prototype.h"
+#include "../include/x_icons.h"
+
+#ifdef HAVE_LIBDMALLOC
+#include <dmalloc.h>
+#endif
+
+typedef struct _icon_entry icon_entry;
+
+struct _icon_entry
+{
+ gchar *stock_id;
+ gchar *filename;
+};
+
+/* Image files may be either XPM, PNG, or SVG. */
+
+static const icon_entry icon_table[] =
+{
+ { GSCHEM_STOCK_BUS, "gschem-bus.xpm" },
+ { GSCHEM_STOCK_COMPONENT, "gschem-comp.xpm" },
+ { GSCHEM_STOCK_NET, "gschem-net.xpm" },
+ { GSCHEM_STOCK_SELECT, "gschem-select.xpm" },
+ { GSCHEM_STOCK_TEXT, "gschem-text.xpm" }
+};
+
+#define ICONS_SIZE (sizeof(icon_table)/sizeof(icon_entry))
+
+/*! \brief Create and register gschem's icon factory.
+ * \par Since the application can operate with missing icons, this function
+ * treats a the failure to load an icon as a warning.
+ * \param w_current A pointer to GSCHEM_TOPLEVEL used to obtain the bitmap
+ * directory. The w_current->toplevel->bitmap_directory must not be NULL.
+ */
+void x_icons_init(GSCHEM_TOPLEVEL *w_current)
+{
+ GError *error=NULL;
+ GtkIconFactory *factory;
+ gchar *filename=NULL;
+ int i;
+ GtkIconSet *icons;
+ GdkPixbuf *pixbuf;
+
+ g_assert(w_current!=NULL);
+ g_assert(w_current->toplevel!=NULL);
+ g_assert(w_current->toplevel->bitmap_directory!=NULL);
+
+ factory = gtk_icon_factory_new();
+
+ for (i=0; i<ICONS_SIZE; i++) {
+ g_clear_error(&error);
+ g_free(filename);
+
+ filename=g_strconcat(w_current->toplevel->bitmap_directory,
+ G_DIR_SEPARATOR_S,
+ icon_table[i].filename, NULL);
+
+ pixbuf = gdk_pixbuf_new_from_file(filename, &error);
+
+ if (pixbuf==NULL) {
+ g_warning("Loading icons: %s\n",error->message);
+ continue;
+ }
+
+ icons = gtk_icon_set_new_from_pixbuf(pixbuf);
+ g_object_unref(pixbuf);
+
+ gtk_icon_factory_add(factory, icon_table[i].stock_id, icons);
+ gtk_icon_set_unref(icons);
+ }
+ g_clear_error(&error);
+ g_free(filename);
+
+ gtk_icon_factory_add_default(factory);
+
+ g_object_unref(factory);
+}
+
+/*! \brief Creates a new GtkImage displaying a GTK stock icon if available.
+ *
+ * If a stock GTK icon with the requested name was not found, this function
+ * falls back to the bitmap icons provided in the distribution.
+ *
+ * \param stock Name of the stock icon ("new", "open", etc.)
+ * \return Pointer to the new GtkImage object.
+ */
+GtkWidget *x_icons_stock_pixmap(const gchar *stock_id)
+{
+ return gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_LARGE_TOOLBAR);
+}
diff --git a/gschem/src/x_window.c b/gschem/src/x_window.c
index 0259843..bed6b6d 100644
--- a/gschem/src/x_window.c
+++ b/gschem/src/x_window.c
@@ -27,6 +27,7 @@
#include "../include/i_vars.h"
#include "../include/globals.h"
#include "../include/prototype.h"
+#include "../include/x_icons.h"
#ifdef HAVE_LIBDMALLOC
#include <dmalloc.h>
@@ -47,6 +48,8 @@ void x_window_setup (GSCHEM_TOPLEVEL *w_current)
/* immediately setup user params */
i_vars_set(w_current);
+ x_icons_init(w_current);
+
/* Initialize the autosave callback */
s_page_autosave_init(toplevel);
@@ -229,54 +232,6 @@ void x_window_setup_draw_events(GSCHEM_TOPLEVEL *w_current)
}
}
-
-/*! \brief Creates a new GtkImage displaying a GTK stock icon if available.
- *
- * If a stock GTK icon with the requested name was not found, this function
- * falls back to the bitmap icons provided in the distribution.
- *
- * \param stock Name of the stock icon ("new", "open", etc.)
- * \return Pointer to the new GtkImage object.
- */
-static GtkWidget *x_window_stock_pixmap(const char *stock, GSCHEM_TOPLEVEL
*w_current)
-{
- GtkWidget *wpixmap = NULL;
- GdkPixmap *pixmap;
- GdkBitmap *mask;
- GtkStockItem item;
-
- GdkWindow *window=w_current->main_window->window;
- GdkColor *background=&w_current->main_window->style->bg[GTK_STATE_NORMAL];
-
- gchar *filename=g_strconcat(w_current->toplevel->bitmap_directory,
- G_DIR_SEPARATOR_S,
- "gschem-", stock, ".xpm", NULL);
-
- gchar *stockid=g_strconcat("gtk-", stock, NULL);
-
- /* First check if GTK knows this stock icon */
- if(gtk_stock_lookup(stockid, &item)) {
- wpixmap = gtk_image_new_from_stock(stockid,
- GTK_ICON_SIZE_SMALL_TOOLBAR);
- } else {
- /* Fallback to the original custom icon */
- pixmap = gdk_pixmap_create_from_xpm (window, &mask,
- background, filename);
- if (pixmap != NULL) {
- wpixmap = gtk_image_new_from_pixmap (pixmap, mask);
- } else {
- s_log_message("Could not find image at file: %s.\n", filename);
- wpixmap = gtk_image_new_from_stock(GTK_STOCK_MISSING_IMAGE ,
- GTK_ICON_SIZE_SMALL_TOOLBAR);
- }
- }
-
- g_free(filename);
- g_free(stockid);
-
- return wpixmap;
-}
-
static void x_window_invoke_macro(GtkEntry *entry, void *userdata)
{
GSCHEM_TOPLEVEL *w_current = userdata;
@@ -368,21 +323,21 @@ void x_window_create_main(GSCHEM_TOPLEVEL *w_current)
_("New"),
_("New file"),
"toolbar/new",
- x_window_stock_pixmap("new", w_current),
+ x_icons_stock_pixmap(GTK_STOCK_NEW),
(GtkSignalFunc) i_callback_toolbar_file_new,
w_current);
gtk_toolbar_append_item (GTK_TOOLBAR (toolbar),
_("Open"),
_("Open file..."),
"toolbar/open",
- x_window_stock_pixmap("open", w_current),
+ x_icons_stock_pixmap(GTK_STOCK_OPEN),
(GtkSignalFunc) i_callback_toolbar_file_open,
w_current);
gtk_toolbar_append_item (GTK_TOOLBAR (toolbar),
_("Save"),
_("Save file"),
"toolbar/save",
- x_window_stock_pixmap("save", w_current),
+ x_icons_stock_pixmap(GTK_STOCK_SAVE),
(GtkSignalFunc) i_callback_toolbar_file_save,
w_current);
gtk_toolbar_append_space (GTK_TOOLBAR(toolbar));
@@ -390,14 +345,14 @@ void x_window_create_main(GSCHEM_TOPLEVEL *w_current)
_("Undo"),
_("Undo last operation"),
"toolbar/undo",
- x_window_stock_pixmap("undo", w_current),
+ x_icons_stock_pixmap(GTK_STOCK_UNDO),
(GtkSignalFunc) i_callback_toolbar_edit_undo,
w_current);
gtk_toolbar_append_item (GTK_TOOLBAR (toolbar),
_("Redo"),
_("Redo last undo"),
"toolbar/redo",
- x_window_stock_pixmap("redo", w_current),
+ x_icons_stock_pixmap(GTK_STOCK_REDO),
(GtkSignalFunc) i_callback_toolbar_edit_redo,
w_current);
gtk_toolbar_append_space (GTK_TOOLBAR(toolbar));
@@ -406,7 +361,7 @@ void x_window_create_main(GSCHEM_TOPLEVEL *w_current)
_("Component"),
_("Add component...\nSelect library and component
from list, move the mouse into main window, click to place\nRight mouse button
to cancel"),
"toolbar/component",
- x_window_stock_pixmap("comp", w_current),
+ x_icons_stock_pixmap(GSCHEM_STOCK_COMPONENT),
(GtkSignalFunc) i_callback_toolbar_add_component,
w_current);
w_current->toolbar_net =
@@ -416,7 +371,7 @@ void x_window_create_main(GSCHEM_TOPLEVEL *w_current)
_("Nets"),
_("Add nets mode\nRight mouse button to
cancel"),
"toolbar/nets",
- x_window_stock_pixmap("net", w_current),
+ x_icons_stock_pixmap(GSCHEM_STOCK_NET),
(GtkSignalFunc) i_callback_toolbar_add_net,
w_current);
w_current->toolbar_bus =
@@ -426,7 +381,7 @@ void x_window_create_main(GSCHEM_TOPLEVEL *w_current)
_("Bus"),
_("Add buses mode\nRight mouse button to
cancel"),
"toolbar/bus",
- x_window_stock_pixmap("bus", w_current),
+ x_icons_stock_pixmap(GSCHEM_STOCK_BUS),
(GtkSignalFunc) i_callback_toolbar_add_bus,
w_current);
/* not part of any radio button group */
@@ -434,7 +389,7 @@ void x_window_create_main(GSCHEM_TOPLEVEL *w_current)
_("Text"),
_("Add Text..."),
"toolbar/text",
- x_window_stock_pixmap("text", w_current),
+ x_icons_stock_pixmap(GSCHEM_STOCK_TEXT),
(GtkSignalFunc) i_callback_toolbar_add_text,
w_current);
gtk_toolbar_append_space (GTK_TOOLBAR(toolbar));
@@ -445,7 +400,7 @@ void x_window_create_main(GSCHEM_TOPLEVEL *w_current)
_("Select"),
_("Select mode"),
"toolbar/select",
- x_window_stock_pixmap("select", w_current),
+ x_icons_stock_pixmap(GSCHEM_STOCK_SELECT),
(GtkSignalFunc)
i_callback_toolbar_edit_select,
w_current);
--
1.5.6
_______________________________________________
geda-dev mailing list
[email protected]
http://www.seul.org/cgi-bin/mailman/listinfo/geda-dev