Debian Codegraph @

pTk/mTk/generic/tkListbox.c

raw symbols SCIP
1 /*
2 * tkListbox.c --
3 *
4 * This module implements listbox widgets for the Tk
5 * toolkit. A listbox displays a collection of strings,
6 * one per line, and provides scrolling and selection.
7 *
8 * Copyright (c) 1990-1994 The Regents of the University of California.
9 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
10 *
11 * See the file "license.terms" for information on usage and redistribution
12 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 *
14 * RCS: @(#) $Id: tkListbox.c,v 1.29.2.1 2003/11/11 19:41:50 hobbs Exp $
15 */
16
17 #include "tkPort.h"
18 #include "default.h"
19 #include "tkInt.h"
20
21 #ifdef WIN32
22 #include "tkWinInt.h"
23 #endif
24
25 #include "tkVMacro.h"
26
27 typedef struct {
28 Tk_OptionTable listboxOptionTable; /* Table defining configuration options
29 * available for the listbox */
30 Tk_OptionTable itemAttrOptionTable; /* Table definining configuration
31 * options available for listbox
32 * items */
33 } ListboxOptionTables;
34
35 /*
36 * A data structure of the following type is kept for each listbox
37 * widget managed by this file:
38 */
39
40 typedef struct {
41 Tk_Window tkwin; /* Window that embodies the listbox. NULL
42 * means that the window has been destroyed
43 * but the data structures haven't yet been
44 * cleaned up.*/
45 Display *display; /* Display containing widget. Used, among
46 * other things, so that resources can be
47 * freed even after tkwin has gone away. */
48 Tcl_Interp *interp; /* Interpreter associated with listbox. */
49 Tcl_Command widgetCmd; /* Token for listbox's widget command. */
50 Tk_OptionTable optionTable; /* Table that defines configuration options
51 * available for this widget. */
52 Tk_OptionTable itemAttrOptionTable; /* Table that defines configuration
53 * options available for listbox
54 * items */
55 Tcl_Obj *listVarName; /* List variable name */
56 Tcl_Obj *listObj; /* Pointer to the list object being used */
57 int nElements; /* Holds the current count of elements */
58 Tcl_HashTable *selection; /* Tracks selection */
59 Tcl_HashTable *itemAttrTable; /* Tracks item attributes */
60
61 /*
62 * Information used when displaying widget:
63 */
64
65 Tk_3DBorder normalBorder; /* Used for drawing border around whole
66 * window, plus used for background. */
67 int borderWidth; /* Width of 3-D border around window. */
68 int relief; /* 3-D effect: TK_RELIEF_RAISED, etc. */
69 int highlightWidth; /* Width in pixels of highlight to draw
70 * around widget when it has the focus.
71 * <= 0 means don't draw a highlight. */
72 XColor *highlightBgColorPtr;
73 /* Color for drawing traversal highlight
74 * area when highlight is off. */
75 XColor *highlightColorPtr; /* Color for drawing traversal highlight. */
76 int inset; /* Total width of all borders, including
77 * traversal highlight and 3-D border.
78 * Indicates how much interior stuff must
79 * be offset from outside edges to leave
80 * room for borders. */
81 Tk_Font tkfont; /* Information about text font, or NULL. */
82 XColor *fgColorPtr; /* Text color in normal mode. */
83 XColor *dfgColorPtr; /* Text color in disabled mode. */
84 GC textGC; /* For drawing normal text. */
85 Tk_3DBorder selBorder; /* Borders and backgrounds for selected
86 * elements. */
87 int selBorderWidth; /* Width of border around selection. */
88 XColor *selFgColorPtr; /* Foreground color for selected elements. */
89 GC selTextGC; /* For drawing selected text. */
90 int width; /* Desired width of window, in characters. */
91 int height; /* Desired height of window, in lines. */
92 int lineHeight; /* Number of pixels allocated for each line
93 * in display. */
94 int topIndex; /* Index of top-most element visible in
95 * window. */
96 int fullLines; /* Number of lines that fit are completely
97 * visible in window. There may be one
98 * additional line at the bottom that is
99 * partially visible. */
100 int partialLine; /* 0 means that the window holds exactly
101 * fullLines lines. 1 means that there is
102 * one additional line that is partially
103 * visble. */
104 int setGrid; /* Non-zero means pass gridding information
105 * to window manager. */
106
107 /*
108 * Information to support horizontal scrolling:
109 */
110
111 int maxWidth; /* Width (in pixels) of widest string in
112 * listbox. */
113 int xScrollUnit; /* Number of pixels in one "unit" for
114 * horizontal scrolling (window scrolls
115 * horizontally in increments of this size).
116 * This is an average character size. */
117 int xOffset; /* The left edge of each string in the
118 * listbox is offset to the left by this
119 * many pixels (0 means no offset, positive
120 * means there is an offset). */
121
122 /*
123 * Information about what's selected or active, if any.
124 */
125
126 Tk_Uid selectMode; /* Selection style: single, browse, multiple,
127 * or extended. This value isn't used in C
128 * code, but the Tcl bindings use it. */
129 int numSelected; /* Number of elements currently selected. */
130 int selectAnchor; /* Fixed end of selection (i.e. element
131 * at which selection was started.) */
132 int exportSelection; /* Non-zero means tie internal listbox
133 * to X selection. */
134 int active; /* Index of "active" element (the one that
135 * has been selected by keyboard traversal).
136 * -1 means none. */
137 int activeStyle; /* style in which to draw the active element.
138 * One of: underline, none, dotbox */
139
140 /*
141 * Information for scanning:
142 */
143
144 int scanMarkX; /* X-position at which scan started (e.g.
145 * button was pressed here). */
146 int scanMarkY; /* Y-position at which scan started (e.g.
147 * button was pressed here). */
148 int scanMarkXOffset; /* Value of "xOffset" field when scan
149 * started. */
150 int scanMarkYIndex; /* Index of line that was at top of window
151 * when scan started. */
152
153 /*
154 * Miscellaneous information:
155 */
156
157 Tk_Cursor cursor; /* Current cursor for window, or None. */
158 char *takeFocus; /* Value of -takefocus option; not used in
159 * the C code, but used by keyboard traversal
160 * scripts. Malloc'ed, but may be NULL. */
161 LangCallback *yScrollCmd; /* Command prefix for communicating with
162 * vertical scrollbar. NULL means no command
163 * to issue. Malloc'ed. */
164 LangCallback *xScrollCmd; /* Command prefix for communicating with
165 * horizontal scrollbar. NULL means no command
166 * to issue. Malloc'ed. */
167 int state; /* Listbox state. */
168 Pixmap gray; /* Pixmap for displaying disabled text. */
169 int flags; /* Various flag bits: see below for
170 * definitions. */
171 } Listbox;
172
173 /*
174 * ItemAttr structures are used to store item configuration information for
175 * the items in a listbox
176 */
177 typedef struct {
178 Tk_3DBorder border; /* Used for drawing background around text */
179 Tk_3DBorder selBorder; /* Used for selected text */
180 XColor *fgColor; /* Text color in normal mode. */
181 XColor *selFgColor; /* Text color in selected mode. */
182 } ItemAttr;
183
184 /*
185 * Flag bits for listboxes:
186 *
187 * REDRAW_PENDING: Non-zero means a DoWhenIdle handler
188 * has already been queued to redraw
189 * this window.
190 * UPDATE_V_SCROLLBAR: Non-zero means vertical scrollbar needs
191 * to be updated.
192 * UPDATE_H_SCROLLBAR: Non-zero means horizontal scrollbar needs
193 * to be updated.
194 * GOT_FOCUS: Non-zero means this widget currently
195 * has the input focus.
196 * MAXWIDTH_IS_STALE: Stored maxWidth may be out-of-date
197 * LISTBOX_DELETED: This listbox has been effectively destroyed.
198 */
199
200 #define REDRAW_PENDING 1
201 #define UPDATE_V_SCROLLBAR 2
202 #define UPDATE_H_SCROLLBAR 4
203 #define GOT_FOCUS 8
204 #define MAXWIDTH_IS_STALE 16
205 #define LISTBOX_DELETED 32
206
207 /*
208 * The following enum is used to define a type for the -state option
209 * of the Entry widget. These values are used as indices into the
210 * string table below.
211 */
212
213 enum state {
214 STATE_DISABLED, STATE_NORMAL
215 };
216
217 static char *stateStrings[] = {
218 "disabled", "normal", (char *) NULL
219 };
220
221 enum activeStyle {
222 ACTIVE_STYLE_DOTBOX, ACTIVE_STYLE_NONE, ACTIVE_STYLE_UNDERLINE
223 };
224
225 static char *activeStyleStrings[] = {
226 "dotbox", "none", "underline", (char *) NULL
227 };
228
229 /*
230 * The optionSpecs table defines the valid configuration options for the
231 * listbox widget
232 */
233 static Tk_OptionSpec optionSpecs[] = {
234 {TK_OPTION_STRING_TABLE, "-activestyle", "activeStyle", "ActiveStyle",
235 DEF_LISTBOX_ACTIVE_STYLE, -1, Tk_Offset(Listbox, activeStyle),
236 0, (ClientData) activeStyleStrings, 0},
237 {TK_OPTION_BORDER, "-background", "background", "Background",
238 DEF_LISTBOX_BG_COLOR, -1, Tk_Offset(Listbox, normalBorder),
239 0, (ClientData) DEF_LISTBOX_BG_MONO, 0},
240 {TK_OPTION_SYNONYM, "-bd", (char *) NULL, (char *) NULL,
241 (char *) NULL, 0, -1, 0, (ClientData) "-borderwidth", 0},
242 {TK_OPTION_SYNONYM, "-bg", (char *) NULL, (char *) NULL,
243 (char *) NULL, 0, -1, 0, (ClientData) "-background", 0},
244 {TK_OPTION_PIXELS, "-borderwidth", "borderWidth", "BorderWidth",
245 DEF_LISTBOX_BORDER_WIDTH, -1, Tk_Offset(Listbox, borderWidth),
246 0, 0, 0},
247 {TK_OPTION_CURSOR, "-cursor", "cursor", "Cursor",
248 DEF_LISTBOX_CURSOR, -1, Tk_Offset(Listbox, cursor),
249 TK_OPTION_NULL_OK, 0, 0},
250 {TK_OPTION_COLOR, "-disabledforeground", "disabledForeground",
251 "DisabledForeground", DEF_LISTBOX_DISABLED_FG, -1,
252 Tk_Offset(Listbox, dfgColorPtr), TK_OPTION_NULL_OK, 0, 0},
253 {TK_OPTION_BOOLEAN, "-exportselection", "exportSelection",
254 "ExportSelection", DEF_LISTBOX_EXPORT_SELECTION, -1,
255 Tk_Offset(Listbox, exportSelection), 0, 0, 0},
256 {TK_OPTION_SYNONYM, "-fg", "foreground", (char *) NULL,
257 (char *) NULL, 0, -1, 0, (ClientData) "-foreground", 0},
258 {TK_OPTION_FONT, "-font", "font", "Font",
259 DEF_LISTBOX_FONT, -1, Tk_Offset(Listbox, tkfont), 0, 0, 0},
260 {TK_OPTION_COLOR, "-foreground", "foreground", "Foreground",
261 DEF_LISTBOX_FG, -1, Tk_Offset(Listbox, fgColorPtr), 0, 0, 0},
262 {TK_OPTION_INT, "-height", "height", "Height",
263 DEF_LISTBOX_HEIGHT, -1, Tk_Offset(Listbox, height), 0, 0, 0},
264 {TK_OPTION_COLOR, "-highlightbackground", "highlightBackground",
265 "HighlightBackground", DEF_LISTBOX_HIGHLIGHT_BG, -1,
266 Tk_Offset(Listbox, highlightBgColorPtr), 0, 0, 0},
267 {TK_OPTION_COLOR, "-highlightcolor", "highlightColor", "HighlightColor",
268 DEF_LISTBOX_HIGHLIGHT, -1, Tk_Offset(Listbox, highlightColorPtr),
269 0, 0, 0},
270 {TK_OPTION_PIXELS, "-highlightthickness", "highlightThickness",
271 "HighlightThickness", DEF_LISTBOX_HIGHLIGHT_WIDTH, -1,
272 Tk_Offset(Listbox, highlightWidth), 0, 0, 0},
273 {TK_OPTION_RELIEF, "-relief", "relief", "Relief",
274 DEF_LISTBOX_RELIEF, -1, Tk_Offset(Listbox, relief), 0, 0, 0},
275 {TK_OPTION_BORDER, "-selectbackground", "selectBackground", "Foreground",
276 DEF_LISTBOX_SELECT_COLOR, -1, Tk_Offset(Listbox, selBorder),
277 0, (ClientData) DEF_LISTBOX_SELECT_MONO, 0},
278 {TK_OPTION_PIXELS, "-selectborderwidth", "selectBorderWidth",
279 "BorderWidth", DEF_LISTBOX_SELECT_BD, -1,
280 Tk_Offset(Listbox, selBorderWidth), 0, 0, 0},
281 {TK_OPTION_COLOR, "-selectforeground", "selectForeground", "Background",
282 DEF_LISTBOX_SELECT_FG_COLOR, -1, Tk_Offset(Listbox, selFgColorPtr),
283 0, (ClientData) DEF_LISTBOX_SELECT_FG_MONO, 0},
284 {TK_OPTION_STRING, "-selectmode", "selectMode", "SelectMode",
285 DEF_LISTBOX_SELECT_MODE, -1, Tk_Offset(Listbox, selectMode),
286 TK_OPTION_NULL_OK, 0, 0},
287 {TK_OPTION_BOOLEAN, "-setgrid", "setGrid", "SetGrid",
288 DEF_LISTBOX_SET_GRID, -1, Tk_Offset(Listbox, setGrid), 0, 0, 0},
289 {TK_OPTION_STRING_TABLE, "-state", "state", "State",
290 DEF_LISTBOX_STATE, -1, Tk_Offset(Listbox, state),
291 0, (ClientData) stateStrings, 0},
292 {TK_OPTION_STRING, "-takefocus", "takeFocus", "TakeFocus",
293 DEF_LISTBOX_TAKE_FOCUS, -1, Tk_Offset(Listbox, takeFocus),
294 TK_OPTION_NULL_OK, 0, 0},
295 {TK_OPTION_INT, "-width", "width", "Width",
296 DEF_LISTBOX_WIDTH, -1, Tk_Offset(Listbox, width), 0, 0, 0},
297 {TK_OPTION_CALLBACK, "-xscrollcommand", "xScrollCommand", "ScrollCommand",
298 DEF_LISTBOX_SCROLL_COMMAND, -1, Tk_Offset(Listbox, xScrollCmd),
299 TK_OPTION_NULL_OK, 0, 0},
300 {TK_OPTION_CALLBACK, "-yscrollcommand", "yScrollCommand", "ScrollCommand",
301 DEF_LISTBOX_SCROLL_COMMAND, -1, Tk_Offset(Listbox, yScrollCmd),
302 TK_OPTION_NULL_OK, 0, 0},
303 {TK_OPTION_OBJ, "-listvariable", "listVariable", "Variable",
304 DEF_LISTBOX_LIST_VARIABLE, -1, Tk_Offset(Listbox, listVarName),
305 TK_OPTION_NULL_OK, 0, 0},
306 {TK_OPTION_END, (char *) NULL, (char *) NULL, (char *) NULL,
307 (char *) NULL, 0, -1, 0, 0, 0}
308 };
309
310 /*
311 * The itemAttrOptionSpecs table defines the valid configuration options for
312 * listbox items
313 */
314 static Tk_OptionSpec itemAttrOptionSpecs[] = {
315 {TK_OPTION_BORDER, "-background", "background", "Background",
316 (char *)NULL, -1, Tk_Offset(ItemAttr, border),
317 TK_OPTION_NULL_OK|TK_OPTION_DONT_SET_DEFAULT,
318 (ClientData) DEF_LISTBOX_BG_MONO, 0},
319 {TK_OPTION_SYNONYM, "-bg", (char *) NULL, (char *) NULL,
320 (char *) NULL, 0, -1, 0, (ClientData) "-background", 0},
321 {TK_OPTION_SYNONYM, "-fg", "foreground", (char *) NULL,
322 (char *) NULL, 0, -1, 0, (ClientData) "-foreground", 0},
323 {TK_OPTION_COLOR, "-foreground", "foreground", "Foreground",
324 (char *) NULL, -1, Tk_Offset(ItemAttr, fgColor),
325 TK_OPTION_NULL_OK|TK_OPTION_DONT_SET_DEFAULT, 0, 0},
326 {TK_OPTION_BORDER, "-selectbackground", "selectBackground", "Foreground",
327 (char *) NULL, -1, Tk_Offset(ItemAttr, selBorder),
328 TK_OPTION_NULL_OK|TK_OPTION_DONT_SET_DEFAULT,
329 (ClientData) DEF_LISTBOX_SELECT_MONO, 0},
330 {TK_OPTION_COLOR, "-selectforeground", "selectForeground", "Background",
331 (char *) NULL, -1, Tk_Offset(ItemAttr, selFgColor),
332 TK_OPTION_NULL_OK|TK_OPTION_DONT_SET_DEFAULT,
333 (ClientData) DEF_LISTBOX_SELECT_FG_MONO, 0},
334 {TK_OPTION_END, (char *) NULL, (char *) NULL, (char *) NULL,
335 (char *) NULL, 0, -1, 0, 0, 0}
336 };
337
338 /*
339 * The following tables define the listbox widget commands (and sub-
340 * commands) and map the indexes into the string tables into
341 * enumerated types used to dispatch the listbox widget command.
342 */
343 static CONST char *commandNames[] = {
344 "activate", "bbox", "cget", "configure", "curselection", "delete", "get",
345 "index", "insert", "itemcget", "itemconfigure", "nearest", "scan",
346 "see", "selection", "size", "xview", "yview",
347 (char *) NULL
348 };
349
350 enum command {
351 COMMAND_ACTIVATE, COMMAND_BBOX, COMMAND_CGET, COMMAND_CONFIGURE,
352 COMMAND_CURSELECTION, COMMAND_DELETE, COMMAND_GET, COMMAND_INDEX,
353 COMMAND_INSERT, COMMAND_ITEMCGET, COMMAND_ITEMCONFIGURE,
354 COMMAND_NEAREST, COMMAND_SCAN, COMMAND_SEE, COMMAND_SELECTION,
355 COMMAND_SIZE, COMMAND_XVIEW, COMMAND_YVIEW
356 };
357
358 static CONST char *selCommandNames[] = {
359 "anchor", "clear", "includes", "set", (char *) NULL
360 };
361
362 enum selcommand {
363 SELECTION_ANCHOR, SELECTION_CLEAR, SELECTION_INCLUDES, SELECTION_SET
364 };
365
366 static CONST char *scanCommandNames[] = {
367 "mark", "dragto", (char *) NULL
368 };
369
370 enum scancommand {
371 SCAN_MARK, SCAN_DRAGTO
372 };
373
374 static CONST char *indexNames[] = {
375 "active", "anchor", "end", (char *)NULL
376 };
377
378 enum indices {
379 INDEX_ACTIVE, INDEX_ANCHOR, INDEX_END
380 };
381
382
383 /* Declarations for procedures defined later in this file */
384 static void ChangeListboxOffset _ANSI_ARGS_((Listbox *listPtr,
385 int offset));
386 static void ChangeListboxView _ANSI_ARGS_((Listbox *listPtr,
387 int index));
388 static int ConfigureListbox _ANSI_ARGS_((Tcl_Interp *interp,
389 Listbox *listPtr, int objc, Tcl_Obj *CONST objv[],
390 int flags));
391 static int ConfigureListboxItem _ANSI_ARGS_ ((Tcl_Interp *interp,
392 Listbox *listPtr, ItemAttr *attrs, int objc,
393 Tcl_Obj *CONST objv[], int index));
394 static int ListboxDeleteSubCmd _ANSI_ARGS_((Listbox *listPtr,
395 int first, int last));
396 static void DestroyListbox _ANSI_ARGS_((char *memPtr));
397 static void DestroyListboxOptionTables _ANSI_ARGS_ (
398 (ClientData clientData, Tcl_Interp *interp));
399 static void DisplayListbox _ANSI_ARGS_((ClientData clientData));
400 static int GetListboxIndex _ANSI_ARGS_((Tcl_Interp *interp,
401 Listbox *listPtr, Tcl_Obj *index, int endIsSize,
402 int *indexPtr));
403 static int ListboxInsertSubCmd _ANSI_ARGS_((Listbox *listPtr,
404 int index, int objc, Tcl_Obj *CONST objv[]));
405 static void ListboxCmdDeletedProc _ANSI_ARGS_((
406 ClientData clientData));
407 static void ListboxComputeGeometry _ANSI_ARGS_((Listbox *listPtr,
408 int fontChanged, int maxIsStale, int updateGrid));
409 static void ListboxEventProc _ANSI_ARGS_((ClientData clientData,
410 XEvent *eventPtr));
411 static int ListboxFetchSelection _ANSI_ARGS_((
412 ClientData clientData, int offset, char *buffer,
413 int maxBytes));
414 static void ListboxLostSelection _ANSI_ARGS_((
415 ClientData clientData));
416 static void EventuallyRedrawRange _ANSI_ARGS_((Listbox *listPtr,
417 int first, int last));
418 static void ListboxScanTo _ANSI_ARGS_((Listbox *listPtr,
419 int x, int y));
420 static int ListboxSelect _ANSI_ARGS_((Listbox *listPtr,
421 int first, int last, int select));
422 static void ListboxUpdateHScrollbar _ANSI_ARGS_(
423 (Listbox *listPtr));
424 static void ListboxUpdateVScrollbar _ANSI_ARGS_(
425 (Listbox *listPtr));
426 static int ListboxWidgetObjCmd _ANSI_ARGS_((ClientData clientData,
427 Tcl_Interp *interp, int objc,
428 Tcl_Obj *CONST objv[]));
429 static int ListboxBboxSubCmd _ANSI_ARGS_ ((Tcl_Interp *interp,
430 Listbox *listPtr, int index));
431 static int ListboxSelectionSubCmd _ANSI_ARGS_ (
432 (Tcl_Interp *interp, Listbox *listPtr, int objc,
433 Tcl_Obj *CONST objv[]));
434 static int ListboxXviewSubCmd _ANSI_ARGS_ ((Tcl_Interp *interp,
435 Listbox *listPtr, int objc,
436 Tcl_Obj *CONST objv[]));
437 static int ListboxYviewSubCmd _ANSI_ARGS_ ((Tcl_Interp *interp,
438 Listbox *listPtr, int objc,
439 Tcl_Obj *CONST objv[]));
440 static ItemAttr * ListboxGetItemAttributes _ANSI_ARGS_ (
441 (Tcl_Interp *interp, Listbox *listPtr, int index));
442 static void ListboxWorldChanged _ANSI_ARGS_((
443 ClientData instanceData));
444 static int NearestListboxElement _ANSI_ARGS_((Listbox *listPtr,
445 int y));
446 static char * ListboxListVarProc _ANSI_ARGS_ ((ClientData clientData,
447 Tcl_Interp *interp, Tcl_Obj *name1,
448 CONST char *name2, int flags));
449 static void MigrateHashEntries _ANSI_ARGS_ ((Tcl_HashTable *table,
450 int first, int last, int offset));
451 /*
452 * The structure below defines button class behavior by means of procedures
453 * that can be invoked from generic window code.
454 */
455
456 static Tk_ClassProcs listboxClass = {
457 sizeof(Tk_ClassProcs), /* size */
458 ListboxWorldChanged, /* worldChangedProc */
459 };
460
461
462 /*
463 *--------------------------------------------------------------
464 *
465 * Tk_ListboxObjCmd --
466 *
467 * This procedure is invoked to process the "listbox" Tcl
468 * command. See the user documentation for details on what
469 * it does.
470 *
471 * Results:
472 * A standard Tcl result.
473 *
474 * Side effects:
475 * See the user documentation.
476 *
477 *--------------------------------------------------------------
478 */
479
480 int
481 Tk_ListboxObjCmd(clientData, interp, objc, objv)
482 ClientData clientData; /* NULL. */
483 Tcl_Interp *interp; /* Current interpreter. */
484 int objc; /* Number of arguments. */
485 Tcl_Obj *CONST objv[]; /* Argument objects. */
486 {
487 register Listbox *listPtr;
488 Tk_Window tkwin;
489 ListboxOptionTables *optionTables;
490
491 if (objc < 2) {
492 Tcl_WrongNumArgs(interp, 1, objv, "pathName ?options?");
493 return TCL_ERROR;
494 }
495
496 tkwin = Tk_CreateWindowFromPath(interp, Tk_MainWindow(interp),
497 Tcl_GetString(objv[1]), (char *) NULL);
498 if (tkwin == NULL) {
499 return TCL_ERROR;
500 }
501
502 optionTables = (ListboxOptionTables *)
503 Tcl_GetAssocData(interp, "ListboxOptionTables", NULL);
504 if (optionTables == NULL) {
505 /*
506 * We haven't created the option tables for this widget class yet.
507 * Do it now and save the a pointer to them as the ClientData for
508 * the command, so future invocations will have access to it.
509 */
510
511 optionTables = (ListboxOptionTables *)
512 ckalloc(sizeof(ListboxOptionTables));
513 /* Set up an exit handler to free the optionTables struct */
514 Tcl_SetAssocData(interp, "ListboxOptionTables",
515 DestroyListboxOptionTables, (ClientData) optionTables);
516
517 /* Create the listbox option table and the listbox item option table */
518 optionTables->listboxOptionTable =
519 Tk_CreateOptionTable(interp, optionSpecs);
520 optionTables->itemAttrOptionTable =
521 Tk_CreateOptionTable(interp, itemAttrOptionSpecs);
522 }
523
524 /*
525 * Initialize the fields of the structure that won't be initialized
526 * by ConfigureListbox, or that ConfigureListbox requires to be
527 * initialized already (e.g. resource pointers).
528 */
529 listPtr = (Listbox *) ckalloc(sizeof(Listbox));
530 memset((void *) listPtr, 0, (sizeof(Listbox)));
531
532 listPtr->tkwin = tkwin;
533 listPtr->display = Tk_Display(tkwin);
534 listPtr->interp = interp;
535 listPtr->widgetCmd = Tcl_CreateObjCommand(interp,
536 Tk_PathName(listPtr->tkwin), ListboxWidgetObjCmd,
537 (ClientData) listPtr, ListboxCmdDeletedProc);
538 listPtr->optionTable = optionTables->listboxOptionTable;
539 listPtr->itemAttrOptionTable = optionTables->itemAttrOptionTable;
540 listPtr->selection =
541 (Tcl_HashTable *) ckalloc(sizeof(Tcl_HashTable));
542 Tcl_InitHashTable(listPtr->selection, TCL_ONE_WORD_KEYS);
543 listPtr->itemAttrTable =
544 (Tcl_HashTable *) ckalloc(sizeof(Tcl_HashTable));
545 Tcl_InitHashTable(listPtr->itemAttrTable, TCL_ONE_WORD_KEYS);
546 listPtr->relief = TK_RELIEF_RAISED;
547 listPtr->textGC = None;
548 listPtr->selFgColorPtr = None;
549 listPtr->selTextGC = None;
550 listPtr->fullLines = 1;
551 listPtr->xScrollUnit = 1;
552 listPtr->exportSelection = 1;
553 listPtr->cursor = None;
554 listPtr->state = STATE_NORMAL;
555 listPtr->gray = None;
556
557 /*
558 * Keep a hold of the associated tkwin until we destroy the listbox,
559 * otherwise Tk might free it while we still need it.
560 */
561
562 Tcl_Preserve((ClientData) listPtr->tkwin);
563
564 Tk_SetClass(listPtr->tkwin, "Listbox");
565 Tk_SetClassProcs(listPtr->tkwin, &listboxClass, (ClientData) listPtr);
566 Tk_CreateEventHandler(listPtr->tkwin,
567 ExposureMask|StructureNotifyMask|FocusChangeMask,
568 ListboxEventProc, (ClientData) listPtr);
569 Tk_CreateSelHandler(listPtr->tkwin, XA_PRIMARY, XA_STRING,
570 ListboxFetchSelection, (ClientData) listPtr, XA_STRING);
571 if (Tk_InitOptions(interp, (char *)listPtr,
572 optionTables->listboxOptionTable, tkwin) != TCL_OK) {
573 Tk_DestroyWindow(listPtr->tkwin);
574 return TCL_ERROR;
575 }
576
577 if (ConfigureListbox(interp, listPtr, objc-2, objv+2, 0) != TCL_OK) {
578 Tk_DestroyWindow(listPtr->tkwin);
579 return TCL_ERROR;
580 }
581
582 Tcl_SetResult(interp, Tk_PathName(listPtr->tkwin), TCL_STATIC);
583 return TCL_OK;
584 }
585
586 /*
587 *----------------------------------------------------------------------
588 *
589 * ListboxWidgetObjCmd --
590 *
591 * This Tcl_Obj based procedure is invoked to process the Tcl command
592 * that corresponds to a widget managed by this module. See the user
593 * documentation for details on what it does.
594 *
595 * Results:
596 * A standard Tcl result.
597 *
598 * Side effects:
599 * See the user documentation.
600 *
601 *----------------------------------------------------------------------
602 */
603
604 static int
605 ListboxWidgetObjCmd(clientData, interp, objc, objv)
606 ClientData clientData; /* Information about listbox widget. */
607 Tcl_Interp *interp; /* Current interpreter. */
608 int objc; /* Number of arguments. */
609 Tcl_Obj *CONST objv[]; /* Arguments as Tcl_Obj's. */
610 {
611 register Listbox *listPtr = (Listbox *) clientData;
612 int cmdIndex, index;
613 int result = TCL_OK;
614
615 if (objc < 2) {
616 Tcl_WrongNumArgs(interp, 1, objv, "option ?arg arg ...?");
617 return TCL_ERROR;
618 }
619
620 /*
621 * Parse the command by looking up the second argument in the list
622 * of valid subcommand names
623 */
624 result = Tcl_GetIndexFromObj(interp, objv[1], commandNames,
625 "option", 0, &cmdIndex);
626 if (result != TCL_OK) {
627 return result;
628 }
629
630 Tcl_Preserve((ClientData)listPtr);
631 /* The subcommand was valid, so continue processing */
632 switch (cmdIndex) {
633 case COMMAND_ACTIVATE: {
634 if (objc != 3) {
635 Tcl_WrongNumArgs(interp, 2, objv, "index");
636 result = TCL_ERROR;
637 break;
638 }
639 result = GetListboxIndex(interp, listPtr, objv[2], 0, &index);
640 if (result != TCL_OK) {
641 break;
642 }
643
644 if (!(listPtr->state & STATE_NORMAL)) {
645 break;
646 }
647
648 if (index >= listPtr->nElements) {
649 index = listPtr->nElements-1;
650 }
651 if (index < 0) {
652 index = 0;
653 }
654 listPtr->active = index;
655 EventuallyRedrawRange(listPtr, listPtr->active, listPtr->active);
656 result = TCL_OK;
657 break;
658 }
659
660 case COMMAND_BBOX: {
661 if (objc != 3) {
662 Tcl_WrongNumArgs(interp, 2, objv, "index");
663 result = TCL_ERROR;
664 break;
665 }
666 result = GetListboxIndex(interp, listPtr, objv[2], 0, &index);
667 if (result != TCL_OK) {
668 break;
669 }
670
671 result = ListboxBboxSubCmd(interp, listPtr, index);
672 break;
673 }
674
675 case COMMAND_CGET: {
676 Tcl_Obj *objPtr;
677 if (objc != 3) {
678 Tcl_WrongNumArgs(interp, 2, objv, "option");
679 result = TCL_ERROR;
680 break;
681 }
682
683 objPtr = Tk_GetOptionValue(interp, (char *)listPtr,
684 listPtr->optionTable, objv[2], listPtr->tkwin);
685 if (objPtr == NULL) {
686 result = TCL_ERROR;
687 break;
688 }
689 Tcl_SetObjResult(interp, objPtr);
690 result = TCL_OK;
691 break;
692 }
693
694 case COMMAND_CONFIGURE: {
695 Tcl_Obj *objPtr;
696 if (objc <= 3) {
697 objPtr = Tk_GetOptionInfo(interp, (char *) listPtr,
698 listPtr->optionTable,
699 (objc == 3) ? objv[2] : (Tcl_Obj *) NULL,
700 listPtr->tkwin);
701 if (objPtr == NULL) {
702 result = TCL_ERROR;
703 break;
704 } else {
705 Tcl_SetObjResult(interp, objPtr);
706 result = TCL_OK;
707 }
708 } else {
709 result = ConfigureListbox(interp, listPtr, objc-2, objv+2, 0);
710 }
711 break;
712 }
713
714 case COMMAND_CURSELECTION: {
715 char indexStringRep[TCL_INTEGER_SPACE];
716 int i;
717 if (objc != 2) {
718 Tcl_WrongNumArgs(interp, 2, objv, NULL);
719 result = TCL_ERROR;
720 break;
721 }
722 /*
723 * Of course, it would be more efficient to use the Tcl_HashTable
724 * search functions (Tcl_FirstHashEntry, Tcl_NextHashEntry), but
725 * then the result wouldn't be in sorted order. So instead we
726 * loop through the indices in order, adding them to the result
727 * if they are selected
728 */
729 for (i = 0; i < listPtr->nElements; i++) {
730 if (Tcl_FindHashEntry(listPtr->selection, (char *)i) != NULL) {
731 sprintf(indexStringRep, "%d", i);
732 Tcl_AppendElement(interp, indexStringRep);
733 }
734 }
735 result = TCL_OK;
736 break;
737 }
738
739 case COMMAND_DELETE: {
740 int first, last;
741 if ((objc < 3) || (objc > 4)) {
742 Tcl_WrongNumArgs(interp, 2, objv,
743 "firstIndex ?lastIndex?");
744 result = TCL_ERROR;
745 break;
746 }
747
748 result = GetListboxIndex(interp, listPtr, objv[2], 0, &first);
749 if (result != TCL_OK) {
750 break;
751 }
752
753 if (!(listPtr->state & STATE_NORMAL)) {
754 break;
755 }
756
757 if (first < listPtr->nElements) {
758 /*
759 * if a "last index" was given, get it now; otherwise, use the
760 * first index as the last index
761 */
762 if (objc == 4) {
763 result = GetListboxIndex(interp, listPtr,
764 objv[3], 0, &last);
765 if (result != TCL_OK) {
766 break;
767 }
768 } else {
769 last = first;
770 }
771 if (last >= listPtr->nElements) {
772 last = listPtr->nElements - 1;
773 }
774 result = ListboxDeleteSubCmd(listPtr, first, last);
775 } else {
776 result = TCL_OK;
777 }
778 break;
779 }
780
781 case COMMAND_GET: {
782 int first, last;
783 Tcl_Obj **elemPtrs;
784 int listLen;
785 if (objc != 3 && objc != 4) {
786 Tcl_WrongNumArgs(interp, 2, objv, "firstIndex ?lastIndex?");
787 result = TCL_ERROR;
788 break;
789 }
790 result = GetListboxIndex(interp, listPtr, objv[2], 0, &first);
791 if (result != TCL_OK) {
792 break;
793 }
794 last = first;
795 if (objc == 4) {
796 result = GetListboxIndex(interp, listPtr, objv[3], 0, &last);
797 if (result != TCL_OK) {
798 break;
799 }
800 }
801 if (first >= listPtr->nElements) {
802 result = TCL_OK;
803 break;
804 }
805 if (last >= listPtr->nElements) {
806 last = listPtr->nElements - 1;
807 }
808 if (first < 0) {
809 first = 0;
810 }
811 if (first > last) {
812 result = TCL_OK;
813 break;
814 }
815 result = Tcl_ListObjGetElements(interp, listPtr->listObj, &listLen,
816 &elemPtrs);
817 if (result != TCL_OK) {
818 break;
819 }
820 if (objc == 3) {
821 /*
822 * One element request - we return a string
823 */
824 Tcl_IncrRefCount(elemPtrs[first]);
825 Tcl_SetObjResult(interp, elemPtrs[first]);
826 } else {
827 Tcl_SetListObj(Tcl_GetObjResult(interp), (last - first + 1),
828 &(elemPtrs[first]));
829 }
830 result = TCL_OK;
831 break;
832 }
833
834 case COMMAND_INDEX:{
835 char buf[TCL_INTEGER_SPACE];
836 if (objc != 3) {
837 Tcl_WrongNumArgs(interp, 2, objv, "index");
838 result = TCL_ERROR;
839 break;
840 }
841 result = GetListboxIndex(interp, listPtr, objv[2], 1, &index);
842 if (result != TCL_OK) {
843 break;
844 }
845 sprintf(buf, "%d", index);
846 Tcl_SetResult(interp, buf, TCL_VOLATILE);
847 result = TCL_OK;
848 break;
849 }
850
851 case COMMAND_INSERT: {
852 if (objc < 3) {
853 Tcl_WrongNumArgs(interp, 2, objv,
854 "index ?element element ...?");
855 result = TCL_ERROR;
856 break;
857 }
858
859 result = GetListboxIndex(interp, listPtr, objv[2], 1, &index);
860 if (result != TCL_OK) {
861 break;
862 }
863
864 if (!(listPtr->state & STATE_NORMAL)) {
865 break;
866 }
867
868 result = ListboxInsertSubCmd(listPtr, index, objc-3, objv+3);
869 break;
870 }
871
872 case COMMAND_ITEMCGET: {
873 Tcl_Obj *objPtr;
874 ItemAttr *attrPtr;
875 if (objc != 4) {
876 Tcl_WrongNumArgs(interp, 2, objv, "index option");
877 result = TCL_ERROR;
878 break;
879 }
880
881 result = GetListboxIndex(interp, listPtr, objv[2], 0, &index);
882 if (result != TCL_OK) {
883 break;
884 }
885
886 if (index < 0 || index >= listPtr->nElements) {
887 Tcl_AppendResult(interp, "item number \"",
888 Tcl_GetString(objv[2]), "\" out of range",
889 (char *)NULL);
890 result = TCL_ERROR;
891 break;
892 }
893
894 attrPtr = ListboxGetItemAttributes(interp, listPtr, index);
895
896 objPtr = Tk_GetOptionValue(interp, (char *)attrPtr,
897 listPtr->itemAttrOptionTable, objv[3], listPtr->tkwin);
898 if (objPtr == NULL) {
899 result = TCL_ERROR;
900 break;
901 }
902 Tcl_SetObjResult(interp, objPtr);
903 result = TCL_OK;
904 break;
905 }
906
907 case COMMAND_ITEMCONFIGURE: {
908 Tcl_Obj *objPtr;
909 ItemAttr *attrPtr;
910 if (objc < 3) {
911 Tcl_WrongNumArgs(interp, 2, objv,
912 "index ?option? ?value? ?option value ...?");
913 result = TCL_ERROR;
914 break;
915 }
916
917 result = GetListboxIndex(interp, listPtr, objv[2], 0, &index);
918 if (result != TCL_OK) {
919 break;
920 }
921
922 if (index < 0 || index >= listPtr->nElements) {
923 Tcl_AppendResult(interp, "item number \"",
924 Tcl_GetString(objv[2]), "\" out of range",
925 (char *)NULL);
926 result = TCL_ERROR;
927 break;
928 }
929
930 attrPtr = ListboxGetItemAttributes(interp, listPtr, index);
931 if (objc <= 4) {
932 objPtr = Tk_GetOptionInfo(interp, (char *)attrPtr,
933 listPtr->itemAttrOptionTable,
934 (objc == 4) ? objv[3] : (Tcl_Obj *) NULL,
935 listPtr->tkwin);
936 if (objPtr == NULL) {
937 result = TCL_ERROR;
938 break;
939 } else {
940 Tcl_SetObjResult(interp, objPtr);
941 result = TCL_OK;
942 }
943 } else {
944 result = ConfigureListboxItem(interp, listPtr, attrPtr,
945 objc-3, objv+3, index);
946 }
947 break;
948 }
949
950 case COMMAND_NEAREST: {
951 char buf[TCL_INTEGER_SPACE];
952 int y;
953 if (objc != 3) {
954 Tcl_WrongNumArgs(interp, 2, objv, "y");
955 result = TCL_ERROR;
956 break;
957 }
958
959 result = Tcl_GetIntFromObj(interp, objv[2], &y);
960 if (result != TCL_OK) {
961 break;
962 }
963 index = NearestListboxElement(listPtr, y);
964 sprintf(buf, "%d", index);
965 Tcl_SetResult(interp, buf, TCL_VOLATILE);
966 result = TCL_OK;
967 break;
968 }
969
970 case COMMAND_SCAN: {
971 int x, y, scanCmdIndex;
972
973 if (objc != 5) {
974 Tcl_WrongNumArgs(interp, 2, objv, "mark|dragto x y");
975 result = TCL_ERROR;
976 break;
977 }
978
979 if (Tcl_GetIntFromObj(interp, objv[3], &x) != TCL_OK
980 || Tcl_GetIntFromObj(interp, objv[4], &y) != TCL_OK) {
981 result = TCL_ERROR;
982 break;
983 }
984
985 result = Tcl_GetIndexFromObj(interp, objv[2], scanCommandNames,
986 "option", 0, &scanCmdIndex);
987 if (result != TCL_OK) {
988 break;
989 }
990 switch (scanCmdIndex) {
991 case SCAN_MARK: {
992 listPtr->scanMarkX = x;
993 listPtr->scanMarkY = y;
994 listPtr->scanMarkXOffset = listPtr->xOffset;
995 listPtr->scanMarkYIndex = listPtr->topIndex;
996 break;
997 }
998 case SCAN_DRAGTO: {
999 ListboxScanTo(listPtr, x, y);
1000 break;
1001 }
1002 }
1003 result = TCL_OK;
1004 break;
1005 }
1006
1007 case COMMAND_SEE: {
1008 int diff;
1009 if (objc != 3) {
1010 Tcl_WrongNumArgs(interp, 2, objv, "index");
1011 result = TCL_ERROR;
1012 break;
1013 }
1014 result = GetListboxIndex(interp, listPtr, objv[2], 0, &index);
1015 if (result != TCL_OK) {
1016 break;
1017 }
1018 if (index >= listPtr->nElements) {
1019 index = listPtr->nElements - 1;
1020 }
1021 if (index < 0) {
1022 index = 0;
1023 }
1024 diff = listPtr->topIndex - index;
1025 if (diff > 0) {
1026 if (diff <= (listPtr->fullLines/3)) {
1027 ChangeListboxView(listPtr, index);
1028 } else {
1029 ChangeListboxView(listPtr,
1030 index - (listPtr->fullLines-1)/2);
1031 }
1032 } else {
1033 diff = index - (listPtr->topIndex + listPtr->fullLines - 1);
1034 if (diff > 0) {
1035 if (diff <= (listPtr->fullLines/3)) {
1036 ChangeListboxView(listPtr, listPtr->topIndex + diff);
1037 } else {
1038 ChangeListboxView(listPtr,
1039 index - (listPtr->fullLines-1)/2);
1040 }
1041 }
1042 }
1043 result = TCL_OK;
1044 break;
1045 }
1046
1047 case COMMAND_SELECTION: {
1048 result = ListboxSelectionSubCmd(interp, listPtr, objc, objv);
1049 break;
1050 }
1051
1052 case COMMAND_SIZE: {
1053 char buf[TCL_INTEGER_SPACE];
1054 if (objc != 2) {
1055 Tcl_WrongNumArgs(interp, 2, objv, NULL);
1056 result = TCL_ERROR;
1057 break;
1058 }
1059 sprintf(buf, "%d", listPtr->nElements);
1060 Tcl_SetResult(interp, buf, TCL_VOLATILE);
1061 result = TCL_OK;
1062 break;
1063 }
1064
1065 case COMMAND_XVIEW: {
1066 result = ListboxXviewSubCmd(interp, listPtr, objc, objv);
1067 break;
1068 }
1069
1070 case COMMAND_YVIEW: {
1071 result = ListboxYviewSubCmd(interp, listPtr, objc, objv);
1072 break;
1073 }
1074 }
1075 Tcl_Release((ClientData)listPtr);
1076 return result;
1077 }
1078
1079 /*
1080 *----------------------------------------------------------------------
1081 *
1082 * ListboxBboxSubCmd --
1083 *
1084 * This procedure is invoked to process a listbox bbox request.
1085 * See the user documentation for more information.
1086 *
1087 * Results:
1088 * A standard Tcl result.
1089 *
1090 * Side effects:
1091 * For valid indices, places the bbox of the requested element in
1092 * the interpreter's result.
1093 *
1094 *----------------------------------------------------------------------
1095 */
1096
1097 static int
1098 ListboxBboxSubCmd(interp, listPtr, index)
1099 Tcl_Interp *interp; /* Pointer to the calling Tcl interpreter */
1100 Listbox *listPtr; /* Information about the listbox */
1101 int index; /* Index of the element to get bbox info on */
1102 {
1103 int lastVisibleIndex;
1104 /* Determine the index of the last visible item in the listbox */
1105 lastVisibleIndex = listPtr->topIndex + listPtr->fullLines
1106 + listPtr->partialLine;
1107 if (listPtr->nElements < lastVisibleIndex) {
1108 lastVisibleIndex = listPtr->nElements;
1109 }
1110
1111 /* Only allow bbox requests for indices that are visible */
1112 if ((listPtr->topIndex <= index) && (index < lastVisibleIndex)) {
1113 char buf[TCL_INTEGER_SPACE * 4];
1114 Tcl_Obj *el;
1115 char *stringRep;
1116 int pixelWidth, stringLen, x, y, result;
1117 Tk_FontMetrics fm;
1118
1119 /* Compute the pixel width of the requested element */
1120 result = Tcl_ListObjIndex(interp, listPtr->listObj, index, &el);
1121 if (result != TCL_OK) {
1122 return result;
1123 }
1124
1125 stringRep = Tcl_GetStringFromObj(el, &stringLen);
1126 Tk_GetFontMetrics(listPtr->tkfont, &fm);
1127 pixelWidth = Tk_TextWidth(listPtr->tkfont, stringRep, stringLen);
1128
1129 x = listPtr->inset + listPtr->selBorderWidth - listPtr->xOffset;
1130 y = ((index - listPtr->topIndex)*listPtr->lineHeight)
1131 + listPtr->inset + listPtr->selBorderWidth;
1132 el = Tcl_GetObjResult(interp);
1133 Tcl_ListObjAppendElement(interp,el,Tcl_NewIntObj(x));
1134 Tcl_ListObjAppendElement(interp,el,Tcl_NewIntObj(y));
1135 Tcl_ListObjAppendElement(interp,el,Tcl_NewIntObj(pixelWidth));
1136 Tcl_ListObjAppendElement(interp,el,Tcl_NewIntObj(fm.linespace));
1137 }
1138 return TCL_OK;
1139 }
1140
1141 /*
1142 *----------------------------------------------------------------------
1143 *
1144 * ListboxSelectionSubCmd --
1145 *
1146 * This procedure is invoked to process the selection sub command
1147 * for listbox widgets.
1148 *
1149 * Results:
1150 * Standard Tcl result.
1151 *
1152 * Side effects:
1153 * May set the interpreter's result field.
1154 *
1155 *----------------------------------------------------------------------
1156 */
1157
1158 static int
1159 ListboxSelectionSubCmd(interp, listPtr, objc, objv)
1160 Tcl_Interp *interp; /* Pointer to the calling Tcl interpreter */
1161 Listbox *listPtr; /* Information about the listbox */
1162 int objc; /* Number of arguments in the objv array */
1163 Tcl_Obj *CONST objv[]; /* Array of arguments to the procedure */
1164 {
1165 int selCmdIndex, first, last;
1166 int result = TCL_OK;
1167 if (objc != 4 && objc != 5) {
1168 Tcl_WrongNumArgs(interp, 2, objv, "option index ?index?");
1169 return TCL_ERROR;
1170 }
1171 result = GetListboxIndex(interp, listPtr, objv[3], 0, &first);
1172 if (result != TCL_OK) {
1173 return result;
1174 }
1175 last = first;
1176 if (objc == 5) {
1177 result = GetListboxIndex(interp, listPtr, objv[4], 0, &last);
1178 if (result != TCL_OK) {
1179 return result;
1180 }
1181 }
1182 result = Tcl_GetIndexFromObj(interp, objv[2], selCommandNames,
1183 "option", 0, &selCmdIndex);
1184 if (result != TCL_OK) {
1185 return result;
1186 }
1187
1188 /*
1189 * Only allow 'selection includes' to respond if disabled. [Bug #632514]
1190 */
1191
1192 if ((listPtr->state == STATE_DISABLED)
1193 && (selCmdIndex != SELECTION_INCLUDES)) {
1194 return TCL_OK;
1195 }
1196
1197 switch (selCmdIndex) {
1198 case SELECTION_ANCHOR: {
1199 if (objc != 4) {
1200 Tcl_WrongNumArgs(interp, 3, objv, "index");
1201 return TCL_ERROR;
1202 }
1203 if (first >= listPtr->nElements) {
1204 first = listPtr->nElements - 1;
1205 }
1206 if (first < 0) {
1207 first = 0;
1208 }
1209 listPtr->selectAnchor = first;
1210 result = TCL_OK;
1211 break;
1212 }
1213 case SELECTION_CLEAR: {
1214 result = ListboxSelect(listPtr, first, last, 0);
1215 break;
1216 }
1217 case SELECTION_INCLUDES: {
1218 if (objc != 4) {
1219 Tcl_WrongNumArgs(interp, 3, objv, "index");
1220 return TCL_ERROR;
1221 }
1222 Tcl_SetObjResult(interp,
1223 Tcl_NewBooleanObj((Tcl_FindHashEntry(listPtr->selection,
1224 (char *)first) != NULL)));
1225 result = TCL_OK;
1226 break;
1227 }
1228 case SELECTION_SET: {
1229 result = ListboxSelect(listPtr, first, last, 1);
1230 break;
1231 }
1232 }
1233 return result;
1234 }
1235
1236 /*
1237 *----------------------------------------------------------------------
1238 *
1239 * ListboxXviewSubCmd --
1240 *
1241 * Process the listbox "xview" subcommand.
1242 *
1243 * Results:
1244 * Standard Tcl result.
1245 *
1246 * Side effects:
1247 * May change the listbox viewing area; may set the interpreter's result.
1248 *
1249 *----------------------------------------------------------------------
1250 */
1251
1252 static int
1253 ListboxXviewSubCmd(interp, listPtr, objc, objv)
1254 Tcl_Interp *interp; /* Pointer to the calling Tcl interpreter */
1255 Listbox *listPtr; /* Information about the listbox */
1256 int objc; /* Number of arguments in the objv array */
1257 Tcl_Obj *CONST objv[]; /* Array of arguments to the procedure */
1258 {
1259
1260 int index, count, type, windowWidth, windowUnits;
1261 int offset = 0; /* Initialized to stop gcc warnings. */
1262 double fraction, fraction2;
1263
1264 windowWidth = Tk_Width(listPtr->tkwin)
1265 - 2*(listPtr->inset + listPtr->selBorderWidth);
1266 if (objc == 2) {
1267 if (listPtr->maxWidth == 0) {
1268 fraction = 0;
1269 fraction2 = 1;
1270 } else {
1271 fraction = listPtr->xOffset/((double) listPtr->maxWidth);
1272 fraction2 = (listPtr->xOffset + windowWidth)
1273 /((double) listPtr->maxWidth);
1274 if (fraction2 > 1.0) {
1275 fraction2 = 1.0;
1276 }
1277 }
1278 Tcl_DoubleResults(interp,2,0, fraction, fraction2);
1279 } else if (objc == 3) {
1280 if (Tcl_GetIntFromObj(interp, objv[2], &index) != TCL_OK) {
1281 return TCL_ERROR;
1282 }
1283 ChangeListboxOffset(listPtr, index*listPtr->xScrollUnit);
1284 } else {
1285 type = Tk_GetScrollInfoObj(interp, objc, objv, &fraction, &count);
1286 switch (type) {
1287 case TK_SCROLL_ERROR:
1288 return TCL_ERROR;
1289 case TK_SCROLL_MOVETO:
1290 offset = (int) (fraction*listPtr->maxWidth + 0.5);
1291 break;
1292 case TK_SCROLL_PAGES:
1293 windowUnits = windowWidth/listPtr->xScrollUnit;
1294 if (windowUnits > 2) {
1295 offset = listPtr->xOffset
1296 + count*listPtr->xScrollUnit*(windowUnits-2);
1297 } else {
1298 offset = listPtr->xOffset + count*listPtr->xScrollUnit;
1299 }
1300 break;
1301 case TK_SCROLL_UNITS:
1302 offset = listPtr->xOffset + count*listPtr->xScrollUnit;
1303 break;
1304 }
1305 ChangeListboxOffset(listPtr, offset);
1306 }
1307 return TCL_OK;
1308 }
1309
1310 /*
1311 *----------------------------------------------------------------------
1312 *
1313 * ListboxYviewSubCmd --
1314 *
1315 * Process the listbox "yview" subcommand.
1316 *
1317 * Results:
1318 * Standard Tcl result.
1319 *
1320 * Side effects:
1321 * May change the listbox viewing area; may set the interpreter's result.
1322 *
1323 *----------------------------------------------------------------------
1324 */
1325
1326 static int
1327 ListboxYviewSubCmd(interp, listPtr, objc, objv)
1328 Tcl_Interp *interp; /* Pointer to the calling Tcl interpreter */
1329 Listbox *listPtr; /* Information about the listbox */
1330 int objc; /* Number of arguments in the objv array */
1331 Tcl_Obj *CONST objv[]; /* Array of arguments to the procedure */
1332 {
1333 int index, count, type;
1334 double fraction, fraction2;
1335
1336 if (objc == 2) {
1337 if (listPtr->nElements == 0) {
1338 fraction = 0;
1339 fraction2 = 1;
1340 } else {
1341 fraction = listPtr->topIndex/((double) listPtr->nElements);
1342 fraction2 = (listPtr->topIndex+listPtr->fullLines)
1343 /((double) listPtr->nElements);
1344 if (fraction2 > 1.0) {
1345 fraction2 = 1.0;
1346 }
1347 }
1348 Tcl_DoubleResults(interp,2,0, fraction, fraction2);
1349 } else if (objc == 3) {
1350 if (GetListboxIndex(interp, listPtr, objv[2], 0, &index) != TCL_OK) {
1351 return TCL_ERROR;
1352 }
1353 ChangeListboxView(listPtr, index);
1354 } else {
1355 type = Tk_GetScrollInfoObj(interp, objc, objv, &fraction, &count);
1356 switch (type) {
1357 case TK_SCROLL_ERROR:
1358 return TCL_ERROR;
1359 case TK_SCROLL_MOVETO:
1360 index = (int) (listPtr->nElements*fraction + 0.5);
1361 break;
1362 case TK_SCROLL_PAGES:
1363 if (listPtr->fullLines > 2) {
1364 index = listPtr->topIndex
1365 + count*(listPtr->fullLines-2);
1366 } else {
1367 index = listPtr->topIndex + count;
1368 }
1369 break;
1370 case TK_SCROLL_UNITS:
1371 index = listPtr->topIndex + count;
1372 break;
1373 }
1374 ChangeListboxView(listPtr, index);
1375 }
1376 return TCL_OK;
1377 }
1378
1379 /*
1380 *----------------------------------------------------------------------
1381 *
1382 * ListboxGetItemAttributes --
1383 *
1384 * Returns a pointer to the ItemAttr record for a given index,
1385 * creating one if it does not already exist.
1386 *
1387 * Results:
1388 * Pointer to an ItemAttr record.
1389 *
1390 * Side effects:
1391 * Memory may be allocated for the ItemAttr record.
1392 *
1393 *----------------------------------------------------------------------
1394 */
1395
1396 static ItemAttr *
1397 ListboxGetItemAttributes(interp, listPtr, index)
1398 Tcl_Interp *interp; /* Pointer to the calling Tcl interpreter */
1399 Listbox *listPtr; /* Information about the listbox */
1400 int index; /* Index of the item to retrieve attributes
1401 * for */
1402 {
1403 int new;
1404 Tcl_HashEntry *entry;
1405 ItemAttr *attrs;
1406
1407 entry = Tcl_CreateHashEntry(listPtr->itemAttrTable, (char *)index, &new);
1408 if (new) {
1409 attrs = (ItemAttr *) ckalloc(sizeof(ItemAttr));
1410 attrs->border = NULL;
1411 attrs->selBorder = NULL;
1412 attrs->fgColor = NULL;
1413 attrs->selFgColor = NULL;
1414 Tk_InitOptions(interp, (char *)attrs, listPtr->itemAttrOptionTable,
1415 listPtr->tkwin);
1416 Tcl_SetHashValue(entry, (ClientData) attrs);
1417 }
1418 attrs = (ItemAttr *)Tcl_GetHashValue(entry);
1419 return attrs;
1420 }
1421
1422 /*
1423 *----------------------------------------------------------------------
1424 *
1425 * DestroyListbox --
1426 *
1427 * This procedure is invoked by Tcl_EventuallyFree or Tcl_Release
1428 * to clean up the internal structure of a listbox at a safe time
1429 * (when no-one is using it anymore).
1430 *
1431 * Results:
1432 * None.
1433 *
1434 * Side effects:
1435 * Everything associated with the listbox is freed up.
1436 *
1437 *----------------------------------------------------------------------
1438 */
1439
1440 static void
1441 DestroyListbox(memPtr)
1442 char *memPtr; /* Info about listbox widget. */
1443 {
1444 register Listbox *listPtr = (Listbox *) memPtr;
1445 Tcl_HashEntry *entry;
1446 Tcl_HashSearch search;
1447
1448 /* If we have an internal list object, free it */
1449 if (listPtr->listObj != NULL) {
1450 Tcl_DecrRefCount(listPtr->listObj);
1451 listPtr->listObj = NULL;
1452 }
1453
1454 if (listPtr->listVarName != NULL) {
1455 Lang_UntraceVar(listPtr->interp, listPtr->listVarName,
1456 TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
1457 ListboxListVarProc, (ClientData) listPtr);
1458 }
1459
1460 /* Free the selection hash table */
1461 Tcl_DeleteHashTable(listPtr->selection);
1462 ckfree((char *)listPtr->selection);
1463
1464 /* Free the item attribute hash table */
1465 for (entry = Tcl_FirstHashEntry(listPtr->itemAttrTable, &search);
1466 entry != NULL; entry = Tcl_NextHashEntry(&search)) {
1467 ckfree((char *)Tcl_GetHashValue(entry));
1468 }
1469 Tcl_DeleteHashTable(listPtr->itemAttrTable);
1470 ckfree((char *)listPtr->itemAttrTable);
1471
1472 /*
1473 * Free up all the stuff that requires special handling, then
1474 * let Tk_FreeOptions handle all the standard option-related
1475 * stuff.
1476 */
1477
1478 if (listPtr->textGC != None) {
1479 Tk_FreeGC(listPtr->display, listPtr->textGC);
1480 }
1481 if (listPtr->selTextGC != None) {
1482 Tk_FreeGC(listPtr->display, listPtr->selTextGC);
1483 }
1484 if (listPtr->gray != None) {
1485 Tk_FreeBitmap(Tk_Display(listPtr->tkwin), listPtr->gray);
1486 }
1487
1488 Tk_FreeConfigOptions((char *)listPtr, listPtr->optionTable,
1489 listPtr->tkwin);
1490 Tcl_Release((ClientData) listPtr->tkwin);
1491 listPtr->tkwin = NULL;
1492 ckfree((char *) listPtr);
1493 }
1494
1495 /*
1496 *----------------------------------------------------------------------
1497 *
1498 * DestroyListboxOptionTables --
1499 *
1500 * This procedure is registered as an exit callback when the listbox
1501 * command is first called. It cleans up the OptionTables structure
1502 * allocated by that command.
1503 *
1504 * Results:
1505 * None.
1506 *
1507 * Side effects:
1508 * Frees memory.
1509 *
1510 *----------------------------------------------------------------------
1511 */
1512
1513 static void
1514 DestroyListboxOptionTables(clientData, interp)
1515 ClientData clientData; /* Pointer to the OptionTables struct */
1516 Tcl_Interp *interp; /* Pointer to the calling interp */
1517 {
1518 ckfree((char *)clientData);
1519 return;
1520 }
1521
1522 /*
1523 *----------------------------------------------------------------------
1524 *
1525 * ConfigureListbox --
1526 *
1527 * This procedure is called to process an objv/objc list, plus
1528 * the Tk option database, in order to configure (or reconfigure)
1529 * a listbox widget.
1530 *
1531 * Results:
1532 * The return value is a standard Tcl result. If TCL_ERROR is
1533 * returned, then the interp's result contains an error message.
1534 *
1535 * Side effects:
1536 * Configuration information, such as colors, border width,
1537 * etc. get set for listPtr; old resources get freed,
1538 * if there were any.
1539 *
1540 *----------------------------------------------------------------------
1541 */
1542
1543 static int
1544 ConfigureListbox(interp, listPtr, objc, objv, flags)
1545 Tcl_Interp *interp; /* Used for error reporting. */
1546 register Listbox *listPtr; /* Information about widget; may or may
1547 * not already have values for some fields. */
1548 int objc; /* Number of valid entries in argv. */
1549 Tcl_Obj *CONST objv[]; /* Arguments. */
1550 int flags; /* Flags to pass to Tk_ConfigureWidget. */
1551 {
1552 Tk_SavedOptions savedOptions;
1553 Tcl_Obj *oldListObj = NULL;
1554 Tcl_Obj *errorResult = NULL;
1555 int oldExport, error;
1556
1557 oldExport = listPtr->exportSelection;
1558 if (listPtr->listVarName != NULL) {
1559 Lang_UntraceVar(interp, listPtr->listVarName,
1560 TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
1561 ListboxListVarProc, (ClientData) listPtr);
1562 }
1563
1564 for (error = 0; error <= 1; error++) {
1565 if (!error) {
1566 /*
1567 * First pass: set options to new values.
1568 */
1569
1570 if (Tk_SetOptions(interp, (char *) listPtr,
1571 listPtr->optionTable, objc, objv,
1572 listPtr->tkwin, &savedOptions, (int *) NULL) != TCL_OK) {
1573 continue;
1574 }
1575 } else {
1576 /*
1577 * Second pass: restore options to old values.
1578 */
1579
1580 errorResult = Tcl_GetObjResult(interp);
1581 Tcl_IncrRefCount(errorResult);
1582 Tk_RestoreSavedOptions(&savedOptions);
1583 }
1584
1585 /*
1586 * A few options need special processing, such as setting the
1587 * background from a 3-D border.
1588 */
1589
1590 Tk_SetBackgroundFromBorder(listPtr->tkwin, listPtr->normalBorder);
1591
1592 if (listPtr->highlightWidth < 0) {
1593 listPtr->highlightWidth = 0;
1594 }
1595 listPtr->inset = listPtr->highlightWidth + listPtr->borderWidth;
1596
1597 /*
1598 * Claim the selection if we've suddenly started exporting it and
1599 * there is a selection to export.
1600 */
1601
1602 if (listPtr->exportSelection && !oldExport
1603 && (listPtr->numSelected != 0)) {
1604 Tk_OwnSelection(listPtr->tkwin, XA_PRIMARY, ListboxLostSelection,
1605 (ClientData) listPtr);
1606 }
1607
1608 /* Verify the current status of the list var.
1609 * PREVIOUS STATE | NEW STATE | ACTION
1610 * ---------------+------------+----------------------------------
1611 * no listvar | listvar | If listvar does not exist, create
1612 * it and copy the internal list obj's
1613 * content to the new var. If it does
1614 * exist, toss the internal list obj.
1615 *
1616 * listvar | no listvar | Copy old listvar content to the
1617 * internal list obj
1618 *
1619 * listvar | listvar | no special action
1620 *
1621 * no listvar | no listvar | no special action
1622 */
1623 oldListObj = listPtr->listObj;
1624 if (listPtr->listVarName != NULL) {
1625 Tcl_Obj *listVarObj = Tcl_ObjGetVar2(interp, listPtr->listVarName,
1626 (char *) NULL, TCL_GLOBAL_ONLY);
1627 int dummy;
1628 if (listVarObj == NULL) {
1629 listVarObj = (oldListObj ? oldListObj : Tcl_NewObj());
1630 if (Tcl_ObjSetVar2(interp, listPtr->listVarName, (char *) NULL,
1631 listVarObj, TCL_GLOBAL_ONLY|TCL_LEAVE_ERR_MSG)
1632 == NULL) {
1633 if (oldListObj == NULL) {
1634 Tcl_DecrRefCount(listVarObj);
1635 }
1636 continue;
1637 }
1638 }
1639 /* Make sure the object is a good list object */
1640 if (Tcl_ListObjLength(listPtr->interp, listVarObj, &dummy)
1641 != TCL_OK) {
1642 Tcl_AppendResult(listPtr->interp,
1643 ": invalid -listvariable value", (char *) NULL);
1644 continue;
1645 }
1646
1647 listPtr->listObj = listVarObj;
1648 Lang_TraceVar(listPtr->interp, listPtr->listVarName,
1649 TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
1650 ListboxListVarProc, (ClientData) listPtr);
1651 } else if (listPtr->listObj == NULL) {
1652 listPtr->listObj = Tcl_NewObj();
1653 }
1654 Tcl_IncrRefCount(listPtr->listObj);
1655 if (oldListObj != NULL) {
1656 Tcl_DecrRefCount(oldListObj);
1657 }
1658 break;
1659 }
1660 if (!error) {
1661 Tk_FreeSavedOptions(&savedOptions);
1662 }
1663
1664 /* Make sure that the list length is correct */
1665 Tcl_ListObjLength(listPtr->interp, listPtr->listObj, &listPtr->nElements);
1666
1667 if (error) {
1668 Tcl_SetObjResult(interp, errorResult);
1669 Tcl_DecrRefCount(errorResult);
1670 return TCL_ERROR;
1671 } else {
1672 ListboxWorldChanged((ClientData) listPtr);
1673 return TCL_OK;
1674 }
1675 }
1676
1677 /*
1678 *----------------------------------------------------------------------
1679 *
1680 * ConfigureListboxItem --
1681 *
1682 * This procedure is called to process an objv/objc list, plus
1683 * the Tk option database, in order to configure (or reconfigure)
1684 * a listbox item.
1685 *
1686 * Results:
1687 * The return value is a standard Tcl result. If TCL_ERROR is
1688 * returned, then the interp's result contains an error message.
1689 *
1690 * Side effects:
1691 * Configuration information, such as colors, border width,
1692 * etc. get set for a listbox item; old resources get freed,
1693 * if there were any.
1694 *
1695 *----------------------------------------------------------------------
1696 */
1697
1698 static int
1699 ConfigureListboxItem(interp, listPtr, attrs, objc, objv, index)
1700 Tcl_Interp *interp; /* Used for error reporting. */
1701 register Listbox *listPtr; /* Information about widget; may or may
1702 * not already have values for some fields. */
1703 ItemAttr *attrs; /* Information about the item to configure */
1704 int objc; /* Number of valid entries in argv. */
1705 Tcl_Obj *CONST objv[]; /* Arguments. */
1706 int index; /* Index of the listbox item being configure */
1707 {
1708 Tk_SavedOptions savedOptions;
1709
1710 if (Tk_SetOptions(interp, (char *)attrs,
1711 listPtr->itemAttrOptionTable, objc, objv, listPtr->tkwin,
1712 &savedOptions, (int *)NULL) != TCL_OK) {
1713 Tk_RestoreSavedOptions(&savedOptions);
1714 return TCL_ERROR;
1715 }
1716 Tk_FreeSavedOptions(&savedOptions);
1717 /*
1718 * Redraw this index - ListboxWorldChanged would need to be called
1719 * if item attributes were checked in the "world".
1720 */
1721 EventuallyRedrawRange(listPtr, index, index);
1722 return TCL_OK;
1723 }
1724
1725 /*
1726 *---------------------------------------------------------------------------
1727 *
1728 * ListboxWorldChanged --
1729 *
1730 * This procedure is called when the world has changed in some
1731 * way and the widget needs to recompute all its graphics contexts
1732 * and determine its new geometry.
1733 *
1734 * Results:
1735 * None.
1736 *
1737 * Side effects:
1738 * Listbox will be relayed out and redisplayed.
1739 *
1740 *---------------------------------------------------------------------------
1741 */
1742
1743 static void
1744 ListboxWorldChanged(instanceData)
1745 ClientData instanceData; /* Information about widget. */
1746 {
1747 XGCValues gcValues;
1748 GC gc;
1749 unsigned long mask;
1750 Listbox *listPtr;
1751
1752 listPtr = (Listbox *) instanceData;
1753
1754 if (listPtr->state & STATE_NORMAL) {
1755 gcValues.foreground = listPtr->fgColorPtr->pixel;
1756 gcValues.graphics_exposures = False;
1757 mask = GCForeground | GCFont | GCGraphicsExposures;
1758 } else {
1759 if (listPtr->dfgColorPtr != NULL) {
1760 gcValues.foreground = listPtr->dfgColorPtr->pixel;
1761 gcValues.graphics_exposures = False;
1762 mask = GCForeground | GCFont | GCGraphicsExposures;
1763 } else {
1764 gcValues.foreground = listPtr->fgColorPtr->pixel;
1765 mask = GCForeground | GCFont;
1766 if (listPtr->gray == None) {
1767 listPtr->gray = Tk_GetBitmap(NULL, listPtr->tkwin, "gray50");
1768 }
1769 if (listPtr->gray != None) {
1770 gcValues.fill_style = FillStippled;
1771 gcValues.stipple = listPtr->gray;
1772 mask |= GCFillStyle | GCStipple;
1773 }
1774 }
1775 }
1776
1777 gcValues.font = Tk_FontId(listPtr->tkfont);
1778 gc = Tk_GetGC(listPtr->tkwin, mask, &gcValues);
1779 if (listPtr->textGC != None) {
1780 Tk_FreeGC(listPtr->display, listPtr->textGC);
1781 }
1782 listPtr->textGC = gc;
1783
1784 gcValues.foreground = listPtr->selFgColorPtr->pixel;
1785 gcValues.font = Tk_FontId(listPtr->tkfont);
1786 mask = GCForeground | GCFont;
1787 gc = Tk_GetGC(listPtr->tkwin, mask, &gcValues);
1788 if (listPtr->selTextGC != None) {
1789 Tk_FreeGC(listPtr->display, listPtr->selTextGC);
1790 }
1791 listPtr->selTextGC = gc;
1792
1793 /*
1794 * Register the desired geometry for the window and arrange for
1795 * the window to be redisplayed.
1796 */
1797
1798 ListboxComputeGeometry(listPtr, 1, 1, 1);
1799 listPtr->flags |= UPDATE_V_SCROLLBAR|UPDATE_H_SCROLLBAR;
1800 EventuallyRedrawRange(listPtr, 0, listPtr->nElements-1);
1801 }
1802
1803 /*
1804 *--------------------------------------------------------------
1805 *
1806 * DisplayListbox --
1807 *
1808 * This procedure redraws the contents of a listbox window.
1809 *
1810 * Results:
1811 * None.
1812 *
1813 * Side effects:
1814 * Information appears on the screen.
1815 *
1816 *--------------------------------------------------------------
1817 */
1818
1819 static void
1820 DisplayListbox(clientData)
1821 ClientData clientData; /* Information about window. */
1822 {
1823 register Listbox *listPtr = (Listbox *) clientData;
1824 register Tk_Window tkwin = listPtr->tkwin;
1825 GC gc;
1826 int i, limit, x, y, width, prevSelected, freeGC;
1827 Tk_FontMetrics fm;
1828 Tcl_Obj *curElement;
1829 Tcl_HashEntry *entry;
1830 char *stringRep;
1831 int stringLen;
1832 ItemAttr *attrs;
1833 Tk_3DBorder selectedBg;
1834 XGCValues gcValues;
1835 unsigned long mask;
1836 int left, right; /* Non-zero values here indicate
1837 * that the left or right edge of
1838 * the listbox is off-screen. */
1839 Pixmap pixmap;
1840
1841 listPtr->flags &= ~REDRAW_PENDING;
1842 if (listPtr->flags & LISTBOX_DELETED) {
1843 return;
1844 }
1845
1846 if (listPtr->flags & MAXWIDTH_IS_STALE) {
1847 ListboxComputeGeometry(listPtr, 0, 1, 0);
1848 listPtr->flags &= ~MAXWIDTH_IS_STALE;
1849 listPtr->flags |= UPDATE_H_SCROLLBAR;
1850 }
1851
1852 Tcl_Preserve((ClientData) listPtr);
1853 if (listPtr->flags & UPDATE_V_SCROLLBAR) {
1854 ListboxUpdateVScrollbar(listPtr);
1855 if ((listPtr->flags & LISTBOX_DELETED) || !Tk_IsMapped(tkwin)) {
1856 Tcl_Release((ClientData) listPtr);
1857 return;
1858 }
1859 }
1860 if (listPtr->flags & UPDATE_H_SCROLLBAR) {
1861 ListboxUpdateHScrollbar(listPtr);
1862 if ((listPtr->flags & LISTBOX_DELETED) || !Tk_IsMapped(tkwin)) {
1863 Tcl_Release((ClientData) listPtr);
1864 return;
1865 }
1866 }
1867 listPtr->flags &= ~(REDRAW_PENDING|UPDATE_V_SCROLLBAR|UPDATE_H_SCROLLBAR);
1868 Tcl_Release((ClientData) listPtr);
1869
1870 /*
1871 * Redrawing is done in a temporary pixmap that is allocated
1872 * here and freed at the end of the procedure. All drawing is
1873 * done to the pixmap, and the pixmap is copied to the screen
1874 * at the end of the procedure. This provides the smoothest
1875 * possible visual effects (no flashing on the screen).
1876 */
1877
1878 pixmap = Tk_GetPixmap(listPtr->display, Tk_WindowId(tkwin),
1879 Tk_Width(tkwin), Tk_Height(tkwin), Tk_Depth(tkwin));
1880 Tk_Fill3DRectangle(tkwin, pixmap, listPtr->normalBorder, 0, 0,
1881 Tk_Width(tkwin), Tk_Height(tkwin), 0, TK_RELIEF_FLAT);
1882
1883 /* Display each item in the listbox */
1884 limit = listPtr->topIndex + listPtr->fullLines + listPtr->partialLine - 1;
1885 if (limit >= listPtr->nElements) {
1886 limit = listPtr->nElements-1;
1887 }
1888 left = right = 0;
1889 if (listPtr->xOffset > 0) {
1890 left = listPtr->selBorderWidth+1;
1891 }
1892 if ((listPtr->maxWidth - listPtr->xOffset) > (Tk_Width(listPtr->tkwin)
1893 - 2*(listPtr->inset + listPtr->selBorderWidth))) {
1894 right = listPtr->selBorderWidth+1;
1895 }
1896 prevSelected = 0;
1897
1898 for (i = listPtr->topIndex; i <= limit; i++) {
1899 x = listPtr->inset;
1900 y = ((i - listPtr->topIndex) * listPtr->lineHeight)
1901 + listPtr->inset;
1902 gc = listPtr->textGC;
1903 freeGC = 0;
1904 /*
1905 * Lookup this item in the item attributes table, to see if it has
1906 * special foreground/background colors
1907 */
1908 entry = Tcl_FindHashEntry(listPtr->itemAttrTable, (char *)i);
1909
1910 /*
1911 * If the listbox is enabled, items may be drawn differently;
1912 * they may be drawn selected, or they may have special foreground
1913 * or background colors.
1914 */
1915 if (listPtr->state & STATE_NORMAL) {
1916 if (Tcl_FindHashEntry(listPtr->selection, (char *)i) != NULL) {
1917 /* Selected items are drawn differently. */
1918 gc = listPtr->selTextGC;
1919 width = Tk_Width(tkwin) - 2*listPtr->inset;
1920 selectedBg = listPtr->selBorder;
1921
1922 /* If there is attribute information for this item,
1923 * adjust the drawing accordingly */
1924 if (entry != NULL) {
1925 attrs = (ItemAttr *)Tcl_GetHashValue(entry);
1926 /* Default GC has the values from the widget at large */
1927 gcValues.foreground = listPtr->selFgColorPtr->pixel;
1928 gcValues.font = Tk_FontId(listPtr->tkfont);
1929 gcValues.graphics_exposures = False;
1930 mask = GCForeground | GCFont | GCGraphicsExposures;
1931
1932 if (attrs->selBorder != NULL) {
1933 selectedBg = attrs->selBorder;
1934 }
1935
1936 if (attrs->selFgColor != NULL) {
1937 gcValues.foreground = attrs->selFgColor->pixel;
1938 gc = Tk_GetGC(listPtr->tkwin, mask, &gcValues);
1939 freeGC = 1;
1940 }
1941 }
1942
1943 Tk_Fill3DRectangle(tkwin, pixmap, selectedBg, x, y,
1944 width, listPtr->lineHeight, 0, TK_RELIEF_FLAT);
1945
1946 /*
1947 * Draw beveled edges around the selection, if there are
1948 * visible edges next to this element. Special considerations:
1949 *
1950 * 1. The left and right bevels may not be visible if
1951 * horizontal scrolling is enabled (the "left" & "right"
1952 * variables are zero to indicate that the corresponding
1953 * bevel is visible).
1954 * 2. Top and bottom bevels are only drawn if this is the
1955 * first or last seleted item.
1956 * 3. If the left or right bevel isn't visible, then the
1957 * "left" & "right" vars, computed above, have non-zero
1958 * values that extend the top and bottom bevels so that
1959 * the mitered corners are off-screen.
1960 */
1961
1962 /* Draw left bevel */
1963 if (left == 0) {
1964 Tk_3DVerticalBevel(tkwin, pixmap, selectedBg,
1965 x, y, listPtr->selBorderWidth, listPtr->lineHeight,
1966 1, TK_RELIEF_RAISED);
1967 }
1968 /* Draw right bevel */
1969 if (right == 0) {
1970 Tk_3DVerticalBevel(tkwin, pixmap, selectedBg,
1971 x + width - listPtr->selBorderWidth, y,
1972 listPtr->selBorderWidth, listPtr->lineHeight,
1973 0, TK_RELIEF_RAISED);
1974 }
1975 /* Draw top bevel */
1976 if (!prevSelected) {
1977 Tk_3DHorizontalBevel(tkwin, pixmap, selectedBg,
1978 x-left, y, width+left+right,
1979 listPtr->selBorderWidth,
1980 1, 1, 1, TK_RELIEF_RAISED);
1981 }
1982 /* Draw bottom bevel */
1983 if (i + 1 == listPtr->nElements ||
1984 Tcl_FindHashEntry(listPtr->selection,
1985 (char *)(i + 1)) == NULL ) {
1986 Tk_3DHorizontalBevel(tkwin, pixmap, selectedBg, x-left,
1987 y + listPtr->lineHeight - listPtr->selBorderWidth,
1988 width+left+right, listPtr->selBorderWidth, 0, 0, 0,
1989 TK_RELIEF_RAISED);
1990 }
1991 prevSelected = 1;
1992 } else {
1993 /*
1994 * If there is an item attributes record for this item, draw
1995 * the background box and set the foreground color accordingly
1996 */
1997 if (entry != NULL) {
1998 attrs = (ItemAttr *)Tcl_GetHashValue(entry);
1999 gcValues.foreground = listPtr->fgColorPtr->pixel;
2000 gcValues.font = Tk_FontId(listPtr->tkfont);
2001 gcValues.graphics_exposures = False;
2002 mask = GCForeground | GCFont | GCGraphicsExposures;
2003
2004 /*
2005 * If the item has its own background color, draw it now.
2006 */
2007
2008 if (attrs->border != NULL) {
2009 width = Tk_Width(tkwin) - 2*listPtr->inset;
2010 Tk_Fill3DRectangle(tkwin, pixmap, attrs->border, x, y,
2011 width, listPtr->lineHeight, 0, TK_RELIEF_FLAT);
2012 }
2013
2014 /*
2015 * If the item has its own foreground, use it to override
2016 * the value in the gcValues structure.
2017 */
2018
2019 if ((listPtr->state & STATE_NORMAL)
2020 && attrs->fgColor != NULL) {
2021 gcValues.foreground = attrs->fgColor->pixel;
2022 gc = Tk_GetGC(listPtr->tkwin, mask, &gcValues);
2023 freeGC = 1;
2024 }
2025 }
2026 prevSelected = 0;
2027 }
2028 }
2029
2030 /* Draw the actual text of this item */
2031 Tk_GetFontMetrics(listPtr->tkfont, &fm);
2032 y += fm.ascent + listPtr->selBorderWidth;
2033 x = listPtr->inset + listPtr->selBorderWidth - listPtr->xOffset;
2034 Tcl_ListObjIndex(listPtr->interp, listPtr->listObj, i, &curElement);
2035 stringRep = Tcl_GetStringFromObj(curElement, &stringLen);
2036 Tk_DrawChars(listPtr->display, pixmap, gc, listPtr->tkfont,
2037 stringRep, stringLen, x, y);
2038
2039 /* If this is the active element, apply the activestyle to it. */
2040 if ((i == listPtr->active) && (listPtr->flags & GOT_FOCUS)) {
2041 if (listPtr->activeStyle == ACTIVE_STYLE_UNDERLINE) {
2042 /* Underline the text. */
2043 Tk_UnderlineChars(listPtr->display, pixmap, gc,
2044 listPtr->tkfont, stringRep, x, y, 0, stringLen);
2045 } else if (listPtr->activeStyle == ACTIVE_STYLE_DOTBOX) {
2046 #ifdef WIN32
2047 /*
2048 * This provides for exact default look and feel on Windows.
2049 */
2050 TkWinDCState state;
2051 HDC dc;
2052 RECT rect;
2053
2054 dc = TkWinGetDrawableDC(listPtr->display, pixmap, &state);
2055 rect.left = listPtr->inset;
2056 rect.top = ((i - listPtr->topIndex) * listPtr->lineHeight)
2057 + listPtr->inset;
2058 rect.right = rect.left + width;
2059 rect.bottom = rect.top + listPtr->lineHeight;
2060 DrawFocusRect(dc, &rect);
2061 TkWinReleaseDrawableDC(pixmap, dc, &state);
2062 #else
2063 /*
2064 * Draw a dotted box around the text.
2065 */
2066 x = listPtr->inset;
2067 y = ((i - listPtr->topIndex) * listPtr->lineHeight)
2068 + listPtr->inset;
2069 width = Tk_Width(tkwin) - 2*listPtr->inset - 1;
2070
2071 gcValues.line_style = LineOnOffDash;
2072 gcValues.line_width = listPtr->selBorderWidth;
2073 if (gcValues.line_width <= 0) {
2074 gcValues.line_width = 1;
2075 }
2076 gcValues.dash_offset = 0;
2077 gcValues.dashes = 1;
2078 /*
2079 * You would think the XSetDashes was necessary, but it
2080 * appears that the default dotting for just saying we
2081 * want dashes appears to work correctly.
2082 static char dashList[] = { 1 };
2083 static int dashLen = sizeof(dashList);
2084 XSetDashes(listPtr->display, gc, 0, dashList, dashLen);
2085 */
2086 mask = GCLineWidth | GCLineStyle | GCDashList | GCDashOffset;
2087 XChangeGC(listPtr->display, gc, mask, &gcValues);
2088 XDrawRectangle(listPtr->display, pixmap, gc, x, y,
2089 (unsigned) width, (unsigned) listPtr->lineHeight - 1);
2090 if (!freeGC) {
2091 /* Don't bother changing if it is about to be freed. */
2092 gcValues.line_style = LineSolid;
2093 XChangeGC(listPtr->display, gc, GCLineStyle, &gcValues);
2094 }
2095 #endif
2096 }
2097 }
2098
2099 if (freeGC) {
2100 Tk_FreeGC(listPtr->display, gc);
2101 }
2102 }
2103
2104 /*
2105 * Redraw the border for the listbox to make sure that it's on top
2106 * of any of the text of the listbox entries.
2107 */
2108
2109 Tk_Draw3DRectangle(tkwin, pixmap, listPtr->normalBorder,
2110 listPtr->highlightWidth, listPtr->highlightWidth,
2111 Tk_Width(tkwin) - 2*listPtr->highlightWidth,
2112 Tk_Height(tkwin) - 2*listPtr->highlightWidth,
2113 listPtr->borderWidth, listPtr->relief);
2114 if (listPtr->highlightWidth > 0) {
2115 GC fgGC, bgGC;
2116
2117 bgGC = Tk_GCForColor(listPtr->highlightBgColorPtr, pixmap);
2118 if (listPtr->flags & GOT_FOCUS) {
2119 fgGC = Tk_GCForColor(listPtr->highlightColorPtr, pixmap);
2120 TkpDrawHighlightBorder(tkwin, fgGC, bgGC,
2121 listPtr->highlightWidth, pixmap);
2122 } else {
2123 TkpDrawHighlightBorder(tkwin, bgGC, bgGC,
2124 listPtr->highlightWidth, pixmap);
2125 }
2126 }
2127 XCopyArea(listPtr->display, pixmap, Tk_WindowId(tkwin),
2128 listPtr->textGC, 0, 0, (unsigned) Tk_Width(tkwin),
2129 (unsigned) Tk_Height(tkwin), 0, 0);
2130 Tk_FreePixmap(listPtr->display, pixmap);
2131 }
2132
2133 /*
2134 *----------------------------------------------------------------------
2135 *
2136 * ListboxComputeGeometry --
2137 *
2138 * This procedure is invoked to recompute geometry information
2139 * such as the sizes of the elements and the overall dimensions
2140 * desired for the listbox.
2141 *
2142 * Results:
2143 * None.
2144 *
2145 * Side effects:
2146 * Geometry information is updated and a new requested size is
2147 * registered for the widget. Internal border and gridding
2148 * information is also set.
2149 *
2150 *----------------------------------------------------------------------
2151 */
2152
2153 static void
2154 ListboxComputeGeometry(listPtr, fontChanged, maxIsStale, updateGrid)
2155 Listbox *listPtr; /* Listbox whose geometry is to be
2156 * recomputed. */
2157 int fontChanged; /* Non-zero means the font may have changed
2158 * so per-element width information also
2159 * has to be computed. */
2160 int maxIsStale; /* Non-zero means the "maxWidth" field may
2161 * no longer be up-to-date and must
2162 * be recomputed. If fontChanged is 1 then
2163 * this must be 1. */
2164 int updateGrid; /* Non-zero means call Tk_SetGrid or
2165 * Tk_UnsetGrid to update gridding for
2166 * the window. */
2167 {
2168 int width, height, pixelWidth, pixelHeight;
2169 Tk_FontMetrics fm;
2170 Tcl_Obj *element;
2171 int textLength;
2172 char *text;
2173 int i, result;
2174
2175 if (fontChanged || maxIsStale) {
2176 listPtr->xScrollUnit = Tk_TextWidth(listPtr->tkfont, "0", 1);
2177 if (listPtr->xScrollUnit == 0) {
2178 listPtr->xScrollUnit = 1;
2179 }
2180 listPtr->maxWidth = 0;
2181 for (i = 0; i < listPtr->nElements; i++) {
2182 /* Compute the pixel width of the current element */
2183 result = Tcl_ListObjIndex(listPtr->interp, listPtr->listObj, i,
2184 &element);
2185 if (result != TCL_OK) {
2186 continue;
2187 }
2188 text = Tcl_GetStringFromObj(element, &textLength);
2189 Tk_GetFontMetrics(listPtr->tkfont, &fm);
2190 pixelWidth = Tk_TextWidth(listPtr->tkfont, text, textLength);
2191 if (pixelWidth > listPtr->maxWidth) {
2192 listPtr->maxWidth = pixelWidth;
2193 }
2194 }
2195 }
2196
2197 Tk_GetFontMetrics(listPtr->tkfont, &fm);
2198 listPtr->lineHeight = fm.linespace + 1 + 2*listPtr->selBorderWidth;
2199 width = listPtr->width;
2200 if (width <= 0) {
2201 width = (listPtr->maxWidth + listPtr->xScrollUnit - 1)
2202 /listPtr->xScrollUnit;
2203 if (width < 1) {
2204 width = 1;
2205 }
2206 }
2207 pixelWidth = width*listPtr->xScrollUnit + 2*listPtr->inset
2208 + 2*listPtr->selBorderWidth;
2209 height = listPtr->height;
2210 if (listPtr->height <= 0) {
2211 height = listPtr->nElements;
2212 if (height < 1) {
2213 height = 1;
2214 }
2215 }
2216 pixelHeight = height*listPtr->lineHeight + 2*listPtr->inset;
2217 Tk_GeometryRequest(listPtr->tkwin, pixelWidth, pixelHeight);
2218 Tk_SetInternalBorder(listPtr->tkwin, listPtr->inset);
2219 if (updateGrid) {
2220 if (listPtr->setGrid) {
2221 Tk_SetGrid(listPtr->tkwin, width, height, listPtr->xScrollUnit,
2222 listPtr->lineHeight);
2223 } else {
2224 Tk_UnsetGrid(listPtr->tkwin);
2225 }
2226 }
2227 }
2228
2229 /*
2230 *----------------------------------------------------------------------
2231 *
2232 * ListboxInsertSubCmd --
2233 *
2234 * This procedure is invoked to handle the listbox "insert"
2235 * subcommand.
2236 *
2237 * Results:
2238 * Standard Tcl result.
2239 *
2240 * Side effects:
2241 * New elements are added to the listbox pointed to by listPtr;
2242 * a refresh callback is registered for the listbox.
2243 *
2244 *----------------------------------------------------------------------
2245 */
2246
2247 static int
2248 ListboxInsertSubCmd(listPtr, index, objc, objv)
2249 register Listbox *listPtr; /* Listbox that is to get the new
2250 * elements. */
2251 int index; /* Add the new elements before this
2252 * element. */
2253 int objc; /* Number of new elements to add. */
2254 Tcl_Obj *CONST objv[]; /* New elements (one per entry). */
2255 {
2256 int i, oldMaxWidth;
2257 Tcl_Obj *newListObj;
2258 int pixelWidth;
2259 int result;
2260 char *stringRep;
2261 int length;
2262 #ifdef _LANG
2263 int refFlag = 0;
2264 #endif
2265
2266 oldMaxWidth = listPtr->maxWidth;
2267 for (i = 0; i < objc; i++) {
2268 /*
2269 * Check if any of the new elements are wider than the current widest;
2270 * if so, update our notion of "widest."
2271 */
2272 stringRep = Tcl_GetStringFromObj(objv[i], &length);
2273 pixelWidth = Tk_TextWidth(listPtr->tkfont, stringRep, length);
2274 if (pixelWidth > listPtr->maxWidth) {
2275 listPtr->maxWidth = pixelWidth;
2276 }
2277 }
2278
2279 /* Adjust selection and attribute information for every index after
2280 * the first index */
2281 MigrateHashEntries(listPtr->selection, index, listPtr->nElements-1, objc);
2282 MigrateHashEntries(listPtr->itemAttrTable, index, listPtr->nElements-1,
2283 objc);
2284
2285 /* If the object is shared, duplicate it before writing to it */
2286 if (Tcl_IsShared(listPtr->listObj)) {
2287 newListObj = Tcl_DuplicateObj(listPtr->listObj);
2288 #ifdef _LANG
2289 refFlag = 1;
2290 #endif
2291 } else {
2292 newListObj = listPtr->listObj;
2293 }
2294 result =
2295 Tcl_ListObjReplace(listPtr->interp, newListObj, index, 0, objc, objv);
2296 if (result != TCL_OK) {
2297 #ifdef _LANG
2298 if (refFlag) {
2299 Tcl_DecrRefCount(newListObj);
2300 }
2301 #endif
2302 return result;
2303 }
2304
2305 #ifdef _LANG
2306 if (!refFlag) {
2307 #endif
2308 Tcl_IncrRefCount(newListObj);
2309 #ifdef _LANG
2310 }
2311 #endif
2312 /* Clean up the old reference */
2313 Tcl_DecrRefCount(listPtr->listObj);
2314
2315 /* Set the internal pointer to the new obj */
2316 listPtr->listObj = newListObj;
2317
2318 /* If there is a listvar, make sure it points at the new object */
2319 if (listPtr->listVarName != NULL) {
2320 if (Tcl_ObjSetVar2(listPtr->interp, listPtr->listVarName,
2321 (char *)NULL, newListObj, TCL_GLOBAL_ONLY) == NULL) {
2322 Tcl_DecrRefCount(newListObj);
2323 return TCL_ERROR;
2324 }
2325 }
2326
2327 /* Get the new list length */
2328 Tcl_ListObjLength(listPtr->interp, listPtr->listObj, &listPtr->nElements);
2329
2330 /*
2331 * Update the "special" indices (anchor, topIndex, active) to account
2332 * for the renumbering that just occurred. Then arrange for the new
2333 * information to be displayed.
2334 */
2335
2336 if (index <= listPtr->selectAnchor) {
2337 listPtr->selectAnchor += objc;
2338 }
2339 if (index < listPtr->topIndex) {
2340 listPtr->topIndex += objc;
2341 }
2342 if (index <= listPtr->active) {
2343 listPtr->active += objc;
2344 if ((listPtr->active >= listPtr->nElements) &&
2345 (listPtr->nElements > 0)) {
2346 listPtr->active = listPtr->nElements-1;
2347 }
2348 }
2349 listPtr->flags |= UPDATE_V_SCROLLBAR;
2350 if (listPtr->maxWidth != oldMaxWidth) {
2351 listPtr->flags |= UPDATE_H_SCROLLBAR;
2352 }
2353 ListboxComputeGeometry(listPtr, 0, 0, 0);
2354 EventuallyRedrawRange(listPtr, index, listPtr->nElements-1);
2355 return TCL_OK;
2356 }
2357
2358 /*
2359 *----------------------------------------------------------------------
2360 *
2361 * ListboxDeleteSubCmd --
2362 *
2363 * Process a listbox "delete" subcommand by removing one or more
2364 * elements from a listbox widget.
2365 *
2366 * Results:
2367 * Standard Tcl result.
2368 *
2369 * Side effects:
2370 * The listbox will be modified and (eventually) redisplayed.
2371 *
2372 *----------------------------------------------------------------------
2373 */
2374
2375 static int
2376 ListboxDeleteSubCmd(listPtr, first, last)
2377 register Listbox *listPtr; /* Listbox widget to modify. */
2378 int first; /* Index of first element to delete. */
2379 int last; /* Index of last element to delete. */
2380 {
2381 int count, i, widthChanged;
2382 Tcl_Obj *newListObj;
2383 Tcl_Obj *element;
2384 int length;
2385 char *stringRep;
2386 int result;
2387 int pixelWidth;
2388 Tcl_HashEntry *entry;
2389
2390 /*
2391 * Adjust the range to fit within the existing elements of the
2392 * listbox, and make sure there's something to delete.
2393 */
2394
2395 if (first < 0) {
2396 first = 0;
2397 }
2398 if (last >= listPtr->nElements) {
2399 last = listPtr->nElements-1;
2400 }
2401 count = last + 1 - first;
2402 if (count <= 0) {
2403 return TCL_OK;
2404 }
2405
2406 /*
2407 * Foreach deleted index we must:
2408 * a) remove selection information
2409 * b) check the width of the element; if it is equal to the max, set
2410 * widthChanged to 1, because it may be the only element with that
2411 * width
2412 */
2413 widthChanged = 0;
2414 for (i = first; i <= last; i++) {
2415 /* Remove selection information */
2416 entry = Tcl_FindHashEntry(listPtr->selection, (char *)i);
2417 if (entry != NULL) {
2418 listPtr->numSelected--;
2419 Tcl_DeleteHashEntry(entry);
2420 }
2421
2422 entry = Tcl_FindHashEntry(listPtr->itemAttrTable, (char *)i);
2423 if (entry != NULL) {
2424 ckfree((char *)Tcl_GetHashValue(entry));
2425 Tcl_DeleteHashEntry(entry);
2426 }
2427
2428 /* Check width of the element. We only have to check if widthChanged
2429 * has not already been set to 1, because we only need one maxWidth
2430 * element to disappear for us to have to recompute the width
2431 */
2432 if (widthChanged == 0) {
2433 Tcl_ListObjIndex(listPtr->interp, listPtr->listObj, i, &element);
2434 stringRep = Tcl_GetStringFromObj(element, &length);
2435 pixelWidth = Tk_TextWidth(listPtr->tkfont, stringRep, length);
2436 if (pixelWidth == listPtr->maxWidth) {
2437 widthChanged = 1;
2438 }
2439 }
2440 }
2441
2442 /* Adjust selection and attribute info for indices after lastIndex */
2443 MigrateHashEntries(listPtr->selection, last+1,
2444 listPtr->nElements-1, count*-1);
2445 MigrateHashEntries(listPtr->itemAttrTable, last+1,
2446 listPtr->nElements-1, count*-1);
2447
2448 /* Delete the requested elements */
2449 if (Tcl_IsShared(listPtr->listObj)) {
2450 newListObj = Tcl_DuplicateObj(listPtr->listObj);
2451 } else {
2452 newListObj = listPtr->listObj;
2453 }
2454 result = Tcl_ListObjReplace(listPtr->interp,
2455 newListObj, first, count, 0, NULL);
2456 if (result != TCL_OK) {
2457 return result;
2458 }
2459
2460 Tcl_IncrRefCount(newListObj);
2461 /* Clean up the old reference */
2462 Tcl_DecrRefCount(listPtr->listObj);
2463
2464 /* Set the internal pointer to the new obj */
2465 listPtr->listObj = newListObj;
2466
2467 /* Get the new list length */
2468 Tcl_ListObjLength(listPtr->interp, listPtr->listObj, &listPtr->nElements);
2469
2470 /* If there is a listvar, make sure it points at the new object */
2471 if (listPtr->listVarName != NULL) {
2472 if (Tcl_ObjSetVar2(listPtr->interp, listPtr->listVarName,
2473 (char *)NULL, newListObj, TCL_GLOBAL_ONLY) == NULL) {
2474 Tcl_DecrRefCount(newListObj);
2475 return TCL_ERROR;
2476 }
2477 }
2478
2479 /*
2480 * Update the selection and viewing information to reflect the change
2481 * in the element numbering, and redisplay to slide information up over
2482 * the elements that were deleted.
2483 */
2484
2485 if (first <= listPtr->selectAnchor) {
2486 listPtr->selectAnchor -= count;
2487 if (listPtr->selectAnchor < first) {
2488 listPtr->selectAnchor = first;
2489 }
2490 }
2491 if (first <= listPtr->topIndex) {
2492 listPtr->topIndex -= count;
2493 if (listPtr->topIndex < first) {
2494 listPtr->topIndex = first;
2495 }
2496 }
2497 if (listPtr->topIndex > (listPtr->nElements - listPtr->fullLines)) {
2498 listPtr->topIndex = listPtr->nElements - listPtr->fullLines;
2499 if (listPtr->topIndex < 0) {
2500 listPtr->topIndex = 0;
2501 }
2502 }
2503 if (listPtr->active > last) {
2504 listPtr->active -= count;
2505 } else if (listPtr->active >= first) {
2506 listPtr->active = first;
2507 if ((listPtr->active >= listPtr->nElements) &&
2508 (listPtr->nElements > 0)) {
2509 listPtr->active = listPtr->nElements-1;
2510 }
2511 }
2512 listPtr->flags |= UPDATE_V_SCROLLBAR;
2513 ListboxComputeGeometry(listPtr, 0, widthChanged, 0);
2514 if (widthChanged) {
2515 listPtr->flags |= UPDATE_H_SCROLLBAR;
2516 }
2517 EventuallyRedrawRange(listPtr, first, listPtr->nElements-1);
2518 return TCL_OK;
2519 }
2520
2521 /*
2522 *--------------------------------------------------------------
2523 *
2524 * ListboxEventProc --
2525 *
2526 * This procedure is invoked by the Tk dispatcher for various
2527 * events on listboxes.
2528 *
2529 * Results:
2530 * None.
2531 *
2532 * Side effects:
2533 * When the window gets deleted, internal structures get
2534 * cleaned up. When it gets exposed, it is redisplayed.
2535 *
2536 *--------------------------------------------------------------
2537 */
2538
2539 static void
2540 ListboxEventProc(clientData, eventPtr)
2541 ClientData clientData; /* Information about window. */
2542 XEvent *eventPtr; /* Information about event. */
2543 {
2544 Listbox *listPtr = (Listbox *) clientData;
2545
2546 if (eventPtr->type == Expose) {
2547 EventuallyRedrawRange(listPtr,
2548 NearestListboxElement(listPtr, eventPtr->xexpose.y),
2549 NearestListboxElement(listPtr, eventPtr->xexpose.y
2550 + eventPtr->xexpose.height));
2551 } else if (eventPtr->type == DestroyNotify) {
2552 if (!(listPtr->flags & LISTBOX_DELETED)) {
2553 listPtr->flags |= LISTBOX_DELETED;
2554 Tcl_DeleteCommandFromToken(listPtr->interp, listPtr->widgetCmd);
2555 if (listPtr->setGrid) {
2556 Tk_UnsetGrid(listPtr->tkwin);
2557 }
2558 if (listPtr->flags & REDRAW_PENDING) {
2559 Tcl_CancelIdleCall(DisplayListbox, clientData);
2560 }
2561 Tcl_EventuallyFree(clientData, DestroyListbox);
2562 }
2563 } else if (eventPtr->type == ConfigureNotify) {
2564 int vertSpace;
2565
2566 vertSpace = Tk_Height(listPtr->tkwin) - 2*listPtr->inset;
2567 listPtr->fullLines = vertSpace / listPtr->lineHeight;
2568 if ((listPtr->fullLines*listPtr->lineHeight) < vertSpace) {
2569 listPtr->partialLine = 1;
2570 } else {
2571 listPtr->partialLine = 0;
2572 }
2573 listPtr->flags |= UPDATE_V_SCROLLBAR|UPDATE_H_SCROLLBAR;
2574 ChangeListboxView(listPtr, listPtr->topIndex);
2575 ChangeListboxOffset(listPtr, listPtr->xOffset);
2576
2577 /*
2578 * Redraw the whole listbox. It's hard to tell what needs
2579 * to be redrawn (e.g. if the listbox has shrunk then we
2580 * may only need to redraw the borders), so just redraw
2581 * everything for safety.
2582 */
2583
2584 EventuallyRedrawRange(listPtr, 0, listPtr->nElements-1);
2585 } else if (eventPtr->type == FocusIn) {
2586 if (eventPtr->xfocus.detail != NotifyInferior) {
2587 listPtr->flags |= GOT_FOCUS;
2588 EventuallyRedrawRange(listPtr, 0, listPtr->nElements-1);
2589 }
2590 } else if (eventPtr->type == FocusOut) {
2591 if (eventPtr->xfocus.detail != NotifyInferior) {
2592 listPtr->flags &= ~GOT_FOCUS;
2593 EventuallyRedrawRange(listPtr, 0, listPtr->nElements-1);
2594 }
2595 }
2596 }
2597
2598 /*
2599 *----------------------------------------------------------------------
2600 *
2601 * ListboxCmdDeletedProc --
2602 *
2603 * This procedure is invoked when a widget command is deleted. If
2604 * the widget isn't already in the process of being destroyed,
2605 * this command destroys it.
2606 *
2607 * Results:
2608 * None.
2609 *
2610 * Side effects:
2611 * The widget is destroyed.
2612 *
2613 *----------------------------------------------------------------------
2614 */
2615
2616 static void
2617 ListboxCmdDeletedProc(clientData)
2618 ClientData clientData; /* Pointer to widget record for widget. */
2619 {
2620 Listbox *listPtr = (Listbox *) clientData;
2621
2622 /*
2623 * This procedure could be invoked either because the window was
2624 * destroyed and the command was then deleted (in which case tkwin
2625 * is NULL) or because the command was deleted, and then this procedure
2626 * destroys the widget.
2627 */
2628
2629 if (!(listPtr->flags & LISTBOX_DELETED)) {
2630 Tk_DestroyWindow(listPtr->tkwin);
2631 }
2632 }
2633
2634 /*
2635 *--------------------------------------------------------------
2636 *
2637 * GetListboxIndex --
2638 *
2639 * Parse an index into a listbox and return either its value
2640 * or an error.
2641 *
2642 * Results:
2643 * A standard Tcl result. If all went well, then *indexPtr is
2644 * filled in with the index (into listPtr) corresponding to
2645 * string. Otherwise an error message is left in the interp's result.
2646 *
2647 * Side effects:
2648 * None.
2649 *
2650 *--------------------------------------------------------------
2651 */
2652
2653 static int
2654 GetListboxIndex(interp, listPtr, indexObj, endIsSize, indexPtr)
2655 Tcl_Interp *interp; /* For error messages. */
2656 Listbox *listPtr; /* Listbox for which the index is being
2657 * specified. */
2658 Tcl_Obj *indexObj; /* Specifies an element in the listbox. */
2659 int endIsSize; /* If 1, "end" refers to the number of
2660 * entries in the listbox. If 0, "end"
2661 * refers to 1 less than the number of
2662 * entries. */
2663 int *indexPtr; /* Where to store converted index. */
2664 {
2665 int result;
2666 int index;
2667 char *stringRep;
2668
2669 /* First see if the index is one of the named indices */
2670 result = Tcl_GetIndexFromObj(NULL, indexObj, indexNames, "", 0, &index);
2671 if (result == TCL_OK) {
2672 switch (index) {
2673 case INDEX_ACTIVE: {
2674 /* "active" index */
2675 *indexPtr = listPtr->active;
2676 break;
2677 }
2678
2679 case INDEX_ANCHOR: {
2680 /* "anchor" index */
2681 *indexPtr = listPtr->selectAnchor;
2682 break;
2683 }
2684
2685 case INDEX_END: {
2686 /* "end" index */
2687 if (endIsSize) {
2688 *indexPtr = listPtr->nElements;
2689 } else {
2690 *indexPtr = listPtr->nElements - 1;
2691 }
2692 break;
2693 }
2694 }
2695 return TCL_OK;
2696 }
2697
2698 /* The index didn't match any of the named indices; maybe it's an @x,y */
2699 stringRep = Tcl_GetString(indexObj);
2700 if (stringRep[0] == '@') {
2701 /* @x,y index */
2702 int y;
2703 char *start, *end;
2704 start = stringRep + 1;
2705 strtol(start, &end, 0);
2706 if ((start == end) || (*end != ',')) {
2707 Tcl_AppendResult(interp, "bad listbox index \"", stringRep,
2708 "\": must be active, anchor, end, @x,y, or a number",
2709 (char *)NULL);
2710 return TCL_ERROR;
2711 }
2712 start = end+1;
2713 y = strtol(start, &end, 0);
2714 if ((start == end) || (*end != '\0')) {
2715 Tcl_AppendResult(interp, "bad listbox index \"", stringRep,
2716 "\": must be active, anchor, end, @x,y, or a number",
2717 (char *)NULL);
2718 return TCL_ERROR;
2719 }
2720 *indexPtr = NearestListboxElement(listPtr, y);
2721 return TCL_OK;
2722 }
2723
2724 /* Maybe the index is just an integer */
2725 if (Tcl_GetIntFromObj(interp, indexObj, indexPtr) == TCL_OK) {
2726 return TCL_OK;
2727 }
2728
2729 /* Everything failed, nothing matched. Throw up an error message */
2730 Tcl_ResetResult(interp);
2731 Tcl_AppendResult(interp, "bad listbox index \"",
2732 Tcl_GetString(indexObj), "\": must be active, anchor, ",
2733 "end, @x,y, or a number", (char *) NULL);
2734 return TCL_ERROR;
2735 }
2736
2737 /*
2738 *----------------------------------------------------------------------
2739 *
2740 * ChangeListboxView --
2741 *
2742 * Change the view on a listbox widget so that a given element
2743 * is displayed at the top.
2744 *
2745 * Results:
2746 * None.
2747 *
2748 * Side effects:
2749 * What's displayed on the screen is changed. If there is a
2750 * scrollbar associated with this widget, then the scrollbar
2751 * is instructed to change its display too.
2752 *
2753 *----------------------------------------------------------------------
2754 */
2755
2756 static void
2757 ChangeListboxView(listPtr, index)
2758 register Listbox *listPtr; /* Information about widget. */
2759 int index; /* Index of element in listPtr
2760 * that should now appear at the
2761 * top of the listbox. */
2762 {
2763 if (index >= (listPtr->nElements - listPtr->fullLines)) {
2764 index = listPtr->nElements - listPtr->fullLines;
2765 }
2766 if (index < 0) {
2767 index = 0;
2768 }
2769 if (listPtr->topIndex != index) {
2770 listPtr->topIndex = index;
2771 EventuallyRedrawRange(listPtr, 0, listPtr->nElements-1);
2772 listPtr->flags |= UPDATE_V_SCROLLBAR;
2773 }
2774 }
2775
2776 /*
2777 *----------------------------------------------------------------------
2778 *
2779 * ChangListboxOffset --
2780 *
2781 * Change the horizontal offset for a listbox.
2782 *
2783 * Results:
2784 * None.
2785 *
2786 * Side effects:
2787 * The listbox may be redrawn to reflect its new horizontal
2788 * offset.
2789 *
2790 *----------------------------------------------------------------------
2791 */
2792
2793 static void
2794 ChangeListboxOffset(listPtr, offset)
2795 register Listbox *listPtr; /* Information about widget. */
2796 int offset; /* Desired new "xOffset" for
2797 * listbox. */
2798 {
2799 int maxOffset;
2800
2801 /*
2802 * Make sure that the new offset is within the allowable range, and
2803 * round it off to an even multiple of xScrollUnit.
2804 *
2805 * Add half a scroll unit to do entry/text-like synchronization.
2806 * [Bug #225025]
2807 */
2808
2809 offset += listPtr->xScrollUnit / 2;
2810 maxOffset = listPtr->maxWidth - (Tk_Width(listPtr->tkwin) -
2811 2*listPtr->inset - 2*listPtr->selBorderWidth)
2812 + listPtr->xScrollUnit - 1;
2813 if (offset > maxOffset) {
2814 offset = maxOffset;
2815 }
2816 if (offset < 0) {
2817 offset = 0;
2818 }
2819 offset -= offset % listPtr->xScrollUnit;
2820 if (offset != listPtr->xOffset) {
2821 listPtr->xOffset = offset;
2822 listPtr->flags |= UPDATE_H_SCROLLBAR;
2823 EventuallyRedrawRange(listPtr, 0, listPtr->nElements-1);
2824 }
2825 }
2826
2827 /*
2828 *----------------------------------------------------------------------
2829 *
2830 * ListboxScanTo --
2831 *
2832 * Given a point (presumably of the curent mouse location)
2833 * drag the view in the window to implement the scan operation.
2834 *
2835 * Results:
2836 * None.
2837 *
2838 * Side effects:
2839 * The view in the window may change.
2840 *
2841 *----------------------------------------------------------------------
2842 */
2843
2844 static void
2845 ListboxScanTo(listPtr, x, y)
2846 register Listbox *listPtr; /* Information about widget. */
2847 int x; /* X-coordinate to use for scan
2848 * operation. */
2849 int y; /* Y-coordinate to use for scan
2850 * operation. */
2851 {
2852 int newTopIndex, newOffset, maxIndex, maxOffset;
2853
2854 maxIndex = listPtr->nElements - listPtr->fullLines;
2855 maxOffset = listPtr->maxWidth + (listPtr->xScrollUnit - 1)
2856 - (Tk_Width(listPtr->tkwin) - 2*listPtr->inset
2857 - 2*listPtr->selBorderWidth - listPtr->xScrollUnit);
2858
2859 /*
2860 * Compute new top line for screen by amplifying the difference
2861 * between the current position and the place where the scan
2862 * started (the "mark" position). If we run off the top or bottom
2863 * of the list, then reset the mark point so that the current
2864 * position continues to correspond to the edge of the window.
2865 * This means that the picture will start dragging as soon as the
2866 * mouse reverses direction (without this reset, might have to slide
2867 * mouse a long ways back before the picture starts moving again).
2868 */
2869
2870 newTopIndex = listPtr->scanMarkYIndex
2871 - (10*(y - listPtr->scanMarkY))/listPtr->lineHeight;
2872 if (newTopIndex > maxIndex) {
2873 newTopIndex = listPtr->scanMarkYIndex = maxIndex;
2874 listPtr->scanMarkY = y;
2875 } else if (newTopIndex < 0) {
2876 newTopIndex = listPtr->scanMarkYIndex = 0;
2877 listPtr->scanMarkY = y;
2878 }
2879 ChangeListboxView(listPtr, newTopIndex);
2880
2881 /*
2882 * Compute new left edge for display in a similar fashion by amplifying
2883 * the difference between the current position and the place where the
2884 * scan started.
2885 */
2886
2887 newOffset = listPtr->scanMarkXOffset - (10*(x - listPtr->scanMarkX));
2888 if (newOffset > maxOffset) {
2889 newOffset = listPtr->scanMarkXOffset = maxOffset;
2890 listPtr->scanMarkX = x;
2891 } else if (newOffset < 0) {
2892 newOffset = listPtr->scanMarkXOffset = 0;
2893 listPtr->scanMarkX = x;
2894 }
2895 ChangeListboxOffset(listPtr, newOffset);
2896 }
2897
2898 /*
2899 *----------------------------------------------------------------------
2900 *
2901 * NearestListboxElement --
2902 *
2903 * Given a y-coordinate inside a listbox, compute the index of
2904 * the element under that y-coordinate (or closest to that
2905 * y-coordinate).
2906 *
2907 * Results:
2908 * The return value is an index of an element of listPtr. If
2909 * listPtr has no elements, then 0 is always returned.
2910 *
2911 * Side effects:
2912 * None.
2913 *
2914 *----------------------------------------------------------------------
2915 */
2916
2917 static int
2918 NearestListboxElement(listPtr, y)
2919 register Listbox *listPtr; /* Information about widget. */
2920 int y; /* Y-coordinate in listPtr's window. */
2921 {
2922 int index;
2923
2924 index = (y - listPtr->inset)/listPtr->lineHeight;
2925 if (index >= (listPtr->fullLines + listPtr->partialLine)) {
2926 index = listPtr->fullLines + listPtr->partialLine - 1;
2927 }
2928 if (index < 0) {
2929 index = 0;
2930 }
2931 index += listPtr->topIndex;
2932 if (index >= listPtr->nElements) {
2933 index = listPtr->nElements-1;
2934 }
2935 return index;
2936 }
2937
2938 /*
2939 *----------------------------------------------------------------------
2940 *
2941 * ListboxSelect --
2942 *
2943 * Select or deselect one or more elements in a listbox..
2944 *
2945 * Results:
2946 * Standard Tcl result.
2947 *
2948 * Side effects:
2949 * All of the elements in the range between first and last are
2950 * marked as either selected or deselected, depending on the
2951 * "select" argument. Any items whose state changes are redisplayed.
2952 * The selection is claimed from X when the number of selected
2953 * elements changes from zero to non-zero.
2954 *
2955 *----------------------------------------------------------------------
2956 */
2957
2958 static int
2959 ListboxSelect(listPtr, first, last, select)
2960 register Listbox *listPtr; /* Information about widget. */
2961 int first; /* Index of first element to
2962 * select or deselect. */
2963 int last; /* Index of last element to
2964 * select or deselect. */
2965 int select; /* 1 means select items, 0 means
2966 * deselect them. */
2967 {
2968 int i, firstRedisplay, increment, oldCount;
2969 Tcl_HashEntry *entry;
2970 int new;
2971
2972 if (last < first) {
2973 i = first;
2974 first = last;
2975 last = i;
2976 }
2977 if ((last < 0) || (first >= listPtr->nElements)) {
2978 return TCL_OK;
2979 }
2980 if (first < 0) {
2981 first = 0;
2982 }
2983 if (last >= listPtr->nElements) {
2984 last = listPtr->nElements - 1;
2985 }
2986 oldCount = listPtr->numSelected;
2987 firstRedisplay = -1;
2988 increment = select ? 1 : -1;
2989
2990 /*
2991 * For each index in the range, find it in our selection hash table.
2992 * If it's not there but should be, add it. If it's there but shouldn't
2993 * be, remove it.
2994 */
2995 for (i = first; i <= last; i++) {
2996 entry = Tcl_FindHashEntry(listPtr->selection, (char *)i);
2997 if (entry != NULL) {
2998 if (!select) {
2999 Tcl_DeleteHashEntry(entry);
3000 listPtr->numSelected--;
3001 if (firstRedisplay < 0) {
3002 firstRedisplay = i;
3003 }
3004 }
3005 } else {
3006 if (select) {
3007 entry = Tcl_CreateHashEntry(listPtr->selection,
3008 (char *)i, &new);
3009 Tcl_SetHashValue(entry, (ClientData) NULL);
3010 listPtr->numSelected++;
3011 if (firstRedisplay < 0) {
3012 firstRedisplay = i;
3013 }
3014 }
3015 }
3016 }
3017
3018 if (firstRedisplay >= 0) {
3019 EventuallyRedrawRange(listPtr, first, last);
3020 }
3021 if ((oldCount == 0) && (listPtr->numSelected > 0)
3022 && (listPtr->exportSelection)) {
3023 Tk_OwnSelection(listPtr->tkwin, XA_PRIMARY, ListboxLostSelection,
3024 (ClientData) listPtr);
3025 }
3026 return TCL_OK;
3027 }
3028
3029 /*
3030 *----------------------------------------------------------------------
3031 *
3032 * ListboxFetchSelection --
3033 *
3034 * This procedure is called back by Tk when the selection is
3035 * requested by someone. It returns part or all of the selection
3036 * in a buffer provided by the caller.
3037 *
3038 * Results:
3039 * The return value is the number of non-NULL bytes stored
3040 * at buffer. Buffer is filled (or partially filled) with a
3041 * NULL-terminated string containing part or all of the selection,
3042 * as given by offset and maxBytes. The selection is returned
3043 * as a Tcl list with one list element for each element in the
3044 * listbox.
3045 *
3046 * Side effects:
3047 * None.
3048 *
3049 *----------------------------------------------------------------------
3050 */
3051
3052 static int
3053 ListboxFetchSelection(clientData, offset, buffer, maxBytes)
3054 ClientData clientData; /* Information about listbox widget. */
3055 int offset; /* Offset within selection of first
3056 * byte to be returned. */
3057 char *buffer; /* Location in which to place
3058 * selection. */
3059 int maxBytes; /* Maximum number of bytes to place
3060 * at buffer, not including terminating
3061 * NULL character. */
3062 {
3063 register Listbox *listPtr = (Listbox *) clientData;
3064 Tcl_DString selection;
3065 int length, count, needNewline;
3066 Tcl_Obj *curElement;
3067 char *stringRep;
3068 int stringLen;
3069 Tcl_HashEntry *entry;
3070 int i;
3071
3072 if (!listPtr->exportSelection) {
3073 return -1;
3074 }
3075
3076 /*
3077 * Use a dynamic string to accumulate the contents of the selection.
3078 */
3079
3080 needNewline = 0;
3081 Tcl_DStringInit(&selection);
3082 for (i = 0; i < listPtr->nElements; i++) {
3083 entry = Tcl_FindHashEntry(listPtr->selection, (char *)i);
3084 if (entry != NULL) {
3085 if (needNewline) {
3086 Tcl_DStringAppend(&selection, "\n", 1);
3087 }
3088 Tcl_ListObjIndex(listPtr->interp, listPtr->listObj, i,
3089 &curElement);
3090 stringRep = Tcl_GetStringFromObj(curElement, &stringLen);
3091 Tcl_DStringAppend(&selection, stringRep, stringLen);
3092 needNewline = 1;
3093 }
3094 }
3095
3096 length = Tcl_DStringLength(&selection);
3097 if (length == 0) {
3098 return -1;
3099 }
3100
3101 /*
3102 * Copy the requested portion of the selection to the buffer.
3103 */
3104
3105 count = length - offset;
3106 if (count <= 0) {
3107 count = 0;
3108 } else {
3109 if (count > maxBytes) {
3110 count = maxBytes;
3111 }
3112 memcpy((VOID *) buffer,
3113 (VOID *) (Tcl_DStringValue(&selection) + offset),
3114 (size_t) count);
3115 }
3116 buffer[count] = '\0';
3117 Tcl_DStringFree(&selection);
3118 return count;
3119 }
3120
3121 /*
3122 *----------------------------------------------------------------------
3123 *
3124 * ListboxLostSelection --
3125 *
3126 * This procedure is called back by Tk when the selection is
3127 * grabbed away from a listbox widget.
3128 *
3129 * Results:
3130 * None.
3131 *
3132 * Side effects:
3133 * The existing selection is unhighlighted, and the window is
3134 * marked as not containing a selection.
3135 *
3136 *----------------------------------------------------------------------
3137 */
3138
3139 static void
3140 ListboxLostSelection(clientData)
3141 ClientData clientData; /* Information about listbox widget. */
3142 {
3143 register Listbox *listPtr = (Listbox *) clientData;
3144
3145 if ((listPtr->exportSelection) && (listPtr->nElements > 0)) {
3146 ListboxSelect(listPtr, 0, listPtr->nElements-1, 0);
3147 }
3148 }
3149
3150 /*
3151 *----------------------------------------------------------------------
3152 *
3153 * EventuallyRedrawRange --
3154 *
3155 * Ensure that a given range of elements is eventually redrawn on
3156 * the display (if those elements in fact appear on the display).
3157 *
3158 * Results:
3159 * None.
3160 *
3161 * Side effects:
3162 * Information gets redisplayed.
3163 *
3164 *----------------------------------------------------------------------
3165 */
3166
3167 static void
3168 EventuallyRedrawRange(listPtr, first, last)
3169 register Listbox *listPtr; /* Information about widget. */
3170 int first; /* Index of first element in list
3171 * that needs to be redrawn. */
3172 int last; /* Index of last element in list
3173 * that needs to be redrawn. May
3174 * be less than first;
3175 * these just bracket a range. */
3176 {
3177 /* We don't have to register a redraw callback if one is already pending,
3178 * or if the window doesn't exist, or if the window isn't mapped */
3179 if ((listPtr->flags & REDRAW_PENDING)
3180 || (listPtr->flags & LISTBOX_DELETED)
3181 || !Tk_IsMapped(listPtr->tkwin)) {
3182 return;
3183 }
3184 listPtr->flags |= REDRAW_PENDING;
3185 Tcl_DoWhenIdle(DisplayListbox, (ClientData) listPtr);
3186 }
3187
3188 /*
3189 *----------------------------------------------------------------------
3190 *
3191 * ListboxUpdateVScrollbar --
3192 *
3193 * This procedure is invoked whenever information has changed in
3194 * a listbox in a way that would invalidate a vertical scrollbar
3195 * display. If there is an associated scrollbar, then this command
3196 * updates it by invoking a Tcl command.
3197 *
3198 * Results:
3199 * None.
3200 *
3201 * Side effects:
3202 * A Tcl command is invoked, and an additional command may be
3203 * invoked to process errors in the command.
3204 *
3205 *----------------------------------------------------------------------
3206 */
3207
3208 static void
3209 ListboxUpdateVScrollbar(listPtr)
3210 register Listbox *listPtr; /* Information about widget. */
3211 {
3212 char string[TCL_DOUBLE_SPACE * 2];
3213 double first, last;
3214 int result;
3215 Tcl_Interp *interp;
3216
3217 if (listPtr->yScrollCmd == NULL) {
3218 return;
3219 }
3220 if (listPtr->nElements == 0) {
3221 first = 0.0;
3222 last = 1.0;
3223 } else {
3224 first = listPtr->topIndex/((double) listPtr->nElements);
3225 last = (listPtr->topIndex+listPtr->fullLines)
3226 /((double) listPtr->nElements);
3227 if (last > 1.0) {
3228 last = 1.0;
3229 }
3230 }
3231 /*
3232 * We must hold onto the interpreter from the listPtr because the data
3233 * at listPtr might be freed as a result of the Tcl_VarEval.
3234 */
3235
3236 interp = listPtr->interp;
3237 Tcl_Preserve((ClientData) interp);
3238 result = LangDoCallback(interp, listPtr->yScrollCmd, 0, 2, " %g %g", first, last);
3239 if (result != TCL_OK) {
3240 Tcl_AddErrorInfo(interp,
3241 "\n (vertical scrolling command executed by listbox)");
3242 Tcl_BackgroundError(interp);
3243 }
3244 Tcl_Release((ClientData) interp);
3245 }
3246
3247 /*
3248 *----------------------------------------------------------------------
3249 *
3250 * ListboxUpdateHScrollbar --
3251 *
3252 * This procedure is invoked whenever information has changed in
3253 * a listbox in a way that would invalidate a horizontal scrollbar
3254 * display. If there is an associated horizontal scrollbar, then
3255 * this command updates it by invoking a Tcl command.
3256 *
3257 * Results:
3258 * None.
3259 *
3260 * Side effects:
3261 * A Tcl command is invoked, and an additional command may be
3262 * invoked to process errors in the command.
3263 *
3264 *----------------------------------------------------------------------
3265 */
3266
3267 static void
3268 ListboxUpdateHScrollbar(listPtr)
3269 register Listbox *listPtr; /* Information about widget. */
3270 {
3271 char string[TCL_DOUBLE_SPACE * 2];
3272 int result, windowWidth;
3273 double first, last;
3274 Tcl_Interp *interp;
3275
3276 if (listPtr->xScrollCmd == NULL) {
3277 return;
3278 }
3279 windowWidth = Tk_Width(listPtr->tkwin) - 2*(listPtr->inset
3280 + listPtr->selBorderWidth);
3281 if (listPtr->maxWidth == 0) {
3282 first = 0;
3283 last = 1.0;
3284 } else {
3285 first = listPtr->xOffset/((double) listPtr->maxWidth);
3286 last = (listPtr->xOffset + windowWidth)
3287 /((double) listPtr->maxWidth);
3288 if (last > 1.0) {
3289 last = 1.0;
3290 }
3291 }
3292 /*
3293 * We must hold onto the interpreter because the data referred to at
3294 * listPtr might be freed as a result of the call to Tcl_VarEval.
3295 */
3296
3297 interp = listPtr->interp;
3298 Tcl_Preserve((ClientData) interp);
3299 result = LangDoCallback(interp, listPtr->xScrollCmd, 0, 2, " %g %g",first, last);
3300 if (result != TCL_OK) {
3301 Tcl_AddErrorInfo(interp,
3302 "\n (horizontal scrolling command executed by listbox)");
3303 Tcl_BackgroundError(interp);
3304 }
3305 Tcl_Release((ClientData) interp);
3306 }
3307
3308 /*
3309 *----------------------------------------------------------------------
3310 *
3311 * ListboxListVarProc --
3312 *
3313 * Called whenever the trace on the listbox list var fires.
3314 *
3315 * Results:
3316 * None.
3317 *
3318 * Side effects:
3319 * None.
3320 *
3321 *----------------------------------------------------------------------
3322 */
3323
3324 static char *
3325 ListboxListVarProc(clientData, interp, name1, name2, flags)
3326 ClientData clientData; /* Information about button. */
3327 Tcl_Interp *interp; /* Interpreter containing variable. */
3328 Tcl_Obj *name1; /* Not used. */
3329 CONST char *name2; /* Not used. */
3330 int flags; /* Information about what happened. */
3331 {
3332 Listbox *listPtr = (Listbox *)clientData;
3333 Tcl_Obj *oldListObj, *varListObj;
3334 int oldLength;
3335 int i;
3336 Tcl_HashEntry *entry;
3337
3338 /* Bwah hahahaha -- puny mortal, you can't unset a -listvar'd variable! */
3339 if (flags & TCL_TRACE_UNSETS) {
3340 if ((flags & TCL_TRACE_DESTROYED) && !(flags & TCL_INTERP_DESTROYED)) {
3341 Tcl_ObjSetVar2(interp, listPtr->listVarName,
3342 (char *)NULL, listPtr->listObj, TCL_GLOBAL_ONLY);
3343 Lang_TraceVar(interp, listPtr->listVarName,
3344 TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
3345 ListboxListVarProc, clientData);
3346 return (char *)NULL;
3347 }
3348 } else {
3349 oldListObj = listPtr->listObj;
3350 varListObj = Tcl_ObjGetVar2(listPtr->interp, listPtr->listVarName,
3351 (char *)NULL, TCL_GLOBAL_ONLY);
3352 /*
3353 * Make sure the new value is a good list; if it's not, disallow
3354 * the change -- the fact that it is a listvar means that it must
3355 * always be a valid list -- and return an error message.
3356 */
3357 if (Tcl_ListObjLength(listPtr->interp, varListObj, &i) != TCL_OK) {
3358 Tcl_ObjSetVar2(interp, listPtr->listVarName, (char *)NULL,
3359 oldListObj, TCL_GLOBAL_ONLY);
3360 return("invalid listvar value");
3361 }
3362
3363 listPtr->listObj = varListObj;
3364 /* Incr the obj ref count so it doesn't vanish if the var is unset */
3365 Tcl_IncrRefCount(listPtr->listObj);
3366 /* Clean up the ref to our old list obj */
3367 Tcl_DecrRefCount(oldListObj);
3368 }
3369
3370 /*
3371 * If the list length has decreased, then we should clean up selection and
3372 * attributes information for elements past the end of the new list
3373 */
3374 oldLength = listPtr->nElements;
3375 Tcl_ListObjLength(listPtr->interp, listPtr->listObj, &listPtr->nElements);
3376 if (listPtr->nElements < oldLength) {
3377 for (i = listPtr->nElements; i < oldLength; i++) {
3378 /* Clean up selection */
3379 entry = Tcl_FindHashEntry(listPtr->selection, (char *)i);
3380 if (entry != NULL) {
3381 listPtr->numSelected--;
3382 Tcl_DeleteHashEntry(entry);
3383 }
3384
3385 /* Clean up attributes */
3386 entry = Tcl_FindHashEntry(listPtr->itemAttrTable, (char *)i);
3387 if (entry != NULL) {
3388 ckfree((char *)Tcl_GetHashValue(entry));
3389 Tcl_DeleteHashEntry(entry);
3390 }
3391 }
3392 }
3393
3394 if (oldLength != listPtr->nElements) {
3395 listPtr->flags |= UPDATE_V_SCROLLBAR;
3396 if (listPtr->topIndex > (listPtr->nElements - listPtr->fullLines)) {
3397 listPtr->topIndex = listPtr->nElements - listPtr->fullLines;
3398 if (listPtr->topIndex < 0) {
3399 listPtr->topIndex = 0;
3400 }
3401 }
3402 }
3403
3404 /*
3405 * The computed maxWidth may have changed as a result of this operation.
3406 * However, we don't want to recompute it every time this trace fires
3407 * (imagine the user doing 1000 lappends to the listvar). Therefore, set
3408 * the MAXWIDTH_IS_STALE flag, which will cause the width to be recomputed
3409 * next time the list is redrawn.
3410 */
3411 listPtr->flags |= MAXWIDTH_IS_STALE;
3412
3413 EventuallyRedrawRange(listPtr, 0, listPtr->nElements-1);
3414 return (char*)NULL;
3415 }
3416
3417 /*
3418 *----------------------------------------------------------------------
3419 *
3420 * MigrateHashEntries --
3421 *
3422 * Given a hash table with entries keyed by a single integer value,
3423 * move all entries in a given range by a fixed amount, so that
3424 * if in the original table there was an entry with key n and
3425 * the offset was i, in the new table that entry would have key n + i.
3426 *
3427 * Results:
3428 * None.
3429 *
3430 * Side effects:
3431 * Rekeys some hash table entries.
3432 *
3433 *----------------------------------------------------------------------
3434 */
3435
3436 static void
3437 MigrateHashEntries(table, first, last, offset)
3438 Tcl_HashTable *table;
3439 int first;
3440 int last;
3441 int offset;
3442 {
3443 int i, new;
3444 Tcl_HashEntry *entry;
3445 ClientData clientData;
3446
3447 if (offset == 0) {
3448 return;
3449 }
3450 /* It's more efficient to do one if/else and nest the for loops inside,
3451 * although we could avoid some code duplication if we nested the if/else
3452 * inside the for loops */
3453 if (offset > 0) {
3454 for (i = last; i >= first; i--) {
3455 entry = Tcl_FindHashEntry(table, (char *)i);
3456 if (entry != NULL) {
3457 clientData = Tcl_GetHashValue(entry);
3458 Tcl_DeleteHashEntry(entry);
3459 entry = Tcl_CreateHashEntry(table, (char *)(i + offset), &new);
3460 Tcl_SetHashValue(entry, clientData);
3461 }
3462 }
3463 } else {
3464 for (i = first; i <= last; i++) {
3465 entry = Tcl_FindHashEntry(table, (char *)i);
3466 if (entry != NULL) {
3467 clientData = Tcl_GetHashValue(entry);
3468 Tcl_DeleteHashEntry(entry);
3469 entry = Tcl_CreateHashEntry(table, (char *)(i + offset), &new);
3470 Tcl_SetHashValue(entry, clientData);
3471 }
3472 }
3473 }
3474 return;
3475 }
3476
3477
3478
3479