GnuCash 7574033+
Loading...
Searching...
No Matches
gnc-currency-edit.cpp
1/*
2 * gnc-currency-edit.c -- Currency editor widget
3 *
4 * Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation
5 * All rights reserved.
6 *
7 * Gnucash is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public License
9 * as published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
11 *
12 * Gnucash is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, contact:
19 *
20 * Free Software Foundation Voice: +1-617-542-5942
21 * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
22 * Boston, MA 02110-1301, USA gnu@gnu.org
23 *
24 */
25
48
49#include <config.h>
50
51#include <gtk/gtk.h>
52#include <glib/gi18n.h>
53#include <string.h>
54#include <ctype.h>
55#include <stdio.h>
56
57#include <algorithm>
58
59#include "gnc-currency-edit.h"
60#include "gnc-commodity.h"
61#include "gnc-gtk-utils.h"
62#include "gnc-ui-util.h"
63#include "gnc-engine.h"
64#include "dialog-utils.h"
65
67static QofLogModule log_module = GNC_MOD_GUI;
68
69static void gnc_currency_edit_finalize (GObject *object);
70static void gnc_currency_edit_mnemonic_changed (GObject *gobject,
71 GParamSpec *pspec,
72 gpointer user_data);
73static void gnc_currency_edit_active_changed (GtkComboBox *gobject,
74 gpointer user_data);
75
77{
78 GtkComboBox combobox;
79
80 glong widest;
81 gchar *mnemonic;
82};
83
84G_DEFINE_TYPE(GNCCurrencyEdit, gnc_currency_edit, GTK_TYPE_COMBO_BOX)
85
86
88
89enum
90{
91 PROP_0,
92
93 PROP_GCE_MNEMONIC,
94
95 N_PROPERTIES
96};
97
98static GParamSpec *obj_properties[N_PROPERTIES] = { nullptr, };
99
100static void
101gnc_currency_edit_set_property (GObject *object,
102 guint property_id,
103 const GValue *value,
104 GParamSpec *pspec)
105{
106 GNCCurrencyEdit *self = GNC_CURRENCY_EDIT (object);
107
108 switch (property_id)
109 {
110 case PROP_GCE_MNEMONIC:
111 g_free (self->mnemonic);
112 self->mnemonic = g_value_dup_string (value);
113 DEBUG ("mnemonic: %s\n", self->mnemonic);
114 break;
115
116 default:
117 /* We don't have any other property... */
118 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
119 break;
120 }
121}
122
123static void
124gnc_currency_edit_get_property (GObject *object,
125 guint property_id,
126 GValue *value,
127 GParamSpec *pspec)
128{
129 GNCCurrencyEdit *self = GNC_CURRENCY_EDIT (object);
130
131 switch (property_id)
132 {
133 case PROP_GCE_MNEMONIC:
134 g_value_set_string (value, self->mnemonic);
135 break;
136
137 default:
138 /* We don't have any other property... */
139 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
140 break;
141 }
142}
143
150static void
151gnc_currency_edit_class_init (GNCCurrencyEditClass *klass)
152{
153 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
154
155 gobject_class->set_property = gnc_currency_edit_set_property;
156 gobject_class->get_property = gnc_currency_edit_get_property;
157 gobject_class->finalize = gnc_currency_edit_finalize;
158
159 obj_properties[PROP_GCE_MNEMONIC] =
160 g_param_spec_string ("mnemonic",
161 "Active currency's mnemonic",
162 "Active currency's mnemonic",
163 "USD" /* default value */,
164 G_PARAM_READWRITE);
165
166 g_object_class_install_properties (gobject_class,
167 N_PROPERTIES,
168 obj_properties);
169}
170
171
178static void
179gnc_currency_edit_init (GNCCurrencyEdit *gce)
180{
181 // Set the name for this widget so it can be easily manipulated with css
182 gtk_widget_set_name (GTK_WIDGET(gce), "gnc-id-currency-edit");
183
184 g_signal_connect (gce, "notify::mnemonic",
185 G_CALLBACK (gnc_currency_edit_mnemonic_changed), gce);
186 g_signal_connect (gce, "changed",
187 G_CALLBACK (gnc_currency_edit_active_changed), gce);
188}
189
190
201static void
202gnc_currency_edit_finalize (GObject *object)
203{
204 g_return_if_fail (object != nullptr);
205 g_return_if_fail (GNC_IS_CURRENCY_EDIT (object));
206
207 GNCCurrencyEdit *self = GNC_CURRENCY_EDIT(object);
208
209 g_free (self->mnemonic);
210
211 G_OBJECT_CLASS(gnc_currency_edit_parent_class)->finalize (object);
212}
213
214
215static void
216gnc_currency_edit_mnemonic_changed (GObject *gobject,
217 GParamSpec *pspec,
218 gpointer user_data)
219{
220
221 GNCCurrencyEdit *self = GNC_CURRENCY_EDIT (gobject);
222
223 gnc_commodity *currency = gnc_commodity_table_lookup (gnc_get_current_commodities (),
224 GNC_COMMODITY_NS_CURRENCY,
225 self->mnemonic);
226
227 /* If there isn't any such commodity, get the default */
228 if (!currency)
229 {
230 currency = gnc_locale_default_currency();
231 DEBUG("gce %p, default currency mnemonic %s",
232 self, gnc_commodity_get_mnemonic(currency));
233 }
234
235 g_signal_handlers_block_by_func(G_OBJECT(self),
236 (gpointer)gnc_currency_edit_mnemonic_changed,
237 user_data);
238 gnc_currency_edit_set_currency(self, currency);
239 g_signal_handlers_unblock_by_func(G_OBJECT(self),
240 (gpointer)gnc_currency_edit_mnemonic_changed,
241 user_data);
242}
243
244
245static void gnc_currency_edit_active_changed (GtkComboBox *gobject,
246 gpointer user_data)
247{
248 GNCCurrencyEdit *self = GNC_CURRENCY_EDIT (gobject);
249
250 /* Check that there is a proper selection before proceeding. Doing so allows
251 * GTK entry completion to proceed. */
252 if (gtk_combo_box_get_active(GTK_COMBO_BOX(self)) != -1)
253 {
254 gnc_commodity *currency = gnc_currency_edit_get_currency (self);
255 const gchar *mnemonic = gnc_commodity_get_mnemonic (currency);
256
257 g_signal_handlers_block_by_func(G_OBJECT(self),
258 (gpointer)gnc_currency_edit_active_changed, user_data);
259 g_object_set (G_OBJECT (self), "mnemonic", mnemonic, nullptr);
260 g_signal_handlers_unblock_by_func(G_OBJECT(self),
261 (gpointer)gnc_currency_edit_active_changed, user_data);
262 }
263}
264
265enum
266{
267 CURRENCY_COL_NAME,
268 CURRENCY_COL_NORMALIZED_FOLDED,
269 NUM_CURRENCY_COLS
270};
271
282static void
283add_item(gnc_commodity *commodity, GNCCurrencyEdit *gce)
284{
285 GtkTreeModel *model;
286 GtkTreeIter iter;
287 const char *string;
288 gchar *normalized, *normalized_folded;
289
290 model = gtk_combo_box_get_model(GTK_COMBO_BOX(gce));
291
292 string = gnc_commodity_get_printname(commodity);
293 normalized = g_utf8_normalize (string, -1, G_NORMALIZE_NFC);
294 normalized_folded = normalized ? g_utf8_casefold (normalized, -1) : nullptr;
295
296 gtk_list_store_append(GTK_LIST_STORE(model), &iter);
297 gtk_list_store_set (GTK_LIST_STORE(model), &iter,
298 CURRENCY_COL_NAME, string,
299 CURRENCY_COL_NORMALIZED_FOLDED, normalized_folded,
300 -1);
301 gce->widest = std::max (gce->widest, g_utf8_strlen (string, -1));
302 g_free (normalized_folded);
303 g_free (normalized);
304}
305
306
315static void
316fill_currencies(GNCCurrencyEdit *gce)
317{
318 GList *currencies;
319
321 (gnc_get_current_commodities (), GNC_COMMODITY_NS_CURRENCY);
322 g_list_foreach(currencies, (GFunc)add_item, gce);
323 g_list_free(currencies);
324
325 auto entry = gtk_bin_get_child (GTK_BIN (gce));
326 gtk_entry_set_width_chars (GTK_ENTRY (entry), gce->widest);
327}
328
329
330static gboolean
331match_func (GtkEntryCompletion *completion, const char *entry_str,
332 GtkTreeIter *iter, GtkTreeModel *model)
333{
334 gchar *currency_name;
335 gboolean ret = FALSE;
336 gtk_tree_model_get (model, iter,
337 CURRENCY_COL_NORMALIZED_FOLDED, &currency_name,
338 -1);
339 if (currency_name && *currency_name && strstr (currency_name, entry_str))
340 ret = TRUE;
341 g_free (currency_name);
342 return ret;
343}
344
345/* Create a new GNCCurrencyEdit widget which can be used to provide
346 * an easy way to enter ISO currency codes.
347 *
348 * @return A GNCCurrencyEdit widget.
349 */
350GtkWidget *
352{
353 GNCCurrencyEdit *gce;
354 GtkListStore *store;
355 GtkEntryCompletion* completion;
356
357 store = gtk_list_store_new (NUM_CURRENCY_COLS, G_TYPE_STRING, G_TYPE_STRING);
358 gce = GNC_CURRENCY_EDIT(g_object_new (GNC_TYPE_CURRENCY_EDIT,
359 "model", store,
360 "has-entry", true,
361 nullptr));
362 g_object_unref (store);
363
364 /* Set the column for the text */
365 gtk_combo_box_set_entry_text_column (GTK_COMBO_BOX(gce), CURRENCY_COL_NAME);
366
367 /* Now the signals to make sure the user can't leave the
368 widget without a valid currency. */
369 gnc_cbwe_require_list_item(GTK_COMBO_BOX(gce));
370
371 /* Fill in all the data. */
372 fill_currencies (gce);
373 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(store),
374 CURRENCY_COL_NAME,
375 GTK_SORT_ASCENDING);
376
377 completion = gtk_entry_completion_new ();
378 gtk_entry_completion_set_model (completion, GTK_TREE_MODEL(store));
379 gtk_entry_completion_set_text_column (completion, CURRENCY_COL_NAME);
380 gtk_entry_completion_set_match_func (completion,
381 (GtkEntryCompletionMatchFunc)match_func,
382 GTK_TREE_MODEL(store), nullptr);
383 gtk_entry_set_completion (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (gce))),
384 completion);
385
386 return GTK_WIDGET (gce);
387}
388
390
393
394/* Set the widget to display a certain currency name.
395 *
396 * @param gce The currency editor widget to set.
397 *
398 * @param currency The currency to set as the displayed/selected
399 * value of the widget.
400 */
401void
402gnc_currency_edit_set_currency (GNCCurrencyEdit *gce,
403 const gnc_commodity *currency)
404{
405 const gchar *printname;
406
407 g_return_if_fail(gce != nullptr);
408 g_return_if_fail(GNC_IS_CURRENCY_EDIT(gce));
409 g_return_if_fail(currency != nullptr);
410
411 printname = gnc_commodity_get_printname(currency);
412 gnc_cbwe_set_by_string(GTK_COMBO_BOX(gce), printname);
413}
414
415
416/* Retrieve the displayed currency of the widget.
417 *
418 * @param gce The currency editor widget whose values should be retrieved.
419 *
420 * @return A pointer to the selected currency (a gnc_commodity
421 * structure).
422 */
423gnc_commodity *
424gnc_currency_edit_get_currency (GNCCurrencyEdit *gce)
425{
426 gnc_commodity *commodity;
427 char *mnemonic, *name;
428 GtkTreeModel *model;
429 GtkTreeIter iter;
430
431 g_return_val_if_fail(gce != nullptr, nullptr);
432 g_return_val_if_fail(GNC_IS_CURRENCY_EDIT(gce), nullptr);
433
434 if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(gce), &iter))
435 {
436 model = gtk_combo_box_get_model(GTK_COMBO_BOX(gce));
437 gtk_tree_model_get (model, &iter, 0, &mnemonic, -1);
438
439 name = strchr(mnemonic, ' ');
440 if (name != nullptr)
441 *name = '\0';
442 commodity = gnc_commodity_table_lookup (gnc_get_current_commodities (),
443 GNC_COMMODITY_NS_CURRENCY,
444 mnemonic);
445 g_free(mnemonic);
446 }
447 else
448 {
449 g_warning("Combo box returned 'inactive'. Using locale default currency.");
450 commodity = gnc_locale_default_currency();
451 }
452
453
454 return commodity;
455}
456
464void
466{
467 GtkTreeModel *model;
468 GtkWidget *entry;
469
470 g_return_if_fail(gce != nullptr);
471 g_return_if_fail(GNC_IS_CURRENCY_EDIT(gce));
472
473 model = gtk_combo_box_get_model (GTK_COMBO_BOX(gce));
474
475 entry = gtk_bin_get_child (GTK_BIN(gce));
476
477 g_object_ref (model);
478
479 g_signal_handlers_block_by_func (G_OBJECT(gce),
480 (gpointer)gnc_currency_edit_active_changed, gce);
481
482 gtk_combo_box_set_model (GTK_COMBO_BOX(gce), nullptr);
483 gtk_entry_set_text (GTK_ENTRY(entry),"");
484 gtk_combo_box_set_active (GTK_COMBO_BOX(gce), -1);
485 gtk_combo_box_set_model (GTK_COMBO_BOX(gce), model);
486
487 g_signal_handlers_unblock_by_func (G_OBJECT(gce),
488 (gpointer)gnc_currency_edit_active_changed, gce);
489
490 g_object_unref (model);
491}
492
496
Commodity handling public routines.
Currency selection widget.
All type declarations for the whole Gnucash engine.
gtk helper routines.
utility functions for the GnuCash UI
const char * gnc_commodity_get_printname(const gnc_commodity *cm)
Retrieve the 'print' name for the specified commodity.
const char * gnc_commodity_get_mnemonic(const gnc_commodity *cm)
Retrieve the mnemonic for the specified commodity.
CommodityList * gnc_commodity_table_get_commodities(const gnc_commodity_table *table, const char *name_space)
Return a list of all commodities in the commodity table that are in the given namespace.
void gnc_currency_edit_set_currency(GNCCurrencyEdit *gce, const gnc_commodity *currency)
Set the widget to display a certain currency name.
void gnc_currency_edit_clear_display(GNCCurrencyEdit *gce)
Clear the displayed currency of the widget.
GtkWidget * gnc_currency_edit_new(void)
Create a new GNCCurrencyEdit widget which can be used to provide an easy way to enter ISO currency co...
gnc_commodity * gnc_currency_edit_get_currency(GNCCurrencyEdit *gce)
Retrieve the displayed currency of the widget.
void gnc_cbwe_set_by_string(GtkComboBox *cbwe, const gchar *text)
Find an entry in the GtkComboBox by its text value, and set the widget to that value.
gnc_commodity * gnc_locale_default_currency(void)
Returns the default currency of the current locale.
#define DEBUG(format, args...)
Print a debugging message.
Definition qoflog.h:264