gEDA-dev: [PATCH 2/3] gattrib: overhaul menu creation code

Ivan Stankovic ivan.stankovic at fer.hr
Sat Jul 21 12:58:27 EDT 2007


---
 gattrib/include/Makefile.am   |    3 +-
 gattrib/include/prototype.h   |    1 -
 gattrib/include/x_menu.h      |  206 -----------------------------------------
 gattrib/lib/Makefile.am       |    4 +-
 gattrib/lib/gattrib-menus.xml |   33 +++++++
 gattrib/src/x_window.c        |  111 ++++++++++++++---------
 6 files changed, 105 insertions(+), 253 deletions(-)
 delete mode 100644 gattrib/include/x_menu.h
 create mode 100644 gattrib/lib/gattrib-menus.xml

diff --git a/gattrib/include/Makefile.am b/gattrib/include/Makefile.am
index 05c77f2..ff01009 100644
--- a/gattrib/include/Makefile.am
+++ b/gattrib/include/Makefile.am
@@ -9,8 +9,7 @@ noinst_HEADERS = \
 	i_vars.h \
 	struct.h \
 	gtkextra-marshal.h \
-	pixmaps.h \
-	x_menu.h
+	pixmaps.h
 
 
 MOSTLYCLEANFILES = *.log core FILE *~ #*#
diff --git a/gattrib/include/prototype.h b/gattrib/include/prototype.h
index 378a5ab..f6e7bcf 100644
--- a/gattrib/include/prototype.h
+++ b/gattrib/include/prototype.h
@@ -290,7 +290,6 @@ gboolean x_fileselect_load_files (GSList *filenames);
 
 /* ------------- x_window.c ------------- */
 void x_window_init();
-void x_window_create_menu(GtkWidget **menubar);
 void x_window_add_items();
 
 
