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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* e-tree-selection-model.c: a Selection Model
*
* Author:
* Christopher James Lahey <clahey@ximian.com>
*
* (C) 2000, 2001 Ximian, Inc.
*/
#include <config.h>
#include <gtk/gtksignal.h>
#include "e-tree-selection-model.h"
#include <gal/util/e-bit-array.h>
#include <gal/util/e-sorter.h>
#include <gal/util/e-util.h>
#include <gdk/gdkkeysyms.h>
#include <gal/e-table/e-tree-sorted.h>
#include <gal/e-table/e-tree-table-adapter.h>
#define ETSM_CLASS(e) ((ETreeSelectionModelClass *)((GtkObject *)e)->klass)
#define PARENT_TYPE e_selection_model_get_type ()
static ESelectionModelClass *parent_class;
enum {
ARG_0,
ARG_CURSOR_ROW,
ARG_CURSOR_COL,
ARG_MODEL,
ARG_ETTA,
ARG_ETS,
};
typedef struct ETreeSelectionModelNode {
guint selected : 1;
guint all_children_selected : 1;
guint any_children_selected : 1;
EBitArray *all_children_selected_array;
EBitArray *any_children_selected_array;
struct ETreeSelectionModelNode **children;
int num_children;
} ETreeSelectionModelNode;
struct ETreeSelectionModelPriv {
ETreeTableAdapter *etta;
ETreeSorted *ets;
ETreeModel *model;
ETreeSelectionModelNode *root;
ETreePath cursor_path;
gint cursor_col;
gint selection_start_row;
guint model_changed_id;
guint model_row_inserted_id, model_row_deleted_id;
guint frozen : 1;
guint selection_model_changed : 1;
guint group_info_changed : 1;
int tree_model_pre_change_id;
int tree_model_node_changed_id;
int tree_model_node_data_changed_id;
int tree_model_node_col_changed_id;
int tree_model_node_inserted_id;
int tree_model_node_removed_id;
};
/* ETreeSelectionModelNode helpers */
static ETreeSelectionModelNode *
e_tree_selection_model_node_new (void)
{
ETreeSelectionModelNode *node = g_new(ETreeSelectionModelNode, 1);
node->selected = 0;
node->all_children_selected = 0;
node->any_children_selected = 0;
node->all_children_selected_array = NULL;
node->any_children_selected_array = NULL;
node->children = NULL;
node->num_children = -1;
return node;
}
static void
e_tree_selection_model_node_fill_children(ETreeSelectionModel *etsm, ETreePath path, ETreeSelectionModelNode *selection_node)
{
int i;
selection_node->num_children = e_tree_sorted_node_num_children(etsm->priv->ets, path);
selection_node->children = g_new(ETreeSelectionModelNode *, selection_node->num_children);
for (i = 0; i < selection_node->num_children; i++) {
selection_node->children[i] = NULL;
}
}
static void
e_tree_selection_model_node_free(ETreeSelectionModelNode *node)
{
int i;
if (node->all_children_selected_array)
gtk_object_unref(GTK_OBJECT(node->all_children_selected_array));
if (node->any_children_selected_array)
gtk_object_unref(GTK_OBJECT(node->any_children_selected_array));
for (i = 0; i < node->num_children; i++)
if (node->children[i])
e_tree_selection_model_node_free(node->children[i]);
g_free(node->children);
g_free(node);
}
/* Other helper functions */
static ETreePath
etsm_node_at_row(ETreeSelectionModel *etsm, int row)
{
ETreePath path;
path = e_tree_table_adapter_node_at_row(etsm->priv->etta, row);
if (path)
path = e_tree_sorted_view_to_model_path(etsm->priv->ets, path);
return path;
}
static int
etsm_row_of_node(ETreeSelectionModel *etsm, ETreePath path)
{
path = e_tree_sorted_model_to_view_path(etsm->priv->ets, path);
if (path)
return e_tree_table_adapter_row_of_node(etsm->priv->etta, path);
else
return 0;
}
static int
etsm_cursor_row_real (ETreeSelectionModel *etsm)
{
if (etsm->priv->cursor_path)
return etsm_row_of_node(etsm, etsm->priv->cursor_path);
else
return -1;
}
static void
etsm_real_clear (ETreeSelectionModel *etsm)
{
if (etsm->priv->root) {
e_tree_selection_model_node_free(etsm->priv->root);
etsm->priv->root = NULL;
}
}
static ETreeSelectionModelNode *
etsm_find_node_unless_equals (ETreeSelectionModel *etsm,
ETreePath path,
gboolean grow)
{
ETreeSelectionModelNode *selection_node;
ETreeSorted *ets = etsm->priv->ets;
ETreePath parent;
parent = e_tree_model_node_get_parent(E_TREE_MODEL(ets), path);
if (parent) {
selection_node = etsm_find_node_unless_equals(etsm, parent, grow);
if (selection_node) {
int position = e_tree_sorted_orig_position(ets, path);
if (selection_node->all_children_selected && grow)
return NULL;
if (!(selection_node->any_children_selected || grow))
return NULL;
if (selection_node->all_children_selected_array && e_bit_array_value_at(selection_node->all_children_selected_array, position) && grow)
return NULL;
if (selection_node->any_children_selected_array && ! (e_bit_array_value_at(selection_node->any_children_selected_array, position) || grow))
return NULL;
if (selection_node->children == NULL) {
e_tree_selection_model_node_fill_children(etsm, parent, selection_node);
}
if (!selection_node->children[position])
selection_node->children[position] = e_tree_selection_model_node_new();
return selection_node->children[position];
} else
return NULL;
} else {
if (!etsm->priv->root)
etsm->priv->root = e_tree_selection_model_node_new();
return etsm->priv->root;
}
}
#if 0
static ETreeSelectionModelNode *
find_or_create_node (ETreeSelectionModel *etsm,
ETreePath path)
{
ETreeSelectionModelNode *selection_node;
ETreeSelectionModelNode **place = NULL;
ETreeSorted *ets = etsm->priv->ets;
ETreePath parent;
parent = e_tree_model_node_get_parent(E_TREE_MODEL(ets), path);
if (parent) {
selection_node = find_or_create_node(etsm, parent);
if (selection_node) {
int position = e_tree_sorted_orig_position(ets, path);
if (!selection_node->children) {
e_tree_selection_model_node_fill_children(etsm, parent, selection_node);
}
if (!selection_node->children[position])
slection_node->children[position] = e_tree_selection_model_node_new();
return selection_node->children[position];
} else
return NULL;
} else {
if (!etsm->priv->root)
etsm->priv->root = e_tree_selection_model_node_new();
return etsm->priv->root;
}
}
#endif
static void
update_parents (ETreeSelectionModel *etsm, ETreePath path)
{
int i;
int depth;
ETreeSorted *ets = etsm->priv->ets;
int *orig_position_sequence;
ETreeSelectionModelNode **node_sequence;
ETreePath parents;
if (!etsm->priv->root)
return;
depth = e_tree_model_node_depth (E_TREE_MODEL(ets), path);
orig_position_sequence = g_new(int, depth + 1);
node_sequence = g_new(ETreeSelectionModelNode *, depth + 1);
parents = path;
for (i = depth; i > 0; i--) {
if (!parents) {
g_free(orig_position_sequence);
g_free(node_sequence);
return;
}
orig_position_sequence[i] = e_tree_sorted_orig_position(etsm->priv->ets, parents);
parents = e_tree_model_node_get_parent(E_TREE_MODEL(etsm->priv->ets), parents);
}
node_sequence[0] = etsm->priv->root;
for (i = 0; i < depth; i++) {
node_sequence[i + 1] = NULL;
if (node_sequence[i]->children)
node_sequence[i + 1] = node_sequence[i]->children[orig_position_sequence[i + 1]];
if (node_sequence[i + 1] == NULL) {
g_free(orig_position_sequence);
g_free(node_sequence);
return;
}
}
if (node_sequence[depth]->num_children == -1)
e_tree_selection_model_node_fill_children(etsm, path, node_sequence[depth]);
if (!node_sequence[depth]->all_children_selected_array)
node_sequence[depth]->all_children_selected_array = e_bit_array_new(node_sequence[depth]->num_children);
if (!node_sequence[depth]->any_children_selected_array)
node_sequence[depth]->any_children_selected_array = e_bit_array_new(node_sequence[depth]->num_children);
node_sequence[depth]->all_children_selected =
e_bit_array_cross_and(node_sequence[depth]->all_children_selected_array) &&
node_sequence[depth]->selected;
node_sequence[depth]->any_children_selected =
e_bit_array_cross_or(node_sequence[depth]->any_children_selected_array) ||
node_sequence[depth]->selected;
for (i = depth - 1; i >= 0; i--) {
gboolean all_children, any_children;
if (!node_sequence[i]->all_children_selected_array)
node_sequence[i]->all_children_selected_array = e_bit_array_new(node_sequence[i]->num_children);
if (!node_sequence[i]->any_children_selected_array)
node_sequence[i]->any_children_selected_array = e_bit_array_new(node_sequence[i]->num_children);
e_bit_array_change_one_row(node_sequence[i]->all_children_selected_array,
orig_position_sequence[i + 1], node_sequence[i + 1]->all_children_selected);
e_bit_array_change_one_row(node_sequence[i]->any_children_selected_array,
orig_position_sequence[i + 1], node_sequence[i + 1]->any_children_selected);
all_children = node_sequence[i]->all_children_selected;
any_children = node_sequence[i]->any_children_selected;
node_sequence[i]->all_children_selected =
e_bit_array_cross_and(node_sequence[i]->all_children_selected_array) &&
node_sequence[i]->selected;
node_sequence[i]->any_children_selected =
e_bit_array_cross_or(node_sequence[i]->any_children_selected_array) ||
node_sequence[i]->selected;
if (all_children == node_sequence[i]->all_children_selected &&
any_children == node_sequence[i]->any_children_selected)
break;
}
g_free(orig_position_sequence);
g_free(node_sequence);
}
/* Signal handlers */
static void
etsm_pre_change (ETreeModel *etm, ETreeSelectionModel *etsm)
{
}
static void
etsm_node_changed (ETreeModel *etm, ETreePath node, ETreeSelectionModel *etsm)
{
etsm_real_clear (etsm);
}
static void
etsm_node_data_changed (ETreeModel *etm, ETreePath node, ETreeSelectionModel *etsm)
{
}
static void
etsm_node_col_changed (ETreeModel *etm, ETreePath node, int col, ETreeSelectionModel *etsm)
{
}
static void
etsm_node_inserted (ETreeModel *etm, ETreePath parent, ETreePath child, ETreeSelectionModel *etsm)
{
etsm_node_changed(etm, parent, etsm);
#if 0
ETreeSelectionModelNode *node;
ETreePath path;
path = e_tree_sorted_model_to_view_path(etsm->priv->ets, parent);
if (!path)
return;
node = etsm_find_node_unless_equals (etsm, path, FALSE);
if (node) {
node->selected = FALSE;
update_parents(etsm, path);
}
#endif
}
static void
etsm_node_removed (ETreeModel *etm, ETreePath parent, ETreePath child, int old_position, ETreeSelectionModel *etsm)
{
etsm_node_changed(etm, parent, etsm);
}
static void
add_model(ETreeSelectionModel *etsm, ETreeModel *model)
{
ETreeSelectionModelPriv *priv = etsm->priv;
priv->model = model;
if (!priv->model)
return;
gtk_object_ref(GTK_OBJECT(priv->model));
priv->tree_model_pre_change_id = gtk_signal_connect (GTK_OBJECT (priv->model), "pre_change",
GTK_SIGNAL_FUNC (etsm_pre_change), etsm);
priv->tree_model_node_changed_id = gtk_signal_connect (GTK_OBJECT (priv->model), "node_changed",
GTK_SIGNAL_FUNC (etsm_node_changed), etsm);
priv->tree_model_node_data_changed_id = gtk_signal_connect (GTK_OBJECT (priv->model), "node_data_changed",
GTK_SIGNAL_FUNC (etsm_node_data_changed), etsm);
priv->tree_model_node_col_changed_id = gtk_signal_connect (GTK_OBJECT (priv->model), "node_col_changed",
GTK_SIGNAL_FUNC (etsm_node_col_changed), etsm);
priv->tree_model_node_inserted_id = gtk_signal_connect (GTK_OBJECT (priv->model), "node_inserted",
GTK_SIGNAL_FUNC (etsm_node_inserted), etsm);
priv->tree_model_node_removed_id = gtk_signal_connect (GTK_OBJECT (priv->model), "node_removed",
GTK_SIGNAL_FUNC (etsm_node_removed), etsm);
}
static void
drop_model(ETreeSelectionModel *etsm)
{
ETreeSelectionModelPriv *priv = etsm->priv;
if (!priv->model)
return;
gtk_signal_disconnect (GTK_OBJECT (priv->model),
priv->tree_model_pre_change_id);
gtk_signal_disconnect (GTK_OBJECT (priv->model),
priv->tree_model_node_changed_id);
gtk_signal_disconnect (GTK_OBJECT (priv->model),
priv->tree_model_node_data_changed_id);
gtk_signal_disconnect (GTK_OBJECT (priv->model),
priv->tree_model_node_col_changed_id);
gtk_signal_disconnect (GTK_OBJECT (priv->model),
priv->tree_model_node_inserted_id);
gtk_signal_disconnect (GTK_OBJECT (priv->model),
priv->tree_model_node_removed_id);
gtk_object_unref (GTK_OBJECT (priv->model));
priv->model = NULL;
priv->tree_model_pre_change_id = 0;
priv->tree_model_node_changed_id = 0;
priv->tree_model_node_data_changed_id = 0;
priv->tree_model_node_col_changed_id = 0;
priv->tree_model_node_inserted_id = 0;
priv->tree_model_node_removed_id = 0;
}
/* Virtual functions */
static void
etsm_destroy (GtkObject *object)
{
ETreeSelectionModel *etsm;
etsm = E_TREE_SELECTION_MODEL (object);
etsm_real_clear (etsm);
drop_model(etsm);
g_free(etsm->priv);
}
static void
etsm_get_arg (GtkObject *o, GtkArg *arg, guint arg_id)
{
ETreeSelectionModel *etsm = E_TREE_SELECTION_MODEL (o);
switch (arg_id){
case ARG_CURSOR_ROW:
GTK_VALUE_INT(*arg) = etsm_cursor_row_real(etsm);
break;
case ARG_CURSOR_COL:
GTK_VALUE_INT(*arg) = etsm->priv->cursor_col;
break;
case ARG_MODEL:
GTK_VALUE_OBJECT(*arg) = (GtkObject *) etsm->priv->model;
break;
case ARG_ETTA:
GTK_VALUE_OBJECT(*arg) = (GtkObject *) etsm->priv->etta;
break;
case ARG_ETS:
GTK_VALUE_OBJECT(*arg) = (GtkObject *) etsm->priv->ets;
break;
}
}
static void
etsm_set_arg (GtkObject *o, GtkArg *arg, guint arg_id)
{
ESelectionModel *esm = E_SELECTION_MODEL (o);
ETreeSelectionModel *etsm = E_TREE_SELECTION_MODEL (o);
switch (arg_id){
case ARG_CURSOR_ROW:
e_selection_model_do_something(esm, GTK_VALUE_INT(*arg), etsm->priv->cursor_col, 0);
break;
case ARG_CURSOR_COL:
e_selection_model_do_something(esm, etsm_cursor_row_real(etsm), GTK_VALUE_INT(*arg), 0);
break;
case ARG_MODEL:
drop_model(etsm);
add_model(etsm, (ETreeModel *) GTK_VALUE_OBJECT(*arg));
break;
case ARG_ETTA:
etsm->priv->etta = (ETreeTableAdapter *) GTK_VALUE_OBJECT(*arg);
break;
case ARG_ETS:
etsm->priv->ets = (ETreeSorted *) GTK_VALUE_OBJECT(*arg);
break;
}
}
static ETreeSelectionModelNode *
etsm_recurse_is_path_selected (ESelectionModel *selection,
ETreePath path,
gboolean *is_selected)
{
ETreeSelectionModelNode *selection_node;
ETreeSelectionModel *etsm = E_TREE_SELECTION_MODEL(selection);
ETreeSorted *ets = etsm->priv->ets;
ETreePath parent;
parent = e_tree_model_node_get_parent(E_TREE_MODEL(ets), path);
if (parent) {
selection_node = etsm_recurse_is_path_selected (selection, parent, is_selected);
if (selection_node) {
int position = e_tree_sorted_orig_position(ets, path);
if (position < 0 || position >= selection_node->num_children) {
*is_selected = FALSE;
return NULL;
}
if (selection_node->all_children_selected) {
*is_selected = TRUE;
return NULL;
}
if (! selection_node->any_children_selected) {
*is_selected = FALSE;
return NULL;
}
if (selection_node->all_children_selected_array && e_bit_array_value_at(selection_node->all_children_selected_array, position)) {
*is_selected = TRUE;
return NULL;
}
if (selection_node->any_children_selected_array && ! e_bit_array_value_at(selection_node->any_children_selected_array, position)) {
*is_selected = FALSE;
return NULL;
}
if (!selection_node->children) {
*is_selected = FALSE;
return NULL;
}
return selection_node->children[position];
} else
return NULL;
} else {
if (etsm->priv->root) {
return etsm->priv->root;
} else {
*is_selected = FALSE;
return NULL;
}
}
}
/**
* e_selection_model_is_row_selected
* @selection: #ESelectionModel to check
* @n: The row to check
*
* This routine calculates whether the given row is selected.
*
* Returns: %TRUE if the given row is selected
*/
static gboolean
etsm_is_row_selected (ESelectionModel *selection,
gint row)
{
ETreeSelectionModel *etsm = E_TREE_SELECTION_MODEL(selection);
ETreePath path;
ETreeSelectionModelNode *selection_node;
gboolean ret_val;
path = e_tree_table_adapter_node_at_row(etsm->priv->etta, row);
selection_node = etsm_recurse_is_path_selected (selection, path, &ret_val);
if (selection_node)
ret_val = selection_node->selected;
return ret_val;
}
typedef struct {
ETreeSelectionModel *etsm;
EForeachFunc callback;
gpointer closure;
} ModelAndCallback;
static void
etsm_row_foreach_cb (ETreePath path, gpointer user_data)
{
ModelAndCallback *mac = user_data;
int row = etsm_row_of_node(mac->etsm, path);
mac->callback(row, mac->closure);
}
/**
* e_selection_model_foreach
* @selection: #ESelectionModel to traverse
* @callback: The callback function to call back.
* @closure: The closure
*
* This routine calls the given callback function once for each
* selected row, passing closure as the closure.
*/
static void
etsm_foreach (ESelectionModel *selection,
EForeachFunc callback,
gpointer closure)
{
ETreeSelectionModel *etsm = E_TREE_SELECTION_MODEL(selection);
ModelAndCallback mac;
mac.etsm = etsm;
mac.callback = callback;
mac.closure = closure;
e_tree_selection_model_foreach(etsm, etsm_row_foreach_cb, &mac);
}
/**
* e_selection_model_clear
* @selection: #ESelectionModel to clear
*
* This routine clears the selection to no rows selected.
*/
static void
etsm_clear(ESelectionModel *selection)
{
ETreeSelectionModel *etsm = E_TREE_SELECTION_MODEL(selection);
etsm_real_clear (etsm);
etsm->priv->cursor_path = NULL;
etsm->priv->cursor_col = -1;
e_selection_model_selection_changed(E_SELECTION_MODEL(etsm));
e_selection_model_cursor_changed(E_SELECTION_MODEL(etsm), -1, -1);
}
#if 0
/**
* e_selection_model_selected_count
* @selection: #ESelectionModel to count
*
* This routine calculates the number of rows selected.
*
* Returns: The number of rows selected in the given model.
*/
static gint
etsm_selected_count (ESelectionModel *selection)
{
ETreeSelectionModel *etsm = E_TREE_SELECTION_MODEL(selection);
return g_hash_table_size(etsm->priv->data);
}
#endif
/**
* e_selection_model_select_all
* @selection: #ESelectionModel to select all
*
* This routine selects all the rows in the given
* #ESelectionModel.
*/
static void
etsm_select_all (ESelectionModel *selection)
{
ETreeSelectionModel *etsm = E_TREE_SELECTION_MODEL(selection);
etsm_real_clear (etsm);
etsm->priv->root = e_tree_selection_model_node_new();
etsm->priv->root->selected = TRUE;
etsm->priv->root->all_children_selected = TRUE;
etsm->priv->root->any_children_selected = TRUE;
e_tree_selection_model_node_fill_children(etsm, e_tree_model_get_root(E_TREE_MODEL(etsm->priv->ets)), etsm->priv->root);
etsm->priv->root->all_children_selected_array = e_bit_array_new(etsm->priv->root->num_children);
etsm->priv->root->any_children_selected_array = e_bit_array_new(etsm->priv->root->num_children);
e_bit_array_select_all(etsm->priv->root->all_children_selected_array);
e_bit_array_select_all(etsm->priv->root->any_children_selected_array);
if (etsm->priv->cursor_col == -1)
etsm->priv->cursor_col = 0;
if (etsm->priv->cursor_path == NULL)
etsm->priv->cursor_path = etsm_node_at_row(etsm, 0);
etsm->priv->selection_start_row = 0;
e_selection_model_selection_changed(E_SELECTION_MODEL(etsm));
e_selection_model_cursor_changed(E_SELECTION_MODEL(etsm), etsm_cursor_row_real(etsm), etsm->priv->cursor_col);
}
static void
etsm_invert_selection_recurse (ETreeSelectionModel *etsm,
ETreeSelectionModelNode *selection_node)
{
gboolean temp;
EBitArray *temp_eba;
selection_node->selected = ! selection_node->selected;
temp = selection_node->all_children_selected;
selection_node->all_children_selected = ! selection_node->any_children_selected;
selection_node->any_children_selected = ! temp;
temp_eba = selection_node->all_children_selected_array;
selection_node->all_children_selected_array = selection_node->any_children_selected_array;
selection_node->any_children_selected_array = temp_eba;
if (selection_node->all_children_selected_array)
e_bit_array_invert_selection(selection_node->all_children_selected_array);
if (selection_node->any_children_selected_array)
e_bit_array_invert_selection(selection_node->any_children_selected_array);
if (selection_node->children) {
int i;
for (i = 0; i < selection_node->num_children; i++) {
if (selection_node->children[i])
etsm_invert_selection_recurse (etsm, selection_node->children[i]);
}
}
}
/**
* e_selection_model_invert_selection
* @selection: #ESelectionModel to invert
*
* This routine inverts all the rows in the given
* #ESelectionModel.
*/
static void
etsm_invert_selection (ESelectionModel *selection)
{
ETreeSelectionModel *etsm = E_TREE_SELECTION_MODEL(selection);
if (etsm->priv->root)
etsm_invert_selection_recurse (etsm, etsm->priv->root);
etsm->priv->cursor_col = -1;
etsm->priv->cursor_path = NULL;
etsm->priv->selection_start_row = 0;
e_selection_model_selection_changed(E_SELECTION_MODEL(etsm));
e_selection_model_cursor_changed(E_SELECTION_MODEL(etsm), -1, -1);
}
static int
etsm_row_count (ESelectionModel *selection)
{
ETreeSelectionModel *etsm = E_TREE_SELECTION_MODEL(selection);
return e_table_model_row_count(E_TABLE_MODEL(etsm->priv->etta));
}
static void
etsm_change_one_row(ESelectionModel *selection, int row, gboolean grow)
{
ETreeSelectionModel *etsm = E_TREE_SELECTION_MODEL(selection);
ETreeSelectionModelNode *node;
ETreePath path = e_tree_table_adapter_node_at_row(etsm->priv->etta, row);
if (!path)
return;
node = etsm_find_node_unless_equals (etsm, path, grow);
if (node) {
node->selected = grow;
update_parents(etsm, path);
}
}
static void
etsm_change_cursor (ESelectionModel *selection, int row, int col)
{
ETreeSelectionModel *etsm;
g_return_if_fail(selection != NULL);
g_return_if_fail(E_IS_SELECTION_MODEL(selection));
etsm = E_TREE_SELECTION_MODEL(selection);
if (row == -1) {
etsm->priv->cursor_path = NULL;
} else {
etsm->priv->cursor_path = etsm_node_at_row(etsm, row);
}
etsm->priv->cursor_col = col;
}
static void
etsm_change_range(ESelectionModel *selection, int start, int end, gboolean grow)
{
int i;
if (start != end) {
if (selection->sorter && e_sorter_needs_sorting(selection->sorter)) {
for ( i = start; i < end; i++) {
e_selection_model_change_one_row(selection, e_sorter_sorted_to_model(selection->sorter, i), grow);
}
} else {
for ( i = start; i < end; i++) {
e_selection_model_change_one_row(selection, i, grow);
}
}
}
}
static int
etsm_cursor_row (ESelectionModel *selection)
{
return etsm_cursor_row_real(E_TREE_SELECTION_MODEL(selection));
}
static int
etsm_cursor_col (ESelectionModel *selection)
{
ETreeSelectionModel *etsm = E_TREE_SELECTION_MODEL(selection);
return etsm->priv->cursor_col;
}
static void
etsm_select_single_row (ESelectionModel *selection, int row)
{
ETreeSelectionModel *etsm = E_TREE_SELECTION_MODEL(selection);
etsm_real_clear (etsm);
etsm_change_one_row(selection, row, TRUE);
etsm->priv->selection_start_row = row;
e_selection_model_selection_changed(E_SELECTION_MODEL(etsm));
}
static void
etsm_toggle_single_row (ESelectionModel *selection, int row)
{
ETreeSelectionModel *etsm = E_TREE_SELECTION_MODEL(selection);
etsm->priv->selection_start_row = row;
etsm_change_one_row(selection, row, !etsm_is_row_selected(selection, row));
e_selection_model_selection_changed(E_SELECTION_MODEL(etsm));
}
static void
etsm_move_selection_end (ESelectionModel *selection, int row)
{
ETreeSelectionModel *etsm = E_TREE_SELECTION_MODEL(selection);
int old_start;
int old_end;
int new_start;
int new_end;
if (selection->sorter && e_sorter_needs_sorting(selection->sorter)) {
old_start = MIN (e_sorter_model_to_sorted(selection->sorter, etsm->priv->selection_start_row),
e_sorter_model_to_sorted(selection->sorter, etsm_cursor_row_real(etsm)));
old_end = MAX (e_sorter_model_to_sorted(selection->sorter, etsm->priv->selection_start_row),
e_sorter_model_to_sorted(selection->sorter, etsm_cursor_row_real(etsm))) + 1;
new_start = MIN (e_sorter_model_to_sorted(selection->sorter, etsm->priv->selection_start_row),
e_sorter_model_to_sorted(selection->sorter, row));
new_end = MAX (e_sorter_model_to_sorted(selection->sorter, etsm->priv->selection_start_row),
e_sorter_model_to_sorted(selection->sorter, row)) + 1;
} else {
old_start = MIN (etsm->priv->selection_start_row, etsm_cursor_row_real(etsm));
old_end = MAX (etsm->priv->selection_start_row, etsm_cursor_row_real(etsm)) + 1;
new_start = MIN (etsm->priv->selection_start_row, row);
new_end = MAX (etsm->priv->selection_start_row, row) + 1;
}
/* This wouldn't work nearly so smoothly if one end of the selection weren't held in place. */
if (old_start < new_start)
etsm_change_range(selection, old_start, new_start, FALSE);
if (new_start < old_start)
etsm_change_range(selection, new_start, old_start, TRUE);
if (old_end < new_end)
etsm_change_range(selection, old_end, new_end, TRUE);
if (new_end < old_end)
etsm_change_range(selection, new_end, old_end, FALSE);
e_selection_model_selection_changed(E_SELECTION_MODEL(etsm));
}
static void
etsm_set_selection_end (ESelectionModel *selection, int row)
{
ETreeSelectionModel *etsm = E_TREE_SELECTION_MODEL(selection);
etsm_select_single_row(selection, etsm->priv->selection_start_row);
if (etsm->priv->selection_start_row != -1)
etsm->priv->cursor_path = etsm_node_at_row(etsm, etsm->priv->selection_start_row);
else
etsm->priv->cursor_path = NULL;
e_selection_model_move_selection_end(selection, row);
}
/* Standard functions */
static void
etsm_foreach_all_recurse (ETreeSelectionModel *etsm,
ETreePath path,
ETreeForeachFunc callback,
gpointer closure)
{
ETreePath model_path;
ETreePath child;
model_path = e_tree_sorted_view_to_model_path(etsm->priv->ets, path);
callback(model_path, closure);
child = e_tree_model_node_get_first_child(E_TREE_MODEL(etsm->priv->ets), path);
for ( ; child; child = e_tree_model_node_get_next(E_TREE_MODEL(etsm->priv->ets), child))
if (child)
etsm_foreach_all_recurse (etsm, child, callback, closure);
}
static void
etsm_foreach_recurse (ETreeSelectionModel *etsm,
ETreeSelectionModelNode *selection_node,
ETreePath path,
ETreeForeachFunc callback,
gpointer closure)
{
if (selection_node->all_children_selected) {
if (path)
etsm_foreach_all_recurse(etsm, path, callback, closure);
return;
}
if (!selection_node->any_children_selected)
return;
if (selection_node->selected) {
ETreePath model_path = e_tree_sorted_view_to_model_path(etsm->priv->ets, path);
callback(model_path, closure);
}
if (selection_node->children) {
ETreePath child = e_tree_model_node_get_first_child(E_TREE_MODEL(etsm->priv->ets), path);
int i;
for (i = 0; i < selection_node->num_children; i++, child = e_tree_model_node_get_next(E_TREE_MODEL(etsm->priv->ets), child))
if (selection_node->children[i])
etsm_foreach_recurse (etsm, selection_node->children[i], child, callback, closure);
}
}
void
e_tree_selection_model_foreach (ETreeSelectionModel *etsm,
ETreeForeachFunc callback,
gpointer closure)
{
if (etsm->priv->root) {
etsm_foreach_recurse(etsm, etsm->priv->root, e_tree_model_get_root(E_TREE_MODEL(etsm->priv->ets)), callback, closure);
}
}
static void
e_tree_selection_model_init (ETreeSelectionModel *etsm)
{
ETreeSelectionModelPriv *priv;
priv = g_new(ETreeSelectionModelPriv, 1);
etsm->priv = priv;
priv->root = NULL;
priv->cursor_path = NULL;
priv->cursor_col = -1;
priv->tree_model_pre_change_id = 0;
priv->tree_model_node_changed_id = 0;
priv->tree_model_node_data_changed_id = 0;
priv->tree_model_node_col_changed_id = 0;
priv->tree_model_node_inserted_id = 0;
priv->tree_model_node_removed_id = 0;
}
static void
e_tree_selection_model_class_init (ETreeSelectionModelClass *klass)
{
GtkObjectClass *object_class;
ESelectionModelClass *esm_class;
parent_class = gtk_type_class (e_selection_model_get_type ());
object_class = GTK_OBJECT_CLASS(klass);
esm_class = E_SELECTION_MODEL_CLASS(klass);
object_class->destroy = etsm_destroy;
object_class->get_arg = etsm_get_arg;
object_class->set_arg = etsm_set_arg;
esm_class->is_row_selected = etsm_is_row_selected ;
esm_class->foreach = etsm_foreach ;
esm_class->clear = etsm_clear ;
#if 0
esm_class->selected_count = etsm_selected_count ;
#endif
esm_class->select_all = etsm_select_all ;
esm_class->invert_selection = etsm_invert_selection ;
esm_class->row_count = etsm_row_count ;
esm_class->change_one_row = etsm_change_one_row ;
esm_class->change_cursor = etsm_change_cursor ;
esm_class->cursor_row = etsm_cursor_row ;
esm_class->cursor_col = etsm_cursor_col ;
esm_class->select_single_row = etsm_select_single_row ;
esm_class->toggle_single_row = etsm_toggle_single_row ;
esm_class->move_selection_end = etsm_move_selection_end ;
esm_class->set_selection_end = etsm_set_selection_end ;
gtk_object_add_arg_type ("ETreeSelectionModel::cursor_row", GTK_TYPE_INT,
GTK_ARG_READWRITE, ARG_CURSOR_ROW);
gtk_object_add_arg_type ("ETreeSelectionModel::cursor_col", GTK_TYPE_INT,
GTK_ARG_READWRITE, ARG_CURSOR_COL);
gtk_object_add_arg_type ("ETreeSelectionModel::model", E_TREE_MODEL_TYPE,
GTK_ARG_READWRITE, ARG_MODEL);
gtk_object_add_arg_type ("ETreeSelectionModel::etta", E_TREE_TABLE_ADAPTER_TYPE,
GTK_ARG_READWRITE, ARG_ETTA);
gtk_object_add_arg_type ("ETreeSelectionModel::ets", E_TREE_SORTED_TYPE,
GTK_ARG_READWRITE, ARG_ETS);
}
ESelectionModel *
e_tree_selection_model_new (void)
{
return gtk_type_new(e_tree_selection_model_get_type());
}
E_MAKE_TYPE(e_tree_selection_model, "ETreeSelectionModel", ETreeSelectionModel,
e_tree_selection_model_class_init, e_tree_selection_model_init, PARENT_TYPE);
|