1 /* Implements a lightweight scrollbar widget.
2 Copyright (C) 1992, 1993, 1994 Lucid, Inc.
3 Copyright (C) 1997 Sun Microsystems, Inc.
5 This file is part of the Lucid Widget Library.
7 The Lucid Widget Library is free software: you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation, either version 3 of the
10 License, or (at your option) any later version.
12 The Lucid Widget Library is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* Created by Douglas Keller <dkeller@vnet.ibm.com> */
21 /* Lots of hacking by Martin Buchholz */
24 * Athena-style scrollbar button bindings added on Sun Dec 24 22:03:57 1995
25 * by Jonathan Stigelman <Stig@hackvan.com>... Ho ho ho!
27 * To use them, put this resource in your .Xdefaults
29 * Emacs*XlwScrollBar.translations: #override \n\
30 * <Btn1Down>: PageDownOrRight() \n\
31 * <Btn3Down>: PageUpOrLeft() \n\
37 * Resources Supported:
42 * XmNbottomShadowColor
43 * XmNbottomShadowPixmap
56 * XmNvalueChangedCallback
57 * XmNincrementCallback
58 * XmNdecrementCallback
59 * XmNpageIncrementCallback
60 * XmNpageDecrementCallback
65 * XmNsliderStyle - values can be: "plain" or "dimple"
66 * XmNarrowPosition - values can be: "opposite" or "same"
75 #include <X11/IntrinsicP.h>
76 #include <X11/StringDefs.h>
77 #include <X11/bitmaps/gray>
79 #include "xlwscrollbarP.h"
80 #include "xlwscrollbar.h"
82 #ifdef USE_DEBUG_MALLOC
88 #define MINL(x,y) ((((unsigned long) (x)) < ((unsigned long) (y))) \
89 ? ((unsigned long) (x)) : ((unsigned long) (y)))
91 #define VERT(w) ((w)->sb.orientation == XmVERTICAL)
109 /*-------------------------- Resources ----------------------------------*/
110 #define offset(field) XtOffset(XlwScrollBarWidget, field)
112 static XtResource resources[] = {
113 {XmNforeground, XmCForeground, XtRPixel, sizeof(Pixel),
114 offset(sb.foreground), XtRImmediate, (XtPointer) XtDefaultForeground}
117 {XmNtopShadowColor, XmCTopShadowColor, XtRPixel,
118 sizeof(Pixel), offset(sb.topShadowColor), XtRImmediate, (XtPointer) ~ 0}
120 {XmNbottomShadowColor, XmCBottomShadowColor, XtRPixel,
121 sizeof(Pixel), offset(sb.bottomShadowColor), XtRImmediate,
125 {XmNtopShadowPixmap, XmCTopShadowPixmap, XtRPixmap,
126 sizeof(Pixmap), offset(sb.topShadowPixmap), XtRImmediate,
129 {XmNbottomShadowPixmap, XmCBottomShadowPixmap,
130 XtRPixmap, sizeof(Pixmap), offset(sb.bottomShadowPixmap),
131 XtRImmediate, (XtPointer) None}
134 {XmNtroughColor, XmCTroughColor, XtRPixel, sizeof(Pixel),
135 offset(sb.troughColor), XtRImmediate, (XtPointer) ~ 0}
138 {XmNshadowThickness, XmCShadowThickness, XtRInt,
139 sizeof(int), offset(sb.shadowThickness), XtRImmediate, (XtPointer) 2},
141 {XmNborderWidth, XmCBorderWidth, XtRDimension,
142 sizeof(Dimension), offset(core.border_width), XtRImmediate,
146 {XmNshowArrows, XmCShowArrows, XtRBoolean,
147 sizeof(Boolean), offset(sb.showArrows), XtRImmediate, (XtPointer) True}
150 {XmNinitialDelay, XmCInitialDelay, XtRInt, sizeof(int),
151 offset(sb.initialDelay), XtRImmediate, (XtPointer) 250},
152 {XmNrepeatDelay, XmCRepeatDelay, XtRInt, sizeof(int),
153 offset(sb.repeatDelay), XtRImmediate, (XtPointer) 50},
155 {XmNorientation, XmCOrientation, XtROrientation,
156 sizeof(unsigned char), offset(sb.orientation), XtRImmediate,
157 (XtPointer) XmVERTICAL},
159 {XmNminimum, XmCMinimum, XtRInt, sizeof(int),
160 offset(sb.minimum), XtRImmediate, (XtPointer) 0},
161 {XmNmaximum, XmCMaximum, XtRInt, sizeof(int),
162 offset(sb.maximum), XtRImmediate, (XtPointer) 100},
163 {XmNvalue, XmCValue, XtRInt, sizeof(int),
164 offset(sb.value), XtRImmediate, (XtPointer) 0},
165 {XmNsliderSize, XmCSliderSize, XtRInt, sizeof(int),
166 offset(sb.sliderSize), XtRImmediate, (XtPointer) 10},
167 {XmNincrement, XmCIncrement, XtRInt, sizeof(int),
168 offset(sb.increment), XtRImmediate, (XtPointer) 1},
169 {XmNpageIncrement, XmCPageIncrement, XtRInt, sizeof(int),
170 offset(sb.pageIncrement), XtRImmediate, (XtPointer) 10},
172 {XmNvalueChangedCallback, XmCValueChangedCallback,
173 XtRCallback, sizeof(XtPointer), offset(sb.valueChangedCBL),
176 {XmNincrementCallback, XmCIncrementCallback,
177 XtRCallback, sizeof(XtPointer), offset(sb.incrementCBL),
180 {XmNdecrementCallback, XmCDecrementCallback,
181 XtRCallback, sizeof(XtPointer), offset(sb.decrementCBL),
184 {XmNpageIncrementCallback, XmCPageIncrementCallback,
185 XtRCallback, sizeof(XtPointer), offset(sb.pageIncrementCBL),
188 {XmNpageDecrementCallback, XmCPageDecrementCallback,
189 XtRCallback, sizeof(XtPointer), offset(sb.pageDecrementCBL),
192 {XmNtoTopCallback, XmCToTopCallback, XtRCallback,
193 sizeof(XtPointer), offset(sb.toTopCBL), XtRCallback, NULL}
195 {XmNtoBottomCallback, XmCToBottomCallback, XtRCallback,
196 sizeof(XtPointer), offset(sb.toBottomCBL), XtRCallback, NULL}
198 {XmNdragCallback, XmCDragCallback, XtRCallback,
199 sizeof(XtPointer), offset(sb.dragCBL), XtRCallback, NULL}
202 /* "knob" is obsolete; use "slider" instead. */
203 {XmNsliderStyle, XmCSliderStyle, XtRString, sizeof(char *),
204 offset(sb.sliderStyle), XtRImmediate, NULL},
205 {XmNknobStyle, XmCKnobStyle, XtRString, sizeof(char *),
206 offset(sb.knobStyle), XtRImmediate, NULL},
208 {XmNarrowPosition, XmCArrowPosition, XtRString, sizeof(char *),
209 offset(sb.arrowPosition), XtRImmediate, NULL},
212 /*-------------------------- Prototypes ---------------------------------*/
215 typedef void Action(Widget w, XEvent * event, String * parms,
216 Cardinal * num_parms);
217 static Action Select, PageUpOrLeft, PageDownOrRight, Drag, Release, Jump, Abort;
220 static void Initialize(Widget treq, Widget tnew, ArgList args,
221 Cardinal * num_args);
222 static Boolean SetValues(Widget current, Widget request, Widget nw,
223 ArgList args, Cardinal * num_args);
224 static void Destroy(Widget widget);
225 static void Redisplay(Widget widget, XEvent * event, Region region);
226 static void Resize(Widget widget);
227 static void Realize(Widget widget, XtValueMask * valuemask,
228 XSetWindowAttributes * attr);
232 /*-------------------------- Actions Table ------------------------------*/
233 static XtActionsRec actions[] = {
235 {"PageDownOrRight", PageDownOrRight},
236 {"PageUpOrLeft", PageUpOrLeft},
238 {"Release", Release},
243 /*--------------------- Default Translation Table -----------------------*/
244 static char default_translations[] =
245 "<Btn1Down>: Select()\n"
246 "<Btn1Motion>: Drag()\n"
247 "<Btn1Up>: Release()\n"
248 "<Btn2Down>: Jump()\n"
249 "<Btn2Motion>: Drag()\n"
250 "<Btn2Up>: Release()\n" "<Key>Delete: Abort()";
252 /*------------------- Class record initialization -----------------------*/
253 XlwScrollBarClassRec xlwScrollBarClassRec = {
254 /* core_class fields */
256 /* superclass */ (WidgetClass) & coreClassRec,
257 /* class_name */ "XlwScrollBar",
258 /* widget_size */ sizeof(XlwScrollBarRec),
259 /* class_initialize */ NULL,
260 /* class_part_init */ NULL,
261 /* class_inited */ False,
262 /* initialize */ Initialize,
263 /* initialize_hook */ NULL,
264 /* realize */ Realize,
265 /* actions */ actions,
266 /* num_actions */ XtNumber(actions),
267 /* resources */ resources,
268 /* num_resources */ XtNumber(resources),
269 /* xrm_class */ NULLQUARK,
270 /* compress_motion */ True,
271 /* compress_exposure */ XtExposeCompressMultiple,
272 /* compress_enterleave */ True,
273 /* visible_interest */ False,
274 /* destroy */ Destroy,
276 /* expose */ Redisplay,
277 /* set_values */ SetValues,
278 /* set_values_hook */ NULL,
279 /* set_values_almost */ XtInheritSetValuesAlmost,
280 /* get_values_hook */ NULL,
281 /* accept_focus */ NULL,
282 /* version */ XtVersionDontCheck,
283 /* callback_private */ NULL,
284 /* tm_table */ default_translations,
285 /* query_geometry */ NULL,
288 /* scrollbar_class fields */
295 WidgetClass xlwScrollBarWidgetClass = (WidgetClass) & xlwScrollBarClassRec;
297 /*-------------------------- Debug Functions ----------------------------*/
301 myXClearArea(Display * dpy, Drawable d, int x, int y, int w, int h,
302 Boolean exp, XlwScrollBarWidget widget)
304 XFillRectangle(dpy, d, widget->sb.topShadowGC, x, y, w, h);
307 XClearArea(dpy, d, x, y, w, h, exp);
310 #define XClearArea(dpy,win,x,y,width,height,exp) myXClearArea(dpy,win,x,y,width,height,exp,w)
314 static void check(XlwScrollBarWidget w)
316 int height = widget_h(w);
317 if (w->sb.showArrows)
318 height -= (2 * arrow_h(w));
320 if ((w->sb.above + w->sb.ss + w->sb.below > height) ||
321 (w->sb.value < w->sb.minimum) ||
322 (w->sb.value > w->sb.maximum - w->sb.sliderSize)) {
323 printf("above=%d ss=%d below=%d height=%d\n",
324 w->sb.above, w->sb.ss, w->sb.below, height);
325 printf("value=%d min=%d max=%d ss=%d max-ss=%d\n",
326 w->sb.value, w->sb.minimum, w->sb.maximum,
327 w->sb.sliderSize, w->sb.maximum - w->sb.sliderSize);
332 # define CHECK(w) check(w)
337 /*-------------------------- Static functions ---------------------------*/
340 call_callbacks(XlwScrollBarWidget w, int reason,
341 int value, int pixel, XEvent * event)
343 XlwScrollBarCallbackStruct cbs;
344 Boolean called_anything;
351 called_anything = False;
354 case XmCR_VALUE_CHANGED:
355 XtCallCallbackList((Widget) w, w->sb.valueChangedCBL, &cbs);
356 called_anything = True;
359 if (w->sb.incrementCBL) {
360 XtCallCallbackList((Widget) w, w->sb.incrementCBL,
362 called_anything = True;
366 if (w->sb.decrementCBL) {
367 XtCallCallbackList((Widget) w, w->sb.decrementCBL,
369 called_anything = True;
372 case XmCR_PAGE_INCREMENT:
373 if (w->sb.incrementCBL) {
374 XtCallCallbackList((Widget) w, w->sb.pageIncrementCBL,
376 called_anything = True;
379 case XmCR_PAGE_DECREMENT:
380 if (w->sb.decrementCBL) {
381 XtCallCallbackList((Widget) w, w->sb.pageDecrementCBL,
383 called_anything = True;
387 if (w->sb.toTopCBL) {
388 XtCallCallbackList((Widget) w, w->sb.toTopCBL, &cbs);
389 called_anything = True;
393 if (w->sb.toBottomCBL) {
394 XtCallCallbackList((Widget) w, w->sb.toBottomCBL, &cbs);
395 called_anything = True;
400 XtCallCallbackList((Widget) w, w->sb.dragCBL, &cbs);
402 called_anything = True; /* Special Case */
406 if (!called_anything) {
407 cbs.reason = XmCR_VALUE_CHANGED;
408 XtCallCallbackList((Widget) w, w->sb.valueChangedCBL, &cbs);
412 /* Widget sizes minus the shadow and highlight area */
414 static int widget_x(XlwScrollBarWidget w)
416 return w->sb.shadowThickness;
419 static int widget_y(XlwScrollBarWidget w)
421 return w->sb.shadowThickness;
424 static int widget_w(XlwScrollBarWidget w)
426 int x = w->sb.shadowThickness;
427 int width = (VERT(w) ? w->core.width : w->core.height) - (2 * x);
428 return width > 1 ? width : 1;
431 static int widget_h(XlwScrollBarWidget w)
433 int y = w->sb.shadowThickness;
434 int height = (VERT(w) ? w->core.height : w->core.width) - (2 * y);
436 return height > 1 ? height : 1;
439 static int arrow_h(XlwScrollBarWidget w)
441 int width = widget_w(w);
442 int minimum_size = ((widget_h(w) - SS_MIN) / 2) - 1;
443 return minimum_size < width ? minimum_size : width;
446 static int event_x(XlwScrollBarWidget w, XEvent * event)
448 return VERT(w) ? event->xbutton.x : event->xbutton.y;
451 static int event_y(XlwScrollBarWidget w, XEvent * event)
453 return VERT(w) ? event->xbutton.y : event->xbutton.x;
456 /* Safe addition and subtraction */
457 static void increment_value(XlwScrollBarWidget w, int diff)
459 w->sb.value = w->sb.maximum - diff < w->sb.value ?
460 w->sb.maximum : w->sb.value + diff;
463 static void decrement_value(XlwScrollBarWidget w, int diff)
465 w->sb.value = w->sb.minimum + diff > w->sb.value ?
466 w->sb.minimum : w->sb.value - diff;
469 static SliderStyle slider_style(XlwScrollBarWidget w)
471 return (w->sb.sliderStyle ? w->sb.sliderStyle[0] == 'd' :
472 w->sb.knobStyle ? w->sb.knobStyle[0] == 'd' :
473 0) ? SLIDER_DIMPLE : SLIDER_PLAIN;
476 static Boolean arrow_same_end(XlwScrollBarWidget w)
478 return w->sb.arrowPosition
479 && w->sb.arrowPosition[0] == 's' ? True : False;
482 /*-------------------------- GC and Pixel allocation --------------------*/
483 #ifndef XmUNSPECIFIED_PIXMAP
484 #define XmUNSPECIFIED_PIXMAP 2
487 static GC get_gc(XlwScrollBarWidget w, Pixel fg, Pixel bg, Pixmap pm)
492 if (pm == w->sb.grayPixmap) {
493 /* If we're using the gray pixmap, guarantee white on black ...
494 * otherwise, we could end up with something odd like grey on white
495 * when we're on a color display that ran out of color cells
498 fg = WhitePixelOfScreen(DefaultScreenOfDisplay(XtDisplay(w)));
499 bg = BlackPixelOfScreen(DefaultScreenOfDisplay(XtDisplay(w)));
502 values.foreground = fg;
503 values.background = bg;
504 values.fill_style = FillOpaqueStippled;
506 /* mask = GCForeground | GCBackground |
507 (pm == None ? 0 : GCStipple | GCFillStyle); gtb */
508 if (pm != None && pm != 0 && pm != XmUNSPECIFIED_PIXMAP)
511 values.stipple = None;
512 mask = GCForeground | GCBackground |
513 (values.stipple == None ? 0 : GCStipple | GCFillStyle);
515 return XtGetGC((Widget) w, mask, &values);
518 /* Replacement for XAllocColor() that tries to return the nearest
519 available color if the colormap is full. From FSF Emacs. */
522 allocate_nearest_color(Display * display, Colormap screen_colormap,
525 int status = XAllocColor(display, screen_colormap, color_def);
530 /* If we got to this point, the colormap is full, so we're
531 going to try to get the next closest color.
532 The algorithm used is a least-squares matching, which is
533 what X uses for closest color matching with StaticColor visuals. */
536 unsigned long nearest_delta = ULONG_MAX;
538 int no_cells = XDisplayCells(display, XDefaultScreen(display));
539 /* Don't use alloca here because lwlib doesn't have the
540 necessary configuration information that src does. */
541 XColor *cells = (XColor *) malloc(sizeof(XColor) * no_cells);
543 for (x = 0; x < no_cells; x++)
546 XQueryColors(display, screen_colormap, cells, no_cells);
548 for (nearest = 0, x = 0; x < no_cells; x++) {
549 long dred = (color_def->red >> 8) - (cells[x].red >> 8);
551 (color_def->green >> 8) - (cells[x].green >> 8);
553 (color_def->blue >> 8) - (cells[x].blue >> 8);
554 unsigned long delta =
555 dred * dred + dgreen * dgreen + dblue * dblue;
557 if (delta < nearest_delta) {
559 nearest_delta = delta;
562 color_def->red = cells[nearest].red;
563 color_def->green = cells[nearest].green;
564 color_def->blue = cells[nearest].blue;
566 return XAllocColor(display, screen_colormap, color_def);
570 static void make_shadow_pixels(XlwScrollBarWidget w)
572 Display *dpy = XtDisplay((Widget) w);
573 Colormap cmap = w->core.colormap;
575 int top_frobbed, bottom_frobbed;
578 top_frobbed = bottom_frobbed = 0;
580 bg = w->core.background_pixel;
581 fg = w->sb.foreground;
583 if (w->sb.topShadowColor == (Pixel) ~ 0)
584 w->sb.topShadowColor = bg;
585 if (w->sb.bottomShadowColor == (Pixel) ~ 0)
586 w->sb.bottomShadowColor = fg;
588 if (w->sb.topShadowColor == bg || w->sb.topShadowColor == fg) {
590 XQueryColor(dpy, cmap, &topc);
591 /* don't overflow/wrap! */
592 topc.red = MINL(65535, topc.red * 1.2);
593 topc.green = MINL(65535, topc.green * 1.2);
594 topc.blue = MINL(65535, topc.blue * 1.2);
595 if (allocate_nearest_color(dpy, cmap, &topc)) {
596 if (topc.pixel == bg) {
597 XFreeColors(dpy, cmap, &topc.pixel, 1, 0);
598 topc.red = MINL(65535, topc.red + 0x8000);
599 topc.green = MINL(65535, topc.green + 0x8000);
600 topc.blue = MINL(65535, topc.blue + 0x8000);
601 if (allocate_nearest_color(dpy, cmap, &topc)) {
602 w->sb.topShadowColor = topc.pixel;
605 w->sb.topShadowColor = topc.pixel;
612 if (w->sb.bottomShadowColor == fg || w->sb.bottomShadowColor == bg) {
614 XQueryColor(dpy, cmap, &botc);
615 botc.red = (botc.red * 3) / 5;
616 botc.green = (botc.green * 3) / 5;
617 botc.blue = (botc.blue * 3) / 5;
618 if (allocate_nearest_color(dpy, cmap, &botc)) {
619 if (botc.pixel == bg) {
620 XFreeColors(dpy, cmap, &botc.pixel, 1, 0);
621 botc.red = MINL(65535, botc.red + 0x4000);
622 botc.green = MINL(65535, botc.green + 0x4000);
623 botc.blue = MINL(65535, botc.blue + 0x4000);
624 if (allocate_nearest_color(dpy, cmap, &botc)) {
625 w->sb.bottomShadowColor = botc.pixel;
628 w->sb.bottomShadowColor = botc.pixel;
634 if (top_frobbed && bottom_frobbed) {
636 ((topc.red / 3) + (topc.green / 3) + (topc.blue / 3));
638 ((botc.red / 3) + (botc.green / 3) + (botc.blue / 3));
639 if (bot_avg > top_avg) {
640 Pixel tmp = w->sb.topShadowColor;
641 w->sb.topShadowColor = w->sb.bottomShadowColor;
642 w->sb.bottomShadowColor = tmp;
643 } else if (topc.pixel == botc.pixel) {
644 if (botc.pixel == bg)
645 w->sb.topShadowColor = bg;
647 w->sb.bottomShadowColor = fg;
651 if (w->sb.topShadowColor == w->core.background_pixel ||
652 w->sb.bottomShadowColor == w->core.background_pixel) {
653 /* Assume we're in mono. This code should be okay even if we're
654 * really in color but just short on color cells -- We want the
655 * following behavior, which has been empirically determined to
656 * work well for all fg/bg combinations in mono: If the trough
657 * and slider are BOTH black, then use a white top shadow and a
658 * grey bottom shadow, otherwise use a grey top shadow and a
659 * black bottom shadow.
663 WhitePixelOfScreen(DefaultScreenOfDisplay(XtDisplay(w)));
665 BlackPixelOfScreen(DefaultScreenOfDisplay(XtDisplay(w)));
667 /* Note: core.background_pixel is the color of the slider ... */
669 if (w->core.background_pixel == black &&
670 w->sb.troughColor == black) {
671 w->sb.topShadowColor = white;
672 w->sb.bottomShadowPixmap = w->sb.grayPixmap;
674 w->sb.topShadowPixmap = w->sb.grayPixmap;
675 w->sb.bottomShadowColor = black;
680 static void make_trough_pixel(XlwScrollBarWidget w)
682 Display *dpy = XtDisplay((Widget) w);
683 Colormap cmap = w->core.colormap;
686 if (w->sb.troughColor == (Pixel) ~ 0)
687 w->sb.troughColor = w->core.background_pixel;
689 if (w->sb.troughColor == w->core.background_pixel) {
690 troughC.pixel = w->core.background_pixel;
691 XQueryColor(dpy, cmap, &troughC);
692 troughC.red = (troughC.red * 4) / 5;
693 troughC.green = (troughC.green * 4) / 5;
694 troughC.blue = (troughC.blue * 4) / 5;
695 if (allocate_nearest_color(dpy, cmap, &troughC))
696 w->sb.troughColor = troughC.pixel;
700 /*-------------------------- Draw 3D Border -----------------------------*/
702 draw_shadows(Display * dpy, Drawable d, GC shine_gc, GC shadow_gc,
703 int x, int y, int width, int height, int shadowT)
705 XSegment shine[10], shadow[10];
708 if (shadowT > (width / 2))
709 shadowT = (width / 2);
710 if (shadowT > (height / 2))
711 shadowT = (height / 2);
715 for (i = 0; i < shadowT; i++) {
718 shine[i].y2 = shine[i].y1 = y + i;
719 shine[i].x2 = x + width - i - 1;
721 shine[i + shadowT].x2 = shine[i + shadowT].x1 = x + i;
722 shine[i + shadowT].y1 = y + shadowT;
723 shine[i + shadowT].y2 = y + height - i - 1;
725 /* Bottom segments */
726 shadow[i].x1 = x + i;
727 shadow[i].y2 = shadow[i].y1 = y + height - i - 1;
728 shadow[i].x2 = x + width - 1;
730 shadow[i + shadowT].x2 = shadow[i + shadowT].x1 =
732 shadow[i + shadowT].y1 = y + i + 1;
733 shadow[i + shadowT].y2 = y + height - 1;
736 XDrawSegments(dpy, d, shine_gc, shine, shadowT * 2);
737 XDrawSegments(dpy, d, shadow_gc, shadow, shadowT * 2);
740 /*------------------ Draw 3D Arrows: left, up, down, right --------------*/
742 make_vert_seg(XSegment * seg, int x1, int y1, int x2, int y2, int shadowT)
746 for (i = 0; i < shadowT; i++, seg++) {
756 make_hor_seg(XSegment * seg, int x1, int y1, int x2, int y2, int shadowT)
760 for (i = 0; i < shadowT; i++, seg++) {
770 draw_arrow_up(Display * dpy, Drawable win, GC bgGC, GC shineGC, GC shadowGC,
771 int x, int y, int width, int height, int shadowT)
773 XSegment shine[10], shadow[10];
779 if (shadowT > (width / 2))
780 shadowT = (width / 2);
781 if (shadowT > (height / 2))
782 shadowT = (height / 2);
787 make_vert_seg(shine, x, y + height - shadowT - 1, x + mid, y, shadowT);
789 make_vert_seg(shadow,
790 x, y + height - shadowT - 1,
791 x + width - 1, y + height - shadowT - 1, shadowT);
792 make_vert_seg(shadow + shadowT,
794 x + width - 1, y + height - shadowT - 1, shadowT);
797 triangle[0].y = y + height - 1;
798 triangle[1].x = x + mid;
800 triangle[2].x = x + width - 1;
801 triangle[2].y = y + height - 1;
803 XFillPolygon(dpy, win, bgGC, triangle, 3, Convex, ArcChord);
805 XDrawSegments(dpy, win, shadowGC, shadow, shadowT * 2);
806 XDrawSegments(dpy, win, shineGC, shine, shadowT);
810 draw_arrow_left(Display * dpy, Drawable win, GC bgGC, GC shineGC, GC shadowGC,
811 int x, int y, int width, int height, int shadowT)
813 XSegment shine[10], shadow[10];
818 if (shadowT > (width / 2))
819 shadowT = (width / 2);
820 if (shadowT > (height / 2))
821 shadowT = (height / 2);
826 make_hor_seg(shine, x, y + mid, x + width - shadowT - 1, y, shadowT);
830 x + width - shadowT - 1, y + height - 1, shadowT);
831 make_hor_seg(shadow + shadowT,
832 x + width - shadowT - 1, y,
833 x + width - shadowT - 1, y + height - 1, shadowT);
835 triangle[0].x = x + width - 1;
836 triangle[0].y = y + height - 1;
838 triangle[1].y = y + mid;
839 triangle[2].x = x + width - 1;
842 XFillPolygon(dpy, win, bgGC, triangle, 3, Convex, ArcChord);
844 XDrawSegments(dpy, win, shadowGC, shadow, shadowT * 2);
845 XDrawSegments(dpy, win, shineGC, shine, shadowT);
849 draw_arrow_down(Display * dpy, Drawable win, GC bgGC, GC shineGC, GC shadowGC,
850 int x, int y, int width, int height, int shadowT)
852 XSegment shine[10], shadow[10];
858 if (shadowT > (width / 2))
859 shadowT = (width / 2);
860 if (shadowT > (height / 2))
861 shadowT = (height / 2);
866 make_vert_seg(shine, x, y, x + mid, y + height - shadowT - 1, shadowT);
867 make_vert_seg(shine + shadowT, x, y, x + width - 1, y, shadowT);
869 make_vert_seg(shadow,
871 x + mid, y + height - shadowT - 1, shadowT);
875 triangle[1].x = x + mid;
876 triangle[1].y = y + height - 1;
877 triangle[2].x = x + width - 1;
880 XFillPolygon(dpy, win, bgGC, triangle, 3, Convex, ArcChord);
882 XDrawSegments(dpy, win, shadowGC, shadow, shadowT);
883 XDrawSegments(dpy, win, shineGC, shine, shadowT * 2);
887 draw_arrow_right(Display * dpy, Drawable win, GC bgGC, GC shineGC, GC shadowGC,
888 int x, int y, int width, int height, int shadowT)
890 XSegment shine[10], shadow[10];
896 if (shadowT > (width / 2))
897 shadowT = (width / 2);
898 if (shadowT > (height / 2))
899 shadowT = (height / 2);
904 make_hor_seg(shine, x, y, x + width - shadowT - 1, y + mid, shadowT);
905 make_hor_seg(shine + shadowT, x, y, x, y + height - 1, shadowT);
909 x + width - shadowT - 1, y + mid, shadowT);
911 triangle[0].x = x + 1;
912 triangle[0].y = y + height - 1;
913 triangle[1].x = x + width - 1;
914 triangle[1].y = y + mid;
915 triangle[2].x = x + 1;
918 XFillPolygon(dpy, win, bgGC, triangle, 3, Convex, ArcChord);
920 XDrawSegments(dpy, win, shadowGC, shadow, shadowT);
921 XDrawSegments(dpy, win, shineGC, shine, shadowT * 2);
925 draw_dimple(Display * dpy, Drawable win, GC shine, GC shadow,
926 int x, int y, int width, int height)
928 XDrawArc(dpy, win, shine, x, y, width, height, 46 * 64, 180 * 64);
929 XDrawArc(dpy, win, shadow, x, y, width, height, 45 * 64, -179 * 64);
932 /*------- Scrollbar values -> pixels, pixels -> scrollbar values --------*/
935 seg_pixel_sizes(XlwScrollBarWidget w, int *above_return,
936 int *ss_return, int *below_return)
938 float total, height, fuz;
939 int value, above, ss, below;
941 height = widget_h(w);
942 if (w->sb.showArrows)
943 height -= (2 * arrow_h(w));
945 value = w->sb.value - w->sb.minimum;
947 total = w->sb.maximum - w->sb.minimum;
950 ss = (int)((height * w->sb.sliderSize + fuz) / total);
951 above = (int)((height * value + fuz) / total);
952 below = (int)((height) - (ss + above));
954 /* Don't let slider get smaller than SS_MIN */
956 /* add a percent amount for integer rounding */
958 (((float)(SS_MIN - ss) * (float)value) / total) + 0.5;
962 below = (int)((height) - (ss + above));
966 below = (int)(height - ss);
969 above = (int)(height - ss);
979 *above_return = above;
981 *below_return = below;
986 static void verify_values(XlwScrollBarWidget w)
988 int total = w->sb.maximum - w->sb.minimum;
990 if (w->sb.sliderSize > total)
991 w->sb.sliderSize = total;
993 if (w->sb.pageIncrement > total)
994 w->sb.pageIncrement = total;
996 if (w->sb.increment > total)
997 w->sb.increment = total;
999 if (w->sb.value < w->sb.minimum)
1000 w->sb.value = w->sb.minimum;
1002 if (w->sb.value > w->sb.maximum)
1003 w->sb.value = w->sb.maximum;
1005 if (w->sb.sliderSize > w->sb.maximum - w->sb.value)
1006 w->sb.sliderSize = w->sb.maximum - w->sb.value;
1009 static int value_from_pixel(XlwScrollBarWidget w, int above)
1011 float total, height, fuz;
1014 height = widget_h(w);
1015 if (w->sb.showArrows)
1016 height -= (2 * arrow_h(w));
1018 total = w->sb.maximum - w->sb.minimum;
1021 ss = (int)((height * w->sb.sliderSize + (total / 2)) / total);
1024 /* add a percent amount for integer rounding */
1025 above += (int)((((SS_MIN - ss) * above) + fuz) / height);
1029 /* Prevent SIGFPE's that would occur if we don't truncate the value. */
1031 w->sb.minimum + ((float)(above * total + fuz) / height);
1032 if (floatval >= (float)INT_MAX)
1034 else if (floatval <= (float)INT_MIN)
1037 value = (int)floatval;
1044 redraw_dimple(XlwScrollBarWidget w, Display * dpy, Window win,
1045 int x, int y, int width, int height)
1047 if (SLIDER_DIMPLE == slider_style(w)) {
1049 int slider_p = (w->sb.armed == ARM_SLIDER);
1050 GC shine = slider_p ? w->sb.bottomShadowGC : w->sb.topShadowGC;
1051 GC shadow = slider_p ? w->sb.topShadowGC : w->sb.bottomShadowGC;
1052 int shadowT = w->sb.shadowThickness;
1056 width -= 2 * shadowT;
1057 height -= 2 * shadowT;
1059 size = (width < height ? width : height) * 3 / 4;
1061 if (size % 2 != (width < height ? width : height) % 2)
1064 DBUG(fprintf(stderr, "%d %d\n",
1065 x + (width / 2) - (size / 2) - 2 * shadowT,
1066 width - size - shadowT));
1068 draw_dimple(dpy, win, shine, shadow,
1069 x + (width / 2) - (size / 2),
1070 y + (height / 2) - (size / 2), size, size);
1074 static void draw_slider(XlwScrollBarWidget w, int above, int ss, int below)
1076 Display *dpy = XtDisplay((Widget) w);
1077 Window win = XtWindow((Widget) w);
1079 int x = widget_x(w);
1080 int y = widget_y(w);
1081 int width = widget_w(w);
1082 int height = widget_h(w);
1083 int shadowT = w->sb.shadowThickness;
1084 int vert_p = VERT(w);
1086 if (shadowT > (width / 2))
1087 shadowT = (width / 2);
1088 if (shadowT > (height / 2))
1089 shadowT = (height / 2);
1093 if (w->sb.showArrows && !arrow_same_end(w))
1096 /* trough above slider */
1099 XClearArea(dpy, win, x, y, width, above, False);
1101 XClearArea(dpy, win, y, x, above, width, False);
1106 draw_shadows(dpy, win, w->sb.topShadowGC, w->sb.bottomShadowGC,
1107 x, y + above, width, ss, shadowT);
1108 XFillRectangle(dpy, win, w->sb.backgroundGC,
1109 x + shadowT, y + above + shadowT,
1110 width - 2 * shadowT, ss - 2 * shadowT);
1111 redraw_dimple(w, dpy, win, x, y + above, width, ss);
1113 draw_shadows(dpy, win, w->sb.topShadowGC, w->sb.bottomShadowGC,
1114 y + above, x, ss, width, shadowT);
1115 XFillRectangle(dpy, win, w->sb.backgroundGC,
1116 y + above + shadowT, x + shadowT,
1117 ss - 2 * shadowT, width - 2 * shadowT);
1118 redraw_dimple(w, dpy, win, y + above, x, ss, width);
1121 /* trough below slider */
1124 XClearArea(dpy, win, x, y + above + ss, width, below,
1127 XClearArea(dpy, win, y + above + ss, x, below, width,
1135 redraw_up_arrow(XlwScrollBarWidget w, Boolean armed, Boolean clear_behind)
1137 Display *dpy = XtDisplay((Widget) w);
1138 Window win = XtWindow((Widget) w);
1140 int x = widget_x(w);
1141 int y = widget_y(w);
1142 int width = widget_w(w);
1143 int height = widget_h(w);
1144 int shadowT = w->sb.shadowThickness;
1145 int arrow_height = arrow_h(w);
1147 GC bg = w->sb.backgroundGC;
1148 GC shine = armed ? w->sb.bottomShadowGC : w->sb.topShadowGC;
1149 GC shadow = armed ? w->sb.topShadowGC : w->sb.bottomShadowGC;
1152 if (arrow_same_end(w))
1153 y += height - 2 * arrow_height;
1155 XClearArea(dpy, win, x, y, width, arrow_height + 1,
1157 draw_arrow_up(dpy, win, bg, shine, shadow,
1158 x + (width - arrow_height) / 2, y, arrow_height,
1159 arrow_height, shadowT);
1161 if (arrow_same_end(w))
1162 y += height - 2 * arrow_height;
1164 XClearArea(dpy, win, y, x, arrow_height + 1, height,
1166 draw_arrow_left(dpy, win, bg, shine, shadow, y,
1167 x + (width - arrow_height) / 2, arrow_height,
1168 arrow_height, shadowT);
1173 redraw_down_arrow(XlwScrollBarWidget w, Boolean armed, Boolean clear_behind)
1175 Display *dpy = XtDisplay((Widget) w);
1176 Window win = XtWindow((Widget) w);
1178 int x = widget_x(w);
1179 int y = widget_y(w);
1180 int width = widget_w(w);
1181 int height = widget_h(w);
1182 int shadowT = w->sb.shadowThickness;
1183 int arrow_height = arrow_h(w);
1185 GC bg = w->sb.backgroundGC;
1186 GC shine = armed ? w->sb.bottomShadowGC : w->sb.topShadowGC;
1187 GC shadow = armed ? w->sb.topShadowGC : w->sb.bottomShadowGC;
1191 XClearArea(dpy, win, x, y + height - arrow_height,
1192 width, arrow_height + 1, False);
1193 draw_arrow_down(dpy, win, bg, shine, shadow,
1194 x + (width - arrow_height) / 2,
1195 y + height - arrow_height + 1, arrow_height,
1196 arrow_height, shadowT);
1199 XClearArea(dpy, win, y + height - arrow_height, x,
1200 arrow_height + 1, height, False);
1201 draw_arrow_right(dpy, win, bg, shine, shadow,
1202 y + height - arrow_height + 1,
1203 x + (width - arrow_height) / 2,
1204 arrow_height, arrow_height, shadowT);
1209 redraw_everything(XlwScrollBarWidget w, Region region, Boolean behind_arrows)
1211 Display *dpy = XtDisplay((Widget) w);
1212 Window win = XtWindow((Widget) w);
1214 if (w->sb.showArrows) {
1215 if (region == NULL) {
1216 redraw_up_arrow(w, False, behind_arrows);
1217 redraw_down_arrow(w, False, behind_arrows);
1219 int x = widget_x(w);
1220 int y = widget_y(w);
1221 int width = widget_w(w);
1222 int height = widget_h(w);
1223 int arrow_height = arrow_h(w);
1226 if (arrow_same_end(w)) {
1228 ay = y + height - arrow_height -
1231 ax = x + height - arrow_height -
1234 if (XRectInRegion(region, ax, ay, width, width))
1235 redraw_up_arrow(w, False, behind_arrows);
1238 ay = y + height - arrow_height;
1240 ax = x + height - arrow_height;
1241 if (XRectInRegion(region, ax, ay, width, width))
1242 redraw_down_arrow(w, False, behind_arrows);
1246 draw_shadows(dpy, win, w->sb.bottomShadowGC, w->sb.topShadowGC, 0, 0,
1247 w->core.width, w->core.height, w->sb.shadowThickness);
1249 draw_slider(w, w->sb.above, w->sb.ss, w->sb.below);
1252 /*-------------------------- Method Functions ---------------------------*/
1255 Initialize(Widget treq, Widget tnew, ArgList args, Cardinal * num_args)
1257 XlwScrollBarWidget request = (XlwScrollBarWidget) treq;
1258 XlwScrollBarWidget w = (XlwScrollBarWidget) tnew;
1259 Display *dpy = XtDisplay((Widget) w);
1260 Window win = RootWindowOfScreen(DefaultScreenOfDisplay(dpy));
1262 if (request->core.width == 0)
1263 w->core.width += (VERT(w) ? 12 : 25);
1264 if (request->core.height == 0)
1265 w->core.height += (VERT(w) ? 25 : 12);
1273 w->sb.armed = ARM_NONE;
1274 w->sb.forced_scroll = FORCED_SCROLL_NONE;
1276 if (w->sb.shadowThickness > 5)
1277 w->sb.shadowThickness = 5;
1280 XCreatePixmapFromBitmapData(dpy, win, (char *)gray_bits, gray_width,
1281 gray_height, 1, 0, 1);
1283 make_trough_pixel(w);
1285 make_shadow_pixels(w);
1287 w->sb.backgroundGC =
1288 get_gc(w, w->core.background_pixel, w->core.background_pixel, None);
1290 get_gc(w, w->sb.topShadowColor, w->core.background_pixel,
1291 w->sb.topShadowPixmap);
1292 w->sb.bottomShadowGC =
1293 get_gc(w, w->sb.bottomShadowColor, w->core.background_pixel,
1294 w->sb.bottomShadowPixmap);
1296 w->sb.fullRedrawNext = True;
1298 w->sb.timerActive = False;
1301 static void Destroy(Widget widget)
1303 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1304 Display *dpy = XtDisplay((Widget) w);
1306 XtReleaseGC(widget, w->sb.bottomShadowGC);
1307 XtReleaseGC(widget, w->sb.topShadowGC);
1308 XtReleaseGC(widget, w->sb.backgroundGC);
1310 XFreePixmap(dpy, w->sb.grayPixmap);
1312 if (w->sb.timerActive) {
1313 XtRemoveTimeOut(w->sb.timerId);
1314 w->sb.timerActive = False; /* Should be a no-op, but you never know */
1319 Realize(Widget widget, XtValueMask * valuemask, XSetWindowAttributes * attr)
1321 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1322 Display *dpy = XtDisplay((Widget) w);
1324 XSetWindowAttributes win_attr;
1326 (*coreClassRec.core_class.realize) (widget, valuemask, attr);
1328 win = XtWindow((Widget) w);
1330 seg_pixel_sizes(w, &w->sb.above, &w->sb.ss, &w->sb.below);
1332 XSetWindowBackground(dpy, win, w->sb.troughColor);
1334 /* Change bit gravity so widget is not cleared on resize */
1335 win_attr.bit_gravity = NorthWestGravity;
1336 XChangeWindowAttributes(dpy, win, CWBitGravity, &win_attr);
1340 static void Resize(Widget widget)
1342 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1343 Display *dpy = XtDisplay((Widget) w);
1344 Window win = XtWindow((Widget) w);
1346 if (XtIsRealized(widget)) {
1347 DBUG(fprintf(stderr, "Resize = %08lx\n", w));
1349 seg_pixel_sizes(w, &w->sb.above, &w->sb.ss, &w->sb.below);
1351 /* redraw_everything (w, NULL, True); */
1353 w->sb.fullRedrawNext = True;
1354 /* Force expose event */
1355 XClearArea(dpy, win, widget_x(w), widget_y(w), 1, 1, True);
1358 if (w->sb.timerActive) {
1359 XtRemoveTimeOut(w->sb.timerId);
1360 w->sb.timerActive = False;
1364 static void Redisplay(Widget widget, XEvent * event, Region region)
1366 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1368 DBUG(fprintf(stderr, "Redisplay = %08lx\n", w));
1370 if (XtIsRealized(widget)) {
1371 if (w->sb.fullRedrawNext)
1372 redraw_everything(w, NULL, True);
1374 redraw_everything(w, region, False);
1375 w->sb.fullRedrawNext = False;
1380 SetValues(Widget current, Widget request, Widget neww,
1381 ArgList args, Cardinal * num_args)
1383 XlwScrollBarWidget cur = (XlwScrollBarWidget) current;
1384 XlwScrollBarWidget w = (XlwScrollBarWidget) neww;
1385 Boolean do_redisplay = False;
1387 if (cur->sb.troughColor != w->sb.troughColor) {
1388 if (XtIsRealized((Widget) w)) {
1389 XSetWindowBackground(XtDisplay((Widget) w),
1390 XtWindow((Widget) w),
1392 do_redisplay = True;
1396 if (cur->core.background_pixel != w->core.background_pixel) {
1397 XtReleaseGC((Widget) cur, cur->sb.backgroundGC);
1398 w->sb.backgroundGC =
1399 get_gc(w, w->core.background_pixel,
1400 w->core.background_pixel, None);
1401 do_redisplay = True;
1404 if (cur->sb.topShadowColor != w->sb.topShadowColor ||
1405 cur->sb.topShadowPixmap != w->sb.topShadowPixmap) {
1406 XtReleaseGC((Widget) cur, cur->sb.topShadowGC);
1408 get_gc(w, w->sb.topShadowColor, w->core.background_pixel,
1409 w->sb.topShadowPixmap);
1410 do_redisplay = True;
1413 if (cur->sb.bottomShadowColor != w->sb.bottomShadowColor ||
1414 cur->sb.bottomShadowPixmap != w->sb.bottomShadowPixmap) {
1415 XtReleaseGC((Widget) cur, cur->sb.bottomShadowGC);
1416 w->sb.bottomShadowGC =
1417 get_gc(w, w->sb.bottomShadowColor, w->core.background_pixel,
1418 w->sb.bottomShadowPixmap);
1419 do_redisplay = True;
1422 if (cur->sb.orientation != w->sb.orientation)
1423 do_redisplay = True;
1425 if (cur->sb.minimum != w->sb.minimum ||
1426 cur->sb.maximum != w->sb.maximum ||
1427 cur->sb.sliderSize != w->sb.sliderSize ||
1428 cur->sb.value != w->sb.value ||
1429 cur->sb.pageIncrement != w->sb.pageIncrement ||
1430 cur->sb.increment != w->sb.increment) {
1432 if (XtIsRealized((Widget) w)) {
1433 seg_pixel_sizes(w, &w->sb.above, &w->sb.ss,
1435 draw_slider(w, w->sb.above, w->sb.ss, w->sb.below);
1439 if (w->sb.shadowThickness > 5)
1440 w->sb.shadowThickness = 5;
1442 return do_redisplay;
1446 XlwScrollBarGetValues(Widget widget, int *value, int *sliderSize,
1447 int *increment, int *pageIncrement)
1449 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1451 if (w && XtClass((Widget) w) == xlwScrollBarWidgetClass) {
1453 *value = w->sb.value;
1455 *sliderSize = w->sb.sliderSize;
1457 *increment = w->sb.increment;
1459 *pageIncrement = w->sb.pageIncrement;
1464 XlwScrollBarSetValues(Widget widget, int value, int sliderSize,
1465 int increment, int pageIncrement, Boolean notify)
1467 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1469 if (w && XtClass((Widget) w) == xlwScrollBarWidgetClass &&
1470 (w->sb.value != value ||
1471 w->sb.sliderSize != sliderSize ||
1472 w->sb.increment != increment ||
1473 w->sb.pageIncrement != pageIncrement)) {
1474 int last_value = w->sb.value;
1476 w->sb.value = value;
1477 w->sb.sliderSize = sliderSize;
1478 w->sb.increment = increment;
1479 w->sb.pageIncrement = pageIncrement;
1483 if (XtIsRealized(widget)) {
1484 seg_pixel_sizes(w, &w->sb.above, &w->sb.ss,
1486 draw_slider(w, w->sb.above, w->sb.ss, w->sb.below);
1488 if (w->sb.value != last_value && notify)
1489 call_callbacks(w, XmCR_VALUE_CHANGED,
1490 w->sb.value, 0, NULL);
1495 /*-------------------------- Action Functions ---------------------------*/
1497 static void timer(XtPointer data, XtIntervalId * id)
1499 XlwScrollBarWidget w = (XlwScrollBarWidget) data;
1500 w->sb.timerActive = False;
1502 if (w->sb.armed != ARM_NONE) {
1503 int last_value = w->sb.value;
1506 switch (w->sb.armed) {
1508 decrement_value(w, w->sb.pageIncrement);
1509 reason = XmCR_PAGE_DECREMENT;
1512 increment_value(w, w->sb.pageIncrement);
1513 reason = XmCR_PAGE_INCREMENT;
1516 decrement_value(w, w->sb.increment);
1517 reason = XmCR_DECREMENT;
1520 increment_value(w, w->sb.increment);
1521 reason = XmCR_INCREMENT;
1529 if (last_value != w->sb.value) {
1530 seg_pixel_sizes(w, &w->sb.above, &w->sb.ss,
1532 draw_slider(w, w->sb.above, w->sb.ss, w->sb.below);
1534 call_callbacks(w, reason, w->sb.value, 0, NULL);
1537 XtAppAddTimeOut(XtWidgetToApplicationContext
1539 (unsigned long)w->sb.repeatDelay,
1540 timer, (XtPointer) w);
1541 w->sb.timerActive = True;
1546 static button_where what_button(XlwScrollBarWidget w, int mouse_x, int mouse_y)
1548 int width = widget_w(w);
1549 int height = widget_h(w);
1550 int arrow_height = arrow_h(w);
1552 mouse_x -= widget_x(w);
1553 mouse_y -= widget_y(w);
1555 if (mouse_x < 0 || mouse_x >= width || mouse_y < 0 || mouse_y >= height)
1558 if (w->sb.showArrows) {
1559 if (mouse_y >= (height -= arrow_height))
1560 return BUTTON_DOWN_ARROW;
1562 if (arrow_same_end(w)) {
1563 if (mouse_y >= (height -= arrow_height))
1564 return BUTTON_UP_ARROW;
1565 } else if ((mouse_y -= arrow_height) < 0)
1566 return BUTTON_UP_ARROW;
1569 if ((mouse_y -= w->sb.above) < 0)
1570 return BUTTON_TROUGH_ABOVE;
1572 if ((mouse_y -= w->sb.ss) < 0)
1573 return BUTTON_SLIDER;
1575 return BUTTON_TROUGH_BELOW;
1579 Select(Widget widget, XEvent * event, String * parms, Cardinal * num_parms)
1581 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1582 button_where sb_button;
1584 int mouse_x = event_x(w, event);
1585 int mouse_y = event_y(w, event);
1587 int last_value = w->sb.savedValue = w->sb.value;
1588 int reason = XmCR_NONE;
1590 XtGrabKeyboard((Widget) w, False, GrabModeAsync, GrabModeAsync,
1591 event->xbutton.time);
1593 sb_button = what_button(w, mouse_x, mouse_y);
1595 if (w->sb.forced_scroll != FORCED_SCROLL_NONE) {
1596 switch (sb_button) {
1597 case BUTTON_TROUGH_ABOVE:
1598 case BUTTON_TROUGH_BELOW:
1600 sb_button = BUTTON_NONE; /* cause next switch to fall through */
1601 if (w->sb.forced_scroll == FORCED_SCROLL_UPLEFT) {
1602 decrement_value(w, w->sb.pageIncrement);
1603 w->sb.armed = ARM_PAGEUP;
1604 reason = XmCR_PAGE_DECREMENT;
1606 } else if (w->sb.forced_scroll ==
1607 FORCED_SCROLL_DOWNRIGHT) {
1608 increment_value(w, w->sb.pageIncrement);
1609 w->sb.armed = ARM_PAGEDOWN;
1610 reason = XmCR_PAGE_INCREMENT;
1619 switch (sb_button) {
1620 case BUTTON_TROUGH_ABOVE:
1621 decrement_value(w, w->sb.pageIncrement);
1622 w->sb.armed = ARM_PAGEUP;
1623 reason = XmCR_PAGE_DECREMENT;
1625 case BUTTON_TROUGH_BELOW:
1626 increment_value(w, w->sb.pageIncrement);
1627 w->sb.armed = ARM_PAGEDOWN;
1628 reason = XmCR_PAGE_INCREMENT;
1631 w->sb.lastY = mouse_y;
1632 w->sb.armed = ARM_SLIDER;
1633 draw_slider(w, w->sb.above, w->sb.ss, w->sb.below);
1635 case BUTTON_UP_ARROW:
1636 if (event->xbutton.state & ControlMask) {
1637 w->sb.value = w->sb.minimum;
1638 reason = XmCR_TO_TOP;
1640 decrement_value(w, w->sb.increment);
1641 reason = XmCR_DECREMENT;
1643 w->sb.armed = ARM_UP;
1644 redraw_up_arrow(w, True, False);
1646 case BUTTON_DOWN_ARROW:
1647 if (event->xbutton.state & ControlMask) {
1648 w->sb.value = w->sb.maximum;
1649 reason = XmCR_TO_BOTTOM;
1651 increment_value(w, w->sb.increment);
1652 reason = XmCR_INCREMENT;
1654 w->sb.armed = ARM_DOWN;
1655 redraw_down_arrow(w, True, False);
1663 if (last_value != w->sb.value) {
1664 seg_pixel_sizes(w, &w->sb.above, &w->sb.ss, &w->sb.below);
1665 draw_slider(w, w->sb.above, w->sb.ss, w->sb.below);
1667 call_callbacks(w, reason, w->sb.value, mouse_y, event);
1669 if (w->sb.timerActive)
1670 XtRemoveTimeOut(w->sb.timerId);
1673 XtAppAddTimeOut(XtWidgetToApplicationContext((Widget) w),
1674 (unsigned long)w->sb.initialDelay,
1675 timer, (XtPointer) w);
1676 w->sb.timerActive = True;
1683 PageDownOrRight(Widget widget, XEvent * event, String * parms,
1684 Cardinal * num_parms)
1686 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1687 w->sb.forced_scroll = FORCED_SCROLL_DOWNRIGHT;
1688 Select(widget, event, parms, num_parms);
1689 w->sb.forced_scroll = FORCED_SCROLL_NONE;
1693 PageUpOrLeft(Widget widget, XEvent * event, String * parms,
1694 Cardinal * num_parms)
1696 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1697 w->sb.forced_scroll = FORCED_SCROLL_UPLEFT;
1698 Select(widget, event, parms, num_parms);
1699 w->sb.forced_scroll = FORCED_SCROLL_NONE;
1703 Drag(Widget widget, XEvent * event, String * parms, Cardinal * num_parms)
1705 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1707 if (w->sb.armed == ARM_SLIDER) {
1708 int mouse_y = event_y(w, event);
1709 int diff = mouse_y - w->sb.lastY;
1711 if (diff < -(w->sb.above)) { /* up */
1712 mouse_y -= (diff + w->sb.above);
1713 diff = -(w->sb.above);
1714 } else if (diff > w->sb.below) { /* down */
1715 mouse_y -= (diff - w->sb.below);
1720 w->sb.above += diff;
1721 w->sb.below -= diff;
1723 draw_slider(w, w->sb.above, w->sb.ss, w->sb.below);
1725 w->sb.lastY = mouse_y;
1727 w->sb.value = value_from_pixel(w, w->sb.above);
1731 call_callbacks(w, XmCR_DRAG, w->sb.value,
1732 event_y(w, event), event);
1739 Release(Widget widget, XEvent * event, String * parms, Cardinal * num_parms)
1741 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1743 switch (w->sb.armed) {
1745 call_callbacks(w, XmCR_VALUE_CHANGED, w->sb.value,
1746 event_y(w, event), event);
1747 w->sb.armed = ARM_NONE;
1748 draw_slider(w, w->sb.above, w->sb.ss, w->sb.below);
1751 redraw_up_arrow(w, False, False);
1754 redraw_down_arrow(w, False, False);
1760 XtUngrabKeyboard((Widget) w, event->xbutton.time);
1762 w->sb.armed = ARM_NONE;
1766 Jump(Widget widget, XEvent * event, String * parms, Cardinal * num_parms)
1768 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1771 int mouse_x = event_x(w, event);
1772 int mouse_y = event_y(w, event);
1774 int scroll_region_y = widget_y(w);
1775 int scroll_region_h = widget_h(w);
1777 if (w->sb.showArrows) {
1778 int arrow_height = arrow_h(w);
1779 scroll_region_h -= 2 * arrow_height;
1780 if (!arrow_same_end(w))
1781 scroll_region_y += arrow_height;
1784 XtGrabKeyboard((Widget) w, False, GrabModeAsync, GrabModeAsync,
1785 event->xbutton.time);
1787 switch (what_button(w, mouse_x, mouse_y)) {
1788 case BUTTON_TROUGH_ABOVE:
1789 case BUTTON_TROUGH_BELOW:
1791 w->sb.savedValue = w->sb.value;
1793 last_value = w->sb.value;
1795 w->sb.above = mouse_y - (w->sb.ss / 2) - scroll_region_y;
1796 if (w->sb.above < 0)
1798 else if (w->sb.above + w->sb.ss > scroll_region_h)
1799 w->sb.above = scroll_region_h - w->sb.ss;
1801 w->sb.below = scroll_region_h - w->sb.ss - w->sb.above;
1803 w->sb.armed = ARM_SLIDER;
1804 draw_slider(w, w->sb.above, w->sb.ss, w->sb.below);
1806 w->sb.value = value_from_pixel(w, w->sb.above);
1810 w->sb.lastY = mouse_y;
1812 if (w->sb.value != last_value)
1813 call_callbacks(w, XmCR_DRAG, w->sb.value, mouse_y,
1824 Abort(Widget widget, XEvent * event, String * parms, Cardinal * num_parms)
1826 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1828 if (w->sb.armed != ARM_NONE) {
1829 if (w->sb.value != w->sb.savedValue) {
1830 w->sb.value = w->sb.savedValue;
1832 seg_pixel_sizes(w, &w->sb.above, &w->sb.ss,
1834 draw_slider(w, w->sb.above, w->sb.ss, w->sb.below);
1836 call_callbacks(w, XmCR_VALUE_CHANGED, w->sb.value,
1837 event_y(w, event), event);
1840 switch (w->sb.armed) {
1842 redraw_up_arrow(w, False, False);
1845 redraw_down_arrow(w, False, False);
1847 default:; /* Do nothing */
1850 w->sb.armed = ARM_NONE;
1852 XtUngrabKeyboard((Widget) w, event->xbutton.time);