diff --git a/gattrib/include/x_menu.h b/gattrib/include/x_menu.h
deleted file mode 100644
index b7f87e7..0000000
--- a/gattrib/include/x_menu.h
+++ /dev/null
@@ -1,206 +0,0 @@
-/* This is the stuff to define the graphical widgets used 
- * by gattrib.  I had to separate them off from the other
- * structs in order to define the callbacks first.  The
- * include order required is:
-   #include <libgeda/libgeda.h>     
-   #include "../include/struct.h"   
-   #include "../include/prototype.h"
-   #include "../include/globals.h"  
-   #include "../include/x_menu.h"   
- */
-
-
-#ifndef X_MENU_H
-#define X_MENU_H
-
-#include <gtk/gtk.h>
-#include <gdk/gdk.h>
-#include <gdk/gdkkeysyms.h>
-
-#include <glib.h>
-#include <glib-object.h>
- 
-
-/* -------  Includes needed to make the GTK stuff work  ------ */
-
-#include "gtksheet_2_2.h"
-#include "gtkitementry_2_2.h"
-
-/* #include "pixmaps.h" */
-
-/* ----------  Menu definition -- copied from the GTK tutorial file  ---------- */
-/* ----------  and edited/adapted by SDB for gattrib.  ---------- */
-/* This is the GtkItemFactoryEntry structure used to generate new menus.
-   Item 1: The menu path. The letter after the underscore indicates an
-           accelerator key once the menu is open.
-   Item 2: The accelerator key for the entry
-   Item 3: The callback function.
-   Item 4: The callback action.  This changes the parameters with
-           which the function is called.  The default is 0.
-   Item 5: The item type, used to define what kind of an item it is.
-           Here are the possible values:
-
-           NULL               -> "<Item>"
-           ""                 -> "<Item>"
-           "<Title>"          -> create a title item
-           "<Item>"           -> create a simple item
-           "<CheckItem>"      -> create a check item
-           "<ToggleItem>"     -> create a toggle item
-           "<RadioItem>"      -> create a radio item
-           <path>             -> path of a radio item to link against
-           "<Separator>"      -> create a separator
-           "<Branch>"         -> create an item to hold sub items (optional)
-           "<LastBranch>"     -> create a right justified branch 
-*/
-
-GtkItemFactoryEntry menu_items[] = {
-  /* ------- File menu ------- */
-  { "/_File", 
-    NULL,
-    NULL, 
-    0, 
-    "<Branch>" 
-  },
-
-  { "/File/_Open",
-    "<control>O", 
-    s_toplevel_menubar_file_open, 
-    0, 
-    NULL 
-  },
-
-  { "/File/_Save",
-    "<control>S", 
-    s_toplevel_menubar_file_save, 
-    0, 
-    NULL },
-
-  { "/File/sep1",    
-    NULL,    
-    NULL,
-    0, 
-    "<Separator>"
-  },
-
-  { "/File/Print setup",
-    NULL,
-    s_toplevel_menubar_unimplemented_feature,
-    0, 
-    NULL 
-  },
-
-  { "/File/Print",
-    NULL,
-    s_toplevel_menubar_unimplemented_feature,
-    0, 
-    NULL },
-
-  { "/File/sep1",    
-    NULL,    
-    NULL,
-    0, 
-    "<Separator>"
-  },
-
-  { "/File/Export CSV",
-    NULL,
-    s_toplevel_menubar_file_export_csv,
-    0, 
-    NULL 
-  },
-
-  { "/File/sep1",    
-    NULL,    
-    NULL,
-    0, 
-    "<Separator>"
-  },
-
-  { "/File/Quit",   
-    "<control>Q", 
-    gattrib_really_quit,
-    0, 
-    NULL
-  },
-  /* ------- Edit menu ------- */
-  { "/_Edit",    
-    NULL,    
-    NULL, 
-    0, 
-    "<Branch>" 
-  },
-
-  { "/Edit/Add new attrib column",      
-    NULL,  
-    s_toplevel_menubar_edit_newattrib, 
-    0, 
-    NULL
-  },
-
-  { "/Edit/Delete attrib column",  
-    NULL,  
-    s_toplevel_menubar_edit_delattrib,
-    0,
-    NULL 
-  },
-
-  { "/Edit/Search for attrib value",
-    NULL,
-    s_toplevel_menubar_unimplemented_feature,
-    0, 
-    NULL 
-  },
-
-  { "/Edit/Search and replace attrib value", 
-    NULL, 
-    s_toplevel_menubar_unimplemented_feature,
-    0, 
-    NULL 
-  },
-
-  { "/Edit/Search for refdes", 
-    NULL,
-    s_toplevel_menubar_unimplemented_feature,
-    0,
-    NULL 
-  },
-  /* ------- Visibility menu ------- */
-  { "/_Visibility",    
-    NULL,
-    NULL,
-    0, 
-    "<Branch>" 
-  },
-  { "/Visibility/Set selected invisible",
-    NULL,
-    s_visibility_set_invisible,
-    10,
-    NULL
-  },
-  { "/Visibility/Set selected name visible only",
-    NULL,
-    s_visibility_set_name_only,
-    11,
-    NULL
-  },
-  { "/Visibility/Set selected value visible only",
-    NULL,
-    s_visibility_set_value_only,
-    12,
-    NULL
-  },
-  { "/Visibility/Set selected name and value visible",
-    NULL,
-    s_visibility_set_name_and_value,
-    13, 
-    NULL
-  },
-  /* ------- Help menu ------- */
-  { "/_Help",         NULL,         NULL, 0, "<LastBranch>" },
-  { "/_Help/About",   NULL,         x_dialog_about_dialog, 0, NULL },
-};
-
-
-#endif
-
-
diff --git a/gattrib/lib/Makefile.am b/gattrib/lib/Makefile.am
index 34ff2b2..919c3db 100644
--- a/gattrib/lib/Makefile.am
+++ b/gattrib/lib/Makefile.am
@@ -1,8 +1,8 @@
 
 rcdatadir = @GEDARCDIR@
-rcdata_DATA = system-gattribrc 
+rcdata_DATA = system-gattribrc gattrib-menus.xml
 
