Debian Codegraph @

pTk/mTk/generic/tkEntry.c

raw symbols SCIP
1 /*
2 * Entry.c --
3 *
4 * This module implements entry and spinbox widgets for the Tk toolkit.
5 * An entry displays a string and allows the string to be edited.
6 * A spinbox expands on the entry by adding up/down buttons that control
7 * the value of the entry widget.
8 *
9 * Copyright (c) 1990-1994 The Regents of the University of California.
10 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
11 * Copyright (c) 2000 Ajuba Solutions.
12 * Copyright (c) 2002 ActiveState 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: tkEntry.c,v 1.35 2003/02/25 00:46:41 hobbs Exp $
18 */
19
20 #include "tkInt.h"
21 #include "tkVMacro.h"
22 #include "default.h"
23
24 enum EntryType {
25 TK_ENTRY, TK_SPINBOX
26 };
27
28 /*
29 * A data structure of the following type is kept for each Entry
30 * widget managed by this file:
31 */
32
33 typedef struct {
34 Tk_Window tkwin; /* Window that embodies the entry. NULL
35 * means that the window has been destroyed
36 * but the data structures haven't yet been
37 * cleaned up.*/
38 Display *display; /* Display containing widget. Used, among
39 * other things, so that resources can be
40 * freed even after tkwin has gone away. */
41 Tcl_Interp *interp; /* Interpreter associated with entry. */
42 Tcl_Command widgetCmd; /* Token for entry's widget command. */
43 Tk_OptionTable optionTable; /* Table that defines configuration options
44 * available for this widget. */
45 enum EntryType type; /* Specialized type of Entry widget */
46
47 /*
48 * Fields that are set by widget commands other than "configure".
49 */
50
51 CONST char *string; /* Pointer to storage for string;
52 * NULL-terminated; malloc-ed. */
53 int insertPos; /* Character index before which next typed
54 * character will be inserted. */
55
56 /*
57 * Information about what's selected, if any.
58 */
59
60 int selectFirst; /* Character index of first selected
61 * character (-1 means nothing selected. */
62 int selectLast; /* Character index just after last selected
63 * character (-1 means nothing selected. */
64 int selectAnchor; /* Fixed end of selection (i.e. "select to"
65 * operation will use this as one end of the
66 * selection). */
67
68 /*
69 * Information for scanning:
70 */
71
72 int scanMarkX; /* X-position at which scan started (e.g.
73 * button was pressed here). */
74 int scanMarkIndex; /* Character index of character that was at
75 * left of window when scan started. */
76
77 /*
78 * Configuration settings that are updated by Tk_ConfigureWidget.
79 */
80
81 Tk_3DBorder normalBorder; /* Used for drawing border around whole
82 * window, plus used for background. */
83 Tk_3DBorder disabledBorder; /* Used for drawing border around whole
84 * window in disabled state, plus used for
85 * background. */
86 Tk_3DBorder readonlyBorder; /* Used for drawing border around whole
87 * window in readonly state, plus used for
88 * background. */
89 int borderWidth; /* Width of 3-D border around window. */
90 Tk_Cursor cursor; /* Current cursor for window, or None. */
91 int exportSelection; /* Non-zero means tie internal entry selection
92 * to X selection. */
93 Tk_Font tkfont; /* Information about text font, or NULL. */
94 XColor *fgColorPtr; /* Text color in normal mode. */
95 XColor *dfgColorPtr; /* Text color in disabled mode. */
96 XColor *highlightBgColorPtr;/* Color for drawing traversal highlight
97 * area when highlight is off. */
98 XColor *highlightColorPtr; /* Color for drawing traversal highlight. */
99 int highlightWidth; /* Width in pixels of highlight to draw
100 * around widget when it has the focus.
101 * <= 0 means don't draw a highlight. */
102 Tk_3DBorder insertBorder; /* Used to draw vertical bar for insertion
103 * cursor. */
104 int insertBorderWidth; /* Width of 3-D border around insert cursor. */
105 int insertOffTime; /* Number of milliseconds cursor should spend
106 * in "off" state for each blink. */
107 int insertOnTime; /* Number of milliseconds cursor should spend
108 * in "on" state for each blink. */
109 int insertWidth; /* Total width of insert cursor. */
110 Tk_Justify justify; /* Justification to use for text within
111 * window. */
112 int relief; /* 3-D effect: TK_RELIEF_RAISED, etc. */
113 Tk_3DBorder selBorder; /* Border and background for selected
114 * characters. */
115 int selBorderWidth; /* Width of border around selection. */
116 XColor *selFgColorPtr; /* Foreground color for selected text. */
117 int state; /* Normal or disabled. Entry is read-only
118 * when disabled. */
119 Tcl_Obj *textVarName; /* Name of variable (malloc'ed) or NULL.
120 * If non-NULL, entry's string tracks the
121 * contents of this variable and vice versa. */
122 char *takeFocus; /* Value of -takefocus option; not used in
123 * the C code, but used by keyboard traversal
124 * scripts. Malloc'ed, but may be NULL. */
125 int prefWidth; /* Desired width of window, measured in
126 * average characters. */
127 Tcl_Obj *scrollCmd; /* Command prefix for communicating with
128 * scrollbar(s). Malloc'ed. NULL means
129 * no command to issue. */
130 char *showChar; /* Value of -show option. If non-NULL, first
131 * character is used for displaying all
132 * characters in entry. Malloc'ed.
133 * This is only used by the Entry widget. */
134
135 /*
136 * Fields whose values are derived from the current values of the
137 * configuration settings above.
138 */
139
140 CONST char *displayString; /* String to use when displaying. This may
141 * be a pointer to string, or a pointer to
142 * malloced memory with the same character
143 * length as string but whose characters
144 * are all equal to showChar. */
145 int numBytes; /* Length of string in bytes. */
146 int numChars; /* Length of string in characters. Both
147 * string and displayString have the same
148 * character length, but may have different
149 * byte lengths due to being made from
150 * different UTF-8 characters. */
151 int numDisplayBytes; /* Length of displayString in bytes. */
152 int inset; /* Number of pixels on the left and right
153 * sides that are taken up by XPAD, borderWidth
154 * (if any), and highlightWidth (if any). */
155 Tk_TextLayout textLayout; /* Cached text layout information. */
156 int layoutX, layoutY; /* Origin for layout. */
157 int leftX; /* X position at which character at leftIndex
158 * is drawn (varies depending on justify). */
159 int leftIndex; /* Character index of left-most character
160 * visible in window. */
161 Tcl_TimerToken insertBlinkHandler;
162 /* Timer handler used to blink cursor on and
163 * off. */
164 GC textGC; /* For drawing normal text. */
165 GC selTextGC; /* For drawing selected text. */
166 GC highlightGC; /* For drawing traversal highlight. */
167 int avgWidth; /* Width of average character. */
168 int xWidth; /* Extra width to reserve for widget.
169 * Used by spinboxes for button space. */
170 int flags; /* Miscellaneous flags; see below for
171 * definitions. */
172
173 int validate; /* Non-zero means try to validate */
174 Tcl_Obj *validateCmd; /* Command prefix to use when invoking
175 * validate command. NULL means don't
176 * invoke commands. Malloc'ed. */
177 Tcl_Obj *invalidCmd; /* Command called when a validation returns 0
178 * (successfully fails), defaults to {}. */
179
180 } Entry;
181
182 /*
183 * A data structure of the following type is kept for each spinbox
184 * widget managed by this file:
185 */
186
187 typedef struct {
188 Entry entry; /* A pointer to the generic entry structure.
189 * This must be the first element of the
190 * Spinbox. */
191
192 /*
193 * Spinbox specific configuration settings.
194 */
195
196 Tk_3DBorder activeBorder; /* Used for drawing border around active
197 * buttons. */
198 Tk_3DBorder buttonBorder; /* Used for drawing border around buttons. */
199 Tk_Cursor bCursor; /* cursor for buttons, or None. */
200 int bdRelief; /* 3-D effect: TK_RELIEF_RAISED, etc. */
201 int buRelief; /* 3-D effect: TK_RELIEF_RAISED, etc. */
202 Tcl_Obj *command; /* Command to invoke for spin buttons.
203 * NULL means no command to issue. */
204
205 /*
206 * Spinbox specific fields for use with configuration settings above.
207 */
208
209 int wrap; /* whether to wrap around when spinning */
210
211 int selElement; /* currently selected control */
212 int curElement; /* currently mouseover control */
213
214 int repeatDelay; /* repeat delay */
215 int repeatInterval; /* repeat interval */
216
217 double fromValue; /* Value corresponding to left/top of dial */
218 double toValue; /* Value corresponding to right/bottom
219 * of dial */
220 double increment; /* If > 0, all values are rounded to an
221 * even multiple of this value. */
222 char *formatBuf; /* string into which to format value.
223 * Malloc'ed. */
224 char *reqFormat; /* Sprintf conversion specifier used for the
225 * value that the users requests. Malloc'ed. */
226 char *valueFormat; /* Sprintf conversion specifier used for
227 * the value. */
228 char digitFormat[10]; /* Sprintf conversion specifier computed from
229 * digits and other information; used for
230 * the value. */
231
232 char *valueStr; /* Values List. Malloc'ed. */
233 Tcl_Obj *listObj; /* Pointer to the list object being used */
234 int eIndex; /* Holds the current index into elements */
235 int nElements; /* Holds the current count of elements */
236
237 } Spinbox;
238
239 /*
240 * Assigned bits of "flags" fields of Entry structures, and what those
241 * bits mean:
242 *
243 * REDRAW_PENDING: Non-zero means a DoWhenIdle handler has
244 * already been queued to redisplay the entry.
245 * BORDER_NEEDED: Non-zero means 3-D border must be redrawn
246 * around window during redisplay. Normally
247 * only text portion needs to be redrawn.
248 * CURSOR_ON: Non-zero means insert cursor is displayed at
249 * present. 0 means it isn't displayed.
250 * GOT_FOCUS: Non-zero means this window has the input
251 * focus.
252 * UPDATE_SCROLLBAR: Non-zero means scrollbar should be updated
253 * during next redisplay operation.
254 * GOT_SELECTION: Non-zero means we've claimed the selection.
255 * ENTRY_DELETED: This entry has been effectively destroyed.
256 * VALIDATING: Non-zero means we are in a validateCmd
257 * VALIDATE_VAR: Non-zero means we are attempting to validate
258 * the entry's textvariable with validateCmd
259 * VALIDATE_ABORT: Non-zero if validatecommand signals an abort
260 * for current procedure and make no changes
261 * ENTRY_VAR_TRACED: Non-zero if a var trace is set.
262 */
263
264 #define REDRAW_PENDING 1
265 #define BORDER_NEEDED 2
266 #define CURSOR_ON 4
267 #define GOT_FOCUS 8
268 #define UPDATE_SCROLLBAR 0x10
269 #define GOT_SELECTION 0x20
270 #define ENTRY_DELETED 0x40
271 #define VALIDATING 0x80
272 #define VALIDATE_VAR 0x100
273 #define VALIDATE_ABORT 0x200
274 #define ENTRY_VAR_TRACED 0x400
275
276 /*
277 * The following macro defines how many extra pixels to leave on each
278 * side of the text in the entry.
279 */
280
281 #define XPAD 1
282 #define YPAD 1
283
284 /*
285 * A comparison function for double values. For Spinboxes.
286 */
287 #define MIN_DBL_VAL 1E-9
288 #define DOUBLES_EQ(d1, d2) (fabs((d1) - (d2)) < MIN_DBL_VAL)
289
290 /*
291 * The following enum is used to define a type for the -state option
292 * of the Entry widget. These values are used as indices into the
293 * string table below.
294 */
295
296 enum state {
297 STATE_DISABLED, STATE_NORMAL, STATE_READONLY
298 };
299
300 static char *stateStrings[] = {
301 "disabled", "normal", "readonly", (char *) NULL
302 };
303
304 /*
305 * Definitions for -validate option values:
306 */
307
308 static char *validateStrings[] = {
309 "all", "key", "focus", "focusin", "focusout", "none", (char *) NULL
310 };
311 enum validateType {
312 VALIDATE_ALL, VALIDATE_KEY, VALIDATE_FOCUS,
313 VALIDATE_FOCUSIN, VALIDATE_FOCUSOUT, VALIDATE_NONE,
314 /*
315 * These extra enums are for use with EntryValidateChange
316 */
317 VALIDATE_FORCED, VALIDATE_DELETE, VALIDATE_INSERT, VALIDATE_BUTTON
318 };
319 #define DEF_ENTRY_VALIDATE "none"
320 #define DEF_ENTRY_INVALIDCMD ""
321
322 /*
323 * Information used for Entry objv parsing.
324 */
325
326 static Tk_OptionSpec entryOptSpec[] = {
327 {TK_OPTION_BORDER, "-background", "background", "Background",
328 DEF_ENTRY_BG_COLOR, -1, Tk_Offset(Entry, normalBorder),
329 0, (ClientData) DEF_ENTRY_BG_MONO, 0},
330 {TK_OPTION_SYNONYM, "-bd", (char *) NULL, (char *) NULL,
331 (char *) NULL, 0, -1, 0, (ClientData) "-borderwidth", 0},
332 {TK_OPTION_SYNONYM, "-bg", (char *) NULL, (char *) NULL,
333 (char *) NULL, 0, -1, 0, (ClientData) "-background", 0},
334 {TK_OPTION_PIXELS, "-borderwidth", "borderWidth", "BorderWidth",
335 DEF_ENTRY_BORDER_WIDTH, -1, Tk_Offset(Entry, borderWidth),
336 0, 0, 0},
337 {TK_OPTION_CURSOR, "-cursor", "cursor", "Cursor",
338 DEF_ENTRY_CURSOR, -1, Tk_Offset(Entry, cursor),
339 TK_OPTION_NULL_OK, 0, 0},
340 {TK_OPTION_BORDER, "-disabledbackground", "disabledBackground",
341 "DisabledBackground", DEF_ENTRY_DISABLED_BG_COLOR, -1,
342 Tk_Offset(Entry, disabledBorder), TK_OPTION_NULL_OK,
343 (ClientData) DEF_ENTRY_DISABLED_BG_MONO, 0},
344 {TK_OPTION_COLOR, "-disabledforeground", "disabledForeground",
345 "DisabledForeground", DEF_ENTRY_DISABLED_FG, -1,
346 Tk_Offset(Entry, dfgColorPtr), TK_OPTION_NULL_OK, 0, 0},
347 {TK_OPTION_BOOLEAN, "-exportselection", "exportSelection",
348 "ExportSelection", DEF_ENTRY_EXPORT_SELECTION, -1,
349 Tk_Offset(Entry, exportSelection), 0, 0, 0},
350 {TK_OPTION_SYNONYM, "-fg", "foreground", (char *) NULL,
351 (char *) NULL, 0, -1, 0, (ClientData) "-foreground", 0},
352 {TK_OPTION_FONT, "-font", "font", "Font",
353 DEF_ENTRY_FONT, -1, Tk_Offset(Entry, tkfont), 0, 0, 0},
354 {TK_OPTION_COLOR, "-foreground", "foreground", "Foreground",
355 DEF_ENTRY_FG, -1, Tk_Offset(Entry, fgColorPtr), 0,
356 0, 0},
357 {TK_OPTION_COLOR, "-highlightbackground", "highlightBackground",
358 "HighlightBackground", DEF_ENTRY_HIGHLIGHT_BG,
359 -1, Tk_Offset(Entry, highlightBgColorPtr),
360 0, 0, 0},
361 {TK_OPTION_COLOR, "-highlightcolor", "highlightColor", "HighlightColor",
362 DEF_ENTRY_HIGHLIGHT, -1, Tk_Offset(Entry, highlightColorPtr),
363 0, 0, 0},
364 {TK_OPTION_PIXELS, "-highlightthickness", "highlightThickness",
365 "HighlightThickness", DEF_ENTRY_HIGHLIGHT_WIDTH, -1,
366 Tk_Offset(Entry, highlightWidth), 0, 0, 0},
367 {TK_OPTION_BORDER, "-insertbackground", "insertBackground", "Foreground",
368 DEF_ENTRY_INSERT_BG,
369 -1, Tk_Offset(Entry, insertBorder),
370 0, 0, 0},
371 {TK_OPTION_PIXELS, "-insertborderwidth", "insertBorderWidth",
372 "BorderWidth", DEF_ENTRY_INSERT_BD_COLOR, -1,
373 Tk_Offset(Entry, insertBorderWidth), 0,
374 (ClientData) DEF_ENTRY_INSERT_BD_MONO, 0},
375 {TK_OPTION_INT, "-insertofftime", "insertOffTime", "OffTime",
376 DEF_ENTRY_INSERT_OFF_TIME, -1, Tk_Offset(Entry, insertOffTime),
377 0, 0, 0},
378 {TK_OPTION_INT, "-insertontime", "insertOnTime", "OnTime",
379 DEF_ENTRY_INSERT_ON_TIME, -1, Tk_Offset(Entry, insertOnTime),
380 0, 0, 0},
381 {TK_OPTION_PIXELS, "-insertwidth", "insertWidth", "InsertWidth",
382 DEF_ENTRY_INSERT_WIDTH, -1, Tk_Offset(Entry, insertWidth),
383 0, 0, 0},
384 {TK_OPTION_CALLBACK, "-invalidcommand", "invalidCommand", "InvalidCommand",
385 DEF_ENTRY_INVALIDCMD, -1, Tk_Offset(Entry, invalidCmd),
386 TK_OPTION_NULL_OK, 0, 0},
387 {TK_OPTION_SYNONYM, "-invcmd", (char *) NULL, (char *) NULL,
388 (char *) NULL, 0, -1, 0, (ClientData) "-invalidcommand", 0},
389 {TK_OPTION_JUSTIFY, "-justify", "justify", "Justify",
390 DEF_ENTRY_JUSTIFY, -1, Tk_Offset(Entry, justify), 0, 0, 0},
391 {TK_OPTION_BORDER, "-readonlybackground", "readonlyBackground",
392 "ReadonlyBackground", DEF_ENTRY_READONLY_BG_COLOR, -1,
393 Tk_Offset(Entry, readonlyBorder), TK_OPTION_NULL_OK,
394 (ClientData) DEF_ENTRY_READONLY_BG_MONO, 0},
395 {TK_OPTION_RELIEF, "-relief", "relief", "Relief",
396 DEF_ENTRY_RELIEF, -1, Tk_Offset(Entry, relief),
397 0, 0, 0},
398 {TK_OPTION_BORDER, "-selectbackground", "selectBackground", "Foreground",
399 DEF_ENTRY_SELECT_COLOR, -1, Tk_Offset(Entry, selBorder),
400 0, (ClientData) DEF_ENTRY_SELECT_MONO, 0},
401 {TK_OPTION_PIXELS, "-selectborderwidth", "selectBorderWidth",
402 "BorderWidth", DEF_ENTRY_SELECT_BD_COLOR, -1,
403 Tk_Offset(Entry, selBorderWidth),
404 0, (ClientData) DEF_ENTRY_SELECT_BD_MONO, 0},
405 {TK_OPTION_COLOR, "-selectforeground", "selectForeground", "Background",
406 DEF_ENTRY_SELECT_FG_COLOR, -1, Tk_Offset(Entry, selFgColorPtr),
407 0, (ClientData) DEF_ENTRY_SELECT_FG_MONO, 0},
408 {TK_OPTION_STRING, "-show", "show", "Show",
409 DEF_ENTRY_SHOW, -1, Tk_Offset(Entry, showChar),
410 TK_OPTION_NULL_OK, 0, 0},
411 {TK_OPTION_STRING_TABLE, "-state", "state", "State",
412 DEF_ENTRY_STATE, -1, Tk_Offset(Entry, state),
413 0, (ClientData) stateStrings, 0},
414 {TK_OPTION_STRING, "-takefocus", "takeFocus", "TakeFocus",
415 DEF_ENTRY_TAKE_FOCUS, -1, Tk_Offset(Entry, takeFocus),
416 TK_OPTION_NULL_OK, 0, 0},
417 {TK_OPTION_OBJ, "-textvariable", "textVariable", "Variable",
418 DEF_ENTRY_TEXT_VARIABLE, -1, Tk_Offset(Entry, textVarName),
419 TK_OPTION_NULL_OK, 0, 0},
420 {TK_OPTION_STRING_TABLE, "-validate", "validate", "Validate",
421 DEF_ENTRY_VALIDATE, -1, Tk_Offset(Entry, validate),
422 0, (ClientData) validateStrings, 0},
423 {TK_OPTION_CALLBACK, "-validatecommand", "validateCommand", "ValidateCommand",
424 (char *) NULL, -1, Tk_Offset(Entry, validateCmd),
425 TK_OPTION_NULL_OK, 0, 0},
426 {TK_OPTION_SYNONYM, "-vcmd", (char *) NULL, (char *) NULL,
427 (char *) NULL, 0, -1, 0, (ClientData) "-validatecommand", 0},
428 {TK_OPTION_INT, "-width", "width", "Width",
429 DEF_ENTRY_WIDTH, -1, Tk_Offset(Entry, prefWidth), 0, 0, 0},
430 {TK_OPTION_CALLBACK, "-xscrollcommand", "xScrollCommand", "ScrollCommand",
431 DEF_ENTRY_SCROLL_COMMAND, -1, Tk_Offset(Entry, scrollCmd),
432 TK_OPTION_NULL_OK, 0, 0},
433 {TK_OPTION_END, (char *) NULL, (char *) NULL, (char *) NULL,
434 (char *) NULL, 0, -1, 0, 0, 0}
435 };
436
437 /*
438 * Information used for Spinbox objv parsing.
439 */
440
441 #define DEF_SPINBOX_REPEAT_DELAY "400"
442 #define DEF_SPINBOX_REPEAT_INTERVAL "100"
443
444 #define DEF_SPINBOX_CMD ""
445
446 #define DEF_SPINBOX_FROM "0"
447 #define DEF_SPINBOX_TO "0"
448 #define DEF_SPINBOX_INCREMENT "1"
449 #define DEF_SPINBOX_FORMAT ""
450
451 #define DEF_SPINBOX_VALUES ""
452 #define DEF_SPINBOX_WRAP "0"
453
454 static Tk_OptionSpec sbOptSpec[] = {
455 {TK_OPTION_BORDER, "-activebackground", "activeBackground", "Background",
456 DEF_BUTTON_ACTIVE_BG_COLOR, -1, Tk_Offset(Spinbox, activeBorder),
457 0, (ClientData) DEF_BUTTON_ACTIVE_BG_MONO, 0},
458 {TK_OPTION_BORDER, "-background", "background", "Background",
459 DEF_ENTRY_BG_COLOR, -1, Tk_Offset(Entry, normalBorder),
460 0, (ClientData) DEF_ENTRY_BG_MONO, 0},
461 {TK_OPTION_SYNONYM, "-bd", (char *) NULL, (char *) NULL,
462 (char *) NULL, 0, -1, 0, (ClientData) "-borderwidth", 0},
463 {TK_OPTION_SYNONYM, "-bg", (char *) NULL, (char *) NULL,
464 (char *) NULL, 0, -1, 0, (ClientData) "-background", 0},
465 {TK_OPTION_PIXELS, "-borderwidth", "borderWidth", "BorderWidth",
466 DEF_ENTRY_BORDER_WIDTH, -1, Tk_Offset(Entry, borderWidth),
467 0, 0, 0},
468 {TK_OPTION_BORDER, "-buttonbackground", "Button.background", "Background",
469 DEF_BUTTON_BG_COLOR, -1, Tk_Offset(Spinbox, buttonBorder),
470 0, (ClientData) DEF_BUTTON_BG_MONO, 0},
471 {TK_OPTION_CURSOR, "-buttoncursor", "Button.cursor", "Cursor",
472 DEF_BUTTON_CURSOR, -1, Tk_Offset(Spinbox, bCursor),
473 TK_OPTION_NULL_OK, 0, 0},
474 {TK_OPTION_RELIEF, "-buttondownrelief", "Button.relief", "Relief",
475 DEF_BUTTON_RELIEF, -1, Tk_Offset(Spinbox, bdRelief),
476 0, 0, 0},
477 {TK_OPTION_RELIEF, "-buttonuprelief", "Button.relief", "Relief",
478 DEF_BUTTON_RELIEF, -1, Tk_Offset(Spinbox, buRelief),
479 0, 0, 0},
480 {TK_OPTION_CALLBACK, "-command", "command", "Command",
481 DEF_SPINBOX_CMD, -1, Tk_Offset(Spinbox, command),
482 TK_OPTION_NULL_OK, 0, 0},
483 {TK_OPTION_CURSOR, "-cursor", "cursor", "Cursor",
484 DEF_ENTRY_CURSOR, -1, Tk_Offset(Entry, cursor),
485 TK_OPTION_NULL_OK, 0, 0},
486 {TK_OPTION_BORDER, "-disabledbackground", "disabledBackground",
487 "DisabledBackground", DEF_ENTRY_DISABLED_BG_COLOR, -1,
488 Tk_Offset(Entry, disabledBorder), TK_OPTION_NULL_OK,
489 (ClientData) DEF_ENTRY_DISABLED_BG_MONO, 0},
490 {TK_OPTION_COLOR, "-disabledforeground", "disabledForeground",
491 "DisabledForeground", DEF_ENTRY_DISABLED_FG, -1,
492 Tk_Offset(Entry, dfgColorPtr), TK_OPTION_NULL_OK, 0, 0},
493 {TK_OPTION_BOOLEAN, "-exportselection", "exportSelection",
494 "ExportSelection", DEF_ENTRY_EXPORT_SELECTION, -1,
495 Tk_Offset(Entry, exportSelection), 0, 0, 0},
496 {TK_OPTION_SYNONYM, "-fg", "foreground", (char *) NULL,
497 (char *) NULL, 0, -1, 0, (ClientData) "-foreground", 0},
498 {TK_OPTION_FONT, "-font", "font", "Font",
499 DEF_ENTRY_FONT, -1, Tk_Offset(Entry, tkfont), 0, 0, 0},
500 {TK_OPTION_COLOR, "-foreground", "foreground", "Foreground",
501 DEF_ENTRY_FG, -1, Tk_Offset(Entry, fgColorPtr), 0,
502 0, 0},
503 {TK_OPTION_STRING, "-format", "format", "Format",
504 DEF_SPINBOX_FORMAT, -1, Tk_Offset(Spinbox, reqFormat),
505 TK_OPTION_NULL_OK, 0, 0},
506 {TK_OPTION_DOUBLE, "-from", "from", "From",
507 DEF_SPINBOX_FROM, -1, Tk_Offset(Spinbox, fromValue), 0, 0, 0},
508 {TK_OPTION_COLOR, "-highlightbackground", "highlightBackground",
509 "HighlightBackground", DEF_ENTRY_HIGHLIGHT_BG,
510 -1, Tk_Offset(Entry, highlightBgColorPtr),
511 0, 0, 0},
512 {TK_OPTION_COLOR, "-highlightcolor", "highlightColor", "HighlightColor",
513 DEF_ENTRY_HIGHLIGHT, -1, Tk_Offset(Entry, highlightColorPtr),
514 0, 0, 0},
515 {TK_OPTION_PIXELS, "-highlightthickness", "highlightThickness",
516 "HighlightThickness", DEF_ENTRY_HIGHLIGHT_WIDTH, -1,
517 Tk_Offset(Entry, highlightWidth), 0, 0, 0},
518 {TK_OPTION_DOUBLE, "-increment", "increment", "Increment",
519 DEF_SPINBOX_INCREMENT, -1, Tk_Offset(Spinbox, increment), 0, 0, 0},
520 {TK_OPTION_BORDER, "-insertbackground", "insertBackground", "Foreground",
521 DEF_ENTRY_INSERT_BG, -1, Tk_Offset(Entry, insertBorder),
522 0, 0, 0},
523 {TK_OPTION_PIXELS, "-insertborderwidth", "insertBorderWidth",
524 "BorderWidth", DEF_ENTRY_INSERT_BD_COLOR, -1,
525 Tk_Offset(Entry, insertBorderWidth), 0,
526 (ClientData) DEF_ENTRY_INSERT_BD_MONO, 0},
527 {TK_OPTION_INT, "-insertofftime", "insertOffTime", "OffTime",
528 DEF_ENTRY_INSERT_OFF_TIME, -1, Tk_Offset(Entry, insertOffTime),
529 0, 0, 0},
530 {TK_OPTION_INT, "-insertontime", "insertOnTime", "OnTime",
531 DEF_ENTRY_INSERT_ON_TIME, -1, Tk_Offset(Entry, insertOnTime),
532 0, 0, 0},
533 {TK_OPTION_PIXELS, "-insertwidth", "insertWidth", "InsertWidth",
534 DEF_ENTRY_INSERT_WIDTH, -1, Tk_Offset(Entry, insertWidth),
535 0, 0, 0},
536 {TK_OPTION_CALLBACK, "-invalidcommand", "invalidCommand", "InvalidCommand",
537 DEF_ENTRY_INVALIDCMD, -1, Tk_Offset(Entry, invalidCmd),
538 TK_OPTION_NULL_OK, 0, 0},
539 {TK_OPTION_SYNONYM, "-invcmd", (char *) NULL, (char *) NULL,
540 (char *) NULL, 0, -1, 0, (ClientData) "-invalidcommand", 0},
541 {TK_OPTION_JUSTIFY, "-justify", "justify", "Justify",
542 DEF_ENTRY_JUSTIFY, -1, Tk_Offset(Entry, justify), 0, 0, 0},
543 {TK_OPTION_RELIEF, "-relief", "relief", "Relief",
544 DEF_ENTRY_RELIEF, -1, Tk_Offset(Entry, relief),
545 0, 0, 0},
546 {TK_OPTION_BORDER, "-readonlybackground", "readonlyBackground",
547 "ReadonlyBackground", DEF_ENTRY_READONLY_BG_COLOR, -1,
548 Tk_Offset(Entry, readonlyBorder), TK_OPTION_NULL_OK,
549 (ClientData) DEF_ENTRY_READONLY_BG_MONO, 0},
550 {TK_OPTION_INT, "-repeatdelay", "repeatDelay", "RepeatDelay",
551 DEF_SPINBOX_REPEAT_DELAY, -1, Tk_Offset(Spinbox, repeatDelay),
552 0, 0, 0},
553 {TK_OPTION_INT, "-repeatinterval", "repeatInterval", "RepeatInterval",
554 DEF_SPINBOX_REPEAT_INTERVAL, -1, Tk_Offset(Spinbox, repeatInterval),
555 0, 0, 0},
556 {TK_OPTION_BORDER, "-selectbackground", "selectBackground", "Foreground",
557 DEF_ENTRY_SELECT_COLOR, -1, Tk_Offset(Entry, selBorder),
558 0, (ClientData) DEF_ENTRY_SELECT_MONO, 0},
559 {TK_OPTION_PIXELS, "-selectborderwidth", "selectBorderWidth",
560 "BorderWidth", DEF_ENTRY_SELECT_BD_COLOR, -1,
561 Tk_Offset(Entry, selBorderWidth),
562 0, (ClientData) DEF_ENTRY_SELECT_BD_MONO, 0},
563 {TK_OPTION_COLOR, "-selectforeground", "selectForeground", "Background",
564 DEF_ENTRY_SELECT_FG_COLOR, -1, Tk_Offset(Entry, selFgColorPtr),
565 0, (ClientData) DEF_ENTRY_SELECT_FG_MONO, 0},
566 {TK_OPTION_STRING_TABLE, "-state", "state", "State",
567 DEF_ENTRY_STATE, -1, Tk_Offset(Entry, state),
568 0, (ClientData) stateStrings, 0},
569 {TK_OPTION_STRING, "-takefocus", "takeFocus", "TakeFocus",
570 DEF_ENTRY_TAKE_FOCUS, -1, Tk_Offset(Entry, takeFocus),
571 TK_CONFIG_NULL_OK, 0, 0},
572 {TK_OPTION_OBJ, "-textvariable", "textVariable", "Variable",
573 DEF_ENTRY_TEXT_VARIABLE, -1, Tk_Offset(Entry, textVarName),
574 TK_CONFIG_NULL_OK, 0, 0},
575 {TK_OPTION_DOUBLE, "-to", "to", "To",
576 DEF_SPINBOX_TO, -1, Tk_Offset(Spinbox, toValue), 0, 0, 0},
577 {TK_OPTION_STRING_TABLE, "-validate", "validate", "Validate",
578 DEF_ENTRY_VALIDATE, -1, Tk_Offset(Entry, validate),
579 0, (ClientData) validateStrings, 0},
580 {TK_OPTION_CALLBACK, "-validatecommand", "validateCommand", "ValidateCommand",
581 (char *) NULL, -1, Tk_Offset(Entry, validateCmd),
582 TK_CONFIG_NULL_OK, 0, 0},
583 {TK_OPTION_STRING, "-values", "values", "Values",
584 DEF_SPINBOX_VALUES, -1, Tk_Offset(Spinbox, valueStr),
585 TK_OPTION_NULL_OK, 0, 0},
586 {TK_OPTION_SYNONYM, "-vcmd", (char *) NULL, (char *) NULL,
587 (char *) NULL, 0, -1, 0, (ClientData) "-validatecommand", 0},
588 {TK_OPTION_INT, "-width", "width", "Width",
589 DEF_ENTRY_WIDTH, -1, Tk_Offset(Entry, prefWidth), 0, 0, 0},
590 {TK_OPTION_BOOLEAN, "-wrap", "wrap", "Wrap",
591 DEF_SPINBOX_WRAP, -1, Tk_Offset(Spinbox, wrap), 0, 0, 0},
592 {TK_OPTION_CALLBACK, "-xscrollcommand", "xScrollCommand", "ScrollCommand",
593 DEF_ENTRY_SCROLL_COMMAND, -1, Tk_Offset(Entry, scrollCmd),
594 TK_CONFIG_NULL_OK, 0, 0},
595 {TK_OPTION_END, (char *) NULL, (char *) NULL, (char *) NULL,
596 (char *) NULL, 0, -1, 0, 0, 0}
597 };
598
599 /*
600 * The following tables define the entry widget commands (and sub-
601 * commands) and map the indexes into the string tables into
602 * enumerated types used to dispatch the entry widget command.
603 */
604
605 static CONST char *entryCmdNames[] = {
606 "bbox", "cget", "configure", "delete", "get", "icursor", "index",
607 "insert", "scan", "selection", "validate", "xview", (char *) NULL
608 };
609
610 enum entryCmd {
611 COMMAND_BBOX, COMMAND_CGET, COMMAND_CONFIGURE, COMMAND_DELETE,
612 COMMAND_GET, COMMAND_ICURSOR, COMMAND_INDEX, COMMAND_INSERT,
613 COMMAND_SCAN, COMMAND_SELECTION, COMMAND_VALIDATE, COMMAND_XVIEW
614 };
615
616 static CONST char *selCmdNames[] = {
617 "adjust", "clear", "from", "present", "range", "to", (char *) NULL
618 };
619
620 enum selCmd {
621 SELECTION_ADJUST, SELECTION_CLEAR, SELECTION_FROM,
622 SELECTION_PRESENT, SELECTION_RANGE, SELECTION_TO
623 };
624
625 /*
626 * The following tables define the spinbox widget commands (and sub-
627 * commands) and map the indexes into the string tables into
628 * enumerated types used to dispatch the spinbox widget command.
629 */
630
631 static CONST char *sbCmdNames[] = {
632 "bbox", "cget", "configure", "delete", "get", "icursor", "identify",
633 "index", "insert", "invoke", "scan", "selection", "set",
634 "validate", "xview", (char *) NULL
635 };
636
637 enum sbCmd {
638 SB_CMD_BBOX, SB_CMD_CGET, SB_CMD_CONFIGURE, SB_CMD_DELETE,
639 SB_CMD_GET, SB_CMD_ICURSOR, SB_CMD_IDENTIFY, SB_CMD_INDEX,
640 SB_CMD_INSERT, SB_CMD_INVOKE, SB_CMD_SCAN, SB_CMD_SELECTION,
641 SB_CMD_SET, SB_CMD_VALIDATE, SB_CMD_XVIEW
642 };
643
644 static CONST char *sbSelCmdNames[] = {
645 "adjust", "clear", "element", "from", "present", "range", "to",
646 (char *) NULL
647 };
648
649 enum sbselCmd {
650 SB_SEL_ADJUST, SB_SEL_CLEAR, SB_SEL_ELEMENT, SB_SEL_FROM,
651 SB_SEL_PRESENT, SB_SEL_RANGE, SB_SEL_TO
652 };
653
654 /*
655 * Extra for selection of elements
656 */
657
658 static CONST char *selElementNames[] = {
659 "none", "buttondown", "buttonup", (char *) NULL, "entry"
660 };
661 enum selelement {
662 SEL_NONE, SEL_BUTTONDOWN, SEL_BUTTONUP, SEL_NULL, SEL_ENTRY
663 };
664
665 /*
666 * Flags for GetEntryIndex procedure:
667 */
668
669 #define ZERO_OK 1
670 #define LAST_PLUS_ONE_OK 2
671
672 /*
673 * Forward declarations for procedures defined later in this file:
674 */
675
676 static int ConfigureEntry _ANSI_ARGS_((Tcl_Interp *interp,
677 Entry *entryPtr, int objc,
678 Tcl_Obj *CONST objv[], int flags));
679 static void DeleteChars _ANSI_ARGS_((Entry *entryPtr, int index,
680 int count));
681 static void DestroyEntry _ANSI_ARGS_((char *memPtr));
682 static void DisplayEntry _ANSI_ARGS_((ClientData clientData));
683 static void EntryBlinkProc _ANSI_ARGS_((ClientData clientData));
684 static void EntryCmdDeletedProc _ANSI_ARGS_((
685 ClientData clientData));
686 static void EntryComputeGeometry _ANSI_ARGS_((Entry *entryPtr));
687 static void EntryEventProc _ANSI_ARGS_((ClientData clientData,
688 XEvent *eventPtr));
689 static void EntryFocusProc _ANSI_ARGS_ ((Entry *entryPtr,
690 int gotFocus));
691 static int EntryFetchSelection _ANSI_ARGS_((ClientData clientData,
692 int offset, char *buffer, int maxBytes));
693 static void EntryLostSelection _ANSI_ARGS_((
694 ClientData clientData));
695 static void EventuallyRedraw _ANSI_ARGS_((Entry *entryPtr));
696 static void EntryScanTo _ANSI_ARGS_((Entry *entryPtr, int y));
697 static void EntrySetValue _ANSI_ARGS_((Entry *entryPtr,
698 CONST char *value));
699 static void EntrySelectTo _ANSI_ARGS_((
700 Entry *entryPtr, int index));
701 static char * EntryTextVarProc _ANSI_ARGS_((ClientData clientData,
702 Tcl_Interp *interp, Tcl_Obj *name1,
703 CONST char *name2, int flags));
704 static void EntryUpdateScrollbar _ANSI_ARGS_((Entry *entryPtr));
705 static int EntryValidate _ANSI_ARGS_((Entry *entryPtr,
706 LangCallback *cmd,char *string));
707 static int EntryValidateChange _ANSI_ARGS_((Entry *entryPtr,
708 char *change, CONST char *new, int index,
709 int type));
710 static void ExpandPercents _ANSI_ARGS_((Entry *entryPtr,
711 CONST char *before, char *change, CONST char *new,
712 int index, int type, Tcl_DString *dsPtr));
713 static void EntryValueChanged _ANSI_ARGS_((Entry *entryPtr,
714 CONST char *newValue));
715 static void EntryVisibleRange _ANSI_ARGS_((Entry *entryPtr,
716 double *firstPtr, double *lastPtr));
717 static int EntryWidgetObjCmd _ANSI_ARGS_((ClientData clientData,
718 Tcl_Interp *interp, int objc,
719 Tcl_Obj *CONST objv[]));
720 static void EntryWorldChanged _ANSI_ARGS_((
721 ClientData instanceData));
722 static int GetEntryIndex _ANSI_ARGS_((Tcl_Interp *interp,
723 Entry *entryPtr, Tcl_Obj *arg, int *indexPtr));
724 static void InsertChars _ANSI_ARGS_((Entry *entryPtr, int index,
725 char *string));
726
727 /*
728 * These forward declarations are the spinbox specific ones:
729 */
730
731 static int SpinboxWidgetObjCmd _ANSI_ARGS_((ClientData clientData,
732 Tcl_Interp *interp, int objc,
733 Tcl_Obj *CONST objv[]));
734 static int GetSpinboxElement _ANSI_ARGS_((Spinbox *sbPtr,
735 int x, int y));
736 static int SpinboxInvoke _ANSI_ARGS_((Tcl_Interp *interp,
737 Spinbox *sbPtr, int element));
738 static int ComputeFormat _ANSI_ARGS_((Spinbox *sbPtr));
739
740 /*
741 * The structure below defines widget class behavior by means of procedures
742 * that can be invoked from generic window code.
743 */
744
745 static Tk_ClassProcs entryClass = {
746 sizeof(Tk_ClassProcs), /* size */
747 EntryWorldChanged, /* worldChangedProc */
748 };
749
750
751 /*
752 *--------------------------------------------------------------
753 *
754 * Tk_EntryObjCmd --
755 *
756 * This procedure is invoked to process the "entry" Tcl
757 * command. See the user documentation for details on what
758 * it does.
759 *
760 * Results:
761 * A standard Tcl result.
762 *
763 * Side effects:
764 * See the user documentation.
765 *
766 *--------------------------------------------------------------
767 */
768
769 int
770 Tk_EntryObjCmd(clientData, interp, objc, objv)
771 ClientData clientData; /* NULL. */
772 Tcl_Interp *interp; /* Current interpreter. */
773 int objc; /* Number of arguments. */
774 Tcl_Obj *CONST objv[]; /* Argument objects. */
775 {
776 register Entry *entryPtr;
777 Tk_OptionTable optionTable;
778 Tk_Window tkwin;
779 char *tmp;
780
781 if (objc < 2) {
782 Tcl_WrongNumArgs(interp, 1, objv, "pathName ?options?");
783 return TCL_ERROR;
784 }
785
786 tkwin = Tk_CreateWindowFromPath(interp, Tk_MainWindow(interp),
787 Tcl_GetString(objv[1]), (char *) NULL);
788 if (tkwin == NULL) {
789 return TCL_ERROR;
790 }
791
792 /*
793 * Create the option table for this widget class. If it has already
794 * been created, Tk will return the cached value.
795 */
796
797 optionTable = Tk_CreateOptionTable(interp, entryOptSpec);
798
799 /*
800 * Initialize the fields of the structure that won't be initialized
801 * by ConfigureEntry, or that ConfigureEntry requires to be
802 * initialized already (e.g. resource pointers). Only the non-NULL/0
803 * data must be initialized as memset covers the rest.
804 */
805
806 entryPtr = (Entry *) ckalloc(sizeof(Entry));
807 memset((VOID *) entryPtr, 0, sizeof(Entry));
808
809 entryPtr->tkwin = tkwin;
810 entryPtr->display = Tk_Display(tkwin);
811 entryPtr->interp = interp;
812 entryPtr->widgetCmd = Tcl_CreateObjCommand(interp,
813 Tk_PathName(entryPtr->tkwin), EntryWidgetObjCmd,
814 (ClientData) entryPtr, EntryCmdDeletedProc);
815 entryPtr->optionTable = optionTable;
816 entryPtr->type = TK_ENTRY;
817 tmp = (char *) ckalloc(1);
818 tmp[0] = '\0';
819 entryPtr->string = tmp;
820 entryPtr->selectFirst = -1;
821 entryPtr->selectLast = -1;
822
823 entryPtr->cursor = None;
824 entryPtr->exportSelection = 1;
825 entryPtr->justify = TK_JUSTIFY_LEFT;
826 entryPtr->relief = TK_RELIEF_FLAT;
827 entryPtr->state = STATE_NORMAL;
828 entryPtr->displayString = entryPtr->string;
829 entryPtr->inset = XPAD;
830 entryPtr->textGC = None;
831 entryPtr->selTextGC = None;
832 entryPtr->highlightGC = None;
833 entryPtr->avgWidth = 1;
834 entryPtr->validate = VALIDATE_NONE;
835
836 /*
837 * Keep a hold of the associated tkwin until we destroy the listbox,
838 * otherwise Tk might free it while we still need it.
839 */
840
841 Tcl_Preserve((ClientData) entryPtr->tkwin);
842
843 Tk_SetClass(entryPtr->tkwin, "Entry");
844 Tk_SetClassProcs(entryPtr->tkwin, &entryClass, (ClientData) entryPtr);
845 Tk_CreateEventHandler(entryPtr->tkwin,
846 ExposureMask|StructureNotifyMask|FocusChangeMask,
847 EntryEventProc, (ClientData) entryPtr);
848 Tk_CreateSelHandler(entryPtr->tkwin, XA_PRIMARY, XA_STRING,
849 EntryFetchSelection, (ClientData) entryPtr, XA_STRING);
850
851 if ((Tk_InitOptions(interp, (char *) entryPtr, optionTable, tkwin)
852 != TCL_OK) ||
853 (ConfigureEntry(interp, entryPtr, objc-2, objv+2, 0) != TCL_OK)) {
854 Tk_DestroyWindow(entryPtr->tkwin);
855 return TCL_ERROR;
856 }
857
858 Tcl_SetResult(interp, Tk_PathName(entryPtr->tkwin), TCL_STATIC);
859 return TCL_OK;
860 }
861
862 /*
863 *--------------------------------------------------------------
864 *
865 * EntryWidgetObjCmd --
866 *
867 * This procedure is invoked to process the Tcl command
868 * that corresponds to a widget managed by this module.
869 * See the user documentation for details on what it does.
870 *
871 * Results:
872 * A standard Tcl result.
873 *
874 * Side effects:
875 * See the user documentation.
876 *
877 *--------------------------------------------------------------
878 */
879
880 static int
881 EntryWidgetObjCmd(clientData, interp, objc, objv)
882 ClientData clientData; /* Information about entry widget. */
883 Tcl_Interp *interp; /* Current interpreter. */
884 int objc; /* Number of arguments. */
885 Tcl_Obj *CONST objv[]; /* Argument objects. */
886 {
887 Entry *entryPtr = (Entry *) clientData;
888 int cmdIndex, selIndex, result;
889 Tcl_Obj *objPtr;
890
891 if (objc < 2) {
892 Tcl_WrongNumArgs(interp, 1, objv, "option ?arg arg ...?");
893 return TCL_ERROR;
894 }
895
896 /*
897 * Parse the widget command by looking up the second token in
898 * the list of valid command names.
899 */
900
901 result = Tcl_GetIndexFromObj(interp, objv[1], entryCmdNames,
902 "option", 0, &cmdIndex);
903 if (result != TCL_OK) {
904 return result;
905 }
906
907 Tcl_Preserve((ClientData) entryPtr);
908 switch ((enum entryCmd) cmdIndex) {
909 case COMMAND_BBOX: {
910 int index, x, y, width, height;
911 char buf[TCL_INTEGER_SPACE * 4];
912
913 if (objc != 3) {
914 Tcl_WrongNumArgs(interp, 2, objv, "index");
915 goto error;
916 }
917 if (GetEntryIndex(interp, entryPtr, objv[2],
918 &index) != TCL_OK) {
919 goto error;
920 }
921 if ((index == entryPtr->numChars) && (index > 0)) {
922 index--;
923 }
924 Tk_CharBbox(entryPtr->textLayout, index, &x, &y,
925 &width, &height);
926 Tcl_IntResults(interp, 4, 0,
927 x + entryPtr->layoutX,
928 y + entryPtr->layoutY, width, height);
929 break;
930 }
931
932 case COMMAND_CGET: {
933 if (objc != 3) {
934 Tcl_WrongNumArgs(interp, 2, objv, "option");
935 goto error;
936 }
937
938 objPtr = Tk_GetOptionValue(interp, (char *) entryPtr,
939 entryPtr->optionTable, objv[2], entryPtr->tkwin);
940 if (objPtr == NULL) {
941 goto error;
942 } else {
943 Tcl_SetObjResult(interp, objPtr);
944 }
945 break;
946 }
947
948 case COMMAND_CONFIGURE: {
949 if (objc <= 3) {
950 objPtr = Tk_GetOptionInfo(interp, (char *) entryPtr,
951 entryPtr->optionTable,
952 (objc == 3) ? objv[2] : (Tcl_Obj *) NULL,
953 entryPtr->tkwin);
954 if (objPtr == NULL) {
955 goto error;
956 } else {
957 Tcl_SetObjResult(interp, objPtr);
958 }
959 } else {
960 result = ConfigureEntry(interp, entryPtr, objc-2, objv+2, 0);
961 }
962 break;
963 }
964
965 case COMMAND_DELETE: {
966 int first, last;
967
968 if ((objc < 3) || (objc > 4)) {
969 Tcl_WrongNumArgs(interp, 2, objv, "firstIndex ?lastIndex?");
970 goto error;
971 }
972 if (GetEntryIndex(interp, entryPtr, objv[2],
973 &first) != TCL_OK) {
974 goto error;
975 }
976 if (objc == 3) {
977 last = first + 1;
978 } else {
979 if (GetEntryIndex(interp, entryPtr, objv[3],
980 &last) != TCL_OK) {
981 goto error;
982 }
983 }
984 if ((last >= first) && (entryPtr->state == STATE_NORMAL)) {
985 DeleteChars(entryPtr, first, last - first);
986 }
987 break;
988 }
989
990 case COMMAND_GET: {
991 if (objc != 2) {
992 Tcl_WrongNumArgs(interp, 2, objv, (char *) NULL);
993 goto error;
994 }
995 Tcl_SetStringObj(Tcl_GetObjResult(interp), entryPtr->string, -1);
996 break;
997 }
998
999 case COMMAND_ICURSOR: {
1000 if (objc != 3) {
1001 Tcl_WrongNumArgs(interp, 2, objv, "pos");
1002 goto error;
1003 }
1004 if (GetEntryIndex(interp, entryPtr, objv[2],
1005 &entryPtr->insertPos) != TCL_OK) {
1006 goto error;
1007 }
1008 EventuallyRedraw(entryPtr);
1009 break;
1010 }
1011
1012 case COMMAND_INDEX: {
1013 int index;
1014
1015 if (objc != 3) {
1016 Tcl_WrongNumArgs(interp, 2, objv, "string");
1017 goto error;
1018 }
1019 if (GetEntryIndex(interp, entryPtr, objv[2],
1020 &index) != TCL_OK) {
1021 goto error;
1022 }
1023 Tcl_SetObjResult(interp, Tcl_NewIntObj(index));
1024 break;
1025 }
1026
1027 case COMMAND_INSERT: {
1028 int index;
1029
1030 if (objc != 4) {
1031 Tcl_WrongNumArgs(interp, 2, objv, "index text");
1032 goto error;
1033 }
1034 if (GetEntryIndex(interp, entryPtr, objv[2],
1035 &index) != TCL_OK) {
1036 goto error;
1037 }
1038 if (entryPtr->state == STATE_NORMAL) {
1039 InsertChars(entryPtr, index, Tcl_GetString(objv[3]));
1040 }
1041 break;
1042 }
1043
1044 case COMMAND_SCAN: {
1045 int x;
1046 char *minorCmd;
1047
1048 if (objc != 4) {
1049 Tcl_WrongNumArgs(interp, 2, objv, "mark|dragto x");
1050 goto error;
1051 }
1052 if (Tcl_GetIntFromObj(interp, objv[3], &x) != TCL_OK) {
1053 goto error;
1054 }
1055
1056 minorCmd = Tcl_GetString(objv[2]);
1057 if (minorCmd[0] == 'm'
1058 && (strncmp(minorCmd, "mark", strlen(minorCmd)) == 0)) {
1059 entryPtr->scanMarkX = x;
1060 entryPtr->scanMarkIndex = entryPtr->leftIndex;
1061 } else if ((minorCmd[0] == 'd')
1062 && (strncmp(minorCmd, "dragto", strlen(minorCmd)) == 0)) {
1063 EntryScanTo(entryPtr, x);
1064 } else {
1065 Tcl_AppendResult(interp, "bad scan option \"",
1066 Tcl_GetString(objv[2]), "\": must be mark or dragto",
1067 (char *) NULL);
1068 goto error;
1069 }
1070 break;
1071 }
1072
1073 case COMMAND_SELECTION: {
1074 int index, index2;
1075
1076 if (objc < 3) {
1077 Tcl_WrongNumArgs(interp, 2, objv, "option ?index?");
1078 goto error;
1079 }
1080
1081 /*
1082 * Parse the selection sub-command, using the command
1083 * table "selCmdNames" defined above.
1084 */
1085
1086 result = Tcl_GetIndexFromObj(interp, objv[2], selCmdNames,
1087 "selection option", 0, &selIndex);
1088 if (result != TCL_OK) {
1089 goto error;
1090 }
1091
1092 /*
1093 * Disabled entries don't allow the selection to be modified,
1094 * but 'selection present' must return a boolean.
1095 */
1096
1097 if ((entryPtr->state == STATE_DISABLED)
1098 && (selIndex != SELECTION_PRESENT)) {
1099 goto done;
1100 }
1101
1102 switch (selIndex) {
1103 case SELECTION_ADJUST: {
1104 if (objc != 4) {
1105 Tcl_WrongNumArgs(interp, 3, objv, "index");
1106 goto error;
1107 }
1108 if (GetEntryIndex(interp, entryPtr,
1109 objv[3], &index) != TCL_OK) {
1110 goto error;
1111 }
1112 if (entryPtr->selectFirst >= 0) {
1113 int half1, half2;
1114
1115 half1 = (entryPtr->selectFirst
1116 + entryPtr->selectLast)/2;
1117 half2 = (entryPtr->selectFirst
1118 + entryPtr->selectLast + 1)/2;
1119 if (index < half1) {
1120 entryPtr->selectAnchor = entryPtr->selectLast;
1121 } else if (index > half2) {
1122 entryPtr->selectAnchor = entryPtr->selectFirst;
1123 } else {
1124 /*
1125 * We're at about the halfway point in the
1126 * selection; just keep the existing anchor.
1127 */
1128 }
1129 }
1130 EntrySelectTo(entryPtr, index);
1131 break;
1132 }
1133
1134 case SELECTION_CLEAR: {
1135 if (objc != 3) {
1136 Tcl_WrongNumArgs(interp, 3, objv, (char *) NULL);
1137 goto error;
1138 }
1139 if (entryPtr->selectFirst >= 0) {
1140 entryPtr->selectFirst = -1;
1141 entryPtr->selectLast = -1;
1142 EventuallyRedraw(entryPtr);
1143 }
1144 goto done;
1145 }
1146
1147 case SELECTION_FROM: {
1148 if (objc != 4) {
1149 Tcl_WrongNumArgs(interp, 3, objv, "index");
1150 goto error;
1151 }
1152 if (GetEntryIndex(interp, entryPtr,
1153 objv[3], &index) != TCL_OK) {
1154 goto error;
1155 }
1156 entryPtr->selectAnchor = index;
1157 break;
1158 }
1159
1160 case SELECTION_PRESENT: {
1161 if (objc != 3) {
1162 Tcl_WrongNumArgs(interp, 3, objv, (char *) NULL);
1163 goto error;
1164 }
1165 Tcl_SetObjResult(interp,
1166 Tcl_NewBooleanObj((entryPtr->selectFirst >= 0)));
1167 goto done;
1168 }
1169
1170 case SELECTION_RANGE: {
1171 if (objc != 5) {
1172 Tcl_WrongNumArgs(interp, 3, objv, "start end");
1173 goto error;
1174 }
1175 if (GetEntryIndex(interp, entryPtr,
1176 objv[3], &index) != TCL_OK) {
1177 goto error;
1178 }
1179 if (GetEntryIndex(interp, entryPtr,
1180 objv[4],& index2) != TCL_OK) {
1181 goto error;
1182 }
1183 if (index >= index2) {
1184 entryPtr->selectFirst = -1;
1185 entryPtr->selectLast = -1;
1186 } else {
1187 entryPtr->selectFirst = index;
1188 entryPtr->selectLast = index2;
1189 }
1190 if (!(entryPtr->flags & GOT_SELECTION)
1191 && (entryPtr->exportSelection)) {
1192 Tk_OwnSelection(entryPtr->tkwin, XA_PRIMARY,
1193 EntryLostSelection, (ClientData) entryPtr);
1194 entryPtr->flags |= GOT_SELECTION;
1195 }
1196 EventuallyRedraw(entryPtr);
1197 break;
1198 }
1199
1200 case SELECTION_TO: {
1201 if (objc != 4) {
1202 Tcl_WrongNumArgs(interp, 3, objv, "index");
1203 goto error;
1204 }
1205 if (GetEntryIndex(interp, entryPtr,
1206 objv[3], &index) != TCL_OK) {
1207 goto error;
1208 }
1209 EntrySelectTo(entryPtr, index);
1210 break;
1211 }
1212 }
1213 break;
1214 }
1215
1216 case COMMAND_VALIDATE: {
1217 int code;
1218
1219 if (objc != 2) {
1220 Tcl_WrongNumArgs(interp, 2, objv, (char *) NULL);
1221 goto error;
1222 }
1223 selIndex = entryPtr->validate;
1224 entryPtr->validate = VALIDATE_ALL;
1225 code = EntryValidateChange(entryPtr, (char *) NULL,
1226 entryPtr->string, -1, VALIDATE_FORCED);
1227 if (entryPtr->validate != VALIDATE_NONE) {
1228 entryPtr->validate = selIndex;
1229 }
1230 Tcl_SetObjResult(interp, Tcl_NewBooleanObj((code == TCL_OK)));
1231 break;
1232 }
1233
1234 case COMMAND_XVIEW: {
1235 int index;
1236
1237 if (objc == 2) {
1238 double first, last;
1239 char buf[TCL_DOUBLE_SPACE * 2];
1240
1241 EntryVisibleRange(entryPtr, &first, &last);
1242 Tcl_DoubleResults(interp,2,0,first, last);
1243 goto done;
1244 } else if (objc == 3) {
1245 if (GetEntryIndex(interp, entryPtr, objv[2],
1246 &index) != TCL_OK) {
1247 goto error;
1248 }
1249 } else {
1250 double fraction;
1251 int count;
1252
1253 index = entryPtr->leftIndex;
1254 switch (Tk_GetScrollInfoObj(interp, objc, objv, &fraction,
1255 &count)) {
1256 case TK_SCROLL_ERROR: {
1257 goto error;
1258 }
1259 case TK_SCROLL_MOVETO: {
1260 index = (int) ((fraction * entryPtr->numChars) + 0.5);
1261 break;
1262 }
1263 case TK_SCROLL_PAGES: {
1264 int charsPerPage;
1265
1266 charsPerPage = ((Tk_Width(entryPtr->tkwin)
1267 - 2 * entryPtr->inset)
1268 / entryPtr->avgWidth) - 2;
1269 if (charsPerPage < 1) {
1270 charsPerPage = 1;
1271 }
1272 index += count * charsPerPage;
1273 break;
1274 }
1275 case TK_SCROLL_UNITS: {
1276 index += count;
1277 break;
1278 }
1279 }
1280 }
1281 if (index >= entryPtr->numChars) {
1282 index = entryPtr->numChars - 1;
1283 }
1284 if (index < 0) {
1285 index = 0;
1286 }
1287 entryPtr->leftIndex = index;
1288 entryPtr->flags |= UPDATE_SCROLLBAR;
1289 EntryComputeGeometry(entryPtr);
1290 EventuallyRedraw(entryPtr);
1291 break;
1292 }
1293 }
1294
1295 done:
1296 Tcl_Release((ClientData) entryPtr);
1297 return result;
1298
1299 error:
1300 Tcl_Release((ClientData) entryPtr);
1301 return TCL_ERROR;
1302 }
1303
1304 /*
1305 *----------------------------------------------------------------------
1306 *
1307 * DestroyEntry --
1308 *
1309 * This procedure is invoked by Tcl_EventuallyFree or Tcl_Release
1310 * to clean up the internal structure of an entry at a safe time
1311 * (when no-one is using it anymore).
1312 *
1313 * Results:
1314 * None.
1315 *
1316 * Side effects:
1317 * Everything associated with the entry is freed up.
1318 *
1319 *----------------------------------------------------------------------
1320 */
1321
1322 static void
1323 DestroyEntry(memPtr)
1324 char *memPtr; /* Info about entry widget. */
1325 {
1326 Entry *entryPtr = (Entry *) memPtr;
1327
1328 /*
1329 * Free up all the stuff that requires special handling, then
1330 * let Tk_FreeOptions handle all the standard option-related
1331 * stuff.
1332 */
1333
1334 ckfree((char *)entryPtr->string);
1335 if (entryPtr->textVarName != NULL) {
1336 Lang_UntraceVar(entryPtr->interp, entryPtr->textVarName,
1337 TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
1338 EntryTextVarProc, (ClientData) entryPtr);
1339 entryPtr->flags &= ~ENTRY_VAR_TRACED;
1340 }
1341 if (entryPtr->textGC != None) {
1342 Tk_FreeGC(entryPtr->display, entryPtr->textGC);
1343 }
1344 if (entryPtr->selTextGC != None) {
1345 Tk_FreeGC(entryPtr->display, entryPtr->selTextGC);
1346 }
1347 Tcl_DeleteTimerHandler(entryPtr->insertBlinkHandler);
1348 if (entryPtr->displayString != entryPtr->string) {
1349 ckfree((char *)entryPtr->displayString);
1350 }
1351 if (entryPtr->type == TK_SPINBOX) {
1352 Spinbox *sbPtr = (Spinbox *) entryPtr;
1353
1354 if (sbPtr->listObj != NULL) {
1355 Tcl_DecrRefCount(sbPtr->listObj);
1356 sbPtr->listObj = NULL;
1357 }
1358 if (sbPtr->formatBuf) {
1359 ckfree(sbPtr->formatBuf);
1360 }
1361 }
1362 Tk_FreeTextLayout(entryPtr->textLayout);
1363 Tk_FreeConfigOptions((char *) entryPtr, entryPtr->optionTable,
1364 entryPtr->tkwin);
1365 Tcl_Release((ClientData) entryPtr->tkwin);
1366 entryPtr->tkwin = NULL;
1367
1368 ckfree((char *) entryPtr);
1369 }
1370
1371 /*
1372 *----------------------------------------------------------------------
1373 *
1374 * ConfigureEntry --
1375 *
1376 * This procedure is called to process an argv/argc list, plus
1377 * the Tk option database, in order to configure (or reconfigure)
1378 * an entry widget.
1379 *
1380 * Results:
1381 * The return value is a standard Tcl result. If TCL_ERROR is
1382 * returned, then the interp's result contains an error message.
1383 *
1384 * Side effects:
1385 * Configuration information, such as colors, border width,
1386 * etc. get set for entryPtr; old resources get freed,
1387 * if there were any.
1388 *
1389 *----------------------------------------------------------------------
1390 */
1391
1392 static int
1393 ConfigureEntry(interp, entryPtr, objc, objv, flags)
1394 Tcl_Interp *interp; /* Used for error reporting. */
1395 Entry *entryPtr; /* Information about widget; may or may not
1396 * already have values for some fields. */
1397 int objc; /* Number of valid entries in argv. */
1398 Tcl_Obj *CONST objv[]; /* Argument objects. */
1399 int flags; /* Flags to pass to Tk_ConfigureWidget. */
1400 {
1401 Tk_SavedOptions savedOptions;
1402 Tk_3DBorder border;
1403 Tcl_Obj *errorResult = NULL;
1404 Spinbox *sbPtr = (Spinbox *) entryPtr; /* Only used when this widget
1405 * is of type TK_SPINBOX */
1406 char *oldValues = NULL; /* lint initialization */
1407 char *oldFormat = NULL; /* lint initialization */
1408 int error;
1409 int oldExport = 0; /* lint initialization */
1410 int valuesChanged = 0; /* lint initialization */
1411 double oldFrom = 0.0; /* lint initialization */
1412 double oldTo = 0.0; /* lint initialization */
1413
1414 /*
1415 * Eliminate any existing trace on a variable monitored by the entry.
1416 */
1417
1418 if ((entryPtr->textVarName != NULL)
1419 && (entryPtr->flags & ENTRY_VAR_TRACED)) {
1420 Lang_UntraceVar(interp, entryPtr->textVarName,
1421 TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
1422 EntryTextVarProc, (ClientData) entryPtr);
1423 entryPtr->flags &= ~ENTRY_VAR_TRACED;
1424 }
1425
1426 /*
1427 * Store old values that we need to effect certain behavior if
1428 * they change value
1429 */
1430 oldExport = entryPtr->exportSelection;
1431 if (entryPtr->type == TK_SPINBOX) {
1432 oldValues = sbPtr->valueStr;
1433 oldFormat = sbPtr->reqFormat;
1434 oldFrom = sbPtr->fromValue;
1435 oldTo = sbPtr->toValue;
1436 }
1437
1438 for (error = 0; error <= 1; error++) {
1439 if (!error) {
1440 /*
1441 * First pass: set options to new values.
1442 */
1443
1444 if (Tk_SetOptions(interp, (char *) entryPtr,
1445 entryPtr->optionTable, objc, objv,
1446 entryPtr->tkwin, &savedOptions, (int *) NULL) != TCL_OK) {
1447 continue;
1448 }
1449 } else {
1450 /*
1451 * Second pass: restore options to old values.
1452 */
1453
1454 errorResult = Tcl_GetObjResult(interp);
1455 Tcl_IncrRefCount(errorResult);
1456 Tk_RestoreSavedOptions(&savedOptions);
1457 }
1458
1459 /*
1460 * A few other options also need special processing, such as parsing
1461 * the geometry and setting the background from a 3-D border.
1462 */
1463
1464 if ((entryPtr->state == STATE_DISABLED) &&
1465 (entryPtr->disabledBorder != NULL)) {
1466 border = entryPtr->disabledBorder;
1467 } else if ((entryPtr->state == STATE_READONLY) &&
1468 (entryPtr->readonlyBorder != NULL)) {
1469 border = entryPtr->readonlyBorder;
1470 } else {
1471 border = entryPtr->normalBorder;
1472 }
1473 Tk_SetBackgroundFromBorder(entryPtr->tkwin, border);
1474
1475 if (entryPtr->insertWidth <= 0) {
1476 entryPtr->insertWidth = 2;
1477 }
1478 if (entryPtr->insertBorderWidth > entryPtr->insertWidth/2) {
1479 entryPtr->insertBorderWidth = entryPtr->insertWidth/2;
1480 }
1481
1482 if (entryPtr->type == TK_SPINBOX) {
1483 if (sbPtr->fromValue > sbPtr->toValue) {
1484 Tcl_SetResult(interp,
1485 "-to value must be greater than -from value",
1486 TCL_VOLATILE);
1487 continue;
1488 }
1489
1490 if (sbPtr->reqFormat && (oldFormat != sbPtr->reqFormat)) {
1491 /*
1492 * Make sure that the given format is somewhat correct, and
1493 * calculate the minimum space we'll need for the values as
1494 * strings.
1495 */
1496 int min, max;
1497 size_t formatLen, formatSpace = TCL_DOUBLE_SPACE;
1498 char fbuf[4], *fmt = sbPtr->reqFormat;
1499
1500 formatLen = strlen(fmt);
1501 if ((fmt[0] != '%') || (fmt[formatLen-1] != 'f')) {
1502 badFormatOpt:
1503 Tcl_AppendResult(interp, "bad spinbox format specifier \"",
1504 sbPtr->reqFormat, "\"", (char *) NULL);
1505 continue;
1506 }
1507 if ((sscanf(fmt, "%%%d.%d%[f]", &min, &max, fbuf) == 3)
1508 && (max >= 0)) {
1509 formatSpace = min + max + 1;
1510 } else if (((sscanf(fmt, "%%.%d%[f]", &min, fbuf) == 2)
1511 || (sscanf(fmt, "%%%d%[f]", &min, fbuf) == 2)
1512 || (sscanf(fmt, "%%%d.%[f]", &min, fbuf) == 2))
1513 && (min >= 0)) {
1514 formatSpace = min + 1;
1515 } else {
1516 goto badFormatOpt;
1517 }
1518 if (formatSpace < TCL_DOUBLE_SPACE) {
1519 formatSpace = TCL_DOUBLE_SPACE;
1520 }
1521 sbPtr->formatBuf = ckrealloc(sbPtr->formatBuf, formatSpace);
1522 /*
1523 * We perturb the value of oldFrom to allow us to go into
1524 * the branch below that will reformat the displayed value.
1525 */
1526 oldFrom = sbPtr->fromValue - 1;
1527 }
1528
1529 /*
1530 * See if we have to rearrange our listObj data
1531 */
1532 if (oldValues != sbPtr->valueStr) {
1533 if (sbPtr->listObj != NULL) {
1534 Tcl_DecrRefCount(sbPtr->listObj);
1535 }
1536 sbPtr->listObj = NULL;
1537 if (sbPtr->valueStr != NULL) {
1538 Tcl_Obj *newObjPtr;
1539 int nelems;
1540
1541 newObjPtr = Tcl_NewStringObj(sbPtr->valueStr, -1);
1542 if (Tcl_ListObjLength(interp, newObjPtr, &nelems)
1543 != TCL_OK) {
1544 valuesChanged = -1;
1545 continue;
1546 }
1547 sbPtr->listObj = newObjPtr;
1548 Tcl_IncrRefCount(sbPtr->listObj);
1549 sbPtr->nElements = nelems;
1550 sbPtr->eIndex = 0;
1551 valuesChanged++;
1552 }
1553 }
1554 }
1555
1556 /*
1557 * Restart the cursor timing sequence in case the on-time or
1558 * off-time just changed. Set validate temporarily to none,
1559 * so the configure doesn't cause it to be triggered.
1560 */
1561
1562 if (entryPtr->flags & GOT_FOCUS) {
1563 int validate = entryPtr->validate;
1564 entryPtr->validate = VALIDATE_NONE;
1565 EntryFocusProc(entryPtr, 1);
1566 entryPtr->validate = validate;
1567 }
1568
1569 /*
1570 * Claim the selection if we've suddenly started exporting it.
1571 */
1572
1573 if (entryPtr->exportSelection && (!oldExport)
1574 && (entryPtr->selectFirst != -1)
1575 && !(entryPtr->flags & GOT_SELECTION)) {
1576 Tk_OwnSelection(entryPtr->tkwin, XA_PRIMARY, EntryLostSelection,
1577 (ClientData) entryPtr);
1578 entryPtr->flags |= GOT_SELECTION;
1579 }
1580
1581 /*
1582 * Recompute the window's geometry and arrange for it to be
1583 * redisplayed.
1584 */
1585
1586 Tk_SetInternalBorder(entryPtr->tkwin,
1587 entryPtr->borderWidth + entryPtr->highlightWidth);
1588 if (entryPtr->highlightWidth <= 0) {
1589 entryPtr->highlightWidth = 0;
1590 }
1591 entryPtr->inset = entryPtr->highlightWidth
1592 + entryPtr->borderWidth + XPAD;
1593 break;
1594 }
1595 if (!error) {
1596 Tk_FreeSavedOptions(&savedOptions);
1597 }
1598
1599 /*
1600 * If the entry is tied to the value of a variable, create the variable if
1601 * it doesn't exist, and set the entry's value from the variable's value.
1602 */
1603
1604 if (entryPtr->textVarName != NULL) {
1605 Tcl_Obj *value;
1606
1607 value = Tcl_ObjGetVar2(interp, entryPtr->textVarName, NULL, TCL_GLOBAL_ONLY);
1608 if (value == NULL) {
1609 EntryValueChanged(entryPtr, NULL);
1610 } else {
1611 EntrySetValue(entryPtr, Tcl_GetString(value));
1612 }
1613 }
1614
1615 if (entryPtr->type == TK_SPINBOX) {
1616 ComputeFormat(sbPtr);
1617
1618 if (valuesChanged > 0) {
1619 Tcl_Obj *objPtr;
1620
1621 /*
1622 * No check for error return, because there shouldn't be one
1623 * given the check for valid list above
1624 */
1625 Tcl_ListObjIndex(interp, sbPtr->listObj, 0, &objPtr);
1626 EntryValueChanged(entryPtr, Tcl_GetString(objPtr));
1627 } else if ((sbPtr->valueStr == NULL)
1628 && !DOUBLES_EQ(sbPtr->fromValue, sbPtr->toValue)
1629 && (!DOUBLES_EQ(sbPtr->fromValue, oldFrom)
1630 || !DOUBLES_EQ(sbPtr->toValue, oldTo))) {
1631 /*
1632 * If the valueStr is empty and -from && -to are specified, check
1633 * to see if the current string is within the range. If not,
1634 * it will be constrained to the nearest edge. If the current
1635 * string isn't a double value, we set it to -from.
1636 */
1637 int code;
1638 double dvalue;
1639
1640 code = Tcl_GetDouble(NULL, entryPtr->string, &dvalue);
1641 if (code != TCL_OK) {
1642 dvalue = sbPtr->fromValue;
1643 } else {
1644 if (dvalue > sbPtr->toValue) {
1645 dvalue = sbPtr->toValue;
1646 } else if (dvalue < sbPtr->fromValue) {
1647 dvalue = sbPtr->fromValue;
1648 }
1649 }
1650 sprintf(sbPtr->formatBuf, sbPtr->valueFormat, dvalue);
1651 EntryValueChanged(entryPtr, sbPtr->formatBuf);
1652 }
1653 }
1654
1655 /*
1656 * Set up a trace on the variable's value after we've possibly
1657 * constrained the value according to new -from/-to values.
1658 */
1659
1660 if ((entryPtr->textVarName != NULL)
1661 && !(entryPtr->flags & ENTRY_VAR_TRACED)) {
1662 Lang_TraceVar(interp, entryPtr->textVarName,
1663 TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
1664 EntryTextVarProc, (ClientData) entryPtr);
1665 entryPtr->flags |= ENTRY_VAR_TRACED;
1666 }
1667
1668 EntryWorldChanged((ClientData) entryPtr);
1669 if (error) {
1670 Tcl_SetObjResult(interp, errorResult);
1671 Tcl_DecrRefCount(errorResult);
1672 return TCL_ERROR;
1673 } else {
1674 return TCL_OK;
1675 }
1676 }
1677
1678 /*
1679 *---------------------------------------------------------------------------
1680 *
1681 * EntryWorldChanged --
1682 *
1683 * This procedure is called when the world has changed in some
1684 * way and the widget needs to recompute all its graphics contexts
1685 * and determine its new geometry.
1686 *
1687 * Results:
1688 * None.
1689 *
1690 * Side effects:
1691 * Entry will be relayed out and redisplayed.
1692 *
1693 *---------------------------------------------------------------------------
1694 */
1695
1696 static void
1697 EntryWorldChanged(instanceData)
1698 ClientData instanceData; /* Information about widget. */
1699 {
1700 XGCValues gcValues;
1701 GC gc = None;
1702 unsigned long mask;
1703 Tk_3DBorder border;
1704 XColor *colorPtr;
1705 Entry *entryPtr = (Entry *) instanceData;
1706
1707 entryPtr->avgWidth = Tk_TextWidth(entryPtr->tkfont, "0", 1);
1708 if (entryPtr->avgWidth == 0) {
1709 entryPtr->avgWidth = 1;
1710 }
1711
1712 if (entryPtr->type == TK_SPINBOX) {
1713 /*
1714 * Compute the button width for a spinbox
1715 */
1716
1717 entryPtr->xWidth = entryPtr->avgWidth + 2 * (1+XPAD);
1718 if (entryPtr->xWidth < 11) {
1719 entryPtr->xWidth = 11; /* we want a min visible size */
1720 }
1721 }
1722
1723 /*
1724 * Default background and foreground are from the normal state.
1725 * In a disabled state, both of those may be overridden; in the readonly
1726 * state, the background may be overridden.
1727 */
1728
1729 border = entryPtr->normalBorder;
1730 colorPtr = entryPtr->fgColorPtr;
1731 switch (entryPtr->state) {
1732 case STATE_DISABLED:
1733 if (entryPtr->disabledBorder != NULL) {
1734 border = entryPtr->disabledBorder;
1735 }
1736 if (entryPtr->dfgColorPtr != NULL) {
1737 colorPtr = entryPtr->dfgColorPtr;
1738 }
1739 break;
1740 case STATE_READONLY:
1741 if (entryPtr->readonlyBorder != NULL) {
1742 border = entryPtr->readonlyBorder;
1743 }
1744 break;
1745 }
1746
1747 Tk_SetBackgroundFromBorder(entryPtr->tkwin, border);
1748 gcValues.foreground = colorPtr->pixel;
1749 gcValues.font = Tk_FontId(entryPtr->tkfont);
1750 gcValues.graphics_exposures = False;
1751 mask = GCForeground | GCFont | GCGraphicsExposures;
1752 gc = Tk_GetGC(entryPtr->tkwin, mask, &gcValues);
1753 if (entryPtr->textGC != None) {
1754 Tk_FreeGC(entryPtr->display, entryPtr->textGC);
1755 }
1756 entryPtr->textGC = gc;
1757
1758 gcValues.foreground = entryPtr->selFgColorPtr->pixel;
1759 gcValues.font = Tk_FontId(entryPtr->tkfont);
1760 mask = GCForeground | GCFont;
1761 gc = Tk_GetGC(entryPtr->tkwin, mask, &gcValues);
1762 if (entryPtr->selTextGC != None) {
1763 Tk_FreeGC(entryPtr->display, entryPtr->selTextGC);
1764 }
1765 entryPtr->selTextGC = gc;
1766
1767 /*
1768 * Recompute the window's geometry and arrange for it to be
1769 * redisplayed.
1770 */
1771
1772 EntryComputeGeometry(entryPtr);
1773 entryPtr->flags |= UPDATE_SCROLLBAR;
1774 EventuallyRedraw(entryPtr);
1775 }
1776
1777 /*
1778 *--------------------------------------------------------------
1779 *
1780 * DisplayEntry --
1781 *
1782 * This procedure redraws the contents of an entry window.
1783 *
1784 * Results:
1785 * None.
1786 *
1787 * Side effects:
1788 * Information appears on the screen.
1789 *
1790 *--------------------------------------------------------------
1791 */
1792
1793 static void
1794 DisplayEntry(clientData)
1795 ClientData clientData; /* Information about window. */
1796 {
1797 Entry *entryPtr = (Entry *) clientData;
1798 Tk_Window tkwin = entryPtr->tkwin;
1799 int baseY, selStartX, selEndX, cursorX;
1800 int showSelection, xBound;
1801 Tk_FontMetrics fm;
1802 Pixmap pixmap;
1803 Tk_3DBorder border;
1804
1805 entryPtr->flags &= ~REDRAW_PENDING;
1806 if ((entryPtr->flags & ENTRY_DELETED) || !Tk_IsMapped(tkwin)) {
1807 return;
1808 }
1809
1810 Tk_GetFontMetrics(entryPtr->tkfont, &fm);
1811
1812 /*
1813 * Update the scrollbar if that's needed.
1814 */
1815
1816 if (entryPtr->flags & UPDATE_SCROLLBAR) {
1817 entryPtr->flags &= ~UPDATE_SCROLLBAR;
1818
1819 /*
1820 * Preserve/Release because updating the scrollbar can have
1821 * the side-effect of destroying or unmapping the entry widget.
1822 */
1823
1824 Tcl_Preserve((ClientData) entryPtr);
1825 EntryUpdateScrollbar(entryPtr);
1826
1827 if ((entryPtr->flags & ENTRY_DELETED) || !Tk_IsMapped(tkwin)) {
1828 Tcl_Release((ClientData) entryPtr);
1829 return;
1830 }
1831 Tcl_Release((ClientData) entryPtr);
1832 }
1833
1834 /*
1835 * In order to avoid screen flashes, this procedure redraws the
1836 * textual area of the entry into off-screen memory, then copies
1837 * it back on-screen in a single operation. This means there's
1838 * no point in time where the on-screen image has been cleared.
1839 */
1840
1841 pixmap = Tk_GetPixmap(entryPtr->display, Tk_WindowId(tkwin),
1842 Tk_Width(tkwin), Tk_Height(tkwin), Tk_Depth(tkwin));
1843
1844 /*
1845 * Compute x-coordinate of the pixel just after last visible
1846 * one, plus vertical position of baseline of text.
1847 */
1848
1849 xBound = Tk_Width(tkwin) - entryPtr->inset - entryPtr->xWidth;
1850 baseY = (Tk_Height(tkwin) + fm.ascent - fm.descent) / 2;
1851
1852 /*
1853 * On Windows and Mac, we need to hide the selection whenever we
1854 * don't have the focus.
1855 */
1856
1857 #ifdef ALWAYS_SHOW_SELECTION
1858 showSelection = 1;
1859 #else
1860 showSelection = (entryPtr->flags & GOT_FOCUS);
1861 #endif
1862
1863 /*
1864 * Draw the background in three layers. From bottom to top the
1865 * layers are: normal background, selection background, and
1866 * insertion cursor background.
1867 */
1868
1869 if ((entryPtr->state == STATE_DISABLED) &&
1870 (entryPtr->disabledBorder != NULL)) {
1871 border = entryPtr->disabledBorder;
1872 } else if ((entryPtr->state == STATE_READONLY) &&
1873 (entryPtr->readonlyBorder != NULL)) {
1874 border = entryPtr->readonlyBorder;
1875 } else {
1876 border = entryPtr->normalBorder;
1877 }
1878 Tk_Fill3DRectangle(tkwin, pixmap, border,
1879 0, 0, Tk_Width(tkwin), Tk_Height(tkwin), 0, TK_RELIEF_FLAT);
1880
1881 if (showSelection && (entryPtr->state != STATE_DISABLED)
1882 && (entryPtr->selectLast > entryPtr->leftIndex)) {
1883 if (entryPtr->selectFirst <= entryPtr->leftIndex) {
1884 selStartX = entryPtr->leftX;
1885 } else {
1886 Tk_CharBbox(entryPtr->textLayout, entryPtr->selectFirst,
1887 &selStartX, NULL, NULL, NULL);
1888 selStartX += entryPtr->layoutX;
1889 }
1890 if ((selStartX - entryPtr->selBorderWidth) < xBound) {
1891 Tk_CharBbox(entryPtr->textLayout, entryPtr->selectLast,
1892 &selEndX, NULL, NULL, NULL);
1893 selEndX += entryPtr->layoutX;
1894 Tk_Fill3DRectangle(tkwin, pixmap, entryPtr->selBorder,
1895 selStartX - entryPtr->selBorderWidth,
1896 baseY - fm.ascent - entryPtr->selBorderWidth,
1897 (selEndX - selStartX) + 2*entryPtr->selBorderWidth,
1898 (fm.ascent + fm.descent) + 2*entryPtr->selBorderWidth,
1899 entryPtr->selBorderWidth, TK_RELIEF_RAISED);
1900 }
1901 }
1902
1903 /*
1904 * Draw a special background for the insertion cursor, overriding
1905 * even the selection background. As a special hack to keep the
1906 * cursor visible when the insertion cursor color is the same as
1907 * the color for selected text (e.g., on mono displays), write
1908 * background in the cursor area (instead of nothing) when the
1909 * cursor isn't on. Otherwise the selection would hide the cursor.
1910 */
1911
1912 if ((entryPtr->state == STATE_NORMAL) && (entryPtr->flags & GOT_FOCUS)) {
1913 Tk_CharBbox(entryPtr->textLayout, entryPtr->insertPos, &cursorX, NULL,
1914 NULL, NULL);
1915 cursorX += entryPtr->layoutX;
1916 cursorX -= (entryPtr->insertWidth)/2;
1917 Tk_SetCaretPos(entryPtr->tkwin, cursorX, baseY - fm.ascent,
1918 fm.ascent + fm.descent);
1919 if (entryPtr->insertPos >= entryPtr->leftIndex) {
1920 if (cursorX < xBound) {
1921 if (entryPtr->flags & CURSOR_ON) {
1922 Tk_Fill3DRectangle(tkwin, pixmap, entryPtr->insertBorder,
1923 cursorX, baseY - fm.ascent, entryPtr->insertWidth,
1924 fm.ascent + fm.descent,
1925 entryPtr->insertBorderWidth,
1926 TK_RELIEF_RAISED);
1927 } else if (entryPtr->insertBorder == entryPtr->selBorder) {
1928 Tk_Fill3DRectangle(tkwin, pixmap, border,
1929 cursorX, baseY - fm.ascent, entryPtr->insertWidth,
1930 fm.ascent + fm.descent, 0, TK_RELIEF_FLAT);
1931 }
1932 }
1933 }
1934 }
1935
1936 /*
1937 * Draw the text in two pieces: first the unselected portion, then the
1938 * selected portion on top of it.
1939 */
1940
1941 Tk_DrawTextLayout(entryPtr->display, pixmap, entryPtr->textGC,
1942 entryPtr->textLayout, entryPtr->layoutX, entryPtr->layoutY,
1943 entryPtr->leftIndex, entryPtr->numChars);
1944
1945 if (showSelection && (entryPtr->state != STATE_DISABLED)
1946 && (entryPtr->selTextGC != entryPtr->textGC)
1947 && (entryPtr->selectFirst < entryPtr->selectLast)) {
1948 int selFirst;
1949
1950 if (entryPtr->selectFirst < entryPtr->leftIndex) {
1951 selFirst = entryPtr->leftIndex;
1952 } else {
1953 selFirst = entryPtr->selectFirst;
1954 }
1955 Tk_DrawTextLayout(entryPtr->display, pixmap, entryPtr->selTextGC,
1956 entryPtr->textLayout, entryPtr->layoutX, entryPtr->layoutY,
1957 selFirst, entryPtr->selectLast);
1958 }
1959
1960 if (entryPtr->type == TK_SPINBOX) {
1961 int startx, height, inset, pad, tHeight, xWidth;
1962 Spinbox *sbPtr = (Spinbox *) entryPtr;
1963
1964 /*
1965 * Draw the spin button controls.
1966 */
1967 xWidth = entryPtr->xWidth;
1968 pad = XPAD + 1;
1969 inset = entryPtr->inset - XPAD;
1970 startx = Tk_Width(tkwin) - (xWidth + inset);
1971 height = (Tk_Height(tkwin) - 2*inset)/2;
1972 #if 0
1973 Tk_Fill3DRectangle(tkwin, pixmap, sbPtr->buttonBorder,
1974 startx, inset, xWidth, height, 1, sbPtr->buRelief);
1975 Tk_Fill3DRectangle(tkwin, pixmap, sbPtr->buttonBorder,
1976 startx, inset+height, xWidth, height, 1, sbPtr->bdRelief);
1977 #else
1978 Tk_Fill3DRectangle(tkwin, pixmap, sbPtr->buttonBorder,
1979 startx, inset, xWidth, height, 1,
1980 (sbPtr->selElement == SEL_BUTTONUP) ?
1981 TK_RELIEF_SUNKEN : TK_RELIEF_RAISED);
1982 Tk_Fill3DRectangle(tkwin, pixmap, sbPtr->buttonBorder,
1983 startx, inset+height, xWidth, height, 1,
1984 (sbPtr->selElement == SEL_BUTTONDOWN) ?
1985 TK_RELIEF_SUNKEN : TK_RELIEF_RAISED);
1986 #endif
1987
1988 xWidth -= 2*pad;
1989 /*
1990 * Only draw the triangles if we have enough display space
1991 */
1992 if ((xWidth > 1)) {
1993 XPoint points[3];
1994 int starty, space, offset;
1995
1996 space = height - 2*pad;
1997 /*
1998 * Ensure width of triangle is odd to guarantee a sharp tip
1999 */
2000 if (!(xWidth % 2)) {
2001 xWidth++;
2002 }
2003 tHeight = (xWidth + 1) / 2;
2004 if (tHeight > space) {
2005 tHeight = space;
2006 }
2007 space = (space - tHeight) / 2;
2008 startx += pad;
2009 starty = inset + height - pad - space;
2010 offset = (sbPtr->selElement == SEL_BUTTONUP);
2011 /*
2012 * The points are slightly different for the up and down arrows
2013 * because (for *.x), we need to account for a bug in the way
2014 * XFillPolygon draws triangles, and we want to shift
2015 * the arrows differently when allowing for depressed behavior.
2016 */
2017 points[0].x = startx + offset;
2018 points[0].y = starty + (offset ? 0 : -1);
2019 points[1].x = startx + xWidth/2 + offset;
2020 points[1].y = starty - tHeight + (offset ? 0 : -1);
2021 points[2].x = startx + xWidth + offset;
2022 points[2].y = points[0].y;
2023 XFillPolygon(entryPtr->display, pixmap, entryPtr->textGC,
2024 points, 3, Convex, CoordModeOrigin);
2025
2026 starty = inset + height + pad + space;
2027 offset = (sbPtr->selElement == SEL_BUTTONDOWN);
2028 points[0].x = startx + 1 + offset;
2029 points[0].y = starty + (offset ? 1 : 0);
2030 points[1].x = startx + xWidth/2 + offset;
2031 points[1].y = starty + tHeight + (offset ? 0 : -1);
2032 points[2].x = startx - 1 + xWidth + offset;
2033 points[2].y = points[0].y;
2034 XFillPolygon(entryPtr->display, pixmap, entryPtr->textGC,
2035 points, 3, Convex, CoordModeOrigin);
2036 }
2037 }
2038
2039 /*
2040 * Draw the border and focus highlight last, so they will overwrite
2041 * any text that extends past the viewable part of the window.
2042 */
2043
2044 xBound = entryPtr->highlightWidth;
2045 if (entryPtr->relief != TK_RELIEF_FLAT) {
2046 Tk_Draw3DRectangle(tkwin, pixmap, border, xBound, xBound,
2047 Tk_Width(tkwin) - 2 * xBound,
2048 Tk_Height(tkwin) - 2 * xBound,
2049 entryPtr->borderWidth, entryPtr->relief);
2050 }
2051 if (xBound > 0) {
2052 GC fgGC, bgGC;
2053
2054 bgGC = Tk_GCForColor(entryPtr->highlightBgColorPtr, pixmap);
2055 if (entryPtr->flags & GOT_FOCUS) {
2056 fgGC = Tk_GCForColor(entryPtr->highlightColorPtr, pixmap);
2057 TkpDrawHighlightBorder(tkwin, fgGC, bgGC, xBound, pixmap);
2058 } else {
2059 TkpDrawHighlightBorder(tkwin, bgGC, bgGC, xBound, pixmap);
2060 }
2061 }
2062
2063 /*
2064 * Everything's been redisplayed; now copy the pixmap onto the screen
2065 * and free up the pixmap.
2066 */
2067
2068 XCopyArea(entryPtr->display, pixmap, Tk_WindowId(tkwin), entryPtr->textGC,
2069 0, 0, (unsigned) Tk_Width(tkwin), (unsigned) Tk_Height(tkwin),
2070 0, 0);
2071 Tk_FreePixmap(entryPtr->display, pixmap);
2072 entryPtr->flags &= ~BORDER_NEEDED;
2073 }
2074
2075 /*
2076 *----------------------------------------------------------------------
2077 *
2078 * EntryComputeGeometry --
2079 *
2080 * This procedure is invoked to recompute information about where
2081 * in its window an entry's string will be displayed. It also
2082 * computes the requested size for the window.
2083 *
2084 * Results:
2085 * None.
2086 *
2087 * Side effects:
2088 * The leftX and tabOrigin fields are recomputed for entryPtr,
2089 * and leftIndex may be adjusted. Tk_GeometryRequest is called
2090 * to register the desired dimensions for the window.
2091 *
2092 *----------------------------------------------------------------------
2093 */
2094
2095 static void
2096 EntryComputeGeometry(entryPtr)
2097 Entry *entryPtr; /* Widget record for entry. */
2098 {
2099 int totalLength, overflow, maxOffScreen, rightX;
2100 int height, width, i;
2101 Tk_FontMetrics fm;
2102 char *p;
2103
2104 if (entryPtr->displayString != entryPtr->string) {
2105 ckfree((char *)entryPtr->displayString);
2106 entryPtr->displayString = entryPtr->string;
2107 entryPtr->numDisplayBytes = entryPtr->numBytes;
2108 }
2109
2110 /*
2111 * If we're displaying a special character instead of the value of
2112 * the entry, recompute the displayString.
2113 */
2114
2115 if (entryPtr->showChar != NULL) {
2116 Tcl_UniChar ch;
2117 char buf[TCL_UTF_MAX];
2118 int size;
2119
2120 /*
2121 * Normalize the special character so we can safely duplicate it
2122 * in the display string. If we didn't do this, then two malformed
2123 * characters might end up looking like one valid UTF character in
2124 * the resulting string.
2125 */
2126
2127 Tcl_UtfToUniChar(entryPtr->showChar, &ch);
2128 size = Tcl_UniCharToUtf(ch, buf);
2129
2130 entryPtr->numDisplayBytes = entryPtr->numChars * size;
2131 p = (char *) ckalloc((unsigned) (entryPtr->numDisplayBytes + 1));
2132 entryPtr->displayString = p;
2133
2134 for (i = entryPtr->numChars; --i >= 0; ) {
2135 p += Tcl_UniCharToUtf(ch, p);
2136 }
2137 *p = '\0';
2138 }
2139
2140 Tk_FreeTextLayout(entryPtr->textLayout);
2141 entryPtr->textLayout = Tk_ComputeTextLayout(entryPtr->tkfont,
2142 entryPtr->displayString, entryPtr->numChars, 0,
2143 entryPtr->justify, TK_IGNORE_NEWLINES, &totalLength, &height);
2144
2145 entryPtr->layoutY = (Tk_Height(entryPtr->tkwin) - height) / 2;
2146
2147 /*
2148 * Recompute where the leftmost character on the display will
2149 * be drawn (entryPtr->leftX) and adjust leftIndex if necessary
2150 * so that we don't let characters hang off the edge of the
2151 * window unless the entire window is full.
2152 */
2153
2154 overflow = totalLength -
2155 (Tk_Width(entryPtr->tkwin) - 2*entryPtr->inset - entryPtr->xWidth);
2156 if (overflow <= 0) {
2157 entryPtr->leftIndex = 0;
2158 if (entryPtr->justify == TK_JUSTIFY_LEFT) {
2159 entryPtr->leftX = entryPtr->inset;
2160 } else if (entryPtr->justify == TK_JUSTIFY_RIGHT) {
2161 entryPtr->leftX = Tk_Width(entryPtr->tkwin) - entryPtr->inset
2162 - entryPtr->xWidth - totalLength;
2163 } else {
2164 entryPtr->leftX = (Tk_Width(entryPtr->tkwin)
2165 - entryPtr->xWidth - totalLength)/2;
2166 }
2167 entryPtr->layoutX = entryPtr->leftX;
2168 } else {
2169 /*
2170 * The whole string can't fit in the window. Compute the
2171 * maximum number of characters that may be off-screen to
2172 * the left without leaving empty space on the right of the
2173 * window, then don't let leftIndex be any greater than that.
2174 */
2175
2176 maxOffScreen = Tk_PointToChar(entryPtr->textLayout, overflow, 0);
2177 Tk_CharBbox(entryPtr->textLayout, maxOffScreen,
2178 &rightX, NULL, NULL, NULL);
2179 if (rightX < overflow) {
2180 maxOffScreen++;
2181 }
2182 if (entryPtr->leftIndex > maxOffScreen) {
2183 entryPtr->leftIndex = maxOffScreen;
2184 }
2185 Tk_CharBbox(entryPtr->textLayout, entryPtr->leftIndex, &rightX,
2186 NULL, NULL, NULL);
2187 entryPtr->leftX = entryPtr->inset;
2188 entryPtr->layoutX = entryPtr->leftX - rightX;
2189 }
2190
2191 Tk_GetFontMetrics(entryPtr->tkfont, &fm);
2192 height = fm.linespace + 2*entryPtr->inset + 2*(YPAD-XPAD);
2193 if (entryPtr->prefWidth > 0) {
2194 width = entryPtr->prefWidth*entryPtr->avgWidth + 2*entryPtr->inset;
2195 } else {
2196 if (totalLength == 0) {
2197 width = entryPtr->avgWidth + 2*entryPtr->inset;
2198 } else {
2199 width = totalLength + 2*entryPtr->inset;
2200 }
2201 }
2202
2203 /*
2204 * Add one extra length for the spin buttons
2205 */
2206 width += entryPtr->xWidth;
2207
2208 Tk_GeometryRequest(entryPtr->tkwin, width, height);
2209 }
2210
2211 /*
2212 *----------------------------------------------------------------------
2213 *
2214 * InsertChars --
2215 *
2216 * Add new characters to an entry widget.
2217 *
2218 * Results:
2219 * None.
2220 *
2221 * Side effects:
2222 * New information gets added to entryPtr; it will be redisplayed
2223 * soon, but not necessarily immediately.
2224 *
2225 *----------------------------------------------------------------------
2226 */
2227
2228 static void
2229 InsertChars(entryPtr, index, value)
2230 Entry *entryPtr; /* Entry that is to get the new elements. */
2231 int index; /* Add the new elements before this
2232 * character index. */
2233 char *value; /* New characters to add (NULL-terminated
2234 * string). */
2235 {
2236 int byteIndex, byteCount, oldChars, charsAdded, newByteCount;
2237 CONST char *string;
2238 char *new;
2239
2240 string = entryPtr->string;
2241 byteIndex = Tcl_UtfAtIndex(string, index) - string;
2242 byteCount = strlen(value);
2243 if (byteCount == 0) {
2244 return;
2245 }
2246
2247 newByteCount = entryPtr->numBytes + byteCount + 1;
2248 new = (char *) ckalloc((unsigned) newByteCount);
2249 memcpy(new, string, (size_t) byteIndex);
2250 strcpy(new + byteIndex, value);
2251 strcpy(new + byteIndex + byteCount, string + byteIndex);
2252
2253 if ((entryPtr->validate == VALIDATE_KEY ||
2254 entryPtr->validate == VALIDATE_ALL) &&
2255 EntryValidateChange(entryPtr, value, new, index,
2256 VALIDATE_INSERT) != TCL_OK) {
2257 ckfree(new);
2258 return;
2259 }
2260
2261 ckfree((char *)string);
2262 entryPtr->string = new;
2263
2264 /*
2265 * The following construction is used because inserting improperly
2266 * formed UTF-8 sequences between other improperly formed UTF-8
2267 * sequences could result in actually forming valid UTF-8 sequences;
2268 * the number of characters added may not be Tcl_NumUtfChars(string, -1),
2269 * because of context. The actual number of characters added is how
2270 * many characters are in the string now minus the number that
2271 * used to be there.
2272 */
2273
2274 oldChars = entryPtr->numChars;
2275 entryPtr->numChars = Tcl_NumUtfChars(new, -1);
2276 charsAdded = entryPtr->numChars - oldChars;
2277 entryPtr->numBytes += byteCount;
2278
2279 if (entryPtr->displayString == string) {
2280 entryPtr->displayString = new;
2281 entryPtr->numDisplayBytes = entryPtr->numBytes;
2282 }
2283
2284 /*
2285 * Inserting characters invalidates all indexes into the string.
2286 * Touch up the indexes so that they still refer to the same
2287 * characters (at new positions). When updating the selection
2288 * end-points, don't include the new text in the selection unless
2289 * it was completely surrounded by the selection.
2290 */
2291
2292 if (entryPtr->selectFirst >= index) {
2293 entryPtr->selectFirst += charsAdded;
2294 }
2295 if (entryPtr->selectLast > index) {
2296 entryPtr->selectLast += charsAdded;
2297 }
2298 if ((entryPtr->selectAnchor > index)
2299 || (entryPtr->selectFirst >= index)) {
2300 entryPtr->selectAnchor += charsAdded;
2301 }
2302 if (entryPtr->leftIndex > index) {
2303 entryPtr->leftIndex += charsAdded;
2304 }
2305 if (entryPtr->insertPos >= index) {
2306 entryPtr->insertPos += charsAdded;
2307 }
2308 EntryValueChanged(entryPtr, NULL);
2309 }
2310
2311 /*
2312 *----------------------------------------------------------------------
2313 *
2314 * DeleteChars --
2315 *
2316 * Remove one or more characters from an entry widget.
2317 *
2318 * Results:
2319 * None.
2320 *
2321 * Side effects:
2322 * Memory gets freed, the entry gets modified and (eventually)
2323 * redisplayed.
2324 *
2325 *----------------------------------------------------------------------
2326 */
2327
2328 static void
2329 DeleteChars(entryPtr, index, count)
2330 Entry *entryPtr; /* Entry widget to modify. */
2331 int index; /* Index of first character to delete. */
2332 int count; /* How many characters to delete. */
2333 {
2334 int byteIndex, byteCount, newByteCount;
2335 CONST char *string;
2336 char *new, *todelete;
2337
2338 if ((index + count) > entryPtr->numChars) {
2339 count = entryPtr->numChars - index;
2340 }
2341 if (count <= 0) {
2342 return;
2343 }
2344
2345 string = entryPtr->string;
2346 byteIndex = Tcl_UtfAtIndex(string, index) - string;
2347 byteCount = Tcl_UtfAtIndex(string + byteIndex, count) - (string + byteIndex);
2348
2349 newByteCount = entryPtr->numBytes + 1 - byteCount;
2350 new = (char *) ckalloc((unsigned) newByteCount);
2351 memcpy(new, string, (size_t) byteIndex);
2352 strcpy(new + byteIndex, string + byteIndex + byteCount);
2353
2354 todelete = (char *) ckalloc((unsigned) (byteCount + 1));
2355 memcpy(todelete, string + byteIndex, (size_t) byteCount);
2356 todelete[byteCount] = '\0';
2357
2358 if ((entryPtr->validate == VALIDATE_KEY ||
2359 entryPtr->validate == VALIDATE_ALL) &&
2360 EntryValidateChange(entryPtr, todelete, new, index,
2361 VALIDATE_DELETE) != TCL_OK) {
2362 ckfree(new);
2363 ckfree(todelete);
2364 return;
2365 }
2366
2367 ckfree(todelete);
2368 ckfree((char *)entryPtr->string);
2369 entryPtr->string = new;
2370 entryPtr->numChars -= count;
2371 entryPtr->numBytes -= byteCount;
2372
2373 if (entryPtr->displayString == string) {
2374 entryPtr->displayString = new;
2375 entryPtr->numDisplayBytes = entryPtr->numBytes;
2376 }
2377
2378 /*
2379 * Deleting characters results in the remaining characters being
2380 * renumbered. Update the various indexes into the string to reflect
2381 * this change.
2382 */
2383
2384 if (entryPtr->selectFirst >= index) {
2385 if (entryPtr->selectFirst >= (index + count)) {
2386 entryPtr->selectFirst -= count;
2387 } else {
2388 entryPtr->selectFirst = index;
2389 }
2390 }
2391 if (entryPtr->selectLast >= index) {
2392 if (entryPtr->selectLast >= (index + count)) {
2393 entryPtr->selectLast -= count;
2394 } else {
2395 entryPtr->selectLast = index;
2396 }
2397 }
2398 if (entryPtr->selectLast <= entryPtr->selectFirst) {
2399 entryPtr->selectFirst = -1;
2400 entryPtr->selectLast = -1;
2401 }
2402 if (entryPtr->selectAnchor >= index) {
2403 if (entryPtr->selectAnchor >= (index+count)) {
2404 entryPtr->selectAnchor -= count;
2405 } else {
2406 entryPtr->selectAnchor = index;
2407 }
2408 }
2409 if (entryPtr->leftIndex > index) {
2410 if (entryPtr->leftIndex >= (index + count)) {
2411 entryPtr->leftIndex -= count;
2412 } else {
2413 entryPtr->leftIndex = index;
2414 }
2415 }
2416 if (entryPtr->insertPos >= index) {
2417 if (entryPtr->insertPos >= (index + count)) {
2418 entryPtr->insertPos -= count;
2419 } else {
2420 entryPtr->insertPos = index;
2421 }
2422 }
2423 EntryValueChanged(entryPtr, NULL);
2424 }
2425
2426 /*
2427 *----------------------------------------------------------------------
2428 *
2429 * EntryValueChanged --
2430 *
2431 * This procedure is invoked when characters are inserted into
2432 * an entry or deleted from it. It updates the entry's associated
2433 * variable, if there is one, and does other bookkeeping such
2434 * as arranging for redisplay.
2435 *
2436 * Results:
2437 * None.
2438 *
2439 * Side effects:
2440 * None.
2441 *
2442 *----------------------------------------------------------------------
2443 */
2444
2445 static void
2446 EntryValueChanged(entryPtr, newValue)
2447 Entry *entryPtr; /* Entry whose value just changed. */
2448 CONST char *newValue; /* If this value is not NULL, we first
2449 * force the value of the entry to this */
2450 {
2451 if (newValue != NULL) {
2452 EntrySetValue(entryPtr, newValue);
2453 }
2454
2455 if (entryPtr->textVarName == NULL) {
2456 newValue = NULL;
2457 } else {
2458 Tcl_Obj *temp = Tcl_NewStringObj(entryPtr->string,-1);
2459 newValue = Tcl_GetString(Tcl_ObjSetVar2(entryPtr->interp, entryPtr->textVarName, NULL,
2460 temp, TCL_GLOBAL_ONLY));
2461 Tcl_DecrRefCount(temp);
2462 }
2463
2464 if ((newValue != NULL) && (strcmp(newValue, entryPtr->string) != 0)) {
2465 /*
2466 * The value of the variable is different than what we asked for.
2467 * This means that a trace on the variable modified it. In this
2468 * case our trace procedure wasn't invoked since the modification
2469 * came while a trace was already active on the variable. So,
2470 * update our value to reflect the variable's latest value.
2471 */
2472
2473 EntrySetValue(entryPtr, newValue);
2474 } else {
2475 /*
2476 * Arrange for redisplay.
2477 */
2478
2479 entryPtr->flags |= UPDATE_SCROLLBAR;
2480 EntryComputeGeometry(entryPtr);
2481 EventuallyRedraw(entryPtr);
2482 }
2483 }
2484
2485 /*
2486 *----------------------------------------------------------------------
2487 *
2488 * EntrySetValue --
2489 *
2490 * Replace the contents of a text entry with a given value. This
2491 * procedure is invoked when updating the entry from the entry's
2492 * associated variable.
2493 *
2494 * Results:
2495 * None.
2496 *
2497 * Side effects:
2498 * The string displayed in the entry will change. The selection,
2499 * insertion point, and view may have to be adjusted to keep them
2500 * within the bounds of the new string. Note: this procedure does
2501 * *not* update the entry's associated variable, since that could
2502 * result in an infinite loop.
2503 *
2504 *----------------------------------------------------------------------
2505 */
2506
2507 static void
2508 EntrySetValue(entryPtr, value)
2509 Entry *entryPtr; /* Entry whose value is to be changed. */
2510 CONST char *value; /* New text to display in entry. */
2511 {
2512 CONST char *oldSource;
2513 int valueLen, malloced = 0;
2514
2515 if (strcmp(value, entryPtr->string) == 0) {
2516 return;
2517 }
2518 valueLen = strlen(value);
2519
2520 if (entryPtr->flags & VALIDATE_VAR) {
2521 /* Recursing : assume we are in fixup code and it knows
2522 what it is doing
2523 */
2524 } else {
2525 /*
2526 * If we validate, we create a copy of the value, as it may
2527 * point to volatile memory, like the value of the -textvar
2528 * which may get freed during validation
2529 */
2530 char *tmp = (char *) ckalloc((unsigned) (valueLen + 1));
2531 strcpy(tmp, value);
2532 value = tmp;
2533 malloced = 1;
2534
2535 entryPtr->flags |= VALIDATE_VAR;
2536 (void) EntryValidateChange(entryPtr, (char *) NULL, value, -1,
2537 VALIDATE_FORCED);
2538 entryPtr->flags &= ~VALIDATE_VAR;
2539 /*
2540 * If VALIDATE_ABORT has been set, then this operation should be
2541 * aborted because the validatecommand did something else instead
2542 */
2543 if (entryPtr->flags & VALIDATE_ABORT) {
2544 entryPtr->flags &= ~VALIDATE_ABORT;
2545 ckfree((char *)value);
2546 return;
2547 }
2548 entryPtr->flags &= ~VALIDATE_VAR;
2549 }
2550
2551 oldSource = entryPtr->string;
2552 ckfree((char *)entryPtr->string);
2553
2554 if (malloced) {
2555 entryPtr->string = value;
2556 } else {
2557 char *tmp = (char *) ckalloc((unsigned) (valueLen + 1));
2558 strcpy(tmp, value);
2559 entryPtr->string = tmp;
2560 }
2561 entryPtr->numBytes = valueLen;
2562 entryPtr->numChars = Tcl_NumUtfChars(value, valueLen);
2563
2564 if (entryPtr->displayString == oldSource) {
2565 entryPtr->displayString = entryPtr->string;
2566 entryPtr->numDisplayBytes = entryPtr->numBytes;
2567 }
2568
2569 if (entryPtr->selectFirst >= 0) {
2570 if (entryPtr->selectFirst >= entryPtr->numChars) {
2571 entryPtr->selectFirst = -1;
2572 entryPtr->selectLast = -1;
2573 } else if (entryPtr->selectLast > entryPtr->numChars) {
2574 entryPtr->selectLast = entryPtr->numChars;
2575 }
2576 }
2577 if (entryPtr->leftIndex >= entryPtr->numChars) {
2578 if (entryPtr->numChars > 0) {
2579 entryPtr->leftIndex = entryPtr->numChars - 1;
2580 } else {
2581 entryPtr->leftIndex = 0;
2582 }
2583 }
2584 if (entryPtr->insertPos > entryPtr->numChars) {
2585 entryPtr->insertPos = entryPtr->numChars;
2586 }
2587
2588 entryPtr->flags |= UPDATE_SCROLLBAR;
2589 EntryComputeGeometry(entryPtr);
2590 EventuallyRedraw(entryPtr);
2591 }
2592
2593 /*
2594 *--------------------------------------------------------------
2595 *
2596 * EntryEventProc --
2597 *
2598 * This procedure is invoked by the Tk dispatcher for various
2599 * events on entries.
2600 *
2601 * Results:
2602 * None.
2603 *
2604 * Side effects:
2605 * When the window gets deleted, internal structures get
2606 * cleaned up. When it gets exposed, it is redisplayed.
2607 *
2608 *--------------------------------------------------------------
2609 */
2610
2611 static void
2612 EntryEventProc(clientData, eventPtr)
2613 ClientData clientData; /* Information about window. */
2614 XEvent *eventPtr; /* Information about event. */
2615 {
2616 Entry *entryPtr = (Entry *) clientData;
2617
2618 if ((entryPtr->type == TK_SPINBOX) && (eventPtr->type == MotionNotify)) {
2619 Spinbox *sbPtr = (Spinbox *) clientData;
2620 int elem;
2621
2622 elem = GetSpinboxElement(sbPtr, eventPtr->xmotion.x,
2623 eventPtr->xmotion.y);
2624 if (elem != sbPtr->curElement) {
2625 Tk_Cursor cursor;
2626
2627 sbPtr->curElement = elem;
2628 if (elem == SEL_ENTRY) {
2629 cursor = entryPtr->cursor;
2630 } else if ((elem == SEL_BUTTONDOWN) || (elem == SEL_BUTTONUP)) {
2631 cursor = sbPtr->bCursor;
2632 } else {
2633 cursor = None;
2634 }
2635 if (cursor != None) {
2636 Tk_DefineCursor(entryPtr->tkwin, cursor);
2637 } else {
2638 Tk_UndefineCursor(entryPtr->tkwin);
2639 }
2640 }
2641 return;
2642 }
2643
2644 switch (eventPtr->type) {
2645 case Expose:
2646 EventuallyRedraw(entryPtr);
2647 entryPtr->flags |= BORDER_NEEDED;
2648 break;
2649 case DestroyNotify:
2650 if (!(entryPtr->flags & ENTRY_DELETED)) {
2651 entryPtr->flags |= (ENTRY_DELETED | VALIDATE_ABORT);
2652 Tcl_DeleteCommandFromToken(entryPtr->interp,
2653 entryPtr->widgetCmd);
2654 if (entryPtr->flags & REDRAW_PENDING) {
2655 Tcl_CancelIdleCall(DisplayEntry, clientData);
2656 }
2657 Tcl_EventuallyFree(clientData, DestroyEntry);
2658 }
2659 break;
2660 case ConfigureNotify:
2661 Tcl_Preserve((ClientData) entryPtr);
2662 entryPtr->flags |= UPDATE_SCROLLBAR;
2663 EntryComputeGeometry(entryPtr);
2664 EventuallyRedraw(entryPtr);
2665 Tcl_Release((ClientData) entryPtr);
2666 break;
2667 case FocusIn:
2668 case FocusOut:
2669 if (eventPtr->xfocus.detail != NotifyInferior) {
2670 EntryFocusProc(entryPtr, (eventPtr->type == FocusIn));
2671 }
2672 break;
2673 }
2674 }
2675
2676 /*
2677 *----------------------------------------------------------------------
2678 *
2679 * EntryCmdDeletedProc --
2680 *
2681 * This procedure is invoked when a widget command is deleted. If
2682 * the widget isn't already in the process of being destroyed,
2683 * this command destroys it.
2684 *
2685 * Results:
2686 * None.
2687 *
2688 * Side effects:
2689 * The widget is destroyed.
2690 *
2691 *----------------------------------------------------------------------
2692 */
2693
2694 static void
2695 EntryCmdDeletedProc(clientData)
2696 ClientData clientData; /* Pointer to widget record for widget. */
2697 {
2698 Entry *entryPtr = (Entry *) clientData;
2699
2700 /*
2701 * This procedure could be invoked either because the window was
2702 * destroyed and the command was then deleted (in which case tkwin
2703 * is NULL) or because the command was deleted, and then this procedure
2704 * destroys the widget.
2705 */
2706
2707 if (!(entryPtr->flags & ENTRY_DELETED)) {
2708 Tk_DestroyWindow(entryPtr->tkwin);
2709 }
2710 }
2711
2712 /*
2713 *---------------------------------------------------------------------------
2714 *
2715 * GetEntryIndex --
2716 *
2717 * Parse an index into an entry and return either its value
2718 * or an error.
2719 *
2720 * Results:
2721 * A standard Tcl result. If all went well, then *indexPtr is
2722 * filled in with the character index (into entryPtr) corresponding to
2723 * string. The index value is guaranteed to lie between 0 and
2724 * the number of characters in the string, inclusive. If an
2725 * error occurs then an error message is left in the interp's result.
2726 *
2727 * Side effects:
2728 * None.
2729 *
2730 *---------------------------------------------------------------------------
2731 */
2732
2733 static int
2734 GetEntryIndex(interp, entryPtr, arg, indexPtr)
2735 Tcl_Interp *interp; /* For error messages. */
2736 Entry *entryPtr; /* Entry for which the index is being
2737 * specified. */
2738 Tcl_Obj *arg; /* Specifies character in entryPtr. */
2739 int *indexPtr; /* Where to store converted index. */
2740 {
2741 size_t length;
2742 char *string = Tcl_GetString(arg);
2743
2744 length = strlen(string);
2745
2746 if (string[0] == 'a') {
2747 if (strncmp(string, "anchor", length) == 0) {
2748 *indexPtr = entryPtr->selectAnchor;
2749 } else {
2750 badIndex:
2751
2752 /*
2753 * Some of the paths here leave messages in the interp's result,
2754 * so we have to clear it out before storing our own message.
2755 */
2756
2757 Tcl_SetResult(interp, (char *) NULL, TCL_STATIC);
2758 Tcl_AppendResult(interp, "bad ",
2759 (entryPtr->type == TK_ENTRY) ? "entry" : "spinbox",
2760 " index \"", string, "\"", (char *) NULL);
2761 return TCL_ERROR;
2762 }
2763 } else if (string[0] == 'e') {
2764 if (strncmp(string, "end", length) == 0) {
2765 *indexPtr = entryPtr->numChars;
2766 } else {
2767 goto badIndex;
2768 }
2769 } else if (string[0] == 'i') {
2770 if (strncmp(string, "insert", length) == 0) {
2771 *indexPtr = entryPtr->insertPos;
2772 } else {
2773 goto badIndex;
2774 }
2775 } else if (string[0] == 's') {
2776 if (entryPtr->selectFirst < 0) {
2777 Tcl_SetResult(interp, (char *) NULL, TCL_STATIC);
2778 Tcl_AppendResult(interp, "selection isn't in widget ",
2779 Tk_PathName(entryPtr->tkwin), (char *) NULL);
2780 return TCL_ERROR;
2781 }
2782 if (length < 5) {
2783 goto badIndex;
2784 }
2785 if (strncmp(string, "sel.first", length) == 0) {
2786 *indexPtr = entryPtr->selectFirst;
2787 } else if (strncmp(string, "sel.last", length) == 0) {
2788 *indexPtr = entryPtr->selectLast;
2789 } else {
2790 goto badIndex;
2791 }
2792 } else if (string[0] == '@') {
2793 int x, roundUp, maxWidth;
2794
2795 Tcl_Obj *tmp = LangStringArg(string+1);
2796
2797 if (Tcl_GetIntFromObj(interp, tmp, &x) != TCL_OK) {
2798 LangFreeArg(tmp,TCL_DYNAMIC);
2799 goto badIndex;
2800 }
2801 LangFreeArg(tmp,TCL_DYNAMIC);
2802 if (x < entryPtr->inset) {
2803 x = entryPtr->inset;
2804 }
2805 roundUp = 0;
2806 maxWidth = Tk_Width(entryPtr->tkwin) - entryPtr->inset
2807 - entryPtr->xWidth - 1;
2808 if (x > maxWidth) {
2809 x = maxWidth;
2810 roundUp = 1;
2811 }
2812 *indexPtr = Tk_PointToChar(entryPtr->textLayout,
2813 x - entryPtr->layoutX, 0);
2814
2815 /*
2816 * Special trick: if the x-position was off-screen to the right,
2817 * round the index up to refer to the character just after the
2818 * last visible one on the screen. This is needed to enable the
2819 * last character to be selected, for example.
2820 */
2821
2822 if (roundUp && (*indexPtr < entryPtr->numChars)) {
2823 *indexPtr += 1;
2824 }
2825 } else {
2826
2827 if (Tcl_GetIntFromObj(interp, arg, indexPtr) != TCL_OK) {
2828 goto badIndex;
2829 }
2830 if (*indexPtr < 0){
2831 *indexPtr = 0;
2832 } else if (*indexPtr > entryPtr->numChars) {
2833 *indexPtr = entryPtr->numChars;
2834 }
2835 }
2836 return TCL_OK;
2837 }
2838
2839 /*
2840 *----------------------------------------------------------------------
2841 *
2842 * EntryScanTo --
2843 *
2844 * Given a y-coordinate (presumably of the curent mouse location)
2845 * drag the view in the window to implement the scan operation.
2846 *
2847 * Results:
2848 * None.
2849 *
2850 * Side effects:
2851 * The view in the window may change.
2852 *
2853 *----------------------------------------------------------------------
2854 */
2855
2856 static void
2857 EntryScanTo(entryPtr, x)
2858 Entry *entryPtr; /* Information about widget. */
2859 int x; /* X-coordinate to use for scan operation. */
2860 {
2861 int newLeftIndex;
2862
2863 /*
2864 * Compute new leftIndex for entry by amplifying the difference
2865 * between the current position and the place where the scan
2866 * started (the "mark" position). If we run off the left or right
2867 * side of the entry, then reset the mark point so that the current
2868 * position continues to correspond to the edge of the window.
2869 * This means that the picture will start dragging as soon as the
2870 * mouse reverses direction (without this reset, might have to slide
2871 * mouse a long ways back before the picture starts moving again).
2872 */
2873
2874 newLeftIndex = entryPtr->scanMarkIndex
2875 - (10 * (x - entryPtr->scanMarkX)) / entryPtr->avgWidth;
2876 if (newLeftIndex >= entryPtr->numChars) {
2877 newLeftIndex = entryPtr->scanMarkIndex = entryPtr->numChars - 1;
2878 entryPtr->scanMarkX = x;
2879 }
2880 if (newLeftIndex < 0) {
2881 newLeftIndex = entryPtr->scanMarkIndex = 0;
2882 entryPtr->scanMarkX = x;
2883 }
2884
2885 if (newLeftIndex != entryPtr->leftIndex) {
2886 entryPtr->leftIndex = newLeftIndex;
2887 entryPtr->flags |= UPDATE_SCROLLBAR;
2888 EntryComputeGeometry(entryPtr);
2889 if (newLeftIndex != entryPtr->leftIndex) {
2890 entryPtr->scanMarkIndex = entryPtr->leftIndex;
2891 entryPtr->scanMarkX = x;
2892 }
2893 EventuallyRedraw(entryPtr);
2894 }
2895 }
2896
2897 /*
2898 *----------------------------------------------------------------------
2899 *
2900 * EntrySelectTo --
2901 *
2902 * Modify the selection by moving its un-anchored end. This could
2903 * make the selection either larger or smaller.
2904 *
2905 * Results:
2906 * None.
2907 *
2908 * Side effects:
2909 * The selection changes.
2910 *
2911 *----------------------------------------------------------------------
2912 */
2913
2914 static void
2915 EntrySelectTo(entryPtr, index)
2916 Entry *entryPtr; /* Information about widget. */
2917 int index; /* Character index of element that is to
2918 * become the "other" end of the selection. */
2919 {
2920 int newFirst, newLast;
2921
2922 /*
2923 * Grab the selection if we don't own it already.
2924 */
2925
2926 if (!(entryPtr->flags & GOT_SELECTION) && (entryPtr->exportSelection)) {
2927 Tk_OwnSelection(entryPtr->tkwin, XA_PRIMARY, EntryLostSelection,
2928 (ClientData) entryPtr);
2929 entryPtr->flags |= GOT_SELECTION;
2930 }
2931
2932 /*
2933 * Pick new starting and ending points for the selection.
2934 */
2935
2936 if (entryPtr->selectAnchor > entryPtr->numChars) {
2937 entryPtr->selectAnchor = entryPtr->numChars;
2938 }
2939 if (entryPtr->selectAnchor <= index) {
2940 newFirst = entryPtr->selectAnchor;
2941 newLast = index;
2942 } else {
2943 newFirst = index;
2944 newLast = entryPtr->selectAnchor;
2945 if (newLast < 0) {
2946 newFirst = newLast = -1;
2947 }
2948 }
2949 if ((entryPtr->selectFirst == newFirst)
2950 && (entryPtr->selectLast == newLast)) {
2951 return;
2952 }
2953 entryPtr->selectFirst = newFirst;
2954 entryPtr->selectLast = newLast;
2955 EventuallyRedraw(entryPtr);
2956 }
2957
2958 /*
2959 *----------------------------------------------------------------------
2960 *
2961 * EntryFetchSelection --
2962 *
2963 * This procedure is called back by Tk when the selection is
2964 * requested by someone. It returns part or all of the selection
2965 * in a buffer provided by the caller.
2966 *
2967 * Results:
2968 * The return value is the number of non-NULL bytes stored
2969 * at buffer. Buffer is filled (or partially filled) with a
2970 * NULL-terminated string containing part or all of the selection,
2971 * as given by offset and maxBytes.
2972 *
2973 * Side effects:
2974 * None.
2975 *
2976 *----------------------------------------------------------------------
2977 */
2978
2979 static int
2980 EntryFetchSelection(clientData, offset, buffer, maxBytes)
2981 ClientData clientData; /* Information about entry widget. */
2982 int offset; /* Byte offset within selection of first
2983 * character to be returned. */
2984 char *buffer; /* Location in which to place selection. */
2985 int maxBytes; /* Maximum number of bytes to place at
2986 * buffer, not including terminating NULL
2987 * character. */
2988 {
2989 Entry *entryPtr = (Entry *) clientData;
2990 int byteCount;
2991 CONST char *string;
2992 CONST char *selStart, *selEnd;
2993
2994 if ((entryPtr->selectFirst < 0) || !(entryPtr->exportSelection)) {
2995 return -1;
2996 }
2997 string = entryPtr->displayString;
2998 selStart = Tcl_UtfAtIndex(string, entryPtr->selectFirst);
2999 selEnd = Tcl_UtfAtIndex(selStart,
3000 entryPtr->selectLast - entryPtr->selectFirst);
3001 byteCount = selEnd - selStart - offset;
3002 if (byteCount > maxBytes) {
3003 byteCount = maxBytes;
3004 }
3005 if (byteCount <= 0) {
3006 return 0;
3007 }
3008 memcpy(buffer, selStart + offset, (size_t) byteCount);
3009 buffer[byteCount] = '\0';
3010 return byteCount;
3011 }
3012
3013 /*
3014 *----------------------------------------------------------------------
3015 *
3016 * EntryLostSelection --
3017 *
3018 * This procedure is called back by Tk when the selection is
3019 * grabbed away from an entry widget.
3020 *
3021 * Results:
3022 * None.
3023 *
3024 * Side effects:
3025 * The existing selection is unhighlighted, and the window is
3026 * marked as not containing a selection.
3027 *
3028 *----------------------------------------------------------------------
3029 */
3030
3031 static void
3032 EntryLostSelection(clientData)
3033 ClientData clientData; /* Information about entry widget. */
3034 {
3035 Entry *entryPtr = (Entry *) clientData;
3036
3037 entryPtr->flags &= ~GOT_SELECTION;
3038
3039 /*
3040 * On Windows and Mac systems, we want to remember the selection
3041 * for the next time the focus enters the window. On Unix, we need
3042 * to clear the selection since it is always visible.
3043 */
3044
3045 #ifdef ALWAYS_SHOW_SELECTION
3046 if ((entryPtr->selectFirst >= 0) && entryPtr->exportSelection) {
3047 entryPtr->selectFirst = -1;
3048 entryPtr->selectLast = -1;
3049 EventuallyRedraw(entryPtr);
3050 }
3051 #endif
3052 }
3053
3054 /*
3055 *----------------------------------------------------------------------
3056 *
3057 * EventuallyRedraw --
3058 *
3059 * Ensure that an entry is eventually redrawn on the display.
3060 *
3061 * Results:
3062 * None.
3063 *
3064 * Side effects:
3065 * Information gets redisplayed. Right now we don't do selective
3066 * redisplays: the whole window will be redrawn. This doesn't
3067 * seem to hurt performance noticeably, but if it does then this
3068 * could be changed.
3069 *
3070 *----------------------------------------------------------------------
3071 */
3072
3073 static void
3074 EventuallyRedraw(entryPtr)
3075 Entry *entryPtr; /* Information about widget. */
3076 {
3077 if ((entryPtr->flags & ENTRY_DELETED) || !Tk_IsMapped(entryPtr->tkwin)) {
3078 return;
3079 }
3080
3081 /*
3082 * Right now we don't do selective redisplays: the whole window
3083 * will be redrawn. This doesn't seem to hurt performance noticeably,
3084 * but if it does then this could be changed.
3085 */
3086
3087 if (!(entryPtr->flags & REDRAW_PENDING)) {
3088 entryPtr->flags |= REDRAW_PENDING;
3089 Tcl_DoWhenIdle(DisplayEntry, (ClientData) entryPtr);
3090 }
3091 }
3092
3093 /*
3094 *----------------------------------------------------------------------
3095 *
3096 * EntryVisibleRange --
3097 *
3098 * Return information about the range of the entry that is
3099 * currently visible.
3100 *
3101 * Results:
3102 * *firstPtr and *lastPtr are modified to hold fractions between
3103 * 0 and 1 identifying the range of characters visible in the
3104 * entry.
3105 *
3106 * Side effects:
3107 * None.
3108 *
3109 *----------------------------------------------------------------------
3110 */
3111
3112 static void
3113 EntryVisibleRange(entryPtr, firstPtr, lastPtr)
3114 Entry *entryPtr; /* Information about widget. */
3115 double *firstPtr; /* Return position of first visible
3116 * character in widget. */
3117 double *lastPtr; /* Return position of char just after last
3118 * visible one. */
3119 {
3120 int charsInWindow;
3121
3122 if (entryPtr->numChars == 0) {
3123 *firstPtr = 0.0;
3124 *lastPtr = 1.0;
3125 } else {
3126 charsInWindow = Tk_PointToChar(entryPtr->textLayout,
3127 Tk_Width(entryPtr->tkwin) - entryPtr->inset
3128 - entryPtr->xWidth - entryPtr->layoutX - 1, 0);
3129 if (charsInWindow < entryPtr->numChars) {
3130 charsInWindow++;
3131 }
3132 charsInWindow -= entryPtr->leftIndex;
3133 if (charsInWindow == 0) {
3134 charsInWindow = 1;
3135 }
3136
3137 *firstPtr = (double) entryPtr->leftIndex / entryPtr->numChars;
3138 *lastPtr = (double) (entryPtr->leftIndex + charsInWindow)
3139 / entryPtr->numChars;
3140 }
3141 }
3142
3143 /*
3144 *----------------------------------------------------------------------
3145 *
3146 * EntryUpdateScrollbar --
3147 *
3148 * This procedure is invoked whenever information has changed in
3149 * an entry in a way that would invalidate a scrollbar display.
3150 * If there is an associated scrollbar, then this procedure updates
3151 * it by invoking a Tcl command.
3152 *
3153 * Results:
3154 * None.
3155 *
3156 * Side effects:
3157 * A Tcl command is invoked, and an additional command may be
3158 * invoked to process errors in the command.
3159 *
3160 *----------------------------------------------------------------------
3161 */
3162
3163 static void
3164 EntryUpdateScrollbar(entryPtr)
3165 Entry *entryPtr; /* Information about widget. */
3166 {
3167 char args[TCL_DOUBLE_SPACE * 2];
3168 int code;
3169 double first, last;
3170 Tcl_Interp *interp;
3171
3172 if (entryPtr->scrollCmd == NULL) {
3173 return;
3174 }
3175
3176 interp = entryPtr->interp;
3177 Tcl_Preserve((ClientData) interp);
3178 EntryVisibleRange(entryPtr, &first, &last);
3179 code = LangDoCallback(entryPtr->interp, entryPtr->scrollCmd, 0, 2, " %g %g", first, last);
3180 if (code != TCL_OK) {
3181 Tcl_AddErrorInfo(interp,
3182 "\n (horizontal scrolling command executed by ");
3183 Tcl_AddErrorInfo(interp, Tk_PathName(entryPtr->tkwin));
3184 Tcl_AddErrorInfo(interp, ")");
3185 Tcl_BackgroundError(interp);
3186 }
3187 Tcl_SetResult(interp, (char *) NULL, TCL_STATIC);
3188 Tcl_Release((ClientData) interp);
3189 }
3190
3191 /*
3192 *----------------------------------------------------------------------
3193 *
3194 * EntryBlinkProc --
3195 *
3196 * This procedure is called as a timer handler to blink the
3197 * insertion cursor off and on.
3198 *
3199 * Results:
3200 * None.
3201 *
3202 * Side effects:
3203 * The cursor gets turned on or off, redisplay gets invoked,
3204 * and this procedure reschedules itself.
3205 *
3206 *----------------------------------------------------------------------
3207 */
3208
3209 static void
3210 EntryBlinkProc(clientData)
3211 ClientData clientData; /* Pointer to record describing entry. */
3212 {
3213 Entry *entryPtr = (Entry *) clientData;
3214
3215 if ((entryPtr->state == STATE_DISABLED) ||
3216 (entryPtr->state == STATE_READONLY) ||
3217 !(entryPtr->flags & GOT_FOCUS) || (entryPtr->insertOffTime == 0)) {
3218 return;
3219 }
3220 if (entryPtr->flags & CURSOR_ON) {
3221 entryPtr->flags &= ~CURSOR_ON;
3222 entryPtr->insertBlinkHandler = Tcl_CreateTimerHandler(
3223 entryPtr->insertOffTime, EntryBlinkProc, (ClientData) entryPtr);
3224 } else {
3225 entryPtr->flags |= CURSOR_ON;
3226 entryPtr->insertBlinkHandler = Tcl_CreateTimerHandler(
3227 entryPtr->insertOnTime, EntryBlinkProc, (ClientData) entryPtr);
3228 }
3229 EventuallyRedraw(entryPtr);
3230 }
3231
3232 /*
3233 *----------------------------------------------------------------------
3234 *
3235 * EntryFocusProc --
3236 *
3237 * This procedure is called whenever the entry gets or loses the
3238 * input focus. It's also called whenever the window is reconfigured
3239 * while it has the focus.
3240 *
3241 * Results:
3242 * None.
3243 *
3244 * Side effects:
3245 * The cursor gets turned on or off.
3246 *
3247 *----------------------------------------------------------------------
3248 */
3249
3250 static void
3251 EntryFocusProc(entryPtr, gotFocus)
3252 Entry *entryPtr; /* Entry that got or lost focus. */
3253 int gotFocus; /* 1 means window is getting focus, 0 means
3254 * it's losing it. */
3255 {
3256 Tcl_DeleteTimerHandler(entryPtr->insertBlinkHandler);
3257 if (gotFocus) {
3258 entryPtr->flags |= GOT_FOCUS | CURSOR_ON;
3259 if (entryPtr->insertOffTime != 0) {
3260 entryPtr->insertBlinkHandler = Tcl_CreateTimerHandler(
3261 entryPtr->insertOnTime, EntryBlinkProc,
3262 (ClientData) entryPtr);
3263 }
3264 if (entryPtr->validate == VALIDATE_ALL ||
3265 entryPtr->validate == VALIDATE_FOCUS ||
3266 entryPtr->validate == VALIDATE_FOCUSIN) {
3267 EntryValidateChange(entryPtr, (char *) NULL,
3268 entryPtr->string, -1, VALIDATE_FOCUSIN);
3269 }
3270 } else {
3271 entryPtr->flags &= ~(GOT_FOCUS | CURSOR_ON);
3272 entryPtr->insertBlinkHandler = (Tcl_TimerToken) NULL;
3273 if (entryPtr->validate == VALIDATE_ALL ||
3274 entryPtr->validate == VALIDATE_FOCUS ||
3275 entryPtr->validate == VALIDATE_FOCUSOUT) {
3276 EntryValidateChange(entryPtr, (char *) NULL,
3277 entryPtr->string, -1, VALIDATE_FOCUSOUT);
3278 }
3279 }
3280 EventuallyRedraw(entryPtr);
3281 }
3282
3283 /*
3284 *--------------------------------------------------------------
3285 *
3286 * EntryTextVarProc --
3287 *
3288 * This procedure is invoked when someone changes the variable
3289 * whose contents are to be displayed in an entry.
3290 *
3291 * Results:
3292 * NULL is always returned.
3293 *
3294 * Side effects:
3295 * The text displayed in the entry will change to match the
3296 * variable.
3297 *
3298 *--------------------------------------------------------------
3299 */
3300
3301 /* ARGSUSED */
3302 static char *
3303 EntryTextVarProc(clientData, interp, name1, name2, flags)
3304 ClientData clientData; /* Information about button. */
3305 Tcl_Interp *interp; /* Interpreter containing variable. */
3306 Tcl_Obj *name1; /* Not used. */
3307 CONST char *name2; /* Not used. */
3308 int flags; /* Information about what happened. */
3309 {
3310 Entry *entryPtr = (Entry *) clientData;
3311 CONST char *value;
3312
3313 if (entryPtr->flags & ENTRY_DELETED) {
3314 /*
3315 * Just abort early if we entered here while being deleted.
3316 */
3317 return (char *) NULL;
3318 }
3319
3320 /*
3321 * If the variable is unset, then immediately recreate it unless
3322 * the whole interpreter is going away.
3323 */
3324
3325 if (flags & TCL_TRACE_UNSETS) {
3326 if ((flags & TCL_TRACE_DESTROYED) && !(flags & TCL_INTERP_DESTROYED)) {
3327 Tcl_Obj *temp = Tcl_NewStringObj(entryPtr->string,-1);
3328 Tcl_ObjSetVar2(entryPtr->interp, entryPtr->textVarName, NULL,
3329 temp, TCL_GLOBAL_ONLY);
3330 Tcl_DecrRefCount(temp);
3331 Lang_TraceVar(interp, entryPtr->textVarName,
3332 TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
3333 EntryTextVarProc, clientData);
3334 entryPtr->flags |= ENTRY_VAR_TRACED;
3335 }
3336 return (char *) NULL;
3337 }
3338
3339 /*
3340 * Update the entry's text with the value of the variable, unless
3341 * the entry already has that value (this happens when the variable
3342 * changes value because we changed it because someone typed in
3343 * the entry).
3344 */
3345
3346 value = Tcl_GetString(Tcl_ObjGetVar2(interp, entryPtr->textVarName, NULL, TCL_GLOBAL_ONLY));
3347 if (value == NULL) {
3348 value = "";
3349 }
3350 EntrySetValue(entryPtr, value);
3351 return (char *) NULL;
3352 }
3353
3354 /*
3355 *--------------------------------------------------------------
3356 *
3357 * EntryValidate --
3358 *
3359 * This procedure is invoked when any character is added or
3360 * removed from the entry widget, or a focus has trigerred validation.
3361 *
3362 * Results:
3363 * TCL_OK if the validatecommand passes the new string.
3364 * TCL_BREAK if the vcmd executed OK, but rejects the string.
3365 * TCL_ERROR if an error occurred while executing the vcmd
3366 * or a valid Tcl_Bool is not returned.
3367 *
3368 * Side effects:
3369 * An error condition may arise
3370 *
3371 *--------------------------------------------------------------
3372 */
3373
3374 #ifndef _LANG
3375 static int
3376 EntryValidate(entryPtr, cmd, string)
3377 register Entry *entryPtr; /* Entry that needs validation. */
3378 register LangCallback *cmd; /* Validation command (NULL-terminated
3379 * string). */
3380 char *string;
3381 {
3382 register Tcl_Interp *interp = entryPtr->interp;
3383 int code, bool;
3384
3385 code = LangDoCallback(entryPtr->interp, cmd, 1, 1, "%s", string);
3386
3387 /*
3388 * We accept TCL_OK and TCL_RETURN as valid return codes from the
3389 * command callback.
3390 */
3391 if (code != TCL_OK && code != TCL_RETURN) {
3392 Tcl_AddErrorInfo(interp, "\n\t(in validation command executed by ");
3393 Tcl_AddErrorInfo(interp, Tk_PathName(entryPtr->tkwin));
3394 Tcl_AddErrorInfo(interp, ")");
3395 Tcl_BackgroundError(interp);
3396 return TCL_ERROR;
3397 }
3398
3399 /*
3400 * The command callback should return an acceptable Tcl boolean.
3401 */
3402 if (Tcl_GetBooleanFromObj(interp, Tcl_GetObjResult(interp),
3403 &bool) != TCL_OK) {
3404 Tcl_AddErrorInfo(interp,
3405 "\nvalid boolean not returned by validation command");
3406 Tcl_BackgroundError(interp);
3407 Tcl_SetResult(interp, NULL, 0);
3408 return TCL_ERROR;
3409 }
3410
3411 Tcl_SetResult(interp, NULL, 0);
3412 return (bool ? TCL_OK : TCL_BREAK);
3413 }
3414 #endif
3415
3416 /*
3417 *--------------------------------------------------------------
3418 *
3419 * EntryValidateChange --
3420 *
3421 * This procedure is invoked when any character is added or
3422 * removed from the entry widget, or a focus has trigerred validation.
3423 *
3424 * Results:
3425 * TCL_OK if the validatecommand accepts the new string,
3426 * TCL_ERROR if any problems occured with validatecommand.
3427 *
3428 * Side effects:
3429 * The insertion/deletion may be aborted, and the
3430 * validatecommand might turn itself off (if an error
3431 * or loop condition arises).
3432 *
3433 *--------------------------------------------------------------
3434 */
3435
3436 static int
3437 EntryValidateChange(entryPtr, change, new, index, type)
3438 register Entry *entryPtr; /* Entry that needs validation. */
3439 char *change; /* Characters to be added/deleted
3440 * (NULL-terminated string). */
3441 CONST char *new; /* Potential new value of entry string */
3442 int index; /* index of insert/delete, -1 otherwise */
3443 int type; /* forced, delete, insert,
3444 * focusin or focusout */
3445 {
3446 int code, varValidate = (entryPtr->flags & VALIDATE_VAR);
3447 char *p;
3448 Tcl_Obj *result;
3449 Tcl_DString script;
3450
3451 if (entryPtr->validateCmd == NULL ||
3452 entryPtr->validate == VALIDATE_NONE) {
3453 if (entryPtr->flags & VALIDATING) {
3454 entryPtr->flags |= VALIDATE_ABORT;
3455 }
3456 return (varValidate ? TCL_ERROR : TCL_OK);
3457 }
3458
3459 /*
3460 * If we're already validating, then we're hitting a loop condition. Set
3461 * validate to none to disallow further validations, arrange for flags
3462 * to prevent current validation from finishing, and return.
3463 */
3464
3465 if (entryPtr->flags & VALIDATING) {
3466 entryPtr->validate = VALIDATE_NONE;
3467 entryPtr->flags |= VALIDATE_ABORT;
3468 return (varValidate ? TCL_ERROR : TCL_OK);
3469 }
3470
3471 entryPtr->flags |= VALIDATING;
3472
3473 /*
3474 * Now form command string and run through the -validatecommand
3475 */
3476
3477 #ifndef _LANG
3478 Tcl_DStringInit(&script);
3479 ExpandPercents(entryPtr, entryPtr->validateCmd,
3480 change, new, index, type, &script);
3481 Tcl_DStringAppend(&script, "", 1);
3482
3483 p = Tcl_DStringValue(&script);
3484 code = EntryValidate(entryPtr, p);
3485 Tcl_DStringFree(&script);
3486 #else
3487
3488 code = LangDoCallback(entryPtr->interp, entryPtr->validateCmd, 1, 5, "%s %s %s %d %d",
3489 new, change, entryPtr->string, index, type);
3490 if (code != TCL_OK && code != TCL_RETURN) {
3491 Tcl_AddErrorInfo(entryPtr->interp,
3492 "\n\t(in validation command executed by entry)");
3493 Tcl_BackgroundError(entryPtr->interp);
3494 goto done;
3495 }
3496
3497 result = Tcl_GetObjResult(entryPtr->interp);
3498
3499 if (Tcl_GetBooleanFromObj(entryPtr->interp, result, &code) != TCL_OK) {
3500 Tcl_AddErrorInfo(entryPtr->interp,
3501 "\nValid Tcl Boolean not returned by validation command");
3502 Tcl_BackgroundError(entryPtr->interp);
3503 Tcl_SetResult(entryPtr->interp, (char *) NULL, TCL_STATIC);
3504 code = TCL_ERROR;
3505 goto done;
3506 }
3507
3508 Tcl_ResetResult(entryPtr->interp);
3509 code = (code) ? TCL_OK : TCL_BREAK;
3510
3511 #endif
3512
3513 /*
3514 * If e->validate has become VALIDATE_NONE during the validation, or
3515 * we now have VALIDATE_VAR set (from EntrySetValue) and didn't before,
3516 * it means that a loop condition almost occured. Do not allow
3517 * this validation result to finish.
3518 */
3519
3520 if (entryPtr->validate == VALIDATE_NONE
3521 || (!varValidate && (entryPtr->flags & VALIDATE_VAR))) {
3522 code = TCL_ERROR;
3523 }
3524
3525 /*
3526 * It's possible that the user deleted the entry during validation.
3527 * In that case, abort future validation and return an error.
3528 */
3529
3530 if (entryPtr->flags & ENTRY_DELETED) {
3531 return TCL_ERROR;
3532 }
3533
3534 /*
3535 * If validate will return ERROR, then disallow further validations
3536 * Otherwise, if it didn't accept the new string (returned TCL_BREAK)
3537 * then eval the invalidCmd (if it's set)
3538 */
3539
3540 if (code == TCL_ERROR) {
3541 entryPtr->validate = VALIDATE_NONE;
3542 } else if (code == TCL_BREAK) {
3543 /*
3544 * If we were doing forced validation (like via a variable
3545 * trace) and the command returned 0, the we turn off validation
3546 * because we assume that textvariables have precedence in
3547 * managing the value. We also don't call the invcmd, as it
3548 * may want to do entry manipulation which the setting of the
3549 * var will later wipe anyway.
3550 */
3551
3552 if (varValidate) {
3553 entryPtr->validate = VALIDATE_NONE;
3554 } else if (entryPtr->invalidCmd != NULL) {
3555 #ifndef _LANG
3556 Tcl_DStringInit(&script);
3557 ExpandPercents(entryPtr, entryPtr->invalidCmd,
3558 change, new, index, type, &script);
3559 Tcl_DStringAppend(&script, "", 1);
3560 p = Tcl_DStringValue(&script);
3561 if (Tcl_EvalEx(entryPtr->interp, p, -1,
3562 TCL_EVAL_GLOBAL | TCL_EVAL_DIRECT) != TCL_OK) {
3563 Tcl_AddErrorInfo(entryPtr->interp,
3564 "\n\t(in invalidcommand executed by entry)");
3565 Tcl_BackgroundError(entryPtr->interp);
3566 code = TCL_ERROR;
3567 entryPtr->validate = VALIDATE_NONE;
3568 }
3569 Tcl_DStringFree(&script);
3570
3571 /*
3572 * It's possible that the user deleted the entry during validation.
3573 * In that case, abort future validation and return an error.
3574 */
3575
3576 if (entryPtr->flags & ENTRY_DELETED) {
3577 return TCL_ERROR;
3578 }
3579 #else
3580 if (LangDoCallback(entryPtr->interp, entryPtr->invalidCmd, 1, 5, "%s %s %s %d %d",
3581 new, change, entryPtr->string, index, type) != TCL_OK) {
3582 Tcl_AddErrorInfo(entryPtr->interp,
3583 "\n\t(in invalidcommand executed by entry)");
3584 Tcl_BackgroundError(entryPtr->interp);
3585 code = TCL_ERROR;
3586 entryPtr->validate = VALIDATE_NONE;
3587 }
3588 #endif
3589 }
3590 }
3591
3592 done:
3593 entryPtr->flags &= ~VALIDATING;
3594 return code;
3595 }
3596
3597 /*
3598 *--------------------------------------------------------------
3599 *
3600 * ExpandPercents --
3601 *
3602 * Given a command and an event, produce a new command
3603 * by replacing % constructs in the original command
3604 * with information from the X event.
3605 *
3606 * Results:
3607 * The new expanded command is appended to the dynamic string
3608 * given by dsPtr.
3609 *
3610 * Side effects:
3611 * None.
3612 *
3613 *--------------------------------------------------------------
3614 */
3615
3616 #ifndef _LANG
3617 static void
3618 ExpandPercents(entryPtr, before, change, new, index, type, dsPtr)
3619 register Entry *entryPtr; /* Entry that needs validation. */
3620 register CONST char *before;
3621 /* Command containing percent
3622 * expressions to be replaced. */
3623 char *change; /* Characters to added/deleted
3624 * (NULL-terminated string). */
3625 CONST char *new; /* Potential new value of entry string */
3626 int index; /* index of insert/delete */
3627 int type; /* INSERT or DELETE */
3628 Tcl_DString *dsPtr; /* Dynamic string in which to append
3629 * new command. */
3630 {
3631 int spaceNeeded, cvtFlags; /* Used to substitute string as proper Tcl
3632 * list element. */
3633 int number, length;
3634 register CONST char *string;
3635 Tcl_UniChar ch;
3636 char numStorage[2*TCL_INTEGER_SPACE];
3637
3638 while (1) {
3639 if (*before == '\0') {
3640 break;
3641 }
3642 /*
3643 * Find everything up to the next % character and append it
3644 * to the result string.
3645 */
3646
3647 string = before;
3648 /* No need to convert '%', as it is in ascii range */
3649 string = Tcl_UtfFindFirst(before, '%');
3650 if (string == (char *) NULL) {
3651 Tcl_DStringAppend(dsPtr, before, -1);
3652 break;
3653 } else if (string != before) {
3654 Tcl_DStringAppend(dsPtr, before, string-before);
3655 before = string;
3656 }
3657
3658 /*
3659 * There's a percent sequence here. Process it.
3660 */
3661
3662 before++; /* skip over % */
3663 if (*before != '\0') {
3664 before += Tcl_UtfToUniChar(before, &ch);
3665 } else {
3666 ch = '%';
3667 }
3668 if (type == VALIDATE_BUTTON) {
3669 /*
3670 * -command %-substitution
3671 */
3672 switch (ch) {
3673 case 's': /* Current string value of spinbox */
3674 string = entryPtr->string;
3675 break;
3676 case 'd': /* direction, up or down */
3677 string = change;
3678 break;
3679 case 'W': /* widget name */
3680 string = Tk_PathName(entryPtr->tkwin);
3681 break;
3682 default:
3683 length = Tcl_UniCharToUtf(ch, numStorage);
3684 numStorage[length] = '\0';
3685 string = numStorage;
3686 break;
3687 }
3688 } else {
3689 /*
3690 * -validatecommand / -invalidcommand %-substitution
3691 */
3692 switch (ch) {
3693 case 'd': /* Type of call that caused validation */
3694 switch (type) {
3695 case VALIDATE_INSERT:
3696 number = 1;
3697 break;
3698 case VALIDATE_DELETE:
3699 number = 0;
3700 break;
3701 default:
3702 number = -1;
3703 break;
3704 }
3705 sprintf(numStorage, "%d", number);
3706 string = numStorage;
3707 break;
3708 case 'i': /* index of insert/delete */
3709 sprintf(numStorage, "%d", index);
3710 string = numStorage;
3711 break;
3712 case 'P': /* 'Peeked' new value of the string */
3713 string = new;
3714 break;
3715 case 's': /* Current string value of spinbox */
3716 string = entryPtr->string;
3717 break;
3718 case 'S': /* string to be inserted/deleted, if any */
3719 string = change;
3720 break;
3721 case 'v': /* type of validation currently set */
3722 string = validateStrings[entryPtr->validate];
3723 break;
3724 case 'V': /* type of validation in effect */
3725 switch (type) {
3726 case VALIDATE_INSERT:
3727 case VALIDATE_DELETE:
3728 string = validateStrings[VALIDATE_KEY];
3729 break;
3730 case VALIDATE_FORCED:
3731 string = "forced";
3732 break;
3733 default:
3734 string = validateStrings[type];
3735 break;
3736 }
3737 break;
3738 case 'W': /* widget name */
3739 string = Tk_PathName(entryPtr->tkwin);
3740 break;
3741 default:
3742 length = Tcl_UniCharToUtf(ch, numStorage);
3743 numStorage[length] = '\0';
3744 string = numStorage;
3745 break;
3746 }
3747 }
3748
3749 spaceNeeded = Tcl_ScanCountedElement(string, -1, &cvtFlags);
3750 length = Tcl_DStringLength(dsPtr);
3751 Tcl_DStringSetLength(dsPtr, length + spaceNeeded);
3752 spaceNeeded = Tcl_ConvertCountedElement(string, -1,
3753 Tcl_DStringValue(dsPtr) + length,
3754 cvtFlags | TCL_DONT_USE_BRACES);
3755 Tcl_DStringSetLength(dsPtr, length + spaceNeeded);
3756 }
3757 }
3758
3759 #endif
3760 /*
3761 *--------------------------------------------------------------
3762 *
3763 * Tk_SpinboxObjCmd --
3764 *
3765 * This procedure is invoked to process the "spinbox" Tcl
3766 * command. See the user documentation for details on what
3767 * it does.
3768 *
3769 * Results:
3770 * A standard Tcl result.
3771 *
3772 * Side effects:
3773 * See the user documentation.
3774 *
3775 *--------------------------------------------------------------
3776 */
3777
3778 int
3779 Tk_SpinboxObjCmd(clientData, interp, objc, objv)
3780 ClientData clientData; /* NULL. */
3781 Tcl_Interp *interp; /* Current interpreter. */
3782 int objc; /* Number of arguments. */
3783 Tcl_Obj *CONST objv[]; /* Argument objects. */
3784 {
3785 register Entry *entryPtr;
3786 register Spinbox *sbPtr;
3787 Tk_OptionTable optionTable;
3788 Tk_Window tkwin;
3789 char *tmp;
3790
3791 if (objc < 2) {
3792 Tcl_WrongNumArgs(interp, 1, objv, "pathName ?options?");
3793 return TCL_ERROR;
3794 }
3795
3796 tkwin = Tk_CreateWindowFromPath(interp, Tk_MainWindow(interp),
3797 Tcl_GetString(objv[1]), (char *) NULL);
3798 if (tkwin == NULL) {
3799 return TCL_ERROR;
3800 }
3801
3802 /*
3803 * Create the option table for this widget class. If it has already
3804 * been created, Tk will return the cached value.
3805 */
3806
3807 optionTable = Tk_CreateOptionTable(interp, sbOptSpec);
3808
3809 /*
3810 * Initialize the fields of the structure that won't be initialized
3811 * by ConfigureEntry, or that ConfigureEntry requires to be
3812 * initialized already (e.g. resource pointers). Only the non-NULL/0
3813 * data must be initialized as memset covers the rest.
3814 */
3815
3816 sbPtr = (Spinbox *) ckalloc(sizeof(Spinbox));
3817 entryPtr = (Entry *) sbPtr;
3818 memset((VOID *) sbPtr, 0, sizeof(Spinbox));
3819
3820 entryPtr->tkwin = tkwin;
3821 entryPtr->display = Tk_Display(tkwin);
3822 entryPtr->interp = interp;
3823 entryPtr->widgetCmd = Tcl_CreateObjCommand(interp,
3824 Tk_PathName(entryPtr->tkwin), SpinboxWidgetObjCmd,
3825 (ClientData) sbPtr, EntryCmdDeletedProc);
3826 entryPtr->optionTable = optionTable;
3827 entryPtr->type = TK_SPINBOX;
3828 tmp = (char *) ckalloc(1);
3829 tmp[0] = '\0';
3830 entryPtr->string = tmp;
3831 entryPtr->selectFirst = -1;
3832 entryPtr->selectLast = -1;
3833
3834 entryPtr->cursor = None;
3835 entryPtr->exportSelection = 1;
3836 entryPtr->justify = TK_JUSTIFY_LEFT;
3837 entryPtr->relief = TK_RELIEF_FLAT;
3838 entryPtr->state = STATE_NORMAL;
3839 entryPtr->displayString = entryPtr->string;
3840 entryPtr->inset = XPAD;
3841 entryPtr->textGC = None;
3842 entryPtr->selTextGC = None;
3843 entryPtr->highlightGC = None;
3844 entryPtr->avgWidth = 1;
3845 entryPtr->validate = VALIDATE_NONE;
3846
3847 sbPtr->selElement = SEL_NONE;
3848 sbPtr->curElement = SEL_NONE;
3849 sbPtr->bCursor = None;
3850 sbPtr->repeatDelay = 400;
3851 sbPtr->repeatInterval = 100;
3852 sbPtr->fromValue = 0.0;
3853 sbPtr->toValue = 100.0;
3854 sbPtr->increment = 1.0;
3855 sbPtr->formatBuf = (char *) ckalloc(TCL_DOUBLE_SPACE);
3856 sbPtr->bdRelief = TK_RELIEF_FLAT;
3857 sbPtr->buRelief = TK_RELIEF_FLAT;
3858
3859 /*
3860 * Keep a hold of the associated tkwin until we destroy the listbox,
3861 * otherwise Tk might free it while we still need it.
3862 */
3863
3864 Tcl_Preserve((ClientData) entryPtr->tkwin);
3865
3866 Tk_SetClass(entryPtr->tkwin, "Spinbox");
3867 Tk_SetClassProcs(entryPtr->tkwin, &entryClass, (ClientData) entryPtr);
3868 Tk_CreateEventHandler(entryPtr->tkwin,
3869 PointerMotionMask|ExposureMask|StructureNotifyMask|FocusChangeMask,
3870 EntryEventProc, (ClientData) entryPtr);
3871 Tk_CreateSelHandler(entryPtr->tkwin, XA_PRIMARY, XA_STRING,
3872 EntryFetchSelection, (ClientData) entryPtr, XA_STRING);
3873
3874 if (Tk_InitOptions(interp, (char *) sbPtr, optionTable, tkwin)
3875 != TCL_OK) {
3876 Tk_DestroyWindow(entryPtr->tkwin);
3877 return TCL_ERROR;
3878 }
3879 if (ConfigureEntry(interp, entryPtr, objc-2, objv+2, 0) != TCL_OK) {
3880 goto error;
3881 }
3882
3883 Tcl_SetResult(interp, Tk_PathName(entryPtr->tkwin), TCL_STATIC);
3884 return TCL_OK;
3885
3886 error:
3887 Tk_DestroyWindow(entryPtr->tkwin);
3888 return TCL_ERROR;
3889 }
3890
3891 /*
3892 *--------------------------------------------------------------
3893 *
3894 * SpinboxWidgetObjCmd --
3895 *
3896 * This procedure is invoked to process the Tcl command
3897 * that corresponds to a widget managed by this module.
3898 * See the user documentation for details on what it does.
3899 *
3900 * Results:
3901 * A standard Tcl result.
3902 *
3903 * Side effects:
3904 * See the user documentation.
3905 *
3906 *--------------------------------------------------------------
3907 */
3908
3909 static int
3910 SpinboxWidgetObjCmd(clientData, interp, objc, objv)
3911 ClientData clientData; /* Information about spinbox widget. */
3912 Tcl_Interp *interp; /* Current interpreter. */
3913 int objc; /* Number of arguments. */
3914 Tcl_Obj *CONST objv[]; /* Argument objects. */
3915 {
3916 Entry *entryPtr = (Entry *) clientData;
3917 Spinbox *sbPtr = (Spinbox *) clientData;
3918 int cmdIndex, selIndex, result;
3919 Tcl_Obj *objPtr;
3920
3921 if (objc < 2) {
3922 Tcl_WrongNumArgs(interp, 1, objv, "option ?arg arg ...?");
3923 return TCL_ERROR;
3924 }
3925
3926 /*
3927 * Parse the widget command by looking up the second token in
3928 * the list of valid command names.
3929 */
3930
3931 result = Tcl_GetIndexFromObj(interp, objv[1], sbCmdNames,
3932 "option", 0, &cmdIndex);
3933 if (result != TCL_OK) {
3934 return result;
3935 }
3936
3937 Tcl_Preserve((ClientData) entryPtr);
3938 switch ((enum sbCmd) cmdIndex) {
3939 case SB_CMD_BBOX: {
3940 int index, x, y, width, height;
3941 char buf[TCL_INTEGER_SPACE * 4];
3942
3943 if (objc != 3) {
3944 Tcl_WrongNumArgs(interp, 2, objv, "index");
3945 goto error;
3946 }
3947 if (GetEntryIndex(interp, entryPtr, objv[2],
3948 &index) != TCL_OK) {
3949 goto error;
3950 }
3951 if ((index == entryPtr->numChars) && (index > 0)) {
3952 index--;
3953 }
3954 Tk_CharBbox(entryPtr->textLayout, index, &x, &y,
3955 &width, &height);
3956 Tcl_IntResults(interp, 4, 0,
3957 x + entryPtr->layoutX,
3958 y + entryPtr->layoutY, width, height);
3959 break;
3960 }
3961
3962 case SB_CMD_CGET: {
3963 if (objc != 3) {
3964 Tcl_WrongNumArgs(interp, 2, objv, "option");
3965 goto error;
3966 }
3967
3968 objPtr = Tk_GetOptionValue(interp, (char *) entryPtr,
3969 entryPtr->optionTable, objv[2], entryPtr->tkwin);
3970 if (objPtr == NULL) {
3971 goto error;
3972 } else {
3973 Tcl_SetObjResult(interp, objPtr);
3974 }
3975 break;
3976 }
3977
3978 case SB_CMD_CONFIGURE: {
3979 if (objc <= 3) {
3980 objPtr = Tk_GetOptionInfo(interp, (char *) entryPtr,
3981 entryPtr->optionTable,
3982 (objc == 3) ? objv[2] : (Tcl_Obj *) NULL,
3983 entryPtr->tkwin);
3984 if (objPtr == NULL) {
3985 goto error;
3986 } else {
3987 Tcl_SetObjResult(interp, objPtr);
3988 }
3989 } else {
3990 result = ConfigureEntry(interp, entryPtr, objc-2, objv+2, 0);
3991 }
3992 break;
3993 }
3994
3995 case SB_CMD_DELETE: {
3996 int first, last;
3997
3998 if ((objc < 3) || (objc > 4)) {
3999 Tcl_WrongNumArgs(interp, 2, objv, "firstIndex ?lastIndex?");
4000 goto error;
4001 }
4002 if (GetEntryIndex(interp, entryPtr, objv[2],
4003 &first) != TCL_OK) {
4004 goto error;
4005 }
4006 if (objc == 3) {
4007 last = first + 1;
4008 } else {
4009 if (GetEntryIndex(interp, entryPtr, objv[3],
4010 &last) != TCL_OK) {
4011 goto error;
4012 }
4013 }
4014 if ((last >= first) && (entryPtr->state == STATE_NORMAL)) {
4015 DeleteChars(entryPtr, first, last - first);
4016 }
4017 break;
4018 }
4019
4020 case SB_CMD_GET: {
4021 if (objc != 2) {
4022 Tcl_WrongNumArgs(interp, 2, objv, (char *) NULL);
4023 goto error;
4024 }
4025 Tcl_SetStringObj(Tcl_GetObjResult(interp), entryPtr->string, -1);
4026 break;
4027 }
4028
4029 case SB_CMD_ICURSOR: {
4030 if (objc != 3) {
4031 Tcl_WrongNumArgs(interp, 2, objv, "pos");
4032 goto error;
4033 }
4034 if (GetEntryIndex(interp, entryPtr, objv[2],
4035 &entryPtr->insertPos) != TCL_OK) {
4036 goto error;
4037 }
4038 EventuallyRedraw(entryPtr);
4039 break;
4040 }
4041
4042 case SB_CMD_IDENTIFY: {
4043 int x, y, elem;
4044
4045 if (objc != 4) {
4046 Tcl_WrongNumArgs(interp, 2, objv, "x y");
4047 goto error;
4048 }
4049 if ((Tcl_GetIntFromObj(interp, objv[2], &x) != TCL_OK) ||
4050 (Tcl_GetIntFromObj(interp, objv[3], &y) != TCL_OK)) {
4051 goto error;
4052 }
4053 elem = GetSpinboxElement(sbPtr, x, y);
4054 if (elem != SEL_NONE) {
4055 Tcl_SetStringObj(Tcl_GetObjResult(interp),
4056 selElementNames[elem], -1);
4057 }
4058 break;
4059 }
4060
4061 case SB_CMD_INDEX: {
4062 int index;
4063
4064 if (objc != 3) {
4065 Tcl_WrongNumArgs(interp, 2, objv, "string");
4066 goto error;
4067 }
4068 if (GetEntryIndex(interp, entryPtr, objv[2],
4069 &index) != TCL_OK) {
4070 goto error;
4071 }
4072 Tcl_SetObjResult(interp, Tcl_NewIntObj(index));
4073 break;
4074 }
4075
4076 case SB_CMD_INSERT: {
4077 int index;
4078
4079 if (objc != 4) {
4080 Tcl_WrongNumArgs(interp, 2, objv, "index text");
4081 goto error;
4082 }
4083 if (GetEntryIndex(interp, entryPtr, objv[2],
4084 &index) != TCL_OK) {
4085 goto error;
4086 }
4087 if (entryPtr->state == STATE_NORMAL) {
4088 InsertChars(entryPtr, index, Tcl_GetString(objv[3]));
4089 }
4090 break;
4091 }
4092
4093 case SB_CMD_INVOKE: {
4094 if (objc != 3) {
4095 Tcl_WrongNumArgs(interp, 2, objv, "elemName");
4096 goto error;
4097 }
4098 result = Tcl_GetIndexFromObj(interp, objv[2],
4099 selElementNames, "element", 0, &cmdIndex);
4100 if (result != TCL_OK) {
4101 goto error;
4102 }
4103 if (entryPtr->state != STATE_DISABLED) {
4104 if (SpinboxInvoke(interp, sbPtr, cmdIndex) != TCL_OK) {
4105 goto error;
4106 }
4107 }
4108 break;
4109 }
4110
4111 case SB_CMD_SCAN: {
4112 int x;
4113 char *minorCmd;
4114
4115 if (objc != 4) {
4116 Tcl_WrongNumArgs(interp, 2, objv, "mark|dragto x");
4117 goto error;
4118 }
4119 if (Tcl_GetIntFromObj(interp, objv[3], &x) != TCL_OK) {
4120 goto error;
4121 }
4122
4123 minorCmd = Tcl_GetString(objv[2]);
4124 if (minorCmd[0] == 'm'
4125 && (strncmp(minorCmd, "mark", strlen(minorCmd)) == 0)) {
4126 entryPtr->scanMarkX = x;
4127 entryPtr->scanMarkIndex = entryPtr->leftIndex;
4128 } else if ((minorCmd[0] == 'd')
4129 && (strncmp(minorCmd, "dragto", strlen(minorCmd)) == 0)) {
4130 EntryScanTo(entryPtr, x);
4131 } else {
4132 Tcl_AppendResult(interp, "bad scan option \"",
4133 Tcl_GetString(objv[2]), "\": must be mark or dragto",
4134 (char *) NULL);
4135 goto error;
4136 }
4137 break;
4138 }
4139
4140 case SB_CMD_SELECTION: {
4141 int index, index2;
4142
4143 if (objc < 3) {
4144 Tcl_WrongNumArgs(interp, 2, objv, "option ?index?");
4145 goto error;
4146 }
4147
4148 /*
4149 * Parse the selection sub-command, using the command
4150 * table "sbSelCmdNames" defined above.
4151 */
4152
4153 result = Tcl_GetIndexFromObj(interp, objv[2], sbSelCmdNames,
4154 "selection option", 0, &selIndex);
4155 if (result != TCL_OK) {
4156 goto error;
4157 }
4158
4159 /*
4160 * Disabled entries don't allow the selection to be modified,
4161 * but 'selection present' must return a boolean.
4162 */
4163
4164 if ((entryPtr->state == STATE_DISABLED)
4165 && (selIndex != SB_SEL_PRESENT)) {
4166 goto done;
4167 }
4168
4169 switch (selIndex) {
4170 case SB_SEL_ADJUST: {
4171 if (objc != 4) {
4172 Tcl_WrongNumArgs(interp, 3, objv, "index");
4173 goto error;
4174 }
4175 if (GetEntryIndex(interp, entryPtr,
4176 objv[3], &index) != TCL_OK) {
4177 goto error;
4178 }
4179 if (entryPtr->selectFirst >= 0) {
4180 int half1, half2;
4181
4182 half1 = (entryPtr->selectFirst
4183 + entryPtr->selectLast)/2;
4184 half2 = (entryPtr->selectFirst
4185 + entryPtr->selectLast + 1)/2;
4186 if (index < half1) {
4187 entryPtr->selectAnchor = entryPtr->selectLast;
4188 } else if (index > half2) {
4189 entryPtr->selectAnchor = entryPtr->selectFirst;
4190 } else {
4191 /*
4192 * We're at about the halfway point in the
4193 * selection; just keep the existing anchor.
4194 */
4195 }
4196 }
4197 EntrySelectTo(entryPtr, index);
4198 break;
4199 }
4200
4201 case SB_SEL_CLEAR: {
4202 if (objc != 3) {
4203 Tcl_WrongNumArgs(interp, 3, objv, (char *) NULL);
4204 goto error;
4205 }
4206 if (entryPtr->selectFirst >= 0) {
4207 entryPtr->selectFirst = -1;
4208 entryPtr->selectLast = -1;
4209 EventuallyRedraw(entryPtr);
4210 }
4211 goto done;
4212 }
4213
4214 case SB_SEL_FROM: {
4215 if (objc != 4) {
4216 Tcl_WrongNumArgs(interp, 3, objv, "index");
4217 goto error;
4218 }
4219 if (GetEntryIndex(interp, entryPtr,
4220 objv[3], &index) != TCL_OK) {
4221 goto error;
4222 }
4223 entryPtr->selectAnchor = index;
4224 break;
4225 }
4226
4227 case SB_SEL_PRESENT: {
4228 if (objc != 3) {
4229 Tcl_WrongNumArgs(interp, 3, objv, (char *) NULL);
4230 goto error;
4231 }
4232 Tcl_SetObjResult(interp,
4233 Tcl_NewBooleanObj((entryPtr->selectFirst >= 0)));
4234 goto done;
4235 }
4236
4237 case SB_SEL_RANGE: {
4238 if (objc != 5) {
4239 Tcl_WrongNumArgs(interp, 3, objv, "start end");
4240 goto error;
4241 }
4242 if (GetEntryIndex(interp, entryPtr,
4243 objv[3], &index) != TCL_OK) {
4244 goto error;
4245 }
4246 if (GetEntryIndex(interp, entryPtr,
4247 objv[4],& index2) != TCL_OK) {
4248 goto error;
4249 }
4250 if (index >= index2) {
4251 entryPtr->selectFirst = -1;
4252 entryPtr->selectLast = -1;
4253 } else {
4254 entryPtr->selectFirst = index;
4255 entryPtr->selectLast = index2;
4256 }
4257 if (!(entryPtr->flags & GOT_SELECTION)
4258 && (entryPtr->exportSelection)) {
4259 Tk_OwnSelection(entryPtr->tkwin, XA_PRIMARY,
4260 EntryLostSelection, (ClientData) entryPtr);
4261 entryPtr->flags |= GOT_SELECTION;
4262 }
4263 EventuallyRedraw(entryPtr);
4264 break;
4265 }
4266
4267 case SB_SEL_TO: {
4268 if (objc != 4) {
4269 Tcl_WrongNumArgs(interp, 3, objv, "index");
4270 goto error;
4271 }
4272 if (GetEntryIndex(interp, entryPtr,
4273 objv[3], &index) != TCL_OK) {
4274 goto error;
4275 }
4276 EntrySelectTo(entryPtr, index);
4277 break;
4278 }
4279
4280 case SB_SEL_ELEMENT: {
4281 if ((objc < 3) || (objc > 4)) {
4282 Tcl_WrongNumArgs(interp, 3, objv, "?elemName?");
4283 goto error;
4284 }
4285 if (objc == 3) {
4286 Tcl_SetStringObj(Tcl_GetObjResult(interp),
4287 selElementNames[sbPtr->selElement], -1);
4288 } else {
4289 int lastElement = sbPtr->selElement;
4290
4291 result = Tcl_GetIndexFromObj(interp, objv[3],
4292 selElementNames, "selection element", 0,
4293 &(sbPtr->selElement));
4294 if (result != TCL_OK) {
4295 goto error;
4296 }
4297 if (lastElement != sbPtr->selElement) {
4298 EventuallyRedraw(entryPtr);
4299 }
4300 }
4301 break;
4302 }
4303 }
4304 break;
4305 }
4306
4307 case SB_CMD_SET: {
4308 if (objc > 3) {
4309 Tcl_WrongNumArgs(interp, 2, objv, "?string?");
4310 goto error;
4311 }
4312 if (objc == 3) {
4313 EntryValueChanged(entryPtr, Tcl_GetString(objv[2]));
4314 }
4315 Tcl_SetStringObj(Tcl_GetObjResult(interp), entryPtr->string, -1);
4316 break;
4317 }
4318
4319 case SB_CMD_VALIDATE: {
4320 int code;
4321
4322 if (objc != 2) {
4323 Tcl_WrongNumArgs(interp, 2, objv, (char *) NULL);
4324 goto error;
4325 }
4326 selIndex = entryPtr->validate;
4327 entryPtr->validate = VALIDATE_ALL;
4328 code = EntryValidateChange(entryPtr, (char *) NULL,
4329 entryPtr->string, -1, VALIDATE_FORCED);
4330 if (entryPtr->validate != VALIDATE_NONE) {
4331 entryPtr->validate = selIndex;
4332 }
4333 Tcl_SetObjResult(interp, Tcl_NewBooleanObj((code == TCL_OK)));
4334 break;
4335 }
4336
4337 case SB_CMD_XVIEW: {
4338 int index;
4339
4340 if (objc == 2) {
4341 double first, last;
4342 char buf[TCL_DOUBLE_SPACE * 2];
4343
4344 EntryVisibleRange(entryPtr, &first, &last);
4345 sprintf(buf, "%g %g", first, last);
4346 Tcl_SetResult(interp, buf, TCL_VOLATILE);
4347 goto done;
4348 } else if (objc == 3) {
4349 if (GetEntryIndex(interp, entryPtr, objv[2],
4350 &index) != TCL_OK) {
4351 goto error;
4352 }
4353 } else {
4354 double fraction;
4355 int count;
4356
4357 index = entryPtr->leftIndex;
4358 switch (Tk_GetScrollInfoObj(interp, objc, objv, &fraction,
4359 &count)) {
4360 case TK_SCROLL_ERROR: {
4361 goto error;
4362 }
4363 case TK_SCROLL_MOVETO: {
4364 index = (int) ((fraction * entryPtr->numChars) + 0.5);
4365 break;
4366 }
4367 case TK_SCROLL_PAGES: {
4368 int charsPerPage;
4369
4370 charsPerPage = ((Tk_Width(entryPtr->tkwin)
4371 - 2 * entryPtr->inset - entryPtr->xWidth)
4372 / entryPtr->avgWidth) - 2;
4373 if (charsPerPage < 1) {
4374 charsPerPage = 1;
4375 }
4376 index += count * charsPerPage;
4377 break;
4378 }
4379 case TK_SCROLL_UNITS: {
4380 index += count;
4381 break;
4382 }
4383 }
4384 }
4385 if (index >= entryPtr->numChars) {
4386 index = entryPtr->numChars - 1;
4387 }
4388 if (index < 0) {
4389 index = 0;
4390 }
4391 entryPtr->leftIndex = index;
4392 entryPtr->flags |= UPDATE_SCROLLBAR;
4393 EntryComputeGeometry(entryPtr);
4394 EventuallyRedraw(entryPtr);
4395 break;
4396 }
4397 }
4398
4399 done:
4400 Tcl_Release((ClientData) entryPtr);
4401 return result;
4402
4403 error:
4404 Tcl_Release((ClientData) entryPtr);
4405 return TCL_ERROR;
4406 }
4407
4408 /*
4409 *---------------------------------------------------------------------------
4410 *
4411 * GetSpinboxElement --
4412 *
4413 * Return the element associated with an x,y coord.
4414 *
4415 * Results:
4416 * Element type as enum selelement.
4417 *
4418 * Side effects:
4419 * None.
4420 *
4421 *---------------------------------------------------------------------------
4422 */
4423
4424 static int
4425 GetSpinboxElement(sbPtr, x, y)
4426 Spinbox *sbPtr; /* Spinbox for which the index is being
4427 * specified. */
4428 int x; /* x coord */
4429 int y; /* y coord */
4430 {
4431 Entry *entryPtr = (Entry *) sbPtr;
4432
4433 if ((x < 0) || (y < 0) || (y > Tk_Height(entryPtr->tkwin))
4434 || (x > Tk_Width(entryPtr->tkwin))) {
4435 return SEL_NONE;
4436 }
4437
4438 if (x > (Tk_Width(entryPtr->tkwin) - entryPtr->inset - entryPtr->xWidth)) {
4439 if (y > (Tk_Height(entryPtr->tkwin) / 2)) {
4440 return SEL_BUTTONDOWN;
4441 } else {
4442 return SEL_BUTTONUP;
4443 }
4444 }
4445 return SEL_ENTRY;
4446 }
4447
4448 /*
4449 *--------------------------------------------------------------
4450 *
4451 * SpinboxInvoke --
4452 *
4453 * This procedure is invoked when the invoke method for the
4454 * widget is called.
4455 *
4456 * Results:
4457 * TCL_OK.
4458 *
4459 * Side effects:
4460 * An background error condition may arise when invoking the
4461 * callback. The widget value may change.
4462 *
4463 *--------------------------------------------------------------
4464 */
4465
4466 static int
4467 SpinboxInvoke(interp, sbPtr, element)
4468 register Tcl_Interp *interp; /* Current interpreter. */
4469 register Spinbox *sbPtr; /* Spinbox to invoke. */
4470 int element; /* element to invoke, either the "up"
4471 * or "down" button. */
4472 {
4473 Entry *entryPtr = (Entry *) sbPtr;
4474 char *type;
4475 int code, up;
4476 Tcl_DString script;
4477
4478 switch (element) {
4479 case SEL_BUTTONUP:
4480 type = "up";
4481 up = 1;
4482 break;
4483 case SEL_BUTTONDOWN:
4484 type = "down";
4485 up = 0;
4486 break;
4487 default:
4488 return TCL_OK;
4489 }
4490
4491 if (fabs(sbPtr->increment) > MIN_DBL_VAL) {
4492 if (sbPtr->listObj != NULL) {
4493 Tcl_Obj *objPtr;
4494
4495 Tcl_ListObjIndex(interp, sbPtr->listObj, sbPtr->eIndex, &objPtr);
4496 if (strcmp(Tcl_GetString(objPtr), entryPtr->string)) {
4497 /*
4498 * Somehow the string changed from what we expected,
4499 * so let's do a search on the list to see if the current
4500 * value is there. If not, move to the first element of
4501 * the list.
4502 */
4503 int i, listc, elemLen, length = entryPtr->numChars;
4504 char *bytes;
4505 Tcl_Obj **listv;
4506
4507 Tcl_ListObjGetElements(interp, sbPtr->listObj, &listc, &listv);
4508 for (i = 0; i < listc; i++) {
4509 bytes = Tcl_GetStringFromObj(listv[i], &elemLen);
4510 if ((length == elemLen) &&
4511 (memcmp(bytes, entryPtr->string,
4512 (size_t) length) == 0)) {
4513 sbPtr->eIndex = i;
4514 break;
4515 }
4516 }
4517 }
4518 if (up) {
4519 if (++sbPtr->eIndex >= sbPtr->nElements) {
4520 if (sbPtr->wrap) {
4521 sbPtr->eIndex = 0;
4522 } else {
4523 sbPtr->eIndex = sbPtr->nElements-1;
4524 }
4525 }
4526 } else {
4527 if (--sbPtr->eIndex < 0) {
4528 if (sbPtr->wrap) {
4529 sbPtr->eIndex = sbPtr->nElements-1;
4530 } else {
4531 sbPtr->eIndex = 0;
4532 }
4533 }
4534 }
4535 Tcl_ListObjIndex(interp, sbPtr->listObj, sbPtr->eIndex, &objPtr);
4536 EntryValueChanged(entryPtr, Tcl_GetString(objPtr));
4537 } else if (!DOUBLES_EQ(sbPtr->fromValue, sbPtr->toValue)) {
4538 double dvalue;
4539
4540 if (Tcl_GetDouble(NULL, entryPtr->string, &dvalue) != TCL_OK) {
4541 /*
4542 * If the string is empty, or isn't a valid double value,
4543 * just use the -from value
4544 */
4545 dvalue = sbPtr->fromValue;
4546 } else {
4547 if (up) {
4548 dvalue += sbPtr->increment;
4549 if (dvalue > sbPtr->toValue) {
4550 if (sbPtr->wrap) {
4551 dvalue = sbPtr->fromValue;
4552 } else {
4553 dvalue = sbPtr->toValue;
4554 }
4555 } else if (dvalue < sbPtr->fromValue) {
4556 /*
4557 * It's possible that when pressing up, we are
4558 * still less than the fromValue, because the
4559 * user may have manipulated the value by hand.
4560 */
4561 dvalue = sbPtr->fromValue;
4562 }
4563 } else {
4564 dvalue -= sbPtr->increment;
4565 if (dvalue < sbPtr->fromValue) {
4566 if (sbPtr->wrap) {
4567 dvalue = sbPtr->toValue;
4568 } else {
4569 dvalue = sbPtr->fromValue;
4570 }
4571 } else if (dvalue > sbPtr->toValue) {
4572 /*
4573 * It's possible that when pressing down, we are
4574 * still greater than the toValue, because the
4575 * user may have manipulated the value by hand.
4576 */
4577 dvalue = sbPtr->toValue;
4578 }
4579 }
4580 }
4581 sprintf(sbPtr->formatBuf, sbPtr->valueFormat, dvalue);
4582 EntryValueChanged(entryPtr, sbPtr->formatBuf);
4583 }
4584 }
4585
4586 if (sbPtr->command != NULL) {
4587 #ifndef _LANG
4588 Tcl_DStringInit(&script);
4589 ExpandPercents(entryPtr, sbPtr->command, type, "", 0,
4590 VALIDATE_BUTTON, &script);
4591 Tcl_DStringAppend(&script, "", 1);
4592
4593 code = Tcl_EvalEx(interp, Tcl_DStringValue(&script), -1,
4594 TCL_EVAL_GLOBAL | TCL_EVAL_DIRECT);
4595 Tcl_DStringFree(&script);
4596 #else
4597 code = LangDoCallback(entryPtr->interp, sbPtr->command, 1, 2,
4598 " %s %s", entryPtr->string, type);
4599 #endif
4600
4601 if (code != TCL_OK) {
4602 Tcl_AddErrorInfo(interp, "\n\t(in command executed by spinbox)");
4603 Tcl_BackgroundError(interp);
4604 /*
4605 * Yes, it's an error, but a bg one, so we return OK
4606 */
4607 return TCL_OK;
4608 }
4609
4610 Tcl_SetResult(interp, NULL, 0);
4611 }
4612
4613 return TCL_OK;
4614 }
4615
4616 /*
4617 *----------------------------------------------------------------------
4618 *
4619 * ComputeFormat --
4620 *
4621 * This procedure is invoked to recompute the "format" fields
4622 * of a spinbox's widget record, which determines how the value
4623 * of the dial is converted to a string.
4624 *
4625 * Results:
4626 * Tcl result code.
4627 *
4628 * Side effects:
4629 * The format fields of the spinbox are modified.
4630 *
4631 *----------------------------------------------------------------------
4632 */
4633 static int
4634 ComputeFormat(sbPtr)
4635 Spinbox *sbPtr; /* Information about dial widget. */
4636 {
4637 double maxValue, x;
4638 int mostSigDigit, numDigits, leastSigDigit, afterDecimal;
4639 int eDigits, fDigits;
4640
4641 /*
4642 * Compute the displacement from the decimal of the most significant
4643 * digit required for any number in the dial's range.
4644 */
4645
4646 if (sbPtr->reqFormat) {
4647 sbPtr->valueFormat = sbPtr->reqFormat;
4648 return TCL_OK;
4649 }
4650
4651 maxValue = fabs(sbPtr->fromValue);
4652 x = fabs(sbPtr->toValue);
4653 if (x > maxValue) {
4654 maxValue = x;
4655 }
4656 if (maxValue == 0) {
4657 maxValue = 1;
4658 }
4659 mostSigDigit = (int) floor(log10(maxValue));
4660
4661 if (fabs(sbPtr->increment) > MIN_DBL_VAL) {
4662 /*
4663 * A increment was specified, so use it.
4664 */
4665 leastSigDigit = (int) floor(log10(sbPtr->increment));
4666 } else {
4667 leastSigDigit = 0;
4668 }
4669 numDigits = mostSigDigit - leastSigDigit + 1;
4670 if (numDigits < 1) {
4671 numDigits = 1;
4672 }
4673
4674 /*
4675 * Compute the number of characters required using "e" format and
4676 * "f" format, and then choose whichever one takes fewer characters.
4677 */
4678
4679 eDigits = numDigits + 4;
4680 if (numDigits > 1) {
4681 eDigits++; /* Decimal point. */
4682 }
4683 afterDecimal = numDigits - mostSigDigit - 1;
4684 if (afterDecimal < 0) {
4685 afterDecimal = 0;
4686 }
4687 fDigits = (mostSigDigit >= 0) ? mostSigDigit + afterDecimal : afterDecimal;
4688 if (afterDecimal > 0) {
4689 fDigits++; /* Decimal point. */
4690 }
4691 if (mostSigDigit < 0) {
4692 fDigits++; /* Zero to left of decimal point. */
4693 }
4694 if (fDigits <= eDigits) {
4695 sprintf(sbPtr->digitFormat, "%%.%df", afterDecimal);
4696 } else {
4697 sprintf(sbPtr->digitFormat, "%%.%de", numDigits-1);
4698 }
4699 sbPtr->valueFormat = sbPtr->digitFormat;
4700 return TCL_OK;
4701 }
4702
4703
4704
4705
4706
4707