Debian Codegraph @

pTk/mTk/generic/tkTextDisp.c

raw symbols SCIP
1 /*
2 * tkTextDisp.c --
3 *
4 * This module provides facilities to display text widgets. It is
5 * the only place where information is kept about the screen layout
6 * of text widgets.
7 *
8 * Copyright (c) 1992-1994 The Regents of the University of California.
9 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
10 *
11 * See the file "license.terms" for information on usage and redistribution
12 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 *
14 * RCS: @(#) $Id: tkTextDisp.c,v 1.14 2002/11/22 23:25:19 hobbs Exp $
15 */
16
17 #include "tkPort.h"
18 #include "tkInt.h"
19 #include "tkText.h"
20
21 #ifdef __WIN32__
22 #include "tkWinInt.h"
23 #endif
24
25 #define MIN(a,b) ((a) < (b) ? a : b)
26
27 /*
28 * The following structure describes how to display a range of characters.
29 * The information is generated by scanning all of the tags associated
30 * with the characters and combining that with default information for
31 * the overall widget. These structures form the hash keys for
32 * dInfoPtr->styleTable.
33 */
34
35 typedef struct StyleValues {
36 Tk_3DBorder border; /* Used for drawing background under text.
37 * NULL means use widget background. */
38 int borderWidth; /* Width of 3-D border for background. */
39 int relief; /* 3-D relief for background. */
40 Pixmap bgStipple; /* Stipple bitmap for background. None
41 * means draw solid. */
42 XColor *fgColor; /* Foreground color for text. */
43 Tk_Font tkfont; /* Font for displaying text. */
44 Pixmap fgStipple; /* Stipple bitmap for text and other
45 * foreground stuff. None means draw
46 * solid.*/
47 int justify; /* Justification style for text. */
48 int lMargin1; /* Left margin, in pixels, for first display
49 * line of each text line. */
50 int lMargin2; /* Left margin, in pixels, for second and
51 * later display lines of each text line. */
52 int offset; /* Offset in pixels of baseline, relative to
53 * baseline of line. */
54 int overstrike; /* Non-zero means draw overstrike through
55 * text. */
56 int rMargin; /* Right margin, in pixels. */
57 int spacing1; /* Spacing above first dline in text line. */
58 int spacing2; /* Spacing between lines of dline. */
59 int spacing3; /* Spacing below last dline in text line. */
60 TkTextTabArray *tabArrayPtr;/* Locations and types of tab stops (may
61 * be NULL). */
62 int underline; /* Non-zero means draw underline underneath
63 * text. */
64 int elide; /* Non-zero means draw text */
65 TkWrapMode wrapMode; /* How to handle wrap-around for this tag.
66 * One of TEXT_WRAPMODE_CHAR,
67 * TEXT_WRAPMODE_NONE or TEXT_WRAPMODE_WORD.*/
68 } StyleValues;
69
70 /*
71 * The following structure extends the StyleValues structure above with
72 * graphics contexts used to actually draw the characters. The entries
73 * in dInfoPtr->styleTable point to structures of this type.
74 */
75
76 typedef struct TextStyle {
77 int refCount; /* Number of times this structure is
78 * referenced in Chunks. */
79 GC bgGC; /* Graphics context for background. None
80 * means use widget background. */
81 GC fgGC; /* Graphics context for foreground. */
82 StyleValues *sValuePtr; /* Raw information from which GCs were
83 * derived. */
84 Tcl_HashEntry *hPtr; /* Pointer to entry in styleTable. Used
85 * to delete entry. */
86 } TextStyle;
87
88 /*
89 * The following macro determines whether two styles have the same
90 * background so that, for example, no beveled border should be drawn
91 * between them.
92 */
93
94 #define SAME_BACKGROUND(s1, s2) \
95 (((s1)->sValuePtr->border == (s2)->sValuePtr->border) \
96 && ((s1)->sValuePtr->borderWidth == (s2)->sValuePtr->borderWidth) \
97 && ((s1)->sValuePtr->relief == (s2)->sValuePtr->relief) \
98 && ((s1)->sValuePtr->bgStipple == (s2)->sValuePtr->bgStipple))
99
100 /*
101 * The following macro is used to compare two floating-point numbers
102 * to within a certain degree of scale. Direct comparison fails on
103 * processors where the processor and memory representations of FP
104 * numbers of a particular precision is different (e.g. Intel)
105 */
106
107 #define FP_EQUAL_SCALE(double1, double2, scaleFactor) \
108 (fabs((double1)-(double2))*((scaleFactor)+1.0) < 0.3)
109
110 /*
111 * The following structure describes one line of the display, which may
112 * be either part or all of one line of the text.
113 */
114
115 typedef struct DLine {
116 TkTextIndex index; /* Identifies first character in text
117 * that is displayed on this line. */
118 int byteCount; /* Number of bytes accounted for by this
119 * display line, including a trailing space
120 * or newline that isn't actually displayed. */
121 int y; /* Y-position at which line is supposed to
122 * be drawn (topmost pixel of rectangular
123 * area occupied by line). */
124 int oldY; /* Y-position at which line currently
125 * appears on display. -1 means line isn't
126 * currently visible on display and must be
127 * redrawn. This is used to move lines by
128 * scrolling rather than re-drawing. */
129 int height; /* Height of line, in pixels. */
130 int baseline; /* Offset of text baseline from y, in
131 * pixels. */
132 int spaceAbove; /* How much extra space was added to the
133 * top of the line because of spacing
134 * options. This is included in height
135 * and baseline. */
136 int spaceBelow; /* How much extra space was added to the
137 * bottom of the line because of spacing
138 * options. This is included in height. */
139 int length; /* Total length of line, in pixels. */
140 TkTextDispChunk *chunkPtr; /* Pointer to first chunk in list of all
141 * of those that are displayed on this
142 * line of the screen. */
143 struct DLine *nextPtr; /* Next in list of all display lines for
144 * this window. The list is sorted in
145 * order from top to bottom. Note: the
146 * next DLine doesn't always correspond
147 * to the next line of text: (a) can have
148 * multiple DLines for one text line, and
149 * (b) can have gaps where DLine's have been
150 * deleted because they're out of date. */
151 int flags; /* Various flag bits: see below for values. */
152 } DLine;
153
154 /*
155 * Flag bits for DLine structures:
156 *
157 * HAS_3D_BORDER - Non-zero means that at least one of the
158 * chunks in this line has a 3D border, so
159 * it potentially interacts with 3D borders
160 * in neighboring lines (see
161 * DisplayLineBackground).
162 * NEW_LAYOUT - Non-zero means that the line has been
163 * re-layed out since the last time the
164 * display was updated.
165 * TOP_LINE - Non-zero means that this was the top line
166 * in the window the last time that the window
167 * was laid out. This is important because
168 * a line may be displayed differently if its
169 * at the top or bottom than if it's in the
170 * middle (e.g. beveled edges aren't displayed
171 * for middle lines if the adjacent line has
172 * a similar background).
173 * BOTTOM_LINE - Non-zero means that this was the bottom line
174 * in the window the last time that the window
175 * was laid out.
176 * IS_DISABLED - This Dline cannot be edited.
177 */
178
179 #define HAS_3D_BORDER 1
180 #define NEW_LAYOUT 2
181 #define TOP_LINE 4
182 #define BOTTOM_LINE 8
183 #define IS_DISABLED 16
184
185 /*
186 * Overall display information for a text widget:
187 */
188
189 typedef struct TextDInfo {
190 Tcl_HashTable styleTable; /* Hash table that maps from StyleValues
191 * to TextStyles for this widget. */
192 DLine *dLinePtr; /* First in list of all display lines for
193 * this widget, in order from top to bottom. */
194 GC copyGC; /* Graphics context for copying from off-
195 * screen pixmaps onto screen. */
196 GC scrollGC; /* Graphics context for copying from one place
197 * in the window to another (scrolling):
198 * differs from copyGC in that we need to get
199 * GraphicsExpose events. */
200 int x; /* First x-coordinate that may be used for
201 * actually displaying line information.
202 * Leaves space for border, etc. */
203 int y; /* First y-coordinate that may be used for
204 * actually displaying line information.
205 * Leaves space for border, etc. */
206 int maxX; /* First x-coordinate to right of available
207 * space for displaying lines. */
208 int maxY; /* First y-coordinate below available
209 * space for displaying lines. */
210 int topOfEof; /* Top-most pixel (lowest y-value) that has
211 * been drawn in the appropriate fashion for
212 * the portion of the window after the last
213 * line of the text. This field is used to
214 * figure out when to redraw part or all of
215 * the eof field. */
216
217 /*
218 * Information used for scrolling:
219 */
220
221 int newByteOffset; /* Desired x scroll position, measured as the
222 * number of average-size characters off-screen
223 * to the left for a line with no left
224 * margin. */
225 int curPixelOffset; /* Actual x scroll position, measured as the
226 * number of pixels off-screen to the left. */
227 int maxLength; /* Length in pixels of longest line that's
228 * visible in window (length may exceed window
229 * size). If there's no wrapping, this will
230 * be zero. */
231 double xScrollFirst, xScrollLast;
232 /* Most recent values reported to horizontal
233 * scrollbar; used to eliminate unnecessary
234 * reports. */
235 double yScrollFirst, yScrollLast;
236 /* Most recent values reported to vertical
237 * scrollbar; used to eliminate unnecessary
238 * reports. */
239
240 /*
241 * The following information is used to implement scanning:
242 */
243
244 int scanMarkIndex; /* Byte index of character that was at the
245 * left edge of the window when the scan
246 * started. */
247 int scanMarkX; /* X-position of mouse at time scan started. */
248 int scanTotalScroll; /* Total scrolling (in screen lines) that has
249 * occurred since scanMarkY was set. */
250 int scanMarkY; /* Y-position of mouse at time scan started. */
251
252 /*
253 * Miscellaneous information:
254 */
255
256 int dLinesInvalidated; /* This value is set to 1 whenever something
257 * happens that invalidates information in
258 * DLine structures; if a redisplay
259 * is in progress, it will see this and
260 * abort the redisplay. This is needed
261 * because, for example, an embedded window
262 * could change its size when it is first
263 * displayed, invalidating the DLine that
264 * is currently being displayed. If redisplay
265 * continues, it will use freed memory and
266 * could dump core. */
267 int flags; /* Various flag values: see below for
268 * definitions. */
269 } TextDInfo;
270
271 /*
272 * In TkTextDispChunk structures for character segments, the clientData
273 * field points to one of the following structures:
274 */
275
276 typedef struct CharInfo {
277 int numBytes; /* Number of bytes to display. */
278 char chars[4]; /* UTF characters to display. Actual size
279 * will be numBytes, not 4. THIS MUST BE
280 * THE LAST FIELD IN THE STRUCTURE. */
281 } CharInfo;
282
283 /*
284 * Flag values for TextDInfo structures:
285 *
286 * DINFO_OUT_OF_DATE: Non-zero means that the DLine structures
287 * for this window are partially or completely
288 * out of date and need to be recomputed.
289 * REDRAW_PENDING: Means that a when-idle handler has been
290 * scheduled to update the display.
291 * REDRAW_BORDERS: Means window border or pad area has
292 * potentially been damaged and must be redrawn.
293 * REPICK_NEEDED: 1 means that the widget has been modified
294 * in a way that could change the current
295 * character (a different character might be
296 * under the mouse cursor now). Need to
297 * recompute the current character before
298 * the next redisplay.
299 */
300
301 #define DINFO_OUT_OF_DATE 1
302 #define REDRAW_PENDING 2
303 #define REDRAW_BORDERS 4
304 #define REPICK_NEEDED 8
305
306 /*
307 * The following counters keep statistics about redisplay that can be
308 * checked to see how clever this code is at reducing redisplays.
309 */
310
311 static int numRedisplays; /* Number of calls to DisplayText. */
312 static int linesRedrawn; /* Number of calls to DisplayDLine. */
313 static int numCopies; /* Number of calls to XCopyArea to copy part
314 * of the screen. */
315
316 /*
317 * Forward declarations for procedures defined later in this file:
318 */
319
320 static void AdjustForTab _ANSI_ARGS_((TkText *textPtr,
321 TkTextTabArray *tabArrayPtr, int index,
322 TkTextDispChunk *chunkPtr));
323 static void CharBboxProc _ANSI_ARGS_((TkTextDispChunk *chunkPtr,
324 int index, int y, int lineHeight, int baseline,
325 int *xPtr, int *yPtr, int *widthPtr,
326 int *heightPtr));
327 static void CharDisplayProc _ANSI_ARGS_((TkTextDispChunk *chunkPtr,
328 int x, int y, int height, int baseline,
329 Display *display, Drawable dst, int screenY));
330 static int CharMeasureProc _ANSI_ARGS_((TkTextDispChunk *chunkPtr,
331 int x));
332 static void CharUndisplayProc _ANSI_ARGS_((TkText *textPtr,
333 TkTextDispChunk *chunkPtr));
334
335 /*
336 Definitions of elided procs.
337 Compiler can't inline these since we use pointers to these functions.
338 ElideDisplayProc, ElideUndisplayProc special-cased for speed,
339 as potentially many elided DLine chunks if large, tag toggle-filled
340 elided region.
341 */
342 static void ElideBboxProc _ANSI_ARGS_((TkTextDispChunk *chunkPtr,
343 int index, int y, int lineHeight, int baseline,
344 int *xPtr, int *yPtr, int *widthPtr,
345 int *heightPtr));
346 static int ElideMeasureProc _ANSI_ARGS_((TkTextDispChunk *chunkPtr,
347 int x));
348
349 static void DisplayDLine _ANSI_ARGS_((TkText *textPtr,
350 DLine *dlPtr, DLine *prevPtr, Pixmap pixmap));
351 static void DisplayLineBackground _ANSI_ARGS_((TkText *textPtr,
352 DLine *dlPtr, DLine *prevPtr, Pixmap pixmap));
353 static void DisplayText _ANSI_ARGS_((ClientData clientData));
354 static DLine * FindDLine _ANSI_ARGS_((DLine *dlPtr,
355 TkTextIndex *indexPtr));
356 static void FreeDLines _ANSI_ARGS_((TkText *textPtr,
357 DLine *firstPtr, DLine *lastPtr, int unlink));
358 static void FreeStyle _ANSI_ARGS_((TkText *textPtr,
359 TextStyle *stylePtr));
360 static TextStyle * GetStyle _ANSI_ARGS_((TkText *textPtr,
361 TkTextIndex *indexPtr));
362 static void GetXView _ANSI_ARGS_((Tcl_Interp *interp,
363 TkText *textPtr, int report));
364 static void GetYView _ANSI_ARGS_((Tcl_Interp *interp,
365 TkText *textPtr, int report));
366 static DLine * LayoutDLine _ANSI_ARGS_((TkText *textPtr,
367 TkTextIndex *indexPtr));
368 static int MeasureChars _ANSI_ARGS_((Tk_Font tkfont,
369 CONST char *source, int maxBytes, int startX,
370 int maxX, int tabOrigin, int *nextXPtr));
371 static void MeasureUp _ANSI_ARGS_((TkText *textPtr,
372 TkTextIndex *srcPtr, int distance,
373 TkTextIndex *dstPtr));
374 static int NextTabStop _ANSI_ARGS_((Tk_Font tkfont, int x,
375 int tabOrigin));
376 static void UpdateDisplayInfo _ANSI_ARGS_((TkText *textPtr));
377 static void ScrollByLines _ANSI_ARGS_((TkText *textPtr,
378 int offset));
379 static int SizeOfTab _ANSI_ARGS_((TkText *textPtr,
380 TkTextTabArray *tabArrayPtr, int index, int x,
381 int maxX));
382 static void TextInvalidateRegion _ANSI_ARGS_((TkText *textPtr,
383 TkRegion region));
384
385
386 /*
387 *----------------------------------------------------------------------
388 *
389 * TkTextCreateDInfo --
390 *
391 * This procedure is called when a new text widget is created.
392 * Its job is to set up display-related information for the widget.
393 *
394 * Results:
395 * None.
396 *
397 * Side effects:
398 * A TextDInfo data structure is allocated and initialized and attached
399 * to textPtr.
400 *
401 *----------------------------------------------------------------------
402 */
403
404 void
405 TkTextCreateDInfo(textPtr)
406 TkText *textPtr; /* Overall information for text widget. */
407 {
408 register TextDInfo *dInfoPtr;
409 XGCValues gcValues;
410
411 dInfoPtr = (TextDInfo *) ckalloc(sizeof(TextDInfo));
412 Tcl_InitHashTable(&dInfoPtr->styleTable, sizeof(StyleValues)/sizeof(int));
413 dInfoPtr->dLinePtr = NULL;
414 dInfoPtr->copyGC = None;
415 gcValues.graphics_exposures = True;
416 dInfoPtr->scrollGC = Tk_GetGC(textPtr->tkwin, GCGraphicsExposures,
417 &gcValues);
418 dInfoPtr->topOfEof = 0;
419 dInfoPtr->newByteOffset = 0;
420 dInfoPtr->curPixelOffset = 0;
421 dInfoPtr->maxLength = 0;
422 dInfoPtr->xScrollFirst = -1;
423 dInfoPtr->xScrollLast = -1;
424 dInfoPtr->yScrollFirst = -1;
425 dInfoPtr->yScrollLast = -1;
426 dInfoPtr->scanMarkIndex = 0;
427 dInfoPtr->scanMarkX = 0;
428 dInfoPtr->scanTotalScroll = 0;
429 dInfoPtr->scanMarkY = 0;
430 dInfoPtr->dLinesInvalidated = 0;
431 dInfoPtr->flags = DINFO_OUT_OF_DATE;
432 textPtr->dInfoPtr = dInfoPtr;
433 }
434
435 /*
436 *----------------------------------------------------------------------
437 *
438 * TkTextFreeDInfo --
439 *
440 * This procedure is called to free up all of the private display
441 * information kept by this file for a text widget.
442 *
443 * Results:
444 * None.
445 *
446 * Side effects:
447 * Lots of resources get freed.
448 *
449 *----------------------------------------------------------------------
450 */
451
452 void
453 TkTextFreeDInfo(textPtr)
454 TkText *textPtr; /* Overall information for text widget. */
455 {
456 register TextDInfo *dInfoPtr = textPtr->dInfoPtr;
457
458 /*
459 * Be careful to free up styleTable *after* freeing up all the
460 * DLines, so that the hash table is still intact to free up the
461 * style-related information from the lines. Once the lines are
462 * all free then styleTable will be empty.
463 */
464
465 FreeDLines(textPtr, dInfoPtr->dLinePtr, (DLine *) NULL, 1);
466 Tcl_DeleteHashTable(&dInfoPtr->styleTable);
467 if (dInfoPtr->copyGC != None) {
468 Tk_FreeGC(textPtr->display, dInfoPtr->copyGC);
469 }
470 Tk_FreeGC(textPtr->display, dInfoPtr->scrollGC);
471 if (dInfoPtr->flags & REDRAW_PENDING) {
472 Tcl_CancelIdleCall(DisplayText, (ClientData) textPtr);
473 }
474 ckfree((char *) dInfoPtr);
475 }
476
477 /*
478 *----------------------------------------------------------------------
479 *
480 * GetStyle --
481 *
482 * This procedure creates all the information needed to display
483 * text at a particular location.
484 *
485 * Results:
486 * The return value is a pointer to a TextStyle structure that
487 * corresponds to *sValuePtr.
488 *
489 * Side effects:
490 * A new entry may be created in the style table for the widget.
491 *
492 *----------------------------------------------------------------------
493 */
494
495 static TextStyle *
496 GetStyle(textPtr, indexPtr)
497 TkText *textPtr; /* Overall information about text widget. */
498 TkTextIndex *indexPtr; /* The character in the text for which
499 * display information is wanted. */
500 {
501 TkTextTag **tagPtrs;
502 register TkTextTag *tagPtr;
503 StyleValues styleValues;
504 TextStyle *stylePtr;
505 Tcl_HashEntry *hPtr;
506 int numTags, new, i;
507 XGCValues gcValues;
508 unsigned long mask;
509
510 /*
511 * The variables below keep track of the highest-priority specification
512 * that has occurred for each of the various fields of the StyleValues.
513 */
514
515 int borderPrio, borderWidthPrio, reliefPrio, bgStipplePrio;
516 int fgPrio, fontPrio, fgStipplePrio;
517 int underlinePrio, elidePrio, justifyPrio, offsetPrio;
518 int lMargin1Prio, lMargin2Prio, rMarginPrio;
519 int spacing1Prio, spacing2Prio, spacing3Prio;
520 int overstrikePrio, tabPrio, wrapPrio;
521
522 /*
523 * Find out what tags are present for the character, then compute
524 * a StyleValues structure corresponding to those tags (scan
525 * through all of the tags, saving information for the highest-
526 * priority tag).
527 */
528
529 tagPtrs = TkBTreeGetTags(indexPtr, &numTags);
530 borderPrio = borderWidthPrio = reliefPrio = bgStipplePrio = -1;
531 fgPrio = fontPrio = fgStipplePrio = -1;
532 underlinePrio = elidePrio = justifyPrio = offsetPrio = -1;
533 lMargin1Prio = lMargin2Prio = rMarginPrio = -1;
534 spacing1Prio = spacing2Prio = spacing3Prio = -1;
535 overstrikePrio = tabPrio = wrapPrio = -1;
536 memset((VOID *) &styleValues, 0, sizeof(StyleValues));
537 styleValues.relief = TK_RELIEF_FLAT;
538 styleValues.fgColor = textPtr->fgColor;
539 styleValues.tkfont = textPtr->tkfont;
540 styleValues.justify = TK_JUSTIFY_LEFT;
541 styleValues.spacing1 = textPtr->spacing1;
542 styleValues.spacing2 = textPtr->spacing2;
543 styleValues.spacing3 = textPtr->spacing3;
544 styleValues.tabArrayPtr = textPtr->tabArrayPtr;
545 styleValues.wrapMode = textPtr->wrapMode;
546 styleValues.elide = 0;
547 for (i = 0 ; i < numTags; i++) {
548 tagPtr = tagPtrs[i];
549
550 /*
551 * On Windows and Mac, we need to skip the selection tag if
552 * we don't have focus.
553 */
554
555 #ifndef ALWAYS_SHOW_SELECTION
556 if ((tagPtr == textPtr->selTagPtr) && !(textPtr->flags & GOT_FOCUS)) {
557 continue;
558 }
559 #endif
560
561 if ((tagPtr->border != NULL) && (tagPtr->priority > borderPrio)) {
562 styleValues.border = tagPtr->border;
563 borderPrio = tagPtr->priority;
564 }
565 if ((tagPtr->bdString != NULL)
566 && (tagPtr->priority > borderWidthPrio)) {
567 styleValues.borderWidth = tagPtr->borderWidth;
568 borderWidthPrio = tagPtr->priority;
569 }
570 if ((tagPtr->reliefString != NULL)
571 && (tagPtr->priority > reliefPrio)) {
572 if (styleValues.border == NULL) {
573 styleValues.border = textPtr->border;
574 }
575 styleValues.relief = tagPtr->relief;
576 reliefPrio = tagPtr->priority;
577 }
578 if ((tagPtr->bgStipple != None)
579 && (tagPtr->priority > bgStipplePrio)) {
580 styleValues.bgStipple = tagPtr->bgStipple;
581 bgStipplePrio = tagPtr->priority;
582 }
583 if ((tagPtr->fgColor != None) && (tagPtr->priority > fgPrio)) {
584 styleValues.fgColor = tagPtr->fgColor;
585 fgPrio = tagPtr->priority;
586 }
587 if ((tagPtr->tkfont != None) && (tagPtr->priority > fontPrio)) {
588 styleValues.tkfont = tagPtr->tkfont;
589 fontPrio = tagPtr->priority;
590 }
591 if ((tagPtr->fgStipple != None)
592 && (tagPtr->priority > fgStipplePrio)) {
593 styleValues.fgStipple = tagPtr->fgStipple;
594 fgStipplePrio = tagPtr->priority;
595 }
596 if ((tagPtr->justifyString != NULL)
597 && (tagPtr->priority > justifyPrio)) {
598 styleValues.justify = tagPtr->justify;
599 justifyPrio = tagPtr->priority;
600 }
601 if ((tagPtr->lMargin1String != NULL)
602 && (tagPtr->priority > lMargin1Prio)) {
603 styleValues.lMargin1 = tagPtr->lMargin1;
604 lMargin1Prio = tagPtr->priority;
605 }
606 if ((tagPtr->lMargin2String != NULL)
607 && (tagPtr->priority > lMargin2Prio)) {
608 styleValues.lMargin2 = tagPtr->lMargin2;
609 lMargin2Prio = tagPtr->priority;
610 }
611 if ((tagPtr->offsetString != NULL)
612 && (tagPtr->priority > offsetPrio)) {
613 styleValues.offset = tagPtr->offset;
614 offsetPrio = tagPtr->priority;
615 }
616 if ((tagPtr->overstrikeString != NULL)
617 && (tagPtr->priority > overstrikePrio)) {
618 styleValues.overstrike = tagPtr->overstrike;
619 overstrikePrio = tagPtr->priority;
620 }
621 if ((tagPtr->rMarginString != NULL)
622 && (tagPtr->priority > rMarginPrio)) {
623 styleValues.rMargin = tagPtr->rMargin;
624 rMarginPrio = tagPtr->priority;
625 }
626 if ((tagPtr->spacing1String != NULL)
627 && (tagPtr->priority > spacing1Prio)) {
628 styleValues.spacing1 = tagPtr->spacing1;
629 spacing1Prio = tagPtr->priority;
630 }
631 if ((tagPtr->spacing2String != NULL)
632 && (tagPtr->priority > spacing2Prio)) {
633 styleValues.spacing2 = tagPtr->spacing2;
634 spacing2Prio = tagPtr->priority;
635 }
636 if ((tagPtr->spacing3String != NULL)
637 && (tagPtr->priority > spacing3Prio)) {
638 styleValues.spacing3 = tagPtr->spacing3;
639 spacing3Prio = tagPtr->priority;
640 }
641 if ((tagPtr->tabString != NULL)
642 && (tagPtr->priority > tabPrio)) {
643 styleValues.tabArrayPtr = tagPtr->tabArrayPtr;
644 tabPrio = tagPtr->priority;
645 }
646 if ((tagPtr->underlineString != NULL)
647 && (tagPtr->priority > underlinePrio)) {
648 styleValues.underline = tagPtr->underline;
649 underlinePrio = tagPtr->priority;
650 }
651 if ((tagPtr->elideString != NULL)
652 && (tagPtr->priority > elidePrio)) {
653 styleValues.elide = tagPtr->elide;
654 elidePrio = tagPtr->priority;
655 }
656 if ((tagPtr->wrapMode != TEXT_WRAPMODE_NULL)
657 && (tagPtr->priority > wrapPrio)) {
658 styleValues.wrapMode = tagPtr->wrapMode;
659 wrapPrio = tagPtr->priority;
660 }
661 }
662 if (tagPtrs != NULL) {
663 ckfree((char *) tagPtrs);
664 }
665
666 /*
667 * Use an existing style if there's one around that matches.
668 */
669
670 hPtr = Tcl_CreateHashEntry(&textPtr->dInfoPtr->styleTable,
671 (char *) &styleValues, &new);
672 if (!new) {
673 stylePtr = (TextStyle *) Tcl_GetHashValue(hPtr);
674 stylePtr->refCount++;
675 return stylePtr;
676 }
677
678 /*
679 * No existing style matched. Make a new one.
680 */
681
682 stylePtr = (TextStyle *) ckalloc(sizeof(TextStyle));
683 stylePtr->refCount = 1;
684 if (styleValues.border != NULL) {
685 gcValues.foreground = Tk_3DBorderColor(styleValues.border)->pixel;
686 mask = GCForeground;
687 if (styleValues.bgStipple != None) {
688 gcValues.stipple = styleValues.bgStipple;
689 gcValues.fill_style = FillStippled;
690 mask |= GCStipple|GCFillStyle;
691 }
692 stylePtr->bgGC = Tk_GetGC(textPtr->tkwin, mask, &gcValues);
693 } else {
694 stylePtr->bgGC = None;
695 }
696 mask = GCFont;
697 gcValues.font = Tk_FontId(styleValues.tkfont);
698 mask |= GCForeground;
699 gcValues.foreground = styleValues.fgColor->pixel;
700 if (styleValues.fgStipple != None) {
701 gcValues.stipple = styleValues.fgStipple;
702 gcValues.fill_style = FillStippled;
703 mask |= GCStipple|GCFillStyle;
704 }
705 stylePtr->fgGC = Tk_GetGC(textPtr->tkwin, mask, &gcValues);
706 stylePtr->sValuePtr = (StyleValues *)
707 Tcl_GetHashKey(&textPtr->dInfoPtr->styleTable, hPtr);
708 stylePtr->hPtr = hPtr;
709 Tcl_SetHashValue(hPtr, stylePtr);
710 return stylePtr;
711 }
712
713 /*
714 *----------------------------------------------------------------------
715 *
716 * FreeStyle --
717 *
718 * This procedure is called when a TextStyle structure is no longer
719 * needed. It decrements the reference count and frees up the
720 * space for the style structure if the reference count is 0.
721 *
722 * Results:
723 * None.
724 *
725 * Side effects:
726 * The storage and other resources associated with the style
727 * are freed up if no-one's still using it.
728 *
729 *----------------------------------------------------------------------
730 */
731
732 static void
733 FreeStyle(textPtr, stylePtr)
734 TkText *textPtr; /* Information about overall widget. */
735 register TextStyle *stylePtr; /* Information about style to free. */
736
737 {
738 stylePtr->refCount--;
739 if (stylePtr->refCount == 0) {
740 if (stylePtr->bgGC != None) {
741 Tk_FreeGC(textPtr->display, stylePtr->bgGC);
742 }
743 if (stylePtr->fgGC != None) {
744 Tk_FreeGC(textPtr->display, stylePtr->fgGC);
745 }
746 Tcl_DeleteHashEntry(stylePtr->hPtr);
747 ckfree((char *) stylePtr);
748 }
749 }
750
751 /*
752 *----------------------------------------------------------------------
753 *
754 * LayoutDLine --
755 *
756 * This procedure generates a single DLine structure for a display
757 * line whose leftmost character is given by indexPtr.
758 *
759 * Results:
760 * The return value is a pointer to a DLine structure desribing the
761 * display line. All fields are filled in and correct except for
762 * y and nextPtr.
763 *
764 * Side effects:
765 * Storage is allocated for the new DLine.
766 *
767 *----------------------------------------------------------------------
768 */
769
770 static DLine *
771 LayoutDLine(textPtr, indexPtr)
772 TkText *textPtr; /* Overall information about text widget. */
773 TkTextIndex *indexPtr; /* Beginning of display line. May not
774 * necessarily point to a character segment. */
775 {
776 register DLine *dlPtr; /* New display line. */
777 TkTextSegment *segPtr; /* Current segment in text. */
778 TkTextDispChunk *lastChunkPtr; /* Last chunk allocated so far
779 * for line. */
780 TkTextDispChunk *chunkPtr; /* Current chunk. */
781 TkTextIndex curIndex;
782 TkTextDispChunk *breakChunkPtr; /* Chunk containing best word break
783 * point, if any. */
784 TkTextIndex breakIndex; /* Index of first character in
785 * breakChunkPtr. */
786 int breakByteOffset; /* Byte offset of character within
787 * breakChunkPtr just to right of best
788 * break point. */
789 int noCharsYet; /* Non-zero means that no characters
790 * have been placed on the line yet. */
791 int justify; /* How to justify line: taken from
792 * style for the first character in
793 * line. */
794 int jIndent; /* Additional indentation (beyond
795 * margins) due to justification. */
796 int rMargin; /* Right margin width for line. */
797 TkWrapMode wrapMode; /* Wrap mode to use for this line. */
798 int x = 0, maxX = 0; /* Initializations needed only to
799 * stop compiler warnings. */
800 int wholeLine; /* Non-zero means this display line
801 * runs to the end of the text line. */
802 int tabIndex; /* Index of the current tab stop. */
803 int gotTab; /* Non-zero means the current chunk
804 * contains a tab. */
805 TkTextDispChunk *tabChunkPtr; /* Pointer to the chunk containing
806 * the previous tab stop. */
807 int maxBytes; /* Maximum number of bytes to
808 * include in this chunk. */
809 TkTextTabArray *tabArrayPtr; /* Tab stops for line; taken from
810 * style for the first character on
811 * line. */
812 int tabSize; /* Number of pixels consumed by current
813 * tab stop. */
814 TkTextDispChunk *lastCharChunkPtr; /* Pointer to last chunk in display
815 * lines with numBytes > 0. Used to
816 * drop 0-sized chunks from the end
817 * of the line. */
818 int byteOffset, ascent, descent, code, elide, elidesize;
819 StyleValues *sValuePtr;
820
821 /*
822 * Create and initialize a new DLine structure.
823 */
824
825 dlPtr = (DLine *) ckalloc(sizeof(DLine));
826 dlPtr->index = *indexPtr;
827 dlPtr->byteCount = 0;
828 dlPtr->y = 0;
829 dlPtr->oldY = -1;
830 dlPtr->height = 0;
831 dlPtr->baseline = 0;
832 dlPtr->chunkPtr = NULL;
833 dlPtr->nextPtr = NULL;
834 dlPtr->flags = NEW_LAYOUT;
835
836 /*
837 * Special case entirely elide line as there may be 1000s or more
838 */
839 elide = TkTextIsElided(textPtr, indexPtr); /* save a malloc */
840 if (elide && indexPtr->byteIndex==0) {
841 maxBytes = 0;
842 for (segPtr = indexPtr->linePtr->segPtr;
843 elide && (segPtr != NULL);
844 segPtr = segPtr->nextPtr) {
845 if ((elidesize = segPtr->size) > 0) {
846 maxBytes += elidesize;
847 /*
848 * If have we have a tag toggle, there is a chance
849 * that invisibility state changed, so bail out
850 */
851 } else if ((segPtr->typePtr == &tkTextToggleOffType)
852 || (segPtr->typePtr == &tkTextToggleOnType)) {
853 if (segPtr->body.toggle.tagPtr->elideString != NULL) {
854 elide = (segPtr->typePtr == &tkTextToggleOffType)
855 ^ segPtr->body.toggle.tagPtr->elide;
856 }
857 }
858 }
859
860 if (elide) {
861 dlPtr->byteCount = maxBytes;
862 dlPtr->spaceAbove = dlPtr->spaceBelow = dlPtr->length = 0;
863 return dlPtr;
864 }
865 }
866
867 /*
868 * Each iteration of the loop below creates one TkTextDispChunk for
869 * the new display line. The line will always have at least one
870 * chunk (for the newline character at the end, if there's nothing
871 * else available).
872 */
873
874 curIndex = *indexPtr;
875 lastChunkPtr = NULL;
876 chunkPtr = NULL;
877 noCharsYet = 1;
878 elide = 0;
879 breakChunkPtr = NULL;
880 breakByteOffset = 0;
881 justify = TK_JUSTIFY_LEFT;
882 tabIndex = -1;
883 tabChunkPtr = NULL;
884 tabArrayPtr = NULL;
885 rMargin = 0;
886 wrapMode = TEXT_WRAPMODE_CHAR;
887 tabSize = 0;
888 lastCharChunkPtr = NULL;
889
890 /*
891 * Find the first segment to consider for the line. Can't call
892 * TkTextIndexToSeg for this because it won't return a segment
893 * with zero size (such as the insertion cursor's mark).
894 */
895
896 for (byteOffset = curIndex.byteIndex, segPtr = curIndex.linePtr->segPtr;
897 (byteOffset > 0) && (byteOffset >= segPtr->size);
898 byteOffset -= segPtr->size, segPtr = segPtr->nextPtr) {
899 /* Empty loop body. */
900 }
901
902 while (segPtr != NULL) {
903 /*
904 * Every line still gets at least one chunk due to expectations
905 * in the rest of the code, but we are able to skip elided portions
906 * of the line quickly.
907 * If current chunk is elided and last chunk was too, coalese
908 */
909 if (elide && (lastChunkPtr != NULL)
910 && (lastChunkPtr->displayProc == NULL /*ElideDisplayProc*/)) {
911 if ((elidesize = segPtr->size - byteOffset) > 0) {
912 curIndex.byteIndex += elidesize;
913 lastChunkPtr->numBytes += elidesize;
914 breakByteOffset = lastChunkPtr->breakIndex = lastChunkPtr->numBytes;
915 /*
916 * If have we have a tag toggle, there is a chance
917 * that invisibility state changed, so bail out
918 */
919 } else if ((segPtr->typePtr == &tkTextToggleOffType)
920 || (segPtr->typePtr == &tkTextToggleOnType)) {
921 if (segPtr->body.toggle.tagPtr->elideString != NULL) {
922 elide = (segPtr->typePtr == &tkTextToggleOffType)
923 ^ segPtr->body.toggle.tagPtr->elide;
924 }
925 }
926
927 byteOffset = 0;
928 segPtr = segPtr->nextPtr;
929 if (segPtr == NULL && chunkPtr != NULL) {
930 ckfree((char *) chunkPtr);
931 }
932 continue;
933 }
934
935 if (segPtr->typePtr->layoutProc == NULL) {
936 segPtr = segPtr->nextPtr;
937 byteOffset = 0;
938 continue;
939 }
940 if (chunkPtr == NULL) {
941 chunkPtr = (TkTextDispChunk *) ckalloc(sizeof(TkTextDispChunk));
942 chunkPtr->nextPtr = NULL;
943 }
944 chunkPtr->stylePtr = GetStyle(textPtr, &curIndex);
945 elide = chunkPtr->stylePtr->sValuePtr->elide;
946
947 /*
948 * Save style information such as justification and indentation,
949 * up until the first character is encountered, then retain that
950 * information for the rest of the line.
951 */
952
953 if (noCharsYet) {
954 tabArrayPtr = chunkPtr->stylePtr->sValuePtr->tabArrayPtr;
955 justify = chunkPtr->stylePtr->sValuePtr->justify;
956 rMargin = chunkPtr->stylePtr->sValuePtr->rMargin;
957 wrapMode = chunkPtr->stylePtr->sValuePtr->wrapMode;
958 x = ((curIndex.byteIndex == 0)
959 ? chunkPtr->stylePtr->sValuePtr->lMargin1
960 : chunkPtr->stylePtr->sValuePtr->lMargin2);
961 if (wrapMode == TEXT_WRAPMODE_NONE) {
962 maxX = -1;
963 } else {
964 maxX = textPtr->dInfoPtr->maxX - textPtr->dInfoPtr->x
965 - rMargin;
966 if (maxX < x) {
967 maxX = x;
968 }
969 }
970 }
971
972 /*
973 * See if there is a tab in the current chunk; if so, only
974 * layout characters up to (and including) the tab.
975 */
976
977 gotTab = 0;
978 maxBytes = segPtr->size - byteOffset;
979 if (!elide && justify == TK_JUSTIFY_LEFT) {
980 if (segPtr->typePtr == &tkTextCharType) {
981 char *p;
982
983 for (p = segPtr->body.chars + byteOffset; *p != 0; p++) {
984 if (*p == '\t') {
985 maxBytes = (p + 1 - segPtr->body.chars) - byteOffset;
986 gotTab = 1;
987 break;
988 }
989 }
990 }
991 }
992 chunkPtr->x = x;
993 if (elide && maxBytes) {
994 /* don't free style here, as other code expects to be able to do that */
995 /*breakByteOffset =*/ chunkPtr->breakIndex = chunkPtr->numBytes = maxBytes;
996 chunkPtr->width = 0;
997 chunkPtr->minAscent = chunkPtr->minDescent = chunkPtr->minHeight = 0;
998
999 /* would just like to point to canonical empty chunk */
1000 chunkPtr->displayProc = (Tk_ChunkDisplayProc *) NULL;
1001 chunkPtr->undisplayProc = (Tk_ChunkUndisplayProc *) NULL;
1002 chunkPtr->measureProc = ElideMeasureProc;
1003 chunkPtr->bboxProc = ElideBboxProc;
1004
1005 code = 1;
1006 } else
1007 code = (*segPtr->typePtr->layoutProc)(textPtr, &curIndex, segPtr,
1008 byteOffset, maxX-tabSize, maxBytes, noCharsYet, wrapMode,
1009 chunkPtr);
1010 if (code <= 0) {
1011 FreeStyle(textPtr, chunkPtr->stylePtr);
1012 if (code < 0) {
1013 /*
1014 * This segment doesn't wish to display itself (e.g. most
1015 * marks).
1016 */
1017
1018 segPtr = segPtr->nextPtr;
1019 byteOffset = 0;
1020 continue;
1021 }
1022
1023 /*
1024 * No characters from this segment fit in the window: this
1025 * means we're at the end of the display line.
1026 */
1027
1028 if (chunkPtr != NULL) {
1029 ckfree((char *) chunkPtr);
1030 }
1031 break;
1032 }
1033 if (chunkPtr->numBytes > 0) {
1034 noCharsYet = 0;
1035 lastCharChunkPtr = chunkPtr;
1036 }
1037 if (lastChunkPtr == NULL) {
1038 dlPtr->chunkPtr = chunkPtr;
1039 } else {
1040 lastChunkPtr->nextPtr = chunkPtr;
1041 }
1042 lastChunkPtr = chunkPtr;
1043 x += chunkPtr->width;
1044 if (chunkPtr->breakIndex > 0) {
1045 breakByteOffset = chunkPtr->breakIndex;
1046 breakIndex = curIndex;
1047 breakChunkPtr = chunkPtr;
1048 }
1049 if (chunkPtr->numBytes != maxBytes) {
1050 break;
1051 }
1052
1053 /*
1054 * If we're at a new tab, adjust the layout for all the chunks
1055 * pertaining to the previous tab. Also adjust the amount of
1056 * space left in the line to account for space that will be eaten
1057 * up by the tab.
1058 */
1059
1060 if (gotTab) {
1061 if (tabIndex >= 0) {
1062 AdjustForTab(textPtr, tabArrayPtr, tabIndex, tabChunkPtr);
1063 x = chunkPtr->x + chunkPtr->width;
1064 }
1065 tabIndex++;
1066 tabChunkPtr = chunkPtr;
1067 tabSize = SizeOfTab(textPtr, tabArrayPtr, tabIndex, x, maxX);
1068 if ((maxX >= 0) && (tabSize >= maxX - x)) {
1069 break;
1070 }
1071 }
1072 curIndex.byteIndex += chunkPtr->numBytes;
1073 byteOffset += chunkPtr->numBytes;
1074 if (byteOffset >= segPtr->size) {
1075 byteOffset = 0;
1076 segPtr = segPtr->nextPtr;
1077 }
1078
1079 chunkPtr = NULL;
1080 }
1081 if (noCharsYet) {
1082 panic("LayoutDLine couldn't place any characters on a line");
1083 }
1084 wholeLine = (segPtr == NULL);
1085
1086 /*
1087 * We're at the end of the display line. Throw away everything
1088 * after the most recent word break, if there is one; this may
1089 * potentially require the last chunk to be layed out again.
1090 */
1091
1092 if (breakChunkPtr == NULL) {
1093 /*
1094 * This code makes sure that we don't accidentally display
1095 * chunks with no characters at the end of the line (such as
1096 * the insertion cursor). These chunks belong on the next
1097 * line. So, throw away everything after the last chunk that
1098 * has characters in it.
1099 */
1100
1101 breakChunkPtr = lastCharChunkPtr;
1102 breakByteOffset = breakChunkPtr->numBytes;
1103 }
1104 if ((breakChunkPtr != NULL) && ((lastChunkPtr != breakChunkPtr)
1105 || (breakByteOffset != lastChunkPtr->numBytes))) {
1106 while (1) {
1107 chunkPtr = breakChunkPtr->nextPtr;
1108 if (chunkPtr == NULL) {
1109 break;
1110 }
1111 FreeStyle(textPtr, chunkPtr->stylePtr);
1112 breakChunkPtr->nextPtr = chunkPtr->nextPtr;
1113 (*chunkPtr->undisplayProc)(textPtr, chunkPtr);
1114 ckfree((char *) chunkPtr);
1115 }
1116 if (breakByteOffset != breakChunkPtr->numBytes) {
1117 (*breakChunkPtr->undisplayProc)(textPtr, breakChunkPtr);
1118 segPtr = TkTextIndexToSeg(&breakIndex, &byteOffset);
1119 (*segPtr->typePtr->layoutProc)(textPtr, &breakIndex,
1120 segPtr, byteOffset, maxX, breakByteOffset, 0,
1121 wrapMode, breakChunkPtr);
1122 }
1123 lastChunkPtr = breakChunkPtr;
1124 wholeLine = 0;
1125 }
1126
1127
1128 /*
1129 * Make tab adjustments for the last tab stop, if there is one.
1130 */
1131
1132 if ((tabIndex >= 0) && (tabChunkPtr != NULL)) {
1133 AdjustForTab(textPtr, tabArrayPtr, tabIndex, tabChunkPtr);
1134 }
1135
1136 /*
1137 * Make one more pass over the line to recompute various things
1138 * like its height, length, and total number of bytes. Also
1139 * modify the x-locations of chunks to reflect justification.
1140 * If we're not wrapping, I'm not sure what is the best way to
1141 * handle left and center justification: should the total length,
1142 * for purposes of justification, be (a) the window width, (b)
1143 * the length of the longest line in the window, or (c) the length
1144 * of the longest line in the text? (c) isn't available, (b) seems
1145 * weird, since it can change with vertical scrolling, so (a) is
1146 * what is implemented below.
1147 */
1148
1149 if (wrapMode == TEXT_WRAPMODE_NONE) {
1150 maxX = textPtr->dInfoPtr->maxX - textPtr->dInfoPtr->x - rMargin;
1151 }
1152 dlPtr->length = lastChunkPtr->x + lastChunkPtr->width;
1153 if (justify == TK_JUSTIFY_LEFT) {
1154 jIndent = 0;
1155 } else if (justify == TK_JUSTIFY_RIGHT) {
1156 jIndent = maxX - dlPtr->length;
1157 } else {
1158 jIndent = (maxX - dlPtr->length)/2;
1159 }
1160 ascent = descent = 0;
1161 for (chunkPtr = dlPtr->chunkPtr; chunkPtr != NULL;
1162 chunkPtr = chunkPtr->nextPtr) {
1163 chunkPtr->x += jIndent;
1164 dlPtr->byteCount += chunkPtr->numBytes;
1165 if (chunkPtr->minAscent > ascent) {
1166 ascent = chunkPtr->minAscent;
1167 }
1168 if (chunkPtr->minDescent > descent) {
1169 descent = chunkPtr->minDescent;
1170 }
1171 if (chunkPtr->minHeight > dlPtr->height) {
1172 dlPtr->height = chunkPtr->minHeight;
1173 }
1174 sValuePtr = chunkPtr->stylePtr->sValuePtr;
1175 if ((sValuePtr->borderWidth > 0)
1176 && (sValuePtr->relief != TK_RELIEF_FLAT)) {
1177 dlPtr->flags |= HAS_3D_BORDER;
1178 }
1179 }
1180 if (dlPtr->height < (ascent + descent)) {
1181 dlPtr->height = ascent + descent;
1182 dlPtr->baseline = ascent;
1183 } else {
1184 dlPtr->baseline = ascent + (dlPtr->height - ascent - descent)/2;
1185 }
1186 sValuePtr = dlPtr->chunkPtr->stylePtr->sValuePtr;
1187 if (dlPtr->index.byteIndex == 0) {
1188 dlPtr->spaceAbove = sValuePtr->spacing1;
1189 } else {
1190 dlPtr->spaceAbove = sValuePtr->spacing2 - sValuePtr->spacing2/2;
1191 }
1192 if (wholeLine) {
1193 dlPtr->spaceBelow = sValuePtr->spacing3;
1194 } else {
1195 dlPtr->spaceBelow = sValuePtr->spacing2/2;
1196 }
1197 dlPtr->height += dlPtr->spaceAbove + dlPtr->spaceBelow;
1198 dlPtr->baseline += dlPtr->spaceAbove;
1199
1200 /*
1201 * Recompute line length: may have changed because of justification.
1202 */
1203
1204 dlPtr->length = lastChunkPtr->x + lastChunkPtr->width;
1205 return dlPtr;
1206 }
1207
1208 /*
1209 *----------------------------------------------------------------------
1210 *
1211 * UpdateDisplayInfo --
1212 *
1213 * This procedure is invoked to recompute some or all of the
1214 * DLine structures for a text widget. At the time it is called
1215 * the DLine structures still left in the widget are guaranteed
1216 * to be correct except that (a) the y-coordinates aren't
1217 * necessarily correct, (b) there may be missing structures
1218 * (the DLine structures get removed as soon as they are potentially
1219 * out-of-date), and (c) DLine structures that don't start at the
1220 * beginning of a line may be incorrect if previous information in
1221 * the same line changed size in a way that moved a line boundary
1222 * (DLines for any info that changed will have been deleted, but
1223 * not DLines for unchanged info in the same text line).
1224 *
1225 * Results:
1226 * None.
1227 *
1228 * Side effects:
1229 * Upon return, the DLine information for textPtr correctly reflects
1230 * the positions where characters will be displayed. However, this
1231 * procedure doesn't actually bring the display up-to-date.
1232 *
1233 *----------------------------------------------------------------------
1234 */
1235
1236 static void
1237 UpdateDisplayInfo(textPtr)
1238 TkText *textPtr; /* Text widget to update. */
1239 {
1240 register TextDInfo *dInfoPtr = textPtr->dInfoPtr;
1241 register DLine *dlPtr, *prevPtr;
1242 TkTextIndex index;
1243 TkTextLine *lastLinePtr;
1244 int y, maxY, pixelOffset, maxOffset;
1245
1246 if (!(dInfoPtr->flags & DINFO_OUT_OF_DATE)) {
1247 return;
1248 }
1249 dInfoPtr->flags &= ~DINFO_OUT_OF_DATE;
1250
1251 /*
1252 * Delete any DLines that are now above the top of the window.
1253 */
1254
1255 index = textPtr->topIndex;
1256 dlPtr = FindDLine(dInfoPtr->dLinePtr, &index);
1257 if ((dlPtr != NULL) && (dlPtr != dInfoPtr->dLinePtr)) {
1258 FreeDLines(textPtr, dInfoPtr->dLinePtr, dlPtr, 1);
1259 }
1260
1261 /*
1262 *--------------------------------------------------------------
1263 * Scan through the contents of the window from top to bottom,
1264 * recomputing information for lines that are missing.
1265 *--------------------------------------------------------------
1266 */
1267
1268 lastLinePtr = TkBTreeFindLine(textPtr->tree,
1269 TkBTreeNumLines(textPtr->tree));
1270 dlPtr = dInfoPtr->dLinePtr;
1271 prevPtr = NULL;
1272 y = dInfoPtr->y;
1273 maxY = dInfoPtr->maxY;
1274 while (1) {
1275 register DLine *newPtr;
1276
1277 if (index.linePtr == lastLinePtr) {
1278 break;
1279 }
1280
1281 /*
1282 * There are three possibilities right now:
1283 * (a) the next DLine (dlPtr) corresponds exactly to the next
1284 * information we want to display: just use it as-is.
1285 * (b) the next DLine corresponds to a different line, or to
1286 * a segment that will be coming later in the same line:
1287 * leave this DLine alone in the hopes that we'll be able
1288 * to use it later, then create a new DLine in front of
1289 * it.
1290 * (c) the next DLine corresponds to a segment in the line we
1291 * want, but it's a segment that has already been processed
1292 * or will never be processed. Delete the DLine and try
1293 * again.
1294 *
1295 * One other twist on all this. It's possible for 3D borders
1296 * to interact between lines (see DisplayLineBackground) so if
1297 * a line is relayed out and has styles with 3D borders, its
1298 * neighbors have to be redrawn if they have 3D borders too,
1299 * since the interactions could have changed (the neighbors
1300 * don't have to be relayed out, just redrawn).
1301 */
1302
1303 if ((dlPtr == NULL) || (dlPtr->index.linePtr != index.linePtr)) {
1304 /*
1305 * Case (b) -- must make new DLine.
1306 */
1307
1308 makeNewDLine:
1309 if (tkTextDebug) {
1310 char string[TK_POS_CHARS];
1311
1312 /*
1313 * Debugging is enabled, so keep a log of all the lines
1314 * that were re-layed out. The test suite uses this
1315 * information.
1316 */
1317
1318 TkTextPrintIndex(&index, string);
1319 #if 0
1320 Tcl_SetVar2(textPtr->interp, "tk_textRelayout", (char *) NULL,
1321 string,
1322 TCL_GLOBAL_ONLY|TCL_APPEND_VALUE|TCL_LIST_ELEMENT);
1323 #endif
1324 }
1325 newPtr = LayoutDLine(textPtr, &index);
1326 if (prevPtr == NULL) {
1327 dInfoPtr->dLinePtr = newPtr;
1328 } else {
1329 prevPtr->nextPtr = newPtr;
1330 if (prevPtr->flags & HAS_3D_BORDER) {
1331 prevPtr->oldY = -1;
1332 }
1333 }
1334 newPtr->nextPtr = dlPtr;
1335 dlPtr = newPtr;
1336 } else {
1337 /*
1338 * DlPtr refers to the line we want. Next check the
1339 * index within the line.
1340 */
1341
1342 if (index.byteIndex == dlPtr->index.byteIndex) {
1343 /*
1344 * Case (a) -- can use existing display line as-is.
1345 */
1346
1347 if ((dlPtr->flags & HAS_3D_BORDER) && (prevPtr != NULL)
1348 && (prevPtr->flags & (NEW_LAYOUT))) {
1349 dlPtr->oldY = -1;
1350 }
1351 goto lineOK;
1352 }
1353 if (index.byteIndex < dlPtr->index.byteIndex) {
1354 goto makeNewDLine;
1355 }
1356
1357 /*
1358 * Case (c) -- dlPtr is useless. Discard it and start
1359 * again with the next display line.
1360 */
1361
1362 newPtr = dlPtr->nextPtr;
1363 FreeDLines(textPtr, dlPtr, newPtr, 0);
1364 dlPtr = newPtr;
1365 if (prevPtr != NULL) {
1366 prevPtr->nextPtr = newPtr;
1367 } else {
1368 dInfoPtr->dLinePtr = newPtr;
1369 }
1370 continue;
1371 }
1372
1373 /*
1374 * Advance to the start of the next line.
1375 */
1376
1377 lineOK:
1378 dlPtr->y = y;
1379 y += dlPtr->height;
1380 TkTextIndexForwBytes(&index, dlPtr->byteCount, &index);
1381 prevPtr = dlPtr;
1382 dlPtr = dlPtr->nextPtr;
1383
1384 /*
1385 * If we switched text lines, delete any DLines left for the
1386 * old text line.
1387 */
1388
1389 if (index.linePtr != prevPtr->index.linePtr) {
1390 register DLine *nextPtr;
1391
1392 nextPtr = dlPtr;
1393 while ((nextPtr != NULL)
1394 && (nextPtr->index.linePtr == prevPtr->index.linePtr)) {
1395 nextPtr = nextPtr->nextPtr;
1396 }
1397 if (nextPtr != dlPtr) {
1398 FreeDLines(textPtr, dlPtr, nextPtr, 0);
1399 prevPtr->nextPtr = nextPtr;
1400 dlPtr = nextPtr;
1401 }
1402 }
1403
1404 /*
1405 * It's important to have the following check here rather than in
1406 * the while statement for the loop, so that there's always at least
1407 * one DLine generated, regardless of how small the window is. This
1408 * keeps a lot of other code from breaking.
1409 */
1410
1411 if (y >= maxY) {
1412 break;
1413 }
1414 }
1415
1416 /*
1417 * Delete any DLine structures that don't fit on the screen.
1418 */
1419
1420 FreeDLines(textPtr, dlPtr, (DLine *) NULL, 1);
1421
1422 /*
1423 *--------------------------------------------------------------
1424 * If there is extra space at the bottom of the window (because
1425 * we've hit the end of the text), then bring in more lines at
1426 * the top of the window, if there are any, to fill in the view.
1427 *--------------------------------------------------------------
1428 */
1429
1430 if (y < maxY) {
1431 int lineNum, spaceLeft, bytesToCount;
1432 DLine *lowestPtr;
1433
1434 /*
1435 * Layout an entire text line (potentially > 1 display line),
1436 * then link in as many display lines as fit without moving
1437 * the bottom line out of the window. Repeat this until
1438 * all the extra space has been used up or we've reached the
1439 * beginning of the text.
1440 */
1441
1442 spaceLeft = maxY - y;
1443 lineNum = TkBTreeLineIndex(dInfoPtr->dLinePtr->index.linePtr);
1444 bytesToCount = dInfoPtr->dLinePtr->index.byteIndex;
1445 if (bytesToCount == 0) {
1446 bytesToCount = INT_MAX;
1447 lineNum--;
1448 }
1449 for ( ; (lineNum >= 0) && (spaceLeft > 0); lineNum--) {
1450 index.linePtr = TkBTreeFindLine(textPtr->tree, lineNum);
1451 index.byteIndex = 0;
1452 lowestPtr = NULL;
1453
1454 do {
1455 dlPtr = LayoutDLine(textPtr, &index);
1456 dlPtr->nextPtr = lowestPtr;
1457 lowestPtr = dlPtr;
1458 if (dlPtr->length == 0 && dlPtr->height == 0) { bytesToCount--; break; } /* elide */
1459 TkTextIndexForwBytes(&index, dlPtr->byteCount, &index);
1460 bytesToCount -= dlPtr->byteCount;
1461 } while ((bytesToCount > 0)
1462 && (index.linePtr == lowestPtr->index.linePtr));
1463
1464 /*
1465 * Scan through the display lines from the bottom one up to
1466 * the top one.
1467 */
1468
1469 while (lowestPtr != NULL) {
1470 dlPtr = lowestPtr;
1471 spaceLeft -= dlPtr->height;
1472 if (spaceLeft < 0) {
1473 break;
1474 }
1475 lowestPtr = dlPtr->nextPtr;
1476 dlPtr->nextPtr = dInfoPtr->dLinePtr;
1477 dInfoPtr->dLinePtr = dlPtr;
1478 if (tkTextDebug) {
1479 char string[TK_POS_CHARS];
1480
1481 TkTextPrintIndex(&dlPtr->index, string);
1482 #if 0
1483 Tcl_SetVar2(textPtr->interp, "tk_textRelayout",
1484 (char *) NULL, string,
1485 TCL_GLOBAL_ONLY|TCL_APPEND_VALUE|TCL_LIST_ELEMENT);
1486 #endif
1487 }
1488 }
1489 FreeDLines(textPtr, lowestPtr, (DLine *) NULL, 0);
1490 bytesToCount = INT_MAX;
1491 }
1492
1493 /*
1494 * Now we're all done except that the y-coordinates in all the
1495 * DLines are wrong and the top index for the text is wrong.
1496 * Update them.
1497 */
1498
1499 textPtr->topIndex = dInfoPtr->dLinePtr->index;
1500 y = dInfoPtr->y;
1501 for (dlPtr = dInfoPtr->dLinePtr; dlPtr != NULL;
1502 dlPtr = dlPtr->nextPtr) {
1503 if (y > dInfoPtr->maxY) {
1504 panic("Added too many new lines in UpdateDisplayInfo");
1505 }
1506 dlPtr->y = y;
1507 y += dlPtr->height;
1508 }
1509 }
1510
1511 /*
1512 *--------------------------------------------------------------
1513 * If the old top or bottom line has scrolled elsewhere on the
1514 * screen, we may not be able to re-use its old contents by
1515 * copying bits (e.g., a beveled edge that was drawn when it was
1516 * at the top or bottom won't be drawn when the line is in the
1517 * middle and its neighbor has a matching background). Similarly,
1518 * if the new top or bottom line came from somewhere else on the
1519 * screen, we may not be able to copy the old bits.
1520 *--------------------------------------------------------------
1521 */
1522
1523 dlPtr = dInfoPtr->dLinePtr;
1524 if ((dlPtr->flags & HAS_3D_BORDER) && !(dlPtr->flags & TOP_LINE)) {
1525 dlPtr->oldY = -1;
1526 }
1527 while (1) {
1528 if ((dlPtr->flags & TOP_LINE) && (dlPtr != dInfoPtr->dLinePtr)
1529 && (dlPtr->flags & HAS_3D_BORDER)) {
1530 dlPtr->oldY = -1;
1531 }
1532 if ((dlPtr->flags & BOTTOM_LINE) && (dlPtr->nextPtr != NULL)
1533 && (dlPtr->flags & HAS_3D_BORDER)) {
1534 dlPtr->oldY = -1;
1535 }
1536 if (dlPtr->nextPtr == NULL) {
1537 if ((dlPtr->flags & HAS_3D_BORDER)
1538 && !(dlPtr->flags & BOTTOM_LINE)) {
1539 dlPtr->oldY = -1;
1540 }
1541 dlPtr->flags &= ~TOP_LINE;
1542 dlPtr->flags |= BOTTOM_LINE;
1543 break;
1544 }
1545 dlPtr->flags &= ~(TOP_LINE|BOTTOM_LINE);
1546 dlPtr = dlPtr->nextPtr;
1547 }
1548 dInfoPtr->dLinePtr->flags |= TOP_LINE;
1549
1550 /*
1551 * Arrange for scrollbars to be updated.
1552 */
1553
1554 textPtr->flags |= UPDATE_SCROLLBARS;
1555
1556 /*
1557 *--------------------------------------------------------------
1558 * Deal with horizontal scrolling:
1559 * 1. If there's empty space to the right of the longest line,
1560 * shift the screen to the right to fill in the empty space.
1561 * 2. If the desired horizontal scroll position has changed,
1562 * force a full redisplay of all the lines in the widget.
1563 * 3. If the wrap mode isn't "none" then re-scroll to the base
1564 * position.
1565 *--------------------------------------------------------------
1566 */
1567
1568 dInfoPtr->maxLength = 0;
1569 for (dlPtr = dInfoPtr->dLinePtr; dlPtr != NULL;
1570 dlPtr = dlPtr->nextPtr) {
1571 if (dlPtr->length > dInfoPtr->maxLength) {
1572 dInfoPtr->maxLength = dlPtr->length;
1573 }
1574 }
1575 maxOffset = (dInfoPtr->maxLength - (dInfoPtr->maxX - dInfoPtr->x)
1576 + textPtr->charWidth - 1)/textPtr->charWidth;
1577 if (dInfoPtr->newByteOffset > maxOffset) {
1578 dInfoPtr->newByteOffset = maxOffset;
1579 }
1580 if (dInfoPtr->newByteOffset < 0) {
1581 dInfoPtr->newByteOffset = 0;
1582 }
1583 pixelOffset = dInfoPtr->newByteOffset * textPtr->charWidth;
1584 if (pixelOffset != dInfoPtr->curPixelOffset) {
1585 dInfoPtr->curPixelOffset = pixelOffset;
1586 for (dlPtr = dInfoPtr->dLinePtr; dlPtr != NULL;
1587 dlPtr = dlPtr->nextPtr) {
1588 dlPtr->oldY = -1;
1589 }
1590 }
1591 }
1592
1593 /*
1594 *----------------------------------------------------------------------
1595 *
1596 * FreeDLines --
1597 *
1598 * This procedure is called to free up all of the resources
1599 * associated with one or more DLine structures.
1600 *
1601 * Results:
1602 * None.
1603 *
1604 * Side effects:
1605 * Memory gets freed and various other resources are released.
1606 *
1607 *----------------------------------------------------------------------
1608 */
1609
1610 static void
1611 FreeDLines(textPtr, firstPtr, lastPtr, unlink)
1612 TkText *textPtr; /* Information about overall text
1613 * widget. */
1614 register DLine *firstPtr; /* Pointer to first DLine to free up. */
1615 DLine *lastPtr; /* Pointer to DLine just after last
1616 * one to free (NULL means everything
1617 * starting with firstPtr). */
1618 int unlink; /* 1 means DLines are currently linked
1619 * into the list rooted at
1620 * textPtr->dInfoPtr->dLinePtr and
1621 * they have to be unlinked. 0 means
1622 * just free without unlinking. */
1623 {
1624 register TkTextDispChunk *chunkPtr, *nextChunkPtr;
1625 register DLine *nextDLinePtr;
1626
1627 if (unlink) {
1628 if (textPtr->dInfoPtr->dLinePtr == firstPtr) {
1629 textPtr->dInfoPtr->dLinePtr = lastPtr;
1630 } else {
1631 register DLine *prevPtr;
1632 for (prevPtr = textPtr->dInfoPtr->dLinePtr;
1633 prevPtr->nextPtr != firstPtr; prevPtr = prevPtr->nextPtr) {
1634 /* Empty loop body. */
1635 }
1636 prevPtr->nextPtr = lastPtr;
1637 }
1638 }
1639 while (firstPtr != lastPtr) {
1640 nextDLinePtr = firstPtr->nextPtr;
1641 for (chunkPtr = firstPtr->chunkPtr; chunkPtr != NULL;
1642 chunkPtr = nextChunkPtr) {
1643 if (chunkPtr->undisplayProc != NULL) {
1644 (*chunkPtr->undisplayProc)(textPtr, chunkPtr);
1645 }
1646 FreeStyle(textPtr, chunkPtr->stylePtr);
1647 nextChunkPtr = chunkPtr->nextPtr;
1648 ckfree((char *) chunkPtr);
1649 }
1650 ckfree((char *) firstPtr);
1651 firstPtr = nextDLinePtr;
1652 }
1653 textPtr->dInfoPtr->dLinesInvalidated = 1;
1654 }
1655
1656 /*
1657 *----------------------------------------------------------------------
1658 *
1659 * DisplayDLine --
1660 *
1661 * This procedure is invoked to draw a single line on the
1662 * screen.
1663 *
1664 * Results:
1665 * None.
1666 *
1667 * Side effects:
1668 * The line given by dlPtr is drawn at its correct position in
1669 * textPtr's window. Note that this is one *display* line, not
1670 * one *text* line.
1671 *
1672 *----------------------------------------------------------------------
1673 */
1674
1675 static void
1676 DisplayDLine(textPtr, dlPtr, prevPtr, pixmap)
1677 TkText *textPtr; /* Text widget in which to draw line. */
1678 register DLine *dlPtr; /* Information about line to draw. */
1679 DLine *prevPtr; /* Line just before one to draw, or NULL
1680 * if dlPtr is the top line. */
1681 Pixmap pixmap; /* Pixmap to use for double-buffering.
1682 * Caller must make sure it's large enough
1683 * to hold line. */
1684 {
1685 register TkTextDispChunk *chunkPtr;
1686 TextDInfo *dInfoPtr = textPtr->dInfoPtr;
1687 Display *display;
1688 int height, x;
1689
1690 if (dlPtr->chunkPtr == NULL) return;
1691
1692 /*
1693 * First, clear the area of the line to the background color for the
1694 * text widget.
1695 */
1696
1697 display = Tk_Display(textPtr->tkwin);
1698 Tk_Fill3DRectangle(textPtr->tkwin, pixmap, textPtr->border, 0, 0,
1699 Tk_Width(textPtr->tkwin), dlPtr->height, 0, TK_RELIEF_FLAT);
1700
1701 /*
1702 * Next, draw background information for the whole line.
1703 */
1704
1705 DisplayLineBackground(textPtr, dlPtr, prevPtr, pixmap);
1706
1707 /*
1708 * Make another pass through all of the chunks to redraw the
1709 * insertion cursor, if it is visible on this line. Must do
1710 * it here rather than in the foreground pass below because
1711 * otherwise a wide insertion cursor will obscure the character
1712 * to its left.
1713 */
1714
1715 if (textPtr->state == TK_STATE_NORMAL) {
1716 for (chunkPtr = dlPtr->chunkPtr; (chunkPtr != NULL);
1717 chunkPtr = chunkPtr->nextPtr) {
1718 x = chunkPtr->x + dInfoPtr->x - dInfoPtr->curPixelOffset;
1719 if (chunkPtr->displayProc == TkTextInsertDisplayProc) {
1720 (*chunkPtr->displayProc)(chunkPtr, x, dlPtr->spaceAbove,
1721 dlPtr->height - dlPtr->spaceAbove - dlPtr->spaceBelow,
1722 dlPtr->baseline - dlPtr->spaceAbove, display, pixmap,
1723 dlPtr->y + dlPtr->spaceAbove);
1724 }
1725 }
1726 }
1727
1728 /*
1729 * Make yet another pass through all of the chunks to redraw all of
1730 * foreground information. Note: we have to call the displayProc
1731 * even for chunks that are off-screen. This is needed, for
1732 * example, so that embedded windows can be unmapped in this case.
1733 * Conve
1734 */
1735
1736 for (chunkPtr = dlPtr->chunkPtr; (chunkPtr != NULL);
1737 chunkPtr = chunkPtr->nextPtr) {
1738 if (chunkPtr->displayProc == TkTextInsertDisplayProc) {
1739 /*
1740 * Already displayed the insertion cursor above. Don't
1741 * do it again here.
1742 */
1743
1744 continue;
1745 }
1746 x = chunkPtr->x + dInfoPtr->x - dInfoPtr->curPixelOffset;
1747 if ((x + chunkPtr->width <= 0) || (x >= dInfoPtr->maxX)) {
1748 /*
1749 * Note: we have to call the displayProc even for chunks
1750 * that are off-screen. This is needed, for example, so
1751 * that embedded windows can be unmapped in this case.
1752 * Display the chunk at a coordinate that can be clearly
1753 * identified by the displayProc as being off-screen to
1754 * the left (the displayProc may not be able to tell if
1755 * something is off to the right).
1756 */
1757
1758 if (chunkPtr->displayProc != NULL)
1759 (*chunkPtr->displayProc)(chunkPtr, -chunkPtr->width,
1760 dlPtr->spaceAbove,
1761 dlPtr->height - dlPtr->spaceAbove - dlPtr->spaceBelow,
1762 dlPtr->baseline - dlPtr->spaceAbove, display, pixmap,
1763 dlPtr->y + dlPtr->spaceAbove);
1764 } else {
1765 /* don't call if elide. This tax ok since not very many visible DLine's in
1766 an area, but potentially many elide ones */
1767 if (chunkPtr->displayProc != NULL)
1768 (*chunkPtr->displayProc)(chunkPtr, x, dlPtr->spaceAbove,
1769 dlPtr->height - dlPtr->spaceAbove - dlPtr->spaceBelow,
1770 dlPtr->baseline - dlPtr->spaceAbove, display, pixmap,
1771 dlPtr->y + dlPtr->spaceAbove);
1772 }
1773 if (dInfoPtr->dLinesInvalidated) {
1774 return;
1775 }
1776 }
1777
1778 /*
1779 * Copy the pixmap onto the screen. If this is the last line on
1780 * the screen then copy a piece of the line, so that it doesn't
1781 * overflow into the border area. Another special trick: copy the
1782 * padding area to the left of the line; this is because the
1783 * insertion cursor sometimes overflows onto that area and we want
1784 * to get as much of the cursor as possible.
1785 */
1786
1787 height = dlPtr->height;
1788 if ((height + dlPtr->y) > dInfoPtr->maxY) {
1789 height = dInfoPtr->maxY - dlPtr->y;
1790 }
1791 XCopyArea(display, pixmap, Tk_WindowId(textPtr->tkwin), dInfoPtr->copyGC,
1792 dInfoPtr->x, 0, (unsigned) (dInfoPtr->maxX - dInfoPtr->x),
1793 (unsigned) height, dInfoPtr->x, dlPtr->y);
1794 linesRedrawn++;
1795 }
1796
1797 /*
1798 *--------------------------------------------------------------
1799 *
1800 * DisplayLineBackground --
1801 *
1802 * This procedure is called to fill in the background for
1803 * a display line. It draws 3D borders cleverly so that
1804 * adjacent chunks with the same style (whether on the same
1805 * line or different lines) have a single 3D border around
1806 * the whole region.
1807 *
1808 * Results:
1809 * There is no return value. Pixmap is filled in with background
1810 * information for dlPtr.
1811 *
1812 * Side effects:
1813 * None.
1814 *
1815 *--------------------------------------------------------------
1816 */
1817
1818 static void
1819 DisplayLineBackground(textPtr, dlPtr, prevPtr, pixmap)
1820 TkText *textPtr; /* Text widget containing line. */
1821 register DLine *dlPtr; /* Information about line to draw. */
1822 DLine *prevPtr; /* Line just above dlPtr, or NULL if dlPtr
1823 * is the top-most line in the window. */
1824 Pixmap pixmap; /* Pixmap to use for double-buffering.
1825 * Caller must make sure it's large enough
1826 * to hold line. Caller must also have
1827 * filled it with the background color for
1828 * the widget. */
1829 {
1830 TextDInfo *dInfoPtr = textPtr->dInfoPtr;
1831 TkTextDispChunk *chunkPtr; /* Pointer to chunk in the current line. */
1832 TkTextDispChunk *chunkPtr2; /* Pointer to chunk in the line above or
1833 * below the current one. NULL if we're to
1834 * the left of or to the right of the chunks
1835 * in the line. */
1836 TkTextDispChunk *nextPtr2; /* Next chunk after chunkPtr2 (it's not the
1837 * same as chunkPtr2->nextPtr in the case
1838 * where chunkPtr2 is NULL because the line
1839 * is indented). */
1840 int leftX; /* The left edge of the region we're
1841 * currently working on. */
1842 int leftXIn; /* 1 means beveled edge at leftX slopes right
1843 * as it goes down, 0 means it slopes left
1844 * as it goes down. */
1845 int rightX; /* Right edge of chunkPtr. */
1846 int rightX2; /* Right edge of chunkPtr2. */
1847 int matchLeft; /* Does the style of this line match that
1848 * of its neighbor just to the left of
1849 * the current x coordinate? */
1850 int matchRight; /* Does line's style match its neighbor
1851 * just to the right of the current x-coord? */
1852 int minX, maxX, xOffset;
1853 StyleValues *sValuePtr;
1854 Display *display;
1855
1856
1857 /*
1858 * Pass 1: scan through dlPtr from left to right. For each range of
1859 * chunks with the same style, draw the main background for the style
1860 * plus the vertical parts of the 3D borders (the left and right
1861 * edges).
1862 */
1863
1864 display = Tk_Display(textPtr->tkwin);
1865 minX = dInfoPtr->curPixelOffset;
1866 xOffset = dInfoPtr->x - minX;
1867 maxX = minX + dInfoPtr->maxX - dInfoPtr->x;
1868 chunkPtr = dlPtr->chunkPtr;
1869
1870 /*
1871 * Note A: in the following statement, and a few others later in
1872 * this file marked with "See Note A above", the right side of the
1873 * assignment was replaced with 0 on 6/18/97. This has the effect
1874 * of highlighting the empty space to the left of a line whenever
1875 * the leftmost character of the line is highlighted. This way,
1876 * multi-line highlights always line up along their left edges.
1877 * However, this may look funny in the case where a single word is
1878 * highlighted. To undo the change, replace "leftX = 0" with "leftX
1879 * = chunkPtr->x" and "rightX2 = 0" with "rightX2 = nextPtr2->x"
1880 * here and at all the marked points below. This restores the old
1881 * behavior where empty space to the left of a line is not
1882 * highlighted, leaving a ragged left edge for multi-line
1883 * highlights.
1884 */
1885
1886 leftX = 0;
1887 for (; leftX < maxX; chunkPtr = chunkPtr->nextPtr) {
1888 if ((chunkPtr->nextPtr != NULL)
1889 && SAME_BACKGROUND(chunkPtr->nextPtr->stylePtr,
1890 chunkPtr->stylePtr)) {
1891 continue;
1892 }
1893 sValuePtr = chunkPtr->stylePtr->sValuePtr;
1894 rightX = chunkPtr->x + chunkPtr->width;
1895 if ((chunkPtr->nextPtr == NULL) && (rightX < maxX)) {
1896 rightX = maxX;
1897 }
1898 if (chunkPtr->stylePtr->bgGC != None) {
1899 /* Not visible - bail out now */
1900 if (rightX + xOffset <= 0) {
1901 leftX = rightX;
1902 continue;
1903 }
1904
1905 /*
1906 * Trim the start position for drawing to be no further away than
1907 * -borderWidth. The reason is that on many X servers drawing from
1908 * -32768 (or less) to +something simply does not display
1909 * correctly. [Patch #541999]
1910 */
1911 if ((leftX + xOffset) < -(sValuePtr->borderWidth)) {
1912 leftX = -sValuePtr->borderWidth - xOffset;
1913 }
1914 if ((rightX - leftX) > 32767) {
1915 rightX = leftX + 32767;
1916 }
1917
1918 XFillRectangle(display, pixmap, chunkPtr->stylePtr->bgGC,
1919 leftX + xOffset, 0, (unsigned int) (rightX - leftX),
1920 (unsigned int) dlPtr->height);
1921 if (sValuePtr->relief != TK_RELIEF_FLAT) {
1922 Tk_3DVerticalBevel(textPtr->tkwin, pixmap, sValuePtr->border,
1923 leftX + xOffset, 0, sValuePtr->borderWidth,
1924 dlPtr->height, 1, sValuePtr->relief);
1925 Tk_3DVerticalBevel(textPtr->tkwin, pixmap, sValuePtr->border,
1926 rightX - sValuePtr->borderWidth + xOffset,
1927 0, sValuePtr->borderWidth, dlPtr->height, 0,
1928 sValuePtr->relief);
1929 }
1930 }
1931 leftX = rightX;
1932 }
1933
1934 /*
1935 * Pass 2: draw the horizontal bevels along the top of the line. To
1936 * do this, scan through dlPtr from left to right while simultaneously
1937 * scanning through the line just above dlPtr. ChunkPtr2 and nextPtr2
1938 * refer to two adjacent chunks in the line above.
1939 */
1940
1941 chunkPtr = dlPtr->chunkPtr;
1942 leftX = 0; /* See Note A above. */
1943 leftXIn = 1;
1944 rightX = chunkPtr->x + chunkPtr->width;
1945 if ((chunkPtr->nextPtr == NULL) && (rightX < maxX)) {
1946 rightX = maxX;
1947 }
1948 chunkPtr2 = NULL;
1949 if (prevPtr != NULL && prevPtr->chunkPtr != NULL) {
1950 /*
1951 * Find the chunk in the previous line that covers leftX.
1952 */
1953
1954 nextPtr2 = prevPtr->chunkPtr;
1955 rightX2 = 0; /* See Note A above. */
1956 while (rightX2 <= leftX) {
1957 chunkPtr2 = nextPtr2;
1958 if (chunkPtr2 == NULL) {
1959 break;
1960 }
1961 nextPtr2 = chunkPtr2->nextPtr;
1962 rightX2 = chunkPtr2->x + chunkPtr2->width;
1963 if (nextPtr2 == NULL) {
1964 rightX2 = INT_MAX;
1965 }
1966 }
1967 } else {
1968 nextPtr2 = NULL;
1969 rightX2 = INT_MAX;
1970 }
1971
1972 while (leftX < maxX) {
1973 matchLeft = (chunkPtr2 != NULL)
1974 && SAME_BACKGROUND(chunkPtr2->stylePtr, chunkPtr->stylePtr);
1975 sValuePtr = chunkPtr->stylePtr->sValuePtr;
1976 if (rightX <= rightX2) {
1977 /*
1978 * The chunk in our line is about to end. If its style
1979 * changes then draw the bevel for the current style.
1980 */
1981
1982 if ((chunkPtr->nextPtr == NULL)
1983 || !SAME_BACKGROUND(chunkPtr->stylePtr,
1984 chunkPtr->nextPtr->stylePtr)) {
1985 if (!matchLeft && (sValuePtr->relief != TK_RELIEF_FLAT)) {
1986 Tk_3DHorizontalBevel(textPtr->tkwin, pixmap,
1987 sValuePtr->border, leftX + xOffset, 0,
1988 rightX - leftX, sValuePtr->borderWidth, leftXIn,
1989 1, 1, sValuePtr->relief);
1990 }
1991 leftX = rightX;
1992 leftXIn = 1;
1993
1994 /*
1995 * If the chunk in the line above is also ending at
1996 * the same point then advance to the next chunk in
1997 * that line.
1998 */
1999
2000 if ((rightX == rightX2) && (chunkPtr2 != NULL)) {
2001 goto nextChunk2;
2002 }
2003 }
2004 chunkPtr = chunkPtr->nextPtr;
2005 if (chunkPtr == NULL) {
2006 break;
2007 }
2008 rightX = chunkPtr->x + chunkPtr->width;
2009 if ((chunkPtr->nextPtr == NULL) && (rightX < maxX)) {
2010 rightX = maxX;
2011 }
2012 continue;
2013 }
2014
2015 /*
2016 * The chunk in the line above is ending at an x-position where
2017 * there is no change in the style of the current line. If the
2018 * style above matches the current line on one side of the change
2019 * but not on the other, we have to draw an L-shaped piece of
2020 * bevel.
2021 */
2022
2023 matchRight = (nextPtr2 != NULL)
2024 && SAME_BACKGROUND(nextPtr2->stylePtr, chunkPtr->stylePtr);
2025 if (matchLeft && !matchRight) {
2026 if (sValuePtr->relief != TK_RELIEF_FLAT) {
2027 Tk_3DVerticalBevel(textPtr->tkwin, pixmap, sValuePtr->border,
2028 rightX2 - sValuePtr->borderWidth + xOffset, 0,
2029 sValuePtr->borderWidth, sValuePtr->borderWidth, 0,
2030 sValuePtr->relief);
2031 }
2032 leftX = rightX2 - sValuePtr->borderWidth;
2033 leftXIn = 0;
2034 } else if (!matchLeft && matchRight
2035 && (sValuePtr->relief != TK_RELIEF_FLAT)) {
2036 Tk_3DVerticalBevel(textPtr->tkwin, pixmap, sValuePtr->border,
2037 rightX2 + xOffset, 0, sValuePtr->borderWidth,
2038 sValuePtr->borderWidth, 1, sValuePtr->relief);
2039 Tk_3DHorizontalBevel(textPtr->tkwin, pixmap, sValuePtr->border,
2040 leftX + xOffset, 0, rightX2 + sValuePtr->borderWidth -leftX,
2041 sValuePtr->borderWidth, leftXIn, 0, 1,
2042 sValuePtr->relief);
2043 }
2044
2045 nextChunk2:
2046 chunkPtr2 = nextPtr2;
2047 if (chunkPtr2 == NULL) {
2048 rightX2 = INT_MAX;
2049 } else {
2050 nextPtr2 = chunkPtr2->nextPtr;
2051 rightX2 = chunkPtr2->x + chunkPtr2->width;
2052 if (nextPtr2 == NULL) {
2053 rightX2 = INT_MAX;
2054 }
2055 }
2056 }
2057 /*
2058 * Pass 3: draw the horizontal bevels along the bottom of the line.
2059 * This uses the same approach as pass 2.
2060 */
2061
2062 chunkPtr = dlPtr->chunkPtr;
2063 leftX = 0; /* See Note A above. */
2064 leftXIn = 0;
2065 rightX = chunkPtr->x + chunkPtr->width;
2066 if ((chunkPtr->nextPtr == NULL) && (rightX < maxX)) {
2067 rightX = maxX;
2068 }
2069 chunkPtr2 = NULL;
2070 if (dlPtr->nextPtr != NULL && dlPtr->nextPtr->chunkPtr != NULL) {
2071 /*
2072 * Find the chunk in the previous line that covers leftX.
2073 */
2074
2075 nextPtr2 = dlPtr->nextPtr->chunkPtr;
2076 rightX2 = 0; /* See Note A above. */
2077 while (rightX2 <= leftX) {
2078 chunkPtr2 = nextPtr2;
2079 if (chunkPtr2 == NULL) {
2080 break;
2081 }
2082 nextPtr2 = chunkPtr2->nextPtr;
2083 rightX2 = chunkPtr2->x + chunkPtr2->width;
2084 if (nextPtr2 == NULL) {
2085 rightX2 = INT_MAX;
2086 }
2087 }
2088 } else {
2089 nextPtr2 = NULL;
2090 rightX2 = INT_MAX;
2091 }
2092
2093 while (leftX < maxX) {
2094 matchLeft = (chunkPtr2 != NULL)
2095 && SAME_BACKGROUND(chunkPtr2->stylePtr, chunkPtr->stylePtr);
2096 sValuePtr = chunkPtr->stylePtr->sValuePtr;
2097 if (rightX <= rightX2) {
2098 if ((chunkPtr->nextPtr == NULL)
2099 || !SAME_BACKGROUND(chunkPtr->stylePtr,
2100 chunkPtr->nextPtr->stylePtr)) {
2101 if (!matchLeft && (sValuePtr->relief != TK_RELIEF_FLAT)) {
2102 Tk_3DHorizontalBevel(textPtr->tkwin, pixmap,
2103 sValuePtr->border, leftX + xOffset,
2104 dlPtr->height - sValuePtr->borderWidth,
2105 rightX - leftX, sValuePtr->borderWidth, leftXIn,
2106 0, 0, sValuePtr->relief);
2107 }
2108 leftX = rightX;
2109 leftXIn = 0;
2110 if ((rightX == rightX2) && (chunkPtr2 != NULL)) {
2111 goto nextChunk2b;
2112 }
2113 }
2114 chunkPtr = chunkPtr->nextPtr;
2115 if (chunkPtr == NULL) {
2116 break;
2117 }
2118 rightX = chunkPtr->x + chunkPtr->width;
2119 if ((chunkPtr->nextPtr == NULL) && (rightX < maxX)) {
2120 rightX = maxX;
2121 }
2122 continue;
2123 }
2124
2125 matchRight = (nextPtr2 != NULL)
2126 && SAME_BACKGROUND(nextPtr2->stylePtr, chunkPtr->stylePtr);
2127 if (matchLeft && !matchRight) {
2128 if (sValuePtr->relief != TK_RELIEF_FLAT) {
2129 Tk_3DVerticalBevel(textPtr->tkwin, pixmap, sValuePtr->border,
2130 rightX2 - sValuePtr->borderWidth + xOffset,
2131 dlPtr->height - sValuePtr->borderWidth,
2132 sValuePtr->borderWidth, sValuePtr->borderWidth, 0,
2133 sValuePtr->relief);
2134 }
2135 leftX = rightX2 - sValuePtr->borderWidth;
2136 leftXIn = 1;
2137 } else if (!matchLeft && matchRight
2138 && (sValuePtr->relief != TK_RELIEF_FLAT)) {
2139 Tk_3DVerticalBevel(textPtr->tkwin, pixmap, sValuePtr->border,
2140 rightX2 + xOffset, dlPtr->height - sValuePtr->borderWidth,
2141 sValuePtr->borderWidth, sValuePtr->borderWidth,
2142 1, sValuePtr->relief);
2143 Tk_3DHorizontalBevel(textPtr->tkwin, pixmap, sValuePtr->border,
2144 leftX + xOffset, dlPtr->height - sValuePtr->borderWidth,
2145 rightX2 + sValuePtr->borderWidth - leftX,
2146 sValuePtr->borderWidth, leftXIn, 1, 0, sValuePtr->relief);
2147 }
2148
2149 nextChunk2b:
2150 chunkPtr2 = nextPtr2;
2151 if (chunkPtr2 == NULL) {
2152 rightX2 = INT_MAX;
2153 } else {
2154 nextPtr2 = chunkPtr2->nextPtr;
2155 rightX2 = chunkPtr2->x + chunkPtr2->width;
2156 if (nextPtr2 == NULL) {
2157 rightX2 = INT_MAX;
2158 }
2159 }
2160 }
2161 }
2162
2163 /*
2164 *----------------------------------------------------------------------
2165 *
2166 * DisplayText --
2167 *
2168 * This procedure is invoked as a when-idle handler to update the
2169 * display. It only redisplays the parts of the text widget that
2170 * are out of date.
2171 *
2172 * Results:
2173 * None.
2174 *
2175 * Side effects:
2176 * Information is redrawn on the screen.
2177 *
2178 *----------------------------------------------------------------------
2179 */
2180
2181 static void
2182 DisplayText(clientData)
2183 ClientData clientData; /* Information about widget. */
2184 {
2185 register TkText *textPtr = (TkText *) clientData;
2186 TextDInfo *dInfoPtr = textPtr->dInfoPtr;
2187 Tk_Window tkwin;
2188 register DLine *dlPtr;
2189 DLine *prevPtr;
2190 Pixmap pixmap;
2191 int maxHeight, borders;
2192 int bottomY = 0; /* Initialization needed only to stop
2193 * compiler warnings. */
2194 Tcl_Interp *interp;
2195
2196 if (textPtr->tkwin == NULL) {
2197
2198 /*
2199 * The widget has been deleted. Don't do anything.
2200 */
2201
2202 return;
2203 }
2204
2205 interp = textPtr->interp;
2206 Tcl_Preserve((ClientData) interp);
2207
2208 if (tkTextDebug) {
2209 #if 0
2210 Tcl_SetVar2(textPtr->interp, "tk_textRelayout", (char *) NULL,
2211 "", TCL_GLOBAL_ONLY);
2212 #endif
2213 }
2214
2215 if (textPtr->tkwin == NULL) {
2216
2217 /*
2218 * The widget has been deleted. Don't do anything.
2219 */
2220
2221 goto end;
2222 }
2223
2224 if (!Tk_IsMapped(textPtr->tkwin) || (dInfoPtr->maxX <= dInfoPtr->x)
2225 || (dInfoPtr->maxY <= dInfoPtr->y)) {
2226 UpdateDisplayInfo(textPtr);
2227 dInfoPtr->flags &= ~REDRAW_PENDING;
2228 goto doScrollbars;
2229 }
2230 numRedisplays++;
2231 if (tkTextDebug) {
2232 #if 0
2233 Tcl_SetVar2(textPtr->interp, "tk_textRedraw", (char *) NULL,
2234 "", TCL_GLOBAL_ONLY);
2235 #endif
2236 }
2237
2238 if (textPtr->tkwin == NULL) {
2239
2240 /*
2241 * The widget has been deleted. Don't do anything.
2242 */
2243
2244 goto end;
2245 }
2246
2247 /*
2248 * Choose a new current item if that is needed (this could cause
2249 * event handlers to be invoked, hence the preserve/release calls
2250 * and the loop, since the handlers could conceivably necessitate
2251 * yet another current item calculation). The tkwin check is because
2252 * the whole window could go away in the Tcl_Release call.
2253 */
2254
2255 while (dInfoPtr->flags & REPICK_NEEDED) {
2256 Tcl_Preserve((ClientData) textPtr);
2257 dInfoPtr->flags &= ~REPICK_NEEDED;
2258 TkTextPickCurrent(textPtr, &textPtr->pickEvent);
2259 tkwin = textPtr->tkwin;
2260 Tcl_Release((ClientData) textPtr);
2261 if (tkwin == NULL) {
2262 goto end;
2263 }
2264 }
2265
2266 /*
2267 * First recompute what's supposed to be displayed.
2268 */
2269
2270 UpdateDisplayInfo(textPtr);
2271 dInfoPtr->dLinesInvalidated = 0;
2272
2273 /*
2274 * See if it's possible to bring some parts of the screen up-to-date
2275 * by scrolling (copying from other parts of the screen).
2276 */
2277
2278 for (dlPtr = dInfoPtr->dLinePtr; dlPtr != NULL; dlPtr = dlPtr->nextPtr) {
2279 register DLine *dlPtr2;
2280 int offset, height, y, oldY;
2281 TkRegion damageRgn;
2282
2283 if ((dlPtr->oldY == -1) || (dlPtr->y == dlPtr->oldY)
2284 || ((dlPtr->oldY + dlPtr->height) > dInfoPtr->maxY)) {
2285 continue;
2286 }
2287
2288 /*
2289 * This line is already drawn somewhere in the window so it only
2290 * needs to be copied to its new location. See if there's a group
2291 * of lines that can all be copied together.
2292 */
2293
2294 offset = dlPtr->y - dlPtr->oldY;
2295 height = dlPtr->height;
2296 y = dlPtr->y;
2297 for (dlPtr2 = dlPtr->nextPtr; dlPtr2 != NULL;
2298 dlPtr2 = dlPtr2->nextPtr) {
2299 if ((dlPtr2->oldY == -1)
2300 || ((dlPtr2->oldY + offset) != dlPtr2->y)
2301 || ((dlPtr2->oldY + dlPtr2->height) > dInfoPtr->maxY)) {
2302 break;
2303 }
2304 height += dlPtr2->height;
2305 }
2306
2307 /*
2308 * Reduce the height of the area being copied if necessary to
2309 * avoid overwriting the border area.
2310 */
2311
2312 if ((y + height) > dInfoPtr->maxY) {
2313 height = dInfoPtr->maxY -y;
2314 }
2315 oldY = dlPtr->oldY;
2316
2317 /*
2318 * Update the lines we are going to scroll to show that they
2319 * have been copied.
2320 */
2321
2322 while (1) {
2323 dlPtr->oldY = dlPtr->y;
2324 if (dlPtr->nextPtr == dlPtr2) {
2325 break;
2326 }
2327 dlPtr = dlPtr->nextPtr;
2328 }
2329
2330 /*
2331 * Scan through the lines following the copied ones to see if
2332 * we are going to overwrite them with the copy operation.
2333 * If so, mark them for redisplay.
2334 */
2335
2336 for ( ; dlPtr2 != NULL; dlPtr2 = dlPtr2->nextPtr) {
2337 if ((dlPtr2->oldY != -1)
2338 && ((dlPtr2->oldY + dlPtr2->height) > y)
2339 && (dlPtr2->oldY < (y + height))) {
2340 dlPtr2->oldY = -1;
2341 }
2342 }
2343
2344 /*
2345 * Now scroll the lines. This may generate damage which we
2346 * handle by calling TextInvalidateRegion to mark the display
2347 * blocks as stale.
2348 */
2349
2350 damageRgn = TkCreateRegion();
2351 if (TkScrollWindow(textPtr->tkwin, dInfoPtr->scrollGC,
2352 dInfoPtr->x, oldY,
2353 (dInfoPtr->maxX - dInfoPtr->x), height,
2354 0, y - oldY, damageRgn)) {
2355 TextInvalidateRegion(textPtr, damageRgn);
2356 }
2357 numCopies++;
2358 TkDestroyRegion(damageRgn);
2359 }
2360
2361 /*
2362 * Clear the REDRAW_PENDING flag here. This is actually pretty
2363 * tricky. We want to wait until *after* doing the scrolling,
2364 * since that could generate more areas to redraw and don't
2365 * want to reschedule a redisplay for them. On the other hand,
2366 * we can't wait until after all the redisplaying, because the
2367 * act of redisplaying could actually generate more redisplays
2368 * (e.g. in the case of a nested window with event bindings triggered
2369 * by redisplay).
2370 */
2371
2372 dInfoPtr->flags &= ~REDRAW_PENDING;
2373
2374 /*
2375 * Redraw the borders if that's needed.
2376 */
2377
2378 if (dInfoPtr->flags & REDRAW_BORDERS) {
2379 if (tkTextDebug) {
2380 #if 0
2381 Tcl_SetVar2(interp, "tk_textRedraw", (char *) NULL, "borders",
2382 TCL_GLOBAL_ONLY|TCL_APPEND_VALUE|TCL_LIST_ELEMENT);
2383 #endif
2384 }
2385
2386 if (textPtr->tkwin == NULL) {
2387
2388 /*
2389 * The widget has been deleted. Don't do anything.
2390 */
2391
2392 goto end;
2393 }
2394
2395 Tk_Draw3DRectangle(textPtr->tkwin, Tk_WindowId(textPtr->tkwin),
2396 textPtr->border, textPtr->highlightWidth,
2397 textPtr->highlightWidth,
2398 Tk_Width(textPtr->tkwin) - 2*textPtr->highlightWidth,
2399 Tk_Height(textPtr->tkwin) - 2*textPtr->highlightWidth,
2400 textPtr->borderWidth, textPtr->relief);
2401 if (textPtr->highlightWidth != 0) {
2402 GC fgGC, bgGC;
2403
2404 bgGC = Tk_GCForColor(textPtr->highlightBgColorPtr,
2405 Tk_WindowId(textPtr->tkwin));
2406 if (textPtr->flags & GOT_FOCUS) {
2407 fgGC = Tk_GCForColor(textPtr->highlightColorPtr,
2408 Tk_WindowId(textPtr->tkwin));
2409 TkpDrawHighlightBorder(textPtr->tkwin, fgGC, bgGC,
2410 textPtr->highlightWidth, Tk_WindowId(textPtr->tkwin));
2411 } else {
2412 TkpDrawHighlightBorder(textPtr->tkwin, bgGC, bgGC,
2413 textPtr->highlightWidth, Tk_WindowId(textPtr->tkwin));
2414 }
2415 }
2416 borders = textPtr->borderWidth + textPtr->highlightWidth;
2417 if (textPtr->padY > 0) {
2418 Tk_Fill3DRectangle(textPtr->tkwin, Tk_WindowId(textPtr->tkwin),
2419 textPtr->border, borders, borders,
2420 Tk_Width(textPtr->tkwin) - 2*borders, textPtr->padY,
2421 0, TK_RELIEF_FLAT);
2422 Tk_Fill3DRectangle(textPtr->tkwin, Tk_WindowId(textPtr->tkwin),
2423 textPtr->border, borders,
2424 Tk_Height(textPtr->tkwin) - borders - textPtr->padY,
2425 Tk_Width(textPtr->tkwin) - 2*borders,
2426 textPtr->padY, 0, TK_RELIEF_FLAT);
2427 }
2428 if (textPtr->padX > 0) {
2429 Tk_Fill3DRectangle(textPtr->tkwin, Tk_WindowId(textPtr->tkwin),
2430 textPtr->border, borders, borders + textPtr->padY,
2431 textPtr->padX,
2432 Tk_Height(textPtr->tkwin) - 2*borders -2*textPtr->padY,
2433 0, TK_RELIEF_FLAT);
2434 Tk_Fill3DRectangle(textPtr->tkwin, Tk_WindowId(textPtr->tkwin),
2435 textPtr->border,
2436 Tk_Width(textPtr->tkwin) - borders - textPtr->padX,
2437 borders + textPtr->padY, textPtr->padX,
2438 Tk_Height(textPtr->tkwin) - 2*borders -2*textPtr->padY,
2439 0, TK_RELIEF_FLAT);
2440 }
2441 dInfoPtr->flags &= ~REDRAW_BORDERS;
2442 }
2443
2444 /*
2445 * Now we have to redraw the lines that couldn't be updated by
2446 * scrolling. First, compute the height of the largest line and
2447 * allocate an off-screen pixmap to use for double-buffered
2448 * displays.
2449 */
2450
2451 maxHeight = -1;
2452 for (dlPtr = dInfoPtr->dLinePtr; dlPtr != NULL;
2453 dlPtr = dlPtr->nextPtr) {
2454 if ((dlPtr->height > maxHeight) && (dlPtr->oldY != dlPtr->y)) {
2455 maxHeight = dlPtr->height;
2456 }
2457 bottomY = dlPtr->y + dlPtr->height;
2458 }
2459 if (maxHeight > dInfoPtr->maxY) {
2460 maxHeight = dInfoPtr->maxY;
2461 }
2462 if (maxHeight > 0) {
2463 pixmap = Tk_GetPixmap(Tk_Display(textPtr->tkwin),
2464 Tk_WindowId(textPtr->tkwin), Tk_Width(textPtr->tkwin),
2465 maxHeight, Tk_Depth(textPtr->tkwin));
2466 for (prevPtr = NULL, dlPtr = textPtr->dInfoPtr->dLinePtr;
2467 (dlPtr != NULL) && (dlPtr->y < dInfoPtr->maxY);
2468 prevPtr = dlPtr, dlPtr = dlPtr->nextPtr) {
2469 if (dlPtr->chunkPtr == NULL) continue;
2470 if (dlPtr->oldY != dlPtr->y) {
2471 if (tkTextDebug) {
2472 char string[TK_POS_CHARS];
2473 TkTextPrintIndex(&dlPtr->index, string);
2474 #if 0
2475 Tcl_SetVar2(textPtr->interp, "tk_textRedraw",
2476 (char *) NULL, string,
2477 TCL_GLOBAL_ONLY|TCL_APPEND_VALUE|TCL_LIST_ELEMENT);
2478 #endif
2479 }
2480 DisplayDLine(textPtr, dlPtr, prevPtr, pixmap);
2481 if (dInfoPtr->dLinesInvalidated) {
2482 Tk_FreePixmap(Tk_Display(textPtr->tkwin), pixmap);
2483 return;
2484 }
2485 dlPtr->oldY = dlPtr->y;
2486 dlPtr->flags &= ~NEW_LAYOUT;
2487 }
2488 /*prevPtr = dlPtr;*/
2489 }
2490 Tk_FreePixmap(Tk_Display(textPtr->tkwin), pixmap);
2491 }
2492
2493 /*
2494 * See if we need to refresh the part of the window below the
2495 * last line of text (if there is any such area). Refresh the
2496 * padding area on the left too, since the insertion cursor might
2497 * have been displayed there previously).
2498 */
2499
2500 if (dInfoPtr->topOfEof > dInfoPtr->maxY) {
2501 dInfoPtr->topOfEof = dInfoPtr->maxY;
2502 }
2503 if (bottomY < dInfoPtr->topOfEof) {
2504 if (tkTextDebug) {
2505 #if 0
2506 Tcl_SetVar2(textPtr->interp, "tk_textRedraw",
2507 (char *) NULL, "eof",
2508 TCL_GLOBAL_ONLY|TCL_APPEND_VALUE|TCL_LIST_ELEMENT);
2509 #endif
2510 }
2511
2512 if (textPtr->tkwin == NULL) {
2513
2514 /*
2515 * The widget has been deleted. Don't do anything.
2516 */
2517
2518 goto end;
2519 }
2520
2521 Tk_Fill3DRectangle(textPtr->tkwin, Tk_WindowId(textPtr->tkwin),
2522 textPtr->border, dInfoPtr->x - textPtr->padX, bottomY,
2523 dInfoPtr->maxX - (dInfoPtr->x - textPtr->padX),
2524 dInfoPtr->topOfEof-bottomY, 0, TK_RELIEF_FLAT);
2525 }
2526 dInfoPtr->topOfEof = bottomY;
2527
2528 doScrollbars:
2529
2530 /*
2531 * Update the vertical scrollbar, if there is one. Note: it's
2532 * important to clear REDRAW_PENDING here, just in case the
2533 * scroll procedure does something that requires redisplay.
2534 */
2535
2536 if (textPtr->flags & UPDATE_SCROLLBARS) {
2537 textPtr->flags &= ~UPDATE_SCROLLBARS;
2538 if (textPtr->yScrollCmd != NULL) {
2539 GetYView(textPtr->interp, textPtr, 1);
2540 }
2541
2542 if (textPtr->tkwin == NULL) {
2543
2544 /*
2545 * The widget has been deleted. Don't do anything.
2546 */
2547
2548 goto end;
2549 }
2550
2551 /*
2552 * Update the horizontal scrollbar, if any.
2553 */
2554
2555 if (textPtr->xScrollCmd != NULL) {
2556 GetXView(textPtr->interp, textPtr, 1);
2557 }
2558 }
2559
2560 end:
2561 Tcl_Release((ClientData) interp);
2562 }
2563
2564 /*
2565 *----------------------------------------------------------------------
2566 *
2567 * TkTextEventuallyRepick --
2568 *
2569 * This procedure is invoked whenever something happens that
2570 * could change the current character or the tags associated
2571 * with it.
2572 *
2573 * Results:
2574 * None.
2575 *
2576 * Side effects:
2577 * A repick is scheduled as an idle handler.
2578 *
2579 *----------------------------------------------------------------------
2580 */
2581
2582 /* ARGSUSED */
2583 void
2584 TkTextEventuallyRepick(textPtr)
2585 TkText *textPtr; /* Widget record for text widget. */
2586 {
2587 TextDInfo *dInfoPtr = textPtr->dInfoPtr;
2588
2589 dInfoPtr->flags |= REPICK_NEEDED;
2590 if (!(dInfoPtr->flags & REDRAW_PENDING)) {
2591 dInfoPtr->flags |= REDRAW_PENDING;
2592 Tcl_DoWhenIdle(DisplayText, (ClientData) textPtr);
2593 }
2594 }
2595
2596 /*
2597 *----------------------------------------------------------------------
2598 *
2599 * TkTextRedrawRegion --
2600 *
2601 * This procedure is invoked to schedule a redisplay for a given
2602 * region of a text widget. The redisplay itself may not occur
2603 * immediately: it's scheduled as a when-idle handler.
2604 *
2605 * Results:
2606 * None.
2607 *
2608 * Side effects:
2609 * Information will eventually be redrawn on the screen.
2610 *
2611 *----------------------------------------------------------------------
2612 */
2613
2614 /* ARGSUSED */
2615 void
2616 TkTextRedrawRegion(textPtr, x, y, width, height)
2617 TkText *textPtr; /* Widget record for text widget. */
2618 int x, y; /* Coordinates of upper-left corner of area
2619 * to be redrawn, in pixels relative to
2620 * textPtr's window. */
2621 int width, height; /* Width and height of area to be redrawn. */
2622 {
2623 TextDInfo *dInfoPtr = textPtr->dInfoPtr;
2624 TkRegion damageRgn = TkCreateRegion();
2625 XRectangle rect;
2626
2627 rect.x = x;
2628 rect.y = y;
2629 rect.width = width;
2630 rect.height = height;
2631 TkUnionRectWithRegion(&rect, damageRgn, damageRgn);
2632
2633 TextInvalidateRegion(textPtr, damageRgn);
2634
2635 if (!(dInfoPtr->flags & REDRAW_PENDING)) {
2636 dInfoPtr->flags |= REDRAW_PENDING;
2637 Tcl_DoWhenIdle(DisplayText, (ClientData) textPtr);
2638 }
2639 TkDestroyRegion(damageRgn);
2640 }
2641
2642 /*
2643 *----------------------------------------------------------------------
2644 *
2645 * TextInvalidateRegion --
2646 *
2647 * Mark a region of text as invalid.
2648 *
2649 * Results:
2650 * None.
2651 *
2652 * Side effects:
2653 * Updates the display information for the text widget.
2654 *
2655 *----------------------------------------------------------------------
2656 */
2657
2658 static void
2659 TextInvalidateRegion(textPtr, region)
2660 TkText *textPtr; /* Widget record for text widget. */
2661 TkRegion region; /* Region of area to redraw. */
2662 {
2663 register DLine *dlPtr;
2664 TextDInfo *dInfoPtr = textPtr->dInfoPtr;
2665 int maxY, inset;
2666 XRectangle rect;
2667
2668 /*
2669 * Find all lines that overlap the given region and mark them for
2670 * redisplay.
2671 */
2672
2673 TkClipBox(region, &rect);
2674 maxY = rect.y + rect.height;
2675 for (dlPtr = dInfoPtr->dLinePtr; dlPtr != NULL;
2676 dlPtr = dlPtr->nextPtr) {
2677 if ((dlPtr->oldY != -1) && (TkRectInRegion(region, rect.x, dlPtr->y,
2678 rect.width, (unsigned int) dlPtr->height) != RectangleOut)) {
2679 dlPtr->oldY = -1;
2680 }
2681 }
2682 if (dInfoPtr->topOfEof < maxY) {
2683 dInfoPtr->topOfEof = maxY;
2684 }
2685
2686 /*
2687 * Schedule the redisplay operation if there isn't one already
2688 * scheduled.
2689 */
2690
2691 inset = textPtr->borderWidth + textPtr->highlightWidth;
2692 if ((rect.x < (inset + textPtr->padX))
2693 || (rect.y < (inset + textPtr->padY))
2694 || ((int) (rect.x + rect.width) > (Tk_Width(textPtr->tkwin)
2695 - inset - textPtr->padX))
2696 || (maxY > (Tk_Height(textPtr->tkwin) - inset - textPtr->padY))) {
2697 dInfoPtr->flags |= REDRAW_BORDERS;
2698 }
2699 }
2700
2701 /*
2702 *----------------------------------------------------------------------
2703 *
2704 * TkTextChanged --
2705 *
2706 * This procedure is invoked when info in a text widget is about
2707 * to be modified in a way that changes how it is displayed (e.g.
2708 * characters were inserted or deleted, or tag information was
2709 * changed). This procedure must be called *before* a change is
2710 * made, so that indexes in the display information are still
2711 * valid.
2712 *
2713 * Results:
2714 * None.
2715 *
2716 * Side effects:
2717 * The range of character between index1Ptr (inclusive) and
2718 * index2Ptr (exclusive) will be redisplayed at some point in the
2719 * future (the actual redisplay is scheduled as a when-idle handler).
2720 *
2721 *----------------------------------------------------------------------
2722 */
2723
2724 void
2725 TkTextChanged(textPtr, index1Ptr, index2Ptr)
2726 TkText *textPtr; /* Widget record for text widget. */
2727 TkTextIndex *index1Ptr; /* Index of first character to redisplay. */
2728 TkTextIndex *index2Ptr; /* Index of character just after last one
2729 * to redisplay. */
2730 {
2731 TextDInfo *dInfoPtr = textPtr->dInfoPtr;
2732 DLine *firstPtr, *lastPtr;
2733 TkTextIndex rounded;
2734
2735 /*
2736 * Schedule both a redisplay and a recomputation of display information.
2737 * It's done here rather than the end of the procedure for two reasons:
2738 *
2739 * 1. If there are no display lines to update we'll want to return
2740 * immediately, well before the end of the procedure.
2741 * 2. It's important to arrange for the redisplay BEFORE calling
2742 * FreeDLines. The reason for this is subtle and has to do with
2743 * embedded windows. The chunk delete procedure for an embedded
2744 * window will schedule an idle handler to unmap the window.
2745 * However, we want the idle handler for redisplay to be called
2746 * first, so that it can put the embedded window back on the screen
2747 * again (if appropriate). This will prevent the window from ever
2748 * being unmapped, and thereby avoid flashing.
2749 */
2750
2751 if (!(dInfoPtr->flags & REDRAW_PENDING)) {
2752 Tcl_DoWhenIdle(DisplayText, (ClientData) textPtr);
2753 }
2754 dInfoPtr->flags |= REDRAW_PENDING|DINFO_OUT_OF_DATE|REPICK_NEEDED;
2755
2756 /*
2757 * Find the DLines corresponding to index1Ptr and index2Ptr. There
2758 * is one tricky thing here, which is that we have to relayout in
2759 * units of whole text lines: round index1Ptr back to the beginning
2760 * of its text line, and include all the display lines after index2,
2761 * up to the end of its text line. This is necessary because the
2762 * indices stored in the display lines will no longer be valid. It's
2763 * also needed because any edit could change the way lines wrap.
2764 */
2765
2766 rounded = *index1Ptr;
2767 rounded.byteIndex = 0;
2768 firstPtr = FindDLine(dInfoPtr->dLinePtr, &rounded);
2769 if (firstPtr == NULL) {
2770 return;
2771 }
2772 lastPtr = FindDLine(dInfoPtr->dLinePtr, index2Ptr);
2773 while ((lastPtr != NULL)
2774 && (lastPtr->index.linePtr == index2Ptr->linePtr)) {
2775 lastPtr = lastPtr->nextPtr;
2776 }
2777
2778 /*
2779 * Delete all the DLines from firstPtr up to but not including lastPtr.
2780 */
2781
2782 FreeDLines(textPtr, firstPtr, lastPtr, 1);
2783 }
2784
2785 /*
2786 *----------------------------------------------------------------------
2787 *
2788 * TkTextRedrawTag --
2789 *
2790 * This procedure is invoked to request a redraw of all characters
2791 * in a given range that have a particular tag on or off. It's
2792 * called, for example, when tag options change.
2793 *
2794 * Results:
2795 * None.
2796 *
2797 * Side effects:
2798 * Information on the screen may be redrawn, and the layout of
2799 * the screen may change.
2800 *
2801 *----------------------------------------------------------------------
2802 */
2803
2804 void
2805 TkTextRedrawTag(textPtr, index1Ptr, index2Ptr, tagPtr, withTag)
2806 TkText *textPtr; /* Widget record for text widget. */
2807 TkTextIndex *index1Ptr; /* First character in range to consider
2808 * for redisplay. NULL means start at
2809 * beginning of text. */
2810 TkTextIndex *index2Ptr; /* Character just after last one to consider
2811 * for redisplay. NULL means process all
2812 * the characters in the text. */
2813 TkTextTag *tagPtr; /* Information about tag. */
2814 int withTag; /* 1 means redraw characters that have the
2815 * tag, 0 means redraw those without. */
2816 {
2817 register DLine *dlPtr;
2818 DLine *endPtr;
2819 int tagOn;
2820 TkTextSearch search;
2821 TextDInfo *dInfoPtr = textPtr->dInfoPtr;
2822 TkTextIndex *curIndexPtr;
2823 TkTextIndex endOfText, *endIndexPtr;
2824
2825 /*
2826 * Round up the starting position if it's before the first line
2827 * visible on the screen (we only care about what's on the screen).
2828 */
2829
2830 dlPtr = dInfoPtr->dLinePtr;
2831 if (dlPtr == NULL) {
2832 return;
2833 }
2834 if ((index1Ptr == NULL) || (TkTextIndexCmp(&dlPtr->index, index1Ptr) > 0)) {
2835 index1Ptr = &dlPtr->index;
2836 }
2837
2838 /*
2839 * Set the stopping position if it wasn't specified.
2840 */
2841
2842 if (index2Ptr == NULL) {
2843 index2Ptr = TkTextMakeByteIndex(textPtr->tree,
2844 TkBTreeNumLines(textPtr->tree), 0, &endOfText);
2845 }
2846
2847 /*
2848 * Initialize a search through all transitions on the tag, starting
2849 * with the first transition where the tag's current state is different
2850 * from what it will eventually be.
2851 */
2852
2853 TkBTreeStartSearch(index1Ptr, index2Ptr, tagPtr, &search);
2854 /*
2855 * Make our own curIndex because at this point search.curIndex
2856 * may not equal index1Ptr->curIndex in the case the first tag toggle
2857 * comes after index1Ptr (See the use of FindTagStart in TkBTreeStartSearch)
2858 */
2859 curIndexPtr = index1Ptr;
2860 tagOn = TkBTreeCharTagged(index1Ptr, tagPtr);
2861 if (tagOn != withTag) {
2862 if (!TkBTreeNextTag(&search)) {
2863 return;
2864 }
2865 curIndexPtr = &search.curIndex;
2866 }
2867
2868 /*
2869 * Schedule a redisplay and layout recalculation if they aren't
2870 * already pending. This has to be done before calling FreeDLines,
2871 * for the reason given in TkTextChanged.
2872 */
2873
2874 if (!(dInfoPtr->flags & REDRAW_PENDING)) {
2875 Tcl_DoWhenIdle(DisplayText, (ClientData) textPtr);
2876 }
2877 dInfoPtr->flags |= REDRAW_PENDING|DINFO_OUT_OF_DATE|REPICK_NEEDED;
2878
2879 /*
2880 * Each loop through the loop below is for one range of characters
2881 * where the tag's current state is different than its eventual
2882 * state. At the top of the loop, search contains information about
2883 * the first character in the range.
2884 */
2885
2886 while (1) {
2887 /*
2888 * Find the first DLine structure in the range. Note: if the
2889 * desired character isn't the first in its text line, then look
2890 * for the character just before it instead. This is needed to
2891 * handle the case where the first character of a wrapped
2892 * display line just got smaller, so that it now fits on the
2893 * line before: need to relayout the line containing the
2894 * previous character.
2895 */
2896
2897 if (curIndexPtr->byteIndex == 0) {
2898 dlPtr = FindDLine(dlPtr, curIndexPtr);
2899 } else {
2900 TkTextIndex tmp;
2901
2902 tmp = *curIndexPtr;
2903 tmp.byteIndex -= 1;
2904 dlPtr = FindDLine(dlPtr, &tmp);
2905 }
2906 if (dlPtr == NULL) {
2907 break;
2908 }
2909
2910 /*
2911 * Find the first DLine structure that's past the end of the range.
2912 */
2913
2914 if (!TkBTreeNextTag(&search)) {
2915 endIndexPtr = index2Ptr;
2916 } else {
2917 curIndexPtr = &search.curIndex;
2918 endIndexPtr = curIndexPtr;
2919 }
2920 endPtr = FindDLine(dlPtr, endIndexPtr);
2921 if ((endPtr != NULL) && (endPtr->index.linePtr == endIndexPtr->linePtr)
2922 && (endPtr->index.byteIndex < endIndexPtr->byteIndex)) {
2923 endPtr = endPtr->nextPtr;
2924 }
2925
2926 /*
2927 * Delete all of the display lines in the range, so that they'll
2928 * be re-layed out and redrawn.
2929 */
2930
2931 FreeDLines(textPtr, dlPtr, endPtr, 1);
2932 dlPtr = endPtr;
2933
2934 /*
2935 * Find the first text line in the next range.
2936 */
2937
2938 if (!TkBTreeNextTag(&search)) {
2939 break;
2940 }
2941 }
2942 }
2943
2944 /*
2945 *----------------------------------------------------------------------
2946 *
2947 * TkTextRelayoutWindow --
2948 *
2949 * This procedure is called when something has happened that
2950 * invalidates the whole layout of characters on the screen, such
2951 * as a change in a configuration option for the overall text
2952 * widget or a change in the window size. It causes all display
2953 * information to be recomputed and the window to be redrawn.
2954 *
2955 * Results:
2956 * None.
2957 *
2958 * Side effects:
2959 * All the display information will be recomputed for the window
2960 * and the window will be redrawn.
2961 *
2962 *----------------------------------------------------------------------
2963 */
2964
2965 void
2966 TkTextRelayoutWindow(textPtr)
2967 TkText *textPtr; /* Widget record for text widget. */
2968 {
2969 TextDInfo *dInfoPtr = textPtr->dInfoPtr;
2970 GC new;
2971 XGCValues gcValues;
2972
2973 /*
2974 * Schedule the window redisplay. See TkTextChanged for the
2975 * reason why this has to be done before any calls to FreeDLines.
2976 */
2977
2978 if (!(dInfoPtr->flags & REDRAW_PENDING)) {
2979 Tcl_DoWhenIdle(DisplayText, (ClientData) textPtr);
2980 }
2981 dInfoPtr->flags |= REDRAW_PENDING|REDRAW_BORDERS|DINFO_OUT_OF_DATE
2982 |REPICK_NEEDED;
2983
2984 /*
2985 * (Re-)create the graphics context for drawing the traversal
2986 * highlight.
2987 */
2988
2989 gcValues.graphics_exposures = False;
2990 new = Tk_GetGC(textPtr->tkwin, GCGraphicsExposures, &gcValues);
2991 if (dInfoPtr->copyGC != None) {
2992 Tk_FreeGC(textPtr->display, dInfoPtr->copyGC);
2993 }
2994 dInfoPtr->copyGC = new;
2995
2996 /*
2997 * Throw away all the current layout information.
2998 */
2999
3000 FreeDLines(textPtr, dInfoPtr->dLinePtr, (DLine *) NULL, 1);
3001 dInfoPtr->dLinePtr = NULL;
3002
3003 /*
3004 * Recompute some overall things for the layout. Even if the
3005 * window gets very small, pretend that there's at least one
3006 * pixel of drawing space in it.
3007 */
3008
3009 if (textPtr->highlightWidth < 0) {
3010 textPtr->highlightWidth = 0;
3011 }
3012 dInfoPtr->x = textPtr->highlightWidth + textPtr->borderWidth
3013 + textPtr->padX;
3014 dInfoPtr->y = textPtr->highlightWidth + textPtr->borderWidth
3015 + textPtr->padY;
3016 dInfoPtr->maxX = Tk_Width(textPtr->tkwin) - textPtr->highlightWidth
3017 - textPtr->borderWidth - textPtr->padX;
3018 if (dInfoPtr->maxX <= dInfoPtr->x) {
3019 dInfoPtr->maxX = dInfoPtr->x + 1;
3020 }
3021 dInfoPtr->maxY = Tk_Height(textPtr->tkwin) - textPtr->highlightWidth
3022 - textPtr->borderWidth - textPtr->padY;
3023 if (dInfoPtr->maxY <= dInfoPtr->y) {
3024 dInfoPtr->maxY = dInfoPtr->y + 1;
3025 }
3026 dInfoPtr->topOfEof = dInfoPtr->maxY;
3027
3028 /*
3029 * If the upper-left character isn't the first in a line, recompute
3030 * it. This is necessary because a change in the window's size
3031 * or options could change the way lines wrap.
3032 */
3033
3034 if (textPtr->topIndex.byteIndex != 0) {
3035 MeasureUp(textPtr, &textPtr->topIndex, 0, &textPtr->topIndex);
3036 }
3037
3038 /*
3039 * Invalidate cached scrollbar positions, so that scrollbars
3040 * sliders will be udpated.
3041 */
3042
3043 dInfoPtr->xScrollFirst = dInfoPtr->xScrollLast = -1;
3044 dInfoPtr->yScrollFirst = dInfoPtr->yScrollLast = -1;
3045 }
3046
3047 /*
3048 *----------------------------------------------------------------------
3049 *
3050 * TkTextSetYView --
3051 *
3052 * This procedure is called to specify what lines are to be
3053 * displayed in a text widget.
3054 *
3055 * Results:
3056 * None.
3057 *
3058 * Side effects:
3059 * The display will (eventually) be updated so that the position
3060 * given by "indexPtr" is visible on the screen at the position
3061 * determined by "pickPlace".
3062 *
3063 *----------------------------------------------------------------------
3064 */
3065
3066 void
3067 TkTextSetYView(textPtr, indexPtr, pickPlace)
3068 TkText *textPtr; /* Widget record for text widget. */
3069 TkTextIndex *indexPtr; /* Position that is to appear somewhere
3070 * in the view. */
3071 int pickPlace; /* 0 means topLine must appear at top of
3072 * screen. 1 means we get to pick where it
3073 * appears: minimize screen motion or else
3074 * display line at center of screen. */
3075 {
3076 TextDInfo *dInfoPtr = textPtr->dInfoPtr;
3077 register DLine *dlPtr;
3078 int bottomY, close, lineIndex;
3079 TkTextIndex tmpIndex, rounded;
3080 Tk_FontMetrics fm;
3081
3082 /*
3083 * If the specified position is the extra line at the end of the
3084 * text, round it back to the last real line.
3085 */
3086
3087 lineIndex = TkBTreeLineIndex(indexPtr->linePtr);
3088 if (lineIndex == TkBTreeNumLines(indexPtr->tree)) {
3089 TkTextIndexBackChars(indexPtr, 1, &rounded);
3090 indexPtr = &rounded;
3091 }
3092
3093 if (!pickPlace) {
3094 /*
3095 * The specified position must go at the top of the screen.
3096 * Just leave all the DLine's alone: we may be able to reuse
3097 * some of the information that's currently on the screen
3098 * without redisplaying it all.
3099 */
3100
3101 if (indexPtr->byteIndex == 0) {
3102 textPtr->topIndex = *indexPtr;
3103 } else {
3104 MeasureUp(textPtr, indexPtr, 0, &textPtr->topIndex);
3105 }
3106 goto scheduleUpdate;
3107 }
3108
3109 /*
3110 * We have to pick where to display the index. First, bring
3111 * the display information up to date and see if the index will be
3112 * completely visible in the current screen configuration. If so
3113 * then there's nothing to do.
3114 */
3115
3116 if (dInfoPtr->flags & DINFO_OUT_OF_DATE) {
3117 UpdateDisplayInfo(textPtr);
3118 }
3119 dlPtr = FindDLine(dInfoPtr->dLinePtr, indexPtr);
3120 if (dlPtr != NULL) {
3121 if ((dlPtr->y + dlPtr->height) > dInfoPtr->maxY) {
3122 /*
3123 * Part of the line hangs off the bottom of the screen;
3124 * pretend the whole line is off-screen.
3125 */
3126
3127 dlPtr = NULL;
3128 } else if ((dlPtr->index.linePtr == indexPtr->linePtr)
3129 && (dlPtr->index.byteIndex <= indexPtr->byteIndex)) {
3130 return;
3131 }
3132 }
3133
3134 /*
3135 * The desired line isn't already on-screen. Figure out what
3136 * it means to be "close" to the top or bottom of the screen.
3137 * Close means within 1/3 of the screen height or within three
3138 * lines, whichever is greater. Add one extra line also, to
3139 * account for the way MeasureUp rounds.
3140 */
3141
3142 Tk_GetFontMetrics(textPtr->tkfont, &fm);
3143 bottomY = (dInfoPtr->y + dInfoPtr->maxY + fm.linespace)/2;
3144 close = (dInfoPtr->maxY - dInfoPtr->y)/3;
3145 if (close < 3*fm.linespace) {
3146 close = 3*fm.linespace;
3147 }
3148 close += fm.linespace;
3149 if (dlPtr != NULL) {
3150 /*
3151 * The desired line is above the top of screen. If it is
3152 * "close" to the top of the window then make it the top
3153 * line on the screen.
3154 */
3155
3156 MeasureUp(textPtr, &textPtr->topIndex, close, &tmpIndex);
3157 if (TkTextIndexCmp(&tmpIndex, indexPtr) <= 0) {
3158 MeasureUp(textPtr, indexPtr, 0, &textPtr->topIndex);
3159 goto scheduleUpdate;
3160 }
3161 } else {
3162 /*
3163 * The desired line is below the bottom of the screen. If it is
3164 * "close" to the bottom of the screen then position it at the
3165 * bottom of the screen.
3166 */
3167
3168 MeasureUp(textPtr, indexPtr, close, &tmpIndex);
3169 if (FindDLine(dInfoPtr->dLinePtr, &tmpIndex) != NULL) {
3170 bottomY = dInfoPtr->maxY - dInfoPtr->y;
3171 }
3172 }
3173
3174 /*
3175 * Our job now is to arrange the display so that indexPtr appears
3176 * as low on the screen as possible but with its bottom no lower
3177 * than bottomY. BottomY is the bottom of the window if the
3178 * desired line is just below the current screen, otherwise it
3179 * is a half-line lower than the center of the window.
3180 */
3181
3182 MeasureUp(textPtr, indexPtr, bottomY, &textPtr->topIndex);
3183
3184 scheduleUpdate:
3185 if (!(dInfoPtr->flags & REDRAW_PENDING)) {
3186 Tcl_DoWhenIdle(DisplayText, (ClientData) textPtr);
3187 }
3188 dInfoPtr->flags |= REDRAW_PENDING|DINFO_OUT_OF_DATE|REPICK_NEEDED;
3189 }
3190
3191 /*
3192 *--------------------------------------------------------------
3193 *
3194 * MeasureUp --
3195 *
3196 * Given one index, find the index of the first character
3197 * on the highest display line that would be displayed no more
3198 * than "distance" pixels above the given index.
3199 *
3200 * Results:
3201 * *dstPtr is filled in with the index of the first character
3202 * on a display line. The display line is found by measuring
3203 * up "distance" pixels above the pixel just below an imaginary
3204 * display line that contains srcPtr. If the display line
3205 * that covers this coordinate actually extends above the
3206 * coordinate, then return the index of the next lower line
3207 * instead (i.e. the returned index will be completely visible
3208 * at or below the given y-coordinate).
3209 *
3210 * Side effects:
3211 * None.
3212 *
3213 *--------------------------------------------------------------
3214 */
3215
3216 static void
3217 MeasureUp(textPtr, srcPtr, distance, dstPtr)
3218 TkText *textPtr; /* Text widget in which to measure. */
3219 TkTextIndex *srcPtr; /* Index of character from which to start
3220 * measuring. */
3221 int distance; /* Vertical distance in pixels measured
3222 * from the pixel just below the lowest
3223 * one in srcPtr's line. */
3224 TkTextIndex *dstPtr; /* Index to fill in with result. */
3225 {
3226 int lineNum; /* Number of current line. */
3227 int bytesToCount; /* Maximum number of bytes to measure in
3228 * current line. */
3229 TkTextIndex bestIndex; /* Best candidate seen so far for result. */
3230 TkTextIndex index;
3231 DLine *dlPtr, *lowestPtr;
3232 int noBestYet; /* 1 means bestIndex hasn't been set. */
3233
3234 noBestYet = 1;
3235 bytesToCount = srcPtr->byteIndex + 1;
3236 index.tree = srcPtr->tree;
3237 for (lineNum = TkBTreeLineIndex(srcPtr->linePtr); lineNum >= 0;
3238 lineNum--) {
3239 /*
3240 * Layout an entire text line (potentially > 1 display line).
3241 * For the first line, which contains srcPtr, only layout the
3242 * part up through srcPtr (bytesToCount is non-infinite to
3243 * accomplish this). Make a list of all the display lines
3244 * in backwards order (the lowest DLine on the screen is first
3245 * in the list).
3246 */
3247
3248 index.linePtr = TkBTreeFindLine(srcPtr->tree, lineNum);
3249 index.byteIndex = 0;
3250 lowestPtr = NULL;
3251 do {
3252 dlPtr = LayoutDLine(textPtr, &index);
3253 dlPtr->nextPtr = lowestPtr;
3254 lowestPtr = dlPtr;
3255 TkTextIndexForwBytes(&index, dlPtr->byteCount, &index);
3256 bytesToCount -= dlPtr->byteCount;
3257 } while ((bytesToCount > 0) && (index.linePtr == dlPtr->index.linePtr));
3258
3259 /*
3260 * Scan through the display lines to see if we've covered enough
3261 * vertical distance. If so, save the starting index for the
3262 * line at the desired location.
3263 */
3264
3265 for (dlPtr = lowestPtr; dlPtr != NULL; dlPtr = dlPtr->nextPtr) {
3266 distance -= dlPtr->height;
3267 if (distance < 0) {
3268 *dstPtr = (noBestYet) ? dlPtr->index : bestIndex;
3269 break;
3270 }
3271 bestIndex = dlPtr->index;
3272 noBestYet = 0;
3273 }
3274
3275 /*
3276 * Discard the display lines, then either return or prepare
3277 * for the next display line to lay out.
3278 */
3279
3280 FreeDLines(textPtr, lowestPtr, (DLine *) NULL, 0);
3281 if (distance < 0) {
3282 return;
3283 }
3284 bytesToCount = INT_MAX; /* Consider all chars. in next line. */
3285 }
3286
3287 /*
3288 * Ran off the beginning of the text. Return the first character
3289 * in the text.
3290 */
3291
3292 TkTextMakeByteIndex(textPtr->tree, 0, 0, dstPtr);
3293 }
3294
3295 /*
3296 *--------------------------------------------------------------
3297 *
3298 * TkTextSeeCmd --
3299 *
3300 * This procedure is invoked to process the "see" option for
3301 * the widget command for text widgets. See the user documentation
3302 * for details on what it does.
3303 *
3304 * Results:
3305 * A standard Tcl result.
3306 *
3307 * Side effects:
3308 * See the user documentation.
3309 *
3310 *--------------------------------------------------------------
3311 */
3312
3313 int
3314 TkTextSeeCmd(textPtr, interp, argc, argv)
3315 TkText *textPtr; /* Information about text widget. */
3316 Tcl_Interp *interp; /* Current interpreter. */
3317 int argc; /* Number of arguments. */
3318 char **argv; /* Argument strings. Someone else has already
3319 * parsed this command enough to know that
3320 * argv[1] is "see". */
3321 {
3322 TextDInfo *dInfoPtr = textPtr->dInfoPtr;
3323 TkTextIndex index;
3324 int x, y, width, height, lineWidth, byteCount, oneThird, delta;
3325 DLine *dlPtr;
3326 TkTextDispChunk *chunkPtr;
3327
3328 if (argc != 3) {
3329 Tcl_AppendResult(interp, "wrong # args: should be \"",
3330 argv[0], " see index\"", (char *) NULL);
3331 return TCL_ERROR;
3332 }
3333 if (TkTextGetIndex(interp, textPtr, argv[2], &index) != TCL_OK) {
3334 return TCL_ERROR;
3335 }
3336
3337 /*
3338 * If the specified position is the extra line at the end of the
3339 * text, round it back to the last real line.
3340 */
3341
3342 if (TkBTreeLineIndex(index.linePtr) == TkBTreeNumLines(index.tree)) {
3343 TkTextIndexBackChars(&index, 1, &index);
3344 }
3345
3346 /*
3347 * First get the desired position into the vertical range of the window.
3348 */
3349
3350 TkTextSetYView(textPtr, &index, 1);
3351
3352 /*
3353 * Now make sure that the character is in view horizontally.
3354 */
3355
3356 if (dInfoPtr->flags & DINFO_OUT_OF_DATE) {
3357 UpdateDisplayInfo(textPtr);
3358 }
3359 lineWidth = dInfoPtr->maxX - dInfoPtr->x;
3360 if (dInfoPtr->maxLength < lineWidth) {
3361 return TCL_OK;
3362 }
3363
3364 /*
3365 * Find the chunk that contains the desired index.
3366 * dlPtr may be NULL if the widget is not mapped. [Bug #641778]
3367 */
3368
3369 dlPtr = FindDLine(dInfoPtr->dLinePtr, &index);
3370 if (dlPtr == NULL) {
3371 return TCL_OK;
3372 }
3373
3374 byteCount = index.byteIndex - dlPtr->index.byteIndex;
3375 for (chunkPtr = dlPtr->chunkPtr; chunkPtr != NULL ;
3376 chunkPtr = chunkPtr->nextPtr) {
3377 if (byteCount < chunkPtr->numBytes) {
3378 break;
3379 }
3380 byteCount -= chunkPtr->numBytes;
3381 }
3382
3383 /*
3384 * Call a chunk-specific procedure to find the horizontal range of
3385 * the character within the chunk.
3386 * chunkPtr is NULL if trying to see in elided region.
3387 */
3388
3389 if (chunkPtr != NULL) {
3390 (*chunkPtr->bboxProc)(chunkPtr, byteCount,
3391 dlPtr->y + dlPtr->spaceAbove,
3392 dlPtr->height - dlPtr->spaceAbove - dlPtr->spaceBelow,
3393 dlPtr->baseline - dlPtr->spaceAbove, &x, &y, &width,
3394 &height);
3395 delta = x - dInfoPtr->curPixelOffset;
3396 oneThird = lineWidth/3;
3397 if (delta < 0) {
3398 if (delta < -oneThird) {
3399 dInfoPtr->newByteOffset = (x - lineWidth/2)
3400 / textPtr->charWidth;
3401 } else {
3402 dInfoPtr->newByteOffset -= ((-delta) + textPtr->charWidth - 1)
3403 / textPtr->charWidth;
3404 }
3405 } else {
3406 delta -= (lineWidth - width);
3407 if (delta > 0) {
3408 if (delta > oneThird) {
3409 dInfoPtr->newByteOffset = (x - lineWidth/2)
3410 / textPtr->charWidth;
3411 } else {
3412 dInfoPtr->newByteOffset += (delta + textPtr->charWidth - 1)
3413 / textPtr->charWidth;
3414 }
3415 } else {
3416 return TCL_OK;
3417 }
3418 }
3419 }
3420 dInfoPtr->flags |= DINFO_OUT_OF_DATE;
3421 if (!(dInfoPtr->flags & REDRAW_PENDING)) {
3422 dInfoPtr->flags |= REDRAW_PENDING;
3423 Tcl_DoWhenIdle(DisplayText, (ClientData) textPtr);
3424 }
3425 return TCL_OK;
3426 }
3427
3428 /*
3429 *--------------------------------------------------------------
3430 *
3431 * TkTextXviewCmd --
3432 *
3433 * This procedure is invoked to process the "xview" option for
3434 * the widget command for text widgets. See the user documentation
3435 * for details on what it does.
3436 *
3437 * Results:
3438 * A standard Tcl result.
3439 *
3440 * Side effects:
3441 * See the user documentation.
3442 *
3443 *--------------------------------------------------------------
3444 */
3445
3446 int
3447 TkTextXviewCmd(textPtr, interp, argc, argv)
3448 TkText *textPtr; /* Information about text widget. */
3449 Tcl_Interp *interp; /* Current interpreter. */
3450 int argc; /* Number of arguments. */
3451 char **argv; /* Argument strings. Someone else has already
3452 * parsed this command enough to know that
3453 * argv[1] is "xview". */
3454 {
3455 TextDInfo *dInfoPtr = textPtr->dInfoPtr;
3456 int type, charsPerPage, count, newOffset;
3457 double fraction;
3458
3459 if (dInfoPtr->flags & DINFO_OUT_OF_DATE) {
3460 UpdateDisplayInfo(textPtr);
3461 }
3462
3463 if (argc == 2) {
3464 GetXView(interp, textPtr, 0);
3465 return TCL_OK;
3466 }
3467
3468 newOffset = dInfoPtr->newByteOffset;
3469 type = Tk_GetScrollInfo(interp, argc, argv, &fraction, &count);
3470 switch (type) {
3471 case TK_SCROLL_ERROR:
3472 return TCL_ERROR;
3473 case TK_SCROLL_MOVETO:
3474 if (fraction > 1.0) {
3475 fraction = 1.0;
3476 }
3477 if (fraction < 0) {
3478 fraction = 0;
3479 }
3480 newOffset = (int) (((fraction * dInfoPtr->maxLength) / textPtr->charWidth)
3481 + 0.5);
3482 break;
3483 case TK_SCROLL_PAGES:
3484 charsPerPage = ((dInfoPtr->maxX - dInfoPtr->x) / textPtr->charWidth)
3485 - 2;
3486 if (charsPerPage < 1) {
3487 charsPerPage = 1;
3488 }
3489 newOffset += charsPerPage * count;
3490 break;
3491 case TK_SCROLL_UNITS:
3492 newOffset += count;
3493 break;
3494 }
3495
3496 dInfoPtr->newByteOffset = newOffset;
3497 dInfoPtr->flags |= DINFO_OUT_OF_DATE;
3498 if (!(dInfoPtr->flags & REDRAW_PENDING)) {
3499 dInfoPtr->flags |= REDRAW_PENDING;
3500 Tcl_DoWhenIdle(DisplayText, (ClientData) textPtr);
3501 }
3502 return TCL_OK;
3503 }
3504
3505 /*
3506 *----------------------------------------------------------------------
3507 *
3508 * ScrollByLines --
3509 *
3510 * This procedure is called to scroll a text widget up or down
3511 * by a given number of lines.
3512 *
3513 * Results:
3514 * None.
3515 *
3516 * Side effects:
3517 * The view in textPtr's window changes to reflect the value
3518 * of "offset".
3519 *
3520 *----------------------------------------------------------------------
3521 */
3522
3523 static void
3524 ScrollByLines(textPtr, offset)
3525 TkText *textPtr; /* Widget to scroll. */
3526 int offset; /* Amount by which to scroll, in *screen*
3527 * lines. Positive means that information
3528 * later in text becomes visible, negative
3529 * means that information earlier in the
3530 * text becomes visible. */
3531 {
3532 int i, bytesToCount, lineNum;
3533 TkTextIndex new, index;
3534 TkTextLine *lastLinePtr;
3535 TextDInfo *dInfoPtr = textPtr->dInfoPtr;
3536 DLine *dlPtr, *lowestPtr;
3537
3538 if (offset < 0) {
3539 /*
3540 * Must scroll up (to show earlier information in the text).
3541 * The code below is similar to that in MeasureUp, except that
3542 * it counts lines instead of pixels.
3543 */
3544
3545 bytesToCount = textPtr->topIndex.byteIndex + 1;
3546 index.tree = textPtr->tree;
3547 offset--; /* Skip line containing topIndex. */
3548 for (lineNum = TkBTreeLineIndex(textPtr->topIndex.linePtr);
3549 lineNum >= 0; lineNum--) {
3550 index.linePtr = TkBTreeFindLine(textPtr->tree, lineNum);
3551 index.byteIndex = 0;
3552 lowestPtr = NULL;
3553 do {
3554 dlPtr = LayoutDLine(textPtr, &index);
3555 dlPtr->nextPtr = lowestPtr;
3556 lowestPtr = dlPtr;
3557 TkTextIndexForwBytes(&index, dlPtr->byteCount, &index);
3558 bytesToCount -= dlPtr->byteCount;
3559 } while ((bytesToCount > 0)
3560 && (index.linePtr == dlPtr->index.linePtr));
3561
3562 for (dlPtr = lowestPtr; dlPtr != NULL; dlPtr = dlPtr->nextPtr) {
3563 offset++;
3564 if (offset == 0) {
3565 textPtr->topIndex = dlPtr->index;
3566 break;
3567 }
3568 }
3569
3570 /*
3571 * Discard the display lines, then either return or prepare
3572 * for the next display line to lay out.
3573 */
3574
3575 FreeDLines(textPtr, lowestPtr, (DLine *) NULL, 0);
3576 if (offset >= 0) {
3577 goto scheduleUpdate;
3578 }
3579 bytesToCount = INT_MAX;
3580 }
3581
3582 /*
3583 * Ran off the beginning of the text. Return the first character
3584 * in the text.
3585 */
3586
3587 TkTextMakeByteIndex(textPtr->tree, 0, 0, &textPtr->topIndex);
3588 } else {
3589 /*
3590 * Scrolling down, to show later information in the text.
3591 * Just count lines from the current top of the window.
3592 */
3593
3594 lastLinePtr = TkBTreeFindLine(textPtr->tree,
3595 TkBTreeNumLines(textPtr->tree));
3596 for (i = 0; i < offset; i++) {
3597 dlPtr = LayoutDLine(textPtr, &textPtr->topIndex);
3598 if (dlPtr->length == 0 && dlPtr->height == 0) offset++;
3599 dlPtr->nextPtr = NULL;
3600 TkTextIndexForwBytes(&textPtr->topIndex, dlPtr->byteCount, &new);
3601 FreeDLines(textPtr, dlPtr, (DLine *) NULL, 0);
3602 if (new.linePtr == lastLinePtr) {
3603 break;
3604 }
3605 textPtr->topIndex = new;
3606 }
3607 }
3608
3609 scheduleUpdate:
3610 if (!(dInfoPtr->flags & REDRAW_PENDING)) {
3611 Tcl_DoWhenIdle(DisplayText, (ClientData) textPtr);
3612 }
3613 dInfoPtr->flags |= REDRAW_PENDING|DINFO_OUT_OF_DATE|REPICK_NEEDED;
3614 }
3615
3616 /*
3617 *--------------------------------------------------------------
3618 *
3619 * TkTextYviewCmd --
3620 *
3621 * This procedure is invoked to process the "yview" option for
3622 * the widget command for text widgets. See the user documentation
3623 * for details on what it does.
3624 *
3625 * Results:
3626 * A standard Tcl result.
3627 *
3628 * Side effects:
3629 * See the user documentation.
3630 *
3631 *--------------------------------------------------------------
3632 */
3633
3634 int
3635 TkTextYviewCmd(textPtr, interp, argc, argv)
3636 TkText *textPtr; /* Information about text widget. */
3637 Tcl_Interp *interp; /* Current interpreter. */
3638 int argc; /* Number of arguments. */
3639 char **argv; /* Argument strings. Someone else has already
3640 * parsed this command enough to know that
3641 * argv[1] is "yview". */
3642 {
3643 TextDInfo *dInfoPtr = textPtr->dInfoPtr;
3644 int pickPlace, lineNum, type, bytesInLine;
3645 Tk_FontMetrics fm;
3646 int pixels, count;
3647 size_t switchLength;
3648 double fraction;
3649 TkTextIndex index, new;
3650 TkTextLine *lastLinePtr;
3651 DLine *dlPtr;
3652
3653 if (dInfoPtr->flags & DINFO_OUT_OF_DATE) {
3654 UpdateDisplayInfo(textPtr);
3655 }
3656
3657 if (argc == 2) {
3658 GetYView(interp, textPtr, 0);
3659 return TCL_OK;
3660 }
3661
3662 /*
3663 * Next, handle the old syntax: "pathName yview ?-pickplace? where"
3664 */
3665
3666 pickPlace = 0;
3667 if (argv[2][0] == '-') {
3668 switchLength = strlen(argv[2]);
3669 if ((switchLength >= 2)
3670 && (LangCmpOpt("-pickplace", argv[2], switchLength) == 0)) {
3671 pickPlace = 1;
3672 if (argc != 4) {
3673 Tcl_AppendResult(interp, "wrong # args: should be \"",
3674 argv[0], " yview -pickplace lineNum|index\"",
3675 (char *) NULL);
3676 return TCL_ERROR;
3677 }
3678 }
3679 }
3680 if ((argc == 3) || pickPlace) {
3681 /* FIXME: Disable integer form altogether ??? */
3682 if (!strchr(Tcl_GetString(objv[2+pickPlace]),'.') &&
3683 Tcl_GetIntFromObj(interp, argv[2+pickPlace], &lineNum) == TCL_OK) {
3684 TkTextMakeByteIndex(textPtr->tree, lineNum, 0, &index);
3685 TkTextSetYView(textPtr, &index, 0);
3686 return TCL_OK;
3687 }
3688
3689 /*
3690 * The argument must be a regular text index.
3691 */
3692
3693 Tcl_ResetResult(interp);
3694 if (TkTextGetIndex(interp, textPtr, argv[2+pickPlace],
3695 &index) != TCL_OK) {
3696 return TCL_ERROR;
3697 }
3698 TkTextSetYView(textPtr, &index, pickPlace);
3699 return TCL_OK;
3700 }
3701
3702 /*
3703 * New syntax: dispatch based on argv[2].
3704 */
3705
3706 type = Tk_GetScrollInfo(interp, argc, argv, &fraction, &count);
3707 switch (type) {
3708 case TK_SCROLL_ERROR:
3709 return TCL_ERROR;
3710 case TK_SCROLL_MOVETO:
3711 if (fraction > 1.0) {
3712 fraction = 1.0;
3713 }
3714 if (fraction < 0) {
3715 fraction = 0;
3716 }
3717 fraction *= TkBTreeNumLines(textPtr->tree);
3718 lineNum = (int) fraction;
3719 TkTextMakeByteIndex(textPtr->tree, lineNum, 0, &index);
3720 bytesInLine = TkBTreeBytesInLine(index.linePtr);
3721 index.byteIndex = (int)((bytesInLine * (fraction-lineNum)) + 0.5);
3722 if (index.byteIndex >= bytesInLine) {
3723 TkTextMakeByteIndex(textPtr->tree, lineNum + 1, 0, &index);
3724 }
3725 TkTextSetYView(textPtr, &index, 0);
3726 break;
3727 case TK_SCROLL_PAGES:
3728 /*
3729 * Scroll up or down by screenfuls. Actually, use the
3730 * window height minus two lines, so that there's some
3731 * overlap between adjacent pages.
3732 */
3733
3734 Tk_GetFontMetrics(textPtr->tkfont, &fm);
3735 if (count < 0) {
3736 pixels = (dInfoPtr->maxY - 2*fm.linespace - dInfoPtr->y)*(-count)
3737 + fm.linespace;
3738 MeasureUp(textPtr, &textPtr->topIndex, pixels, &new);
3739 if (TkTextIndexCmp(&textPtr->topIndex, &new) == 0) {
3740 /*
3741 * A page of scrolling ended up being less than one line.
3742 * Scroll one line anyway.
3743 */
3744
3745 count = -1;
3746 goto scrollByLines;
3747 }
3748 textPtr->topIndex = new;
3749 } else {
3750 /*
3751 * Scrolling down by pages. Layout lines starting at the
3752 * top index and count through the desired vertical distance.
3753 */
3754
3755 pixels = (dInfoPtr->maxY - 2*fm.linespace - dInfoPtr->y)*count;
3756 lastLinePtr = TkBTreeFindLine(textPtr->tree,
3757 TkBTreeNumLines(textPtr->tree));
3758 do {
3759 dlPtr = LayoutDLine(textPtr, &textPtr->topIndex);
3760 dlPtr->nextPtr = NULL;
3761 TkTextIndexForwBytes(&textPtr->topIndex, dlPtr->byteCount,
3762 &new);
3763 pixels -= dlPtr->height;
3764 FreeDLines(textPtr, dlPtr, (DLine *) NULL, 0);
3765 if (new.linePtr == lastLinePtr) {
3766 break;
3767 }
3768 textPtr->topIndex = new;
3769 } while (pixels > 0);
3770 }
3771 if (!(dInfoPtr->flags & REDRAW_PENDING)) {
3772 Tcl_DoWhenIdle(DisplayText, (ClientData) textPtr);
3773 }
3774 dInfoPtr->flags |= REDRAW_PENDING|DINFO_OUT_OF_DATE|REPICK_NEEDED;
3775 break;
3776 case TK_SCROLL_UNITS:
3777 scrollByLines:
3778 ScrollByLines(textPtr, count);
3779 break;
3780 }
3781 return TCL_OK;
3782 }
3783
3784 /*
3785 *--------------------------------------------------------------
3786 *
3787 * TkTextScanCmd --
3788 *
3789 * This procedure is invoked to process the "scan" option for
3790 * the widget command for text widgets. See the user documentation
3791 * for details on what it does.
3792 *
3793 * Results:
3794 * A standard Tcl result.
3795 *
3796 * Side effects:
3797 * See the user documentation.
3798 *
3799 *--------------------------------------------------------------
3800 */
3801
3802 int
3803 TkTextScanCmd(textPtr, interp, argc, argv)
3804 register TkText *textPtr; /* Information about text widget. */
3805 Tcl_Interp *interp; /* Current interpreter. */
3806 int argc; /* Number of arguments. */
3807 char **argv; /* Argument strings. Someone else has already
3808 * parsed this command enough to know that
3809 * argv[1] is "scan". */
3810 {
3811 TextDInfo *dInfoPtr = textPtr->dInfoPtr;
3812 TkTextIndex index;
3813 int c, x, y, totalScroll, newByte, maxByte, gain=10;
3814 Tk_FontMetrics fm;
3815 size_t length;
3816
3817 if ((argc != 5) && (argc != 6)) {
3818 Tcl_AppendResult(interp, "wrong # args: should be \"",
3819 argv[0], " scan mark x y\" or \"",
3820 argv[0], " scan dragto x y ?gain?\"", (char *) NULL);
3821 return TCL_ERROR;
3822 }
3823 if (Tcl_GetIntFromObj(interp, argv[3], &x) != TCL_OK) {
3824 return TCL_ERROR;
3825 }
3826 if (Tcl_GetIntFromObj(interp, argv[4], &y) != TCL_OK) {
3827 return TCL_ERROR;
3828 }
3829 if ((argc == 6) && (Tcl_GetIntFromObj(interp, argv[5], &gain) != TCL_OK))
3830 return TCL_ERROR;
3831 c = argv[2][0];
3832 length = strlen(argv[2]);
3833 if ((c == 'd') && (strncmp(argv[2], "dragto", length) == 0)) {
3834 /*
3835 * Amplify the difference between the current position and the
3836 * mark position to compute how much the view should shift, then
3837 * update the mark position to correspond to the new view. If we
3838 * run off the edge of the text, reset the mark point so that the
3839 * current position continues to correspond to the edge of the
3840 * window. This means that the picture will start dragging as
3841 * soon as the mouse reverses direction (without this reset, might
3842 * have to slide mouse a long ways back before the picture starts
3843 * moving again).
3844 */
3845
3846 newByte = dInfoPtr->scanMarkIndex + (gain*(dInfoPtr->scanMarkX - x))
3847 / (textPtr->charWidth);
3848 maxByte = 1 + (dInfoPtr->maxLength - (dInfoPtr->maxX - dInfoPtr->x)
3849 + textPtr->charWidth - 1)/textPtr->charWidth;
3850 if (newByte < 0) {
3851 newByte = 0;
3852 dInfoPtr->scanMarkIndex = 0;
3853 dInfoPtr->scanMarkX = x;
3854 } else if (newByte > maxByte) {
3855 newByte = maxByte;
3856 dInfoPtr->scanMarkIndex = maxByte;
3857 dInfoPtr->scanMarkX = x;
3858 }
3859 dInfoPtr->newByteOffset = newByte;
3860
3861 Tk_GetFontMetrics(textPtr->tkfont, &fm);
3862 totalScroll = (gain*(dInfoPtr->scanMarkY - y)) / fm.linespace;
3863 if (totalScroll != dInfoPtr->scanTotalScroll) {
3864 index = textPtr->topIndex;
3865 ScrollByLines(textPtr, totalScroll-dInfoPtr->scanTotalScroll);
3866 dInfoPtr->scanTotalScroll = totalScroll;
3867 if ((index.linePtr == textPtr->topIndex.linePtr) &&
3868 (index.byteIndex == textPtr->topIndex.byteIndex)) {
3869 dInfoPtr->scanTotalScroll = 0;
3870 dInfoPtr->scanMarkY = y;
3871 }
3872 }
3873 } else if ((c == 'm') && (strncmp(argv[2], "mark", length) == 0)) {
3874 dInfoPtr->scanMarkIndex = dInfoPtr->newByteOffset;
3875 dInfoPtr->scanMarkX = x;
3876 dInfoPtr->scanTotalScroll = 0;
3877 dInfoPtr->scanMarkY = y;
3878 } else {
3879 Tcl_AppendResult(interp, "bad scan option \"", argv[2],
3880 "\": must be mark or dragto", (char *) NULL);
3881 return TCL_ERROR;
3882 }
3883 dInfoPtr->flags |= DINFO_OUT_OF_DATE;
3884 if (!(dInfoPtr->flags & REDRAW_PENDING)) {
3885 dInfoPtr->flags |= REDRAW_PENDING;
3886 Tcl_DoWhenIdle(DisplayText, (ClientData) textPtr);
3887 }
3888 return TCL_OK;
3889 }
3890
3891 /*
3892 *----------------------------------------------------------------------
3893 *
3894 * GetXView --
3895 *
3896 * This procedure computes the fractions that indicate what's
3897 * visible in a text window and, optionally, evaluates a
3898 * Tcl script to report them to the text's associated scrollbar.
3899 *
3900 * Results:
3901 * If report is zero, then the interp's result is filled in with
3902 * two real numbers separated by a space, giving the position of
3903 * the left and right edges of the window as fractions from 0 to
3904 * 1, where 0 means the left edge of the text and 1 means the right
3905 * edge. If report is non-zero, then the interp's result isn't modified
3906 * directly, but instead a script is evaluated in interp to report
3907 * the new horizontal scroll position to the scrollbar (if the scroll
3908 * position hasn't changed then no script is invoked).
3909 *
3910 * Side effects:
3911 * None.
3912 *
3913 *----------------------------------------------------------------------
3914 */
3915
3916 static void
3917 GetXView(interp, textPtr, report)
3918 Tcl_Interp *interp; /* If "report" is FALSE, string
3919 * describing visible range gets
3920 * stored in the interp's result. */
3921 TkText *textPtr; /* Information about text widget. */
3922 int report; /* Non-zero means report info to
3923 * scrollbar if it has changed. */
3924 {
3925 TextDInfo *dInfoPtr = textPtr->dInfoPtr;
3926 char buffer[TCL_DOUBLE_SPACE * 2 + 1];
3927 double first, last;
3928 int code;
3929
3930 if (dInfoPtr->maxLength > 0) {
3931 first = ((double) dInfoPtr->curPixelOffset)
3932 / dInfoPtr->maxLength;
3933 last = first + ((double) (dInfoPtr->maxX - dInfoPtr->x))
3934 / dInfoPtr->maxLength;
3935 if (last > 1.0) {
3936 last = 1.0;
3937 }
3938 } else {
3939 first = 0;
3940 last = 1.0;
3941 }
3942 if (!report) {
3943 Tcl_DoubleResults(interp,2,0,first, last);
3944 return;
3945 }
3946 if (FP_EQUAL_SCALE(first, dInfoPtr->xScrollFirst, dInfoPtr->maxLength) &&
3947 FP_EQUAL_SCALE(last, dInfoPtr->xScrollLast, dInfoPtr->maxLength)) {
3948 return;
3949 }
3950 dInfoPtr->xScrollFirst = first;
3951 dInfoPtr->xScrollLast = last;
3952 code = LangDoCallback(interp, textPtr->xScrollCmd, 0, 2, " %g %g", first, last);
3953 if (code != TCL_OK) {
3954 Tcl_AddErrorInfo(interp,
3955 "\n (horizontal scrolling command executed by text)");
3956 Tcl_BackgroundError(interp);
3957 }
3958 }
3959
3960 /*
3961 *----------------------------------------------------------------------
3962 *
3963 * GetYView --
3964 *
3965 * This procedure computes the fractions that indicate what's
3966 * visible in a text window and, optionally, evaluates a
3967 * Tcl script to report them to the text's associated scrollbar.
3968 *
3969 * Results:
3970 * If report is zero, then the interp's result is filled in with
3971 * two real numbers separated by a space, giving the position of
3972 * the top and bottom of the window as fractions from 0 to 1, where
3973 * 0 means the beginning of the text and 1 means the end. If
3974 * report is non-zero, then the interp's result isn't modified directly,
3975 * but a script is evaluated in interp to report the new scroll
3976 * position to the scrollbar (if the scroll position hasn't changed
3977 * then no script is invoked).
3978 *
3979 * Side effects:
3980 * None.
3981 *
3982 *----------------------------------------------------------------------
3983 */
3984
3985 static void
3986 GetYView(interp, textPtr, report)
3987 Tcl_Interp *interp; /* If "report" is FALSE, string
3988 * describing visible range gets
3989 * stored in the interp's result. */
3990 TkText *textPtr; /* Information about text widget. */
3991 int report; /* Non-zero means report info to
3992 * scrollbar if it has changed. */
3993 {
3994 TextDInfo *dInfoPtr = textPtr->dInfoPtr;
3995 char buffer[TCL_DOUBLE_SPACE * 2 + 1];
3996 double first, last;
3997 DLine *dlPtr;
3998 int totalLines, code, count;
3999
4000 dlPtr = dInfoPtr->dLinePtr;
4001 totalLines = TkBTreeNumLines(textPtr->tree);
4002 first = (double) TkBTreeLineIndex(dlPtr->index.linePtr)
4003 + (double) dlPtr->index.byteIndex
4004 / TkBTreeBytesInLine(dlPtr->index.linePtr);
4005 first /= totalLines;
4006 while (1) {
4007 if ((dlPtr->y + dlPtr->height) > dInfoPtr->maxY) {
4008 /*
4009 * The last line is only partially visible, so don't
4010 * count its characters in what's visible.
4011 */
4012 count = 0;
4013 break;
4014 }
4015 if (dlPtr->nextPtr == NULL) {
4016 count = dlPtr->byteCount;
4017 break;
4018 }
4019 dlPtr = dlPtr->nextPtr;
4020 }
4021 last = ((double) TkBTreeLineIndex(dlPtr->index.linePtr))
4022 + ((double) (dlPtr->index.byteIndex + count))
4023 / (TkBTreeBytesInLine(dlPtr->index.linePtr));
4024 last /= totalLines;
4025 if (!report) {
4026 Tcl_DoubleResults(interp,2,0,first, last);
4027 return;
4028 }
4029 if (FP_EQUAL_SCALE(first, dInfoPtr->yScrollFirst, totalLines) &&
4030 FP_EQUAL_SCALE(last, dInfoPtr->yScrollLast, totalLines)) {
4031 return;
4032 }
4033 dInfoPtr->yScrollFirst = first;
4034 dInfoPtr->yScrollLast = last;
4035
4036 code = LangDoCallback(interp, textPtr->yScrollCmd, 0, 2, " %g %g",first, last);
4037
4038 if (code != TCL_OK) {
4039 Tcl_AddErrorInfo(interp,
4040 "\n (vertical scrolling command executed by text)");
4041 Tcl_BackgroundError(interp);
4042 }
4043 }
4044
4045 /*
4046 *----------------------------------------------------------------------
4047 *
4048 * FindDLine --
4049 *
4050 * This procedure is called to find the DLine corresponding to a
4051 * given text index.
4052 *
4053 * Results:
4054 * The return value is a pointer to the first DLine found in the
4055 * list headed by dlPtr that displays information at or after the
4056 * specified position. If there is no such line in the list then
4057 * NULL is returned.
4058 *
4059 * Side effects:
4060 * None.
4061 *
4062 *----------------------------------------------------------------------
4063 */
4064
4065 static DLine *
4066 FindDLine(dlPtr, indexPtr)
4067 register DLine *dlPtr; /* Pointer to first in list of DLines
4068 * to search. */
4069 TkTextIndex *indexPtr; /* Index of desired character. */
4070 {
4071 TkTextLine *linePtr;
4072
4073 if (dlPtr == NULL) {
4074 return NULL;
4075 }
4076 if (TkBTreeLineIndex(indexPtr->linePtr)
4077 < TkBTreeLineIndex(dlPtr->index.linePtr)) {
4078 /*
4079 * The first display line is already past the desired line.
4080 */
4081 return dlPtr;
4082 }
4083
4084 /*
4085 * Find the first display line that covers the desired text line.
4086 */
4087
4088 linePtr = dlPtr->index.linePtr;
4089 while (linePtr != indexPtr->linePtr) {
4090 while (dlPtr->index.linePtr == linePtr) {
4091 dlPtr = dlPtr->nextPtr;
4092 if (dlPtr == NULL) {
4093 return NULL;
4094 }
4095 }
4096 linePtr = TkBTreeNextLine(linePtr);
4097 if (linePtr == NULL) {
4098 panic("FindDLine reached end of text");
4099 }
4100 }
4101 if (indexPtr->linePtr != dlPtr->index.linePtr) {
4102 return dlPtr;
4103 }
4104
4105 /*
4106 * Now get to the right position within the text line.
4107 */
4108
4109 while (indexPtr->byteIndex >= (dlPtr->index.byteIndex + dlPtr->byteCount)) {
4110 dlPtr = dlPtr->nextPtr;
4111 if ((dlPtr == NULL) || (dlPtr->index.linePtr != indexPtr->linePtr)) {
4112 break;
4113 }
4114 }
4115 return dlPtr;
4116 }
4117
4118 /*
4119 *----------------------------------------------------------------------
4120 *
4121 * TkTextPixelIndex --
4122 *
4123 * Given an (x,y) coordinate on the screen, find the location of
4124 * the character closest to that location.
4125 *
4126 * Results:
4127 * The index at *indexPtr is modified to refer to the character
4128 * on the display that is closest to (x,y).
4129 *
4130 * Side effects:
4131 * None.
4132 *
4133 *----------------------------------------------------------------------
4134 */
4135
4136 void
4137 TkTextPixelIndex(textPtr, x, y, indexPtr)
4138 TkText *textPtr; /* Widget record for text widget. */
4139 int x, y; /* Pixel coordinates of point in widget's
4140 * window. */
4141 TkTextIndex *indexPtr; /* This index gets filled in with the
4142 * index of the character nearest to (x,y). */
4143 {
4144 TextDInfo *dInfoPtr = textPtr->dInfoPtr;
4145 register DLine *dlPtr, *validdlPtr;
4146 register TkTextDispChunk *chunkPtr;
4147
4148 /*
4149 * Make sure that all of the layout information about what's
4150 * displayed where on the screen is up-to-date.
4151 */
4152
4153 if (dInfoPtr->flags & DINFO_OUT_OF_DATE) {
4154 UpdateDisplayInfo(textPtr);
4155 }
4156
4157 /*
4158 * If the coordinates are above the top of the window, then adjust
4159 * them to refer to the upper-right corner of the window. If they're
4160 * off to one side or the other, then adjust to the closest side.
4161 */
4162
4163 if (y < dInfoPtr->y) {
4164 y = dInfoPtr->y;
4165 x = dInfoPtr->x;
4166 }
4167 if (x >= dInfoPtr->maxX) {
4168 x = dInfoPtr->maxX - 1;
4169 }
4170 if (x < dInfoPtr->x) {
4171 x = dInfoPtr->x;
4172 }
4173
4174 /*
4175 * Find the display line containing the desired y-coordinate.
4176 */
4177
4178 for (dlPtr = validdlPtr = dInfoPtr->dLinePtr; y >= (dlPtr->y + dlPtr->height);
4179 dlPtr = dlPtr->nextPtr) {
4180 if (dlPtr->chunkPtr !=NULL) validdlPtr = dlPtr;
4181 if (dlPtr->nextPtr == NULL) {
4182 /*
4183 * Y-coordinate is off the bottom of the displayed text.
4184 * Use the last character on the last line.
4185 */
4186
4187 x = dInfoPtr->maxX - 1;
4188 break;
4189 }
4190 }
4191 if (dlPtr->chunkPtr == NULL) dlPtr = validdlPtr;
4192
4193
4194 /*
4195 * Scan through the line's chunks to find the one that contains
4196 * the desired x-coordinate. Before doing this, translate the
4197 * x-coordinate from the coordinate system of the window to the
4198 * coordinate system of the line (to take account of x-scrolling).
4199 */
4200
4201 *indexPtr = dlPtr->index;
4202 x = x - dInfoPtr->x + dInfoPtr->curPixelOffset;
4203 for (chunkPtr = dlPtr->chunkPtr; x >= (chunkPtr->x + chunkPtr->width);
4204 indexPtr->byteIndex += chunkPtr->numBytes,
4205 chunkPtr = chunkPtr->nextPtr) {
4206 if (chunkPtr->nextPtr == NULL) {
4207 indexPtr->byteIndex += chunkPtr->numBytes;
4208 TkTextIndexBackChars(indexPtr, 1, indexPtr);
4209 return;
4210 }
4211 }
4212
4213 /*
4214 * If the chunk has more than one byte in it, ask it which
4215 * character is at the desired location.
4216 */
4217
4218 if (chunkPtr->numBytes > 1) {
4219 indexPtr->byteIndex += (*chunkPtr->measureProc)(chunkPtr, x);
4220 }
4221 }
4222
4223 /*
4224 *----------------------------------------------------------------------
4225 *
4226 * TkTextCharBbox --
4227 *
4228 * Given an index, find the bounding box of the screen area
4229 * occupied by that character.
4230 *
4231 * Results:
4232 * Zero is returned if the character is on the screen. -1
4233 * means the character isn't on the screen. If the return value
4234 * is 0, then the bounding box of the part of the character that's
4235 * visible on the screen is returned to *xPtr, *yPtr, *widthPtr,
4236 * and *heightPtr.
4237 *
4238 * Side effects:
4239 * None.
4240 *
4241 *----------------------------------------------------------------------
4242 */
4243
4244 int
4245 TkTextCharBbox(textPtr, indexPtr, xPtr, yPtr, widthPtr, heightPtr)
4246 TkText *textPtr; /* Widget record for text widget. */
4247 TkTextIndex *indexPtr; /* Index of character whose bounding
4248 * box is desired. */
4249 int *xPtr, *yPtr; /* Filled with character's upper-left
4250 * coordinate. */
4251 int *widthPtr, *heightPtr; /* Filled in with character's dimensions. */
4252 {
4253 TextDInfo *dInfoPtr = textPtr->dInfoPtr;
4254 DLine *dlPtr;
4255 register TkTextDispChunk *chunkPtr;
4256 int byteIndex;
4257
4258 /*
4259 * Make sure that all of the screen layout information is up to date.
4260 */
4261
4262 if (dInfoPtr->flags & DINFO_OUT_OF_DATE) {
4263 UpdateDisplayInfo(textPtr);
4264 }
4265
4266 /*
4267 * Find the display line containing the desired index.
4268 */
4269
4270 dlPtr = FindDLine(dInfoPtr->dLinePtr, indexPtr);
4271 if ((dlPtr == NULL) || (TkTextIndexCmp(&dlPtr->index, indexPtr) > 0)) {
4272 return -1;
4273 }
4274
4275 /*
4276 * Find the chunk within the line that contains the desired
4277 * index.
4278 */
4279
4280 byteIndex = indexPtr->byteIndex - dlPtr->index.byteIndex;
4281 for (chunkPtr = dlPtr->chunkPtr; ; chunkPtr = chunkPtr->nextPtr) {
4282 if (chunkPtr == NULL) {
4283 return -1;
4284 }
4285 if (byteIndex < chunkPtr->numBytes) {
4286 break;
4287 }
4288 byteIndex -= chunkPtr->numBytes;
4289 }
4290
4291 /*
4292 * Call a chunk-specific procedure to find the horizontal range of
4293 * the character within the chunk, then fill in the vertical range.
4294 * The x-coordinate returned by bboxProc is a coordinate within a
4295 * line, not a coordinate on the screen. Translate it to reflect
4296 * horizontal scrolling.
4297 */
4298
4299 (*chunkPtr->bboxProc)(chunkPtr, byteIndex, dlPtr->y + dlPtr->spaceAbove,
4300 dlPtr->height - dlPtr->spaceAbove - dlPtr->spaceBelow,
4301 dlPtr->baseline - dlPtr->spaceAbove, xPtr, yPtr, widthPtr,
4302 heightPtr);
4303 *xPtr = *xPtr + dInfoPtr->x - dInfoPtr->curPixelOffset;
4304 if ((byteIndex == (chunkPtr->numBytes - 1)) && (chunkPtr->nextPtr == NULL)) {
4305 /*
4306 * Last character in display line. Give it all the space up to
4307 * the line.
4308 */
4309
4310 if (*xPtr > dInfoPtr->maxX) {
4311 *xPtr = dInfoPtr->maxX;
4312 }
4313 *widthPtr = dInfoPtr->maxX - *xPtr;
4314 }
4315 if ((*xPtr + *widthPtr) <= dInfoPtr->x) {
4316 return -1;
4317 }
4318 if ((*xPtr + *widthPtr) > dInfoPtr->maxX) {
4319 *widthPtr = dInfoPtr->maxX - *xPtr;
4320 if (*widthPtr <= 0) {
4321 return -1;
4322 }
4323 }
4324 if ((*yPtr + *heightPtr) > dInfoPtr->maxY) {
4325 *heightPtr = dInfoPtr->maxY - *yPtr;
4326 if (*heightPtr <= 0) {
4327 return -1;
4328 }
4329 }
4330 return 0;
4331 }
4332
4333 /*
4334 *----------------------------------------------------------------------
4335 *
4336 * TkTextDLineInfo --
4337 *
4338 * Given an index, return information about the display line
4339 * containing that character.
4340 *
4341 * Results:
4342 * Zero is returned if the character is on the screen. -1
4343 * means the character isn't on the screen. If the return value
4344 * is 0, then information is returned in the variables pointed
4345 * to by xPtr, yPtr, widthPtr, heightPtr, and basePtr.
4346 *
4347 * Side effects:
4348 * None.
4349 *
4350 *----------------------------------------------------------------------
4351 */
4352
4353 int
4354 TkTextDLineInfo(textPtr, indexPtr, xPtr, yPtr, widthPtr, heightPtr, basePtr)
4355 TkText *textPtr; /* Widget record for text widget. */
4356 TkTextIndex *indexPtr; /* Index of character whose bounding
4357 * box is desired. */
4358 int *xPtr, *yPtr; /* Filled with line's upper-left
4359 * coordinate. */
4360 int *widthPtr, *heightPtr; /* Filled in with line's dimensions. */
4361 int *basePtr; /* Filled in with the baseline position,
4362 * measured as an offset down from *yPtr. */
4363 {
4364 TextDInfo *dInfoPtr = textPtr->dInfoPtr;
4365 DLine *dlPtr;
4366 int dlx;
4367
4368 /*
4369 * Make sure that all of the screen layout information is up to date.
4370 */
4371
4372 if (dInfoPtr->flags & DINFO_OUT_OF_DATE) {
4373 UpdateDisplayInfo(textPtr);
4374 }
4375
4376 /*
4377 * Find the display line containing the desired index.
4378 */
4379
4380 dlPtr = FindDLine(dInfoPtr->dLinePtr, indexPtr);
4381 if ((dlPtr == NULL) || (TkTextIndexCmp(&dlPtr->index, indexPtr) > 0)) {
4382 return -1;
4383 }
4384
4385 dlx = (dlPtr->chunkPtr != NULL? dlPtr->chunkPtr->x: 0);
4386 *xPtr = dInfoPtr->x - dInfoPtr->curPixelOffset + dlx;
4387 *widthPtr = dlPtr->length - dlx;
4388 *yPtr = dlPtr->y;
4389 if ((dlPtr->y + dlPtr->height) > dInfoPtr->maxY) {
4390 *heightPtr = dInfoPtr->maxY - dlPtr->y;
4391 } else {
4392 *heightPtr = dlPtr->height;
4393 }
4394 *basePtr = dlPtr->baseline;
4395 return 0;
4396 }
4397
4398 static void
4399 ElideBboxProc(chunkPtr, index, y, lineHeight, baseline, xPtr, yPtr,
4400 widthPtr, heightPtr)
4401 TkTextDispChunk *chunkPtr; /* Chunk containing desired char. */
4402 int index; /* Index of desired character within
4403 * the chunk. */
4404 int y; /* Topmost pixel in area allocated
4405 * for this line. */
4406 int lineHeight; /* Height of line, in pixels. */
4407 int baseline; /* Location of line's baseline, in
4408 * pixels measured down from y. */
4409 int *xPtr, *yPtr; /* Gets filled in with coords of
4410 * character's upper-left pixel.
4411 * X-coord is in same coordinate
4412 * system as chunkPtr->x. */
4413 int *widthPtr; /* Gets filled in with width of
4414 * character, in pixels. */
4415 int *heightPtr; /* Gets filled in with height of
4416 * character, in pixels. */
4417 {
4418 *xPtr = chunkPtr->x;
4419 *yPtr = y;
4420 *widthPtr = *heightPtr = 0;
4421 }
4422
4423
4424 static int
4425 ElideMeasureProc(chunkPtr, x)
4426 TkTextDispChunk *chunkPtr; /* Chunk containing desired coord. */
4427 int x; /* X-coordinate, in same coordinate
4428 * system as chunkPtr->x. */
4429 {
4430 return 0 /*chunkPtr->numBytes - 1*/;
4431 }
4432
4433 /*
4434 *--------------------------------------------------------------
4435 *
4436 * TkTextCharLayoutProc --
4437 *
4438 * This procedure is the "layoutProc" for character segments.
4439 *
4440 * Results:
4441 * If there is something to display for the chunk then a
4442 * non-zero value is returned and the fields of chunkPtr
4443 * will be filled in (see the declaration of TkTextDispChunk
4444 * in tkText.h for details). If zero is returned it means
4445 * that no characters from this chunk fit in the window.
4446 * If -1 is returned it means that this segment just doesn't
4447 * need to be displayed (never happens for text).
4448 *
4449 * Side effects:
4450 * Memory is allocated to hold additional information about
4451 * the chunk.
4452 *
4453 *--------------------------------------------------------------
4454 */
4455
4456 int
4457 TkTextCharLayoutProc(textPtr, indexPtr, segPtr, byteOffset, maxX, maxBytes,
4458 noCharsYet, wrapMode, chunkPtr)
4459 TkText *textPtr; /* Text widget being layed out. */
4460 TkTextIndex *indexPtr; /* Index of first character to lay out
4461 * (corresponds to segPtr and offset). */
4462 TkTextSegment *segPtr; /* Segment being layed out. */
4463 int byteOffset; /* Byte offset within segment of first
4464 * character to consider. */
4465 int maxX; /* Chunk must not occupy pixels at this
4466 * position or higher. */
4467 int maxBytes; /* Chunk must not include more than this
4468 * many characters. */
4469 int noCharsYet; /* Non-zero means no characters have been
4470 * assigned to this display line yet. */
4471 TkWrapMode wrapMode; /* How to handle line wrapping: TEXT_WRAPMODE_CHAR,
4472 * TEXT_WRAPMODE_NONE, or TEXT_WRAPMODE_WORD. */
4473 register TkTextDispChunk *chunkPtr;
4474 /* Structure to fill in with information
4475 * about this chunk. The x field has already
4476 * been set by the caller. */
4477 {
4478 Tk_Font tkfont;
4479 int nextX, bytesThatFit, count;
4480 CharInfo *ciPtr;
4481 char *p;
4482 TkTextSegment *nextPtr;
4483 Tk_FontMetrics fm;
4484
4485 /*
4486 * Figure out how many characters will fit in the space we've got.
4487 * Include the next character, even though it won't fit completely,
4488 * if any of the following is true:
4489 * (a) the chunk contains no characters and the display line contains
4490 * no characters yet (i.e. the line isn't wide enough to hold
4491 * even a single character).
4492 * (b) at least one pixel of the character is visible, we haven't
4493 * already exceeded the character limit, and the next character
4494 * is a white space character.
4495 */
4496
4497 p = segPtr->body.chars + byteOffset;
4498 tkfont = chunkPtr->stylePtr->sValuePtr->tkfont;
4499 bytesThatFit = MeasureChars(tkfont, p, maxBytes, chunkPtr->x, maxX, 0,
4500 &nextX);
4501 if (bytesThatFit < maxBytes) {
4502 if ((bytesThatFit == 0) && noCharsYet) {
4503 Tcl_UniChar ch;
4504
4505 bytesThatFit = MeasureChars(tkfont, p, Tcl_UtfToUniChar(p, &ch),
4506 chunkPtr->x, -1, 0, &nextX);
4507 }
4508 if ((nextX < maxX) && ((p[bytesThatFit] == ' ')
4509 || (p[bytesThatFit] == '\t'))) {
4510 /*
4511 * Space characters are funny, in that they are considered
4512 * to fit if there is at least one pixel of space left on the
4513 * line. Just give the space character whatever space is left.
4514 */
4515
4516 nextX = maxX;
4517 bytesThatFit++;
4518 }
4519 if (p[bytesThatFit] == '\n') {
4520 /*
4521 * A newline character takes up no space, so if the previous
4522 * character fits then so does the newline.
4523 */
4524
4525 bytesThatFit++;
4526 }
4527 if (bytesThatFit == 0) {
4528 return 0;
4529 }
4530 }
4531
4532 Tk_GetFontMetrics(tkfont, &fm);
4533
4534 /*
4535 * Fill in the chunk structure and allocate and initialize a
4536 * CharInfo structure. If the last character is a newline
4537 * then don't bother to display it.
4538 */
4539
4540 chunkPtr->displayProc = CharDisplayProc;
4541 chunkPtr->undisplayProc = CharUndisplayProc;
4542 chunkPtr->measureProc = CharMeasureProc;
4543 chunkPtr->bboxProc = CharBboxProc;
4544 chunkPtr->numBytes = bytesThatFit;
4545 chunkPtr->minAscent = fm.ascent + chunkPtr->stylePtr->sValuePtr->offset;
4546 chunkPtr->minDescent = fm.descent - chunkPtr->stylePtr->sValuePtr->offset;
4547 chunkPtr->minHeight = 0;
4548 chunkPtr->width = nextX - chunkPtr->x;
4549 chunkPtr->breakIndex = -1;
4550 ciPtr = (CharInfo *) ckalloc((unsigned)
4551 (sizeof(CharInfo) - 3 + bytesThatFit));
4552 chunkPtr->clientData = (ClientData) ciPtr;
4553 ciPtr->numBytes = bytesThatFit;
4554 memcpy(ciPtr->chars, p, MIN(bytesThatFit, strlen(p) + 1));
4555 if (p[bytesThatFit - 1] == '\n') {
4556 ciPtr->numBytes--;
4557 }
4558
4559 /*
4560 * Compute a break location. If we're in word wrap mode, a
4561 * break can occur after any space character, or at the end of
4562 * the chunk if the next segment (ignoring those with zero size)
4563 * is not a character segment.
4564 */
4565
4566 if (wrapMode != TEXT_WRAPMODE_WORD) {
4567 chunkPtr->breakIndex = chunkPtr->numBytes;
4568 } else {
4569 for (count = bytesThatFit, p += bytesThatFit - 1; count > 0;
4570 count--, p--) {
4571 if (isspace(UCHAR(*p))) {
4572 chunkPtr->breakIndex = count;
4573 break;
4574 }
4575 }
4576 if ((bytesThatFit + byteOffset) == segPtr->size) {
4577 for (nextPtr = segPtr->nextPtr; nextPtr != NULL;
4578 nextPtr = nextPtr->nextPtr) {
4579 if (nextPtr->size != 0) {
4580 if (nextPtr->typePtr != &tkTextCharType) {
4581 chunkPtr->breakIndex = chunkPtr->numBytes;
4582 }
4583 break;
4584 }
4585 }
4586 }
4587 }
4588 return 1;
4589 }
4590
4591 /*
4592 *--------------------------------------------------------------
4593 *
4594 * CharDisplayProc --
4595 *
4596 * This procedure is called to display a character chunk on
4597 * the screen or in an off-screen pixmap.
4598 *
4599 * Results:
4600 * None.
4601 *
4602 * Side effects:
4603 * Graphics are drawn.
4604 *
4605 *--------------------------------------------------------------
4606 */
4607
4608 static void
4609 CharDisplayProc(chunkPtr, x, y, height, baseline, display, dst, screenY)
4610 TkTextDispChunk *chunkPtr; /* Chunk that is to be drawn. */
4611 int x; /* X-position in dst at which to
4612 * draw this chunk (may differ from
4613 * the x-position in the chunk because
4614 * of scrolling). */
4615 int y; /* Y-position at which to draw this
4616 * chunk in dst. */
4617 int height; /* Total height of line. */
4618 int baseline; /* Offset of baseline from y. */
4619 Display *display; /* Display to use for drawing. */
4620 Drawable dst; /* Pixmap or window in which to draw
4621 * chunk. */
4622 int screenY; /* Y-coordinate in text window that
4623 * corresponds to y. */
4624 {
4625 CharInfo *ciPtr = (CharInfo *) chunkPtr->clientData;
4626 TextStyle *stylePtr;
4627 StyleValues *sValuePtr;
4628 int offsetBytes, offsetX;
4629
4630 if ((x + chunkPtr->width) <= 0) {
4631 /*
4632 * The chunk is off-screen.
4633 */
4634
4635 return;
4636 }
4637
4638 stylePtr = chunkPtr->stylePtr;
4639 sValuePtr = stylePtr->sValuePtr;
4640
4641 /*
4642 * If the text sticks out way to the left of the window, skip
4643 * over the characters that aren't in the visible part of the
4644 * window. This is essential if x is very negative (such as
4645 * less than 32K); otherwise overflow problems will occur
4646 * in servers that use 16-bit arithmetic, like X.
4647 */
4648
4649 offsetX = x;
4650 offsetBytes = 0;
4651 if (x < 0) {
4652 offsetBytes = MeasureChars(sValuePtr->tkfont, ciPtr->chars,
4653 ciPtr->numBytes, x, 0, x - chunkPtr->x, &offsetX);
4654 }
4655
4656 /*
4657 * Draw the text, underline, and overstrike for this chunk.
4658 */
4659
4660 if (!sValuePtr->elide && (ciPtr->numBytes > offsetBytes) && (stylePtr->fgGC != None)) {
4661 int numBytes = ciPtr->numBytes - offsetBytes;
4662 char *string = ciPtr->chars + offsetBytes;
4663
4664 if ((numBytes > 0) && (string[numBytes - 1] == '\t')) {
4665 numBytes--;
4666 }
4667 Tk_DrawChars(display, dst, stylePtr->fgGC, sValuePtr->tkfont, string,
4668 numBytes, offsetX, y + baseline - sValuePtr->offset);
4669 if (sValuePtr->underline) {
4670 Tk_UnderlineChars(display, dst, stylePtr->fgGC, sValuePtr->tkfont,
4671 ciPtr->chars + offsetBytes, offsetX,
4672 y + baseline - sValuePtr->offset, 0, numBytes);
4673
4674 }
4675 if (sValuePtr->overstrike) {
4676 Tk_FontMetrics fm;
4677
4678 Tk_GetFontMetrics(sValuePtr->tkfont, &fm);
4679 Tk_UnderlineChars(display, dst, stylePtr->fgGC, sValuePtr->tkfont,
4680 ciPtr->chars + offsetBytes, offsetX,
4681 y + baseline - sValuePtr->offset
4682 - fm.descent - (fm.ascent * 3) / 10,
4683 0, numBytes);
4684 }
4685 }
4686 }
4687
4688 /*
4689 *--------------------------------------------------------------
4690 *
4691 * CharUndisplayProc --
4692 *
4693 * This procedure is called when a character chunk is no
4694 * longer going to be displayed. It frees up resources
4695 * that were allocated to display the chunk.
4696 *
4697 * Results:
4698 * None.
4699 *
4700 * Side effects:
4701 * Memory and other resources get freed.
4702 *
4703 *--------------------------------------------------------------
4704 */
4705
4706 static void
4707 CharUndisplayProc(textPtr, chunkPtr)
4708 TkText *textPtr; /* Overall information about text
4709 * widget. */
4710 TkTextDispChunk *chunkPtr; /* Chunk that is about to be freed. */
4711 {
4712 CharInfo *ciPtr = (CharInfo *) chunkPtr->clientData;
4713
4714 ckfree((char *) ciPtr);
4715 }
4716
4717 /*
4718 *--------------------------------------------------------------
4719 *
4720 * CharMeasureProc --
4721 *
4722 * This procedure is called to determine which character in
4723 * a character chunk lies over a given x-coordinate.
4724 *
4725 * Results:
4726 * The return value is the index *within the chunk* of the
4727 * character that covers the position given by "x".
4728 *
4729 * Side effects:
4730 * None.
4731 *
4732 *--------------------------------------------------------------
4733 */
4734
4735 static int
4736 CharMeasureProc(chunkPtr, x)
4737 TkTextDispChunk *chunkPtr; /* Chunk containing desired coord. */
4738 int x; /* X-coordinate, in same coordinate
4739 * system as chunkPtr->x. */
4740 {
4741 CharInfo *ciPtr = (CharInfo *) chunkPtr->clientData;
4742 int endX;
4743
4744 return MeasureChars(chunkPtr->stylePtr->sValuePtr->tkfont, ciPtr->chars,
4745 chunkPtr->numBytes, chunkPtr->x, x, 0, &endX);
4746 /* CHAR OFFSET */
4747 }
4748
4749 /*
4750 *--------------------------------------------------------------
4751 *
4752 * CharBboxProc --
4753 *
4754 * This procedure is called to compute the bounding box of
4755 * the area occupied by a single character.
4756 *
4757 * Results:
4758 * There is no return value. *xPtr and *yPtr are filled in
4759 * with the coordinates of the upper left corner of the
4760 * character, and *widthPtr and *heightPtr are filled in with
4761 * the dimensions of the character in pixels. Note: not all
4762 * of the returned bbox is necessarily visible on the screen
4763 * (the rightmost part might be off-screen to the right,
4764 * and the bottommost part might be off-screen to the bottom).
4765 *
4766 * Side effects:
4767 * None.
4768 *
4769 *--------------------------------------------------------------
4770 */
4771
4772 static void
4773 CharBboxProc(chunkPtr, byteIndex, y, lineHeight, baseline, xPtr, yPtr,
4774 widthPtr, heightPtr)
4775 TkTextDispChunk *chunkPtr; /* Chunk containing desired char. */
4776 int byteIndex; /* Byte offset of desired character
4777 * within the chunk. */
4778 int y; /* Topmost pixel in area allocated
4779 * for this line. */
4780 int lineHeight; /* Height of line, in pixels. */
4781 int baseline; /* Location of line's baseline, in
4782 * pixels measured down from y. */
4783 int *xPtr, *yPtr; /* Gets filled in with coords of
4784 * character's upper-left pixel.
4785 * X-coord is in same coordinate
4786 * system as chunkPtr->x. */
4787 int *widthPtr; /* Gets filled in with width of
4788 * character, in pixels. */
4789 int *heightPtr; /* Gets filled in with height of
4790 * character, in pixels. */
4791 {
4792 CharInfo *ciPtr = (CharInfo *) chunkPtr->clientData;
4793 int maxX;
4794
4795 maxX = chunkPtr->width + chunkPtr->x;
4796 MeasureChars(chunkPtr->stylePtr->sValuePtr->tkfont, ciPtr->chars,
4797 byteIndex, chunkPtr->x, -1, 0, xPtr);
4798
4799 if (byteIndex == ciPtr->numBytes) {
4800 /*
4801 * This situation only happens if the last character in a line
4802 * is a space character, in which case it absorbs all of the
4803 * extra space in the line (see TkTextCharLayoutProc).
4804 */
4805
4806 *widthPtr = maxX - *xPtr;
4807 } else if ((ciPtr->chars[byteIndex] == '\t')
4808 && (byteIndex == ciPtr->numBytes - 1)) {
4809 /*
4810 * The desired character is a tab character that terminates a
4811 * chunk; give it all the space left in the chunk.
4812 */
4813
4814 *widthPtr = maxX - *xPtr;
4815 } else {
4816 Tcl_UniChar ch;
4817 char *p = ciPtr->chars + byteIndex;
4818 MeasureChars(chunkPtr->stylePtr->sValuePtr->tkfont,
4819 p, Tcl_UtfToUniChar(p, &ch), *xPtr, -1, 0, widthPtr);
4820 if (*widthPtr > maxX) {
4821 *widthPtr = maxX - *xPtr;
4822 } else {
4823 *widthPtr -= *xPtr;
4824 }
4825 }
4826 *yPtr = y + baseline - chunkPtr->minAscent;
4827 *heightPtr = chunkPtr->minAscent + chunkPtr->minDescent;
4828 }
4829
4830 /*
4831 *----------------------------------------------------------------------
4832 *
4833 * AdjustForTab --
4834 *
4835 * This procedure is called to move a series of chunks right
4836 * in order to align them with a tab stop.
4837 *
4838 * Results:
4839 * None.
4840 *
4841 * Side effects:
4842 * The width of chunkPtr gets adjusted so that it absorbs the
4843 * extra space due to the tab. The x locations in all the chunks
4844 * after chunkPtr are adjusted rightward to align with the tab
4845 * stop given by tabArrayPtr and index.
4846 *
4847 *----------------------------------------------------------------------
4848 */
4849
4850 static void
4851 AdjustForTab(textPtr, tabArrayPtr, index, chunkPtr)
4852 TkText *textPtr; /* Information about the text widget as
4853 * a whole. */
4854 TkTextTabArray *tabArrayPtr; /* Information about the tab stops
4855 * that apply to this line. May be
4856 * NULL to indicate default tabbing
4857 * (every 8 chars). */
4858 int index; /* Index of current tab stop. */
4859 TkTextDispChunk *chunkPtr; /* Chunk whose last character is
4860 * the tab; the following chunks
4861 * contain information to be shifted
4862 * right. */
4863
4864 {
4865 int x, desired, delta, width, decimal, i, gotDigit;
4866 TkTextDispChunk *chunkPtr2, *decimalChunkPtr;
4867 CharInfo *ciPtr;
4868 int tabX, prev, spaceWidth;
4869 char *p;
4870 TkTextTabAlign alignment;
4871
4872 if (chunkPtr->nextPtr == NULL) {
4873 /*
4874 * Nothing after the actual tab; just return.
4875 */
4876
4877 return;
4878 }
4879
4880 /*
4881 * If no tab information has been given, do the usual thing:
4882 * round up to the next boundary of 8 average-sized characters.
4883 */
4884
4885 x = chunkPtr->nextPtr->x;
4886 if ((tabArrayPtr == NULL) || (tabArrayPtr->numTabs == 0)) {
4887 /*
4888 * No tab information has been given, so use the default
4889 * interpretation of tabs.
4890 */
4891
4892 desired = NextTabStop(textPtr->tkfont, x, 0);
4893 goto update;
4894 }
4895
4896 if (index < tabArrayPtr->numTabs) {
4897 alignment = tabArrayPtr->tabs[index].alignment;
4898 tabX = tabArrayPtr->tabs[index].location;
4899 } else {
4900 /*
4901 * Ran out of tab stops; compute a tab position by extrapolating
4902 * from the last two tab positions.
4903 */
4904
4905 if (tabArrayPtr->numTabs > 1) {
4906 prev = tabArrayPtr->tabs[tabArrayPtr->numTabs-2].location;
4907 } else {
4908 prev = 0;
4909 }
4910 alignment = tabArrayPtr->tabs[tabArrayPtr->numTabs-1].alignment;
4911 tabX = tabArrayPtr->tabs[tabArrayPtr->numTabs-1].location
4912 + (index + 1 - tabArrayPtr->numTabs)
4913 * (tabArrayPtr->tabs[tabArrayPtr->numTabs-1].location - prev);
4914 }
4915
4916 if (alignment == LEFT) {
4917 desired = tabX;
4918 goto update;
4919 }
4920
4921 if ((alignment == CENTER) || (alignment == RIGHT)) {
4922 /*
4923 * Compute the width of all the information in the tab group,
4924 * then use it to pick a desired location.
4925 */
4926
4927 width = 0;
4928 for (chunkPtr2 = chunkPtr->nextPtr; chunkPtr2 != NULL;
4929 chunkPtr2 = chunkPtr2->nextPtr) {
4930 width += chunkPtr2->width;
4931 }
4932 if (alignment == CENTER) {
4933 desired = tabX - width/2;
4934 } else {
4935 desired = tabX - width;
4936 }
4937 goto update;
4938 }
4939
4940 /*
4941 * Must be numeric alignment. Search through the text to be
4942 * tabbed, looking for the last , or . before the first character
4943 * that isn't a number, comma, period, or sign.
4944 */
4945
4946 decimalChunkPtr = NULL;
4947 decimal = gotDigit = 0;
4948 for (chunkPtr2 = chunkPtr->nextPtr; chunkPtr2 != NULL;
4949 chunkPtr2 = chunkPtr2->nextPtr) {
4950 if (chunkPtr2->displayProc != CharDisplayProc) {
4951 continue;
4952 }
4953 ciPtr = (CharInfo *) chunkPtr2->clientData;
4954 for (p = ciPtr->chars, i = 0; i < ciPtr->numBytes; p++, i++) {
4955 if (isdigit(UCHAR(*p))) {
4956 gotDigit = 1;
4957 } else if ((*p == '.') || (*p == ',')) {
4958 decimal = p-ciPtr->chars;
4959 decimalChunkPtr = chunkPtr2;
4960 } else if (gotDigit) {
4961 if (decimalChunkPtr == NULL) {
4962 decimal = p-ciPtr->chars;
4963 decimalChunkPtr = chunkPtr2;
4964 }
4965 goto endOfNumber;
4966 }
4967 }
4968 }
4969 endOfNumber:
4970 if (decimalChunkPtr != NULL) {
4971 int curX;
4972
4973 ciPtr = (CharInfo *) decimalChunkPtr->clientData;
4974 MeasureChars(decimalChunkPtr->stylePtr->sValuePtr->tkfont,
4975 ciPtr->chars, decimal, decimalChunkPtr->x, -1, 0, &curX);
4976 desired = tabX - (curX - x);
4977 goto update;
4978 } else {
4979 /*
4980 * There wasn't a decimal point. Right justify the text.
4981 */
4982
4983 width = 0;
4984 for (chunkPtr2 = chunkPtr->nextPtr; chunkPtr2 != NULL;
4985 chunkPtr2 = chunkPtr2->nextPtr) {
4986 width += chunkPtr2->width;
4987 }
4988 desired = tabX - width;
4989 }
4990
4991 /*
4992 * Shift all of the chunks to the right so that the left edge is
4993 * at the desired location, then expand the chunk containing the
4994 * tab. Be sure that the tab occupies at least the width of a
4995 * space character.
4996 */
4997
4998 update:
4999 delta = desired - x;
5000 MeasureChars(textPtr->tkfont, " ", 1, 0, -1, 0, &spaceWidth);
5001 if (delta < spaceWidth) {
5002 delta = spaceWidth;
5003 }
5004 for (chunkPtr2 = chunkPtr->nextPtr; chunkPtr2 != NULL;
5005 chunkPtr2 = chunkPtr2->nextPtr) {
5006 chunkPtr2->x += delta;
5007 }
5008 chunkPtr->width += delta;
5009 }
5010
5011 /*
5012 *----------------------------------------------------------------------
5013 *
5014 * SizeOfTab --
5015 *
5016 * This returns an estimate of the amount of white space that will
5017 * be consumed by a tab.
5018 *
5019 * Results:
5020 * The return value is the minimum number of pixels that will
5021 * be occupied by the index'th tab of tabArrayPtr, assuming that
5022 * the current position on the line is x and the end of the
5023 * line is maxX. For numeric tabs, this is a conservative
5024 * estimate. The return value is always >= 0.
5025 *
5026 * Side effects:
5027 * None.
5028 *
5029 *----------------------------------------------------------------------
5030 */
5031
5032 static int
5033 SizeOfTab(textPtr, tabArrayPtr, index, x, maxX)
5034 TkText *textPtr; /* Information about the text widget as
5035 * a whole. */
5036 TkTextTabArray *tabArrayPtr; /* Information about the tab stops
5037 * that apply to this line. NULL
5038 * means use default tabbing (every
5039 * 8 chars.) */
5040 int index; /* Index of current tab stop. */
5041 int x; /* Current x-location in line. Only
5042 * used if tabArrayPtr == NULL. */
5043 int maxX; /* X-location of pixel just past the
5044 * right edge of the line. */
5045 {
5046 int tabX, prev, result, spaceWidth;
5047 TkTextTabAlign alignment;
5048
5049 if ((tabArrayPtr == NULL) || (tabArrayPtr->numTabs == 0)) {
5050 tabX = NextTabStop(textPtr->tkfont, x, 0);
5051 return tabX - x;
5052 }
5053 if (index < tabArrayPtr->numTabs) {
5054 tabX = tabArrayPtr->tabs[index].location;
5055 alignment = tabArrayPtr->tabs[index].alignment;
5056 } else {
5057 /*
5058 * Ran out of tab stops; compute a tab position by extrapolating
5059 * from the last two tab positions.
5060 */
5061
5062 if (tabArrayPtr->numTabs > 1) {
5063 prev = tabArrayPtr->tabs[tabArrayPtr->numTabs-2].location;
5064 } else {
5065 prev = 0;
5066 }
5067 tabX = tabArrayPtr->tabs[tabArrayPtr->numTabs-1].location
5068 + (index + 1 - tabArrayPtr->numTabs)
5069 * (tabArrayPtr->tabs[tabArrayPtr->numTabs-1].location - prev);
5070 alignment = tabArrayPtr->tabs[tabArrayPtr->numTabs-1].alignment;
5071 }
5072 if (alignment == CENTER) {
5073 /*
5074 * Be very careful in the arithmetic below, because maxX may
5075 * be the largest positive number: watch out for integer
5076 * overflow.
5077 */
5078
5079 if ((maxX-tabX) < (tabX - x)) {
5080 result = (maxX - x) - 2*(maxX - tabX);
5081 } else {
5082 result = 0;
5083 }
5084 goto done;
5085 }
5086 if (alignment == RIGHT) {
5087 result = 0;
5088 goto done;
5089 }
5090
5091 /*
5092 * Note: this treats NUMERIC alignment the same as LEFT
5093 * alignment, which is somewhat conservative. However, it's
5094 * pretty tricky at this point to figure out exactly where
5095 * the damn decimal point will be.
5096 */
5097
5098 if (tabX > x) {
5099 result = tabX - x;
5100 } else {
5101 result = 0;
5102 }
5103
5104 done:
5105 MeasureChars(textPtr->tkfont, " ", 1, 0, -1, 0, &spaceWidth);
5106 if (result < spaceWidth) {
5107 result = spaceWidth;
5108 }
5109 return result;
5110 }
5111
5112 /*
5113 *---------------------------------------------------------------------------
5114 *
5115 * NextTabStop --
5116 *
5117 * Given the current position, determine where the next default
5118 * tab stop would be located. This procedure is called when the
5119 * current chunk in the text has no tabs defined and so the default
5120 * tab spacing for the font should be used.
5121 *
5122 * Results:
5123 * The location in pixels of the next tab stop.
5124 *
5125 * Side effects:
5126 * None.
5127 *
5128 *---------------------------------------------------------------------------
5129 */
5130
5131 static int
5132 NextTabStop(tkfont, x, tabOrigin)
5133 Tk_Font tkfont; /* Font in which chunk that contains tab
5134 * stop will be drawn. */
5135 int x; /* X-position in pixels where last
5136 * character was drawn. The next tab stop
5137 * occurs somewhere after this location. */
5138 int tabOrigin; /* The origin for tab stops. May be
5139 * non-zero if text has been scrolled. */
5140 {
5141 int tabWidth, rem;
5142
5143 tabWidth = Tk_TextWidth(tkfont, "0", 1) * 8;
5144 if (tabWidth == 0) {
5145 tabWidth = 1;
5146 }
5147
5148 x += tabWidth;
5149 rem = (x - tabOrigin) % tabWidth;
5150 if (rem < 0) {
5151 rem += tabWidth;
5152 }
5153 x -= rem;
5154 return x;
5155 }
5156
5157 /*
5158 *---------------------------------------------------------------------------
5159 *
5160 * MeasureChars --
5161 *
5162 * Determine the number of characters from the string that will fit
5163 * in the given horizontal span. The measurement is done under the
5164 * assumption that Tk_DrawTextLayout will be used to actually display
5165 * the characters.
5166 *
5167 * If tabs are encountered in the string, they will be expanded
5168 * to the next tab stop, unless the TK_IGNORE_TABS flag is specified.
5169 *
5170 * If a newline is encountered in the string, the line will be
5171 * broken at that point, unless the TK_NEWSLINES_NOT_SPECIAL flag
5172 * is specified.
5173 *
5174 * Results:
5175 * The return value is the number of bytes from source
5176 * that fit in the span given by startX and maxX. *nextXPtr
5177 * is filled in with the x-coordinate at which the first
5178 * character that didn't fit would be drawn, if it were to
5179 * be drawn.
5180 *
5181 * Side effects:
5182 * None.
5183 *
5184 *--------------------------------------------------------------
5185 */
5186
5187 static int
5188 MeasureChars(tkfont, source, maxBytes, startX, maxX, tabOrigin, nextXPtr)
5189 Tk_Font tkfont; /* Font in which to draw characters. */
5190 CONST char *source; /* Characters to be displayed. Need not
5191 * be NULL-terminated. */
5192 int maxBytes; /* Maximum # of bytes to consider from
5193 * source. */
5194 int startX; /* X-position at which first character will
5195 * be drawn. */
5196 int maxX; /* Don't consider any character that would
5197 * cross this x-position. */
5198 int tabOrigin; /* X-location that serves as "origin" for
5199 * tab stops. */
5200 int *nextXPtr; /* Return x-position of terminating
5201 * character here. */
5202 {
5203 int curX, width, ch;
5204 CONST char *special, *end, *start;
5205
5206 ch = 0; /* lint. */
5207 curX = startX;
5208 special = source;
5209 end = source + maxBytes;
5210 for (start = source; start < end; ) {
5211 if (start >= special) {
5212 /*
5213 * Find the next special character in the string.
5214 */
5215
5216 for (special = start; special < end; special++) {
5217 ch = *special;
5218 if ((ch == '\t') || (ch == '\n')) {
5219 break;
5220 }
5221 }
5222 }
5223
5224 /*
5225 * Special points at the next special character (or the end of the
5226 * string). Process characters between start and special.
5227 */
5228
5229 if ((maxX >= 0) && (curX >= maxX)) {
5230 break;
5231 }
5232 start += Tk_MeasureChars(tkfont, start, special - start, maxX - curX,
5233 0, &width);
5234 curX += width;
5235 if (start < special) {
5236 /*
5237 * No more chars fit in line.
5238 */
5239
5240 break;
5241 }
5242 if (special < end) {
5243 if (ch == '\t') {
5244 start++;
5245 } else {
5246 break;
5247 }
5248 }
5249 }
5250
5251 *nextXPtr = curX;
5252 return start - source;
5253 }
5254