-
Notifications
You must be signed in to change notification settings - Fork 761
/
gnc-tree-view.c
2310 lines (1985 loc) · 77.7 KB
/
gnc-tree-view.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* gnc-tree-view.c -- new GtkTreeView with extra features used by
* all the tree views in gnucash
*
* Copyright (C) 2003,2005 David Hampton <hampton@employees.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, contact:
*
* Free Software Foundation Voice: +1-617-542-5942
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
* Boston, MA 02110-1301, USA gnu@gnu.org
*/
/** @addtogroup GUI
@{ */
/** @addtogroup GncTreeView
* @{ */
/** @file gnc-tree-view.c
@brief Base GncTreeView implementation for gnucash trees.
@author David Hampton <hampton@employees.org>
*/
#include <config.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <gdk/gdkkeysyms.h>
#include <string.h>
#include "gnc-tree-view.h"
#include "gnc-engine.h"
#include "gnc-gnome-utils.h"
#include "gnc-gobject-utils.h"
#include "gnc-cell-renderer-date.h"
#include "gnc-cell-renderer-text-view.h"
#include "gnc-state.h"
#include "gnc-prefs.h"
#include "dialog-utils.h"
/* The actual state key for a particular column visibility. This is
* attached to the menu items that are in the column selection menu.
* Makes it very easy to update saved state when a menu item is toggled. */
#define STATE_KEY "state-key"
/* State keys within this particular saved state section. */
#define STATE_KEY_SORT_COLUMN "sort_column"
#define STATE_KEY_SORT_ORDER "sort_order"
#define STATE_KEY_COLUMN_ORDER "column_order"
/* Partial state keys within this particular saved state section. These
are appended to the various column names to create the actual
keys. */
#define STATE_KEY_SUFF_VISIBLE "visible"
#define STATE_KEY_SUFF_WIDTH "width"
enum
{
PROP_0,
PROP_STATE_SECTION,
PROP_SHOW_COLUMN_MENU,
};
/** Static Globals *******************************************************/
/* This static indicates the debugging module that this .o belongs to. */
static QofLogModule log_module = GNC_MOD_GUI;
/**** Declarations ******************************************************/
static void gnc_tree_view_class_init (GncTreeViewClass *klass);
static void gnc_tree_view_init (GncTreeView *view, void *data);
static void gnc_tree_view_finalize (GObject *object);
static void gnc_tree_view_destroy (GtkWidget *widget);
static void gnc_tree_view_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void gnc_tree_view_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
static gboolean gnc_tree_view_drop_ok_cb (GtkTreeView *view,
GtkTreeViewColumn *column,
GtkTreeViewColumn *prev_column,
GtkTreeViewColumn *next_column,
gpointer data);
static void gnc_tree_view_build_column_menu (GncTreeView *view);
static void gnc_tree_view_select_column_cb (GtkTreeViewColumn *column,
GncTreeView *view);
static gchar *gnc_tree_view_get_sort_order (GncTreeView *view);
static gchar *gnc_tree_view_get_sort_column (GncTreeView *view);
static gchar **gnc_tree_view_get_column_order (GncTreeView *view,
gsize *length);
/** Private Data Structure ***********************************************/
typedef struct GncTreeViewPrivate
{
/* Column selection menu related values */
GtkTreeViewColumn *column_menu_column;
GtkWidget *column_menu;
gboolean show_column_menu;
GtkWidget *column_menu_icon_box;
/* Sort callback model */
GtkTreeModel *sort_model;
/* Editing callback functions */
GFunc editing_started_cb;
GFunc editing_finished_cb;
gpointer editing_cb_data;
/* State related values */
gchar *state_section;
gboolean seen_state_visibility;
gulong columns_changed_cb_id;
gulong sort_column_changed_cb_id;
gulong size_allocate_cb_id;
} GncTreeViewPrivate;
GNC_DEFINE_TYPE_WITH_CODE(GncTreeView, gnc_tree_view, GTK_TYPE_TREE_VIEW,
G_ADD_PRIVATE(GncTreeView))
#define GNC_TREE_VIEW_GET_PRIVATE(o) \
((GncTreeViewPrivate*)g_type_instance_get_private((GTypeInstance*)o, GNC_TYPE_TREE_VIEW))
/************************************************************/
/* g_object required functions */
/************************************************************/
/** @name Gnc Tree View Object Implementation
@{ */
static GObjectClass *parent_class = NULL;
/** Initialize the class for the new base gnucash tree view. This
* will set up any function pointers that override functions in the
* parent class, and also installs the properties that are unique to
* this class.
*
* @param klass The new class structure created by the object system.
*
* @internal
*/
static void
gnc_tree_view_class_init (GncTreeViewClass *klass)
{
GObjectClass *gobject_class;
GtkWidgetClass *gtkwidget_class;
parent_class = g_type_class_peek_parent (klass);
gobject_class = G_OBJECT_CLASS(klass);
gtkwidget_class = GTK_WIDGET_CLASS(klass);
gobject_class->set_property = gnc_tree_view_set_property;
gobject_class->get_property = gnc_tree_view_get_property;
g_object_class_install_property (gobject_class,
PROP_STATE_SECTION,
g_param_spec_string ("state-section",
"State Section",
"The section name in the saved state to use for (re)storing the treeview's visual state (visible columns, sort order,...",
NULL,
G_PARAM_READWRITE));
g_object_class_install_property (gobject_class,
PROP_SHOW_COLUMN_MENU,
g_param_spec_boolean ("show-column-menu",
"Show Column Menu",
"Show the column menu so user can change what columns are visible.",
FALSE,
G_PARAM_READWRITE));
/* GObject signals */
gobject_class->finalize = gnc_tree_view_finalize;
/* GtkWidget signals */
gtkwidget_class->destroy = gnc_tree_view_destroy;
}
static void
gnc_tree_view_update_grid_lines (gpointer prefs, gchar* pref, gpointer user_data)
{
GncTreeView *view = user_data;
gtk_tree_view_set_grid_lines (GTK_TREE_VIEW(view), gnc_tree_view_get_grid_lines_pref ());
}
static gboolean
gnc_tree_view_select_column_icon_cb (GtkWidget *widget, GdkEventButton *event, gpointer user_data)
{
GncTreeView *view = user_data;
GncTreeViewPrivate *priv;
GtkStyleContext *stylectxt = gtk_widget_get_style_context (widget);
GtkBorder padding;
// if the event button is not the right one, leave.
if (event->button != 1)
return FALSE;
priv = GNC_TREE_VIEW_GET_PRIVATE(view);
gtk_style_context_get_padding (stylectxt, GTK_STATE_FLAG_NORMAL, &padding);
if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
{
if (event->x < (gtk_widget_get_allocated_width (priv->column_menu_icon_box) + padding.left))
gnc_tree_view_select_column_cb (priv->column_menu_column, view);
}
else
{
if (event->x > (gtk_widget_get_allocated_width (widget) -
(gtk_widget_get_allocated_width (priv->column_menu_icon_box) + padding.right)))
gnc_tree_view_select_column_cb (priv->column_menu_column, view);
}
return FALSE;
}
/** Initialize a new instance of a base gnucash tree view. This
* function allocates and initializes the object private storage
* space. It also adds the new object to a list (for memory tracking
* purposes), and sets up a callback for the column drag function.
*
* @param view The new object instance created by the object system.
*
* @internal
*/
static void
gnc_tree_view_init (GncTreeView *view, void *data)
{
GncTreeViewPrivate *priv;
GtkTreeViewColumn *column;
GtkWidget *sep, *icon;
GncTreeViewClass *klass = (GncTreeViewClass*)data;
gnc_gobject_tracking_remember (G_OBJECT(view),
G_OBJECT_CLASS(klass));
priv = GNC_TREE_VIEW_GET_PRIVATE(view);
priv->column_menu = NULL;
priv->show_column_menu = FALSE;
priv->sort_model = NULL;
priv->state_section = NULL;
priv->seen_state_visibility = FALSE;
priv->columns_changed_cb_id = 0;
priv->sort_column_changed_cb_id = 0;
priv->size_allocate_cb_id = 0;
// Set the name for this widget so it can be easily manipulated with css
gtk_widget_set_name (GTK_WIDGET(view), "gnc-id-tree-view");
/* Handle column drag and drop */
gtk_tree_view_set_column_drag_function (GTK_TREE_VIEW(view),
gnc_tree_view_drop_ok_cb, NULL, NULL);
// Set grid lines option to preference
gtk_tree_view_set_grid_lines (GTK_TREE_VIEW(view), gnc_tree_view_get_grid_lines_pref ());
gnc_prefs_register_cb (GNC_PREFS_GROUP_GENERAL, GNC_PREF_GRID_LINES_HORIZONTAL,
gnc_tree_view_update_grid_lines, view);
gnc_prefs_register_cb (GNC_PREFS_GROUP_GENERAL, GNC_PREF_GRID_LINES_VERTICAL,
gnc_tree_view_update_grid_lines, view);
/* Create the last column which contains the column selection
* widget. gnc_tree_view_add_text_column will do most of the
* work. */
icon = gtk_image_new_from_icon_name ("pan-down-symbolic", GTK_ICON_SIZE_SMALL_TOOLBAR);
priv->column_menu_icon_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
gtk_box_set_homogeneous (GTK_BOX(priv->column_menu_icon_box), FALSE);
gtk_widget_set_margin_start (GTK_WIDGET(icon), 5);
gtk_box_pack_end (GTK_BOX(priv->column_menu_icon_box), icon, FALSE, FALSE, 0);
sep = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
gtk_box_pack_end (GTK_BOX(priv->column_menu_icon_box), sep, FALSE, FALSE, 0);
gtk_widget_show_all (priv->column_menu_icon_box);
column = gnc_tree_view_add_text_column (view, NULL, NULL, NULL, NULL,
-1, -1, NULL);
g_object_set (G_OBJECT(column),
"clickable", TRUE,
"widget", priv->column_menu_icon_box,
"alignment", 1.0,
"expand", TRUE,
(gchar *)NULL);
priv->column_menu_column = column;
// get the actual column button by looking at the parents of the column_menu_icon
{
GtkWidget *mybox = gtk_widget_get_parent (icon);
GtkWidget *walign = gtk_widget_get_parent (mybox);
GtkWidget *box = gtk_widget_get_parent (walign);
GtkWidget *button = gtk_widget_get_parent (box);
if (!GTK_IS_BUTTON(button)) // just in case this order changes.
{
// this will fire for the whole column header
g_signal_connect (G_OBJECT(column), "clicked",
G_CALLBACK(gnc_tree_view_select_column_cb),
view);
}
else
{
/* this part will restrict the mouse click to just where the
icon is, tried using an eventbox but it would only work
some of the time */
gtk_widget_set_events (button, GDK_BUTTON_PRESS_MASK);
g_signal_connect (G_OBJECT(button), "button_press_event",
G_CALLBACK(gnc_tree_view_select_column_icon_cb),
view);
}
}
gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED);
}
/** Finalize the GncTreeView object. This function is called from the
* G_Object level to complete the destruction of the object. It
* should release any memory not previously released by the destroy
* function (i.e. the private data structure), then chain up to the
* parent's destroy function.
*
* @param object The object being destroyed.
*
* @internal
*/
static void
gnc_tree_view_finalize (GObject *object)
{
ENTER("view %p", object);
g_return_if_fail (object != NULL);
g_return_if_fail (GNC_IS_TREE_VIEW(object));
gnc_gobject_tracking_forget (object);
if (G_OBJECT_CLASS(parent_class)->finalize)
G_OBJECT_CLASS(parent_class)->finalize (object);
LEAVE(" ");
}
/** Destroy the GncTreeView widget. This function is called (possibly
* multiple times) from the Gtk_Object level to destroy the widget.
* It should release any memory owned by the widget that isn't
* fundamental to the implementation. In this function any active
* callbacks are disconnected, all memory other than the private data
* structure are freed.
*
* @param widget The widget being destroyed.
*
* @internal
*/
static void
gnc_tree_view_destroy (GtkWidget *widget)
{
GncTreeView *view;
GncTreeViewPrivate *priv;
ENTER("view %p", widget);
g_return_if_fail (widget != NULL);
g_return_if_fail (GNC_IS_TREE_VIEW(widget));
view = GNC_TREE_VIEW(widget);
gnc_prefs_remove_cb_by_func (GNC_PREFS_GROUP_GENERAL, GNC_PREF_GRID_LINES_HORIZONTAL,
gnc_tree_view_update_grid_lines, view);
gnc_prefs_remove_cb_by_func (GNC_PREFS_GROUP_GENERAL, GNC_PREF_GRID_LINES_VERTICAL,
gnc_tree_view_update_grid_lines, view);
priv = GNC_TREE_VIEW_GET_PRIVATE(view);
if (priv->state_section)
{
gnc_tree_view_save_state (view);
}
g_free (priv->state_section);
priv->state_section = NULL;
if (priv->column_menu)
{
DEBUG("removing column selection menu");
g_object_unref (priv->column_menu);
priv->column_menu = NULL;
}
if (GTK_WIDGET_CLASS(parent_class)->destroy)
GTK_WIDGET_CLASS(parent_class)->destroy (widget);
LEAVE(" ");
}
/** @} */
/************************************************************/
/* g_object other functions */
/************************************************************/
/** @name Gnc Tree View Object Implementation
@{ */
/** Retrieve a property specific to this GncTreeView object. This is
* nothing more than a dispatch function for routines that can be
* called directly. It has the nice feature of allowing a single
* function call to retrieve multiple properties.
*
* @internal
*/
static void
gnc_tree_view_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GncTreeView *view = GNC_TREE_VIEW(object);
GncTreeViewPrivate *priv;
priv = GNC_TREE_VIEW_GET_PRIVATE(view);
switch (prop_id)
{
case PROP_STATE_SECTION:
g_value_set_string (value, priv->state_section);
break;
case PROP_SHOW_COLUMN_MENU:
g_value_set_boolean (value, priv->show_column_menu);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
/** Set a property specific to this GncTreeView object. This is
* nothing more than a dispatch function for routines that can be
* called directly. It has the nice feature of allowing a new view
* to be created with a varargs list specifying the properties,
* instead of having to explicitly call each property function.
*
* @internal
*/
static void
gnc_tree_view_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GncTreeView *view = GNC_TREE_VIEW(object);
switch (prop_id)
{
case PROP_STATE_SECTION:
gnc_tree_view_set_state_section (view, g_value_get_string (value));
break;
case PROP_SHOW_COLUMN_MENU:
gnc_tree_view_set_show_column_menu (view, g_value_get_boolean (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
/** @} */
/************************************************************/
/* Auxiliary Functions */
/************************************************************/
/** @name Gnc Tree View Auxiliary Functions
@{ */
/** Find a tree column given a column id number from the underlying
* data model. This function should only be called by code that has
* visibility into the data model. The column id numbers shouldn't
* be used for any other purpose.
*
* This function simply runs the list of all (visible and invisible)
* columns looking for a match. Column id numbers were attached to
* each column at the time the column was created.
*
* @param view The visible tree widget.
*
* @param wanted The column id number to find.
*
* @internal
*/
static GtkTreeViewColumn *
view_column_find_by_model_id (GncTreeView *view,
const gint wanted)
{
GtkTreeViewColumn *column, *found = NULL;
GList *column_list, *tmp;
gint id;
// ENTER("view %p, name %s", view, name);
column_list = gtk_tree_view_get_columns (GTK_TREE_VIEW(view));
for (tmp = column_list; tmp; tmp = g_list_next (tmp))
{
column = tmp->data;
id = GPOINTER_TO_INT(g_object_get_data (G_OBJECT(column), MODEL_COLUMN));
if (id != wanted)
continue;
found = column;
break;
}
g_list_free (column_list);
// LEAVE("column %p", found);
return found;
}
/** Find a tree column given the "pref name" used with saved state. This
* function simply runs the list of all (visible and invisible)
* columns looking for a match. Column names were attached to each
* column at the time the column was created.
*
* @param view The visible tree widget.
*
* @param wanted The "pref name" to find.
*
*/
GtkTreeViewColumn *
gnc_tree_view_find_column_by_name (GncTreeView *view,
const gchar *wanted)
{
GtkTreeViewColumn *column, *found = NULL;
GList *column_list, *tmp;
const gchar *name;
// ENTER("view %p, wanted %s", view, wanted);
column_list = gtk_tree_view_get_columns(GTK_TREE_VIEW(view));
for (tmp = column_list; tmp; tmp = g_list_next (tmp))
{
column = tmp->data;
name = g_object_get_data (G_OBJECT(column), PREF_NAME);
if (!name || (strcmp(name, wanted) != 0))
continue;
found = column;
break;
}
g_list_free (column_list);
// LEAVE("column %p", found);
return found;
}
/** @} */
/************************************************************/
/* Tree Callbacks */
/************************************************************/
/** @name Gnc Tree View Callback Functions
@{ */
/** This function is called to determine whether it is acceptable to
* drop a dragged tree column at a given location. This function
* will be called multiple times by the GtkTreeView code while the
* user is dragging a column. Each call is a check to see if the
* proposed location is an acceptable place to drop the column. In
* the case of a GncTreeView, the only drop locations that are
* prohibited are on the right side of the visible tree.
*
* @param view The visible tree widget.
*
* @param column The column being dragged.
*
* @param prev_column The column before (left) of the proposed drop
* location.
*
* @param next_column The column after (right) of the proposed drop
* location.
*
* @param data Unused.
*
* @return TRUE if this drop location is acceptable. FALSE if not
* acceptable.
*
* @internal
*/
static gboolean
gnc_tree_view_drop_ok_cb (GtkTreeView *view,
GtkTreeViewColumn *column,
GtkTreeViewColumn *prev_column,
GtkTreeViewColumn *next_column,
gpointer data)
{
const gchar *pref_name;
/* Should we allow a drop at the left side of the tree view before
* the widget to open a new display level? I can think of cases
* where the user might want to do this with a checkbox column. */
if (prev_column == NULL)
return TRUE;
/* Do not allow a drop at the right side of the tree view after the
* column selection widget. */
if (next_column == NULL)
return FALSE;
/* Columns without pref names are considered fixed at the right hand
* side of the view. At the time of this writing, the only two are
* the column where the "column selection widget" is stored, and the
* "padding" column to the left of that where extra view space ends
* up. */
pref_name = g_object_get_data (G_OBJECT(prev_column), PREF_NAME);
if (!pref_name)
return FALSE;
/* Everything else is allowed. */
return TRUE;
}
/** @} */
/************************************************************/
/* State Setup / Callbacks */
/************************************************************/
/** @name Gnc Tree View state Callback / Related Functions
@{ */
/** Determine the visibility of a column. This function first looks
* for columns specially marked to be always visible, or columns
* without a preference name. These are always shown. Next, this
* function checks if visibility is stored in saved state. If so
* use the value found there. If none of the above the default
* visibility for the column is used.
*
* @param view A GncTreeView.
*
* @param column The GtkTreeViewColumn in question. Either this
* value or the pref_name parameter must be non-NULL.
*
* @param pref_name The name of the column in question. Either this
* value or the column parameter must be non-NULL.
*
* @return TRUE if the column should be visible. FALSE otherwise.
*
* @internal
*/
static gboolean
gnc_tree_view_column_visible (GncTreeView *view,
GtkTreeViewColumn *column,
const gchar *pref_name)
{
GncTreeViewPrivate *priv;
gboolean visible;
const gchar *col_name = pref_name;
ENTER("column %p, name %s", column, pref_name ? pref_name : "(null)");
priv = GNC_TREE_VIEW_GET_PRIVATE(view);
if (column)
{
if (g_object_get_data (G_OBJECT(column), ALWAYS_VISIBLE))
{
LEAVE("1, first column");
return TRUE;
}
col_name = g_object_get_data (G_OBJECT(column), PREF_NAME);
DEBUG("col_name is %s", col_name ? col_name : "(null)");
}
if (!col_name)
{
LEAVE("1, no pref name");
return TRUE;
}
/* Using saved state ? */
if (priv->state_section)
{
GKeyFile *state_file = gnc_state_get_current ();
gchar *key = g_strdup_printf ("%s_%s", col_name, STATE_KEY_SUFF_VISIBLE);
if (g_key_file_has_key (state_file, priv->state_section, key, NULL))
{
visible = g_key_file_get_boolean (state_file, priv->state_section, key, NULL);
g_free (key);
LEAVE("%d, state defined visibility", visible);
return visible;
}
}
/* Check the default columns list */
visible = column ?
(g_object_get_data (G_OBJECT(column), DEFAULT_VISIBLE) != NULL) : FALSE;
LEAVE("defaults says %d", visible);
return visible;
}
/** This function updates the visibility of a single column. It
* checks if the column should be visible, and if so tells the view
* to show the column.
*
* @param column The column whose visibility should be updated.
*
* @param view The GncTreeView containing the column.
*
* @internal
*/
static void
gnc_tree_view_update_visibility (GtkTreeViewColumn *column,
GncTreeView *view)
{
gboolean visible;
g_return_if_fail (GTK_IS_TREE_VIEW_COLUMN(column));
g_return_if_fail (GNC_IS_TREE_VIEW(view));
ENTER(" ");
visible = gnc_tree_view_column_visible (view, column, NULL);
gtk_tree_view_column_set_visible (column, visible);
LEAVE("made %s", visible ? "visible" : "invisible");
}
/** Get the sort order for the sort column, converted into a string.
*
* @param view The tree view.
*
* @return a string representing the sort order. NULL if not sorted, else
* either "ascending" or "descending".
* Should be freed with g_free if no longer needed.
*
* @internal
*/
static gchar *
gnc_tree_view_get_sort_order (GncTreeView *view)
{
GtkTreeModel *s_model;
GtkSortType order;
gint current;
gchar *order_str = NULL;
s_model = gtk_tree_view_get_model (GTK_TREE_VIEW(view));
if (!s_model)
return NULL; /* no model, so sort order doesn't make sense */
if (!gtk_tree_sortable_get_sort_column_id (GTK_TREE_SORTABLE(s_model),
¤t, &order))
return NULL; /* Model is not sorted, return */
gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(s_model),
current, order);
order_str = g_strdup (order == GTK_SORT_ASCENDING ? "ascending" : "descending");
DEBUG("current sort_order is %s", order_str);
return order_str;
}
/** Get the current sort column.
*
* @param view The tree view.
*
* @return a string with the name of the sort column, or NULL if not sorted.
* Should be freed with g_free if no longer needed.
*
* @internal
*/
static gchar *
gnc_tree_view_get_sort_column (GncTreeView *view)
{
GtkTreeModel *s_model;
GtkTreeViewColumn *column;
GtkSortType order;
gint current;
const gchar *name;
s_model = gtk_tree_view_get_model (GTK_TREE_VIEW(view));
if (!s_model)
return NULL; /* no model -> no sort column */
if (!gtk_tree_sortable_get_sort_column_id (GTK_TREE_SORTABLE(s_model),
¤t, &order))
return NULL; /* model not sorted */
column = view_column_find_by_model_id (view, current);
if (!column)
return NULL; /* column not visible, can't be used for sorting */
name = g_object_get_data (G_OBJECT(column), PREF_NAME);
DEBUG("current sort column is %s", name ? name : "(NULL)");
return g_strdup (name);
}
/** Get the current column order.
*
* @param view The tree view.
*
* @return an array of strings with the names of the columns in the order
* they are currently ordered.
* Should be freed with g_free if no longer needed.
*
* @internal
*/
static gchar **
gnc_tree_view_get_column_order (GncTreeView *view,
gsize *length)
{
const GList *tmp;
GList *columns;
gulong num_cols = 0;
gchar *col_names = NULL;
gchar **col_str_list;
/* First, convert from names to pointers */
ENTER(" ");
columns = gtk_tree_view_get_columns (GTK_TREE_VIEW(view));
for (tmp = columns; tmp; tmp = g_list_next(tmp))
{
GtkTreeViewColumn *column = tmp->data;
const gchar *name = g_object_get_data (G_OBJECT(column), PREF_NAME);
if (!col_names)
col_names = g_strdup (name);
else
{
gchar *col_names_prev = col_names;
col_names = g_strjoin (";", col_names_prev, name, NULL);
g_free (col_names_prev);
}
num_cols++;
}
//DEBUG ("got %lu columns: %s", num_cols, col_names);
col_str_list = g_strsplit (col_names, ";", 0);
/* Clean up */
g_list_free (columns);
g_free (col_names);
LEAVE("column order get");
*length = num_cols;
return col_str_list;
}
/** Set the sort order for the sort column (if there is one)
* of this tree view.
*
* @param view The tree view.
*
* @param name The sort order enum (in string form). Either
* "ascending" or "descending".
*
* @internal
*/
static void
gnc_tree_view_set_sort_order (GncTreeView *view,
const gchar *name)
{
GtkTreeModel *s_model;
GtkSortType order = GTK_SORT_ASCENDING;
gint current;
s_model = gtk_tree_view_get_model (GTK_TREE_VIEW(view));
if (!s_model)
return;
if (g_strcmp0 (name, "descending") == 0)
order = GTK_SORT_DESCENDING;
if (!gtk_tree_sortable_get_sort_column_id (GTK_TREE_SORTABLE(s_model),
¤t, NULL))
current = GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID;
gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(s_model),
current, order);
DEBUG("sort_order set to %s", order == GTK_SORT_ASCENDING ? "ascending" : "descending");
}
/** Set the sort column for this tree view.
*
* @param view The tree view.
*
* @param name The name of the column that should be made the sort column.
*
* @internal
*/
static void
gnc_tree_view_set_sort_column (GncTreeView *view,
const gchar *name)
{
GtkTreeModel *s_model;
GtkTreeViewColumn *column;
GtkSortType order;
gint model_column, current;
s_model = gtk_tree_view_get_model (GTK_TREE_VIEW(view));
if (!s_model)
return;
column = gnc_tree_view_find_column_by_name (view, name);
if (!column)
{
gtk_tree_sortable_set_sort_column_id (
GTK_TREE_SORTABLE(s_model), GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID,
GTK_SORT_ASCENDING);
return;
}
model_column =
GPOINTER_TO_INT(g_object_get_data (G_OBJECT(column), MODEL_COLUMN));
if (model_column == GNC_TREE_VIEW_COLUMN_DATA_NONE)
return;
if (!gtk_tree_sortable_get_sort_column_id (GTK_TREE_SORTABLE(s_model),
¤t, &order))
order = GTK_SORT_ASCENDING;
gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(s_model),
model_column, order);
DEBUG("sort column set to %s", name);
}
/** Set the order of the columns (visible and invisible) for this
* tree view.
*
* @param view The tree view.
*
* @param column_names An array of strings. These strings are the
* names of the columns in the order they should appear.
*
* @param length The number of strings in the array.
*
* @internal
*/
static void
gnc_tree_view_set_column_order (GncTreeView *view,
gchar **column_names,
gsize length)
{
GtkTreeViewColumn *column, *prev;
const GSList *tmp;
GSList *columns;
gsize idx;
/* First, convert from names to pointers */
ENTER(" ");
columns = NULL;
for (idx = 0; idx < length; idx++)
{
const gchar *name = column_names [idx];
column = gnc_tree_view_find_column_by_name (view, name);
if (!column)
continue;
columns = g_slist_append (columns, column);
}
/* Then reorder the columns */
for (prev = NULL, tmp = columns; tmp; tmp = g_slist_next (tmp))
{
column = tmp->data;
gtk_tree_view_move_column_after (GTK_TREE_VIEW(view), column, prev);
prev = column;
}
/* Clean up */
g_slist_free (columns);
LEAVE("column order set");
}
/** Completely wipe the treeview's state information (column visibility, width,
* sorting order,..). This function may be called at any time;
* either when the user wants to disconnect or
* when the view object is being destroyed.
*
* @param view The tree view.
*/
void gnc_tree_view_remove_state_information (GncTreeView *view)
{
GncTreeViewPrivate *priv;
GKeyFile *state_file = gnc_state_get_current ();
ENTER(" ");
priv = GNC_TREE_VIEW_GET_PRIVATE(view);
if (!priv->state_section)
{
LEAVE("no state section");
return;
}
g_key_file_remove_group (state_file, priv->state_section, NULL);
g_free (priv->state_section);
priv->state_section = NULL;
LEAVE(" ");
}
/** Set up or remove an association between a saved state section
* and the display of a view. It will first remove any existing association,
* and then install the new one. If the new section has state
* information, update the view with this information.