-EXTRA_DIST = system-gattribrc.in 
+EXTRA_DIST = system-gattribrc.in gattrib-menus.xml
 
 MOSTLYCLEANFILES = *.log *~
 CLEANFILES = *.log *~
diff --git a/gattrib/lib/gattrib-menus.xml b/gattrib/lib/gattrib-menus.xml
new file mode 100644
index 0000000..1526cb3
--- /dev/null
+++ b/gattrib/lib/gattrib-menus.xml
@@ -0,0 +1,33 @@
+<ui>
+  <menubar>
+    <menu action="file">
+      <menuitem action="file-open" />
+      <menuitem action="file-save" />
+      <menuitem action="file-export-csv" />
+      <separator/>
+      <menuitem action="file-print" />
+      <separator/>
+      <menuitem action="file-quit" />
+    </menu>
+
+    <menu action="edit">
+      <menuitem action="edit-add-attrib" />
+      <menuitem action="edit-delete-attrib" />
+      <menuitem action="edit-find-attrib" />
+      <menuitem action="edit-search-replace-attrib-value" />
+      <menuitem action="edit-search-for-refdes" />
+    </menu>
+
+    <menu action="visibility">
+      <menuitem action="visibility-invisible" />
+      <menuitem action="visibility-name-only" />
+      <menuitem action="visibility-value-only" />
+      <menuitem action="visibility-name-value" />
+    </menu>
+
+    <menu action="help">
+      <menuitem action="help-about" />
+    </menu>
+  </menubar>
+</ui>
+
diff --git a/gattrib/src/x_window.c b/gattrib/src/x_window.c
index b09ba0d..8afb72b 100644
--- a/gattrib/src/x_window.c
+++ b/gattrib/src/x_window.c
@@ -55,12 +55,13 @@
 #include "../include/struct.h"     /* typdef and struct declarations */
 #include "../include/prototype.h"  /* function prototypes */
 #include "../include/globals.h"
-#include "../include/x_menu.h"
 
 #ifdef HAVE_LIBDMALLOC
 #include <dmalloc.h>
 #endif
 
+static void
+x_window_create_menu(GtkWidget **menubar);
 
 /* ======================  Public functions  ======================== */
 
@@ -167,52 +168,78 @@ x_window_init()
   
 }
 
