Debian Codegraph @

pTk/mTk/tixGeneric/tixTList.c

raw symbols SCIP
1
2 /* $Id: tixTList.c,v 1.1.1.1 2000/05/17 11:08:42 idiscovery Exp $ */
3
4 /*
5 * tixTList.c --
6 *
7 * This module implements "TList" widgets.
8 *
9 *
10 * Copyright (c) 1996, Expert Interface Technologies
11 *
12 * See the file "license.terms" for information on usage and redistribution
13 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 */
15
16 #include "tixPort.h"
17 #include "tixInt.h"
18 #include "tkInt.h"
19 #include "tixTList.h"
20 #include "tixDef.h"
21
22 #define TIX_UP 1
23 #define TIX_DOWN 2
24 #define TIX_LEFT 3
25 #define TIX_RIGHT 4
26
27 /*
28 * Information used for argv parsing.
29 */
30 static Tk_ConfigSpec configSpecs[] = {
31 {TK_CONFIG_COLOR, "-background", "background", "Background",
32 DEF_TLIST_BG_COLOR, Tk_Offset(WidgetRecord, normalBg),
33 TK_CONFIG_COLOR_ONLY},
34
35 {TK_CONFIG_COLOR, "-background", "background", "Background",
36 DEF_TLIST_BG_MONO, Tk_Offset(WidgetRecord, normalBg),
37 TK_CONFIG_MONO_ONLY},
38
39 {TK_CONFIG_SYNONYM, "-bd", "borderWidth", (char *) NULL,
40 (char *) NULL, 0, 0},
41
42 {TK_CONFIG_SYNONYM, "-bg", "background", (char *) NULL,
43 (char *) NULL, 0, 0},
44
45 {TK_CONFIG_BORDER, "-highlightbackground", "highlightBackground",
46 "HighlightBackground",
47 DEF_TLIST_BG_COLOR, Tk_Offset(WidgetRecord, border),
48 TK_CONFIG_COLOR_ONLY},
49
50 {TK_CONFIG_BORDER, "-highlightbackground", "highlightBackground",
51 "HighlightBackground",
52 DEF_TLIST_BG_MONO, Tk_Offset(WidgetRecord, border),
53 TK_CONFIG_MONO_ONLY},
54
55 {TK_CONFIG_PIXELS, "-borderwidth", "borderWidth", "BorderWidth",
56 DEF_TLIST_BORDER_WIDTH, Tk_Offset(WidgetRecord, borderWidth), 0},
57
58 {TK_CONFIG_CALLBACK, "-browsecmd", "browseCmd", "BrowseCmd",
59 DEF_TLIST_BROWSE_COMMAND, Tk_Offset(WidgetRecord, browseCmd),
60 TK_CONFIG_NULL_OK},
61
62 {TK_CONFIG_CALLBACK, "-command", "command", "Command",
63 DEF_TLIST_COMMAND, Tk_Offset(WidgetRecord, command),
64 TK_CONFIG_NULL_OK},
65
66 {TK_CONFIG_ACTIVE_CURSOR, "-cursor", "cursor", "Cursor",
67 DEF_TLIST_CURSOR, Tk_Offset(WidgetRecord, cursor),
68 TK_CONFIG_NULL_OK},
69
70 {TK_CONFIG_SYNONYM, "-fg", "foreground", (char *) NULL,
71 (char *) NULL, 0, 0},
72
73 {TK_CONFIG_FONT, "-font", "font", "Font",
74 DEF_TLIST_FONT, Tk_Offset(WidgetRecord, font), 0},
75
76 {TK_CONFIG_COLOR, "-foreground", "foreground", "Foreground",
77 DEF_TLIST_FG_COLOR, Tk_Offset(WidgetRecord, normalFg),
78 TK_CONFIG_COLOR_ONLY},
79
80 {TK_CONFIG_COLOR, "-foreground", "foreground", "Foreground",
81 DEF_TLIST_FG_MONO, Tk_Offset(WidgetRecord, normalFg),
82 TK_CONFIG_MONO_ONLY},
83
84 {TK_CONFIG_INT, "-height", "height", "Height",
85 DEF_TLIST_HEIGHT, Tk_Offset(WidgetRecord, height), 0},
86
87 {TK_CONFIG_COLOR, "-highlightcolor", "highlightColor", "HighlightColor",
88 DEF_TLIST_HIGHLIGHT_COLOR, Tk_Offset(WidgetRecord, highlightColorPtr),
89 TK_CONFIG_COLOR_ONLY},
90
91 {TK_CONFIG_COLOR, "-highlightcolor", "highlightColor", "HighlightColor",
92 DEF_TLIST_HIGHLIGHT_MONO, Tk_Offset(WidgetRecord, highlightColorPtr),
93 TK_CONFIG_MONO_ONLY},
94
95 {TK_CONFIG_PIXELS, "-highlightthickness", "highlightThickness",
96 "HighlightThickness",
97 DEF_TLIST_HIGHLIGHT_WIDTH, Tk_Offset(WidgetRecord, highlightWidth), 0},
98
99 {TK_CONFIG_CUSTOM, "-itemtype", "itemType", "ItemType",
100 DEF_TLIST_ITEM_TYPE, Tk_Offset(WidgetRecord, diTypePtr),
101 0, &tixConfigItemType},
102
103 {TK_CONFIG_UID, "-orient", "orient", "Orient",
104 DEF_TLIST_ORIENT, Tk_Offset(WidgetRecord, orientUid), 0},
105
106 {TK_CONFIG_PIXELS, "-padx", "padX", "Pad",
107 DEF_TLIST_PADX, Tk_Offset(WidgetRecord, padX), 0},
108
109 {TK_CONFIG_PIXELS, "-pady", "padY", "Pad",
110 DEF_TLIST_PADY, Tk_Offset(WidgetRecord, padY), 0},
111
112 {TK_CONFIG_RELIEF, "-relief", "relief", "Relief",
113 DEF_TLIST_RELIEF, Tk_Offset(WidgetRecord, relief), 0},
114
115 {TK_CONFIG_BORDER, "-selectbackground", "selectBackground", "Foreground",
116 DEF_TLIST_SELECT_BG_COLOR, Tk_Offset(WidgetRecord, selectBorder),
117 TK_CONFIG_COLOR_ONLY},
118
119 {TK_CONFIG_BORDER, "-selectbackground", "selectBackground", "Foreground",
120 DEF_TLIST_SELECT_BG_MONO, Tk_Offset(WidgetRecord, selectBorder),
121 TK_CONFIG_MONO_ONLY},
122
123 {TK_CONFIG_PIXELS, "-selectborderwidth", "selectBorderWidth","BorderWidth",
124 DEF_TLIST_SELECT_BORDERWIDTH,Tk_Offset(WidgetRecord, selBorderWidth),0},
125
126 {TK_CONFIG_COLOR, "-selectforeground", "selectForeground", "Background",
127 DEF_TLIST_SELECT_FG_COLOR, Tk_Offset(WidgetRecord, selectFg),
128 TK_CONFIG_COLOR_ONLY},
129
130 {TK_CONFIG_COLOR, "-selectforeground", "selectForeground", "Background",
131 DEF_TLIST_SELECT_FG_MONO, Tk_Offset(WidgetRecord, selectFg),
132 TK_CONFIG_MONO_ONLY},
133
134 {TK_CONFIG_UID, "-selectmode", "selectMode", "SelectMode",
135 DEF_TLIST_SELECT_MODE, Tk_Offset(WidgetRecord, selectMode), 0},
136
137 {TK_CONFIG_UID, "-state", (char*)NULL, (char*)NULL,
138 DEF_TLIST_STATE, Tk_Offset(WidgetRecord, state), 0},
139
140 {TK_CONFIG_CALLBACK, "-sizecmd", "sizeCmd", "SizeCmd",
141 DEF_TLIST_SIZE_COMMAND, Tk_Offset(WidgetRecord, sizeCmd),
142 TK_CONFIG_NULL_OK},
143
144 {TK_CONFIG_STRING, "-takefocus", "takeFocus", "TakeFocus",
145 DEF_TLIST_TAKE_FOCUS, Tk_Offset(WidgetRecord, takeFocus),
146 TK_CONFIG_NULL_OK},
147
148 {TK_CONFIG_INT, "-width", "width", "Width",
149 DEF_TLIST_WIDTH, Tk_Offset(WidgetRecord, width), 0},
150
151 {TK_CONFIG_CALLBACK, "-xscrollcommand", "xScrollCommand", "ScrollCommand",
152 DEF_TLIST_X_SCROLL_COMMAND,
153 Tk_Offset(WidgetRecord, scrollInfo[0].command),
154 TK_CONFIG_NULL_OK},
155
156 {TK_CONFIG_CALLBACK, "-yscrollcommand", "yScrollCommand", "ScrollCommand",
157 DEF_TLIST_Y_SCROLL_COMMAND,
158 Tk_Offset(WidgetRecord, scrollInfo[1].command),
159 TK_CONFIG_NULL_OK},
160
161 {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
162 (char *) NULL, 0, 0}
163 };
164
165 #define DEF_TLISTENTRY_STATE "normal"
166 #define DEF_TLISTENTRY_DATA NULL
167
168 static Tk_ConfigSpec entryConfigSpecs[] = {
169
170 {TK_CONFIG_LANGARG, "-data", (char *) NULL, (char *) NULL,
171 DEF_TLISTENTRY_DATA, Tk_Offset(ListEntry, data), TK_CONFIG_NULL_OK},
172
173 {TK_CONFIG_UID, "-state", (char*)NULL, (char*)NULL,
174 DEF_TLISTENTRY_STATE, Tk_Offset(ListEntry, state), 0},
175
176 {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
177 (char *) NULL, 0, 0}
178 };
179
180 static Tix_ListInfo entListInfo = {
181 Tk_Offset(ListEntry, next),
182 TIX_UNDEFINED,
183 };
184
185
186 /*
187 * Forward declarations for procedures defined later in this file:
188 */
189
190 /* These are standard procedures for TK widgets
191 * implemeted in C
192 */
193
194 static void WidgetCmdDeletedProc _ANSI_ARGS_((
195 ClientData clientData));
196 static int WidgetConfigure _ANSI_ARGS_((Tcl_Interp *interp,
197 WidgetPtr wPtr, int argc, char **argv,
198 int flags));
199 static void WidgetDestroy _ANSI_ARGS_((char *clientData));
200 static void WidgetEventProc _ANSI_ARGS_((ClientData clientData,
201 XEvent *eventPtr));
202 static int WidgetCommand _ANSI_ARGS_((ClientData clientData,
203 Tcl_Interp *, int argc, char **argv));
204 static void WidgetDisplay _ANSI_ARGS_((ClientData clientData));
205 static void WidgetComputeGeometry _ANSI_ARGS_((
206 ClientData clientData));
207
208 /* Extra procedures for this widget
209 */
210 static void CancelRedrawWhenIdle _ANSI_ARGS_((WidgetPtr wPtr));
211 static void CancelResizeWhenIdle _ANSI_ARGS_((WidgetPtr wPtr));
212 static int ConfigElement _ANSI_ARGS_((WidgetPtr wPtr,
213 ListEntry *chPtr, int argc, char ** argv,
214 int flags, int forced));
215 static void RedrawRows _ANSI_ARGS_((
216 WidgetPtr wPtr, Drawable pixmap));
217 static void RedrawWhenIdle _ANSI_ARGS_((WidgetPtr wPtr));
218 static void ResizeRows _ANSI_ARGS_((
219 WidgetPtr wPtr, int winW, int winH));
220 static void ResizeWhenIdle _ANSI_ARGS_((WidgetPtr wPtr));
221 static void ResizeNow _ANSI_ARGS_((WidgetPtr wPtr));
222 static void UpdateScrollBars _ANSI_ARGS_((WidgetPtr wPtr,
223 int sizeChanged));
224 static int Tix_TLGetFromTo _ANSI_ARGS_((
225 Tcl_Interp *interp, WidgetPtr wPtr,
226 int argc, char **argv,
227 ListEntry ** fromPtr_ret, ListEntry ** toPtr_ret));
228 static void Tix_TLDItemSizeChanged _ANSI_ARGS_((
229 Tix_DItem *iPtr));
230 static void MakeGeomRequest _ANSI_ARGS_((
231 WidgetPtr wPtr));
232 static int Tix_TLGetNearest _ANSI_ARGS_((
233 WidgetPtr wPtr, int posn[2]));
234 static int Tix_TLGetAt _ANSI_ARGS_((WidgetPtr wPtr,
235 Tcl_Interp *interp, char * spec, int *at));
236 static int Tix_TLGetNeighbor _ANSI_ARGS_((
237 WidgetPtr wPtr, Tcl_Interp *interp,
238 int type, int argc, char **argv));
239 static int Tix_TranslateIndex _ANSI_ARGS_((
240 WidgetPtr wPtr, Tcl_Interp *interp, Tcl_Obj * string,
241 int * index, int isInsert));
242 static int Tix_TLDeleteRange _ANSI_ARGS_((
243 WidgetPtr wPtr, ListEntry * fromPtr,
244 ListEntry *toPtr));
245 static ListEntry * AllocEntry _ANSI_ARGS_((WidgetPtr wPtr));
246 static int AddElement _ANSI_ARGS_((WidgetPtr wPtr,
247 ListEntry * chPtr, int at));
248 static void FreeEntry _ANSI_ARGS_((WidgetPtr wPtr,
249 ListEntry * chPtr));
250 static int Tix_TLSpecialEntryInfo _ANSI_ARGS_((
251 WidgetPtr wPtr, Tcl_Interp *interp,
252 ListEntry * chPtr));
253 static void Realloc _ANSI_ARGS_((WidgetPtr wPtr,int new_size));
254
255 static TIX_DECLARE_SUBCMD(Tix_TLInsert);
256 static TIX_DECLARE_SUBCMD(Tix_TLCGet);
257 static TIX_DECLARE_SUBCMD(Tix_TLConfig);
258 static TIX_DECLARE_SUBCMD(Tix_TLDelete);
259 static TIX_DECLARE_SUBCMD(Tix_TLEntryCget);
260 static TIX_DECLARE_SUBCMD(Tix_TLEntryConfig);
261 static TIX_DECLARE_SUBCMD(Tix_TLGeometryInfo);
262 static TIX_DECLARE_SUBCMD(Tix_TLIndex);
263 static TIX_DECLARE_SUBCMD(Tix_TLInfo);
264 static TIX_DECLARE_SUBCMD(Tix_TLNearest);
265 static TIX_DECLARE_SUBCMD(Tix_TLSee);
266 static TIX_DECLARE_SUBCMD(Tix_TLSelection);
267 static TIX_DECLARE_SUBCMD(Tix_TLSetSite);
268 static TIX_DECLARE_SUBCMD(Tix_TLView);
269
270 /*
271 *--------------------------------------------------------------
272 *
273 * Tix_TListCmd --
274 *
275 * This procedure is invoked to process the "tixTList" Tcl
276 * command. It creates a new "TixTList" widget.
277 *
278 * Results:
279 * A standard Tcl result.
280 *
281 * Side effects:
282 * A new widget is created and configured.
283 *
284 *--------------------------------------------------------------
285 */
286 int
287 Tix_TListCmd(clientData, interp, argc, argv)
288 ClientData clientData;
289 Tcl_Interp *interp; /* Current interpreter. */
290 int argc; /* Number of arguments. */
291 char **argv; /* Argument strings. */
292 {
293 Tk_Window mainw = (Tk_Window) clientData;
294 WidgetPtr wPtr;
295 Tk_Window tkwin;
296
297 if (argc < 2) {
298 Tcl_AppendResult(interp, "wrong # args: should be \"",
299 argv[0], " pathName ?options?\"", (char *) NULL);
300 return TCL_ERROR;
301 }
302
303 tkwin = Tk_CreateWindowFromPath(interp, mainw, argv[1], (char *) NULL);
304 if (tkwin == NULL) {
305 return TCL_ERROR;
306 }
307
308 Tk_SetClass(tkwin, "TixTList");
309
310 /*
311 * Allocate and initialize the widget record.
312 */
313 wPtr = (WidgetPtr) ckalloc(sizeof(WidgetRecord));
314
315 wPtr->dispData.tkwin = tkwin;
316 wPtr->dispData.display = Tk_Display(tkwin);
317 wPtr->dispData.interp = interp;
318 wPtr->dispData.sizeChangedProc = Tix_TLDItemSizeChanged;
319 wPtr->font = NULL;
320 wPtr->normalBg = NULL;
321 wPtr->normalFg = NULL;
322 wPtr->command = NULL;
323 wPtr->border = NULL;
324 wPtr->borderWidth = 0;
325 wPtr->selectBorder = NULL;
326 wPtr->selBorderWidth = 0;
327 wPtr->selectFg = NULL;
328 wPtr->backgroundGC = None;
329 wPtr->selectGC = None;
330 wPtr->anchorGC = None;
331 wPtr->highlightWidth = 0;
332 wPtr->highlightColorPtr = NULL;
333 wPtr->highlightGC = None;
334 wPtr->relief = TK_RELIEF_FLAT;
335 wPtr->cursor = None;
336 wPtr->redrawing = 0;
337 wPtr->resizing = 0;
338 wPtr->hasFocus = 0;
339 wPtr->selectMode = NULL;
340 wPtr->seeElemPtr = NULL;
341 wPtr->anchor = NULL;
342 wPtr->active = NULL;
343 wPtr->dropSite = NULL;
344 wPtr->dragSite = NULL;
345 wPtr->sizeCmd = NULL;
346 wPtr->browseCmd = NULL;
347 wPtr->takeFocus = NULL;
348 wPtr->orientUid = NULL;
349 wPtr->serial = 0;
350 wPtr->state = tixNormalUid;
351 wPtr->rows = (ListRow*)ckalloc(sizeof(ListRow) *1);
352 wPtr->numRow = 1;
353 wPtr->numRowAllocd = 1;
354 wPtr->width = 0;
355 wPtr->height = 0;
356
357 Tix_LinkListInit(&wPtr->entList);
358 Tix_InitScrollInfo((Tix_ScrollInfo*)&wPtr->scrollInfo[0], TIX_SCROLL_INT);
359 Tix_InitScrollInfo((Tix_ScrollInfo*)&wPtr->scrollInfo[1], TIX_SCROLL_INT);
360
361 Tk_CreateEventHandler(wPtr->dispData.tkwin,
362 ExposureMask|StructureNotifyMask|FocusChangeMask,
363 WidgetEventProc, (ClientData) wPtr);
364 wPtr->widgetCmd = Tcl_CreateCommand(interp,
365 Tk_PathName(wPtr->dispData.tkwin), WidgetCommand, (ClientData) wPtr,
366 WidgetCmdDeletedProc);
367
368 if (WidgetConfigure(interp, wPtr, argc-2, argv+2, 0) != TCL_OK) {
369 Tk_DestroyWindow(wPtr->dispData.tkwin);
370 return TCL_ERROR;
371 }
372
373 interp->result = Tk_PathName(wPtr->dispData.tkwin);
374 return TCL_OK;
375 }
376
377 /*
378 *----------------------------------------------------------------------
379 *
380 * WidgetConfigure --
381 *
382 * This procedure is called to process an argv/argc list in
383 * conjunction with the Tk option database to configure (or
384 * reconfigure) a List widget.
385 *
386 * Results:
387 * The return value is a standard Tcl result. If TCL_ERROR is
388 * returned, then interp->result contains an error message.
389 *
390 * Side effects:
391 * Configuration information, such as colors, border width,
392 * etc. get set for wPtr; old resources get freed,
393 * if there were any.
394 *
395 *----------------------------------------------------------------------
396 */
397 static int
398 WidgetConfigure(interp, wPtr, argc, argv, flags)
399 Tcl_Interp *interp; /* Used for error reporting. */
400 WidgetPtr wPtr; /* Information about widget. */
401 int argc; /* Number of valid entries in argv. */
402 char **argv; /* Arguments. */
403 int flags; /* Flags to pass to
404 * Tk_ConfigureWidget. */
405 {
406 XGCValues gcValues;
407 GC newGC;
408 TixFont oldfont;
409 size_t length;
410 Tix_StyleTemplate stTmpl;
411
412 oldfont = wPtr->font;
413
414 if (Tk_ConfigureWidget(interp, wPtr->dispData.tkwin, configSpecs,
415 argc, argv, (char *) wPtr, flags) != TCL_OK) {
416 return TCL_ERROR;
417 }
418
419 length = strlen(wPtr->orientUid);
420 if (strncmp(wPtr->orientUid, "vertical", length) == 0) {
421 wPtr->isVertical = 1;
422 } else if (strncmp(wPtr->orientUid, "horizontal", length) == 0) {
423 wPtr->isVertical = 0;
424 } else {
425 Tcl_AppendResult(interp, "bad orientation \"", wPtr->orientUid,
426 "\": must be vertical or horizontal", (char *) NULL);
427 wPtr->orientUid = Tk_GetUid("vertical");
428 wPtr->isVertical = 1;
429 return TCL_ERROR;
430 }
431
432 if ((wPtr->state != tixNormalUid) && (wPtr->state != tixDisabledUid)) {
433 Tcl_AppendResult(interp, "bad state value \"", wPtr->state,
434 "\": must be normal or disabled", (char *) NULL);
435 wPtr->state = tixNormalUid;
436 return TCL_ERROR;
437 }
438
439
440 if (oldfont != wPtr->font) {
441 /* Font has been changed (initialized) */
442 TixComputeTextGeometry(wPtr->font, "0", 1,
443 0, &wPtr->scrollInfo[0].unit, &wPtr->scrollInfo[1].unit);
444 }
445
446 /*
447 * A few options need special processing, such as setting the
448 * background from a 3-D border, or filling in complicated
449 * defaults that couldn't be specified to Tk_ConfigureWidget.
450 */
451
452 Tk_SetBackgroundFromBorder(wPtr->dispData.tkwin, wPtr->border);
453
454 /*
455 * Note: GraphicsExpose events are disabled in normalGC because it's
456 * used to copy stuff from an off-screen pixmap onto the screen (we know
457 * that there's no problem with obscured areas).
458 */
459
460 /* The background GC */
461 gcValues.foreground = wPtr->normalBg->pixel;
462 gcValues.graphics_exposures = False;
463
464 newGC = Tk_GetGC(wPtr->dispData.tkwin,
465 GCForeground|GCGraphicsExposures, &gcValues);
466 if (wPtr->backgroundGC != None) {
467 Tk_FreeGC(wPtr->dispData.display, wPtr->backgroundGC);
468 }
469 wPtr->backgroundGC = newGC;
470
471 /* The selected text GC */
472 gcValues.font = TixFontId(wPtr->font);
473 gcValues.foreground = wPtr->selectFg->pixel;
474 gcValues.background = Tk_3DBorderColor(wPtr->selectBorder)->pixel;
475 gcValues.graphics_exposures = False;
476
477 newGC = Tk_GetGC(wPtr->dispData.tkwin,
478 GCForeground|GCBackground|GCFont|GCGraphicsExposures, &gcValues);
479 if (wPtr->selectGC != None) {
480 Tk_FreeGC(wPtr->dispData.display, wPtr->selectGC);
481 }
482 wPtr->selectGC = newGC;
483
484 /* The dotted anchor lines */
485 gcValues.foreground = wPtr->normalFg->pixel;
486 gcValues.background = wPtr->normalBg->pixel;
487 gcValues.graphics_exposures = False;
488 gcValues.line_style = LineDoubleDash;
489 gcValues.dashes = 2;
490 gcValues.subwindow_mode = IncludeInferiors;
491
492 newGC = Tk_GetGC(wPtr->dispData.tkwin,
493 GCForeground|GCBackground|GCGraphicsExposures|GCLineStyle|GCDashList|
494 GCSubwindowMode, &gcValues);
495 if (wPtr->anchorGC != None) {
496 Tk_FreeGC(wPtr->dispData.display, wPtr->anchorGC);
497 }
498 wPtr->anchorGC = newGC;
499
500 /* The highlight border */
501 gcValues.background = wPtr->selectFg->pixel;
502 gcValues.foreground = wPtr->highlightColorPtr->pixel;
503 gcValues.graphics_exposures = False;
504
505 newGC = Tk_GetGC(wPtr->dispData.tkwin,
506 GCForeground|GCBackground|GCGraphicsExposures, &gcValues);
507 if (wPtr->highlightGC != None) {
508 Tk_FreeGC(wPtr->dispData.display, wPtr->highlightGC);
509 }
510 wPtr->highlightGC = newGC;
511
512 /* We must set the options of the default styles so that
513 * -- the default styles will change according to what is in
514 * stTmpl
515 */
516 stTmpl.font = wPtr->font;
517 stTmpl.pad[0] = wPtr->padX;
518 stTmpl.pad[1] = wPtr->padY;
519 stTmpl.colors[TIX_DITEM_NORMAL].fg = wPtr->normalFg;
520 stTmpl.colors[TIX_DITEM_NORMAL].bg = wPtr->normalBg;
521 stTmpl.colors[TIX_DITEM_SELECTED].fg= wPtr->selectFg;
522 stTmpl.colors[TIX_DITEM_SELECTED].bg= Tk_3DBorderColor(wPtr->selectBorder);
523 stTmpl.flags = TIX_DITEM_FONT|TIX_DITEM_NORMAL_BG|
524 TIX_DITEM_SELECTED_BG|TIX_DITEM_NORMAL_FG|TIX_DITEM_SELECTED_FG |
525 TIX_DITEM_PADX|TIX_DITEM_PADY;
526
527 Tix_SetDefaultStyleTemplate(wPtr->dispData.tkwin, &stTmpl);
528
529 /*
530 * Probably the -font or -width or -height options have changed. Let's
531 * make geometry request
532 */
533 MakeGeomRequest(wPtr);
534
535 ResizeWhenIdle(wPtr);
536
537 return TCL_OK;
538 }
539
540 /*
541 *--------------------------------------------------------------
542 *
543 * WidgetCommand --
544 *
545 * This procedure is invoked to process the Tcl command
546 * that corresponds to a widget managed by this module.
547 * See the user documentation for details on what it does.
548 *
549 * Results:
550 * A standard Tcl result.
551 *
552 * Side effects:
553 * See the user documentation.
554 *
555 *--------------------------------------------------------------
556 */
557
558 static int
559 WidgetCommand(clientData, interp, argc, argv)
560 ClientData clientData; /* Information about the widget. */
561 Tcl_Interp *interp; /* Current interpreter. */
562 int argc; /* Number of arguments. */
563 char **argv; /* Argument strings. */
564 {
565 int code;
566
567 static Tix_SubCmdInfo subCmdInfo[] = {
568 {TIX_DEFAULT_LEN, "active", 1, 2, Tix_TLSetSite,
569 "option ?index?"},
570 {TIX_DEFAULT_LEN, "anchor", 1, 2, Tix_TLSetSite,
571 "option ?index?"},
572 {TIX_DEFAULT_LEN, "cget", 1, 1, Tix_TLCGet,
573 "option"},
574 {TIX_DEFAULT_LEN, "configure", 0, TIX_VAR_ARGS, Tix_TLConfig,
575 "?option? ?value? ?option value ... ?"},
576 {TIX_DEFAULT_LEN, "delete", 1, 2, Tix_TLDelete,
577 "from ?to?"},
578 {TIX_DEFAULT_LEN, "dragsite", 1, 2, Tix_TLSetSite,
579 "option ?entryPath?"},
580 {TIX_DEFAULT_LEN, "dropsite", 1, 2, Tix_TLSetSite,
581 "option ?entryPath?"},
582 {TIX_DEFAULT_LEN, "entrycget", 2, 2, Tix_TLEntryCget,
583 "entryPath option"},
584 {TIX_DEFAULT_LEN, "entryconfigure", 1, TIX_VAR_ARGS, Tix_TLEntryConfig,
585 "index ?option? ?value? ?option value ... ?"},
586 {TIX_DEFAULT_LEN, "geometryinfo", 0, 2, Tix_TLGeometryInfo,
587 "?width height?"},
588 {TIX_DEFAULT_LEN, "index", 1, 1, Tix_TLIndex,
589 "index"},
590 {TIX_DEFAULT_LEN, "info", 1, TIX_VAR_ARGS, Tix_TLInfo,
591 "option ?args ...?"},
592 {TIX_DEFAULT_LEN, "insert", 1, TIX_VAR_ARGS, Tix_TLInsert,
593 "where ?option value ..."},
594 {TIX_DEFAULT_LEN, "nearest", 2, 2, Tix_TLNearest,
595 "x y"},
596 {TIX_DEFAULT_LEN, "see", 1, 1, Tix_TLSee,
597 "entryPath"},
598 {TIX_DEFAULT_LEN, "selection", 1, 3, Tix_TLSelection,
599 "option arg ?arg ...?"},
600 {TIX_DEFAULT_LEN, "xview", 0, 3, Tix_TLView,
601 "args"},
602 {TIX_DEFAULT_LEN, "yview", 0, 3, Tix_TLView,
603 "args"},
604 };
605
606 static Tix_CmdInfo cmdInfo = {
607 Tix_ArraySize(subCmdInfo), 1, TIX_VAR_ARGS, "?option? arg ?arg ...?",
608 };
609
610 Tcl_Preserve(clientData);
611 code = Tix_HandleSubCmds(&cmdInfo, subCmdInfo, clientData,
612 interp, argc, argv);
613 Tcl_Release(clientData);
614
615 return code;
616 }
617
618 /*
619 *--------------------------------------------------------------
620 *
621 * WidgetEventProc --
622 *
623 * This procedure is invoked by the Tk dispatcher for various
624 * events on Lists.
625 *
626 * Results:
627 * None.
628 *
629 * Side effects:
630 * When the window gets deleted, internal structures get
631 * cleaned up. When it gets exposed, it is redisplayed.
632 *
633 *--------------------------------------------------------------
634 */
635 static void
636 WidgetEventProc(clientData, eventPtr)
637 ClientData clientData; /* Information about window. */
638 XEvent *eventPtr; /* Information about event. */
639 {
640 WidgetPtr wPtr = (WidgetPtr) clientData;
641
642 switch (eventPtr->type) {
643 case DestroyNotify:
644 if (wPtr->dispData.tkwin != NULL) {
645 wPtr->dispData.tkwin = NULL;
646 wPtr->dispData.sizeChangedProc = NULL;
647 Tcl_DeleteCommand(wPtr->dispData.interp,
648 Tcl_GetCommandName(wPtr->dispData.interp, wPtr->widgetCmd));
649 }
650 CancelResizeWhenIdle(wPtr);
651 CancelRedrawWhenIdle(wPtr);
652 Tcl_EventuallyFree((ClientData) wPtr, WidgetDestroy);
653 break;
654
655 case ConfigureNotify:
656 ResizeWhenIdle(wPtr);
657 break;
658
659 case Expose:
660 RedrawWhenIdle(wPtr);
661 break;
662
663 case FocusIn:
664 wPtr->hasFocus = 1;
665 RedrawWhenIdle(wPtr);
666 break;
667
668 case FocusOut:
669 wPtr->hasFocus = 0;
670 RedrawWhenIdle(wPtr);
671 break;
672 }
673 }
674
675 /*
676 *----------------------------------------------------------------------
677 *
678 * WidgetDestroy --
679 *
680 * This procedure is invoked by Tk_EventuallyFree or Tk_Release
681 * to clean up the internal structure of a List at a safe time
682 * (when no-one is using it anymore).
683 *
684 * Results:
685 * None.
686 *
687 * Side effects:
688 * Everything associated with the List is freed up.
689 *
690 *----------------------------------------------------------------------
691 */
692
693 static void
694 WidgetDestroy(clientData)
695 char *clientData; /* Info about my widget. */
696 {
697 WidgetPtr wPtr = (WidgetPtr) clientData;
698
699
700 if (wPtr->backgroundGC != None) {
701 Tk_FreeGC(wPtr->dispData.display, wPtr->backgroundGC);
702 }
703 if (wPtr->selectGC != None) {
704 Tk_FreeGC(wPtr->dispData.display, wPtr->selectGC);
705 }
706 if (wPtr->anchorGC != None) {
707 Tk_FreeGC(wPtr->dispData.display, wPtr->anchorGC);
708 }
709 if (wPtr->highlightGC != None) {
710 Tk_FreeGC(wPtr->dispData.display, wPtr->highlightGC);
711 }
712
713 if (wPtr->entList.numItems > 0) {
714 ListEntry * fromPtr=NULL, *toPtr=NULL;
715 Tcl_Obj *objv[2];
716 objv[0] = Tcl_NewIntObj(0);
717 objv[1] = Tcl_NewStringObj("end",3);
718
719 Tix_TLGetFromTo(wPtr->dispData.interp, wPtr, 2, objv, &fromPtr,&toPtr);
720 Tcl_DecrRefCount(objv[0]);
721 Tcl_DecrRefCount(objv[1]);
722 Tcl_ResetResult(wPtr->dispData.interp);
723
724 if (fromPtr && toPtr) {
725 Tix_TLDeleteRange(wPtr, fromPtr, toPtr);
726 }
727 }
728
729 if (wPtr->rows) {
730 ckfree((char*)wPtr->rows);
731 }
732
733 Tk_FreeOptions(configSpecs, (char *) wPtr, wPtr->dispData.display, 0);
734 ckfree((char *) wPtr);
735 }
736
737 /*
738 *----------------------------------------------------------------------
739 *
740 * WidgetCmdDeletedProc --
741 *
742 * This procedure is invoked when a widget command is deleted. If
743 * the widget isn't already in the process of being destroyed,
744 * this command destroys it.
745 *
746 * Results:
747 * None.
748 *
749 * Side effects:
750 * The widget is destroyed.
751 *
752 *----------------------------------------------------------------------
753 */
754 static void
755 WidgetCmdDeletedProc(clientData)
756 ClientData clientData; /* Pointer to widget record for widget. */
757 {
758 WidgetPtr wPtr = (WidgetPtr) clientData;
759
760 /*
761 * This procedure could be invoked either because the window was
762 * destroyed and the command was then deleted (in which case tkwin
763 * is NULL) or because the command was deleted, and then this procedure
764 * destroys the widget.
765 */
766 if (wPtr->dispData.tkwin != NULL) {
767 Tk_Window tkwin = wPtr->dispData.tkwin;
768 wPtr->dispData.tkwin = NULL;
769 wPtr->dispData.sizeChangedProc = NULL;
770 Tk_DestroyWindow(tkwin);
771 }
772 }
773
774 /*
775 *--------------------------------------------------------------
776 *
777 * WidgetComputeGeometry --
778 *
779 * This procedure is invoked to process the Tcl command
780 * that corresponds to a widget managed by this module.
781 * See the user documentation for details on what it does.
782 *
783 * Results:
784 * A standard Tcl result.
785 *
786 * Side effects:
787 * none
788 *
789 *--------------------------------------------------------------
790 */
791 static void
792 WidgetComputeGeometry(clientData)
793 ClientData clientData;
794 {
795 WidgetPtr wPtr = (WidgetPtr)clientData;
796 int winW, winH;
797 Tk_Window tkwin = wPtr->dispData.tkwin;
798
799 wPtr->resizing = 0;
800
801 if (tkwin == NULL) {
802 return;
803 }
804
805 winW = Tk_Width(tkwin) - 2*wPtr->highlightWidth - 2*wPtr->borderWidth;
806 winH = Tk_Height(tkwin) - 2*wPtr->highlightWidth - 2*wPtr->borderWidth;
807
808 ResizeRows(wPtr, winW, winH);
809 /* Update scrollbars */
810 UpdateScrollBars(wPtr, 1);
811
812 RedrawWhenIdle(wPtr);
813 }
814
815 static void
816 MakeGeomRequest(wPtr)
817 WidgetPtr wPtr;
818 {
819 int reqW, reqH;
820
821 reqW = wPtr->width * wPtr->scrollInfo[0].unit;
822 reqH = wPtr->height * wPtr->scrollInfo[1].unit;
823
824 Tk_GeometryRequest(wPtr->dispData.tkwin, reqW, reqH);
825 }
826
827
828
829 /*----------------------------------------------------------------------
830 * DItemSizeChanged --
831 *
832 * This is called whenever the size of one of the HList's items
833 * changes its size.
834 *----------------------------------------------------------------------
835 */
836 static void
837 Tix_TLDItemSizeChanged(iPtr)
838 Tix_DItem *iPtr;
839 {
840 WidgetPtr wPtr = (WidgetPtr)iPtr->base.clientData;
841
842 if (wPtr) {
843 /* double-check: perhaps we haven't set the clientData yet! */
844 ResizeWhenIdle(wPtr);
845 }
846 }
847
848 /*
849 *----------------------------------------------------------------------
850 * ResizeWhenIdle --
851 *----------------------------------------------------------------------
852 */
853 static void
854 ResizeWhenIdle(wPtr)
855 WidgetPtr wPtr;
856 {
857 if (wPtr->redrawing) {
858 CancelRedrawWhenIdle(wPtr);
859 }
860 if (!wPtr->resizing) {
861 wPtr->resizing = 1;
862 Tcl_DoWhenIdle(WidgetComputeGeometry, (ClientData)wPtr);
863 }
864 }
865
866 /*
867 *----------------------------------------------------------------------
868 * ResizeWhenIdle --
869 *----------------------------------------------------------------------
870 */
871 static void
872 ResizeNow(wPtr)
873 WidgetPtr wPtr;
874 {
875 if (wPtr->resizing) {
876 Tcl_CancelIdleCall(WidgetComputeGeometry, (ClientData)wPtr);
877
878 WidgetComputeGeometry((ClientData)wPtr);
879 wPtr->resizing = 0;
880 }
881 }
882
883 /*
884 *----------------------------------------------------------------------
885 * CancelResizeWhenIdle --
886 *----------------------------------------------------------------------
887 */
888 static void
889 CancelResizeWhenIdle(wPtr)
890 WidgetPtr wPtr;
891 {
892 if (wPtr->resizing) {
893 wPtr->resizing = 0;
894 Tcl_CancelIdleCall(WidgetComputeGeometry, (ClientData)wPtr);
895 }
896 }
897
898 /*
899 *----------------------------------------------------------------------
900 * RedrawWhenIdle --
901 *----------------------------------------------------------------------
902 */
903 static void
904 RedrawWhenIdle(wPtr)
905 WidgetPtr wPtr;
906 {
907 if (wPtr->resizing) {
908 /*
909 * Resize will eventually call redraw.
910 */
911 return;
912 }
913 if (!wPtr->redrawing && Tk_IsMapped(wPtr->dispData.tkwin)) {
914 wPtr->redrawing = 1;
915 Tcl_DoWhenIdle(WidgetDisplay, (ClientData)wPtr);
916 }
917 }
918
919 /*
920 *----------------------------------------------------------------------
921 * CancelRedrawWhenIdle --
922 *----------------------------------------------------------------------
923 */
924 static void
925 CancelRedrawWhenIdle(wPtr)
926 WidgetPtr wPtr;
927 {
928 if (wPtr->redrawing) {
929 wPtr->redrawing = 0;
930 Tcl_CancelIdleCall(WidgetDisplay, (ClientData)wPtr);
931 }
932 }
933
934 /*
935 *----------------------------------------------------------------------
936 *
937 * WidgetDisplay --
938 *
939 * Draw the widget to the screen.
940 *
941 * Results:
942 * None.
943 *
944 * Side effects:
945 *
946 *----------------------------------------------------------------------
947 */
948 static void
949 WidgetDisplay(clientData)
950 ClientData clientData; /* Info about my widget. */
951 {
952 WidgetPtr wPtr = (WidgetPtr) clientData;
953 Pixmap pixmap;
954 Tk_Window tkwin = wPtr->dispData.tkwin;
955 int winH, winW;
956
957 wPtr->redrawing = 0; /* clear the redraw flag */
958 wPtr->serial ++;
959
960 pixmap = Tk_GetPixmap(wPtr->dispData.display, Tk_WindowId(tkwin),
961 Tk_Width(tkwin), Tk_Height(tkwin), Tk_Depth(tkwin));
962
963 /* Fill the background */
964 XFillRectangle(wPtr->dispData.display, pixmap, wPtr->backgroundGC,
965 0, 0, Tk_Width(tkwin), Tk_Height(tkwin));
966
967 winW = Tk_Width(tkwin) - 2*wPtr->highlightWidth - 2*wPtr->borderWidth;
968 winH = Tk_Height(tkwin) - 2*wPtr->highlightWidth - 2*wPtr->borderWidth;
969
970 if (winW > 0 && winH > 0) {
971 RedrawRows(wPtr, pixmap);
972 }
973
974 /* Draw the border */
975 Tk_Draw3DRectangle(wPtr->dispData.tkwin, pixmap, wPtr->border,
976 wPtr->highlightWidth, wPtr->highlightWidth,
977 Tk_Width(tkwin) - 2*wPtr->highlightWidth,
978 Tk_Height(tkwin) - 2*wPtr->highlightWidth, wPtr->borderWidth,
979 wPtr->relief);
980
981 /* Draw the highlight */
982 if (wPtr->highlightWidth > 0) {
983 GC gc;
984
985 if (wPtr->hasFocus) {
986 gc = wPtr->highlightGC;
987 } else {
988 gc = Tk_3DBorderGC(wPtr->dispData.tkwin, wPtr->border,
989 TK_3D_FLAT_GC);
990 }
991 Tk_DrawFocusHighlight(tkwin, gc, wPtr->highlightWidth, pixmap);
992 }
993
994 /*
995 * Copy the information from the off-screen pixmap onto the screen,
996 * then delete the pixmap.
997 */
998 XCopyArea(wPtr->dispData.display, pixmap, Tk_WindowId(tkwin),
999 wPtr->backgroundGC, 0, 0, Tk_Width(tkwin), Tk_Height(tkwin), 0, 0);
1000 Tk_FreePixmap(wPtr->dispData.display, pixmap);
1001
1002 }
1003
1004 /*----------------------------------------------------------------------
1005 * AddElement --
1006 *
1007 * Add the element at the position indicated by "at".
1008 *----------------------------------------------------------------------
1009 */
1010 static int
1011 AddElement(wPtr, chPtr, at)
1012 WidgetPtr wPtr;
1013 ListEntry * chPtr;
1014 int at;
1015 {
1016 if (at >= wPtr->entList.numItems) {
1017 /* The "end" position */
1018 Tix_LinkListAppend(&entListInfo, &wPtr->entList, (char*)chPtr, 0);
1019 }
1020 else {
1021 Tix_ListIterator li;
1022 Tix_LinkListIteratorInit(&li);
1023
1024 for (Tix_LinkListStart(&entListInfo, &wPtr->entList, &li);
1025 !Tix_LinkListDone(&li);
1026 Tix_LinkListNext (&entListInfo, &wPtr->entList, &li)) {
1027
1028 if (at == 0) {
1029 Tix_LinkListInsert(&entListInfo, &wPtr->entList,
1030 (char*)chPtr, &li);
1031 break;
1032 } else {
1033 -- at;
1034 }
1035 }
1036 }
1037
1038 return TCL_OK;
1039 }
1040
1041 /*----------------------------------------------------------------------
1042 * AllocEntry --
1043 *
1044 * Allocates memory for a new entry and initializes it to a
1045 * proper state.
1046 *----------------------------------------------------------------------
1047 */
1048 static ListEntry *
1049 AllocEntry(wPtr)
1050 WidgetPtr wPtr;
1051 {
1052 ListEntry * chPtr;
1053
1054 chPtr = (ListEntry *)ckalloc(sizeof(ListEntry));
1055 chPtr->state = NULL;
1056 chPtr->selected = 0;
1057 chPtr->iPtr = NULL;
1058
1059 return chPtr;
1060 }
1061
1062 /*----------------------------------------------------------------------
1063 * FreeEntry --
1064 *
1065 * Free the entry and all resources allocated to this entry.
1066 * This entry must already be deleted from the list.
1067 *----------------------------------------------------------------------
1068 */
1069 static void
1070 FreeEntry(wPtr, chPtr)
1071 WidgetPtr wPtr;
1072 ListEntry * chPtr;
1073 {
1074
1075 if (wPtr->seeElemPtr == chPtr) {
1076 /*
1077 * This is the element that should be visible the next time
1078 * we draw the window. Adjust the "to see element" to an element
1079 * that is close to it.
1080 */
1081 if (chPtr->next != NULL) {
1082 wPtr->seeElemPtr = chPtr->next;
1083 } else {
1084 ListEntry *p;
1085
1086 wPtr->seeElemPtr = NULL;
1087 for (p=(ListEntry*)wPtr->entList.head; p; p=p->next) {
1088 if (p->next == chPtr) {
1089 wPtr->seeElemPtr = p;
1090 break;
1091 }
1092 }
1093 }
1094 }
1095
1096 if (wPtr->anchor == chPtr) {
1097 wPtr->anchor = NULL;
1098 }
1099 if (wPtr->active == chPtr) {
1100 wPtr->active = NULL;
1101 }
1102 if (wPtr->dragSite == chPtr) {
1103 wPtr->dragSite = NULL;
1104 }
1105 if (wPtr->dropSite == chPtr) {
1106 wPtr->dropSite = NULL;
1107 }
1108
1109 if (chPtr->iPtr != NULL) {
1110 if (Tix_DItemType(chPtr->iPtr) == TIX_DITEM_WINDOW) {
1111 #if 0
1112 Tix_WindowItemListRemove(&wPtr->mappedWindows,
1113 chPtr->iPtr);
1114 #endif
1115 }
1116 Tix_DItemFree(chPtr->iPtr);
1117 }
1118
1119 Tk_FreeOptions(entryConfigSpecs, (char *)chPtr,wPtr->dispData.display, 0);
1120 ckfree((char*)chPtr);
1121 }
1122
1123 /*----------------------------------------------------------------------
1124 * "insert" sub command --
1125 *
1126 * Insert a new item into the list
1127 *----------------------------------------------------------------------
1128 */
1129 static int
1130 Tix_TLInsert(clientData, interp, argc, argv)
1131 ClientData clientData;
1132 Tcl_Interp *interp; /* Current interpreter. */
1133 int argc; /* Number of arguments. */
1134 char **argv; /* Argument strings. */
1135 {
1136 WidgetPtr wPtr = (WidgetPtr) clientData;
1137 ListEntry * chPtr = NULL;
1138 char buff[40];
1139 char * ditemType;
1140 int at;
1141 int added = 0;
1142 int code = TCL_OK;
1143
1144 /*------------------------------------------------------------
1145 * (1) We need to determine the options:
1146 * -itemtype and -at.
1147 *------------------------------------------------------------
1148 */
1149
1150 /* (1.1) Find out where */
1151 if (Tix_TranslateIndex(wPtr, interp, objv[0], &at, 1) != TCL_OK) {
1152 code = TCL_ERROR; goto done;
1153 }
1154
1155 /* (1.2) Find out the -itemtype, if specified */
1156 ditemType = wPtr->diTypePtr->name; /* default value */
1157 if (argc > 1) {
1158 size_t len;
1159 int i;
1160 if (argc %2 != 1) {
1161 Tcl_AppendResult(interp, "value for \"", argv[argc-1],
1162 "\" missing", NULL);
1163 code = TCL_ERROR; goto done;
1164 }
1165 for (i=1; i<argc; i+=2) {
1166 len = strlen(argv[i]);
1167 if (strncmp(argv[i], "-itemtype", len) == 0) {
1168 ditemType = argv[i+1];
1169 }
1170 }
1171 }
1172
1173 if (Tix_GetDItemType(interp, ditemType) == NULL) {
1174 code = TCL_ERROR; goto done;
1175 }
1176
1177 /*
1178 * (2) Allocate a new entry
1179 */
1180 chPtr = AllocEntry(wPtr);
1181
1182 /* (2.1) The Display item data */
1183 if ((chPtr->iPtr = Tix_DItemCreate(&wPtr->dispData, ditemType)) == NULL) {
1184 code = TCL_ERROR; goto done;
1185 }
1186 chPtr->iPtr->base.clientData = (ClientData)wPtr;
1187 chPtr->size[0] = chPtr->iPtr->base.size[0];
1188 chPtr->size[1] = chPtr->iPtr->base.size[1];
1189
1190 /*
1191 * (3) Add the entry into the list
1192 */
1193 if (AddElement(wPtr, chPtr, at) != TCL_OK) {
1194 code = TCL_ERROR; goto done;
1195 } else {
1196 added = 1;
1197 }
1198
1199 if (ConfigElement(wPtr, chPtr, argc-1, argv+1, 0, 1) != TCL_OK) {
1200 code = TCL_ERROR; goto done;
1201 }
1202
1203 ResizeWhenIdle(wPtr);
1204
1205 done:
1206 if (code == TCL_ERROR) {
1207 if (chPtr != NULL) {
1208 if (added) {
1209 Tix_LinkListFindAndDelete(&entListInfo, &wPtr->entList,
1210 (char*)chPtr, NULL);
1211 }
1212 FreeEntry(wPtr, chPtr);
1213 }
1214 } else {
1215 sprintf(buff, "%d", at);
1216 Tcl_AppendResult(interp, buff, NULL);
1217 }
1218
1219 return code;
1220 }
1221
1222 static int
1223 Tix_TLSpecialEntryInfo(wPtr, interp, chPtr)
1224 WidgetPtr wPtr;
1225 Tcl_Interp *interp;
1226 ListEntry * chPtr;
1227 {
1228 char buff[100];
1229
1230 if (chPtr) {
1231 int i;
1232 Tix_ListIterator li;
1233
1234 Tix_LinkListIteratorInit(&li);
1235
1236 for (i=0,Tix_LinkListStart(&entListInfo, &wPtr->entList, &li);
1237 !Tix_LinkListDone(&li);
1238 Tix_LinkListNext(&entListInfo, &wPtr->entList, &li),i++) {
1239 if (li.curr == (char*)chPtr) {
1240 break;
1241 }
1242 }
1243 if (li.curr != NULL) {
1244 sprintf(buff, "%d", i);
1245 Tcl_AppendResult(interp, buff, NULL);
1246 } else {
1247 panic("TList list entry is invalid");
1248 }
1249 } else {
1250 Tcl_ResetResult(interp);
1251 }
1252 return TCL_OK;
1253 }
1254
1255 /*----------------------------------------------------------------------
1256 * "index" sub command
1257 *----------------------------------------------------------------------
1258 */
1259 static int
1260 Tix_TLIndex(clientData, interp, argc, argv)
1261 ClientData clientData; /* TList widget record. */
1262 Tcl_Interp *interp; /* Current interpreter. */
1263 int argc; /* Number of arguments. */
1264 char **argv; /* Argument strings. */
1265 {
1266 WidgetPtr wPtr = (WidgetPtr) clientData;
1267 int index;
1268 char buff[100];
1269
1270 if (Tix_TranslateIndex(wPtr, interp, objv[0], &index, 0) != TCL_OK) {
1271 return TCL_ERROR;
1272 }
1273
1274 sprintf(buff, "%d", index);
1275 Tcl_AppendResult(interp, buff, NULL);
1276 return TCL_OK;
1277 }
1278
1279 /*----------------------------------------------------------------------
1280 * "info" sub command
1281 *----------------------------------------------------------------------
1282 */
1283 static int
1284 Tix_TLInfo(clientData, interp, argc, argv)
1285 ClientData clientData;
1286 Tcl_Interp *interp; /* Current interpreter. */
1287 int argc; /* Number of arguments. */
1288 char **argv; /* Argument strings. */
1289 {
1290 WidgetPtr wPtr = (WidgetPtr) clientData;
1291 size_t len = strlen(argv[0]);
1292
1293 if (strncmp(argv[0], "anchor", len)==0) {
1294 return Tix_TLSpecialEntryInfo(wPtr, interp, wPtr->anchor);
1295 }
1296 else if (strncmp(argv[0], "active", len)==0) {
1297 return Tix_TLSpecialEntryInfo(wPtr, interp, wPtr->active);
1298 }
1299 else if (strncmp(argv[0], "down", len)==0) {
1300 return Tix_TLGetNeighbor(wPtr, interp, TIX_DOWN, argc-1, argv+1);
1301 }
1302 else if (strncmp(argv[0], "left", len)==0) {
1303 return Tix_TLGetNeighbor(wPtr, interp, TIX_LEFT, argc-1, argv+1);
1304 }
1305 else if (strncmp(argv[0], "right", len)==0) {
1306 return Tix_TLGetNeighbor(wPtr, interp, TIX_RIGHT, argc-1, argv+1);
1307 }
1308 else if (strncmp(argv[0], "selection", len)==0) {
1309 ListEntry *chPtr;
1310 int i;
1311 char buffer[32];
1312
1313 for (chPtr=(ListEntry*)wPtr->entList.head, i=0;
1314 chPtr;
1315 chPtr=chPtr->next, i++) {
1316
1317 if (chPtr->selected) {
1318 Tcl_IntResults(interp,1,1,i);
1319 }
1320 }
1321 return TCL_OK;
1322 }
1323 else if (strncmp(argv[0], "size", len)==0) {
1324 char buff[100];
1325
1326 sprintf(buff, "%d", wPtr->entList.numItems);
1327 Tcl_AppendResult(interp, buff, NULL);
1328
1329 return TCL_OK;
1330 }
1331 else if (strncmp(argv[0], "up", len)==0) {
1332 return Tix_TLGetNeighbor(wPtr, interp, TIX_UP, argc-1, argv+1);
1333 }
1334 else {
1335 Tcl_AppendResult(interp, "unknown option \"", argv[0],
1336 "\": must be anchor or selection",
1337 NULL);
1338 return TCL_ERROR;
1339 }
1340 }
1341
1342 static int
1343 Tix_TranslateIndex(wPtr, interp, string, index, isInsert)
1344 WidgetPtr wPtr; /* TList widget record. */
1345 Tcl_Interp *interp; /* Current interpreter. */
1346 Tcl_Obj * string; /* String representation of the index. */
1347 int * index; /* Returns the index value (0 = 1st element).*/
1348 int isInsert; /* Is this function called by an "insert"
1349 * operation? */
1350 {
1351 if (strcmp(Tcl_GetString(string), "end") == 0) {
1352 *index = wPtr->entList.numItems;
1353 }
1354 else if (Tix_TLGetAt(wPtr, interp, Tcl_GetString(string), index) != TCL_OK) {
1355 if (Tcl_GetIntFromObj(interp, string, index) != TCL_OK) {
1356 return TCL_ERROR;
1357 }
1358 else if (*index < 0) {
1359 Tcl_AppendResult(interp,"expected non-negative integer but got \"",
1360 Tcl_GetString(string), "\"", NULL);
1361 return TCL_ERROR;
1362 }
1363 }
1364
1365
1366 /*
1367 * The meaning of "end" means:
1368 * isInsert:wPtr->entList.numItems
1369 * !isInsert:wPtr->entList.numItems-1;
1370 */
1371
1372 if (isInsert) {
1373 if (*index > wPtr->entList.numItems) {
1374 /*
1375 * By default add it to the end, just to follow what TK
1376 * does for the Listbox widget
1377 */
1378 *index = wPtr->entList.numItems;
1379 }
1380 } else {
1381 if (*index >= wPtr->entList.numItems) {
1382 /*
1383 * By default add it to the end, just to follow what TK
1384 * does for the Listbox widget
1385 */
1386 *index = wPtr->entList.numItems - 1;
1387 }
1388 }
1389
1390 if (*index < 0) {
1391 *index = 0;
1392 }
1393
1394 return TCL_OK;
1395 }
1396
1397 static int Tix_TLGetNeighbor(wPtr, interp, type, argc, argv)
1398 WidgetPtr wPtr;
1399 Tcl_Interp *interp; /* Current interpreter. */
1400 int type;
1401 int argc; /* Number of arguments. */
1402 char **argv; /* Argument strings. */
1403 {
1404 int index;
1405 int dst = 0;
1406 int xStep, yStep;
1407 int numPerRow;
1408 char buff[100];
1409
1410 if (argc != 1) {
1411 Tix_ArgcError(interp, argc+3, argv-3, 3, "index");
1412 }
1413
1414 if (Tix_TranslateIndex(wPtr, interp, objv[0], &index, 0) != TCL_OK) {
1415 return TCL_ERROR;
1416 }
1417
1418 if (wPtr->entList.numItems == 0) {
1419 Tcl_ResetResult(interp);
1420 return TCL_OK;
1421 }
1422
1423 numPerRow = wPtr->rows[0].numEnt;
1424
1425 if (wPtr->isVertical) {
1426 xStep = numPerRow;
1427 yStep = 1;
1428 } else {
1429 xStep = 1;
1430 yStep = numPerRow;
1431 }
1432
1433 switch (type) {
1434 case TIX_UP:
1435 dst = index - yStep;
1436 break;
1437 case TIX_DOWN:
1438 dst = index + yStep;
1439 break;
1440 case TIX_LEFT:
1441 dst = index - xStep;
1442 break;
1443 case TIX_RIGHT:
1444 dst = index + xStep;
1445 break;
1446 }
1447
1448 if (dst < 0) {
1449 dst = index;
1450 } else if (dst >= wPtr->entList.numItems) {
1451 dst = index;
1452 }
1453
1454 sprintf(buff, "%d", dst);
1455 Tcl_AppendResult(interp, buff, NULL);
1456
1457 return TCL_OK;
1458 }
1459
1460
1461
1462 /*----------------------------------------------------------------------
1463 * "cget" sub command --
1464 *----------------------------------------------------------------------
1465 */
1466 static int
1467 Tix_TLCGet(clientData, interp, argc, argv)
1468 ClientData clientData;
1469 Tcl_Interp *interp; /* Current interpreter. */
1470 int argc; /* Number of arguments. */
1471 char **argv; /* Argument strings. */
1472 {
1473 WidgetPtr wPtr = (WidgetPtr) clientData;
1474
1475 return Tk_ConfigureValue(interp, wPtr->dispData.tkwin, configSpecs,
1476 (char *)wPtr, argv[0], 0);
1477 }
1478
1479 /*----------------------------------------------------------------------
1480 * "configure" sub command
1481 *----------------------------------------------------------------------
1482 */
1483 static int
1484 Tix_TLConfig(clientData, interp, argc, argv)
1485 ClientData clientData;
1486 Tcl_Interp *interp; /* Current interpreter. */
1487 int argc; /* Number of arguments. */
1488 char **argv; /* Argument strings. */
1489 {
1490 WidgetPtr wPtr = (WidgetPtr) clientData;
1491
1492 if (argc == 0) {
1493 return Tk_ConfigureInfo(interp, wPtr->dispData.tkwin, configSpecs,
1494 (char *) wPtr, (char *) NULL, 0);
1495 } else if (argc == 1) {
1496 return Tk_ConfigureInfo(interp, wPtr->dispData.tkwin, configSpecs,
1497 (char *) wPtr, argv[0], 0);
1498 } else {
1499 return WidgetConfigure(interp, wPtr, argc, argv,
1500 TK_CONFIG_ARGV_ONLY);
1501 }
1502 }
1503
1504 /*----------------------------------------------------------------------
1505 * "geometryinfo" sub command
1506 *----------------------------------------------------------------------
1507 */
1508 static int
1509 Tix_TLGeometryInfo(clientData, interp, argc, argv)
1510 ClientData clientData;
1511 Tcl_Interp *interp; /* Current interpreter. */
1512 int argc; /* Number of arguments. */
1513 char **argv; /* Argument strings. */
1514 {
1515 WidgetPtr wPtr = (WidgetPtr) clientData;
1516 int qSize[2];
1517 double first[2], last[2];
1518 char string[40];
1519 int i;
1520
1521 if (argc == 2) {
1522 if (Tcl_GetIntFromObj(interp, argv[0], &qSize[0]) != TCL_OK) {
1523 return TCL_ERROR;
1524 }
1525 if (Tcl_GetIntFromObj(interp, argv[1], &qSize[1]) != TCL_OK) {
1526 return TCL_ERROR;
1527 }
1528 } else {
1529 qSize[0] = Tk_Width (wPtr->dispData.tkwin);
1530 qSize[1] = Tk_Height(wPtr->dispData.tkwin);
1531 }
1532 qSize[0] -= 2*wPtr->borderWidth + 2*wPtr->highlightWidth;
1533 qSize[1] -= 2*wPtr->borderWidth + 2*wPtr->highlightWidth;
1534
1535 for (i=0; i<2; i++) {
1536 qSize[i] -= 2*wPtr->borderWidth + 2*wPtr->highlightWidth;
1537 Tix_GetScrollFractions((Tix_ScrollInfo*)&wPtr->scrollInfo[i],
1538 &first[i], &last[i]);
1539 }
1540
1541 #if 0
1542 /* FIXME */
1543 sprintf(string, "{%f %f} {%f %f}", first[0], last[0], first[1], last[1]);
1544 Tcl_AppendResult(interp, string, NULL);
1545 #else
1546 /* Not quite right - one list of four rather than two lists of two */
1547 Tcl_DoubleResults(interp, 4, 1, first[0], last[0], first[1], last[1]);
1548 #endif
1549 return TCL_OK;
1550 }
1551
1552 /*----------------------------------------------------------------------
1553 * "delete" sub command
1554 *----------------------------------------------------------------------
1555 */
1556 static int
1557 Tix_TLDelete(clientData, interp, argc, argv)
1558 ClientData clientData;
1559 Tcl_Interp *interp; /* Current interpreter. */
1560 int argc; /* Number of arguments. */
1561 char **argv; /* Argument strings. */
1562 {
1563 WidgetPtr wPtr = (WidgetPtr) clientData;
1564 ListEntry * fromPtr, *toPtr;
1565 int code = TCL_OK;
1566
1567 if (argc < 1 || argc > 2) {
1568 Tix_ArgcError(interp, argc+2, argv-2, 2, "from ?to?");
1569 code = TCL_ERROR;
1570 goto done;
1571 }
1572
1573 if (Tix_TLGetFromTo(interp, wPtr, argc, argv, &fromPtr, &toPtr)!= TCL_OK) {
1574 code = TCL_ERROR;
1575 goto done;
1576 }
1577 if (fromPtr == NULL) {
1578 goto done;
1579 }
1580
1581 if (Tix_TLDeleteRange(wPtr, fromPtr, toPtr)) {
1582 ResizeWhenIdle(wPtr);
1583 }
1584
1585 done:
1586 return code;
1587 }
1588
1589 /* returns true if some element has been deleted */
1590 static int Tix_TLDeleteRange(wPtr, fromPtr, toPtr)
1591 WidgetPtr wPtr;
1592 ListEntry * fromPtr;
1593 ListEntry *toPtr;
1594 {
1595 int started;
1596 Tix_ListIterator li;
1597
1598 Tix_LinkListIteratorInit(&li);
1599 started = 0;
1600 for (Tix_LinkListStart(&entListInfo, &wPtr->entList, &li);
1601 !Tix_LinkListDone(&li);
1602 Tix_LinkListNext (&entListInfo, &wPtr->entList, &li)) {
1603
1604 ListEntry * curr = (ListEntry *)li.curr;
1605 if (curr == fromPtr) {
1606 started = 1;
1607 }
1608 if (started) {
1609 Tix_LinkListDelete(&entListInfo, &wPtr->entList, &li);
1610 FreeEntry(wPtr, curr);
1611 }
1612 if (curr == toPtr) {
1613 break;
1614 }
1615 }
1616
1617 return started;
1618 }
1619
1620
1621 /*----------------------------------------------------------------------
1622 * "entrycget" sub command
1623 *----------------------------------------------------------------------
1624 */
1625 static int
1626 Tix_TLEntryCget(clientData, interp, argc, argv)
1627 ClientData clientData;
1628 Tcl_Interp *interp; /* Current interpreter. */
1629 int argc; /* Number of arguments. */
1630 char **argv; /* Argument strings. */
1631 {
1632 WidgetPtr wPtr = (WidgetPtr) clientData;
1633 ListEntry * chPtr, * dummy;
1634
1635 if (Tix_TLGetFromTo(interp, wPtr, 1, argv, &chPtr, &dummy)
1636 != TCL_OK) {
1637 return TCL_ERROR;
1638 }
1639
1640 if (chPtr == NULL) {
1641 Tcl_AppendResult(interp, "list entry \"", argv[0],
1642 "\" does not exist", NULL);
1643 return TCL_ERROR;
1644 }
1645
1646 return Tix_ConfigureValue2(interp, wPtr->dispData.tkwin, (char *)chPtr,
1647 entryConfigSpecs, chPtr->iPtr, argv[1], 0);
1648 }
1649
1650 /*----------------------------------------------------------------------
1651 * "entryconfigure" sub command
1652 *----------------------------------------------------------------------
1653 */
1654 static int
1655 Tix_TLEntryConfig(clientData, interp, argc, argv)
1656 ClientData clientData;
1657 Tcl_Interp *interp; /* Current interpreter. */
1658 int argc; /* Number of arguments. */
1659 char **argv; /* Argument strings. */
1660 {
1661 WidgetPtr wPtr = (WidgetPtr) clientData;
1662 ListEntry * chPtr, * dummy;
1663
1664 if (Tix_TLGetFromTo(interp, wPtr, 1, argv, &chPtr, &dummy)
1665 != TCL_OK) {
1666 return TCL_ERROR;
1667 }
1668
1669 if (chPtr == NULL) {
1670 Tcl_AppendResult(interp, "list entry \"", argv[0],
1671 "\" does not exist", NULL);
1672 return TCL_ERROR;
1673 }
1674
1675 if (argc == 1) {
1676 return Tix_ConfigureInfo2(interp, wPtr->dispData.tkwin,
1677 (char*)chPtr, entryConfigSpecs, chPtr->iPtr, (char *) NULL, 0);
1678 } else if (argc == 2) {
1679 return Tix_ConfigureInfo2(interp, wPtr->dispData.tkwin,
1680 (char*)chPtr, entryConfigSpecs, chPtr->iPtr, (char *) argv[1], 0);
1681 } else {
1682 return ConfigElement(wPtr, chPtr, argc-1, argv+1,
1683 TK_CONFIG_ARGV_ONLY, 0);
1684 }
1685 }
1686
1687 /*----------------------------------------------------------------------
1688 * "nearest" sub command
1689 *----------------------------------------------------------------------
1690 */
1691 static int
1692 Tix_TLNearest(clientData, interp, argc, argv)
1693 ClientData clientData;
1694 Tcl_Interp *interp; /* Current interpreter. */
1695 int argc; /* Number of arguments. */
1696 char **argv; /* Argument strings. */
1697 {
1698 WidgetPtr wPtr = (WidgetPtr) clientData;
1699 int posn[2];
1700 int index;
1701 char buff[100];
1702
1703 if (Tcl_GetIntFromObj(interp, argv[0], &posn[0]) != TCL_OK) {
1704 return TCL_ERROR;
1705 }
1706 if (Tcl_GetIntFromObj(interp, argv[1], &posn[1]) != TCL_OK) {
1707 return TCL_ERROR;
1708 }
1709
1710 index = Tix_TLGetNearest(wPtr, posn);
1711 Tcl_ResetResult(interp);
1712
1713 if (index != -1) {
1714 sprintf(buff, "%d", index);
1715 Tcl_AppendResult(interp, buff, NULL);
1716 }
1717 return TCL_OK;
1718 }
1719
1720 static int Tix_TLGetAt(wPtr, interp, spec, at)
1721 WidgetPtr wPtr;
1722 Tcl_Interp *interp;
1723 char * spec;
1724 int *at;
1725 {
1726 int posn[2];
1727 char *p, *end;
1728
1729 if (spec[0] != '@') {
1730 return TCL_ERROR;
1731 }
1732
1733 p = spec+1;
1734 posn[0] = strtol(p, &end, 0);
1735 if ((end == p) || (*end != ',')) {
1736 return TCL_ERROR;
1737 }
1738 p = end+1;
1739 posn[1] = strtol(p, &end, 0);
1740 if ((end == p) || (*end != 0)) {
1741 return TCL_ERROR;
1742 }
1743
1744 *at = Tix_TLGetNearest(wPtr, posn);
1745 return TCL_OK;
1746 }
1747
1748 static int Tix_TLGetNearest(wPtr, posn)
1749 WidgetPtr wPtr;
1750 int posn[2];
1751 {
1752 int i, j, index;
1753 int maxX, maxY;
1754 int r, c;
1755
1756 if (wPtr->resizing) {
1757 ResizeNow(wPtr);
1758 }
1759
1760 if (wPtr->entList.numItems == 0) {
1761 return -1;
1762 }
1763
1764 /* clip off the position with the border of the window */
1765
1766 posn[0] -= wPtr->borderWidth + wPtr->highlightWidth;
1767 posn[1] -= wPtr->borderWidth + wPtr->highlightWidth;
1768
1769 maxX = Tk_Width (wPtr->dispData.tkwin);
1770 maxY = Tk_Height(wPtr->dispData.tkwin);
1771
1772 maxX -= 2*(wPtr->borderWidth + wPtr->highlightWidth);
1773 maxY -= 2*(wPtr->borderWidth + wPtr->highlightWidth);
1774
1775 if (posn[0] >= maxX) {
1776 posn[0] = maxX -1;
1777 }
1778 if (posn[1] >= maxY) {
1779 posn[1] = maxY -1;
1780 }
1781 if (posn[0] < 0) {
1782 posn[0] = 0;
1783 }
1784 if (posn[1] < 0) {
1785 posn[1] = 0;
1786 }
1787
1788 i = (wPtr->isVertical == 0);
1789 j = (wPtr->isVertical == 1);
1790
1791 posn[0] += wPtr->scrollInfo[0].offset;
1792 posn[1] += wPtr->scrollInfo[1].offset;
1793
1794 r = posn[i] / wPtr->maxSize[i];
1795 c = posn[j] / wPtr->maxSize[j];
1796
1797 index = (r * wPtr->rows[0].numEnt) + c;
1798
1799 if (index >= wPtr->entList.numItems) {
1800 index = wPtr->entList.numItems - 1;
1801 }
1802
1803 return index;
1804 }
1805
1806 /*----------------------------------------------------------------------
1807 * "selection" sub command
1808 * Modify the selection in this HList box
1809 *----------------------------------------------------------------------
1810 */
1811 static int
1812 Tix_TLGetFromTo(interp, wPtr, argc, argv, fromPtr_ret, toPtr_ret)
1813 Tcl_Interp *interp;
1814 WidgetPtr wPtr;
1815 int argc;
1816 char **argv;
1817 ListEntry ** fromPtr_ret;
1818 ListEntry ** toPtr_ret;
1819 {
1820 /*
1821 * ToDo: make it more efficient by saving the previous from and to
1822 * pointers and make the list of childrens a doubly-linked list
1823 */
1824 ListEntry * fromPtr;
1825 ListEntry * toPtr;
1826 int from, to, tmp;
1827
1828 if (Tix_TranslateIndex(wPtr, interp, objv[0], &from, 0) != TCL_OK) {
1829 return TCL_ERROR;
1830 }
1831 if (argc == 2) {
1832 if (Tix_TranslateIndex(wPtr, interp, objv[1], &to, 0) != TCL_OK) {
1833 return TCL_ERROR;
1834 }
1835 } else {
1836 to = from;
1837 }
1838
1839 if (from > to) {
1840 /* swap from and to */
1841 tmp = to; to = from; from = tmp;
1842 }
1843
1844 fromPtr = NULL;
1845 toPtr = NULL;
1846
1847 if (from >= wPtr->entList.numItems) {
1848 fromPtr = (ListEntry *)wPtr->entList.tail;
1849 toPtr = (ListEntry *)wPtr->entList.tail;
1850 }
1851 if (to >= wPtr->entList.numItems) {
1852 toPtr = (ListEntry *)wPtr->entList.tail;
1853 }
1854
1855 if (fromPtr == NULL) {
1856 for (fromPtr = (ListEntry*)wPtr->entList.head;
1857 from > 0;
1858 fromPtr=fromPtr->next) {
1859
1860 -- from;
1861 -- to;
1862 }
1863 }
1864 if (toPtr == NULL) {
1865 for (toPtr = fromPtr; to > 0; toPtr=toPtr->next) {
1866 -- to;
1867 }
1868 }
1869
1870 * fromPtr_ret = fromPtr;
1871 if (toPtr_ret) {
1872 * toPtr_ret = toPtr;
1873 }
1874 return TCL_OK;
1875 }
1876
1877 static int
1878 Tix_TLSelection(clientData, interp, argc, argv)
1879 ClientData clientData;
1880 Tcl_Interp *interp; /* Current interpreter. */
1881 int argc; /* Number of arguments. */
1882 char **argv; /* Argument strings. */
1883 {
1884 WidgetPtr wPtr = (WidgetPtr) clientData;
1885 size_t len = strlen(argv[0]);
1886 int code = TCL_OK;
1887 int changed = 0;
1888 ListEntry * chPtr, * fromPtr, * toPtr;
1889
1890 if (strncmp(argv[0], "clear", len)==0) {
1891 if (argc == 1) {
1892 /*
1893 * Clear all entries
1894 */
1895 for (chPtr=(ListEntry*)wPtr->entList.head;
1896 chPtr;
1897 chPtr=chPtr->next) {
1898
1899 chPtr->selected = 0;
1900 }
1901 changed = 1;
1902 }
1903 else {
1904 if (Tix_TLGetFromTo(interp, wPtr, argc-1, argv+1, &fromPtr, &toPtr)
1905 != TCL_OK) {
1906 code = TCL_ERROR;
1907 goto done;
1908 }
1909 if (fromPtr == NULL) {
1910 goto done;
1911 }
1912 else {
1913 while (1) {
1914 fromPtr->selected = 0;
1915 if (fromPtr == toPtr) {
1916 break;
1917 } else {
1918 fromPtr=fromPtr->next;
1919 }
1920 }
1921 changed = 1;
1922 goto done;
1923 }
1924 }
1925 }
1926 else if (strncmp(argv[0], "includes", len)==0) {
1927 if (argc != 2) {
1928 Tix_ArgcError(interp, argc+2, argv-2, 3, "index");
1929 code = TCL_ERROR;
1930 goto done;
1931 }
1932 if (Tix_TLGetFromTo(interp, wPtr, argc-1, argv+1, &fromPtr, &toPtr)
1933 != TCL_OK) {
1934 code = TCL_ERROR;
1935 goto done;
1936 }
1937 if (fromPtr->selected) {
1938 Tcl_AppendResult(interp, "1", NULL);
1939 } else {
1940 Tcl_AppendResult(interp, "0", NULL);
1941 }
1942 }
1943 else if (strncmp(argv[0], "set", len)==0) {
1944 if (argc < 2 || argc > 3) {
1945 Tix_ArgcError(interp, argc+2, argv-2, 3, "from ?to?");
1946 code = TCL_ERROR;
1947 goto done;
1948 }
1949
1950 if (Tix_TLGetFromTo(interp, wPtr, argc-1, argv+1, &fromPtr, &toPtr)
1951 != TCL_OK) {
1952 code = TCL_ERROR;
1953 goto done;
1954 }
1955 if (fromPtr == NULL) {
1956 goto done;
1957 }
1958 else {
1959 while (1) {
1960 fromPtr->selected = 1;
1961 if (fromPtr == toPtr) {
1962 break;
1963 } else {
1964 fromPtr=fromPtr->next;
1965 }
1966 }
1967 changed = 1;
1968 goto done;
1969 }
1970 }
1971 else {
1972 Tcl_AppendResult(interp, "unknown option \"", argv[0],
1973 "\": must be anchor, clear, includes or set", NULL);
1974 code = TCL_ERROR;
1975 }
1976
1977 done:
1978 if (changed) {
1979 RedrawWhenIdle(wPtr);
1980 }
1981
1982 return code;
1983 }
1984
1985 /*----------------------------------------------------------------------
1986 * "see" command
1987 *----------------------------------------------------------------------
1988 */
1989 static int
1990 Tix_TLSee(clientData, interp, argc, argv)
1991 ClientData clientData;
1992 Tcl_Interp *interp; /* Current interpreter. */
1993 int argc; /* Number of arguments. */
1994 char **argv; /* Argument strings. */
1995 {
1996 WidgetPtr wPtr = (WidgetPtr) clientData;
1997 ListEntry * chPtr, * dummy;
1998
1999 if (argc == 1) {
2000 if (Tix_TLGetFromTo(interp, wPtr, 1, argv, &chPtr, &dummy) != TCL_OK) {
2001 return TCL_ERROR;
2002 }
2003 if (chPtr != NULL) {
2004 wPtr->seeElemPtr = chPtr;
2005 RedrawWhenIdle(wPtr);
2006 }
2007 } else {
2008 Tcl_AppendResult(interp, "wrong # of arguments, must be: ",
2009 Tk_PathName(wPtr->dispData.tkwin), " ", argv[-1],
2010 " index", NULL);
2011 }
2012
2013 return TCL_OK;
2014 }
2015
2016 /*----------------------------------------------------------------------
2017 * "anchor", "dragsite" and "dropsite" sub commands --
2018 *
2019 * Set/remove the anchor element
2020 *----------------------------------------------------------------------
2021 */
2022 static int
2023 Tix_TLSetSite(clientData, interp, argc, argv)
2024 ClientData clientData;
2025 Tcl_Interp *interp; /* Current interpreter. */
2026 int argc; /* Number of arguments. */
2027 char **argv; /* Argument strings. */
2028 {
2029 int changed = 0;
2030 WidgetPtr wPtr = (WidgetPtr) clientData;
2031 ListEntry * fromPtr;
2032 ListEntry * toPtr; /* unused */
2033 ListEntry ** changePtr;
2034 size_t len;
2035
2036 /* Determine which site should be changed (the last else clause
2037 * doesn't need to check the string because HandleSubCommand
2038 * already ensures that only the valid options can be specified.
2039 **/
2040 len = strlen(argv[-1]);
2041 if (strncmp(argv[-1], "anchor", len)==0) {
2042 changePtr = &wPtr->anchor;
2043 }
2044 else if (strncmp(argv[-1], "active", len)==0) {
2045 changePtr = &wPtr->active;
2046 }
2047 else if (strncmp(argv[-1], "dragsite", len)==0) {
2048 changePtr = &wPtr->dragSite;
2049 }
2050 else {
2051 changePtr = &wPtr->dropSite;
2052 }
2053
2054 len = strlen(argv[0]);
2055 if (strncmp(argv[0], "set", len)==0) {
2056 if (argc == 2) {
2057 if (Tix_TLGetFromTo(interp,wPtr, argc-1, argv+1, &fromPtr, &toPtr)
2058 != TCL_OK) {
2059 return TCL_ERROR;
2060 }
2061 if (*changePtr != fromPtr) {
2062 *changePtr = fromPtr;
2063 changed = 1;
2064 }
2065 } else {
2066 Tcl_AppendResult(interp, "wrong # of arguments, must be: ",
2067 Tk_PathName(wPtr->dispData.tkwin), " ", argv[-1],
2068 " set index", NULL);
2069 return TCL_ERROR;
2070 }
2071 }
2072 else if (strncmp(argv[0], "clear", len)==0) {
2073 if (*changePtr != NULL) {
2074 *changePtr = NULL;
2075 changed = 1;
2076 }
2077 }
2078 else {
2079 Tcl_AppendResult(interp, "wrong option \"", argv[0], "\", ",
2080 "must be clear or set", NULL);
2081 return TCL_ERROR;
2082 }
2083
2084 if (changed) {
2085 RedrawWhenIdle(wPtr);
2086 }
2087
2088 return TCL_OK;
2089 }
2090
2091 /*----------------------------------------------------------------------
2092 * "xview" and "yview" sub command
2093 *----------------------------------------------------------------------
2094 */
2095 static int
2096 Tix_TLView(clientData, interp, argc, argv)
2097 ClientData clientData;
2098 Tcl_Interp *interp; /* Current interpreter. */
2099 int argc; /* Number of arguments. */
2100 char **argv; /* Argument strings. */
2101 {
2102 WidgetPtr wPtr = (WidgetPtr) clientData;
2103 int axis;
2104
2105 if (argv[-1][0] == 'x') {
2106 axis = 0;
2107 } else {
2108 axis = 1;
2109 }
2110
2111 if (argc == 0) {
2112 char string[80];
2113 double first, last;
2114
2115 Tix_GetScrollFractions((Tix_ScrollInfo*)&wPtr->scrollInfo[axis],
2116 &first, &last);
2117
2118 #if 0
2119 sprintf(string, "{%f %f}", first, last);
2120 Tcl_AppendResult(interp, string, NULL);
2121 #else
2122 Tcl_DoubleResults(interp, 2, 1, first, last);
2123 #endif
2124 return TCL_OK;
2125 }
2126 else if (Tix_SetScrollBarView(interp,
2127 (Tix_ScrollInfo*)&wPtr->scrollInfo[axis], argc, argv, 0) != TCL_OK) {
2128
2129 return TCL_ERROR;
2130 }
2131
2132 UpdateScrollBars(wPtr, 0);
2133 RedrawWhenIdle(wPtr);
2134 return TCL_OK;
2135 }
2136 /*----------------------------------------------------------------------
2137 *
2138 *
2139 * Memory Management Section
2140 *
2141 *
2142 *----------------------------------------------------------------------
2143 */
2144 static int
2145 ConfigElement(wPtr, chPtr, argc, argv, flags, forced)
2146 WidgetPtr wPtr;
2147 ListEntry *chPtr;
2148 int argc;
2149 char ** argv;
2150 int flags;
2151 int forced;
2152 {
2153 int sizeChanged;
2154
2155 if (Tix_WidgetConfigure2(wPtr->dispData.interp, wPtr->dispData.tkwin,
2156 (char*)chPtr, entryConfigSpecs, chPtr->iPtr, argc, argv, flags,
2157 forced, &sizeChanged) != TCL_OK) {
2158 return TCL_ERROR;
2159 }
2160
2161 if (sizeChanged) {
2162 chPtr->size[0] = chPtr->iPtr->base.size[0];
2163 chPtr->size[1] = chPtr->iPtr->base.size[1];
2164 ResizeWhenIdle(wPtr);
2165 } else {
2166 RedrawWhenIdle(wPtr);
2167 }
2168 return TCL_OK;
2169 }
2170
2171 static void
2172 Realloc(wPtr, new_size)
2173 WidgetPtr wPtr;
2174 int new_size;
2175 {
2176 if (new_size < 1) {
2177 new_size = 1;
2178 }
2179 if (new_size == wPtr->numRowAllocd) {
2180 return;
2181 }
2182 wPtr->rows = (ListRow*)ckrealloc( (char *)wPtr->rows,
2183 sizeof(ListRow)*new_size);
2184 wPtr->numRowAllocd = new_size;
2185 }
2186
2187 static void ResizeRows(wPtr, winW, winH)
2188 WidgetPtr wPtr;
2189 int winW; /* -1 == current width */
2190 int winH; /* -1 == current height */
2191 {
2192 ListEntry * chPtr;
2193 ListEntry * rowHead;
2194 int n, c, r;
2195 int maxI; /* max of width in the current column */
2196 int maxJ; /* max of height among all elements */
2197 int curRow;
2198 int i, j;
2199 int sizeJ;
2200 int winSize[2];
2201
2202 if (wPtr->isVertical) {
2203 i = 0; /* Column major, 0,0 -> 0,1 -> 0,2 ... -> 1,0 */
2204 j = 1;
2205 } else {
2206 i = 1; /* Row major, 0,0 -> 1,0 -> 2,0 ... -> 0,1 */
2207 j = 0;
2208 }
2209
2210 if (winW == -1) {
2211 winW = Tk_Width (wPtr->dispData.tkwin);
2212 }
2213 if (winH == -1) {
2214 winH = Tk_Height(wPtr->dispData.tkwin);
2215 }
2216
2217 winSize[0] = winW;
2218 winSize[1] = winH;
2219
2220 if (wPtr->entList.numItems == 0) {
2221 wPtr->rows[0].chPtr = NULL;
2222 wPtr->rows[0].size[0] = 1;
2223 wPtr->rows[0].size[1] = 1;
2224 wPtr->rows[0].numEnt = 0;
2225
2226 wPtr->numRow = 1;
2227 goto done;
2228 }
2229
2230 /* -- The following verbal description follows the "Column Major"
2231 * model. Row major are similar, just the i,j incides are swapped
2232 *
2233 * (1) (a) Search for the tallest element, use it as the height of all
2234 * the elements;
2235 * (b) Search for the widest element, use it as the width of all
2236 * the elements;
2237 */
2238 for (maxJ=1, maxI=1, chPtr = (ListEntry*)wPtr->entList.head;
2239 chPtr;
2240 chPtr=chPtr->next) {
2241
2242 if (maxJ < chPtr->iPtr->base.size[j]) {
2243 maxJ = chPtr->iPtr->base.size[j];
2244 }
2245 if (maxI < chPtr->iPtr->base.size[i]) {
2246 maxI = chPtr->iPtr->base.size[i];
2247 }
2248 }
2249 wPtr->maxSize[i] = maxI;
2250 wPtr->maxSize[j] = maxJ;
2251
2252 /* (2) Calculate how many elements can be in each column
2253 *
2254 */
2255 n = winSize[j] / maxJ;
2256 if (n <=0) {
2257 n = 1;
2258 }
2259
2260 wPtr->numRow = 0;
2261 curRow = 0;
2262 c = 0;
2263 sizeJ = 0;
2264 rowHead = (ListEntry*)wPtr->entList.head;
2265 for(chPtr = (ListEntry*)wPtr->entList.head; chPtr; chPtr=chPtr->next) {
2266 sizeJ += chPtr->iPtr->base.size[j];
2267 ++ c;
2268 if (c == n || chPtr->next == NULL) {
2269 if (curRow >= wPtr->numRowAllocd) {
2270 Realloc(wPtr, curRow*2);
2271 }
2272 wPtr->rows[curRow].chPtr = rowHead;
2273 wPtr->rows[curRow].size[i] = maxI;
2274 wPtr->rows[curRow].size[j] = sizeJ;
2275 wPtr->rows[curRow].numEnt = c;
2276 ++ curRow;
2277 ++ wPtr->numRow;
2278 c = 0;
2279 rowHead = chPtr->next;
2280 sizeJ = 0;
2281 }
2282 }
2283
2284 done:
2285 /* calculate the size of the total and visible area */
2286 wPtr->scrollInfo[i].total = 0;
2287 wPtr->scrollInfo[j].total = 0;
2288
2289 for (r=0; r<wPtr->numRow; r++) {
2290 wPtr->scrollInfo[i].total += wPtr->rows[r].size[i];
2291 if (wPtr->scrollInfo[j].total < wPtr->rows[r].size[j]) {
2292 wPtr->scrollInfo[j].total = wPtr->rows[r].size[j];
2293 }
2294 }
2295
2296 wPtr->scrollInfo[i].window = winSize[i];
2297 wPtr->scrollInfo[j].window = winSize[j];
2298
2299 if (wPtr->scrollInfo[i].total < 1) {
2300 wPtr->scrollInfo[i].total = 1;
2301 }
2302 if (wPtr->scrollInfo[j].total < 1) {
2303 wPtr->scrollInfo[j].total = 1;
2304 }
2305 if (wPtr->scrollInfo[i].window < 1) {
2306 wPtr->scrollInfo[i].window = 1;
2307 }
2308 if (wPtr->scrollInfo[j].window < 1) {
2309 wPtr->scrollInfo[j].window = 1;
2310 }
2311
2312 /* If we have much fewer rows now, adjust the size of the rows list */
2313 if (wPtr->numRowAllocd > (2*wPtr->numRow)) {
2314 Realloc(wPtr, 2*wPtr->numRow);
2315 }
2316
2317 /* Update the scrollbars */
2318
2319 UpdateScrollBars(wPtr, 1);
2320 }
2321 /*----------------------------------------------------------------------
2322 * RedrawRows --
2323 *
2324 * Redraw the rows, according to the "offset: in both directions
2325 *----------------------------------------------------------------------
2326 */
2327
2328 static void
2329 RedrawRows(wPtr, pixmap)
2330 WidgetPtr wPtr;
2331 Drawable pixmap;
2332 {
2333 int r, n;
2334 int p[2];
2335 ListEntry * chPtr;
2336 int i, j;
2337 int total;
2338 int windowSize;
2339
2340 if (wPtr->entList.numItems == 0) {
2341 return;
2342 }
2343
2344 if (wPtr->isVertical) {
2345 i = 0; /* Column major, 0,0 -> 0,1 -> 0,2 ... -> 1,0 */
2346 j = 1;
2347 windowSize = Tk_Width(wPtr->dispData.tkwin);
2348 } else {
2349 i = 1; /* Row major, 0,0 -> 1,0 -> 2,0 ... -> 0,1 */
2350 j = 0;
2351 windowSize = Tk_Height(wPtr->dispData.tkwin);
2352 }
2353
2354 p[i] = wPtr->highlightWidth + wPtr->borderWidth;
2355 windowSize -= 2*p[i];
2356
2357 if (windowSize < 1) {
2358 windowSize = 1;
2359 }
2360
2361 if (wPtr->seeElemPtr != NULL) {
2362 /*
2363 * Adjust the scrolling so that the given entry is visible.
2364 */
2365 int start = 0; /* x1 position of the element to see. */
2366 int size = 0; /* width of the element to see. */
2367 int old = wPtr->scrollInfo[i].offset;
2368
2369 for (r=0, n=0, chPtr=(ListEntry*)wPtr->entList.head; chPtr;
2370 chPtr=chPtr->next, n++) {
2371 if (n == wPtr->rows[r].numEnt) {
2372 n=0;
2373 r++;
2374 start += wPtr->rows[r].size[i];
2375 }
2376 if (chPtr == wPtr->seeElemPtr) {
2377 size = wPtr->rows[r].size[i];
2378 break;
2379 }
2380 }
2381
2382 if ((wPtr->scrollInfo[i].offset + windowSize) < (start + size)) {
2383 wPtr->scrollInfo[i].offset = start + size - windowSize;
2384 }
2385 if (wPtr->scrollInfo[i].offset > start) {
2386 wPtr->scrollInfo[i].offset = start;
2387 }
2388 if (wPtr->scrollInfo[i].offset != old) {
2389 UpdateScrollBars(wPtr, 0);
2390 }
2391 wPtr->seeElemPtr = NULL;
2392 }
2393
2394 /* Search for a row that is (possibly partially) visible*/
2395 total=0; r=0;
2396 if (wPtr->scrollInfo[i].offset != 0) {
2397 for (; r<wPtr->numRow; r++) {
2398 total += wPtr->rows[r].size[i];
2399
2400 if (total > wPtr->scrollInfo[i].offset) {
2401 p[i] -= wPtr->scrollInfo[i].offset -
2402 (total - wPtr->rows[r].size[i]);
2403 break;
2404 }
2405 if (total == wPtr->scrollInfo[i].offset) {
2406 r++;
2407 break;
2408 }
2409 }
2410 }
2411
2412 /* Redraw all the visible rows */
2413 for (; r<wPtr->numRow; r++) {
2414
2415 p[j] = wPtr->highlightWidth + wPtr->borderWidth;
2416
2417 total=0; n=0; chPtr=wPtr->rows[r].chPtr;
2418 if (wPtr->scrollInfo[j].offset > 0) {
2419 /* Search for a column that is (possibly partially) visible*/
2420 for (;
2421 n<wPtr->rows[r].numEnt;
2422 n++, chPtr = chPtr->next) {
2423
2424 total += chPtr->iPtr->base.size[j];
2425 if (total > wPtr->scrollInfo[j].offset) {
2426 /* Adjust for the shift due to partially visible elements*/
2427 p[j] -= wPtr->scrollInfo[j].offset -
2428 (total - chPtr->iPtr->base.size[j]);
2429 break;
2430 }
2431 if (total == wPtr->scrollInfo[j].offset) {
2432 n++; chPtr = chPtr->next;
2433 break;
2434 }
2435 }
2436 }
2437
2438 /* Redraw all the visible columns in this row */
2439 for (; n<wPtr->rows[r].numEnt; n++, chPtr = chPtr->next) {
2440 int flags = TIX_DITEM_NORMAL_FG;
2441 int W, H;
2442
2443 if (chPtr->selected) {
2444 flags |= TIX_DITEM_SELECTED_FG;
2445 flags |= TIX_DITEM_SELECTED_BG;
2446 }
2447
2448 #if 0
2449 if (wPtr->isVertical) {
2450 W = wPtr->rows[r].size[0];
2451 H = chPtr->iPtr->base.size[1];
2452 } else {
2453 H = wPtr->rows[r].size[1];
2454 W = chPtr->iPtr->base.size[0];
2455 }
2456 #else
2457 W = wPtr->maxSize[0];
2458 H = wPtr->maxSize[1];
2459 #endif
2460
2461 Tix_DItemDisplay(pixmap, None, chPtr->iPtr, p[0], p[1], W, H,
2462 flags);
2463
2464 if (chPtr == wPtr->anchor) {
2465 Tix_DrawAnchorLines(Tk_Display(wPtr->dispData.tkwin), pixmap,
2466 wPtr->anchorGC, p[0], p[1], W-1, H-1);
2467 }
2468 p[j] += wPtr->maxSize[j];
2469 }
2470
2471 /* advance to the next row */
2472 p[i]+= wPtr->rows[r].size[i];
2473 }
2474 }
2475
2476 /*----------------------------------------------------------------------
2477 * UpdateScrollBars
2478 *----------------------------------------------------------------------
2479 */
2480 static void UpdateScrollBars(wPtr, sizeChanged)
2481 WidgetPtr wPtr;
2482 int sizeChanged;
2483 {
2484 Tix_UpdateScrollBar(wPtr->dispData.interp,
2485 (Tix_ScrollInfo*)&wPtr->scrollInfo[0]);
2486 Tix_UpdateScrollBar(wPtr->dispData.interp,
2487 (Tix_ScrollInfo*)&wPtr->scrollInfo[1]);
2488
2489 if (wPtr->sizeCmd && sizeChanged) {
2490 if (LangDoCallback(wPtr->dispData.interp, wPtr->sizeCmd, 0, 0) != TCL_OK) {
2491 Tcl_AddErrorInfo(wPtr->dispData.interp,
2492 "\n (size command executed by tixTList)");
2493 Tcl_BackgroundError(wPtr->dispData.interp);
2494 }
2495 }
2496 }