1 /**********************************************************************\
2  * gnc-tree-view-account.h -- GtkTreeView implementation to display   *
3  *                            accounts in a GtkTreeView.              *
4  * Copyright (C) 2003,2005,2006 David Hampton <hampton@employees.org> *
5  *                                                                    *
6  * This program is free software; you can redistribute it and/or      *
7  * modify it under the terms of the GNU General Public License as     *
8  * published by the Free Software Foundation; either version 2 of     *
9  * the License, or (at your option) any later version.                *
10  *                                                                    *
11  * This program is distributed in the hope that it will be useful,    *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      *
14  * GNU General Public License for more details.                       *
15  *                                                                    *
16  * You should have received a copy of the GNU General Public License  *
17  * along with this program; if not, contact:                          *
18  *                                                                    *
19  * Free Software Foundation           Voice:  +1-617-542-5942         *
20  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652         *
21  * Boston, MA  02110-1301,  USA       gnu@gnu.org                     *
22  *                                                                    *
23 \**********************************************************************/
24 
25 /** @addtogroup GUI
26     @{ */
27 /** @addtogroup GuiTreeModel
28  * @{ */
29 /** @file gnc-tree-view-account.h
30     @brief GtkTreeView implementation for gnucash account tree.
31     @author Copyright (C) 2003,2005,2006 David Hampton <hampton@employees.org>
32 */
33 
34 #ifndef __GNC_TREE_VIEW_ACCOUNT_H
35 #define __GNC_TREE_VIEW_ACCOUNT_H
36 
37 #include <gtk/gtk.h>
38 #include "gnc-tree-view.h"
39 
40 #include "gnc-ui-util.h"
41 #include "gnc-plugin-page.h"
42 
43 G_BEGIN_DECLS
44 
45 /* type macros */
46 #define GNC_TYPE_TREE_VIEW_ACCOUNT            (gnc_tree_view_account_get_type ())
47 #define GNC_TREE_VIEW_ACCOUNT(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GNC_TYPE_TREE_VIEW_ACCOUNT, GncTreeViewAccount))
48 #define GNC_TREE_VIEW_ACCOUNT_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GNC_TYPE_TREE_VIEW_ACCOUNT, GncTreeViewAccountClass))
49 #define GNC_IS_TREE_VIEW_ACCOUNT(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GNC_TYPE_TREE_VIEW_ACCOUNT))
50 #define GNC_IS_TREE_VIEW_ACCOUNT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GNC_TYPE_TREE_VIEW_ACCOUNT))
51 #define GNC_TREE_VIEW_ACCOUNT_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GNC_TYPE_TREE_VIEW_ACCOUNT, GncTreeViewAccountClass))
52 #define GNC_TREE_VIEW_ACCOUNT_NAME            "GncTreeViewAccount"
53 
54 /* typedefs & structures */
55 typedef struct AccountViewInfo_s     AccountViewInfo;
56 
57 
58 struct AccountViewInfo_s
59 {
60     gboolean include_type[NUM_ACCOUNT_TYPES];
61     gboolean show_hidden;
62 };
63 
64 
65 typedef struct
66 {
67     GncTreeView   gnc_tree_view;
68     int           stamp;
69 } GncTreeViewAccount;
70 
71 typedef struct
72 {
73     GncTreeViewClass gnc_tree_view;
74 } GncTreeViewAccountClass;
75 
76 typedef	struct
77 {
78     GtkWidget           *dialog;
79     GtkTreeModel        *model;
80     GncTreeViewAccount  *tree_view;
81     GHashTable          *filter_override;
82     guint32              visible_types;
83     guint32              original_visible_types;
84     gboolean             show_hidden;
85     gboolean             original_show_hidden;
86     gboolean             show_zero_total;
87     gboolean             original_show_zero_total;
88     gboolean             show_unused;
89     gboolean             original_show_unused;
90 } AccountFilterDialog;
91 
92 void account_filter_dialog_create(AccountFilterDialog *fd,
93                                   GncPluginPage *page);
94 
95 gboolean gnc_plugin_page_account_tree_filter_accounts (Account *account,
96         gpointer user_data);
97 
98 /* "Filter By" dialog callbacks */
99 void gppat_filter_show_hidden_toggled_cb (GtkToggleButton *togglebutton,
100         AccountFilterDialog *fd);
101 void gppat_filter_show_zero_toggled_cb (GtkToggleButton *togglebutton,
102                                         AccountFilterDialog *fd);
103 void gppat_filter_show_unused_toggled_cb (GtkToggleButton *togglebutton,
104                                         AccountFilterDialog *fd);
105 void gppat_filter_clear_all_cb (GtkWidget *button, AccountFilterDialog *fd);
106 void gppat_filter_select_all_cb (GtkWidget *button, AccountFilterDialog *fd);
107 void gppat_filter_select_default_cb (GtkWidget *button,
108                                      AccountFilterDialog *fd);
109 void gppat_filter_response_cb (GtkWidget *dialog, gint response,
110                                AccountFilterDialog *fd);
111 
112 /* Saving/Restoring */
113 void gnc_tree_view_account_save(GncTreeViewAccount *tree_view,
114                                 AccountFilterDialog *fd,
115                                 GKeyFile *key_file, const gchar *group_name);
116 void gnc_tree_view_account_restore(GncTreeViewAccount *view,
117                                    AccountFilterDialog *fd,
118                                    GKeyFile *key_file,
119                                    const gchar *group_name);
120 
121 void gnc_tree_view_account_save_filter (GncTreeViewAccount *tree_view,
122                                         AccountFilterDialog *fd,
123                                         GKeyFile *key_file,
124                                         const gchar *group_name);
125 void gnc_tree_view_account_restore_filter (GncTreeViewAccount *view,
126                                            AccountFilterDialog *fd,
127                                            GKeyFile *key_file,
128                                            const gchar *group_name);
129 
130 
131 /* Get the GType for an GncTreeViewAccount object. */
132 GType gnc_tree_view_account_get_type (void);
133 
134 
135 /** @name Account Tree View Constructors
136  @{ */
137 
138 /** Create a new account tree view.  This view may or may not show a
139  *  pseudo top-level account.  The gnucash engine does not have a
140  *  single top level account (it has a list of top level accounts),
141  *  but this code provides one so that it can be used with all parts
142  *  of the gnucash gui.
143  *
144  *  @param root The account to use as the first level of the created tree.
145  *
146  *  @param show_root Show the pseudo top-level account in this view.
147  *
148  *  @return A pointer to a new account tree view.
149  */
150 GtkTreeView *gnc_tree_view_account_new_with_root (Account *root,
151         gboolean show_root);
152 
153 /** Create a new account tree view.  This view may or may not show a
154  *  pseudo top-level account.  The gnucash engine does not have a
155  *  single top level account (it has a list of top level accounts),
156  *  but this code provides one so that it can be used with all parts
157  *  of the gnucash gui.  The first level of accounts in the created
158  *  tree will be the top level of accounts in the current book.
159  *
160  *  @param show_root Show the pseudo top-level account in this view.
161  *
162  *  @return A pointer to a new account tree view.
163  */
164 GtkTreeView *gnc_tree_view_account_new (gboolean show_root);
165 
166 /** @} */
167 
168 
169 /** @name Account Tree View Configuration
170  @{ */
171 
172 typedef gchar * (*GncTreeViewAccountColumnSource) (Account *account,
173         GtkTreeViewColumn *col,
174         GtkCellRenderer *cell);
175 
176 typedef void (*GncTreeViewAccountColumnTextEdited) (Account *account,
177         GtkTreeViewColumn *col,
178         const gchar *new_text);
179 
180 
181 /** Add a new custom column to the set of columns in an account tree
182  *  view.  This column will be visible as soon as it is added and will
183  *  query the provided functions to determine what data to display.
184  *  The TreeView will own the resulting TreeViewColumn, but caller may
185  *  set any additional properties they wish.
186  *
187  *  @param view A pointer to an account tree view.
188  *
189  *  @param column_title The title for this new column.
190  *
191  *  @param source_cb A callback function that is expected to provide
192  *  the data to be displayed.
193  *
194  *  @param edited_cb A callback function that will be called if the
195  *  user edits the displayed data.
196  */
197 GtkTreeViewColumn * gnc_tree_view_account_add_custom_column(
198     GncTreeViewAccount *view, const gchar *column_title,
199     GncTreeViewAccountColumnSource source_cb,
200     GncTreeViewAccountColumnTextEdited edited_cb);
201 GtkTreeViewColumn *gnc_tree_view_account_add_custom_column_renderer(
202     GncTreeViewAccount *account_view, const gchar *column_title,
203     GncTreeViewAccountColumnSource col_source_cb,
204     GncTreeViewAccountColumnTextEdited col_edited_cb,
205     GtkCellRenderer *renderer);
206 void gnc_tree_view_account_set_name_edited(GncTreeViewAccount *view,
207         GncTreeViewAccountColumnTextEdited edited_cb);
208 void gnc_tree_view_account_name_edited_cb(Account *account, GtkTreeViewColumn *col, const gchar *new_name);
209 
210 void gnc_tree_view_account_set_code_edited(GncTreeViewAccount *view,
211         GncTreeViewAccountColumnTextEdited edited_cb);
212 void gnc_tree_view_account_code_edited_cb(Account *account, GtkTreeViewColumn *col, const gchar *new_code);
213 
214 void gnc_tree_view_account_set_description_edited(GncTreeViewAccount *view,
215         GncTreeViewAccountColumnTextEdited edited_cb);
216 void gnc_tree_view_account_description_edited_cb(Account *account, GtkTreeViewColumn *col, const gchar *new_desc);
217 
218 void gnc_tree_view_account_set_notes_edited(GncTreeViewAccount *view,
219         GncTreeViewAccountColumnTextEdited edited_cb);
220 void gnc_tree_view_account_notes_edited_cb(Account *account, GtkTreeViewColumn *col, const gchar *new_notes);
221 
222 /** Add a new column to the set of columns in an account tree view.
223  *  This column will be visible as soon as it is added and will
224  *  display the contents of the specified account property
225  *
226  *  @param view A pointer to an account tree view.
227  *
228  *  @param column_title The title for this new column.
229  *
230  *  @param propname The g_object_property name of the desired
231  *  value. This must be a string property.
232  */
233 GtkTreeViewColumn *
234 gnc_tree_view_account_add_property_column (GncTreeViewAccount *view,
235 					   const gchar *column_title,
236 					   const gchar *propname);
237 
238 /** @} */
239 
240 
241 /** @name Account Tree View Filtering
242  @{ */
243 
244 /** Given pointers to an account tree and old style filter block, this
245  *  function will copy the current configuration of the account tree
246  *  widget into the data block.  This may be used in conjunction with
247  *  the gnc_tree_view_account_set_view_info function to modify the
248  *  filters on an existing account tree.
249  *
250  *  @param account_view A pointer to an account tree view.
251  *
252  *  @param avi A pointer to an old style filter block to fill in.
253  */
254 void gnc_tree_view_account_get_view_info (GncTreeViewAccount *account_view,
255         AccountViewInfo *avi);
256 
257 /** Given pointers to an account tree and old style filter block, this
258  *  function will applies the settings specified to the current
259  *  configuration of the account tree widget.  This may be used in
260  *  conjunction with the gnc_tree_view_account_get_view_info function
261  *  to modify the filters on an existing account tree.
262  *
263  *  @param account_view A pointer to an account tree view.
264  *
265  *  @param avi A pointer to an old style filter block to apply to the
266  *  view.
267  */
268 void gnc_tree_view_account_set_view_info (GncTreeViewAccount *account_view,
269         AccountViewInfo *avi);
270 
271 
272 /** This is the description of a filter function used by the account tree.
273  *
274  *  @param account The account to be tested.
275  *
276  *  @param data The data provided when the filter function was added.
277  *
278  *  @return TRUE if the account should be displayed.
279  */
280 typedef gboolean (*gnc_tree_view_account_filter_func)(Account *account, gpointer data);
281 
282 
283 /** This function attaches a filter function to the given account
284  *  tree.  This function will be called for each account that the view
285  *  thinks should possibly show.  The filter may perform any actions
286  *  necessary on the account to decide whether it should be shown or
287  *  not.  (I.E. Check type, placeholder status, etc.)  If the filter
288  *  returns TRUE then the account will be displayed.
289  *
290  *  @param account_view A pointer to an account tree view.
291  *
292  *  @param func A filtration function that is called on individual
293  *  elements in the tree.  If this function returns TRUE, the account
294  *  will be displayed.
295  *
296  *  @param data A data block passed into each instance of the function.
297  *
298  *  @param destroy A function to destroy the data block.  This
299  *  function will be called when the filter is destroyed.  may be
300  *  NULL.
301  */
302 void gnc_tree_view_account_set_filter (GncTreeViewAccount *account_view,
303                                        gnc_tree_view_account_filter_func func,
304                                        gpointer data,
305                                        GSourceFunc destroy);
306 
307 /*  This is a convenient filter function for use with
308  *  gnc_tree_view_account_set_filter() and the functions in
309  *  gnc-tree-model-account-types.h.  If you have some view that is
310  *  backed by the "account types" tree model, you can get a guint32
311  *  from that view's tree selection.  Then, you can use that account
312  *  type selection as a filter for the account tree view.  This also
313  *  can filter by whether an account is hidden or not.
314  */
315 gboolean gnc_tree_view_account_filter_by_view_info(
316     Account* acct, gpointer data);
317 
318 
319 /** This function forces the account tree filter to be evaluated.  It
320  *  may be necessary to call this function if the initial state of the
321  *  view is incorrect.  This appears to only be necessary if the
322  *  filter affects one of the top level accounts in gnucash.
323  *
324  *  @note This calls a function in gtk that is annotated in the
325  *  sources as being slow.  You have been warned.
326  *
327  *  @param view A pointer to an account tree view.
328  */
329 void gnc_tree_view_account_refilter (GncTreeViewAccount *view);
330 /** @} */
331 
332 
333 /** @name Account Tree View Get/Set Functions
334  @{ */
335 
336 /** This function determines if an account in the account tree view
337  *  has any visible children.
338  *
339  *  @param view A pointer to an account tree view.
340  *
341  *  @param account A pointer to the account to check.
342  *
343  *  @return The number of children of the specified account. Returns 0
344  *  on error.
345  */
346 gint gnc_tree_view_account_count_children (GncTreeViewAccount *view,
347         Account *account);
348 
349 /** This function clears the tree model account cache so the values will
350  *  be updated/refreshed.
351  *
352  *  @param view A pointer to an account tree view.
353  *
354  */
355 void gnc_tree_view_account_clear_model_cache (GncTreeViewAccount *view);
356 
357 
358 /** This function returns the account associated with the specified
359  *  path.  This function is useful in selection callbacks on an
360  *  account tree widget.
361  *
362  *  @param view A pointer to an account tree view.
363  *
364  *  @param path A path specifying a node in the account tree.
365  *
366  *  @return The account associated with this path.
367  */
368 Account * gnc_tree_view_account_get_account_from_path (GncTreeViewAccount *view,
369         GtkTreePath *path);
370 
371 
372 /** This function returns the account associated with the specified
373  *  iter.  This function is useful in selection callbacks on an
374  *  account tree widget.
375  *
376  *  @param model The model provided to the callback function.
377  *
378  *  @param iter The iter provided to the callback function.
379  *
380  *  @return The account associated with this iter.
381  */
382 Account * gnc_tree_view_account_get_account_from_iter (GtkTreeModel *model,
383         GtkTreeIter  *iter);
384 
385 
386 /** This function returns the account in the account tree view at the
387  *  current location of the cursor. (The outline frame. Usually is
388  *  selected and therefore filled in, but not always.)
389  *
390  *  @param view A pointer to an account tree view.
391  *
392  *  @return The account at the cursor.
393  */
394 Account * gnc_tree_view_account_get_cursor_account (GncTreeViewAccount *view);
395 
396 
397 /** This function returns the account associated with the selected
398  *  item in the account tree view.
399  *
400  *  @note It only makes sense to call this function when the account
401  *  tree is set to select a single item.  There is a different
402  *  function to use when the tree supports multiple selections.
403  *
404  *  @param view A pointer to an account tree view.
405  *
406  *  @return The selected account, or NULL if no account was selected.
407  */
408 Account * gnc_tree_view_account_get_selected_account (GncTreeViewAccount *view);
409 
410 
411 /** This function selects an account in the account tree view.  All
412  *  other accounts will be unselected.  In addition, this function
413  *  collapses the entire tree and then expands only the path to the
414  *  selected account, making the item easy to find.  In general, this
415  *  routine only need be called when initially putting up a window
416  *  containing an account tree view widget.
417  *
418  *  @note It only makes sense to call this function when the account
419  *  tree is set to select a single item.  There is a different
420  *  function to use when the tree supports multiple selections.
421  *
422  *  @param view A pointer to an account tree view.
423  *
424  *  @param account A pointer to the account to select.
425  */
426 void gnc_tree_view_account_set_selected_account (GncTreeViewAccount *view,
427         Account *account);
428 
429 
430 /** This function returns a list of the accounts associated with the
431  *  selected items in the account tree view.
432  *
433  *  @note It only makes sense to call this function when the account
434  *  tree is set to select multiple items.  There is a different
435  *  function to use when the tree supports single selection.
436  *
437  *  @param view A pointer to an account tree view.
438  *
439  *  @return A list of accounts, or NULL if no account was selected.
440  */
441 GList * gnc_tree_view_account_get_selected_accounts (GncTreeViewAccount *view);
442 
443 
444 /** This function selects a set of accounts in the account tree view.
445  *  All other accounts will be unselected.  In addition, this function
446  *  collapses the entire tree and then expands only the path to the
447  *  selected accounts, making them easy to find.  In general, this
448  *  routine only need be called when initially putting up a window
449  *  containing an account tree view widget.
450  *
451  *  @note It only makes sense to call this function when the account
452  *  tree is set to select a single item.  There is a different
453  *  function to use when the tree supports multiple selections.
454  *
455  *  @note It is the responsibility of the caller to free the returned
456  *  list.
457  *
458  *  @param view A pointer to an account tree view.
459  *
460  *  @param account_list A list of accounts to select.
461  *
462  *  @param show_last Force the window to scroll to the last account
463  *  selected.
464  */
465 void gnc_tree_view_account_set_selected_accounts (GncTreeViewAccount *view,
466         GList *account_list,
467         gboolean show_last);
468 
469 
470 /** This function selects all sub-accounts of an account in the
471  *  account tree view.  All other accounts will be unselected.
472  *
473  *  @note It only makes sense to call this function when the account
474  *  tree is set to select multiple items.  There is a different
475  *  function to use when the tree supports multiple selections.
476  *
477  *  @param view A pointer to an account tree view.
478  *
479  *  @param account A pointer to the account whose children should be
480  *  selected.
481  */
482 void gnc_tree_view_account_select_subaccounts (GncTreeViewAccount *view,
483         Account *account);
484 
485 /** This function forces the account tree expand whatever levels are
486  *  necessary to make the specified account visible.
487  *
488  *  @param view A pointer to an account tree view.
489  *
490  *  @param account A pointer to the account to show.
491  */
492 void gnc_tree_view_account_expand_to_account (GncTreeViewAccount *view, Account *account);
493 
494 /** Add the account color background data function to the GncTreeViewAccount column to
495  *  show or not the column background in the account color.
496  */
497 void gnc_tree_view_account_column_add_color (GncTreeViewAccount *view, GtkTreeViewColumn *col);
498 
499 /** Setup the callback for when the user starts editing the account tree so actions can be disabled
500  *  like the delete menu option as required.
501  */
502 void gnc_tree_view_account_set_editing_started_cb
503     (GncTreeViewAccount *view, GFunc editing_started_cb, gpointer editing_cb_data );
504 
505 /** Setup the callback for when the user finishes editing the account tree so actions can be enabled
506  *  like the delete menu option as required.
507  */
508 void gnc_tree_view_account_set_editing_finished_cb
509     (GncTreeViewAccount *view, GFunc editing_finished_cb, gpointer editing_cb_data );
510 
511 
512 /** @} */
513 
514 /** @} */
515 /** @} */
516 
517 G_END_DECLS
518 
519 #endif /* __GNC_TREE_VIEW_ACCOUNT_H */
520