From a8f15f1aa7f9553c85824e0e5ea281e5ee00abc9 Mon Sep 17 00:00:00 2001 From: Linas Vepstas Date: Wed, 18 Mar 1998 04:23:34 +0000 Subject: [PATCH] patches from rob browning git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@656 57a11ea4-9604-0410-9ed3-97b8803252fd --- src/gnome/MainWindow.c | 122 +++++++++++++++++-- src/gnome/MainWindow.h | 12 +- src/gnome/MenuBar.c | 261 ++++++++++++++++++++--------------------- src/gnome/MenuBar.h | 39 ++++++ src/gnome/main.c | 50 +++++--- src/gnome/main.h | 12 +- 6 files changed, 338 insertions(+), 158 deletions(-) create mode 100644 src/gnome/MenuBar.h diff --git a/src/gnome/MainWindow.c b/src/gnome/MainWindow.c index 4e13f5a0eb..bcda1de1de 100644 --- a/src/gnome/MainWindow.c +++ b/src/gnome/MainWindow.c @@ -24,6 +24,7 @@ \********************************************************************/ #include "MainWindow.h" +#include "MenuBar.h" #include "messages.h" #include "main.h" @@ -39,7 +40,32 @@ struct main_window { }; -void main_window_init() +/* This should most likely be rewritting to use a Tree, not a Clist so + we can represent the heirarchical account structure */ +static void +cram_accts_into_clist(GtkCList *list, AccountGroup *accts) { + int count = xaccGetNumAccounts(accts); + int i; + GtkWidget *item; + + for(i=0; iaccountName, + acc->description, + acc->notes }; + + gint new_row = gtk_clist_append(GTK_CLIST(list), rowstrs); + + /* Set the row to point to the actual account so we can reach it + trivially when the user selects the row. (Should we use + gtk_*_data_full and have a destroy notify?) */ + gtk_clist_set_row_data(GTK_CLIST(list), new_row, acc); + } +} + +void +main_window_init(AccountGroup *accts) { GtkWidget *window; @@ -56,23 +82,98 @@ void main_window_init() GtkWidget *clist; GtkWidget *list_item; - GtkAcceleratorTable *accel; + /* this is the GtkMenuEntry structure used to create new menus. The + first member is the menu definition string. The second, the default + accelerator key used to access this menu function with the keyboard. + The third is the callback function to call when this menu item is + selected (by the accelerator key, or with the mouse.) The last + member is the data to pass to your callback function. */ + + GtkMenuEntry menu_items[] = + { + {"
/File/New", "N", NULL, NULL}, + {"
/File/Open", "O", file_cmd_open, NULL}, + {"
/File/Save", "S", NULL, NULL}, + {"
/File/Save as", NULL, NULL, NULL}, + {"
/File/", NULL, NULL, NULL}, + {"
/File/Quit", "Q", gtk_main_quit, "OK, I'll quit"}, + {"
/Options/General..", "A", NULL, NULL}, + {"
/Help/About..", NULL, NULL, NULL} + }; + + /* calculate the number of menu_item's */ + int nmenu_items = sizeof(menu_items) / sizeof(menu_items[0]); + + MenuBar *main_menu_bar; + clist = gtk_clist_new_with_titles(3, clist_titles); /* Fix the column widths */ gtk_clist_set_column_width ( GTK_CLIST(clist), 1, 85 ); gtk_clist_set_column_width ( GTK_CLIST(clist), 0, 85 ); + cram_accts_into_clist(GTK_CLIST(clist), accts); + window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(window), "GnoMoney"); main_vbox = gtk_vbox_new(FALSE, 1); gtk_container_border_width(GTK_CONTAINER(main_vbox), 1); gtk_container_add(GTK_CONTAINER(window), main_vbox); + + { + MenuBarGroup *mbg = menuBarGroupCreate(); + main_menu_bar = menuBarCreate(mbg, "
"); + menuBarGroupAddItems(mbg, menu_items, nmenu_items); + } + + menubar = main_menu_bar->widget; + accel = main_menu_bar->table; + +#if 0 + + { + /* Here's how to use the new MenBar stuff to create multiple menu + bars */ + + GtkWidget *test_win = gtk_window_new (GTK_WINDOW_TOPLEVEL); + GtkWidget *test_vbox = gtk_vbox_new(FALSE, 1); + MenuBarGroup *mbg = menuBarGroupCreate(); + MenuBar *mb1; + MenuBar *mb2; + GtkMenuEntry items[] = + { + {"/Eat/Grubs", "N", NULL, NULL}, + {"/Eat/Gravel", "O", NULL, NULL}, + {"/So/Scary", "S", NULL, NULL}, + {"/Eat-2/Grubs", "N", NULL, NULL}, + {"/Eat-2/Gravel", "O", NULL, NULL}, + {"/So-2/Scary/Err/Umm", "S", NULL, NULL} + }; + int nitems = sizeof(items) / sizeof(items[0]); + + mb1 = menuBarCreate(mbg, ""); + mb2 = menuBarCreate(mbg, ""); + + menuBarGroupAddItems(mbg, items, nitems); + + gtk_window_set_title(GTK_WINDOW(test_win), "Stumpy"); + gtk_container_add(GTK_CONTAINER(test_win), test_vbox); + gtk_box_pack_start(GTK_BOX(test_vbox), menuBarGetWidget(mb1), + FALSE, TRUE, 0); + gtk_box_pack_start(GTK_BOX(test_vbox), menuBarGetWidget(mb2), + FALSE, TRUE, 0); + + gtk_widget_show(menuBarGetWidget(mb1)); + gtk_widget_show(menuBarGetWidget(mb2)); + gtk_widget_show(test_vbox); + gtk_widget_show(test_win); + + } +#endif - get_main_menu(&menubar, &accel); gtk_window_add_accelerator_table(GTK_WINDOW(window), accel); gtk_box_pack_start(GTK_BOX(main_vbox), menubar, FALSE, TRUE, 0); gtk_widget_show(menubar); @@ -133,11 +234,18 @@ void main_window_init() /* Show everything now that it is created */ gtk_widget_show(main_vbox); gtk_container_border_width (GTK_CONTAINER (window), 2); - gtk_widget_show(window); - - - + gtk_widget_show(window); } /********************* END OF FILE **********************************/ + +/* + Local Variables: + tab-width: 2 + indent-tabs-mode: nil + mode: c-mode + c-indentation-style: gnu + eval: (c-set-offset 'block-open '-) + End: +*/ diff --git a/src/gnome/MainWindow.h b/src/gnome/MainWindow.h index e72a5fa06b..ec025a6951 100644 --- a/src/gnome/MainWindow.h +++ b/src/gnome/MainWindow.h @@ -34,7 +34,7 @@ /** PROTOTYPES ******************************************************/ void refreshMainWindow( void ); -void main_window_init( ); +void main_window_init(AccountGroup *); void xaccMainWindowAddAccount ( GtkWidget * ); @@ -78,3 +78,13 @@ enum { #define exit 4 #endif + +/* + Local Variables: + tab-width: 2 + indent-tabs-mode: nil + mode: c-mode + c-indentation-style: gnu + eval: (c-set-offset 'block-open '-) + End: +*/ diff --git a/src/gnome/MenuBar.c b/src/gnome/MenuBar.c index 8694109076..3275ceb2e8 100644 --- a/src/gnome/MenuBar.c +++ b/src/gnome/MenuBar.c @@ -1,156 +1,151 @@ -// WaterMark/GnoMoney -// mainMenu.c -// from Gtk tutorial +/* MenuBar.c -- GTK menu functions for xacc (X-Accountant) Copyright + (C) 1998 Jeremy Collins + (C) 1998 Rob Browning + + 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., 675 Mass Ave, Cambridge, MA 02139, USA. + + Author: Jeremy Collins + Internet: linux@cyberramp.net +*/ #include #include +#include "MenuBar.h" -#include "main.h" - -static void menus_remove_accel(GtkWidget * widget, gchar * signal_name, gchar * - path); -static gint menus_install_accel(GtkWidget * widget, gchar * signal_name, gchar -key, gchar modifiers, gchar * path); -void menus_init(void); -void menus_create(GtkMenuEntry * entries, int nmenu_entries); - - -/* this is the GtkMenuEntry structure used to create new menus. The - * first member is the menu definition string. The second, the - * default accelerator key used to access this menu function with - * the keyboard. The third is the callback function to call when - * this menu item is selected (by the accelerator key, or with the - * mouse.) The last member is the data to pass to your callback function. - */ - -static GtkMenuEntry menu_items[] = -{ - {"
/File/New", "N", NULL, NULL}, - {"
/File/Open", "O", file_cmd_open, NULL}, - {"
/File/Save", "S", NULL, NULL}, - {"
/File/Save as", NULL, NULL, NULL}, - {"
/File/", NULL, NULL, NULL}, - {"
/File/Quit", "Q", gtk_main_quit, "OK, I'll quit"}, - {"
/Account/General..", "A", NULL, NULL}, - {"
/Help/About..", NULL, NULL, NULL} -}; - -/* calculate the number of menu_item's */ -static int nmenu_items = sizeof(menu_items) / sizeof(menu_items[0]); - -static int initialize = TRUE; -static GtkMenuFactory *factory = NULL; -static GtkMenuFactory *subfactory[1]; +#if 0 static GHashTable *entry_ht = NULL; -void get_main_menu(GtkWidget ** menubar, GtkAcceleratorTable ** table) +static gint +menus_install_accel(GtkWidget * widget, gchar * signal_name, gchar + key, gchar modifiers, gchar * path) { - if (initialize) - menus_init(); - - if (menubar) - *menubar = subfactory[0]->widget; - if (table) - *table = subfactory[0]->table; + char accel[64]; + char *t1, t2[2]; + + accel[0] = '\0'; + if (modifiers & GDK_CONTROL_MASK) strcat(accel, ""); + if (modifiers & GDK_SHIFT_MASK) strcat(accel, ""); + if (modifiers & GDK_MOD1_MASK) strcat(accel, ""); + + t2[0] = key; + t2[1] = '\0'; + strcat(accel, t2); + + if (entry_ht) { + t1 = g_hash_table_lookup(entry_ht, path); + g_free(t1); + } else { + entry_ht = g_hash_table_new(g_str_hash, g_str_equal); + } + g_hash_table_insert(entry_ht, path, g_strdup(accel)); + + return TRUE; } -void menus_init(void) +static void +menus_remove_accel(GtkWidget * widget, gchar * signal_name, gchar *path) { - if (initialize) { - initialize = FALSE; + char *t; + + if (entry_ht) { + t = g_hash_table_lookup(entry_ht, path); + g_free(t); + + g_hash_table_insert(entry_ht, path, g_strdup("")); + } +} - factory = gtk_menu_factory_new(GTK_MENU_FACTORY_MENU_BAR); - subfactory[0] = gtk_menu_factory_new(GTK_MENU_FACTORY_MENU_BAR); +#endif - gtk_menu_factory_add_subfactory(factory, subfactory[0], "
"); - menus_create(menu_items, nmenu_items); +MenuBarGroup *menuBarGroupCreate() +{ + GtkMenuFactory * f; + f = gtk_menu_factory_new(GTK_MENU_FACTORY_MENU_BAR); + return(f); +} + +void +menuBarGroupAddItems(MenuBarGroup *group, + GtkMenuEntry items[], guint nitems) +{ +#if 0 + int i; + char *accelerator; + + if (entry_ht) { + for (i = 0; i < nitems; i++) { + accelerator = g_hash_table_lookup(entry_ht, items[i].path); + if (accelerator) { + if (accelerator[0] == '\0') + items[i].accelerator = NULL; + else + items[i].accelerator = accelerator; + } } + } +#endif + + gtk_menu_factory_add_entries(group, items, nitems); + +#if 0 + for(i = 0; i < nitems; i++) + { + if (items[i].widget) { + gtk_signal_connect(GTK_OBJECT(items[i].widget), "install_accelerator", + GTK_SIGNAL_FUNC(menus_install_accel), + items[i].path); + gtk_signal_connect(GTK_OBJECT(items[i].widget), "remove_accelerator", + GTK_SIGNAL_FUNC(menus_remove_accel), + items[i].path); + } + } +#endif + } -void menus_create(GtkMenuEntry * entries, int nmenu_entries) +MenuBar *menuBarCreate(MenuBarGroup *group, const gchar menuBarName[]) { - char *accelerator; - int i; + GtkMenuFactory *subfactory = gtk_menu_factory_new(GTK_MENU_FACTORY_MENU_BAR); + gtk_menu_factory_add_subfactory(group, subfactory, menuBarName); - if (initialize) - menus_init(); - - if (entry_ht) - for (i = 0; i < nmenu_entries; i++) { - accelerator = g_hash_table_lookup(entry_ht, entries[i].path); - if (accelerator) { - if (accelerator[0] == '\0') - entries[i].accelerator = NULL; - else - entries[i].accelerator = accelerator; - } - } - gtk_menu_factory_add_entries(factory, entries, nmenu_entries); - - for (i = 0; i < nmenu_entries; i++) - if (entries[i].widget) { - gtk_signal_connect(GTK_OBJECT(entries[i].widget), "install_accelerator", - GTK_SIGNAL_FUNC(menus_install_accel), - entries[i].path); - gtk_signal_connect(GTK_OBJECT(entries[i].widget), "remove_accelerator", - GTK_SIGNAL_FUNC(menus_remove_accel), - entries[i].path); - } + return(subfactory); } -static gint menus_install_accel(GtkWidget * widget, gchar * signal_name, gchar -key, gchar modifiers, gchar * path) + +GtkWidget *menuBarGroupFindItem(MenuBarGroup *group, const gchar path[]) { - char accel[64]; - char *t1, t2[2]; + GtkMenuPath *mp; + mp = gtk_menu_factory_find(group, path); + if(mp) { + return(mp->widget); + } else { + return NULL; + } +} - accel[0] = '\0'; - if (modifiers & GDK_CONTROL_MASK) - strcat(accel, ""); - if (modifiers & GDK_SHIFT_MASK) - strcat(accel, ""); - if (modifiers & GDK_MOD1_MASK) - strcat(accel, ""); +GtkWidget *menuBarGetWidget(MenuBar *mb) +{ + return mb->widget; +} - t2[0] = key; - t2[1] = '\0'; - strcat(accel, t2); - -/* if (entry_ht) { - t1 = g_hash_table_lookup(entry_ht, path); - g_free(t1); - } else - entry_ht = g_hash_table_new(g_string_hash, g_string_equal); +/* + Local Variables: + tab-width: 2 + indent-tabs-mode: nil + mode: c-mode + c-indentation-style: gnu + eval: (c-set-offset 'block-open '-) + End: */ - g_hash_table_insert(entry_ht, path, g_strdup(accel)); - - return TRUE; -} - -static void menus_remove_accel(GtkWidget * widget, gchar * signal_name, gchar * - path) -{ - char *t; - - if (entry_ht) { - t = g_hash_table_lookup(entry_ht, path); - g_free(t); - - g_hash_table_insert(entry_ht, path, g_strdup("")); - } -} - -void menus_set_sensitive(char *path, int sensitive) -{ - GtkMenuPath *menu_path; - - if (initialize) - menus_init(); - - menu_path = gtk_menu_factory_find(factory, path); - if (menu_path) - gtk_widget_set_sensitive(menu_path->widget, sensitive); - else - g_warning("Unable to set sensitivity for menu which doesn't exist: -%s", path); -} diff --git a/src/gnome/MenuBar.h b/src/gnome/MenuBar.h new file mode 100644 index 0000000000..a637aa149f --- /dev/null +++ b/src/gnome/MenuBar.h @@ -0,0 +1,39 @@ +/* MenuBar.h -- GTK menu functions for xacc (X-Accountant) Copyright + (C) 1998 Rob Browning + + 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., 675 Mass Ave, Cambridge, MA 02139, USA. + + Author: Rob Browning + Internet: rlb@cs.utexas.edu +*/ + +#ifndef __MENUBAR_H__ +#define __MENUBAR_H__ + +typedef GtkMenuFactory MenuBarGroup; +typedef GtkMenuFactory MenuBar; + +/* Order must be: create group, create menu bar, add items. */ + +MenuBarGroup *menuBarGroupCreate(); +void menuBarGroupAddItems(MenuBarGroup *group, + GtkMenuEntry items[], guint nitems); + +MenuBar *menuBarCreate(MenuBarGroup *group, const gchar menuBarName[]); +GtkWidget *menuBarGroupFindItem(MenuBarGroup *group, const gchar path[]); + +GtkWidget *menuBarGetWidget(MenuBar *mb); + +#endif diff --git a/src/gnome/main.c b/src/gnome/main.c index a512d24acf..af8a74aa84 100644 --- a/src/gnome/main.c +++ b/src/gnome/main.c @@ -76,8 +76,7 @@ void file_ok_sel (GtkWidget *w, GtkFileSelection *fs) topgroup->new = TRUE; } - main_window_init (); - + main_window_init(topgroup); } void destroy (GtkWidget *widget, gpointer *data) @@ -85,7 +84,7 @@ void destroy (GtkWidget *widget, gpointer *data) gtk_main_quit (); } -void file_cmd_open () +void file_cmd_open (GtkWidget *widget, gpointer data) { g_print ( "Menu Command: file open\n" ); } @@ -107,6 +106,16 @@ main( int argc, char *argv[] ) { gtk_init (&argc, &argv); + { + /* Use a nicer font IMO, if available */ + char font[] = "-adobe-helvetica-medium-r-normal--*-100-*-*-*-*-*-*"; + GtkStyle *st = gtk_widget_get_default_style(); + GdkFont *f = gdk_font_load(font); + if(st && f) { + st->font = f; + } + } + filebox = gtk_file_selection_new ( "Open..." ); /* read in the filename (should be the first arg after all @@ -145,7 +154,7 @@ main( int argc, char *argv[] ) } } /* Create main window */ - main_window_init(); + main_window_init(topgroup); } else { @@ -156,27 +165,36 @@ main( int argc, char *argv[] ) /* Check to see if this is a valid datafile */ if ( datafile != NULL ) topgroup = xaccReadAccountGroup (datafile); - - + + /* Callbacks for File Box and Stuff */ - + gtk_signal_connect (GTK_OBJECT (filebox), "delete_event", - (GtkSignalFunc) gtk_widget_destroy, - GTK_OBJECT(filebox)); - + (GtkSignalFunc) gtk_widget_destroy, + GTK_OBJECT(filebox)); + /* Connect the ok_button to file_ok_sel function */ gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (filebox)->ok_button), - "clicked", (GtkSignalFunc) file_ok_sel, filebox ); - + "clicked", (GtkSignalFunc) file_ok_sel, filebox ); + /* Connect the cancel_button to also kill the app */ gtk_signal_connect_object ( GTK_OBJECT (GTK_FILE_SELECTION (filebox)->cancel_button), - "clicked", (GtkSignalFunc) gtk_exit, NULL ); - - - + "clicked", (GtkSignalFunc) gtk_exit, NULL ); + + + /* Enter event loop */ gtk_main(); return 0; } +/* + Local Variables: + tab-width: 2 + indent-tabs-mode: nil + mode: c-mode + c-indentation-style: gnu + eval: (c-set-offset 'block-open '-) + End: +*/ diff --git a/src/gnome/main.h b/src/gnome/main.h index d4231ca4a5..80a7bff7f2 100644 --- a/src/gnome/main.h +++ b/src/gnome/main.h @@ -51,9 +51,19 @@ /** PROTOTYPES ******************************************************/ void destroy (GtkWidget *widget, gpointer *data); -void file_cmd_open (void); +void file_cmd_open (GtkWidget *widget, gpointer data); /** GLOBALS *********************************************************/ extern char *helpPath; #endif + +/* + Local Variables: + tab-width: 2 + indent-tabs-mode: nil + mode: c-mode + c-indentation-style: gnu + eval: (c-set-offset 'block-open '-) + End: +*/