Debian Codegraph @

pTk/mTk/generic/tkText.c

raw symbols SCIP
1 /*
2 * tkText.c --
3 *
4 * This module provides a big chunk of the implementation of
5 * multi-line editable text widgets for Tk. Among other things,
6 * it provides the Tcl command interfaces to text widgets and
7 * the display code. The B-tree representation of text is
8 * implemented elsewhere.
9 *
10 * Copyright (c) 1992-1994 The Regents of the University of California.
11 * Copyright (c) 1994-1996 Sun Microsystems, Inc.
12 * Copyright (c) 1999 by Scriptics Corporation.
13 *
14 * See the file "license.terms" for information on usage and redistribution
15 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
16 *
17 * RCS: @(#) $Id: tkText.c,v 1.33 2003/02/18 21:53:59 hobbs Exp $
18 */
19 #define NO_COREXT
20
21 static char sccsid[] = "@(#) tkText.c 1.82 95/08/21 11:46:27";
22
23 #include "default.h"
24 #include "tkPort.h"
25 #include "tkInt.h"
26 #include "tkUndo.h"
27
28 #if defined(MAC_TCL) || defined(MAC_OSX_TK)
29 #define Style TkStyle
30 #define DInfo TkDInfo
31 #endif
32
33 #include "tkText.h"
34
35 /*
36 * Custom options for handling "-state"
37 */
38
39 static Tk_CustomOption stateOption = {
40 TkStateParseProc,
41 TkStatePrintProc,
42 (ClientData) NULL /* only "normal" and "disabled" */
43 };
44
45 static Tk_CustomOption tileOption = {
46 Tk_TileParseProc,
47 Tk_TilePrintProc,
48 (ClientData) NULL
49 };
50
51 static Tk_CustomOption offsetOption = {
52 Tk_OffsetParseProc,
53 Tk_OffsetPrintProc,
54 (ClientData) NULL
55 };
56
57
58 /*
59 * Information used to parse text configuration options:
60 */
61
62 static Tk_ConfigSpec configSpecs[] = {
63 {TK_CONFIG_BOOLEAN, "-autoseparators", "autoSeparators",
64 "AutoSeparators", DEF_TEXT_AUTO_SEPARATORS,
65 Tk_Offset(TkText, autoSeparators), 0},
66 {TK_CONFIG_BORDER, "-background", "background", "Background",
67 DEF_TEXT_BG_COLOR, Tk_Offset(TkText, border), TK_CONFIG_COLOR_ONLY},
68 {TK_CONFIG_BORDER, "-background", "background", "Background",
69 DEF_TEXT_BG_MONO, Tk_Offset(TkText, border), TK_CONFIG_MONO_ONLY},
70 {TK_CONFIG_SYNONYM, "-bd", "borderWidth", (char *) NULL,
71 (char *) NULL, 0, 0},
72 {TK_CONFIG_SYNONYM, "-bg", "background", (char *) NULL,
73 (char *) NULL, 0, 0},
74 {TK_CONFIG_PIXELS, "-borderwidth", "borderWidth", "BorderWidth",
75 DEF_TEXT_BORDER_WIDTH, Tk_Offset(TkText, borderWidth), 0},
76 {TK_CONFIG_ACTIVE_CURSOR, "-cursor", "cursor", "Cursor",
77 DEF_TEXT_CURSOR, Tk_Offset(TkText, cursor), TK_CONFIG_NULL_OK},
78 {TK_CONFIG_BOOLEAN, "-exportselection", "exportSelection",
79 "ExportSelection", DEF_TEXT_EXPORT_SELECTION,
80 Tk_Offset(TkText, exportSelection), 0},
81 {TK_CONFIG_SYNONYM, "-fg", "foreground", (char *) NULL,
82 (char *) NULL, 0, 0},
83 {TK_CONFIG_FONT, "-font", "font", "Font",
84 DEF_TEXT_FONT, Tk_Offset(TkText, tkfont), 0},
85 {TK_CONFIG_COLOR, "-foreground", "foreground", "Foreground",
86 DEF_TEXT_FG, Tk_Offset(TkText, fgColor), 0},
87 {TK_CONFIG_PIXELS, "-height", "height", "Height",
88 DEF_TEXT_HEIGHT, Tk_Offset(TkText, height), 0},
89 {TK_CONFIG_COLOR, "-highlightbackground", "highlightBackground",
90 "HighlightBackground", DEF_TEXT_HIGHLIGHT_BG,
91 Tk_Offset(TkText, highlightBgColorPtr), 0},
92 {TK_CONFIG_COLOR, "-highlightcolor", "highlightColor", "HighlightColor",
93 DEF_TEXT_HIGHLIGHT, Tk_Offset(TkText, highlightColorPtr), 0},
94 {TK_CONFIG_PIXELS, "-highlightthickness", "highlightThickness",
95 "HighlightThickness",
96 DEF_TEXT_HIGHLIGHT_WIDTH, Tk_Offset(TkText, highlightWidth), 0},
97 {TK_CONFIG_BORDER, "-insertbackground", "insertBackground", "Foreground",
98 DEF_TEXT_INSERT_BG, Tk_Offset(TkText, insertBorder), 0},
99 {TK_CONFIG_PIXELS, "-insertborderwidth", "insertBorderWidth", "BorderWidth",
100 DEF_TEXT_INSERT_BD_COLOR, Tk_Offset(TkText, insertBorderWidth),
101 TK_CONFIG_COLOR_ONLY},
102 {TK_CONFIG_PIXELS, "-insertborderwidth", "insertBorderWidth", "BorderWidth",
103 DEF_TEXT_INSERT_BD_MONO, Tk_Offset(TkText, insertBorderWidth),
104 TK_CONFIG_MONO_ONLY},
105 {TK_CONFIG_INT, "-insertofftime", "insertOffTime", "OffTime",
106 DEF_TEXT_INSERT_OFF_TIME, Tk_Offset(TkText, insertOffTime), 0},
107 {TK_CONFIG_INT, "-insertontime", "insertOnTime", "OnTime",
108 DEF_TEXT_INSERT_ON_TIME, Tk_Offset(TkText, insertOnTime), 0},
109 {TK_CONFIG_PIXELS, "-insertwidth", "insertWidth", "InsertWidth",
110 DEF_TEXT_INSERT_WIDTH, Tk_Offset(TkText, insertWidth), 0},
111 {TK_CONFIG_INT, "-maxundo", "maxUndo", "MaxUndo",
112 DEF_TEXT_MAX_UNDO, Tk_Offset(TkText, maxUndo), 0},
113 #ifdef NOT_YET
114 {TK_CONFIG_CUSTOM, "-offset", "offset", "Offset", "0 0",
115 Tk_Offset(TkText, tsoffset), TK_CONFIG_DONT_SET_DEFAULT,
116 &offsetOption},
117 #endif
118 {TK_CONFIG_PIXELS, "-padx", "padX", "Pad",
119 DEF_TEXT_PADX, Tk_Offset(TkText, padX), 0},
120 {TK_CONFIG_PIXELS, "-pady", "padY", "Pad",
121 DEF_TEXT_PADY, Tk_Offset(TkText, padY), 0},
122 {TK_CONFIG_RELIEF, "-relief", "relief", "Relief",
123 DEF_TEXT_RELIEF, Tk_Offset(TkText, relief), 0},
124 {TK_CONFIG_BORDER, "-selectbackground", "selectBackground", "Foreground",
125 DEF_TEXT_SELECT_COLOR, Tk_Offset(TkText, selBorder),
126 TK_CONFIG_COLOR_ONLY},
127 {TK_CONFIG_BORDER, "-selectbackground", "selectBackground", "Foreground",
128 DEF_TEXT_SELECT_MONO, Tk_Offset(TkText, selBorder),
129 TK_CONFIG_MONO_ONLY},
130 {TK_CONFIG_STRING, "-selectborderwidth", "selectBorderWidth", "BorderWidth",
131 DEF_TEXT_SELECT_BD_COLOR, Tk_Offset(TkText, selBdString),
132 TK_CONFIG_COLOR_ONLY|TK_CONFIG_NULL_OK},
133 {TK_CONFIG_STRING, "-selectborderwidth", "selectBorderWidth", "BorderWidth",
134 DEF_TEXT_SELECT_BD_MONO, Tk_Offset(TkText, selBdString),
135 TK_CONFIG_MONO_ONLY|TK_CONFIG_NULL_OK},
136 {TK_CONFIG_COLOR, "-selectforeground", "selectForeground", "Background",
137 DEF_TEXT_SELECT_FG_COLOR, Tk_Offset(TkText, selFgColorPtr),
138 TK_CONFIG_COLOR_ONLY},
139 {TK_CONFIG_COLOR, "-selectforeground", "selectForeground", "Background",
140 DEF_TEXT_SELECT_FG_MONO, Tk_Offset(TkText, selFgColorPtr),
141 TK_CONFIG_MONO_ONLY},
142 {TK_CONFIG_BOOLEAN, "-setgrid", "setGrid", "SetGrid",
143 DEF_TEXT_SET_GRID, Tk_Offset(TkText, setGrid), 0},
144 {TK_CONFIG_PIXELS, "-spacing1", "spacing1", "Spacing",
145 DEF_TEXT_SPACING1, Tk_Offset(TkText, spacing1),
146 TK_CONFIG_DONT_SET_DEFAULT},
147 {TK_CONFIG_PIXELS, "-spacing2", "spacing2", "Spacing",
148 DEF_TEXT_SPACING2, Tk_Offset(TkText, spacing2),
149 TK_CONFIG_DONT_SET_DEFAULT},
150 {TK_CONFIG_PIXELS, "-spacing3", "spacing3", "Spacing",
151 DEF_TEXT_SPACING3, Tk_Offset(TkText, spacing3),
152 TK_CONFIG_DONT_SET_DEFAULT},
153 {TK_CONFIG_CUSTOM, "-state", "state", "State",
154 DEF_TEXT_STATE, Tk_Offset(TkText, state), 0, &stateOption},
155 {TK_CONFIG_LANGARG, "-tabs", "tabs", "Tabs",
156 DEF_TEXT_TABS, Tk_Offset(TkText, tabOptionString), TK_CONFIG_NULL_OK},
157 {TK_CONFIG_STRING, "-takefocus", "takeFocus", "TakeFocus",
158 DEF_TEXT_TAKE_FOCUS, Tk_Offset(TkText, takeFocus),
159 TK_CONFIG_NULL_OK},
160 {TK_CONFIG_BOOLEAN, "-undo", "undo", "Undo",
161 DEF_TEXT_UNDO, Tk_Offset(TkText, undo), 0},
162 {TK_CONFIG_INT, "-width", "width", "Width",
163 DEF_TEXT_WIDTH, Tk_Offset(TkText, width), 0},
164 {TK_CONFIG_CUSTOM, "-wrap", "wrap", "Wrap",
165 DEF_TEXT_WRAP, Tk_Offset(TkText, wrapMode), 0, &textWrapModeOption},
166 {TK_CONFIG_CALLBACK, "-xscrollcommand", "xScrollCommand", "ScrollCommand",
167 DEF_TEXT_XSCROLL_COMMAND, Tk_Offset(TkText, xScrollCmd),
168 TK_CONFIG_NULL_OK},
169 {TK_CONFIG_CALLBACK, "-yscrollcommand", "yScrollCommand", "ScrollCommand",
170 DEF_TEXT_YSCROLL_COMMAND, Tk_Offset(TkText, yScrollCmd),
171 TK_CONFIG_NULL_OK},
172 {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
173 (char *) NULL, 0, 0}
174 };
175
176 /*
177 * Boolean variable indicating whether or not special debugging code
178 * should be executed.
179 */
180
181 int tkTextDebug = 0;
182
183 /*
184 * Custom options for handling "-wrap":
185 */
186
187 static int WrapModeParseProc _ANSI_ARGS_((ClientData clientData,
188 Tcl_Interp *interp, Tk_Window tkwin,
189 Tcl_Obj *value, char *widgRec, int offset));
190 static Tcl_Obj * WrapModePrintProc _ANSI_ARGS_((ClientData clientData,
191 Tk_Window tkwin, char *widgRec, int offset,
192 Tcl_FreeProc **freeProcPtr));
193
194 Tk_CustomOption textWrapModeOption = {
195 WrapModeParseProc,
196 WrapModePrintProc,
197 (ClientData) NULL
198 };
199
200 /*
201 *--------------------------------------------------------------
202 *
203 * WrapModeParseProc --
204 *
205 * This procedure is invoked during option processing to handle
206 * "-wrap" options for text widgets.
207 *
208 * Results:
209 * A standard Tcl return value.
210 *
211 * Side effects:
212 * The wrap mode for a given item gets replaced by the wrap mode
213 * indicated in the value argument.
214 *
215 *--------------------------------------------------------------
216 */
217
218 static int
219 WrapModeParseProc(clientData, interp, tkwin, ovalue, widgRec, offset)
220 ClientData clientData; /* some flags.*/
221 Tcl_Interp *interp; /* Used for reporting errors. */
222 Tk_Window tkwin; /* Window containing canvas widget. */
223 Tcl_Obj *ovalue; /* Value of option (list of tag
224 * names). */
225 char *widgRec; /* Pointer to record for item. */
226 int offset; /* Offset into item. */
227 {
228 int c;
229 size_t length;
230 char *value = Tcl_GetString(ovalue);
231
232 register TkWrapMode *wrapPtr = (TkWrapMode *) (widgRec + offset);
233
234 if(value == NULL || *value == 0) {
235 *wrapPtr = TEXT_WRAPMODE_NULL;
236 return TCL_OK;
237 }
238
239 c = value[0];
240 length = strlen(value);
241
242 if ((c == 'c') && (strncmp(value, "char", length) == 0)) {
243 *wrapPtr = TEXT_WRAPMODE_CHAR;
244 return TCL_OK;
245 }
246 if ((c == 'n') && (strncmp(value, "none", length) == 0)) {
247 *wrapPtr = TEXT_WRAPMODE_NONE;
248 return TCL_OK;
249 }
250 if ((c == 'w') && (strncmp(value, "word", length) == 0)) {
251 *wrapPtr = TEXT_WRAPMODE_WORD;
252 return TCL_OK;
253 }
254 Tcl_AppendResult(interp, "bad wrap mode \"", value,
255 "\": must be char, none, or word",
256 (char *) NULL);
257 *wrapPtr = TEXT_WRAPMODE_CHAR;
258 return TCL_ERROR;
259 }
260
261 /*
262 *--------------------------------------------------------------
263 *
264 * WrapModePrintProc --
265 *
266 * This procedure is invoked by the Tk configuration code
267 * to produce a printable string for the "-wrap" configuration
268 * option for canvas items.
269 *
270 * Results:
271 * The return value is a string describing the state for
272 * the item referred to by "widgRec". In addition, *freeProcPtr
273 * is filled in with the address of a procedure to call to free
274 * the result string when it's no longer needed (or NULL to
275 * indicate that the string doesn't need to be freed).
276 *
277 * Side effects:
278 * None.
279 *
280 *--------------------------------------------------------------
281 */
282
283 static Tcl_Obj *
284 WrapModePrintProc(clientData, tkwin, widgRec, offset, freeProcPtr)
285 ClientData clientData; /* Ignored. */
286 Tk_Window tkwin; /* Window containing canvas widget. */
287 char *widgRec; /* Pointer to record for item. */
288 int offset; /* Ignored. */
289 Tcl_FreeProc **freeProcPtr; /* Pointer to variable to fill in with
290 * information about how to reclaim
291 * storage for return string. */
292 {
293 register TkWrapMode *wrapPtr = (TkWrapMode *) (widgRec + offset);
294
295 if (*wrapPtr==TEXT_WRAPMODE_CHAR) {
296 return LangStringArg("char");
297 } else if (*wrapPtr==TEXT_WRAPMODE_NONE) {
298 return LangStringArg("none");
299 } else if (*wrapPtr==TEXT_WRAPMODE_WORD) {
300 return LangStringArg("word");
301 } else {
302 return LangStringArg("");
303 }
304 }
305
306 /*
307 * Forward declarations for procedures defined later in this file:
308 */
309
310 static int ConfigureText _ANSI_ARGS_((Tcl_Interp *interp,
311 TkText *textPtr, int argc, char **argv,
312 int flags));
313 static int DeleteChars _ANSI_ARGS_((TkText *textPtr,
314 CONST char *index1String, CONST char *index2String,
315 TkTextIndex *indexPtr1, TkTextIndex *indexPtr2));
316 static void DestroyText _ANSI_ARGS_((char *memPtr));
317 static void InsertChars _ANSI_ARGS_((TkText *textPtr,
318 TkTextIndex *indexPtr, CONST char *string));
319 static void TextBlinkProc _ANSI_ARGS_((ClientData clientData));
320 static void TextCmdDeletedProc _ANSI_ARGS_((
321 ClientData clientData));
322 static void TextEventProc _ANSI_ARGS_((ClientData clientData,
323 XEvent *eventPtr));
324 static int TextFetchSelection _ANSI_ARGS_((ClientData clientData,
325 int offset, char *buffer, int maxBytes));
326 static int TextIndexSortProc _ANSI_ARGS_((CONST VOID *first,
327 CONST VOID *second));
328 static int TextSearchCmd _ANSI_ARGS_((TkText *textPtr,
329 Tcl_Interp *interp, int argc, char **argv));
330 static int TextEditCmd _ANSI_ARGS_((TkText *textPtr,
331 Tcl_Interp *interp, int argc, char **argv));
332 static int TextWidgetCmd _ANSI_ARGS_((ClientData clientData,
333 Tcl_Interp *interp, int argc, char **argv));
334 static void TextWorldChanged _ANSI_ARGS_((
335 ClientData instanceData));
336 static int TextDumpCmd _ANSI_ARGS_((TkText *textPtr,
337 Tcl_Interp *interp, int argc, char **argv));
338 static void DumpLine _ANSI_ARGS_((Tcl_Interp *interp,
339 TkText *textPtr, int what, TkTextLine *linePtr,
340 int start, int end, int lineno,
341 LangCallback *command));
342 static int DumpSegment _ANSI_ARGS_((Tcl_Interp *interp, char *key,
343 char *value, Tcl_Obj *arg, LangCallback *command,
344 TkTextIndex *index, int what));
345 static int TextEditUndo _ANSI_ARGS_((TkText *textPtr));
346 static int TextEditRedo _ANSI_ARGS_((TkText *textPtr));
347 static void TextGetText _ANSI_ARGS_((TkTextIndex * index1,
348 TkTextIndex * index2, Tcl_DString *dsPtr));
349 static void updateDirtyFlag _ANSI_ARGS_((TkText *textPtr));
350
351 /*
352 * The structure below defines text class behavior by means of procedures
353 * that can be invoked from generic window code.
354 */
355
356 static Tk_ClassProcs textClass = {
357 sizeof(Tk_ClassProcs), /* size */
358 TextWorldChanged, /* worldChangedProc */
359 };
360
361
362 /*
363 *--------------------------------------------------------------
364 *
365 * Tk_TextCmd --
366 *
367 * This procedure is invoked to process the "text" Tcl command.
368 * See the user documentation for details on what it does.
369 *
370 * Results:
371 * A standard Tcl result.
372 *
373 * Side effects:
374 * See the user documentation.
375 *
376 *--------------------------------------------------------------
377 */
378
379 int
380 Tk_TextCmd(clientData, interp, argc, argv)
381 ClientData clientData; /* Main window associated with
382 * interpreter. */
383 Tcl_Interp *interp; /* Current interpreter. */
384 int argc; /* Number of arguments. */
385 Tcl_Obj *CONST objv[]; /* Argument strings. */
386 {
387 Tk_Window tkwin = (Tk_Window) clientData;
388 Tk_Window new;
389 register TkText *textPtr;
390 TkTextIndex startIndex;
391
392 if (argc < 2) {
393 Tcl_AppendResult(interp, "wrong # args: should be \"",
394 argv[0], " pathName ?options?\"", (char *) NULL);
395 return TCL_ERROR;
396 }
397
398 /*
399 * Create the window.
400 */
401
402 new = Tk_CreateWindowFromPath(interp, tkwin, argv[1], (char *) NULL);
403 if (new == NULL) {
404 return TCL_ERROR;
405 }
406
407 /*
408 * Create the text widget and initialize everything to zero,
409 * then set the necessary initial (non-NULL) values.
410 */
411
412 textPtr = (TkText *) ckalloc(sizeof(TkText));
413 memset((VOID *) textPtr, 0, sizeof(TkText));
414
415 textPtr->tkwin = new;
416 textPtr->display = Tk_Display(new);
417 textPtr->interp = interp;
418 textPtr->widgetCmd = Tcl_CreateCommand(interp,
419 Tk_PathName(textPtr->tkwin), TextWidgetCmd,
420 (ClientData) textPtr, TextCmdDeletedProc);
421 textPtr->tree = TkBTreeCreate(textPtr);
422 Tcl_InitHashTable(&textPtr->tagTable, TCL_STRING_KEYS);
423 Tcl_InitHashTable(&textPtr->markTable, TCL_STRING_KEYS);
424 Tcl_InitHashTable(&textPtr->windowTable, TCL_STRING_KEYS);
425 Tcl_InitHashTable(&textPtr->imageTable, TCL_STRING_KEYS);
426 textPtr->state = TK_STATE_NORMAL;
427 textPtr->relief = TK_RELIEF_FLAT;
428 textPtr->cursor = None;
429 textPtr->charWidth = 1;
430 textPtr->wrapMode = TEXT_WRAPMODE_CHAR;
431 textPtr->prevWidth = Tk_Width(new);
432 textPtr->prevHeight = Tk_Height(new);
433 TkTextCreateDInfo(textPtr);
434 TkTextMakeByteIndex(textPtr->tree, 0, 0, &startIndex);
435 TkTextSetYView(textPtr, &startIndex, 0);
436 textPtr->exportSelection = 1;
437 textPtr->pickEvent.type = LeaveNotify;
438 textPtr->undoStack = TkUndoInitStack(interp,0);
439 textPtr->undo = 1;
440 textPtr->isDirtyIncrement = 1;
441 textPtr->autoSeparators = 1;
442 textPtr->lastEditMode = TK_TEXT_EDIT_OTHER;
443
444 /*
445 * Create the "sel" tag and the "current" and "insert" marks.
446 */
447
448 textPtr->selTagPtr = TkTextCreateTag(textPtr, "sel");
449 textPtr->selTagPtr->reliefString =
450 (char *) ckalloc(sizeof(DEF_TEXT_SELECT_RELIEF));
451 strcpy(textPtr->selTagPtr->reliefString, DEF_TEXT_SELECT_RELIEF);
452 textPtr->selTagPtr->relief = TK_RELIEF_RAISED;
453 textPtr->currentMarkPtr = TkTextSetMark(textPtr, "current", &startIndex);
454 textPtr->insertMarkPtr = TkTextSetMark(textPtr, "insert", &startIndex);
455
456 Tk_SetClass(textPtr->tkwin, "Text");
457 Tk_SetClassProcs(textPtr->tkwin, &textClass, (ClientData) textPtr);
458 Tk_CreateEventHandler(textPtr->tkwin,
459 ExposureMask|StructureNotifyMask|FocusChangeMask,
460 TextEventProc, (ClientData) textPtr);
461 Tk_CreateEventHandler(textPtr->tkwin, KeyPressMask|KeyReleaseMask
462 |ButtonPressMask|ButtonReleaseMask|EnterWindowMask
463 |LeaveWindowMask|PointerMotionMask|VirtualEventMask,
464 TkTextBindProc, (ClientData) textPtr);
465 Tk_CreateSelHandler(textPtr->tkwin, XA_PRIMARY, XA_STRING,
466 TextFetchSelection, (ClientData) textPtr, XA_STRING);
467 if (ConfigureText(interp, textPtr, argc-2, argv+2, 0) != TCL_OK) {
468 Tk_DestroyWindow(textPtr->tkwin);
469 return TCL_ERROR;
470 }
471 Tcl_SetResult(interp, Tk_PathName(textPtr->tkwin), TCL_STATIC);
472
473 return TCL_OK;
474 }
475
476 /*
477 *--------------------------------------------------------------
478 *
479 * TextWidgetCmd --
480 *
481 * This procedure is invoked to process the Tcl command
482 * that corresponds to a text widget. See the user
483 * documentation for details on what it does.
484 *
485 * Results:
486 * A standard Tcl result.
487 *
488 * Side effects:
489 * See the user documentation.
490 *
491 *--------------------------------------------------------------
492 */
493
494 static int
495 TextWidgetCmd(clientData, interp, argc, argv)
496 ClientData clientData; /* Information about text widget. */
497 Tcl_Interp *interp; /* Current interpreter. */
498 int argc; /* Number of arguments. */
499 char **argv; /* Argument strings. */
500 {
501 register TkText *textPtr = (TkText *) clientData;
502 int c, result = TCL_OK;
503 size_t length;
504 TkTextIndex index1, index2;
505
506 if (argc < 2) {
507 Tcl_AppendResult(interp, "wrong # args: should be \"",
508 argv[0], " option ?arg arg ...?\"", (char *) NULL);
509 return TCL_ERROR;
510 }
511 Tcl_Preserve((ClientData) textPtr);
512 c = argv[1][0];
513 length = strlen(argv[1]);
514 if ((c == 'b') && (strncmp(argv[1], "bbox", length) == 0)) {
515 int x, y, width, height;
516
517 if (argc != 3) {
518 Tcl_AppendResult(interp, "wrong # args: should be \"",
519 argv[0], " bbox index\"", (char *) NULL);
520 result = TCL_ERROR;
521 goto done;
522 }
523 if (TkTextGetIndex(interp, textPtr, argv[2], &index1) != TCL_OK) {
524 result = TCL_ERROR;
525 goto done;
526 }
527 if (TkTextCharBbox(textPtr, &index1, &x, &y, &width, &height) == 0) {
528 Tcl_IntResults(interp,4,0, x, y, width, height);
529 }
530 } else if ((c == 'c') && (strncmp(argv[1], "cget", length) == 0)
531 && (length >= 2)) {
532 if (argc != 3) {
533 Tcl_AppendResult(interp, "wrong # args: should be \"",
534 argv[0], " cget option\"",
535 (char *) NULL);
536 result = TCL_ERROR;
537 goto done;
538 }
539 result = Tk_ConfigureValue(interp, textPtr->tkwin, configSpecs,
540 (char *) textPtr, argv[2], 0);
541 } else if ((c == 'c') && (strncmp(argv[1], "compare", length) == 0)
542 && (length >= 3)) {
543 int relation, value;
544 CONST char *p;
545
546 if (argc != 5) {
547 Tcl_AppendResult(interp, "wrong # args: should be \"",
548 argv[0], " compare index1 op index2\"", (char *) NULL);
549 result = TCL_ERROR;
550 goto done;
551 }
552 if ((TkTextGetIndex(interp, textPtr, argv[2], &index1) != TCL_OK)
553 || (TkTextGetIndex(interp, textPtr, argv[4], &index2)
554 != TCL_OK)) {
555 result = TCL_ERROR;
556 goto done;
557 }
558 relation = TkTextIndexCmp(&index1, &index2);
559 p = argv[3];
560 if (p[0] == '<') {
561 value = (relation < 0);
562 if ((p[1] == '=') && (p[2] == 0)) {
563 value = (relation <= 0);
564 } else if (p[1] != 0) {
565 compareError:
566 Tcl_AppendResult(interp, "bad comparison operator \"",
567 argv[3], "\": must be <, <=, ==, >=, >, or !=",
568 (char *) NULL);
569 result = TCL_ERROR;
570 goto done;
571 }
572 } else if (p[0] == '>') {
573 value = (relation > 0);
574 if ((p[1] == '=') && (p[2] == 0)) {
575 value = (relation >= 0);
576 } else if (p[1] != 0) {
577 goto compareError;
578 }
579 } else if ((p[0] == '=') && (p[1] == '=') && (p[2] == 0)) {
580 value = (relation == 0);
581 } else if ((p[0] == '!') && (p[1] == '=') && (p[2] == 0)) {
582 value = (relation != 0);
583 } else {
584 goto compareError;
585 }
586 Tcl_SetResult(interp, ((value) ? "1" : "0"), TCL_STATIC);
587 } else if ((c == 'c') && (strncmp(argv[1], "configure", length) == 0)
588 && (length >= 3)) {
589 if (argc == 2) {
590 result = Tk_ConfigureInfo(interp, textPtr->tkwin, configSpecs,
591 (char *) textPtr, (char *) NULL, 0);
592 } else if (argc == 3) {
593 result = Tk_ConfigureInfo(interp, textPtr->tkwin, configSpecs,
594 (char *) textPtr, argv[2], 0);
595 } else {
596 result = ConfigureText(interp, textPtr, argc-2, argv+2,
597 TK_CONFIG_ARGV_ONLY);
598 }
599 } else if ((c == 'd') && (strncmp(argv[1], "debug", length) == 0)
600 && (length >= 3)) {
601 if (argc > 3) {
602 Tcl_AppendResult(interp, "wrong # args: should be \"",
603 argv[0], " debug boolean\"", (char *) NULL);
604 result = TCL_ERROR;
605 goto done;
606 }
607 if (argc == 2) {
608 Tcl_SetResult(interp, ((tkBTreeDebug) ? "1" : "0"), TCL_STATIC);
609 } else {
610 if (Tcl_GetBooleanFromObj(interp, argv[2], &tkBTreeDebug) != TCL_OK) {
611 result = TCL_ERROR;
612 goto done;
613 }
614 tkTextDebug = tkBTreeDebug;
615 }
616 } else if ((c == 'd') && (strncmp(argv[1], "delete", length) == 0)
617 && (length >= 3)) {
618 int i;
619
620 if (argc < 3) {
621 Tcl_AppendResult(interp, "wrong # args: should be \"",
622 argv[0], " delete index1 ?index2 ...?\"", (char *) NULL);
623 result = TCL_ERROR;
624 goto done;
625 }
626 if (textPtr->state == TK_STATE_NORMAL) {
627 if (argc < 5) {
628 /*
629 * Simple case requires no predetermination of indices.
630 */
631 result = DeleteChars(textPtr, argv[2],
632 (argc == 4) ? argv[3] : NULL, NULL, NULL);
633 } else {
634 /*
635 * Multi-index pair case requires that we prevalidate the
636 * indices and sort from last to first so that deletes
637 * occur in the exact (unshifted) text. It also needs to
638 * handle partial and fully overlapping ranges. We have to
639 * do this with multiple passes.
640 */
641 TkTextIndex *indices, *ixStart, *ixEnd, *lastStart;
642 char *useIdx;
643
644 argc -= 2;
645 argv += 2;
646 indices = (TkTextIndex *)
647 ckalloc((argc + 1) * sizeof(TkTextIndex));
648
649 /*
650 * First pass verifies that all indices are valid.
651 */
652 for (i = 0; i < argc; i++) {
653 if (TkTextGetIndex(interp, textPtr, argv[i],
654 &indices[i]) != TCL_OK) {
655 result = TCL_ERROR;
656 ckfree((char *) indices);
657 goto done;
658 }
659 }
660 /*
661 * Pad out the pairs evenly to make later code easier.
662 */
663 if (argc & 1) {
664 indices[i] = indices[i-1];
665 TkTextIndexForwChars(&indices[i], 1, &indices[i]);
666 argc++;
667 }
668 useIdx = (char *) ckalloc((unsigned) argc);
669 memset(useIdx, 0, (unsigned) argc);
670 /*
671 * Do a decreasing order sort so that we delete the end
672 * ranges first to maintain index consistency.
673 */
674 qsort((VOID *) indices, (unsigned) (argc / 2),
675 2 * sizeof(TkTextIndex), TextIndexSortProc);
676 lastStart = NULL;
677 /*
678 * Second pass will handle bogus ranges (end < start) and
679 * overlapping ranges.
680 */
681 for (i = 0; i < argc; i += 2) {
682 ixStart = &indices[i];
683 ixEnd = &indices[i+1];
684 if (TkTextIndexCmp(ixEnd, ixStart) <= 0) {
685 continue;
686 }
687 if (lastStart) {
688 if (TkTextIndexCmp(ixStart, lastStart) == 0) {
689 /*
690 * Start indices were equal, and the sort placed
691 * the longest range first, so skip this one.
692 */
693 continue;
694 } else if (TkTextIndexCmp(lastStart, ixEnd) < 0) {
695 /*
696 * The next pair has a start range before the end
697 * point of the last range. Constrain the delete
698 * range, but use the pointer values.
699 */
700 *ixEnd = *lastStart;
701 if (TkTextIndexCmp(ixEnd, ixStart) <= 0) {
702 continue;
703 }
704 }
705 }
706 lastStart = ixStart;
707 useIdx[i] = 1;
708 }
709 /*
710 * Final pass take the input from the previous and deletes
711 * the ranges which are flagged to be deleted.
712 */
713 for (i = 0; i < argc; i += 2) {
714 if (useIdx[i]) {
715 /*
716 * We don't need to check the return value because all
717 * indices are preparsed above.
718 */
719 DeleteChars(textPtr, NULL, NULL,
720 &indices[i], &indices[i+1]);
721 }
722 }
723 ckfree((char *) indices);
724 }
725 }
726 } else if ((c == 'd') && (strncmp(argv[1], "dlineinfo", length) == 0)
727 && (length >= 2)) {
728 int x, y, width, height, base;
729
730 if (argc != 3) {
731 Tcl_AppendResult(interp, "wrong # args: should be \"",
732 argv[0], " dlineinfo index\"", (char *) NULL);
733 result = TCL_ERROR;
734 goto done;
735 }
736 if (TkTextGetIndex(interp, textPtr, argv[2], &index1) != TCL_OK) {
737 result = TCL_ERROR;
738 goto done;
739 }
740 if (TkTextDLineInfo(textPtr, &index1, &x, &y, &width, &height, &base)
741 == 0) {
742 char buf[TCL_INTEGER_SPACE * 5];
743 Tcl_Obj *el;
744 el = Tcl_GetObjResult(interp);
745
746 Tcl_ListObjAppendElement(interp,el,Tcl_NewIntObj(x));
747 Tcl_ListObjAppendElement(interp,el,Tcl_NewIntObj(y));
748 Tcl_ListObjAppendElement(interp,el,Tcl_NewIntObj(width));
749 Tcl_ListObjAppendElement(interp,el,Tcl_NewIntObj(height));
750 Tcl_ListObjAppendElement(interp,el,Tcl_NewIntObj(base));
751 /*
752 sprintf(buf, "%d %d %d %d %d", x, y, width, height, base);
753 Tcl_SetResult(interp, buf, TCL_VOLATILE);
754 */
755 }
756 } else if ((c == 'e') && (strncmp(argv[1], "edit", length) == 0)) {
757 result = TextEditCmd(textPtr, interp, argc, argv);
758 } else if ((c == 'g') && (strncmp(argv[1], "get", length) == 0)) {
759 Tcl_Obj *objPtr = NULL;
760 Tcl_DString ds;
761 int i, found = 0;
762
763 if (argc < 3) {
764 Tcl_AppendResult(interp, "wrong # args: should be \"",
765 argv[0], " get index1 ?index2 ...?\"", (char *) NULL);
766 result = TCL_ERROR;
767 goto done;
768 }
769 for (i = 2; i < argc; i += 2) {
770 if (TkTextGetIndex(interp, textPtr, argv[i], &index1) != TCL_OK) {
771 result = TCL_ERROR;
772 goto done;
773 }
774 if (i+1 == argc) {
775 index2 = index1;
776 TkTextIndexForwChars(&index2, 1, &index2);
777 } else if (TkTextGetIndex(interp, textPtr, argv[i+1], &index2)
778 != TCL_OK) {
779 if (objPtr) {
780 Tcl_DecrRefCount(objPtr);
781 }
782 result = TCL_ERROR;
783 goto done;
784 }
785 /* Using <= gives perl/Tk null strings for equal case and undef
786 for reversed range - which seems "correct"
787 */
788 if (TkTextIndexCmp(&index1, &index2) <= 0) {
789 /*
790 * Place the text in a DString and move it to the result.
791 * Since this could in principle be a megabyte or more, we
792 * want to do it efficiently!
793 */
794 TextGetText(&index1, &index2, &ds);
795 found++;
796 if (found == 1) {
797 Tcl_DStringResult(interp, &ds);
798 } else {
799 if (found == 2) {
800 /*
801 * Move the first item we put into the result into
802 * the first element of the list object.
803 */
804 objPtr = Tcl_NewObj();
805 #ifdef _LANG
806 Tcl_IncrRefCount(Tcl_GetObjResult(interp));
807 #endif
808 Tcl_ListObjAppendElement(NULL, objPtr,
809 Tcl_GetObjResult(interp));
810 }
811 Tcl_ListObjAppendElement(NULL, objPtr,
812 Tcl_NewStringObj(Tcl_DStringValue(&ds),
813 Tcl_DStringLength(&ds)));
814 }
815 Tcl_DStringFree(&ds);
816 }
817 }
818 if (found > 1) {
819 Tcl_SetObjResult(interp, objPtr);
820 }
821 } else if ((c == 'i') && (strncmp(argv[1], "index", length) == 0)
822 && (length >= 3)) {
823 char buf[200];
824
825 if (argc != 3) {
826 Tcl_AppendResult(interp, "wrong # args: should be \"",
827 argv[0], " index index\"",
828 (char *) NULL);
829 result = TCL_ERROR;
830 goto done;
831 }
832 if (TkTextGetIndex(interp, textPtr, argv[2], &index1) != TCL_OK) {
833 result = TCL_ERROR;
834 goto done;
835 }
836 TkTextPrintIndex(&index1, buf);
837 Tcl_SetResult(interp, buf, TCL_VOLATILE);
838 } else if ((c == 'i') && (strncmp(argv[1], "insert", length) == 0)
839 && (length >= 3)) {
840 int i, j, numTags;
841 Tcl_Obj * *tagNames;
842 TkTextTag **oldTagArrayPtr;
843
844 if (argc < 4) {
845 Tcl_AppendResult(interp, "wrong # args: should be \"",
846 argv[0],
847 " insert index chars ?tagList chars tagList ...?\"",
848 (char *) NULL);
849 result = TCL_ERROR;
850 goto done;
851 }
852 if (TkTextGetIndex(interp, textPtr, argv[2], &index1) != TCL_OK) {
853 result = TCL_ERROR;
854 goto done;
855 }
856 if (textPtr->state == TK_STATE_NORMAL) {
857 for (j = 3; j < argc; j += 2) {
858 InsertChars(textPtr, &index1, argv[j]);
859 if (argc > (j+1)) {
860 TkTextIndexForwBytes(&index1, (int) strlen(argv[j]),
861 &index2);
862 oldTagArrayPtr = TkBTreeGetTags(&index1, &numTags);
863 if (oldTagArrayPtr != NULL) {
864 for (i = 0; i < numTags; i++) {
865 TkBTreeTag(&index1, &index2, oldTagArrayPtr[i], 0);
866 }
867 ckfree((char *) oldTagArrayPtr);
868 }
869 if (Tcl_ListObjGetElements(interp, objv[j+1], &numTags, &tagNames)
870 != TCL_OK) {
871 result = TCL_ERROR;
872 goto done;
873 }
874 for (i = 0; i < numTags; i++) {
875 TkBTreeTag(&index1, &index2,
876 TkTextCreateTag(textPtr, Tcl_GetString(tagNames[i])), 1);
877 }
878 index1 = index2;
879 }
880 }
881 }
882 } else if ((c == 'd') && (strncmp(argv[1], "dump", length) == 0)) {
883 result = TextDumpCmd(textPtr, interp, argc, argv);
884 } else if ((c == 'i') && (strncmp(argv[1], "image", length) == 0)) {
885 result = TkTextImageCmd(textPtr, interp, argc, objv);
886 } else if ((c == 'm') && (strncmp(argv[1], "mark", length) == 0)) {
887 result = TkTextMarkCmd(textPtr, interp, argc, argv);
888 } else if ((c == 's') && (strcmp(argv[1], "scan") == 0) && (length >= 2)) {
889 result = TkTextScanCmd(textPtr, interp, argc, argv);
890 } else if ((c == 's') && (strcmp(argv[1], "search") == 0)
891 && (length >= 3)) {
892 result = TextSearchCmd(textPtr, interp, argc, argv);
893 } else if ((c == 's') && (strcmp(argv[1], "see") == 0) && (length >= 3)) {
894 result = TkTextSeeCmd(textPtr, interp, argc, argv);
895 } else if ((c == 't') && (strcmp(argv[1], "tag") == 0)) {
896 result = TkTextTagCmd(textPtr, interp, argc, argv);
897 } else if ((c == 'w') && (strncmp(argv[1], "window", length) == 0)) {
898 result = TkTextWindowCmd(textPtr, interp, argc, argv);
899 } else if ((c == 'x') && (strncmp(argv[1], "xview", length) == 0)) {
900 result = TkTextXviewCmd(textPtr, interp, argc, argv);
901 } else if ((c == 'y') && (strncmp(argv[1], "yview", length) == 0)
902 && (length >= 2)) {
903 result = TkTextYviewCmd(textPtr, interp, argc, argv);
904 } else {
905 Tcl_AppendResult(interp, "bad option \"", argv[1],
906 "\": must be bbox, cget, compare, configure, debug, delete, ",
907 "dlineinfo, dump, edit, get, image, index, insert, mark, ",
908 "scan, search, see, tag, window, xview, or yview",
909 (char *) NULL);
910 result = TCL_ERROR;
911 }
912
913 done:
914 Tcl_Release((ClientData) textPtr);
915 return result;
916 }
917
918 /*
919 *----------------------------------------------------------------------
920 *
921 * TextIndexSortProc --
922 *
923 * This procedure is called by qsort when sorting an array of
924 * indices in *decreasing* order (last to first).
925 *
926 * Results:
927 * The return value is -1 if the first argument should be before
928 * the second element, 0 if it's equivalent, and 1 if it should be
929 * after the second element.
930 *
931 * Side effects:
932 * None.
933 *
934 *----------------------------------------------------------------------
935 */
936
937 static int
938 TextIndexSortProc(first, second)
939 CONST VOID *first, *second; /* Elements to be compared. */
940 {
941 TkTextIndex *pair1 = (TkTextIndex *) first;
942 TkTextIndex *pair2 = (TkTextIndex *) second;
943 int cmp = TkTextIndexCmp(&pair1[1], &pair2[1]);
944
945 if (cmp == 0) {
946 /*
947 * If the first indices were equal, we want the second index of the
948 * pair also to be the greater. Use pointer magic to access the
949 * second index pair.
950 */
951 cmp = TkTextIndexCmp(&pair1[0], &pair2[0]);
952 }
953 if (cmp > 0) {
954 return -1;
955 } else if (cmp < 0) {
956 return 1;
957 }
958 return 0;
959 }
960
961 /*
962 *----------------------------------------------------------------------
963 *
964 * DestroyText --
965 *
966 * This procedure is invoked by Tcl_EventuallyFree or Tcl_Release
967 * to clean up the internal structure of a text at a safe time
968 * (when no-one is using it anymore).
969 *
970 * Results:
971 * None.
972 *
973 * Side effects:
974 * Everything associated with the text is freed up.
975 *
976 *----------------------------------------------------------------------
977 */
978
979 static void
980 DestroyText(memPtr)
981 char *memPtr; /* Info about text widget. */
982 {
983 register TkText *textPtr = (TkText *) memPtr;
984 Tcl_HashSearch search;
985 Tcl_HashEntry *hPtr;
986 TkTextTag *tagPtr;
987
988 /*
989 * Free up all the stuff that requires special handling, then
990 * let Tk_FreeOptions handle all the standard option-related
991 * stuff. Special note: free up display-related information
992 * before deleting the B-tree, since display-related stuff
993 * may refer to stuff in the B-tree.
994 */
995
996 TkTextFreeDInfo(textPtr);
997 TkBTreeDestroy(textPtr->tree);
998 for (hPtr = Tcl_FirstHashEntry(&textPtr->tagTable, &search);
999 hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
1000 tagPtr = (TkTextTag *) Tcl_GetHashValue(hPtr);
1001 TkTextFreeTag(textPtr, tagPtr);
1002 }
1003 Tcl_DeleteHashTable(&textPtr->tagTable);
1004 for (hPtr = Tcl_FirstHashEntry(&textPtr->markTable, &search);
1005 hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
1006 ckfree((char *) Tcl_GetHashValue(hPtr));
1007 }
1008 Tcl_DeleteHashTable(&textPtr->markTable);
1009 if (textPtr->tabArrayPtr != NULL) {
1010 ckfree((char *) textPtr->tabArrayPtr);
1011 }
1012 if (textPtr->insertBlinkHandler != NULL) {
1013 Tcl_DeleteTimerHandler(textPtr->insertBlinkHandler);
1014 }
1015 if (textPtr->bindingTable != NULL) {
1016 Tk_DeleteBindingTable(textPtr->bindingTable);
1017 }
1018 TkUndoFreeStack(textPtr->undoStack);
1019
1020 /*
1021 * NOTE: do NOT free up selBorder, selBdString, or selFgColorPtr:
1022 * they are duplicates of information in the "sel" tag, which was
1023 * freed up as part of deleting the tags above.
1024 */
1025
1026 textPtr->selBorder = NULL;
1027 textPtr->selBdString = NULL;
1028 textPtr->selFgColorPtr = NULL;
1029 Tk_FreeOptions(configSpecs, (char *) textPtr, textPtr->display, 0);
1030 ckfree((char *) textPtr);
1031 }
1032
1033 /*
1034 *----------------------------------------------------------------------
1035 *
1036 * ConfigureText --
1037 *
1038 * This procedure is called to process an argv/argc list, plus
1039 * the Tk option database, in order to configure (or
1040 * reconfigure) a text widget.
1041 *
1042 * Results:
1043 * The return value is a standard Tcl result. If TCL_ERROR is
1044 * returned, then the interp's result contains an error message.
1045 *
1046 * Side effects:
1047 * Configuration information, such as text string, colors, font,
1048 * etc. get set for textPtr; old resources get freed, if there
1049 * were any.
1050 *
1051 *----------------------------------------------------------------------
1052 */
1053
1054 static int
1055 ConfigureText(interp, textPtr, argc, argv, flags)
1056 Tcl_Interp *interp; /* Used for error reporting. */
1057 register TkText *textPtr; /* Information about widget; may or may
1058 * not already have values for some fields. */
1059 int argc; /* Number of valid entries in argv. */
1060 char **argv; /* Arguments. */
1061 int flags; /* Flags to pass to Tk_ConfigureWidget. */
1062 {
1063 int oldExport = textPtr->exportSelection;
1064
1065 if (Tk_ConfigureWidget(interp, textPtr->tkwin, configSpecs,
1066 argc, argv, (char *) textPtr, flags) != TCL_OK) {
1067 return TCL_ERROR;
1068 }
1069
1070 TkUndoSetDepth(textPtr->undoStack, textPtr->maxUndo);
1071
1072 /*
1073 * A few other options also need special processing, such as parsing
1074 * the geometry and setting the background from a 3-D border.
1075 */
1076
1077 Tk_SetBackgroundFromBorder(textPtr->tkwin, textPtr->border);
1078
1079 /*
1080 * Don't allow negative spacings.
1081 */
1082
1083 if (textPtr->spacing1 < 0) {
1084 textPtr->spacing1 = 0;
1085 }
1086 if (textPtr->spacing2 < 0) {
1087 textPtr->spacing2 = 0;
1088 }
1089 if (textPtr->spacing3 < 0) {
1090 textPtr->spacing3 = 0;
1091 }
1092
1093 /*
1094 * Parse tab stops.
1095 */
1096
1097 if (textPtr->tabArrayPtr != NULL) {
1098 ckfree((char *) textPtr->tabArrayPtr);
1099 textPtr->tabArrayPtr = NULL;
1100 }
1101 if (textPtr->tabOptionString != NULL) {
1102 textPtr->tabArrayPtr = TkTextGetTabs(interp, textPtr->tkwin,
1103 textPtr->tabOptionString);
1104 if (textPtr->tabArrayPtr == NULL) {
1105 Tcl_AddErrorInfo(interp,"\n (while processing -tabs option)");
1106 return TCL_ERROR;
1107 }
1108 }
1109
1110 /*
1111 * Make sure that configuration options are properly mirrored
1112 * between the widget record and the "sel" tags. NOTE: we don't
1113 * have to free up information during the mirroring; old
1114 * information was freed when it was replaced in the widget
1115 * record.
1116 */
1117
1118 textPtr->selTagPtr->border = textPtr->selBorder;
1119 if (textPtr->selTagPtr->bdString != textPtr->selBdString) {
1120 textPtr->selTagPtr->bdString = textPtr->selBdString;
1121 if (textPtr->selBdString != NULL) {
1122 if (Tk_GetPixels(interp, textPtr->tkwin, textPtr->selBdString,
1123 &textPtr->selTagPtr->borderWidth) != TCL_OK) {
1124 return TCL_ERROR;
1125 }
1126 if (textPtr->selTagPtr->borderWidth < 0) {
1127 textPtr->selTagPtr->borderWidth = 0;
1128 }
1129 }
1130 }
1131 textPtr->selTagPtr->fgColor = textPtr->selFgColorPtr;
1132 textPtr->selTagPtr->affectsDisplay = 0;
1133 if ((textPtr->selTagPtr->border != NULL)
1134 || (textPtr->selTagPtr->bdString != NULL)
1135 || (textPtr->selTagPtr->reliefString != NULL)
1136 || (textPtr->selTagPtr->bgStipple != None)
1137 || (textPtr->selTagPtr->fgColor != NULL)
1138 || (textPtr->selTagPtr->tkfont != None)
1139 || (textPtr->selTagPtr->fgStipple != None)
1140 || (textPtr->selTagPtr->justifyString != NULL)
1141 || (textPtr->selTagPtr->lMargin1String != NULL)
1142 || (textPtr->selTagPtr->lMargin2String != NULL)
1143 || (textPtr->selTagPtr->offsetString != NULL)
1144 || (textPtr->selTagPtr->overstrikeString != NULL)
1145 || (textPtr->selTagPtr->rMarginString != NULL)
1146 || (textPtr->selTagPtr->spacing1String != NULL)
1147 || (textPtr->selTagPtr->spacing2String != NULL)
1148 || (textPtr->selTagPtr->spacing3String != NULL)
1149 || (textPtr->selTagPtr->tabString != NULL)
1150 || (textPtr->selTagPtr->underlineString != NULL)
1151 || (textPtr->selTagPtr->elideString != NULL)
1152 || (textPtr->selTagPtr->wrapMode != TEXT_WRAPMODE_NULL)) {
1153 textPtr->selTagPtr->affectsDisplay = 1;
1154 }
1155 TkTextRedrawTag(textPtr, (TkTextIndex *) NULL, (TkTextIndex *) NULL,
1156 textPtr->selTagPtr, 1);
1157
1158 /*
1159 * Claim the selection if we've suddenly started exporting it and there
1160 * are tagged characters.
1161 */
1162
1163 if (textPtr->exportSelection && (!oldExport)) {
1164 TkTextSearch search;
1165 TkTextIndex first, last;
1166
1167 TkTextMakeByteIndex(textPtr->tree, 0, 0, &first);
1168 TkTextMakeByteIndex(textPtr->tree,
1169 TkBTreeNumLines(textPtr->tree), 0, &last);
1170 TkBTreeStartSearch(&first, &last, textPtr->selTagPtr, &search);
1171 if (TkBTreeCharTagged(&first, textPtr->selTagPtr)
1172 || TkBTreeNextTag(&search)) {
1173 Tk_OwnSelection(textPtr->tkwin, XA_PRIMARY, TkTextLostSelection,
1174 (ClientData) textPtr);
1175 textPtr->flags |= GOT_SELECTION;
1176 }
1177 }
1178
1179 /*
1180 * Account for state changes that would reenable blinking cursor state.
1181 */
1182
1183 if (textPtr->flags & GOT_FOCUS) {
1184 Tcl_DeleteTimerHandler(textPtr->insertBlinkHandler);
1185 textPtr->insertBlinkHandler = (Tcl_TimerToken) NULL;
1186 TextBlinkProc((ClientData) textPtr);
1187 }
1188
1189 /*
1190 * Register the desired geometry for the window, and arrange for
1191 * the window to be redisplayed.
1192 */
1193
1194 if (textPtr->width <= 0) {
1195 textPtr->width = 1;
1196 }
1197 if (textPtr->height <= 0) {
1198 textPtr->height = 1;
1199 }
1200 TextWorldChanged((ClientData) textPtr);
1201 return TCL_OK;
1202 }
1203
1204 /*
1205 *---------------------------------------------------------------------------
1206 *
1207 * TextWorldChanged --
1208 *
1209 * This procedure is called when the world has changed in some
1210 * way and the widget needs to recompute all its graphics contexts
1211 * and determine its new geometry.
1212 *
1213 * Results:
1214 * None.
1215 *
1216 * Side effects:
1217 * Configures all tags in the Text with a empty argc/argv, for
1218 * the side effect of causing all the items to recompute their
1219 * geometry and to be redisplayed.
1220 *
1221 *---------------------------------------------------------------------------
1222 */
1223
1224 static void
1225 TextWorldChanged(instanceData)
1226 ClientData instanceData; /* Information about widget. */
1227 {
1228 TkText *textPtr;
1229 Tk_FontMetrics fm;
1230
1231 textPtr = (TkText *) instanceData;
1232
1233 textPtr->charWidth = Tk_TextWidth(textPtr->tkfont, "0", 1);
1234 if (textPtr->charWidth <= 0) {
1235 textPtr->charWidth = 1;
1236 }
1237 Tk_GetFontMetrics(textPtr->tkfont, &fm);
1238 Tk_GeometryRequest(textPtr->tkwin,
1239 textPtr->width * textPtr->charWidth + 2*textPtr->borderWidth
1240 + 2*textPtr->padX + 2*textPtr->highlightWidth,
1241 textPtr->height * (fm.linespace + textPtr->spacing1
1242 + textPtr->spacing3) + 2*textPtr->borderWidth
1243 + 2*textPtr->padY + 2*textPtr->highlightWidth);
1244 Tk_SetInternalBorder(textPtr->tkwin,
1245 textPtr->borderWidth + textPtr->highlightWidth);
1246 if (textPtr->setGrid) {
1247 Tk_SetGrid(textPtr->tkwin, textPtr->width, textPtr->height,
1248 textPtr->charWidth, fm.linespace);
1249 } else {
1250 Tk_UnsetGrid(textPtr->tkwin);
1251 }
1252
1253 TkTextRelayoutWindow(textPtr);
1254 }
1255
1256 /*
1257 *--------------------------------------------------------------
1258 *
1259 * TextEventProc --
1260 *
1261 * This procedure is invoked by the Tk dispatcher on
1262 * structure changes to a text. For texts with 3D
1263 * borders, this procedure is also invoked for exposures.
1264 *
1265 * Results:
1266 * None.
1267 *
1268 * Side effects:
1269 * When the window gets deleted, internal structures get
1270 * cleaned up. When it gets exposed, it is redisplayed.
1271 *
1272 *--------------------------------------------------------------
1273 */
1274
1275 static void
1276 TextEventProc(clientData, eventPtr)
1277 ClientData clientData; /* Information about window. */
1278 register XEvent *eventPtr; /* Information about event. */
1279 {
1280 register TkText *textPtr = (TkText *) clientData;
1281 TkTextIndex index, index2;
1282
1283 if (eventPtr->type == Expose) {
1284 TkTextRedrawRegion(textPtr, eventPtr->xexpose.x,
1285 eventPtr->xexpose.y, eventPtr->xexpose.width,
1286 eventPtr->xexpose.height);
1287 } else if (eventPtr->type == ConfigureNotify) {
1288 if ((textPtr->prevWidth != Tk_Width(textPtr->tkwin))
1289 || (textPtr->prevHeight != Tk_Height(textPtr->tkwin))) {
1290 TkTextRelayoutWindow(textPtr);
1291 textPtr->prevWidth = Tk_Width(textPtr->tkwin);
1292 textPtr->prevHeight = Tk_Height(textPtr->tkwin);
1293 }
1294 } else if (eventPtr->type == DestroyNotify) {
1295 if (textPtr->tkwin != NULL) {
1296 if (textPtr->setGrid) {
1297 Tk_UnsetGrid(textPtr->tkwin);
1298 }
1299 textPtr->tkwin = NULL;
1300 Tcl_DeleteCommandFromToken(textPtr->interp,
1301 textPtr->widgetCmd);
1302 }
1303 Tcl_EventuallyFree((ClientData) textPtr, DestroyText);
1304 } else if ((eventPtr->type == FocusIn) || (eventPtr->type == FocusOut)) {
1305 if (eventPtr->xfocus.detail != NotifyInferior) {
1306 Tcl_DeleteTimerHandler(textPtr->insertBlinkHandler);
1307 if (eventPtr->type == FocusIn) {
1308 textPtr->flags |= GOT_FOCUS | INSERT_ON;
1309 if (textPtr->insertOffTime != 0) {
1310 textPtr->insertBlinkHandler = Tcl_CreateTimerHandler(
1311 textPtr->insertOnTime, TextBlinkProc,
1312 (ClientData) textPtr);
1313 }
1314 } else {
1315 textPtr->flags &= ~(GOT_FOCUS | INSERT_ON);
1316 textPtr->insertBlinkHandler = (Tcl_TimerToken) NULL;
1317 }
1318 #ifndef ALWAYS_SHOW_SELECTION
1319 TkTextRedrawTag(textPtr, NULL, NULL, textPtr->selTagPtr, 1);
1320 #endif
1321 TkTextMarkSegToIndex(textPtr, textPtr->insertMarkPtr, &index);
1322 TkTextIndexForwChars(&index, 1, &index2);
1323 TkTextChanged(textPtr, &index, &index2);
1324 if (textPtr->highlightWidth > 0) {
1325 TkTextRedrawRegion(textPtr, 0, 0, textPtr->highlightWidth,
1326 textPtr->highlightWidth);
1327 }
1328 }
1329 }
1330 }
1331
1332 /*
1333 *----------------------------------------------------------------------
1334 *
1335 * TextCmdDeletedProc --
1336 *
1337 * This procedure is invoked when a widget command is deleted. If
1338 * the widget isn't already in the process of being destroyed,
1339 * this command destroys it.
1340 *
1341 * Results:
1342 * None.
1343 *
1344 * Side effects:
1345 * The widget is destroyed.
1346 *
1347 *----------------------------------------------------------------------
1348 */
1349
1350 static void
1351 TextCmdDeletedProc(clientData)
1352 ClientData clientData; /* Pointer to widget record for widget. */
1353 {
1354 TkText *textPtr = (TkText *) clientData;
1355 Tk_Window tkwin = textPtr->tkwin;
1356
1357 /*
1358 * This procedure could be invoked either because the window was
1359 * destroyed and the command was then deleted (in which case tkwin
1360 * is NULL) or because the command was deleted, and then this procedure
1361 * destroys the widget.
1362 */
1363
1364 if (tkwin != NULL) {
1365 if (textPtr->setGrid) {
1366 Tk_UnsetGrid(textPtr->tkwin);
1367 }
1368 textPtr->tkwin = NULL;
1369 Tk_DestroyWindow(tkwin);
1370 }
1371 }
1372
1373 /*
1374 *----------------------------------------------------------------------
1375 *
1376 * InsertChars --
1377 *
1378 * This procedure implements most of the functionality of the
1379 * "insert" widget command.
1380 *
1381 * Results:
1382 * None.
1383 *
1384 * Side effects:
1385 * The characters in "string" get added to the text just before
1386 * the character indicated by "indexPtr".
1387 *
1388 *----------------------------------------------------------------------
1389 */
1390
1391 static void
1392 InsertChars(textPtr, indexPtr, string)
1393 TkText *textPtr; /* Overall information about text widget. */
1394 TkTextIndex *indexPtr; /* Where to insert new characters. May be
1395 * modified and/or invalidated. */
1396 CONST char *string; /* Null-terminated string containing new
1397 * information to add to text. */
1398 {
1399 int lineIndex, resetView, offset;
1400 TkTextIndex newTop;
1401 char indexBuffer[TK_POS_CHARS];
1402
1403 /*
1404 * Don't allow insertions on the last (dummy) line of the text.
1405 */
1406
1407 lineIndex = TkBTreeLineIndex(indexPtr->linePtr);
1408 if (lineIndex == TkBTreeNumLines(textPtr->tree)) {
1409 lineIndex--;
1410 TkTextMakeByteIndex(textPtr->tree, lineIndex, 1000000, indexPtr);
1411 }
1412
1413 /*
1414 * Notify the display module that lines are about to change, then do
1415 * the insertion. If the insertion occurs on the top line of the
1416 * widget (textPtr->topIndex), then we have to recompute topIndex
1417 * after the insertion, since the insertion could invalidate it.
1418 */
1419
1420 resetView = offset = 0;
1421 if (indexPtr->linePtr == textPtr->topIndex.linePtr) {
1422 resetView = 1;
1423 offset = textPtr->topIndex.byteIndex;
1424 if (offset > indexPtr->byteIndex) {
1425 offset += strlen(string);
1426 }
1427 }
1428 TkTextChanged(textPtr, indexPtr, indexPtr);
1429 TkBTreeInsertChars(indexPtr, string);
1430
1431 /*
1432 * Push the insertion on the undo stack
1433 */
1434
1435 if ( textPtr->undo ) {
1436 #ifndef _LANG
1437 TkTextIndex toIndex;
1438
1439 Tcl_DString actionCommand;
1440 Tcl_DString revertCommand;
1441
1442 if (textPtr->autoSeparators &&
1443 textPtr->lastEditMode != TK_TEXT_EDIT_INSERT) {
1444 TkUndoInsertUndoSeparator(textPtr->undoStack);
1445 }
1446
1447 textPtr->lastEditMode = TK_TEXT_EDIT_INSERT;
1448
1449 Tcl_DStringInit(&actionCommand);
1450 Tcl_DStringInit(&revertCommand);
1451
1452 Tcl_DStringAppend(&actionCommand,Tcl_GetCommandName(textPtr->interp,textPtr->widgetCmd),-1);
1453 Tcl_DStringAppend(&actionCommand," insert ",-1);
1454 TkTextPrintIndex(indexPtr,indexBuffer);
1455 Tcl_DStringAppend(&actionCommand,indexBuffer,-1);
1456 Tcl_DStringAppend(&actionCommand," ",-1);
1457 Tcl_DStringAppendElement(&actionCommand,string);
1458 Tcl_DStringAppend(&actionCommand,";",-1);
1459 Tcl_DStringAppend(&actionCommand,Tcl_GetCommandName(textPtr->interp,textPtr->widgetCmd),-1);
1460 Tcl_DStringAppend(&actionCommand," mark set insert ",-1);
1461 TkTextIndexForwBytes(indexPtr, (int) strlen(string),
1462 &toIndex);
1463 TkTextPrintIndex(&toIndex, indexBuffer);
1464 Tcl_DStringAppend(&actionCommand,indexBuffer,-1);
1465 Tcl_DStringAppend(&actionCommand,"; ",-1);
1466 Tcl_DStringAppend(&actionCommand,Tcl_GetCommandName(textPtr->interp,textPtr->widgetCmd),-1);
1467 Tcl_DStringAppend(&actionCommand," see insert",-1);
1468
1469 Tcl_DStringAppend(&revertCommand,Tcl_GetCommandName(textPtr->interp,textPtr->widgetCmd),-1);
1470 Tcl_DStringAppend(&revertCommand," delete ",-1);
1471 TkTextPrintIndex(indexPtr,indexBuffer);
1472 Tcl_DStringAppend(&revertCommand,indexBuffer,-1);
1473 Tcl_DStringAppend(&revertCommand," ",-1);
1474 TkTextPrintIndex(&toIndex, indexBuffer);
1475 Tcl_DStringAppend(&revertCommand,indexBuffer,-1);
1476 Tcl_DStringAppend(&revertCommand," ;",-1);
1477 Tcl_DStringAppend(&revertCommand,Tcl_GetCommandName(textPtr->interp,textPtr->widgetCmd),-1);
1478 Tcl_DStringAppend(&revertCommand," mark set insert ",-1);
1479 TkTextPrintIndex(indexPtr,indexBuffer);
1480 Tcl_DStringAppend(&revertCommand,indexBuffer,-1);
1481 Tcl_DStringAppend(&revertCommand,"; ",-1);
1482 Tcl_DStringAppend(&revertCommand,Tcl_GetCommandName(textPtr->interp,textPtr->widgetCmd),-1);
1483 Tcl_DStringAppend(&revertCommand," see insert",-1);
1484
1485 TkUndoPushAction(textPtr->undoStack,&actionCommand, &revertCommand);
1486
1487 Tcl_DStringFree(&actionCommand);
1488 Tcl_DStringFree(&revertCommand);
1489 #endif
1490
1491 }
1492 updateDirtyFlag(textPtr);
1493
1494 if (resetView) {
1495 TkTextMakeByteIndex(textPtr->tree, lineIndex, 0, &newTop);
1496 TkTextIndexForwBytes(&newTop, offset, &newTop);
1497 TkTextSetYView(textPtr, &newTop, 0);
1498 }
1499
1500 /*
1501 * Invalidate any selection retrievals in progress.
1502 */
1503
1504 textPtr->abortSelections = 1;
1505 }
1506
1507 /*
1508 *----------------------------------------------------------------------
1509 *
1510 * DeleteChars --
1511 *
1512 * This procedure implements most of the functionality of the
1513 * "delete" widget command.
1514 *
1515 * Results:
1516 * Returns a standard Tcl result, and leaves an error message
1517 * in textPtr->interp if there is an error.
1518 *
1519 * Side effects:
1520 * Characters get deleted from the text.
1521 *
1522 *----------------------------------------------------------------------
1523 */
1524
1525 static int
1526 DeleteChars(textPtr, index1String, index2String, indexPtr1, indexPtr2)
1527 TkText *textPtr; /* Overall information about text widget. */
1528 CONST char *index1String; /* String describing location of first
1529 * character to delete. */
1530 CONST char *index2String; /* String describing location of last
1531 * character to delete. NULL means just
1532 * delete the one character given by
1533 * index1String. */
1534 TkTextIndex *indexPtr1; /* index describing location of first
1535 * character to delete. */
1536 TkTextIndex *indexPtr2; /* index describing location of last
1537 * character to delete. NULL means just
1538 * delete the one character given by
1539 * indexPtr1. */
1540 {
1541 int line1, line2, line, byteIndex, resetView;
1542 TkTextIndex index1, index2;
1543 char indexBuffer[TK_POS_CHARS];
1544
1545 /*
1546 * Parse the starting and stopping indices.
1547 */
1548
1549 if (index1String != NULL) {
1550 if (TkTextGetIndex(textPtr->interp, textPtr, index1String, &index1)
1551 != TCL_OK) {
1552 return TCL_ERROR;
1553 }
1554 if (index2String != NULL) {
1555 if (TkTextGetIndex(textPtr->interp, textPtr, index2String, &index2)
1556 != TCL_OK) {
1557 return TCL_ERROR;
1558 }
1559 } else {
1560 index2 = index1;
1561 TkTextIndexForwChars(&index2, 1, &index2);
1562 }
1563 } else {
1564 index1 = *indexPtr1;
1565 if (indexPtr2 != NULL) {
1566 index2 = *indexPtr2;
1567 } else {
1568 index2 = index1;
1569 TkTextIndexForwChars(&index2, 1, &index2);
1570 }
1571 }
1572
1573 /*
1574 * Make sure there's really something to delete.
1575 */
1576
1577 if (TkTextIndexCmp(&index1, &index2) >= 0) {
1578 return TCL_OK;
1579 }
1580
1581 /*
1582 * The code below is ugly, but it's needed to make sure there
1583 * is always a dummy empty line at the end of the text. If the
1584 * final newline of the file (just before the dummy line) is being
1585 * deleted, then back up index to just before the newline. If
1586 * there is a newline just before the first character being deleted,
1587 * then back up the first index too, so that an even number of lines
1588 * gets deleted. Furthermore, remove any tags that are present on
1589 * the newline that isn't going to be deleted after all (this simulates
1590 * deleting the newline and then adding a "clean" one back again).
1591 */
1592
1593 line1 = TkBTreeLineIndex(index1.linePtr);
1594 line2 = TkBTreeLineIndex(index2.linePtr);
1595 if (line2 == TkBTreeNumLines(textPtr->tree)) {
1596 TkTextTag **arrayPtr;
1597 int arraySize, i;
1598 TkTextIndex oldIndex2;
1599
1600 oldIndex2 = index2;
1601 TkTextIndexBackChars(&oldIndex2, 1, &index2);
1602 line2--;
1603 if ((index1.byteIndex == 0) && (line1 != 0)) {
1604 TkTextIndexBackChars(&index1, 1, &index1);
1605 line1--;
1606 }
1607 arrayPtr = TkBTreeGetTags(&index2, &arraySize);
1608 if (arrayPtr != NULL) {
1609 for (i = 0; i < arraySize; i++) {
1610 TkBTreeTag(&index2, &oldIndex2, arrayPtr[i], 0);
1611 }
1612 ckfree((char *) arrayPtr);
1613 }
1614 }
1615
1616 /*
1617 * Tell the display what's about to happen so it can discard
1618 * obsolete display information, then do the deletion. Also,
1619 * if the deletion involves the top line on the screen, then
1620 * we have to reset the view (the deletion will invalidate
1621 * textPtr->topIndex). Compute what the new first character
1622 * will be, then do the deletion, then reset the view.
1623 */
1624
1625 TkTextChanged(textPtr, &index1, &index2);
1626 resetView = 0;
1627 line = 0;
1628 byteIndex = 0;
1629 if (TkTextIndexCmp(&index2, &textPtr->topIndex) >= 0) {
1630 if (TkTextIndexCmp(&index1, &textPtr->topIndex) <= 0) {
1631 /*
1632 * Deletion range straddles topIndex: use the beginning
1633 * of the range as the new topIndex.
1634 */
1635
1636 resetView = 1;
1637 line = line1;
1638 byteIndex = index1.byteIndex;
1639 } else if (index1.linePtr == textPtr->topIndex.linePtr) {
1640 /*
1641 * Deletion range starts on top line but after topIndex.
1642 * Use the current topIndex as the new one.
1643 */
1644
1645 resetView = 1;
1646 line = line1;
1647 byteIndex = textPtr->topIndex.byteIndex;
1648 }
1649 } else if (index2.linePtr == textPtr->topIndex.linePtr) {
1650 /*
1651 * Deletion range ends on top line but before topIndex.
1652 * Figure out what will be the new character index for
1653 * the character currently pointed to by topIndex.
1654 */
1655
1656 resetView = 1;
1657 line = line2;
1658 byteIndex = textPtr->topIndex.byteIndex;
1659 if (index1.linePtr != index2.linePtr) {
1660 byteIndex -= index2.byteIndex;
1661 } else {
1662 byteIndex -= (index2.byteIndex - index1.byteIndex);
1663 }
1664 }
1665
1666 /*
1667 * Push the deletion on the undo stack
1668 */
1669
1670 if (textPtr->undo) {
1671 #ifndef _LANG
1672 Tcl_DString ds;
1673 Tcl_DString actionCommand;
1674 Tcl_DString revertCommand;
1675
1676 if (textPtr->autoSeparators
1677 && (textPtr->lastEditMode != TK_TEXT_EDIT_DELETE)) {
1678 TkUndoInsertUndoSeparator(textPtr->undoStack);
1679 }
1680
1681 textPtr->lastEditMode = TK_TEXT_EDIT_DELETE;
1682
1683 Tcl_DStringInit(&actionCommand);
1684 Tcl_DStringInit(&revertCommand);
1685
1686 Tcl_DStringAppend(&actionCommand,Tcl_GetCommandName(textPtr->interp,textPtr->widgetCmd),-1);
1687 Tcl_DStringAppend(&actionCommand," delete ",-1);
1688 TkTextPrintIndex(&index1,indexBuffer);
1689 Tcl_DStringAppend(&actionCommand,indexBuffer,-1);
1690 Tcl_DStringAppend(&actionCommand," ",-1);
1691 TkTextPrintIndex(&index2, indexBuffer);
1692 Tcl_DStringAppend(&actionCommand,indexBuffer,-1);
1693 Tcl_DStringAppend(&actionCommand,"; ",-1);
1694 Tcl_DStringAppend(&actionCommand,Tcl_GetCommandName(textPtr->interp,textPtr->widgetCmd),-1);
1695 Tcl_DStringAppend(&actionCommand," mark set insert ",-1);
1696 TkTextPrintIndex(&index1,indexBuffer);
1697 Tcl_DStringAppend(&actionCommand,indexBuffer,-1);
1698
1699 Tcl_DStringAppend(&actionCommand,"; ",-1);
1700 Tcl_DStringAppend(&actionCommand,Tcl_GetCommandName(textPtr->interp,textPtr->widgetCmd),-1);
1701 Tcl_DStringAppend(&actionCommand," see insert",-1);
1702
1703 TextGetText(&index1, &index2, &ds);
1704
1705 Tcl_DStringAppend(&revertCommand,Tcl_GetCommandName(textPtr->interp,textPtr->widgetCmd),-1);
1706 Tcl_DStringAppend(&revertCommand," insert ",-1);
1707 TkTextPrintIndex(&index1,indexBuffer);
1708 Tcl_DStringAppend(&revertCommand,indexBuffer,-1);
1709 Tcl_DStringAppend(&revertCommand," ",-1);
1710 Tcl_DStringAppendElement(&revertCommand,Tcl_DStringValue(&ds));
1711 Tcl_DStringAppend(&revertCommand,"; ",-1);
1712 Tcl_DStringAppend(&revertCommand,Tcl_GetCommandName(textPtr->interp,textPtr->widgetCmd),-1);
1713 Tcl_DStringAppend(&revertCommand," mark set insert ",-1);
1714 TkTextPrintIndex(&index2, indexBuffer);
1715 Tcl_DStringAppend(&revertCommand,indexBuffer,-1);
1716 Tcl_DStringAppend(&revertCommand,"; ",-1);
1717 Tcl_DStringAppend(&revertCommand,Tcl_GetCommandName(textPtr->interp,textPtr->widgetCmd),-1);
1718 Tcl_DStringAppend(&revertCommand," see insert",-1);
1719
1720 TkUndoPushAction(textPtr->undoStack,&actionCommand, &revertCommand);
1721
1722 Tcl_DStringFree(&actionCommand);
1723 Tcl_DStringFree(&revertCommand);
1724 #endif
1725 }
1726 updateDirtyFlag(textPtr);
1727
1728 TkBTreeDeleteChars(&index1, &index2);
1729 if (resetView) {
1730 TkTextMakeByteIndex(textPtr->tree, line, byteIndex, &index1);
1731 TkTextSetYView(textPtr, &index1, 0);
1732 }
1733
1734 /*
1735 * Invalidate any selection retrievals in progress.
1736 */
1737
1738 textPtr->abortSelections = 1;
1739
1740 return TCL_OK;
1741 }
1742
1743 /*
1744 *----------------------------------------------------------------------
1745 *
1746 * TextFetchSelection --
1747 *
1748 * This procedure is called back by Tk when the selection is
1749 * requested by someone. It returns part or all of the selection
1750 * in a buffer provided by the caller.
1751 *
1752 * Results:
1753 * The return value is the number of non-NULL bytes stored
1754 * at buffer. Buffer is filled (or partially filled) with a
1755 * NULL-terminated string containing part or all of the selection,
1756 * as given by offset and maxBytes.
1757 *
1758 * Side effects:
1759 * None.
1760 *
1761 *----------------------------------------------------------------------
1762 */
1763
1764 static int
1765 TextFetchSelection(clientData, offset, buffer, maxBytes)
1766 ClientData clientData; /* Information about text widget. */
1767 int offset; /* Offset within selection of first
1768 * character to be returned. */
1769 char *buffer; /* Location in which to place
1770 * selection. */
1771 int maxBytes; /* Maximum number of bytes to place
1772 * at buffer, not including terminating
1773 * NULL character. */
1774 {
1775 register TkText *textPtr = (TkText *) clientData;
1776 TkTextIndex eof;
1777 int count, chunkSize, offsetInSeg;
1778 TkTextSearch search;
1779 TkTextSegment *segPtr;
1780
1781 if (!textPtr->exportSelection) {
1782 return -1;
1783 }
1784
1785 /*
1786 * Find the beginning of the next range of selected text. Note: if
1787 * the selection is being retrieved in multiple pieces (offset != 0)
1788 * and some modification has been made to the text that affects the
1789 * selection then reject the selection request (make 'em start over
1790 * again).
1791 */
1792
1793 if (offset == 0) {
1794 TkTextMakeByteIndex(textPtr->tree, 0, 0, &textPtr->selIndex);
1795 textPtr->abortSelections = 0;
1796 } else if (textPtr->abortSelections) {
1797 return 0;
1798 }
1799 TkTextMakeByteIndex(textPtr->tree, TkBTreeNumLines(textPtr->tree), 0, &eof);
1800 TkBTreeStartSearch(&textPtr->selIndex, &eof, textPtr->selTagPtr, &search);
1801 if (!TkBTreeCharTagged(&textPtr->selIndex, textPtr->selTagPtr)) {
1802 if (!TkBTreeNextTag(&search)) {
1803 if (offset == 0) {
1804 return -1;
1805 } else {
1806 return 0;
1807 }
1808 }
1809 textPtr->selIndex = search.curIndex;
1810 }
1811
1812 /*
1813 * Each iteration through the outer loop below scans one selected range.
1814 * Each iteration through the inner loop scans one segment in the
1815 * selected range.
1816 */
1817
1818 count = 0;
1819 while (1) {
1820 /*
1821 * Find the end of the current range of selected text.
1822 */
1823
1824 if (!TkBTreeNextTag(&search)) {
1825 panic("TextFetchSelection couldn't find end of range");
1826 }
1827
1828 /*
1829 * Copy information from character segments into the buffer
1830 * until either we run out of space in the buffer or we get
1831 * to the end of this range of text.
1832 */
1833
1834 while (1) {
1835 if (maxBytes == 0) {
1836 goto done;
1837 }
1838 segPtr = TkTextIndexToSeg(&textPtr->selIndex, &offsetInSeg);
1839 chunkSize = segPtr->size - offsetInSeg;
1840 if (chunkSize > maxBytes) {
1841 chunkSize = maxBytes;
1842 }
1843 if (textPtr->selIndex.linePtr == search.curIndex.linePtr) {
1844 int leftInRange;
1845
1846 leftInRange = search.curIndex.byteIndex
1847 - textPtr->selIndex.byteIndex;
1848 if (leftInRange < chunkSize) {
1849 chunkSize = leftInRange;
1850 if (chunkSize <= 0) {
1851 break;
1852 }
1853 }
1854 }
1855 if ((segPtr->typePtr == &tkTextCharType)
1856 && !TkTextIsElided(textPtr, &textPtr->selIndex)) {
1857 memcpy((VOID *) buffer, (VOID *) (segPtr->body.chars
1858 + offsetInSeg), (size_t) chunkSize);
1859 buffer += chunkSize;
1860 maxBytes -= chunkSize;
1861 count += chunkSize;
1862 }
1863 TkTextIndexForwBytes(&textPtr->selIndex, chunkSize,
1864 &textPtr->selIndex);
1865 }
1866
1867 /*
1868 * Find the beginning of the next range of selected text.
1869 */
1870
1871 if (!TkBTreeNextTag(&search)) {
1872 break;
1873 }
1874 textPtr->selIndex = search.curIndex;
1875 }
1876
1877 done:
1878 *buffer = 0;
1879 return count;
1880 }
1881
1882 /*
1883 *----------------------------------------------------------------------
1884 *
1885 * TkTextLostSelection --
1886 *
1887 * This procedure is called back by Tk when the selection is
1888 * grabbed away from a text widget. On Windows and Mac systems, we
1889 * want to remember the selection for the next time the focus
1890 * enters the window. On Unix, just remove the "sel" tag from
1891 * everything in the widget.
1892 *
1893 * Results:
1894 * None.
1895 *
1896 * Side effects:
1897 * The "sel" tag is cleared from the window.
1898 *
1899 *----------------------------------------------------------------------
1900 */
1901
1902 void
1903 TkTextLostSelection(clientData)
1904 ClientData clientData; /* Information about text widget. */
1905 {
1906 register TkText *textPtr = (TkText *) clientData;
1907 XEvent event;
1908 #ifdef ALWAYS_SHOW_SELECTION
1909 TkTextIndex start, end;
1910
1911 if (!textPtr->exportSelection) {
1912 return;
1913 }
1914
1915 /*
1916 * On Windows and Mac systems, we want to remember the selection
1917 * for the next time the focus enters the window. On Unix,
1918 * just remove the "sel" tag from everything in the widget.
1919 */
1920
1921 TkTextMakeByteIndex(textPtr->tree, 0, 0, &start);
1922 TkTextMakeByteIndex(textPtr->tree, TkBTreeNumLines(textPtr->tree), 0, &end);
1923 TkTextRedrawTag(textPtr, &start, &end, textPtr->selTagPtr, 1);
1924 TkBTreeTag(&start, &end, textPtr->selTagPtr, 0);
1925 #endif
1926
1927 /*
1928 * Send an event that the selection changed. This is equivalent to
1929 * "event generate $textWidget <<Selection>>"
1930 */
1931
1932 memset((VOID *) &event, 0, sizeof(event));
1933 event.xany.type = VirtualEvent;
1934 event.xany.serial = NextRequest(Tk_Display(textPtr->tkwin));
1935 event.xany.send_event = False;
1936 event.xany.window = Tk_WindowId(textPtr->tkwin);
1937 event.xany.display = Tk_Display(textPtr->tkwin);
1938 ((XVirtualEvent *) &event)->name = Tk_GetUid("Selection");
1939 Tk_HandleEvent(&event);
1940
1941 textPtr->flags &= ~GOT_SELECTION;
1942 }
1943
1944 /*
1945 *----------------------------------------------------------------------
1946 *
1947 * TextBlinkProc --
1948 *
1949 * This procedure is called as a timer handler to blink the
1950 * insertion cursor off and on.
1951 *
1952 * Results:
1953 * None.
1954 *
1955 * Side effects:
1956 * The cursor gets turned on or off, redisplay gets invoked,
1957 * and this procedure reschedules itself.
1958 *
1959 *----------------------------------------------------------------------
1960 */
1961
1962 static void
1963 TextBlinkProc(clientData)
1964 ClientData clientData; /* Pointer to record describing text. */
1965 {
1966 register TkText *textPtr = (TkText *) clientData;
1967 TkTextIndex index;
1968 int x, y, w, h;
1969
1970 if ((textPtr->state == TK_STATE_DISABLED) ||
1971 !(textPtr->flags & GOT_FOCUS) || (textPtr->insertOffTime == 0)) {
1972 return;
1973 }
1974 if (textPtr->flags & INSERT_ON) {
1975 textPtr->flags &= ~INSERT_ON;
1976 textPtr->insertBlinkHandler = Tcl_CreateTimerHandler(
1977 textPtr->insertOffTime, TextBlinkProc, (ClientData) textPtr);
1978 } else {
1979 textPtr->flags |= INSERT_ON;
1980 textPtr->insertBlinkHandler = Tcl_CreateTimerHandler(
1981 textPtr->insertOnTime, TextBlinkProc, (ClientData) textPtr);
1982 }
1983 TkTextMarkSegToIndex(textPtr, textPtr->insertMarkPtr, &index);
1984 if (TkTextCharBbox(textPtr, &index, &x, &y, &w, &h) == 0) {
1985 TkTextRedrawRegion(textPtr, x - textPtr->insertWidth / 2, y,
1986 textPtr->insertWidth, h);
1987 }
1988 }
1989
1990 /*
1991 *----------------------------------------------------------------------
1992 *
1993 * TextSearchCmd --
1994 *
1995 * This procedure is invoked to process the "search" widget command
1996 * for text widgets. See the user documentation for details on what
1997 * it does.
1998 *
1999 * Results:
2000 * A standard Tcl result.
2001 *
2002 * Side effects:
2003 * See the user documentation.
2004 *
2005 *----------------------------------------------------------------------
2006 */
2007
2008 static int
2009 TextSearchCmd(textPtr, interp, argc, argv)
2010 TkText *textPtr; /* Information about text widget. */
2011 Tcl_Interp *interp; /* Current interpreter. */
2012 int argc; /* Number of arguments. */
2013 char **argv; /* Argument strings. */
2014 {
2015 int backwards, exact, searchElide, c, i, argsLeft, noCase, leftToScan;
2016 size_t length;
2017 int numLines, startingLine, startingByte, lineNum, firstByte, lastByte;
2018 int code, matchLength, matchByte, passes, stopLine, searchWholeText;
2019 int patLength;
2020 CONST char *arg, *pattern, *p, *startOfLine;
2021 Tcl_Obj *varName;
2022 char buffer[20];
2023 TkTextIndex index, stopIndex;
2024 Tcl_DString line;
2025 Tcl_DString patDString;
2026 TkTextSegment *segPtr;
2027 TkTextLine *linePtr;
2028 TkTextIndex curIndex;
2029 Tcl_Obj *patObj = NULL;
2030 Tcl_RegExp regexp = NULL; /* Initialization needed only to
2031 * prevent compiler warning. */
2032
2033 /*
2034 * Parse switches and other arguments.
2035 */
2036
2037 exact = 1;
2038 searchElide = 0;
2039 curIndex.tree = textPtr->tree;
2040 backwards = 0;
2041 noCase = 0;
2042 varName = NULL;
2043 for (i = 2; i < argc; i++) {
2044 arg = argv[i];
2045 if (arg[0] != '-') {
2046 break;
2047 }
2048 length = strlen(arg);
2049 if (length < 2) {
2050 badSwitch:
2051 Tcl_AppendResult(interp, "bad switch \"", arg,
2052 "\": must be --, -backward, -count, -elide, -exact, ",
2053 "-forward, -nocase, or -regexp", (char *) NULL);
2054 return TCL_ERROR;
2055 }
2056 c = arg[1];
2057 if ((c == 'b') && (strncmp(argv[i], "-backwards", length) == 0)) {
2058 backwards = 1;
2059 } else if ((c == 'c') && (strncmp(argv[i], "-count", length) == 0)) {
2060 if (i >= (argc-1)) {
2061 Tcl_SetResult(interp, "no value given for \"-count\" option",
2062 TCL_STATIC);
2063 return TCL_ERROR;
2064 }
2065 i++;
2066 varName = objv[i];
2067 } else if ((c == 'e') && (length > 2)
2068 && (strncmp(argv[i], "-exact", length) == 0)) {
2069 exact = 1;
2070 } else if ((c == 'e') && (length > 2)
2071 && (strncmp(argv[i], "-elide", length) == 0)) {
2072 searchElide = 1;
2073 } else if ((c == 'h') && (strncmp(argv[i], "-hidden", length) == 0)) {
2074 /*
2075 * -hidden is kept around for backwards compatibility with
2076 * the dash patch, but -elide is the official option
2077 */
2078 searchElide = 1;
2079 } else if ((c == 'f') && (strncmp(argv[i], "-forwards", length) == 0)) {
2080 backwards = 0;
2081 } else if ((c == 'n') && (strncmp(argv[i], "-nocase", length) == 0)) {
2082 noCase = 1;
2083 } else if ((c == 'r') && (strncmp(argv[i], "-regexp", length) == 0)) {
2084 exact = 0;
2085 } else if ((c == 'h') && (strncmp(argv[i], "-hidden", length) == 0)) {
2086 searchElide = 1;
2087 } else if ((c == 'e') && (strncmp(argv[i], "-elide", length) == 0)) {
2088 searchElide = 1;
2089 } else if ((c == '-') && (strncmp(argv[i], "--", length) == 0)) {
2090 i++;
2091 break;
2092 } else {
2093 goto badSwitch;
2094 }
2095 }
2096 argsLeft = argc - (i+2);
2097 if ((argsLeft != 0) && (argsLeft != 1)) {
2098 Tcl_AppendResult(interp, "wrong # args: should be \"",
2099 argv[0], " search ?switches? pattern index ?stopIndex?\"",
2100 (char *) NULL);
2101 return TCL_ERROR;
2102 }
2103 pattern = argv[i];
2104 #ifdef _LANG
2105 patObj = objv[i];
2106 Tcl_IncrRefCount(patObj);
2107 #endif
2108
2109 /*
2110 * Convert the pattern to lower-case if we're supposed to ignore case.
2111 */
2112
2113 if (noCase && exact) {
2114 Tcl_DStringInit(&patDString);
2115 Tcl_DStringAppend(&patDString, pattern, -1);
2116 Tcl_UtfToLower(Tcl_DStringValue(&patDString));
2117 pattern = Tcl_DStringValue(&patDString);
2118 }
2119
2120 Tcl_DStringInit(&line);
2121 if (TkTextGetIndex(interp, textPtr, argv[i+1], &index) != TCL_OK) {
2122 code = TCL_ERROR;
2123 goto done;
2124 }
2125 numLines = TkBTreeNumLines(textPtr->tree);
2126 startingLine = TkBTreeLineIndex(index.linePtr);
2127 startingByte = index.byteIndex;
2128 if (startingLine >= numLines) {
2129 if (backwards) {
2130 startingLine = TkBTreeNumLines(textPtr->tree) - 1;
2131 startingByte = TkBTreeBytesInLine(TkBTreeFindLine(textPtr->tree,
2132 startingLine));
2133 } else {
2134 startingLine = 0;
2135 startingByte = 0;
2136 }
2137 }
2138 if (argsLeft == 1) {
2139 if (TkTextGetIndex(interp, textPtr, argv[i+2], &stopIndex) != TCL_OK) {
2140 code = TCL_ERROR;
2141 goto done;
2142 }
2143 stopLine = TkBTreeLineIndex(stopIndex.linePtr);
2144 if (!backwards && (stopLine == numLines)) {
2145 stopLine = numLines-1;
2146 }
2147 searchWholeText = 0;
2148 } else {
2149 stopLine = 0;
2150 searchWholeText = 1;
2151 }
2152
2153 /*
2154 * Scan through all of the lines of the text circularly, starting
2155 * at the given index.
2156 */
2157
2158 matchLength = patLength = 0; /* Only needed to prevent compiler
2159 * warnings. */
2160 if (exact) {
2161 patLength = strlen(pattern);
2162 } else {
2163 #ifndef _LANG
2164 patObj = Tcl_NewStringObj(pattern, -1);
2165 Tcl_IncrRefCount(patObj);
2166 #endif
2167 regexp = Tcl_GetRegExpFromObj(interp, patObj,
2168 (noCase ? TCL_REG_NOCASE : 0) | TCL_REG_ADVANCED);
2169 if (regexp == NULL) {
2170 code = TCL_ERROR;
2171 goto done;
2172 }
2173 }
2174 lineNum = startingLine;
2175 code = TCL_OK;
2176 for (passes = 0; passes < 2; ) {
2177 if (lineNum >= numLines) {
2178 /*
2179 * Don't search the dummy last line of the text.
2180 */
2181
2182 goto nextLine;
2183 }
2184
2185 /*
2186 * Extract the text from the line. If we're doing regular
2187 * expression matching, drop the newline from the line, so
2188 * that "$" can be used to match the end of the line.
2189 */
2190
2191 linePtr = TkBTreeFindLine(textPtr->tree, lineNum);
2192 curIndex.linePtr = linePtr; curIndex.byteIndex = 0;
2193 for (segPtr = linePtr->segPtr; segPtr != NULL;
2194 curIndex.byteIndex += segPtr->size, segPtr = segPtr->nextPtr) {
2195 if ((segPtr->typePtr != &tkTextCharType)
2196 || (!searchElide && TkTextIsElided(textPtr, &curIndex))) {
2197 continue;
2198 }
2199 Tcl_DStringAppend(&line, segPtr->body.chars, segPtr->size);
2200 }
2201 if (!exact) {
2202 Tcl_DStringSetLength(&line, Tcl_DStringLength(&line)-1);
2203 }
2204 startOfLine = Tcl_DStringValue(&line);
2205
2206 /*
2207 * If we're ignoring case, convert the line to lower case.
2208 */
2209
2210 if (noCase && exact) {
2211 Tcl_DStringSetLength(&line,
2212 Tcl_UtfToLower(Tcl_DStringValue(&line)));
2213 }
2214
2215 /*
2216 * Check for matches within the current line. If so, and if we're
2217 * searching backwards, repeat the search to find the last match
2218 * in the line. (Note: The lastByte should include the NULL char
2219 * so we can handle searching for end of line easier.)
2220 */
2221
2222 matchByte = -1;
2223 firstByte = 0;
2224 lastByte = Tcl_DStringLength(&line) + 1;
2225 if (lineNum == startingLine) {
2226 int indexInDString;
2227
2228 /*
2229 * The starting line is tricky: the first time we see it
2230 * we check one part of the line, and the second pass through
2231 * we check the other part of the line. We have to be very
2232 * careful here because there could be embedded windows or
2233 * other things that are not in the extracted line. Rescan
2234 * the original line to compute the index in it of the first
2235 * character.
2236 */
2237
2238 indexInDString = startingByte;
2239 for (segPtr = linePtr->segPtr, leftToScan = startingByte;
2240 leftToScan > 0; segPtr = segPtr->nextPtr) {
2241 if (segPtr->typePtr != &tkTextCharType) {
2242 indexInDString -= segPtr->size;
2243 }
2244 leftToScan -= segPtr->size;
2245 }
2246
2247 passes++;
2248 if ((passes == 1) ^ backwards) {
2249 /*
2250 * Only use the last part of the line.
2251 */
2252
2253 firstByte = indexInDString;
2254 if ((firstByte >= Tcl_DStringLength(&line))
2255 && !((Tcl_DStringLength(&line) == 0) && !exact)) {
2256 goto nextLine;
2257 }
2258 } else {
2259 /*
2260 * Use only the first part of the line.
2261 */
2262
2263 lastByte = indexInDString;
2264 }
2265 }
2266 do {
2267 int thisLength;
2268 Tcl_UniChar ch;
2269
2270 if (exact) {
2271 p = strstr(startOfLine + firstByte, /* INTL: Native. */
2272 pattern);
2273 if (p == NULL) {
2274 break;
2275 }
2276 i = p - startOfLine;
2277 thisLength = patLength;
2278 } else {
2279 CONST84 char *start, *end;
2280 int match;
2281
2282 match = Tcl_RegExpExec(interp, regexp,
2283 startOfLine + firstByte, startOfLine);
2284 if (match < 0) {
2285 code = TCL_ERROR;
2286 goto done;
2287 }
2288 if (!match) {
2289 break;
2290 }
2291 Tcl_RegExpRange(regexp, 0, &start, &end);
2292 i = start - startOfLine;
2293 thisLength = end - start;
2294 }
2295 if (i >= lastByte) {
2296 break;
2297 }
2298 matchByte = i;
2299 matchLength = thisLength;
2300 firstByte = i + Tcl_UtfToUniChar(startOfLine + matchByte, &ch);
2301 } while (backwards);
2302
2303 /*
2304 * If we found a match then we're done. Make sure that
2305 * the match occurred before the stopping index, if one was
2306 * specified.
2307 */
2308
2309 if (matchByte >= 0) {
2310 int numChars;
2311
2312 /*
2313 * Convert the byte length to a character count.
2314 */
2315
2316 numChars = Tcl_NumUtfChars(startOfLine + matchByte,
2317 matchLength);
2318
2319 /*
2320 * The index information returned by the regular expression
2321 * parser only considers textual information: it doesn't
2322 * account for embedded windows, elided text (when we are not
2323 * searching elided text) or any other non-textual info.
2324 * Scan through the line's segments again to adjust both
2325 * matchChar and matchCount.
2326 *
2327 * We will walk through the segments of this line until we have
2328 * either reached the end of the match or we have reached the end
2329 * of the line.
2330 */
2331
2332 curIndex.linePtr = linePtr; curIndex.byteIndex = 0;
2333 for (segPtr = linePtr->segPtr, leftToScan = matchByte;
2334 leftToScan >= 0 && segPtr; segPtr = segPtr->nextPtr) {
2335 if (segPtr->typePtr != &tkTextCharType || \
2336 (!searchElide && TkTextIsElided(textPtr, &curIndex))) {
2337 matchByte += segPtr->size;
2338 } else {
2339 leftToScan -= segPtr->size;
2340 }
2341 curIndex.byteIndex += segPtr->size;
2342 }
2343 for (leftToScan += matchLength; leftToScan > 0;
2344 segPtr = segPtr->nextPtr) {
2345 if (segPtr->typePtr != &tkTextCharType) {
2346 numChars += segPtr->size;
2347 continue;
2348 }
2349 leftToScan -= segPtr->size;
2350 }
2351 TkTextMakeByteIndex(textPtr->tree, lineNum, matchByte, &index);
2352 if (!searchWholeText) {
2353 if (!backwards && (TkTextIndexCmp(&index, &stopIndex) >= 0)) {
2354 goto done;
2355 }
2356 if (backwards && (TkTextIndexCmp(&index, &stopIndex) < 0)) {
2357 goto done;
2358 }
2359 }
2360 if (varName != NULL) {
2361 Tcl_Obj *temp = Tcl_NewIntObj(numChars);
2362 if (Tcl_ObjSetVar2(interp, varName, NULL, temp, TCL_LEAVE_ERR_MSG)
2363 == NULL) {
2364 code = TCL_ERROR;
2365 Tcl_DecrRefCount(temp);
2366 goto done;
2367 }
2368 Tcl_DecrRefCount(temp);
2369 }
2370 TkTextPrintIndex(&index, buffer);
2371 Tcl_SetResult(interp, buffer, TCL_VOLATILE);
2372 goto done;
2373 }
2374
2375 /*
2376 * Go to the next (or previous) line;
2377 */
2378
2379 nextLine:
2380 if (backwards) {
2381 lineNum--;
2382 if (!searchWholeText) {
2383 if (lineNum < stopLine) {
2384 break;
2385 }
2386 } else if (lineNum < 0) {
2387 lineNum = numLines-1;
2388 }
2389 } else {
2390 lineNum++;
2391 if (!searchWholeText) {
2392 if (lineNum > stopLine) {
2393 break;
2394 }
2395 } else if (lineNum >= numLines) {
2396 lineNum = 0;
2397 }
2398 }
2399 Tcl_DStringSetLength(&line, 0);
2400 }
2401 done:
2402 Tcl_DStringFree(&line);
2403 if (noCase && exact) {
2404 Tcl_DStringFree(&patDString);
2405 }
2406 if (patObj != NULL) {
2407 Tcl_DecrRefCount(patObj);
2408 }
2409 #ifdef _LANG
2410 if (regexp) {
2411 Lang_FreeRegExp(regexp);
2412 }
2413 #endif
2414 return code;
2415 }
2416
2417 /*
2418 *----------------------------------------------------------------------
2419 *
2420 * TkTextGetTabs --
2421 *
2422 * Parses a string description of a set of tab stops.
2423 *
2424 * Results:
2425 * The return value is a pointer to a malloc'ed structure holding
2426 * parsed information about the tab stops. If an error occurred
2427 * then the return value is NULL and an error message is left in
2428 * the interp's result.
2429 *
2430 * Side effects:
2431 * Memory is allocated for the structure that is returned. It is
2432 * up to the caller to free this structure when it is no longer
2433 * needed.
2434 *
2435 *----------------------------------------------------------------------
2436 */
2437
2438 TkTextTabArray *
2439 TkTextGetTabs(interp, tkwin, arg)
2440 Tcl_Interp *interp; /* Used for error reporting. */
2441 Tk_Window tkwin; /* Window in which the tabs will be
2442 * used. */
2443 Tcl_Obj * arg; /* Description of the tab stops. See
2444 * the text manual entry for details. */
2445 {
2446 int argc, i, count, c;
2447 Tcl_Obj **objv;
2448 TkTextTabArray *tabArrayPtr;
2449 TkTextTab *tabPtr;
2450 Tcl_UniChar ch;
2451
2452 if (Tcl_ListObjGetElements(interp, arg, &argc, &argv) != TCL_OK) {
2453 return NULL;
2454 }
2455
2456 /*
2457 * First find out how many entries we need to allocate in the
2458 * tab array.
2459 */
2460
2461 count = 0;
2462 for (i = 0; i < argc; i++) {
2463 c = argv[i][0];
2464 if ((c != 'l') && (c != 'r') && (c != 'c') && (c != 'n')) {
2465 count++;
2466 }
2467 }
2468
2469 /*
2470 * Parse the elements of the list one at a time to fill in the
2471 * array.
2472 */
2473
2474 tabArrayPtr = (TkTextTabArray *) ckalloc((unsigned)
2475 (sizeof(TkTextTabArray) + (count-1)*sizeof(TkTextTab)));
2476 tabArrayPtr->numTabs = 0;
2477 for (i = 0, tabPtr = &tabArrayPtr->tabs[0]; i < argc; i++, tabPtr++) {
2478 if (Tk_GetPixels(interp, tkwin, argv[i], &tabPtr->location)
2479 != TCL_OK) {
2480 goto error;
2481 }
2482 tabArrayPtr->numTabs++;
2483
2484 /*
2485 * See if there is an explicit alignment in the next list
2486 * element. Otherwise just use "left".
2487 */
2488
2489 tabPtr->alignment = LEFT;
2490 if ((i+1) == argc) {
2491 continue;
2492 }
2493 Tcl_UtfToUniChar(argv[i+1], &ch);
2494 if (!Tcl_UniCharIsAlpha(ch)) {
2495 continue;
2496 }
2497 i += 1;
2498 c = argv[i][0];
2499 if ((c == 'l') && (strncmp(argv[i], "left",
2500 strlen(argv[i])) == 0)) {
2501 tabPtr->alignment = LEFT;
2502 } else if ((c == 'r') && (strncmp(argv[i], "right",
2503 strlen(argv[i])) == 0)) {
2504 tabPtr->alignment = RIGHT;
2505 } else if ((c == 'c') && (strncmp(argv[i], "center",
2506 strlen(argv[i])) == 0)) {
2507 tabPtr->alignment = CENTER;
2508 } else if ((c == 'n') && (strncmp(argv[i],
2509 "numeric", strlen(argv[i])) == 0)) {
2510 tabPtr->alignment = NUMERIC;
2511 } else {
2512 Tcl_AppendResult(interp, "bad tab alignment \"",
2513 argv[i], "\": must be left, right, center, or numeric",
2514 (char *) NULL);
2515 goto error;
2516 }
2517 }
2518 return tabArrayPtr;
2519
2520 error:
2521 ckfree((char *) tabArrayPtr);
2522 return NULL;
2523 }
2524
2525 /*
2526 *----------------------------------------------------------------------
2527 *
2528 * TextDumpCmd --
2529 *
2530 * Return information about the text, tags, marks, and embedded windows
2531 * and images in a text widget. See the man page for the description
2532 * of the text dump operation for all the details.
2533 *
2534 * Results:
2535 * A standard Tcl result.
2536 *
2537 * Side effects:
2538 * Memory is allocated for the result, if needed (standard Tcl result
2539 * side effects).
2540 *
2541 *----------------------------------------------------------------------
2542 */
2543
2544 static int
2545 TextDumpCmd(textPtr, interp, argc, argv)
2546 register TkText *textPtr; /* Information about text widget. */
2547 Tcl_Interp *interp; /* Current interpreter. */
2548 int argc; /* Number of arguments. */
2549 char **argv; /* Argument strings. Someone else has already
2550 * parsed this command enough to know that
2551 * argv[1] is "dump". */
2552 {
2553 TkTextIndex index1, index2;
2554 int arg;
2555 int lineno; /* Current line number */
2556 int what = 0; /* bitfield to select segment types */
2557 int atEnd; /* True if dumping up to logical end */
2558 TkTextLine *linePtr;
2559 LangCallback *command = NULL; /* Script callback to apply to segments */
2560 #define TK_DUMP_TEXT 0x1
2561 #define TK_DUMP_MARK 0x2
2562 #define TK_DUMP_TAG 0x4
2563 #define TK_DUMP_WIN 0x8
2564 #define TK_DUMP_IMG 0x10
2565 #define TK_DUMP_ALL (TK_DUMP_TEXT|TK_DUMP_MARK|TK_DUMP_TAG| \
2566 TK_DUMP_WIN|TK_DUMP_IMG)
2567
2568 for (arg=2 ; argv[arg] != (char *) NULL ; arg++) {
2569 size_t len;
2570 if (argv[arg][0] != '-') {
2571 break;
2572 }
2573 len = strlen(argv[arg]);
2574 if (strncmp("-all", argv[arg], len) == 0) {
2575 what = TK_DUMP_ALL;
2576 } else if (strncmp("-text", argv[arg], len) == 0) {
2577 what |= TK_DUMP_TEXT;
2578 } else if (strncmp("-tag", argv[arg], len) == 0) {
2579 what |= TK_DUMP_TAG;
2580 } else if (strncmp("-mark", argv[arg], len) == 0) {
2581 what |= TK_DUMP_MARK;
2582 } else if (strncmp("-image", argv[arg], len) == 0) {
2583 what |= TK_DUMP_IMG;
2584 } else if (strncmp("-window", argv[arg], len) == 0) {
2585 what |= TK_DUMP_WIN;
2586 } else if (strncmp("-command", argv[arg], len) == 0) {
2587 arg++;
2588 if (arg >= argc) {
2589 Tcl_AppendResult(interp, "Usage: ", argv[0], " dump ?-all -image -text -mark -tag -window? ?-command script? index ?index2?", NULL);
2590 return TCL_ERROR;
2591 }
2592 command = LangMakeCallback(objv[arg]);
2593 } else {
2594 Tcl_AppendResult(interp, "Usage: ", argv[0], " dump ?-all -image -text -mark -tag -window? ?-command script? index ?index2?", NULL);
2595 return TCL_ERROR;
2596 }
2597 }
2598 if (arg >= argc) {
2599 Tcl_AppendResult(interp, "Usage: ", argv[0], " dump ?-all -image -text -mark -tag -window? ?-command script? index ?index2?", NULL);
2600 return TCL_ERROR;
2601 }
2602 if (what == 0) {
2603 what = TK_DUMP_ALL;
2604 }
2605 if (TkTextGetIndex(interp, textPtr, argv[arg], &index1) != TCL_OK) {
2606 goto error;
2607 }
2608 lineno = TkBTreeLineIndex(index1.linePtr);
2609 arg++;
2610 atEnd = 0;
2611 if (argc == arg) {
2612 TkTextIndexForwChars(&index1, 1, &index2);
2613 } else {
2614 if (TkTextGetIndex(interp, textPtr, argv[arg], &index2) != TCL_OK) {
2615 goto error;
2616 }
2617 if (strncmp(argv[arg], "end", strlen(argv[arg])) == 0) {
2618 atEnd = 1;
2619 }
2620 }
2621 if (TkTextIndexCmp(&index1, &index2) >= 0) {
2622 goto ok;
2623 }
2624 if (index1.linePtr == index2.linePtr) {
2625 DumpLine(interp, textPtr, what, index1.linePtr,
2626 index1.byteIndex, index2.byteIndex, lineno, command);
2627 } else {
2628 DumpLine(interp, textPtr, what, index1.linePtr,
2629 index1.byteIndex, 32000000, lineno, command);
2630 linePtr = index1.linePtr;
2631 while ((linePtr = TkBTreeNextLine(linePtr)) != (TkTextLine *)NULL) {
2632 lineno++;
2633 if (linePtr == index2.linePtr) {
2634 break;
2635 }
2636 DumpLine(interp, textPtr, what, linePtr, 0, 32000000,
2637 lineno, command);
2638 }
2639 DumpLine(interp, textPtr, what, index2.linePtr, 0,
2640 index2.byteIndex, lineno, command);
2641 }
2642 /*
2643 * Special case to get the leftovers hiding at the end mark.
2644 */
2645 if (atEnd) {
2646 DumpLine(interp, textPtr, what & ~TK_DUMP_TEXT, index2.linePtr,
2647 0, 1, lineno, command);
2648
2649 }
2650 ok:
2651 if (command != (LangCallback *) NULL)
2652 LangFreeCallback(command);
2653 return TCL_OK;
2654 error:
2655 if (command != (LangCallback *) NULL)
2656 LangFreeCallback(command);
2657 return TCL_ERROR;
2658 }
2659
2660 /*
2661 * DumpLine
2662 * Return information about a given text line from character
2663 * position "start" up to, but not including, "end".
2664 *
2665 * Results:
2666 * A standard Tcl result.
2667 *
2668 * Side effects:
2669 * None, but see DumpSegment.
2670 */
2671 static void
2672 DumpLine(interp, textPtr, what, linePtr, startByte, endByte, lineno, command)
2673 Tcl_Interp *interp;
2674 TkText *textPtr;
2675 int what; /* bit flags to select segment types */
2676 TkTextLine *linePtr; /* The current line */
2677 int startByte, endByte; /* Byte range to dump */
2678 int lineno; /* Line number for indices dump */
2679 LangCallback *command; /* Script to apply to the segment */
2680 {
2681 int offset;
2682 TkTextSegment *segPtr;
2683 TkTextIndex index;
2684 /*
2685 * Must loop through line looking at its segments.
2686 * character
2687 * toggleOn, toggleOff
2688 * mark
2689 * image
2690 * window
2691 */
2692
2693 for (offset = 0, segPtr = linePtr->segPtr ;
2694 (offset < endByte) && (segPtr != (TkTextSegment *)NULL) ;
2695 offset += segPtr->size, segPtr = segPtr->nextPtr) {
2696 if ((what & TK_DUMP_TEXT) && (segPtr->typePtr == &tkTextCharType) &&
2697 (offset + segPtr->size > startByte)) {
2698 char savedChar; /* Last char used in the seg */
2699 int last = segPtr->size; /* Index of savedChar */
2700 int first = 0; /* Index of first char in seg */
2701 if (offset + segPtr->size > endByte) {
2702 last = endByte - offset;
2703 }
2704 if (startByte > offset) {
2705 first = startByte - offset;
2706 }
2707 savedChar = segPtr->body.chars[last];
2708 segPtr->body.chars[last] = '\0';
2709
2710 TkTextMakeByteIndex(textPtr->tree, lineno, offset + first, &index);
2711 DumpSegment(interp, "text", segPtr->body.chars + first, NULL,
2712 command, &index, what);
2713 segPtr->body.chars[last] = savedChar;
2714 } else if ((offset >= startByte)) {
2715 if ((what & TK_DUMP_MARK) && (segPtr->typePtr->name[0] == 'm')) {
2716 TkTextMark *markPtr = (TkTextMark *)&segPtr->body;
2717 char *name = Tcl_GetHashKey(&textPtr->markTable, markPtr->hPtr);
2718
2719 TkTextMakeByteIndex(textPtr->tree, lineno, offset, &index);
2720 DumpSegment(interp, "mark", name, NULL, command, &index, what);
2721 } else if ((what & TK_DUMP_TAG) &&
2722 (segPtr->typePtr == &tkTextToggleOnType)) {
2723 TkTextMakeByteIndex(textPtr->tree, lineno, offset, &index);
2724 DumpSegment(interp, "tagon",
2725 segPtr->body.toggle.tagPtr->name, NULL,
2726 command, &index, what);
2727 } else if ((what & TK_DUMP_TAG) &&
2728 (segPtr->typePtr == &tkTextToggleOffType)) {
2729 TkTextMakeByteIndex(textPtr->tree, lineno, offset, &index);
2730 DumpSegment(interp, "tagoff",
2731 segPtr->body.toggle.tagPtr->name, NULL,
2732 command, &index, what);
2733 } else if ((what & TK_DUMP_IMG) &&
2734 (segPtr->typePtr->name[0] == 'i')) {
2735 TkTextEmbImage *eiPtr = (TkTextEmbImage *)&segPtr->body;
2736 char *name = (eiPtr->name == NULL) ? "" : eiPtr->name;
2737 Tcl_Obj *obj;
2738 TkTextMakeByteIndex(textPtr->tree, lineno, offset, &index);
2739 obj = LangObjectObj( interp, eiPtr->name);
2740 DumpSegment(interp, "image", name, obj,
2741 command, &index, what);
2742 } else if ((what & TK_DUMP_WIN) &&
2743 (segPtr->typePtr->name[0] == 'w')) {
2744 TkTextEmbWindow *ewPtr = (TkTextEmbWindow *)&segPtr->body;
2745 TkTextMakeByteIndex(textPtr->tree, lineno, offset, &index);
2746 if (ewPtr->tkwin == (Tk_Window) NULL) {
2747 DumpSegment(interp, "window", NULL, NULL,
2748 command, &index, what);
2749 } else {
2750 Tcl_Obj *obj = LangWidgetObj(interp, ewPtr->tkwin);
2751 DumpSegment(interp, "window", NULL, obj,
2752 command, &index, what);
2753 Tcl_DecrRefCount(obj);
2754 }
2755 }
2756 }
2757 }
2758 }
2759
2760 /*
2761 * DumpSegment
2762 * Either append information about the current segment to the result,
2763 * or make a script callback with that information as arguments.
2764 *
2765 * Results:
2766 * None
2767 *
2768 * Side effects:
2769 * Either evals the callback or appends elements to the result string.
2770 */
2771 static int
2772 DumpSegment(interp, key, value, arg, command, index, what)
2773 Tcl_Interp *interp;
2774 char *key; /* Segment type key */
2775 char *value; /* Segment value */
2776 Tcl_Obj *arg; /* Segment as an Tcl_Obj * */
2777 LangCallback *command; /* Script callback */
2778 TkTextIndex *index; /* index with line/byte position info */
2779 int what; /* Look for TK_DUMP_INDEX bit */
2780 {
2781 char buffer[TCL_INTEGER_SPACE*2];
2782 TkTextPrintIndex(index, buffer);
2783 if (command == NULL) {
2784 Tcl_AppendElement(interp, key);
2785 if (arg || !value) {
2786 Tcl_IncrRefCount(arg);
2787 Tcl_ListObjAppendElement(interp, Tcl_GetObjResult(interp), arg);
2788 } else {
2789 Tcl_AppendElement(interp, value);
2790 }
2791 Tcl_AppendElement(interp, buffer);
2792 return TCL_OK;
2793 } else {
2794 int result;
2795 #if 0
2796 /* Tcl/Tk passes list of three strings */
2797 CONST char *argv[4];
2798 char *list;
2799 argv[0] = key;
2800 argv[1] = value;
2801 argv[2] = buffer;
2802 argv[3] = NULL;
2803 list = Tcl_Merge(3, argv);
2804 result = Tcl_VarEval(interp, command, " ", list, (char *) NULL);
2805 ckfree(list);
2806 #else
2807 if (arg) {
2808 result = LangDoCallback(interp, command, 1, 3, "%s %_ %s", key, arg, buffer);
2809 } else {
2810 result = LangDoCallback(interp, command, 1, 3, "%s %s %s", key, value, buffer);
2811 }
2812 #endif
2813 return result;
2814 }
2815 }
2816
2817 /*
2818 * TextEditUndo --
2819 * undo the last change.
2820 *
2821 * Results:
2822 * None
2823 *
2824 * Side effects:
2825 * None.
2826 */
2827
2828 static int
2829 TextEditUndo(textPtr)
2830 TkText * textPtr; /* Overall information about text widget. */
2831 {
2832 int status;
2833
2834 if (!textPtr->undo) {
2835 return TCL_OK;
2836 }
2837
2838 /* Turn off the undo feature */
2839 textPtr->undo = 0;
2840
2841 /* The dirty counter should count downwards as we are undoing things */
2842 textPtr->isDirtyIncrement = -1;
2843
2844 /* revert one compound action */
2845 status = TkUndoRevert(textPtr->undoStack);
2846
2847 /* Restore the isdirty increment */
2848 textPtr->isDirtyIncrement = 1;
2849
2850 /* Turn back on the undo feature */
2851 textPtr->undo = 1;
2852
2853 return status;
2854 }
2855
2856 /*
2857 * TextEditRedo --
2858 * redo the last undone change.
2859 *
2860 * Results:
2861 * None
2862 *
2863 * Side effects:
2864 * None.
2865 */
2866
2867 static int
2868 TextEditRedo(textPtr)
2869 TkText * textPtr; /* Overall information about text widget. */
2870 {
2871 int status;
2872
2873 if (!textPtr->undo) {
2874 return TCL_OK;
2875 }
2876
2877 /* Turn off the undo feature temporarily */
2878 textPtr->undo = 0;
2879
2880 /* reapply one compound action */
2881 status = TkUndoApply(textPtr->undoStack);
2882
2883 /* Turn back on the undo feature */
2884 textPtr->undo = 1;
2885
2886 return status;
2887 }
2888
2889 /*
2890 * TextEditCmd --
2891 *
2892 * Handle the subcommands to "$text edit ...".
2893 * See documentation for details.
2894 *
2895 * Results:
2896 * None
2897 *
2898 * Side effects:
2899 * None.
2900 */
2901
2902 static int
2903 TextEditCmd(textPtr, interp, argc, argv)
2904 TkText *textPtr; /* Information about text widget. */
2905 Tcl_Interp *interp; /* Current interpreter. */
2906 int argc; /* Number of arguments. */
2907 char **argv; /* Argument strings. */
2908 {
2909 int c, setModified;
2910 size_t length;
2911
2912 if (argc < 3) {
2913 Tcl_AppendResult(interp, "wrong # args: should be \"",
2914 argv[0], " edit option ?arg arg ...?\"", (char *) NULL);
2915 return TCL_ERROR;
2916 }
2917 c = argv[2][0];
2918 length = strlen(argv[2]);
2919 if ((c == 'm') && (strncmp(argv[2], "modified", length) == 0)) {
2920 if (argc == 3) {
2921 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(textPtr->isDirty));
2922 } else if (argc != 4) {
2923 Tcl_AppendResult(interp, "wrong # args: should be \"",
2924 argv[0], " edit modified ?boolean?\"", (char *) NULL);
2925 return TCL_ERROR;
2926 } else {
2927 XEvent event;
2928 if (Tcl_GetBooleanFromObj(interp, objv[3], &setModified) != TCL_OK) {
2929 return TCL_ERROR;
2930 }
2931 /*
2932 * Set or reset the dirty info and trigger a Modified event.
2933 */
2934
2935 if (setModified) {
2936 textPtr->isDirty = 1;
2937 textPtr->modifiedSet = 1;
2938 } else {
2939 textPtr->isDirty = 0;
2940 textPtr->modifiedSet = 0;
2941 }
2942
2943 /*
2944 * Send an event that the text was modified. This is equivalent to
2945 * "event generate $textWidget <<Modified>>"
2946 */
2947
2948 memset((VOID *) &event, 0, sizeof(event));
2949 event.xany.type = VirtualEvent;
2950 event.xany.serial = NextRequest(Tk_Display(textPtr->tkwin));
2951 event.xany.send_event = False;
2952 event.xany.window = Tk_WindowId(textPtr->tkwin);
2953 event.xany.display = Tk_Display(textPtr->tkwin);
2954 ((XVirtualEvent *) &event)->name = Tk_GetUid("Modified");
2955 Tk_HandleEvent(&event);
2956 }
2957 } else if ((c == 'r') && (strncmp(argv[2], "redo", length) == 0)
2958 && (length >= 3)) {
2959 if (argc != 3) {
2960 Tcl_AppendResult(interp, "wrong # args: should be \"",
2961 argv[0], " edit redo\"", (char *) NULL);
2962 return TCL_ERROR;
2963 }
2964 if ( TextEditRedo(textPtr) ) {
2965 Tcl_AppendResult(interp, "nothing to redo", (char *) NULL);
2966 return TCL_ERROR;
2967 }
2968 } else if ((c == 'r') && (strncmp(argv[2], "reset", length) == 0)
2969 && (length >= 3)) {
2970 if (argc != 3) {
2971 Tcl_AppendResult(interp, "wrong # args: should be \"",
2972 argv[0], " edit reset\"", (char *) NULL);
2973 return TCL_ERROR;
2974 }
2975 TkUndoClearStacks(textPtr->undoStack);
2976 } else if ((c == 's') && (strncmp(argv[2], "separator", length) == 0)) {
2977 if (argc != 3) {
2978 Tcl_AppendResult(interp, "wrong # args: should be \"",
2979 argv[0], " edit separator\"", (char *) NULL);
2980 return TCL_ERROR;
2981 }
2982 TkUndoInsertUndoSeparator(textPtr->undoStack);
2983 } else if ((c == 'u') && (strncmp(argv[2], "undo", length) == 0)) {
2984 if (argc != 3) {
2985 Tcl_AppendResult(interp, "wrong # args: should be \"",
2986 argv[0], " edit undo\"", (char *) NULL);
2987 return TCL_ERROR;
2988 }
2989 if ( TextEditUndo(textPtr) ) {
2990 Tcl_AppendResult(interp, "nothing to undo",
2991 (char *) NULL);
2992 return TCL_ERROR;
2993 }
2994 } else {
2995 Tcl_AppendResult(interp, "bad edit option \"", argv[2],
2996 "\": must be modified, redo, reset, separator or undo",
2997 (char *) NULL);
2998 return TCL_ERROR;
2999 }
3000
3001 return TCL_OK;
3002 }
3003
3004 /*
3005 * TextGetText --
3006 * Returns the text from indexPtr1 to indexPtr2, placing that text
3007 * in the Tcl_DString given. That DString should be free or uninitialized.
3008 *
3009 * Results:
3010 * None.
3011 *
3012 * Side effects:
3013 * Memory will be allocated for the DString. Remember to free it.
3014 */
3015
3016 static void
3017 TextGetText(indexPtr1,indexPtr2, dsPtr)
3018 TkTextIndex *indexPtr1;
3019 TkTextIndex *indexPtr2;
3020 Tcl_DString *dsPtr;
3021 {
3022 TkTextIndex tmpIndex;
3023 Tcl_DStringInit(dsPtr);
3024
3025 TkTextMakeByteIndex(indexPtr1->tree, TkBTreeLineIndex(indexPtr1->linePtr),
3026 indexPtr1->byteIndex, &tmpIndex);
3027
3028 if (TkTextIndexCmp(indexPtr1, indexPtr2) < 0) {
3029 while (1) {
3030 int offset, last;
3031 TkTextSegment *segPtr;
3032
3033 segPtr = TkTextIndexToSeg(&tmpIndex, &offset);
3034 last = segPtr->size;
3035 if (tmpIndex.linePtr == indexPtr2->linePtr) {
3036 int last2;
3037
3038 if (indexPtr2->byteIndex == tmpIndex.byteIndex) {
3039 break;
3040 }
3041 last2 = indexPtr2->byteIndex - tmpIndex.byteIndex + offset;
3042 if (last2 < last) {
3043 last = last2;
3044 }
3045 }
3046 if (segPtr->typePtr == &tkTextCharType) {
3047 Tcl_DStringAppend(dsPtr, segPtr->body.chars + offset,
3048 last - offset);
3049 }
3050 TkTextIndexForwBytes(&tmpIndex, last-offset, &tmpIndex);
3051 }
3052 }
3053 }
3054
3055 /*
3056 * updateDirtyFlag --
3057 * increases the dirtyness of the text widget
3058 *
3059 * Results:
3060 * None
3061 *
3062 * Side effects:
3063 * None.
3064 */
3065
3066 static void updateDirtyFlag (textPtr)
3067 TkText *textPtr; /* Information about text widget. */
3068 {
3069 int oldDirtyFlag;
3070
3071 if (textPtr->modifiedSet) {
3072 return;
3073 }
3074 oldDirtyFlag = textPtr->isDirty;
3075 textPtr->isDirty += textPtr->isDirtyIncrement;
3076 if (textPtr->isDirty == 0 || oldDirtyFlag == 0) {
3077 XEvent event;
3078 /*
3079 * Send an event that the text was modified. This is equivalent to
3080 * "event generate $textWidget <<Modified>>"
3081 */
3082
3083 memset((VOID *) &event, 0, sizeof(event));
3084 event.xany.type = VirtualEvent;
3085 event.xany.serial = NextRequest(Tk_Display(textPtr->tkwin));
3086 event.xany.send_event = False;
3087 event.xany.window = Tk_WindowId(textPtr->tkwin);
3088 event.xany.display = Tk_Display(textPtr->tkwin);
3089 ((XVirtualEvent *) &event)->name = Tk_GetUid("Modified");
3090 Tk_HandleEvent(&event);
3091 }
3092 }
3093
3094
3095
3096
3097
3098