Debian Codegraph @

generic/bltHtext.c

raw symbols ◀ ▶
Source reconstructed at patch state 1 of 3 — not the installed (fully patched) source.
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 }
3104
3105 /* Draw each line */
3106 for (i = htPtr->first; i <= htPtr->last; i++) {
3107
3108 /* Initialize character position in text buffer to start */
3109 linePtr = htPtr->lineArr + lineNum;
3110 sgmt.textStart = linePtr->textStart;
3111 sgmt.textEnd = linePtr->textEnd;
3112
3113 /* Initialize X position */
3114 x = -(htPtr->xOffset);
3115 for (linkPtr = Blt_ChainFirstLink(linePtr->chainPtr); linkPtr != NULL;
3116 linkPtr = Blt_ChainNextLink(linkPtr)) {
3117 winPtr = Blt_ChainGetValue(linkPtr);
3118
3119 if (winPtr->tkwin != NULL) {
3120 winPtr->flags |= WIDGET_VISIBLE;
3121 MoveEmbeddedWidget(winPtr, linePtr->offset);
3122 }
3123 sgmt.textEnd = winPtr->precedingTextEnd - 1;
3124 if (sgmt.textEnd >= sgmt.textStart) {
3125 DrawSegment(htPtr, pixmap, linePtr, x, y, &sgmt);
3126 x += winPtr->precedingTextWidth;
3127 }
3128 /* Skip over the extra trailing space which designates the widget */
3129 sgmt.textStart = winPtr->precedingTextEnd + 1;
3130 x += winPtr->cavityWidth;
3131 forceCopy++;
3132 }
3133
3134 /*
3135 * This may be the text trailing the last widget or the entire
3136 * line if no widgets occur on it.
3137 */
3138 sgmt.textEnd = linePtr->textEnd;
3139 if (sgmt.textEnd >= sgmt.textStart) {
3140 DrawSegment(htPtr, pixmap, linePtr, x, y, &sgmt);
3141 }
3142 /* Go to the top of the next line */
3143 if (deltaY >= 0) {
3144 y += htPtr->lineArr[lineNum].height;
3145 lineNum++;
3146 }
3147 if ((forceCopy > 0) && !(htPtr->flags & TEXT_DIRTY)) {
3148 if (deltaY >= 0) {
3149 XCopyArea(display, pixmap, Tk_WindowId(tkwin), htPtr->drawGC,
3150 0, lastY, width, y - lastY, 0, lastY);
3151 } else {
3152 XCopyArea(display, pixmap, Tk_WindowId(tkwin), htPtr->drawGC,
3153 0, y, width, lastY - y, 0, y);
3154 }
3155 forceCopy = 0; /* Reset drawing flag */
3156 lastY = y; /* Record last Y position */
3157 }
3158 if ((deltaY < 0) && (lineNum > 0)) {
3159 --lineNum;
3160 y -= htPtr->lineArr[lineNum].height;
3161 }
3162 }
3163 /*
3164 * If the viewport was resized, draw the page in one operation.
3165 * Otherwise draw any left-over block of text (either at the top
3166 * or bottom of the page)
3167 */
3168 if (htPtr->flags & TEXT_DIRTY) {
3169 XCopyArea(display, pixmap, Tk_WindowId(tkwin),
3170 htPtr->drawGC, 0, 0, width, height, 0, 0);
3171 } else if (lastY != y) {
3172 if (deltaY >= 0) {
3173 height -= lastY;
3174 XCopyArea(display, pixmap, Tk_WindowId(tkwin),
3175 htPtr->drawGC, 0, lastY, width, height, 0, lastY);
3176 } else {
3177 height = lastY;
3178 XCopyArea(display, pixmap, Tk_WindowId(tkwin),
3179 htPtr->drawGC, 0, 0, width, height, 0, 0);
3180 }
3181 }
3182 Tk_FreePixmap(display, pixmap);
3183 }
3184
3185
3186 static void
3187 SendBogusEvent(tkwin)
3188 Tk_Window tkwin;
3189 {
3190 #define DONTPROPAGATE 0
3191 XEvent event;
3192
3193 event.type = event.xexpose.type = Expose;
3194 event.xexpose.window = Tk_WindowId(tkwin);
3195 event.xexpose.display = Tk_Display(tkwin);
3196 event.xexpose.count = 0;
3197 event.xexpose.x = event.xexpose.y = 0;
3198 event.xexpose.width = Tk_Width(tkwin);
3199 event.xexpose.height = Tk_Height(tkwin);
3200
3201 XSendEvent(Tk_Display(tkwin), Tk_WindowId(tkwin), DONTPROPAGATE,
3202 ExposureMask, &event);
3203 }
3204
3205 /*
3206 * ----------------------------------------------------------------------
3207 *
3208 * DisplayText --
3209 *
3210 * This procedure is invoked to display a hypertext widget.
3211 * Many of the operations which might ordinarily be performed
3212 * elsewhere (e.g. in a configuration routine) are done here
3213 * because of the somewhat unusual interactions occurring between
3214 * the parent and embedded widgets.
3215 *
3216 * Recompute the layout of the text if necessary. This is
3217 * necessary if the world coordinate system has changed.
3218 * Specifically, the following may have occurred:
3219 *
3220 * 1. a text attribute has changed (font, linespacing, etc.).
3221 * 2. widget option changed (anchor, width, height).
3222 * 3. actual embedded widget was resized.
3223 * 4. new text string or file.
3224 *
3225 * This is deferred to the display routine since potentially
3226 * many of these may occur (especially embedded widget changes).
3227 *
3228 * Set the vertical and horizontal scrollbars (if they are
3229 * designated) by issuing a Tcl command. Done here since
3230 * the text window width and height are needed.
3231 *
3232 * If the viewport position or contents have changed in the
3233 * vertical direction, the now out-of-view embedded widgets
3234 * must be moved off the viewport. Since embedded widgets will
3235 * obscure the text window, it is imperative that the widgets
3236 * are moved off before we try to redraw text in the same area.
3237 * This is necessary only for vertical movements. Horizontal
3238 * embedded widget movements are handled automatically in the
3239 * page drawing routine.
3240 *
3241 * Get the new first and last line numbers for the viewport.
3242 * These line numbers may have changed because either a)
3243 * the viewport changed size or position, or b) the text
3244 * (embedded widget sizes or text attributes) have changed.
3245 *
3246 * If the viewport has changed vertically (i.e. the first or
3247 * last line numbers have changed), move the now out-of-view
3248 * embedded widgets off the viewport.
3249 *
3250 * Potentially many expose events may be generated when the
3251 * the individual embedded widgets are moved and/or resized.
3252 * These events need to be ignored. Since (I think) expose
3253 * events are guaranteed to happen in order, we can bracket
3254 * them by sending phony events (via XSendEvent). The phony
3255 * events turn on and off flags indicating which events
3256 * should be ignored.
3257 *
3258 * Finally, the page drawing routine is called.
3259 *
3260 * Results:
3261 * None.
3262 *
3263 * Side effects:
3264 * Commands are output to X to display the hypertext in its
3265 * current mode.
3266 *
3267 * ----------------------------------------------------------------------
3268 */
3269 static void
3270 DisplayText(clientData)
3271 ClientData clientData; /* Information about widget. */
3272 {
3273 HText *htPtr = clientData;
3274 Tk_Window tkwin = htPtr->tkwin;
3275 int oldFirst; /* First line of old viewport */
3276 int oldLast; /* Last line of old viewport */
3277 int deltaY; /* Change in viewport in Y direction */
3278 int reqWidth, reqHeight;
3279
3280 #ifdef notdef
3281 fprintf(stderr, "calling DisplayText(%s)\n", Tk_PathName(htPtr->tkwin));
3282 #endif
3283 htPtr->flags &= ~REDRAW_PENDING;
3284 if (tkwin == NULL) {
3285 return; /* Window has been destroyed */
3286 }
3287 if (htPtr->flags & REQUEST_LAYOUT) {
3288 /*
3289 * Recompute the layout when widgets are created, deleted,
3290 * moved, or resized. Also when text attributes (such as
3291 * font, linespacing) have changed.
3292 */
3293 ComputeLayout(htPtr);
3294 }
3295 htPtr->lastWidth = Tk_Width(tkwin);
3296 htPtr->lastHeight = Tk_Height(tkwin);
3297
3298 /*
3299 * Check the requested width and height. We allow two modes:
3300 * 1) If the user requested value is greater than zero, use it.
3301 * 2) Otherwise, let the window be as big as the virtual text.
3302 * This could be too large to display, so constrain it by
3303 * the maxWidth and maxHeight values.
3304 *
3305 * In any event, we need to calculate the size of the virtual
3306 * text and then make a geometry request. This is so that widgets
3307 * whose size is relative to the master, will be set once.
3308 */
3309 if (htPtr->reqWidth > 0) {
3310 reqWidth = htPtr->reqWidth;
3311 } else {
3312 reqWidth = MIN(htPtr->worldWidth, htPtr->maxWidth);
3313 if (reqWidth < 1) {
3314 reqWidth = 1;
3315 }
3316 }
3317 if (htPtr->reqHeight > 0) {
3318 reqHeight = htPtr->reqHeight;
3319 } else {
3320 reqHeight = MIN(htPtr->worldHeight, htPtr->maxHeight);
3321 if (reqHeight < 1) {
3322 reqHeight = 1;
3323 }
3324 }
3325 if ((reqWidth != Tk_ReqWidth(tkwin)) || (reqHeight != Tk_ReqHeight(tkwin))) {
3326 Tk_GeometryRequest(tkwin, reqWidth, reqHeight);
3327
3328 EventuallyRedraw(htPtr);
3329 return; /* Try again with new geometry */
3330 }
3331 if (!Tk_IsMapped(tkwin)) {
3332 return;
3333 }
3334 /*
3335 * Turn off layout requests here, after the text window has been
3336 * mapped. Otherwise, relative embedded widget size requests wrt
3337 * to the size of parent text window will be wrong.
3338 */
3339 htPtr->flags &= ~REQUEST_LAYOUT;
3340
3341 /* Is there a pending goto request? */
3342 if (htPtr->flags & GOTO_PENDING) {
3343 htPtr->pendingY = htPtr->lineArr[htPtr->reqLineNum].offset;
3344 htPtr->flags &= ~GOTO_PENDING;
3345 }
3346 deltaY = htPtr->pendingY - htPtr->yOffset;
3347 oldFirst = htPtr->first, oldLast = htPtr->last;
3348
3349 /*
3350 * If the viewport has changed size or position, or the text
3351 * and/or embedded widgets have changed, adjust the scrollbars to
3352 * new positions.
3353 */
3354 if (htPtr->flags & TEXT_DIRTY) {
3355 int width, height;
3356
3357 width = Tk_Width(htPtr->tkwin);
3358 height = Tk_Height(htPtr->tkwin);
3359
3360 /* Reset viewport origin and world extents */
3361 htPtr->xOffset = Blt_AdjustViewport(htPtr->pendingX,
3362 htPtr->worldWidth, width,
3363 htPtr->xScrollUnits, BLT_SCROLL_MODE_LISTBOX);
3364 htPtr->yOffset = Blt_AdjustViewport(htPtr->pendingY,
3365 htPtr->worldHeight, height,
3366 htPtr->yScrollUnits, BLT_SCROLL_MODE_LISTBOX);
3367 if (htPtr->xScrollCmdPrefix != NULL) {
3368 Blt_UpdateScrollbar(htPtr->interp, htPtr->xScrollCmdPrefix,
3369 (double)htPtr->xOffset / htPtr->worldWidth,
3370 (double)(htPtr->xOffset + width) / htPtr->worldWidth);
3371 }
3372 if (htPtr->yScrollCmdPrefix != NULL) {
3373 Blt_UpdateScrollbar(htPtr->interp, htPtr->yScrollCmdPrefix,
3374 (double)htPtr->yOffset / htPtr->worldHeight,
3375 (double)(htPtr->yOffset + height) / htPtr->worldHeight);
3376 }
3377 /*
3378 * Given a new viewport or text height, find the first and
3379 * last line numbers of the new viewport.
3380 */
3381 if (GetVisibleLines(htPtr) != TCL_OK) {
3382 return;
3383 }
3384 }
3385 /*
3386 * This is a kludge: Send an expose event before and after
3387 * drawing the page of text. Since moving and resizing of the
3388 * embedded widgets will cause redundant expose events in the parent
3389 * window, the phony events will bracket them indicating no
3390 * action should be taken.
3391 */
3392 SendBogusEvent(tkwin);
3393
3394 /*
3395 * If either the position of the viewport has changed or the size
3396 * of width or height of the entire text have changed, move the
3397 * widgets from the previous viewport out of the current
3398 * viewport. Worry only about the vertical embedded widget movements.
3399 * The page is always draw at full width and the viewport will clip
3400 * the text.
3401 */
3402 if ((htPtr->first != oldFirst) || (htPtr->last != oldLast)
3403 || htPtr->flags & TEXT_DIRTY ) {
3404 int offset;
3405 int i;
3406 int first, last;
3407 Blt_ChainLink *linkPtr;
3408 EmbeddedWidget *winPtr;
3409
3410 /* Figure out which lines are now out of the viewport */
3411
3412 if ((htPtr->first > oldFirst) && (htPtr->first <= oldLast)) {
3413 first = oldFirst, last = htPtr->first;
3414 } else if ((htPtr->last < oldLast) && (htPtr->last >= oldFirst)) {
3415 first = htPtr->last, last = oldLast;
3416 } else {
3417 first = oldFirst, last = oldLast;
3418 }
3419 if (! htPtr->lineArr) return;
3420
3421
3422 if (htPtr->arraySize > 0 && htPtr->nLines > 0) {
3423 for (i = first; i <= last; i++) {
3424 offset = htPtr->lineArr[i].offset;
3425 for (linkPtr = Blt_ChainFirstLink(htPtr->lineArr[i].chainPtr);
3426 linkPtr != NULL; linkPtr = Blt_ChainNextLink(linkPtr)) {
3427 winPtr = Blt_ChainGetValue(linkPtr);
3428 if (winPtr->tkwin != NULL) {
3429 MoveEmbeddedWidget(winPtr, offset);
3430 winPtr->flags &= ~WIDGET_VISIBLE;
3431 }
3432 }
3433 }
3434 }
3435 DrawPage(htPtr, deltaY);
3436 SendBogusEvent(tkwin);
3437
3438 /* Reset flags */
3439 htPtr->flags &= ~TEXT_DIRTY;
3440 }
3441 }
3442
3443 /* Selection Procedures */
3444 /*
3445 *----------------------------------------------------------------------
3446 *
3447 * TextSelectionProc --
3448 *
3449 * This procedure is called back by Tk when the selection is
3450 * requested by someone. It returns part or all of the selection
3451 * in a buffer provided by the caller.
3452 *
3453 * Results:
3454 * The return value is the number of non-NULL bytes stored
3455 * at buffer. Buffer is filled (or partially filled) with a
3456 * NULL-terminated string containing part or all of the selection,
3457 * as given by offset and maxBytes.
3458 *
3459 * Side effects:
3460 * None.
3461 *
3462 *----------------------------------------------------------------------
3463 */
3464 static int
3465 TextSelectionProc(clientData, offset, buffer, maxBytes)
3466 ClientData clientData; /* Information about Text widget. */
3467 int offset; /* Offset within selection of first
3468 * character to be returned. */
3469 char *buffer; /* Location in which to place
3470 * selection. */
3471 int maxBytes; /* Maximum number of bytes to place
3472 * at buffer, not including terminating
3473 * NULL character. */
3474 {
3475 HText *htPtr = clientData;
3476 int size;
3477
3478 if ((htPtr->selFirst < 0) || (!htPtr->exportSelection)) {
3479 return -1;
3480 }
3481 size = (htPtr->selLast - htPtr->selFirst) + 1 - offset;
3482 if (size > maxBytes) {
3483 size = maxBytes;
3484 }
3485 if (size <= 0) {
3486 return 0; /* huh? */
3487 }
3488 strncpy(buffer, htPtr->charArr + htPtr->selFirst + offset, size);
3489 buffer[size] = '\0';
3490 return size;
3491 }
3492
3493 /*
3494 *----------------------------------------------------------------------
3495 *
3496 * TextLostSelection --
3497 *
3498 * This procedure is called back by Tk when the selection is
3499 * grabbed away from a Text widget.
3500 *
3501 * Results:
3502 * None.
3503 *
3504 * Side effects:
3505 * The existing selection is unhighlighted, and the window is
3506 * marked as not containing a selection.
3507 *
3508 *----------------------------------------------------------------------
3509 */
3510 static void
3511 TextLostSelection(clientData)
3512 ClientData clientData; /* Information about Text widget. */
3513 {
3514 HText *htPtr = clientData;
3515
3516 if ((htPtr->selFirst >= 0) && (htPtr->exportSelection)) {
3517 htPtr->selFirst = htPtr->selLast = -1;
3518 EventuallyRedraw(htPtr);
3519 }
3520 }
3521
3522 /*
3523 *----------------------------------------------------------------------
3524 *
3525 * SelectLine --
3526 *
3527 * Modify the selection by moving both its anchored and un-anchored
3528 * ends. This could make the selection either larger or smaller.
3529 *
3530 * Results:
3531 * None.
3532 *
3533 * Side effects:
3534 * The selection changes.
3535 *
3536 *----------------------------------------------------------------------
3537 */
3538 static int
3539 SelectLine(htPtr, tindex)
3540 HText *htPtr; /* Information about widget. */
3541 int tindex; /* Index of element that is to
3542 * become the "other" end of the
3543 * selection. */
3544 {
3545 int selFirst, selLast;
3546 int lineNum;
3547 Line *linePtr;
3548
3549 lineNum = IndexSearch(htPtr, tindex, 0, htPtr->nLines - 1);
3550 if (lineNum < 0) {
3551 char string[200];
3552
3553 sprintf(string, "can't determine line number from index \"%d\"",
3554 tindex);
3555 Tcl_AppendResult(htPtr->interp, string, (char *)NULL);
3556 return TCL_ERROR;
3557 }
3558 linePtr = htPtr->lineArr + lineNum;
3559 /*
3560 * Grab the selection if we don't own it already.
3561 */
3562 if ((htPtr->exportSelection) && (htPtr->selFirst == -1)) {
3563 Tk_OwnSelection(htPtr->tkwin, XA_PRIMARY, TextLostSelection, htPtr);
3564 }
3565 selFirst = linePtr->textStart;
3566 selLast = linePtr->textEnd;
3567 htPtr->selAnchor = tindex;
3568 if ((htPtr->selFirst != selFirst) ||
3569 (htPtr->selLast != selLast)) {
3570 htPtr->selFirst = selFirst;
3571 htPtr->selLast = selLast;
3572 EventuallyRedraw(htPtr);
3573 }
3574 return TCL_OK;
3575 }
3576
3577 /*
3578 *----------------------------------------------------------------------
3579 *
3580 * SelectWord --
3581 *
3582 * Modify the selection by moving both its anchored and un-anchored
3583 * ends. This could make the selection either larger or smaller.
3584 *
3585 * Results:
3586 * None.
3587 *
3588 * Side effects:
3589 * The selection changes.
3590 *
3591 *----------------------------------------------------------------------
3592 */
3593 static int
3594 SelectWord(htPtr, tindex)
3595 HText *htPtr; /* Information about widget. */
3596 int tindex; /* Index of element that is to
3597 * become the "other" end of the
3598 * selection. */
3599 {
3600 int selFirst, selLast;
3601 int i;
3602
3603 for (i = tindex; i < htPtr->nChars; i++) {
3604 if (isspace(UCHAR(htPtr->charArr[i]))) {
3605 break;
3606 }
3607 }
3608 selLast = i - 1;
3609 for (i = tindex; i >= 0; i--) {
3610 if (isspace(UCHAR(htPtr->charArr[i]))) {
3611 break;
3612 }
3613 }
3614 selFirst = i + 1;
3615 if (selFirst > selLast) {
3616 selFirst = selLast = tindex;
3617 }
3618 /*
3619 * Grab the selection if we don't own it already.
3620 */
3621 if ((htPtr->exportSelection) && (htPtr->selFirst == -1)) {
3622 Tk_OwnSelection(htPtr->tkwin, XA_PRIMARY, TextLostSelection, htPtr);
3623 }
3624 htPtr->selAnchor = tindex;
3625 if ((htPtr->selFirst != selFirst) || (htPtr->selLast != selLast)) {
3626 htPtr->selFirst = selFirst, htPtr->selLast = selLast;
3627 EventuallyRedraw(htPtr);
3628 }
3629 return TCL_OK;
3630 }
3631
3632 /*
3633 *----------------------------------------------------------------------
3634 *
3635 * SelectTextBlock --
3636 *
3637 * Modify the selection by moving its un-anchored end. This
3638 * could make the selection either larger or smaller.
3639 *
3640 * Results:
3641 * None.
3642 *
3643 * Side effects:
3644 * The selection changes.
3645 *
3646 *----------------------------------------------------------------------
3647 */
3648 static int
3649 SelectTextBlock(htPtr, tindex)
3650 HText *htPtr; /* Information about widget. */
3651 int tindex; /* Index of element that is to
3652 * become the "other" end of the
3653 * selection. */
3654 {
3655 int selFirst, selLast;
3656
3657 /*
3658 * Grab the selection if we don't own it already.
3659 */
3660
3661 if ((htPtr->exportSelection) && (htPtr->selFirst == -1)) {
3662 Tk_OwnSelection(htPtr->tkwin, XA_PRIMARY, TextLostSelection, htPtr);
3663 }
3664 /* If the anchor hasn't been set yet, assume the beginning of the text*/
3665 if (htPtr->selAnchor < 0) {
3666 htPtr->selAnchor = 0;
3667 }
3668 if (htPtr->selAnchor <= tindex) {
3669 selFirst = htPtr->selAnchor;
3670 selLast = tindex;
3671 } else {
3672 selFirst = tindex;
3673 selLast = htPtr->selAnchor;
3674 }
3675 if ((htPtr->selFirst != selFirst) || (htPtr->selLast != selLast)) {
3676 htPtr->selFirst = selFirst, htPtr->selLast = selLast;
3677 EventuallyRedraw(htPtr);
3678 }
3679 return TCL_OK;
3680 }
3681
3682 /*
3683 *----------------------------------------------------------------------
3684 *
3685 * SelectOp --
3686 *
3687 * This procedure handles the individual options for text
3688 * selections. The selected text is designated by start and end
3689 * indices into the text pool. The selected segment has both a
3690 * anchored and unanchored ends. The following selection
3691 * operations are implemented:
3692 *
3693 * "adjust" - resets either the first or last index
3694 * of the selection.
3695 * "clear" - clears the selection. Sets first/last
3696 * indices to -1.
3697 * "from" - sets the index of the selection anchor.
3698 * "line" - sets the first of last indices to the
3699 * start and end of the line at the
3700 * designated point.
3701 * "present" - return "1" if a selection is available,
3702 * "0" otherwise.
3703 * "range" - sets the first and last indices.
3704 * "to" - sets the index of the un-anchored end.
3705 * "word" - sets the first of last indices to the
3706 * start and end of the word at the
3707 * designated point.
3708 * Results:
3709 * None.
3710 *
3711 * Side effects:
3712 * The selection changes.
3713 *
3714 *----------------------------------------------------------------------
3715 */
3716 static int
3717 SelectOp(htPtr, interp, argc, argv)
3718 HText *htPtr;
3719 Tcl_Interp *interp;
3720 int argc;
3721 char **argv;
3722 {
3723 int iselection;
3724 unsigned int length;
3725 int result = TCL_OK;
3726 char c;
3727
3728 length = strlen(argv[2]);
3729 c = argv[2][0];
3730 if ((c == 'c') && (strncmp(argv[2], "clear", length) == 0)) {
3731 if (argc != 3) {
3732 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3733 " selection clear\"", (char *)NULL);
3734 return TCL_ERROR;
3735 }
3736 if (htPtr->selFirst != -1) {
3737 htPtr->selFirst = htPtr->selLast = -1;
3738 EventuallyRedraw(htPtr);
3739 }
3740 return TCL_OK;
3741 } else if ((c == 'p') && (strncmp(argv[2], "present", length) == 0)) {
3742 if (argc != 3) {
3743 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3744 " selection present\"", (char *)NULL);
3745 return TCL_ERROR;
3746 }
3747 Tcl_AppendResult(interp, (htPtr->selFirst != -1) ? "0" : "1",
3748 (char *)NULL);
3749 return TCL_OK;
3750 } else if ((c == 'r') && (strncmp(argv[2], "range", length) == 0)) {
3751 int selFirst, selLast;
3752
3753 if (argc != 5) {
3754 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3755 " selection range first last\"", (char *)NULL);
3756 return TCL_ERROR;
3757 }
3758 if (GetIndex(htPtr, argv[3], &selFirst) != TCL_OK) {
3759 return TCL_ERROR;
3760 }
3761 if (GetIndex(htPtr, argv[4], &selLast) != TCL_OK) {
3762 return TCL_ERROR;
3763 }
3764 htPtr->selAnchor = selFirst;
3765 SelectTextBlock(htPtr, selLast);
3766 return TCL_OK;
3767 }
3768 if (argc != 4) {
3769 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3770 " selection ", argv[2], " index\"", (char *)NULL);
3771 return TCL_ERROR;
3772 }
3773 if (GetIndex(htPtr, argv[3], &iselection) != TCL_OK) {
3774 return TCL_ERROR;
3775 }
3776 if ((c == 'f') && (strncmp(argv[2], "from", length) == 0)) {
3777 htPtr->selAnchor = iselection;
3778 } else if ((c == 'a') && (strncmp(argv[2], "adjust", length) == 0)) {
3779 int half1, half2;
3780
3781 half1 = (htPtr->selFirst + htPtr->selLast) / 2;
3782 half2 = (htPtr->selFirst + htPtr->selLast + 1) / 2;
3783 if (iselection < half1) {
3784 htPtr->selAnchor = htPtr->selLast;
3785 } else if (iselection > half2) {
3786 htPtr->selAnchor = htPtr->selFirst;
3787 }
3788 result = SelectTextBlock(htPtr, iselection);
3789 } else if ((c == 't') && (strncmp(argv[2], "to", length) == 0)) {
3790 result = SelectTextBlock(htPtr, iselection);
3791 } else if ((c == 'w') && (strncmp(argv[2], "word", length) == 0)) {
3792 result = SelectWord(htPtr, iselection);
3793 } else if ((c == 'l') && (strncmp(argv[2], "line", length) == 0)) {
3794 result = SelectLine(htPtr, iselection);
3795 } else {
3796 Tcl_AppendResult(interp, "bad selection operation \"", argv[2],
3797 "\": should be \"adjust\", \"clear\", \"from\", \"line\", \
3798 \"present\", \"range\", \"to\", or \"word\"", (char *)NULL);
3799 return TCL_ERROR;
3800 }
3801 return result;
3802 }
3803
3804 /*
3805 *----------------------------------------------------------------------
3806 *
3807 * GotoOp --
3808 *
3809 * Move the top line of the viewport to the new location based
3810 * upon the given line number. Force out-of-range requests to the
3811 * top or bottom of text.
3812 *
3813 * Results:
3814 * A standard Tcl result. If TCL_OK, interp->result contains the
3815 * current line number.
3816 *
3817 * Side effects:
3818 * At the next idle point, the text viewport will be move to the
3819 * new line.
3820 *
3821 *----------------------------------------------------------------------
3822 */
3823 /*ARGSUSED*/
3824 static int
3825 GotoOp(htPtr, interp, argc, argv)
3826 HText *htPtr;
3827 Tcl_Interp *interp; /* Not used. */
3828 int argc;
3829 char **argv;
3830 {
3831 int line;
3832
3833 line = htPtr->first;
3834 if (argc == 3) {
3835 int tindex;
3836
3837 if (GetIndex(htPtr, argv[2], &tindex) != TCL_OK) {
3838 return TCL_ERROR;
3839 }
3840 line = IndexSearch(htPtr, tindex, 0, htPtr->nLines - 1);
3841 if (line < 0) {
3842 char string[200];
3843
3844 sprintf(string, "can't determine line number from index \"%d\"",
3845 tindex);
3846 Tcl_AppendResult(htPtr->interp, string, (char *)NULL);
3847 return TCL_ERROR;
3848 }
3849 htPtr->reqLineNum = line;
3850 htPtr->flags |= TEXT_DIRTY;
3851
3852 /*
3853 * Make only a request for a change in the viewport. Defer
3854 * the actual scrolling until the text layout is adjusted at
3855 * the next idle point.
3856 */
3857 if (line != htPtr->first) {
3858 htPtr->flags |= GOTO_PENDING;
3859 EventuallyRedraw(htPtr);
3860 }
3861 }
3862 Tcl_SetResult(htPtr->interp, Blt_Itoa(line), TCL_VOLATILE);
3863 return TCL_OK;
3864 }
3865
3866
3867 static int
3868 XViewOp(htPtr, interp, argc, argv)
3869 HText *htPtr;
3870 Tcl_Interp *interp;
3871 int argc;
3872 char **argv;
3873 {
3874 int width, worldWidth;
3875
3876 width = Tk_Width(htPtr->tkwin);
3877 worldWidth = htPtr->worldWidth;
3878 if (argc == 2) {
3879 double fract;
3880
3881 /* Report first and last fractions */
3882 fract = (double)htPtr->xOffset / worldWidth;
3883 Tcl_AppendElement(interp, Blt_Dtoa(interp, CLAMP(fract, 0.0, 1.0)));
3884 fract = (double)(htPtr->xOffset + width) / worldWidth;
3885 Tcl_AppendElement(interp, Blt_Dtoa(interp, CLAMP(fract, 0.0, 1.0)));
3886 return TCL_OK;
3887 }
3888 htPtr->pendingX = htPtr->xOffset;
3889 if (Blt_GetScrollInfo(interp, argc - 2, argv + 2, &(htPtr->pendingX),
3890 worldWidth, width, htPtr->xScrollUnits, BLT_SCROLL_MODE_LISTBOX)
3891 != TCL_OK) {
3892 return TCL_ERROR;
3893 }
3894 htPtr->flags |= TEXT_DIRTY;
3895 EventuallyRedraw(htPtr);
3896 return TCL_OK;
3897 }
3898
3899 static int
3900 YViewOp(htPtr, interp, argc, argv)
3901 HText *htPtr;
3902 Tcl_Interp *interp;
3903 int argc;
3904 char **argv;
3905 {
3906 int height, worldHeight;
3907
3908 height = Tk_Height(htPtr->tkwin);
3909 worldHeight = htPtr->worldHeight;
3910 if (argc == 2) {
3911 double fract;
3912
3913 /* Report first and last fractions */
3914 fract = (double)htPtr->yOffset / worldHeight;
3915 Tcl_AppendElement(interp, Blt_Dtoa(interp, CLAMP(fract, 0.0, 1.0)));
3916 fract = (double)(htPtr->yOffset + height) / worldHeight;
3917 Tcl_AppendElement(interp, Blt_Dtoa(interp, CLAMP(fract, 0.0, 1.0)));
3918 return TCL_OK;
3919 }
3920 htPtr->pendingY = htPtr->yOffset;
3921 if (Blt_GetScrollInfo(interp, argc - 2, argv + 2, &(htPtr->pendingY),
3922 worldHeight, height, htPtr->yScrollUnits, BLT_SCROLL_MODE_LISTBOX)
3923 != TCL_OK) {
3924 return TCL_ERROR;
3925 }
3926 htPtr->flags |= TEXT_DIRTY;
3927 EventuallyRedraw(htPtr);
3928 return TCL_OK;
3929 }
3930
3931 /*
3932 * ----------------------------------------------------------------------
3933 *
3934 * AppendOp --
3935 *
3936 * This procedure embeds a Tk widget into the hypertext.
3937 *
3938 * Results:
3939 * A standard Tcl result.
3940 *
3941 * Side effects:
3942 * Memory is allocated. EmbeddedWidget gets configured.
3943 *
3944 * ----------------------------------------------------------------------
3945 */
3946 static int
3947 AppendOp(htPtr, interp, argc, argv)
3948 HText *htPtr; /* Hypertext widget */
3949 Tcl_Interp *interp; /* Interpreter associated with widget */
3950 int argc; /* Number of arguments. */
3951 char **argv; /* Argument strings. */
3952 {
3953 Line *linePtr;
3954 EmbeddedWidget *winPtr;
3955
3956 winPtr = CreateEmbeddedWidget(htPtr, argv[2]);
3957 if (winPtr == NULL) {
3958 return TCL_ERROR;
3959 }
3960 if (Blt_ConfigureWidget(interp, htPtr->tkwin, widgetConfigSpecs,
3961 argc - 3, argv + 3, (char *)winPtr, 0) != TCL_OK) {
3962 return TCL_ERROR;
3963 }
3964 /*
3965 * Append widget to list of embedded widgets of the last line.
3966 */
3967 linePtr = GetLastLine(htPtr);
3968 if (linePtr == NULL) {
3969 Tcl_AppendResult(htPtr->interp, "can't allocate line structure",
3970 (char *)NULL);
3971 return TCL_ERROR;
3972 }
3973 Blt_ChainAppend(linePtr->chainPtr, winPtr);
3974 linePtr->width += winPtr->cavityWidth;
3975 winPtr->precedingTextEnd = linePtr->textEnd;
3976
3977 htPtr->flags |= (WIDGET_APPENDED | REQUEST_LAYOUT);
3978 EventuallyRedraw(htPtr);
3979 return TCL_OK;
3980 }
3981
3982 /*
3983 *----------------------------------------------------------------------
3984 *
3985 * WindowsOp --
3986 *
3987 * Returns a list of all the pathNames of embedded widgets of the
3988 * HText widget. If a pattern argument is given, only the names
3989 * of windows matching it will be placed into the list.
3990 *
3991 * Results:
3992 * Standard Tcl result. If TCL_OK, interp->result will contain
3993 * the list of the embedded widget pathnames. Otherwise it will
3994 * contain an error message.
3995 *
3996 *----------------------------------------------------------------------
3997 */
3998 static int
3999 WindowsOp(htPtr, interp, argc, argv)
4000 HText *htPtr; /* Hypertext widget record */
4001 Tcl_Interp *interp; /* Interpreter associated with widget */
4002 int argc;
4003 char **argv;
4004 {
4005 EmbeddedWidget *winPtr;
4006 Blt_HashEntry *hPtr;
4007 Blt_HashSearch cursor;
4008 char *name;
4009
4010 for (hPtr = Blt_FirstHashEntry(&(htPtr->widgetTable), &cursor);
4011 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
4012 winPtr = (EmbeddedWidget *)Blt_GetHashValue(hPtr);
4013 if (winPtr->tkwin == NULL) {
4014 fprintf(stderr, "window `%s' is null\n",
4015 Tk_PathName(Blt_GetHashKey(&(htPtr->widgetTable), hPtr)));
4016 continue;
4017 }
4018 name = Tk_PathName(winPtr->tkwin);
4019 if ((argc == 2) || (Tcl_StringMatch(name, argv[2]))) {
4020 Tcl_AppendElement(interp, name);
4021 }
4022 }
4023 return TCL_OK;
4024 }
4025
4026 /*
4027 *----------------------------------------------------------------------
4028 *
4029 * CgetOp --
4030 *
4031 *----------------------------------------------------------------------
4032 */
4033 /*ARGSUSED*/
4034 static int
4035 CgetOp(htPtr, interp, argc, argv)
4036 HText *htPtr;
4037 Tcl_Interp *interp;
4038 int argc; /* Not used. */
4039 char **argv;
4040 {
4041 char *itemPtr;
4042 Tk_ConfigSpec *specsPtr;
4043
4044 if ((argc > 2) && (argv[2][0] == '.')) {
4045 Tk_Window tkwin;
4046 EmbeddedWidget *winPtr;
4047
4048 /* EmbeddedWidget window to be configured */
4049 tkwin = Tk_NameToWindow(interp, argv[2], htPtr->tkwin);
4050 if (tkwin == NULL) {
4051 return TCL_ERROR;
4052 }
4053 winPtr = FindEmbeddedWidget(htPtr, tkwin);
4054 if (winPtr == NULL) {
4055 Tcl_AppendResult(interp, "window \"", argv[2],
4056 "\" is not managed by \"", argv[0], "\"", (char *)NULL);
4057 return TCL_ERROR;
4058 }
4059 specsPtr = widgetConfigSpecs;
4060 itemPtr = (char *)winPtr;
4061 argv++;
4062 } else {
4063 specsPtr = configSpecs;
4064 itemPtr = (char *)htPtr;
4065 }
4066 return Tk_ConfigureValue(interp, htPtr->tkwin, specsPtr, itemPtr,
4067 argv[2], 0);
4068 }
4069
4070 /*
4071 *----------------------------------------------------------------------
4072 *
4073 * ConfigureOp --
4074 *
4075 * This procedure is called to process an argv/argc list, plus
4076 * the Tk option database, in order to configure (or reconfigure)
4077 * a hypertext widget.
4078 *
4079 * Results:
4080 * A standard Tcl result. If TCL_ERROR is returned, then
4081 * interp->result contains an error message.
4082 *
4083 * Side effects:
4084 * Configuration information, such as text string, colors, font,
4085 * etc. get set for htPtr; old resources get freed, if there were any.
4086 * The hypertext is redisplayed.
4087 *
4088 *----------------------------------------------------------------------
4089 */
4090 static int
4091 ConfigureOp(htPtr, interp, argc, argv)
4092 HText *htPtr;
4093 Tcl_Interp *interp;
4094 int argc;
4095 char **argv;
4096 {
4097 char *itemPtr;
4098 Tk_ConfigSpec *specsPtr;
4099
4100 if ((argc > 2) && (argv[2][0] == '.')) {
4101 Tk_Window tkwin;
4102 EmbeddedWidget *winPtr;
4103
4104 /* EmbeddedWidget window to be configured */
4105 tkwin = Tk_NameToWindow(interp, argv[2], htPtr->tkwin);
4106 if (tkwin == NULL) {
4107 return TCL_ERROR;
4108 }
4109 winPtr = FindEmbeddedWidget(htPtr, tkwin);
4110 if (winPtr == NULL) {
4111 Tcl_AppendResult(interp, "window \"", argv[2],
4112 "\" is not managed by \"", argv[0], "\"", (char *)NULL);
4113 return TCL_ERROR;
4114 }
4115 specsPtr = widgetConfigSpecs;
4116 itemPtr = (char *)winPtr;
4117 argv++;
4118 argc--;
4119 } else {
4120 specsPtr = configSpecs;
4121 itemPtr = (char *)htPtr;
4122 }
4123 if (argc == 2) {
4124 return Tk_ConfigureInfo(interp, htPtr->tkwin, specsPtr, itemPtr,
4125 (char *)NULL, 0);
4126 } else if (argc == 3) {
4127 return Tk_ConfigureInfo(interp, htPtr->tkwin, specsPtr, itemPtr,
4128 argv[2], 0);
4129 }
4130 if (Blt_ConfigureWidget(interp, htPtr->tkwin, specsPtr, argc - 2,
4131 argv + 2, itemPtr, TK_CONFIG_ARGV_ONLY) != TCL_OK) {
4132
4133 /*
4134 * in 8.5 cant tell if in option was modified
4135 * supposed to convert to Tk_SetOptions
4136 */
4137 return TCL_ERROR;
4138 }
4139 if (itemPtr == (char *)htPtr) {
4140 /* Reconfigure the master */
4141 if (ConfigureText(interp, htPtr) != TCL_OK) {
4142 return TCL_ERROR;
4143 }
4144 } else {
4145 htPtr->flags |= REQUEST_LAYOUT;
4146 }
4147 EventuallyRedraw(htPtr);
4148 return TCL_OK;
4149 }
4150
4151 /*
4152 *----------------------------------------------------------------------
4153 *
4154 * ScanOp --
4155 *
4156 * Implements the quick scan for hypertext widgets.
4157 *
4158 *----------------------------------------------------------------------
4159 */
4160 /*ARGSUSED*/
4161 static int
4162 ScanOp(htPtr, interp, argc, argv)
4163 HText *htPtr;
4164 Tcl_Interp *interp;
4165 int argc; /* Not used. */
4166 char **argv;
4167 {
4168 int x, y;
4169 char c;
4170 unsigned int length;
4171
4172
4173 if (Blt_GetXY(interp, htPtr->tkwin, argv[3], &x, &y) != TCL_OK) {
4174 return TCL_ERROR;
4175 }
4176 c = argv[2][0];
4177 length = strlen(argv[2]);
4178 if ((c == 'm') && (strncmp(argv[2], "mark", length) == 0)) {
4179 htPtr->scanMark.x = x, htPtr->scanMark.y = y;
4180 htPtr->scanPt.x = htPtr->xOffset;
4181 htPtr->scanPt.y = htPtr->yOffset;
4182
4183 } else if ((c == 'd') && (strncmp(argv[2], "dragto", length) == 0)) {
4184 int px, py;
4185
4186 px = htPtr->scanPt.x - (10 * (x - htPtr->scanMark.x));
4187 py = htPtr->scanPt.y - (10 * (y - htPtr->scanMark.y));
4188
4189 if (px < 0) {
4190 px = htPtr->scanPt.x = 0;
4191 htPtr->scanMark.x = x;
4192 } else if (px >= htPtr->worldWidth) {
4193 px = htPtr->scanPt.x = htPtr->worldWidth - htPtr->xScrollUnits;
4194 htPtr->scanMark.x = x;
4195 }
4196 if (py < 0) {
4197 py = htPtr->scanPt.y = 0;
4198 htPtr->scanMark.y = y;
4199 } else if (py >= htPtr->worldHeight) {
4200 py = htPtr->scanPt.y = htPtr->worldHeight - htPtr->yScrollUnits;
4201 htPtr->scanMark.y = y;
4202 }
4203 if ((py != htPtr->pendingY) || (px != htPtr->pendingX)) {
4204 htPtr->pendingX = px, htPtr->pendingY = py;
4205 htPtr->flags |= TEXT_DIRTY;
4206 EventuallyRedraw(htPtr);
4207 }
4208 } else {
4209 Tcl_AppendResult(interp, "bad scan operation \"", argv[2],
4210 "\": should be either \"mark\" or \"dragto\"", (char *)NULL);
4211 return TCL_ERROR;
4212 }
4213 return TCL_OK;
4214 }
4215
4216 /*
4217 *----------------------------------------------------------------------
4218 *
4219 * SearchOp --
4220 *
4221 * Returns the linenumber of the next line matching the given
4222 * pattern within the range of lines provided. If the first
4223 * line number is greater than the last, the search is done in
4224 * reverse.
4225 *
4226 *----------------------------------------------------------------------
4227 */
4228 static int
4229 SearchOp(htPtr, interp, argc, argv)
4230 HText *htPtr;
4231 Tcl_Interp *interp;
4232 int argc;
4233 char **argv;
4234 {
4235 char *startPtr, *endPtr;
4236 char saved;
4237 Tcl_RegExp regExpToken;
4238 int iFirst, iLast;
4239 int matchStart, matchEnd;
4240 int match;
4241
4242 regExpToken = Tcl_RegExpCompile(interp, argv[2]);
4243 if (regExpToken == NULL) {
4244 return TCL_ERROR;
4245 }
4246 iFirst = 0;
4247 iLast = htPtr->nChars;
4248 if (argc > 3) {
4249 if (GetIndex(htPtr, argv[3], &iFirst) != TCL_OK) {
4250 return TCL_ERROR;
4251 }
4252 }
4253 if (argc == 4) {
4254 if (GetIndex(htPtr, argv[4], &iLast) != TCL_OK) {
4255 return TCL_ERROR;
4256 }
4257 }
4258 if (iLast < iFirst) {
4259 return TCL_ERROR;
4260 }
4261 matchStart = matchEnd = -1;
4262 startPtr = htPtr->charArr + iFirst;
4263 endPtr = htPtr->charArr + (iLast + 1);
4264 saved = *endPtr;
4265 *endPtr = '\0'; /* Make the line a string by changing the
4266 * '\n' into a NUL byte before searching */
4267 match = Tcl_RegExpExec(interp, regExpToken, startPtr, startPtr);
4268 *endPtr = saved;
4269 if (match < 0) {
4270 return TCL_ERROR;
4271 } else if (match > 0) {
4272 Tcl_RegExpRange(regExpToken, 0, &startPtr, &endPtr);
4273 if ((startPtr != NULL) || (endPtr != NULL)) {
4274 matchStart = startPtr - htPtr->charArr;
4275 matchEnd = endPtr - htPtr->charArr - 1;
4276 }
4277 }
4278 if (match > 0) {
4279 Tcl_AppendElement(interp, Blt_Itoa(matchStart));
4280 Tcl_AppendElement(interp, Blt_Itoa(matchEnd));
4281 } else {
4282 Tcl_ResetResult(interp);
4283 }
4284 return TCL_OK;
4285 }
4286
4287 /*
4288 *----------------------------------------------------------------------
4289 *
4290 * RangeOp --
4291 *
4292 * Returns the characters designated by the range of elements.
4293 *
4294 *----------------------------------------------------------------------
4295 */
4296 static int
4297 RangeOp(htPtr, interp, argc, argv)
4298 HText *htPtr;
4299 Tcl_Interp *interp;
4300 int argc;
4301 char **argv;
4302 {
4303 char *startPtr, *endPtr;
4304 char saved;
4305 int textFirst, textLast;
4306
4307 textFirst = htPtr->selFirst;
4308 textLast = htPtr->selLast;
4309 if (textFirst < 0) {
4310 textFirst = 0;
4311 textLast = htPtr->nChars - 1;
4312 }
4313 if (argc > 2) {
4314 if (GetIndex(htPtr, argv[2], &textFirst) != TCL_OK) {
4315 return TCL_ERROR;
4316 }
4317 }
4318 if (argc == 4) {
4319 if (GetIndex(htPtr, argv[3], &textLast) != TCL_OK) {
4320 return TCL_ERROR;
4321 }
4322 }
4323 if (textLast < textFirst) {
4324 Tcl_AppendResult(interp, "first index is greater than last", (char *)NULL);
4325 return TCL_ERROR;
4326 }
4327 startPtr = htPtr->charArr + textFirst;
4328 endPtr = htPtr->charArr + (textLast + 1);
4329 saved = *endPtr;
4330 *endPtr = '\0'; /* Make the line into a string by
4331 * changing the * '\n' into a '\0'
4332 * before copying */
4333 Tcl_SetResult(interp, startPtr, TCL_VOLATILE);
4334 *endPtr = saved;
4335 return TCL_OK;
4336 }
4337
4338 /*
4339 *----------------------------------------------------------------------
4340 *
4341 * IndexOp --
4342 *
4343 *----------------------------------------------------------------------
4344 */
4345 /*ARGSUSED*/
4346 static int
4347 IndexOp(htPtr, interp, argc, argv)
4348 HText *htPtr;
4349 Tcl_Interp *interp;
4350 int argc; /* Not used. */
4351 char **argv;
4352 {
4353 int tindex;
4354
4355 if (GetIndex(htPtr, argv[2], &tindex) != TCL_OK) {
4356 return TCL_ERROR;
4357 }
4358 Tcl_SetResult(interp, Blt_Itoa(tindex), TCL_VOLATILE);
4359 return TCL_OK;
4360 }
4361
4362 /*
4363 *----------------------------------------------------------------------
4364 *
4365 * LinePosOp --
4366 *
4367 *----------------------------------------------------------------------
4368 */
4369 /*ARGSUSED*/
4370 static int
4371 LinePosOp(htPtr, interp, argc, argv)
4372 HText *htPtr;
4373 Tcl_Interp *interp;
4374 int argc; /* Not used. */
4375 char **argv;
4376 {
4377 int line, cpos, tindex;
4378 char string[200];
4379
4380 if (GetIndex(htPtr, argv[2], &tindex) != TCL_OK) {
4381 return TCL_ERROR;
4382 }
4383 if (GetTextPosition(htPtr, tindex, &line, &cpos) != TCL_OK) {
4384 return TCL_ERROR;
4385 }
4386 sprintf(string, "%d.%d", line, cpos);
4387 Tcl_SetResult(interp, string, TCL_VOLATILE);
4388 return TCL_OK;
4389 }
4390
4391 /*
4392 * --------------------------------------------------------------
4393 *
4394 * TextWidgetCmd --
4395 *
4396 * This procedure is invoked to process the Tcl command that
4397 * corresponds to a widget managed by this module. See the user
4398 * documentation for details on what it does.
4399 *
4400 * Results:
4401 * A standard Tcl result.
4402 *
4403 * Side effects:
4404 * See the user documentation.
4405 *
4406 * --------------------------------------------------------------
4407 */
4408
4409 static Blt_OpSpec textOps[] =
4410 {
4411 {"append", 1, (Blt_Op)AppendOp, 3, 0, "window ?option value?...",},
4412 {"cget", 2, (Blt_Op)CgetOp, 3, 3, "?window? option",},
4413 {"configure", 2, (Blt_Op)ConfigureOp, 2, 0,
4414 "?window? ?option value?...",},
4415 {"gotoline", 2, (Blt_Op)GotoOp, 2, 3, "?line?",},
4416 {"index", 1, (Blt_Op)IndexOp, 3, 3, "string",},
4417 {"linepos", 1, (Blt_Op)LinePosOp, 3, 3, "string",},
4418 {"range", 2, (Blt_Op)RangeOp, 2, 4, "?from? ?to?",},
4419 {"scan", 2, (Blt_Op)ScanOp, 4, 4, "oper @x,y",},
4420 {"search", 3, (Blt_Op)SearchOp, 3, 5, "pattern ?from? ?to?",},
4421 {"selection", 3, (Blt_Op)SelectOp, 3, 5, "oper ?index?",},
4422 {"windows", 6, (Blt_Op)WindowsOp, 2, 3, "?pattern?",},
4423 {"xview", 1, (Blt_Op)XViewOp, 2, 5,
4424 "?moveto fract? ?scroll number what?",},
4425 {"yview", 1, (Blt_Op)YViewOp, 2, 5,
4426 "?moveto fract? ?scroll number what?",},
4427 };
4428 static int nTextOps = sizeof(textOps) / sizeof(Blt_OpSpec);
4429
4430 static int
4431 TextWidgetCmd(clientData, interp, argc, argv)
4432 ClientData clientData; /* Information about hypertext widget. */
4433 Tcl_Interp *interp; /* Current interpreter. */
4434 int argc; /* Number of arguments. */
4435 char **argv; /* Argument strings. */
4436 {
4437 Blt_Op proc;
4438 int result;
4439 HText *htPtr = clientData;
4440
4441 proc = Blt_GetOp(interp, nTextOps, textOps, BLT_OP_ARG1, argc, argv, 0);
4442 if (proc == NULL) {
4443 return TCL_ERROR;
4444 }
4445 Tcl_Preserve(htPtr);
4446 result = (*proc) (htPtr, interp, argc, argv);
4447 Tcl_Release(htPtr);
4448 return result;
4449 }
4450
4451 /*
4452 * --------------------------------------------------------------
4453 *
4454 * TextCmd --
4455 *
4456 * This procedure is invoked to process the "htext" Tcl command.
4457 * See the user documentation for details on what it does.
4458 *
4459 * Results:
4460 * A standard Tcl result.
4461 *
4462 * Side effects:
4463 * See the user documentation.
4464 *
4465 * --------------------------------------------------------------
4466 */
4467 /* ARGSUSED */
4468 static int
4469 TextCmd(clientData, interp, argc, argv)
4470 ClientData clientData; /* Main window associated with interpreter. */
4471 Tcl_Interp *interp; /* Current interpreter. */
4472 int argc; /* Number of arguments. */
4473 char **argv; /* Argument strings. */
4474 {
4475 HText *htPtr;
4476 Screen *screenPtr;
4477 Tk_Window tkwin;
4478
4479 if (argc < 2) {
4480 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4481 " pathName ?option value?...\"", (char *)NULL);
4482 return TCL_ERROR;
4483 }
4484 htPtr = Blt_Calloc(1, sizeof(HText));
4485 assert(htPtr);
4486 tkwin = Tk_MainWindow(interp);
4487 tkwin = Tk_CreateWindowFromPath(interp, tkwin, argv[1], (char *)NULL);
4488 if (tkwin == NULL) {
4489 Blt_Free(htPtr);
4490 return TCL_ERROR;
4491 }
4492 /* Initialize the new hypertext widget */
4493
4494 Tk_SetClass(tkwin, "Htext");
4495 htPtr->tkwin = tkwin;
4496 htPtr->display = Tk_Display(tkwin);
4497 htPtr->interp = interp;
4498 htPtr->nLines = htPtr->arraySize = 0;
4499 htPtr->leader = 1;
4500 htPtr->xScrollUnits = htPtr->yScrollUnits = 10;
4501 htPtr->nRows = htPtr->nColumns = 0;
4502 htPtr->selFirst = htPtr->selLast = -1;
4503 htPtr->selAnchor = 0;
4504 htPtr->exportSelection = TRUE;
4505 htPtr->selBorderWidth = 2;
4506 screenPtr = Tk_Screen(htPtr->tkwin);
4507 htPtr->maxWidth = WidthOfScreen(screenPtr);
4508 htPtr->maxHeight = HeightOfScreen(screenPtr);
4509 Blt_InitHashTable(&(htPtr->widgetTable), BLT_ONE_WORD_KEYS);
4510
4511 Tk_CreateSelHandler(tkwin, XA_PRIMARY, XA_STRING, TextSelectionProc,
4512 htPtr, XA_STRING);
4513 Tk_CreateEventHandler(tkwin, ExposureMask | StructureNotifyMask,
4514 TextEventProc, htPtr);
4515 #if (TK_MAJOR_VERSION > 4)
4516 Blt_SetWindowInstanceData(tkwin, htPtr);
4517 #endif
4518 /*
4519 * -----------------------------------------------------------------
4520 *
4521 * Create the widget command before configuring the widget. This
4522 * is because the "-file" and "-text" options may have embedded
4523 * commands that self-reference the widget through the
4524 * "$blt_htext(widget)" variable.
4525 *
4526 * ------------------------------------------------------------------
4527 */
4528 htPtr->cmdToken = Tcl_CreateCommand(interp, argv[1], TextWidgetCmd, htPtr,
4529 TextDeleteCmdProc);
4530 #ifdef ITCL_NAMESPACES
4531 Itk_SetWidgetCommand(htPtr->tkwin, htPtr->cmdToken);
4532 #endif
4533 if ((Blt_ConfigureWidget(interp, htPtr->tkwin, configSpecs, argc - 2,
4534 argv + 2, (char *)htPtr, 0) != TCL_OK) ||
4535 (ConfigureText(interp, htPtr) != TCL_OK)) {
4536 Tk_DestroyWindow(htPtr->tkwin);
4537 return TCL_ERROR;
4538 }
4539 Tcl_SetResult(interp, Tk_PathName(htPtr->tkwin), TCL_VOLATILE);
4540 Tk_SetClassProcs(tkwin, &htextClass, (ClientData)htPtr);
4541 return TCL_OK;
4542 }
4543
4544 int
4545 Blt_HtextInit(interp)
4546 Tcl_Interp *interp;
4547 {
4548 static Blt_CmdSpec cmdSpec =
4549 {"htext", TextCmd,};
4550
4551 if (Blt_InitCmd(interp, "blt", &cmdSpec) == NULL) {
4552 return TCL_ERROR;
4553 }
4554 return TCL_OK;
4555 }
4556
4557 #endif /* NO_HTEXT */