From bed1d91aa936816dc1f29c4cd4dd3c0e4edb269f Mon Sep 17 00:00:00 2001 From: David Hampton Date: Mon, 23 Jun 2003 17:53:03 +0000 Subject: [PATCH] Enhance the commodity selection dialog so that it can be limited to currencies, non-currency commodities, or allow all commodities. Fixes #102023 and #115461. git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@8657 57a11ea4-9604-0410-9ed3-97b8803252fd --- ChangeLog | 17 +++ src/gnome-utils/commodity.glade | 2 +- src/gnome-utils/dialog-account.c | 41 +++++-- src/gnome-utils/dialog-commodity.c | 111 ++++++++++-------- src/gnome-utils/dialog-commodity.h | 40 +++++-- src/gnome-utils/gnc-commodity-edit.c | 6 +- src/gnome-utils/gnc-commodity-edit.h | 4 +- .../binary-import/druid-commodity.c | 2 +- src/import-export/import-commodity-matcher.c | 1 + .../qif-import/druid-qif-import.c | 2 +- 10 files changed, 152 insertions(+), 74 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3eaf2088fa..c5691efebd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,20 @@ +2003-06-23 David Hampton + + * src/gnome-utils/dialog-account.c: Limit the commodity choices + based upon the current account type. + + * src/gnome-utils/dialog-commodity.c: Add a new argument to the + dialog creation functions to specify what types of commodities + should be presented. Also tweak the title of the dialog based + upon this argument. Remove the "new" button when the dialog is + limited strictly to currencies. + + * src/gnome-utils/gnc-commodity-edit.c: + * src/import-export/binary-import/druid-commodity.c: + * src/import-export/import-commodity-matcher.c: + * src/import-export/qif-import/druid-qif-import.c: Updated for new + commodity dialog argument. + 2003-06-22 David Hampton * src/gnome-utils/dialog-commodity.[ch]: Clean up usage of the diff --git a/src/gnome-utils/commodity.glade b/src/gnome-utils/commodity.glade index c1953fc776..28cb716f02 100644 --- a/src/gnome-utils/commodity.glade +++ b/src/gnome-utils/commodity.glade @@ -70,7 +70,7 @@ GtkButton - button64 + new_button True True diff --git a/src/gnome-utils/dialog-account.c b/src/gnome-utils/dialog-account.c index bf73c2f5f1..982754448b 100644 --- a/src/gnome-utils/dialog-account.c +++ b/src/gnome-utils/dialog-account.c @@ -30,6 +30,7 @@ #include "AccWindow.h" #include "Transaction.h" #include "dialog-account.h" +#include "dialog-commodity.h" #include "dialog-utils.h" #include "global-options.h" #include "gnc-account-tree.h" @@ -80,6 +81,7 @@ struct _AccountWindow GtkWidget * notes_text; GtkWidget * commodity_edit; + dialog_commodity_mode commodity_mode; GtkWidget * account_scu; GList * valid_types; @@ -145,6 +147,24 @@ aw_get_account (AccountWindow *aw) return xaccAccountLookup (&aw->account, gnc_get_current_book ()); } +static void +gnc_account_commodity_from_type (AccountWindow * aw, gboolean update) +{ + dialog_commodity_mode new_mode; + + if ((aw->type == STOCK) || (aw->type == MUTUAL)) + new_mode = DIAG_COMM_NON_CURRENCY; + else + new_mode = DIAG_COMM_CURRENCY; + + if (update && (new_mode != aw->commodity_mode)) { + gnc_general_select_set_selected(GNC_GENERAL_SELECT (aw->commodity_edit), + NULL); + } + + aw->commodity_mode = new_mode; +} + /* Copy the account values to the GUI widgets */ static void gnc_account_to_ui(AccountWindow *aw) @@ -169,6 +189,7 @@ gnc_account_to_ui(AccountWindow *aw) commodity = xaccAccountGetCommodity (account); gnc_general_select_set_selected (GNC_GENERAL_SELECT (aw->commodity_edit), commodity); + gnc_account_commodity_from_type (aw, FALSE); nonstd_scu = xaccAccountGetNonStdSCU (account); if (nonstd_scu) { @@ -417,6 +438,7 @@ gnc_finish_ok (AccountWindow *aw, commodity = xaccAccountGetCommodity (parent); gnc_general_select_set_selected (GNC_GENERAL_SELECT (aw->commodity_edit), commodity); + gnc_account_commodity_from_type (aw, FALSE); gnc_account_tree_select_account (GNC_ACCOUNT_TREE(aw->parent_tree), parent, TRUE); @@ -1090,6 +1112,7 @@ gnc_type_list_select_cb(GtkCList * type_list, gint row, gint column, aw->type != STOCK && aw->type != MUTUAL); + gnc_account_commodity_from_type (aw, TRUE); gtk_widget_set_sensitive(aw->opening_balance_page, sensitive); if (!sensitive) { @@ -1388,7 +1411,7 @@ gnc_account_window_create(AccountWindow *aw) aw->commodity_edit = gnc_general_select_new (GNC_GENERAL_SELECT_TYPE_SELECT, gnc_commodity_edit_get_string, gnc_commodity_edit_new_select, - NULL); + &aw->commodity_mode); gtk_box_pack_start(GTK_BOX(box), aw->commodity_edit, TRUE, TRUE, 0); gtk_signal_connect (GTK_OBJECT (aw->commodity_edit), "changed", @@ -1629,16 +1652,16 @@ gnc_ui_new_account_window_internal (Account *base_account, gnc_resume_gui_refresh (); - if(default_commodity!=NULL) - { - commodity = default_commodity; - } - else - { - commodity = gnc_default_currency (); - } + if (default_commodity != NULL) { + commodity = default_commodity; + } else if ((aw->type != STOCK) && (aw->type != MUTUAL)) { + commodity = gnc_default_currency (); + } else { + commodity = NULL; + } gnc_general_select_set_selected (GNC_GENERAL_SELECT (aw->commodity_edit), commodity); + gnc_account_commodity_from_type (aw, FALSE); gtk_widget_show_all (aw->dialog); diff --git a/src/gnome-utils/dialog-commodity.c b/src/gnome-utils/dialog-commodity.c index d480e7a022..e034edb499 100644 --- a/src/gnome-utils/dialog-commodity.c +++ b/src/gnome-utils/dialog-commodity.c @@ -88,8 +88,8 @@ static gnc_commodity_help_callback help_callback = NULL; /* The commodity selection window */ static SelectCommodityWindow * -gnc_ui_select_commodity_create(const gnc_commodity * orig_sel); - +gnc_ui_select_commodity_create(const gnc_commodity * orig_sel, + dialog_commodity_mode mode); void gnc_ui_select_commodity_new_cb(GtkButton * button, gpointer user_data); void gnc_ui_select_commodity_changed_cb(GtkEditable * entry, @@ -121,6 +121,7 @@ gnc_ui_commodity_set_help_callback (gnc_commodity_help_callback cb) gnc_commodity * gnc_ui_select_commodity_modal_full(gnc_commodity * orig_sel, GtkWidget * parent, + dialog_commodity_mode mode, const char * user_message, const char * code, const char * fullname, @@ -133,7 +134,7 @@ gnc_ui_select_commodity_modal_full(gnc_commodity * orig_sel, gboolean done; gint value; - win = gnc_ui_select_commodity_create(orig_sel); + win = gnc_ui_select_commodity_create(orig_sel, mode); win->default_exchange_code=code; win->default_fullname=fullname; win->default_mnemonic=mnemonic; @@ -191,10 +192,12 @@ gnc_ui_select_commodity_modal_full(gnc_commodity * orig_sel, gnc_commodity * gnc_ui_select_commodity_modal(gnc_commodity * orig_sel, - GtkWidget * parent) + GtkWidget * parent, + dialog_commodity_mode mode) { return gnc_ui_select_commodity_modal_full(orig_sel, parent, + mode, NULL, NULL, NULL, @@ -207,11 +210,13 @@ gnc_ui_select_commodity_modal(gnc_commodity * orig_sel, ********************************************************************/ static SelectCommodityWindow * -gnc_ui_select_commodity_create(const gnc_commodity * orig_sel) +gnc_ui_select_commodity_create(const gnc_commodity * orig_sel, + dialog_commodity_mode mode) { SelectCommodityWindow * retval = g_new0(SelectCommodityWindow, 1); GladeXML *xml; - const char * namespace; + const char * namespace, *title; + GtkWidget *button; xml = gnc_glade_xml_new ("commodity.glade", "Commodity Selector Dialog"); glade_xml_signal_autoconnect_full( xml, @@ -227,10 +232,26 @@ gnc_ui_select_commodity_create(const gnc_commodity * orig_sel) gtk_label_set_text ((GtkLabel *)retval->select_user_prompt, ""); + switch (mode) { + case DIAG_COMM_ALL: + title = _("Select currency/security"); + break; + case DIAG_COMM_NON_CURRENCY: + title = _("Select security"); + break; + case DIAG_COMM_CURRENCY: + default: + title = _("Select currency"); + button = glade_xml_get_widget (xml, "new_button"); + gtk_widget_destroy(button); + break; + } + gtk_window_set_title (GTK_WINDOW(retval->dialog), title); + /* build the menus of namespaces and commodities */ gnc_ui_update_namespace_picker(retval->namespace_combo, gnc_commodity_get_namespace(orig_sel), - TRUE, FALSE); + mode); namespace = gnc_ui_namespace_picker_ns(retval->namespace_combo); gnc_ui_update_commodity_picker(retval->commodity_combo, namespace, gnc_commodity_get_printname(orig_sel)); @@ -270,7 +291,7 @@ gnc_ui_select_commodity_new_cb(GtkButton * button, if(new_commodity) { gnc_ui_update_namespace_picker(w->namespace_combo, gnc_commodity_get_namespace(new_commodity), - TRUE, FALSE); + DIAG_COMM_ALL); gnc_ui_update_commodity_picker(w->commodity_combo, gnc_commodity_get_namespace(new_commodity), gnc_commodity_get_printname(new_commodity)); @@ -406,56 +427,50 @@ gnc_ui_update_commodity_picker(GtkWidget * combobox, void gnc_ui_update_namespace_picker(GtkWidget * combobox, const char * init_string, - gboolean include_iso, - gboolean include_all) + dialog_commodity_mode mode) { - GList * namespaces; + GList * namespaces, *node; const char * active; /* fetch a list of the namespaces */ - if (!include_all) - namespaces = - gnc_commodity_table_get_namespaces (gnc_get_current_commodities()); - else - { - namespaces = NULL; - namespaces = g_list_prepend (namespaces, GNC_COMMODITY_NS_ISO); - namespaces = g_list_prepend (namespaces, GNC_COMMODITY_NS_NASDAQ); - namespaces = g_list_prepend (namespaces, GNC_COMMODITY_NS_NYSE); - namespaces = g_list_prepend (namespaces, GNC_COMMODITY_NS_EUREX); - namespaces = g_list_prepend (namespaces, GNC_COMMODITY_NS_MUTUAL); - namespaces = g_list_prepend (namespaces, GNC_COMMODITY_NS_ASX); - namespaces = g_list_prepend (namespaces, GNC_COMMODITY_NS_AMEX); + switch (mode) { + case DIAG_COMM_ALL: + namespaces = + gnc_commodity_table_get_namespaces (gnc_get_current_commodities()); + break; + + case DIAG_COMM_NON_CURRENCY: + namespaces = + gnc_commodity_table_get_namespaces (gnc_get_current_commodities()); + node = g_list_find_custom (namespaces, GNC_COMMODITY_NS_ISO, g_strcmp); + if (node) { + namespaces = g_list_remove_link (namespaces, node); + g_list_free_1 (node); + } else { + node->data = "CURRENCY"; + } + + if (gnc_commodity_namespace_is_iso (init_string)) + init_string = NULL; + break; + + case DIAG_COMM_CURRENCY: + default: + namespaces = g_list_prepend (NULL, "CURRENCY"); + break; } - namespaces = g_list_sort(namespaces, g_strcmp); - - { - GList *node; - - node = g_list_find_custom (namespaces, GNC_COMMODITY_NS_ISO, g_strcmp); - if (node && !include_iso) - { - namespaces = g_list_remove_link (namespaces, node); - g_list_free_1 (node); - } - else - node->data = "CURRENCY"; - - node = g_list_find_custom (namespaces, GNC_COMMODITY_NS_LEGACY, g_strcmp); - if (node) - { - namespaces = g_list_remove_link (namespaces, node); - g_list_free_1 (node); - } + /* Legacy namespace should never be seen */ + node = g_list_find_custom (namespaces, GNC_COMMODITY_NS_LEGACY, g_strcmp); + if (node) { + namespaces = g_list_remove_link (namespaces, node); + g_list_free_1 (node); } /* stick them in the combobox */ + namespaces = g_list_sort(namespaces, g_strcmp); gtk_combo_set_popdown_strings (GTK_COMBO (combobox), namespaces); - if (!include_iso && gnc_commodity_namespace_is_iso (init_string)) - init_string = NULL; - /* set the entry text */ if (init_string) active = init_string; @@ -615,7 +630,7 @@ gnc_ui_new_commodity_dialog(const char * selected_namespace, gtk_entry_set_text (GTK_ENTRY (retval->mnemonic_entry), mnemonic ? mnemonic : ""); gnc_ui_update_namespace_picker(retval->namespace_combo, selected_namespace, - include_iso, TRUE); + include_iso ? DIAG_COMM_ALL : DIAG_COMM_NON_CURRENCY); gtk_entry_set_text (GTK_ENTRY (retval->code_entry), cusip ? cusip : ""); if (fraction > 0) gtk_spin_button_set_value (GTK_SPIN_BUTTON (retval->fraction_spinbutton), diff --git a/src/gnome-utils/dialog-commodity.h b/src/gnome-utils/dialog-commodity.h index e9ce6207f3..8cd18f305d 100644 --- a/src/gnome-utils/dialog-commodity.h +++ b/src/gnome-utils/dialog-commodity.h @@ -36,6 +36,21 @@ #include "gnc-commodity.h" #include "gnc-engine.h" +/** The dialog commodity types are used to determine what commodity + * namespaces the currency dialog will present to a user. These + * values can be safely changed from one release to the next. Note + * that if values are added, the routines in dialog-commodity.c will + * need to be updated to match. + */ +typedef enum { + DIAG_COMM_CURRENCY, /**< Dialog box should only allow selection + of a currency. */ + DIAG_COMM_NON_CURRENCY, /**< Dialog box should allow selection of + anything but a currency. */ + DIAG_COMM_ALL, /**< Dialog box should allow selection of + anything. */ +} dialog_commodity_mode; + typedef void (* gnc_commodity_help_callback)(void); /** This function is used to set the action routine for the help @@ -79,12 +94,16 @@ void gnc_ui_commodity_set_help_callback (gnc_commodity_help_callback cb); * instruction providing this commodity's mnemonic, and this will be * the default mnemonic for any newly created commodities. * + * @param mode Determines which namespaces the user may select a + * commodity from. + * * @return The commodity selected. May or may not be a newly created * commodity. */ gnc_commodity * gnc_ui_select_commodity_modal_full(gnc_commodity * orig_sel, GtkWidget * parent, + dialog_commodity_mode mode, const char * user_message, const char * exchange_code, const char * fullname, @@ -100,12 +119,18 @@ gnc_ui_select_commodity_modal_full(gnc_commodity * orig_sel, * @param orig_sel A pointer to a commodity that should initially be * selected in the dialog box. * + * @param parent The parent window for this new selection window. + * + * @param mode Determines which namespaces the user may select a + * commodity from. + * * @return The commodity selected. May or may not be a newly created * commodity. */ gnc_commodity * gnc_ui_select_commodity_modal(gnc_commodity * orig_sel, - GtkWidget * parent); + GtkWidget * parent, + dialog_commodity_mode mode); /** @} */ @@ -194,14 +219,8 @@ gnc_ui_edit_commodity_modal(gnc_commodity *commodity, * @param sel The namespace that should be initially selected when * the combo box appears. * - * @param include_iso Set to TRUE if the combo box should inlude the - * ISO4217 namespace for currencies. FALSE if the currency namespace - * should not be included. This flag has precedence over the - * following flag. - * - * @param include_all Set to TRUE if the combo box should include all - * known namespaces, both application and user defined. FALSE if - * only the default application namespaces should be included. + * @param mode Determines which namespaces the user may select a + * commodity * * @return The currently selected namespace. * @@ -209,8 +228,7 @@ gnc_ui_edit_commodity_modal(gnc_commodity *commodity, */ void gnc_ui_update_namespace_picker(GtkWidget * combobox, const char * sel, - gboolean include_iso, - gboolean include_all); + dialog_commodity_mode mode); /** Given a combo box, return the currently selected namespaces. * diff --git a/src/gnome-utils/gnc-commodity-edit.c b/src/gnome-utils/gnc-commodity-edit.c index 3420595477..27134d9f91 100644 --- a/src/gnome-utils/gnc-commodity-edit.c +++ b/src/gnome-utils/gnc-commodity-edit.c @@ -48,7 +48,11 @@ gpointer gnc_commodity_edit_new_select (gpointer arg, gpointer ptr, GtkWidget *toplevel) { gnc_commodity * comm = (gnc_commodity *)ptr; - return gnc_ui_select_commodity_modal(comm, toplevel); + dialog_commodity_mode *mode_ptr = arg; + dialog_commodity_mode mode; + + mode = mode_ptr ? *mode_ptr : DIAG_COMM_ALL; + return gnc_ui_select_commodity_modal(comm, toplevel, mode); } /* diff --git a/src/gnome-utils/gnc-commodity-edit.h b/src/gnome-utils/gnc-commodity-edit.h index 2b4a33f0b1..49ca90549c 100644 --- a/src/gnome-utils/gnc-commodity-edit.h +++ b/src/gnome-utils/gnc-commodity-edit.h @@ -40,8 +40,8 @@ const char * gnc_commodity_edit_get_string (gpointer ptr); /* Callback function to popup a new selection (modal) dialog. * - * NOTE: The argument here is ignored, so you can safely set the - * cb_arg to NULL when you create the general_select widget + * The generic argument is a pointer to a dialog_commodity_mode + * enum. This tells the dialog how to limit the namespaces provided. */ gpointer gnc_commodity_edit_new_select (gpointer arg, gpointer ptr, GtkWidget *toplevel); diff --git a/src/import-export/binary-import/druid-commodity.c b/src/import-export/binary-import/druid-commodity.c index 6ebc8077d9..502209c431 100644 --- a/src/import-export/binary-import/druid-commodity.c +++ b/src/import-export/binary-import/druid-commodity.c @@ -283,7 +283,7 @@ make_commodity_druid_page(gnc_commodity * comm) gnc_ui_update_namespace_picker(retval->new_type_combo, gnc_commodity_get_namespace(comm), - TRUE, TRUE); + DIAG_COMM_ALL); temp = gtk_hbox_new(FALSE, 0); gtk_box_pack_start(GTK_BOX(top_vbox), temp, FALSE, FALSE, 5); diff --git a/src/import-export/import-commodity-matcher.c b/src/import-export/import-commodity-matcher.c index 813287a90d..c9945f756c 100755 --- a/src/import-export/import-commodity-matcher.c +++ b/src/import-export/import-commodity-matcher.c @@ -117,6 +117,7 @@ gnc_commodity * gnc_import_select_commodity(char * exchange_code, { retval=gnc_ui_select_commodity_modal_full(NULL, NULL, + DIAG_COMM_ALL, _("Please select a commodity to match the following exchange specific code.\nPlease note that the exchange code of the commodity you select will be overwritten.\n"), exchange_code, default_fullname, diff --git a/src/import-export/qif-import/druid-qif-import.c b/src/import-export/qif-import/druid-qif-import.c index b46d94c63c..616d42de04 100644 --- a/src/import-export/qif-import/druid-qif-import.c +++ b/src/import-export/qif-import/druid-qif-import.c @@ -1583,7 +1583,7 @@ make_qif_druid_page(gnc_commodity * comm) gnc_ui_update_namespace_picker(retval->new_type_combo, gnc_commodity_get_namespace(comm), - TRUE, TRUE); + DIAG_COMM_ALL); info_label = gtk_label_new(_("Enter the full name of the commodity, "