GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-tree-model-budget.c
1 /*
2  * Copyright (C) 2005, Chris Shoemaker <[email protected]>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, contact:
16  *
17  * Free Software Foundation Voice: +1-617-542-5942
18  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
19  * Boston, MA 02110-1301, USA [email protected]
20  */
21 
25 #include "config.h"
26 
27 #include <gtk/gtk.h>
28 #include <glib/gi18n.h>
29 #include "gnc-tree-model-budget.h"
30 #include "gnc-budget.h"
31 #include "gnc-ui-util.h"
32 
33 /* Add the new budget object to the tree model. */
34 static void add_budget_to_model(QofInstance* data, gpointer user_data )
35 {
36  GtkTreeIter iter;
37  GncBudget* budget = GNC_BUDGET(data);
38  GtkTreeModel* treeModel = user_data;
39 
40  g_return_if_fail(GNC_IS_BUDGET(budget));
41  g_return_if_fail(budget && treeModel);
42 
43  gtk_list_store_append (GTK_LIST_STORE(treeModel), &iter);
44  gtk_list_store_set (GTK_LIST_STORE(treeModel), &iter,
45  BUDGET_GUID_COLUMN, gnc_budget_get_guid(budget),
46  BUDGET_NAME_COLUMN, gnc_budget_get_name(budget),
47  BUDGET_DESCRIPTION_COLUMN,
48  gnc_budget_get_description(budget), -1);
49 }
50 
51 /* CAS: Even though it works, something feels not-quite-right with
52  * this design. The idea here is to _not_ provide yet another
53  * implementation of GtkTreeModel, this time for budgets. Instead,
54  * right now, we're using the already implemented GtkListStore. This
55  * has a couple consequences: 1) We allocate a new store upon every
56  * call, so the memory is owned by caller. 2) The model won't reflect
57  * later updates to the book, so the model shouldn't be expected to
58  * track asynchronous changes.
59  *
60  * If, for some reason, I decide I can't live with or remove those
61  * consequences, I still think there must be some better way than
62  * re-implementing GtkTreeModel. One idea I'm toying with is to
63  * implement a GtkTreeModel for QofCollections, which would offer only
64  * the GncGUID as a field. Then, TreeViews could add their own columns
65  * with custom CellDataFuncs to display the object-specific fields.
66  * Or, something like that. :)
67  *
68  */
69 GtkTreeModel *
70 gnc_tree_model_budget_new(QofBook *book)
71 {
72  GtkListStore* store;
73 
74  store = gtk_list_store_new (BUDGET_LIST_NUM_COLS,
75  G_TYPE_POINTER,
76  G_TYPE_STRING,
77  G_TYPE_STRING);
78 
80  add_budget_to_model, GTK_TREE_MODEL(store));
81 
82  return GTK_TREE_MODEL(store);
83 }
84 
85 void
86 gnc_tree_view_budget_set_model(GtkTreeView *tv, GtkTreeModel *tm)
87 {
88  GtkCellRenderer *renderer;
89  GtkTreeViewColumn *column;
90 
91  gtk_tree_view_set_model (tv, tm);
92 
93  /* column for name */
94  renderer = gtk_cell_renderer_text_new ();
95  column = gtk_tree_view_column_new_with_attributes (
96  _("Name"), renderer, "text", BUDGET_NAME_COLUMN, NULL);
97  gtk_tree_view_append_column (tv, column);
98 
99  /* column for description */
100  renderer = gtk_cell_renderer_text_new ();
101  column = gtk_tree_view_column_new_with_attributes (
102  _("Description"), renderer, "text", BUDGET_DESCRIPTION_COLUMN, NULL);
103  gtk_tree_view_append_column (tv, column);
104 
105 }
106 
107 GncBudget *
108 gnc_tree_model_budget_get_budget(GtkTreeModel *tm, GtkTreeIter *iter)
109 {
110  GncBudget *bgt;
111  GValue gv = { 0 };
112  GncGUID *guid;
113 
114  gtk_tree_model_get_value(tm, iter, BUDGET_GUID_COLUMN, &gv);
115  guid = (GncGUID *) g_value_get_pointer(&gv);
116  g_value_unset(&gv);
117 
118  bgt = gnc_budget_lookup(guid, gnc_get_current_book());
119  return bgt;
120 }
121 
122 gboolean
123 gnc_tree_model_budget_get_iter_for_budget(GtkTreeModel *tm, GtkTreeIter *iter,
124  GncBudget *bgt)
125 {
126  GValue gv = { 0 };
127  const GncGUID *guid1;
128  GncGUID *guid2;
129 
130  g_return_val_if_fail(GNC_BUDGET(bgt), FALSE);
131 
132  guid1 = gnc_budget_get_guid(bgt);
133  if (!gtk_tree_model_get_iter_first(tm, iter))
134  return FALSE;
135  while (gtk_list_store_iter_is_valid(GTK_LIST_STORE(tm), iter))
136  {
137  gtk_tree_model_get_value(tm, iter, BUDGET_GUID_COLUMN, &gv);
138  guid2 = (GncGUID *) g_value_get_pointer(&gv);
139  g_value_unset(&gv);
140 
141  if (guid_equal(guid1, guid2))
142  return TRUE;
143 
144  if (!gtk_tree_model_iter_next(tm, iter))
145  return FALSE;
146  }
147  return FALSE;
148 }
149 
utility functions for the GnuCash UI
GnuCash Budgets.
void qof_collection_foreach(const QofCollection *, QofInstanceForeachCB, gpointer user_data)
Definition: guid.h:65
gboolean guid_equal(const GncGUID *guid_1, const GncGUID *guid_2)
QofCollection * qof_book_get_collection(const QofBook *, QofIdType)
provides some utilities for working with the list of budgets in a book.