28 #include <glib/gi18n.h> 36 #include "gnc-component-manager.h" 40 #include "gnc-icons.h" 47 static QofLogModule log_module = GNC_MOD_GUI;
52 static void gnc_tree_view_commodity_finalize (GObject *
object);
53 static void gnc_tree_view_commodity_destroy (GtkWidget *widget);
60 #define GNC_TREE_VIEW_COMMODITY_GET_PRIVATE(o) \ 61 ((GncTreeViewCommodityPrivate*)gnc_tree_view_commodity_get_instance_private(GncTreeViewCommodity*)o)) 68 static GObjectClass *parent_class = NULL;
75 GObjectClass *o_class;
76 GtkWidgetClass *widget_class;
78 parent_class = g_type_class_peek_parent (klass);
80 o_class = G_OBJECT_CLASS (klass);
81 widget_class = GTK_WIDGET_CLASS (klass);
84 o_class->finalize = gnc_tree_view_commodity_finalize;
87 widget_class->destroy = gnc_tree_view_commodity_destroy;
96 gnc_tree_view_commodity_finalize (GObject *
object)
98 g_return_if_fail (
object != NULL);
99 g_return_if_fail (GNC_IS_TREE_VIEW_COMMODITY (
object));
101 ENTER(
"view %p",
object);
102 if (G_OBJECT_CLASS (parent_class)->finalize)
103 (* G_OBJECT_CLASS (parent_class)->finalize) (
object);
108 gnc_tree_view_commodity_destroy (GtkWidget *widget)
110 g_return_if_fail (widget != NULL);
111 g_return_if_fail (GNC_IS_TREE_VIEW_COMMODITY (widget));
113 ENTER(
"view %p", widget);
115 if (GTK_WIDGET_CLASS (parent_class)->destroy)
116 (* GTK_WIDGET_CLASS (parent_class)->destroy) (widget);
126 get_commodities_w_iters (GtkTreeModel *f_model,
127 GtkTreeIter *f_iter_a,
128 GtkTreeIter *f_iter_b,
129 GtkTreeModel **model_p,
132 gnc_commodity **comm_a,
133 gnc_commodity **comm_b)
136 GtkTreeModel *tree_model;
138 tree_model = gtk_tree_model_filter_get_model(GTK_TREE_MODEL_FILTER(f_model));
139 model = GNC_TREE_MODEL_COMMODITY(tree_model);
141 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER(f_model),
145 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER(f_model),
158 *model_p = tree_model;
163 get_commodities (GtkTreeModel *f_model,
164 GtkTreeIter *f_iter_a,
165 GtkTreeIter *f_iter_b,
166 GtkTreeModel **model_p,
167 gnc_commodity **comm_a,
168 gnc_commodity **comm_b)
170 GtkTreeIter iter_a, iter_b;
172 return get_commodities_w_iters(f_model, f_iter_a, f_iter_b, model_p,
173 &iter_a, &iter_b, comm_a, comm_b);
177 sort_namespace (GtkTreeModel *f_model,
178 GtkTreeIter *f_iter_a,
179 GtkTreeIter *f_iter_b)
182 GtkTreeModel *tree_model;
183 GtkTreeIter iter_a, iter_b;
184 gnc_commodity_namespace *ns_a, *ns_b;
186 tree_model = gtk_tree_model_filter_get_model(GTK_TREE_MODEL_FILTER(f_model));
187 model = GNC_TREE_MODEL_COMMODITY(tree_model);
189 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER(f_model),
192 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER(f_model),
203 default_sort (gnc_commodity *comm_a, gnc_commodity *comm_b)
205 gint fraction_a, fraction_b, result;
209 if (result != 0)
return result;
213 if (result != 0)
return result;
217 if (result != 0)
return result;
221 if (result != 0)
return result;
226 if (fraction_a < fraction_b)
229 if (fraction_b < fraction_a)
236 sort_by_commodity_string (GtkTreeModel *f_model,
237 GtkTreeIter *f_iter_a,
238 GtkTreeIter *f_iter_b,
242 GtkTreeIter iter_a, iter_b;
243 gnc_commodity *comm_a, *comm_b;
245 gint column = GPOINTER_TO_INT(user_data);
248 if (!get_commodities_w_iters(f_model, f_iter_a, f_iter_b,
249 &model, &iter_a, &iter_b, &comm_a, &comm_b))
250 return sort_namespace (f_model, f_iter_a, f_iter_b);
253 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter_a, column, &str1, -1);
254 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter_b, column, &str2, -1);
261 return default_sort(comm_a, comm_b);
266 sort_by_fraction (GtkTreeModel *f_model,
267 GtkTreeIter *f_iter_a,
268 GtkTreeIter *f_iter_b,
271 gnc_commodity *comm_a, *comm_b;
272 gint fraction_a, fraction_b;
274 if (!get_commodities (f_model, f_iter_a, f_iter_b, NULL, &comm_a, &comm_b))
275 return sort_namespace (f_model, f_iter_a, f_iter_b);
280 if (fraction_a < fraction_b)
283 if (fraction_b < fraction_a)
286 return default_sort(comm_a, comm_b);
290 sort_by_quote_flag (GtkTreeModel *f_model,
291 GtkTreeIter *f_iter_a,
292 GtkTreeIter *f_iter_b,
295 gnc_commodity *comm_a, *comm_b;
296 gboolean flag_a, flag_b;
298 if (!get_commodities (f_model, f_iter_a, f_iter_b, NULL, &comm_a, &comm_b))
299 return sort_namespace (f_model, f_iter_a, f_iter_b);
306 else if (flag_a > flag_b)
308 return default_sort(comm_a, comm_b);
323 const gchar *first_property_name,
327 GtkTreeModel *model, *f_model, *s_model;
328 GtkTreeViewColumn *col;
329 gnc_commodity_table *ct;
338 f_model = gtk_tree_model_filter_new (model, NULL);
339 g_object_unref(G_OBJECT(model));
340 s_model = gtk_tree_model_sort_new_with_model (f_model);
341 g_object_unref(G_OBJECT(f_model));
344 view = g_object_new (GNC_TYPE_TREE_VIEW_COMMODITY,
345 "name",
"gnc-id-commodity-tree", NULL);
346 gtk_tree_view_set_model (GTK_TREE_VIEW (view), s_model);
347 g_object_unref(G_OBJECT(s_model));
349 DEBUG(
"model ref count is %d", G_OBJECT(model)->ref_count);
350 DEBUG(
"f_model ref count is %d", G_OBJECT(f_model)->ref_count);
351 DEBUG(
"s_model ref count is %d", G_OBJECT(s_model)->ref_count);
354 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(view), FALSE);
357 view, _(
"Namespace"),
"namespace", NULL,
"NASDAQ",
358 GNC_TREE_MODEL_COMMODITY_COL_NAMESPACE,
359 GNC_TREE_VIEW_COLUMN_VISIBLE_ALWAYS,
360 sort_by_commodity_string);
362 view, _(
"Symbol"),
"symbol", NULL,
"ACMEACME",
363 GNC_TREE_MODEL_COMMODITY_COL_MNEMONIC,
364 GNC_TREE_MODEL_COMMODITY_COL_VISIBILITY,
365 sort_by_commodity_string);
366 g_object_set_data(G_OBJECT(col), DEFAULT_VISIBLE, GINT_TO_POINTER(1));
368 view, _(
"Name"),
"name", NULL,
"Acme Corporation, Inc.",
369 GNC_TREE_MODEL_COMMODITY_COL_FULLNAME,
370 GNC_TREE_MODEL_COMMODITY_COL_VISIBILITY,
371 sort_by_commodity_string);
372 g_object_set_data(G_OBJECT(col), DEFAULT_VISIBLE, GINT_TO_POINTER(1));
374 view, _(
"Print Name"),
"printname", NULL,
375 "ACMEACME (Acme Corporation, Inc.)",
376 GNC_TREE_MODEL_COMMODITY_COL_PRINTNAME,
377 GNC_TREE_MODEL_COMMODITY_COL_VISIBILITY,
378 sort_by_commodity_string);
380 view, _(
"Display symbol"),
"user_symbol", NULL,
"ACME",
381 GNC_TREE_MODEL_COMMODITY_COL_USER_SYMBOL,
382 GNC_TREE_MODEL_COMMODITY_COL_VISIBILITY,
383 sort_by_commodity_string);
384 g_object_set_data(G_OBJECT(col), DEFAULT_VISIBLE, GINT_TO_POINTER(1));
386 view, _(
"Unique Name"),
"uniquename", NULL,
387 "NASDAQ::ACMEACME", GNC_TREE_MODEL_COMMODITY_COL_UNIQUE_NAME,
388 GNC_TREE_MODEL_COMMODITY_COL_VISIBILITY,
389 sort_by_commodity_string);
393 view, _(
"ISIN/CUSIP"),
"cusip_code", NULL,
"US1234567890",
394 GNC_TREE_MODEL_COMMODITY_COL_CUSIP,
395 GNC_TREE_MODEL_COMMODITY_COL_VISIBILITY,
396 sort_by_commodity_string);
397 g_object_set_data(G_OBJECT(col), DEFAULT_VISIBLE, GINT_TO_POINTER(1));
399 view, _(
"Fraction"),
"fraction",
"10000",
400 GNC_TREE_MODEL_COMMODITY_COL_FRACTION,
401 GNC_TREE_VIEW_COLUMN_COLOR_NONE,
402 GNC_TREE_MODEL_COMMODITY_COL_VISIBILITY,
404 g_object_set_data(G_OBJECT(col), DEFAULT_VISIBLE, GINT_TO_POINTER(1));
406 view, _(
"Get Quotes"),
407 C_(
"Column letter for 'Get Quotes'",
"Q"),
"quote_flag",
408 GNC_TREE_MODEL_COMMODITY_COL_QUOTE_FLAG,
409 GNC_TREE_MODEL_COMMODITY_COL_VISIBILITY,
413 view, _(
"Source"),
"quote_source", NULL,
"alphavantage",
414 GNC_TREE_MODEL_COMMODITY_COL_QUOTE_SOURCE,
415 GNC_TREE_MODEL_COMMODITY_COL_VISIBILITY,
416 sort_by_commodity_string);
418 view, _(
"Timezone"),
"quote_timezone", NULL,
"America/New_York",
419 GNC_TREE_MODEL_COMMODITY_COL_QUOTE_TZ,
420 GNC_TREE_MODEL_COMMODITY_COL_VISIBILITY,
421 sort_by_commodity_string);
422 g_object_set_data(G_OBJECT(col), DEFAULT_VISIBLE, GINT_TO_POINTER(1));
427 va_start (var_args, first_property_name);
428 g_object_set_valist (G_OBJECT(view), first_property_name, var_args);
433 if (!gtk_tree_sortable_get_sort_column_id(GTK_TREE_SORTABLE(s_model),
436 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(s_model),
437 GNC_TREE_MODEL_COMMODITY_COL_FULLNAME,
441 gtk_widget_show(GTK_WIDGET(view));
443 return GTK_TREE_VIEW(view);
450 #define debug_path(fn, path) { \ 451 gchar *path_string = gtk_tree_path_to_string(path); \ 452 fn("tree path %s", path_string); \ 453 g_free(path_string); \ 459 gnc_commodity *commodity,
462 GtkTreeModel *model, *f_model, *s_model;
463 GtkTreeIter iter, f_iter;
465 g_return_val_if_fail(GNC_IS_TREE_VIEW_COMMODITY(view), FALSE);
466 g_return_val_if_fail(commodity != NULL, FALSE);
467 g_return_val_if_fail(s_iter != NULL, FALSE);
472 s_model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
473 f_model = gtk_tree_model_sort_get_model(GTK_TREE_MODEL_SORT(s_model));
474 model = gtk_tree_model_filter_get_model(GTK_TREE_MODEL_FILTER(f_model));
477 LEAVE(
"model_get_iter_from_commodity failed");
482 gtk_tree_model_filter_convert_child_iter_to_iter (GTK_TREE_MODEL_FILTER(f_model),
484 gtk_tree_model_sort_convert_child_iter_to_iter (GTK_TREE_MODEL_SORT(s_model),
497 gnc_tree_view_commodity_ns_filter_func user_ns_fn;
498 gnc_tree_view_commodity_cm_filter_func user_cm_fn;
500 GDestroyNotify user_destroy;
504 gnc_tree_view_commodity_filter_destroy (gpointer data)
508 if (fd->user_destroy)
509 fd->user_destroy(fd->user_data);
514 gnc_tree_view_commodity_filter_helper (GtkTreeModel *model,
518 gnc_commodity_namespace *name_space;
519 gnc_commodity *commodity;
522 g_return_val_if_fail (GNC_IS_TREE_MODEL_COMMODITY (model), FALSE);
523 g_return_val_if_fail (iter != NULL, FALSE);
530 return fd->user_ns_fn(name_space, fd->user_data);
540 return fd->user_cm_fn(commodity, fd->user_data);
555 gnc_tree_view_commodity_ns_filter_func ns_func,
556 gnc_tree_view_commodity_cm_filter_func cm_func,
558 GDestroyNotify destroy)
560 GtkTreeModel *f_model, *s_model;
563 g_return_if_fail(GNC_IS_TREE_VIEW_COMMODITY(view));
564 g_return_if_fail((ns_func != NULL) || (cm_func != NULL));
566 ENTER(
"view %p, ns func %p, cm func %p, data %p, destroy %p",
567 view, ns_func, cm_func, data, destroy);
570 fd->user_ns_fn = ns_func;
571 fd->user_cm_fn = cm_func;
572 fd->user_data = data;
573 fd->user_destroy = destroy;
575 s_model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
576 f_model = gtk_tree_model_sort_get_model(GTK_TREE_MODEL_SORT(s_model));
579 g_object_ref (G_OBJECT(s_model));
580 gtk_tree_view_set_model (GTK_TREE_VIEW(view), NULL);
582 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (f_model),
583 gnc_tree_view_commodity_filter_helper,
585 gnc_tree_view_commodity_filter_destroy);
589 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (f_model));
592 gtk_tree_view_set_model (GTK_TREE_VIEW(view), s_model);
593 g_object_unref (G_OBJECT(s_model));
604 GtkTreeModel *f_model, *s_model;
606 g_return_if_fail(GNC_IS_TREE_VIEW_COMMODITY(view));
608 ENTER(
"view %p", view);
609 s_model = gtk_tree_view_get_model (GTK_TREE_VIEW(view));
610 f_model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (s_model));
611 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (f_model));
627 GtkTreeSelection *selection;
628 GtkTreeModel *model, *f_model, *s_model;
629 GtkTreeIter iter, f_iter, s_iter;
630 gnc_commodity *commodity;
632 g_return_val_if_fail (GNC_IS_TREE_VIEW_COMMODITY (view), NULL);
634 ENTER(
"view %p", view);
636 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(view));
637 if (!gtk_tree_selection_get_selected (selection, &s_model, &s_iter))
639 LEAVE(
"no commodity, get_selected failed");
643 gtk_tree_model_sort_convert_iter_to_child_iter (GTK_TREE_MODEL_SORT (s_model),
646 f_model = gtk_tree_model_sort_get_model(GTK_TREE_MODEL_SORT(s_model));
647 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (f_model),
650 model = gtk_tree_model_filter_get_model(GTK_TREE_MODEL_FILTER(f_model));
653 LEAVE(
"commodity %p (%s)", commodity,
664 GtkTreeSelection *selection;
665 GtkTreeModel *model, *f_model, *s_model;
666 GtkTreePath *tree_path;
667 GtkTreePath *f_tree_path;
668 GtkTreePath *s_tree_path;
670 g_return_if_fail (GNC_IS_TREE_VIEW_COMMODITY(view));
675 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(view));
677 s_model = gtk_tree_view_get_model (GTK_TREE_VIEW(view));
678 f_model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (s_model));
679 model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (f_model));
685 f_tree_path = gtk_tree_model_filter_convert_child_path_to_path
686 (GTK_TREE_MODEL_FILTER (f_model), tree_path);
688 s_tree_path = gtk_tree_model_sort_convert_child_path_to_path
689 (GTK_TREE_MODEL_SORT (s_model), f_tree_path);
691 gtk_tree_view_expand_to_path (GTK_TREE_VIEW(view), s_tree_path);
692 gtk_tree_selection_select_path (selection, s_tree_path);
693 gtk_tree_path_free (tree_path);
694 gtk_tree_path_free (f_tree_path);
695 gtk_tree_path_free (s_tree_path);
706 get_selected_commodities_helper (GtkTreeModel *s_model,
711 GList **return_list = data;
712 GtkTreeModel *model, *f_model;
713 GtkTreeIter iter, f_iter;
714 gnc_commodity *commodity;
716 gtk_tree_model_sort_convert_iter_to_child_iter (GTK_TREE_MODEL_SORT (s_model),
719 f_model = gtk_tree_model_sort_get_model(GTK_TREE_MODEL_SORT(s_model));
720 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (f_model),
723 model = gtk_tree_model_filter_get_model(GTK_TREE_MODEL_FILTER(f_model));
726 *return_list = g_list_append(*return_list, commodity);
GtkTreeModel implementation for gnucash commodities.
GtkTreeView * gnc_tree_view_commodity_new(QofBook *book, const gchar *first_property_name,...)
Create a new commodity tree view.
const char * gnc_commodity_get_cusip(const gnc_commodity *cm)
Retrieve the 'exchange code' for the specified commodity.
gnc_commodity_table * gnc_commodity_table_get_table(QofBook *book)
Returns the commodity table associated with a book.
int gnc_commodity_get_fraction(const gnc_commodity *cm)
Retrieve the fraction for the specified commodity.
gnc_commodity * gnc_tree_model_commodity_get_commodity(GncTreeModelCommodity *model, GtkTreeIter *iter)
Convert a model/iter pair to a gnucash commodity.
gboolean gnc_tree_model_commodity_get_iter_from_commodity(GncTreeModelCommodity *model, gnc_commodity *commodity, GtkTreeIter *iter)
Convert a commodity pointer into a GtkTreeIter.
const char * gnc_commodity_get_mnemonic(const gnc_commodity *cm)
Retrieve the mnemonic for the specified commodity.
GtkTreePath * gnc_tree_model_commodity_get_path_from_commodity(GncTreeModelCommodity *model, gnc_commodity *commodity)
Convert a commodity pointer into a GtkTreePath.
void gnc_tree_view_commodity_set_filter(GncTreeViewCommodity *view, gnc_tree_view_commodity_ns_filter_func ns_func, gnc_tree_view_commodity_cm_filter_func cm_func, gpointer data, GDestroyNotify destroy)
This function attaches a filter function to the given commodity tree.
const char * gnc_commodity_namespace_get_gui_name(const gnc_commodity_namespace *ns)
Return the textual name of a namespace data structure in a form suitable to present to the user...
utility functions for the GnuCash UI
int safe_utf8_collate(const char *da, const char *db)
Collate two UTF-8 strings.
gboolean gnc_commodity_get_quote_flag(const gnc_commodity *cm)
Retrieve the automatic price quote flag for the specified commodity.
common utilities for manipulating a GtkTreeView within gnucash
#define DEBUG(format, args...)
Print a debugging message.
gnc_commodity_namespace * gnc_tree_model_commodity_get_namespace(GncTreeModelCommodity *model, GtkTreeIter *iter)
Convert a model/iter pair to a gnucash commodity namespace.
const char * gnc_commodity_get_namespace(const gnc_commodity *cm)
Retrieve the namespace for the specified commodity.
GtkTreeViewColumn * gnc_tree_view_add_numeric_column(GncTreeView *view, const gchar *column_title, const gchar *pref_name, const gchar *sizing_text, gint model_data_column, gint model_color_column, gint model_visibility_column, GtkTreeIterCompareFunc column_sort_fn)
This function adds a new numeric column to a GncTreeView base view.
#define ENTER(format, args...)
Print a function entry debugging message.
GtkTreeViewColumn * gnc_tree_view_add_toggle_column(GncTreeView *view, const gchar *column_title, const gchar *column_short_title, const gchar *pref_name, gint model_data_column, gint model_visibility_column, GtkTreeIterCompareFunc column_sort_fn, renderer_toggled toggle_edited_cb)
This function adds a new toggle column to a GncTreeView base view.
void gnc_tree_view_commodity_select_commodity(GncTreeViewCommodity *view, gnc_commodity *commodity)
Select the commodity in the associated commodity tree view.
void gnc_tree_view_configure_columns(GncTreeView *view)
Make all the correct columns visible, respecting their default visibility setting, their "always" visibility setting, and the last saved state if available.
GtkTreeModel * gnc_tree_model_commodity_new(QofBook *book, gnc_commodity_table *ct)
Create a new GtkTreeModel for manipulating gnucash commodities.
const char * gnc_commodity_get_fullname(const gnc_commodity *cm)
Retrieve the full name for the specified commodity.
Gnome specific utility functions.
All type declarations for the whole Gnucash engine.
gboolean gnc_tree_model_commodity_iter_is_commodity(GncTreeModelCommodity *model, GtkTreeIter *iter)
Determine whether or not the specified GtkTreeIter points to a commodity.
gboolean gnc_tree_model_commodity_iter_is_namespace(GncTreeModelCommodity *model, GtkTreeIter *iter)
Determine whether or not the specified GtkTreeIter points to a commodity namespace.
gnc_commodity * gnc_tree_view_commodity_get_selected_commodity(GncTreeViewCommodity *view)
This function returns the commodity associated with the selected item in the commodity tree view...
GtkTreeViewColumn * gnc_tree_view_add_text_column(GncTreeView *view, const gchar *column_title, const gchar *pref_name, const gchar *icon_name, const gchar *sizing_text, gint model_data_column, gint model_visibility_column, GtkTreeIterCompareFunc column_sort_fn)
This function adds a new text column to a GncTreeView base view.
#define LEAVE(format, args...)
Print a function exit debugging message.
GtkTreeView implementation for gnucash commodity tree.
void gnc_tree_view_commodity_refilter(GncTreeViewCommodity *view)
This function forces the commodity tree filter to be evaluated.
Commodity handling public routines.
The instance data structure for a commodity tree model.