GnuCash c935c2f+
Loading...
Searching...
No Matches
gnc-tree-view-sx-list.c
1
5/********************************************************************
6 * This program is free software; you can redistribute it and/or *
7 * modify it under the terms of version 2 and/or version 3 of the *
8 * GNU General Public *
9 * License as published by the Free Software Foundation. *
10 * *
11 * As a special exception, permission is granted to link the binary *
12 * module resultant from this code with the OpenSSL project's *
13 * "OpenSSL" library (or modified versions of it that use the same *
14 * license as the "OpenSSL" library), and distribute the linked *
15 * executable. You must obey the GNU General Public License in all *
16 * respects for all of the code used other than "OpenSSL". If you *
17 * modify this file, you may extend this exception to your version *
18 * of the file, but you are not obligated to do so. If you do not *
19 * wish to do so, delete this exception statement from your version *
20 * of this file. *
21 * *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
26 * *
27 * You should have received a copy of the GNU General Public License*
28 * along with this program; if not, contact: *
29 * *
30 * Free Software Foundation Voice: +1-617-542-5942 *
31 * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
32 * Boston, MA 02110-1301, USA gnu@gnu.org *
33 * *
34 *******************************************************************/
35
36#include <config.h>
37
38#include <gtk/gtk.h>
39#include <glib/gi18n.h>
40#include <string.h>
41
42#include "gnc-tree-view.h"
44#include "gnc-sx-list-tree-model-adapter.h"
45
46#define LOG_MOD "gnc.ui.tree-view.sx-list"
47static QofLogModule log_module = LOG_MOD;
48#undef G_LOG_DOMAIN
49#define G_LOG_DOMAIN LOG_MOD
50
51static void gnc_tree_view_sx_list_dispose (GObject *object);
52static void gnc_tree_view_sx_list_finalize (GObject *object);
53
55{
56 GncTreeView gnc_tree_view;
57
58 GtkTreeModel *tree_model;
59
60 SchedXaction *sx;
61 GtkAdjustment *adjustment;
62 gdouble position;
63
64 gboolean disposed;
65};
66
67G_DEFINE_TYPE(GncTreeViewSxList, gnc_tree_view_sx_list, GNC_TYPE_TREE_VIEW)
68
69static void
70gnc_tree_view_sx_list_class_init (GncTreeViewSxListClass *klass)
71{
72 GObjectClass *o_class = G_OBJECT_CLASS(klass);
73
74 o_class->dispose = gnc_tree_view_sx_list_dispose;
75 o_class->finalize = gnc_tree_view_sx_list_finalize;
76}
77
78static void
79gnc_tree_view_sx_list_init (GncTreeViewSxList *view)
80{
81 ; /* nop */
82}
83
84static void
85gnc_tree_view_sx_list_dispose (GObject *object)
86{
87 GncTreeViewSxList *view;
88
89 gnc_leave_return_if_fail (object != NULL);
90 gnc_leave_return_if_fail (GNC_IS_TREE_VIEW_SX_LIST(object));
91
92 view = GNC_TREE_VIEW_SX_LIST(object);
93
94 if (view->disposed)
95 return;
96 view->disposed = TRUE;
97
98 g_object_unref (G_OBJECT(view->tree_model));
99 view->tree_model = NULL;
100
101 G_OBJECT_CLASS(gnc_tree_view_sx_list_parent_class)->dispose (object);
102}
103
104static void
105gnc_tree_view_sx_list_finalize(GObject *object)
106{
107 gnc_leave_return_if_fail (object != NULL);
108 gnc_leave_return_if_fail (GNC_IS_TREE_VIEW_SX_LIST(object));
109
110 G_OBJECT_CLASS(gnc_tree_view_sx_list_parent_class)->finalize (object);
111}
112
113/************************************************************
114 * Callbacks *
115 ************************************************************/
116
117static gboolean
118gnc_tree_view_sx_list_restore (gpointer user_data)
119{
120 GncTreeViewSxList *view = user_data;
121
122 if (view->adjustment)
123 {
124 gtk_adjustment_set_value (view->adjustment, view->position);
125 view->adjustment = NULL;
126 }
127 if (view->sx)
128 {
129 SchedXaction *sx = view->sx;
130 GtkTreePath *path = gtk_tree_path_new_first ();
131
132 while (gnc_tree_view_path_is_valid (GNC_TREE_VIEW(view), path))
133 {
134 if (sx == gnc_tree_view_sx_list_get_sx_from_path (view, path))
135 {
136 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(view));
137
138 gtk_tree_selection_unselect_all (selection);
139 gtk_tree_selection_select_path (selection, path);
140 gtk_tree_view_set_cursor (GTK_TREE_VIEW(view), path, NULL, FALSE);
141 gtk_widget_grab_focus (GTK_WIDGET(view));
142 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW(view), path, NULL, FALSE, 0.0, 0.0);
143 break;
144 }
145 gtk_tree_path_next (path);
146 }
147 gtk_tree_path_free (path);
148
149 view->sx = NULL;
150 }
151
152 return FALSE;
153}
154
155static void
156gnc_tree_view_sx_list_enabled_toggled (GtkCellRendererToggle *cell,
157 const gchar *path_str,
158 gpointer user_data)
159{
160 GncTreeViewSxList *view = user_data;
161 GtkTreePath *path = gtk_tree_path_new_from_string (path_str);
162 SchedXaction *sx = gnc_tree_view_sx_list_get_sx_from_path (view, path);
163
164 if (sx)
165 {
166 GtkTreeSortable *sortable = GTK_TREE_SORTABLE(view->tree_model);
167 gint sort_column_id;
168 GtkSortType sort_order;
169
170 if (gtk_tree_sortable_get_sort_column_id (sortable, &sort_column_id, &sort_order) &&
171 sort_column_id == SXLTMA_COL_ENABLED)
172 {
173 view->sx = sx;
174 }
175 else
176 {
177 view->adjustment = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE(view));
178 view->position = gtk_adjustment_get_value (view->adjustment);
179 }
180
181 gboolean enabled = !gtk_cell_renderer_toggle_get_active (cell);
182
183 xaccSchedXactionSetEnabled (sx, enabled);
184
185 g_idle_add((GSourceFunc)gnc_tree_view_sx_list_restore, user_data);
186 }
187
188 gtk_tree_path_free (path);
189}
190
191
192GtkTreeView*
193gnc_tree_view_sx_list_new (GncSxInstanceModel *sx_instances)
194{
195 GncTreeViewSxList *view = (GncTreeViewSxList*)g_object_new (GNC_TYPE_TREE_VIEW_SX_LIST, NULL);
196 g_object_set (view, "name", "gnc-id-sx-list-tree", NULL);
197
198 view->tree_model = GTK_TREE_MODEL(gnc_sx_list_tree_model_adapter_new (sx_instances));
199 gtk_tree_view_set_model (GTK_TREE_VIEW(view), GTK_TREE_MODEL(view->tree_model));
200
201 GtkTreeViewColumn *col = gnc_tree_view_add_text_column (GNC_TREE_VIEW(view), _("Name"), "name", NULL,
202 "Semi-Monthly Paycheck",
203 SXLTMA_COL_NAME, -1, NULL);
204 g_object_set_data (G_OBJECT(col), DEFAULT_VISIBLE, GINT_TO_POINTER(1));
205
206 col = gnc_tree_view_add_toggle_column (GNC_TREE_VIEW(view), _("Enabled"),
207 C_("Single-character short column-title form of 'Enabled'", "E"),
208 "enabled", SXLTMA_COL_ENABLED,
209 GNC_TREE_VIEW_COLUMN_VISIBLE_ALWAYS,
210 NULL, gnc_tree_view_sx_list_enabled_toggled);
211 g_object_set_data (G_OBJECT(col), DEFAULT_VISIBLE, GINT_TO_POINTER(1));
212
213 col = gnc_tree_view_add_text_column (GNC_TREE_VIEW(view), _("Frequency"), "frequency", NULL,
214 "Weekly (x3): -------",
215 SXLTMA_COL_FREQUENCY, -1, NULL);
216 g_object_set_data (G_OBJECT(col), DEFAULT_VISIBLE, GINT_TO_POINTER(1));
217
218 col = gnc_tree_view_add_numeric_column (GNC_TREE_VIEW(view), _("Postponed"),
219 "postponed", " Postponed",
220 SXLTMA_COL_NUM_POSTPONED,
221 GNC_TREE_VIEW_COLUMN_COLOR_NONE,
222 GNC_TREE_VIEW_COLUMN_VISIBLE_ALWAYS, NULL);
223 g_object_set_data (G_OBJECT(col), DEFAULT_VISIBLE, GINT_TO_POINTER(0));
224
225 col = gnc_tree_view_add_text_column (GNC_TREE_VIEW(view), _("Last Occur"), "last-occur", NULL,
226 "2007-01-02",
227 SXLTMA_COL_LAST_OCCUR, -1, NULL);
228 g_object_set_data (G_OBJECT(col), DEFAULT_VISIBLE, GINT_TO_POINTER(1));
229
230 col = gnc_tree_view_add_text_column (GNC_TREE_VIEW(view), _("Next Occur"), "next-occur", NULL,
231 "2007-01-02",
232 SXLTMA_COL_NEXT_OCCUR, -1, NULL);
233 g_object_set_data (G_OBJECT(col), DEFAULT_VISIBLE, GINT_TO_POINTER(1));
234
235 gnc_tree_view_configure_columns (GNC_TREE_VIEW(view));
236
237 gtk_widget_show (GTK_WIDGET(view));
238 return GTK_TREE_VIEW(view);
239}
240
241SchedXaction*
242gnc_tree_view_sx_list_get_sx_from_path (GncTreeViewSxList *view, GtkTreePath *path)
243{
244 GtkTreeIter iter;
245 gtk_tree_model_get_iter (GTK_TREE_MODEL(view->tree_model), &iter, path);
246 return gnc_sx_list_tree_model_adapter_get_sx_instances(
247 GNC_SX_LIST_TREE_MODEL_ADAPTER(view->tree_model), &iter)->sx;
248}
GncTreeView implementation for Scheduled Transaction List.
common utilities for manipulating a GtkTreeView within gnucash
void gnc_tree_view_configure_columns(GncTreeView *view)
Make all the correct columns visible, respecting their default visibility setting,...
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.
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.
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 gnc_leave_return_if_fail(test)
Replacement for g_return_if_fail, but calls LEAVE if the test fails.
Definition qoflog.h:300