|
GnuCash 2.4.99
|
00001 /* 00002 * gnc-currency-edit.c -- Currency editor widget 00003 * 00004 * Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation 00005 * All rights reserved. 00006 * 00007 * Gnucash is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Library General Public License 00009 * as published by the Free Software Foundation; either version 2 of the 00010 * License, or (at your option) any later version. 00011 * 00012 * Gnucash is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Library General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program; if not, contact: 00019 * 00020 * Free Software Foundation Voice: +1-617-542-5942 00021 * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 00022 * Boston, MA 02110-1301, USA gnu@gnu.org 00023 * 00024 */ 00025 00058 #include "config.h" 00059 00060 #include <gtk/gtk.h> 00061 #include <string.h> 00062 #include <ctype.h> 00063 #include <stdio.h> 00064 00065 #include "gnc-currency-edit.h" 00066 #include "gnc-commodity.h" 00067 #include "gnc-gtk-utils.h" 00068 #include "gnc-ui-util.h" 00069 00070 static void gnc_currency_edit_init (GNCCurrencyEdit *gce); 00071 static void gnc_currency_edit_class_init (GNCCurrencyEditClass *class); 00072 00073 static GtkComboBoxClass *parent_class; 00074 00076 typedef struct _GNCCurrencyEditPrivate 00077 { 00078 gint dummy; 00079 } GNCCurrencyEditPrivate; 00080 00081 #define GET_PRIVATE(o) \ 00082 (G_TYPE_INSTANCE_GET_PRIVATE ((o), GNC_TYPE_CURRENCY_EDIT, GNCCurrencyEditPrivate)) 00083 00087 /* Return the GType for the GNCCurrencyEdit currency selection widget. 00088 */ 00089 GType 00090 gnc_currency_edit_get_type (void) 00091 { 00092 static GType currency_edit_type = 0; 00093 00094 if (currency_edit_type == 0) 00095 { 00096 static const GTypeInfo currency_edit_info = 00097 { 00098 sizeof (GNCCurrencyEditClass), 00099 NULL, 00100 NULL, 00101 (GClassInitFunc) gnc_currency_edit_class_init, 00102 NULL, 00103 NULL, 00104 sizeof (GNCCurrencyEdit), 00105 0, /* n_preallocs */ 00106 (GInstanceInitFunc) gnc_currency_edit_init, 00107 NULL 00108 }; 00109 00110 currency_edit_type = g_type_register_static (GTK_TYPE_COMBO_BOX_ENTRY, 00111 "GNCCurrencyEdit", 00112 ¤cy_edit_info, 0); 00113 } 00114 00115 return currency_edit_type; 00116 } 00117 00118 00125 static void 00126 gnc_currency_edit_class_init (GNCCurrencyEditClass *klass) 00127 { 00128 parent_class = g_type_class_peek_parent (klass); 00129 00130 g_type_class_add_private(klass, sizeof(GNCCurrencyEditPrivate)); 00131 } 00132 00133 00141 static void 00142 gnc_currency_edit_init (GNCCurrencyEdit *gce) 00143 { 00144 } 00145 00146 00157 static void 00158 add_item(gnc_commodity *commodity, GNCCurrencyEdit *gce) 00159 { 00160 const char *string; 00161 00162 string = gnc_commodity_get_printname(commodity); 00163 gtk_combo_box_append_text(GTK_COMBO_BOX(gce), string); 00164 } 00165 00166 00175 static void 00176 fill_currencies(GNCCurrencyEdit *gce) 00177 { 00178 GList *currencies; 00179 00180 currencies = gnc_commodity_table_get_commodities 00181 (gnc_get_current_commodities (), GNC_COMMODITY_NS_CURRENCY); 00182 g_list_foreach(currencies, (GFunc)add_item, gce); 00183 g_list_free(currencies); 00184 } 00185 00186 00187 /* Create a new GNCCurrencyEdit widget which can be used to provide 00188 * an easy way to enter ISO currency codes. 00189 * 00190 * @return A GNCCurrencyEdit widget. 00191 */ 00192 GtkWidget * 00193 gnc_currency_edit_new (void) 00194 { 00195 GNCCurrencyEdit *gce; 00196 GtkListStore *store; 00197 00198 store = gtk_list_store_new (1, G_TYPE_STRING); 00199 gce = g_object_new (GNC_TYPE_CURRENCY_EDIT, 00200 "model", store, 00201 "text-column", 0, 00202 NULL); 00203 g_object_unref (store); 00204 00205 /* Now the signals to make sure the user can't leave the 00206 widget without a valid currency. */ 00207 gnc_cbe_require_list_item(GTK_COMBO_BOX_ENTRY(gce)); 00208 00209 /* Fill in all the data. */ 00210 fill_currencies (gce); 00211 gtk_tree_sortable_set_sort_column_id 00212 (GTK_TREE_SORTABLE(store), 0, GTK_SORT_ASCENDING); 00213 00214 return GTK_WIDGET (gce); 00215 } 00216 00222 /* Set the widget to display a certain currency name. 00223 * 00224 * @param gce The currency editor widget to set. 00225 * 00226 * @param currency The currency to set as the displayed/selected 00227 * value of the widget. 00228 */ 00229 void 00230 gnc_currency_edit_set_currency (GNCCurrencyEdit *gce, 00231 const gnc_commodity *currency) 00232 { 00233 const gchar *printname; 00234 00235 g_return_if_fail(gce != NULL); 00236 g_return_if_fail(GNC_IS_CURRENCY_EDIT(gce)); 00237 g_return_if_fail(currency != NULL); 00238 00239 printname = gnc_commodity_get_printname(currency); 00240 gnc_cbe_set_by_string(GTK_COMBO_BOX_ENTRY(gce), printname); 00241 } 00242 00243 00244 /* Retrieve the displayed currency of the widget. 00245 * 00246 * @param gce The currency editor widget whose values should be retrieved. 00247 * 00248 * @return A pointer to the selected currency (a gnc_commodity 00249 * structure). 00250 */ 00251 gnc_commodity * 00252 gnc_currency_edit_get_currency (GNCCurrencyEdit *gce) 00253 { 00254 gnc_commodity *commodity; 00255 const char *fullname; 00256 char *mnemonic, *name; 00257 GtkTreeModel *model; 00258 GtkTreeIter iter; 00259 GValue value = { 0 }; 00260 00261 g_return_val_if_fail(gce != NULL, NULL); 00262 g_return_val_if_fail(GNC_IS_CURRENCY_EDIT(gce), NULL); 00263 00264 if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(gce), &iter)) 00265 { 00266 model = gtk_combo_box_get_model(GTK_COMBO_BOX(gce)); 00267 gtk_tree_model_get_value(model, &iter, 0, &value); 00268 fullname = g_value_get_string(&value); 00269 mnemonic = g_strdup(fullname); 00270 g_value_unset(&value); 00271 00272 name = strchr(mnemonic, ' '); 00273 if (name != NULL) 00274 *name = '\0'; 00275 commodity = gnc_commodity_table_lookup (gnc_get_current_commodities (), 00276 GNC_COMMODITY_NS_CURRENCY, 00277 mnemonic); 00278 g_free(mnemonic); 00279 } 00280 else 00281 { 00282 g_warning("Combo box returned 'inactive'. Using locale default currency."); 00283 commodity = gnc_locale_default_currency(); 00284 } 00285 00286 00287 return commodity; 00288 } 00289
1.7.4