-
Notifications
You must be signed in to change notification settings - Fork 1
/
ckText.c
1587 lines (1466 loc) · 45.4 KB
/
ckText.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
/*
* ckText.c --
*
* This module provides a big chunk of the implementation of
* multi-line editable text widgets for ck. Among other things,
* it provides the Tcl command interfaces to text widgets and
* the display code. The B-tree representation of text is
* implemented elsewhere.
*
* Copyright (c) 1992-1994 The Regents of the University of California.
* Copyright (c) 1994-1995 Sun Microsystems, Inc.
* Copyright (c) 1995 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 "ckText.h"
#include "default.h"
/*
* Information used to parse text configuration options:
*/
static Ck_ConfigSpec configSpecs[] = {
{CK_CONFIG_ATTR, "-attributes", "attributes", "Attributes",
DEF_TEXT_ATTR, Ck_Offset(CkText, attr), 0},
{CK_CONFIG_COLOR, "-background", "background", "Background",
DEF_TEXT_BG_COLOR, Ck_Offset(CkText, bg), CK_CONFIG_COLOR_ONLY},
{CK_CONFIG_COLOR, "-background", "background", "Background",
DEF_TEXT_BG_MONO, Ck_Offset(CkText, bg), 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_TEXT_FG, Ck_Offset(CkText, fg), 0},
{CK_CONFIG_COORD, "-height", "height", "Height",
DEF_TEXT_HEIGHT, Ck_Offset(CkText, height), 0},
{CK_CONFIG_ATTR, "-selectattributes", "selectAttributes",
"SelectAttributes", DEF_TEXT_SELECT_ATTR_COLOR,
Ck_Offset(CkText, selAttr), CK_CONFIG_COLOR_ONLY},
{CK_CONFIG_ATTR, "-selectattributes", "selectAttributes",
"SelectAttributes", DEF_TEXT_SELECT_ATTR_MONO,
Ck_Offset(CkText, selAttr), CK_CONFIG_MONO_ONLY},
{CK_CONFIG_COLOR, "-selectbackground", "selectBackground", "Foreground",
DEF_TEXT_SELECT_BG_COLOR, Ck_Offset(CkText, selBg),
CK_CONFIG_COLOR_ONLY},
{CK_CONFIG_COLOR, "-selectbackground", "selectBackground", "Foreground",
DEF_TEXT_SELECT_BG_MONO, Ck_Offset(CkText, selBg),
CK_CONFIG_MONO_ONLY},
{CK_CONFIG_COLOR, "-selectforeground", "selectForeground", "Background",
DEF_TEXT_SELECT_FG_COLOR, Ck_Offset(CkText, selFg),
CK_CONFIG_COLOR_ONLY},
{CK_CONFIG_COLOR, "-selectforeground", "selectForeground", "Background",
DEF_TEXT_SELECT_FG_MONO, Ck_Offset(CkText, selFg),
CK_CONFIG_MONO_ONLY},
{CK_CONFIG_UID, "-state", "state", "State",
DEF_TEXT_STATE, Ck_Offset(CkText, state), 0},
{CK_CONFIG_STRING, "-tabs", "tabs", "Tabs",
DEF_TEXT_TABS, Ck_Offset(CkText, tabOptionString), CK_CONFIG_NULL_OK},
{CK_CONFIG_STRING, "-takefocus", "takeFocus", "TakeFocus",
DEF_TEXT_TAKE_FOCUS, Ck_Offset(CkText, takeFocus),
CK_CONFIG_NULL_OK},
{CK_CONFIG_COORD, "-width", "width", "Width",
DEF_TEXT_WIDTH, Ck_Offset(CkText, width), 0},
{CK_CONFIG_UID, "-wrap", "wrap", "Wrap",
DEF_TEXT_WRAP, Ck_Offset(CkText, wrapMode), 0},
{CK_CONFIG_STRING, "-xscrollcommand", "xScrollCommand", "ScrollCommand",
DEF_TEXT_XSCROLL_COMMAND, Ck_Offset(CkText, xScrollCmd),
CK_CONFIG_NULL_OK},
{CK_CONFIG_STRING, "-yscrollcommand", "yScrollCommand", "ScrollCommand",
DEF_TEXT_YSCROLL_COMMAND, Ck_Offset(CkText, yScrollCmd),
CK_CONFIG_NULL_OK},
{CK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
(char *) NULL, 0, 0}
};
/*
* Ck_Uid's used to represent text states:
*/
Ck_Uid ckTextCharUid = NULL;
Ck_Uid ckTextDisabledUid = NULL;
Ck_Uid ckTextNoneUid = NULL;
Ck_Uid ckTextNormalUid = NULL;
Ck_Uid ckTextWordUid = NULL;
/*
* Boolean variable indicating whether or not special debugging code
* should be executed.
*/
int ckTextDebug = 0;
/*
* Forward declarations for procedures defined later in this file:
*/
static int ConfigureText _ANSI_ARGS_((Tcl_Interp *interp,
CkText *textPtr, int argc, char **argv, int flags));
static int DeleteChars _ANSI_ARGS_((CkText *textPtr,
char *index1String, char *index2String));
static void DestroyText _ANSI_ARGS_((ClientData clientData));
static void InsertChars _ANSI_ARGS_((CkText *textPtr,
CkTextIndex *indexPtr, char *string));
static void TextCmdDeletedProc _ANSI_ARGS_((
ClientData clientData));
static void TextEventProc _ANSI_ARGS_((ClientData clientData,
CkEvent *eventPtr));
static int TextSearchCmd _ANSI_ARGS_((CkText *textPtr,
Tcl_Interp *interp, int argc, char **argv));
static int TextWidgetCmd _ANSI_ARGS_((ClientData clientData,
Tcl_Interp *interp, int argc, char **argv));
/*
*--------------------------------------------------------------
*
* Ck_TextCmd --
*
* This procedure is invoked to process the "text" Tcl command.
* See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*--------------------------------------------------------------
*/
int
Ck_TextCmd(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. */
{
CkWindow *mainPtr = (CkWindow *) clientData;
CkWindow *new;
register CkText *textPtr;
CkTextIndex startIndex;
if (argc < 2) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " pathName ?options?\"", (char *) NULL);
return TCL_ERROR;
}
/*
* Perform once-only initialization:
*/
if (ckTextNormalUid == NULL) {
ckTextCharUid = Ck_GetUid("char");
ckTextDisabledUid = Ck_GetUid("disabled");
ckTextNoneUid = Ck_GetUid("none");
ckTextNormalUid = Ck_GetUid("normal");
ckTextWordUid = Ck_GetUid("word");
}
/*
* Create the window.
*/
new = Ck_CreateWindowFromPath(interp, mainPtr, argv[1], 0);
if (new == NULL) {
return TCL_ERROR;
}
textPtr = (CkText *) ckalloc(sizeof(CkText));
textPtr->winPtr = new;
textPtr->interp = interp;
textPtr->widgetCmd = Tcl_CreateCommand(interp,
new->pathName, TextWidgetCmd, (ClientData) textPtr,
TextCmdDeletedProc);
textPtr->tree = CkBTreeCreate();
Tcl_InitHashTable(&textPtr->tagTable, TCL_STRING_KEYS);
textPtr->numTags = 0;
Tcl_InitHashTable(&textPtr->markTable, TCL_STRING_KEYS);
Tcl_InitHashTable(&textPtr->windowTable, TCL_STRING_KEYS);
textPtr->state = ckTextNormalUid;
textPtr->bg = 0;
textPtr->fg = 0;
textPtr->attr = 0;
textPtr->tabOptionString = NULL;
textPtr->tabArrayPtr = NULL;
textPtr->wrapMode = ckTextCharUid;
textPtr->width = 0;
textPtr->height = 0;
textPtr->prevWidth = new->width;
textPtr->prevHeight = new->height;
CkTextCreateDInfo(textPtr);
#if CK_USE_UTF
CkTextMakeByteIndex(textPtr->tree, 0, 0, &startIndex);
#else
CkTextMakeIndex(textPtr->tree, 0, 0, &startIndex);
#endif
CkTextSetYView(textPtr, &startIndex, 0);
textPtr->selTagPtr = NULL;
textPtr->selBg = 0;
textPtr->selFg = 0;
textPtr->selAttr = 0;
textPtr->abortSelections = 0;
textPtr->insertMarkPtr = NULL;
textPtr->bindingTable = NULL;
textPtr->currentMarkPtr = NULL;
textPtr->pickEvent.type = -1;
textPtr->numCurTags = 0;
textPtr->curTagArrayPtr = NULL;
textPtr->takeFocus = NULL;
textPtr->xScrollCmd = NULL;
textPtr->yScrollCmd = NULL;
textPtr->flags = 0;
/*
* Create the "sel" tag and the "current" and "insert" marks.
*/
textPtr->selTagPtr = CkTextCreateTag(textPtr, "sel");
textPtr->currentMarkPtr = CkTextSetMark(textPtr, "current", &startIndex);
textPtr->insertMarkPtr = CkTextSetMark(textPtr, "insert", &startIndex);
Ck_SetClass(new, "Text");
Ck_CreateEventHandler(textPtr->winPtr,
CK_EV_EXPOSE | CK_EV_DESTROY | CK_EV_MAP | CK_EV_FOCUSIN |
CK_EV_FOCUSOUT,
TextEventProc, (ClientData) textPtr);
Ck_CreateEventHandler(textPtr->winPtr, CK_EV_KEYPRESS,
CkTextBindProc, (ClientData) textPtr);
if (ConfigureText(interp, textPtr, argc-2, argv+2, 0) != TCL_OK) {
Ck_DestroyWindow(textPtr->winPtr);
return TCL_ERROR;
}
Tcl_SetResult(interp, textPtr->winPtr->pathName, TCL_VOLATILE);
return TCL_OK;
}
/*
*--------------------------------------------------------------
*
* TextWidgetCmd --
*
* This procedure is invoked to process the Tcl command
* that corresponds to a text widget. See the user
* documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*--------------------------------------------------------------
*/
static int
TextWidgetCmd(clientData, interp, argc, argv)
ClientData clientData; /* Information about text widget. */
Tcl_Interp *interp; /* Current interpreter. */
int argc; /* Number of arguments. */
char **argv; /* Argument strings. */
{
register CkText *textPtr = (CkText *) clientData;
int result = TCL_OK;
size_t length;
int c;
CkTextIndex index1, index2;
if (argc < 2) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " option ?arg arg ...?\"", (char *) NULL);
return TCL_ERROR;
}
Ck_Preserve((ClientData) textPtr);
c = argv[1][0];
length = strlen(argv[1]);
if ((c == 'b') && (strncmp(argv[1], "bbox", length) == 0)) {
int x, y, width, height;
if (argc != 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " bbox index\"", (char *) NULL);
result = TCL_ERROR;
goto done;
}
if (CkTextGetIndex(interp, textPtr, argv[2], &index1) != TCL_OK) {
result = TCL_ERROR;
goto done;
}
if (CkTextCharBbox(textPtr, &index1, &x, &y, &width, &height) == 0) {
char resultbuf[64];
snprintf(resultbuf, sizeof(resultbuf),
"%d %d %d %d", x, y, width, height);
Tcl_SetResult(interp, resultbuf, TCL_VOLATILE);
}
} 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);
result = TCL_ERROR;
goto done;
}
result = Ck_ConfigureValue(interp, textPtr->winPtr, configSpecs,
(char *) textPtr, argv[2], 0);
} else if ((c == 'c') && (strncmp(argv[1], "compare", length) == 0)
&& (length >= 3)) {
int relation, value;
char *p;
if (argc != 5) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " compare index1 op index2\"", (char *) NULL);
result = TCL_ERROR;
goto done;
}
if ((CkTextGetIndex(interp, textPtr, argv[2], &index1) != TCL_OK)
|| (CkTextGetIndex(interp, textPtr, argv[4], &index2)
!= TCL_OK)) {
result = TCL_ERROR;
goto done;
}
relation = CkTextIndexCmp(&index1, &index2);
p = argv[3];
if (p[0] == '<') {
value = (relation < 0);
if ((p[1] == '=') && (p[2] == 0)) {
value = (relation <= 0);
} else if (p[1] != 0) {
compareError:
Tcl_AppendResult(interp, "bad comparison operator \"",
argv[3], "\": must be <, <=, ==, >=, >, or !=",
(char *) NULL);
result = TCL_ERROR;
goto done;
}
} else if (p[0] == '>') {
value = (relation > 0);
if ((p[1] == '=') && (p[2] == 0)) {
value = (relation >= 0);
} else if (p[1] != 0) {
goto compareError;
}
} else if ((p[0] == '=') && (p[1] == '=') && (p[2] == 0)) {
value = (relation == 0);
} else if ((p[0] == '!') && (p[1] == '=') && (p[2] == 0)) {
value = (relation != 0);
} else {
goto compareError;
}
Tcl_SetResult(interp, (value) ? "1" : "0", TCL_STATIC);
} else if ((c == 'c') && (strncmp(argv[1], "configure", length) == 0)
&& (length >= 3)) {
if (argc == 2) {
result = Ck_ConfigureInfo(interp, textPtr->winPtr, configSpecs,
(char *) textPtr, (char *) NULL, 0);
} else if (argc == 3) {
result = Ck_ConfigureInfo(interp, textPtr->winPtr, configSpecs,
(char *) textPtr, argv[2], 0);
} else {
result = ConfigureText(interp, textPtr, argc-2, argv+2,
CK_CONFIG_ARGV_ONLY);
}
} else if ((c == 'd') && (strncmp(argv[1], "debug", length) == 0)
&& (length >= 3)) {
if (argc > 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " debug boolean\"", (char *) NULL);
result = TCL_ERROR;
goto done;
}
if (argc == 2) {
Tcl_SetResult(interp, (ckBTreeDebug) ? "1" : "0", TCL_STATIC);
} else {
if (Tcl_GetBoolean(interp, argv[2], &ckBTreeDebug) != TCL_OK) {
result = TCL_ERROR;
goto done;
}
ckTextDebug = ckBTreeDebug;
}
} else if ((c == 'd') && (strncmp(argv[1], "delete", length) == 0)
&& (length >= 3)) {
if ((argc != 3) && (argc != 4)) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " delete index1 ?index2?\"", (char *) NULL);
result = TCL_ERROR;
goto done;
}
if (textPtr->state == ckTextNormalUid) {
result = DeleteChars(textPtr, argv[2],
(argc == 4) ? argv[3] : (char *) NULL);
}
} else if ((c == 'd') && (strncmp(argv[1], "dlineinfo", length) == 0)
&& (length >= 2)) {
int x, y, width, height, base;
if (argc != 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " dlineinfo index\"", (char *) NULL);
result = TCL_ERROR;
goto done;
}
if (CkTextGetIndex(interp, textPtr, argv[2], &index1) != TCL_OK) {
result = TCL_ERROR;
goto done;
}
if (CkTextDLineInfo(textPtr, &index1, &x, &y, &width, &height, &base)
== 0) {
char resultbuf[64];
snprintf(resultbuf, sizeof(resultbuf),
"%d %d %d %d %d", x, y, width,
height, base);
Tcl_SetResult(interp, resultbuf, TCL_VOLATILE);
}
} else if ((c == 'g') && (strncmp(argv[1], "get", length) == 0)) {
if ((argc != 3) && (argc != 4)) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " get index1 ?index2?\"", (char *) NULL);
result = TCL_ERROR;
goto done;
}
if (CkTextGetIndex(interp, textPtr, argv[2], &index1) != TCL_OK) {
result = TCL_ERROR;
goto done;
}
if (argc == 3) {
index2 = index1;
CkTextIndexForwChars(&index2, 1, &index2);
} else if (CkTextGetIndex(interp, textPtr, argv[3], &index2)
!= TCL_OK) {
result = TCL_ERROR;
goto done;
}
if (CkTextIndexCmp(&index1, &index2) >= 0) {
goto done;
}
while (1) {
int offset, last, savedChar;
CkTextSegment *segPtr;
segPtr = CkTextIndexToSeg(&index1, &offset);
last = segPtr->size;
if (index1.linePtr == index2.linePtr) {
int last2;
if (index2.charIndex == index1.charIndex) {
break;
}
last2 = index2.charIndex - index1.charIndex + offset;
if (last2 < last) {
last = last2;
}
}
if (segPtr->typePtr == &ckTextCharType) {
savedChar = segPtr->body.chars[last];
segPtr->body.chars[last] = 0;
Tcl_AppendResult(interp, segPtr->body.chars + offset,
(char *) NULL);
segPtr->body.chars[last] = savedChar;
}
CkTextIndexForwChars(&index1, last-offset, &index1);
}
} else if ((c == 'i') && (strncmp(argv[1], "index", length) == 0)
&& (length >= 3)) {
if (argc != 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " index index\"",
(char *) NULL);
result = TCL_ERROR;
goto done;
}
if (CkTextGetIndex(interp, textPtr, argv[2], &index1) != TCL_OK) {
result = TCL_ERROR;
goto done;
}
CkTextPrintIndex(&index1, Tcl_GetStringResult(interp));
} else if ((c == 'i') && (strncmp(argv[1], "insert", length) == 0)
&& (length >= 3)) {
int i, j, numTags;
char **tagNames;
CkTextTag **oldTagArrayPtr;
if (argc < 4) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0],
" insert index chars ?tagList chars tagList ...?\"",
(char *) NULL);
result = TCL_ERROR;
goto done;
}
if (CkTextGetIndex(interp, textPtr, argv[2], &index1) != TCL_OK) {
result = TCL_ERROR;
goto done;
}
if (textPtr->state == ckTextNormalUid) {
for (j = 3; j < argc; j += 2) {
InsertChars(textPtr, &index1, argv[j]);
if (argc > (j+1)) {
CkTextIndexForwChars(&index1, (int) strlen(argv[j]),
&index2);
oldTagArrayPtr = CkBTreeGetTags(&index1, &numTags);
if (oldTagArrayPtr != NULL) {
for (i = 0; i < numTags; i++) {
CkBTreeTag(&index1, &index2, oldTagArrayPtr[i], 0);
}
ckfree((char *) oldTagArrayPtr);
}
if (Tcl_SplitList(interp, argv[j+1], &numTags, &tagNames)
!= TCL_OK) {
result = TCL_ERROR;
goto done;
}
for (i = 0; i < numTags; i++) {
CkBTreeTag(&index1, &index2,
CkTextCreateTag(textPtr, tagNames[i]), 1);
}
ckfree((char *) tagNames);
index1 = index2;
}
}
}
} else if ((c == 'm') && (strncmp(argv[1], "mark", length) == 0)) {
result = CkTextMarkCmd(textPtr, interp, argc, argv);
} else if ((c == 's') && (strcmp(argv[1], "search") == 0)
&& (length >= 3)) {
result = TextSearchCmd(textPtr, interp, argc, argv);
} else if ((c == 's') && (strcmp(argv[1], "see") == 0) && (length >= 3)) {
result = CkTextSeeCmd(textPtr, interp, argc, argv);
} else if ((c == 't') && (strcmp(argv[1], "tag") == 0)) {
result = CkTextTagCmd(textPtr, interp, argc, argv);
#if 0
} else if ((c == 'w') && (strncmp(argv[1], "window", length) == 0)) {
result = CkTextWindowCmd(textPtr, interp, argc, argv);
#endif
} else if ((c == 'x') && (strncmp(argv[1], "xview", length) == 0)) {
result = CkTextXviewCmd(textPtr, interp, argc, argv);
} else if ((c == 'y') && (strncmp(argv[1], "yview", length) == 0)
&& (length >= 2)) {
result = CkTextYviewCmd(textPtr, interp, argc, argv);
} else {
Tcl_AppendResult(interp, "bad option \"", argv[1],
"\": must be bbox, cget, compare, configure, debug, delete, ",
"dlineinfo, get, index, insert, mark, scan, search, see, ",
"tag, window, xview, or yview",
(char *) NULL);
result = TCL_ERROR;
}
done:
Ck_Release((ClientData) textPtr);
return result;
}
/*
*----------------------------------------------------------------------
*
* DestroyText --
*
* This procedure is invoked by Ck_EventuallyFree or Ck_Release
* to clean up the internal structure of a text at a safe time
* (when no-one is using it anymore).
*
* Results:
* None.
*
* Side effects:
* Everything associated with the text is freed up.
*
*----------------------------------------------------------------------
*/
static void
DestroyText(clientData)
ClientData clientData; /* Info about text widget. */
{
register CkText *textPtr = (CkText *) clientData;
Tcl_HashSearch search;
Tcl_HashEntry *hPtr;
CkTextTag *tagPtr;
/*
* Free up all the stuff that requires special handling, then
* let Ck_FreeOptions handle all the standard option-related
* stuff. Special note: free up display-related information
* before deleting the B-tree, since display-related stuff
* may refer to stuff in the B-tree.
*/
CkTextFreeDInfo(textPtr);
CkBTreeDestroy(textPtr->tree);
for (hPtr = Tcl_FirstHashEntry(&textPtr->tagTable, &search);
hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
tagPtr = (CkTextTag *) Tcl_GetHashValue(hPtr);
CkTextFreeTag(textPtr, tagPtr);
}
Tcl_DeleteHashTable(&textPtr->tagTable);
for (hPtr = Tcl_FirstHashEntry(&textPtr->markTable, &search);
hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
ckfree((char *) Tcl_GetHashValue(hPtr));
}
Tcl_DeleteHashTable(&textPtr->markTable);
if (textPtr->tabArrayPtr != NULL) {
ckfree((char *) textPtr->tabArrayPtr);
}
if (textPtr->bindingTable != NULL) {
Ck_DeleteBindingTable(textPtr->bindingTable);
}
/*
* NOTE: do NOT free up selBorder, selBdString, or selFgColorPtr:
* they are duplicates of information in the "sel" tag, which was
* freed up as part of deleting the tags above.
*/
Ck_FreeOptions(configSpecs, (char *) textPtr, 0);
ckfree((char *) textPtr);
}
/*
*----------------------------------------------------------------------
*
* ConfigureText --
*
* This procedure is called to process an argv/argc list, plus
* the Ck option database, in order to configure (or
* reconfigure) a text 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 textPtr; old resources get freed, if there
* were any.
*
*----------------------------------------------------------------------
*/
static int
ConfigureText(interp, textPtr, argc, argv, flags)
Tcl_Interp *interp; /* Used for error reporting. */
register CkText *textPtr; /* 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. */
{
if (Ck_ConfigureWidget(interp, textPtr->winPtr, configSpecs,
argc, argv, (char *) textPtr, flags) != TCL_OK) {
return TCL_ERROR;
}
/*
* A few other options also need special processing, such as parsing
* the geometry and setting the background from a 3-D border.
*/
if ((textPtr->state != ckTextNormalUid)
&& (textPtr->state != ckTextDisabledUid)) {
Tcl_AppendResult(interp, "bad state value \"", textPtr->state,
"\": must be normal or disabled", (char *) NULL);
textPtr->state = ckTextNormalUid;
return TCL_ERROR;
}
if ((textPtr->wrapMode != ckTextCharUid)
&& (textPtr->wrapMode != ckTextNoneUid)
&& (textPtr->wrapMode != ckTextWordUid)) {
Tcl_AppendResult(interp, "bad wrap mode \"", textPtr->wrapMode,
"\": must be char, none, or word", (char *) NULL);
textPtr->wrapMode = ckTextCharUid;
return TCL_ERROR;
}
/*
* Parse tab stops.
*/
if (textPtr->tabArrayPtr != NULL) {
ckfree((char *) textPtr->tabArrayPtr);
textPtr->tabArrayPtr = NULL;
}
if (textPtr->tabOptionString != NULL) {
textPtr->tabArrayPtr = CkTextGetTabs(interp, textPtr->winPtr,
textPtr->tabOptionString);
if (textPtr->tabArrayPtr == NULL) {
Tcl_AddErrorInfo(interp,"\n (while processing -tabs option)");
return TCL_ERROR;
}
}
/*
* Make sure that configuration options are properly mirrored
* between the widget record and the "sel" tags. NOTE: we don't
* have to free up information during the mirroring; old
* information was freed when it was replaced in the widget
* record.
*/
textPtr->selTagPtr->bg = textPtr->selBg;
textPtr->selTagPtr->fg = textPtr->selFg;
textPtr->selTagPtr->attr = textPtr->selAttr;
textPtr->selTagPtr->affectsDisplay = 0;
#if 0
/* ??? */
if ((textPtr->selTagPtr->border != NULL)
|| (textPtr->selTagPtr->bdString != NULL)
|| (textPtr->selTagPtr->reliefString != NULL)
|| (textPtr->selTagPtr->bgStipple != None)
|| (textPtr->selTagPtr->fgColor != NULL)
|| (textPtr->selTagPtr->fontPtr != None)
|| (textPtr->selTagPtr->fgStipple != None)
|| (textPtr->selTagPtr->justifyString != NULL)
|| (textPtr->selTagPtr->lMargin1String != NULL)
|| (textPtr->selTagPtr->lMargin2String != NULL)
|| (textPtr->selTagPtr->offsetString != NULL)
|| (textPtr->selTagPtr->overstrikeString != NULL)
|| (textPtr->selTagPtr->rMarginString != NULL)
|| (textPtr->selTagPtr->spacing1String != NULL)
|| (textPtr->selTagPtr->spacing2String != NULL)
|| (textPtr->selTagPtr->spacing3String != NULL)
|| (textPtr->selTagPtr->tabString != NULL)
|| (textPtr->selTagPtr->underlineString != NULL)
|| (textPtr->selTagPtr->wrapMode != NULL)) {
textPtr->selTagPtr->affectsDisplay = 1;
}
#endif
CkTextRedrawTag(textPtr, (CkTextIndex *) NULL, (CkTextIndex *) NULL,
textPtr->selTagPtr, 1);
/*
* Register the desired geometry for the window, and arrange for
* the window to be redisplayed.
*/
if (textPtr->width <= 0) {
textPtr->width = 1;
}
if (textPtr->height <= 0) {
textPtr->height = 1;
}
Ck_GeometryRequest(textPtr->winPtr, textPtr->width, textPtr->height);
CkTextRelayoutWindow(textPtr);
return TCL_OK;
}
/*
*--------------------------------------------------------------
*
* TextEventProc --
*
* This procedure is invoked by the Ck dispatcher on
* structure changes to a text. For texts with 3D
* borders, this procedure is also invoked for exposures.
*
* Results:
* None.
*
* Side effects:
* When the window gets deleted, internal structures get
* cleaned up. When it gets exposed, it is redisplayed.
*
*--------------------------------------------------------------
*/
static void
TextEventProc(clientData, eventPtr)
ClientData clientData; /* Information about window. */
register CkEvent *eventPtr; /* Information about event. */
{
register CkText *textPtr = (CkText *) clientData;
CkWindow *winPtr = textPtr->winPtr;
CkTextIndex index, index2;
if (eventPtr->type == CK_EV_EXPOSE) {
if ((textPtr->prevWidth != winPtr->width)
|| (textPtr->prevHeight != winPtr->height)) {
CkTextRelayoutWindow(textPtr);
textPtr->prevWidth = winPtr->width;
textPtr->prevHeight = winPtr->height;
}
CkTextRedrawRegion(textPtr, 0, 0, winPtr->width, winPtr->height);
} else if (eventPtr->type == CK_EV_DESTROY) {
if (textPtr->winPtr != NULL) {
textPtr->winPtr = NULL;
Tcl_DeleteCommand(textPtr->interp,
Tcl_GetCommandName(textPtr->interp,
textPtr->widgetCmd));
}
Ck_EventuallyFree((ClientData) textPtr, (Ck_FreeProc *) DestroyText);
} else if ((eventPtr->type == CK_EV_FOCUSIN)
|| (eventPtr->type == CK_EV_FOCUSOUT)) {
if (eventPtr->type == CK_EV_FOCUSIN) {
textPtr->flags |= GOT_FOCUS;
} else {
textPtr->flags &= ~GOT_FOCUS;
}
CkTextMarkSegToIndex(textPtr, textPtr->insertMarkPtr, &index);
CkTextIndexForwChars(&index, 1, &index2);
CkTextChanged(textPtr, &index, &index2);
CkTextRedrawRegion(textPtr, 0, 0, winPtr->width,
winPtr->height);
}
}
/*
*----------------------------------------------------------------------
*
* TextCmdDeletedProc --
*
* This procedure is invoked when a widget command is deleted. If
* the widget isn't already in the process of being destroyed,
* this command destroys it.
*
* Results:
* None.
*
* Side effects:
* The widget is destroyed.
*
*----------------------------------------------------------------------
*/
static void
TextCmdDeletedProc(clientData)
ClientData clientData; /* Pointer to widget record for widget. */
{
CkText *textPtr = (CkText *) clientData;
CkWindow *winPtr = textPtr->winPtr;
/*
* This procedure could be invoked either because the window was
* destroyed and the command was then deleted (in which case ckwin
* is NULL) or because the command was deleted, and then this procedure
* destroys the widget.
*/
if (winPtr != NULL) {
textPtr->winPtr = NULL;
Ck_DestroyWindow(winPtr);
}
}
/*
*----------------------------------------------------------------------
*
* InsertChars --
*
* This procedure implements most of the functionality of the
* "insert" widget command.
*
* Results:
* None.
*
* Side effects:
* The characters in "string" get added to the text just before
* the character indicated by "indexPtr".
*
*----------------------------------------------------------------------
*/
static void
InsertChars(textPtr, indexPtr, string)
CkText *textPtr; /* Overall information about text widget. */
CkTextIndex *indexPtr; /* Where to insert new characters. May be
* modified and/or invalidated. */
char *string; /* Null-terminated string containing new
* information to add to text. */
{
int lineIndex;
/*
* Don't allow insertions on the last (dummy) line of the text.
*/
lineIndex = CkBTreeLineIndex(indexPtr->linePtr);
if (lineIndex == CkBTreeNumLines(textPtr->tree)) {
lineIndex--;
#if CK_USE_UTF
CkTextMakeByteIndex(textPtr->tree, lineIndex, 1000000, indexPtr);
#else
CkTextMakeIndex(textPtr->tree, lineIndex, 1000000, indexPtr);
#endif
}
/*
* Notify the display module that lines are about to change, then do
* the insertion.
*/
CkTextChanged(textPtr, indexPtr, indexPtr);
CkBTreeInsertChars(indexPtr, string);
/*
* Invalidate any selection retrievals in progress.
*/
textPtr->abortSelections = 1;
}
/*
*----------------------------------------------------------------------
*
* DeleteChars --
*
* This procedure implements most of the functionality of the
* "delete" widget command.
*
* Results:
* Returns a standard Tcl result, and leaves an error message
* in textPtr->interp if there is an error.
*
* Side effects:
* Characters get deleted from the text.
*
*----------------------------------------------------------------------
*/
static int
DeleteChars(textPtr, index1String, index2String)
CkText *textPtr; /* Overall information about text widget. */
char *index1String; /* String describing location of first
* character to delete. */
char *index2String; /* String describing location of last
* character to delete. NULL means just
* delete the one character given by
* index1String. */
{
int line1, line2, line, charIndex, resetView;
CkTextIndex index1, index2;
/*
* Parse the starting and stopping indices.
*/
if (CkTextGetIndex(textPtr->interp, textPtr, index1String, &index1)
!= TCL_OK) {
return TCL_ERROR;
}
if (index2String != NULL) {
if (CkTextGetIndex(textPtr->interp, textPtr, index2String, &index2)
!= TCL_OK) {
return TCL_ERROR;
}
} else {
index2 = index1;
CkTextIndexForwChars(&index2, 1, &index2);
}
/*
* Make sure there's really something to delete.
*/
if (CkTextIndexCmp(&index1, &index2) >= 0) {
return TCL_OK;
}
/*
* The code below is ugly, but it's needed to make sure there
* is always a dummy empty line at the end of the text. If the
* final newline of the file (just before the dummy line) is being
* deleted, then back up index to just before the newline. If
* there is a newline just before the first character being deleted,
* then back up the first index too, so that an even number of lines
* gets deleted. Furthermore, remove any tags that are present on
* the newline that isn't going to be deleted after all (this simulates
* deleting the newline and then adding a "clean" one back again).
*/
line1 = CkBTreeLineIndex(index1.linePtr);
line2 = CkBTreeLineIndex(index2.linePtr);
if (line2 == CkBTreeNumLines(textPtr->tree)) {
CkTextTag **arrayPtr;
int arraySize, i;
CkTextIndex oldIndex2;
oldIndex2 = index2;
CkTextIndexBackChars(&oldIndex2, 1, &index2);
line2--;
if ((index1.charIndex == 0) && (line1 != 0)) {
CkTextIndexBackChars(&index1, 1, &index1);
line1--;
}
arrayPtr = CkBTreeGetTags(&index2, &arraySize);
if (arrayPtr != NULL) {
for (i = 0; i < arraySize; i++) {
CkBTreeTag(&index2, &oldIndex2, arrayPtr[i], 0);
}
ckfree((char *) arrayPtr);
}