-
Notifications
You must be signed in to change notification settings - Fork 1
/
ckTree.c
2178 lines (2002 loc) · 61.3 KB
/
ckTree.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
/*
* ckTree.c --
*
* This module implements a tree widget.
*
* Copyright (c) 1996 Christian Werner
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include <stdio.h>
#include "ckPort.h"
#include "ck.h"
#include "default.h"
/*
* Widget defaults:
*/
#define DEF_TREE_ACTIVE_ATTR_COLOR "normal"
#define DEF_TREE_ACTIVE_ATTR_MONO "reverse"
#define DEF_TREE_ACTIVE_BG_COLOR "white"
#define DEF_TREE_ACTIVE_BG_MONO "black"
#define DEF_TREE_ACTIVE_FG_COLOR "black"
#define DEF_TREE_ACTIVE_FG_MONO "white"
#define DEF_TREE_ATTR_COLOR "normal"
#define DEF_TREE_ATTR_MONO "normal"
#define DEF_TREE_BG_COLOR "black"
#define DEF_TREE_BG_MONO "black"
#define DEF_TREE_FG_COLOR "white"
#define DEF_TREE_FG_MONO "white"
#define DEF_TREE_HEIGHT "10"
#define DEF_TREE_SELECT_ATTR_COLOR "bold"
#define DEF_TREE_SELECT_ATTR_MONO "bold"
#define DEF_TREE_SELECT_BG_COLOR "black"
#define DEF_TREE_SELECT_BG_MONO "black"
#define DEF_TREE_SELECT_FG_COLOR "white"
#define DEF_TREE_SELECT_FG_MONO "white"
#define DEF_TREE_TAKE_FOCUS "1"
#define DEF_TREE_WIDTH "40"
#define DEF_TREE_SCROLL_COMMAND NULL
/*
* A node in the tree is represented by this data structure.
*/
#define TAG_SPACE 5
typedef struct Node {
long id; /* Unique id of the node. */
int level; /* Level in tree, 0 means root. */
struct Tree *tree; /* Pointer to widget. */
struct Node *parent; /* Pointer to parent node or NULL. */
struct Node *next; /* Pointer to next node in this level. */
struct Node *firstChild, *lastChild;
Ck_Uid staticTagSpace[TAG_SPACE];
Ck_Uid *tagPtr; /* Pointer to tag array. */
int tagSpace; /* Total size of tag array. */
int numTags; /* Number of tags in tag array. */
int fg; /* Foreground color of node's text. */
int bg; /* Background color of node's text. */
int attr; /* Video attributes of node's text. */
char *text; /* Text to display for this node. */
int textWidth; /* Width of node's text. */
int flags; /* Flag bits (see below). */
} Node;
/*
* Flag bits for node:
*
* SELECTED: Non-zero means node is selected
* SHOWCHILDREN: Non-zero means if node has children
* they shall be displayed.
*/
#define SELECTED 1
#define SHOWCHILDREN 2
/*
* Custom option for handling "-tags" options for tree nodes:
*/
static int TreeTagsParseProc _ANSI_ARGS_((ClientData clientData,
Tcl_Interp *interp, CkWindow *winPtr, char *value,
char *widgRec, int offset));
static char * TreeTagsPrintProc _ANSI_ARGS_((ClientData clientData,
CkWindow *winPtr, char *widgRec, int offset,
Tcl_FreeProc **freeProcPtr));
Ck_CustomOption treeTagsOption = {
TreeTagsParseProc,
TreeTagsPrintProc,
(ClientData) NULL
};
/*
* Information used for parsing configuration specs for nodes:
*/
static Ck_ConfigSpec nodeConfigSpecs[] = {
{CK_CONFIG_ATTR, "-attributes", (char *) NULL, (char *) NULL,
"", Ck_Offset(Node, attr), CK_CONFIG_DONT_SET_DEFAULT },
{CK_CONFIG_COLOR, "-background", (char *) NULL, (char *) NULL,
"", Ck_Offset(Node, bg), CK_CONFIG_DONT_SET_DEFAULT },
{CK_CONFIG_SYNONYM, "-bg", "background", (char *) NULL,
(char *) NULL, 0, 0},
{CK_CONFIG_SYNONYM, "-fg", "foreground", (char *) NULL,
(char *) NULL, 0, 0},
{CK_CONFIG_COLOR, "-foreground", (char *) NULL, (char *) NULL,
"", Ck_Offset(Node, fg), CK_CONFIG_DONT_SET_DEFAULT},
{CK_CONFIG_CUSTOM, "-tags", (char *) NULL, (char *) NULL,
(char *) NULL, 0, CK_CONFIG_NULL_OK, &treeTagsOption},
{CK_CONFIG_STRING, "-text", (char *) NULL, (char *) NULL,
NULL, Ck_Offset(Node, text), CK_CONFIG_NULL_OK},
{CK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
(char *) NULL, 0, 0}
};
/*
* A data structure of the following type is kept for each
* widget managed by this file:
*/
typedef struct Tree {
CkWindow *winPtr; /* Window that embodies the widget. NULL
* means that the window has been destroyed
* but the data structures haven't yet been
* cleaned up.*/
Tcl_Interp *interp; /* Interpreter associated with menubutton. */
Tcl_Command widgetCmd; /* Token for menubutton's widget command. */
long idCount; /* For unique ids for nodes. */
/*
* Information about what's displayed in the menu button:
*/
Node *firstChild, *lastChild;
Tcl_HashTable nodeTable;
/*
* Information used when displaying widget:
*/
int normalFg; /* Foreground color in normal mode. */
int normalBg; /* Background color in normal mode. */
int normalAttr; /* Attributes in normal mode. */
int activeFg; /* Foreground color in active mode. */
int activeBg; /* Ditto, background color. */
int activeAttr; /* Attributes in active mode. */
int selectFg; /* Foreground color for selected nodes. */
int selectBg; /* Ditto, background color. */
int selectAttr; /* Attributes for selected nodes. */
int width, height; /* If > 0, these specify dimensions to request
* for window, in characters for text and in
* pixels for bitmaps. In this case the actual
* size of the text string or bitmap is
* ignored in computing desired window size. */
int visibleNodes; /* Total number of visible nodes. */
int topIndex; /* Index of starting line. */
Node *topNode; /* Node at top line of window. */
Node *activeNode; /* Node which has active tag or NULL. */
int leadingSpace; /* For displaying: size of leadingString. */
int *leadingString; /* Malloc'ed leading vertical lines for
* displaying. */
/*
* Miscellaneous information:
*/
char *takeFocus; /* Value of -takefocus option; not used in
* the C code, but used by keyboard traversal
* scripts. Malloc'ed, but may be NULL. */
char *yScrollCmd; /* Command prefix for communicating with
* vertical scrollbar. NULL means no command
* to issue. Malloc'ed. */
char *xScrollCmd; /* Command prefix for communicating with
* horizontal scrollbar. NULL means no command
* to issue. Malloc'ed. */
int flags; /* Various flags; see below for
* definitions. */
} Tree;
/*
* Flag bits for entire tree:
*
* REDRAW_PENDING: Non-zero means a DoWhenIdle handler
* has already been queued to redraw
* this window.
* GOT_FOCUS: Non-zero means this button currently
* has the input focus.
* UPDATE_V_SCROLLBAR: Non-zero means vertical scrollbar needs
* to be updated.
* UPDATE_H_SCROLLBAR: Non-zero means horizontal scrollbar needs
* to be updated.
*/
#define REDRAW_PENDING 1
#define GOT_FOCUS 2
#define UPDATE_V_SCROLLBAR 4
#define UPDATE_H_SCROLLBAR 4
/*
* Information used for parsing configuration specs:
*/
static Ck_ConfigSpec configSpecs[] = {
{CK_CONFIG_ATTR, "-activeattributes", "activeAttributes",
"ActiveAttributes", DEF_TREE_ACTIVE_ATTR_COLOR,
Ck_Offset(Tree, activeAttr), CK_CONFIG_COLOR_ONLY},
{CK_CONFIG_ATTR, "-activeattributes", "activeAttributes",
"ActiveAttributes", DEF_TREE_ACTIVE_ATTR_MONO,
Ck_Offset(Tree, activeAttr), CK_CONFIG_MONO_ONLY},
{CK_CONFIG_ATTR, "-attributes", "attributes", "Attributes",
DEF_TREE_ATTR_COLOR, Ck_Offset(Tree, normalAttr),
CK_CONFIG_COLOR_ONLY},
{CK_CONFIG_ATTR, "-attributes", "attributes", "Attributes",
DEF_TREE_ATTR_MONO, Ck_Offset(Tree, normalAttr),
CK_CONFIG_MONO_ONLY},
{CK_CONFIG_COLOR, "-activebackground", "activeBackground", "Foreground",
DEF_TREE_ACTIVE_BG_COLOR, Ck_Offset(Tree, activeBg),
CK_CONFIG_COLOR_ONLY},
{CK_CONFIG_COLOR, "-activebackground", "activeBackground", "Foreground",
DEF_TREE_ACTIVE_BG_MONO, Ck_Offset(Tree, activeBg),
CK_CONFIG_MONO_ONLY},
{CK_CONFIG_COLOR, "-activeforeground", "activeForeground", "Background",
DEF_TREE_ACTIVE_FG_COLOR, Ck_Offset(Tree, activeFg),
CK_CONFIG_COLOR_ONLY},
{CK_CONFIG_COLOR, "-activeforeground", "activeForeground", "Background",
DEF_TREE_ACTIVE_FG_MONO, Ck_Offset(Tree, activeFg),
CK_CONFIG_MONO_ONLY},
{CK_CONFIG_COLOR, "-background", "background", "Background",
DEF_TREE_BG_COLOR, Ck_Offset(Tree, normalBg),
CK_CONFIG_COLOR_ONLY},
{CK_CONFIG_COLOR, "-background", "background", "Background",
DEF_TREE_BG_MONO, Ck_Offset(Tree, normalBg),
CK_CONFIG_MONO_ONLY},
{CK_CONFIG_SYNONYM, "-bg", "background", (char *) NULL,
(char *) NULL, 0, 0},
{CK_CONFIG_SYNONYM, "-fg", "foreground", (char *) NULL,
(char *) NULL, 0, 0},
{CK_CONFIG_COLOR, "-foreground", "foreground", "Foreground",
DEF_TREE_FG_COLOR, Ck_Offset(Tree, normalFg), CK_CONFIG_COLOR_ONLY},
{CK_CONFIG_COLOR, "-foreground", "foreground", "Foreground",
DEF_TREE_FG_MONO, Ck_Offset(Tree, normalFg), CK_CONFIG_MONO_ONLY},
{CK_CONFIG_COORD, "-height", "height", "Height",
DEF_TREE_HEIGHT, Ck_Offset(Tree, height), 0},
{CK_CONFIG_ATTR, "-selectattributes", "selectAttributes",
"SelectAttributes", DEF_TREE_SELECT_ATTR_COLOR,
Ck_Offset(Tree, selectAttr), CK_CONFIG_COLOR_ONLY},
{CK_CONFIG_ATTR, "-selectattributes", "selectAttributes",
"SelectAttributes", DEF_TREE_SELECT_ATTR_MONO,
Ck_Offset(Tree, selectAttr), CK_CONFIG_MONO_ONLY},
{CK_CONFIG_COLOR, "-selectbackground", "selectBackground", "Foreground",
DEF_TREE_SELECT_BG_COLOR, Ck_Offset(Tree, selectBg),
CK_CONFIG_COLOR_ONLY},
{CK_CONFIG_COLOR, "-selectbackground", "selectBackground", "Foreground",
DEF_TREE_SELECT_BG_MONO, Ck_Offset(Tree, selectBg),
CK_CONFIG_MONO_ONLY},
{CK_CONFIG_COLOR, "-selectforeground", "selectForeground", "Background",
DEF_TREE_SELECT_FG_COLOR, Ck_Offset(Tree, selectFg),
CK_CONFIG_COLOR_ONLY},
{CK_CONFIG_COLOR, "-selectforeground", "selectForeground", "Background",
DEF_TREE_SELECT_FG_MONO, Ck_Offset(Tree, selectFg),
CK_CONFIG_MONO_ONLY},
{CK_CONFIG_STRING, "-takefocus", "takeFocus", "TakeFocus",
DEF_TREE_TAKE_FOCUS, Ck_Offset(Tree, takeFocus),
CK_CONFIG_NULL_OK},
{CK_CONFIG_COORD, "-width", "width", "Width",
DEF_TREE_WIDTH, Ck_Offset(Tree, width), 0},
{CK_CONFIG_STRING, "-xscrollcommand", "xScrollCommand", "ScrollCommand",
DEF_TREE_SCROLL_COMMAND, Ck_Offset(Tree, xScrollCmd),
CK_CONFIG_NULL_OK},
{CK_CONFIG_STRING, "-yscrollcommand", "yScrollCommand", "ScrollCommand",
DEF_TREE_SCROLL_COMMAND, Ck_Offset(Tree, yScrollCmd),
CK_CONFIG_NULL_OK},
{CK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
(char *) NULL, 0, 0}
};
/*
* The structure defined below is used to keep track of a tag search
* in progress. Only the "prevPtr" field should be accessed by anyone
* other than StartTagSearch and NextNode.
*/
typedef struct TagSearch {
Tree *treePtr; /* Tree widget being searched. */
Tcl_HashSearch search; /* Hash search for nodeTable. */
Ck_Uid tag; /* Tag to search for. 0 means return
* all nodes. */
int searchOver; /* Non-zero means NextNode should always
* return NULL. */
} TagSearch;
static Ck_Uid allUid = NULL;
static Ck_Uid hideChildrenUid = NULL;
static Ck_Uid activeUid = NULL;
/*
* Forward declarations for procedures defined later in this file:
*/
static Node * StartTagSearch _ANSI_ARGS_((Tree *treePtr,
char *tag, TagSearch *searchPtr));
static Node * NextNode _ANSI_ARGS_((TagSearch *searchPtr));
static void DoNode _ANSI_ARGS_((Tcl_Interp *interp,
Node *nodePtr, Ck_Uid tag));
static void TreeCmdDeletedProc _ANSI_ARGS_((
ClientData clientData));
static void TreeEventProc _ANSI_ARGS_((ClientData clientData,
CkEvent *eventPtr));
static int TreeWidgetCmd _ANSI_ARGS_((ClientData clientData,
Tcl_Interp *interp, int argc, char **argv));
static int ConfigureTree _ANSI_ARGS_((Tcl_Interp *interp,
Tree *treePtr, int argc, char **argv,
int flags));
static void DestroyTree _ANSI_ARGS_((ClientData clientData));
static void DisplayTree _ANSI_ARGS_((ClientData clientData));
static void TreeEventuallyRedraw _ANSI_ARGS_((Tree *treePtr));
static int FindNodes _ANSI_ARGS_((Tcl_Interp *interp,
Tree *treePtr, int argc, char **argv,
char *newTag, char *cmdName, char *option));
static void DeleteNode _ANSI_ARGS_((Tree *treePtr, Node *nodePtr));
static void RecomputeVisibleNodes _ANSI_ARGS_((Tree *treePtr));
static void ChangeTreeView _ANSI_ARGS_((Tree *treePtr, int index));
static void TreeUpdateVScrollbar _ANSI_ARGS_((Tree *treePtr));
static int GetNodeYCoord _ANSI_ARGS_((Tree *treePtr,
Node *thisPtr, int *yPtr));
/*
*--------------------------------------------------------------
*
* Ck_TreeCmd --
*
* This procedure is invoked to process the "tree"
* Tcl commands. See the user documentation for details
* on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*--------------------------------------------------------------
*/
int
Ck_TreeCmd(clientData, interp, argc, argv)
ClientData clientData; /* Main window associated with
* interpreter. */
Tcl_Interp *interp; /* Current interpreter. */
int argc; /* Number of arguments. */
char **argv; /* Argument strings. */
{
Tree *treePtr;
CkWindow *mainPtr = (CkWindow *) clientData;
CkWindow *new;
allUid = Ck_GetUid("all");
hideChildrenUid = Ck_GetUid("hidechildren");
activeUid = Ck_GetUid("active");
if (argc < 2) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " pathName ?options?\"", (char *) NULL);
return TCL_ERROR;
}
/*
* Create the new window.
*/
new = Ck_CreateWindowFromPath(interp, mainPtr, argv[1], 0);
if (new == NULL) {
return TCL_ERROR;
}
/*
* Initialize the data structure for the button.
*/
treePtr = (Tree *) ckalloc(sizeof (Tree));
treePtr->winPtr = new;
treePtr->interp = interp;
treePtr->widgetCmd = Tcl_CreateCommand(interp, treePtr->winPtr->pathName,
TreeWidgetCmd, (ClientData) treePtr, TreeCmdDeletedProc);
treePtr->idCount = 0;
treePtr->firstChild = treePtr->lastChild = NULL;
Tcl_InitHashTable(&treePtr->nodeTable, TCL_ONE_WORD_KEYS);
treePtr->normalBg = 0;
treePtr->normalFg = 0;
treePtr->normalAttr = 0;
treePtr->activeBg = 0;
treePtr->activeFg = 0;
treePtr->activeAttr = 0;
treePtr->selectBg = 0;
treePtr->selectFg = 0;
treePtr->selectAttr = 0;
treePtr->width = 0;
treePtr->height = 0;
treePtr->visibleNodes = 0;
treePtr->topIndex = 0;
treePtr->topNode = NULL;
treePtr->activeNode = NULL;
treePtr->leadingSpace = 0;
treePtr->leadingString = NULL;
treePtr->takeFocus = NULL;
treePtr->xScrollCmd = NULL;
treePtr->yScrollCmd = NULL;
treePtr->flags = 0;
Ck_SetClass(treePtr->winPtr, "Tree");
Ck_CreateEventHandler(treePtr->winPtr,
CK_EV_EXPOSE | CK_EV_MAP | CK_EV_DESTROY |
CK_EV_FOCUSIN | CK_EV_FOCUSOUT, TreeEventProc, (ClientData) treePtr);
if (ConfigureTree(interp, treePtr, argc-2, argv+2, 0) != TCL_OK) {
Ck_DestroyWindow(treePtr->winPtr);
return TCL_ERROR;
}
Tcl_SetResult(interp, treePtr->winPtr->pathName, TCL_VOLATILE);
return TCL_OK;
}
/*
*--------------------------------------------------------------
*
* TreeWidgetCmd --
*
* This procedure is invoked to process the Tcl command
* that corresponds to a widget managed by this module.
* See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*--------------------------------------------------------------
*/
static int
TreeWidgetCmd(clientData, interp, argc, argv)
ClientData clientData; /* Information about button widget. */
Tcl_Interp *interp; /* Current interpreter. */
int argc; /* Number of arguments. */
char **argv; /* Argument strings. */
{
Tree *treePtr = (Tree *) clientData;
int result = TCL_OK, redraw = 0, recompute = 0;
size_t length;
int c;
if (argc < 2) {
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" option ?arg arg ...?\"", (char *) NULL);
return TCL_ERROR;
}
Ck_Preserve((ClientData) treePtr);
c = argv[1][0];
length = strlen(argv[1]);
if ((c == 'a') && (strncmp(argv[1], "addtag", length) == 0)) {
if (argc < 4) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " addtags tag searchCommand ?arg arg ...?\"",
(char *) NULL);
goto error;
}
result = FindNodes(interp, treePtr, argc-3, argv+3, argv[2], argv[0],
" addtag tag");
} else if ((c == 'c') && (strncmp(argv[1], "cget", length) == 0)
&& (length >= 2)) {
if (argc != 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " cget option\"",
(char *) NULL);
goto error;
}
result = Ck_ConfigureValue(interp, treePtr->winPtr, configSpecs,
(char *) treePtr, argv[2], 0);
} else if ((c == 'c') && (strncmp(argv[1], "children", length) == 0)
&& (length >= 2)) {
Node *nodePtr = NULL;
TagSearch search;
if (argc > 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " children ?tagOrId?\"",
(char *) NULL);
goto error;
}
if (argc > 2) {
nodePtr = StartTagSearch(treePtr, argv[2], &search);
if (nodePtr == NULL)
goto error;
}
if (nodePtr == NULL)
nodePtr = treePtr->firstChild;
else
nodePtr = nodePtr->firstChild;
while (nodePtr != NULL) {
DoNode(interp, nodePtr, (Ck_Uid) NULL);
nodePtr = nodePtr->next;
}
result = TCL_OK;
} else if ((c == 'c') && (strncmp(argv[1], "configure", length) == 0)
&& (length >= 2)) {
if (argc == 2) {
result = Ck_ConfigureInfo(interp, treePtr->winPtr, configSpecs,
(char *) treePtr, (char *) NULL, 0);
} else if (argc == 3) {
result = Ck_ConfigureInfo(interp, treePtr->winPtr, configSpecs,
(char *) treePtr, argv[2], 0);
} else {
result = ConfigureTree(interp, treePtr, argc-2, argv+2,
CK_CONFIG_ARGV_ONLY);
}
} else if ((c == 'd') && (strncmp(argv[1], "delete", length) == 0)
&& (length >= 2)) {
int i;
for (i = 2; i < argc; i++) {
for (;;) {
Node *nodePtr;
TagSearch search;
nodePtr = StartTagSearch(treePtr, argv[i], &search);
if (nodePtr == NULL)
break;
DeleteNode(treePtr, nodePtr);
recompute++;
}
}
if (recompute)
redraw++;
} else if ((c == 'd') && (strncmp(argv[1], "dtag", length) == 0)
&& (length >= 2)) {
Ck_Uid tag;
int i;
Node *nodePtr;
TagSearch search;
if ((argc != 3) && (argc != 4)) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " dtag tagOrId ?tagToDelete?\"",
(char *) NULL);
goto error;
}
if (argc == 4) {
tag = Ck_GetUid(argv[3]);
} else {
tag = Ck_GetUid(argv[2]);
}
for (nodePtr = StartTagSearch(treePtr, argv[2], &search);
nodePtr != NULL; nodePtr = NextNode(&search)) {
for (i = nodePtr->numTags-1; i >= 0; i--) {
if (nodePtr->tagPtr[i] == tag) {
nodePtr->tagPtr[i] = nodePtr->tagPtr[nodePtr->numTags-1];
nodePtr->numTags--;
if (tag == activeUid)
redraw++;
else if (tag == hideChildrenUid) {
if (!(nodePtr->flags & SHOWCHILDREN)) {
nodePtr->flags |= SHOWCHILDREN;
recompute++;
redraw++;
}
}
}
}
}
} else if ((c == 'f') && (strncmp(argv[1], "find", length) == 0)
&& (length >= 2)) {
if (argc < 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " find searchCommand ?arg arg ...?\"",
(char *) NULL);
goto error;
}
result = FindNodes(interp, treePtr, argc - 2, argv + 2, (char *) NULL,
argv[0], " find");
} else if ((c == 'g') && (strncmp(argv[1], "gettags", length) == 0)) {
Node *nodePtr;
TagSearch search;
if (argc != 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " gettags tagOrId\"", (char *) NULL);
goto error;
}
nodePtr = StartTagSearch(treePtr, argv[2], &search);
if (nodePtr != NULL) {
int i;
for (i = 0; i < nodePtr->numTags; i++) {
Tcl_AppendElement(interp, (char *) nodePtr->tagPtr[i]);
}
}
} else if ((c == 'i') && (strncmp(argv[1], "insert", length) == 0)) {
int optargc = 2;
long id;
Node *nodePtr = NULL, *new;
char *end;
if (argc < 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " insert ?id? ?option value ...?\"",
(char *) NULL);
goto error;
}
id = strtoul(argv[2], &end, 0);
if (*end == 0) {
if (end != argv[2]) {
Tcl_HashEntry *hPtr;
hPtr = Tcl_FindHashEntry(&treePtr->nodeTable, (char *) id);
if (hPtr == NULL) {
Tcl_AppendResult(interp, "no node with id \"", argv[2],
"\"", (char *) NULL);
goto error;
}
nodePtr = (Node *) Tcl_GetHashValue(hPtr);
}
optargc = 3;
}
new = (Node *) ckalloc (sizeof (Node));
new->id = treePtr->idCount++;
new->level = nodePtr == NULL ? 0 : nodePtr->level + 1;
new->tree = treePtr;
new->parent = nodePtr;
new->next = NULL;
new->firstChild = new->lastChild = NULL;
new->tagPtr = new->staticTagSpace;
new->tagSpace = TAG_SPACE;
new->numTags = 0;
new->fg = new->bg = new->attr = -1;
new->text = NULL;
new->textWidth = 0;
new->flags = SHOWCHILDREN;
if (new->level * 2 > treePtr->leadingSpace) {
int *newString;
treePtr->leadingSpace = new->level * 8;
newString = (int *) ckalloc(treePtr->leadingSpace * sizeof (int));
if (treePtr->leadingString != NULL)
ckfree((char *) treePtr->leadingString);
treePtr->leadingString = newString;
}
result = Ck_ConfigureWidget(interp, treePtr->winPtr,
nodeConfigSpecs, argc - optargc, &argv[optargc],
(char *) new, CK_CONFIG_ARGV_ONLY);
if (result == TCL_OK) {
Tcl_HashEntry *hPtr;
int newHash;
char buf[32];
hPtr = Tcl_CreateHashEntry(&treePtr->nodeTable,
(char *) new->id, &newHash);
Tcl_SetHashValue(hPtr, (ClientData) new);
if (new->parent == NULL) {
new->level = 0;
if (treePtr->lastChild == NULL)
treePtr->firstChild = new;
else
treePtr->lastChild->next = new;
treePtr->lastChild = new;
} else {
new->level = nodePtr->level + 1;
if (nodePtr->lastChild == NULL)
nodePtr->firstChild = new;
else
nodePtr->lastChild->next = new;
nodePtr->lastChild = new;
}
recompute++;
redraw++;
sprintf(buf, "%ld", new->id);
Tcl_AppendResult(interp, buf, (char *) NULL);
} else {
ckfree((char *) new);
}
} else if ((c == 'n') && (strncmp(argv[1], "nodecget", length) == 0)
&& (length >= 6)) {
Node *nodePtr;
TagSearch search;
if (argc != 4) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " nodecget tagOrId option\"",
(char *) NULL);
return TCL_ERROR;
}
nodePtr = StartTagSearch(treePtr, argv[2], &search);
if (nodePtr != NULL) {
result = Ck_ConfigureValue(treePtr->interp, treePtr->winPtr,
nodeConfigSpecs, (char *) nodePtr,
argv[3], 0);
}
} else if ((c == 'n') && (strncmp(argv[1], "nodeconfigure", length) == 0)
&& (length >= 6)) {
Node *nodePtr;
TagSearch search;
if (argc < 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " nodeconfigure tagOrId ?option value ...?\"",
(char *) NULL);
goto error;
}
for (nodePtr = StartTagSearch(treePtr, argv[2], &search);
nodePtr != NULL; nodePtr = NextNode(&search)) {
if (argc == 3) {
result = Ck_ConfigureInfo(treePtr->interp, treePtr->winPtr,
nodeConfigSpecs, (char *) nodePtr,
(char *) NULL, 0);
} else if (argc == 4) {
result = Ck_ConfigureInfo(treePtr->interp, treePtr->winPtr,
nodeConfigSpecs, (char *) nodePtr,
argv[3], 0);
} else {
result = Ck_ConfigureWidget(interp, treePtr->winPtr,
nodeConfigSpecs, argc - 3, &argv[3],
(char *) nodePtr, CK_CONFIG_ARGV_ONLY);
redraw++;
}
if ((result != TCL_OK) || (argc < 5)) {
break;
}
}
} else if ((c == 'p') && (strncmp(argv[1], "parent", length) == 0)
&& (length >= 2)) {
Node *nodePtr;
TagSearch search;
if (argc != 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " parent tagOrId\"",
(char *) NULL);
goto error;
}
nodePtr = StartTagSearch(treePtr, argv[2], &search);
if (nodePtr == NULL)
goto error;
if (nodePtr->parent != NULL)
DoNode(interp, nodePtr->parent, (Ck_Uid) NULL);
result = TCL_OK;
} else if ((c == 's') && (strncmp(argv[1], "select", length) == 0)
&& (length >= 2)) {
Node *nodePtr;
TagSearch search;
if (argc != 4) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " select option tagOrId\"", (char *) NULL);
goto error;
}
nodePtr = StartTagSearch(treePtr, argv[3], &search);
if (nodePtr == NULL) {
Tcl_AppendResult(interp, "can't find a selectable node \"",
argv[3], "\"", (char *) NULL);
goto error;
}
length = strlen(argv[2]);
c = argv[2][0];
if ((c == 'c') && (argv[2] != NULL) &&
(strncmp(argv[2], "clear", length) == 0)) {
do {
nodePtr->flags &= ~SELECTED;
nodePtr = NextNode(&search);
redraw++;
} while (nodePtr != NULL);
} else if ((c == 'i') && (strncmp(argv[2], "includes", length) == 0)) {
Tcl_SetResult(interp, (nodePtr->flags & SELECTED) ? "1" : "0", TCL_STATIC);
} else if ((c == 's') && (strncmp(argv[2], "set", length) == 0)) {
do {
nodePtr->flags |= SELECTED;
nodePtr = NextNode(&search);
redraw++;
} while (nodePtr != NULL);
} else {
Tcl_AppendResult(interp, "bad select option \"", argv[2],
"\": must be clear, includes, or set", (char *) NULL);
goto error;
}
} else if ((c == 'x') && (strncmp(argv[1], "xview", length) == 0)) {
int type, count;
double fraction;
if (argc == 2) {
} else {
type = Ck_GetScrollInfo(interp, argc, argv, &fraction, &count);
switch (type) {
case CK_SCROLL_ERROR:
goto error;
case CK_SCROLL_MOVETO:
break;
case CK_SCROLL_PAGES:
break;
case CK_SCROLL_UNITS:
break;
}
}
} else if ((c == 'y') && (strncmp(argv[1], "yview", length) == 0)) {
int type, count, index = 0;
double fraction;
if (argc == 2) {
if (treePtr->visibleNodes == 0) {
Tcl_SetResult(interp, "0 1", TCL_STATIC);
} else {
double fraction2;
char resultbuf[TCL_RESULT_SIZE];
fraction = treePtr->topIndex / (double) treePtr->visibleNodes;
fraction2 = (treePtr->topIndex + treePtr->winPtr->height) /
(double) treePtr->visibleNodes;
if (fraction2 > 1.0) {
fraction2 = 1.0;
}
snprintf(resultbuf, sizeof(resultbuf), "%g %g", fraction, fraction2);
Tcl_SetResult(interp, resultbuf, TCL_VOLATILE);
}
} else if (argc == 3) {
Node *nodePtr;
TagSearch search;
nodePtr = StartTagSearch(treePtr, argv[2], &search);
if (nodePtr == NULL) {
Tcl_AppendResult(interp, "can't find a selectable node \"",
argv[3], "\"", (char *) NULL);
goto error;
}
if (GetNodeYCoord(treePtr, nodePtr, &index) == TCL_OK) {
if (index < treePtr->topIndex ||
index >= treePtr->topIndex + treePtr->winPtr->height)
ChangeTreeView(treePtr,
index - treePtr->winPtr->height / 2);
}
} else {
type = Ck_GetScrollInfo(interp, argc, argv, &fraction, &count);
switch (type) {
case CK_SCROLL_ERROR:
goto error;
case CK_SCROLL_MOVETO:
index = (int) (treePtr->visibleNodes * fraction + 0.5);
break;
case CK_SCROLL_PAGES:
if (treePtr->visibleNodes > 2) {
index = treePtr->topIndex
+ count * (treePtr->winPtr->height - 2);
} else {
index = treePtr->topIndex + count;
}
break;
case CK_SCROLL_UNITS:
index = treePtr->topIndex + count;
break;
}
ChangeTreeView(treePtr, index);
}
} else {
Tcl_AppendResult(interp, "bad option \"", argv[1],
"\": must be cget or configure",
(char *) NULL);
goto error;
}
if (recompute)
RecomputeVisibleNodes(treePtr);
if (redraw)
TreeEventuallyRedraw(treePtr);
Ck_Release((ClientData) treePtr);
return result;
error:
Ck_Release((ClientData) treePtr);
return TCL_ERROR;
}
/*
*----------------------------------------------------------------------
*
* DestroyTree --
*
* This procedure is invoked to recycle all of the resources
* associated with a tree widget. It is invoked as a
* when-idle handler in order to make sure that there is no
* other use of the tree pending at the time of the deletion.
*
* Results:
* None.
*
* Side effects:
* Everything associated with the widget is freed up.
*
*----------------------------------------------------------------------
*/
static void
DestroyTree(clientData)
ClientData clientData; /* Info about tree widget. */
{
Tree *treePtr = (Tree *) clientData;
Tcl_HashEntry *hPtr;
Tcl_HashSearch search;
Node *nodePtr;
/*
* Free up all the stuff that requires special handling, then
* let Ck_FreeOptions handle all the standard option-related
* stuff.
*/
if (treePtr->leadingString != NULL) {
ckfree((char *) treePtr->leadingString);
treePtr->leadingString = NULL;
}
hPtr = Tcl_FirstHashEntry(&treePtr->nodeTable, &search);
while (hPtr != NULL) {
nodePtr = (Node *) Tcl_GetHashValue(hPtr);
Ck_FreeOptions(nodeConfigSpecs, (char *) nodePtr, 0);
if (nodePtr->tagPtr != nodePtr->staticTagSpace)
ckfree((char *) nodePtr->tagPtr);
ckfree((char *) nodePtr);
hPtr = Tcl_NextHashEntry(&search);
}
Tcl_DeleteHashTable(&treePtr->nodeTable);
Ck_FreeOptions(configSpecs, (char *) treePtr, 0);
ckfree((char *) treePtr);
}
/*
*----------------------------------------------------------------------
*
* ConfigureTree --
*
* This procedure is called to process an argv/argc list, plus
* the option database, in order to configure (or
* reconfigure) a tree widget.
*
* Results:
* The return value is a standard Tcl result. If TCL_ERROR is
* returned, then interp->result contains an error message.
*
* Side effects:
* Configuration information, such as text string, colors, font,
* etc. get set for treePtr; old resources get freed, if there
* were any. The tree is redisplayed.
*
*----------------------------------------------------------------------
*/
static int
ConfigureTree(interp, treePtr, argc, argv, flags)
Tcl_Interp *interp; /* Used for error reporting. */
Tree *treePtr; /* Information about widget; may or may
* not already have values for some fields. */
int argc; /* Number of valid entries in argv. */
char **argv; /* Arguments. */
int flags; /* Flags to pass to Ck_ConfigureWidget. */
{
int result, width, height;
result = Ck_ConfigureWidget(interp, treePtr->winPtr, configSpecs,
argc, argv, (char *) treePtr, flags);
if (result != TCL_OK)
return TCL_ERROR;
width = treePtr->width;
if (width <= 0)
width = 1;
height = treePtr->height;
if (height <= 0)
height = 1;
Ck_GeometryRequest(treePtr->winPtr, width, height);
TreeEventuallyRedraw(treePtr);
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* TreeEventuallyRedraw --
*
* This procedure is called to dispatch a do-when-idle
* handler for redrawing the tree.
*