stk/Tk/generic/tkTextDisp.c

5015 lines
149 KiB
C
Raw Permalink Normal View History

1996-09-27 06:29:02 -04:00
/*
* tkTextDisp.c --
*
* This module provides facilities to display text widgets. It is
* the only place where information is kept about the screen layout
* of text widgets.
*
* Copyright (c) 1992-1994 The Regents of the University of California.
1998-04-10 06:59:06 -04:00
* Copyright (c) 1994-1997 Sun Microsystems, Inc.
1996-09-27 06:29:02 -04:00
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
1998-04-10 06:59:06 -04:00
* SCCS: @(#) tkTextDisp.c 1.124 97/07/11 18:01:03
1996-09-27 06:29:02 -04:00
*/
#include "tkPort.h"
#include "tkInt.h"
#include "tkText.h"
/*
* The following structure describes how to display a range of characters.
* The information is generated by scanning all of the tags associated
* with the characters and combining that with default information for
* the overall widget. These structures form the hash keys for
* dInfoPtr->styleTable.
*/
typedef struct StyleValues {
Tk_3DBorder border; /* Used for drawing background under text.
* NULL means use widget background. */
int borderWidth; /* Width of 3-D border for background. */
int relief; /* 3-D relief for background. */
Pixmap bgStipple; /* Stipple bitmap for background. None
* means draw solid. */
XColor *fgColor; /* Foreground color for text. */
1998-04-10 06:59:06 -04:00
Tk_Font tkfont; /* Font for displaying text. */
1996-09-27 06:29:02 -04:00
Pixmap fgStipple; /* Stipple bitmap for text and other
* foreground stuff. None means draw
* solid.*/
int justify; /* Justification style for text. */
int lMargin1; /* Left margin, in pixels, for first display
* line of each text line. */
int lMargin2; /* Left margin, in pixels, for second and
* later display lines of each text line. */
int offset; /* Offset in pixels of baseline, relative to
* baseline of line. */
int overstrike; /* Non-zero means draw overstrike through
* text. */
int rMargin; /* Right margin, in pixels. */
int spacing1; /* Spacing above first dline in text line. */
int spacing2; /* Spacing between lines of dline. */
int spacing3; /* Spacing below last dline in text line. */
TkTextTabArray *tabArrayPtr;/* Locations and types of tab stops (may
* be NULL). */
int underline; /* Non-zero means draw underline underneath
* text. */
Tk_Uid wrapMode; /* How to handle wrap-around for this tag.
* One of tkTextCharUid, tkTextNoneUid,
* or tkTextWordUid. */
} StyleValues;
/*
* The following structure extends the StyleValues structure above with
* graphics contexts used to actually draw the characters. The entries
* in dInfoPtr->styleTable point to structures of this type.
*/
typedef struct TextStyle {
int refCount; /* Number of times this structure is
* referenced in Chunks. */
GC bgGC; /* Graphics context for background. None
* means use widget background. */
GC fgGC; /* Graphics context for foreground. */
StyleValues *sValuePtr; /* Raw information from which GCs were
* derived. */
Tcl_HashEntry *hPtr; /* Pointer to entry in styleTable. Used
* to delete entry. */
} TextStyle;
/*
* The following macro determines whether two styles have the same
* background so that, for example, no beveled border should be drawn
* between them.
*/
#define SAME_BACKGROUND(s1, s2) \
(((s1)->sValuePtr->border == (s2)->sValuePtr->border) \
&& ((s1)->sValuePtr->borderWidth == (s2)->sValuePtr->borderWidth) \
&& ((s1)->sValuePtr->relief == (s2)->sValuePtr->relief) \
&& ((s1)->sValuePtr->bgStipple == (s2)->sValuePtr->bgStipple))
/*
* The following structure describes one line of the display, which may
* be either part or all of one line of the text.
*/
typedef struct DLine {
TkTextIndex index; /* Identifies first character in text
* that is displayed on this line. */
int count; /* Number of characters accounted for by this
* display line, including a trailing space
* or newline that isn't actually displayed. */
int y; /* Y-position at which line is supposed to
* be drawn (topmost pixel of rectangular
* area occupied by line). */
int oldY; /* Y-position at which line currently
* appears on display. -1 means line isn't
* currently visible on display and must be
* redrawn. This is used to move lines by
* scrolling rather than re-drawing. */
int height; /* Height of line, in pixels. */
int baseline; /* Offset of text baseline from y, in
* pixels. */
int spaceAbove; /* How much extra space was added to the
* top of the line because of spacing
* options. This is included in height
* and baseline. */
int spaceBelow; /* How much extra space was added to the
* bottom of the line because of spacing
* options. This is included in height. */
int length; /* Total length of line, in pixels. */
TkTextDispChunk *chunkPtr; /* Pointer to first chunk in list of all
* of those that are displayed on this
* line of the screen. */
struct DLine *nextPtr; /* Next in list of all display lines for
* this window. The list is sorted in
* order from top to bottom. Note: the
* next DLine doesn't always correspond
* to the next line of text: (a) can have
* multiple DLines for one text line, and
* (b) can have gaps where DLine's have been
* deleted because they're out of date. */
int flags; /* Various flag bits: see below for values. */
} DLine;
/*
* Flag bits for DLine structures:
*
* HAS_3D_BORDER - Non-zero means that at least one of the
* chunks in this line has a 3D border, so
* it potentially interacts with 3D borders
* in neighboring lines (see
* DisplayLineBackground).
* NEW_LAYOUT - Non-zero means that the line has been
* re-layed out since the last time the
* display was updated.
* TOP_LINE - Non-zero means that this was the top line
* in the window the last time that the window
* was laid out. This is important because
* a line may be displayed differently if its
* at the top or bottom than if it's in the
* middle (e.g. beveled edges aren't displayed
* for middle lines if the adjacent line has
* a similar background).
* BOTTOM_LINE - Non-zero means that this was the bottom line
* in the window the last time that the window
* was laid out.
*/
#define HAS_3D_BORDER 1
#define NEW_LAYOUT 2
#define TOP_LINE 4
#define BOTTOM_LINE 8
/*
* Overall display information for a text widget:
*/
typedef struct TextDInfo {
Tcl_HashTable styleTable; /* Hash table that maps from StyleValues
* to TextStyles for this widget. */
DLine *dLinePtr; /* First in list of all display lines for
* this widget, in order from top to bottom. */
GC copyGC; /* Graphics context for copying from off-
* screen pixmaps onto screen. */
GC scrollGC; /* Graphics context for copying from one place
* in the window to another (scrolling):
* differs from copyGC in that we need to get
* GraphicsExpose events. */
int x; /* First x-coordinate that may be used for
* actually displaying line information.
* Leaves space for border, etc. */
int y; /* First y-coordinate that may be used for
* actually displaying line information.
* Leaves space for border, etc. */
int maxX; /* First x-coordinate to right of available
* space for displaying lines. */
int maxY; /* First y-coordinate below available
* space for displaying lines. */
int topOfEof; /* Top-most pixel (lowest y-value) that has
* been drawn in the appropriate fashion for
* the portion of the window after the last
* line of the text. This field is used to
* figure out when to redraw part or all of
* the eof field. */
/*
* Information used for scrolling:
*/
int newCharOffset; /* Desired x scroll position, measured as the
* number of average-size characters off-screen
* to the left for a line with no left
* margin. */
int curPixelOffset; /* Actual x scroll position, measured as the
* number of pixels off-screen to the left. */
int maxLength; /* Length in pixels of longest line that's
* visible in window (length may exceed window
* size). If there's no wrapping, this will
* be zero. */
double xScrollFirst, xScrollLast;
/* Most recent values reported to horizontal
* scrollbar; used to eliminate unnecessary
* reports. */
double yScrollFirst, yScrollLast;
/* Most recent values reported to vertical
* scrollbar; used to eliminate unnecessary
* reports. */
/*
* The following information is used to implement scanning:
*/
int scanMarkChar; /* Character that was at the left edge of
* the window when the scan started. */
int scanMarkX; /* X-position of mouse at time scan started. */
int scanTotalScroll; /* Total scrolling (in screen lines) that has
* occurred since scanMarkY was set. */
int scanMarkY; /* Y-position of mouse at time scan started. */
/*
* Miscellaneous information:
*/
int dLinesInvalidated; /* This value is set to 1 whenever something
* happens that invalidates information in
* DLine structures; if a redisplay
* is in progress, it will see this and
* abort the redisplay. This is needed
* because, for example, an embedded window
* could change its size when it is first
* displayed, invalidating the DLine that
* is currently being displayed. If redisplay
* continues, it will use freed memory and
* could dump core. */
int flags; /* Various flag values: see below for
* definitions. */
} TextDInfo;
/*
* In TkTextDispChunk structures for character segments, the clientData
* field points to one of the following structures:
*/
typedef struct CharInfo {
int numChars; /* Number of characters to display. */
char chars[4]; /* Characters to display. Actual size
* will be numChars, not 4. THIS MUST BE
* THE LAST FIELD IN THE STRUCTURE. */
} CharInfo;
/*
* Flag values for TextDInfo structures:
*
* DINFO_OUT_OF_DATE: Non-zero means that the DLine structures
* for this window are partially or completely
* out of date and need to be recomputed.
* REDRAW_PENDING: Means that a when-idle handler has been
* scheduled to update the display.
* REDRAW_BORDERS: Means window border or pad area has
* potentially been damaged and must be redrawn.
* REPICK_NEEDED: 1 means that the widget has been modified
* in a way that could change the current
* character (a different character might be
* under the mouse cursor now). Need to
* recompute the current character before
* the next redisplay.
*/
#define DINFO_OUT_OF_DATE 1
#define REDRAW_PENDING 2
#define REDRAW_BORDERS 4
#define REPICK_NEEDED 8
/*
* The following counters keep statistics about redisplay that can be
* checked to see how clever this code is at reducing redisplays.
*/
static int numRedisplays; /* Number of calls to DisplayText. */
static int linesRedrawn; /* Number of calls to DisplayDLine. */
static int numCopies; /* Number of calls to XCopyArea to copy part
* of the screen. */
/*
* Forward declarations for procedures defined later in this file:
*/
static void AdjustForTab _ANSI_ARGS_((TkText *textPtr,
TkTextTabArray *tabArrayPtr, int index,
TkTextDispChunk *chunkPtr));
static void CharBboxProc _ANSI_ARGS_((TkTextDispChunk *chunkPtr,
int index, int y, int lineHeight, int baseline,
int *xPtr, int *yPtr, int *widthPtr,
int *heightPtr));
static void CharDisplayProc _ANSI_ARGS_((TkTextDispChunk *chunkPtr,
int x, int y, int height, int baseline,
Display *display, Drawable dst, int screenY));
static int CharMeasureProc _ANSI_ARGS_((TkTextDispChunk *chunkPtr,
int x));
static void CharUndisplayProc _ANSI_ARGS_((TkText *textPtr,
TkTextDispChunk *chunkPtr));
static void DisplayDLine _ANSI_ARGS_((TkText *textPtr,
DLine *dlPtr, DLine *prevPtr, Pixmap pixmap));
static void DisplayLineBackground _ANSI_ARGS_((TkText *textPtr,
DLine *dlPtr, DLine *prevPtr, Pixmap pixmap));
static void DisplayText _ANSI_ARGS_((ClientData clientData));
static DLine * FindDLine _ANSI_ARGS_((DLine *dlPtr,
TkTextIndex *indexPtr));
static void FreeDLines _ANSI_ARGS_((TkText *textPtr,
DLine *firstPtr, DLine *lastPtr, int unlink));
static void FreeStyle _ANSI_ARGS_((TkText *textPtr,
TextStyle *stylePtr));
static TextStyle * GetStyle _ANSI_ARGS_((TkText *textPtr,
TkTextIndex *indexPtr));
static void GetXView _ANSI_ARGS_((Tcl_Interp *interp,
TkText *textPtr, int report));
static void GetYView _ANSI_ARGS_((Tcl_Interp *interp,
TkText *textPtr, int report));
static DLine * LayoutDLine _ANSI_ARGS_((TkText *textPtr,
TkTextIndex *indexPtr));
1998-04-10 06:59:06 -04:00
static int MeasureChars _ANSI_ARGS_((Tk_Font tkfont,
CONST char *source, int maxChars, int startX,
int maxX, int tabOrigin, int *nextXPtr));
1996-09-27 06:29:02 -04:00
static void MeasureUp _ANSI_ARGS_((TkText *textPtr,
TkTextIndex *srcPtr, int distance,
TkTextIndex *dstPtr));
1998-04-10 06:59:06 -04:00
static int NextTabStop _ANSI_ARGS_((Tk_Font tkfont, int x,
int tabOrigin));
1996-09-27 06:29:02 -04:00
static void UpdateDisplayInfo _ANSI_ARGS_((TkText *textPtr));
static void ScrollByLines _ANSI_ARGS_((TkText *textPtr,
int offset));
static int SizeOfTab _ANSI_ARGS_((TkText *textPtr,
TkTextTabArray *tabArrayPtr, int index, int x,
int maxX));
static void TextInvalidateRegion _ANSI_ARGS_((TkText *textPtr,
TkRegion region));
/*
*----------------------------------------------------------------------
*
* TkTextCreateDInfo --
*
* This procedure is called when a new text widget is created.
* Its job is to set up display-related information for the widget.
*
* Results:
* None.
*
* Side effects:
* A TextDInfo data structure is allocated and initialized and attached
* to textPtr.
*
*----------------------------------------------------------------------
*/
void
TkTextCreateDInfo(textPtr)
TkText *textPtr; /* Overall information for text widget. */
{
register TextDInfo *dInfoPtr;
XGCValues gcValues;
dInfoPtr = (TextDInfo *) ckalloc(sizeof(TextDInfo));
Tcl_InitHashTable(&dInfoPtr->styleTable, sizeof(StyleValues)/sizeof(int));
dInfoPtr->dLinePtr = NULL;
dInfoPtr->copyGC = None;
gcValues.graphics_exposures = True;
dInfoPtr->scrollGC = Tk_GetGC(textPtr->tkwin, GCGraphicsExposures,
&gcValues);
dInfoPtr->topOfEof = 0;
dInfoPtr->newCharOffset = 0;
dInfoPtr->curPixelOffset = 0;
dInfoPtr->maxLength = 0;
dInfoPtr->xScrollFirst = -1;
dInfoPtr->xScrollLast = -1;
dInfoPtr->yScrollFirst = -1;
dInfoPtr->yScrollLast = -1;
dInfoPtr->scanMarkChar = 0;
dInfoPtr->scanMarkX = 0;
dInfoPtr->scanTotalScroll = 0;
dInfoPtr->scanMarkY = 0;
dInfoPtr->dLinesInvalidated = 0;
dInfoPtr->flags = DINFO_OUT_OF_DATE;
textPtr->dInfoPtr = dInfoPtr;
}
/*
*----------------------------------------------------------------------
*
* TkTextFreeDInfo --
*
* This procedure is called to free up all of the private display
* information kept by this file for a text widget.
*
* Results:
* None.
*
* Side effects:
* Lots of resources get freed.
*
*----------------------------------------------------------------------
*/
void
TkTextFreeDInfo(textPtr)
TkText *textPtr; /* Overall information for text widget. */
{
register TextDInfo *dInfoPtr = textPtr->dInfoPtr;
/*
* Be careful to free up styleTable *after* freeing up all the
* DLines, so that the hash table is still intact to free up the
* style-related information from the lines. Once the lines are
* all free then styleTable will be empty.
*/
FreeDLines(textPtr, dInfoPtr->dLinePtr, (DLine *) NULL, 1);
Tcl_DeleteHashTable(&dInfoPtr->styleTable);
if (dInfoPtr->copyGC != None) {
Tk_FreeGC(textPtr->display, dInfoPtr->copyGC);
}
Tk_FreeGC(textPtr->display, dInfoPtr->scrollGC);
if (dInfoPtr->flags & REDRAW_PENDING) {
Tcl_CancelIdleCall(DisplayText, (ClientData) textPtr);
}
ckfree((char *) dInfoPtr);
}
/*
*----------------------------------------------------------------------
*
* GetStyle --
*
* This procedure creates all the information needed to display
* text at a particular location.
*
* Results:
* The return value is a pointer to a TextStyle structure that
* corresponds to *sValuePtr.
*
* Side effects:
* A new entry may be created in the style table for the widget.
*
*----------------------------------------------------------------------
*/
static TextStyle *
GetStyle(textPtr, indexPtr)
TkText *textPtr; /* Overall information about text widget. */
TkTextIndex *indexPtr; /* The character in the text for which
* display information is wanted. */
{
TkTextTag **tagPtrs;
register TkTextTag *tagPtr;
StyleValues styleValues;
TextStyle *stylePtr;
Tcl_HashEntry *hPtr;
int numTags, new, i;
XGCValues gcValues;
unsigned long mask;
/*
* The variables below keep track of the highest-priority specification
* that has occurred for each of the various fields of the StyleValues.
*/
int borderPrio, borderWidthPrio, reliefPrio, bgStipplePrio;
int fgPrio, fontPrio, fgStipplePrio;
int underlinePrio, justifyPrio, offsetPrio;
int lMargin1Prio, lMargin2Prio, rMarginPrio;
int spacing1Prio, spacing2Prio, spacing3Prio;
int overstrikePrio, tabPrio, wrapPrio;
/*
* Find out what tags are present for the character, then compute
* a StyleValues structure corresponding to those tags (scan
* through all of the tags, saving information for the highest-
* priority tag).
*/
tagPtrs = TkBTreeGetTags(indexPtr, &numTags);
borderPrio = borderWidthPrio = reliefPrio = bgStipplePrio = -1;
fgPrio = fontPrio = fgStipplePrio = -1;
underlinePrio = justifyPrio = offsetPrio = -1;
lMargin1Prio = lMargin2Prio = rMarginPrio = -1;
spacing1Prio = spacing2Prio = spacing3Prio = -1;
overstrikePrio = tabPrio = wrapPrio = -1;
memset((VOID *) &styleValues, 0, sizeof(StyleValues));
styleValues.relief = TK_RELIEF_FLAT;
styleValues.fgColor = textPtr->fgColor;
1998-04-10 06:59:06 -04:00
styleValues.tkfont = textPtr->tkfont;
1996-09-27 06:29:02 -04:00
styleValues.justify = TK_JUSTIFY_LEFT;
styleValues.spacing1 = textPtr->spacing1;
styleValues.spacing2 = textPtr->spacing2;
styleValues.spacing3 = textPtr->spacing3;
styleValues.tabArrayPtr = textPtr->tabArrayPtr;
styleValues.wrapMode = textPtr->wrapMode;
for (i = 0 ; i < numTags; i++) {
tagPtr = tagPtrs[i];
1998-04-10 06:59:06 -04:00
/*
* On Windows and Mac, we need to skip the selection tag if
* we don't have focus.
*/
#ifndef ALWAYS_SHOW_SELECTION
if ((tagPtr == textPtr->selTagPtr) && !(textPtr->flags & GOT_FOCUS)) {
continue;
}
#endif
1996-09-27 06:29:02 -04:00
if ((tagPtr->border != NULL) && (tagPtr->priority > borderPrio)) {
styleValues.border = tagPtr->border;
borderPrio = tagPtr->priority;
}
if ((tagPtr->bdString != NULL)
&& (tagPtr->priority > borderWidthPrio)) {
styleValues.borderWidth = tagPtr->borderWidth;
borderWidthPrio = tagPtr->priority;
}
if ((tagPtr->reliefString != NULL)
&& (tagPtr->priority > reliefPrio)) {
if (styleValues.border == NULL) {
styleValues.border = textPtr->border;
}
styleValues.relief = tagPtr->relief;
reliefPrio = tagPtr->priority;
}
if ((tagPtr->bgStipple != None)
&& (tagPtr->priority > bgStipplePrio)) {
styleValues.bgStipple = tagPtr->bgStipple;
bgStipplePrio = tagPtr->priority;
}
if ((tagPtr->fgColor != None) && (tagPtr->priority > fgPrio)) {
styleValues.fgColor = tagPtr->fgColor;
fgPrio = tagPtr->priority;
}
1998-04-10 06:59:06 -04:00
if ((tagPtr->tkfont != None) && (tagPtr->priority > fontPrio)) {
styleValues.tkfont = tagPtr->tkfont;
1996-09-27 06:29:02 -04:00
fontPrio = tagPtr->priority;
}
if ((tagPtr->fgStipple != None)
&& (tagPtr->priority > fgStipplePrio)) {
styleValues.fgStipple = tagPtr->fgStipple;
fgStipplePrio = tagPtr->priority;
}
if ((tagPtr->justifyString != NULL)
&& (tagPtr->priority > justifyPrio)) {
styleValues.justify = tagPtr->justify;
justifyPrio = tagPtr->priority;
}
if ((tagPtr->lMargin1String != NULL)
&& (tagPtr->priority > lMargin1Prio)) {
styleValues.lMargin1 = tagPtr->lMargin1;
lMargin1Prio = tagPtr->priority;
}
if ((tagPtr->lMargin2String != NULL)
&& (tagPtr->priority > lMargin2Prio)) {
styleValues.lMargin2 = tagPtr->lMargin2;
lMargin2Prio = tagPtr->priority;
}
if ((tagPtr->offsetString != NULL)
&& (tagPtr->priority > offsetPrio)) {
styleValues.offset = tagPtr->offset;
offsetPrio = tagPtr->priority;
}
if ((tagPtr->overstrikeString != NULL)
&& (tagPtr->priority > overstrikePrio)) {
styleValues.overstrike = tagPtr->overstrike;
overstrikePrio = tagPtr->priority;
}
if ((tagPtr->rMarginString != NULL)
&& (tagPtr->priority > rMarginPrio)) {
styleValues.rMargin = tagPtr->rMargin;
rMarginPrio = tagPtr->priority;
}
if ((tagPtr->spacing1String != NULL)
&& (tagPtr->priority > spacing1Prio)) {
styleValues.spacing1 = tagPtr->spacing1;
spacing1Prio = tagPtr->priority;
}
if ((tagPtr->spacing2String != NULL)
&& (tagPtr->priority > spacing2Prio)) {
styleValues.spacing2 = tagPtr->spacing2;
spacing2Prio = tagPtr->priority;
}
if ((tagPtr->spacing3String != NULL)
&& (tagPtr->priority > spacing3Prio)) {
styleValues.spacing3 = tagPtr->spacing3;
spacing3Prio = tagPtr->priority;
}
if ((tagPtr->tabString != NULL)
&& (tagPtr->priority > tabPrio)) {
styleValues.tabArrayPtr = tagPtr->tabArrayPtr;
tabPrio = tagPtr->priority;
}
if ((tagPtr->underlineString != NULL)
&& (tagPtr->priority > underlinePrio)) {
styleValues.underline = tagPtr->underline;
underlinePrio = tagPtr->priority;
}
if ((tagPtr->wrapMode != NULL)
&& (tagPtr->priority > wrapPrio)) {
styleValues.wrapMode = tagPtr->wrapMode;
wrapPrio = tagPtr->priority;
}
}
if (tagPtrs != NULL) {
ckfree((char *) tagPtrs);
}
/*
* Use an existing style if there's one around that matches.
*/
hPtr = Tcl_CreateHashEntry(&textPtr->dInfoPtr->styleTable,
(char *) &styleValues, &new);
if (!new) {
stylePtr = (TextStyle *) Tcl_GetHashValue(hPtr);
stylePtr->refCount++;
return stylePtr;
}
/*
* No existing style matched. Make a new one.
*/
stylePtr = (TextStyle *) ckalloc(sizeof(TextStyle));
stylePtr->refCount = 1;
if (styleValues.border != NULL) {
gcValues.foreground = Tk_3DBorderColor(styleValues.border)->pixel;
mask = GCForeground;
if (styleValues.bgStipple != None) {
gcValues.stipple = styleValues.bgStipple;
gcValues.fill_style = FillStippled;
mask |= GCStipple|GCFillStyle;
}
stylePtr->bgGC = Tk_GetGC(textPtr->tkwin, mask, &gcValues);
} else {
stylePtr->bgGC = None;
}
mask = GCForeground|GCFont;
gcValues.foreground = styleValues.fgColor->pixel;
1998-04-10 06:59:06 -04:00
gcValues.font = Tk_FontId(styleValues.tkfont);
1996-09-27 06:29:02 -04:00
if (styleValues.fgStipple != None) {
gcValues.stipple = styleValues.fgStipple;
gcValues.fill_style = FillStippled;
mask |= GCStipple|GCFillStyle;
}
stylePtr->fgGC = Tk_GetGC(textPtr->tkwin, mask, &gcValues);
stylePtr->sValuePtr = (StyleValues *)
Tcl_GetHashKey(&textPtr->dInfoPtr->styleTable, hPtr);
stylePtr->hPtr = hPtr;
Tcl_SetHashValue(hPtr, stylePtr);
return stylePtr;
}
/*
*----------------------------------------------------------------------
*
* FreeStyle --
*
* This procedure is called when a TextStyle structure is no longer
* needed. It decrements the reference count and frees up the
* space for the style structure if the reference count is 0.
*
* Results:
* None.
*
* Side effects:
* The storage and other resources associated with the style
* are freed up if no-one's still using it.
*
*----------------------------------------------------------------------
*/
static void
FreeStyle(textPtr, stylePtr)
TkText *textPtr; /* Information about overall widget. */
register TextStyle *stylePtr; /* Information about style to free. */
{
stylePtr->refCount--;
if (stylePtr->refCount == 0) {
if (stylePtr->bgGC != None) {
Tk_FreeGC(textPtr->display, stylePtr->bgGC);
}
Tk_FreeGC(textPtr->display, stylePtr->fgGC);
Tcl_DeleteHashEntry(stylePtr->hPtr);
ckfree((char *) stylePtr);
}
}
/*
*----------------------------------------------------------------------
*
* LayoutDLine --
*
* This procedure generates a single DLine structure for a display
* line whose leftmost character is given by indexPtr.
*
* Results:
* The return value is a pointer to a DLine structure desribing the
* display line. All fields are filled in and correct except for
* y and nextPtr.
*
* Side effects:
* Storage is allocated for the new DLine.
*
*----------------------------------------------------------------------
*/
static DLine *
LayoutDLine(textPtr, indexPtr)
TkText *textPtr; /* Overall information about text widget. */
TkTextIndex *indexPtr; /* Beginning of display line. May not
* necessarily point to a character segment. */
{
register DLine *dlPtr; /* New display line. */
TkTextSegment *segPtr; /* Current segment in text. */
TkTextDispChunk *lastChunkPtr; /* Last chunk allocated so far
* for line. */
TkTextDispChunk *chunkPtr; /* Current chunk. */
TkTextIndex curIndex;
TkTextDispChunk *breakChunkPtr; /* Chunk containing best word break
* point, if any. */
TkTextIndex breakIndex; /* Index of first character in
* breakChunkPtr. */
int breakCharOffset; /* Character within breakChunkPtr just
* to right of best break point. */
int noCharsYet; /* Non-zero means that no characters
* have been placed on the line yet. */
int justify; /* How to justify line: taken from
* style for first character in line. */
int jIndent; /* Additional indentation (beyond
* margins) due to justification. */
int rMargin; /* Right margin width for line. */
Tk_Uid wrapMode; /* Wrap mode to use for this line. */
int x = 0, maxX = 0; /* Initializations needed only to
* stop compiler warnings. */
int wholeLine; /* Non-zero means this display line
* runs to the end of the text line. */
int tabIndex; /* Index of the current tab stop. */
int gotTab; /* Non-zero means the current chunk
* contains a tab. */
TkTextDispChunk *tabChunkPtr; /* Pointer to the chunk containing
* the previous tab stop. */
int maxChars; /* Maximum number of characters to
* include in this chunk. */
TkTextTabArray *tabArrayPtr; /* Tab stops for line; taken from
* style for first character on line. */
int tabSize; /* Number of pixels consumed by current
* tab stop. */
TkTextDispChunk *lastCharChunkPtr; /* Pointer to last chunk in display
* lines with numChars > 0. Used to
* drop 0-sized chunks from the end
* of the line. */
int offset, ascent, descent, code;
StyleValues *sValuePtr;
/*
* Create and initialize a new DLine structure.
*/
dlPtr = (DLine *) ckalloc(sizeof(DLine));
dlPtr->index = *indexPtr;
dlPtr->count = 0;
dlPtr->y = 0;
dlPtr->oldY = -1;
dlPtr->height = 0;
dlPtr->baseline = 0;
dlPtr->chunkPtr = NULL;
dlPtr->nextPtr = NULL;
dlPtr->flags = NEW_LAYOUT;
/*
* Each iteration of the loop below creates one TkTextDispChunk for
* the new display line. The line will always have at least one
* chunk (for the newline character at the end, if there's nothing
* else available).
*/
curIndex = *indexPtr;
lastChunkPtr = NULL;
chunkPtr = NULL;
noCharsYet = 1;
breakChunkPtr = NULL;
breakCharOffset = 0;
justify = TK_JUSTIFY_LEFT;
tabIndex = -1;
tabChunkPtr = NULL;
tabArrayPtr = NULL;
rMargin = 0;
wrapMode = tkTextCharUid;
tabSize = 0;
lastCharChunkPtr = NULL;
/*
* Find the first segment to consider for the line. Can't call
* TkTextIndexToSeg for this because it won't return a segment
* with zero size (such as the insertion cursor's mark).
*/
for (offset = curIndex.charIndex, segPtr = curIndex.linePtr->segPtr;
(offset > 0) && (offset >= segPtr->size);
offset -= segPtr->size, segPtr = segPtr->nextPtr) {
/* Empty loop body. */
}
while (segPtr != NULL) {
if (segPtr->typePtr->layoutProc == NULL) {
segPtr = segPtr->nextPtr;
offset = 0;
continue;
}
if (chunkPtr == NULL) {
chunkPtr = (TkTextDispChunk *) ckalloc(sizeof(TkTextDispChunk));
chunkPtr->nextPtr = NULL;
}
chunkPtr->stylePtr = GetStyle(textPtr, &curIndex);
/*
* Save style information such as justification and indentation,
* up until the first character is encountered, then retain that
* information for the rest of the line.
*/
if (noCharsYet) {
tabArrayPtr = chunkPtr->stylePtr->sValuePtr->tabArrayPtr;
justify = chunkPtr->stylePtr->sValuePtr->justify;
rMargin = chunkPtr->stylePtr->sValuePtr->rMargin;
wrapMode = chunkPtr->stylePtr->sValuePtr->wrapMode;
x = ((curIndex.charIndex == 0)
? chunkPtr->stylePtr->sValuePtr->lMargin1
: chunkPtr->stylePtr->sValuePtr->lMargin2);
if (wrapMode == tkTextNoneUid) {
maxX = INT_MAX;
} else {
maxX = textPtr->dInfoPtr->maxX - textPtr->dInfoPtr->x
- rMargin;
if (maxX < x) {
maxX = x;
}
}
}
/*
* See if there is a tab in the current chunk; if so, only
* layout characters up to (and including) the tab.
*/
gotTab = 0;
maxChars = segPtr->size - offset;
if (justify == TK_JUSTIFY_LEFT) {
if (segPtr->typePtr == &tkTextCharType) {
char *p;
for (p = segPtr->body.chars + offset; *p != 0; p++) {
if (*p == '\t') {
maxChars = (p + 1 - segPtr->body.chars) - offset;
gotTab = 1;
break;
}
}
}
}
chunkPtr->x = x;
code = (*segPtr->typePtr->layoutProc)(textPtr, &curIndex, segPtr,
offset, maxX-tabSize, maxChars, noCharsYet, wrapMode,
chunkPtr);
if (code <= 0) {
FreeStyle(textPtr, chunkPtr->stylePtr);
if (code < 0) {
/*
* This segment doesn't wish to display itself (e.g. most
* marks).
*/
segPtr = segPtr->nextPtr;
offset = 0;
continue;
}
/*
* No characters from this segment fit in the window: this
* means we're at the end of the display line.
*/
if (chunkPtr != NULL) {
ckfree((char *) chunkPtr);
}
break;
}
if (chunkPtr->numChars > 0) {
noCharsYet = 0;
lastCharChunkPtr = chunkPtr;
}
if (lastChunkPtr == NULL) {
dlPtr->chunkPtr = chunkPtr;
} else {
lastChunkPtr->nextPtr = chunkPtr;
}
lastChunkPtr = chunkPtr;
x += chunkPtr->width;
if (chunkPtr->breakIndex > 0) {
breakCharOffset = chunkPtr->breakIndex;
breakIndex = curIndex;
breakChunkPtr = chunkPtr;
}
if (chunkPtr->numChars != maxChars) {
break;
}
/*
* If we're at a new tab, adjust the layout for all the chunks
* pertaining to the previous tab. Also adjust the amount of
* space left in the line to account for space that will be eaten
* up by the tab.
*/
if (gotTab) {
if (tabIndex >= 0) {
AdjustForTab(textPtr, tabArrayPtr, tabIndex, tabChunkPtr);
x = chunkPtr->x + chunkPtr->width;
}
tabIndex++;
tabChunkPtr = chunkPtr;
tabSize = SizeOfTab(textPtr, tabArrayPtr, tabIndex, x, maxX);
if (tabSize >= (maxX - x)) {
break;
}
}
curIndex.charIndex += chunkPtr->numChars;
offset += chunkPtr->numChars;
if (offset >= segPtr->size) {
offset = 0;
segPtr = segPtr->nextPtr;
}
chunkPtr = NULL;
}
if (noCharsYet) {
panic("LayoutDLine couldn't place any characters on a line");
}
wholeLine = (segPtr == NULL);
/*
* We're at the end of the display line. Throw away everything
* after the most recent word break, if there is one; this may
* potentially require the last chunk to be layed out again.
*/
if (breakChunkPtr == NULL) {
/*
* This code makes sure that we don't accidentally display
* chunks with no characters at the end of the line (such as
* the insertion cursor). These chunks belong on the next
* line. So, throw away everything after the last chunk that
* has characters in it.
*/
breakChunkPtr = lastCharChunkPtr;
breakCharOffset = breakChunkPtr->numChars;
}
if ((breakChunkPtr != NULL) && ((lastChunkPtr != breakChunkPtr)
|| (breakCharOffset != lastChunkPtr->numChars))) {
while (1) {
chunkPtr = breakChunkPtr->nextPtr;
if (chunkPtr == NULL) {
break;
}
FreeStyle(textPtr, chunkPtr->stylePtr);
breakChunkPtr->nextPtr = chunkPtr->nextPtr;
(*chunkPtr->undisplayProc)(textPtr, chunkPtr);
ckfree((char *) chunkPtr);
}
if (breakCharOffset != breakChunkPtr->numChars) {
(*breakChunkPtr->undisplayProc)(textPtr, breakChunkPtr);
segPtr = TkTextIndexToSeg(&breakIndex, &offset);
(*segPtr->typePtr->layoutProc)(textPtr, &breakIndex,
segPtr, offset, maxX, breakCharOffset, 0,
wrapMode, breakChunkPtr);
}
lastChunkPtr = breakChunkPtr;
wholeLine = 0;
}
/*
* Make tab adjustments for the last tab stop, if there is one.
*/
if ((tabIndex >= 0) && (tabChunkPtr != NULL)) {
AdjustForTab(textPtr, tabArrayPtr, tabIndex, tabChunkPtr);
}
/*
* Make one more pass over the line to recompute various things
* like its height, length, and total number of characters. Also
* modify the x-locations of chunks to reflect justification.
* If we're not wrapping, I'm not sure what is the best way to
* handle left and center justification: should the total length,
* for purposes of justification, be (a) the window width, (b)
* the length of the longest line in the window, or (c) the length
* of the longest line in the text? (c) isn't available, (b) seems
* weird, since it can change with vertical scrolling, so (a) is
* what is implemented below.
*/
if (wrapMode == tkTextNoneUid) {
maxX = textPtr->dInfoPtr->maxX - textPtr->dInfoPtr->x - rMargin;
}
dlPtr->length = lastChunkPtr->x + lastChunkPtr->width;
if (justify == TK_JUSTIFY_LEFT) {
jIndent = 0;
} else if (justify == TK_JUSTIFY_RIGHT) {
jIndent = maxX - dlPtr->length;
} else {
jIndent = (maxX - dlPtr->length)/2;
}
ascent = descent = 0;
for (chunkPtr = dlPtr->chunkPtr; chunkPtr != NULL;
chunkPtr = chunkPtr->nextPtr) {