Debian Codegraph @

generic/tkScrollbar.c

raw symbols SCIP
1
2 /*
3 * tkScrollbar.c --
4 *
5 * This module implements a scrollbar widgets for the Tk
6 * toolkit. A scrollbar displays a slider and two arrows;
7 * mouse clicks on features within the scrollbar cause
8 * scrolling commands to be invoked.
9 *
10 * Copyright (c) 1990-1994 The Regents of the University of California.
11 * Copyright (c) 1994-1995 Sun Microsystems, Inc.
12 *
13 * See the file "license.terms" for information on usage and redistribution
14 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
15 *
16 * SCCS: @(#) tkScrollbar.c 1.79 96/02/15 18:52:40
17 */
18
19 #include "bltInt.h"
20
21 #ifndef NO_TILESCROLLBAR
22
23 #include "bltTile.h"
24
25 extern Tk_CustomOption bltTileOption;
26
27 #define NORMAL_BG "#d9d9d9"
28 #define ACTIVE_BG "#ececec"
29 #define SELECT_BG "#c3c3c3"
30 #define TROUGH "#c3c3c3"
31 #define INDICATOR "#b03060"
32 #define DISABLED "#a3a3a3"
33
34 /*
35 * Defaults for scrollbars:
36 */
37 #define DEF_SCROLLBAR_ACTIVE_BACKGROUND ACTIVE_BG
38 #define DEF_SCROLLBAR_ACTIVE_BG_MONO RGB_BLACK
39 #define DEF_SCROLLBAR_ACTIVE_RELIEF "raised"
40 #define DEF_SCROLLBAR_BACKGROUND NORMAL_BG
41 #define DEF_SCROLLBAR_BG_MONO RGB_WHITE
42 #define DEF_SCROLLBAR_BORDERWIDTH "2"
43 #define DEF_SCROLLBAR_COMMAND ""
44 #define DEF_SCROLLBAR_CURSOR ""
45 #define DEF_SCROLLBAR_EL_BORDERWIDTH "-1"
46 #define DEF_SCROLLBAR_HIGHLIGHT_BG NORMAL_BG
47 #define DEF_SCROLLBAR_HIGHLIGHT RGB_BLACK
48 #define DEF_SCROLLBAR_HIGHLIGHT_WIDTH "2"
49 #define DEF_SCROLLBAR_JUMP "0"
50 #define DEF_SCROLLBAR_ORIENT "vertical"
51 #define DEF_SCROLLBAR_RELIEF "sunken"
52 #define DEF_SCROLLBAR_REPEAT_DELAY "300"
53 #define DEF_SCROLLBAR_REPEAT_INTERVAL "100"
54 #define DEF_SCROLLBAR_TAKE_FOCUS (char *) NULL
55 #define DEF_SCROLLBAR_TROUGH_COLOR TROUGH
56 #define DEF_SCROLLBAR_TROUGH_MONO RGB_WHITE
57 #define DEF_SCROLLBAR_WIDTH "15"
58
59
60 /*
61 * A data structure of the following type is kept for each scrollbar
62 * widget managed by this file:
63 */
64
65 typedef struct {
66 Tk_Window tkwin; /* Window that embodies the scrollbar. NULL
67 * means that the window has been destroyed
68 * but the data structures haven't yet been
69 * cleaned up.*/
70 Display *display; /* Display containing widget. Used, among
71 * other things, so that resources can be
72 * freed even after tkwin has gone away. */
73 Tcl_Interp *interp; /* Interpreter associated with scrollbar. */
74 Tcl_Command widgetCmd; /* Token for scrollbar's widget command. */
75 Tk_Uid orientUid; /* Orientation for window ("vertical" or
76 * "horizontal"). */
77 int vertical; /* Non-zero means vertical orientation
78 * requested, zero means horizontal. */
79 int width; /* Desired narrow dimension of scrollbar,
80 * in pixels. */
81 char *command; /* Command prefix to use when invoking
82 * scrolling commands. NULL means don't
83 * invoke commands. Malloc'ed. */
84 int commandSize; /* Number of non-NULL bytes in command. */
85 int repeatDelay; /* How long to wait before auto-repeating
86 * on scrolling actions (in ms). */
87 int repeatInterval; /* Interval between autorepeats (in ms). */
88 int jump; /* Value of -jump option. */
89
90 /*
91 * Information used when displaying widget:
92 */
93
94 int borderWidth; /* Width of 3-D borders. */
95 Tk_3DBorder bgBorder; /* Used for drawing background (all flat
96 * surfaces except for trough). */
97 Tk_3DBorder activeBorder; /* For drawing backgrounds when active (i.e.
98 * when mouse is positioned over element). */
99 XColor *troughColorPtr; /* Color for drawing trough. */
100 GC troughGC; /* For drawing trough. */
101 GC copyGC; /* Used for copying from pixmap onto screen. */
102 int relief; /* Indicates whether window as a whole is
103 * raised, sunken, or flat. */
104 int highlightWidth; /* Width in pixels of highlight to draw
105 * around widget when it has the focus.
106 * <= 0 means don't draw a highlight. */
107 XColor *highlightBgColorPtr;
108 /* Color for drawing traversal highlight
109 * area when highlight is off. */
110 XColor *highlightColorPtr; /* Color for drawing traversal highlight. */
111 int inset; /* Total width of all borders, including
112 * traversal highlight and 3-D border.
113 * Indicates how much interior stuff must
114 * be offset from outside edges to leave
115 * room for borders. */
116 int elementBorderWidth; /* Width of border to draw around elements
117 * inside scrollbar (arrows and slider).
118 * -1 means use borderWidth. */
119 int arrowLength; /* Length of arrows along long dimension of
120 * scrollbar, including space for a small gap
121 * between the arrow and the slider.
122 * Recomputed on window size changes. */
123 int sliderFirst; /* Pixel coordinate of top or left edge
124 * of slider area, including border. */
125 int sliderLast; /* Coordinate of pixel just after bottom
126 * or right edge of slider area, including
127 * border. */
128 int activeField; /* Names field to be displayed in active
129 * colors, such as TOP_ARROW, or 0 for
130 * no field. */
131 int activeRelief; /* Value of -activeRelief option: relief
132 * to use for active element. */
133
134 /*
135 * Information describing the application related to the scrollbar.
136 * This information is provided by the application by invoking the
137 * "set" widget command. This information can now be provided in
138 * two ways: the "old" form (totalUnits, windowUnits, firstUnit,
139 * and lastUnit), or the "new" form (firstFraction and lastFraction).
140 * FirstFraction and lastFraction will always be valid, but
141 * the old-style information is only valid if the NEW_STYLE_COMMANDS
142 * flag is 0.
143 */
144
145 int totalUnits; /* Total dimension of application, in
146 * units. Valid only if the NEW_STYLE_COMMANDS
147 * flag isn't set. */
148 int windowUnits; /* Maximum number of units that can be
149 * displayed in the window at once. Valid
150 * only if the NEW_STYLE_COMMANDS flag isn't
151 * set. */
152 int firstUnit; /* Number of last unit visible in
153 * application's window. Valid only if the
154 * NEW_STYLE_COMMANDS flag isn't set. */
155 int lastUnit; /* Index of last unit visible in window.
156 * Valid only if the NEW_STYLE_COMMANDS
157 * flag isn't set. */
158 double firstFraction; /* Position of first visible thing in window,
159 * specified as a fraction between 0 and
160 * 1.0. */
161 double lastFraction; /* Position of last visible thing in window,
162 * specified as a fraction between 0 and
163 * 1.0. */
164
165 /*
166 * Miscellaneous information:
167 */
168
169 Tk_Cursor cursor; /* Current cursor for window, or None. */
170 char *takeFocus; /* Value of -takefocus option; not used in
171 * the C code, but used by keyboard traversal
172 * scripts. Malloc'ed, but may be NULL. */
173 int flags; /* Various flags; see below for
174 * definitions. */
175 Blt_Tile tile, tile2, activeTile;
176 char *imagePtr;
177 Tk_Image image;
178 } Scrollbar;
179
180 /*
181 * Legal values for "activeField" field of Scrollbar structures. These
182 * are also the return values from the ScrollbarPosition procedure.
183 */
184
185 #define OUTSIDE 0
186 #define TOP_ARROW 1
187 #define TOP_GAP 2
188 #define SLIDER 3
189 #define BOTTOM_GAP 4
190 #define BOTTOM_ARROW 5
191
192 /*
193 * Flag bits for scrollbars:
194 *
195 * REDRAW_PENDING: Non-zero means a DoWhenIdle handler
196 * has already been queued to redraw
197 * this window.
198 * NEW_STYLE_COMMANDS: Non-zero means the new style of commands
199 * should be used to communicate with the
200 * widget: ".t yview scroll 2 lines", instead
201 * of ".t yview 40", for example.
202 * GOT_FOCUS: Non-zero means this window has the input
203 * focus.
204 */
205
206 #define REDRAW_PENDING 1
207 #define NEW_STYLE_COMMANDS 2
208 #define GOT_FOCUS 4
209
210 /*
211 * Minimum slider length, in pixels (designed to make sure that the slider
212 * is always easy to grab with the mouse).
213 */
214
215 #define MIN_SLIDER_LENGTH 8
216
217 /*
218 * Information used for argv parsing.
219 */
220
221 static Tk_ConfigSpec configSpecs[] =
222 {
223 {TK_CONFIG_BORDER, "-activebackground", "activeBackground", "Foreground",
224 DEF_SCROLLBAR_ACTIVE_BACKGROUND, Tk_Offset(Scrollbar, activeBorder),
225 TK_CONFIG_COLOR_ONLY},
226 {TK_CONFIG_BORDER, "-activebackground", "activeBackground", "Foreground",
227 DEF_SCROLLBAR_ACTIVE_BG_MONO, Tk_Offset(Scrollbar, activeBorder),
228 TK_CONFIG_MONO_ONLY},
229 {TK_CONFIG_RELIEF, "-activerelief", "activeRelief", "Relief",
230 DEF_SCROLLBAR_ACTIVE_RELIEF, Tk_Offset(Scrollbar, activeRelief), 0},
231 {TK_CONFIG_CUSTOM, "-activetile", "activeTile", "Tile",
232 (char *)NULL, Tk_Offset(Scrollbar, activeTile), TK_CONFIG_NULL_OK,
233 &bltTileOption},
234 {TK_CONFIG_BORDER, "-background", "background", "Background",
235 DEF_SCROLLBAR_BACKGROUND, Tk_Offset(Scrollbar, bgBorder),
236 TK_CONFIG_COLOR_ONLY},
237 {TK_CONFIG_BORDER, "-background", "background", "Background",
238 DEF_SCROLLBAR_BG_MONO, Tk_Offset(Scrollbar, bgBorder),
239 TK_CONFIG_MONO_ONLY},
240 {TK_CONFIG_SYNONYM, "-bd", "borderWidth", (char *)NULL,
241 (char *)NULL, 0, 0},
242 {TK_CONFIG_SYNONYM, "-bg", "background", (char *)NULL,
243 (char *)NULL, 0, 0},
244 {TK_CONFIG_PIXELS, "-borderwidth", "borderWidth", "BorderWidth",
245 DEF_SCROLLBAR_BORDERWIDTH, Tk_Offset(Scrollbar, borderWidth), 0},
246 {TK_CONFIG_STRING, "-command", "command", "Command",
247 DEF_SCROLLBAR_COMMAND, Tk_Offset(Scrollbar, command),
248 TK_CONFIG_NULL_OK},
249 {TK_CONFIG_ACTIVE_CURSOR, "-cursor", "cursor", "Cursor",
250 DEF_SCROLLBAR_CURSOR, Tk_Offset(Scrollbar, cursor), TK_CONFIG_NULL_OK},
251 {TK_CONFIG_PIXELS, "-elementborderwidth", "elementBorderWidth",
252 "BorderWidth", DEF_SCROLLBAR_EL_BORDERWIDTH,
253 Tk_Offset(Scrollbar, elementBorderWidth), 0},
254 {TK_CONFIG_COLOR, "-highlightbackground", "highlightBackground",
255 "HighlightBackground", DEF_SCROLLBAR_HIGHLIGHT_BG,
256 Tk_Offset(Scrollbar, highlightBgColorPtr), 0},
257 {TK_CONFIG_COLOR, "-highlightcolor", "highlightColor", "HighlightColor",
258 DEF_SCROLLBAR_HIGHLIGHT,
259 Tk_Offset(Scrollbar, highlightColorPtr), 0},
260 {TK_CONFIG_PIXELS, "-highlightthickness", "highlightThickness",
261 "HighlightThickness",
262 DEF_SCROLLBAR_HIGHLIGHT_WIDTH, Tk_Offset(Scrollbar, highlightWidth), 0},
263 {TK_CONFIG_STRING, "-image", "image", "Image",
264 NULL, Tk_Offset(TkScrollbar, imagePtr), 0},
265 {TK_CONFIG_BOOLEAN, "-jump", "jump", "Jump",
266 DEF_SCROLLBAR_JUMP, Tk_Offset(Scrollbar, jump), 0},
267 {TK_CONFIG_UID, "-orient", "orient", "Orient",
268 DEF_SCROLLBAR_ORIENT, Tk_Offset(Scrollbar, orientUid), 0},
269 {TK_CONFIG_RELIEF, "-relief", "relief", "Relief",
270 DEF_SCROLLBAR_RELIEF, Tk_Offset(Scrollbar, relief), 0},
271 {TK_CONFIG_INT, "-repeatdelay", "repeatDelay", "RepeatDelay",
272 DEF_SCROLLBAR_REPEAT_DELAY, Tk_Offset(Scrollbar, repeatDelay), 0},
273 {TK_CONFIG_INT, "-repeatinterval", "repeatInterval", "RepeatInterval",
274 DEF_SCROLLBAR_REPEAT_INTERVAL, Tk_Offset(Scrollbar, repeatInterval), 0},
275 {TK_CONFIG_STRING, "-takefocus", "takeFocus", "TakeFocus",
276 DEF_SCROLLBAR_TAKE_FOCUS, Tk_Offset(Scrollbar, takeFocus),
277 TK_CONFIG_NULL_OK},
278 {TK_CONFIG_CUSTOM, "-tile", "tile", "Tile",
279 (char *)NULL, Tk_Offset(Scrollbar, tile), TK_CONFIG_NULL_OK,
280 &bltTileOption},
281 {TK_CONFIG_CUSTOM, "-tile2", "tile2", "Tile",
282 (char *)NULL, Tk_Offset(Scrollbar, tile2), TK_CONFIG_NULL_OK,
283 &bltTileOption},
284 {TK_CONFIG_COLOR, "-troughcolor", "troughColor", "Background",
285 DEF_SCROLLBAR_TROUGH_COLOR, Tk_Offset(Scrollbar, troughColorPtr),
286 TK_CONFIG_COLOR_ONLY},
287 {TK_CONFIG_COLOR, "-troughcolor", "troughColor", "Background",
288 DEF_SCROLLBAR_TROUGH_MONO, Tk_Offset(Scrollbar, troughColorPtr),
289 TK_CONFIG_MONO_ONLY},
290 {TK_CONFIG_PIXELS, "-width", "width", "Width",
291 DEF_SCROLLBAR_WIDTH, Tk_Offset(Scrollbar, width), 0},
292 {TK_CONFIG_END, (char *)NULL, (char *)NULL, (char *)NULL,
293 (char *)NULL, 0, 0}
294 };
295
296 /*
297 * Forward declarations for procedures defined later in this file:
298 */
299
300 static void ComputeScrollbarGeometry _ANSI_ARGS_((
301 Scrollbar *scrollPtr));
302 static int ConfigureScrollbar _ANSI_ARGS_((Tcl_Interp *interp,
303 Scrollbar *scrollPtr, int argc, char **argv,
304 int flags));
305 static void DestroyScrollbar _ANSI_ARGS_((DestroyData *memPtr));
306 static void DisplayScrollbar _ANSI_ARGS_((ClientData clientData));
307 static void EventuallyRedraw _ANSI_ARGS_((Scrollbar *scrollPtr));
308 static void ScrollbarCmdDeletedProc _ANSI_ARGS_((
309 ClientData clientData));
310 static void ScrollbarEventProc _ANSI_ARGS_((ClientData clientData,
311 XEvent *eventPtr));
312 static int ScrollbarPosition _ANSI_ARGS_((Scrollbar *scrollPtr,
313 int x, int y));
314 static int ScrollbarWidgetCmd _ANSI_ARGS_((ClientData clientData,
315 Tcl_Interp *, int argc, char **argv));
316
317 /*
318 *--------------------------------------------------------------
319 *
320 * ScrollbarCmd --
321 *
322 * This procedure is invoked to process the "scrollbar" Tcl
323 * command. See the user documentation for details on what
324 * it does.
325 *
326 * Results:
327 * A standard Tcl result.
328 *
329 * Side effects:
330 * See the user documentation.
331 *
332 *--------------------------------------------------------------
333 */
334
335 static int
336 scrollbarCmd(clientData, interp, argc, argv, stype)
337 ClientData clientData; /* Main window associated with
338 * interpreter. */
339 Tcl_Interp *interp; /* Current interpreter. */
340 int argc; /* Number of arguments. */
341 char **argv; /* Argument strings. */
342 int stype;
343 {
344 register Scrollbar *scrollPtr;
345 Tk_Window tkwin;
346
347 if (argc < 2) {
348 Tcl_AppendResult(interp, "wrong # args: should be \"",
349 argv[0], " pathName ?options?\"", (char *)NULL);
350 return TCL_ERROR;
351 }
352 tkwin = Tk_CreateWindowFromPath(interp, Tk_MainWindow(interp), argv[1],
353 (char *)NULL);
354 if (tkwin == NULL) {
355 return TCL_ERROR;
356 }
357 /*
358 * Initialize fields that won't be initialized by ConfigureScrollbar,
359 * or which ConfigureScrollbar expects to have reasonable values
360 * (e.g. resource pointers).
361 */
362
363 scrollPtr = Blt_Calloc(1, sizeof(Scrollbar));
364 scrollPtr->tkwin = tkwin;
365 scrollPtr->display = Tk_Display(tkwin);
366 scrollPtr->interp = interp;
367 scrollPtr->widgetCmd = Tcl_CreateCommand(interp,
368 Tk_PathName(scrollPtr->tkwin), ScrollbarWidgetCmd,
369 (ClientData)scrollPtr, ScrollbarCmdDeletedProc);
370 #ifdef ITCL_NAMESPACES
371 Itk_SetWidgetCommand(scrollPtr->tkwin, scrollPtr->widgetCmd);
372 #endif /* ITCL_NAMESPACES */
373 scrollPtr->orientUid = NULL;
374 scrollPtr->vertical = 0;
375 scrollPtr->width = 0;
376 scrollPtr->command = NULL;
377 scrollPtr->commandSize = 0;
378 scrollPtr->repeatDelay = 0;
379 scrollPtr->repeatInterval = 0;
380 scrollPtr->borderWidth = 0;
381 scrollPtr->bgBorder = NULL;
382 scrollPtr->activeBorder = NULL;
383 scrollPtr->troughColorPtr = NULL;
384 scrollPtr->troughGC = None;
385 scrollPtr->copyGC = None;
386 scrollPtr->relief = TK_RELIEF_FLAT;
387 scrollPtr->highlightWidth = 0;
388 scrollPtr->highlightBgColorPtr = NULL;
389 scrollPtr->highlightColorPtr = NULL;
390 scrollPtr->inset = 0;
391 scrollPtr->elementBorderWidth = -1;
392 scrollPtr->arrowLength = 0;
393 scrollPtr->sliderFirst = 0;
394 scrollPtr->sliderLast = 0;
395 scrollPtr->activeField = 0;
396 scrollPtr->activeRelief = TK_RELIEF_RAISED;
397 scrollPtr->totalUnits = 0;
398 scrollPtr->windowUnits = 0;
399 scrollPtr->firstUnit = 0;
400 scrollPtr->lastUnit = 0;
401 scrollPtr->firstFraction = 0.0;
402 scrollPtr->lastFraction = 0.0;
403 scrollPtr->cursor = None;
404 scrollPtr->takeFocus = NULL;
405 scrollPtr->flags = 0;
406 scrollPtr->tile = scrollPtr->tile2 = scrollPtr->activeTile = NULL;
407
408 if (stype) {
409 Tk_SetClass(scrollPtr->tkwin, "BScrollbar");
410 } else {
411 Tk_SetClass(scrollPtr->tkwin, "Scrollbar");
412 }
413 Tk_CreateEventHandler(scrollPtr->tkwin,
414 ExposureMask | StructureNotifyMask | FocusChangeMask,
415 ScrollbarEventProc, (ClientData)scrollPtr);
416 if (ConfigureScrollbar(interp, scrollPtr, argc - 2, argv + 2, 0) != TCL_OK) {
417 goto error;
418 }
419 Tcl_SetResult(interp, Tk_PathName(scrollPtr->tkwin), TCL_VOLATILE);
420 return TCL_OK;
421
422 error:
423 Tk_DestroyWindow(scrollPtr->tkwin);
424 return TCL_ERROR;
425 }
426
427 static int
428 BScrollbarCmd(clientData, interp, argc, argv)
429 ClientData clientData; /* Main window associated with
430 * interpreter. */
431 Tcl_Interp *interp; /* Current interpreter. */
432 int argc; /* Number of arguments. */
433 char **argv; /* Argument strings. */
434 {
435 return scrollbarCmd(clientData, interp, argc, argv, 1);
436 }
437
438 static int
439 ScrollbarCmd(clientData, interp, argc, argv)
440 ClientData clientData; /* Main window associated with
441 * interpreter. */
442 Tcl_Interp *interp; /* Current interpreter. */
443 int argc; /* Number of arguments. */
444 char **argv; /* Argument strings. */
445 {
446 return scrollbarCmd(clientData, interp, argc, argv, 0);
447 }
448
449 /*
450 *--------------------------------------------------------------
451 *
452 * ScrollbarWidgetCmd --
453 *
454 * This procedure is invoked to process the Tcl command
455 * that corresponds to a widget managed by this module.
456 * See the user documentation for details on what it does.
457 *
458 * Results:
459 * A standard Tcl result.
460 *
461 * Side effects:
462 * See the user documentation.
463 *
464 *--------------------------------------------------------------
465 */
466
467 static int
468 ScrollbarWidgetCmd(clientData, interp, argc, argv)
469 ClientData clientData; /* Information about scrollbar
470 * widget. */
471 Tcl_Interp *interp; /* Current interpreter. */
472 int argc; /* Number of arguments. */
473 char **argv; /* Argument strings. */
474 {
475 register Scrollbar *scrollPtr = clientData;
476 char string[200];
477 int result = TCL_OK;
478 size_t length;
479 int c;
480
481 if (argc < 2) {
482 Tcl_AppendResult(interp, "wrong # args: should be \"",
483 argv[0], " option ?arg arg ...?\"", (char *)NULL);
484 return TCL_ERROR;
485 }
486 Tcl_Preserve((ClientData)scrollPtr);
487 c = argv[1][0];
488 length = strlen(argv[1]);
489 if ((c == 'a') && (strncmp(argv[1], "activate", length) == 0)) {
490 if (argc == 2) {
491 switch (scrollPtr->activeField) {
492 case TOP_ARROW:
493 Tcl_SetResult(interp, "arrow1", TCL_STATIC);
494 break;
495 case SLIDER:
496 Tcl_SetResult(interp, "slider", TCL_STATIC);
497 break;
498 case BOTTOM_ARROW:
499 Tcl_SetResult(interp, "arrow2", TCL_STATIC);
500 break;
501 }
502 goto done;
503 }
504 if (argc != 3) {
505 Tcl_AppendResult(interp, "wrong # args: should be \"",
506 argv[0], " activate element\"", (char *)NULL);
507 goto error;
508 }
509 c = argv[2][0];
510 length = strlen(argv[2]);
511 if ((c == 'a') && (strcmp(argv[2], "arrow1") == 0)) {
512 scrollPtr->activeField = TOP_ARROW;
513 } else if ((c == 'a') && (strcmp(argv[2], "arrow2") == 0)) {
514 scrollPtr->activeField = BOTTOM_ARROW;
515 } else if ((c == 's') && (strncmp(argv[2], "slider", length) == 0)) {
516 scrollPtr->activeField = SLIDER;
517 } else {
518 scrollPtr->activeField = OUTSIDE;
519 }
520 EventuallyRedraw(scrollPtr);
521 } else if ((c == 'c') && (strncmp(argv[1], "cget", length) == 0)
522 && (length >= 2)) {
523 if (argc != 3) {
524 Tcl_AppendResult(interp, "wrong # args: should be \"",
525 argv[0], " cget option\"",
526 (char *)NULL);
527 goto error;
528 }
529 result = Tk_ConfigureValue(interp, scrollPtr->tkwin, configSpecs,
530 (char *)scrollPtr, argv[2], 0);
531 } else if ((c == 'c') && (strncmp(argv[1], "configure", length) == 0)
532 && (length >= 2)) {
533 if (argc == 2) {
534 result = Tk_ConfigureInfo(interp, scrollPtr->tkwin, configSpecs,
535 (char *)scrollPtr, (char *)NULL, 0);
536 } else if (argc == 3) {
537 result = Tk_ConfigureInfo(interp, scrollPtr->tkwin, configSpecs,
538 (char *)scrollPtr, argv[2], 0);
539 } else {
540 result = ConfigureScrollbar(interp, scrollPtr, argc - 2, argv + 2,
541 TK_CONFIG_ARGV_ONLY);
542 }
543 } else if ((c == 'd') && (strncmp(argv[1], "delta", length) == 0)) {
544 int xDelta, yDelta, pixels, barWidth;
545 double fraction;
546
547 if (argc != 4) {
548 Tcl_AppendResult(interp, "wrong # args: should be \"",
549 argv[0], " delta xDelta yDelta\"", (char *)NULL);
550 goto error;
551 }
552 if ((Tcl_GetInt(interp, argv[2], &xDelta) != TCL_OK)
553 || (Tcl_GetInt(interp, argv[3], &yDelta) != TCL_OK)) {
554 goto error;
555 }
556 if (scrollPtr->vertical) {
557 pixels = yDelta;
558 barWidth = Tk_Height(scrollPtr->tkwin) - 1
559 - 2 * (scrollPtr->arrowLength + scrollPtr->inset);
560 } else {
561 pixels = xDelta;
562 barWidth = Tk_Width(scrollPtr->tkwin) - 1
563 - 2 * (scrollPtr->arrowLength + scrollPtr->inset);
564 }
565 if (barWidth == 0) {
566 fraction = 0.0;
567 } else {
568 fraction = ((double)pixels / (double)barWidth);
569 }
570 sprintf(interp->result, "%g", fraction);
571 } else if ((c == 'f') && (strncmp(argv[1], "fraction", length) == 0)) {
572 int x, y, pos, barWidth;
573 double fraction;
574
575 if (argc != 4) {
576 Tcl_AppendResult(interp, "wrong # args: should be \"",
577 argv[0], " fraction x y\"", (char *)NULL);
578 goto error;
579 }
580 if ((Tcl_GetInt(interp, argv[2], &x) != TCL_OK)
581 || (Tcl_GetInt(interp, argv[3], &y) != TCL_OK)) {
582 goto error;
583 }
584 if (scrollPtr->vertical) {
585 pos = y - (scrollPtr->arrowLength + scrollPtr->inset);
586 barWidth = Tk_Height(scrollPtr->tkwin) - 1
587 - 2 * (scrollPtr->arrowLength + scrollPtr->inset);
588 } else {
589 pos = x - (scrollPtr->arrowLength + scrollPtr->inset);
590 barWidth = Tk_Width(scrollPtr->tkwin) - 1
591 - 2 * (scrollPtr->arrowLength + scrollPtr->inset);
592 }
593 if (barWidth == 0) {
594 fraction = 0.0;
595 } else {
596 fraction = ((double)pos / (double)barWidth);
597 }
598 if (fraction < 0) {
599 fraction = 0;
600 } else if (fraction > 1.0) {
601 fraction = 1.0;
602 }
603 sprintf(string, "%g", fraction);
604 Tcl_SetResult(interp, string, TCL_VOLATILE);
605 } else if ((c == 'g') && (strncmp(argv[1], "get", length) == 0)) {
606 if (argc != 2) {
607 Tcl_AppendResult(interp, "wrong # args: should be \"",
608 argv[0], " get\"", (char *)NULL);
609 goto error;
610 }
611 if (scrollPtr->flags & NEW_STYLE_COMMANDS) {
612 char first[TCL_DOUBLE_SPACE], last[TCL_DOUBLE_SPACE];
613
614 Tcl_PrintDouble(interp, scrollPtr->firstFraction, first);
615 Tcl_PrintDouble(interp, scrollPtr->lastFraction, last);
616 Tcl_AppendResult(interp, first, " ", last, (char *)NULL);
617 } else {
618 sprintf(string, "%d %d %d %d", scrollPtr->totalUnits,
619 scrollPtr->windowUnits, scrollPtr->firstUnit,
620 scrollPtr->lastUnit);
621 Tcl_SetResult(interp, string, TCL_VOLATILE);
622 }
623 } else if ((c == 'i') && (strncmp(argv[1], "identify", length) == 0)) {
624 int x, y, thing;
625
626 if (argc != 4) {
627 Tcl_AppendResult(interp, "wrong # args: should be \"",
628 argv[0], " identify x y\"", (char *)NULL);
629 goto error;
630 }
631 if ((Tcl_GetInt(interp, argv[2], &x) != TCL_OK)
632 || (Tcl_GetInt(interp, argv[3], &y) != TCL_OK)) {
633 goto error;
634 }
635 thing = ScrollbarPosition(scrollPtr, x, y);
636 switch (thing) {
637 case TOP_ARROW:
638 Tcl_SetResult(interp, "arrow1", TCL_STATIC);
639 break;
640 case TOP_GAP:
641 Tcl_SetResult(interp, "trough1", TCL_STATIC);
642 break;
643 case SLIDER:
644 Tcl_SetResult(interp, "slider", TCL_STATIC);
645 break;
646 case BOTTOM_GAP:
647 Tcl_SetResult(interp, "trough2", TCL_STATIC);
648 break;
649 case BOTTOM_ARROW:
650 Tcl_SetResult(interp, "arrow2", TCL_STATIC);
651 break;
652 }
653 } else if ((c == 's') && (strncmp(argv[1], "set", length) == 0)) {
654 int totalUnits, windowUnits, firstUnit, lastUnit;
655
656 if (argc == 4) {
657 double first, last;
658
659 if (Tcl_GetDouble(interp, argv[2], &first) != TCL_OK) {
660 goto error;
661 }
662 if (Tcl_GetDouble(interp, argv[3], &last) != TCL_OK) {
663 goto error;
664 }
665 if (first < 0) {
666 scrollPtr->firstFraction = 0;
667 } else if (first > 1.0) {
668 scrollPtr->firstFraction = 1.0;
669 } else {
670 scrollPtr->firstFraction = first;
671 }
672 if (last < scrollPtr->firstFraction) {
673 scrollPtr->lastFraction = scrollPtr->firstFraction;
674 } else if (last > 1.0) {
675 scrollPtr->lastFraction = 1.0;
676 } else {
677 scrollPtr->lastFraction = last;
678 }
679 scrollPtr->flags |= NEW_STYLE_COMMANDS;
680 } else if (argc == 6) {
681 if (Tcl_GetInt(interp, argv[2], &totalUnits) != TCL_OK) {
682 goto error;
683 }
684 if (totalUnits < 0) {
685 totalUnits = 0;
686 }
687 if (Tcl_GetInt(interp, argv[3], &windowUnits) != TCL_OK) {
688 goto error;
689 }
690 if (windowUnits < 0) {
691 windowUnits = 0;
692 }
693 if (Tcl_GetInt(interp, argv[4], &firstUnit) != TCL_OK) {
694 goto error;
695 }
696 if (Tcl_GetInt(interp, argv[5], &lastUnit) != TCL_OK) {
697 goto error;
698 }
699 if (totalUnits > 0) {
700 if (lastUnit < firstUnit) {
701 lastUnit = firstUnit;
702 }
703 } else {
704 firstUnit = lastUnit = 0;
705 }
706 scrollPtr->totalUnits = totalUnits;
707 scrollPtr->windowUnits = windowUnits;
708 scrollPtr->firstUnit = firstUnit;
709 scrollPtr->lastUnit = lastUnit;
710 if (scrollPtr->totalUnits == 0) {
711 scrollPtr->firstFraction = 0.0;
712 scrollPtr->lastFraction = 1.0;
713 } else {
714 scrollPtr->firstFraction = ((double)firstUnit) / totalUnits;
715 scrollPtr->lastFraction = ((double)(lastUnit + 1)) / totalUnits;
716 }
717 scrollPtr->flags &= ~NEW_STYLE_COMMANDS;
718 } else {
719 Tcl_AppendResult(interp, "wrong # args: should be \"",
720 argv[0], " set firstFraction lastFraction\" or \"",
721 argv[0],
722 " set totalUnits windowUnits firstUnit lastUnit\"",
723 (char *)NULL);
724 goto error;
725 }
726 ComputeScrollbarGeometry(scrollPtr);
727 EventuallyRedraw(scrollPtr);
728 } else {
729 Tcl_AppendResult(interp, "bad option \"", argv[1],
730 "\": must be activate, cget, configure, delta, fraction, ",
731 "get, identify, or set", (char *)NULL);
732 goto error;
733 }
734 done:
735 Tcl_Release((ClientData)scrollPtr);
736 return result;
737
738 error:
739 Tcl_Release((ClientData)scrollPtr);
740 return TCL_ERROR;
741 }
742
743 /*
744 *----------------------------------------------------------------------
745 *
746 * DestroyScrollbar --
747 *
748 * This procedure is invoked by Tcl_EventuallyFree or Tcl_Release
749 * to clean up the internal structure of a scrollbar at a safe time
750 * (when no-one is using it anymore).
751 *
752 * Results:
753 * None.
754 *
755 * Side effects:
756 * Everything associated with the scrollbar is freed up.
757 *
758 *----------------------------------------------------------------------
759 */
760
761 static void
762 DestroyScrollbar(memPtr)
763 DestroyData *memPtr; /* Info about scrollbar widget. */
764 {
765 register Scrollbar *scrollPtr = (Scrollbar *)memPtr;
766
767 /*
768 * Free up all the stuff that requires special handling, then
769 * let Tk_FreeOptions handle all the standard option-related
770 * stuff.
771 */
772
773 if (scrollPtr->troughGC != None) {
774 Tk_FreeGC(scrollPtr->display, scrollPtr->troughGC);
775 }
776 if (scrollPtr->copyGC != None) {
777 Tk_FreeGC(scrollPtr->display, scrollPtr->copyGC);
778 }
779 if (scrollPtr->activeTile != NULL) {
780 Blt_FreeTile(scrollPtr->activeTile);
781 }
782 if (scrollPtr->tile != NULL) {
783 Blt_FreeTile(scrollPtr->tile);
784 }
785 if (scrollPtr->tile2 != NULL) {
786 Blt_FreeTile(scrollPtr->tile2);
787 }
788 if (scrollPtr->image != NULL) {
789 Tk_FreeImage(scrollPtr->image);
790 }
791 Tk_FreeOptions(configSpecs, (char *)scrollPtr, scrollPtr->display, 0);
792 Blt_Free(scrollPtr);
793 }
794
795 /*
796 *----------------------------------------------------------------------
797 *
798 * TileChangedProc
799 *
800 * Routine for tile change notifications.
801 *
802 * Results:
803 * None.
804 *
805 *----------------------------------------------------------------------
806 */
807 /*ARGSUSED*/
808 static void
809 TileChangedProc(clientData, tile)
810 ClientData clientData;
811 Blt_Tile tile; /* Not used. */
812 {
813 Scrollbar *scrollPtr = clientData;
814
815 if (scrollPtr->tkwin != NULL) {
816 EventuallyRedraw(scrollPtr);
817 }
818 }
819
820 /*
821 *----------------------------------------------------------------------
822 *
823 * ScrollbarImageProc --
824 *
825 * This function is invoked by the image code whenever the manager for an
826 * image does something that affects the size or contents of an image
827 * displayed in a button.
828 *
829 * Results:
830 * None.
831 *
832 * Side effects:
833 * Arranges for the button to get redisplayed.
834 *
835 *----------------------------------------------------------------------
836 */
837
838 static void
839 ScrollbarImageProc(
840 ClientData clientData, /* Pointer to widget record. */
841 int x, int y, /* Upper left pixel (within image) that must
842 * be redisplayed. */
843 int width, int height, /* Dimensions of area to redisplay (might be
844 * <= 0). */
845 int imgWidth, int imgHeight)/* New dimensions of image. */
846 {
847 register TkScrollbar *scrollPtr = (TkScrollbar *) clientData;
848
849 if (scrollPtr->tkwin != NULL) {
850 ComputeScrollbarGeometry(scrollPtr);
851 if (Tk_IsMapped(scrollPtr->tkwin) && !(scrollPtr->flags & REDRAW_PENDING)) {
852 Tcl_DoWhenIdle(TkpDisplayScrollbar, (ClientData) scrollPtr);
853 scrollPtr->flags |= REDRAW_PENDING;
854 }
855 }
856 }
857
858 /*
859 *----------------------------------------------------------------------
860 *
861 * ConfigureScrollbar --
862 *
863 * This procedure is called to process an argv/argc list, plus
864 * the Tk option database, in order to configure (or
865 * reconfigure) a scrollbar widget.
866 *
867 * Results:
868 * The return value is a standard Tcl result. If TCL_ERROR is
869 * returned, then interp->result contains an error message.
870 *
871 * Side effects:
872 * Configuration information, such as colors, border width,
873 * etc. get set for scrollPtr; old resources get freed,
874 * if there were any.
875 *
876 *----------------------------------------------------------------------
877 */
878
879 static int
880 ConfigureScrollbar(interp, scrollPtr, argc, argv, flags)
881 Tcl_Interp *interp; /* Used for error reporting. */
882 register Scrollbar *scrollPtr; /* Information about widget; may or
883 * may not already have values for
884 * some fields. */
885 int argc; /* Number of valid entries in argv. */
886 char **argv; /* Arguments. */
887 int flags; /* Flags to pass to
888 * Tk_ConfigureWidget. */
889 {
890 size_t length;
891 XGCValues gcValues;
892 GC new;
893 Tk_Image image;
894
895 if (Tk_ConfigureWidget(interp, scrollPtr->tkwin, configSpecs,
896 argc, argv, (char *)scrollPtr, flags) != TCL_OK) {
897 return TCL_ERROR;
898 }
899 /*
900 * A few options need special processing, such as parsing the
901 * orientation or setting the background from a 3-D border.
902 */
903
904 length = strlen(scrollPtr->orientUid);
905 if (strncmp(scrollPtr->orientUid, "vertical", length) == 0) {
906 scrollPtr->vertical = 1;
907 } else if (strncmp(scrollPtr->orientUid, "horizontal", length) == 0) {
908 scrollPtr->vertical = 0;
909 } else {
910 Tcl_AppendResult(interp, "bad orientation \"", scrollPtr->orientUid,
911 "\": must be vertical or horizontal", (char *)NULL);
912 return TCL_ERROR;
913 }
914
915 if (scrollPtr->command != NULL) {
916 scrollPtr->commandSize = strlen(scrollPtr->command);
917 } else {
918 scrollPtr->commandSize = 0;
919 }
920 /* Get the images for the widget, if there are any. Allocate the new
921 * images before freeing the old ones, so that the reference counts
922 * don't go to zero and cause image data to be discarded.
923 */
924
925 if (scrollPtr->imagePtr != NULL) {
926 image = Tk_GetImage(scrollPtr->interp, scrollPtr->tkwin,
927 scrollPtr->imagePtr, ScrollbarImageProc,
928 (ClientData) scrollPtr);
929 } else {
930 image = NULL;
931 }
932 if (scrollPtr->image != NULL) {
933 Tk_FreeImage(scrollPtr->image);
934 }
935 scrollPtr->image = image;
936
937
938 if (scrollPtr->activeTile != NULL) {
939 Blt_SetTileChangedProc(scrollPtr->activeTile, TileChangedProc,
940 (ClientData)scrollPtr);
941 }
942 if (scrollPtr->tile != NULL) {
943 Blt_SetTileChangedProc(scrollPtr->tile, TileChangedProc,
944 (ClientData)scrollPtr);
945 }
946 if (scrollPtr->tile2 != NULL) {
947 Blt_SetTileChangedProc(scrollPtr->tile2, TileChangedProc,
948 (ClientData)scrollPtr);
949 }
950 Tk_SetBackgroundFromBorder(scrollPtr->tkwin, scrollPtr->bgBorder);
951
952 gcValues.foreground = scrollPtr->troughColorPtr->pixel;
953 new = Tk_GetGC(scrollPtr->tkwin, GCForeground, &gcValues);
954 if (scrollPtr->troughGC != None) {
955 Tk_FreeGC(scrollPtr->display, scrollPtr->troughGC);
956 }
957 scrollPtr->troughGC = new;
958 if (scrollPtr->copyGC == None) {
959 gcValues.graphics_exposures = False;
960 scrollPtr->copyGC = Tk_GetGC(scrollPtr->tkwin, GCGraphicsExposures,
961 &gcValues);
962 }
963 /*
964 * Register the desired geometry for the window (leave enough space
965 * for the two arrows plus a minimum-size slider, plus border around
966 * the whole window, if any). Then arrange for the window to be
967 * redisplayed.
968 */
969
970 ComputeScrollbarGeometry(scrollPtr);
971 EventuallyRedraw(scrollPtr);
972 return TCL_OK;
973 }
974
975 /*
976 *--------------------------------------------------------------
977 *
978 * DisplayScrollbar --
979 *
980 * This procedure redraws the contents of a scrollbar window.
981 * It is invoked as a do-when-idle handler, so it only runs
982 * when there's nothing else for the application to do.
983 *
984 * Results:
985 * None.
986 *
987 * Side effects:
988 * Information appears on the screen.
989 *
990 *--------------------------------------------------------------
991 */
992
993 static void
994 DisplayScrollbar(clientData)
995 ClientData clientData; /* Information about window. */
996 {
997 register Scrollbar *scrollPtr = clientData;
998 register Tk_Window tkwin = scrollPtr->tkwin;
999 XPoint points[7];
1000 Tk_3DBorder border;
1001 int relief, width, elementBorderWidth;
1002 Pixmap pixmap;
1003 Blt_Tile tile;
1004
1005 if ((scrollPtr->tkwin == NULL) || !Tk_IsMapped(tkwin)) {
1006 goto done;
1007 }
1008 if (scrollPtr->vertical) {
1009 width = Tk_Width(tkwin) - 2 * scrollPtr->inset;
1010 } else {
1011 width = Tk_Height(tkwin) - 2 * scrollPtr->inset;
1012 }
1013 elementBorderWidth = scrollPtr->elementBorderWidth;
1014 if (elementBorderWidth < 0) {
1015 elementBorderWidth = scrollPtr->borderWidth;
1016 }
1017
1018 /*
1019 * In order to avoid screen flashes, this procedure redraws
1020 * the scrollbar in a pixmap, then copies the pixmap to the
1021 * screen in a single operation. This means that there's no
1022 * point in time where the on-sreen image has been cleared.
1023 */
1024
1025 pixmap = Tk_GetPixmap(scrollPtr->display, Tk_WindowId(tkwin),
1026 Tk_Width(tkwin), Tk_Height(tkwin), Tk_Depth(tkwin));
1027
1028 if (scrollPtr->highlightWidth != 0) {
1029 GC gc;
1030
1031 if (scrollPtr->flags & GOT_FOCUS) {
1032 gc = Tk_GCForColor(scrollPtr->highlightColorPtr, pixmap);
1033 } else {
1034 gc = Tk_GCForColor(scrollPtr->highlightBgColorPtr, pixmap);
1035 }
1036 Tk_DrawFocusHighlight(tkwin, gc, scrollPtr->highlightWidth, pixmap);
1037 }
1038 Blt_Draw3DRectangle(tkwin, pixmap, scrollPtr->bgBorder,
1039 scrollPtr->highlightWidth, scrollPtr->highlightWidth,
1040 Tk_Width(tkwin) - 2 * scrollPtr->highlightWidth,
1041 Tk_Height(tkwin) - 2 * scrollPtr->highlightWidth,
1042 scrollPtr->borderWidth, scrollPtr->relief);
1043
1044 if (Blt_HasTile(scrollPtr->tile)) {
1045 Blt_SetTileOrigin(tkwin, scrollPtr->tile, 0, 0);
1046 Blt_TileRectangle(tkwin, pixmap, scrollPtr->tile, scrollPtr->inset,
1047 scrollPtr->inset,
1048 (unsigned)(Tk_Width(tkwin) - 2 * scrollPtr->inset),
1049 (unsigned)(Tk_Height(tkwin) - 2 * scrollPtr->inset));
1050 } else {
1051 XFillRectangle(scrollPtr->display, pixmap, scrollPtr->troughGC,
1052 scrollPtr->inset, scrollPtr->inset,
1053 (unsigned)(Tk_Width(tkwin) - 2 * scrollPtr->inset),
1054 (unsigned)(Tk_Height(tkwin) - 2 * scrollPtr->inset));
1055 }
1056
1057 /*
1058 * Draw the top or left arrow. The coordinates of the polygon
1059 * points probably seem odd, but they were carefully chosen with
1060 * respect to X's rules for filling polygons. These point choices
1061 * cause the arrows to just fill the narrow dimension of the
1062 * scrollbar and be properly centered.
1063 */
1064 tile = NULL;
1065 if (scrollPtr->activeField == TOP_ARROW) {
1066 border = scrollPtr->activeBorder;
1067 relief = scrollPtr->activeField == TOP_ARROW ? scrollPtr->activeRelief
1068 : TK_RELIEF_RAISED;
1069 if (scrollPtr->activeTile != NULL) {
1070 Blt_SetTileOrigin(tkwin, scrollPtr->activeTile, 0, 0);
1071 tile = scrollPtr->activeTile;
1072 }
1073 } else {
1074 border = scrollPtr->bgBorder;
1075 relief = TK_RELIEF_RAISED;
1076 if (scrollPtr->tile != NULL) {
1077 tile = scrollPtr->tile;
1078 }
1079 }
1080 if (scrollPtr->vertical) {
1081 points[0].x = scrollPtr->inset - 1;
1082 points[0].y = scrollPtr->arrowLength + scrollPtr->inset - 1;
1083 points[1].x = width + scrollPtr->inset;
1084 points[1].y = points[0].y;
1085 points[2].x = width / 2 + scrollPtr->inset;
1086 points[2].y = scrollPtr->inset - 1;
1087 } else {
1088 points[0].x = scrollPtr->arrowLength + scrollPtr->inset - 1;
1089 points[0].y = scrollPtr->inset - 1;
1090 points[1].x = scrollPtr->inset;
1091 points[1].y = width / 2 + scrollPtr->inset;
1092 points[2].x = points[0].x;
1093 points[2].y = width + scrollPtr->inset;
1094 }
1095 if (Blt_HasTile(tile)) {
1096 Blt_TilePolygon(tkwin, pixmap, tile, points, 3);
1097 Tk_Draw3DPolygon(tkwin, pixmap, border, points, 3,
1098 scrollPtr->borderWidth, relief);
1099 } else {
1100 Tk_Fill3DPolygon(tkwin, pixmap, border, points, 3,
1101 scrollPtr->borderWidth, relief);
1102 }
1103
1104 /*
1105 * Display the bottom or right arrow.
1106 */
1107 tile = NULL;
1108 if (scrollPtr->activeField == BOTTOM_ARROW) {
1109 border = scrollPtr->activeBorder;
1110 relief = scrollPtr->activeField == BOTTOM_ARROW
1111 ? scrollPtr->activeRelief : TK_RELIEF_RAISED;
1112 if (scrollPtr->activeTile != NULL) {
1113 Blt_SetTileOrigin(tkwin, scrollPtr->activeTile, 0, 0);
1114 tile = scrollPtr->activeTile;
1115 }
1116 } else {
1117 border = scrollPtr->bgBorder;
1118 relief = TK_RELIEF_RAISED;
1119 if (scrollPtr->tile != NULL) {
1120 tile = scrollPtr->tile;
1121 }
1122 }
1123 if (scrollPtr->vertical) {
1124 points[0].x = scrollPtr->inset;
1125 points[0].y = Tk_Height(tkwin) - scrollPtr->arrowLength
1126 - scrollPtr->inset + 1;
1127 points[1].x = width / 2 + scrollPtr->inset;
1128 points[1].y = Tk_Height(tkwin) - scrollPtr->inset;
1129 points[2].x = width + scrollPtr->inset;
1130 points[2].y = points[0].y;
1131 } else {
1132 points[0].x = Tk_Width(tkwin) - scrollPtr->arrowLength
1133 - scrollPtr->inset + 1;
1134 points[0].y = scrollPtr->inset - 1;
1135 points[1].x = points[0].x;
1136 points[1].y = width + scrollPtr->inset;
1137 points[2].x = Tk_Width(tkwin) - scrollPtr->inset;
1138 points[2].y = width / 2 + scrollPtr->inset;
1139 }
1140 if (Blt_HasTile(tile)) {
1141 Blt_TilePolygon(tkwin, pixmap, tile, points, 3);
1142 Tk_Draw3DPolygon(tkwin, pixmap, border, points, 3,
1143 scrollPtr->borderWidth, relief);
1144 } else {
1145 Tk_Fill3DPolygon(tkwin, pixmap, border, points, 3,
1146 scrollPtr->borderWidth, relief);
1147 }
1148
1149 /*
1150 * Display the slider.
1151 */
1152 if (scrollPtr->activeField == SLIDER) {
1153 border = scrollPtr->activeBorder;
1154 relief = scrollPtr->activeField == SLIDER ? scrollPtr->activeRelief
1155 : TK_RELIEF_RAISED;
1156 tile = scrollPtr->activeTile;
1157 } else {
1158 border = scrollPtr->bgBorder;
1159 relief = TK_RELIEF_RAISED;
1160 tile = (scrollPtr->tile2 ? scrollPtr->tile2 : scrollPtr->tile);
1161 }
1162 if (scrollPtr->vertical) {
1163 if (Blt_HasTile(tile)) {
1164 Blt_TileRectangle(tkwin, pixmap, tile, scrollPtr->inset,
1165 scrollPtr->sliderFirst, width - 1,
1166 scrollPtr->sliderLast - scrollPtr->sliderFirst - 1);
1167 Blt_Draw3DRectangle(tkwin, pixmap, border,
1168 scrollPtr->inset, scrollPtr->sliderFirst,
1169 width, scrollPtr->sliderLast - scrollPtr->sliderFirst,
1170 scrollPtr->borderWidth, relief);
1171 } else {
1172 Blt_Fill3DRectangle(tkwin, pixmap, border,
1173 scrollPtr->inset, scrollPtr->sliderFirst,
1174 width, scrollPtr->sliderLast - scrollPtr->sliderFirst,
1175 elementBorderWidth, relief);
1176 }
1177 } else {
1178 if (Blt_HasTile(tile)) {
1179 Blt_TileRectangle(tkwin, pixmap, tile, scrollPtr->sliderFirst,
1180 scrollPtr->inset,
1181 scrollPtr->sliderLast - scrollPtr->sliderFirst - 1, width - 1);
1182 Blt_Draw3DRectangle(tkwin, pixmap, border,
1183 scrollPtr->sliderFirst, scrollPtr->inset,
1184 scrollPtr->sliderLast - scrollPtr->sliderFirst, width,
1185 scrollPtr->borderWidth, relief);
1186 } else {
1187 Blt_Fill3DRectangle(tkwin, pixmap, border,
1188 scrollPtr->sliderFirst, scrollPtr->inset,
1189 scrollPtr->sliderLast - scrollPtr->sliderFirst, width,
1190 scrollPtr->borderWidth, relief);
1191 }
1192 }
1193
1194 if (scrollPtr->image != NULL) {
1195 /* Draw adornment image centered on bar. */
1196 int imgW, imgH, xLen, yLen, yStart, xStart, yOfs = 0, xOfs = 0, pad = 1;
1197
1198 Tk_SizeOfImage(scrollPtr->image, &imgW, &imgH);
1199
1200 if (scrollPtr->vertical) {
1201 xLen = width;
1202 yLen = scrollPtr->sliderLast - scrollPtr->sliderFirst;
1203 } else {
1204 yLen = width;
1205 xLen = scrollPtr->sliderLast - scrollPtr->sliderFirst;
1206
1207 }
1208 if (imgW > (xLen-pad*2)) {
1209 int oWid = imgW;
1210 imgW = (xLen-pad*2);
1211 xOfs = (oWid-imgW)/2;
1212 }
1213 if (imgH > (yLen-pad*2)) {
1214 int oWid = imgH;
1215 imgH = (yLen-pad*2);
1216 yOfs = (oWid-imgH)/2;
1217 }
1218 xStart = (xLen - imgW)/2+pad;
1219 yStart = (yLen - imgH)/2+pad;
1220 if (scrollPtr->vertical) {
1221 yStart += scrollPtr->sliderFirst;
1222 } else {
1223 xStart += scrollPtr->sliderFirst;
1224 }
1225 if (xStart>=0 && yStart>=0 && imgW > 0 && imgH > 0) {
1226 Tk_RedrawImage(scrollPtr->image, xOfs, yOfs, imgW, imgH, pixmap, xStart, yStart);
1227 }
1228 }
1229
1230 /*
1231 * Copy the information from the off-screen pixmap onto the screen,
1232 * then delete the pixmap.
1233 */
1234
1235 XCopyArea(scrollPtr->display, pixmap, Tk_WindowId(tkwin),
1236 scrollPtr->copyGC, 0, 0, (unsigned)Tk_Width(tkwin),
1237 (unsigned)Tk_Height(tkwin), 0, 0);
1238 Tk_FreePixmap(scrollPtr->display, pixmap);
1239
1240 done:
1241 scrollPtr->flags &= ~REDRAW_PENDING;
1242 }
1243
1244 /*
1245 *--------------------------------------------------------------
1246 *
1247 * ScrollbarEventProc --
1248 *
1249 * This procedure is invoked by the Tk dispatcher for various
1250 * events on scrollbars.
1251 *
1252 * Results:
1253 * None.
1254 *
1255 * Side effects:
1256 * When the window gets deleted, internal structures get
1257 * cleaned up. When it gets exposed, it is redisplayed.
1258 *
1259 *--------------------------------------------------------------
1260 */
1261
1262 static void
1263 ScrollbarEventProc(clientData, eventPtr)
1264 ClientData clientData; /* Information about window. */
1265 XEvent *eventPtr; /* Information about event. */
1266 {
1267 Scrollbar *scrollPtr = clientData;
1268
1269 if ((eventPtr->type == Expose) && (eventPtr->xexpose.count == 0)) {
1270 EventuallyRedraw(scrollPtr);
1271 } else if (eventPtr->type == DestroyNotify) {
1272 if (scrollPtr->tkwin != NULL) {
1273 scrollPtr->tkwin = NULL;
1274 Tcl_DeleteCommandFromToken(scrollPtr->interp,scrollPtr->widgetCmd);
1275 }
1276 if (scrollPtr->flags & REDRAW_PENDING) {
1277 Tcl_CancelIdleCall(DisplayScrollbar, (ClientData)scrollPtr);
1278 }
1279 Tcl_EventuallyFree((ClientData)scrollPtr,
1280 (Tcl_FreeProc *)DestroyScrollbar);
1281 } else if (eventPtr->type == ConfigureNotify) {
1282 ComputeScrollbarGeometry(scrollPtr);
1283 EventuallyRedraw(scrollPtr);
1284 } else if (eventPtr->type == FocusIn) {
1285 if (eventPtr->xfocus.detail != NotifyInferior) {
1286 scrollPtr->flags |= GOT_FOCUS;
1287 if (scrollPtr->highlightWidth > 0) {
1288 EventuallyRedraw(scrollPtr);
1289 }
1290 }
1291 } else if (eventPtr->type == FocusOut) {
1292 if (eventPtr->xfocus.detail != NotifyInferior) {
1293 scrollPtr->flags &= ~GOT_FOCUS;
1294 if (scrollPtr->highlightWidth > 0) {
1295 EventuallyRedraw(scrollPtr);
1296 }
1297 }
1298 }
1299 }
1300
1301 /*
1302 *----------------------------------------------------------------------
1303 *
1304 * ScrollbarCmdDeletedProc --
1305 *
1306 * This procedure is invoked when a widget command is deleted. If
1307 * the widget isn't already in the process of being destroyed,
1308 * this command destroys it.
1309 *
1310 * Results:
1311 * None.
1312 *
1313 * Side effects:
1314 * The widget is destroyed.
1315 *
1316 *----------------------------------------------------------------------
1317 */
1318
1319 static void
1320 ScrollbarCmdDeletedProc(clientData)
1321 ClientData clientData; /* Pointer to widget record for widget. */
1322 {
1323 Scrollbar *scrollPtr = clientData;
1324 Tk_Window tkwin = scrollPtr->tkwin;
1325
1326 /*
1327 * This procedure could be invoked either because the window was
1328 * destroyed and the command was then deleted (in which case tkwin
1329 * is NULL) or because the command was deleted, and then this procedure
1330 * destroys the widget.
1331 */
1332
1333 if (tkwin != NULL) {
1334 #ifdef ITCL_NAMESPACES
1335 Itk_SetWidgetCommand(scrollPtr->tkwin, (Tcl_Command) NULL);
1336 #endif
1337 scrollPtr->tkwin = NULL;
1338 Tk_DestroyWindow(tkwin);
1339 }
1340 }
1341
1342 /*
1343 *----------------------------------------------------------------------
1344 *
1345 * ComputeScrollbarGeometry --
1346 *
1347 * After changes in a scrollbar's size or configuration, this
1348 * procedure recomputes various geometry information used in
1349 * displaying the scrollbar.
1350 *
1351 * Results:
1352 * None.
1353 *
1354 * Side effects:
1355 * The scrollbar will be displayed differently.
1356 *
1357 *----------------------------------------------------------------------
1358 */
1359
1360 static void
1361 ComputeScrollbarGeometry(scrollPtr)
1362 register Scrollbar *scrollPtr; /* Scrollbar whose geometry may
1363 * have changed. */
1364 {
1365 int width, fieldLength;
1366
1367 if (scrollPtr->highlightWidth < 0) {
1368 scrollPtr->highlightWidth = 0;
1369 }
1370 scrollPtr->inset = scrollPtr->highlightWidth + scrollPtr->borderWidth;
1371 width = (scrollPtr->vertical) ? Tk_Width(scrollPtr->tkwin)
1372 : Tk_Height(scrollPtr->tkwin);
1373 scrollPtr->arrowLength = width - 2 * scrollPtr->inset + 1;
1374 fieldLength = (scrollPtr->vertical ? Tk_Height(scrollPtr->tkwin)
1375 : Tk_Width(scrollPtr->tkwin))
1376 - 2 * (scrollPtr->arrowLength + scrollPtr->inset);
1377 if (fieldLength < 0) {
1378 fieldLength = 0;
1379 }
1380 scrollPtr->sliderFirst = fieldLength * scrollPtr->firstFraction;
1381 scrollPtr->sliderLast = fieldLength * scrollPtr->lastFraction;
1382
1383 /*
1384 * Adjust the slider so that some piece of it is always
1385 * displayed in the scrollbar and so that it has at least
1386 * a minimal width (so it can be grabbed with the mouse).
1387 */
1388
1389 if (scrollPtr->sliderFirst > (fieldLength - 2 * scrollPtr->borderWidth)) {
1390 scrollPtr->sliderFirst = fieldLength - 2 * scrollPtr->borderWidth;
1391 }
1392 if (scrollPtr->sliderFirst < 0) {
1393 scrollPtr->sliderFirst = 0;
1394 }
1395 if (scrollPtr->sliderLast < (scrollPtr->sliderFirst + MIN_SLIDER_LENGTH)) {
1396 scrollPtr->sliderLast = scrollPtr->sliderFirst + MIN_SLIDER_LENGTH;
1397 }
1398 if (scrollPtr->sliderLast > fieldLength) {
1399 scrollPtr->sliderLast = fieldLength;
1400 }
1401 scrollPtr->sliderFirst += scrollPtr->arrowLength + scrollPtr->inset;
1402 scrollPtr->sliderLast += scrollPtr->arrowLength + scrollPtr->inset;
1403
1404 /*
1405 * Register the desired geometry for the window (leave enough space
1406 * for the two arrows plus a minimum-size slider, plus border around
1407 * the whole window, if any). Then arrange for the window to be
1408 * redisplayed.
1409 */
1410
1411 if (scrollPtr->vertical) {
1412 Tk_GeometryRequest(scrollPtr->tkwin,
1413 scrollPtr->width + 2 * scrollPtr->inset,
1414 2 * (scrollPtr->arrowLength + scrollPtr->borderWidth
1415 + scrollPtr->inset));
1416 } else {
1417 Tk_GeometryRequest(scrollPtr->tkwin,
1418 2 * (scrollPtr->arrowLength + scrollPtr->borderWidth
1419 + scrollPtr->inset), scrollPtr->width + 2 * scrollPtr->inset);
1420 }
1421 Tk_SetInternalBorder(scrollPtr->tkwin, scrollPtr->inset);
1422 }
1423
1424 /*
1425 *--------------------------------------------------------------
1426 *
1427 * ScrollbarPosition --
1428 *
1429 * Determine the scrollbar element corresponding to a
1430 * given position.
1431 *
1432 * Results:
1433 * One of TOP_ARROW, TOP_GAP, etc., indicating which element
1434 * of the scrollbar covers the position given by (x, y). If
1435 * (x,y) is outside the scrollbar entirely, then OUTSIDE is
1436 * returned.
1437 *
1438 * Side effects:
1439 * None.
1440 *
1441 *--------------------------------------------------------------
1442 */
1443 static int
1444 ScrollbarPosition(scrollPtr, x, y)
1445 register Scrollbar *scrollPtr; /* Scrollbar widget record. */
1446 int x, y; /* Coordinates within scrollPtr's
1447 * window. */
1448 {
1449 int length, width, tmp;
1450
1451 if (scrollPtr->vertical) {
1452 length = Tk_Height(scrollPtr->tkwin);
1453 width = Tk_Width(scrollPtr->tkwin);
1454 } else {
1455 tmp = x;
1456 x = y;
1457 y = tmp;
1458 length = Tk_Width(scrollPtr->tkwin);
1459 width = Tk_Height(scrollPtr->tkwin);
1460 }
1461
1462 if ((x < scrollPtr->inset) || (x >= (width - scrollPtr->inset))
1463 || (y < scrollPtr->inset) || (y >= (length - scrollPtr->inset))) {
1464 return OUTSIDE;
1465 }
1466 /*
1467 * All of the calculations in this procedure mirror those in
1468 * DisplayScrollbar. Be sure to keep the two consistent.
1469 */
1470
1471 if (y < (scrollPtr->inset + scrollPtr->arrowLength)) {
1472 return TOP_ARROW;
1473 }
1474 if (y < scrollPtr->sliderFirst) {
1475 return TOP_GAP;
1476 }
1477 if (y < scrollPtr->sliderLast) {
1478 return SLIDER;
1479 }
1480 if (y >= (length - (scrollPtr->arrowLength + scrollPtr->inset))) {
1481 return BOTTOM_ARROW;
1482 }
1483 return BOTTOM_GAP;
1484 }
1485
1486 /*
1487 *--------------------------------------------------------------
1488 *
1489 * EventuallyRedraw --
1490 *
1491 * Arrange for one or more of the fields of a scrollbar
1492 * to be redrawn.
1493 *
1494 * Results:
1495 * None.
1496 *
1497 * Side effects:
1498 * None.
1499 *
1500 *--------------------------------------------------------------
1501 */
1502
1503 static void
1504 EventuallyRedraw(scrollPtr)
1505 register Scrollbar *scrollPtr; /* Information about widget. */
1506 {
1507 if ((scrollPtr->tkwin == NULL) || (!Tk_IsMapped(scrollPtr->tkwin))) {
1508 return;
1509 }
1510 if ((scrollPtr->flags & REDRAW_PENDING) == 0) {
1511 Tcl_DoWhenIdle(DisplayScrollbar, (ClientData)scrollPtr);
1512 scrollPtr->flags |= REDRAW_PENDING;
1513 }
1514 }
1515
1516 int
1517 Blt_ScrollbarInit(interp)
1518 Tcl_Interp *interp;
1519 {
1520 static Blt_CmdSpec cmdSpecs[2] =
1521 {
1522 { "scrollbar", ScrollbarCmd, },
1523 { "bscrollbar", BScrollbarCmd, },
1524 };
1525
1526 return Blt_InitCmds(interp, "blt::tile", cmdSpecs, 2);
1527 }
1528
1529 #endif /* NO_TILESCROLLBAR */