Debian Codegraph @

generic/bltHtext.c

raw symbols ◀ ▶ SCIP
3 Debian patches touch this file
1 /*
2 * bltHtext.c --
3 *
4 * This module implements a hypertext widget for the BLT toolkit.
5 *
6 * Copyright 1991-1998 Lucent Technologies, Inc.
7 *
8 * Permission to use, copy, modify, and distribute this software and
9 * its documentation for any purpose and without fee is hereby
10 * granted, provided that the above copyright notice appear in all
11 * copies and that both that the copyright notice and warranty
12 * disclaimer appear in supporting documentation, and that the names
13 * of Lucent Technologies any of their entities not be used in
14 * advertising or publicity pertaining to distribution of the software
15 * without specific, written prior permission.
16 *
17 * Lucent Technologies disclaims all warranties with regard to this
18 * software, including all implied warranties of merchantability and
19 * fitness. In no event shall Lucent Technologies be liable for any
20 * special, indirect or consequential damages or any damages
21 * whatsoever resulting from loss of use, data or profits, whether in
22 * an action of contract, negligence or other tortuous action, arising
23 * out of or in connection with the use or performance of this
24 * software.
25 *
26 * The "htext" widget was created by George Howlett.
27 */
28
29 /*
30 * To do:
31 *
32 * 1) Fix scroll unit round off errors.
33 *
34 * 2) Better error checking.
35 *
36 * 3) Use html format.
37 *
38 * 4) The dimension of cavities using -relwidth and -relheight
39 * should be 0 when computing initial estimates for the size
40 * of the virtual text.
41 */
42
•43 /*
44 * the htext widget is broken in tk8.5.6 as
45 * Tk_ConfigureWidget no longer supports
46 * queries of changed options. Need to migrate to Tk_SetOptions
47 * instead hack a wrapper Blt_ConfigureWidget
48 */
49
50 #include "bltInt.h"
51
52 #ifndef NO_HTEXT
53 #include <bltChain.h>
54 #include <bltHash.h>
55 #include "bltTile.h"
56
57 #include <sys/stat.h>
58 #include <X11/Xatom.h>
59
60 #define DEF_LINES_ALLOC 512 /* Default block of lines allocated */
61 #define CLAMP(val,low,hi) \
62 (((val) < (low)) ? (low) : ((val) > (hi)) ? (hi) : (val))
63
64 /*
65 * Justify option values
66 */
67 typedef enum {
68 JUSTIFY_CENTER, JUSTIFY_TOP, JUSTIFY_BOTTOM
69 } Justify;
70
71 extern Tk_CustomOption bltFillOption;
72 extern Tk_CustomOption bltPadOption;
73 extern Tk_CustomOption bltDistanceOption;
74 extern Tk_CustomOption bltTileOption;
75
76 static int StringToWidth _ANSI_ARGS_((ClientData clientData,
77 Tcl_Interp *interp, Tk_Window tkwin, char *string, char *widgRec,
78 int flags));
79 static int StringToHeight _ANSI_ARGS_((ClientData clientData,
80 Tcl_Interp *interp, Tk_Window tkwin, char *string, char *widgRec,
81 int flags));
82 static char *WidthHeightToString _ANSI_ARGS_((ClientData clientData,
83 Tk_Window tkwin, char *widgRec, int offset, Tcl_FreeProc **freeProc));
84
85 static Tk_CustomOption widthOption =
86 {
87 StringToWidth, WidthHeightToString, (ClientData)0
88 };
89
90 static Tk_CustomOption heightOption =
91 {
92 StringToHeight, WidthHeightToString, (ClientData)0
93 };
94
95 static int StringToJustify _ANSI_ARGS_((ClientData clientData,
96 Tcl_Interp *interp, Tk_Window tkwin, char *string, char *widgRec,
97 int offset));
98 static char *JustifyToString _ANSI_ARGS_((ClientData clientData,
99 Tk_Window tkwin, char *widgRec, int offset, Tcl_FreeProc **freeProcPtr));
100
101 static Tk_CustomOption justifyOption =
102 {
103 StringToJustify, JustifyToString, (ClientData)0
104 };
105
106
107 static void EmbeddedWidgetGeometryProc _ANSI_ARGS_((ClientData, Tk_Window));
108 static void EmbeddedWidgetCustodyProc _ANSI_ARGS_((ClientData, Tk_Window));
109
110 static Tk_GeomMgr htextMgrInfo =
111 {
112 "htext", /* Name of geometry manager used by winfo */
113 EmbeddedWidgetGeometryProc, /* Procedure to for new geometry requests */
114 EmbeddedWidgetCustodyProc, /* Procedure when window is taken away */
115 };
116
117
118 /*
119 * Line --
120 *
121 * Structure to contain the contents of a single line of text and
122 * the widgets on that line.
123 *
124 * Individual lines are not configurable, although changes to the
125 * size of widgets do effect its values.
126 */
127 typedef struct {
128 int offset; /* Offset of line from y-origin (0) in
129 * world coordinates */
130 int baseline; /* Baseline y-coordinate of the text */
131 short int width, height; /* Dimensions of the line */
132 int textStart, textEnd; /* Start and end indices of characters
133 * forming the line in the text array */
134 Blt_Chain *chainPtr; /* Chain of embedded widgets on the line of
135 * text */
136 } Line;
137
138 typedef struct {
139 int textStart;
140 int textEnd;
141 } Segment;
142
143 typedef struct {
144 int x, y;
145 } Position;
146
147 /*
148 * Hypertext widget.
149 */
150 typedef struct {
151 Tk_Window tkwin; /* Window that embodies the widget.
152 * NULL means that the window has been
153 * destroyed but the data structures
154 * haven't yet been cleaned up.*/
155 Display *display; /* Display containing widget; needed,
156 * among other things, to release
157 * resources after tkwin has already
158 * gone away. */
159 Tcl_Interp *interp; /* Interpreter associated with widget. */
160
161 Tcl_Command cmdToken; /* Token for htext's widget command. */
162 int flags;
163
164 /* User-configurable fields */
165
166 XColor *normalFg, *normalBg;
167 Tk_Font font; /* Font for normal text. May affect the size
168 * of the viewport if the width/height is
169 * specified in columns/rows */
170 GC drawGC; /* Graphics context for normal text */
171 Blt_Tile tile;
172 int tileOffsetPage; /* Set tile offset to top of page instead
173 * of toplevel window */
174 GC fillGC; /* GC for clearing the window in the
175 * designated background color. The
176 * background color is the foreground
177 * attribute in GC. */
178
179 int nRows, nColumns; /* # of characters of the current font
180 * for a row or column of the viewport.
181 * Used to determine the width and height
182 * of the text window (i.e. viewport) */
183 int reqWidth, reqHeight; /* Requested dimensions of the viewport */
184 int maxWidth, maxHeight; /* Maximum dimensions allowed for the viewport,
185 * regardless of the size of the text */
186
187 Tk_Cursor cursor; /* X Cursor */
188
189 char *fileName; /* If non-NULL, indicates the name of a
190 * hypertext file to be read into the widget.
191 * If NULL, the *text* field is considered
192 * instead */
193 char *text; /* Hypertext to be loaded into the widget. This
194 * value is ignored if *fileName* is non-NULL */
195 int specChar; /* Special character designating a TCL
196 * command block in a hypertext file. */
197 int leader; /* # of pixels between lines */
198
199 char *yScrollCmdPrefix; /* Name of vertical scrollbar to invoke */
200 int yScrollUnits; /* # of pixels per vertical scroll */
201 char *xScrollCmdPrefix; /* Name of horizontal scroll bar to invoke */
202 int xScrollUnits; /* # of pixels per horizontal scroll */
203
204 int reqLineNum; /* Line requested by "goto" command */
205
206 /*
207 * The view port is the width and height of the window and the
208 * origin of the viewport (upper left corner) in world coordinates.
209 */
210 int worldWidth, worldHeight;/* Size of view text in world coordinates */
211 int xOffset, yOffset; /* Position of viewport in world coordinates */
212
213 int pendingX, pendingY; /* New upper-left corner (origin) of
214 * the viewport (not yet posted) */
215
216 int first, last; /* Range of lines displayed */
217
218 int lastWidth, lastHeight;
219 /* Last known size of the window: saved to
220 * recognize when the viewport is resized. */
221
222 Blt_HashTable widgetTable; /* Table of embedded widgets. */
223
224 /*
225 * Selection display information:
226 */
227 Tk_3DBorder selBorder; /* Border and background color */
228 int selBorderWidth; /* Border width */
229 XColor *selFgColor; /* Text foreground color */
230 GC selectGC; /* GC for drawing selected text */
231 int selAnchor; /* Fixed end of selection
232 * (i.e. "selection to" operation will
233 * use this as one end of the selection).*/
234 int selFirst; /* The index of first character in the
235 * text array selected */
236 int selLast; /* The index of the last character selected */
237 int exportSelection; /* Non-zero means to export the internal text
238 * selection to the X server. */
239 char *takeFocus;
240
241 /*
242 * Scanning information:
243 */
244 XPoint scanMark; /* Anchor position of scan */
245 XPoint scanPt; /* x,y position where the scan started. */
246
247 char *charArr; /* Pool of characters representing the text
248 * to be displayed */
249 int nChars; /* Length of the text pool */
250
251 Line *lineArr; /* Array of pointers to text lines */
252 int nLines; /* # of line entered into array. */
253 int arraySize; /* Size of array allocated. */
254
255 } HText;
256
257 /*
258 * Bit flags for the hypertext widget:
259 */
260 #define REDRAW_PENDING (1<<0) /* A DoWhenIdle handler has already
261 * been queued to redraw the window */
262 #define IGNORE_EXPOSURES (1<<1) /* Ignore exposure events in the text
263 * window. Potentially many expose
264 * events can occur while rearranging
265 * embedded widgets during a single call to
266 * the DisplayText. */
267
268 #define REQUEST_LAYOUT (1<<4) /* Something has happened which
269 * requires the layout of text and
270 * embedded widget positions to be
271 * recalculated. The following
272 * actions may cause this:
273 *
274 * 1) the contents of the hypertext
275 * has changed by either the -file or
276 * -text options.
277 *
278 * 2) a text attribute has changed
279 * (line spacing, font, etc)
280 *
281 * 3) a embedded widget has been resized or
282 * moved.
283 *
284 * 4) a widget configuration option has
285 * changed.
286 */
287 #define TEXT_DIRTY (1<<5) /* The layout was recalculated and the
288 * size of the world (text layout) has
289 * changed. */
290 #define GOTO_PENDING (1<<6) /* Indicates the starting text line
291 * number has changed. To be reflected
292 * the next time the widget is redrawn. */
293 #define WIDGET_APPENDED (1<<7) /* Indicates a embedded widget has just
294 * been appended to the text. This is
295 * used to determine when to add a
296 * space to the text array */
297
298 #define DEF_HTEXT_BACKGROUND STD_NORMAL_BACKGROUND
299 #define DEF_HTEXT_BG_MONO STD_NORMAL_BG_MONO
300 #define DEF_HTEXT_CURSOR "arrow"
301 #define DEF_HTEXT_EXPORT_SELECTION "1"
302
303 #define DEF_HTEXT_FOREGROUND STD_NORMAL_FOREGROUND
304 #define DEF_HTEXT_FG_MONO STD_NORMAL_FG_MONO
305 #define DEF_HTEXT_FILE_NAME (char *)NULL
306 #define DEF_HTEXT_FONT STD_FONT
307 #define DEF_HTEXT_HEIGHT "0"
308 #define DEF_HTEXT_LINE_SPACING "1"
309 #define DEF_HTEXT_MAX_HEIGHT (char *)NULL
310 #define DEF_HTEXT_MAX_WIDTH (char *)NULL
311 #define DEF_HTEXT_SCROLL_UNITS "10"
312 #define DEF_HTEXT_SPEC_CHAR "0x25"
313 #define DEF_HTEXT_SELECT_BORDERWIDTH STD_SELECT_BORDERWIDTH
314 #define DEF_HTEXT_SELECT_BACKGROUND STD_SELECT_BACKGROUND
315 #define DEF_HTEXT_SELECT_BG_MONO STD_SELECT_BG_MONO
316 #define DEF_HTEXT_SELECT_FOREGROUND STD_SELECT_FOREGROUND
317 #define DEF_HTEXT_SELECT_FG_MONO STD_SELECT_FG_MONO
318 #define DEF_HTEXT_TAKE_FOCUS "1"
319 #define DEF_HTEXT_TEXT (char *)NULL
320 #define DEF_HTEXT_TILE_OFFSET "1"
321 #define DEF_HTEXT_WIDTH "0"
322
323 static Tk_ConfigSpec configSpecs[] =
324 {
325 {TK_CONFIG_COLOR, "-background", "background", "Background",
326 DEF_HTEXT_BACKGROUND, Tk_Offset(HText, normalBg), TK_CONFIG_COLOR_ONLY},
327 {TK_CONFIG_COLOR, "-background", "background", "Background",
328 DEF_HTEXT_BG_MONO, Tk_Offset(HText, normalBg), TK_CONFIG_MONO_ONLY},
329 {TK_CONFIG_SYNONYM, "-bg", "background", (char *)NULL, (char *)NULL, 0, 0},
330 {TK_CONFIG_ACTIVE_CURSOR, "-cursor", "cursor", "Cursor",
331 DEF_HTEXT_CURSOR, Tk_Offset(HText, cursor), TK_CONFIG_NULL_OK},
332 {TK_CONFIG_BOOLEAN, "-exportselection", "exportSelection", "ExportSelection",
333 DEF_HTEXT_EXPORT_SELECTION, Tk_Offset(HText, exportSelection), 0},
334 {TK_CONFIG_SYNONYM, "-fg", "foreground", (char *)NULL, (char *)NULL, 0, 0},
335 {TK_CONFIG_STRING, "-file", "file", "File",
336 DEF_HTEXT_FILE_NAME, Tk_Offset(HText, fileName), TK_CONFIG_NULL_OK},
337 {TK_CONFIG_FONT, "-font", "font", "Font",
338 DEF_HTEXT_FONT, Tk_Offset(HText, font), 0},
339 {TK_CONFIG_COLOR, "-foreground", "foreground", "Foreground",
340 DEF_HTEXT_FOREGROUND, Tk_Offset(HText, normalFg), TK_CONFIG_COLOR_ONLY},
341 {TK_CONFIG_COLOR, "-foreground", "foreground", "Foreground",
342 DEF_HTEXT_FG_MONO, Tk_Offset(HText, normalFg), TK_CONFIG_MONO_ONLY},
343 {TK_CONFIG_CUSTOM, "-height", "height", "Height",
344 DEF_HTEXT_HEIGHT, Tk_Offset(HText, reqHeight),
345 TK_CONFIG_DONT_SET_DEFAULT, &heightOption},
346 {TK_CONFIG_CUSTOM, "-linespacing", "lineSpacing", "LineSpacing",
347 DEF_HTEXT_LINE_SPACING, Tk_Offset(HText, leader),
•348 TK_CONFIG_DONT_SET_DEFAULT, &bltDistanceOption},
349 {TK_CONFIG_CUSTOM, "-maxheight", "maxHeight", "MaxHeight",
350 DEF_HTEXT_MAX_HEIGHT, Tk_Offset(HText, maxHeight),
351 TK_CONFIG_DONT_SET_DEFAULT, &bltDistanceOption},
352 {TK_CONFIG_CUSTOM, "-maxwidth", "maxWidth", "MaxWidth",
353 DEF_HTEXT_MAX_WIDTH, Tk_Offset(HText, maxWidth),
354 TK_CONFIG_DONT_SET_DEFAULT, &bltDistanceOption},
355 {TK_CONFIG_BORDER, "-selectbackground", "selectBackground", "Background",
356 DEF_HTEXT_SELECT_BG_MONO, Tk_Offset(HText, selBorder),
357 TK_CONFIG_MONO_ONLY},
358 {TK_CONFIG_BORDER, "-selectbackground", "selectBackground", "Background",
359 DEF_HTEXT_SELECT_BACKGROUND, Tk_Offset(HText, selBorder),
360 TK_CONFIG_COLOR_ONLY},
361 {TK_CONFIG_CUSTOM, "-selectborderwidth", "selectBorderWidth", "BorderWidth",
362 DEF_HTEXT_SELECT_BORDERWIDTH, Tk_Offset(HText, selBorderWidth),
363 TK_CONFIG_DONT_SET_DEFAULT, &bltDistanceOption},
364 {TK_CONFIG_COLOR, "-selectforeground", "selectForeground", "Foreground",
365 DEF_HTEXT_SELECT_FG_MONO, Tk_Offset(HText, selFgColor),
366 TK_CONFIG_MONO_ONLY},
367 {TK_CONFIG_COLOR, "-selectforeground", "selectForeground", "Foreground",
368 DEF_HTEXT_SELECT_FOREGROUND, Tk_Offset(HText, selFgColor),
369 TK_CONFIG_COLOR_ONLY},
370 {TK_CONFIG_INT, "-specialchar", "specialChar", "SpecialChar",
371 DEF_HTEXT_SPEC_CHAR, Tk_Offset(HText, specChar), 0},
372 {TK_CONFIG_STRING, "-takefocus", "takeFocus", "TakeFocus",
373 DEF_HTEXT_TAKE_FOCUS, Tk_Offset(HText, takeFocus),
374 TK_CONFIG_NULL_OK},
375 {TK_CONFIG_CUSTOM, "-tile", "tile", "Tile",
376 (char *)NULL, Tk_Offset(HText, tile), TK_CONFIG_NULL_OK,
377 &bltTileOption},
378 {TK_CONFIG_BOOLEAN, "-tileoffset", "tileOffset", "TileOffset",
379 DEF_HTEXT_TILE_OFFSET, Tk_Offset(HText, tileOffsetPage), 0},
380 {TK_CONFIG_STRING, "-text", "text", "Text",
381 DEF_HTEXT_TEXT, Tk_Offset(HText, text), TK_CONFIG_NULL_OK},
382 {TK_CONFIG_CUSTOM, "-width", "width", "Width",
383 DEF_HTEXT_WIDTH, Tk_Offset(HText, reqWidth),
384 TK_CONFIG_DONT_SET_DEFAULT, &widthOption},
385 {TK_CONFIG_STRING, "-xscrollcommand", "xScrollCommand", "ScrollCommand",
386 (char *)NULL, Tk_Offset(HText, xScrollCmdPrefix), TK_CONFIG_NULL_OK},
387 {TK_CONFIG_CUSTOM, "-xscrollunits", "xScrollUnits", "ScrollUnits",
388 DEF_HTEXT_SCROLL_UNITS, Tk_Offset(HText, xScrollUnits),
389 TK_CONFIG_DONT_SET_DEFAULT, &bltDistanceOption},
390 {TK_CONFIG_STRING, "-yscrollcommand", "yScrollCommand", "ScrollCommand",
391 (char *)NULL, Tk_Offset(HText, yScrollCmdPrefix), TK_CONFIG_NULL_OK},
392 {TK_CONFIG_CUSTOM, "-yscrollunits", "yScrollUnits", "yScrollUnits",
393 DEF_HTEXT_SCROLL_UNITS, Tk_Offset(HText, yScrollUnits),
394 TK_CONFIG_DONT_SET_DEFAULT, &bltDistanceOption},
395 {TK_CONFIG_END, (char *)NULL, (char *)NULL, (char *)NULL,
396 (char *)NULL, 0, 0}
397 };
398
399 typedef struct {
400 HText *htPtr; /* Pointer to parent's Htext structure */
401 Tk_Window tkwin; /* Widget window */
402 int flags;
403
404 int x, y; /* Origin of embedded widget in text */
405
406 int cavityWidth, cavityHeight; /* Dimensions of the cavity
407 * surrounding the embedded widget */
408 /*
409 * Dimensions of the embedded widget. Compared against actual
410 * embedded widget sizes when checking for resizing.
411 */
412 int winWidth, winHeight;
413
414 int precedingTextEnd; /* Index (in charArr) of the the last
415 * character immediatedly preceding
416 * the embedded widget */
417 int precedingTextWidth; /* Width of normal text preceding widget. */
418
419 Tk_Anchor anchor;
420 Justify justify; /* Justification of region wrt to line */
421
422 /*
423 * Requested dimensions of the cavity (includes padding). If non-zero,
424 * it overrides the calculated dimension of the cavity.
425 */
426 int reqCavityWidth, reqCavityHeight;
427
428 /*
429 * Relative dimensions of cavity wrt the size of the viewport. If
430 * greater than 0.0.
431 */
432 double relCavityWidth, relCavityHeight;
433
434 int reqWidth, reqHeight; /* If non-zero, overrides the requested
435 * dimension of the embedded widget */
436
437 double relWidth, relHeight; /* Relative dimensions of embedded
438 * widget wrt the size of the viewport */
439
440 Blt_Pad padX, padY; /* Extra padding to frame around */
441
442 int ipadX, ipadY; /* internal padding for window */
443
444 int fill; /* Fill style flag */
445
446 } EmbeddedWidget;
447
448 /*
449 * Flag bits embedded widgets:
450 */
451 #define WIDGET_VISIBLE (1<<2) /* Widget is currently visible in the
452 * viewport. */
453 #define WIDGET_NOT_CHILD (1<<3) /* Widget is not a child of hypertext. */
454 /*
455 * Defaults for embedded widgets:
456 */
457 #define DEF_WIDGET_ANCHOR "center"
458 #define DEF_WIDGET_FILL "none"
459 #define DEF_WIDGET_HEIGHT "0"
460 #define DEF_WIDGET_JUSTIFY "center"
461 #define DEF_WIDGET_PAD_X "0"
462 #define DEF_WIDGET_PAD_Y "0"
463 #define DEF_WIDGET_REL_HEIGHT "0.0"
464 #define DEF_WIDGET_REL_WIDTH "0.0"
465 #define DEF_WIDGET_WIDTH "0"
466
467 static Tk_ConfigSpec widgetConfigSpecs[] =
468 {
469 {TK_CONFIG_ANCHOR, "-anchor", (char *)NULL, (char *)NULL,
470 DEF_WIDGET_ANCHOR, Tk_Offset(EmbeddedWidget, anchor),
471 TK_CONFIG_DONT_SET_DEFAULT},
472 {TK_CONFIG_CUSTOM, "-fill", (char *)NULL, (char *)NULL,
473 DEF_WIDGET_FILL, Tk_Offset(EmbeddedWidget, fill),
474 TK_CONFIG_DONT_SET_DEFAULT, &bltFillOption},
475 {TK_CONFIG_CUSTOM, "-cavityheight", (char *)NULL, (char *)NULL,
476 DEF_WIDGET_HEIGHT, Tk_Offset(EmbeddedWidget, reqCavityHeight),
477 TK_CONFIG_DONT_SET_DEFAULT, &bltDistanceOption},
478 {TK_CONFIG_CUSTOM, "-cavitywidth", (char *)NULL, (char *)NULL,
479 DEF_WIDGET_WIDTH, Tk_Offset(EmbeddedWidget, reqCavityWidth),
480 TK_CONFIG_DONT_SET_DEFAULT, &bltDistanceOption},
481 {TK_CONFIG_CUSTOM, "-height", (char *)NULL, (char *)NULL,
482 DEF_WIDGET_HEIGHT, Tk_Offset(EmbeddedWidget, reqHeight),
483 TK_CONFIG_DONT_SET_DEFAULT, &bltDistanceOption},
484 {TK_CONFIG_CUSTOM, "-justify", (char *)NULL, (char *)NULL,
485 DEF_WIDGET_JUSTIFY, Tk_Offset(EmbeddedWidget, justify),
486 TK_CONFIG_DONT_SET_DEFAULT, &justifyOption},
487 {TK_CONFIG_CUSTOM, "-padx", (char *)NULL, (char *)NULL,
488 DEF_WIDGET_PAD_X, Tk_Offset(EmbeddedWidget, padX),
489 TK_CONFIG_DONT_SET_DEFAULT, &bltPadOption},
490 {TK_CONFIG_CUSTOM, "-pady", (char *)NULL, (char *)NULL,
491 DEF_WIDGET_PAD_Y, Tk_Offset(EmbeddedWidget, padY),
492 TK_CONFIG_DONT_SET_DEFAULT, &bltPadOption},
493 {TK_CONFIG_DOUBLE, "-relcavityheight", (char *)NULL, (char *)NULL,
494 DEF_WIDGET_REL_HEIGHT, Tk_Offset(EmbeddedWidget, relCavityHeight),
495 TK_CONFIG_DONT_SET_DEFAULT},
496 {TK_CONFIG_DOUBLE, "-relcavitywidth", (char *)NULL, (char *)NULL,
497 DEF_WIDGET_REL_WIDTH, Tk_Offset(EmbeddedWidget, relCavityWidth),
498 TK_CONFIG_DONT_SET_DEFAULT},
499 {TK_CONFIG_DOUBLE, "-relheight", (char *)NULL, (char *)NULL,
500 DEF_WIDGET_REL_HEIGHT, Tk_Offset(EmbeddedWidget, relHeight),
501 TK_CONFIG_DONT_SET_DEFAULT},
502 {TK_CONFIG_DOUBLE, "-relwidth", (char *)NULL, (char *)NULL,
503 DEF_WIDGET_REL_WIDTH, Tk_Offset(EmbeddedWidget, relWidth),
504 TK_CONFIG_DONT_SET_DEFAULT},
505 {TK_CONFIG_CUSTOM, "-width", (char *)NULL, (char *)NULL,
506 DEF_WIDGET_WIDTH, Tk_Offset(EmbeddedWidget, reqWidth),
507 TK_CONFIG_DONT_SET_DEFAULT, &bltDistanceOption},
508 {TK_CONFIG_END, (char *)NULL, (char *)NULL, (char *)NULL,
509 (char *)NULL, 0, 0}
510 };
511
512
513 /* Forward Declarations */
514 static void DestroyText _ANSI_ARGS_((DestroyData dataPtr));
515 static void EmbeddedWidgetEventProc _ANSI_ARGS_((ClientData clientdata,
516 XEvent *eventPtr));
517 static void DisplayText _ANSI_ARGS_((ClientData clientData));
518 static void TextDeleteCmdProc _ANSI_ARGS_((ClientData clientdata));
519
520 static Tcl_VarTraceProc TextVarProc;
521 static Blt_TileChangedProc TileChangedProc;
522 static Tk_LostSelProc TextLostSelection;
523 static Tk_SelectionProc TextSelectionProc;
524 static Tk_EventProc TextEventProc;
525 static Tcl_CmdProc TextWidgetCmd;
526 static Tcl_CmdProc TextCmd;
527
528 static void widgetWorldChanged(ClientData clientData);
529
530 static Tk_ClassProcs htextClass = {
531 sizeof(Tk_ClassProcs), /* size */
532 widgetWorldChanged, /* worldChangedProc */
533 };
534
535 /* end of Forward Declarations */
536
537
538 /* Custom options */
539 /*
540 *----------------------------------------------------------------------
541 *
542 * StringToJustify --
543 *
544 * Converts the justification string into its numeric
545 * representation. This configuration option affects how the
546 * embedded widget is positioned with respect to the line on which
547 * it sits.
548 *
549 * Valid style strings are:
550 *
551 * "top" Uppermost point of region is top of the line's
552 * text
553 * "center" Center point of region is line's baseline.
554 * "bottom" Lowermost point of region is bottom of the
555 * line's text
556 *
557 * Returns:
558 * A standard Tcl result. If the value was not valid
559 *
560 *---------------------------------------------------------------------- */
561 /*ARGSUSED*/
562 static int
563 StringToJustify(clientData, interp, tkwin, string, widgRec, offset)
564 ClientData clientData; /* Not used. */
565 Tcl_Interp *interp; /* Interpreter to send results back to */
566 Tk_Window tkwin; /* Not used. */
567 char *string; /* Justification string */
568 char *widgRec; /* Structure record */
569 int offset; /* Offset of justify in record */
570 {
571 Justify *justPtr = (Justify *)(widgRec + offset);
572 unsigned int length;
573 char c;
574
575 c = string[0];
576 length = strlen(string);
577 if ((c == 'c') && (strncmp(string, "center", length) == 0)) {
578 *justPtr = JUSTIFY_CENTER;
579 } else if ((c == 't') && (strncmp(string, "top", length) == 0)) {
580 *justPtr = JUSTIFY_TOP;
581 } else if ((c == 'b') && (strncmp(string, "bottom", length) == 0)) {
582 *justPtr = JUSTIFY_BOTTOM;
583 } else {
584 Tcl_AppendResult(interp, "bad justification argument \"", string,
585 "\": should be \"center\", \"top\", or \"bottom\"", (char *)NULL);
586 return TCL_ERROR;
587 }
588 return TCL_OK;
589 }
590
591 /*
592 *----------------------------------------------------------------------
593 *
594 * NameOfJustify --
595 *
596 * Returns the justification style string based upon the value.
597 *
598 * Results:
599 * The static justification style string is returned.
600 *
601 *----------------------------------------------------------------------
602 */
603 static char *
604 NameOfJustify(justify)
605 Justify justify;
606 {
607 switch (justify) {
608 case JUSTIFY_CENTER:
609 return "center";
610 case JUSTIFY_TOP:
611 return "top";
612 case JUSTIFY_BOTTOM:
613 return "bottom";
614 default:
615 return "unknown justification value";
616 }
617 }
618
619 /*
620 *----------------------------------------------------------------------
621 *
622 * JustifyToString --
623 *
624 * Returns the justification style string based upon the value.
625 *
626 * Results:
627 * The justification style string is returned.
628 *
629 *----------------------------------------------------------------------
630 */
631 /*ARGSUSED*/
632 static char *
633 JustifyToString(clientData, tkwin, widgRec, offset, freeProcPtr)
634 ClientData clientData; /* Not used. */
635 Tk_Window tkwin; /* Not used. */
636 char *widgRec; /* Structure record */
637 int offset; /* Offset of justify record */
638 Tcl_FreeProc **freeProcPtr; /* Not used. */
639 {
640 Justify justify = *(Justify *)(widgRec + offset);
641
642 return NameOfJustify(justify);
643 }
644
645 /*
646 *----------------------------------------------------------------------
647 *
648 * GetScreenDistance --
649 *
650 * Converts the given string into the screen distance or number
651 * of characters. The valid formats are
652 *
653 * N - pixels Nm - millimeters
654 * Ni - inches Np - pica
655 * Nc - centimeters N# - number of characters
656 *
657 * where N is a non-negative decimal number.
658 *
659 * Results:
660 * A standard Tcl result. The screen distance and the number of
661 * characters are returned. If the string can't be converted,
662 * TCL_ERROR is returned and interp->result will contain an error
663 * message.
664 *
665 *----------------------------------------------------------------------
666 */
667 static int
668 GetScreenDistance(interp, tkwin, string, sizePtr, countPtr)
669 Tcl_Interp *interp;
670 Tk_Window tkwin;
671 char *string;
672 int *sizePtr;
673 int *countPtr;
674 {
675 int nPixels, nChars;
676 char *endPtr; /* Pointer to last character scanned */
677 double value;
678 int rounded;
679
680 value = strtod(string, &endPtr);
681 if (endPtr == string) {
682 Tcl_AppendResult(interp, "bad screen distance \"", string, "\"",
683 (char *)NULL);
684 return TCL_ERROR;
685 }
686 if (value < 0.0) {
687 Tcl_AppendResult(interp, "screen distance \"", string,
688 "\" must be non-negative value", (char *)NULL);
689 return TCL_ERROR;
690 }
691 while (isspace(UCHAR(*endPtr))) {
692 if (*endPtr == '\0') {
693 break;
694 }
695 endPtr++;
696 }
697 nPixels = nChars = 0;
698 rounded = ROUND(value);
699 switch (*endPtr) {
700 case '\0': /* Distance in pixels */
701 nPixels = rounded;
702 break;
703 case '#': /* Number of characters */
704 nChars = rounded;
705 break;
706 default: /* cm, mm, pica, inches */
707 if (Tk_GetPixels(interp, tkwin, string, &rounded) != TCL_OK) {
708 return TCL_ERROR;
709 }
710 nPixels = rounded;
711 break;
712 }
713 *sizePtr = nPixels;
714 *countPtr = nChars;
715 return TCL_OK;
716 }
717
718 /*
719 *----------------------------------------------------------------------
720 *
721 * StringToHeight --
722 *
723 * Like TK_CONFIG_PIXELS, but adds an extra check for negative
724 * values.
725 *
726 *----------------------------------------------------------------------
727 */
728 /*ARGSUSED*/
729 static int
730 StringToHeight(clientData, interp, tkwin, string, widgRec, offset)
731 ClientData clientData; /* Not used. */
732 Tcl_Interp *interp; /* Interpreter to send results back to */
733 Tk_Window tkwin; /* Window */
734 char *string; /* Pixel value string */
735 char *widgRec; /* Widget record */
736 int offset; /* Not used. */
737 {
738 HText *htPtr = (HText *)widgRec;
739 int height, nRows;
740
741 if (GetScreenDistance(interp, tkwin, string, &height, &nRows) != TCL_OK) {
742 return TCL_ERROR;
743 }
744 htPtr->nRows = nRows;
745 htPtr->reqHeight = height;
746 return TCL_OK;
747 }
748
749 /*
750 *----------------------------------------------------------------------
751 *
752 * StringToWidth --
753 *
754 * Like TK_CONFIG_PIXELS, but adds an extra check for negative
755 * values.
756 *
757 *----------------------------------------------------------------------
758 */
759 /*ARGSUSED*/
760 static int
761 StringToWidth(clientData, interp, tkwin, string, widgRec, offset)
762 ClientData clientData; /* Not used. */
763 Tcl_Interp *interp; /* Interpreter to send results back to */
764 Tk_Window tkwin; /* Window */
765 char *string; /* Pixel value string */
766 char *widgRec; /* Widget record */
767 int offset; /* Not used. */
768 {
769 HText *htPtr = (HText *)widgRec;
770 int width, nColumns;
771
772 if (GetScreenDistance(interp, tkwin, string, &width,
773 &nColumns) != TCL_OK) {
774 return TCL_ERROR;
775 }
776 htPtr->nColumns = nColumns;
777 htPtr->reqWidth = width;
778 return TCL_OK;
779 }
780
781 /*
782 *----------------------------------------------------------------------
783 *
784 * WidthHeightToString --
785 *
786 * Returns the string representing the positive pixel size.
787 *
788 * Results:
789 * The pixel size string is returned.
790 *
791 *----------------------------------------------------------------------
792 */
793 /*ARGSUSED*/
794 static char *
795 WidthHeightToString(clientData, tkwin, widgRec, offset, freeProcPtr)
796 ClientData clientData; /* Not used. */
797 Tk_Window tkwin; /* Not used. */
798 char *widgRec; /* Row/column structure record */
799 int offset; /* Offset of fill in Partition record */
800 Tcl_FreeProc **freeProcPtr; /* Not used. */
801 {
802 int pixels = *(int *)(widgRec + offset);
803 char *result;
804 char string[200];
805
806 sprintf(string, "%d", pixels);
807 result = Blt_Strdup(string);
808 if (result == NULL) {
809 return "out of memory";
810 }
811 *freeProcPtr = (Tcl_FreeProc *)Blt_Free;
812 return result;
813 }
814
815 /* General routines */
816 /*
817 *----------------------------------------------------------------------
818 *
819 * EventuallyRedraw --
820 *
821 * Queues a request to redraw the text window at the next idle
822 * point.
823 *
824 * Results:
825 * None.
826 *
827 * Side effects:
828 * Information gets redisplayed. Right now we don't do selective
829 * redisplays: the whole window will be redrawn. This doesn't
830 * seem to hurt performance noticeably, but if it does then this
831 * could be changed.
832 *
833 *----------------------------------------------------------------------
834 */
835 static void
836 EventuallyRedraw(htPtr)
837 HText *htPtr; /* Information about widget. */
838 {
839 if ((htPtr->tkwin != NULL) && !(htPtr->flags & REDRAW_PENDING)) {
840 htPtr->flags |= REDRAW_PENDING;
841 Tcl_DoWhenIdle(DisplayText, htPtr);
842 }
843 }
844
845
846 /*
847 * --------------------------------------------------------------------
848 *
849 * ResizeArray --
850 *
851 * Reallocates memory to the new size given. New memory
852 * is also cleared (zeros).
853 *
854 * Results:
855 * Returns a pointer to the new object or NULL if an error occurred.
856 *
857 * Side Effects:
858 * Memory is re/allocated.
859 *
860 * --------------------------------------------------------------------
861 */
862 static int
863 ResizeArray(arrayPtr, elemSize, newSize, prevSize)
864 char **arrayPtr;
865 int elemSize;
866 int newSize;
867 int prevSize;
868 {
869 char *newPtr;
870
871 if (newSize == prevSize) {
872 return TCL_OK;
873 }
874 if (newSize == 0) { /* Free entire array */
875 Blt_Free(*arrayPtr);
876 *arrayPtr = NULL;
877 return TCL_OK;
878 }
879 newPtr = Blt_Calloc(elemSize, newSize);
880 if (newPtr == NULL) {
881 return TCL_ERROR;
882 }
883 if ((prevSize > 0) && (*arrayPtr != NULL)) {
884 int size;
885
886 size = MIN(prevSize, newSize) * elemSize;
887 if (size > 0) {
888 memcpy(newPtr, *arrayPtr, size);
889 }
890 Blt_Free(*arrayPtr);
891 }
892 *arrayPtr = newPtr;
893 return TCL_OK;
894 }
895
896 /*
897 * ----------------------------------------------------------------------
898 *
899 * LineSearch --
900 *
901 * Performs a binary search for the line of text located at some
902 * world y-coordinate (not screen y-coordinate). The search is
903 * inclusive of those lines from low to high.
904 *
905 * Results:
906 * Returns the array index of the line found at the given
907 * y-coordinate. If the y-coordinate is outside of the given range
908 * of lines, -1 is returned.
909 *
910 * ----------------------------------------------------------------------
911 */
912 static int
913 LineSearch(htPtr, yCoord, low, high)
914 HText *htPtr; /* HText widget */
915 int yCoord; /* Search y-coordinate */
916 int low, high; /* Range of lines to search */
917 {
918 int median;
919 Line *linePtr;
920
921 while (low <= high) {
922 median = (low + high) >> 1;
923 linePtr = htPtr->lineArr + median;
924 if (yCoord < linePtr->offset) {
925 high = median - 1;
926 } else if (yCoord >= (linePtr->offset + linePtr->height)) {
927 low = median + 1;
928 } else {
929 return median;
930 }
931 }
932 return -1;
933 }
934
935 /*
936 * ----------------------------------------------------------------------
937 *
938 * IndexSearch --
939 *
940 * Try to find what line contains a given text index. Performs
941 * a binary search for the text line which contains the given index.
942 * The search is inclusive of those lines from low and high.
943 *
944 * Results:
945 * Returns the line number containing the given index. If the index
946 * is outside the range of lines, -1 is returned.
947 *
948 * ----------------------------------------------------------------------
949 */
950 static int
951 IndexSearch(htPtr, key, low, high)
952 HText *htPtr; /* HText widget */
953 int key; /* Search index */
954 int low, high; /* Range of lines to search */
955 {
956 int median;
957 Line *linePtr;
958
959 while (low <= high) {
960 median = (low + high) >> 1;
961 linePtr = htPtr->lineArr + median;
962 if (key < linePtr->textStart) {
963 high = median - 1;
964 } else if (key > linePtr->textEnd) {
965 low = median + 1;
966 } else {
967 return median;
968 }
969 }
970 return -1;
971 }
972
973 /*
974 * ----------------------------------------------------------------------
975 *
976 * GetXYPosIndex --
977 *
978 * Converts a string in the form "@x,y", where x and y are
979 * window coordinates, to a text index.
980 *
981 * Window coordinates are first translated into world coordinates.
982 * Any coordinate outside of the bounds of the virtual text is
983 * silently set the nearest boundary.
984 *
985 * Results:
986 * A standard Tcl result. If "string" is a valid index, then
987 * *indexPtr is filled with the numeric index corresponding.
988 * Otherwise an error message is left in interp->result.
989 *
990 * ----------------------------------------------------------------------
991 */
992 static int
993 GetXYPosIndex(htPtr, string, indexPtr)
994 HText *htPtr;
995 char *string;
996 int *indexPtr;
997 {
998 int x, y, curX, dummy;
999 int textLength, textStart;
1000 int cindex, lindex;
1001 Line *linePtr;
1002
1003 if (Blt_GetXY(htPtr->interp, htPtr->tkwin, string, &x, &y) != TCL_OK) {
1004 return TCL_ERROR;
1005 }
1006 /* Locate the line corresponding to the window y-coordinate position */
1007
1008 y += htPtr->yOffset;
1009 if (y < 0) {
1010 lindex = htPtr->first;
1011 } else if (y >= htPtr->worldHeight) {
1012 lindex = htPtr->last;
1013 } else {
1014 lindex = LineSearch(htPtr, y, 0, htPtr->nLines - 1);
1015 }
1016 if (lindex < 0) {
1017 Tcl_AppendResult(htPtr->interp, "can't find line at \"", string, "\"",
1018 (char *)NULL);
1019 return TCL_ERROR;
1020 }
1021 x += htPtr->xOffset;
1022 if (x < 0) {
1023 x = 0;
1024 } else if (x > htPtr->worldWidth) {
1025 x = htPtr->worldWidth;
1026 }
1027 linePtr = htPtr->lineArr + lindex;
1028 curX = 0;
1029 textStart = linePtr->textStart;
1030 textLength = linePtr->textEnd - linePtr->textStart;
1031 if (Blt_ChainGetLength(linePtr->chainPtr) > 0) {
1032 Blt_ChainLink *linkPtr;
1033 int deltaX;
1034 EmbeddedWidget *winPtr;
1035
1036 for (linkPtr = Blt_ChainFirstLink(linePtr->chainPtr); linkPtr != NULL;
1037 linkPtr = Blt_ChainNextLink(linkPtr)) {
1038 winPtr = Blt_ChainGetValue(linkPtr);
1039 deltaX = winPtr->precedingTextWidth + winPtr->cavityWidth;
1040 if ((curX + deltaX) > x) {
1041 textLength = (winPtr->precedingTextEnd - textStart);
1042 break;
1043 }
1044 curX += deltaX;
1045 /*
1046 * Skip over the trailing space. It designates the position of
1047 * a embedded widget in the text
1048 */
1049 textStart = winPtr->precedingTextEnd + 1;
1050 }
1051 }
1052 cindex = Tk_MeasureChars(htPtr->font, htPtr->charArr + textStart,
1053 textLength, 10000, DEF_TEXT_FLAGS, &dummy);
1054 *indexPtr = textStart + cindex;
1055 return TCL_OK;
1056 }
1057
1058 /*
1059 *--------------------------------------------------------------
1060 *
1061 * ParseIndex --
1062 *
1063 * Parse a string representing a text index into numeric
1064 * value. A text index can be in one of the following forms.
1065 *
1066 * "anchor" - anchor position of the selection.
1067 * "sel.first" - index of the first character in the selection.
1068 * "sel.last" - index of the last character in the selection.
1069 * "page.top" - index of the first character on the page.
1070 * "page.bottom" - index of the last character on the page.
1071 * "@x,y" - x and y are window coordinates.
1072 * "number - raw index of text
1073 * "line.char" - line number and character position
1074 *
1075 * Results:
1076 * A standard Tcl result. If "string" is a valid index, then
1077 * *indexPtr is filled with the corresponding numeric index.
1078 * Otherwise an error message is left in interp->result.
1079 *
1080 * Side effects:
1081 * None.
1082 *
1083 *--------------------------------------------------------------
1084 */
1085 static int
1086 ParseIndex(htPtr, string, indexPtr)
1087 HText *htPtr; /* Text for which the index is being
1088 * specified. */
1089 char *string; /* Numerical index into htPtr's element
1090 * list, or "end" to refer to last element. */
1091 int *indexPtr; /* Where to store converted relief. */
1092 {
1093 unsigned int length;
1094 char c;
1095 Tcl_Interp *interp = htPtr->interp;
1096
1097 length = strlen(string);
1098 c = string[0];
1099
1100 if ((c == 'a') && (strncmp(string, "anchor", length) == 0)) {
1101 *indexPtr = htPtr->selAnchor;
1102 } else if ((c == 's') && (length > 4)) {
1103 if (strncmp(string, "sel.first", length) == 0) {
1104 *indexPtr = htPtr->selFirst;
1105 } else if (strncmp(string, "sel.last", length) == 0) {
1106 *indexPtr = htPtr->selLast;
1107 } else {
1108 goto badIndex; /* Not a valid index */
1109 }
1110 if (*indexPtr < 0) {
1111 Tcl_AppendResult(interp, "bad index \"", string,
1112 "\": nothing selected in \"",
1113 Tk_PathName(htPtr->tkwin), "\"", (char *)NULL);
1114 return TCL_ERROR;
1115 }
1116 } else if ((c == 'p') && (length > 5) &&
1117 (strncmp(string, "page.top", length) == 0)) {
1118 int first;
1119
1120 first = htPtr->first;
1121 if (first < 0) {
1122 first = 0;
1123 }
1124 *indexPtr = htPtr->lineArr[first].textStart;
1125 } else if ((c == 'p') && (length > 5) &&
1126 (strncmp(string, "page.bottom", length) == 0)) {
1127 *indexPtr = htPtr->lineArr[htPtr->last].textEnd;
1128 } else if (c == '@') { /* Screen position */
1129 if (GetXYPosIndex(htPtr, string, indexPtr) != TCL_OK) {
1130 return TCL_ERROR;
1131 }
1132 } else {
1133 char *period;
1134
1135 period = strchr(string, '.');
1136 if (period == NULL) { /* Raw index */
1137 int tindex;
1138
1139 if ((string[0] == 'e') && (strcmp(string, "end") == 0)) {
1140 tindex = htPtr->nChars - 1;
1141 } else if (Tcl_GetInt(interp, string, &tindex) != TCL_OK) {
1142 goto badIndex;
1143 }
1144 if (tindex < 0) {
1145 tindex = 0;
1146 } else if (tindex > (htPtr->nChars - 1)) {
1147 tindex = htPtr->nChars - 1;
1148 }
1149 *indexPtr = tindex;
1150 } else {
1151 int lindex, cindex, offset;
1152 Line *linePtr;
1153 int result;
1154
1155 *period = '\0';
1156 result = TCL_OK;
1157 if ((string[0] == 'e') && (strcmp(string, "end") == 0)) {
1158 lindex = htPtr->nLines - 1;
1159 } else {
1160 result = Tcl_GetInt(interp, string, &lindex);
1161 }
1162 *period = '.'; /* Repair index string before returning */
1163 if (result != TCL_OK) {
1164 goto badIndex; /* Bad line number */
1165 }
1166 if (lindex < 0) {
1167 lindex = 0; /* Silently repair bad line numbers */
1168 }
1169 if (htPtr->nChars == 0) {
1170 *indexPtr = 0;
1171 return TCL_OK;
1172 }
1173 if (lindex >= htPtr->nLines) {
1174 lindex = htPtr->nLines - 1;
1175 }
1176 linePtr = htPtr->lineArr + lindex;
1177 cindex = 0;
1178 if ((*(period + 1) != '\0')) {
1179 string = period + 1;
1180 if ((string[0] == 'e') && (strcmp(string, "end") == 0)) {
1181 cindex = linePtr->textEnd - linePtr->textStart;
1182 } else if (Tcl_GetInt(interp, string, &cindex) != TCL_OK) {
1183 goto badIndex;
1184 }
1185 }
1186 if (cindex < 0) {
1187 cindex = 0; /* Silently fix bogus indices */
1188 }
1189 offset = 0;
1190 if (htPtr->nChars > 0) {
1191 offset = linePtr->textStart + cindex;
1192 if (offset > linePtr->textEnd) {
1193 offset = linePtr->textEnd;
1194 }
1195 }
1196 *indexPtr = offset;
1197 }
1198 }
1199 if (htPtr->nChars == 0) {
1200 *indexPtr = 0;
1201 }
1202 return TCL_OK;
1203
1204 badIndex:
1205
1206 /*
1207 * Some of the paths here leave messages in interp->result, so we
1208 * have to clear it out before storing our own message.
1209 */
1210 Tcl_ResetResult(interp);
1211 Tcl_AppendResult(interp, "bad index \"", string, "\": \
1212 should be one of the following: anchor, sel.first, sel.last, page.bottom, \
1213 page.top, @x,y, index, line.char", (char *)NULL);
1214 return TCL_ERROR;
1215 }
1216
1217 /*
1218 *--------------------------------------------------------------
1219 *
1220 * GetIndex --
1221 *
1222 * Get the index from a string representing a text index.
1223 *
1224 *
1225 * Results:
1226 * A standard Tcl result. If "string" is a valid index, then
1227 * *indexPtr is filled with the numeric index corresponding.
1228 * Otherwise an error message is left in interp->result.
1229 *
1230 * Side effects:
1231 * None.
1232 *
1233 *--------------------------------------------------------------
1234 */
1235 static int
1236 GetIndex(htPtr, string, indexPtr)
1237 HText *htPtr; /* Text for which the index is being
1238 * specified. */
1239 char *string; /* Numerical index into htPtr's element
1240 * list, or "end" to refer to last element. */
1241 int *indexPtr; /* Where to store converted relief. */
1242 {
1243 int tindex;
1244
1245 if (ParseIndex(htPtr, string, &tindex) != TCL_OK) {
1246 return TCL_ERROR;
1247 }
1248 *indexPtr = tindex;
1249 return TCL_OK;
1250 }
1251
1252 /*
1253 * ----------------------------------------------------------------------
1254 *
1255 * GetTextPosition --
1256 *
1257 * Performs a binary search for the index located on line in
1258 * the text. The search is limited to those lines between
1259 * low and high inclusive.
1260 *
1261 * Results:
1262 * Returns the line number at the given Y coordinate. If position
1263 * does not correspond to any of the lines in the given the set,
1264 * -1 is returned.
1265 *
1266 * ----------------------------------------------------------------------
1267 */
1268 static int
1269 GetTextPosition(htPtr, tindex, lindexPtr, cindexPtr)
1270 HText *htPtr;
1271 int tindex;
1272 int *lindexPtr;
1273 int *cindexPtr;
1274 {
1275 int lindex, cindex;
1276
1277 lindex = cindex = 0;
1278 if (htPtr->nChars > 0) {
1279 Line *linePtr;
1280
1281 lindex = IndexSearch(htPtr, tindex, 0, htPtr->nLines - 1);
1282 if (lindex < 0) {
1283 char string[200];
1284
1285 sprintf(string, "can't determine line number from index \"%d\"",
1286 tindex);
1287 Tcl_AppendResult(htPtr->interp, string, (char *)NULL);
1288 return TCL_ERROR;
1289 }
1290 linePtr = htPtr->lineArr + lindex;
1291 if (tindex > linePtr->textEnd) {
1292 tindex = linePtr->textEnd;
1293 }
1294 cindex = tindex - linePtr->textStart;
1295 }
1296 *lindexPtr = lindex;
1297 *cindexPtr = cindex;
1298 return TCL_OK;
1299 }
1300
1301 /* EmbeddedWidget Procedures */
1302 /*
1303 *----------------------------------------------------------------------
1304 *
1305 * GetEmbeddedWidgetWidth --
1306 *
1307 * Returns the width requested by the embedded widget. The requested
1308 * space also includes any internal padding which has been designated
1309 * for this window.
1310 *
1311 * Results:
1312 * Returns the requested width of the embedded widget.
1313 *
1314 *----------------------------------------------------------------------
1315 */
1316 static int
1317 GetEmbeddedWidgetWidth(winPtr)
1318 EmbeddedWidget *winPtr;
1319 {
1320 int width;
1321
1322 if (winPtr->reqWidth > 0) {
1323 width = winPtr->reqWidth;
1324 } else if (winPtr->relWidth > 0.0) {
1325 width = (int)
1326 ((double)Tk_Width(winPtr->htPtr->tkwin) * winPtr->relWidth + 0.5);
1327 } else {
1328 width = Tk_ReqWidth(winPtr->tkwin);
1329 }
1330 width += (2 * winPtr->ipadX);
1331 return width;
1332 }
1333
1334 /*
1335 *----------------------------------------------------------------------
1336 *
1337 * GetEmbeddedWidgetHeight --
1338 *
1339 * Returns the height requested by the embedded widget. The requested
1340 * space also includes any internal padding which has been designated
1341 * for this window.
1342 *
1343 * Results:
1344 * Returns the requested height of the embedded widget.
1345 *
1346 *----------------------------------------------------------------------
1347 */
1348 static int
1349 GetEmbeddedWidgetHeight(winPtr)
1350 EmbeddedWidget *winPtr;
1351 {
1352 int height;
1353
1354 if (winPtr->reqHeight > 0) {
1355 height = winPtr->reqHeight;
1356 } else if (winPtr->relHeight > 0.0) {
1357 height = (int)((double)Tk_Height(winPtr->htPtr->tkwin) *
1358 winPtr->relHeight + 0.5);
1359 } else {
1360 height = Tk_ReqHeight(winPtr->tkwin);
1361 }
1362 height += (2 * winPtr->ipadY);
1363 return height;
1364 }
1365
1366 /*
1367 * --------------------------------------------------------------
1368 *
1369 * EmbeddedWidgetEventProc --
1370 *
1371 * This procedure is invoked by the Tk dispatcher for various
1372 * events on hypertext widgets.
1373 *
1374 * Results:
1375 * None.
1376 *
1377 * Side effects:
1378 * When the window gets deleted, internal structures get
1379 * cleaned up. When it gets exposed, it is redisplayed.
1380 *
1381 * --------------------------------------------------------------
1382 */
1383 static void
1384 EmbeddedWidgetEventProc(clientData, eventPtr)
1385 ClientData clientData; /* Information about the embedded widget. */
1386 XEvent *eventPtr; /* Information about event. */
1387 {
1388 EmbeddedWidget *winPtr = clientData;
1389 HText *htPtr;
1390
1391 if ((winPtr == NULL) || (winPtr->tkwin == NULL)) {
1392 return;
1393 }
1394 htPtr = winPtr->htPtr;
1395
1396 if (eventPtr->type == DestroyNotify) {
1397 Blt_HashEntry *hPtr;
1398 /*
1399 * Mark the widget as deleted by dereferencing the Tk window
1400 * pointer. Zero out the height and width to collapse the area
1401 * used by the widget. Redraw the window only if the widget is
1402 * currently visible.
1403 */
1404 winPtr->htPtr->flags |= REQUEST_LAYOUT;
1405 if (Tk_IsMapped(winPtr->tkwin) && (winPtr->flags & WIDGET_VISIBLE)) {
1406 EventuallyRedraw(htPtr);
1407 }
1408 Tk_DeleteEventHandler(winPtr->tkwin, StructureNotifyMask,
1409 EmbeddedWidgetEventProc, winPtr);
1410 hPtr = Blt_FindHashEntry(&(htPtr->widgetTable), (char *)winPtr->tkwin);
1411 Blt_DeleteHashEntry(&(htPtr->widgetTable), hPtr);
1412 winPtr->cavityWidth = winPtr->cavityHeight = 0;
1413 winPtr->tkwin = NULL;
1414
1415 } else if (eventPtr->type == ConfigureNotify) {
1416 /*
1417 * EmbeddedWidgets can't request new positions. Worry only about resizing.
1418 */
1419 if (winPtr->winWidth != Tk_Width(winPtr->tkwin) ||
1420 winPtr->winHeight != Tk_Height(winPtr->tkwin)) {
1421 EventuallyRedraw(htPtr);
1422 htPtr->flags |= REQUEST_LAYOUT;
1423 }
1424 }
1425 }
1426
1427 /*
1428 *--------------------------------------------------------------
1429 *
1430 * EmbeddedWidgetCustodyProc --
1431 *
1432 * This procedure is invoked when a embedded widget has been
1433 * stolen by another geometry manager. The information and
1434 * memory associated with the embedded widget is released.
1435 *
1436 * Results:
1437 * None.
1438 *
1439 * Side effects:
1440 * Arranges for the widget formerly associated with the widget
1441 * to have its layout re-computed and arranged at the
1442 * next idle point.
1443 *
1444 *--------------------------------------------------------------
1445 */
1446 /* ARGSUSED */
1447 static void
1448 EmbeddedWidgetCustodyProc(clientData, tkwin)
1449 ClientData clientData; /* Information about the former embedded widget. */
1450 Tk_Window tkwin; /* Not used. */
1451 {
1452 Blt_HashEntry *hPtr;
1453 EmbeddedWidget *winPtr = clientData;
1454 /*
1455 * Mark the widget as deleted by dereferencing the Tk window
1456 * pointer. Zero out the height and width to collapse the area
1457 * used by the widget. Redraw the window only if the widget is
1458 * currently visible.
1459 */
1460 winPtr->htPtr->flags |= REQUEST_LAYOUT;
1461 if (Tk_IsMapped(winPtr->tkwin) && (winPtr->flags & WIDGET_VISIBLE)) {
1462 EventuallyRedraw(winPtr->htPtr);
1463 }
1464 Tk_DeleteEventHandler(winPtr->tkwin, StructureNotifyMask,
1465 EmbeddedWidgetEventProc, winPtr);
1466 hPtr = Blt_FindHashEntry(&(winPtr->htPtr->widgetTable),
1467 (char *)winPtr->tkwin);
1468 Blt_DeleteHashEntry(&(winPtr->htPtr->widgetTable), hPtr);
1469 winPtr->cavityWidth = winPtr->cavityHeight = 0;
1470 winPtr->tkwin = NULL;
1471 }
1472
1473 /*
1474 *--------------------------------------------------------------
1475 *
1476 * EmbeddedWidgetGeometryProc --
1477 *
1478 * This procedure is invoked by Tk_GeometryRequest for
1479 * embedded widgets managed by the hypertext widget.
1480 *
1481 * Results:
1482 * None.
1483 *
1484 * Side effects:
1485 * Arranges for tkwin, and all its managed siblings, to
1486 * be repacked and drawn at the next idle point.
1487 *
1488 *--------------------------------------------------------------
1489 */
1490 /* ARGSUSED */
1491 static void
1492 EmbeddedWidgetGeometryProc(clientData, tkwin)
1493 ClientData clientData; /* Information about window that got new
1494 * preferred geometry. */
1495 Tk_Window tkwin; /* Other Tk-related information about the
1496 * window. */
1497 {
1498 EmbeddedWidget *winPtr = clientData;
1499
1500 winPtr->htPtr->flags |= REQUEST_LAYOUT;
1501 EventuallyRedraw(winPtr->htPtr);
1502 }
1503
1504 /*
1505 * ----------------------------------------------------------------------
1506 *
1507 * FindEmbeddedWidget --
1508 *
1509 * Searches for a widget matching the path name given
1510 * If found, the pointer to the widget structure is returned,
1511 * otherwise NULL.
1512 *
1513 * Results:
1514 * The pointer to the widget structure. If not found, NULL.
1515 *
1516 * ----------------------------------------------------------------------
1517 */
1518 static EmbeddedWidget *
1519 FindEmbeddedWidget(htPtr, tkwin)
1520 HText *htPtr; /* Hypertext widget structure */
1521 Tk_Window tkwin; /* Path name of embedded widget */
1522 {
1523 Blt_HashEntry *hPtr;
1524
1525 hPtr = Blt_FindHashEntry(&(htPtr->widgetTable), (char *)tkwin);
1526 if (hPtr != NULL) {
1527 return (EmbeddedWidget *) Blt_GetHashValue(hPtr);
1528 }
1529 return NULL;
1530 }
1531
1532 /*
1533 * ----------------------------------------------------------------------
1534 *
1535 * CreateEmbeddedWidget --
1536 *
1537 * This procedure creates and initializes a new embedded widget
1538 * in the hyper text widget.
1539 *
1540 * Results:
1541 * The return value is a pointer to a structure describing the
1542 * new embedded widget. If an error occurred, then the return
1543 * value is NULL and an error message is left in interp->result.
1544 *
1545 * Side effects:
1546 * Memory is allocated. EmbeddedWidget window is mapped.
1547 * Callbacks are set up for embedded widget resizes and geometry
1548 * requests.
1549 *
1550 * ----------------------------------------------------------------------
1551 */
1552 static EmbeddedWidget *
1553 CreateEmbeddedWidget(htPtr, name)
1554 HText *htPtr; /* Hypertext widget */
1555 char *name; /* Name of embedded widget */
1556 {
1557 EmbeddedWidget *winPtr;
1558 Tk_Window tkwin;
1559 Blt_HashEntry *hPtr;
1560 int isNew;
1561
1562 tkwin = Tk_NameToWindow(htPtr->interp, name, htPtr->tkwin);
1563 if (tkwin == NULL) {
1564 return NULL;
1565 }
1566 if (Tk_Parent(tkwin) != htPtr->tkwin) {
1567 Tcl_AppendResult(htPtr->interp, "parent window of \"", name,
1568 "\" must be \"", Tk_PathName(htPtr->tkwin), "\"", (char *)NULL);
1569 return NULL;
1570 }
1571 hPtr = Blt_CreateHashEntry(&(htPtr->widgetTable), (char *)tkwin, &isNew);
1572 /* Check is the widget is already embedded into this widget */
1573 if (!isNew) {
1574 Tcl_AppendResult(htPtr->interp, "\"", name,
1575 "\" is already appended to ", Tk_PathName(htPtr->tkwin),
1576 (char *)NULL);
1577 return NULL;
1578 }
1579 winPtr = Blt_Calloc(1, sizeof(EmbeddedWidget));
1580 assert(winPtr);
1581 winPtr->flags = 0;
1582 winPtr->tkwin = tkwin;
1583 winPtr->htPtr = htPtr;
1584 winPtr->x = winPtr->y = 0;
1585 winPtr->fill = FILL_NONE;
1586 winPtr->justify = JUSTIFY_CENTER;
1587 winPtr->anchor = TK_ANCHOR_CENTER;
1588 Blt_SetHashValue(hPtr, winPtr);
1589
1590 Tk_ManageGeometry(tkwin, &htextMgrInfo, winPtr);
1591 Tk_CreateEventHandler(tkwin, StructureNotifyMask, EmbeddedWidgetEventProc,
1592 winPtr);
1593 return winPtr;
1594 }
1595
1596 /*
1597 * ----------------------------------------------------------------------
1598 *
1599 * DestroyEmbeddedWidget --
1600 *
1601 * This procedure is invoked by DestroyLine to clean up the
1602 * internal structure of a widget.
1603 *
1604 * Results:
1605 * None.
1606 *
1607 * Side effects:
1608 * Everything associated with the widget is freed up.
1609 *
1610 * ----------------------------------------------------------------------
1611 */
1612 static void
1613 DestroyEmbeddedWidget(winPtr)
1614 EmbeddedWidget *winPtr;
1615 {
1616 /* Destroy the embedded widget if it still exists */
1617 if (winPtr->tkwin != NULL) {
1618 Blt_HashEntry *hPtr;
1619
1620 Tk_DeleteEventHandler(winPtr->tkwin, StructureNotifyMask,
1621 EmbeddedWidgetEventProc, winPtr);
1622 hPtr = Blt_FindHashEntry(&(winPtr->htPtr->widgetTable),
1623 (char *)winPtr->tkwin);
1624 Blt_DeleteHashEntry(&(winPtr->htPtr->widgetTable), hPtr);
1625 Tk_DestroyWindow(winPtr->tkwin);
1626 }
1627 Blt_Free(winPtr);
1628 }
1629
1630 /* Line Procedures */
1631 /*
1632 * ----------------------------------------------------------------------
1633 *
1634 * CreateLine --
1635 *
1636 * This procedure creates and initializes a new line of text.
1637 *
1638 * Results:
1639 * The return value is a pointer to a structure describing the new
1640 * line of text. If an error occurred, then the return value is NULL
1641 * and an error message is left in interp->result.
1642 *
1643 * Side effects:
1644 * Memory is allocated.
1645 *
1646 * ----------------------------------------------------------------------
1647 */
1648 static Line *
1649 CreateLine(htPtr)
1650 HText *htPtr;
1651 {
1652 Line *linePtr;
1653
1654 if (htPtr->nLines >= htPtr->arraySize) {
1655 if (htPtr->arraySize == 0) {
1656 htPtr->arraySize = DEF_LINES_ALLOC;
1657 } else {
1658 htPtr->arraySize += htPtr->arraySize;
1659 }
1660 if (ResizeArray((char **)&(htPtr->lineArr), sizeof(Line),
1661 htPtr->arraySize, htPtr->nLines) != TCL_OK) {
1662 return NULL;
1663 }
1664 }
1665 /* Initialize values in the new entry */
1666
1667 linePtr = htPtr->lineArr + htPtr->nLines;
1668 linePtr->offset = 0;
1669 linePtr->height = linePtr->width = 0;
1670 linePtr->textStart = 0;
1671 linePtr->textEnd = -1;
1672 linePtr->baseline = 0;
1673 linePtr->chainPtr = Blt_ChainCreate();
1674
1675 htPtr->nLines++;
1676 return linePtr;
1677 }
1678
1679 /*
1680 * ----------------------------------------------------------------------
1681 *
1682 * DestroyLine --
1683 *
1684 * This procedure is invoked to clean up the internal structure
1685 * of a line.
1686 *
1687 * Results:
1688 * None.
1689 *
1690 * Side effects:
1691 * Everything associated with the line (text and widgets) is
1692 * freed up.
1693 *
1694 * ----------------------------------------------------------------------
1695 */
1696 static void
1697 DestroyLine(linePtr)
1698 Line *linePtr;
1699 {
1700 Blt_ChainLink *linkPtr;
1701 EmbeddedWidget *winPtr;
1702
1703 /* Free the list of embedded widget structures */
1704 for (linkPtr = Blt_ChainFirstLink(linePtr->chainPtr); linkPtr != NULL;
1705 linkPtr = Blt_ChainNextLink(linkPtr)) {
1706 winPtr = Blt_ChainGetValue(linkPtr);
1707 DestroyEmbeddedWidget(winPtr);
1708 }
1709 Blt_ChainDestroy(linePtr->chainPtr);
1710 }
1711
1712 static void
1713 FreeText(htPtr)
1714 HText *htPtr;
1715 {
1716 int i;
1717
1718 for (i = 0; i < htPtr->nLines; i++) {
1719 DestroyLine(htPtr->lineArr + i);
1720 }
1721 htPtr->nLines = 0;
1722 htPtr->nChars = 0;
1723 if (htPtr->charArr != NULL) {
1724 Blt_Free(htPtr->charArr);
1725 htPtr->charArr = NULL;
1726 }
1727 }
1728
1729 /* Text Procedures */
1730 /*
1731 * ----------------------------------------------------------------------
1732 *
1733 * DestroyText --
1734 *
1735 * This procedure is invoked by Tcl_EventuallyFree or Tcl_Release
1736 * to clean up the internal structure of a HText at a safe time
1737 * (when no-one is using it anymore).
1738 *
1739 * Results:
1740 * None.
1741 *
1742 * Side effects:
1743 * Everything associated with the widget is freed up.
1744 *
1745 * ----------------------------------------------------------------------
1746 */
1747 static void
1748 DestroyText(dataPtr)
1749 DestroyData dataPtr; /* Info about hypertext widget. */
1750 {
1751 HText *htPtr = (HText *)dataPtr;
1752
1753 Tk_FreeOptions(configSpecs, (char *)htPtr, htPtr->display, 0);
1754 if (htPtr->drawGC != NULL) {
1755 Tk_FreeGC(htPtr->display, htPtr->drawGC);
1756 }
1757 if (htPtr->fillGC != NULL) {
1758 Tk_FreeGC(htPtr->display, htPtr->fillGC);
1759 }
1760 if (htPtr->tile != NULL) {
1761 Blt_FreeTile(htPtr->tile);
1762 }
1763 if (htPtr->selectGC != NULL) {
1764 Tk_FreeGC(htPtr->display, htPtr->selectGC);
1765 }
1766 FreeText(htPtr);
1767 if (htPtr->lineArr != NULL) {
1768 Blt_Free(htPtr->lineArr);
1769 }
1770 Blt_DeleteHashTable(&(htPtr->widgetTable));
1771 Blt_Free(htPtr);
1772 }
1773
1774 /*
1775 * --------------------------------------------------------------
1776 *
1777 * TextEventProc --
1778 *
1779 * This procedure is invoked by the Tk dispatcher for various
1780 * events on hypertext widgets.
1781 *
1782 * Results:
1783 * None.
1784 *
1785 * Side effects:
1786 * When the window gets deleted, internal structures get
1787 * cleaned up. When it gets exposed, it is redisplayed.
1788 *
1789 * --------------------------------------------------------------
1790 */
1791 static void
1792 TextEventProc(clientData, eventPtr)
1793 ClientData clientData; /* Information about window. */
1794 XEvent *eventPtr; /* Information about event. */
1795 {
1796 HText *htPtr = clientData;
1797
1798 if (eventPtr->type == ConfigureNotify) {
1799 if ((htPtr->lastWidth != Tk_Width(htPtr->tkwin)) ||
1800 (htPtr->lastHeight != Tk_Height(htPtr->tkwin))) {
1801 htPtr->flags |= (REQUEST_LAYOUT | TEXT_DIRTY);
1802 EventuallyRedraw(htPtr);
1803 }
1804 } else if (eventPtr->type == Expose) {
1805
1806 /*
1807 * If the Expose event was synthetic (i.e. we manufactured it
1808 * ourselves during a redraw operation), toggle the bit flag
1809 * which controls redraws.
1810 */
1811
1812 if (eventPtr->xexpose.send_event) {
1813 htPtr->flags ^= IGNORE_EXPOSURES;
1814 return;
1815 }
1816 if ((eventPtr->xexpose.count == 0) &&
1817 !(htPtr->flags & IGNORE_EXPOSURES)) {
1818 htPtr->flags |= TEXT_DIRTY;
1819 EventuallyRedraw(htPtr);
1820 }
1821 } else if (eventPtr->type == DestroyNotify) {
1822 if (htPtr->tkwin != NULL) {
1823 htPtr->tkwin = NULL;
1824 Tcl_DeleteCommandFromToken(htPtr->interp, htPtr->cmdToken);
1825 }
1826 if (htPtr->flags & REDRAW_PENDING) {
1827 Tcl_CancelIdleCall(DisplayText, htPtr);
1828 }
1829 Tcl_EventuallyFree(htPtr, DestroyText);
1830 }
1831 }
1832
1833 /*
1834 *----------------------------------------------------------------------
1835 *
1836 * TextDeleteCmdProc --
1837 *
1838 * This procedure is invoked when a widget command is deleted. If
1839 * the widget isn't already in the process of being destroyed,
1840 * this command destroys it.
1841 *
1842 * Results:
1843 * None.
1844 *
1845 * Side effects:
1846 * The widget is destroyed.
1847 *
1848 *----------------------------------------------------------------------
1849 */
1850
1851 static void
1852 TextDeleteCmdProc(clientData)
1853 ClientData clientData; /* Pointer to widget record for widget. */
1854 {
1855 HText *htPtr = clientData;
1856
1857 /*
1858 * This procedure could be invoked either because the window was
1859 * destroyed and the command was then deleted (in which case tkwin
1860 * is NULL) or because the command was deleted, and then this procedure
1861 * destroys the widget.
1862 */
1863
1864 if (htPtr->tkwin != NULL) {
1865 Tk_Window tkwin;
1866
1867 tkwin = htPtr->tkwin;
1868 htPtr->tkwin = NULL;
1869 Tk_DestroyWindow(tkwin);
1870 #ifdef ITCL_NAMESPACES
1871 Itk_SetWidgetCommand(tkwin, (Tcl_Command) NULL);
1872 #endif /* ITCL_NAMESPACES */
1873 }
1874 }
1875
1876 /*
1877 *----------------------------------------------------------------------
1878 *
1879 * TileChangedProc
1880 *
1881 * Stub for image change notifications. Since we immediately draw
1882 * the image into a pixmap, we don't care about image changes.
1883 *
1884 * It would be better if Tk checked for NULL proc pointers.
1885 *
1886 * Results:
1887 * None.
1888 *
1889 *----------------------------------------------------------------------
1890 */
1891 /*ARGSUSED*/
1892 static void
1893 TileChangedProc(clientData, tile)
1894 ClientData clientData;
1895 Blt_Tile tile; /* Not used. */
1896 {
1897 HText *htPtr = clientData;
1898
1899 if (htPtr->tkwin != NULL) {
1900 EventuallyRedraw(htPtr);
1901 }
1902 }
1903
1904 /* Configuration Procedures */
1905 static void
1906 ResetTextInfo(htPtr)
1907 HText *htPtr;
1908 {
1909 htPtr->first = 0;
1910 htPtr->last = htPtr->nLines - 1;
1911 htPtr->selFirst = htPtr->selLast = -1;
1912 htPtr->selAnchor = 0;
1913 htPtr->pendingX = htPtr->pendingY = 0;
1914 htPtr->worldWidth = htPtr->worldHeight = 0;
1915 htPtr->xOffset = htPtr->yOffset = 0;
1916 }
1917
1918 static Line *
1919 GetLastLine(htPtr)
1920 HText *htPtr;
1921 {
1922 if (htPtr->nLines == 0) {
1923 return CreateLine(htPtr);
1924 }
1925 return (htPtr->lineArr + (htPtr->nLines - 1));
1926 }
1927
1928 /*
1929 * ----------------------------------------------------------------------
1930 *
1931 * ReadNamedFile --
1932 *
1933 * Read the named file into a newly allocated buffer.
1934 *
1935 * Results:
1936 * Returns the size of the allocated buffer if the file was
1937 * read correctly. Otherwise -1 is returned and "interp->result"
1938 * will contain an error message.
1939 *
1940 * Side Effects:
1941 * If successful, the contents of "bufferPtr" will point
1942 * to the allocated buffer.
1943 *
1944 * ----------------------------------------------------------------------
1945 */
1946 static int
1947 ReadNamedFile(interp, fileName, bufferPtr)
1948 Tcl_Interp *interp;
1949 char *fileName;
1950 char **bufferPtr;
1951 {
1952 FILE *f;
1953 int nRead, fileSize;
1954 int count, bytesLeft;
1955 char *buffer;
1956 #if defined(_MSC_VER) || defined(__BORLANDC__)
1957 #define fstat _fstat
1958 #define stat _stat
1959 #ifdef _MSC_VER
1960 #define fileno _fileno
1961 #endif
1962 #endif /* _MSC_VER || __BORLANDC__ */
1963
1964 struct stat fileInfo;
1965
1966 f = fopen(fileName, "r");
1967 if (f == NULL) {
1968 Tcl_AppendResult(interp, "can't open \"", fileName,
1969 "\" for reading: ", Tcl_PosixError(interp), (char *)NULL);
1970 return -1;
1971 }
1972 if (fstat(fileno(f), &fileInfo) < 0) {
1973 Tcl_AppendResult(interp, "can't stat \"", fileName, "\": ",
1974 Tcl_PosixError(interp), (char *)NULL);
1975 fclose(f);
1976 return -1;
1977 }
1978 fileSize = fileInfo.st_size + 1;
1979 buffer = Blt_Malloc(sizeof(char) * fileSize);
1980 if (buffer == NULL) {
1981 fclose(f);
1982 return -1; /* Can't allocate memory for file buffer */
1983 }
1984 count = 0;
1985 for (bytesLeft = fileInfo.st_size; bytesLeft > 0; bytesLeft -= nRead) {
1986 nRead = fread(buffer + count, sizeof(char), bytesLeft, f);
1987 if (nRead < 0) {
1988 Tcl_AppendResult(interp, "error reading \"", fileName, "\": ",
1989 Tcl_PosixError(interp), (char *)NULL);
1990 fclose(f);
1991 Blt_Free(buffer);
1992 return -1;
1993 } else if (nRead == 0) {
1994 break;
1995 }
1996 count += nRead;
1997 }
1998 fclose(f);
1999 buffer[count] = '\0';
2000 *bufferPtr = buffer;
2001 return count;
2002 }
2003
2004 /*
2005 * ----------------------------------------------------------------------
2006 *
2007 * CollectCommand --
2008 *
2009 * Collect the characters representing a Tcl command into a
2010 * given buffer.
2011 *
2012 * Results:
2013 * Returns the number of bytes examined. If an error occurred,
2014 * -1 is returned and "interp->result" will contain an error
2015 * message.
2016 *
2017 * Side Effects:
2018 * If successful, the "cmdArr" will be filled with the string
2019 * representing the Tcl command.
2020 *
2021 * ----------------------------------------------------------------------
2022 */
2023
2024 static int
2025 CollectCommand(htPtr, inputArr, maxBytes, cmdArr)
2026 HText *htPtr; /* Widget record */
2027 char inputArr[]; /* Array of bytes representing the htext input */
2028 int maxBytes; /* Maximum number of bytes left in input */
2029 char cmdArr[]; /* Output buffer to be filled with the Tcl
2030 * command */
2031 {
2032 int c;
2033 int i;
2034 int state, count;
2035
2036 /* Simply collect the all the characters until %% into a buffer */
2037
2038 state = count = 0;
2039 for (i = 0; i < maxBytes; i++) {
2040 c = inputArr[i];
2041 if (c == htPtr->specChar) {
2042 state++;
2043 } else if ((state == 0) && (c == '\\')) {
2044 state = 3;
2045 } else {
2046 state = 0;
2047 }
2048 switch (state) {
2049 case 2: /* End of command block found */
2050 cmdArr[count - 1] = '\0';
2051 return i;
2052
2053 case 4: /* Escaped block designator */
2054 cmdArr[count] = c;
2055 state = 0;
2056 break;
2057
2058 default: /* Add to command buffer */
2059 cmdArr[count++] = c;
2060 break;
2061 }
2062 }
2063 Tcl_AppendResult(htPtr->interp, "premature end of TCL command block",
2064 (char *)NULL);
2065 return -1;
2066 }
2067
2068 /*
2069 * ----------------------------------------------------------------------
2070 *
2071 * ParseInput --
2072 *
2073 * Parse the input to the HText structure into an array of lines.
2074 * Each entry contains the beginning index and end index of the
2075 * characters in the text array which comprise the line.
2076 *
2077 * |*|*|*|\n|T|h|i|s| |a| |l|i|n|e| |o|f| |t|e|x|t|.|\n|*|*|*|
2078 * ^ ^
2079 * textStart textEnd
2080 *
2081 * Note that the end index contains the '\n'.
2082 *
2083 * Results:
2084 * Returns TCL_OK or error depending if the file was read correctly.
2085 *
2086 * ----------------------------------------------------------------------
2087 */
2088 static int
2089 ParseInput(interp, htPtr, input, nBytes)
2090 Tcl_Interp *interp;
2091 HText *htPtr;
2092 char input[];
2093 int nBytes;
2094 {
2095 int c;
2096 int i;
2097 char *textArr;
2098 char *cmdArr;
2099 int count, nLines;
2100 int length;
2101 int state;
2102 Line *linePtr;
2103
2104 linePtr = CreateLine(htPtr);
2105 if (linePtr == NULL) {
2106 return TCL_ERROR; /* Error allocating the line structure */
2107 }
2108 /* Right now, we replace the text array instead of appending to it */
2109
2110 linePtr->textStart = 0;
2111
2112 /* In the worst case, assume the entire input could be Tcl commands */
2113 cmdArr = Blt_Malloc(sizeof(char) * (nBytes + 1));
2114
2115 textArr = Blt_Malloc(sizeof(char) * (nBytes + 1));
2116 if (htPtr->charArr != NULL) {
2117 Blt_Free(htPtr->charArr);
2118 }
2119 htPtr->charArr = textArr;
2120 htPtr->nChars = 0;
2121
2122 nLines = count = state = 0;
2123 htPtr->flags &= ~WIDGET_APPENDED;
2124
2125 for (i = 0; i < nBytes; i++) {
2126 c = input[i];
2127 if (c == htPtr->specChar) {
2128 state++;
2129 } else if (c == '\n') {
2130 state = -1;
2131 } else if ((state == 0) && (c == '\\')) {
2132 state = 3;
2133 } else {
2134 state = 0;
2135 }
2136 switch (state) {
2137 case 2: /* Block of Tcl commands found */
2138 count--, i++;
2139 length = CollectCommand(htPtr, input + i, nBytes - i, cmdArr);
2140 if (length < 0) {
2141 goto error;
2142 }
2143 i += length;
2144 linePtr->textEnd = count;
2145 htPtr->nChars = count + 1;
2146 if (Tcl_Eval(interp, cmdArr) != TCL_OK) {
2147 goto error;
2148 }
2149 if (htPtr->flags & WIDGET_APPENDED) {
2150 /* Indicates the location a embedded widget in the text array */
2151 textArr[count++] = ' ';
2152 htPtr->flags &= ~WIDGET_APPENDED;
2153 }
2154 state = 0;
2155 break;
2156
2157 case 4: /* Escaped block designator */
2158 textArr[count - 1] = c;
2159 state = 0;
2160 break;
2161
2162 case -1: /* End of line or input */
2163 linePtr->textEnd = count;
2164 textArr[count++] = '\n';
2165 nLines++;
2166 linePtr = CreateLine(htPtr);
2167 if (linePtr == NULL) {
2168 goto error;
2169 }
2170 linePtr->textStart = count;
2171 state = 0;
2172 break;
2173
2174 default: /* Default action, add to text buffer */
2175 textArr[count++] = c;
2176 break;
2177 }
2178 }
2179 if (count > linePtr->textStart) {
2180 linePtr->textEnd = count;
2181 textArr[count++] = '\n';/* Every line must end with a '\n' */
2182 nLines++;
2183 }
2184 Blt_Free(cmdArr);
2185 /* Reset number of lines allocated */
2186 if (ResizeArray((char **)&(htPtr->lineArr), sizeof(Line), nLines,
2187 htPtr->arraySize) != TCL_OK) {
2188 Tcl_AppendResult(interp, "can't reallocate array of lines", (char *)NULL);
2189 return TCL_ERROR;
2190 }
2191 htPtr->nLines = htPtr->arraySize = nLines;
2192 /* and the size of the character array */
2193 if (ResizeArray(&(htPtr->charArr), sizeof(char), count,
2194 nBytes) != TCL_OK) {
2195 Tcl_AppendResult(interp, "can't reallocate text character buffer",
2196 (char *)NULL);
2197 return TCL_ERROR;
2198 }
2199 htPtr->nChars = count;
2200 return TCL_OK;
2201 error:
2202 Blt_Free(cmdArr);
2203 return TCL_ERROR;
2204 }
2205
2206 static int
2207 IncludeText(interp, htPtr, fileName)
2208 Tcl_Interp *interp;
2209 HText *htPtr;
2210 char *fileName;
2211 {
2212 char *buffer;
2213 int result;
2214 int nBytes;
2215
2216 if ((htPtr->text == NULL) && (fileName == NULL)) {
2217 return TCL_OK; /* Empty text string */
2218 }
2219 if (fileName != NULL) {
2220 nBytes = ReadNamedFile(interp, fileName, &buffer);
2221 if (nBytes < 0) {
2222 return TCL_ERROR;
2223 }
2224 } else {
2225 buffer = htPtr->text;
2226 nBytes = strlen(htPtr->text);
2227 }
2228 result = ParseInput(interp, htPtr, buffer, nBytes);
2229 if (fileName != NULL) {
2230 Blt_Free(buffer);
2231 }
2232 return result;
2233 }
2234
2235 /* ARGSUSED */
2236 static char *
2237 TextVarProc(clientData, interp, name1, name2, flags)
2238 ClientData clientData; /* Information about widget. */
2239 Tcl_Interp *interp; /* Interpreter containing variable. */
2240 char *name1; /* Name of variable. */
2241 char *name2; /* Second part of variable name. */
2242 int flags; /* Information about what happened. */
2243 {
2244 HText *htPtr = clientData;
2245 HText *lasthtPtr;
2246
2247 /* Check to see of this is the most recent trace */
2248 lasthtPtr = (HText *)Tcl_VarTraceInfo2(interp, name1, name2, flags,
2249 TextVarProc, NULL);
2250 if (lasthtPtr != htPtr) {
2251 return NULL; /* Ignore all but most current trace */
2252 }
2253 if (flags & TCL_TRACE_READS) {
2254 char c;
2255
2256 c = name2[0];
2257 if ((c == 'w') && (strcmp(name2, "widget") == 0)) {
2258 Tcl_SetVar2(interp, name1, name2, Tk_PathName(htPtr->tkwin),
2259 flags);
2260 } else if ((c == 'l') && (strcmp(name2, "line") == 0)) {
2261 char buf[80];
2262 int lineNum;
2263
2264 lineNum = htPtr->nLines - 1;
2265 if (lineNum < 0) {
2266 lineNum = 0;
2267 }
2268 sprintf(buf, "%d", lineNum);
2269 Tcl_SetVar2(interp, name1, name2, buf, flags);
2270 } else if ((c == 'i') && (strcmp(name2, "index") == 0)) {
2271 char buf[80];
2272
2273 sprintf(buf, "%d", htPtr->nChars - 1);
2274 Tcl_SetVar2(interp, name1, name2, buf, flags);
2275 } else if ((c == 'f') && (strcmp(name2, "file") == 0)) {
2276 char *fileName;
2277
2278 fileName = htPtr->fileName;
2279 if (fileName == NULL) {
2280 fileName = "";
2281 }
2282 Tcl_SetVar2(interp, name1, name2, fileName, flags);
2283 } else {
2284 return "?unknown?";
2285 }
2286 }
2287 return NULL;
2288 }
2289
2290 static char *varNames[] =
2291 {
2292 "widget", "line", "file", "index", (char *)NULL
2293 };
2294
2295 static void
2296 CreateTraces(htPtr)
2297 HText *htPtr;
2298 {
2299 char **ptr;
2300 static char globalCmd[] = "global htext";
2301
2302 /*
2303 * Make the traced variables global to the widget
2304 */
2305 Tcl_Eval(htPtr->interp, globalCmd);
2306 for (ptr = varNames; *ptr != NULL; ptr++) {
2307 Tcl_TraceVar2(htPtr->interp, "htext", *ptr,
2308 (TCL_GLOBAL_ONLY | TCL_TRACE_READS), TextVarProc, htPtr);
2309 }
2310 }
2311
2312 static void
2313 DeleteTraces(htPtr)
2314 HText *htPtr;
2315 {
2316 char **ptr;
2317
2318 for (ptr = varNames; *ptr != NULL; ptr++) {
2319 Tcl_UntraceVar2(htPtr->interp, "htext", *ptr,
2320 (TCL_GLOBAL_ONLY | TCL_TRACE_READS), TextVarProc, htPtr);
2321 }
2322 }
2323
2324 /*
2325 * ----------------------------------------------------------------------
2326 *
2327 * ConfigureText --
2328 *
2329 * This procedure is called to process an argv/argc list, plus
2330 * the Tk option database, in order to configure (or reconfigure)
2331 * a hypertext widget.
2332 *
2333 * The layout of the text must be calculated (by ComputeLayout)
2334 * whenever particular options change; -font, -file, -linespacing
2335 * and -text options. If the user has changes one of these options,
2336 * it must be detected so that the layout can be recomputed. Since the
2337 * coordinates of the layout are virtual, there is no need to adjust
2338 * them if physical window attributes (window size, etc.)
2339 * change.
2340 *
2341 * Results:
2342 * The return value is a standard Tcl result. If TCL_ERROR is
2343 * returned, then interp->result contains an error message.
2344 *
2345 * Side effects:
2346 * Configuration information, such as text string, colors, font,
2347 * etc. get set for htPtr; old resources get freed, if there were any.
2348 * The hypertext is redisplayed.
2349 *
2350 * ----------------------------------------------------------------------
2351 */
2352 static int
2353 ConfigureText(interp, htPtr)
2354 Tcl_Interp *interp; /* Used for error reporting. */
2355 HText *htPtr; /* Information about widget; may or may not
2356 * already have values for some fields. */
2357 {
2358 XGCValues gcValues;
2359 unsigned long gcMask;
2360 GC newGC;
2361
2362 if (Blt_ConfigModified(configSpecs, interp, "-font", "-linespacing", "-file",
2363 "-text", "-width", "-height", (char *)NULL)) {
2364 /*
2365 * These options change the layout of the text. Width/height
2366 * and rows/columns may change a relatively sized window or cavity.
2367 */
2368 htPtr->flags |= (REQUEST_LAYOUT | TEXT_DIRTY); /* Mark for update */
2369 }
2370 gcMask = GCForeground | GCFont;
2371 gcValues.font = Tk_FontId(htPtr->font);
2372 gcValues.foreground = htPtr->normalFg->pixel;
2373 newGC = Tk_GetGC(htPtr->tkwin, gcMask, &gcValues);
2374 if (htPtr->drawGC != NULL) {
2375 Tk_FreeGC(htPtr->display, htPtr->drawGC);
2376 }
2377 htPtr->drawGC = newGC;
2378
2379 gcValues.foreground = htPtr->selFgColor->pixel;
2380 newGC = Tk_GetGC(htPtr->tkwin, gcMask, &gcValues);
2381 if (htPtr->selectGC != NULL) {
2382 Tk_FreeGC(htPtr->display, htPtr->selectGC);
2383 }
2384 htPtr->selectGC = newGC;
2385
2386 if (htPtr->xScrollUnits < 1) {
2387 htPtr->xScrollUnits = 1;
2388 }
2389 if (htPtr->yScrollUnits < 1) {
2390 htPtr->yScrollUnits = 1;
2391 }
2392 if (htPtr->tile != NULL) {
2393 Blt_SetTileChangedProc(htPtr->tile, TileChangedProc, htPtr);
2394 }
2395 gcValues.foreground = htPtr->normalBg->pixel;
2396 newGC = Tk_GetGC(htPtr->tkwin, gcMask, &gcValues);
2397 if (htPtr->fillGC != NULL) {
2398 Tk_FreeGC(htPtr->display, htPtr->fillGC);
2399 }
2400 htPtr->fillGC = newGC;
2401
2402 if (htPtr->nColumns > 0) {
2403 htPtr->reqWidth =
2404 htPtr->nColumns * Tk_TextWidth(htPtr->font, "0", 1);
2405 }
2406 if (htPtr->nRows > 0) {
2407 Tk_FontMetrics fontMetrics;
2408
2409 Tk_GetFontMetrics(htPtr->font, &fontMetrics);
2410 htPtr->reqHeight = htPtr->nRows * fontMetrics.linespace;
2411 }
2412 /*
2413 * If the either the -text or -file option changed, read in the
2414 * new text. The -text option supersedes any -file option.
2415 */
2416 if (Blt_ConfigModified(configSpecs, interp, "-file", "-text", (char *)NULL)) {
2417 int result;
2418
2419 FreeText(htPtr);
2420 CreateTraces(htPtr); /* Create variable traces */
2421
2422 result = IncludeText(interp, htPtr, htPtr->fileName);
2423
2424 DeleteTraces(htPtr);
2425 if (result == TCL_ERROR) {
2426 FreeText(htPtr);
2427 return TCL_ERROR;
2428 }
2429 ResetTextInfo(htPtr);
2430 }
2431 EventuallyRedraw(htPtr);
2432 return TCL_OK;
2433 }
2434 static void
2435 widgetWorldChanged(ClientData clientData)
2436 {
2437 HText *htPtr = (HText *)clientData;
2438 htPtr->flags |= (TEXT_DIRTY | REQUEST_LAYOUT);
2439 ConfigureText(htPtr->interp, htPtr);
2440 EventuallyRedraw(htPtr);
2441 }
2442
2443 /* Layout Procedures */
2444 /*
2445 * -----------------------------------------------------------------
2446 *
2447 * TranslateAnchor --
2448 *
2449 * Translate the coordinates of a given bounding box based
2450 * upon the anchor specified. The anchor indicates where
2451 * the given xy position is in relation to the bounding box.
2452 *
2453 * nw --- n --- ne
2454 * | | x,y ---+
2455 * w center e | |
2456 * | | +-----+
2457 * sw --- s --- se
2458 *
2459 * Results:
2460 * The translated coordinates of the bounding box are returned.
2461 *
2462 * -----------------------------------------------------------------
2463 */
2464 static XPoint
2465 TranslateAnchor(deltaX, deltaY, anchor)
2466 int deltaX, deltaY; /* Difference between outer and inner regions
2467 */
2468 Tk_Anchor anchor; /* Direction of the anchor */
2469 {
2470 XPoint point;
2471
2472 point.x = point.y = 0;
2473 switch (anchor) {
2474 case TK_ANCHOR_NW: /* Upper left corner */
2475 break;
2476 case TK_ANCHOR_W: /* Left center */
2477 point.y = (deltaY / 2);
2478 break;
2479 case TK_ANCHOR_SW: /* Lower left corner */
2480 point.y = deltaY;
2481 break;
2482 case TK_ANCHOR_N: /* Top center */
2483 point.x = (deltaX / 2);
2484 break;
2485 case TK_ANCHOR_CENTER: /* Centered */
2486 point.x = (deltaX / 2);
2487 point.y = (deltaY / 2);
2488 break;
2489 case TK_ANCHOR_S: /* Bottom center */
2490 point.x = (deltaX / 2);
2491 point.y = deltaY;
2492 break;
2493 case TK_ANCHOR_NE: /* Upper right corner */
2494 point.x = deltaX;
2495 break;
2496 case TK_ANCHOR_E: /* Right center */
2497 point.x = deltaX;
2498 point.y = (deltaY / 2);
2499 break;
2500 case TK_ANCHOR_SE: /* Lower right corner */
2501 point.x = deltaX;
2502 point.y = deltaY;
2503 break;
2504 }
2505 return point;
2506 }
2507
2508 /*
2509 *----------------------------------------------------------------------
2510 *
2511 * ComputeCavitySize --
2512 *
2513 * Sets the width and height of the cavity based upon the
2514 * requested size of the embedded widget. The requested space also
2515 * includes any external padding which has been designated for
2516 * this window.
2517 *
2518 * Results:
2519 * None.
2520 *
2521 * Side Effects:
2522 * The size of the cavity is set in the embedded widget information
2523 * structure. These values can effect how the embedded widget is
2524 * packed into the master window.
2525 *
2526 *----------------------------------------------------------------------
2527 */
2528 static void
2529 ComputeCavitySize(winPtr)
2530 EmbeddedWidget *winPtr;
2531 {
2532 int width, height;
2533 int twiceBW;
2534
2535 twiceBW = 2 * Tk_Changes(winPtr->tkwin)->border_width;
2536 if (winPtr->reqCavityWidth > 0) {
2537 width = winPtr->reqCavityWidth;
2538 } else if (winPtr->relCavityWidth > 0.0) {
2539 width = (int)((double)Tk_Width(winPtr->htPtr->tkwin) *
2540 winPtr->relCavityWidth + 0.5);
2541 } else {
2542 width = GetEmbeddedWidgetWidth(winPtr) + PADDING(winPtr->padX) +
2543 twiceBW;
2544 }
2545 winPtr->cavityWidth = width;
2546
2547 if (winPtr->reqCavityHeight > 0) {
2548 height = winPtr->reqCavityHeight;
2549 } else if (winPtr->relCavityHeight > 0.0) {
2550 height = (int)((double)Tk_Height(winPtr->htPtr->tkwin) *
2551 winPtr->relCavityHeight + 0.5);
2552 } else {
2553 height = GetEmbeddedWidgetHeight(winPtr) + PADDING(winPtr->padY) +
2554 twiceBW;
2555 }
2556 winPtr->cavityHeight = height;
2557 }
2558
2559 /*
2560 *----------------------------------------------------------------------
2561 *
2562 * LayoutLine --
2563 *
2564 * This procedure computes the total width and height needed
2565 * to contain the text and widgets for a particular line.
2566 * It also calculates the baseline of the text on the line with
2567 * respect to the other widgets on the line.
2568 *
2569 * Results:
2570 * None.
2571 *
2572 *----------------------------------------------------------------------
2573 */
2574 static void
2575 LayoutLine(htPtr, linePtr)
2576 HText *htPtr;
2577 Line *linePtr;
2578 {
2579 EmbeddedWidget *winPtr;
2580 int textStart, textLength;
2581 int maxAscent, maxDescent, maxHeight;
2582 int ascent, descent;
2583 int median; /* Difference of font ascent/descent values */
2584 Blt_ChainLink *linkPtr;
2585 int x, y;
2586 int newX;
2587 Tk_FontMetrics fontMetrics;
2588
2589 /* Initialize line defaults */
2590 Tk_GetFontMetrics(htPtr->font, &fontMetrics);
2591 maxAscent = fontMetrics.ascent;
2592 maxDescent = fontMetrics.descent;
2593 median = fontMetrics.ascent - fontMetrics.descent;
2594 ascent = descent = 0; /* Suppress compiler warnings */
2595
2596 /*
2597 * Pass 1: Determine the maximum ascent (baseline) and descent
2598 * needed for the line. We'll need this for figuring the top,
2599 * bottom, and center anchors.
2600 */
2601 for (linkPtr = Blt_ChainFirstLink(linePtr->chainPtr); linkPtr != NULL;
2602 linkPtr = Blt_ChainNextLink(linkPtr)) {
2603 winPtr = Blt_ChainGetValue(linkPtr);
2604 if (winPtr->tkwin == NULL) {
2605 continue;
2606 }
2607 ComputeCavitySize(winPtr);
2608
2609 switch (winPtr->justify) {
2610 case JUSTIFY_TOP:
2611 ascent = fontMetrics.ascent + winPtr->padTop;
2612 descent = winPtr->cavityHeight - fontMetrics.ascent;
2613 break;
2614 case JUSTIFY_CENTER:
2615 ascent = (winPtr->cavityHeight + median) / 2;
2616 descent = (winPtr->cavityHeight - median) / 2;
2617 break;
2618 case JUSTIFY_BOTTOM:
2619 ascent = winPtr->cavityHeight - fontMetrics.descent;
2620 descent = fontMetrics.descent;
2621 break;
2622 }
2623 if (descent > maxDescent) {
2624 maxDescent = descent;
2625 }
2626 if (ascent > maxAscent) {
2627 maxAscent = ascent;
2628 }
2629 }
2630
2631 maxHeight = maxAscent + maxDescent + htPtr->leader;
2632 x = 0; /* Always starts from x=0 */
2633 y = 0; /* Suppress compiler warning */
2634 textStart = linePtr->textStart;
2635
2636 /*
2637 * Pass 2: Find the placements of the text and widgets along each
2638 * line.
2639 */
2640 for (linkPtr = Blt_ChainFirstLink(linePtr->chainPtr); linkPtr != NULL;
2641 linkPtr = Blt_ChainNextLink(linkPtr)) {
2642 winPtr = Blt_ChainGetValue(linkPtr);
2643 if (winPtr->tkwin == NULL) {
2644 continue;
2645 }
2646 /* Get the width of the text leading to the widget. */
2647 textLength = (winPtr->precedingTextEnd - textStart);
2648 if (textLength > 0) {
2649 Tk_MeasureChars(htPtr->font, htPtr->charArr + textStart,
2650 textLength, 10000, TK_AT_LEAST_ONE, &newX);
2651 winPtr->precedingTextWidth = newX;
2652 x += newX;
2653 }
2654 switch (winPtr->justify) {
2655 case JUSTIFY_TOP:
2656 y = maxAscent - fontMetrics.ascent;
2657 break;
2658 case JUSTIFY_CENTER:
2659 y = maxAscent - (winPtr->cavityHeight + median) / 2;
2660 break;
2661 case JUSTIFY_BOTTOM:
2662 y = maxAscent + fontMetrics.descent - winPtr->cavityHeight;
2663 break;
2664 }
2665 winPtr->x = x, winPtr->y = y;
2666
2667 /* Skip over trailing space */
2668 textStart = winPtr->precedingTextEnd + 1;
2669
2670 x += winPtr->cavityWidth;
2671 }
2672
2673 /*
2674 * This can be either the trailing piece of a line after the last widget
2675 * or the entire line if no widgets are embedded in it.
2676 */
2677 textLength = (linePtr->textEnd - textStart) + 1;
2678 if (textLength > 0) {
2679 Tk_MeasureChars(htPtr->font, htPtr->charArr + textStart,
2680 textLength, 10000, DEF_TEXT_FLAGS, &newX);
2681 x += newX;
2682 }
2683 /* Update line parameters */
2684 if ((linePtr->width != x) || (linePtr->height != maxHeight) ||
2685 (linePtr->baseline != maxAscent)) {
2686 htPtr->flags |= TEXT_DIRTY;
2687 }
2688 linePtr->width = x;
2689 linePtr->height = maxHeight;
2690 linePtr->baseline = maxAscent;
2691 }
2692
2693 /*
2694 *----------------------------------------------------------------------
2695 *
2696 * ComputeLayout --
2697 *
2698 * This procedure computes the total width and height needed
2699 * to contain the text and widgets from all the lines of text.
2700 * It merely sums the heights and finds the maximum width of
2701 * all the lines. The width and height are needed for scrolling.
2702 *
2703 * Results:
2704 * None.
2705 *
2706 *----------------------------------------------------------------------
2707 */
2708 static void
2709 ComputeLayout(htPtr)
2710 HText *htPtr;
2711 {
2712 int count;
2713 Line *linePtr;
2714 int height, width;
2715
2716 width = height = 0;
2717 for (count = 0; count < htPtr->nLines; count++) {
2718 linePtr = htPtr->lineArr + count;
2719
2720 linePtr->offset = height;
2721 LayoutLine(htPtr, linePtr);
2722 height += linePtr->height;
2723 if (linePtr->width > width) {
2724 width = linePtr->width;
2725 }
2726 }
2727 /*
2728 * Set changed flag if new layout changed size of virtual text.
2729 */
2730 if ((height != htPtr->worldHeight) || (width != htPtr->worldWidth)) {
2731 htPtr->worldHeight = height, htPtr->worldWidth = width;
2732 htPtr->flags |= TEXT_DIRTY;
2733 }
2734 }
2735
2736 /* Display Procedures */
2737 /*
2738 * ----------------------------------------------------------------------
2739 *
2740 * GetVisibleLines --
2741 *
2742 * Calculates which lines are visible using the height
2743 * of the viewport and y offset from the top of the text.
2744 *
2745 * Results:
2746 * None.
2747 *
2748 * Side effects:
2749 * Only those line between first and last inclusive are
2750 * redrawn.
2751 *
2752 * ----------------------------------------------------------------------
2753 */
2754 static int
2755 GetVisibleLines(htPtr)
2756 HText *htPtr;
2757 {
2758 int topLine, bottomLine;
2759 int firstY, lastY;
2760 int lastLine;
2761
2762 if (htPtr->nLines == 0) {
2763 htPtr->first = 0;
2764 htPtr->last = -1;
2765 return TCL_OK;
2766 }
2767 firstY = htPtr->pendingY;
2768 lastLine = htPtr->nLines - 1;
2769
2770 /* First line */
2771 topLine = LineSearch(htPtr, firstY, 0, lastLine);
2772 if (topLine < 0) {
2773 /*
2774 * This can't be. The y-coordinate offset must be corrupted.
2775 */
2776 fprintf(stderr, "internal error: First position not found `%d'\n",
2777 firstY);
2778 return TCL_ERROR;
2779 }
2780 htPtr->first = topLine;
2781
2782 /*
2783 * If there is less text than window space, the bottom line is the
2784 * last line of text. Otherwise search for the line at the bottom
2785 * of the window.
2786 */
2787 lastY = firstY + Tk_Height(htPtr->tkwin) - 1;
2788 if (lastY < htPtr->worldHeight) {
2789 bottomLine = LineSearch(htPtr, lastY, topLine, lastLine);
2790 } else {
2791 bottomLine = lastLine;
2792 }
2793 if (bottomLine < 0) {
2794 /*
2795 * This can't be. The newY offset must be corrupted.
2796 */
2797 fprintf(stderr, "internal error: Last position not found `%d'\n",
2798 lastY);
2799 #ifdef notdef
2800 fprintf(stderr, "worldHeight=%d,height=%d,top=%d,first=%d,last=%d\n",
2801 htPtr->worldHeight, Tk_Height(htPtr->tkwin), firstY,
2802 htPtr->lineArr[topLine].offset, htPtr->lineArr[lastLine].offset);
2803 #endif
2804 return TCL_ERROR;
2805 }
2806 htPtr->last = bottomLine;
2807 return TCL_OK;
2808 }
2809
2810 /*
2811 * ----------------------------------------------------------------------
2812 *
2813 * DrawSegment --
2814 *
2815 * Draws a line segment, designated by the segment structure.
2816 * This routine handles the display of selected text by drawing
2817 * a raised 3D border underneath the selected text.
2818 *
2819 * Results:
2820 * None.
2821 *
2822 * Side effects:
2823 * The line segment is drawn on *draw*.
2824 *
2825 * ----------------------------------------------------------------------
2826 */
2827 static void
2828 DrawSegment(htPtr, draw, linePtr, x, y, segPtr)
2829 HText *htPtr;
2830 Drawable draw;
2831 Line *linePtr;
2832 int x, y;
2833 Segment *segPtr;
2834 {
2835 int lastX, curPos, nChars;
2836 int textLength;
2837 int selStart, selEnd, selLength;
2838 Tk_FontMetrics fontMetrics;
2839
2840 #ifdef notdef
2841 fprintf(stderr, "DS select: first=%d,last=%d text: first=%d,last=%d\n",
2842 htPtr->selFirst, htPtr->selLast, segPtr->textStart, segPtr->textEnd);
2843 #endif
2844 textLength = (segPtr->textEnd - segPtr->textStart) + 1;
2845 if (textLength < 1) {
2846 return;
2847 }
2848 Tk_GetFontMetrics(htPtr->font, &fontMetrics);
2849 if ((segPtr->textEnd < htPtr->selFirst) ||
2850 (segPtr->textStart > htPtr->selLast)) { /* No selected text */
•2851 #ifdef TK_DRAWCHARS_ANLGE
2852 Tk_DrawChars(htPtr->display, draw, htPtr->drawGC, htPtr->font,
2853 htPtr->charArr + segPtr->textStart, textLength - 1,
2854 x, y + linePtr->baseline, 0.);
2855 #else
2856 Tk_DrawChars(htPtr->display, draw, htPtr->drawGC, htPtr->font,
2857 htPtr->charArr + segPtr->textStart, textLength - 1,
2858 x, y + linePtr->baseline);
•2859 #endif
2860 return;
2861 }
2862 /*
2863 * Text in a segment (with selected text) may have
2864 * up to three regions:
2865 *
2866 * 1) the text before the start the selection
2867 * 2) the selected text itself (drawn in a raised border)
2868 * 3) the text following the selection.
2869 */
2870
2871 selStart = segPtr->textStart;
2872 selEnd = segPtr->textEnd;
2873 if (htPtr->selFirst > segPtr->textStart) {
2874 selStart = htPtr->selFirst;
2875 }
2876 if (htPtr->selLast < segPtr->textEnd) {
2877 selEnd = htPtr->selLast;
2878 }
2879 selLength = (selEnd - selStart) + 1;
2880 lastX = x;
2881 curPos = segPtr->textStart;
2882
2883 if (selStart > segPtr->textStart) { /* Text preceding selection */
2884 nChars = (selStart - segPtr->textStart);
2885 Tk_MeasureChars(htPtr->font, htPtr->charArr + segPtr->textStart,
2886 nChars, 10000, DEF_TEXT_FLAGS, &lastX);
2887 lastX += x;
•2888 #ifdef TK_DRAWCHARS_ANGLE
2889 Tk_DrawChars(htPtr->display, draw, htPtr->drawGC, htPtr->font,
2890 htPtr->charArr + segPtr->textStart, nChars, x,
2891 y + linePtr->baseline, 0.);
2892 #else
2893 Tk_DrawChars(htPtr->display, draw, htPtr->drawGC, htPtr->font,
2894 htPtr->charArr + segPtr->textStart, nChars, x,
2895 y + linePtr->baseline);
•2896 #endif
2897 curPos = selStart;
2898 }
2899 if (selLength > 0) { /* The selection itself */
2900 int width, nextX;
2901
2902 Tk_MeasureChars(htPtr->font, htPtr->charArr + selStart,
2903 selLength, 10000, DEF_TEXT_FLAGS, &nextX);
2904 nextX += x;
2905 width = (selEnd == linePtr->textEnd)
2906 ? htPtr->worldWidth - htPtr->xOffset - lastX :
2907 nextX - lastX;
2908 Blt_Fill3DRectangle(htPtr->tkwin, draw, htPtr->selBorder,
2909 lastX, y + linePtr->baseline - fontMetrics.ascent,
2910 width, fontMetrics.linespace, htPtr->selBorderWidth,
2911 TK_RELIEF_RAISED);
•2912 #ifdef TK_DRAWCHARS_ANGLE
2913 Tk_DrawChars(htPtr->display, draw, htPtr->selectGC,
2914 htPtr->font, htPtr->charArr + selStart, selLength,
2915 lastX, y + linePtr->baseline, 0.);
2916 #else
2917 Tk_DrawChars(htPtr->display, draw, htPtr->selectGC,
2918 htPtr->font, htPtr->charArr + selStart, selLength,
2919 lastX, y + linePtr->baseline);
•2920 #endif
2921 lastX = nextX;
2922 curPos = selStart + selLength;
2923 }
2924 nChars = segPtr->textEnd - curPos;
2925 if (nChars > 0) { /* Text following the selection */
•2926 #ifdef TK_DRAWCHARS_ANGLE
2927 Tk_DrawChars(htPtr->display, draw, htPtr->drawGC, htPtr->font,
2928 htPtr->charArr + curPos, nChars - 1,
2929 lastX, y + linePtr->baseline, 0.);
2930 #else
2931 Tk_DrawChars(htPtr->display, draw, htPtr->drawGC, htPtr->font,
2932 htPtr->charArr + curPos, nChars - 1, lastX, y + linePtr->baseline);
•2933 #endif
2934 }
2935 }
2936
2937 /*
2938 * ----------------------------------------------------------------------
2939 *
2940 * MoveEmbeddedWidget --
2941 *
2942 * Move a embedded widget to a new location in the hypertext
2943 * parent window. If the window has no geometry (i.e. width,
2944 * or height is 0), simply unmap to window.
2945 *
2946 * Results:
2947 * None.
2948 *
2949 * Side effects:
2950 * Each embedded widget is moved to its new location, generating
2951 * Expose events in the parent for each embedded widget moved.
2952 *
2953 * ----------------------------------------------------------------------
2954 */
2955 static void
2956 MoveEmbeddedWidget(winPtr, offset)
2957 EmbeddedWidget *winPtr;
2958 int offset;
2959 {
2960 int winWidth, winHeight;
2961 int width, height;
2962 int deltaX, deltaY;
2963 int x, y;
2964 int intBW;
2965
2966 winWidth = GetEmbeddedWidgetWidth(winPtr);
2967 winHeight = GetEmbeddedWidgetHeight(winPtr);
2968 if ((winWidth < 1) || (winHeight < 1)) {
2969 if (Tk_IsMapped(winPtr->tkwin)) {
2970 Tk_UnmapWindow(winPtr->tkwin);
2971 }
2972 return;
2973 }
2974 intBW = Tk_Changes(winPtr->tkwin)->border_width;
2975 x = (winPtr->x + intBW + winPtr->padLeft) -
2976 winPtr->htPtr->xOffset;
2977 y = offset + (winPtr->y + intBW + winPtr->padTop) -
2978 winPtr->htPtr->yOffset;
2979
2980 width = winPtr->cavityWidth - (2 * intBW + PADDING(winPtr->padX));
2981 if (width < 0) {
2982 width = 0;
2983 }
2984 if ((width < winWidth) || (winPtr->fill & FILL_X)) {
2985 winWidth = width;
2986 }
2987 deltaX = width - winWidth;
2988
2989 height = winPtr->cavityHeight - (2 * intBW + PADDING(winPtr->padY));
2990 if (height < 0) {
2991 height = 0;
2992 }
2993 if ((height < winHeight) || (winPtr->fill & FILL_Y)) {
2994 winHeight = height;
2995 }
2996 deltaY = height - winHeight;
2997
2998 if ((deltaX > 0) || (deltaY > 0)) {
2999 XPoint point;
3000
3001 point = TranslateAnchor(deltaX, deltaY, winPtr->anchor);
3002 x += point.x, y += point.y;
3003 }
3004 winPtr->winWidth = winWidth;
3005 winPtr->winHeight = winHeight;
3006
3007 if ((x != Tk_X(winPtr->tkwin)) || (y != Tk_Y(winPtr->tkwin)) ||
3008 (winWidth != Tk_Width(winPtr->tkwin)) ||
3009 (winHeight != Tk_Height(winPtr->tkwin))) {
3010 Tk_MoveResizeWindow(winPtr->tkwin, x, y, winWidth, winHeight);
3011 }
3012 if (!Tk_IsMapped(winPtr->tkwin)) {
3013 Tk_MapWindow(winPtr->tkwin);
3014 }
3015 }
3016
3017 /*
3018 * ----------------------------------------------------------------------
3019 *
3020 * DrawPage --
3021 *
3022 * This procedure displays the lines of text and moves the widgets
3023 * to their new positions. It draws lines with regard to
3024 * the direction of the scrolling. The idea here is to make the
3025 * text and buttons appear to move together. Otherwise you will
3026 * get a "jiggling" effect where the windows appear to bump into
3027 * the next line before that line is moved. In the worst case, where
3028 * every line has at least one widget, you can get an aquarium effect
3029 * (lines appear to ripple up).
3030 *
3031 * The text area may start between line boundaries (to accommodate
3032 * both variable height lines and constant scrolling). Subtract the
3033 * difference of the page offset and the line offset from the starting
3034 * coordinates. For horizontal scrolling, simply subtract the offset
3035 * of the viewport. The window will clip the top of the first line,
3036 * the bottom of the last line, whatever text extends to the left
3037 * or right of the viewport on any line.
3038 *
3039 * Results:
3040 * None.
3041 *
3042 * Side effects:
3043 * Commands are output to X to display the line in its current
3044 * mode.
3045 *
3046 * ----------------------------------------------------------------------
3047 */
3048 static void
3049 DrawPage(htPtr, deltaY)
3050 HText *htPtr;
3051 int deltaY; /* Change from previous Y coordinate */
3052 {
3053 Line *linePtr;
3054 EmbeddedWidget *winPtr;
3055 Tk_Window tkwin = htPtr->tkwin;
3056 Segment sgmt;
3057 Pixmap pixmap;
3058 int forceCopy;
3059 int i;
3060 int lineNum;
3061 int x, y, lastY;
3062 Blt_ChainLink *linkPtr;
3063 int width, height;
3064 Display *display;
3065
3066 display = htPtr->display;
3067 width = Tk_Width(tkwin);
3068 height = Tk_Height(tkwin);
3069
3070 /* Create an off-screen pixmap for semi-smooth scrolling. */
3071 pixmap = Tk_GetPixmap(display, Tk_WindowId(tkwin), width, height,
3072 Tk_Depth(tkwin));
3073
3074 x = -(htPtr->xOffset);
3075 y = -(htPtr->yOffset);
3076
3077 if (Blt_HasTile(htPtr->tile)) {
3078 if (htPtr->tileOffsetPage) {
3079 Blt_SetTSOrigin(htPtr->tkwin, htPtr->tile, x, y);
3080 } else {
3081 Blt_SetTileOrigin(htPtr->tkwin, htPtr->tile, 0, 0);
3082 }
3083 Blt_TileRectangle(htPtr->tkwin, pixmap, htPtr->tile, 0, 0, width,
3084 height);
3085 } else {
3086 XFillRectangle(display, pixmap, htPtr->fillGC, 0, 0, width, height);
3087 }
3088
•3089 /* handle null lineArr */
3090 if (htPtr->arraySize > 0 && htPtr->nLines > 0) {
3091 if (deltaY >= 0) {
3092 y += htPtr->lineArr[htPtr->first].offset;
3093 lineNum = htPtr->first;
3094 lastY = 0;
•3095 } else {
3096 y += htPtr->lineArr[htPtr->last].offset;
3097 lineNum = htPtr->last;
3098 lastY = height;
•3099 }
3100 forceCopy = 0;
3101 } else {
3102 htPtr->last = htPtr->first - 1;
•3103 lineNum = htPtr->first;
3104 lastY = 0;
3105 }
3106
3107 /* Draw each line */
3108 for (i = htPtr->first; i <= htPtr->last; i++) {
3109
3110 /* Initialize character position in text buffer to start */
3111 linePtr = htPtr->lineArr + lineNum;
3112 sgmt.textStart = linePtr->textStart;
3113 sgmt.textEnd = linePtr->textEnd;
3114
3115 /* Initialize X position */
3116 x = -(htPtr->xOffset);
3117 for (linkPtr = Blt_ChainFirstLink(linePtr->chainPtr); linkPtr != NULL;
3118 linkPtr = Blt_ChainNextLink(linkPtr)) {
3119 winPtr = Blt_ChainGetValue(linkPtr);
3120
3121 if (winPtr->tkwin != NULL) {
3122 winPtr->flags |= WIDGET_VISIBLE;
3123 MoveEmbeddedWidget(winPtr, linePtr->offset);
3124 }
3125 sgmt.textEnd = winPtr->precedingTextEnd - 1;
3126 if (sgmt.textEnd >= sgmt.textStart) {
3127 DrawSegment(htPtr, pixmap, linePtr, x, y, &sgmt);
3128 x += winPtr->precedingTextWidth;
3129 }
3130 /* Skip over the extra trailing space which designates the widget */
3131 sgmt.textStart = winPtr->precedingTextEnd + 1;
3132 x += winPtr->cavityWidth;
3133 forceCopy++;
3134 }
3135
3136 /*
3137 * This may be the text trailing the last widget or the entire
3138 * line if no widgets occur on it.
3139 */
3140 sgmt.textEnd = linePtr->textEnd;
3141 if (sgmt.textEnd >= sgmt.textStart) {
3142 DrawSegment(htPtr, pixmap, linePtr, x, y, &sgmt);
3143 }
3144 /* Go to the top of the next line */
3145 if (deltaY >= 0) {
3146 y += htPtr->lineArr[lineNum].height;
3147 lineNum++;
3148 }
3149 if ((forceCopy > 0) && !(htPtr->flags & TEXT_DIRTY)) {
3150 if (deltaY >= 0) {
3151 XCopyArea(display, pixmap, Tk_WindowId(tkwin), htPtr->drawGC,
3152 0, lastY, width, y - lastY, 0, lastY);
3153 } else {
3154 XCopyArea(display, pixmap, Tk_WindowId(tkwin), htPtr->drawGC,
3155 0, y, width, lastY - y, 0, y);
3156 }
3157 forceCopy = 0; /* Reset drawing flag */
3158 lastY = y; /* Record last Y position */
3159 }
3160 if ((deltaY < 0) && (lineNum > 0)) {
3161 --lineNum;
3162 y -= htPtr->lineArr[lineNum].height;
3163 }
3164 }
3165 /*
3166 * If the viewport was resized, draw the page in one operation.
3167 * Otherwise draw any left-over block of text (either at the top
3168 * or bottom of the page)
3169 */
3170 if (htPtr->flags & TEXT_DIRTY) {
3171 XCopyArea(display, pixmap, Tk_WindowId(tkwin),
3172 htPtr->drawGC, 0, 0, width, height, 0, 0);
3173 } else if (lastY != y) {
3174 if (deltaY >= 0) {
3175 height -= lastY;
3176 XCopyArea(display, pixmap, Tk_WindowId(tkwin),
3177 htPtr->drawGC, 0, lastY, width, height, 0, lastY);
3178 } else {
3179 height = lastY;
3180 XCopyArea(display, pixmap, Tk_WindowId(tkwin),
3181 htPtr->drawGC, 0, 0, width, height, 0, 0);
3182 }
3183 }
3184 Tk_FreePixmap(display, pixmap);
3185 }
3186
3187
3188 static void
3189 SendBogusEvent(tkwin)
3190 Tk_Window tkwin;
3191 {
3192 #define DONTPROPAGATE 0
3193 XEvent event;
3194
3195 event.type = event.xexpose.type = Expose;
3196 event.xexpose.window = Tk_WindowId(tkwin);
3197 event.xexpose.display = Tk_Display(tkwin);
3198 event.xexpose.count = 0;
3199 event.xexpose.x = event.xexpose.y = 0;
3200 event.xexpose.width = Tk_Width(tkwin);
3201 event.xexpose.height = Tk_Height(tkwin);
3202
3203 XSendEvent(Tk_Display(tkwin), Tk_WindowId(tkwin), DONTPROPAGATE,
3204 ExposureMask, &event);
3205 }
3206
3207 /*
3208 * ----------------------------------------------------------------------
3209 *
3210 * DisplayText --
3211 *
3212 * This procedure is invoked to display a hypertext widget.
3213 * Many of the operations which might ordinarily be performed
3214 * elsewhere (e.g. in a configuration routine) are done here
3215 * because of the somewhat unusual interactions occurring between
3216 * the parent and embedded widgets.
3217 *
3218 * Recompute the layout of the text if necessary. This is
3219 * necessary if the world coordinate system has changed.
3220 * Specifically, the following may have occurred:
3221 *
3222 * 1. a text attribute has changed (font, linespacing, etc.).
3223 * 2. widget option changed (anchor, width, height).
3224 * 3. actual embedded widget was resized.
3225 * 4. new text string or file.
3226 *
3227 * This is deferred to the display routine since potentially
3228 * many of these may occur (especially embedded widget changes).
3229 *
3230 * Set the vertical and horizontal scrollbars (if they are
3231 * designated) by issuing a Tcl command. Done here since
3232 * the text window width and height are needed.
3233 *
3234 * If the viewport position or contents have changed in the
3235 * vertical direction, the now out-of-view embedded widgets
3236 * must be moved off the viewport. Since embedded widgets will
3237 * obscure the text window, it is imperative that the widgets
3238 * are moved off before we try to redraw text in the same area.
3239 * This is necessary only for vertical movements. Horizontal
3240 * embedded widget movements are handled automatically in the
3241 * page drawing routine.
3242 *
3243 * Get the new first and last line numbers for the viewport.
3244 * These line numbers may have changed because either a)
3245 * the viewport changed size or position, or b) the text
3246 * (embedded widget sizes or text attributes) have changed.
3247 *
3248 * If the viewport has changed vertically (i.e. the first or
3249 * last line numbers have changed), move the now out-of-view
3250 * embedded widgets off the viewport.
3251 *
3252 * Potentially many expose events may be generated when the
3253 * the individual embedded widgets are moved and/or resized.
3254 * These events need to be ignored. Since (I think) expose
3255 * events are guaranteed to happen in order, we can bracket
3256 * them by sending phony events (via XSendEvent). The phony
3257 * events turn on and off flags indicating which events
3258 * should be ignored.
3259 *
3260 * Finally, the page drawing routine is called.
3261 *
3262 * Results:
3263 * None.
3264 *
3265 * Side effects:
3266 * Commands are output to X to display the hypertext in its
3267 * current mode.
3268 *
3269 * ----------------------------------------------------------------------
3270 */
3271 static void
3272 DisplayText(clientData)
3273 ClientData clientData; /* Information about widget. */
3274 {
3275 HText *htPtr = clientData;
3276 Tk_Window tkwin = htPtr->tkwin;
3277 int oldFirst; /* First line of old viewport */
3278 int oldLast; /* Last line of old viewport */
3279 int deltaY; /* Change in viewport in Y direction */
3280 int reqWidth, reqHeight;
3281
3282 #ifdef notdef
3283 fprintf(stderr, "calling DisplayText(%s)\n", Tk_PathName(htPtr->tkwin));
3284 #endif
3285 htPtr->flags &= ~REDRAW_PENDING;
3286 if (tkwin == NULL) {
3287 return; /* Window has been destroyed */
3288 }
3289 if (htPtr->flags & REQUEST_LAYOUT) {
3290 /*
3291 * Recompute the layout when widgets are created, deleted,
3292 * moved, or resized. Also when text attributes (such as
3293 * font, linespacing) have changed.
3294 */
3295 ComputeLayout(htPtr);
3296 }
3297 htPtr->lastWidth = Tk_Width(tkwin);
3298 htPtr->lastHeight = Tk_Height(tkwin);
3299
3300 /*
3301 * Check the requested width and height. We allow two modes:
3302 * 1) If the user requested value is greater than zero, use it.
3303 * 2) Otherwise, let the window be as big as the virtual text.
3304 * This could be too large to display, so constrain it by
3305 * the maxWidth and maxHeight values.
3306 *
3307 * In any event, we need to calculate the size of the virtual
3308 * text and then make a geometry request. This is so that widgets
3309 * whose size is relative to the master, will be set once.
3310 */
3311 if (htPtr->reqWidth > 0) {
3312 reqWidth = htPtr->reqWidth;
3313 } else {
3314 reqWidth = MIN(htPtr->worldWidth, htPtr->maxWidth);
3315 if (reqWidth < 1) {
3316 reqWidth = 1;
3317 }
3318 }
3319 if (htPtr->reqHeight > 0) {
3320 reqHeight = htPtr->reqHeight;
3321 } else {
3322 reqHeight = MIN(htPtr->worldHeight, htPtr->maxHeight);
3323 if (reqHeight < 1) {
3324 reqHeight = 1;
3325 }
3326 }
3327 if ((reqWidth != Tk_ReqWidth(tkwin)) || (reqHeight != Tk_ReqHeight(tkwin))) {
3328 Tk_GeometryRequest(tkwin, reqWidth, reqHeight);
3329
3330 EventuallyRedraw(htPtr);
3331 return; /* Try again with new geometry */
3332 }
3333 if (!Tk_IsMapped(tkwin)) {
3334 return;
3335 }
3336 /*
3337 * Turn off layout requests here, after the text window has been
3338 * mapped. Otherwise, relative embedded widget size requests wrt
3339 * to the size of parent text window will be wrong.
3340 */
3341 htPtr->flags &= ~REQUEST_LAYOUT;
3342
3343 /* Is there a pending goto request? */
3344 if (htPtr->flags & GOTO_PENDING) {
3345 htPtr->pendingY = htPtr->lineArr[htPtr->reqLineNum].offset;
3346 htPtr->flags &= ~GOTO_PENDING;
3347 }
3348 deltaY = htPtr->pendingY - htPtr->yOffset;
3349 oldFirst = htPtr->first, oldLast = htPtr->last;
3350
3351 /*
3352 * If the viewport has changed size or position, or the text
3353 * and/or embedded widgets have changed, adjust the scrollbars to
3354 * new positions.
3355 */
3356 if (htPtr->flags & TEXT_DIRTY) {
3357 int width, height;
3358
3359 width = Tk_Width(htPtr->tkwin);
3360 height = Tk_Height(htPtr->tkwin);
3361
3362 /* Reset viewport origin and world extents */
3363 htPtr->xOffset = Blt_AdjustViewport(htPtr->pendingX,
3364 htPtr->worldWidth, width,
3365 htPtr->xScrollUnits, BLT_SCROLL_MODE_LISTBOX);
3366 htPtr->yOffset = Blt_AdjustViewport(htPtr->pendingY,
3367 htPtr->worldHeight, height,
3368 htPtr->yScrollUnits, BLT_SCROLL_MODE_LISTBOX);
3369 if (htPtr->xScrollCmdPrefix != NULL) {
3370 Blt_UpdateScrollbar(htPtr->interp, htPtr->xScrollCmdPrefix,
3371 (double)htPtr->xOffset / htPtr->worldWidth,
3372 (double)(htPtr->xOffset + width) / htPtr->worldWidth);
3373 }
3374 if (htPtr->yScrollCmdPrefix != NULL) {
3375 Blt_UpdateScrollbar(htPtr->interp, htPtr->yScrollCmdPrefix,
3376 (double)htPtr->yOffset / htPtr->worldHeight,
3377 (double)(htPtr->yOffset + height) / htPtr->worldHeight);
3378 }
3379 /*
3380 * Given a new viewport or text height, find the first and
3381 * last line numbers of the new viewport.
3382 */
3383 if (GetVisibleLines(htPtr) != TCL_OK) {
3384 return;
3385 }
3386 }
3387 /*
3388 * This is a kludge: Send an expose event before and after
3389 * drawing the page of text. Since moving and resizing of the
3390 * embedded widgets will cause redundant expose events in the parent
3391 * window, the phony events will bracket them indicating no
3392 * action should be taken.
3393 */
3394 SendBogusEvent(tkwin);
3395
3396 /*
3397 * If either the position of the viewport has changed or the size
3398 * of width or height of the entire text have changed, move the
3399 * widgets from the previous viewport out of the current
3400 * viewport. Worry only about the vertical embedded widget movements.
3401 * The page is always draw at full width and the viewport will clip
3402 * the text.
3403 */
•3404 if ((htPtr->first != oldFirst) || (htPtr->last != oldLast)
3405 || htPtr->flags & TEXT_DIRTY ) {
3406 int offset;
3407 int i;
3408 int first, last;
3409 Blt_ChainLink *linkPtr;
3410 EmbeddedWidget *winPtr;
3411
3412 /* Figure out which lines are now out of the viewport */
3413
3414 if ((htPtr->first > oldFirst) && (htPtr->first <= oldLast)) {
3415 first = oldFirst, last = htPtr->first;
3416 } else if ((htPtr->last < oldLast) && (htPtr->last >= oldFirst)) {
3417 first = htPtr->last, last = oldLast;
3418 } else {
3419 first = oldFirst, last = oldLast;
3420 }
3421 if (! htPtr->lineArr) return;
3422
•3423
3424 if (htPtr->arraySize > 0 && htPtr->nLines > 0) {
3425 for (i = first; i <= last; i++) {
3426 offset = htPtr->lineArr[i].offset;
3427 for (linkPtr = Blt_ChainFirstLink(htPtr->lineArr[i].chainPtr);
•3428 linkPtr != NULL; linkPtr = Blt_ChainNextLink(linkPtr)) {
3429 winPtr = Blt_ChainGetValue(linkPtr);
3430 if (winPtr->tkwin != NULL) {
3431 MoveEmbeddedWidget(winPtr, offset);
3432 winPtr->flags &= ~WIDGET_VISIBLE;
3433 }
3434 }
•3435 }
3436 }
•3437 DrawPage(htPtr, deltaY);
3438 SendBogusEvent(tkwin);
3439
•3440 /* Reset flags */
3441 htPtr->flags &= ~TEXT_DIRTY;
3442 }
3443 }
3444
3445 /* Selection Procedures */
3446 /*
3447 *----------------------------------------------------------------------
3448 *
3449 * TextSelectionProc --
3450 *
3451 * This procedure is called back by Tk when the selection is
3452 * requested by someone. It returns part or all of the selection
3453 * in a buffer provided by the caller.
3454 *
3455 * Results:
3456 * The return value is the number of non-NULL bytes stored
3457 * at buffer. Buffer is filled (or partially filled) with a
3458 * NULL-terminated string containing part or all of the selection,
3459 * as given by offset and maxBytes.
3460 *
3461 * Side effects:
3462 * None.
3463 *
3464 *----------------------------------------------------------------------
3465 */
3466 static int
3467 TextSelectionProc(clientData, offset, buffer, maxBytes)
3468 ClientData clientData; /* Information about Text widget. */
3469 int offset; /* Offset within selection of first
3470 * character to be returned. */
3471 char *buffer; /* Location in which to place
3472 * selection. */
3473 int maxBytes; /* Maximum number of bytes to place
3474 * at buffer, not including terminating
3475 * NULL character. */
3476 {
3477 HText *htPtr = clientData;
3478 int size;
3479
3480 if ((htPtr->selFirst < 0) || (!htPtr->exportSelection)) {
3481 return -1;
3482 }
3483 size = (htPtr->selLast - htPtr->selFirst) + 1 - offset;
3484 if (size > maxBytes) {
3485 size = maxBytes;
3486 }
3487 if (size <= 0) {
3488 return 0; /* huh? */
3489 }
3490 strncpy(buffer, htPtr->charArr + htPtr->selFirst + offset, size);
3491 buffer[size] = '\0';
3492 return size;
3493 }
3494
3495 /*
3496 *----------------------------------------------------------------------
3497 *
3498 * TextLostSelection --
3499 *
3500 * This procedure is called back by Tk when the selection is
3501 * grabbed away from a Text widget.
3502 *
3503 * Results:
3504 * None.
3505 *
3506 * Side effects:
3507 * The existing selection is unhighlighted, and the window is
3508 * marked as not containing a selection.
3509 *
3510 *----------------------------------------------------------------------
3511 */
3512 static void
3513 TextLostSelection(clientData)
3514 ClientData clientData; /* Information about Text widget. */
3515 {
3516 HText *htPtr = clientData;
3517
3518 if ((htPtr->selFirst >= 0) && (htPtr->exportSelection)) {
3519 htPtr->selFirst = htPtr->selLast = -1;
3520 EventuallyRedraw(htPtr);
3521 }
3522 }
3523
3524 /*
3525 *----------------------------------------------------------------------
3526 *
3527 * SelectLine --
3528 *
3529 * Modify the selection by moving both its anchored and un-anchored
3530 * ends. This could make the selection either larger or smaller.
3531 *
3532 * Results:
3533 * None.
3534 *
3535 * Side effects:
3536 * The selection changes.
3537 *
3538 *----------------------------------------------------------------------
3539 */
3540 static int
3541 SelectLine(htPtr, tindex)
3542 HText *htPtr; /* Information about widget. */
3543 int tindex; /* Index of element that is to
3544 * become the "other" end of the
3545 * selection. */
3546 {
3547 int selFirst, selLast;
3548 int lineNum;
3549 Line *linePtr;
3550
3551 lineNum = IndexSearch(htPtr, tindex, 0, htPtr->nLines - 1);
3552 if (lineNum < 0) {
3553 char string[200];
3554
3555 sprintf(string, "can't determine line number from index \"%d\"",
3556 tindex);
3557 Tcl_AppendResult(htPtr->interp, string, (char *)NULL);
3558 return TCL_ERROR;
3559 }
3560 linePtr = htPtr->lineArr + lineNum;
3561 /*
3562 * Grab the selection if we don't own it already.
3563 */
3564 if ((htPtr->exportSelection) && (htPtr->selFirst == -1)) {
3565 Tk_OwnSelection(htPtr->tkwin, XA_PRIMARY, TextLostSelection, htPtr);
3566 }
3567 selFirst = linePtr->textStart;
3568 selLast = linePtr->textEnd;
3569 htPtr->selAnchor = tindex;
3570 if ((htPtr->selFirst != selFirst) ||
3571 (htPtr->selLast != selLast)) {
3572 htPtr->selFirst = selFirst;
3573 htPtr->selLast = selLast;
3574 EventuallyRedraw(htPtr);
3575 }
3576 return TCL_OK;
3577 }
3578
3579 /*
3580 *----------------------------------------------------------------------
3581 *
3582 * SelectWord --
3583 *
3584 * Modify the selection by moving both its anchored and un-anchored
3585 * ends. This could make the selection either larger or smaller.
3586 *
3587 * Results:
3588 * None.
3589 *
3590 * Side effects:
3591 * The selection changes.
3592 *
3593 *----------------------------------------------------------------------
3594 */
3595 static int
3596 SelectWord(htPtr, tindex)
3597 HText *htPtr; /* Information about widget. */
3598 int tindex; /* Index of element that is to
3599 * become the "other" end of the
3600 * selection. */
3601 {
3602 int selFirst, selLast;
3603 int i;
3604
3605 for (i = tindex; i < htPtr->nChars; i++) {
3606 if (isspace(UCHAR(htPtr->charArr[i]))) {
3607 break;
3608 }
3609 }
3610 selLast = i - 1;
3611 for (i = tindex; i >= 0; i--) {
3612 if (isspace(UCHAR(htPtr->charArr[i]))) {
3613 break;
3614 }
3615 }
3616 selFirst = i + 1;
3617 if (selFirst > selLast) {
3618 selFirst = selLast = tindex;
3619 }
3620 /*
3621 * Grab the selection if we don't own it already.
3622 */
3623 if ((htPtr->exportSelection) && (htPtr->selFirst == -1)) {
3624 Tk_OwnSelection(htPtr->tkwin, XA_PRIMARY, TextLostSelection, htPtr);
3625 }
3626 htPtr->selAnchor = tindex;
3627 if ((htPtr->selFirst != selFirst) || (htPtr->selLast != selLast)) {
3628 htPtr->selFirst = selFirst, htPtr->selLast = selLast;
3629 EventuallyRedraw(htPtr);
3630 }
3631 return TCL_OK;
3632 }
3633
3634 /*
3635 *----------------------------------------------------------------------
3636 *
3637 * SelectTextBlock --
3638 *
3639 * Modify the selection by moving its un-anchored end. This
3640 * could make the selection either larger or smaller.
3641 *
3642 * Results:
3643 * None.
3644 *
3645 * Side effects:
3646 * The selection changes.
3647 *
3648 *----------------------------------------------------------------------
3649 */
3650 static int
3651 SelectTextBlock(htPtr, tindex)
3652 HText *htPtr; /* Information about widget. */
3653 int tindex; /* Index of element that is to
3654 * become the "other" end of the
3655 * selection. */
3656 {
3657 int selFirst, selLast;
3658
3659 /*
3660 * Grab the selection if we don't own it already.
3661 */
3662
3663 if ((htPtr->exportSelection) && (htPtr->selFirst == -1)) {
3664 Tk_OwnSelection(htPtr->tkwin, XA_PRIMARY, TextLostSelection, htPtr);
3665 }
3666 /* If the anchor hasn't been set yet, assume the beginning of the text*/
3667 if (htPtr->selAnchor < 0) {
3668 htPtr->selAnchor = 0;
3669 }
3670 if (htPtr->selAnchor <= tindex) {
3671 selFirst = htPtr->selAnchor;
3672 selLast = tindex;
3673 } else {
3674 selFirst = tindex;
3675 selLast = htPtr->selAnchor;
3676 }
3677 if ((htPtr->selFirst != selFirst) || (htPtr->selLast != selLast)) {
3678 htPtr->selFirst = selFirst, htPtr->selLast = selLast;
3679 EventuallyRedraw(htPtr);
3680 }
3681 return TCL_OK;
3682 }
3683
3684 /*
3685 *----------------------------------------------------------------------
3686 *
3687 * SelectOp --
3688 *
3689 * This procedure handles the individual options for text
3690 * selections. The selected text is designated by start and end
3691 * indices into the text pool. The selected segment has both a
3692 * anchored and unanchored ends. The following selection
3693 * operations are implemented:
3694 *
3695 * "adjust" - resets either the first or last index
3696 * of the selection.
3697 * "clear" - clears the selection. Sets first/last
3698 * indices to -1.
3699 * "from" - sets the index of the selection anchor.
3700 * "line" - sets the first of last indices to the
3701 * start and end of the line at the
3702 * designated point.
3703 * "present" - return "1" if a selection is available,
3704 * "0" otherwise.
3705 * "range" - sets the first and last indices.
3706 * "to" - sets the index of the un-anchored end.
3707 * "word" - sets the first of last indices to the
3708 * start and end of the word at the
3709 * designated point.
3710 * Results:
3711 * None.
3712 *
3713 * Side effects:
3714 * The selection changes.
3715 *
3716 *----------------------------------------------------------------------
3717 */
3718 static int
3719 SelectOp(htPtr, interp, argc, argv)
3720 HText *htPtr;
3721 Tcl_Interp *interp;
3722 int argc;
3723 char **argv;
3724 {
3725 int iselection;
3726 unsigned int length;
3727 int result = TCL_OK;
3728 char c;
3729
3730 length = strlen(argv[2]);
3731 c = argv[2][0];
3732 if ((c == 'c') && (strncmp(argv[2], "clear", length) == 0)) {
3733 if (argc != 3) {
3734 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3735 " selection clear\"", (char *)NULL);
3736 return TCL_ERROR;
3737 }
3738 if (htPtr->selFirst != -1) {
3739 htPtr->selFirst = htPtr->selLast = -1;
3740 EventuallyRedraw(htPtr);
3741 }
3742 return TCL_OK;
3743 } else if ((c == 'p') && (strncmp(argv[2], "present", length) == 0)) {
3744 if (argc != 3) {
3745 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3746 " selection present\"", (char *)NULL);
3747 return TCL_ERROR;
3748 }
3749 Tcl_AppendResult(interp, (htPtr->selFirst != -1) ? "0" : "1",
3750 (char *)NULL);
3751 return TCL_OK;
3752 } else if ((c == 'r') && (strncmp(argv[2], "range", length) == 0)) {
3753 int selFirst, selLast;
3754
3755 if (argc != 5) {
3756 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3757 " selection range first last\"", (char *)NULL);
3758 return TCL_ERROR;
3759 }
3760 if (GetIndex(htPtr, argv[3], &selFirst) != TCL_OK) {
3761 return TCL_ERROR;
3762 }
3763 if (GetIndex(htPtr, argv[4], &selLast) != TCL_OK) {
3764 return TCL_ERROR;
3765 }
3766 htPtr->selAnchor = selFirst;
3767 SelectTextBlock(htPtr, selLast);
3768 return TCL_OK;
3769 }
3770 if (argc != 4) {
3771 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3772 " selection ", argv[2], " index\"", (char *)NULL);
3773 return TCL_ERROR;
3774 }
3775 if (GetIndex(htPtr, argv[3], &iselection) != TCL_OK) {
3776 return TCL_ERROR;
3777 }
3778 if ((c == 'f') && (strncmp(argv[2], "from", length) == 0)) {
3779 htPtr->selAnchor = iselection;
3780 } else if ((c == 'a') && (strncmp(argv[2], "adjust", length) == 0)) {
3781 int half1, half2;
3782
3783 half1 = (htPtr->selFirst + htPtr->selLast) / 2;
3784 half2 = (htPtr->selFirst + htPtr->selLast + 1) / 2;
3785 if (iselection < half1) {
3786 htPtr->selAnchor = htPtr->selLast;
3787 } else if (iselection > half2) {
3788 htPtr->selAnchor = htPtr->selFirst;
3789 }
3790 result = SelectTextBlock(htPtr, iselection);
3791 } else if ((c == 't') && (strncmp(argv[2], "to", length) == 0)) {
3792 result = SelectTextBlock(htPtr, iselection);
3793 } else if ((c == 'w') && (strncmp(argv[2], "word", length) == 0)) {
3794 result = SelectWord(htPtr, iselection);
3795 } else if ((c == 'l') && (strncmp(argv[2], "line", length) == 0)) {
3796 result = SelectLine(htPtr, iselection);
3797 } else {
3798 Tcl_AppendResult(interp, "bad selection operation \"", argv[2],
3799 "\": should be \"adjust\", \"clear\", \"from\", \"line\", \
3800 \"present\", \"range\", \"to\", or \"word\"", (char *)NULL);
3801 return TCL_ERROR;
3802 }
3803 return result;
3804 }
3805
3806 /*
3807 *----------------------------------------------------------------------
3808 *
3809 * GotoOp --
3810 *
3811 * Move the top line of the viewport to the new location based
3812 * upon the given line number. Force out-of-range requests to the
3813 * top or bottom of text.
3814 *
3815 * Results:
3816 * A standard Tcl result. If TCL_OK, interp->result contains the
3817 * current line number.
3818 *
3819 * Side effects:
3820 * At the next idle point, the text viewport will be move to the
3821 * new line.
3822 *
3823 *----------------------------------------------------------------------
3824 */
3825 /*ARGSUSED*/
3826 static int
3827 GotoOp(htPtr, interp, argc, argv)
3828 HText *htPtr;
3829 Tcl_Interp *interp; /* Not used. */
3830 int argc;
3831 char **argv;
3832 {
3833 int line;
3834
3835 line = htPtr->first;
3836 if (argc == 3) {
3837 int tindex;
3838
3839 if (GetIndex(htPtr, argv[2], &tindex) != TCL_OK) {
3840 return TCL_ERROR;
3841 }
3842 line = IndexSearch(htPtr, tindex, 0, htPtr->nLines - 1);
3843 if (line < 0) {
3844 char string[200];
3845
3846 sprintf(string, "can't determine line number from index \"%d\"",
3847 tindex);
3848 Tcl_AppendResult(htPtr->interp, string, (char *)NULL);
3849 return TCL_ERROR;
3850 }
3851 htPtr->reqLineNum = line;
3852 htPtr->flags |= TEXT_DIRTY;
3853
3854 /*
3855 * Make only a request for a change in the viewport. Defer
3856 * the actual scrolling until the text layout is adjusted at
3857 * the next idle point.
3858 */
3859 if (line != htPtr->first) {
3860 htPtr->flags |= GOTO_PENDING;
3861 EventuallyRedraw(htPtr);
3862 }
3863 }
3864 Tcl_SetResult(htPtr->interp, Blt_Itoa(line), TCL_VOLATILE);
3865 return TCL_OK;
3866 }
3867
3868
3869 static int
3870 XViewOp(htPtr, interp, argc, argv)
3871 HText *htPtr;
3872 Tcl_Interp *interp;
3873 int argc;
3874 char **argv;
3875 {
3876 int width, worldWidth;
3877
3878 width = Tk_Width(htPtr->tkwin);
3879 worldWidth = htPtr->worldWidth;
3880 if (argc == 2) {
3881 double fract;
3882
3883 /* Report first and last fractions */
3884 fract = (double)htPtr->xOffset / worldWidth;
3885 Tcl_AppendElement(interp, Blt_Dtoa(interp, CLAMP(fract, 0.0, 1.0)));
3886 fract = (double)(htPtr->xOffset + width) / worldWidth;
3887 Tcl_AppendElement(interp, Blt_Dtoa(interp, CLAMP(fract, 0.0, 1.0)));
3888 return TCL_OK;
3889 }
3890 htPtr->pendingX = htPtr->xOffset;
3891 if (Blt_GetScrollInfo(interp, argc - 2, argv + 2, &(htPtr->pendingX),
3892 worldWidth, width, htPtr->xScrollUnits, BLT_SCROLL_MODE_LISTBOX)
3893 != TCL_OK) {
3894 return TCL_ERROR;
3895 }
3896 htPtr->flags |= TEXT_DIRTY;
3897 EventuallyRedraw(htPtr);
3898 return TCL_OK;
3899 }
3900
3901 static int
3902 YViewOp(htPtr, interp, argc, argv)
3903 HText *htPtr;
3904 Tcl_Interp *interp;
3905 int argc;
3906 char **argv;
3907 {
3908 int height, worldHeight;
3909
3910 height = Tk_Height(htPtr->tkwin);
3911 worldHeight = htPtr->worldHeight;
3912 if (argc == 2) {
3913 double fract;
3914
3915 /* Report first and last fractions */
3916 fract = (double)htPtr->yOffset / worldHeight;
3917 Tcl_AppendElement(interp, Blt_Dtoa(interp, CLAMP(fract, 0.0, 1.0)));
3918 fract = (double)(htPtr->yOffset + height) / worldHeight;
3919 Tcl_AppendElement(interp, Blt_Dtoa(interp, CLAMP(fract, 0.0, 1.0)));
3920 return TCL_OK;
3921 }
3922 htPtr->pendingY = htPtr->yOffset;
3923 if (Blt_GetScrollInfo(interp, argc - 2, argv + 2, &(htPtr->pendingY),
3924 worldHeight, height, htPtr->yScrollUnits, BLT_SCROLL_MODE_LISTBOX)
3925 != TCL_OK) {
3926 return TCL_ERROR;
3927 }
3928 htPtr->flags |= TEXT_DIRTY;
3929 EventuallyRedraw(htPtr);
3930 return TCL_OK;
3931 }
3932
3933 /*
3934 * ----------------------------------------------------------------------
3935 *
3936 * AppendOp --
3937 *
3938 * This procedure embeds a Tk widget into the hypertext.
3939 *
3940 * Results:
3941 * A standard Tcl result.
3942 *
3943 * Side effects:
3944 * Memory is allocated. EmbeddedWidget gets configured.
3945 *
3946 * ----------------------------------------------------------------------
3947 */
3948 static int
3949 AppendOp(htPtr, interp, argc, argv)
3950 HText *htPtr; /* Hypertext widget */
3951 Tcl_Interp *interp; /* Interpreter associated with widget */
3952 int argc; /* Number of arguments. */
•3953 CONST char **argv; /* Argument strings. */
3954 {
3955 Line *linePtr;
3956 EmbeddedWidget *winPtr;
3957
3958 winPtr = CreateEmbeddedWidget(htPtr, argv[2]);
3959 if (winPtr == NULL) {
3960 return TCL_ERROR;
3961 }
•3962 if (Blt_ConfigureWidget(interp, htPtr->tkwin, widgetConfigSpecs,
3963 argc - 3, argv + 3, (char *)winPtr, 0) != TCL_OK) {
3964 return TCL_ERROR;
3965 }
3966 /*
3967 * Append widget to list of embedded widgets of the last line.
3968 */
3969 linePtr = GetLastLine(htPtr);
3970 if (linePtr == NULL) {
3971 Tcl_AppendResult(htPtr->interp, "can't allocate line structure",
3972 (char *)NULL);
3973 return TCL_ERROR;
3974 }
3975 Blt_ChainAppend(linePtr->chainPtr, winPtr);
3976 linePtr->width += winPtr->cavityWidth;
3977 winPtr->precedingTextEnd = linePtr->textEnd;
3978
3979 htPtr->flags |= (WIDGET_APPENDED | REQUEST_LAYOUT);
3980 EventuallyRedraw(htPtr);
3981 return TCL_OK;
3982 }
3983
3984 /*
3985 *----------------------------------------------------------------------
3986 *
3987 * WindowsOp --
3988 *
3989 * Returns a list of all the pathNames of embedded widgets of the
3990 * HText widget. If a pattern argument is given, only the names
3991 * of windows matching it will be placed into the list.
3992 *
3993 * Results:
3994 * Standard Tcl result. If TCL_OK, interp->result will contain
3995 * the list of the embedded widget pathnames. Otherwise it will
3996 * contain an error message.
3997 *
3998 *----------------------------------------------------------------------
3999 */
4000 static int
4001 WindowsOp(htPtr, interp, argc, argv)
4002 HText *htPtr; /* Hypertext widget record */
4003 Tcl_Interp *interp; /* Interpreter associated with widget */
4004 int argc;
4005 char **argv;
4006 {
4007 EmbeddedWidget *winPtr;
4008 Blt_HashEntry *hPtr;
4009 Blt_HashSearch cursor;
4010 char *name;
4011
4012 for (hPtr = Blt_FirstHashEntry(&(htPtr->widgetTable), &cursor);
4013 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
4014 winPtr = (EmbeddedWidget *)Blt_GetHashValue(hPtr);
4015 if (winPtr->tkwin == NULL) {
4016 fprintf(stderr, "window `%s' is null\n",
4017 Tk_PathName(Blt_GetHashKey(&(htPtr->widgetTable), hPtr)));
4018 continue;
4019 }
4020 name = Tk_PathName(winPtr->tkwin);
4021 if ((argc == 2) || (Tcl_StringMatch(name, argv[2]))) {
4022 Tcl_AppendElement(interp, name);
4023 }
4024 }
4025 return TCL_OK;
4026 }
4027
4028 /*
4029 *----------------------------------------------------------------------
4030 *
4031 * CgetOp --
4032 *
4033 *----------------------------------------------------------------------
4034 */
4035 /*ARGSUSED*/
4036 static int
4037 CgetOp(htPtr, interp, argc, argv)
4038 HText *htPtr;
4039 Tcl_Interp *interp;
4040 int argc; /* Not used. */
4041 char **argv;
4042 {
4043 char *itemPtr;
4044 Tk_ConfigSpec *specsPtr;
4045
4046 if ((argc > 2) && (argv[2][0] == '.')) {
4047 Tk_Window tkwin;
4048 EmbeddedWidget *winPtr;
4049
4050 /* EmbeddedWidget window to be configured */
4051 tkwin = Tk_NameToWindow(interp, argv[2], htPtr->tkwin);
4052 if (tkwin == NULL) {
4053 return TCL_ERROR;
4054 }
4055 winPtr = FindEmbeddedWidget(htPtr, tkwin);
4056 if (winPtr == NULL) {
4057 Tcl_AppendResult(interp, "window \"", argv[2],
4058 "\" is not managed by \"", argv[0], "\"", (char *)NULL);
4059 return TCL_ERROR;
4060 }
4061 specsPtr = widgetConfigSpecs;
4062 itemPtr = (char *)winPtr;
4063 argv++;
4064 } else {
4065 specsPtr = configSpecs;
4066 itemPtr = (char *)htPtr;
4067 }
4068 return Tk_ConfigureValue(interp, htPtr->tkwin, specsPtr, itemPtr,
4069 argv[2], 0);
4070 }
4071
4072 /*
4073 *----------------------------------------------------------------------
4074 *
4075 * ConfigureOp --
4076 *
4077 * This procedure is called to process an argv/argc list, plus
4078 * the Tk option database, in order to configure (or reconfigure)
4079 * a hypertext widget.
4080 *
4081 * Results:
4082 * A standard Tcl result. If TCL_ERROR is returned, then
4083 * interp->result contains an error message.
4084 *
4085 * Side effects:
4086 * Configuration information, such as text string, colors, font,
4087 * etc. get set for htPtr; old resources get freed, if there were any.
4088 * The hypertext is redisplayed.
4089 *
4090 *----------------------------------------------------------------------
4091 */
4092 static int
4093 ConfigureOp(htPtr, interp, argc, argv)
4094 HText *htPtr;
4095 Tcl_Interp *interp;
4096 int argc;
•4097 CONST char **argv;
4098 {
4099 char *itemPtr;
4100 Tk_ConfigSpec *specsPtr;
4101
4102 if ((argc > 2) && (argv[2][0] == '.')) {
4103 Tk_Window tkwin;
4104 EmbeddedWidget *winPtr;
4105
4106 /* EmbeddedWidget window to be configured */
4107 tkwin = Tk_NameToWindow(interp, argv[2], htPtr->tkwin);
4108 if (tkwin == NULL) {
4109 return TCL_ERROR;
4110 }
4111 winPtr = FindEmbeddedWidget(htPtr, tkwin);
4112 if (winPtr == NULL) {
4113 Tcl_AppendResult(interp, "window \"", argv[2],
4114 "\" is not managed by \"", argv[0], "\"", (char *)NULL);
4115 return TCL_ERROR;
4116 }
4117 specsPtr = widgetConfigSpecs;
4118 itemPtr = (char *)winPtr;
4119 argv++;
4120 argc--;
4121 } else {
4122 specsPtr = configSpecs;
4123 itemPtr = (char *)htPtr;
4124 }
4125 if (argc == 2) {
4126 return Tk_ConfigureInfo(interp, htPtr->tkwin, specsPtr, itemPtr,
4127 (char *)NULL, 0);
4128 } else if (argc == 3) {
4129 return Tk_ConfigureInfo(interp, htPtr->tkwin, specsPtr, itemPtr,
4130 argv[2], 0);
4131 }
•4132 if (Blt_ConfigureWidget(interp, htPtr->tkwin, specsPtr, argc - 2,
4133 argv + 2, itemPtr, TK_CONFIG_ARGV_ONLY) != TCL_OK) {
4134
4135 /*
4136 * in 8.5 cant tell if in option was modified
4137 * supposed to convert to Tk_SetOptions
4138 */
4139 return TCL_ERROR;
4140 }
4141 if (itemPtr == (char *)htPtr) {
4142 /* Reconfigure the master */
4143 if (ConfigureText(interp, htPtr) != TCL_OK) {
4144 return TCL_ERROR;
4145 }
4146 } else {
4147 htPtr->flags |= REQUEST_LAYOUT;
4148 }
4149 EventuallyRedraw(htPtr);
4150 return TCL_OK;
4151 }
4152
4153 /*
4154 *----------------------------------------------------------------------
4155 *
4156 * ScanOp --
4157 *
4158 * Implements the quick scan for hypertext widgets.
4159 *
4160 *----------------------------------------------------------------------
4161 */
4162 /*ARGSUSED*/
4163 static int
4164 ScanOp(htPtr, interp, argc, argv)
4165 HText *htPtr;
4166 Tcl_Interp *interp;
4167 int argc; /* Not used. */
4168 char **argv;
4169 {
4170 int x, y;
4171 char c;
4172 unsigned int length;
4173
4174
4175 if (Blt_GetXY(interp, htPtr->tkwin, argv[3], &x, &y) != TCL_OK) {
4176 return TCL_ERROR;
4177 }
4178 c = argv[2][0];
4179 length = strlen(argv[2]);
4180 if ((c == 'm') && (strncmp(argv[2], "mark", length) == 0)) {
4181 htPtr->scanMark.x = x, htPtr->scanMark.y = y;
4182 htPtr->scanPt.x = htPtr->xOffset;
4183 htPtr->scanPt.y = htPtr->yOffset;
4184
4185 } else if ((c == 'd') && (strncmp(argv[2], "dragto", length) == 0)) {
4186 int px, py;
4187
4188 px = htPtr->scanPt.x - (10 * (x - htPtr->scanMark.x));
4189 py = htPtr->scanPt.y - (10 * (y - htPtr->scanMark.y));
4190
4191 if (px < 0) {
4192 px = htPtr->scanPt.x = 0;
4193 htPtr->scanMark.x = x;
4194 } else if (px >= htPtr->worldWidth) {
4195 px = htPtr->scanPt.x = htPtr->worldWidth - htPtr->xScrollUnits;
4196 htPtr->scanMark.x = x;
4197 }
4198 if (py < 0) {
4199 py = htPtr->scanPt.y = 0;
4200 htPtr->scanMark.y = y;
4201 } else if (py >= htPtr->worldHeight) {
4202 py = htPtr->scanPt.y = htPtr->worldHeight - htPtr->yScrollUnits;
4203 htPtr->scanMark.y = y;
4204 }
4205 if ((py != htPtr->pendingY) || (px != htPtr->pendingX)) {
4206 htPtr->pendingX = px, htPtr->pendingY = py;
4207 htPtr->flags |= TEXT_DIRTY;
4208 EventuallyRedraw(htPtr);
4209 }
4210 } else {
4211 Tcl_AppendResult(interp, "bad scan operation \"", argv[2],
4212 "\": should be either \"mark\" or \"dragto\"", (char *)NULL);
4213 return TCL_ERROR;
4214 }
4215 return TCL_OK;
4216 }
4217
4218 /*
4219 *----------------------------------------------------------------------
4220 *
4221 * SearchOp --
4222 *
4223 * Returns the linenumber of the next line matching the given
4224 * pattern within the range of lines provided. If the first
4225 * line number is greater than the last, the search is done in
4226 * reverse.
4227 *
4228 *----------------------------------------------------------------------
4229 */
4230 static int
4231 SearchOp(htPtr, interp, argc, argv)
4232 HText *htPtr;
4233 Tcl_Interp *interp;
4234 int argc;
4235 char **argv;
4236 {
4237 char *startPtr, *endPtr;
4238 char saved;
4239 Tcl_RegExp regExpToken;
4240 int iFirst, iLast;
4241 int matchStart, matchEnd;
4242 int match;
4243
4244 regExpToken = Tcl_RegExpCompile(interp, argv[2]);
4245 if (regExpToken == NULL) {
4246 return TCL_ERROR;
4247 }
4248 iFirst = 0;
4249 iLast = htPtr->nChars;
4250 if (argc > 3) {
4251 if (GetIndex(htPtr, argv[3], &iFirst) != TCL_OK) {
4252 return TCL_ERROR;
4253 }
4254 }
4255 if (argc == 4) {
4256 if (GetIndex(htPtr, argv[4], &iLast) != TCL_OK) {
4257 return TCL_ERROR;
4258 }
4259 }
4260 if (iLast < iFirst) {
4261 return TCL_ERROR;
4262 }
4263 matchStart = matchEnd = -1;
4264 startPtr = htPtr->charArr + iFirst;
4265 endPtr = htPtr->charArr + (iLast + 1);
4266 saved = *endPtr;
4267 *endPtr = '\0'; /* Make the line a string by changing the
4268 * '\n' into a NUL byte before searching */
4269 match = Tcl_RegExpExec(interp, regExpToken, startPtr, startPtr);
4270 *endPtr = saved;
4271 if (match < 0) {
4272 return TCL_ERROR;
4273 } else if (match > 0) {
4274 Tcl_RegExpRange(regExpToken, 0, &startPtr, &endPtr);
4275 if ((startPtr != NULL) || (endPtr != NULL)) {
4276 matchStart = startPtr - htPtr->charArr;
4277 matchEnd = endPtr - htPtr->charArr - 1;
4278 }
4279 }
4280 if (match > 0) {
4281 Tcl_AppendElement(interp, Blt_Itoa(matchStart));
4282 Tcl_AppendElement(interp, Blt_Itoa(matchEnd));
4283 } else {
4284 Tcl_ResetResult(interp);
4285 }
4286 return TCL_OK;
4287 }
4288
4289 /*
4290 *----------------------------------------------------------------------
4291 *
4292 * RangeOp --
4293 *
4294 * Returns the characters designated by the range of elements.
4295 *
4296 *----------------------------------------------------------------------
4297 */
4298 static int
4299 RangeOp(htPtr, interp, argc, argv)
4300 HText *htPtr;
4301 Tcl_Interp *interp;
4302 int argc;
4303 char **argv;
4304 {
4305 char *startPtr, *endPtr;
4306 char saved;
4307 int textFirst, textLast;
4308
4309 textFirst = htPtr->selFirst;
4310 textLast = htPtr->selLast;
4311 if (textFirst < 0) {
4312 textFirst = 0;
4313 textLast = htPtr->nChars - 1;
4314 }
4315 if (argc > 2) {
4316 if (GetIndex(htPtr, argv[2], &textFirst) != TCL_OK) {
4317 return TCL_ERROR;
4318 }
4319 }
4320 if (argc == 4) {
4321 if (GetIndex(htPtr, argv[3], &textLast) != TCL_OK) {
4322 return TCL_ERROR;
4323 }
4324 }
4325 if (textLast < textFirst) {
4326 Tcl_AppendResult(interp, "first index is greater than last", (char *)NULL);
4327 return TCL_ERROR;
4328 }
4329 startPtr = htPtr->charArr + textFirst;
4330 endPtr = htPtr->charArr + (textLast + 1);
4331 saved = *endPtr;
4332 *endPtr = '\0'; /* Make the line into a string by
4333 * changing the * '\n' into a '\0'
4334 * before copying */
4335 Tcl_SetResult(interp, startPtr, TCL_VOLATILE);
4336 *endPtr = saved;
4337 return TCL_OK;
4338 }
4339
4340 /*
4341 *----------------------------------------------------------------------
4342 *
4343 * IndexOp --
4344 *
4345 *----------------------------------------------------------------------
4346 */
4347 /*ARGSUSED*/
4348 static int
4349 IndexOp(htPtr, interp, argc, argv)
4350 HText *htPtr;
4351 Tcl_Interp *interp;
4352 int argc; /* Not used. */
4353 char **argv;
4354 {
4355 int tindex;
4356
4357 if (GetIndex(htPtr, argv[2], &tindex) != TCL_OK) {
4358 return TCL_ERROR;
4359 }
4360 Tcl_SetResult(interp, Blt_Itoa(tindex), TCL_VOLATILE);
4361 return TCL_OK;
4362 }
4363
4364 /*
4365 *----------------------------------------------------------------------
4366 *
4367 * LinePosOp --
4368 *
4369 *----------------------------------------------------------------------
4370 */
4371 /*ARGSUSED*/
4372 static int
4373 LinePosOp(htPtr, interp, argc, argv)
4374 HText *htPtr;
4375 Tcl_Interp *interp;
4376 int argc; /* Not used. */
4377 char **argv;
4378 {
4379 int line, cpos, tindex;
4380 char string[200];
4381
4382 if (GetIndex(htPtr, argv[2], &tindex) != TCL_OK) {
4383 return TCL_ERROR;
4384 }
4385 if (GetTextPosition(htPtr, tindex, &line, &cpos) != TCL_OK) {
4386 return TCL_ERROR;
4387 }
4388 sprintf(string, "%d.%d", line, cpos);
4389 Tcl_SetResult(interp, string, TCL_VOLATILE);
4390 return TCL_OK;
4391 }
4392
4393 /*
4394 * --------------------------------------------------------------
4395 *
4396 * TextWidgetCmd --
4397 *
4398 * This procedure is invoked to process the Tcl command that
4399 * corresponds to a widget managed by this module. See the user
4400 * documentation for details on what it does.
4401 *
4402 * Results:
4403 * A standard Tcl result.
4404 *
4405 * Side effects:
4406 * See the user documentation.
4407 *
4408 * --------------------------------------------------------------
4409 */
4410
4411 static Blt_OpSpec textOps[] =
4412 {
4413 {"append", 1, (Blt_Op)AppendOp, 3, 0, "window ?option value?...",},
4414 {"cget", 2, (Blt_Op)CgetOp, 3, 3, "?window? option",},
4415 {"configure", 2, (Blt_Op)ConfigureOp, 2, 0,
4416 "?window? ?option value?...",},
4417 {"gotoline", 2, (Blt_Op)GotoOp, 2, 3, "?line?",},
4418 {"index", 1, (Blt_Op)IndexOp, 3, 3, "string",},
4419 {"linepos", 1, (Blt_Op)LinePosOp, 3, 3, "string",},
4420 {"range", 2, (Blt_Op)RangeOp, 2, 4, "?from? ?to?",},
4421 {"scan", 2, (Blt_Op)ScanOp, 4, 4, "oper @x,y",},
4422 {"search", 3, (Blt_Op)SearchOp, 3, 5, "pattern ?from? ?to?",},
4423 {"selection", 3, (Blt_Op)SelectOp, 3, 5, "oper ?index?",},
4424 {"windows", 6, (Blt_Op)WindowsOp, 2, 3, "?pattern?",},
4425 {"xview", 1, (Blt_Op)XViewOp, 2, 5,
4426 "?moveto fract? ?scroll number what?",},
4427 {"yview", 1, (Blt_Op)YViewOp, 2, 5,
4428 "?moveto fract? ?scroll number what?",},
4429 };
4430 static int nTextOps = sizeof(textOps) / sizeof(Blt_OpSpec);
4431
4432 static int
4433 TextWidgetCmd(clientData, interp, argc, argv)
4434 ClientData clientData; /* Information about hypertext widget. */
4435 Tcl_Interp *interp; /* Current interpreter. */
4436 int argc; /* Number of arguments. */
4437 char **argv; /* Argument strings. */
4438 {
4439 Blt_Op proc;
4440 int result;
4441 HText *htPtr = clientData;
4442
4443 proc = Blt_GetOp(interp, nTextOps, textOps, BLT_OP_ARG1, argc, argv, 0);
4444 if (proc == NULL) {
4445 return TCL_ERROR;
4446 }
4447 Tcl_Preserve(htPtr);
4448 result = (*proc) (htPtr, interp, argc, argv);
4449 Tcl_Release(htPtr);
4450 return result;
4451 }
4452
4453 /*
4454 * --------------------------------------------------------------
4455 *
4456 * TextCmd --
4457 *
4458 * This procedure is invoked to process the "htext" Tcl command.
4459 * See the user documentation for details on what it does.
4460 *
4461 * Results:
4462 * A standard Tcl result.
4463 *
4464 * Side effects:
4465 * See the user documentation.
4466 *
4467 * --------------------------------------------------------------
4468 */
4469 /* ARGSUSED */
4470 static int
4471 TextCmd(clientData, interp, argc, argv)
4472 ClientData clientData; /* Main window associated with interpreter. */
4473 Tcl_Interp *interp; /* Current interpreter. */
4474 int argc; /* Number of arguments. */
4475 char **argv; /* Argument strings. */
4476 {
4477 HText *htPtr;
4478 Screen *screenPtr;
4479 Tk_Window tkwin;
4480
4481 if (argc < 2) {
4482 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4483 " pathName ?option value?...\"", (char *)NULL);
4484 return TCL_ERROR;
4485 }
4486 htPtr = Blt_Calloc(1, sizeof(HText));
4487 assert(htPtr);
4488 tkwin = Tk_MainWindow(interp);
4489 tkwin = Tk_CreateWindowFromPath(interp, tkwin, argv[1], (char *)NULL);
4490 if (tkwin == NULL) {
4491 Blt_Free(htPtr);
4492 return TCL_ERROR;
4493 }
4494 /* Initialize the new hypertext widget */
4495
4496 Tk_SetClass(tkwin, "Htext");
4497 htPtr->tkwin = tkwin;
4498 htPtr->display = Tk_Display(tkwin);
4499 htPtr->interp = interp;
4500 htPtr->nLines = htPtr->arraySize = 0;
4501 htPtr->leader = 1;
4502 htPtr->xScrollUnits = htPtr->yScrollUnits = 10;
4503 htPtr->nRows = htPtr->nColumns = 0;
4504 htPtr->selFirst = htPtr->selLast = -1;
4505 htPtr->selAnchor = 0;
4506 htPtr->exportSelection = TRUE;
4507 htPtr->selBorderWidth = 2;
4508 screenPtr = Tk_Screen(htPtr->tkwin);
4509 htPtr->maxWidth = WidthOfScreen(screenPtr);
4510 htPtr->maxHeight = HeightOfScreen(screenPtr);
4511 Blt_InitHashTable(&(htPtr->widgetTable), BLT_ONE_WORD_KEYS);
4512
4513 Tk_CreateSelHandler(tkwin, XA_PRIMARY, XA_STRING, TextSelectionProc,
4514 htPtr, XA_STRING);
4515 Tk_CreateEventHandler(tkwin, ExposureMask | StructureNotifyMask,
4516 TextEventProc, htPtr);
4517 #if (TK_MAJOR_VERSION > 4)
4518 Blt_SetWindowInstanceData(tkwin, htPtr);
4519 #endif
4520 /*
4521 * -----------------------------------------------------------------
4522 *
4523 * Create the widget command before configuring the widget. This
4524 * is because the "-file" and "-text" options may have embedded
4525 * commands that self-reference the widget through the
4526 * "$blt_htext(widget)" variable.
4527 *
4528 * ------------------------------------------------------------------
4529 */
4530 htPtr->cmdToken = Tcl_CreateCommand(interp, argv[1], TextWidgetCmd, htPtr,
4531 TextDeleteCmdProc);
4532 #ifdef ITCL_NAMESPACES
4533 Itk_SetWidgetCommand(htPtr->tkwin, htPtr->cmdToken);
4534 #endif
•4535 if ((Blt_ConfigureWidget(interp, htPtr->tkwin, configSpecs, argc - 2,
•4536 (CONST char **)argv + 2, (char *)htPtr, 0) != TCL_OK) ||
4537 (ConfigureText(interp, htPtr) != TCL_OK)) {
4538 Tk_DestroyWindow(htPtr->tkwin);
4539 return TCL_ERROR;
4540 }
4541 Tcl_SetResult(interp, Tk_PathName(htPtr->tkwin), TCL_VOLATILE);
4542 Tk_SetClassProcs(tkwin, &htextClass, (ClientData)htPtr);
4543 return TCL_OK;
4544 }
4545
4546 int
4547 Blt_HtextInit(interp)
4548 Tcl_Interp *interp;
4549 {
4550 static Blt_CmdSpec cmdSpec =
4551 {"htext", TextCmd,};
4552
4553 if (Blt_InitCmd(interp, "blt", &cmdSpec) == NULL) {
4554 return TCL_ERROR;
4555 }
4556 return TCL_OK;
4557 }
4558
4559 #endif /* NO_HTEXT */