-
-/*------------------------------------------------------------------
- * x_window_create_menu:  This creates the menu widget.  This fcn
- * cloned from GTK+ tutorial.
- *------------------------------------------------------------------*/
-void
+static const GtkActionEntry actions[] = {
+  /* name, stock-id, label, accelerator, tooltip, callback function */
+  /* File menu */
+  { "file", NULL, "_File"},
+  { "file-open", GTK_STOCK_OPEN, "Open", "<Control>O", "", s_toplevel_menubar_file_open},
+  { "file-save", GTK_STOCK_SAVE, "Save", "<Control>S", "", s_toplevel_menubar_file_save},
+  { "file-export-csv", NULL, "Export CSV", "", "", s_toplevel_menubar_file_export_csv},
+  { "file-print", GTK_STOCK_PRINT, "Print", "<Control>P", "", s_toplevel_menubar_unimplemented_feature},
+  { "file-quit", GTK_STOCK_QUIT, "Quit", "<Control>Q", "", gattrib_really_quit},
+
+  /* Edit menu */
+  { "edit", NULL, "_Edit"},
+  { "edit-add-attrib", NULL, "Add new attrib column", "", "", s_toplevel_menubar_edit_newattrib},
+  { "edit-delete-attrib", NULL, "Delete attrib column", "", "",s_toplevel_menubar_edit_delattrib},
+  { "edit-find-attrib", GTK_STOCK_FIND, "Find attrib value", "<Control>F", "",s_toplevel_menubar_unimplemented_feature},
+  { "edit-search-replace-attrib-value", NULL, "Search and replace attrib value", "", "",s_toplevel_menubar_unimplemented_feature},
+  { "edit-search-for-refdes", NULL, "Search for refdes", "", "",s_toplevel_menubar_unimplemented_feature},
+
+  /* Visibility menu */
+  { "visibility", NULL, "_Visibility"},
+  { "visibility-invisible", NULL, "Set selected invisible", "", "", s_visibility_set_invisible},
+  { "visibility-name-only", NULL, "Set selected name visible only", "", "", s_visibility_set_name_only},
+  { "visibility-value-only", NULL, "Set selected value visible only", "", "", s_visibility_set_value_only},
+  { "visibility-name-value", NULL, "Set selected name and value visible", "", "", s_visibility_set_name_and_value},
+
+  /* Help menu */
+  { "help", NULL, "_Help"},
+  { "help-about", GTK_STOCK_ABOUT, "About", "", "", x_dialog_about_dialog},
+};
+
+
+/*! \brief Create the menu bar and attach it to the main window.
+ *
+ *  First, the GtkActionGroup object is created and filled with
+ *  entries of type GtkActionEntry (each entry specifies a single
+ *  action, such as opening a file). Then the GtkUIManager object
+ *  is created and used to load menus.xml file with the menu
+ *  description. Finally, the GtkAccelGroup is added to the
+ *  main window to enable keyboard accelerators and a pointer
+ *  to the menu bar is retrieved from the GtkUIManager object.
+ */
+static void
 x_window_create_menu(GtkWidget **menubar)
 {
-  GtkItemFactory *item_factory;
-  GtkAccelGroup *accel_group;
-  gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
-
-  accel_group = gtk_accel_group_new ();
-
-  /* This function initializes the item factory.
-     Param 1: The type of menu - can be GTK_TYPE_MENU_BAR, GTK_TYPE_MENU,
-              or GTK_TYPE_OPTION_MENU.
-     Param 2: The path of the menu.
-     Param 3: A pointer to a gtk_accel_group.  The item factory sets up
-              the accelerator table while generating menus.
-  */
-
-#ifdef DEBUG
-  printf("In x_window_create_menu, about to create new item factory\n");
-#endif
-  item_factory = gtk_item_factory_new (GTK_TYPE_MENU_BAR, "<main>", 
-                                       accel_group);
-
-  /* This function generates the menu items. Pass the item factory,
-     the number of items in the array, the array itself, and any
-     callback data for the the menu items. */
-  /* SDB notes: callback data is pr_current, which should hopefully have pointers
-   * to any data required in the callback. */
-  gtk_item_factory_create_items (item_factory, nmenu_items, menu_items, pr_current);
-
+  gchar *menu_file;
+  GtkUIManager *ui;
+  GtkActionGroup *action_group;
+  GError *error = NULL;
+
+  /* Create and fill the action group object */
+  action_group = gtk_action_group_new("");
+  gtk_action_group_add_actions(action_group, actions, G_N_ELEMENTS(actions), 0);
+
+  /* Create the UI manager object */
+  ui = gtk_ui_manager_new();
+
+  gtk_ui_manager_insert_action_group(ui, action_group, 0);
+
+  menu_file = g_build_filename(GEDADATADIR, "gattrib-menus.xml", NULL);
+  gtk_ui_manager_add_ui_from_file(ui, menu_file, &error);
+  if(error != NULL) {
+    /* An error occured, terminate */
+    fprintf(stderr, "Error loading %s:\n%s\n", menu_file, error->message);
+    exit(1);
+  }
 
-  /* Attach the new accelerator group to the window. */
-  /* SDB says: Here's where it comes in handy to have attached a pointer to 
-   * the main window to the TOPLEVEL structure. */
-  gtk_window_add_accel_group (GTK_WINDOW(pr_current->main_window), accel_group);
+  g_free(menu_file);
 
-  if (menubar)
-    /* Finally, return the actual menu bar created by the item factory. */ 
-    *menubar = gtk_item_factory_get_widget (item_factory, "<main>");
+  gtk_window_add_accel_group (GTK_WINDOW(pr_current->main_window),
+      gtk_ui_manager_get_accel_group(ui));
 
-  return;
+  *menubar = gtk_ui_manager_get_widget(ui, "/ui/menubar/");
 }
 
 
-- 
1.5.2.2


More information about the geda-dev mailing list