-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathtkCanvText.c
More file actions
1776 lines (1614 loc) · 52.4 KB
/
Copy pathtkCanvText.c
File metadata and controls
1776 lines (1614 loc) · 52.4 KB
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
/*
* tkCanvText.c --
*
* This file implements text items for canvas widgets.
*
* Copyright © 1991-1994 The Regents of the University of California.
* Copyright © 1994-1997 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "tkInt.h"
#include "tkCanvas.h"
#include "default.h"
#ifdef _WIN32
#include "tkWinInt.h"
#endif
/*
* The structure below defines the record for each text item.
*/
typedef struct TextItem {
Tk_Item header; /* Generic stuff that's the same for all
* types. MUST BE FIRST IN STRUCTURE. */
Tk_CanvasTextInfo *textInfoPtr;
/* Pointer to a structure containing
* information about the selection and
* insertion cursor. The structure is owned by
* (and shared with) the generic canvas
* code. */
/*
* Fields that are set by widget commands other than "configure".
*/
double x, y; /* Positioning point for text. */
Tcl_Size insertPos; /* Character index of character just before
* which the insertion cursor is displayed. */
/*
* Configuration settings that are updated by Tk_ConfigureWidget.
*/
Tk_Anchor anchor; /* Where to anchor text relative to (x,y). */
Tk_TSOffset tsoffset;
XColor *color; /* Color for text. */
XColor *activeColor; /* Color for text. */
XColor *disabledColor; /* Color for text. */
Tk_Font tkfont; /* Font for drawing text. */
Tk_Justify justify; /* Justification mode for text. */
Pixmap stipple; /* Stipple bitmap for text, or None. */
Pixmap activeStipple; /* Stipple bitmap for text, or None. */
Pixmap disabledStipple; /* Stipple bitmap for text, or None. */
Tcl_Obj *textObj; /* Text for item (malloc-ed). */
Tcl_Obj *widthObj; /* Width of lines for word-wrap, pixels. Zero
* means no word-wrap. */
int underline; /* Index of character to put underline beneath
* or INT_MIN for no underlining. */
double angle; /* What angle, in degrees, to draw the text
* at. */
/*
* Fields whose values are derived from the current values of the
* configuration settings above.
*/
Tk_TextLayout textLayout; /* Cached text layout information. */
int actualWidth; /* Width of text as computed. Used to make
* selections of wrapped text display
* right. */
double drawOrigin[2]; /* Where we start drawing from. */
GC gc; /* Graphics context for drawing text. */
GC selTextGC; /* Graphics context for selected text. */
GC cursorOffGC; /* If not None, this gives a graphics context
* to use to draw the insertion cursor when
* it's off. Used if the selection and
* insertion cursor colors are the same. */
double sine; /* Sine of angle field. */
double cosine; /* Cosine of angle field. */
} TextItem;
/*
* Information used for parsing configuration specs:
*/
static const Tk_CustomOption stateOption = {
TkStateParseProc, TkStatePrintProc, INT2PTR(2)
};
static const Tk_CustomOption tagsOption = {
Tk_CanvasTagsParseProc, Tk_CanvasTagsPrintProc, NULL
};
static const Tk_CustomOption offsetOption = {
TkOffsetParseProc, TkOffsetPrintProc, INT2PTR(TK_OFFSET_RELATIVE)
};
static int
UnderlineParseProc(
TCL_UNUSED(void *), /* Not used.*/
Tcl_Interp *interp, /* Used for reporting errors. */
TCL_UNUSED(Tk_Window), /* Window containing canvas widget. */
const char *value, /* Value of option. */
char *widgRec, /* Pointer to record for item. */
Tcl_Size offset) /* Offset into item (ignored). */
{
int *underlinePtr = (int *) (widgRec + offset);
Tcl_Obj obj;
int code;
Tcl_Size underline;
if (value == NULL || *value == 0) {
*underlinePtr = INT_MIN; /* No underline */
return TCL_OK;
}
obj.refCount = 1;
obj.bytes = (char *)value;
obj.length = (Tcl_Size)strlen(value);
obj.typePtr = NULL;
code = TkGetIntForIndex(&obj, TCL_INDEX_NONE, 0, &underline);
if (code == TCL_OK) {
if (underline < INT_MIN) {
underline = INT_MIN;
} else if (underline > INT_MAX) {
underline = INT_MAX;
}
*underlinePtr = (int)underline;
} else {
Tcl_AppendResult(interp, "bad index \"", value,
"\": must be integer?[+-]integer?, end?[+-]integer?, or \"\"", (char *)NULL);
}
return code;
}
static const char *
UnderlinePrintProc(
TCL_UNUSED(void *),
TCL_UNUSED(Tk_Window), /* Window containing canvas widget. */
char *widgRec, /* Pointer to record for item. */
Tcl_Size offset, /* Pointer to record for item. */
Tcl_FreeProc **freeProcPtr) /* Pointer to variable to fill in with
* information about how to reclaim storage
* for return string. */
{
int underline = *(int *)(widgRec + offset);
char *p;
if (underline == INT_MIN) {
p = (char *)"";
*freeProcPtr = TCL_STATIC;
return p;
} else if (underline == INT_MAX) {
p = (char *)"end+1";
*freeProcPtr = TCL_STATIC;
return p;
} else if (underline == -1) {
p = (char *)"end";
*freeProcPtr = TCL_STATIC;
return p;
}
p = (char *)ckalloc(32);
if (underline < 0) {
snprintf(p, 32, "end%d", 1 + underline);
} else {
snprintf(p, 32, "%d", underline);
}
*freeProcPtr = TCL_DYNAMIC;
return p;
}
static const Tk_CustomOption underlineOption = {
UnderlineParseProc, UnderlinePrintProc, NULL
};
static const Tk_ConfigSpec configSpecs[] = {
{TK_CONFIG_COLOR, "-activefill", NULL, NULL,
NULL, offsetof(TextItem, activeColor), TK_CONFIG_NULL_OK, NULL},
{TK_CONFIG_BITMAP, "-activestipple", NULL, NULL,
NULL, offsetof(TextItem, activeStipple), TK_CONFIG_NULL_OK, NULL},
{TK_CONFIG_ANCHOR, "-anchor", NULL, NULL,
"center", offsetof(TextItem, anchor), TK_CONFIG_DONT_SET_DEFAULT, NULL},
{TK_CONFIG_DOUBLE, "-angle", NULL, NULL,
"0.0", offsetof(TextItem, angle), TK_CONFIG_DONT_SET_DEFAULT, NULL},
{TK_CONFIG_COLOR, "-disabledfill", NULL, NULL,
NULL, offsetof(TextItem, disabledColor), TK_CONFIG_NULL_OK, NULL},
{TK_CONFIG_BITMAP, "-disabledstipple", NULL, NULL,
NULL, offsetof(TextItem, disabledStipple), TK_CONFIG_NULL_OK, NULL},
{TK_CONFIG_COLOR, "-fill", NULL, NULL,
DEF_CANVITEM_OUTLINE, offsetof(TextItem, color), TK_CONFIG_NULL_OK, NULL},
{TK_CONFIG_FONT, "-font", NULL, NULL,
DEF_CANVTEXT_FONT, offsetof(TextItem, tkfont), 0, NULL},
{TK_CONFIG_JUSTIFY, "-justify", NULL, NULL,
"left", offsetof(TextItem, justify), TK_CONFIG_DONT_SET_DEFAULT, NULL},
{TK_CONFIG_CUSTOM, "-offset", NULL, NULL,
"0,0", offsetof(TextItem, tsoffset),
TK_CONFIG_DONT_SET_DEFAULT, &offsetOption},
{TK_CONFIG_CUSTOM, "-state", NULL, NULL,
NULL, offsetof(Tk_Item, state), TK_CONFIG_NULL_OK, &stateOption},
{TK_CONFIG_BITMAP, "-stipple", NULL, NULL,
NULL, offsetof(TextItem, stipple), TK_CONFIG_NULL_OK, NULL},
{TK_CONFIG_CUSTOM, "-tags", NULL, NULL,
NULL, 0, TK_CONFIG_NULL_OK, &tagsOption},
{TK_CONFIG_STRING, "-text", NULL, NULL,
"", offsetof(TextItem, textObj), TK_CONFIG_OBJS|TK_CONFIG_NULL_OK, NULL},
{TK_CONFIG_CUSTOM, "-underline", NULL, NULL, NULL,
offsetof(TextItem, underline), TK_CONFIG_NULL_OK, &underlineOption},
{TK_CONFIG_PIXELS, "-width", NULL, NULL,
"0", offsetof(TextItem, widthObj), TK_CONFIG_OBJS, NULL},
{TK_CONFIG_END, NULL, NULL, NULL, NULL, 0, 0, NULL}
};
/*
* Prototypes for functions defined in this file:
*/
static void ComputeTextBbox(Tk_Canvas canvas, TextItem *textPtr);
static int ConfigureText(Tcl_Interp *interp,
Tk_Canvas canvas, Tk_Item *itemPtr, Tcl_Size objc,
Tcl_Obj *const objv[], int flags);
static int CreateText(Tcl_Interp *interp,
Tk_Canvas canvas, struct Tk_Item *itemPtr,
Tcl_Size objc, Tcl_Obj *const objv[]);
static void DeleteText(Tk_Canvas canvas,
Tk_Item *itemPtr, Display *display);
static void DisplayCanvText(Tk_Canvas canvas,
Tk_Item *itemPtr, Display *display, Drawable dst,
int x, int y, int width, int height);
static Tcl_Size GetSelText(Tk_Canvas canvas,
Tk_Item *itemPtr, Tcl_Size offset, char *buffer,
Tcl_Size maxBytes);
static int GetTextIndex(Tcl_Interp *interp,
Tk_Canvas canvas, Tk_Item *itemPtr,
Tcl_Obj *obj, Tcl_Size *indexPtr);
static void ScaleText(Tk_Canvas canvas,
Tk_Item *itemPtr, double originX, double originY,
double scaleX, double scaleY);
static void SetTextCursor(Tk_Canvas canvas,
Tk_Item *itemPtr, Tcl_Size index);
static int TextCoords(Tcl_Interp *interp,
Tk_Canvas canvas, Tk_Item *itemPtr,
Tcl_Size objc, Tcl_Obj *const objv[]);
static void TextDeleteChars(Tk_Canvas canvas,
Tk_Item *itemPtr, Tcl_Size first, Tcl_Size last);
static void TextInsert(Tk_Canvas canvas,
Tk_Item *itemPtr, Tcl_Size beforeThis, Tcl_Obj *obj);
static int TextToArea(Tk_Canvas canvas,
Tk_Item *itemPtr, double *rectPtr);
static double TextToPoint(Tk_Canvas canvas,
Tk_Item *itemPtr, double *pointPtr);
static int TextToPostscript(Tcl_Interp *interp,
Tk_Canvas canvas, Tk_Item *itemPtr, int prepass);
static void RotateText(Tk_Canvas canvas, Tk_Item *itemPtr,
double originX, double originY, double angleRad);
static void TranslateText(Tk_Canvas canvas,
Tk_Item *itemPtr, double deltaX, double deltaY);
/*
* The structures below defines the rectangle and oval item types by means of
* functions that can be invoked by generic item code.
*/
Tk_ItemType tkTextType = {
"text", /* name */
sizeof(TextItem), /* itemSize */
CreateText, /* createProc */
configSpecs, /* configSpecs */
ConfigureText, /* configureProc */
TextCoords, /* coordProc */
DeleteText, /* deleteProc */
DisplayCanvText, /* displayProc */
0, /* flags */
TextToPoint, /* pointProc */
TextToArea, /* areaProc */
TextToPostscript, /* postscriptProc */
ScaleText, /* scaleProc */
TranslateText, /* translateProc */
GetTextIndex, /* indexProc */
SetTextCursor, /* icursorProc */
GetSelText, /* selectionProc */
TextInsert, /* insertProc */
TextDeleteChars, /* dTextProc */
NULL, /* nextPtr */
RotateText, /* rotateProc */
0, NULL, NULL
};
#define ROUND(d) ((int) floor((d) + 0.5))
/*
*--------------------------------------------------------------
*
* CreateText --
*
* This function is invoked to create a new text item in a canvas.
*
* Results:
* A standard Tcl return value. If an error occurred in creating the item
* then an error message is left in the interp's result; in this case
* itemPtr is left uninitialized so it can be safely freed by the caller.
*
* Side effects:
* A new text item is created.
*
*--------------------------------------------------------------
*/
static int
CreateText(
Tcl_Interp *interp, /* Interpreter for error reporting. */
Tk_Canvas canvas, /* Canvas to hold new item. */
Tk_Item *itemPtr, /* Record to hold new item; header has been
* initialized by caller. */
Tcl_Size objc, /* Number of arguments in objv. */
Tcl_Obj *const objv[]) /* Arguments describing rectangle. */
{
TextItem *textPtr = (TextItem *) itemPtr;
Tcl_Size i;
if (objc == 0) {
Tcl_Panic("canvas did not pass any coords");
}
/*
* Carry out initialization that is needed in order to clean up after
* errors during the the remainder of this function.
*/
textPtr->textInfoPtr = Tk_CanvasGetTextInfo(canvas);
textPtr->insertPos = 0;
textPtr->anchor = TK_ANCHOR_CENTER;
textPtr->tsoffset.flags = 0;
textPtr->tsoffset.xoffset = 0;
textPtr->tsoffset.yoffset = 0;
textPtr->color = NULL;
textPtr->activeColor = NULL;
textPtr->disabledColor = NULL;
textPtr->tkfont = NULL;
textPtr->justify = TK_JUSTIFY_LEFT;
textPtr->stipple = None;
textPtr->activeStipple = None;
textPtr->disabledStipple = None;
textPtr->textObj = NULL;
textPtr->widthObj = NULL;
textPtr->underline = INT_MIN;
textPtr->angle = 0.0;
textPtr->textLayout = NULL;
textPtr->actualWidth = 0;
textPtr->drawOrigin[0] = textPtr->drawOrigin[1] = 0.0;
textPtr->gc = NULL;
textPtr->selTextGC = NULL;
textPtr->cursorOffGC = NULL;
textPtr->sine = 0.0;
textPtr->cosine = 1.0;
/*
* Process the arguments to fill in the item record. Only 1 (list) or 2 (x
* y) coords are allowed.
*/
if (objc == 1) {
i = 1;
} else {
const char *arg = Tcl_GetString(objv[1]);
i = 2;
if ((arg[0] == '-') && (arg[1] >= 'a') && (arg[1] <= 'z')) {
i = 1;
}
}
if ((TextCoords(interp, canvas, itemPtr, i, objv) != TCL_OK)) {
goto error;
}
if (ConfigureText(interp, canvas, itemPtr, objc-i, objv+i, 0) == TCL_OK) {
return TCL_OK;
}
error:
DeleteText(canvas, itemPtr, Tk_Display(Tk_CanvasTkwin(canvas)));
return TCL_ERROR;
}
/*
*--------------------------------------------------------------
*
* TextCoords --
*
* This function is invoked to process the "coords" widget command on
* text items. See the user documentation for details on what it does.
*
* Results:
* Returns TCL_OK or TCL_ERROR, and sets the interp's result.
*
* Side effects:
* The coordinates for the given item may be changed.
*
*--------------------------------------------------------------
*/
static int
TextCoords(
Tcl_Interp *interp, /* Used for error reporting. */
Tk_Canvas canvas, /* Canvas containing item. */
Tk_Item *itemPtr, /* Item whose coordinates are to be read or
* modified. */
Tcl_Size objc, /* Number of coordinates supplied in objv. */
Tcl_Obj *const objv[]) /* Array of coordinates: x1, y1, x2, y2, ... */
{
TextItem *textPtr = (TextItem *) itemPtr;
if (objc == 0) {
Tcl_Obj *obj = Tcl_NewObj();
Tcl_Obj *subobj = Tcl_NewDoubleObj(textPtr->x);
Tcl_ListObjAppendElement(interp, obj, subobj);
subobj = Tcl_NewDoubleObj(textPtr->y);
Tcl_ListObjAppendElement(interp, obj, subobj);
Tcl_SetObjResult(interp, obj);
return TCL_OK;
} else if (objc > 2) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf(
"wrong # coordinates: expected 0 or 2, got %" TCL_SIZE_MODIFIER "d", objc));
Tcl_SetErrorCode(interp, "TK", "CANVAS", "COORDS", "TEXT", (char *)NULL);
return TCL_ERROR;
}
if (objc == 1) {
if (Tcl_ListObjGetElements(interp, objv[0], &objc,
(Tcl_Obj ***) &objv) != TCL_OK) {
return TCL_ERROR;
} else if (objc != 2) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf(
"wrong # coordinates: expected 2, got %" TCL_SIZE_MODIFIER "d", objc));
Tcl_SetErrorCode(interp, "TK", "CANVAS", "COORDS", "TEXT", (char *)NULL);
return TCL_ERROR;
}
}
if ((Tk_CanvasGetCoordFromObj(interp, canvas, objv[0],
&textPtr->x) != TCL_OK)
|| (Tk_CanvasGetCoordFromObj(interp, canvas, objv[1],
&textPtr->y) != TCL_OK)) {
return TCL_ERROR;
}
ComputeTextBbox(canvas, textPtr);
return TCL_OK;
}
/*
*--------------------------------------------------------------
*
* ConfigureText --
*
* This function is invoked to configure various aspects of a text item,
* such as its border and background colors.
*
* Results:
* A standard Tcl result code. If an error occurs, then an error message
* is left in the interp's result.
*
* Side effects:
* Configuration information, such as colors and stipple patterns, may be
* set for itemPtr.
*
*--------------------------------------------------------------
*/
static int
ConfigureText(
Tcl_Interp *interp, /* Interpreter for error reporting. */
Tk_Canvas canvas, /* Canvas containing itemPtr. */
Tk_Item *itemPtr, /* Rectangle item to reconfigure. */
Tcl_Size objc, /* Number of elements in objv. */
Tcl_Obj *const objv[], /* Arguments describing things to configure. */
int flags) /* Flags to pass to Tk_ConfigureWidget. */
{
TextItem *textPtr = (TextItem *) itemPtr;
XGCValues gcValues;
GC newGC, newSelGC;
unsigned long mask;
Tk_Window tkwin;
Tk_CanvasTextInfo *textInfoPtr = textPtr->textInfoPtr;
XColor *selBgColorPtr;
XColor *color;
Pixmap stipple;
Tk_State state;
tkwin = Tk_CanvasTkwin(canvas);
if (TCL_OK != Tk_ConfigureWidget(interp, tkwin, configSpecs, objc,
objv, textPtr, flags)) {
return TCL_ERROR;
}
/*
* A few of the options require additional processing, such as graphics
* contexts.
*/
state = itemPtr->state;
if (textPtr->activeColor != NULL || textPtr->activeStipple != None) {
itemPtr->redraw_flags |= TK_ITEM_STATE_DEPENDANT;
} else {
itemPtr->redraw_flags &= ~TK_ITEM_STATE_DEPENDANT;
}
if (state == TK_STATE_NULL) {
state = Canvas(canvas)->canvas_state;
}
color = textPtr->color;
stipple = textPtr->stipple;
if (Canvas(canvas)->currentItemPtr == itemPtr) {
if (textPtr->activeColor != NULL) {
color = textPtr->activeColor;
}
if (textPtr->activeStipple != None) {
stipple = textPtr->activeStipple;
}
} else if (state == TK_STATE_DISABLED) {
if (textPtr->disabledColor != NULL) {
color = textPtr->disabledColor;
}
if (textPtr->disabledStipple != None) {
stipple = textPtr->disabledStipple;
}
}
newGC = newSelGC = NULL;
if (textPtr->tkfont != NULL) {
gcValues.font = Tk_FontId(textPtr->tkfont);
mask = GCFont;
if (color != NULL) {
gcValues.foreground = color->pixel;
mask |= GCForeground;
if (stipple != None) {
gcValues.stipple = stipple;
gcValues.fill_style = FillStippled;
mask |= GCStipple|GCFillStyle;
}
newGC = Tk_GetGC(tkwin, mask, &gcValues);
}
mask &= ~(GCTile|GCFillStyle|GCStipple);
if (stipple != None) {
gcValues.stipple = stipple;
gcValues.fill_style = FillStippled;
mask |= GCStipple|GCFillStyle;
}
if (textInfoPtr->selFgColorPtr != NULL) {
gcValues.foreground = textInfoPtr->selFgColorPtr->pixel;
}
newSelGC = Tk_GetGC(tkwin, mask|GCForeground, &gcValues);
}
if (textPtr->gc != NULL) {
Tk_FreeGC(Tk_Display(tkwin), textPtr->gc);
}
textPtr->gc = newGC;
if (textPtr->selTextGC != NULL) {
Tk_FreeGC(Tk_Display(tkwin), textPtr->selTextGC);
}
textPtr->selTextGC = newSelGC;
selBgColorPtr = Tk_3DBorderColor(textInfoPtr->selBorder);
if (Tk_3DBorderColor(textInfoPtr->insertBorder)->pixel
== selBgColorPtr->pixel) {
if (selBgColorPtr->pixel == BlackPixelOfScreen(Tk_Screen(tkwin))) {
gcValues.foreground = WhitePixelOfScreen(Tk_Screen(tkwin));
} else {
gcValues.foreground = BlackPixelOfScreen(Tk_Screen(tkwin));
}
newGC = Tk_GetGC(tkwin, GCForeground, &gcValues);
} else {
newGC = NULL;
}
if (textPtr->cursorOffGC != NULL) {
Tk_FreeGC(Tk_Display(tkwin), textPtr->cursorOffGC);
}
textPtr->cursorOffGC = newGC;
/*
* If the text was changed, move the selection and insertion indices to
* keep them inside the item.
*/
Tcl_Size numChars = textPtr->textObj ? Tcl_GetCharLength(textPtr->textObj) : 0;
if (textInfoPtr->selItemPtr == itemPtr) {
if (textInfoPtr->selectFirst >= numChars) {
textInfoPtr->selItemPtr = NULL;
} else {
if (textInfoPtr->selectLast >= numChars) {
textInfoPtr->selectLast = numChars - 1;
}
if ((textInfoPtr->anchorItemPtr == itemPtr)
&& (textInfoPtr->selectAnchor >= numChars)) {
textInfoPtr->selectAnchor = numChars - 1;
}
}
}
if (textPtr->insertPos >= numChars) {
textPtr->insertPos = numChars;
}
/*
* Restrict so that 0.0 <= angle < 360.0, and then recompute the cached
* sine and cosine of the angle. Note that fmod() can produce negative
* results, and we try to avoid negative zero as well.
*/
textPtr->angle = fmod(textPtr->angle, 360.0);
if (textPtr->angle < 0.0) {
textPtr->angle += 360.0;
}
if (textPtr->angle == 0.0) {
textPtr->angle = 0.0;
}
textPtr->sine = sin(textPtr->angle * PI/180.0);
textPtr->cosine = cos(textPtr->angle * PI/180.0);
ComputeTextBbox(canvas, textPtr);
return TCL_OK;
}
/*
*--------------------------------------------------------------
*
* DeleteText --
*
* This function is called to clean up the data structure associated with
* a text item.
*
* Results:
* None.
*
* Side effects:
* Resources associated with itemPtr are released.
*
*--------------------------------------------------------------
*/
static void
DeleteText(
TCL_UNUSED(Tk_Canvas), /* Info about overall canvas widget. */
Tk_Item *itemPtr, /* Item that is being deleted. */
Display *display) /* Display containing window for canvas. */
{
TextItem *textPtr = (TextItem *) itemPtr;
if (textPtr->color != NULL) {
Tk_FreeColor(textPtr->color);
}
if (textPtr->activeColor != NULL) {
Tk_FreeColor(textPtr->activeColor);
}
if (textPtr->disabledColor != NULL) {
Tk_FreeColor(textPtr->disabledColor);
}
Tk_FreeFont(textPtr->tkfont);
if (textPtr->stipple != None) {
Tk_FreeBitmap(display, textPtr->stipple);
}
if (textPtr->activeStipple != None) {
Tk_FreeBitmap(display, textPtr->activeStipple);
}
if (textPtr->disabledStipple != None) {
Tk_FreeBitmap(display, textPtr->disabledStipple);
}
if (textPtr->textObj != NULL) {
Tcl_DecrRefCount(textPtr->textObj);
}
Tk_FreeTextLayout(textPtr->textLayout);
if (textPtr->gc != NULL) {
Tk_FreeGC(display, textPtr->gc);
}
if (textPtr->selTextGC != NULL) {
Tk_FreeGC(display, textPtr->selTextGC);
}
if (textPtr->cursorOffGC != NULL) {
Tk_FreeGC(display, textPtr->cursorOffGC);
}
}
/*
*--------------------------------------------------------------
*
* ComputeTextBbox --
*
* This function is invoked to compute the bounding box of all the pixels
* that may be drawn as part of a text item. In addition, it recomputes
* all of the geometry information used to display a text item or check
* for mouse hits.
*
* Results:
* None.
*
* Side effects:
* The fields x1, y1, x2, and y2 are updated in the header for itemPtr,
* and the linePtr structure is regenerated for itemPtr.
*
*--------------------------------------------------------------
*/
static void
ComputeTextBbox(
Tk_Canvas canvas, /* Canvas that contains item. */
TextItem *textPtr) /* Item whose bbox is to be recomputed. */
{
Tk_CanvasTextInfo *textInfoPtr;
int width, height, fudge, i;
Tk_State state = textPtr->header.state;
double x[4], y[4], dx[4], dy[4], sinA, cosA, tmp;
if (state == TK_STATE_NULL) {
state = Canvas(canvas)->canvas_state;
}
Tk_FreeTextLayout(textPtr->textLayout);
width = 0;
if (textPtr->widthObj) {
Tk_GetPixelsFromObj(NULL, Tk_CanvasTkwin(canvas), textPtr->widthObj, &width);
}
Tcl_Size numChars = textPtr->textObj ? Tcl_GetCharLength(textPtr->textObj) : 0;
textPtr->textLayout = Tk_ComputeTextLayout(textPtr->tkfont,
(textPtr->textObj ? Tcl_GetString(textPtr->textObj) : ""), numChars, width,
textPtr->justify, 0, &width, &height);
if (state == TK_STATE_HIDDEN || textPtr->color == NULL) {
width = height = 0;
}
/*
* Use overall geometry information to compute the top-left corner of the
* bounding box for the text item.
*/
for (i=0 ; i<4 ; i++) {
dx[i] = dy[i] = 0.0;
}
switch (textPtr->anchor) {
case TK_ANCHOR_NW:
case TK_ANCHOR_N:
case TK_ANCHOR_NE:
break;
case TK_ANCHOR_SW:
case TK_ANCHOR_S:
case TK_ANCHOR_SE:
for (i=0 ; i<4 ; i++) {
dy[i] = -height;
}
break;
default:
for (i=0 ; i<4 ; i++) {
dy[i] = -height / 2;
}
break;
}
switch (textPtr->anchor) {
case TK_ANCHOR_NW:
case TK_ANCHOR_W:
case TK_ANCHOR_SW:
break;
case TK_ANCHOR_NE:
case TK_ANCHOR_E:
case TK_ANCHOR_SE:
for (i=0 ; i<4 ; i++) {
dx[i] = -width;
}
break;
default:
for (i=0 ; i<4 ; i++) {
dx[i] = -width / 2;
}
break;
}
textPtr->actualWidth = width;
sinA = textPtr->sine;
cosA = textPtr->cosine;
textPtr->drawOrigin[0] = textPtr->x + dx[0]*cosA + dy[0]*sinA;
textPtr->drawOrigin[1] = textPtr->y + dy[0]*cosA - dx[0]*sinA;
/*
* Last of all, update the bounding box for the item. The item's bounding
* box includes the bounding box of all its lines, plus an extra fudge
* factor for the cursor border (which could potentially be quite large).
*/
textInfoPtr = textPtr->textInfoPtr;
Tk_GetPixelsFromObj(NULL, Tk_CanvasTkwin(canvas), (Tcl_Obj *)textInfoPtr->reserved2, &textInfoPtr->insertWidth);
Tk_GetPixelsFromObj(NULL, Tk_CanvasTkwin(canvas), (Tcl_Obj *)textInfoPtr->reserved3, &textInfoPtr->selBorderWidth);
fudge = (textInfoPtr->insertWidth + 1) / 2;
if (textInfoPtr->selBorderWidth > fudge) {
fudge = textInfoPtr->selBorderWidth;
}
/*
* Apply the rotation before computing the bounding box.
*/
dx[0] -= fudge;
dx[1] += width + fudge;
dx[2] += width + fudge;
dy[2] += height;
dx[3] -= fudge;
dy[3] += height;
for (i=0 ; i<4 ; i++) {
x[i] = textPtr->x + dx[i] * cosA + dy[i] * sinA;
y[i] = textPtr->y + dy[i] * cosA - dx[i] * sinA;
}
/*
* Convert to a rectilinear bounding box.
*/
for (i=1,tmp=x[0] ; i<4 ; i++) {
if (x[i] < tmp) {
tmp = x[i];
}
}
textPtr->header.x1 = ROUND(tmp);
for (i=1,tmp=y[0] ; i<4 ; i++) {
if (y[i] < tmp) {
tmp = y[i];
}
}
textPtr->header.y1 = ROUND(tmp);
for (i=1,tmp=x[0] ; i<4 ; i++) {
if (x[i] > tmp) {
tmp = x[i];
}
}
textPtr->header.x2 = ROUND(tmp);
for (i=1,tmp=y[0] ; i<4 ; i++) {
if (y[i] > tmp) {
tmp = y[i];
}
}
textPtr->header.y2 = ROUND(tmp);
}
/*
*--------------------------------------------------------------
*
* DisplayCanvText --
*
* This function is invoked to draw a text item in a given drawable.
*
* Results:
* None.
*
* Side effects:
* ItemPtr is drawn in drawable using the transformation information in
* canvas.
*
*--------------------------------------------------------------
*/
static void
DisplayCanvText(
Tk_Canvas canvas, /* Canvas that contains item. */
Tk_Item *itemPtr, /* Item to be displayed. */
Display *display, /* Display on which to draw item. */
Drawable drawable, /* Pixmap or window in which to draw item. */
int x, int y, int width, int height)
/* Describes region of canvas that must be
* redisplayed (not used). */
{
TextItem *textPtr;
Tk_CanvasTextInfo *textInfoPtr;
Tcl_Size selFirstChar, selLastChar;
short drawableX, drawableY;
Pixmap stipple;
Tk_State state = itemPtr->state;
textPtr = (TextItem *) itemPtr;
textInfoPtr = textPtr->textInfoPtr;
if (state == TK_STATE_NULL) {
state = Canvas(canvas)->canvas_state;
}
stipple = textPtr->stipple;
if (Canvas(canvas)->currentItemPtr == itemPtr) {
if (textPtr->activeStipple != None) {
stipple = textPtr->activeStipple;
}
} else if (state == TK_STATE_DISABLED) {
if (textPtr->disabledStipple != None) {
stipple = textPtr->disabledStipple;
}
}
if (textPtr->gc == NULL) {
return;
}
/*
* If we're stippling, then modify the stipple offset in the GC. Be sure
* to reset the offset when done, since the GC is supposed to be
* read-only.
*/
if (stipple != None) {
Tk_CanvasSetOffset(canvas, textPtr->gc, &textPtr->tsoffset);
}
selFirstChar = TCL_INDEX_NONE;
selLastChar = 0;
Tk_CanvasDrawableCoords(canvas, textPtr->drawOrigin[0],
textPtr->drawOrigin[1], &drawableX, &drawableY);
Tcl_Size numChars = textPtr->textObj ? Tcl_GetCharLength(textPtr->textObj) : 0;
if (textInfoPtr->selItemPtr == itemPtr) {
selFirstChar = textInfoPtr->selectFirst;
selLastChar = textInfoPtr->selectLast;
if (selLastChar > numChars) {
selLastChar = numChars - 1;
}
if ((selFirstChar >= 0) && (selFirstChar <= selLastChar)) {
int xFirst, yFirst, hFirst;
int xLast, yLast, wLast;
/*
* Draw a special background under the selection.
*/
Tk_CharBbox(textPtr->textLayout, selFirstChar, &xFirst, &yFirst,
NULL, &hFirst);
Tk_CharBbox(textPtr->textLayout, selLastChar, &xLast, &yLast,
&wLast, NULL);
/*
* If the selection spans the end of this line, then display
* selection background all the way to the end of the line.
* However, for the last line we only want to display up to the
* last character, not the end of the line.
*/
x = xFirst;
height = hFirst;
Tk_GetPixelsFromObj(NULL, Tk_CanvasTkwin(canvas), (Tcl_Obj *)textInfoPtr->reserved3, &textInfoPtr->selBorderWidth);
for (y = yFirst ; y <= yLast; y += height) {
int dx1, dy1, dx2, dy2;
double s = textPtr->sine, c = textPtr->cosine;
XPoint points[4];
if (y == yLast) {
width = xLast + wLast - x;
} else {
width = textPtr->actualWidth - x;
}
dx1 = x - textInfoPtr->selBorderWidth;
dy1 = y;
dx2 = width + 2 * textInfoPtr->selBorderWidth;
dy2 = height;
points[0].x = (short)(drawableX + dx1*c + dy1*s);
points[0].y = (short)(drawableY + dy1*c - dx1*s);
points[1].x = (short)(drawableX + (dx1+dx2)*c + dy1*s);
points[1].y = (short)(drawableY + dy1*c - (dx1+dx2)*s);
points[2].x = (short)(drawableX + (dx1+dx2)*c + (dy1+dy2)*s);
points[2].y = (short)(drawableY + (dy1+dy2)*c - (dx1+dx2)*s);
points[3].x = (short)(drawableX + dx1*c + (dy1+dy2)*s);
points[3].y = (short)(drawableY + (dy1+dy2)*c - dx1*s);
Tk_Fill3DPolygon(Tk_CanvasTkwin(canvas), drawable,
textInfoPtr->selBorder, points, 4,
textInfoPtr->selBorderWidth, TK_RELIEF_RAISED);
x = 0;
}
}
}
/*
* If the insertion point should be displayed, then draw a special
* background for the cursor before drawing the text. Note: if we're the
* cursor item but the cursor is turned off, then redraw background over
* the area of the cursor. This guarantees that the selection won't make
* the cursor invisible on mono displays, where both are drawn in the same
* color.
*/
if ((textInfoPtr->focusItemPtr == itemPtr) && (textInfoPtr->gotFocus)) {
if (Tk_CharBbox(textPtr->textLayout, textPtr->insertPos,
&x, &y, NULL, &height)) {
int dx1, dy1, dx2, dy2;
double s = textPtr->sine, c = textPtr->cosine;
XPoint points[4];
Tk_GetPixelsFromObj(NULL, Tk_CanvasTkwin(canvas), (Tcl_Obj *)textInfoPtr->reserved2, &textInfoPtr->insertWidth);
dx1 = x - (textInfoPtr->insertWidth / 2);
dy1 = y;
dx2 = textInfoPtr->insertWidth;
dy2 = height;