28 #include <glib/gi18n.h>
36 #include "gnc-component-manager.h"
40 #include "gnc-icons.h"
52 static void gnc_tree_view_commodity_finalize (GObject *
object);
53 static void gnc_tree_view_commodity_destroy (GtkObject *
object);
60 #define GNC_TREE_VIEW_COMMODITY_GET_PRIVATE(o) \
61 (G_TYPE_INSTANCE_GET_PRIVATE ((o), GNC_TYPE_TREE_VIEW_COMMODITY, GncTreeViewCommodityPrivate))
68 static GObjectClass *parent_class = NULL;
71 gnc_tree_view_commodity_get_type (
void)
73 static GType gnc_tree_view_commodity_type = 0;
75 if (gnc_tree_view_commodity_type == 0)
77 static const GTypeInfo our_info =
82 (GClassInitFunc) gnc_tree_view_commodity_class_init,
87 (GInstanceInitFunc) gnc_tree_view_commodity_init
90 gnc_tree_view_commodity_type = g_type_register_static (GNC_TYPE_TREE_VIEW,
91 "GncTreeViewCommodity",
95 return gnc_tree_view_commodity_type;
101 GObjectClass *o_class;
102 GtkObjectClass *object_class;
104 parent_class = g_type_class_peek_parent (klass);
106 o_class = G_OBJECT_CLASS (klass);
107 object_class = GTK_OBJECT_CLASS (klass);
110 o_class->finalize = gnc_tree_view_commodity_finalize;
113 object_class->destroy = gnc_tree_view_commodity_destroy;
124 gnc_tree_view_commodity_finalize (GObject *
object)
126 g_return_if_fail (
object != NULL);
127 g_return_if_fail (GNC_IS_TREE_VIEW_COMMODITY (
object));
129 ENTER(
"view %p",
object);
130 if (G_OBJECT_CLASS (parent_class)->finalize)
131 (* G_OBJECT_CLASS (parent_class)->finalize) (
object);
136 gnc_tree_view_commodity_destroy (GtkObject *
object)
138 g_return_if_fail (
object != NULL);
139 g_return_if_fail (GNC_IS_TREE_VIEW_COMMODITY (
object));
141 ENTER(
"view %p",
object);
143 if (GTK_OBJECT_CLASS (parent_class)->destroy)
144 (* GTK_OBJECT_CLASS (parent_class)->destroy) (
object);
154 get_commodities_w_iters (GtkTreeModel *f_model,
155 GtkTreeIter *f_iter_a,
156 GtkTreeIter *f_iter_b,
157 GtkTreeModel **model_p,
164 GtkTreeModel *tree_model;
166 tree_model = gtk_tree_model_filter_get_model(GTK_TREE_MODEL_FILTER(f_model));
167 model = GNC_TREE_MODEL_COMMODITY(tree_model);
169 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER(f_model),
173 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER(f_model),
186 *model_p = tree_model;
191 get_commodities (GtkTreeModel *f_model,
192 GtkTreeIter *f_iter_a,
193 GtkTreeIter *f_iter_b,
194 GtkTreeModel **model_p,
198 GtkTreeIter iter_a, iter_b;
200 return get_commodities_w_iters(f_model, f_iter_a, f_iter_b, model_p,
201 &iter_a, &iter_b, comm_a, comm_b);
205 sort_namespace (GtkTreeModel *f_model,
206 GtkTreeIter *f_iter_a,
207 GtkTreeIter *f_iter_b)
210 GtkTreeModel *tree_model;
211 GtkTreeIter iter_a, iter_b;
214 tree_model = gtk_tree_model_filter_get_model(GTK_TREE_MODEL_FILTER(f_model));
215 model = GNC_TREE_MODEL_COMMODITY(tree_model);
217 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER(f_model),
220 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER(f_model),
233 gint fraction_a, fraction_b, result;
237 if (result != 0)
return result;
241 if (result != 0)
return result;
245 if (result != 0)
return result;
249 if (result != 0)
return result;
254 if (fraction_a < fraction_b)
257 if (fraction_b < fraction_a)
264 sort_by_commodity_string (GtkTreeModel *f_model,
265 GtkTreeIter *f_iter_a,
266 GtkTreeIter *f_iter_b,
270 GtkTreeIter iter_a, iter_b;
273 gint column = GPOINTER_TO_INT(user_data);
276 if (!get_commodities_w_iters(f_model, f_iter_a, f_iter_b,
277 &model, &iter_a, &iter_b, &comm_a, &comm_b))
278 return sort_namespace (f_model, f_iter_a, f_iter_b);
281 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter_a, column, &str1, -1);
282 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter_b, column, &str2, -1);
289 return default_sort(comm_a, comm_b);
294 sort_by_fraction (GtkTreeModel *f_model,
295 GtkTreeIter *f_iter_a,
296 GtkTreeIter *f_iter_b,
300 gint fraction_a, fraction_b;
302 if (!get_commodities (f_model, f_iter_a, f_iter_b, NULL, &comm_a, &comm_b))
303 return sort_namespace (f_model, f_iter_a, f_iter_b);
308 if (fraction_a < fraction_b)
311 if (fraction_b < fraction_a)
314 return default_sort(comm_a, comm_b);
318 sort_by_quote_flag (GtkTreeModel *f_model,
319 GtkTreeIter *f_iter_a,
320 GtkTreeIter *f_iter_b,
324 gboolean flag_a, flag_b;
326 if (!get_commodities (f_model, f_iter_a, f_iter_b, NULL, &comm_a, &comm_b))
327 return sort_namespace (f_model, f_iter_a, f_iter_b);
334 else if (flag_a > flag_b)
336 return default_sort(comm_a, comm_b);
351 const gchar *first_property_name,
355 GtkTreeModel *model, *f_model, *s_model;
356 GtkTreeViewColumn *col;
366 f_model = gtk_tree_model_filter_new (model, NULL);
367 g_object_unref(G_OBJECT(model));
368 s_model = gtk_tree_model_sort_new_with_model (f_model);
369 g_object_unref(G_OBJECT(f_model));
372 view = g_object_new (GNC_TYPE_TREE_VIEW_COMMODITY,
373 "name",
"commodity_tree", NULL);
374 gtk_tree_view_set_model (GTK_TREE_VIEW (view), s_model);
375 g_object_unref(G_OBJECT(s_model));
377 DEBUG(
"model ref count is %d", G_OBJECT(model)->ref_count);
378 DEBUG(
"f_model ref count is %d", G_OBJECT(f_model)->ref_count);
379 DEBUG(
"s_model ref count is %d", G_OBJECT(s_model)->ref_count);
382 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(view), FALSE);
385 view, _(
"Namespace"),
"namespace", NULL,
"NASDAQ",
386 GNC_TREE_MODEL_COMMODITY_COL_NAMESPACE,
387 GNC_TREE_VIEW_COLUMN_VISIBLE_ALWAYS,
388 sort_by_commodity_string);
390 view, _(
"Symbol"),
"symbol", NULL,
"ACMEACME",
391 GNC_TREE_MODEL_COMMODITY_COL_MNEMONIC,
392 GNC_TREE_MODEL_COMMODITY_COL_VISIBILITY,
393 sort_by_commodity_string);
394 g_object_set_data(G_OBJECT(col), DEFAULT_VISIBLE, GINT_TO_POINTER(1));
396 view, _(
"Name"),
"name", NULL,
"Acme Corporation, Inc.",
397 GNC_TREE_MODEL_COMMODITY_COL_FULLNAME,
398 GNC_TREE_MODEL_COMMODITY_COL_VISIBILITY,
399 sort_by_commodity_string);
400 g_object_set_data(G_OBJECT(col), DEFAULT_VISIBLE, GINT_TO_POINTER(1));
402 view, _(
"Print Name"),
"printname", NULL,
403 "ACMEACME (Acme Corporation, Inc.)",
404 GNC_TREE_MODEL_COMMODITY_COL_PRINTNAME,
405 GNC_TREE_MODEL_COMMODITY_COL_VISIBILITY,
406 sort_by_commodity_string);
408 view, _(
"Display symbol"),
"user_symbol", NULL,
"ACME",
409 GNC_TREE_MODEL_COMMODITY_COL_USER_SYMBOL,
410 GNC_TREE_MODEL_COMMODITY_COL_VISIBILITY,
411 sort_by_commodity_string);
412 g_object_set_data(G_OBJECT(col), DEFAULT_VISIBLE, GINT_TO_POINTER(1));
414 view, _(
"Unique Name"),
"uniquename", NULL,
415 "NASDAQ::ACMEACME", GNC_TREE_MODEL_COMMODITY_COL_UNIQUE_NAME,
416 GNC_TREE_MODEL_COMMODITY_COL_VISIBILITY,
417 sort_by_commodity_string);
421 view, _(
"ISIN/CUSIP"),
"cusip_code", NULL,
"QWERTYUIOP",
422 GNC_TREE_MODEL_COMMODITY_COL_CUSIP,
423 GNC_TREE_MODEL_COMMODITY_COL_VISIBILITY,
424 sort_by_commodity_string);
425 g_object_set_data(G_OBJECT(col), DEFAULT_VISIBLE, GINT_TO_POINTER(1));
427 view, _(
"Fraction"),
"fraction",
"10000",
428 GNC_TREE_MODEL_COMMODITY_COL_FRACTION,
429 GNC_TREE_VIEW_COLUMN_COLOR_NONE,
430 GNC_TREE_MODEL_COMMODITY_COL_VISIBILITY,
432 g_object_set_data(G_OBJECT(col), DEFAULT_VISIBLE, GINT_TO_POINTER(1));
434 view, _(
"Get Quotes"),
437 Q_(
"Column letter for 'Get Quotes'|Q"),
"quote_flag",
438 GNC_TREE_MODEL_COMMODITY_COL_QUOTE_FLAG,
439 GNC_TREE_MODEL_COMMODITY_COL_VISIBILITY,
443 view, _(
"Source"),
"quote_source", NULL,
"yahoo",
444 GNC_TREE_MODEL_COMMODITY_COL_QUOTE_SOURCE,
445 GNC_TREE_MODEL_COMMODITY_COL_VISIBILITY,
446 sort_by_commodity_string);
448 view, _(
"Timezone"),
"quote_timezone", NULL,
"America/New_York",
449 GNC_TREE_MODEL_COMMODITY_COL_QUOTE_TZ,
450 GNC_TREE_MODEL_COMMODITY_COL_VISIBILITY,
451 sort_by_commodity_string);
452 g_object_set_data(G_OBJECT(col), DEFAULT_VISIBLE, GINT_TO_POINTER(1));
457 va_start (var_args, first_property_name);
458 g_object_set_valist (G_OBJECT(view), first_property_name, var_args);
463 if (!gtk_tree_sortable_get_sort_column_id(GTK_TREE_SORTABLE(s_model),
466 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(s_model),
467 GNC_TREE_MODEL_COMMODITY_COL_FULLNAME,
471 gtk_widget_show(GTK_WIDGET(view));
473 return GTK_TREE_VIEW(view);
480 #define debug_path(fn, path) { \
481 gchar *path_string = gtk_tree_path_to_string(path); \
482 fn("tree path %s", path_string); \
483 g_free(path_string); \
492 GtkTreeModel *model, *f_model, *s_model;
493 GtkTreeIter iter, f_iter;
495 g_return_val_if_fail(GNC_IS_TREE_VIEW_COMMODITY(view), FALSE);
496 g_return_val_if_fail(commodity != NULL, FALSE);
497 g_return_val_if_fail(s_iter != NULL, FALSE);
502 s_model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
503 f_model = gtk_tree_model_sort_get_model(GTK_TREE_MODEL_SORT(s_model));
504 model = gtk_tree_model_filter_get_model(GTK_TREE_MODEL_FILTER(f_model));
507 LEAVE(
"model_get_iter_from_commodity failed");
512 gtk_tree_model_filter_convert_child_iter_to_iter (GTK_TREE_MODEL_FILTER(f_model),
514 gtk_tree_model_sort_convert_child_iter_to_iter (GTK_TREE_MODEL_SORT(s_model),
527 gnc_tree_view_commodity_ns_filter_func user_ns_fn;
528 gnc_tree_view_commodity_cm_filter_func user_cm_fn;
530 GDestroyNotify user_destroy;
534 gnc_tree_view_commodity_filter_destroy (gpointer data)
538 if (fd->user_destroy)
539 fd->user_destroy(fd->user_data);
544 gnc_tree_view_commodity_filter_helper (GtkTreeModel *model,
552 g_return_val_if_fail (GNC_IS_TREE_MODEL_COMMODITY (model), FALSE);
553 g_return_val_if_fail (iter != NULL, FALSE);
560 return fd->user_ns_fn(name_space, fd->user_data);
570 return fd->user_cm_fn(commodity, fd->user_data);
585 gnc_tree_view_commodity_ns_filter_func ns_func,
586 gnc_tree_view_commodity_cm_filter_func cm_func,
588 GDestroyNotify destroy)
590 GtkTreeModel *f_model, *s_model;
593 g_return_if_fail(GNC_IS_TREE_VIEW_COMMODITY(view));
594 g_return_if_fail((ns_func != NULL) || (cm_func != NULL));
596 ENTER(
"view %p, ns func %p, cm func %p, data %p, destroy %p",
597 view, ns_func, cm_func, data, destroy);
600 fd->user_ns_fn = ns_func;
601 fd->user_cm_fn = cm_func;
602 fd->user_data = data;
603 fd->user_destroy = destroy;
605 s_model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
606 f_model = gtk_tree_model_sort_get_model(GTK_TREE_MODEL_SORT(s_model));
607 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (f_model),
608 gnc_tree_view_commodity_filter_helper,
610 gnc_tree_view_commodity_filter_destroy);
614 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (f_model));
624 GtkTreeModel *f_model, *s_model;
626 g_return_if_fail(GNC_IS_TREE_VIEW_COMMODITY(view));
628 ENTER(
"view %p", view);
629 s_model = gtk_tree_view_get_model (GTK_TREE_VIEW(view));
630 f_model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (s_model));
631 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (f_model));
647 GtkTreeSelection *selection;
648 GtkTreeModel *model, *f_model, *s_model;
649 GtkTreeIter iter, f_iter, s_iter;
652 g_return_val_if_fail (GNC_IS_TREE_VIEW_COMMODITY (view), NULL);
654 ENTER(
"view %p", view);
656 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(view));
657 if (!gtk_tree_selection_get_selected (selection, &s_model, &s_iter))
659 LEAVE(
"no commodity, get_selected failed");
663 gtk_tree_model_sort_convert_iter_to_child_iter (GTK_TREE_MODEL_SORT (s_model),
666 f_model = gtk_tree_model_sort_get_model(GTK_TREE_MODEL_SORT(s_model));
667 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (f_model),
670 model = gtk_tree_model_filter_get_model(GTK_TREE_MODEL_FILTER(f_model));
673 LEAVE(
"commodity %p (%s)", commodity,
686 get_selected_commodities_helper (GtkTreeModel *s_model,
691 GList **return_list = data;
692 GtkTreeModel *model, *f_model;
693 GtkTreeIter iter, f_iter;
696 gtk_tree_model_sort_convert_iter_to_child_iter (GTK_TREE_MODEL_SORT (s_model),
699 f_model = gtk_tree_model_sort_get_model(GTK_TREE_MODEL_SORT(s_model));
700 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (f_model),
703 model = gtk_tree_model_filter_get_model(GTK_TREE_MODEL_FILTER(f_model));
706 *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,...)
const char * gnc_commodity_get_cusip(const gnc_commodity *cm)
gnc_commodity_table * gnc_commodity_table_get_table(QofBook *book)
int gnc_commodity_get_fraction(const gnc_commodity *cm)
gnc_commodity * gnc_tree_model_commodity_get_commodity(GncTreeModelCommodity *model, GtkTreeIter *iter)
gboolean gnc_tree_model_commodity_get_iter_from_commodity(GncTreeModelCommodity *model, gnc_commodity *commodity, GtkTreeIter *iter)
const char * gnc_commodity_get_mnemonic(const gnc_commodity *cm)
GtkTreeViewColumn * gnc_tree_view_add_text_column(GncTreeView *view, const gchar *column_title, const gchar *pref_name, const gchar *stock_icon_name, const gchar *sizing_text, gint model_data_column, gint model_visibility_column, GtkTreeIterCompareFunc column_sort_fn)
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)
utility functions for the GnuCash UI
int safe_utf8_collate(const char *da, const char *db)
gboolean gnc_commodity_get_quote_flag(const gnc_commodity *cm)
common utilities for manipulating a GtkTreeView within gnucash
#define DEBUG(format, args...)
gnc_commodity_namespace * gnc_tree_model_commodity_get_namespace(GncTreeModelCommodity *model, GtkTreeIter *iter)
const char * gnc_commodity_get_namespace(const gnc_commodity *cm)
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)
#define ENTER(format, args...)
const char * gnc_commodity_namespace_get_name(const gnc_commodity_namespace *ns)
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)
void gnc_tree_view_configure_columns(GncTreeView *view)
GtkTreeModel * gnc_tree_model_commodity_new(QofBook *book, gnc_commodity_table *ct)
const char * gnc_commodity_get_fullname(const gnc_commodity *cm)
Gnome specific utility functions.
All type declarations for the whole Gnucash engine.
gboolean gnc_tree_model_commodity_iter_is_commodity(GncTreeModelCommodity *model, GtkTreeIter *iter)
gboolean gnc_tree_model_commodity_iter_is_namespace(GncTreeModelCommodity *model, GtkTreeIter *iter)
gnc_commodity * gnc_tree_view_commodity_get_selected_commodity(GncTreeViewCommodity *view)
#define LEAVE(format, args...)
GtkTreeView implementation for gnucash commodity tree.
void gnc_tree_view_commodity_refilter(GncTreeViewCommodity *view)
Commodity handling public routines.
const gchar * QofLogModule