forked from L1L1/cardpeek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dyntree_model.c
1697 lines (1433 loc) · 50.3 KB
/
dyntree_model.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
#include "config.h"
#include "dyntree_model.h"
#include "misc.h"
#include "a_string.h"
#include <glib/gstdio.h>
#include <stdarg.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
DyntreeModel *CARD_DATA_STORE = NULL;
/*
* NOTE: Integration of the data model in GTK+ is based on the GtkTreeView
* tutorial by Tim-Philipp Müller (thanks!).
*
*/
/* internal stuff */
static void dyntree_model_init (DyntreeModel *pkg_tree);
static void dyntree_model_class_init (DyntreeModelClass *klass);
static void dyntree_model_tree_model_init (GtkTreeModelIface *iface);
static void dyntree_model_finalize (GObject *object);
static GtkTreeModelFlags dyntree_model_get_flags (GtkTreeModel *tree_model);
static GType dyntree_model_get_column_type (GtkTreeModel *tree_model,
gint c_index);
static gboolean dyntree_model_get_iter (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreePath *path);
static GtkTreePath *dyntree_model_get_path (GtkTreeModel *tree_model,
GtkTreeIter *iter);
static void dyntree_model_get_value (GtkTreeModel *tree_model,
GtkTreeIter *iter,
gint column,
GValue *value);
static gboolean dyntree_model_iter_next (GtkTreeModel *tree_model,
GtkTreeIter *iter);
static gboolean dyntree_model_iter_children (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *parent);
static gboolean dyntree_model_iter_has_child (GtkTreeModel *tree_model,
GtkTreeIter *iter);
static gint dyntree_model_iter_n_children (GtkTreeModel *tree_model,
GtkTreeIter *iter);
static gboolean dyntree_model_iter_nth_child (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *parent,
gint n);
static gboolean dyntree_model_iter_parent (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *child);
static GObjectClass *parent_class = NULL;
static gboolean internal_dyntree_model_iter_remove(DyntreeModel *ctm,
GtkTreeIter *iter,
gboolean emit_signal);
/* implementation */
GType dyntree_model_get_type (void)
{
static GType dyntree_model_type = 0;
if (dyntree_model_type)
return dyntree_model_type;
if (1)
{
static const GTypeInfo dyntree_model_info =
{
sizeof (DyntreeModelClass),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) dyntree_model_class_init,
NULL, /* class finalize */
NULL, /* class_data */
sizeof (DyntreeModel),
0, /* n_preallocs */
(GInstanceInitFunc) dyntree_model_init,
NULL,
};
dyntree_model_type = g_type_register_static (G_TYPE_OBJECT, "DyntreeModel",
&dyntree_model_info, (GTypeFlags)0);
}
if (1)
{
static const GInterfaceInfo tree_model_info =
{
(GInterfaceInitFunc) dyntree_model_tree_model_init,
NULL,
NULL
};
g_type_add_interface_static (dyntree_model_type, GTK_TYPE_TREE_MODEL, &tree_model_info);
}
return dyntree_model_type;
}
static void dyntree_model_class_init(DyntreeModelClass *klass)
{
GObjectClass *object_class;
parent_class = (GObjectClass *) g_type_class_peek_parent (klass);
object_class = (GObjectClass *) klass;
object_class->finalize = dyntree_model_finalize;
}
static void dyntree_model_tree_model_init (GtkTreeModelIface *iface)
{
iface->get_flags = dyntree_model_get_flags;
iface->get_n_columns = dyntree_model_get_n_columns;
iface->get_column_type = dyntree_model_get_column_type;
iface->get_iter = dyntree_model_get_iter;
iface->get_path = dyntree_model_get_path;
iface->get_value = dyntree_model_get_value;
iface->iter_next = dyntree_model_iter_next;
iface->iter_children = dyntree_model_iter_children;
iface->iter_has_child = dyntree_model_iter_has_child;
iface->iter_n_children = dyntree_model_iter_n_children;
iface->iter_nth_child = dyntree_model_iter_nth_child;
iface->iter_parent = dyntree_model_iter_parent;
}
int dyntree_model_column_name_to_index(DyntreeModel *ctm, const char *column_name)
{
/* the value in the table is c+1 to differentiate the NULL from N=0, so we substract 1 */
return GPOINTER_TO_INT((char *)g_hash_table_lookup(ctm->columns_by_name,(gpointer)column_name))-1;
}
const char *dyntree_model_column_index_to_name(DyntreeModel *ctm, int c_index)
{
if (c_index<0 || c_index>=ctm->n_columns)
return NULL;
return ctm->columns_by_index[c_index].name;
}
int dyntree_model_column_register(DyntreeModel *ctm,
const char *column_name)
{
int col;
if ((col = dyntree_model_column_name_to_index(ctm,column_name))<0)
{
col = ctm->n_columns++;
if (col==ctm->max_columns)
{
ctm->max_columns<<=1;
ctm->columns_by_index = g_realloc(ctm->columns_by_index,
sizeof(DyntreeModelAttributeDescriptor)*ctm->max_columns);
}
ctm->columns_by_index[col].name = g_strdup(column_name);
/* we add 1 to col so that col==0 is stored as 1 and not NULL */
g_hash_table_insert(ctm->columns_by_name,(gpointer)ctm->columns_by_index[col].name,GINT_TO_POINTER(col+1));
}
return col;
}
static void dyntree_model_init (DyntreeModel *ctm)
{
ctm->root = NULL;
ctm->columns_by_name = g_hash_table_new(cstring_hash,cstring_equal);
g_assert(ctm->columns_by_name != NULL);
ctm->columns_by_index = g_try_malloc(sizeof(char *)*16);
g_assert(ctm->columns_by_index != NULL);
ctm->max_columns = 16;
ctm->n_columns = 0;
ctm->stamp = g_random_int(); /* Random int to check whether an iter belongs to our model */
}
static void dyntree_model_finalize (GObject *object)
{
DyntreeModel *ctm = DYNTREE_MODEL(object);
int i;
internal_dyntree_model_iter_remove(ctm,NULL,FALSE);
for (i=0; i<ctm->n_columns; i++)
{
if (ctm->columns_by_index[i].name)
g_free(ctm->columns_by_index[i].name);
}
g_free(ctm->columns_by_index);
g_hash_table_destroy(ctm->columns_by_name);
/* must chain up - finalize parent */
(* parent_class->finalize) (object);
}
static GtkTreeModelFlags dyntree_model_get_flags (GtkTreeModel *tree_model)
{
g_return_val_if_fail (DYNTREE_IS_MODEL(tree_model), (GtkTreeModelFlags)0);
return (GTK_TREE_MODEL_ITERS_PERSIST);
}
/*****************************************************************************
*
* dyntree_model_get_n_columns: tells the rest of the world how many data
* columns we export via the tree model interface
*
*****************************************************************************/
gint dyntree_model_get_n_columns (GtkTreeModel *tree_model)
{
g_return_val_if_fail (DYNTREE_IS_MODEL(tree_model), 0);
return DYNTREE_MODEL(tree_model)->n_columns;
}
/*****************************************************************************
*
* dyntree_model_get_column_type: tells the rest of the world which type of
* data an exported model column contains
*
*****************************************************************************/
static GType dyntree_model_get_column_type (GtkTreeModel *tree_model,
gint c_index)
{
g_return_val_if_fail (DYNTREE_IS_MODEL(tree_model), G_TYPE_INVALID);
g_return_val_if_fail (c_index < DYNTREE_MODEL(tree_model)->n_columns && c_index >= 0, G_TYPE_INVALID);
return G_TYPE_STRING;
}
/*****************************************************************************
*
* dyntree_model_get_iter: converts a tree path (physical position) into a
* tree iter structure
*
*****************************************************************************/
static gboolean dyntree_model_get_iter (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreePath *path)
{
/* DyntreeModel *dyntree_model; */
GtkTreeIter parent;
gint *indices, n, depth;
g_assert(DYNTREE_IS_MODEL(tree_model));
g_assert(path!=NULL);
/* dyntree_model = DYNTREE_MODEL(tree_model); */
indices = gtk_tree_path_get_indices(path);
depth = gtk_tree_path_get_depth(path);
g_assert (depth>0);
if (dyntree_model_iter_nth_child(tree_model,iter,NULL,indices[0])==FALSE)
return FALSE;
for (n=1; n<depth; n++)
{
parent = *iter;
if (dyntree_model_iter_nth_child(tree_model,iter,&parent,indices[n])==FALSE)
return FALSE;
}
return TRUE;
}
/*****************************************************************************
*
* dyntree_model_get_path: converts a tree iter into a tree path.
*
*****************************************************************************/
static GtkTreePath *dyntree_model_get_path (GtkTreeModel *tree_model,
GtkTreeIter *iter)
{
GtkTreePath *path;
DyntreeModelNode *node;
g_return_val_if_fail (DYNTREE_IS_MODEL(tree_model), NULL);
g_return_val_if_fail (iter != NULL, NULL);
g_return_val_if_fail (iter->user_data != NULL, NULL);
node = (DyntreeModelNode *)iter->user_data;
path = gtk_tree_path_new();
while (node)
{
gtk_tree_path_prepend_index(path, node->sibling_index);
node = node -> parent;
}
return path;
}
/*****************************************************************************
*
* dyntree_model_get_value: Returns a row's exported data columns
* (_get_value is what gtk_tree_model_get uses)
*
*****************************************************************************/
static void dyntree_model_get_value (GtkTreeModel *tree_model,
GtkTreeIter *iter,
gint column,
GValue *value)
{
DyntreeModelNode *node;
/* DyntreeModel *ctm; */
g_return_if_fail (DYNTREE_IS_MODEL (tree_model));
g_return_if_fail (iter != NULL);
g_return_if_fail (column < DYNTREE_MODEL(tree_model)->n_columns);
/* ctm = DYNTREE_MODEL(tree_model); */
node = (DyntreeModelNode *) iter->user_data;
g_return_if_fail ( node != NULL );
g_value_init (value, G_TYPE_STRING);
if (column<node->max_attributes && node->attributes[column].value)
g_value_set_string(value, node->attributes[column].value);
}
/*****************************************************************************
*
* dyntree_model_iter_next: Takes an iter structure and sets it to point
* to the next row.
*
*****************************************************************************/
static gboolean dyntree_model_iter_next (GtkTreeModel *tree_model,
GtkTreeIter *iter)
{
DyntreeModelNode *node, *nextnode;
DyntreeModel *ctm;
g_return_val_if_fail (DYNTREE_IS_MODEL (tree_model), FALSE);
g_return_val_if_fail (iter != NULL, FALSE);
g_return_val_if_fail (iter->user_data != NULL, FALSE);
ctm = DYNTREE_MODEL(tree_model);
node = (DyntreeModelNode *) iter->user_data;
if (node->next == NULL)
return FALSE;
nextnode = node->next;
iter->stamp = ctm->stamp;
iter->user_data = nextnode;
return TRUE;
}
/*****************************************************************************
*
* dyntree_model_iter_children: Returns TRUE or FALSE depending on whether
* the row specified by 'parent' has any children.
* If it has children, then 'iter' is set to
* point to the first child. Special case: if
* 'parent' is NULL, then the first top-level
* row should be returned if it exists.
*
*****************************************************************************/
static gboolean dyntree_model_iter_children (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *parent)
{
DyntreeModel *ctm;
DyntreeModelNode *node;
g_return_val_if_fail (DYNTREE_IS_MODEL (tree_model), FALSE);
ctm = DYNTREE_MODEL(tree_model);
if (parent==NULL)
{
if (ctm->root == NULL)
return FALSE;
iter->user_data = ctm->root;
iter->stamp = ctm->stamp;
return TRUE;
}
node = (DyntreeModelNode *)parent->user_data;
if (node->children == NULL)
return FALSE;
iter->user_data = node->children;
iter->stamp = ctm->stamp;
return TRUE;
}
/*****************************************************************************
*
* dyntree_model_iter_has_child: Returns TRUE or FALSE depending on whether
* the row specified by 'iter' has any children.
*
*****************************************************************************/
static gboolean dyntree_model_iter_has_child (GtkTreeModel *tree_model,
GtkTreeIter *iter)
{
DyntreeModelNode *node;
g_return_val_if_fail (DYNTREE_IS_MODEL (tree_model), FALSE);
g_return_val_if_fail (iter != NULL, FALSE);
node = (DyntreeModelNode *)iter->user_data;
if (node->children == NULL)
return FALSE;
return TRUE;
}
/*****************************************************************************
*
* dyntree_model_iter_n_children: Returns the number of children the row
* specified by 'iter' has.
*
*****************************************************************************/
static gint dyntree_model_iter_n_children (GtkTreeModel *tree_model,
GtkTreeIter *iter)
{
DyntreeModel *ctm;
DyntreeModelNode *node;
gint count_children;
g_return_val_if_fail (DYNTREE_IS_MODEL (tree_model), -1);
ctm = DYNTREE_MODEL(tree_model);
/* if we are at the root of ctm, we can't rely on node->n_children so
we manually count them */
if (iter==NULL)
{
node = ctm->root;
count_children = 0;
while (node)
{
node = node->next;
count_children++;
}
return count_children;
}
node = (DyntreeModelNode *)iter->user_data;
if (node==NULL) return 0;
return node->n_children;
}
/*****************************************************************************
*
* dyntree_model_iter_nth_child: If the row specified by 'parent' has any
* children, set 'iter' to the n-th child and
* return TRUE if it exists, otherwise FALSE.
* A special case is when 'parent' is NULL, in
* which case we need to set 'iter' to the n-th
* row if it exists.
*
*****************************************************************************/
static gboolean dyntree_model_iter_nth_child (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *parent,
gint n)
{
DyntreeModelNode *node;
DyntreeModel *ctm;
g_return_val_if_fail (DYNTREE_IS_MODEL (tree_model), FALSE);
ctm = DYNTREE_MODEL(tree_model);
if (parent==NULL)
{
node = ctm->root;
}
else
{
node = (DyntreeModelNode *)parent->user_data;
if (node==NULL) return FALSE;
node = node->children;
}
while (node)
{
if (n==0)
{
iter->stamp = ctm->stamp;
iter->user_data = node;
return TRUE;
}
node = node -> next;
n--;
}
return FALSE;
}
/*****************************************************************************
*
* dyntree_model_iter_parent: Point 'iter' to the parent node of 'child'. As
* we have a list and thus no children and no
* parents of children, we can just return FALSE.
*
*****************************************************************************/
static gboolean dyntree_model_iter_parent (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *child)
{
DyntreeModel *ctm;
DyntreeModelNode *node;
g_return_val_if_fail (DYNTREE_IS_MODEL (tree_model), -1);
ctm = DYNTREE_MODEL(tree_model);
node = (DyntreeModelNode *)child->user_data;
if (node->parent==NULL)
return FALSE;
iter->stamp = ctm->stamp;
iter->user_data = node->parent;
return TRUE;
}
/*****************************************************************************
*
* dyntree_model_new: This is what you use in your own code to create a
* new custom list tree model for you to use.
*
*****************************************************************************/
DyntreeModel *dyntree_model_new (void)
{
CARD_DATA_STORE = (DyntreeModel *) g_object_new (DYNTREE_MODEL_TYPE, NULL);
g_assert( CARD_DATA_STORE != NULL );
dyntree_model_column_register(CARD_DATA_STORE,"classname");
dyntree_model_column_register(CARD_DATA_STORE,"label");
dyntree_model_column_register(CARD_DATA_STORE,"id");
dyntree_model_column_register(CARD_DATA_STORE,"size");
dyntree_model_column_register(CARD_DATA_STORE,"val");
dyntree_model_column_register(CARD_DATA_STORE,"alt");
dyntree_model_column_register(CARD_DATA_STORE,"mime-type");
g_assert(CARD_DATA_STORE->n_columns == CC_INITIAL_COUNT);
return CARD_DATA_STORE;
}
/*****************************************************************************
*
* Implement user level functions
*
*****************************************************************************/
gboolean dyntree_model_iter_attribute_set(DyntreeModel *m,
GtkTreeIter *iter,
int c_index,
const char *str)
{
int i,pos;
DyntreeModelNode *node;
GtkTreePath *path;
g_return_val_if_fail ( iter!=NULL , FALSE );
g_return_val_if_fail ( c_index < m->n_columns && c_index >= 0, FALSE);
node = (DyntreeModelNode *)iter->user_data;
if (c_index>=node->max_attributes)
{
pos = node->max_attributes;
node->max_attributes = m->max_columns;
node->attributes = g_realloc(node->attributes,
sizeof(DyntreeModelAttributeValue)*m->max_columns);
for (i=pos; i<node->max_attributes; i++)
{
node->attributes[i].value = NULL;
}
}
if (node->attributes[c_index].value)
g_free(node->attributes[c_index].value);
if (str==NULL)
node->attributes[c_index].value = NULL;
else
node->attributes[c_index].value = g_strdup(str);
path = dyntree_model_get_path(GTK_TREE_MODEL(m), iter);
gtk_tree_model_row_changed(GTK_TREE_MODEL(m), path, iter);
gtk_tree_path_free(path);
return TRUE;
}
gboolean dyntree_model_iter_attribute_set_by_name(DyntreeModel *m,
GtkTreeIter *iter,
const char *attr_name,
const char *str)
{
int col = dyntree_model_column_register(m,attr_name);
if (col>=0)
return dyntree_model_iter_attribute_set(m,iter,col,str);
return FALSE;
}
static int dyntree_model_iter_n_attributes(DyntreeModel *m, GtkTreeIter *iter)
{
int attr_index;
int attr_count = 0;
DyntreeModelNode *node = iter->user_data;
for (attr_index=0; attr_index<m->n_columns && attr_index<node->max_attributes; attr_index++)
{
if (node->attributes[attr_index].value) attr_count++;
}
return attr_count;
}
gboolean dyntree_model_iter_attribute_get(DyntreeModel *m,
GtkTreeIter *iter,
int c_index,
const char **str)
{
DyntreeModelNode *node;
g_return_val_if_fail ( iter!=NULL, FALSE);
g_return_val_if_fail ( str!=NULL , FALSE);
node = (DyntreeModelNode *)iter->user_data;
g_return_val_if_fail (c_index < m->n_columns && c_index >= 0, FALSE);
if (c_index < node->max_attributes)
*str = node->attributes[c_index].value;
else
*str = NULL;
return TRUE;
}
gboolean dyntree_model_iter_attribute_get_by_name(DyntreeModel *m,
GtkTreeIter *iter,
const char *attr_name,
const char **str)
{
int col = dyntree_model_column_name_to_index(m,attr_name);
if (col>=0)
return dyntree_model_iter_attribute_get(m,iter,col,str);
return FALSE;
}
static DyntreeModelNode *dyntree_model_node_new(DyntreeModel *ctm)
{
DyntreeModelNode *node = g_new0(DyntreeModelNode,1);
UNUSED(ctm);
node->max_attributes = 8;
node->attributes = g_new0(DyntreeModelAttributeValue,node->max_attributes);
return node;
}
static void dyntree_model_node_reindex_from_parent(DyntreeModel *ct, DyntreeModelNode *parent)
{
int n = 0;
DyntreeModelNode *cur_node;
if (parent==NULL)
cur_node = ct->root;
else
cur_node = parent->children;
while (cur_node)
{
cur_node->sibling_index = n++;
cur_node = cur_node->next;
}
if (parent)
parent->n_children = n;
}
static gboolean internal_dyntree_model_iter_remove(DyntreeModel *ctm,
GtkTreeIter *iter,
gboolean emit_signal)
{
DyntreeModelNode *node;
int i;
GtkTreeIter other;
GtkTreePath *path;
if (iter == NULL)
{
while (ctm->root)
{
other.user_data = ctm->root;
internal_dyntree_model_iter_remove(ctm,&other,emit_signal);
}
return TRUE;
}
g_return_val_if_fail(iter->user_data != NULL, FALSE);
node = iter->user_data;
while (node->children)
{
other.stamp = ctm->stamp;
other.user_data = node->children;
internal_dyntree_model_iter_remove(ctm,&other,emit_signal);
}
if (emit_signal)
path = dyntree_model_get_path(GTK_TREE_MODEL(ctm),iter);
if (node->prev==NULL)
{
if (node->parent)
(node->parent)->children = node->next;
else
ctm->root = node->next;
}
if (node->prev) (node->prev)->next = node->next;
if (node->next) (node->next)->prev = node->prev;
dyntree_model_node_reindex_from_parent(ctm,node->parent);
for (i=0; i<node->max_attributes; i++)
{
if (node->attributes[i].value)
g_free(node->attributes[i].value);
}
g_free(node->attributes);
g_free(node);
if (emit_signal)
{
gtk_tree_model_row_deleted(GTK_TREE_MODEL(ctm), path);
gtk_tree_path_free(path);
}
return TRUE;
}
gboolean dyntree_model_iter_remove(DyntreeModel *ctm,
GtkTreeIter *iter)
{
return internal_dyntree_model_iter_remove(ctm,iter,TRUE);
}
#define dyntree_model_clear(ctm) dyntree_model_iter_remove(ctm,NULL)
void dyntree_model_iter_append (DyntreeModel *ctm,
GtkTreeIter *child,
GtkTreeIter *parent)
{
DyntreeModelNode *pred_node;
DyntreeModelNode *node;
DyntreeModelNode *parent_node;
GtkTreePath *path;
g_return_if_fail( child!=NULL );
if (parent==NULL)
{
parent_node = NULL;
if (ctm->root==NULL)
{
ctm->root = dyntree_model_node_new(ctm);
child->stamp = ctm->stamp;
child->user_data = node = ctm->root;
child->user_data2 = NULL;
child->user_data3 = NULL;
goto row_inserted;
}
pred_node = ctm->root;
}
else
{
parent_node = parent->user_data;
g_assert(parent_node != NULL);
if (parent_node->children==NULL)
{
parent_node->children = dyntree_model_node_new(ctm);
(parent_node->children)->parent = parent_node;
parent_node->n_children = 1;
child->stamp = ctm->stamp;
child->user_data = node = parent_node->children;
child->user_data2 = NULL;
child->user_data3 = NULL;
goto row_inserted;
}
parent_node->n_children++;
pred_node = parent_node->children;
}
while (pred_node->next)
pred_node = pred_node->next;
node = dyntree_model_node_new(ctm);
pred_node->next = node;
node->prev = pred_node;
node->sibling_index = pred_node->sibling_index+1;
node->parent = parent_node;
child->stamp = ctm->stamp;
child->user_data = node;
child->user_data2 = NULL;
child->user_data3 = NULL;
row_inserted:
path = dyntree_model_get_path(GTK_TREE_MODEL(ctm), child);
gtk_tree_model_row_inserted(GTK_TREE_MODEL(ctm), path, child);
gtk_tree_path_free(path);
}
static gboolean dyntree_model_iter_attributes_setv(DyntreeModel *ctm,
GtkTreeIter *iter,
va_list al)
{
int column;
g_assert(DYNTREE_IS_MODEL(ctm));
g_assert(iter!=NULL);
while ((column = va_arg(al,int))>=0)
{
if (dyntree_model_iter_attribute_set(ctm, iter, column,va_arg(al,char *))==FALSE)
return FALSE;
}
return TRUE;
}
gboolean dyntree_model_iter_attributes_set(DyntreeModel *ctm,
GtkTreeIter *iter,
...)
{
va_list al;
gboolean retval;
va_start(al,iter);
retval = dyntree_model_iter_attributes_setv(ctm,iter,al);
va_end(al);
return retval;
}
static gboolean dyntree_model_iter_attributes_getv(DyntreeModel *ctm,
GtkTreeIter *iter,
va_list al)
{
int column;
g_assert(DYNTREE_IS_MODEL(ctm));
g_assert(iter!=NULL);
while ((column = va_arg(al,int))>=0)
{
if (dyntree_model_iter_attribute_get(ctm,iter,column,va_arg(al,const char **))==FALSE)
return FALSE;
}
return TRUE;
}
gboolean dyntree_model_iter_attributes_get(DyntreeModel *ctm,
GtkTreeIter *iter,
...)
{
va_list al;
gboolean retval;
va_start(al,iter);
retval = dyntree_model_iter_attributes_getv(ctm,iter,al);
va_end(al);
return retval;
}
/* TEXT Export */
/* internal_node_to_text() is called by dyntree_model_iter_to_text() to do the
actual work of exporting part of the treeview in text format.
*/
static gboolean internal_node_to_text(a_string_t *res, DyntreeModel *store, GtkTreeIter *iter, unsigned depth, gboolean process_siblings)
{
unsigned i,j;
GtkTreeIter child;
DyntreeModelNode *node;
const char *classname;
const char *icontext;
unsigned pos_a;
unsigned pos_b;
do
{
pos_a = a_strlen(res);
node = iter->user_data;
g_assert(node != NULL);
for(i=0; i<depth; i++) a_strcat(res," ");
if (dyntree_model_iter_n_attributes(store,iter)==0
&& dyntree_model_iter_n_children(GTK_TREE_MODEL(store),iter)==0)
a_strcat(res,"(i)\n");
else
{
classname = node->attributes[CC_CLASSNAME].value;
icontext = NULL;
if (classname!=NULL && classname[0]=='t')
{
switch (classname[2])
{
case 'a':
if (strcmp("t:application",classname)==0) icontext = "[A] ";
else if (strcmp("t:atr",classname)==0) icontext = "[a] ";
break;
case 'b':
if (strcmp("t:block",classname)==0) icontext = "[b] ";
else if (strcmp("t:body",classname)==0) icontext = "[B] ";
break;
case 'c':
if (strcmp("t:card",classname)==0) icontext = "[C] ";
break;
case 'f':
if (strcmp("t:file",classname)==0) icontext = "[f] ";
else if (strcmp("t:folder",classname)==0) icontext = "[F] ";
break;
case 'h':
if (strcmp("t:header",classname)==0) icontext = "[h] ";
break;
case 'r':
if (strcmp("t:record",classname)==0) icontext = "[r] ";
break;
}
}
if (icontext==NULL)
a_strcat(res,"[i] ");
else
a_strcat(res,icontext);
if (node->attributes[CC_LABEL].value)
a_strcat(res,node->attributes[CC_LABEL].value+2);
a_strcat(res," (");
if (node->attributes[CC_ID].value)
{
a_strcat(res,"id=");
a_strcat(res,node->attributes[CC_ID].value+2);
}
if (node->attributes[CC_ID].value && node->attributes[CC_SIZE].value)
a_strcat(res,",");
if (node->attributes[CC_SIZE].value)
{
a_strcat(res,"size=");
a_strcat(res,node->attributes[CC_SIZE].value+2);
}
a_strcat(res,")");
if (node->attributes[CC_VAL].value && strlen(node->attributes[CC_VAL].value)>2)
{
if (node->attributes[CC_VAL].value[0]!='t')
{
pos_b = a_strlen(res);
a_strcat(res," :");
for (i=0;i<strlen(node->attributes[CC_VAL].value)-2;i++)
{
if (i && (i&0x1F)==0)
{
a_strcat(res,"\n");
for(j=0; j<pos_b-pos_a; j++) a_strpushback(res,' ');
a_strcat(res," ");
}
if ((i&1)==0)
a_strpushback(res,' ');