-
Notifications
You must be signed in to change notification settings - Fork 0
/
ckTextDisp.c
3826 lines (3468 loc) · 111 KB
/
ckTextDisp.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
/*
* ckTextDisp.c --
*
* This module provides facilities to display text widgets. It is
* the only place where information is kept about the screen layout
* of text widgets.
*
* 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 "ckPort.h"
#include "ck.h"
#include "ckText.h"
/*
* The following structure describes how to display a range of characters.
* The information is generated by scanning all of the tags associated
* with the characters and combining that with default information for
* the overall widget. These structures form the hash keys for
* dInfoPtr->styleTable.
*/
typedef struct StyleValues {
int fg, bg, attr;
int justify; /* Justification style for text. */
int lMargin1; /* Left margin, in pixels, for first display
* line of each text line. */
int lMargin2; /* Left margin, in pixels, for second and
* later display lines of each text line. */
int rMargin; /* Right margin, in pixels. */
CkTextTabArray *tabArrayPtr;/* Locations and types of tab stops (may
* be NULL). */
Ck_Uid wrapMode; /* How to handle wrap-around for this tag.
* One of ckTextCharUid, ckTextNoneUid,
* or ckTextWordUid. */
} StyleValues;
/*
* The following structure extends the StyleValues structure above with
* graphics contexts used to actually draw the characters. The entries
* in dInfoPtr->styleTable point to structures of this type.
*/
typedef struct Style {
int refCount; /* Number of times this structure is
* referenced in Chunks. */
StyleValues *sValuePtr; /* Raw information from which GCs were
* derived. */
Tcl_HashEntry *hPtr; /* Pointer to entry in styleTable. Used
* to delete entry. */
} Style;
/*
* The following macro determines whether two styles have the same
* background so that, for example, no beveled border should be drawn
* between them.
*/
#define SAME_BACKGROUND(s1, s2) \
(((s1)->sValuePtr->border == (s2)->sValuePtr->border) \
&& ((s1)->sValuePtr->borderWidth == (s2)->sValuePtr->borderWidth) \
&& ((s1)->sValuePtr->relief == (s2)->sValuePtr->relief) \
&& ((s1)->sValuePtr->bgStipple == (s2)->sValuePtr->bgStipple))
/*
* The following structure describes one line of the display, which may
* be either part or all of one line of the text.
*/
typedef struct DLine {
CkTextIndex index; /* Identifies first character in text
* that is displayed on this line. */
int count; /* Number of characters accounted for by this
* display line, including a trailing space
* or newline that isn't actually displayed. */
int y; /* Y-position at which line is supposed to
* be drawn (topmost pixel of rectangular
* area occupied by line). */
int oldY; /* Y-position at which line currently
* appears on display. -1 means line isn't
* currently visible on display and must be
* redrawn. This is used to move lines by
* scrolling rather than re-drawing. */
int height; /* Height of line, in chars. */
int length; /* Total length of line, in chars. */
CkTextDispChunk *chunkPtr; /* Pointer to first chunk in list of all
* of those that are displayed on this
* line of the screen. */
struct DLine *nextPtr; /* Next in list of all display lines for
* this window. The list is sorted in
* order from top to bottom. Note: the
* next DLine doesn't always correspond
* to the next line of text: (a) can have
* multiple DLines for one text line, and
* (b) can have gaps where DLine's have been
* deleted because they're out of date. */
int flags; /* Various flag bits: see below for values. */
} DLine;
/*
* Flag bits for DLine structures:
*
* NEW_LAYOUT - Non-zero means that the line has been
* re-layed out since the last time the
* display was updated.
* TOP_LINE - Non-zero means that this was the top line
* in the window the last time that the window
* was laid out. This is important because
* a line may be displayed differently if its
* at the top or bottom than if it's in the
* middle (e.g. beveled edges aren't displayed
* for middle lines if the adjacent line has
* a similar background).
* BOTTOM_LINE - Non-zero means that this was the bottom line
* in the window the last time that the window
* was laid out.
*/
#define NEW_LAYOUT 2
#define TOP_LINE 4
#define BOTTOM_LINE 8
/*
* Overall display information for a text widget:
*/
typedef struct DInfo {
Tcl_HashTable styleTable; /* Hash table that maps from StyleValues
* to Styles for this widget. */
DLine *dLinePtr; /* First in list of all display lines for
* this widget, in order from top to bottom. */
int x; /* First x-coordinate that may be used for
* actually displaying line information.
* Leaves space for border, etc. */
int y; /* First y-coordinate that may be used for
* actually displaying line information.
* Leaves space for border, etc. */
int maxX; /* First x-coordinate to right of available
* space for displaying lines. */
int maxY; /* First y-coordinate below available
* space for displaying lines. */
int topOfEof; /* Top-most pixel (lowest y-value) that has
* been drawn in the appropriate fashion for
* the portion of the window after the last
* line of the text. This field is used to
* figure out when to redraw part or all of
* the eof field. */
/*
* Information used for scrolling:
*/
int newCharOffset; /* Desired x scroll position, measured as the
* number of average-size characters off-screen
* to the left for a line with no left
* margin. */
int curOffset; /* Actual x scroll position, measured as the
* number of chars off-screen to the left. */
int maxLength; /* Length in chars of longest line that's
* visible in window (length may exceed window
* size). If there's no wrapping, this will
* be zero. */
double xScrollFirst, xScrollLast;
/* Most recent values reported to horizontal
* scrollbar; used to eliminate unnecessary
* reports. */
double yScrollFirst, yScrollLast;
/* Most recent values reported to vertical
* scrollbar; used to eliminate unnecessary
* reports. */
/*
* Miscellaneous information:
*/
int dLinesInvalidated; /* This value is set to 1 whenever something
* happens that invalidates information in
* DLine structures; if a redisplay
* is in progress, it will see this and
* abort the redisplay. This is needed
* because, for example, an embedded window
* could change its size when it is first
* displayed, invalidating the DLine that
* is currently being displayed. If redisplay
* continues, it will use freed memory and
* could dump core. */
int flags; /* Various flag values: see below for
* definitions. */
} DInfo;
/*
* In CkTextDispChunk structures for character segments, the clientData
* field points to one of the following structures:
*/
typedef struct CharInfo {
int numChars; /* Number of characters to display. */
CkWindow *winPtr; /* For Ck_SetWindowAttr. */
char chars[4]; /* Characters to display. Actual size
* will be numChars, not 4. THIS MUST BE
* THE LAST FIELD IN THE STRUCTURE. */
} CharInfo;
/*
* Flag values for DInfo structures:
*
* DINFO_OUT_OF_DATE: Non-zero means that the DLine structures
* for this window are partially or completely
* out of date and need to be recomputed.
* REDRAW_PENDING: Means that a when-idle handler has been
* scheduled to update the display.
* REDRAW_BORDERS: Means window border or pad area has
* potentially been damaged and must be redrawn.
* REPICK_NEEDED: 1 means that the widget has been modified
* in a way that could change the current
* character (a different character might be
* under the mouse cursor now). Need to
* recompute the current character before
* the next redisplay.
*/
#define DINFO_OUT_OF_DATE 1
#define REDRAW_PENDING 2
#define REDRAW_BORDERS 4
#define REPICK_NEEDED 8
/*
* The following counters keep statistics about redisplay that can be
* checked to see how clever this code is at reducing redisplays.
*/
static int numRedisplays; /* Number of calls to DisplayText. */
static int linesRedrawn; /* Number of calls to DisplayDLine. */
/*
* Forward declarations for procedures defined later in this file:
*/
static void AdjustForTab _ANSI_ARGS_((CkText *textPtr,
CkTextTabArray *tabArrayPtr, int index,
CkTextDispChunk *chunkPtr));
static void CharBboxProc _ANSI_ARGS_((CkTextDispChunk *chunkPtr,
int index, int y, int lineHeight, int baseline,
int *xPtr, int *yPtr, int *widthPtr,
int *heightPtr));
static void CharDisplayProc _ANSI_ARGS_((CkTextDispChunk *chunkPtr,
int x, int y, int height, int baseline,
WINDOW *window, int screenY));
static int CharMeasureProc _ANSI_ARGS_((CkTextDispChunk *chunkPtr,
int x));
static void CharUndisplayProc _ANSI_ARGS_((CkText *textPtr,
CkTextDispChunk *chunkPtr));
static void DisplayDLine _ANSI_ARGS_((CkText *textPtr,
DLine *dlPtr, DLine *prevPtr, WINDOW *window));
static void DisplayText _ANSI_ARGS_((ClientData clientData));
static DLine * FindDLine _ANSI_ARGS_((DLine *dlPtr,
CkTextIndex *indexPtr));
static void FreeDLines _ANSI_ARGS_((CkText *textPtr,
DLine *firstPtr, DLine *lastPtr, int unlink));
static void FreeStyle _ANSI_ARGS_((CkText *textPtr,
Style *stylePtr));
static Style * GetStyle _ANSI_ARGS_((CkText *textPtr,
CkTextIndex *indexPtr));
static void GetXView _ANSI_ARGS_((Tcl_Interp *interp,
CkText *textPtr, int report));
static void GetYView _ANSI_ARGS_((Tcl_Interp *interp,
CkText *textPtr, int report));
static DLine * LayoutDLine _ANSI_ARGS_((CkText *textPtr,
CkTextIndex *indexPtr));
static void MeasureUp _ANSI_ARGS_((CkText *textPtr,
CkTextIndex *srcPtr, int distance,
CkTextIndex *dstPtr));
static void UpdateDisplayInfo _ANSI_ARGS_((CkText *textPtr));
static void ScrollByLines _ANSI_ARGS_((CkText *textPtr,
int offset));
static int SizeOfTab _ANSI_ARGS_((CkText *textPtr,
CkTextTabArray *tabArrayPtr, int index, int x,
int maxX));
/*
*----------------------------------------------------------------------
*
* CkTextCreateDInfo --
*
* This procedure is called when a new text widget is created.
* Its job is to set up display-related information for the widget.
*
* Results:
* None.
*
* Side effects:
* A DInfo data structure is allocated and initialized and attached
* to textPtr.
*
*----------------------------------------------------------------------
*/
void
CkTextCreateDInfo(textPtr)
CkText *textPtr; /* Overall information for text widget. */
{
register DInfo *dInfoPtr;
dInfoPtr = (DInfo *) ckalloc(sizeof(DInfo));
Tcl_InitHashTable(&dInfoPtr->styleTable, sizeof(StyleValues)/sizeof(int));
dInfoPtr->dLinePtr = NULL;
dInfoPtr->topOfEof = 0;
dInfoPtr->newCharOffset = 0;
dInfoPtr->curOffset = 0;
dInfoPtr->maxLength = 0;
dInfoPtr->xScrollFirst = -1;
dInfoPtr->xScrollLast = -1;
dInfoPtr->yScrollFirst = -1;
dInfoPtr->yScrollLast = -1;
dInfoPtr->dLinesInvalidated = 0;
dInfoPtr->flags = DINFO_OUT_OF_DATE;
textPtr->dInfoPtr = dInfoPtr;
}
/*
*----------------------------------------------------------------------
*
* CkTextFreeDInfo --
*
* This procedure is called to free up all of the private display
* information kept by this file for a text widget.
*
* Results:
* None.
*
* Side effects:
* Lots of resources get freed.
*
*----------------------------------------------------------------------
*/
void
CkTextFreeDInfo(textPtr)
CkText *textPtr; /* Overall information for text widget. */
{
register DInfo *dInfoPtr = textPtr->dInfoPtr;
/*
* Be careful to free up styleTable *after* freeing up all the
* DLines, so that the hash table is still intact to free up the
* style-related information from the lines. Once the lines are
* all free then styleTable will be empty.
*/
FreeDLines(textPtr, dInfoPtr->dLinePtr, (DLine *) NULL, 1);
Tcl_DeleteHashTable(&dInfoPtr->styleTable);
if (dInfoPtr->flags & REDRAW_PENDING) {
Tk_CancelIdleCall(DisplayText, (ClientData) textPtr);
}
ckfree((char *) dInfoPtr);
}
/*
*----------------------------------------------------------------------
*
* GetStyle --
*
* This procedure creates all the information needed to display
* text at a particular location.
*
* Results:
* The return value is a pointer to a Style structure that
* corresponds to *sValuePtr.
*
* Side effects:
* A new entry may be created in the style table for the widget.
*
*----------------------------------------------------------------------
*/
static Style *
GetStyle(textPtr, indexPtr)
CkText *textPtr; /* Overall information about text widget. */
CkTextIndex *indexPtr; /* The character in the text for which
* display information is wanted. */
{
CkTextTag **tagPtrs;
register CkTextTag *tagPtr;
StyleValues styleValues;
Style *stylePtr;
Tcl_HashEntry *hPtr;
int numTags, new, i;
/*
* The variables below keep track of the highest-priority specification
* that has occurred for each of the various fields of the StyleValues.
*/
int bgPrio, fgPrio, attrPrio, justifyPrio;
int lMargin1Prio, lMargin2Prio, rMarginPrio;
int tabPrio, wrapPrio;
/*
* Find out what tags are present for the character, then compute
* a StyleValues structure corresponding to those tags (scan
* through all of the tags, saving information for the highest-
* priority tag).
*/
tagPtrs = CkBTreeGetTags(indexPtr, &numTags);
bgPrio = fgPrio = attrPrio = justifyPrio = -1;
lMargin1Prio = lMargin2Prio = rMarginPrio = -1;
tabPrio = wrapPrio = -1;
memset((VOID *) &styleValues, 0, sizeof(StyleValues));
styleValues.fg = textPtr->fg;
styleValues.bg = textPtr->bg;
styleValues.attr = textPtr->attr;
styleValues.justify = CK_JUSTIFY_LEFT;
styleValues.tabArrayPtr = textPtr->tabArrayPtr;
styleValues.wrapMode = textPtr->wrapMode;
for (i = 0 ; i < numTags; i++) {
tagPtr = tagPtrs[i];
if ((tagPtr->bg != -1) && (tagPtr->priority > bgPrio)) {
styleValues.bg = tagPtr->bg;
bgPrio = tagPtr->priority;
}
if ((tagPtr->fg != -1) && (tagPtr->priority > fgPrio)) {
styleValues.fg = tagPtr->fg;
fgPrio = tagPtr->priority;
}
if ((tagPtr->attr != -1) && (tagPtr->priority > attrPrio)) {
styleValues.attr = tagPtr->attr;
attrPrio = tagPtr->priority;
}
if ((tagPtr->justifyString != NULL)
&& (tagPtr->priority > justifyPrio)) {
styleValues.justify = tagPtr->justify;
justifyPrio = tagPtr->priority;
}
if ((tagPtr->lMargin1String != NULL)
&& (tagPtr->priority > lMargin1Prio)) {
styleValues.lMargin1 = tagPtr->lMargin1;
lMargin1Prio = tagPtr->priority;
}
if ((tagPtr->lMargin2String != NULL)
&& (tagPtr->priority > lMargin2Prio)) {
styleValues.lMargin2 = tagPtr->lMargin2;
lMargin2Prio = tagPtr->priority;
}
if ((tagPtr->rMarginString != NULL)
&& (tagPtr->priority > rMarginPrio)) {
styleValues.rMargin = tagPtr->rMargin;
rMarginPrio = tagPtr->priority;
}
if ((tagPtr->tabString != NULL)
&& (tagPtr->priority > tabPrio)) {
styleValues.tabArrayPtr = tagPtr->tabArrayPtr;
tabPrio = tagPtr->priority;
}
if ((tagPtr->wrapMode != NULL)
&& (tagPtr->priority > wrapPrio)) {
styleValues.wrapMode = tagPtr->wrapMode;
wrapPrio = tagPtr->priority;
}
}
if (tagPtrs != NULL) {
ckfree((char *) tagPtrs);
}
/*
* Use an existing style if there's one around that matches.
*/
hPtr = Tcl_CreateHashEntry(&textPtr->dInfoPtr->styleTable,
(char *) &styleValues, &new);
if (!new) {
stylePtr = (Style *) Tcl_GetHashValue(hPtr);
stylePtr->refCount++;
return stylePtr;
}
/*
* No existing style matched. Make a new one.
*/
stylePtr = (Style *) ckalloc(sizeof(Style));
stylePtr->refCount = 1;
stylePtr->sValuePtr = (StyleValues *)
Tcl_GetHashKey(&textPtr->dInfoPtr->styleTable, hPtr);
stylePtr->hPtr = hPtr;
Tcl_SetHashValue(hPtr, stylePtr);
return stylePtr;
}
/*
*----------------------------------------------------------------------
*
* FreeStyle --
*
* This procedure is called when a Style structure is no longer
* needed. It decrements the reference count and frees up the
* space for the style structure if the reference count is 0.
*
* Results:
* None.
*
* Side effects:
* The storage and other resources associated with the style
* are freed up if no-one's still using it.
*
*----------------------------------------------------------------------
*/
static void
FreeStyle(textPtr, stylePtr)
CkText *textPtr; /* Information about overall widget. */
register Style *stylePtr; /* Information about style to be freed. */
{
stylePtr->refCount--;
if (stylePtr->refCount == 0) {
Tcl_DeleteHashEntry(stylePtr->hPtr);
ckfree((char *) stylePtr);
}
}
/*
*----------------------------------------------------------------------
*
* LayoutDLine --
*
* This procedure generates a single DLine structure for a display
* line whose leftmost character is given by indexPtr.
*
* Results:
* The return value is a pointer to a DLine structure desribing the
* display line. All fields are filled in and correct except for
* y and nextPtr.
*
* Side effects:
* Storage is allocated for the new DLine.
*
*----------------------------------------------------------------------
*/
static DLine *
LayoutDLine(textPtr, indexPtr)
CkText *textPtr; /* Overall information about text widget. */
CkTextIndex *indexPtr; /* Beginning of display line. May not
* necessarily point to a character segment. */
{
register DLine *dlPtr; /* New display line. */
CkTextSegment *segPtr; /* Current segment in text. */
CkTextDispChunk *lastChunkPtr; /* Last chunk allocated so far
* for line. */
CkTextDispChunk *chunkPtr; /* Current chunk. */
CkTextIndex curIndex;
CkTextDispChunk *breakChunkPtr; /* Chunk containing best word break
* point, if any. */
CkTextIndex breakIndex; /* Index of first character in
* breakChunkPtr. */
int breakCharOffset; /* Character within breakChunkPtr just
* to right of best break point. */
int noCharsYet; /* Non-zero means that no characters
* have been placed on the line yet. */
int justify; /* How to justify line: taken from
* style for first character in line. */
int jIndent; /* Additional indentation (beyond
* margins) due to justification. */
int rMargin; /* Right margin width for line. */
Ck_Uid wrapMode; /* Wrap mode to use for this line. */
int x = 0, maxX = 0; /* Initializations needed only to
* stop compiler warnings. */
int wholeLine; /* Non-zero means this display line
* runs to the end of the text line. */
int tabIndex; /* Index of the current tab stop. */
int gotTab; /* Non-zero means the current chunk
* contains a tab. */
CkTextDispChunk *tabChunkPtr; /* Pointer to the chunk containing
* the previous tab stop. */
int maxChars; /* Maximum number of characters to
* include in this chunk. */
CkTextTabArray *tabArrayPtr; /* Tab stops for line; taken from
* style for first character on line. */
int tabSize; /* Number of pixels consumed by current
* tab stop. */
int offset, code;
StyleValues *sValuePtr;
/*
* Create and initialize a new DLine structure.
*/
dlPtr = (DLine *) ckalloc(sizeof(DLine));
dlPtr->index = *indexPtr;
dlPtr->count = 0;
dlPtr->y = 0;
dlPtr->oldY = -1;
dlPtr->height = 0;
dlPtr->chunkPtr = NULL;
dlPtr->nextPtr = NULL;
dlPtr->flags = NEW_LAYOUT;
/*
* Each iteration of the loop below creates one CkTextDispChunk for
* the new display line. The line will always have at least one
* chunk (for the newline character at the end, if there's nothing
* else available).
*/
curIndex = *indexPtr;
lastChunkPtr = NULL;
chunkPtr = NULL;
noCharsYet = 1;
breakChunkPtr = NULL;
breakCharOffset = 0;
justify = CK_JUSTIFY_LEFT;
tabIndex = -1;
tabChunkPtr = NULL;
tabArrayPtr = NULL;
rMargin = 0;
wrapMode = ckTextCharUid;
tabSize = 0;
/*
* Find the first segment to consider for the line. Can't call
* CkTextIndexToSeg for this because it won't return a segment
* with zero size (such as the insertion cursor's mark).
*/
for (offset = curIndex.charIndex, segPtr = curIndex.linePtr->segPtr;
(offset > 0) && (offset >= segPtr->size);
offset -= segPtr->size, segPtr = segPtr->nextPtr) {
/* Empty loop body. */
}
while (segPtr != NULL) {
if (segPtr->typePtr->layoutProc == NULL) {
segPtr = segPtr->nextPtr;
offset = 0;
continue;
}
if (chunkPtr == NULL) {
chunkPtr = (CkTextDispChunk *) ckalloc(sizeof(CkTextDispChunk));
chunkPtr->nextPtr = NULL;
}
chunkPtr->stylePtr = GetStyle(textPtr, &curIndex);
/*
* Save style information such as justification and indentation,
* up until the first character is encountered, then retain that
* information for the rest of the line.
*/
if (noCharsYet) {
tabArrayPtr = chunkPtr->stylePtr->sValuePtr->tabArrayPtr;
justify = chunkPtr->stylePtr->sValuePtr->justify;
rMargin = chunkPtr->stylePtr->sValuePtr->rMargin;
wrapMode = chunkPtr->stylePtr->sValuePtr->wrapMode;
x = ((curIndex.charIndex == 0)
? chunkPtr->stylePtr->sValuePtr->lMargin1
: chunkPtr->stylePtr->sValuePtr->lMargin2);
if (wrapMode == ckTextNoneUid) {
maxX = INT_MAX;
} else {
maxX = textPtr->dInfoPtr->maxX - textPtr->dInfoPtr->x
- rMargin;
if (maxX < x) {
maxX = x;
}
}
}
/*
* See if there is a tab in the current chunk; if so, only
* layout characters up to (and including) the tab.
*/
gotTab = 0;
maxChars = segPtr->size - offset;
if (justify == CK_JUSTIFY_LEFT) {
if (segPtr->typePtr == &ckTextCharType) {
char *p;
for (p = segPtr->body.chars + offset; *p != 0; p++) {
if (*p == '\t') {
maxChars = (p + 1 - segPtr->body.chars) - offset;
gotTab = 1;
break;
}
}
}
}
chunkPtr->x = x;
code = (*segPtr->typePtr->layoutProc)(textPtr, &curIndex, segPtr,
offset, maxX-tabSize, maxChars, noCharsYet, wrapMode,
chunkPtr);
if (code <= 0) {
FreeStyle(textPtr, chunkPtr->stylePtr);
if (code < 0) {
/*
* This segment doesn't wish to display itself (e.g. most
* marks).
*/
segPtr = segPtr->nextPtr;
offset = 0;
continue;
}
/*
* No characters from this segment fit in the window: this
* means we're at the end of the display line.
*/
if (chunkPtr != NULL) {
ckfree((char *) chunkPtr);
}
break;
}
if (chunkPtr->numChars > 0) {
noCharsYet = 0;
}
if (lastChunkPtr == NULL) {
dlPtr->chunkPtr = chunkPtr;
} else {
lastChunkPtr->nextPtr = chunkPtr;
}
lastChunkPtr = chunkPtr;
x += chunkPtr->width;
if (chunkPtr->breakIndex > 0) {
breakCharOffset = chunkPtr->breakIndex;
breakIndex = curIndex;
breakChunkPtr = chunkPtr;
}
if (chunkPtr->numChars != maxChars) {
break;
}
/*
* If we're at a new tab, adjust the layout for all the chunks
* pertaining to the previous tab. Also adjust the amount of
* space left in the line to account for space that will be eaten
* up by the tab.
*/
if (gotTab) {
if (tabIndex >= 0) {
AdjustForTab(textPtr, tabArrayPtr, tabIndex, tabChunkPtr);
x = chunkPtr->x + chunkPtr->width;
}
tabIndex++;
tabChunkPtr = chunkPtr;
tabSize = SizeOfTab(textPtr, tabArrayPtr, tabIndex, x, maxX);
if (tabSize >= (maxX - x)) {
break;
}
}
curIndex.charIndex += chunkPtr->numChars;
offset += chunkPtr->numChars;
if (offset >= segPtr->size) {
offset = 0;
segPtr = segPtr->nextPtr;
}
chunkPtr = NULL;
}
if (noCharsYet) {
panic("LayoutDLine couldn't place any characters on a line");
}
wholeLine = (segPtr == NULL);
/*
* We're at the end of the display line. Throw away everything
* after the most recent word break, if there is one; this may
* potentially require the last chunk to be layed out again.
*/
if ((breakChunkPtr != NULL) && ((lastChunkPtr != breakChunkPtr)
|| (breakCharOffset != lastChunkPtr->numChars))) {
while (1) {
chunkPtr = breakChunkPtr->nextPtr;
if (chunkPtr == NULL) {
break;
}
FreeStyle(textPtr, chunkPtr->stylePtr);
breakChunkPtr->nextPtr = chunkPtr->nextPtr;
(*chunkPtr->undisplayProc)(textPtr, chunkPtr);
ckfree((char *) chunkPtr);
}
if (breakCharOffset != breakChunkPtr->numChars) {
(*breakChunkPtr->undisplayProc)(textPtr, breakChunkPtr);
segPtr = CkTextIndexToSeg(&breakIndex, &offset);
(*segPtr->typePtr->layoutProc)(textPtr, &breakIndex,
segPtr, offset, maxX, breakCharOffset, 0,
wrapMode, breakChunkPtr);
}
lastChunkPtr = breakChunkPtr;
wholeLine = 0;
}
/*
* Make tab adjustments for the last tab stop, if there is one.
*/
if ((tabIndex >= 0) && (tabChunkPtr != NULL)) {
AdjustForTab(textPtr, tabArrayPtr, tabIndex, tabChunkPtr);
}
/*
* Make one more pass over the line to recompute various things
* like its height, length, and total number of characters. Also
* modify the x-locations of chunks to reflect justification.
* If we're not wrapping, I'm not sure what is the best way to
* handle left and center justification: should the total length,
* for purposes of justification, be (a) the window width, (b)
* the length of the longest line in the window, or (c) the length
* of the longest line in the text? (c) isn't available, (b) seems
* weird, since it can change with vertical scrolling, so (a) is
* what is implemented below.
*/
if (wrapMode == ckTextNoneUid) {
maxX = textPtr->dInfoPtr->maxX - textPtr->dInfoPtr->x - rMargin;
}
dlPtr->length = lastChunkPtr->x + lastChunkPtr->width;
if (justify == CK_JUSTIFY_LEFT) {
jIndent = 0;
} else if (justify == CK_JUSTIFY_RIGHT) {
jIndent = maxX - dlPtr->length;
} else {
jIndent = (maxX - dlPtr->length)/2;
}
for (chunkPtr = dlPtr->chunkPtr; chunkPtr != NULL;
chunkPtr = chunkPtr->nextPtr) {
chunkPtr->x += jIndent;
dlPtr->count += chunkPtr->numChars;
if (chunkPtr->minHeight > dlPtr->height) {
dlPtr->height = chunkPtr->minHeight;
}
sValuePtr = chunkPtr->stylePtr->sValuePtr;
}
sValuePtr = dlPtr->chunkPtr->stylePtr->sValuePtr;
/*
* Recompute line length: may have changed because of justification.
*/
dlPtr->length = lastChunkPtr->x + lastChunkPtr->width;
return dlPtr;
}
/*
*----------------------------------------------------------------------
*
* UpdateDisplayInfo --
*
* This procedure is invoked to recompute some or all of the
* DLine structures for a text widget. At the time it is called
* the DLine structures still left in the widget are guaranteed
* to be correct except that (a) the y-coordinates aren't
* necessarily correct, (b) there may be missing structures
* (the DLine structures get removed as soon as they are potentially
* out-of-date), and (c) DLine structures that don't start at the
* beginning of a line may be incorrect if previous information in
* the same line changed size in a way that moved a line boundary
* (DLines for any info that changed will have been deleted, but
* not DLines for unchanged info in the same text line).
*
* Results:
* None.
*
* Side effects:
* Upon return, the DLine information for textPtr correctly reflects
* the positions where characters will be displayed. However, this
* procedure doesn't actually bring the display up-to-date.
*
*----------------------------------------------------------------------
*/
static void
UpdateDisplayInfo(textPtr)
CkText *textPtr; /* Text widget to update. */
{
register DInfo *dInfoPtr = textPtr->dInfoPtr;
register DLine *dlPtr, *prevPtr;
CkTextIndex index;
CkTextLine *lastLinePtr;
int y, maxY, maxOffset;
if (!(dInfoPtr->flags & DINFO_OUT_OF_DATE)) {
return;
}
dInfoPtr->flags &= ~DINFO_OUT_OF_DATE;
/*
* Delete any DLines that are now above the top of the window.
*/
index = textPtr->topIndex;
dlPtr = FindDLine(dInfoPtr->dLinePtr, &index);
if ((dlPtr != NULL) && (dlPtr != dInfoPtr->dLinePtr)) {
FreeDLines(textPtr, dInfoPtr->dLinePtr, dlPtr, 1);
}
/*
*--------------------------------------------------------------
* Scan through the contents of the window from top to bottom,
* recomputing information for lines that are missing.
*--------------------------------------------------------------
*/
lastLinePtr = CkBTreeFindLine(textPtr->tree,
CkBTreeNumLines(textPtr->tree));
dlPtr = dInfoPtr->dLinePtr;
prevPtr = NULL;
y = dInfoPtr->y;
maxY = dInfoPtr->maxY;
while (1) {
register DLine *newPtr;
if (index.linePtr == lastLinePtr) {
break;
}
/*
* There are three possibilities right now:
* (a) the next DLine (dlPtr) corresponds exactly to the next
* information we want to display: just use it as-is.
* (b) the next DLine corresponds to a different line, or to
* a segment that will be coming later in the same line:
* leave this DLine alone in the hopes that we'll be able
* to use it later, then create a new DLine in front of
* it.
* (c) the next DLine corresponds to a segment in the line we
* want, but it's a segment that has already been processed
* or will never be processed. Delete the DLine and try
* again.
*
* One other twist on all this. It's possible for 3D borders
* to interact between lines (see DisplayLineBackground) so if
* a line is relayed out and has styles with 3D borders, its
* neighbors have to be redrawn if they have 3D borders too,
* since the interactions could have changed (the neighbors
* don't have to be relayed out, just redrawn).
*/
if ((dlPtr == NULL) || (dlPtr->index.linePtr != index.linePtr)) {
/*
* Case (b) -- must make new DLine.
*/
makeNewDLine:
if (ckTextDebug) {
char string[TK_POS_CHARS];
/*
* Debugging is enabled, so keep a log of all the lines
* that were re-layed out. The test suite uses this
* information.
*/
CkTextPrintIndex(&index, string);
Tcl_SetVar2(textPtr->interp, "ck_textRelayout", (char *) NULL,
string,
TCL_GLOBAL_ONLY|TCL_APPEND_VALUE|TCL_LIST_ELEMENT);
}
newPtr = LayoutDLine(textPtr, &index);
if (prevPtr == NULL) {
dInfoPtr->dLinePtr = newPtr;
} else {
prevPtr->nextPtr = newPtr;
}
newPtr->nextPtr = dlPtr;
dlPtr = newPtr;
} else {
/*
* DlPtr refers to the line we want. Next check the
* index within the line.
*/
if (index.charIndex == dlPtr->index.charIndex) {
/*
* Case (a) -- can use existing display line as-is.
*/
goto lineOK;
}
if (index.charIndex < dlPtr->index.charIndex) {
goto makeNewDLine;
}
/*
* Case (c) -- dlPtr is useless. Discard it and start
* again with the next display line.
*/
newPtr = dlPtr->nextPtr;
FreeDLines(textPtr, dlPtr, newPtr, 0);
dlPtr = newPtr;
continue;