|
GnuCash 2.4.99
|
00001 /* 00002 * Copyright (C) 2005, Chris Shoemaker <c.shoemaker@cox.net> 00003 * 00004 * This program is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU General Public License as 00006 * published by the Free Software Foundation; either version 2 of 00007 * the License, or (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, contact: 00016 * 00017 * Free Software Foundation Voice: +1-617-542-5942 00018 * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 00019 * Boston, MA 02110-1301, USA gnu@gnu.org 00020 */ 00021 00025 #include "config.h" 00026 00027 #include <gtk/gtk.h> 00028 #include <glib/gi18n.h> 00029 #include "gnc-tree-model-budget.h" 00030 #include "gnc-budget.h" 00031 #include "gnc-ui-util.h" 00032 00033 /* Add the new budget object to the tree model. */ 00034 static void add_budget_to_model(QofInstance* data, gpointer user_data ) 00035 { 00036 GtkTreeIter iter; 00037 GncBudget* budget = GNC_BUDGET(data); 00038 GtkTreeModel* treeModel = user_data; 00039 00040 g_return_if_fail(GNC_IS_BUDGET(budget)); 00041 g_return_if_fail(budget && treeModel); 00042 00043 gtk_list_store_append (GTK_LIST_STORE(treeModel), &iter); 00044 gtk_list_store_set (GTK_LIST_STORE(treeModel), &iter, 00045 BUDGET_GUID_COLUMN, gnc_budget_get_guid(budget), 00046 BUDGET_NAME_COLUMN, gnc_budget_get_name(budget), 00047 BUDGET_DESCRIPTION_COLUMN, 00048 gnc_budget_get_description(budget), -1); 00049 } 00050 00051 /* CAS: Even though it works, something feels not-quite-right with 00052 * this design. The idea here is to _not_ provide yet another 00053 * implementation of GtkTreeModel, this time for budgets. Instead, 00054 * right now, we're using the already implemented GtkListStore. This 00055 * has a couple consequences: 1) We allocate a new store upon every 00056 * call, so the memory is owned by caller. 2) The model won't reflect 00057 * later updates to the book, so the model shouldn't be expected to 00058 * track asynchronous changes. 00059 * 00060 * If, for some reason, I decide I can't live with or remove those 00061 * consequences, I still think there must be some better way than 00062 * re-implementing GtkTreeModel. One idea I'm toying with is to 00063 * implement a GtkTreeModel for QofCollections, which would offer only 00064 * the GncGUID as a field. Then, TreeViews could add their own columns 00065 * with custom CellDataFuncs to display the object-specific fields. 00066 * Or, something like that. :) 00067 * 00068 */ 00069 GtkTreeModel * 00070 gnc_tree_model_budget_new(QofBook *book) 00071 { 00072 GtkListStore* store; 00073 00074 store = gtk_list_store_new (BUDGET_LIST_NUM_COLS, 00075 G_TYPE_POINTER, 00076 G_TYPE_STRING, 00077 G_TYPE_STRING); 00078 00079 qof_collection_foreach(qof_book_get_collection(book, GNC_ID_BUDGET), 00080 add_budget_to_model, GTK_TREE_MODEL(store)); 00081 00082 return GTK_TREE_MODEL(store); 00083 } 00084 00085 void 00086 gnc_tree_view_budget_set_model(GtkTreeView *tv, GtkTreeModel *tm) 00087 { 00088 GtkCellRenderer *renderer; 00089 GtkTreeViewColumn *column; 00090 00091 gtk_tree_view_set_model (tv, tm); 00092 00093 /* column for name */ 00094 renderer = gtk_cell_renderer_text_new (); 00095 column = gtk_tree_view_column_new_with_attributes ( 00096 _("Name"), renderer, "text", BUDGET_NAME_COLUMN, NULL); 00097 gtk_tree_view_append_column (tv, column); 00098 00099 /* column for description */ 00100 renderer = gtk_cell_renderer_text_new (); 00101 column = gtk_tree_view_column_new_with_attributes ( 00102 _("Description"), renderer, "text", BUDGET_DESCRIPTION_COLUMN, NULL); 00103 gtk_tree_view_append_column (tv, column); 00104 00105 } 00106 00107 GncBudget * 00108 gnc_tree_model_budget_get_budget(GtkTreeModel *tm, GtkTreeIter *iter) 00109 { 00110 GncBudget *bgt; 00111 GValue gv = { 0 }; 00112 GncGUID *guid; 00113 00114 gtk_tree_model_get_value(tm, iter, BUDGET_GUID_COLUMN, &gv); 00115 guid = (GncGUID *) g_value_get_pointer(&gv); 00116 g_value_unset(&gv); 00117 00118 bgt = gnc_budget_lookup(guid, gnc_get_current_book()); 00119 return bgt; 00120 } 00121 00122 gboolean 00123 gnc_tree_model_budget_get_iter_for_budget(GtkTreeModel *tm, GtkTreeIter *iter, 00124 GncBudget *bgt) 00125 { 00126 GValue gv = { 0 }; 00127 const GncGUID *guid1; 00128 GncGUID *guid2; 00129 00130 g_return_val_if_fail(GNC_BUDGET(bgt), FALSE); 00131 00132 guid1 = gnc_budget_get_guid(bgt); 00133 if (!gtk_tree_model_get_iter_first(tm, iter)) 00134 return FALSE; 00135 while (gtk_list_store_iter_is_valid(GTK_LIST_STORE(tm), iter)) 00136 { 00137 gtk_tree_model_get_value(tm, iter, BUDGET_GUID_COLUMN, &gv); 00138 guid2 = (GncGUID *) g_value_get_pointer(&gv); 00139 g_value_unset(&gv); 00140 00141 if (guid_equal(guid1, guid2)) 00142 return TRUE; 00143 00144 if (!gtk_tree_model_iter_next(tm, iter)) 00145 return FALSE; 00146 } 00147 return FALSE; 00148 } 00149
1.7.4