Debian Codegraph @

pTk/mTk/generic/tkScrollbar.c

raw symbols SCIP
1 /*
2 * tkScrollbar.c --
3 *
4 * This module implements a scrollbar widgets for the Tk
5 * toolkit. A scrollbar displays a slider and two arrows;
6 * mouse clicks on features within the scrollbar cause
7 * scrolling commands to be invoked.
8 *
9 * Copyright (c) 1990-1994 The Regents of the University of California.
10 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
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 * RCS: @(#) $Id: tkScrollbar.c,v 1.6 2002/08/05 04:30:40 dgp Exp $
16 */
17
18 #include "tkPort.h"
19 #include "tkScrollbar.h"
20 #include "default.h"
21 #include "tkInt.h"
22 #include "tkVMacro.h"
23
24 /*
25 * Custom option for handling "-orient"
26 */
27
28 static Tk_CustomOption orientOption = {
29 Tk_OrientParseProc,
30 Tk_OrientPrintProc,
31 (ClientData) NULL
32 };
33
34 static Tk_CustomOption tileOption = {
35 Tk_TileParseProc,
36 Tk_TilePrintProc,
37 (ClientData) NULL
38 };
39
40 static Tk_CustomOption offsetOption = {
41 Tk_OffsetParseProc,
42 Tk_OffsetPrintProc,
43 (ClientData) NULL
44 };
45
46 /*
47 * Information used for argv parsing.
48 */
49
50 Tk_ConfigSpec tkpScrollbarConfigSpecs[] = {
51 {TK_CONFIG_BORDER, "-activebackground", "activeBackground", "Foreground",
52 DEF_SCROLLBAR_ACTIVE_BG_COLOR, Tk_Offset(TkScrollbar, activeBorder),
53 TK_CONFIG_COLOR_ONLY},
54 {TK_CONFIG_BORDER, "-activebackground", "activeBackground", "Foreground",
55 DEF_SCROLLBAR_ACTIVE_BG_MONO, Tk_Offset(TkScrollbar, activeBorder),
56 TK_CONFIG_MONO_ONLY},
57 {TK_CONFIG_RELIEF, "-activerelief", "activeRelief", "Relief",
58 DEF_SCROLLBAR_ACTIVE_RELIEF, Tk_Offset(TkScrollbar, activeRelief), 0},
59 {TK_CONFIG_BORDER, "-background", "background", "Background",
60 DEF_SCROLLBAR_BG_COLOR, Tk_Offset(TkScrollbar, bgBorder),
61 TK_CONFIG_COLOR_ONLY},
62 {TK_CONFIG_BORDER, "-background", "background", "Background",
63 DEF_SCROLLBAR_BG_MONO, Tk_Offset(TkScrollbar, bgBorder),
64 TK_CONFIG_MONO_ONLY},
65 {TK_CONFIG_SYNONYM, "-bd", "borderWidth", (char *) NULL,
66 (char *) NULL, 0, 0},
67 {TK_CONFIG_SYNONYM, "-bg", "background", (char *) NULL,
68 (char *) NULL, 0, 0},
69 {TK_CONFIG_PIXELS, "-borderwidth", "borderWidth", "BorderWidth",
70 DEF_SCROLLBAR_BORDER_WIDTH, Tk_Offset(TkScrollbar, borderWidth), 0},
71 {TK_CONFIG_CALLBACK, "-command", "command", "Command",
72 DEF_SCROLLBAR_COMMAND, Tk_Offset(TkScrollbar, command),
73 TK_CONFIG_NULL_OK},
74 {TK_CONFIG_ACTIVE_CURSOR, "-cursor", "cursor", "Cursor",
75 DEF_SCROLLBAR_CURSOR, Tk_Offset(TkScrollbar, cursor), TK_CONFIG_NULL_OK},
76 {TK_CONFIG_PIXELS, "-elementborderwidth", "elementBorderWidth",
77 "BorderWidth", DEF_SCROLLBAR_EL_BORDER_WIDTH,
78 Tk_Offset(TkScrollbar, elementBorderWidth), 0},
79 {TK_CONFIG_COLOR, "-highlightbackground", "highlightBackground",
80 "HighlightBackground", DEF_SCROLLBAR_HIGHLIGHT_BG,
81 Tk_Offset(TkScrollbar, highlightBgColorPtr), 0},
82 {TK_CONFIG_COLOR, "-highlightcolor", "highlightColor", "HighlightColor",
83 DEF_SCROLLBAR_HIGHLIGHT,
84 Tk_Offset(TkScrollbar, highlightColorPtr), 0},
85 {TK_CONFIG_PIXELS, "-highlightthickness", "highlightThickness",
86 "HighlightThickness",
87 DEF_SCROLLBAR_HIGHLIGHT_WIDTH, Tk_Offset(TkScrollbar, highlightWidth), 0},
88 {TK_CONFIG_BOOLEAN, "-jump", "jump", "Jump",
89 DEF_SCROLLBAR_JUMP, Tk_Offset(TkScrollbar, jump), 0},
90 {TK_CONFIG_CUSTOM, "-orient", "orient", "Orient",
91 DEF_SCROLLBAR_ORIENT, Tk_Offset(TkScrollbar, vertical), 0,
92 &orientOption},
93 {TK_CONFIG_RELIEF, "-relief", "relief", "Relief",
94 DEF_SCROLLBAR_RELIEF, Tk_Offset(TkScrollbar, relief), 0},
95 {TK_CONFIG_INT, "-repeatdelay", "repeatDelay", "RepeatDelay",
96 DEF_SCROLLBAR_REPEAT_DELAY, Tk_Offset(TkScrollbar, repeatDelay), 0},
97 {TK_CONFIG_INT, "-repeatinterval", "repeatInterval", "RepeatInterval",
98 DEF_SCROLLBAR_REPEAT_INTERVAL, Tk_Offset(TkScrollbar, repeatInterval), 0},
99 {TK_CONFIG_STRING, "-takefocus", "takeFocus", "TakeFocus",
100 DEF_SCROLLBAR_TAKE_FOCUS, Tk_Offset(TkScrollbar, takeFocus),
101 TK_CONFIG_NULL_OK},
102 #ifdef NOT_YET
103 {TK_CONFIG_CUSTOM, "-tile", "tile", "Tile", (char *) NULL,
104 Tk_Offset(TkScrollbar, tile), TK_CONFIG_DONT_SET_DEFAULT,
105 &tileOption},
106 {TK_CONFIG_CUSTOM, "-offset", "offset", "Offset", "0 0",
107 Tk_Offset(TkScrollbar, tsoffset), TK_CONFIG_DONT_SET_DEFAULT,
108 &offsetOption},
109 #endif
110 {TK_CONFIG_COLOR, "-troughcolor", "troughColor", "Background",
111 DEF_SCROLLBAR_TROUGH_COLOR, Tk_Offset(TkScrollbar, troughColorPtr),
112 TK_CONFIG_COLOR_ONLY},
113 {TK_CONFIG_COLOR, "-troughcolor", "troughColor", "Background",
114 DEF_SCROLLBAR_TROUGH_MONO, Tk_Offset(TkScrollbar, troughColorPtr),
115 TK_CONFIG_MONO_ONLY},
116 {TK_CONFIG_PIXELS, "-width", "width", "Width",
117 DEF_SCROLLBAR_WIDTH, Tk_Offset(TkScrollbar, width), 0},
118 {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
119 (char *) NULL, 0, 0}
120 };
121
122 /*
123 * Forward declarations for procedures defined later in this file:
124 */
125
126 static int ConfigureScrollbar _ANSI_ARGS_((Tcl_Interp *interp,
127 TkScrollbar *scrollPtr, int argc, CONST char **argv,
128 int flags));
129 static void ScrollbarCmdDeletedProc _ANSI_ARGS_((
130 ClientData clientData));
131 static int ScrollbarWidgetCmd _ANSI_ARGS_((ClientData clientData,
132 Tcl_Interp *, int argc, CONST char **argv));
133
134 /*
135 *--------------------------------------------------------------
136 *
137 * Tk_ScrollbarCmd --
138 *
139 * This procedure is invoked to process the "scrollbar" Tcl
140 * command. See the user documentation for details on what
141 * it does.
142 *
143 * Results:
144 * A standard Tcl result.
145 *
146 * Side effects:
147 * See the user documentation.
148 *
149 *--------------------------------------------------------------
150 */
151
152 int
153 Tk_ScrollbarCmd(clientData, interp, argc, argv)
154 ClientData clientData; /* Main window associated with
155 * interpreter. */
156 Tcl_Interp *interp; /* Current interpreter. */
157 int argc; /* Number of arguments. */
158 CONST char **argv; /* Argument strings. */
159 {
160 Tk_Window tkwin = (Tk_Window) clientData;
161 register TkScrollbar *scrollPtr;
162 Tk_Window new;
163
164 if (argc < 2) {
165 Tcl_AppendResult(interp, "wrong # args: should be \"",
166 argv[0], " pathName ?options?\"", (char *) NULL);
167 return TCL_ERROR;
168 }
169
170 new = Tk_CreateWindowFromPath(interp, tkwin, argv[1], (char *) NULL);
171 if (new == NULL) {
172 return TCL_ERROR;
173 }
174
175 Tk_SetClass(new, "Scrollbar");
176 scrollPtr = TkpCreateScrollbar(new);
177
178 Tk_SetClassProcs(new, &tkpScrollbarProcs, (ClientData) scrollPtr);
179
180 /*
181 * Initialize fields that won't be initialized by ConfigureScrollbar,
182 * or which ConfigureScrollbar expects to have reasonable values
183 * (e.g. resource pointers).
184 */
185
186 scrollPtr->tkwin = new;
187 scrollPtr->display = Tk_Display(new);
188 scrollPtr->interp = interp;
189 scrollPtr->widgetCmd = Tcl_CreateCommand(interp,
190 Tk_PathName(scrollPtr->tkwin), ScrollbarWidgetCmd,
191 (ClientData) scrollPtr, ScrollbarCmdDeletedProc);
192 scrollPtr->vertical = 0;
193 scrollPtr->width = 0;
194 scrollPtr->command = NULL;
195 scrollPtr->repeatDelay = 0;
196 scrollPtr->repeatInterval = 0;
197 scrollPtr->borderWidth = 0;
198 scrollPtr->bgBorder = NULL;
199 scrollPtr->activeBorder = NULL;
200 scrollPtr->troughColorPtr = NULL;
201 scrollPtr->relief = TK_RELIEF_FLAT;
202 scrollPtr->highlightWidth = 0;
203 scrollPtr->highlightBgColorPtr = NULL;
204 scrollPtr->highlightColorPtr = NULL;
205 scrollPtr->inset = 0;
206 scrollPtr->elementBorderWidth = -1;
207 scrollPtr->arrowLength = 0;
208 scrollPtr->sliderFirst = 0;
209 scrollPtr->sliderLast = 0;
210 scrollPtr->activeField = 0;
211 scrollPtr->activeRelief = TK_RELIEF_RAISED;
212 scrollPtr->totalUnits = 0;
213 scrollPtr->windowUnits = 0;
214 scrollPtr->firstUnit = 0;
215 scrollPtr->lastUnit = 0;
216 scrollPtr->firstFraction = 0.0;
217 scrollPtr->lastFraction = 0.0;
218 scrollPtr->cursor = None;
219 scrollPtr->takeFocus = NULL;
220 scrollPtr->flags = 0;
221
222 if (ConfigureScrollbar(interp, scrollPtr, argc-2, argv+2, 0) != TCL_OK) {
223 Tk_DestroyWindow(scrollPtr->tkwin);
224 return TCL_ERROR;
225 }
226
227 Tcl_SetResult(interp, Tk_PathName(scrollPtr->tkwin), TCL_STATIC);
228 return TCL_OK;
229 }
230
231 /*
232 *--------------------------------------------------------------
233 *
234 * ScrollbarWidgetCmd --
235 *
236 * This procedure is invoked to process the Tcl command
237 * that corresponds to a widget managed by this module.
238 * See the user documentation for details on what it does.
239 *
240 * Results:
241 * A standard Tcl result.
242 *
243 * Side effects:
244 * See the user documentation.
245 *
246 *--------------------------------------------------------------
247 */
248
249 static int
250 ScrollbarWidgetCmd(clientData, interp, argc, argv)
251 ClientData clientData; /* Information about scrollbar
252 * widget. */
253 Tcl_Interp *interp; /* Current interpreter. */
254 int argc; /* Number of arguments. */
255 CONST char **argv; /* Argument strings. */
256 {
257 register TkScrollbar *scrollPtr = (TkScrollbar *) clientData;
258 int result = TCL_OK;
259 size_t length;
260 int c;
261
262 if (argc < 2) {
263 Tcl_AppendResult(interp, "wrong # args: should be \"",
264 argv[0], " option ?arg arg ...?\"", (char *) NULL);
265 return TCL_ERROR;
266 }
267 Tcl_Preserve((ClientData) scrollPtr);
268 c = argv[1][0];
269 length = strlen(argv[1]);
270 if ((c == 'a') && (strncmp(argv[1], "activate", length) == 0)) {
271 int oldActiveField;
272 if (argc == 2) {
273 switch (scrollPtr->activeField) {
274 case TOP_ARROW:
275 Tcl_SetResult(interp, "arrow1", TCL_STATIC);
276 break;
277 case SLIDER:
278 Tcl_SetResult(interp, "slider", TCL_STATIC);
279 break;
280 case BOTTOM_ARROW:
281 Tcl_SetResult(interp, "arrow2", TCL_STATIC);
282 break;
283 }
284 goto done;
285 }
286 if (argc != 3) {
287 Tcl_AppendResult(interp, "wrong # args: should be \"",
288 argv[0], " activate element\"", (char *) NULL);
289 goto error;
290 }
291 c = argv[2][0];
292 length = strlen(argv[2]);
293 oldActiveField = scrollPtr->activeField;
294 if ((c == 'a') && (strcmp(argv[2], "arrow1") == 0)) {
295 scrollPtr->activeField = TOP_ARROW;
296 } else if ((c == 'a') && (strcmp(argv[2], "arrow2") == 0)) {
297 scrollPtr->activeField = BOTTOM_ARROW;
298 } else if ((c == 's') && (strncmp(argv[2], "slider", length) == 0)) {
299 scrollPtr->activeField = SLIDER;
300 } else {
301 scrollPtr->activeField = OUTSIDE;
302 }
303 if (oldActiveField != scrollPtr->activeField) {
304 TkScrollbarEventuallyRedraw(scrollPtr);
305 }
306 if (oldActiveField != scrollPtr->activeField) {
307 TkScrollbarEventuallyRedraw(scrollPtr);
308 }
309 } else if ((c == 'c') && (strncmp(argv[1], "cget", length) == 0)
310 && (length >= 2)) {
311 if (argc != 3) {
312 Tcl_AppendResult(interp, "wrong # args: should be \"",
313 argv[0], " cget option\"",
314 (char *) NULL);
315 goto error;
316 }
317 result = Tk_ConfigureValue(interp, scrollPtr->tkwin,
318 tkpScrollbarConfigSpecs, (char *) scrollPtr, argv[2], 0);
319 } else if ((c == 'c') && (strncmp(argv[1], "configure", length) == 0)
320 && (length >= 2)) {
321 if (argc == 2) {
322 result = Tk_ConfigureInfo(interp, scrollPtr->tkwin,
323 tkpScrollbarConfigSpecs, (char *) scrollPtr,
324 (char *) NULL, 0);
325 } else if (argc == 3) {
326 result = Tk_ConfigureInfo(interp, scrollPtr->tkwin,
327 tkpScrollbarConfigSpecs, (char *) scrollPtr, argv[2], 0);
328 } else {
329 result = ConfigureScrollbar(interp, scrollPtr, argc-2, argv+2,
330 TK_CONFIG_ARGV_ONLY);
331 }
332 } else if ((c == 'd') && (strncmp(argv[1], "delta", length) == 0)) {
333 int xDelta, yDelta, pixels, length;
334 double fraction;
335 char buf[TCL_DOUBLE_SPACE];
336
337 if (argc != 4) {
338 Tcl_AppendResult(interp, "wrong # args: should be \"",
339 argv[0], " delta xDelta yDelta\"", (char *) NULL);
340 goto error;
341 }
342 if ((Tcl_GetIntFromObj(interp, argv[2], &xDelta) != TCL_OK)
343 || (Tcl_GetIntFromObj(interp, argv[3], &yDelta) != TCL_OK)) {
344 goto error;
345 }
346 if (scrollPtr->vertical) {
347 pixels = yDelta;
348 length = Tk_Height(scrollPtr->tkwin) - 1
349 - 2*(scrollPtr->arrowLength + scrollPtr->inset);
350 } else {
351 pixels = xDelta;
352 length = Tk_Width(scrollPtr->tkwin) - 1
353 - 2*(scrollPtr->arrowLength + scrollPtr->inset);
354 }
355 if (length == 0) {
356 fraction = 0.0;
357 } else {
358 fraction = ((double) pixels / (double) length);
359 }
360 sprintf(buf, "%g", fraction);
361 Tcl_SetResult(interp, buf, TCL_VOLATILE);
362 } else if ((c == 'f') && (strncmp(argv[1], "fraction", length) == 0)) {
363 int x, y, pos, length;
364 double fraction;
365 char buf[TCL_DOUBLE_SPACE];
366
367 if (argc != 4) {
368 Tcl_AppendResult(interp, "wrong # args: should be \"",
369 argv[0], " fraction x y\"", (char *) NULL);
370 goto error;
371 }
372 if ((Tcl_GetIntFromObj(interp, argv[2], &x) != TCL_OK)
373 || (Tcl_GetIntFromObj(interp, argv[3], &y) != TCL_OK)) {
374 goto error;
375 }
376 if (scrollPtr->vertical) {
377 pos = y - (scrollPtr->arrowLength + scrollPtr->inset);
378 length = Tk_Height(scrollPtr->tkwin) - 1
379 - 2*(scrollPtr->arrowLength + scrollPtr->inset);
380 } else {
381 pos = x - (scrollPtr->arrowLength + scrollPtr->inset);
382 length = Tk_Width(scrollPtr->tkwin) - 1
383 - 2*(scrollPtr->arrowLength + scrollPtr->inset);
384 }
385 if (length == 0) {
386 fraction = 0.0;
387 } else {
388 fraction = ((double) pos / (double) length);
389 }
390 if (fraction < 0) {
391 fraction = 0;
392 } else if (fraction > 1.0) {
393 fraction = 1.0;
394 }
395 sprintf(buf, "%g", fraction);
396 Tcl_SetResult(interp, buf, TCL_VOLATILE);
397 } else if ((c == 'g') && (strncmp(argv[1], "get", length) == 0)) {
398 if (argc != 2) {
399 Tcl_AppendResult(interp, "wrong # args: should be \"",
400 argv[0], " get\"", (char *) NULL);
401 goto error;
402 }
403 if (scrollPtr->flags & NEW_STYLE_COMMANDS) {
404 char first[TCL_DOUBLE_SPACE], last[TCL_DOUBLE_SPACE];
405 Tcl_DoubleResults(interp, 2, 1, scrollPtr->firstFraction, scrollPtr->lastFraction);
406 } else {
407 Tcl_IntResults(interp, 4, 1, scrollPtr->totalUnits,
408 scrollPtr->windowUnits, scrollPtr->firstUnit,
409 scrollPtr->lastUnit);
410 }
411 } else if ((c == 'i') && (strncmp(argv[1], "identify", length) == 0)) {
412 int x, y, thing;
413
414 if (argc != 4) {
415 Tcl_AppendResult(interp, "wrong # args: should be \"",
416 argv[0], " identify x y\"", (char *) NULL);
417 goto error;
418 }
419 if ((Tcl_GetIntFromObj(interp, argv[2], &x) != TCL_OK)
420 || (Tcl_GetIntFromObj(interp, argv[3], &y) != TCL_OK)) {
421 goto error;
422 }
423 thing = TkpScrollbarPosition(scrollPtr, x,y);
424 switch (thing) {
425 case TOP_ARROW:
426 Tcl_SetResult(interp, "arrow1", TCL_STATIC);
427 break;
428 case TOP_GAP:
429 Tcl_SetResult(interp, "trough1", TCL_STATIC);
430 break;
431 case SLIDER:
432 Tcl_SetResult(interp, "slider", TCL_STATIC);
433 break;
434 case BOTTOM_GAP:
435 Tcl_SetResult(interp, "trough2", TCL_STATIC);
436 break;
437 case BOTTOM_ARROW:
438 Tcl_SetResult(interp, "arrow2", TCL_STATIC);
439 break;
440 }
441 } else if ((c == 's') && (strncmp(argv[1], "set", length) == 0)) {
442 int totalUnits, windowUnits, firstUnit, lastUnit;
443
444 if (argc == 4) {
445 double first, last;
446
447 if (Tcl_GetDoubleFromObj(interp, argv[2], &first) != TCL_OK) {
448 goto error;
449 }
450 if (Tcl_GetDoubleFromObj(interp, argv[3], &last) != TCL_OK) {
451 goto error;
452 }
453 if (first < 0) {
454 scrollPtr->firstFraction = 0;
455 } else if (first > 1.0) {
456 scrollPtr->firstFraction = 1.0;
457 } else {
458 scrollPtr->firstFraction = first;
459 }
460 if (last < scrollPtr->firstFraction) {
461 scrollPtr->lastFraction = scrollPtr->firstFraction;
462 } else if (last > 1.0) {
463 scrollPtr->lastFraction = 1.0;
464 } else {
465 scrollPtr->lastFraction = last;
466 }
467 scrollPtr->flags |= NEW_STYLE_COMMANDS;
468 } else if (argc == 6) {
469 if (Tcl_GetIntFromObj(interp, argv[2], &totalUnits) != TCL_OK) {
470 goto error;
471 }
472 if (totalUnits < 0) {
473 totalUnits = 0;
474 }
475 if (Tcl_GetIntFromObj(interp, argv[3], &windowUnits) != TCL_OK) {
476 goto error;
477 }
478 if (windowUnits < 0) {
479 windowUnits = 0;
480 }
481 if (Tcl_GetIntFromObj(interp, argv[4], &firstUnit) != TCL_OK) {
482 goto error;
483 }
484 if (Tcl_GetIntFromObj(interp, argv[5], &lastUnit) != TCL_OK) {
485 goto error;
486 }
487 if (totalUnits > 0) {
488 if (lastUnit < firstUnit) {
489 lastUnit = firstUnit;
490 }
491 } else {
492 firstUnit = lastUnit = 0;
493 }
494 scrollPtr->totalUnits = totalUnits;
495 scrollPtr->windowUnits = windowUnits;
496 scrollPtr->firstUnit = firstUnit;
497 scrollPtr->lastUnit = lastUnit;
498 if (scrollPtr->totalUnits == 0) {
499 scrollPtr->firstFraction = 0.0;
500 scrollPtr->lastFraction = 1.0;
501 } else {
502 scrollPtr->firstFraction = ((double) firstUnit)/totalUnits;
503 scrollPtr->lastFraction = ((double) (lastUnit+1))/totalUnits;
504 }
505 scrollPtr->flags &= ~NEW_STYLE_COMMANDS;
506 } else {
507 Tcl_AppendResult(interp, "wrong # args: should be \"",
508 argv[0], " set firstFraction lastFraction\" or \"",
509 argv[0],
510 " set totalUnits windowUnits firstUnit lastUnit\"",
511 (char *) NULL);
512 goto error;
513 }
514 TkpComputeScrollbarGeometry(scrollPtr);
515 TkScrollbarEventuallyRedraw(scrollPtr);
516 } else {
517 Tcl_AppendResult(interp, "bad option \"", argv[1],
518 "\": must be activate, cget, configure, delta, fraction, ",
519 "get, identify, or set", (char *) NULL);
520 goto error;
521 }
522 done:
523 Tcl_Release((ClientData) scrollPtr);
524 return result;
525
526 error:
527 Tcl_Release((ClientData) scrollPtr);
528 return TCL_ERROR;
529 }
530
531 /*
532 *----------------------------------------------------------------------
533 *
534 * ConfigureScrollbar --
535 *
536 * This procedure is called to process an argv/argc list, plus
537 * the Tk option database, in order to configure (or
538 * reconfigure) a scrollbar widget.
539 *
540 * Results:
541 * The return value is a standard Tcl result. If TCL_ERROR is
542 * returned, then the interp's result contains an error message.
543 *
544 * Side effects:
545 * Configuration information, such as colors, border width,
546 * etc. get set for scrollPtr; old resources get freed,
547 * if there were any.
548 *
549 *----------------------------------------------------------------------
550 */
551
552 static int
553 ConfigureScrollbar(interp, scrollPtr, argc, argv, flags)
554 Tcl_Interp *interp; /* Used for error reporting. */
555 register TkScrollbar *scrollPtr; /* Information about widget; may or
556 * may not already have values for
557 * some fields. */
558 int argc; /* Number of valid entries in argv. */
559 CONST char **argv; /* Arguments. */
560 int flags; /* Flags to pass to
561 * Tk_ConfigureWidget. */
562 {
563 if (Tk_ConfigureWidget(interp, scrollPtr->tkwin, tkpScrollbarConfigSpecs,
564 argc, argv, (char *) scrollPtr, flags) != TCL_OK) {
565 return TCL_ERROR;
566 }
567
568 /*
569 * A few options need special processing, such as setting the
570 * background from a 3-D border.
571 */
572
573 /* Force width to be odd, so point of arrow has a place to be ... */
574 scrollPtr->width |= 1;
575
576 /*
577 * Configure platform specific options.
578 */
579
580 TkpConfigureScrollbar(scrollPtr);
581
582 /*
583 * Register the desired geometry for the window (leave enough space
584 * for the two arrows plus a minimum-size slider, plus border around
585 * the whole window, if any). Then arrange for the window to be
586 * redisplayed.
587 */
588
589 TkpComputeScrollbarGeometry(scrollPtr);
590 TkScrollbarEventuallyRedraw(scrollPtr);
591 return TCL_OK;
592 }
593
594 /*
595 *--------------------------------------------------------------
596 *
597 * TkScrollbarEventProc --
598 *
599 * This procedure is invoked by the Tk dispatcher for various
600 * events on scrollbars.
601 *
602 * Results:
603 * None.
604 *
605 * Side effects:
606 * When the window gets deleted, internal structures get
607 * cleaned up. When it gets exposed, it is redisplayed.
608 *
609 *--------------------------------------------------------------
610 */
611
612 void
613 TkScrollbarEventProc(clientData, eventPtr)
614 ClientData clientData; /* Information about window. */
615 XEvent *eventPtr; /* Information about event. */
616 {
617 TkScrollbar *scrollPtr = (TkScrollbar *) clientData;
618
619 if ((eventPtr->type == Expose) && (eventPtr->xexpose.count == 0)) {
620 TkScrollbarEventuallyRedraw(scrollPtr);
621 } else if (eventPtr->type == DestroyNotify) {
622 TkpDestroyScrollbar(scrollPtr);
623 if (scrollPtr->tkwin != NULL) {
624 scrollPtr->tkwin = NULL;
625 Tcl_DeleteCommandFromToken(scrollPtr->interp,
626 scrollPtr->widgetCmd);
627 }
628 if (scrollPtr->flags & REDRAW_PENDING) {
629 Tcl_CancelIdleCall(TkpDisplayScrollbar, (ClientData) scrollPtr);
630 }
631 /*
632 * Free up all the stuff that requires special handling, then
633 * let Tk_FreeOptions handle all the standard option-related
634 * stuff.
635 */
636
637 Tk_FreeOptions(tkpScrollbarConfigSpecs, (char *) scrollPtr,
638 scrollPtr->display, 0);
639 Tcl_EventuallyFree((ClientData) scrollPtr, TCL_DYNAMIC);
640 } else if (eventPtr->type == ConfigureNotify) {
641 TkpComputeScrollbarGeometry(scrollPtr);
642 TkScrollbarEventuallyRedraw(scrollPtr);
643 } else if (eventPtr->type == FocusIn) {
644 if (eventPtr->xfocus.detail != NotifyInferior) {
645 scrollPtr->flags |= GOT_FOCUS;
646 if (scrollPtr->highlightWidth > 0) {
647 TkScrollbarEventuallyRedraw(scrollPtr);
648 }
649 }
650 } else if (eventPtr->type == FocusOut) {
651 if (eventPtr->xfocus.detail != NotifyInferior) {
652 scrollPtr->flags &= ~GOT_FOCUS;
653 if (scrollPtr->highlightWidth > 0) {
654 TkScrollbarEventuallyRedraw(scrollPtr);
655 }
656 }
657 }
658 }
659
660 /*
661 *----------------------------------------------------------------------
662 *
663 * ScrollbarCmdDeletedProc --
664 *
665 * This procedure is invoked when a widget command is deleted. If
666 * the widget isn't already in the process of being destroyed,
667 * this command destroys it.
668 *
669 * Results:
670 * None.
671 *
672 * Side effects:
673 * The widget is destroyed.
674 *
675 *----------------------------------------------------------------------
676 */
677
678 static void
679 ScrollbarCmdDeletedProc(clientData)
680 ClientData clientData; /* Pointer to widget record for widget. */
681 {
682 TkScrollbar *scrollPtr = (TkScrollbar *) clientData;
683 Tk_Window tkwin = scrollPtr->tkwin;
684
685 /*
686 * This procedure could be invoked either because the window was
687 * destroyed and the command was then deleted (in which case tkwin
688 * is NULL) or because the command was deleted, and then this procedure
689 * destroys the widget.
690 */
691
692 if (tkwin != NULL) {
693 scrollPtr->tkwin = NULL;
694 Tk_DestroyWindow(tkwin);
695 }
696 }
697
698 /*
699 *--------------------------------------------------------------
700 *
701 * TkScrollbarEventuallyRedraw --
702 *
703 * Arrange for one or more of the fields of a scrollbar
704 * to be redrawn.
705 *
706 * Results:
707 * None.
708 *
709 * Side effects:
710 * None.
711 *
712 *--------------------------------------------------------------
713 */
714
715 void
716 TkScrollbarEventuallyRedraw(scrollPtr)
717 register TkScrollbar *scrollPtr; /* Information about widget. */
718 {
719 if ((scrollPtr->tkwin == NULL) || (!Tk_IsMapped(scrollPtr->tkwin))) {
720 return;
721 }
722 if ((scrollPtr->flags & REDRAW_PENDING) == 0) {
723 Tcl_DoWhenIdle(TkpDisplayScrollbar, (ClientData) scrollPtr);
724 scrollPtr->flags |= REDRAW_PENDING;
725 }
726 }