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

gEDA-dev: [PATCH] Modified toolbar to use GtkIconFactory for icons.



An older patch, brought up to date.  This patch modifies the toolbar  
to use GtkIconFactory.  This change will allow removal of the  
deprecated toolbar API calls (WIP).

From f355d43ab8cfac9371c85d863cde64dccc9326e5 Mon Sep 17 00:00:00 2001
From: Edward Hennessy <ehennes@sbcglobal.net>
Date: Tue, 9 Dec 2008 13:48:12 -0800
Subject: [PATCH] Modified toolbar to use GtkIconFactory for icons.

---
 gschem/include/Makefile.am |    2 +-
 gschem/include/gschem.h    |    1 +
 gschem/include/prototype.h |    5 +-
 gschem/include/x_icons.h   |    8 ++
 gschem/src/Makefile.am     |    1 +
 gschem/src/gschem.c        |    7 ++-
 gschem/src/x_icons.c       |  153 ++++++++++++++++++++++++++++++++++++++++++++
 gschem/src/x_window.c      |   68 +++-----------------
 8 files changed, 183 insertions(+), 62 deletions(-)
 create mode 100644 gschem/include/x_icons.h
 create mode 100644 gschem/src/x_icons.c

diff --git a/gschem/include/Makefile.am b/gschem/include/Makefile.am
index ca13ee0..2a25b20 100644
--- a/gschem/include/Makefile.am
+++ b/gschem/include/Makefile.am
@@ -5,7 +5,7 @@
 noinst_HEADERS = \
 	globals.h i_vars.h prototype.h x_dialog.h x_states.h \
 	gettext.h \
-	x_compselect.h \
+	x_compselect.h x_icons.h \
 	x_log.h x_multiattrib.h x_pagesel.h x_print.h x_preview.h \
 	gschem.h \
 	gschem_defines.h \
diff --git a/gschem/include/gschem.h b/gschem/include/gschem.h
index b5180b7..787cc43 100644
--- a/gschem/include/gschem.h
+++ b/gschem/include/gschem.h
@@ -12,6 +12,7 @@
 #include "x_preview.h"
 #include "x_compselect.h"
 #include "x_dialog.h"
+#include "x_icons.h"
 #include "x_log.h"
 #include "x_multiattrib.h"
 #include "x_pagesel.h"
diff --git a/gschem/include/prototype.h b/gschem/include/prototype.h
index d338a7a..7cd4eaf 100644
--- a/gschem/include/prototype.h
+++ b/gschem/include/prototype.h
@@ -829,6 +829,10 @@ 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(const gchar *bitmap_directory);
+GtkWidget *x_icons_new_toolbar_pixmap(const gchar *stock_id);
+void x_icons_set_default_icon(void);
 /* x_image.c */
 void x_image_lowlevel(GSCHEM_TOPLEVEL *w_current, const char* filename,
 		      int desired_width, int desired_height, char *filetype);
@@ -886,4 +890,3 @@ PAGE *x_window_open_page (GSCHEM_TOPLEVEL *w_current, const gchar *filename);
 void x_window_set_current_page (GSCHEM_TOPLEVEL *w_current, PAGE *page);
 gint x_window_save_page (GSCHEM_TOPLEVEL *w_current, PAGE *page, const gchar *filename);
 void x_window_close_page (GSCHEM_TOPLEVEL *w_current, PAGE *page);
-void x_window_set_default_icon (void);
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 c47bf73..633274f 100644
--- a/gschem/src/Makefile.am
+++ b/gschem/src/Makefile.am
@@ -56,6 +56,7 @@ gschem_SOURCES = \
 	x_event.c \
 	x_fileselect.c \
 	x_grid.c \
+	x_icons.c \
 	x_image.c \
 	x_log.c \
 	x_menus.c \
diff --git a/gschem/src/gschem.c b/gschem/src/gschem.c
index 9b3384a..1da471b 100644
--- a/gschem/src/gschem.c
+++ b/gschem/src/gschem.c
@@ -262,8 +262,11 @@ void main_prog(void *closure, int argc, char *argv[])
   recent_files_load();
   gschem_atexit(recent_files_save, NULL);
 
-  /* Set default icon */
-  x_window_set_default_icon();
+  /* Set default icon; load gschem's ui icons */
+  /* TODO Need public mechanism to get default bitmap directory */
+  extern const char* default_bitmap_directory;
+  x_icons_set_default_icon();
+  x_icons_init(default_bitmap_directory);
 
   /* At end, complete set up of window. */
   colormap = gdk_colormap_get_system ();
