GnuCash  5.6-150-g038405b370+
gnc-tree-model-budget.c
1 /*
2  * Copyright (C) 2005, Chris Shoemaker <c.shoemaker@cox.net>
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 gnu@gnu.org
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 
79  qof_collection_foreach(qof_book_get_collection(book, GNC_ID_BUDGET),
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  GncGUID *guid;
112 
113  gtk_tree_model_get (tm, iter, BUDGET_GUID_COLUMN, &guid, -1);
114  bgt = gnc_budget_lookup(guid, gnc_get_current_book());
115  return bgt;
116 }
117 
118 gboolean
119 gnc_tree_model_budget_get_iter_for_budget(GtkTreeModel *tm, GtkTreeIter *iter,
120  GncBudget *bgt)
121 {
122  const GncGUID *guid1;
123  GncGUID *guid2;
124 
125  g_return_val_if_fail(GNC_BUDGET(bgt), FALSE);
126 
127  guid1 = gnc_budget_get_guid(bgt);
128  if (!gtk_tree_model_get_iter_first(tm, iter))
129  return FALSE;
130  while (gtk_list_store_iter_is_valid(GTK_LIST_STORE(tm), iter))
131  {
132  gtk_tree_model_get (tm, iter, BUDGET_GUID_COLUMN, &guid2, -1);
133 
134  if (guid_equal(guid1, guid2))
135  return TRUE;
136 
137  if (!gtk_tree_model_iter_next(tm, iter))
138  return FALSE;
139  }
140  return FALSE;
141 }
142 
utility functions for the GnuCash UI
GnuCash Budgets.
gboolean guid_equal(const GncGUID *guid_1, const GncGUID *guid_2)
Given two GUIDs, return TRUE if they are non-NULL and equal.
Definition: guid.cpp:204
QofCollection * qof_book_get_collection(const QofBook *book, QofIdType entity_type)
Return The table of entities of the given type.
Definition: qofbook.cpp:521
The type used to store guids in C.
Definition: guid.h:75
provides some utilities for working with the list of budgets in a book.