1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2017 Maxime DOYEN
4 * This file is part of HomeBank.
6 * HomeBank is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * HomeBank 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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "ui-category.h"
33 /* our global datas */
34 extern struct HomeBank
*GLOBALS
;
35 extern struct Preferences
*PREFS
;
38 gchar
*CYA_CAT_TYPE
[] = {
44 static void ui_cat_manage_populate_listview(struct ui_cat_manage_dialog_data
*data
);
46 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
49 * ui_cat_comboboxentry_get_name:
51 * get the name of the active category or -1
53 * Return value: a new allocated name tobe freed with g_free
57 ui_cat_comboboxentry_get_name(GtkComboBox
*entry_box
)
62 DB( g_print ("ui_cat_comboboxentry_get_name()\n") );
64 cbname
= (gchar
*)gtk_entry_get_text(GTK_ENTRY (gtk_bin_get_child(GTK_BIN (entry_box
))));
67 name
= g_strdup(cbname
);
76 * ui_cat_comboboxentry_get_key:
78 * get the key of the active category or -1
80 * Return value: the key or -1
84 ui_cat_comboboxentry_get_key_add_new(GtkComboBox
*entry_box
)
89 DB( g_print ("ui_cat_comboboxentry_get_key()\n") );
91 name
= (gchar
*)gtk_entry_get_text(GTK_ENTRY (gtk_bin_get_child(GTK_BIN (entry_box
))));
96 item
= da_cat_get_by_fullname(name
);
100 //todo: check prefs + ask the user here 1st time
101 item
= da_cat_append_ifnew_by_fullname(name
, FALSE
);
103 ui_cat_comboboxentry_add(entry_box
, item
);
111 * ui_cat_comboboxentry_get_key:
113 * get the key of the active category or -1
115 * Return value: the key or -1
119 ui_cat_comboboxentry_get_key(GtkComboBox
*entry_box
)
124 DB( g_print ("ui_cat_comboboxentry_get_key()\n") );
126 name
= (gchar
*)gtk_entry_get_text(GTK_ENTRY (gtk_bin_get_child(GTK_BIN (entry_box
))));
130 item
= da_cat_get_by_fullname(name
);
139 *ui_cat_comboboxentry_get(GtkComboBox
*entry_box
)
141 Category
*item
= NULL
;
144 DB( g_print ("ui_cat_comboboxentry_get_key()\n") );
146 name
= (gchar
*)gtk_entry_get_text(GTK_ENTRY (gtk_bin_get_child(GTK_BIN (entry_box
))));
150 item
= da_cat_get_by_fullname(name
);
157 ui_cat_comboboxentry_set_active(GtkComboBox
*entry_box
, guint32 key
)
162 DB( g_print ("ui_cat_comboboxentry_set_active()\n") );
167 item
= da_cat_get(key
);
170 if( item
->parent
== 0)
171 gtk_entry_set_text(GTK_ENTRY (gtk_bin_get_child(GTK_BIN (entry_box
))), item
->name
);
174 fullname
= da_cat_get_fullname(item
);
175 gtk_entry_set_text(GTK_ENTRY (gtk_bin_get_child(GTK_BIN (entry_box
))), fullname
);
181 gtk_entry_set_text(GTK_ENTRY (gtk_bin_get_child(GTK_BIN (entry_box
))), "");
186 * ui_cat_comboboxentry_add:
188 * Add a single element (useful for dynamics add)
194 ui_cat_comboboxentry_add(GtkComboBox
*entry_box
, Category
*item
)
197 DB( g_print ("ui_cat_comboboxentry_add()\n") );
200 DB( g_print (" -> try to add: '%s'\n", item
->name
) );
202 if( item
->name
!= NULL
)
207 gchar
*fullname
, *name
;
209 fullname
= da_cat_get_fullname(item
);
210 model
= gtk_combo_box_get_model(GTK_COMBO_BOX(entry_box
));
212 if( item
->parent
== 0 )
213 name
= g_strdup(item
->name
);
215 name
= g_strdup_printf(" - %s", item
->name
);
217 gtk_list_store_append (GTK_LIST_STORE(model
), &iter
);
218 gtk_list_store_set (GTK_LIST_STORE(model
), &iter
,
219 LST_CMBCAT_DATAS
, item
,
220 LST_CMBCAT_FULLNAME
, fullname
,
221 LST_CMBCAT_SORTNAME
, NULL
,
222 LST_CMBCAT_NAME
, name
,
223 LST_CMBCAT_SUBCAT
, item
->parent
== 0 ? 1 : 0,
234 ui_cat_comboboxentry_populate_ghfunc(gpointer key
, gpointer value
, struct catPopContext
*ctx
)
237 Category
*item
= value
;
238 Category
*pitem
= NULL
;
239 gchar
*fullname
, *name
, *sortname
;
242 if( ( item
->key
!= ctx
->except_key
) )
244 pitem
= da_cat_get(item
->parent
);
246 type
= (item
->flags
& GF_INCOME
) ? '+' : '-';
247 fullname
= da_cat_get_fullname(item
);
251 //DB( g_print ("cat combo populate [%d:%d] %s\n", item->parent, item->key, fullname) );
255 name
= g_strdup(item
->name
);
256 sortname
= g_strdup(item
->name
);
260 if( item
->parent
== 0 )
262 name
= g_strdup_printf("%s [%c]", item
->name
, type
);
263 sortname
= g_strdup_printf("%s", item
->name
);
269 name
= g_strdup_printf(" %c %s", type
, item
->name
);
270 sortname
= g_strdup_printf("%s_%s", pitem
->name
, item
->name
);
275 hb_string_replace_char(' ', sortname
);
277 //gtk_list_store_append (GTK_LIST_STORE(ctx->model), &iter);
278 //gtk_list_store_set (GTK_LIST_STORE(ctx->model), &iter,
279 gtk_list_store_insert_with_values(GTK_LIST_STORE(ctx
->model
), &iter
, -1,
280 LST_CMBCAT_DATAS
, item
,
281 LST_CMBCAT_FULLNAME
, fullname
,
282 LST_CMBCAT_SORTNAME
, sortname
,
283 LST_CMBCAT_NAME
, name
,
284 LST_CMBCAT_SUBCAT
, item
->parent
== 0 ? 1 : 0,
287 DB( g_print(" - add [%2d:%2d] '%-12s' '%-12s' '%s' '%s' %d\n", item
->parent
, item
->key
, pitem
->name
, name
, fullname
, sortname
, item
->parent
== 0 ? 1 : 0) );
297 * ui_cat_comboboxentry_populate:
299 * Populate the list and completion
305 ui_cat_comboboxentry_populate(GtkComboBox
*entry_box
, GHashTable
*hash
)
307 ui_cat_comboboxentry_populate_except(entry_box
, hash
, -1);
311 ui_cat_comboboxentry_populate_except(GtkComboBox
*entry_box
, GHashTable
*hash
, guint except_key
)
314 //GtkEntryCompletion *completion;
315 struct catPopContext ctx
;
317 DB( g_print ("ui_cat_comboboxentry_populate()\n") );
319 model
= gtk_combo_box_get_model(GTK_COMBO_BOX(entry_box
));
320 //completion = gtk_entry_get_completion(GTK_ENTRY (gtk_bin_get_child(GTK_BIN (entry_box))));
322 /* keep our model alive and detach from comboboxentry and completion */
323 //g_object_ref(model);
324 //gtk_combo_box_set_model(GTK_COMBO_BOX(entry_box), NULL);
325 //gtk_entry_completion_set_model (completion, NULL);
327 /* clear and populate */
330 ctx
.except_key
= except_key
;
331 gtk_list_store_clear (GTK_LIST_STORE(model
));
333 //gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(GTK_LIST_STORE(model)), GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, GTK_SORT_ASCENDING);
335 g_hash_table_foreach(hash
, (GHFunc
)ui_cat_comboboxentry_populate_ghfunc
, &ctx
);
337 /* reatach our model */
338 //gtk_combo_box_set_model(GTK_COMBO_BOX(entry_box), model);
339 //gtk_entry_completion_set_model (completion, model);
340 //g_object_unref(model);
342 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(model
), GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID
, GTK_SORT_ASCENDING
);
349 ui_cat_comboboxentry_compare_func (GtkTreeModel
*model
, GtkTreeIter
*a
, GtkTreeIter
*b
, gpointer userdata
)
352 gchar
*name1
, *name2
;
354 gtk_tree_model_get(model
, a
,
355 LST_CMBCAT_SORTNAME
, &name1
,
357 gtk_tree_model_get(model
, b
,
358 LST_CMBCAT_SORTNAME
, &name2
,
361 //DB( g_print(" compare '%s' '%s'\n", name1, name2) );
363 retval
= hb_string_utf8_compare(name1
, name2
);
373 ui_cat_comboboxentry_test (GtkCellLayout
*cell_layout
,
374 GtkCellRenderer
*cell
,
375 GtkTreeModel
*tree_model
,
385 gtk_tree_model_get(tree_model
, iter
,
386 LST_CMBCAT_NAME
, &name
,
387 LST_CMBCAT_SUBCAT
, &subcat
,
390 style
= subcat
== 0 ? PANGO_STYLE_ITALIC
: PANGO_STYLE_NORMAL
;
393 name
= _("(no category)"); //todo: not used
404 ui_cat_comboboxentry_completion_func (GtkEntryCompletion
*completion
,
410 gchar
*normalized_string
;
411 gchar
*case_normalized_string
;
413 gboolean ret
= FALSE
;
417 model
= gtk_entry_completion_get_model (completion
);
419 gtk_tree_model_get (model
, iter
,
420 LST_CMBCAT_FULLNAME
, &item
,
425 normalized_string
= g_utf8_normalize (item
, -1, G_NORMALIZE_ALL
);
427 if (normalized_string
!= NULL
)
429 case_normalized_string
= g_utf8_casefold (normalized_string
, -1);
431 //g_print("match '%s' for '%s' ?\n", key, case_normalized_string);
432 //if (!strncmp (key, case_normalized_string, strlen (key)))
433 if (g_strstr_len (case_normalized_string
, strlen (case_normalized_string
), key
))
436 // g_print(" ==> yes !\n");
440 g_free (case_normalized_string
);
442 g_free (normalized_string
);
451 * ui_cat_comboboxentry_new:
453 * Create a new category comboboxentry
455 * Return value: the new widget
459 ui_cat_comboboxentry_new(GtkWidget
*label
)
462 GtkWidget
*comboboxentry
;
463 GtkEntryCompletion
*completion
;
464 GtkCellRenderer
*renderer
;
466 DB( g_print ("ui_cat_comboboxentry_new()\n") );
468 store
= gtk_list_store_new (NUM_LST_CMBCAT
,
470 G_TYPE_STRING
, //fullname Car:Fuel
471 G_TYPE_STRING
, //parent name Car
472 G_TYPE_STRING
, //name Car or Fuel
473 G_TYPE_BOOLEAN
//subcat = 1
475 gtk_tree_sortable_set_default_sort_func(GTK_TREE_SORTABLE(store
), ui_cat_comboboxentry_compare_func
, NULL
, NULL
);
477 completion
= gtk_entry_completion_new ();
478 gtk_entry_completion_set_model (completion
, GTK_TREE_MODEL(store
));
479 //g_object_set(completion, "text-column", LST_CMBCAT_FULLNAME, NULL);
480 gtk_entry_completion_set_match_func(completion
, ui_cat_comboboxentry_completion_func
, NULL
, NULL
);
481 //gtk_entry_completion_set_minimum_key_length(completion, 2);
483 gtk_entry_completion_set_text_column(completion
, 1);
485 /*renderer = gtk_cell_renderer_text_new ();
486 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion), renderer, TRUE);
487 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (completion), renderer, "text", LST_CMBCAT_FULLNAME, NULL);
489 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (completion),
491 ui_cat_comboboxentry_test,
495 // dothe same for combobox
497 comboboxentry
= gtk_combo_box_new_with_model_and_entry(GTK_TREE_MODEL(store
));
498 gtk_combo_box_set_entry_text_column(GTK_COMBO_BOX(comboboxentry
), LST_CMBCAT_FULLNAME
);
500 gtk_cell_layout_clear(GTK_CELL_LAYOUT (comboboxentry
));
502 renderer
= gtk_cell_renderer_text_new ();
503 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comboboxentry
), renderer
, TRUE
);
504 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (comboboxentry
), renderer
, "text", LST_CMBCAT_FULLNAME
, NULL
);
506 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (comboboxentry
),
508 ui_cat_comboboxentry_test
,
511 gtk_entry_set_completion (GTK_ENTRY (gtk_bin_get_child(GTK_BIN (comboboxentry
))), completion
);
513 g_object_unref(store
);
516 gtk_label_set_mnemonic_widget (GTK_LABEL(label
), comboboxentry
);
518 gtk_widget_set_size_request(comboboxentry
, HB_MINWIDTH_LIST
, -1);
520 return comboboxentry
;
523 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
526 ui_cat_listview_fixed_toggled (GtkCellRendererToggle
*cell
,
530 GtkTreeModel
*model
= (GtkTreeModel
*)data
;
532 GtkTreePath
*path
= gtk_tree_path_new_from_string (path_str
);
535 /* get toggled iter */
536 gtk_tree_model_get_iter (model
, &iter
, path
);
537 gtk_tree_model_get (model
, &iter
, LST_DEFCAT_TOGGLE
, &fixed
, -1);
539 /* do something with the value */
543 gtk_tree_store_set (GTK_TREE_STORE (model
), &iter
, LST_DEFCAT_TOGGLE
, fixed
, -1);
546 gtk_tree_path_free (path
);
550 ui_cat_listview_compare_func (GtkTreeModel
*model
, GtkTreeIter
*a
, GtkTreeIter
*b
, gpointer userdata
)
552 gint sortcol
= GPOINTER_TO_INT(userdata
);
553 Category
*entry1
, *entry2
;
556 gtk_tree_model_get(model
, a
, LST_DEFCAT_DATAS
, &entry1
, -1);
557 gtk_tree_model_get(model
, b
, LST_DEFCAT_DATAS
, &entry2
, -1);
561 case LST_DEFCAT_SORT_NAME
:
562 retval
= (entry1
->flags
& GF_INCOME
) - (entry2
->flags
& GF_INCOME
);
565 retval
= hb_string_utf8_compare(entry1
->name
, entry2
->name
);
568 case LST_DEFCAT_SORT_USED
:
569 retval
= entry1
->usage_count
- entry2
->usage_count
;
572 g_return_val_if_reached(0);
579 ** draw some text from the stored data structure
582 ui_cat_listview_text_cell_data_function (GtkTreeViewColumn
*col
,
583 GtkCellRenderer
*renderer
,
592 gtk_tree_model_get(model
, iter
, LST_DEFCAT_DATAS
, &entry
, -1);
594 name
= _("(no category)");
598 gchar type
= (entry
->flags
& GF_INCOME
) ? '+' : '-';
601 string
= g_markup_printf_escaped ("%d > [%d] %s [%c] %d", entry
->key
, entry
->parent
, name
, type
, entry
->flags
);
604 string
= g_strdup(name
);
607 if( entry
->parent
== 0 )
608 string
= g_markup_printf_escaped("%s [%c]", name
, type
);
610 string
= g_markup_printf_escaped(" %c <i>%s</i>", type
, name
);
611 //string = g_strdup_printf(" - %s", name);
615 //g_object_set(renderer, "text", string, NULL);
616 g_object_set(renderer
, "markup", string
, NULL
);
624 ui_cat_listview_count_cell_data_function (GtkTreeViewColumn
*col
,
625 GtkCellRenderer
*renderer
,
633 gtk_tree_model_get(model
, iter
, LST_DEFCAT_DATAS
, &entry
, -1);
634 if(entry
->usage_count
> 0)
636 g_snprintf(buffer
, 256-1, "%d", entry
->usage_count
);
637 g_object_set(renderer
, "text", buffer
, NULL
);
640 g_object_set(renderer
, "text", "", NULL
);
644 /* = = = = = = = = = = = = = = = = */
648 ui_cat_listview_add(GtkTreeView
*treeview
, Category
*item
, GtkTreeIter
*parent
)
654 DB( g_print ("ui_cat_listview_add()\n") );
656 if( item
->name
!= NULL
)
658 model
= gtk_tree_view_get_model(treeview
);
660 gtk_tree_store_append (GTK_TREE_STORE(model
), &iter
, parent
);
661 gtk_tree_store_set (GTK_TREE_STORE(model
), &iter
,
662 LST_DEFCAT_TOGGLE
, FALSE
,
663 LST_DEFCAT_DATAS
, item
,
664 LST_DEFCAT_NAME
, item
->name
,
667 //select the added line
669 path
= gtk_tree_model_get_path(model
, &iter
);
670 gtk_tree_view_expand_to_path (treeview
, path
);
671 gtk_tree_path_free(path
);
672 gtk_tree_selection_select_iter (gtk_tree_view_get_selection(treeview
), &iter
);
678 ui_cat_listview_get_selected(GtkTreeView
*treeview
)
680 GtkTreeSelection
*selection
;
684 DB( g_print ("ui_cat_listview_get_selected()\n") );
686 selection
= gtk_tree_view_get_selection(treeview
);
687 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
691 gtk_tree_model_get(model
, &iter
, LST_DEFCAT_DATAS
, &item
, -1);
699 ui_cat_listview_get_selected_parent(GtkTreeView
*treeview
, GtkTreeIter
*return_iter
)
701 GtkTreeSelection
*selection
;
707 DB( g_print ("ui_cat_listview_get_selected_parent()\n") );
710 selection
= gtk_tree_view_get_selection(treeview
);
711 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
713 path
= gtk_tree_model_get_path(model
, &iter
);
715 DB( g_print ("path depth = %d\n", gtk_tree_path_get_depth(path
)) );
718 if(gtk_tree_path_get_depth(path
) > 1)
720 if( gtk_tree_path_up(path
) )
723 DB( g_print ("up ok\n") );
725 if(gtk_tree_model_get_iter(model
, &iter
, path
))
728 DB( g_print ("iter ok\n") );
731 gtk_tree_model_get(model
, &iter
, LST_DEFCAT_DATAS
, &item
, -1);
743 DB( g_print ("path <=1\n") );
745 gtk_tree_model_get(model
, &iter
, LST_DEFCAT_DATAS
, &item
, -1);
759 gboolean
ui_cat_listview_remove (GtkTreeModel
*model
, guint32 key
)
761 GtkTreeIter iter
, child
;
762 gboolean valid
, cvalid
;
765 DB( g_print("ui_cat_listview_remove() \n") );
767 valid
= gtk_tree_model_get_iter_first(GTK_TREE_MODEL(model
), &iter
);
770 gtk_tree_model_get (model
, &iter
, LST_DEFCAT_DATAS
, &item
, -1);
772 DB( g_print(" + item %p, %s\n", item
, item
->name
) );
774 if(item
->key
== key
|| item
->parent
== key
)
776 DB( g_print(" + removing cat %s\n", item
->name
) );
777 gtk_tree_store_remove(GTK_TREE_STORE(model
), &iter
);
781 cvalid
= gtk_tree_model_iter_children (GTK_TREE_MODEL(model
), &child
, &iter
);
784 gtk_tree_model_get(GTK_TREE_MODEL(model
), &child
, LST_DEFCAT_DATAS
, &item
, -1);
785 if(item
->key
== key
|| item
->parent
== key
)
787 DB( g_print(" + removing subcat %s\n", item
->name
) );
788 gtk_tree_store_remove(GTK_TREE_STORE(model
), &child
);
791 cvalid
= gtk_tree_model_iter_next(GTK_TREE_MODEL(model
), &child
);
793 valid
= gtk_tree_model_iter_next(GTK_TREE_MODEL(model
), &iter
);
802 ui_cat_listview_remove_selected(GtkTreeView
*treeview
)
804 GtkTreeSelection
*selection
;
808 DB( g_print("ui_cat_listview_remove_selected() \n") );
810 selection
= gtk_tree_view_get_selection(treeview
);
811 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
813 gtk_tree_store_remove(GTK_TREE_STORE(model
), &iter
);
819 ui_cat_listview_get_top_level (GtkTreeModel
*liststore
, guint32 key
, GtkTreeIter
*return_iter
)
825 DB( g_print("ui_cat_listview_get_top_level() \n") );
827 if( liststore
!= NULL
)
829 valid
= gtk_tree_model_get_iter_first(GTK_TREE_MODEL(liststore
), &iter
);
832 gtk_tree_model_get (liststore
, &iter
, LST_DEFCAT_DATAS
, &item
, -1);
840 valid
= gtk_tree_model_iter_next(GTK_TREE_MODEL(liststore
), &iter
);
848 static void ui_cat_listview_populate_cat_ghfunc(gpointer key
, gpointer value
, struct catPopContext
*ctx
)
850 GtkTreeIter toplevel
;
851 Category
*item
= value
;
854 item_type
= (item
->flags
& GF_INCOME
) ? CAT_TYPE_INCOME
: CAT_TYPE_EXPENSE
;
856 //DB( g_print("cat listview populate: %d %s\n", (guint32 *)key, item->name) );
857 if( (ctx
->type
== CAT_TYPE_ALL
) || ctx
->type
== item_type
|| item
->key
== 0 )
859 if( item
->parent
== 0 )
861 gtk_tree_store_append (GTK_TREE_STORE(ctx
->model
), &toplevel
, NULL
);
863 gtk_tree_store_set (GTK_TREE_STORE(ctx
->model
), &toplevel
,
864 LST_DEFCAT_TOGGLE
, FALSE
,
865 LST_DEFCAT_DATAS
, item
,
866 LST_DEFCAT_NAME
, item
->name
,
874 static void ui_cat_listview_populate_subcat_ghfunc(gpointer key
, gpointer value
, struct catPopContext
*ctx
)
876 GtkTreeIter toplevel
, child
;
877 Category
*item
= value
;
881 item_type
= (item
->flags
& GF_INCOME
) ? CAT_TYPE_INCOME
: CAT_TYPE_EXPENSE
;
883 if( (ctx
->type
== CAT_TYPE_ALL
) || ctx
->type
== item_type
)
885 if( item
->parent
!= 0 )
887 ret
= ui_cat_listview_get_top_level(ctx
->model
, item
->parent
, &toplevel
);
890 gtk_tree_store_append (GTK_TREE_STORE(ctx
->model
), &child
, &toplevel
);
892 gtk_tree_store_set (GTK_TREE_STORE(ctx
->model
), &child
,
893 LST_DEFCAT_TOGGLE
, FALSE
,
894 LST_DEFCAT_DATAS
, item
,
895 LST_DEFCAT_NAME
, item
->name
,
904 static void ui_cat_listview_sort_force(GtkTreeSortable
*sortable
, gpointer user_data
)
909 DB( g_print("ui_cat_listview_sort_force()\n") );
911 gtk_tree_sortable_get_sort_column_id(sortable
, &sort_column_id
, &order
);
912 DB( g_print(" - id %d order %d\n", sort_column_id
, order
) );
914 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(sortable
), GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID
, order
);
915 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(sortable
), sort_column_id
, order
);
919 void ui_cat_listview_populate(GtkWidget
*view
, gint type
)
922 struct catPopContext ctx
= { 0 };
924 DB( g_print("ui_cat_listview_populate() \n") );
927 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(view
));
929 gtk_tree_store_clear (GTK_TREE_STORE(model
));
931 g_object_ref(model
); /* Make sure the model stays with us after the tree view unrefs it */
932 gtk_tree_view_set_model(GTK_TREE_VIEW(view
), NULL
); /* Detach model from view */
934 /* clear and populate */
938 /* we have to do this in 2 times to ensure toplevel (cat) will be added before childs */
939 /* populate cat 1st */
940 g_hash_table_foreach(GLOBALS
->h_cat
, (GHFunc
)ui_cat_listview_populate_cat_ghfunc
, &ctx
);
941 g_hash_table_foreach(GLOBALS
->h_cat
, (GHFunc
)ui_cat_listview_populate_subcat_ghfunc
, &ctx
);
944 gtk_tree_view_set_model(GTK_TREE_VIEW(view
), model
); /* Re-attach model to view */
945 g_object_unref(model
);
947 gtk_tree_view_expand_all (GTK_TREE_VIEW(view
));
952 static gboolean
ui_cat_listview_search_equal_func (GtkTreeModel
*model
,
956 gpointer search_data
)
958 gboolean retval
= TRUE
;
959 gchar
*normalized_string
;
960 gchar
*normalized_key
;
961 gchar
*case_normalized_string
= NULL
;
962 gchar
*case_normalized_key
= NULL
;
965 //gtk_tree_model_get_value (model, iter, column, &value);
966 gtk_tree_model_get(model
, iter
, LST_DEFCAT_DATAS
, &item
, -1);
970 normalized_string
= g_utf8_normalize (item
->name
, -1, G_NORMALIZE_ALL
);
971 normalized_key
= g_utf8_normalize (key
, -1, G_NORMALIZE_ALL
);
973 if (normalized_string
&& normalized_key
)
975 case_normalized_string
= g_utf8_casefold (normalized_string
, -1);
976 case_normalized_key
= g_utf8_casefold (normalized_key
, -1);
978 if (strncmp (case_normalized_key
, case_normalized_string
, strlen (case_normalized_key
)) == 0)
982 g_free (normalized_key
);
983 g_free (normalized_string
);
984 g_free (case_normalized_key
);
985 g_free (case_normalized_string
);
992 ui_cat_listview_new(gboolean withtoggle
, gboolean withcount
)
996 GtkCellRenderer
*renderer
;
997 GtkTreeViewColumn
*column
;
999 DB( g_print("ui_cat_listview_new() \n") );
1001 store
= gtk_tree_store_new(
1008 treeview
= gtk_tree_view_new_with_model(GTK_TREE_MODEL(store
));
1009 g_object_unref(store
);
1011 gtk_tree_view_set_grid_lines (GTK_TREE_VIEW (treeview
), PREFS
->grid_lines
);
1015 if( withtoggle
== TRUE
)
1017 renderer
= gtk_cell_renderer_toggle_new ();
1018 column
= gtk_tree_view_column_new_with_attributes (_("Visible"),
1019 renderer
, "active", LST_DEFCAT_TOGGLE
, NULL
);
1020 gtk_tree_view_append_column (GTK_TREE_VIEW(treeview
), column
);
1022 g_signal_connect (G_OBJECT(renderer
), "toggled",
1023 G_CALLBACK (ui_cat_listview_fixed_toggled
), store
);
1028 renderer
= gtk_cell_renderer_text_new ();
1029 g_object_set(renderer
,
1030 "ellipsize", PANGO_ELLIPSIZE_END
,
1031 "ellipsize-set", TRUE
,
1034 column
= gtk_tree_view_column_new();
1035 gtk_tree_view_column_set_title(column
, _("Name"));
1036 gtk_tree_view_column_pack_start(column
, renderer
, TRUE
);
1037 gtk_tree_view_column_set_cell_data_func(column
, renderer
, ui_cat_listview_text_cell_data_function
, GINT_TO_POINTER(LST_DEFCAT_NAME
), NULL
);
1038 gtk_tree_view_column_set_alignment (column
, 0.5);
1039 gtk_tree_view_column_set_min_width(column
, HB_MINWIDTH_LIST
);
1040 gtk_tree_view_column_set_sort_column_id (column
, LST_DEFCAT_SORT_NAME
);
1041 gtk_tree_view_column_set_resizable(column
, TRUE
);
1042 gtk_tree_view_append_column (GTK_TREE_VIEW(treeview
), column
);
1044 if( withcount
== TRUE
)
1046 column
= gtk_tree_view_column_new();
1047 gtk_tree_view_column_set_title(column
, _("Usage"));
1048 renderer
= gtk_cell_renderer_text_new ();
1049 g_object_set(renderer
, "xalign", 0.5, NULL
);
1050 gtk_tree_view_column_pack_start(column
, renderer
, TRUE
);
1051 gtk_tree_view_column_set_cell_data_func(column
, renderer
, ui_cat_listview_count_cell_data_function
, GINT_TO_POINTER(LST_DEFCAT_DATAS
), NULL
);
1052 gtk_tree_view_column_set_alignment (column
, 0.5);
1053 gtk_tree_view_column_set_sort_column_id (column
, LST_DEFCAT_SORT_USED
);
1054 gtk_tree_view_append_column (GTK_TREE_VIEW(treeview
), column
);
1058 gtk_tree_view_set_search_equal_func(GTK_TREE_VIEW(treeview
), ui_cat_listview_search_equal_func
, NULL
, NULL
);
1060 // treeview attribute
1061 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(treeview
), withcount
);
1063 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store
), LST_DEFCAT_SORT_NAME
, ui_cat_listview_compare_func
, GINT_TO_POINTER(LST_DEFCAT_SORT_NAME
), NULL
);
1064 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store
), LST_DEFCAT_SORT_USED
, ui_cat_listview_compare_func
, GINT_TO_POINTER(LST_DEFCAT_SORT_USED
), NULL
);
1066 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store
), LST_DEFCAT_SORT_NAME
, GTK_SORT_ASCENDING
);
1072 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
1075 * ui_cat_manage_filter_text_handler
1077 * filter to entry to avoid seizure of ':' char
1080 static void ui_cat_manage_filter_text_handler (GtkEntry
*entry
,
1086 GtkEditable
*editable
= GTK_EDITABLE(entry
);
1088 gchar
*result
= g_new0 (gchar
, length
+1);
1090 for (i
=0; i
< length
; i
++)
1094 result
[count
++] = text
[i
];
1099 g_signal_handlers_block_by_func (G_OBJECT (editable
),
1100 G_CALLBACK (ui_cat_manage_filter_text_handler
),
1102 gtk_editable_insert_text (editable
, result
, count
, position
);
1103 g_signal_handlers_unblock_by_func (G_OBJECT (editable
),
1104 G_CALLBACK (ui_cat_manage_filter_text_handler
),
1107 g_signal_stop_emission_by_name (G_OBJECT (editable
), "insert_text");
1114 ui_cat_manage_dialog_delete_unused( GtkWidget
*widget
, gpointer user_data
)
1116 struct ui_cat_manage_dialog_data
*data
= user_data
;
1119 //data = g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(GTK_WIDGET(widget), GTK_TYPE_WINDOW)), "inst_data");
1121 DB( g_print("(ui_cat_manage_dialog) delete unused - data %p\n", data
) );
1123 result
= ui_dialog_msg_confirm_alert(
1124 GTK_WINDOW(data
->window
),
1125 _("Delete unused categories"),
1126 _("Are you sure you want to permanently\ndelete unused categories?"),
1130 if( result
== GTK_RESPONSE_OK
)
1132 GtkTreeModel
*model
;
1134 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(data
->LV_cat
));
1135 gtk_tree_store_clear (GTK_TREE_STORE(model
));
1137 category_delete_unused();
1139 ui_cat_manage_populate_listview (data
);
1146 * ui_cat_manage_dialog_load_csv:
1150 ui_cat_manage_dialog_load_csv( GtkWidget
*widget
, gpointer user_data
)
1152 struct ui_cat_manage_dialog_data
*data
= user_data
;
1153 gchar
*filename
= NULL
;
1156 //data = g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(GTK_WIDGET(widget), GTK_TYPE_WINDOW)), "inst_data");
1158 DB( g_print("(ui_cat_manage_dialog) load csv - data %p\n", data
) );
1160 if( ui_file_chooser_csv(GTK_WINDOW(data
->window
), GTK_FILE_CHOOSER_ACTION_OPEN
, &filename
, NULL
) == TRUE
)
1162 DB( g_print(" + filename is %s\n", filename
) );
1164 if(!category_load_csv(filename
, &error
))
1166 ui_dialog_msg_infoerror(GTK_WINDOW(data
->window
), GTK_MESSAGE_ERROR
,
1167 _("File format error"),
1168 _("The CSV file must contains the exact numbers of column,\nseparated by a semi-colon, please see the help for more details.")
1173 ui_cat_manage_populate_listview(data
);
1179 * ui_cat_manage_dialog_save_csv:
1183 ui_cat_manage_dialog_save_csv( GtkWidget
*widget
, gpointer user_data
)
1185 struct ui_cat_manage_dialog_data
*data
= user_data
;
1186 gchar
*filename
= NULL
;
1189 DB( g_print("(defcategory) save csv\n") );
1191 //data = g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW)), "inst_data");
1193 if( ui_file_chooser_csv(GTK_WINDOW(data
->window
), GTK_FILE_CHOOSER_ACTION_SAVE
, &filename
, NULL
) == TRUE
)
1195 DB( g_print(" + filename is %s\n", filename
) );
1197 category_save_csv(filename
, &error
);
1203 * ui_cat_manage_dialog_add:
1205 * add an empty new category/subcategory
1209 ui_cat_manage_dialog_add(GtkWidget
*widget
, gpointer user_data
)
1211 struct ui_cat_manage_dialog_data
*data
;
1212 gboolean subcat
= GPOINTER_TO_INT(user_data
);
1214 //GtkTreeModel *model;
1215 GtkTreeIter parent_iter
;
1216 GtkWidget
*tmpwidget
;
1217 Category
*item
, *paritem
;
1220 data
= g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget
, GTK_TYPE_WINDOW
)), "inst_data");
1221 DB( g_print("\n(defcategory) add (data=%p) is subcat=%d\n", data
, subcat
) );
1223 tmpwidget
= (subcat
== FALSE
? data
->ST_name1
: data
->ST_name2
);
1224 name
= gtk_entry_get_text(GTK_ENTRY(tmpwidget
));
1225 //model = gtk_tree_view_get_model(GTK_TREE_VIEW(data->LV_cat));
1227 /* ignore if item is empty */
1232 item
= da_cat_malloc();
1233 item
->name
= g_strdup(name
);
1235 g_strstrip(item
->name
);
1237 /* if cat use new id */
1240 type
= radio_get_active(GTK_CONTAINER(data
->RA_type
));
1242 item
->flags
|= GF_INCOME
;
1244 if( da_cat_append(item
) )
1246 DB( g_print(" => add cat: %p %d, %s type=%d\n", item
, subcat
, item
->name
, type
) );
1247 ui_cat_listview_add(GTK_TREE_VIEW(data
->LV_cat
), item
, NULL
);
1250 /* if subcat use parent id & gf_income */
1253 paritem
= ui_cat_listview_get_selected_parent(GTK_TREE_VIEW(data
->LV_cat
), &parent_iter
);
1256 DB( g_print(" => selitem parent: %d, %s\n", paritem
->key
, paritem
->name
) );
1258 item
->parent
= paritem
->key
;
1259 item
->flags
|= (paritem
->flags
& GF_INCOME
);
1260 item
->flags
|= GF_SUB
;
1262 if(da_cat_append(item
))
1264 DB( g_print(" => add subcat: %p %d, %s\n", item
, subcat
, item
->name
) );
1265 ui_cat_listview_add(GTK_TREE_VIEW(data
->LV_cat
), item
, &parent_iter
);
1270 gtk_entry_set_text(GTK_ENTRY(tmpwidget
),"");
1275 static void ui_cat_manage_dialog_edit_entry_cb(GtkEditable
*editable
, gpointer user_data
)
1277 GtkDialog
*window
= user_data
;
1278 const gchar
*buffer
;
1280 buffer
= gtk_entry_get_text(GTK_ENTRY(editable
));
1281 gtk_dialog_set_response_sensitive(GTK_DIALOG(window
), GTK_RESPONSE_ACCEPT
, strlen(buffer
) > 0 ? TRUE
: FALSE
);
1285 static void ui_cat_manage_dialog_edit(GtkWidget
*widget
, gpointer user_data
)
1287 struct ui_cat_manage_dialog_data
*data
;
1288 GtkWidget
*dialog
, *content
, *mainvbox
, *w_name
, *w_type
= NULL
;
1289 GtkTreeSelection
*selection
;
1290 GtkTreeModel
*model
;
1293 data
= g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget
, GTK_TYPE_WINDOW
)), "inst_data");
1294 DB( g_print("\n(defcategory) edit\n") );
1296 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(data
->LV_cat
));
1297 //if true there is a selected node
1298 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
1302 gtk_tree_model_get(model
, &iter
, LST_DEFCAT_DATAS
, &item
, -1);
1304 dialog
= gtk_dialog_new_with_buttons (_("Edit..."),
1305 GTK_WINDOW (data
->window
),
1308 GTK_RESPONSE_REJECT
,
1310 GTK_RESPONSE_ACCEPT
,
1313 content
= gtk_dialog_get_content_area(GTK_DIALOG (dialog
));
1314 mainvbox
= gtk_box_new (GTK_ORIENTATION_VERTICAL
, 0);
1315 gtk_box_pack_start (GTK_BOX (content
), mainvbox
, TRUE
, TRUE
, 0);
1316 gtk_container_set_border_width (GTK_CONTAINER (mainvbox
), SPACING_MEDIUM
);
1318 w_name
= gtk_entry_new();
1319 gtk_box_pack_start (GTK_BOX (mainvbox
), w_name
, TRUE
, TRUE
, 0);
1321 gtk_entry_set_text(GTK_ENTRY(w_name
), item
->name
);
1322 gtk_widget_grab_focus (w_name
);
1324 gtk_entry_set_activates_default (GTK_ENTRY(w_name
), TRUE
);
1326 if(!(item
->flags
& GF_SUB
))
1328 w_type
= gtk_check_button_new_with_mnemonic(_("_Income"));
1329 gtk_box_pack_start (GTK_BOX (mainvbox
), w_type
, TRUE
, TRUE
, 0);
1330 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w_type
), item
->flags
& GF_INCOME
? TRUE
: FALSE
);
1333 g_signal_connect (G_OBJECT (w_name
), "changed", G_CALLBACK (ui_cat_manage_dialog_edit_entry_cb
), dialog
);
1336 gtk_widget_show_all(mainvbox
);
1338 gtk_dialog_set_default_response(GTK_DIALOG( dialog
), GTK_RESPONSE_ACCEPT
);
1341 gint result
= gtk_dialog_run (GTK_DIALOG (dialog
));
1343 if(result
== GTK_RESPONSE_ACCEPT
)
1347 // 1: manage renaming
1348 name
= gtk_entry_get_text(GTK_ENTRY(w_name
));
1349 // ignore if item is empty
1352 if( category_rename(item
, name
) )
1354 //to redraw the active entry
1355 gtk_tree_view_columns_autosize (GTK_TREE_VIEW(data
->LV_cat
));
1361 gchar
*fromname
, *toname
= NULL
;
1363 fromname
= da_cat_get_fullname(item
);
1365 if( item
->parent
== 0)
1366 toname
= g_strdup(name
);
1369 parent
= da_cat_get(item
->parent
);
1372 toname
= g_strdup_printf("%s:%s", parent
->name
, name
);
1377 ui_dialog_msg_infoerror(GTK_WINDOW(dialog
), GTK_MESSAGE_ERROR
,
1379 _("Cannot rename this Category,\n"
1380 "from '%s' to '%s',\n"
1381 "this name already exists."),
1392 // 2: manage flag change
1393 if(!(item
->flags
& GF_SUB
))
1397 isIncome
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w_type
));
1398 data
->change
+= category_change_type(item
, isIncome
);
1401 ui_cat_listview_sort_force(GTK_TREE_SORTABLE(model
), NULL
);
1404 // cleanup and destroy
1405 gtk_widget_destroy (dialog
);
1411 static void ui_cat_manage_dialog_merge_entry_cb(GtkComboBox
*widget
, gpointer user_data
)
1413 GtkDialog
*window
= user_data
;
1416 buffer
= (gchar
*)gtk_entry_get_text(GTK_ENTRY (gtk_bin_get_child(GTK_BIN (widget
))));
1417 gtk_dialog_set_response_sensitive(GTK_DIALOG(window
), GTK_RESPONSE_OK
, strlen(buffer
) > 0 ? TRUE
: FALSE
);
1421 static void ui_cat_manage_dialog_merge(GtkWidget
*widget
, gpointer user_data
)
1423 struct ui_cat_manage_dialog_data
*data
;
1424 GtkWidget
*dialog
, *content
, *mainvbox
;
1425 GtkWidget
*getwidget
, *togglebutton
;
1426 GtkTreeSelection
*selection
;
1427 GtkTreeModel
*model
;
1430 data
= g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget
, GTK_TYPE_WINDOW
)), "inst_data");
1431 DB( g_print("(defcategory) merge\n") );
1433 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(data
->LV_cat
));
1434 //if true there is a selected node
1435 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
1441 gtk_tree_model_get(model
, &iter
, LST_DEFCAT_DATAS
, &srccat
, -1);
1443 title
= g_strdup_printf (
1444 _("Merge category '%s'"), srccat
->name
);
1446 dialog
= gtk_message_dialog_new (GTK_WINDOW (data
->window
),
1447 GTK_DIALOG_MODAL
| GTK_DIALOG_DESTROY_WITH_PARENT
,
1448 GTK_MESSAGE_WARNING
,
1454 gtk_dialog_add_buttons (GTK_DIALOG(dialog
),
1455 _("_Cancel"), GTK_RESPONSE_CANCEL
,
1456 _("Merge"), GTK_RESPONSE_OK
,
1459 gtk_dialog_set_default_response(GTK_DIALOG (dialog
), GTK_RESPONSE_CANCEL
);
1461 content
= gtk_message_dialog_get_message_area(GTK_MESSAGE_DIALOG (dialog
));
1462 mainvbox
= gtk_box_new (GTK_ORIENTATION_VERTICAL
, SPACING_SMALL
);
1463 gtk_box_pack_start (GTK_BOX (content
), mainvbox
, TRUE
, TRUE
, 0);
1465 secondtext
= _("Transactions assigned to this category,\n"
1466 "will be moved to the category selected below.");
1468 g_object_set(GTK_MESSAGE_DIALOG (dialog
), "secondary-text", secondtext
, NULL
);
1471 getwidget
= ui_cat_comboboxentry_new(NULL
);
1472 gtk_box_pack_start (GTK_BOX (mainvbox
), getwidget
, FALSE
, FALSE
, 0);
1474 secondtext
= g_strdup_printf (
1475 _("_Delete the category '%s'"), srccat
->name
);
1476 togglebutton
= gtk_check_button_new_with_mnemonic(secondtext
);
1477 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(togglebutton
), TRUE
);
1479 gtk_box_pack_start (GTK_BOX (mainvbox
), togglebutton
, FALSE
, FALSE
, 0);
1482 //gtk_combo_box_set_active(GTK_COMBO_BOX(getwidget), oldpos);
1483 g_signal_connect (G_OBJECT (getwidget
), "changed", G_CALLBACK (ui_cat_manage_dialog_merge_entry_cb
), dialog
);
1484 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog
), GTK_RESPONSE_OK
, FALSE
);
1486 ui_cat_comboboxentry_populate_except(GTK_COMBO_BOX(getwidget
), GLOBALS
->h_cat
, srccat
->key
);
1487 gtk_widget_grab_focus (getwidget
);
1489 gtk_widget_show_all(mainvbox
);
1492 gint result
= gtk_dialog_run (GTK_DIALOG (dialog
));
1494 if(result
== GTK_RESPONSE_OK
)
1496 GtkTreeModel
*model
;
1497 Category
*newcat
, *parent
;
1501 model
= gtk_tree_view_get_model(GTK_TREE_VIEW(data
->LV_cat
));
1502 gtk_tree_store_clear (GTK_TREE_STORE(model
));
1504 dstcatkey
= ui_cat_comboboxentry_get_key_add_new(GTK_COMBO_BOX(getwidget
));
1506 DB( g_print(" -> move cat to %d\n", dstcatkey
) );
1508 category_move(srccat
->key
, dstcatkey
);
1510 newcat
= da_cat_get (dstcatkey
);
1512 //keep the income type with us
1513 parent
= da_cat_get(srccat
->parent
);
1514 if(parent
!= NULL
&& (parent
->flags
& GF_INCOME
))
1515 newcat
->flags
|= GF_INCOME
;
1517 //add the new category into listview
1519 ui_cat_listview_add(GTK_TREE_VIEW(data
->LV_cat
), newcat
, NULL
);
1521 // delete the old category
1522 if( gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(togglebutton
)) )
1524 DB( g_print(" -> delete %d '%s'\n", srccat
->key
, srccat
->name
) );
1526 da_cat_remove(srccat
->key
);
1527 ui_cat_listview_remove_selected(GTK_TREE_VIEW(data
->LV_cat
));
1533 ui_cat_manage_populate_listview(data
);
1537 // cleanup and destroy
1538 gtk_widget_destroy (dialog
);
1545 ** delete the selected payee to our treeview and temp GList
1547 static void ui_cat_manage_dialog_delete(GtkWidget
*widget
, gpointer user_data
)
1549 struct ui_cat_manage_dialog_data
*data
;
1553 data
= g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget
, GTK_TYPE_WINDOW
)), "inst_data");
1554 DB( g_print("\n(defcategory) delete (data=%x)\n", (guint
)data
) );
1556 item
= ui_cat_listview_get_selected(GTK_TREE_VIEW(data
->LV_cat
));
1559 gchar
*title
= NULL
;
1560 gchar
*secondtext
= NULL
;
1562 title
= g_strdup_printf (
1563 _("Are you sure you want to permanently delete '%s'?"), item
->name
);
1565 if( item
->usage_count
> 0 )
1567 secondtext
= _("This category is used.\n"
1568 "Any transaction using that category will be set to (no category)");
1571 result
= ui_dialog_msg_confirm_alert(
1572 GTK_WINDOW(data
->window
),
1580 if( result
== GTK_RESPONSE_OK
)
1582 category_move(item
->key
, 0);
1583 ui_cat_listview_remove(gtk_tree_view_get_model(GTK_TREE_VIEW(data
->LV_cat
)), item
->key
);
1584 da_cat_remove(item
->key
);
1593 static void ui_cat_manage_dialog_expand_all(GtkWidget
*widget
, gpointer user_data
)
1595 struct ui_cat_manage_dialog_data
*data
;
1597 data
= g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget
, GTK_TYPE_WINDOW
)), "inst_data");
1598 DB( g_print("\n(defcategory) expand all (data=%x)\n", (guint
)data
) );
1600 gtk_tree_view_expand_all(GTK_TREE_VIEW(data
->LV_cat
));
1605 static void ui_cat_manage_dialog_collapse_all(GtkWidget
*widget
, gpointer user_data
)
1607 struct ui_cat_manage_dialog_data
*data
;
1609 data
= g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget
, GTK_TYPE_WINDOW
)), "inst_data");
1610 DB( g_print("\n(defcategory) collapse all (data=%x)\n", (guint
)data
) );
1612 gtk_tree_view_collapse_all(GTK_TREE_VIEW(data
->LV_cat
));
1617 static void ui_cat_manage_dialog_update(GtkWidget
*treeview
, gpointer user_data
)
1619 struct ui_cat_manage_dialog_data
*data
;
1621 gboolean selected
, sensitive
;
1622 GtkTreeSelection
*selection
;
1623 GtkTreeModel
*model
;
1627 gboolean haschild
= FALSE
;
1629 DB( g_print("ui_cat_manage_dialog_update()\n") );
1631 data
= g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(GTK_WIDGET(treeview
), GTK_TYPE_WINDOW
)), "inst_data");
1632 //window = gtk_widget_get_ancestor(GTK_WIDGET(treeview), GTK_TYPE_WINDOW);
1633 //DB( g_print("(defpayee) widget=%08lx, window=%08lx, inst_data=%08lx\n", treeview, window, data) );
1635 //if true there is a selected node
1636 selected
= gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(data
->LV_cat
)), &model
, &iter
);
1638 DB( g_print(" selected = %d\n", selected
) );
1643 gtk_tree_model_get_iter_first(model
, &iter
);
1644 if(gtk_tree_selection_iter_is_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(data
->LV_cat
)), &iter
))
1646 DB( g_print(" 0 active = %d\n", 1) );
1651 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview
));
1653 //count = gtk_tree_selection_count_selected_rows(selection);
1654 //DB( g_print(" => select count=%d\n", count) );
1657 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
1659 gchar
*tree_path_str
;
1662 gtk_tree_model_get(GTK_TREE_MODEL(model
), &iter
,
1663 LST_DEFCAT_DATAS
, &item
,
1666 haschild
= gtk_tree_model_iter_has_child(GTK_TREE_MODEL(model
), &iter
);
1667 DB( g_print(" => has child=%d\n", haschild
) );
1670 path
= gtk_tree_model_get_path(gtk_tree_view_get_model(GTK_TREE_VIEW(treeview
)), &iter
);
1671 tree_path_str
= gtk_tree_path_to_string(path
);
1672 DB( g_print(" => select is=%s, depth=%d (id=%d, %s) flags=%d\n",
1674 gtk_tree_path_get_depth(path
),
1679 g_free(tree_path_str
);
1681 //get parent if subcategory selectd
1682 DB( g_print(" => get parent for title\n") );
1683 if(gtk_tree_path_get_depth(path
) != 1)
1684 gtk_tree_path_up(path
);
1686 if(gtk_tree_model_get_iter(model
, &iter
, path
))
1690 gtk_tree_model_get(GTK_TREE_MODEL(model
), &iter
,
1691 LST_DEFCAT_DATAS
, &tmpitem
,
1694 if(tmpitem
->key
> 0)
1695 category
= tmpitem
->name
;
1697 DB( g_print(" => parent is %s\n", category
) );
1701 gtk_tree_path_free(path
);
1705 gtk_label_set_text(GTK_LABEL(data
->LA_category
), category
);
1707 sensitive
= (selected
== TRUE
) ? TRUE
: FALSE
;
1708 gtk_widget_set_sensitive(data
->ST_name2
, sensitive
);
1709 gtk_widget_set_sensitive(data
->BT_edit
, sensitive
);
1710 gtk_widget_set_sensitive(data
->BT_merge
, sensitive
);
1712 //avoid removing top categories
1713 sensitive
= (haschild
== TRUE
) ? FALSE
: sensitive
;
1715 gtk_widget_set_sensitive(data
->BT_delete
, sensitive
);
1722 static void ui_cat_manage_dialog_selection(GtkTreeSelection
*treeselection
, gpointer user_data
)
1724 ui_cat_manage_dialog_update(GTK_WIDGET(gtk_tree_selection_get_tree_view (treeselection
)), NULL
);
1727 static void ui_cat_manage_dialog_onRowActivated (GtkTreeView
*treeview
,
1729 GtkTreeViewColumn
*col
,
1732 GtkTreeModel
*model
;
1735 DB( g_print("ui_cat_manage_dialog_onRowActivated()\n") );
1738 model
= gtk_tree_view_get_model(treeview
);
1739 gtk_tree_model_get_iter_first(model
, &iter
);
1740 if(gtk_tree_selection_iter_is_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview
)), &iter
) == FALSE
)
1742 ui_cat_manage_dialog_edit(GTK_WIDGET(treeview
), NULL
);
1747 static gboolean
ui_cat_manage_dialog_cleanup(struct ui_cat_manage_dialog_data
*data
, gint result
)
1749 gboolean doupdate
= FALSE
;
1751 DB( g_print("(defcategory) cleanup\n") );
1753 if(result
== GTK_RESPONSE_ACCEPT
)
1756 //do_application_specific_something ();
1757 DB( g_print(" accept\n") );
1760 GLOBALS
->changes_count
+= data
->change
;
1763 DB( g_print(" free tmp_list\n") );
1765 //da_category_destroy(data->tmp_list);
1771 static void ui_cat_manage_populate_listview(struct ui_cat_manage_dialog_data
*data
)
1775 type
= radio_get_active(GTK_CONTAINER(data
->RA_type
)) == 1 ? CAT_TYPE_INCOME
: CAT_TYPE_EXPENSE
;
1776 ui_cat_listview_populate(data
->LV_cat
, type
);
1777 gtk_tree_view_expand_all (GTK_TREE_VIEW(data
->LV_cat
));
1784 static void ui_cat_manage_dialog_setup(struct ui_cat_manage_dialog_data
*data
)
1787 DB( g_print("(defcategory) setup\n") );
1790 data
->tmp_list
= NULL
; //data->tmp_list = hb-glist_clone_list(GLOBALS->cat_list, sizeof(struct _Group));
1794 //da_cat_debug_list();
1796 ui_cat_manage_populate_listview(data
);
1801 static void ui_cat_manage_type_changed_cb (GtkToggleButton
*button
, gpointer user_data
)
1803 ui_cat_manage_populate_listview(user_data
);
1804 //g_print(" toggle type=%d\n", gtk_toggle_button_get_active(button));
1808 GtkWidget
*ui_cat_manage_dialog (void)
1810 struct ui_cat_manage_dialog_data data
;
1811 GtkWidget
*window
, *content
, *mainvbox
, *bbox
, *table
, *hbox
, *vbox
, *label
, *scrollwin
, *treeview
;
1812 GtkWidget
*menu
, *menuitem
, *widget
, *image
, *tbar
;
1813 GtkToolItem
*toolitem
;
1816 window
= gtk_dialog_new_with_buttons (_("Manage Categories"),
1817 GTK_WINDOW(GLOBALS
->mainwindow
),
1820 GTK_RESPONSE_ACCEPT
,
1823 data
.window
= window
;
1826 gtk_window_set_icon_name(GTK_WINDOW (window
), ICONNAME_HB_CATEGORY
);
1828 //set a nice dialog size
1829 gtk_window_get_size(GTK_WINDOW(GLOBALS
->mainwindow
), &w
, &h
);
1830 gtk_window_set_default_size (GTK_WINDOW(window
), -1, h
/PHI
);
1833 //store our window private data
1834 g_object_set_data(G_OBJECT(window
), "inst_data", (gpointer
)&data
);
1835 DB( g_print("(defcategory) window=%x, inst_data=%x\n", (guint
)window
, (guint
)&data
) );
1837 g_signal_connect (window
, "destroy",
1838 G_CALLBACK (gtk_widget_destroyed
), &window
);
1841 content
= gtk_dialog_get_content_area(GTK_DIALOG (window
));
1842 mainvbox
= gtk_box_new (GTK_ORIENTATION_VERTICAL
, SPACING_SMALL
);
1843 gtk_box_pack_start (GTK_BOX (content
), mainvbox
, TRUE
, TRUE
, 0);
1844 gtk_container_set_border_width (GTK_CONTAINER(mainvbox
), SPACING_MEDIUM
);
1847 table
= gtk_grid_new ();
1848 gtk_grid_set_row_spacing (GTK_GRID (table
), SPACING_SMALL
);
1849 gtk_grid_set_column_spacing (GTK_GRID (table
), SPACING_MEDIUM
);
1850 gtk_box_pack_start (GTK_BOX (mainvbox
), table
, TRUE
, TRUE
, 0);
1853 bbox
= make_radio(CYA_CAT_TYPE
, TRUE
, GTK_ORIENTATION_HORIZONTAL
);
1854 data
.RA_type
= bbox
;
1855 gtk_widget_set_halign (bbox
, GTK_ALIGN_CENTER
);
1856 gtk_grid_attach (GTK_GRID (table
), bbox
, 0, row
, 2, 1);
1858 widget
= radio_get_nth_widget(GTK_CONTAINER(bbox
), 1);
1860 g_signal_connect (widget
, "toggled", G_CALLBACK (ui_cat_manage_type_changed_cb
), &data
);
1862 menu
= gtk_menu_new ();
1863 gtk_widget_set_halign (menu
, GTK_ALIGN_END
);
1865 menuitem
= gtk_menu_item_new_with_mnemonic (_("_Import CSV"));
1866 gtk_menu_shell_append (GTK_MENU_SHELL (menu
), menuitem
);
1867 g_signal_connect (G_OBJECT (menuitem
), "activate", G_CALLBACK (ui_cat_manage_dialog_load_csv
), &data
);
1869 menuitem
= gtk_menu_item_new_with_mnemonic (_("E_xport CSV"));
1870 gtk_menu_shell_append (GTK_MENU_SHELL (menu
), menuitem
);
1871 g_signal_connect (G_OBJECT (menuitem
), "activate", G_CALLBACK (ui_cat_manage_dialog_save_csv
), &data
);
1873 menuitem
= gtk_separator_menu_item_new();
1874 gtk_menu_shell_append (GTK_MENU_SHELL (menu
), menuitem
);
1876 menuitem
= gtk_menu_item_new_with_mnemonic (_("_Delete unused"));
1877 gtk_menu_shell_append (GTK_MENU_SHELL (menu
), menuitem
);
1878 g_signal_connect (G_OBJECT (menuitem
), "activate", G_CALLBACK (ui_cat_manage_dialog_delete_unused
), &data
);
1880 gtk_widget_show_all (menu
);
1882 widget
= gtk_menu_button_new();
1883 image
= gtk_image_new_from_icon_name (ICONNAME_HB_BUTTON_MENU
, GTK_ICON_SIZE_MENU
);
1886 //gtk_image_get_icon_name(image, &thename, NULL);
1887 //g_print("the name is %s\n", thename);
1889 g_object_set (widget
, "image", image
, "popup", GTK_MENU(menu
), NULL
);
1890 gtk_widget_set_halign (widget
, GTK_ALIGN_END
);
1891 gtk_grid_attach (GTK_GRID (table
), widget
, 1, row
, 1, 1);
1894 widget
= gtk_entry_new ();
1895 data
.ST_name1
= widget
;
1896 gtk_entry_set_placeholder_text(GTK_ENTRY(data
.ST_name1
), _("new category") );
1897 gtk_widget_set_hexpand (widget
, TRUE
);
1898 gtk_grid_attach (GTK_GRID (table
), widget
, 0, row
, 2, 1);
1900 // subcategory + add button
1902 hbox
= gtk_box_new (GTK_ORIENTATION_HORIZONTAL
, SPACING_SMALL
);
1903 gtk_grid_attach (GTK_GRID (table
), hbox
, 0, row
, 2, 1);
1904 data
.LA_category
= gtk_label_new(NULL
);
1905 gtk_box_pack_start (GTK_BOX (hbox
), data
.LA_category
, FALSE
, FALSE
, 0);
1906 label
= gtk_label_new(":");
1907 gtk_box_pack_start (GTK_BOX (hbox
), label
, FALSE
, FALSE
, 0);
1908 data
.ST_name2
= gtk_entry_new ();
1909 gtk_entry_set_placeholder_text(GTK_ENTRY(data
.ST_name2
), _("new subcategory") );
1910 gtk_box_pack_start (GTK_BOX (hbox
), data
.ST_name2
, TRUE
, TRUE
, 0);
1914 vbox
= gtk_box_new (GTK_ORIENTATION_VERTICAL
, 0);
1915 gtk_grid_attach (GTK_GRID (table
), vbox
, 0, row
, 2, 1);
1917 scrollwin
= gtk_scrolled_window_new(NULL
,NULL
);
1918 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrollwin
), GTK_SHADOW_ETCHED_IN
);
1919 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollwin
), GTK_POLICY_NEVER
, GTK_POLICY_AUTOMATIC
);
1920 gtk_scrolled_window_set_min_content_height(GTK_SCROLLED_WINDOW(scrollwin
), HB_MINHEIGHT_LIST
);
1921 treeview
= ui_cat_listview_new(FALSE
, TRUE
);
1922 data
.LV_cat
= treeview
;
1923 gtk_container_add(GTK_CONTAINER(scrollwin
), treeview
);
1924 gtk_widget_set_hexpand (scrollwin
, TRUE
);
1925 gtk_widget_set_vexpand (scrollwin
, TRUE
);
1926 gtk_box_pack_start (GTK_BOX(vbox
), scrollwin
, TRUE
, TRUE
, 0);
1929 tbar
= gtk_toolbar_new();
1930 gtk_toolbar_set_icon_size (GTK_TOOLBAR(tbar
), GTK_ICON_SIZE_MENU
);
1931 gtk_toolbar_set_style(GTK_TOOLBAR(tbar
), GTK_TOOLBAR_ICONS
);
1932 gtk_style_context_add_class (gtk_widget_get_style_context (tbar
), GTK_STYLE_CLASS_INLINE_TOOLBAR
);
1933 gtk_box_pack_start (GTK_BOX (vbox
), tbar
, FALSE
, FALSE
, 0);
1935 /*widget = gtk_tool_item_new ();
1936 label = gtk_label_new("test");
1937 gtk_container_add(GTK_CONTAINER(widget), label);
1938 gtk_toolbar_insert(GTK_TOOLBAR(tbar), GTK_TOOL_ITEM(widget), -1);*/
1940 //hbox = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
1942 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1943 toolitem = gtk_tool_item_new();
1944 gtk_container_add (GTK_CONTAINER(toolitem), hbox);
1945 gtk_toolbar_insert(GTK_TOOLBAR(tbar), GTK_TOOL_ITEM(toolitem), -1);
1947 //widget = make_image_button("text-editor-symbolic", _("Edit"));
1948 widget = gtk_button_new_with_mnemonic(_("_Edit"));
1949 data.BT_edit = widget;
1950 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1952 //widget = make_image_button("merge-symbolic", _("Merge"));
1953 widget = gtk_button_new_with_mnemonic(_("_Merge"));
1954 data.BT_merge = widget;
1955 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1957 //widget = make_image_button(ICONNAME_SYM_EDIT_DELETE, _("Delete"));
1958 widget = gtk_button_new_with_mnemonic(_("_Delete"));
1959 data.BT_delete = widget;
1960 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1963 toolitem
= gtk_separator_tool_item_new ();
1964 gtk_tool_item_set_expand (toolitem
, TRUE
);
1965 gtk_separator_tool_item_set_draw(GTK_SEPARATOR_TOOL_ITEM(toolitem
), FALSE
);
1966 gtk_toolbar_insert(GTK_TOOLBAR(tbar
), GTK_TOOL_ITEM(toolitem
), -1);
1968 hbox
= gtk_box_new (GTK_ORIENTATION_HORIZONTAL
, 0);
1969 toolitem
= gtk_tool_item_new();
1970 gtk_container_add (GTK_CONTAINER(toolitem
), hbox
);
1971 gtk_toolbar_insert(GTK_TOOLBAR(tbar
), GTK_TOOL_ITEM(toolitem
), -1);
1973 widget
= make_image_button(ICONNAME_HB_BUTTON_EXPAND
, _("Expand all"));
1974 data
.BT_expand
= widget
;
1975 gtk_box_pack_start (GTK_BOX (hbox
), widget
, FALSE
, FALSE
, 0);
1977 widget
= make_image_button(ICONNAME_HB_BUTTON_COLLAPSE
, _("Collapse all"));
1978 data
.BT_collapse
= widget
;
1979 gtk_box_pack_start (GTK_BOX (hbox
), widget
, FALSE
, FALSE
, 0);
1983 bbox
= gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL
);
1984 gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox
), GTK_BUTTONBOX_START
);
1985 gtk_box_set_spacing (GTK_BOX (bbox
), SPACING_SMALL
);
1986 gtk_grid_attach (GTK_GRID (table
), bbox
, 0, row
, 2, 1);
1988 data
.BT_edit
= gtk_button_new_with_mnemonic(_("_Edit"));
1989 gtk_container_add (GTK_CONTAINER (bbox
), data
.BT_edit
);
1991 data
.BT_merge
= gtk_button_new_with_mnemonic(_("_Merge"));
1992 gtk_container_add (GTK_CONTAINER (bbox
), data
.BT_merge
);
1994 data
.BT_delete
= gtk_button_new_with_mnemonic(_("_Delete"));
1995 gtk_container_add (GTK_CONTAINER (bbox
), data
.BT_delete
);
1999 widget = gtk_check_button_new_with_mnemonic(_("I_ncome"));
2000 data.CM_type = widget;
2001 gtk_grid_attach (GTK_GRID (table), widget, 0, row, 3, 1);*/
2004 //connect all our signals
2005 g_signal_connect (G_OBJECT (data
.ST_name1
), "activate", G_CALLBACK (ui_cat_manage_dialog_add
), GINT_TO_POINTER(FALSE
));
2006 g_signal_connect (G_OBJECT (data
.ST_name2
), "activate", G_CALLBACK (ui_cat_manage_dialog_add
), GINT_TO_POINTER(TRUE
));
2008 g_signal_connect(G_OBJECT(data
.ST_name1
), "insert-text", G_CALLBACK(ui_cat_manage_filter_text_handler
), NULL
);
2009 g_signal_connect(G_OBJECT(data
.ST_name2
), "insert-text", G_CALLBACK(ui_cat_manage_filter_text_handler
), NULL
);
2012 g_signal_connect (gtk_tree_view_get_selection(GTK_TREE_VIEW(data
.LV_cat
)), "changed", G_CALLBACK (ui_cat_manage_dialog_selection
), NULL
);
2013 g_signal_connect (GTK_TREE_VIEW(data
.LV_cat
), "row-activated", G_CALLBACK (ui_cat_manage_dialog_onRowActivated
), NULL
);
2015 g_signal_connect (G_OBJECT (data
.BT_edit
), "clicked", G_CALLBACK (ui_cat_manage_dialog_edit
), NULL
);
2016 g_signal_connect (G_OBJECT (data
.BT_merge
), "clicked", G_CALLBACK (ui_cat_manage_dialog_merge
), NULL
);
2017 g_signal_connect (G_OBJECT (data
.BT_delete
), "clicked", G_CALLBACK (ui_cat_manage_dialog_delete
), NULL
);
2019 g_signal_connect (G_OBJECT (data
.BT_expand
), "clicked", G_CALLBACK (ui_cat_manage_dialog_expand_all
), NULL
);
2020 g_signal_connect (G_OBJECT (data
.BT_collapse
), "clicked", G_CALLBACK (ui_cat_manage_dialog_collapse_all
), NULL
);
2022 //setup, init and show window
2023 category_fill_usage();
2024 ui_cat_manage_dialog_setup(&data
);
2025 ui_cat_manage_dialog_update(data
.LV_cat
, NULL
);
2027 gtk_widget_show_all (window
);
2030 gint result
= gtk_dialog_run (GTK_DIALOG (window
));
2034 case GTK_RESPONSE_ACCEPT
:
2035 //do_application_specific_something ();
2038 //do_nothing_since_dialog_was_cancelled ();
2042 // cleanup and destroy
2043 ui_cat_manage_dialog_cleanup(&data
, result
);
2044 gtk_widget_destroy (window
);
2046 GLOBALS
->changes_count
+= data
.change
;