diff --git a/gschem/src/x_icons.c b/gschem/src/x_icons.c
new file mode 100644
index 0000000..dcd03f0
--- /dev/null
+++ b/gschem/src/x_icons.c
@@ -0,0 +1,153 @@
+/* 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 <stdio.h>
+
+#include "gschem.h"
+
+#ifdef HAVE_LIBDMALLOC
+#include <dmalloc.h>
+#endif
+
+#define GSCHEM_THEME_ICON_NAME "geda-gschem"
+#define GSCHEM_ICON_SIZES 16, 22, 48
+
+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.
+ *
+ *  Since the application can operate with missing icons, this function
+ *  treats a the failure to load an icon as a warning.
+ *
+ *  \param [in] bitmap_directory The directory containing the toolbar icons.
+ *  The bitmap_directory must not be NULL.
+ */
+void x_icons_init(const gchar *bitmap_directory)
+{
+  GError *error=NULL;
+  GtkIconFactory *factory;
+  gchar *filename;
+  int i;
+  GtkIconSet *icons;
+  GdkPixbuf *pixbuf;
+
+  g_assert(bitmap_directory != NULL);
+
+  factory = gtk_icon_factory_new();
+
+  for (i=0; i<ICONS_SIZE; i++) {
+
+    filename=g_build_filename(bitmap_directory,
+                              icon_table[i].filename, NULL);
+
+    pixbuf = gdk_pixbuf_new_from_file(filename, &error);
+    g_free(filename);
+
+    if (pixbuf == NULL) {
+      g_warning("Loading icons: %s\n",error->message);
+      g_clear_error(&error);
+      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);
+  }
+
+  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 [in] stock_id Name of the stock icon ("new", "open", etc.)
+ * \return Pointer to the new GtkImage object.
+ */
+GtkWidget *x_icons_new_toolbar_pixmap(const gchar *stock_id)
+{
+  return gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_LARGE_TOOLBAR);
+}
+
+/*! \brief Setup default icon for GTK windows
+ *
+ *  \par Function Description
+ *  Sets the default window icon by name, to be found in the current icon
+ *  theme. The name used is #defined above as GSCHEM_THEME_ICON_NAME.
+ *
+ *  For GTK versions < 2.6, we have to load the icons explicitly, and pass
+ *  them to GTK. As we don't know what size is appropriate, a #define above,
+ *  GSCHEM_ICON_SIZES is used to list (comma separated) the sizes we have
+ *  icons for. Icons loaded at all of these sizes are passed to GTK.
+ */
+void x_icons_set_default_icon( void )
+{
+#if GTK_CHECK_VERSION (2,6,0)
+  gtk_window_set_default_icon_name( GSCHEM_THEME_ICON_NAME );
+#else
+  GtkIconTheme *icon_theme;
+  GdkPixbuf *icon;
+  GList *icon_list = NULL;
+  int icon_size[] = { GSCHEM_ICON_SIZES };
+  int i;
+
+  for ( i = 0; i < sizeof( icon_size ) / sizeof( *icon_size ); i++ ) {
+    icon_theme = gtk_icon_theme_get_default();
+    icon = gtk_icon_theme_load_icon( icon_theme,
+                                     GSCHEM_THEME_ICON_NAME,
+                                     icon_size[i],
+                                     0,   /* flags */
+                                     NULL /* **error */ );
+    if (icon != NULL)
+      icon_list = g_list_append( icon_list, icon );
+  }
+
+  gtk_window_set_default_icon_list( icon_list );
+
+  g_list_foreach( icon_list, (GFunc) g_object_unref, NULL );
+  g_list_free( icon_list );
+#endif
+}
+
diff --git a/gschem/src/x_window.c b/gschem/src/x_window.c
index 6d4904c..b6ae680 100644
--- a/gschem/src/x_window.c
+++ b/gschem/src/x_window.c
@@ -223,54 +223,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;
@@ -367,21 +319,21 @@ void x_window_create_main(GSCHEM_TOPLEVEL *w_current)
                              _("New"), 
                              _("New file"), 
                              "toolbar/new", 
-                             x_window_stock_pixmap("new", w_current),
+                             x_icons_new_toolbar_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_new_toolbar_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_new_toolbar_pixmap(GTK_STOCK_SAVE),
                              (GtkSignalFunc) i_callback_toolbar_file_save, 
                              w_current);
     gtk_toolbar_append_space (GTK_TOOLBAR(toolbar)); 
@@ -389,14 +341,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_new_toolbar_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_new_toolbar_pixmap(GTK_STOCK_REDO),
                              (GtkSignalFunc) i_callback_toolbar_edit_redo, 
                              w_current);
     gtk_toolbar_append_space (GTK_TOOLBAR(toolbar)); 
@@ -405,7 +357,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_new_toolbar_pixmap(GSCHEM_STOCK_COMPONENT),
                              (GtkSignalFunc) i_callback_toolbar_add_component, 
                              w_current);
     w_current->toolbar_net = 
@@ -415,7 +367,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_new_toolbar_pixmap(GSCHEM_STOCK_NET),
                                  (GtkSignalFunc) i_callback_toolbar_add_net,
                                  w_current);
     w_current->toolbar_bus = 
@@ -425,7 +377,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_new_toolbar_pixmap(GSCHEM_STOCK_BUS),
                                  (GtkSignalFunc) i_callback_toolbar_add_bus,
                                  w_current);
     /* not part of any radio button group */
@@ -433,7 +385,7 @@ void x_window_create_main(GSCHEM_TOPLEVEL *w_current)
                              _("Text"), 
                              _("Add Text..."), 
                              "toolbar/text", 
-                             x_window_stock_pixmap("text", w_current),
+                             x_icons_new_toolbar_pixmap(GSCHEM_STOCK_TEXT),
                              (GtkSignalFunc) i_callback_toolbar_add_text, 
                              w_current);
     gtk_toolbar_append_space (GTK_TOOLBAR(toolbar)); 
@@ -444,7 +396,7 @@ void x_window_create_main(GSCHEM_TOPLEVEL *w_current)
                                  _("Select"),
                                  _("Select mode"),
                                  "toolbar/select",
-                                 x_window_stock_pixmap("select", w_current),
+                                 x_icons_new_toolbar_pixmap(GSCHEM_STOCK_SELECT),
                                  (GtkSignalFunc) i_callback_toolbar_edit_select, 
                                  w_current);
 
-- 
1.5.4.3



_______________________________________________
geda-dev mailing list
geda-dev@moria.seul.org
http://www.seul.org/cgi-bin/mailman/listinfo/geda-dev