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 and/or
8 modify it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 The Lucid Widget Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with XEmacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* Created by Douglas Keller <dkeller@vnet.ibm.com> */
23 /* Lots of hacking by Martin Buchholz */
26 * Athena-style scrollbar button bindings added on Sun Dec 24 22:03:57 1995
27 * by Jonathan Stigelman <Stig@hackvan.com>... Ho ho ho!
29 * To use them, put this resource in your .Xdefaults
31 * Emacs*XlwScrollBar.translations: #override \n\
32 * <Btn1Down>: PageDownOrRight() \n\
33 * <Btn3Down>: PageUpOrLeft()
38 * Resources Supported:
43 * XmNbottomShadowColor
44 * XmNbottomShadowPixmap
57 * XmNvalueChangedCallback
58 * XmNincrementCallback
59 * XmNdecrementCallback
60 * XmNpageIncrementCallback
61 * XmNpageDecrementCallback
66 * XmNsliderStyle - values can be: "plain" or "dimple"
67 * XmNarrowPosition - values can be: "opposite" or "same"
76 #include <X11/IntrinsicP.h>
77 #include <X11/StringDefs.h>
78 #include <X11/bitmaps/gray>
80 #include "xlwscrollbarP.h"
81 #include "xlwscrollbar.h"
83 #ifdef USE_DEBUG_MALLOC
89 #define MINL(x,y) ((((unsigned long) (x)) < ((unsigned long) (y))) \
90 ? ((unsigned long) (x)) : ((unsigned long) (y)))
92 #define VERT(w) ((w)->sb.orientation == XmVERTICAL)
112 /*-------------------------- Resources ----------------------------------*/
113 #define offset(field) XtOffset(XlwScrollBarWidget, field)
115 static XtResource resources[] = {
116 { XmNforeground, XmCForeground, XtRPixel, sizeof(Pixel),
117 offset(sb.foreground), XtRImmediate, (XtPointer) XtDefaultForeground },
119 { XmNtopShadowColor, XmCTopShadowColor, XtRPixel,
120 sizeof(Pixel), offset(sb.topShadowColor), XtRImmediate, (XtPointer) ~0 },
121 { XmNbottomShadowColor, XmCBottomShadowColor, XtRPixel,
122 sizeof(Pixel), offset(sb.bottomShadowColor), XtRImmediate,
125 { XmNtopShadowPixmap, XmCTopShadowPixmap, XtRPixmap,
126 sizeof (Pixmap), offset(sb.topShadowPixmap), XtRImmediate,
128 { XmNbottomShadowPixmap, XmCBottomShadowPixmap,
129 XtRPixmap, sizeof (Pixmap), offset(sb.bottomShadowPixmap),
130 XtRImmediate, (XtPointer)None},
132 { XmNtroughColor, XmCTroughColor, XtRPixel, sizeof(Pixel),
133 offset(sb.troughColor), XtRImmediate, (XtPointer)~0 },
135 { XmNshadowThickness, XmCShadowThickness, XtRInt,
136 sizeof(int), offset(sb.shadowThickness), XtRImmediate, (XtPointer)2 },
138 { XmNborderWidth, XmCBorderWidth, XtRDimension,
139 sizeof(Dimension), offset(core.border_width), XtRImmediate,
142 { XmNshowArrows, XmCShowArrows, XtRBoolean,
143 sizeof(Boolean), offset(sb.showArrows), XtRImmediate, (XtPointer)True },
145 { XmNinitialDelay, XmCInitialDelay, XtRInt, sizeof(int),
146 offset(sb.initialDelay), XtRImmediate, (XtPointer) 250 },
147 { XmNrepeatDelay, XmCRepeatDelay, XtRInt, sizeof(int),
148 offset(sb.repeatDelay), XtRImmediate, (XtPointer) 50 },
150 { XmNorientation, XmCOrientation, XtROrientation,
151 sizeof(unsigned char), offset(sb.orientation), XtRImmediate,
152 (XtPointer) XmVERTICAL },
154 { XmNminimum, XmCMinimum, XtRInt, sizeof(int),
155 offset(sb.minimum), XtRImmediate, (XtPointer) 0},
156 { XmNmaximum, XmCMaximum, XtRInt, sizeof(int),
157 offset(sb.maximum), XtRImmediate, (XtPointer) 100},
158 { XmNvalue, XmCValue, XtRInt, sizeof(int),
159 offset(sb.value), XtRImmediate, (XtPointer) 0},
160 { XmNsliderSize, XmCSliderSize, XtRInt, sizeof(int),
161 offset(sb.sliderSize), XtRImmediate, (XtPointer) 10},
162 { XmNincrement, XmCIncrement, XtRInt, sizeof(int),
163 offset(sb.increment), XtRImmediate, (XtPointer) 1},
164 { XmNpageIncrement, XmCPageIncrement, XtRInt, sizeof(int),
165 offset(sb.pageIncrement), XtRImmediate, (XtPointer) 10},
167 { XmNvalueChangedCallback, XmCValueChangedCallback,
168 XtRCallback, sizeof(XtPointer), offset(sb.valueChangedCBL),
170 { XmNincrementCallback, XmCIncrementCallback,
171 XtRCallback, sizeof(XtPointer), offset(sb.incrementCBL),
173 { XmNdecrementCallback, XmCDecrementCallback,
174 XtRCallback, sizeof(XtPointer), offset(sb.decrementCBL),
176 { XmNpageIncrementCallback, XmCPageIncrementCallback,
177 XtRCallback, sizeof(XtPointer), offset(sb.pageIncrementCBL),
179 { XmNpageDecrementCallback, XmCPageDecrementCallback,
180 XtRCallback, sizeof(XtPointer), offset(sb.pageDecrementCBL),
182 { XmNtoTopCallback, XmCToTopCallback, XtRCallback,
183 sizeof(XtPointer), offset(sb.toTopCBL), XtRCallback, NULL},
184 { XmNtoBottomCallback, XmCToBottomCallback, XtRCallback,
185 sizeof(XtPointer), offset(sb.toBottomCBL), XtRCallback, NULL},
186 { XmNdragCallback, XmCDragCallback, XtRCallback,
187 sizeof(XtPointer), offset(sb.dragCBL), XtRCallback, NULL},
189 /* "knob" is obsolete; use "slider" instead. */
190 { XmNsliderStyle, XmCSliderStyle, XtRString, sizeof(char *),
191 offset(sb.sliderStyle), XtRImmediate, NULL},
192 { XmNknobStyle, XmCKnobStyle, XtRString, sizeof(char *),
193 offset(sb.knobStyle), XtRImmediate, NULL},
195 { XmNarrowPosition, XmCArrowPosition, XtRString, sizeof(char *),
196 offset(sb.arrowPosition), XtRImmediate, NULL},
199 /*-------------------------- Prototypes ---------------------------------*/
202 typedef void Action(Widget w, XEvent *event, String *parms, Cardinal *num_parms);
203 static Action Select, PageUpOrLeft, PageDownOrRight, Drag, Release, Jump, Abort;
206 static void Initialize(Widget treq, Widget tnew, ArgList args, Cardinal *num_args);
207 static Boolean SetValues(Widget current, Widget request, Widget nw, ArgList args, Cardinal *num_args);
208 static void Destroy(Widget widget);
209 static void Redisplay(Widget widget, XEvent *event, Region region);
210 static void Resize(Widget widget);
211 static void Realize(Widget widget, XtValueMask *valuemask, XSetWindowAttributes *attr);
215 /*-------------------------- Actions Table ------------------------------*/
216 static XtActionsRec actions[] =
219 {"PageDownOrRight", PageDownOrRight},
220 {"PageUpOrLeft", PageUpOrLeft},
222 {"Release", Release},
227 /*--------------------- Default Translation Table -----------------------*/
228 static char default_translations[] =
229 "<Btn1Down>: Select()\n"
230 "<Btn1Motion>: Drag()\n"
231 "<Btn1Up>: Release()\n"
232 "<Btn2Down>: Jump()\n"
233 "<Btn2Motion>: Drag()\n"
234 "<Btn2Up>: Release()\n"
235 "<Key>Delete: Abort()"
238 /*------------------- Class record initialization -----------------------*/
239 XlwScrollBarClassRec xlwScrollBarClassRec = {
240 /* core_class fields */
242 /* superclass */ (WidgetClass) &coreClassRec,
243 /* class_name */ "XlwScrollBar",
244 /* widget_size */ sizeof(XlwScrollBarRec),
245 /* class_initialize */ NULL,
246 /* class_part_init */ NULL,
247 /* class_inited */ False,
248 /* initialize */ Initialize,
249 /* initialize_hook */ NULL,
250 /* realize */ Realize,
251 /* actions */ actions,
252 /* num_actions */ XtNumber(actions),
253 /* resources */ resources,
254 /* num_resources */ XtNumber(resources),
255 /* xrm_class */ NULLQUARK,
256 /* compress_motion */ True,
257 /* compress_exposure */ XtExposeCompressMultiple,
258 /* compress_enterleave */ True,
259 /* visible_interest */ False,
260 /* destroy */ Destroy,
262 /* expose */ Redisplay,
263 /* set_values */ SetValues,
264 /* set_values_hook */ NULL,
265 /* set_values_almost */ XtInheritSetValuesAlmost,
266 /* get_values_hook */ NULL,
267 /* accept_focus */ NULL,
268 /* version */ XtVersionDontCheck,
269 /* callback_private */ NULL,
270 /* tm_table */ default_translations,
271 /* query_geometry */ NULL,
273 /* scrollbar_class fields */
279 WidgetClass xlwScrollBarWidgetClass = (WidgetClass) &xlwScrollBarClassRec;
281 /*-------------------------- Debug Functions ----------------------------*/
285 myXClearArea(Display *dpy, Drawable d, int x, int y, int w, int h,
286 Boolean exp, XlwScrollBarWidget widget)
288 XFillRectangle (dpy, d, widget->sb.topShadowGC, x, y, w, h);
291 XClearArea (dpy, d, x, y, w, h, exp);
294 #define XClearArea(dpy,win,x,y,width,height,exp) myXClearArea(dpy,win,x,y,width,height,exp,w)
299 check(XlwScrollBarWidget w)
301 int height = widget_h (w);
302 if (w->sb.showArrows)
303 height -= (2 * arrow_h (w));
305 if ((w->sb.above + w->sb.ss + w->sb.below > height) ||
306 (w->sb.value < w->sb.minimum) ||
307 (w->sb.value > w->sb.maximum - w->sb.sliderSize))
309 printf("above=%d ss=%d below=%d height=%d\n",
310 w->sb.above, w->sb.ss, w->sb.below, height);
311 printf("value=%d min=%d max=%d ss=%d max-ss=%d\n",
312 w->sb.value, w->sb.minimum, w->sb.maximum,
313 w->sb.sliderSize, w->sb.maximum - w->sb.sliderSize);
318 # define CHECK(w) check(w)
323 /*-------------------------- Static functions ---------------------------*/
326 call_callbacks (XlwScrollBarWidget w, int reason,
327 int value, int pixel, XEvent *event)
329 XlwScrollBarCallbackStruct cbs;
330 Boolean called_anything;
337 called_anything = False;
341 case XmCR_VALUE_CHANGED:
342 XtCallCallbackList ((Widget) w, w->sb.valueChangedCBL, &cbs);
343 called_anything = True;
346 if (w->sb.incrementCBL)
348 XtCallCallbackList ((Widget) w, w->sb.incrementCBL, &cbs);
349 called_anything = True;
353 if (w->sb.decrementCBL)
355 XtCallCallbackList ((Widget) w, w->sb.decrementCBL, &cbs);
356 called_anything = True;
359 case XmCR_PAGE_INCREMENT:
360 if (w->sb.incrementCBL)
362 XtCallCallbackList ((Widget) w, w->sb.pageIncrementCBL, &cbs);
363 called_anything = True;
366 case XmCR_PAGE_DECREMENT:
367 if (w->sb.decrementCBL)
369 XtCallCallbackList ((Widget) w, w->sb.pageDecrementCBL, &cbs);
370 called_anything = True;
376 XtCallCallbackList ((Widget) w, w->sb.toTopCBL, &cbs);
377 called_anything = True;
381 if (w->sb.toBottomCBL)
383 XtCallCallbackList ((Widget) w, w->sb.toBottomCBL, &cbs);
384 called_anything = True;
390 XtCallCallbackList ((Widget) w, w->sb.dragCBL, &cbs);
392 called_anything = True; /* Special Case */
396 if (!called_anything)
398 cbs.reason = XmCR_VALUE_CHANGED;
399 XtCallCallbackList ((Widget) w, w->sb.valueChangedCBL, &cbs);
403 /* Widget sizes minus the shadow and highlight area */
406 widget_x (XlwScrollBarWidget w)
408 return w->sb.shadowThickness;
412 widget_y (XlwScrollBarWidget w)
414 return w->sb.shadowThickness;
418 widget_w (XlwScrollBarWidget w)
420 int x = w->sb.shadowThickness;
421 int width = (VERT (w) ? w->core.width : w->core.height) - (2 * x);
422 return width > 1 ? width : 1;
426 widget_h (XlwScrollBarWidget w)
428 int y = w->sb.shadowThickness;
429 int height = (VERT (w) ? w->core.height : w->core.width) - (2 * y);
431 return height > 1 ? height : 1;
435 arrow_h (XlwScrollBarWidget w)
437 int width = widget_w (w);
438 int minimum_size = ((widget_h (w) - SS_MIN) / 2) - 1;
439 return minimum_size < width ? minimum_size : width;
443 event_x (XlwScrollBarWidget w, XEvent *event)
445 return VERT (w) ? event->xbutton.x : event->xbutton.y;
449 event_y (XlwScrollBarWidget w, XEvent *event)
451 return VERT (w) ? event->xbutton.y : event->xbutton.x;
454 /* Safe addition and subtraction */
456 increment_value (XlwScrollBarWidget w, int diff)
458 w->sb.value = w->sb.maximum - diff < w->sb.value ?
464 decrement_value (XlwScrollBarWidget w, int diff)
466 w->sb.value = w->sb.minimum + diff > w->sb.value ?
472 slider_style (XlwScrollBarWidget w)
474 return (w->sb.sliderStyle ? w->sb.sliderStyle[0] == 'd' :
475 w->sb.knobStyle ? w->sb.knobStyle[0] == 'd' :
482 arrow_same_end (XlwScrollBarWidget w)
484 return w->sb.arrowPosition && w->sb.arrowPosition[0] == 's' ? True : False;
487 /*-------------------------- GC and Pixel allocation --------------------*/
488 #ifndef XmUNSPECIFIED_PIXMAP
489 #define XmUNSPECIFIED_PIXMAP 2
493 get_gc (XlwScrollBarWidget w, Pixel fg, Pixel bg, Pixmap pm)
498 if (pm == w->sb.grayPixmap)
500 /* If we're using the gray pixmap, guarantee white on black ...
501 * otherwise, we could end up with something odd like grey on white
502 * when we're on a color display that ran out of color cells
505 fg = WhitePixelOfScreen (DefaultScreenOfDisplay (XtDisplay (w)));
506 bg = BlackPixelOfScreen (DefaultScreenOfDisplay (XtDisplay (w)));
509 values.foreground = fg;
510 values.background = bg;
511 values.fill_style = FillOpaqueStippled;
513 /* mask = GCForeground | GCBackground |
514 (pm == None ? 0 : GCStipple | GCFillStyle); gtb */
515 if (pm != None && pm != 0 && pm != XmUNSPECIFIED_PIXMAP)
518 values.stipple = None;
519 mask = GCForeground | GCBackground |
520 (values.stipple == None ? 0 : GCStipple | GCFillStyle);
522 return XtGetGC((Widget) w, mask, &values);
525 /* Replacement for XAllocColor() that tries to return the nearest
526 available color if the colormap is full. From FSF Emacs. */
529 allocate_nearest_color (Display *display, Colormap screen_colormap,
532 int status = XAllocColor (display, screen_colormap, color_def);
537 /* If we got to this point, the colormap is full, so we're
538 going to try to get the next closest color.
539 The algorithm used is a least-squares matching, which is
540 what X uses for closest color matching with StaticColor visuals. */
543 unsigned long nearest_delta = ULONG_MAX;
545 int no_cells = XDisplayCells (display, XDefaultScreen (display));
546 /* Don't use alloca here because lwlib doesn't have the
547 necessary configuration information that src does. */
548 XColor *cells = (XColor *) malloc (sizeof (XColor) * no_cells);
550 for (x = 0; x < no_cells; x++)
553 XQueryColors (display, screen_colormap, cells, no_cells);
555 for (nearest = 0, x = 0; x < no_cells; x++)
557 long dred = (color_def->red >> 8) - (cells[x].red >> 8);
558 long dgreen = (color_def->green >> 8) - (cells[x].green >> 8);
559 long dblue = (color_def->blue >> 8) - (cells[x].blue >> 8);
560 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
562 if (delta < nearest_delta)
565 nearest_delta = delta;
568 color_def->red = cells[nearest].red;
569 color_def->green = cells[nearest].green;
570 color_def->blue = cells[nearest].blue;
572 return XAllocColor (display, screen_colormap, color_def);
577 make_shadow_pixels (XlwScrollBarWidget w)
579 Display *dpy = XtDisplay((Widget) w);
580 Colormap cmap = w->core.colormap;
582 int top_frobbed, bottom_frobbed;
585 top_frobbed = bottom_frobbed = 0;
587 bg = w->core.background_pixel;
588 fg = w->sb.foreground;
590 if (w->sb.topShadowColor == (Pixel)~0) w->sb.topShadowColor = bg;
591 if (w->sb.bottomShadowColor == (Pixel)~0) w->sb.bottomShadowColor = fg;
593 if (w->sb.topShadowColor == bg || w->sb.topShadowColor == fg)
596 XQueryColor (dpy, cmap, &topc);
597 /* don't overflow/wrap! */
598 topc.red = MINL(65535, topc.red * 1.2);
599 topc.green = MINL(65535, topc.green * 1.2);
600 topc.blue = MINL(65535, topc.blue * 1.2);
601 if (allocate_nearest_color (dpy, cmap, &topc))
603 if (topc.pixel == bg)
605 XFreeColors (dpy, cmap, &topc.pixel, 1, 0);
606 topc.red = MINL(65535, topc.red + 0x8000);
607 topc.green = MINL(65535, topc.green + 0x8000);
608 topc.blue = MINL(65535, topc.blue + 0x8000);
609 if (allocate_nearest_color (dpy, cmap, &topc))
611 w->sb.topShadowColor = topc.pixel;
616 w->sb.topShadowColor = topc.pixel;
623 if (w->sb.bottomShadowColor == fg || w->sb.bottomShadowColor == bg)
626 XQueryColor (dpy, cmap, &botc);
627 botc.red = (botc.red * 3) / 5;
628 botc.green = (botc.green * 3) / 5;
629 botc.blue = (botc.blue * 3) / 5;
630 if (allocate_nearest_color (dpy, cmap, &botc))
632 if (botc.pixel == bg)
634 XFreeColors (dpy, cmap, &botc.pixel, 1, 0);
635 botc.red = MINL(65535, botc.red + 0x4000);
636 botc.green = MINL(65535, botc.green + 0x4000);
637 botc.blue = MINL(65535, botc.blue + 0x4000);
638 if (allocate_nearest_color (dpy, cmap, &botc))
640 w->sb.bottomShadowColor = botc.pixel;
645 w->sb.bottomShadowColor = botc.pixel;
651 if (top_frobbed && bottom_frobbed)
653 int top_avg = ((topc.red / 3) + (topc.green / 3) + (topc.blue / 3));
654 int bot_avg = ((botc.red / 3) + (botc.green / 3) + (botc.blue / 3));
655 if (bot_avg > top_avg)
657 Pixel tmp = w->sb.topShadowColor;
658 w->sb.topShadowColor = w->sb.bottomShadowColor;
659 w->sb.bottomShadowColor = tmp;
661 else if (topc.pixel == botc.pixel)
663 if (botc.pixel == bg)
664 w->sb.topShadowColor = bg;
666 w->sb.bottomShadowColor = fg;
670 if (w->sb.topShadowColor == w->core.background_pixel ||
671 w->sb.bottomShadowColor == w->core.background_pixel)
673 /* Assume we're in mono. This code should be okay even if we're
674 * really in color but just short on color cells -- We want the
675 * following behavior, which has been empirically determined to
676 * work well for all fg/bg combinations in mono: If the trough
677 * and slider are BOTH black, then use a white top shadow and a
678 * grey bottom shadow, otherwise use a grey top shadow and a
679 * black bottom shadow.
682 Pixel white = WhitePixelOfScreen (DefaultScreenOfDisplay (XtDisplay (w)));
683 Pixel black = BlackPixelOfScreen (DefaultScreenOfDisplay (XtDisplay (w)));
685 /* Note: core.background_pixel is the color of the slider ... */
687 if (w->core.background_pixel == black &&
688 w->sb.troughColor == black)
690 w->sb.topShadowColor = white;
691 w->sb.bottomShadowPixmap = w->sb.grayPixmap;
693 w->sb.topShadowPixmap = w->sb.grayPixmap;
694 w->sb.bottomShadowColor = black;
700 make_trough_pixel (XlwScrollBarWidget w)
702 Display *dpy = XtDisplay((Widget) w);
703 Colormap cmap = w->core.colormap;
706 if (w->sb.troughColor == (Pixel)~0) w->sb.troughColor = w->core.background_pixel;
708 if (w->sb.troughColor == w->core.background_pixel)
710 troughC.pixel = w->core.background_pixel;
711 XQueryColor (dpy, cmap, &troughC);
712 troughC.red = (troughC.red * 4) / 5;
713 troughC.green = (troughC.green * 4) / 5;
714 troughC.blue = (troughC.blue * 4) / 5;
715 if (allocate_nearest_color (dpy, cmap, &troughC))
716 w->sb.troughColor = troughC.pixel;
720 /*-------------------------- Draw 3D Border -----------------------------*/
722 draw_shadows (Display *dpy, Drawable d, GC shine_gc, GC shadow_gc,
723 int x, int y, int width, int height, int shadowT)
725 XSegment shine[10], shadow[10];
728 if (shadowT > (width / 2)) shadowT = (width / 2);
729 if (shadowT > (height / 2)) shadowT = (height / 2);
730 if (shadowT <= 0) return;
732 for (i = 0; i < shadowT; i++)
736 shine[i].y2 = shine[i].y1 = y + i;
737 shine[i].x2 = x + width - i - 1;
739 shine[i + shadowT].x2 = shine[i + shadowT].x1 = x + i;
740 shine[i + shadowT].y1 = y + shadowT;
741 shine[i + shadowT].y2 = y + height - i - 1;
743 /* Bottom segments */
744 shadow[i].x1 = x + i;
745 shadow[i].y2 = shadow[i].y1 = y + height - i - 1;
746 shadow[i].x2 = x + width - 1 ;
748 shadow[i + shadowT].x2 = shadow[i + shadowT].x1 = x + width - i - 1;
749 shadow[i + shadowT].y1 = y + i + 1;
750 shadow[i + shadowT].y2 = y + height - 1 ;
753 XDrawSegments (dpy, d, shine_gc, shine, shadowT * 2);
754 XDrawSegments (dpy, d, shadow_gc, shadow, shadowT * 2);
757 /*------------------ Draw 3D Arrows: left, up, down, right --------------*/
759 make_vert_seg (XSegment *seg, int x1, int y1, int x2, int y2, int shadowT)
763 for (i=0; i<shadowT; i++, seg++)
774 make_hor_seg (XSegment *seg, int x1, int y1, int x2, int y2, int shadowT)
778 for (i=0; i<shadowT; i++, seg++)
789 draw_arrow_up (Display *dpy, Drawable win, GC bgGC, GC shineGC, GC shadowGC,
790 int x, int y, int width, int height, int shadowT)
792 XSegment shine[10], shadow[10];
798 if (shadowT > (width / 2)) shadowT = (width / 2);
799 if (shadowT > (height / 2)) shadowT = (height / 2);
800 if (shadowT < 0) shadowT = 0;
803 make_vert_seg (shine,
804 x, y + height - shadowT - 1,
805 x + mid, y, shadowT);
807 make_vert_seg (shadow,
808 x, y + height - shadowT - 1,
809 x + width - 1, y + height - shadowT - 1, shadowT);
810 make_vert_seg (shadow + shadowT,
812 x + width - 1, y + height - shadowT - 1, shadowT);
815 triangle[0].y = y + height - 1;
816 triangle[1].x = x + mid;
818 triangle[2].x = x + width - 1;
819 triangle[2].y = y + height - 1;
821 XFillPolygon (dpy, win, bgGC, triangle, 3, Convex, ArcChord);
823 XDrawSegments (dpy, win, shadowGC, shadow, shadowT * 2);
824 XDrawSegments (dpy, win, shineGC, shine, shadowT);
828 draw_arrow_left (Display *dpy, Drawable win, GC bgGC, GC shineGC, GC shadowGC,
829 int x, int y, int width, int height, int shadowT)
831 XSegment shine[10], shadow[10];
836 if (shadowT > (width / 2)) shadowT = (width / 2);
837 if (shadowT > (height / 2)) shadowT = (height / 2);
838 if (shadowT < 0) shadowT = 0;
843 x + width - shadowT - 1, y, shadowT);
845 make_hor_seg (shadow,
847 x + width - shadowT - 1, y + height - 1, shadowT);
848 make_hor_seg (shadow + shadowT,
849 x + width - shadowT - 1, y,
850 x + width - shadowT - 1, y + height - 1, shadowT);
852 triangle[0].x = x + width - 1;
853 triangle[0].y = y + height - 1;
855 triangle[1].y = y + mid;
856 triangle[2].x = x + width - 1;
859 XFillPolygon (dpy, win, bgGC, triangle, 3, Convex, ArcChord);
861 XDrawSegments (dpy, win, shadowGC, shadow, shadowT * 2);
862 XDrawSegments (dpy, win, shineGC, shine, shadowT);
866 draw_arrow_down (Display *dpy, Drawable win, GC bgGC, GC shineGC, GC shadowGC,
867 int x, int y, int width, int height, int shadowT)
869 XSegment shine[10], shadow[10];
875 if (shadowT > (width / 2)) shadowT = (width / 2);
876 if (shadowT > (height / 2)) shadowT = (height / 2);
877 if (shadowT < 0) shadowT = 0;
880 make_vert_seg (shine,
882 x + mid, y + height - shadowT - 1, shadowT);
883 make_vert_seg (shine + shadowT,
885 x + width - 1, y, shadowT);
887 make_vert_seg (shadow,
889 x + mid, y + height - shadowT - 1, shadowT);
893 triangle[1].x = x + mid;
894 triangle[1].y = y + height - 1;
895 triangle[2].x = x + width - 1;
898 XFillPolygon (dpy, win, bgGC, triangle, 3, Convex, ArcChord);
900 XDrawSegments (dpy, win, shadowGC, shadow, shadowT);
901 XDrawSegments (dpy, win, shineGC, shine, shadowT * 2);
905 draw_arrow_right (Display *dpy, Drawable win, GC bgGC, GC shineGC, GC shadowGC,
906 int x, int y, int width, int height, int shadowT)
908 XSegment shine[10], shadow[10];
914 if (shadowT > (width / 2)) shadowT = (width / 2);
915 if (shadowT > (height / 2)) shadowT = (height / 2);
916 if (shadowT < 0) shadowT = 0;
921 x + width - shadowT - 1, y + mid, shadowT);
922 make_hor_seg (shine + shadowT,
924 x, y + height - 1, shadowT);
926 make_hor_seg (shadow,
928 x + width - shadowT - 1, y + mid, shadowT);
930 triangle[0].x = x + 1;
931 triangle[0].y = y + height - 1;
932 triangle[1].x = x + width - 1;
933 triangle[1].y = y + mid;
934 triangle[2].x = x + 1;
937 XFillPolygon (dpy, win, bgGC, triangle, 3, Convex, ArcChord);
939 XDrawSegments (dpy, win, shadowGC, shadow, shadowT);
940 XDrawSegments (dpy, win, shineGC, shine, shadowT * 2);
944 draw_dimple (Display *dpy, Drawable win, GC shine, GC shadow,
945 int x, int y, int width, int height)
947 XDrawArc (dpy, win, shine, x, y, width, height, 46*64, 180*64);
948 XDrawArc (dpy, win, shadow, x, y, width, height, 45*64, -179*64);
951 /*------- Scrollbar values -> pixels, pixels -> scrollbar values --------*/
954 seg_pixel_sizes (XlwScrollBarWidget w, int *above_return,
955 int *ss_return, int *below_return)
957 float total, height, fuz;
958 int value, above, ss, below;
960 height = widget_h (w);
961 if (w->sb.showArrows) height -= (2 * arrow_h (w));
963 value = w->sb.value - w->sb.minimum;
965 total = w->sb.maximum - w->sb.minimum;
968 ss = (int) ((height * w->sb.sliderSize + fuz) / total);
969 above = (int) ((height * value + fuz) / total);
970 below = (int) ((height) - (ss + above));
972 /* Don't let slider get smaller than SS_MIN */
975 /* add a percent amount for integer rounding */
976 float tmp = (((float) (SS_MIN - ss) * (float) value) / total) + 0.5;
980 below = (int) ((height) - (ss + above));
985 below = (int) (height - ss);
989 above = (int) (height - ss);
1000 *above_return = above;
1002 *below_return = below;
1008 verify_values (XlwScrollBarWidget w)
1010 int total = w->sb.maximum - w->sb.minimum;
1012 if (w->sb.sliderSize > total)
1013 w->sb.sliderSize = total;
1015 if (w->sb.pageIncrement > total)
1016 w->sb.pageIncrement = total;
1018 if (w->sb.increment > total)
1019 w->sb.increment = total;
1021 if (w->sb.value < w->sb.minimum)
1022 w->sb.value = w->sb.minimum;
1024 if (w->sb.value > w->sb.maximum)
1025 w->sb.value = w->sb.maximum;
1027 if (w->sb.sliderSize > w->sb.maximum - w->sb.value)
1028 w->sb.sliderSize = w->sb.maximum - w->sb.value;
1032 value_from_pixel (XlwScrollBarWidget w, int above)
1034 float total, height, fuz;
1037 height = widget_h (w);
1038 if (w->sb.showArrows)
1039 height -= (2 * arrow_h (w));
1041 total = w->sb.maximum - w->sb.minimum;
1044 ss = (int) ((height * w->sb.sliderSize + (total / 2)) / total);
1048 /* add a percent amount for integer rounding */
1049 above += (int) ((((SS_MIN - ss) * above) + fuz) / height);
1053 /* Prevent SIGFPE's that would occur if we don't truncate the value. */
1054 float floatval = w->sb.minimum + ((float)(above * total + fuz) / height);
1055 if (floatval >= (float) INT_MAX)
1057 else if (floatval <= (float) INT_MIN)
1060 value = (int) floatval;
1068 redraw_dimple (XlwScrollBarWidget w, Display *dpy, Window win,
1069 int x, int y, int width, int height)
1071 if (SLIDER_DIMPLE == slider_style (w))
1074 int slider_p = (w->sb.armed == ARM_SLIDER);
1075 GC shine = slider_p ? w->sb.bottomShadowGC : w->sb.topShadowGC;
1076 GC shadow = slider_p ? w->sb.topShadowGC : w->sb.bottomShadowGC;
1077 int shadowT = w->sb.shadowThickness;
1082 height -= 2*shadowT;
1084 size = (width < height ? width : height) * 3 / 4;
1086 if (size%2 != (width < height ? width : height)%2) size--;
1088 DBUG (fprintf (stderr, "%d %d\n",
1089 x + (width / 2) - (size / 2) - 2*shadowT,
1090 width - size - shadowT));
1092 draw_dimple (dpy, win, shine, shadow,
1093 x + (width / 2) - (size / 2),
1094 y + (height / 2) - (size / 2),
1100 draw_slider (XlwScrollBarWidget w, int above, int ss, int below)
1102 Display *dpy = XtDisplay ((Widget) w);
1103 Window win = XtWindow ((Widget) w);
1105 int x = widget_x (w);
1106 int y = widget_y (w);
1107 int width = widget_w (w);
1108 int height = widget_h (w);
1109 int shadowT = w->sb.shadowThickness;
1110 int vert_p = VERT (w);
1112 if (shadowT > (width / 2)) shadowT = (width / 2);
1113 if (shadowT > (height / 2)) shadowT = (height / 2);
1114 if (shadowT < 0) shadowT = 0;
1116 if (w->sb.showArrows && !arrow_same_end (w))
1119 /* trough above slider */
1123 XClearArea (dpy, win, x, y, width, above, False);
1125 XClearArea (dpy, win, y, x, above, width, False);
1131 draw_shadows (dpy, win, w->sb.topShadowGC, w->sb.bottomShadowGC,
1132 x, y + above, width, ss, shadowT);
1133 XFillRectangle (dpy, win, w->sb.backgroundGC,
1134 x+shadowT, y + above + shadowT,
1135 width-2*shadowT, ss-2*shadowT);
1136 redraw_dimple (w, dpy, win, x, y + above, width, ss);
1140 draw_shadows (dpy, win, w->sb.topShadowGC, w->sb.bottomShadowGC,
1141 y + above, x, ss, width, shadowT);
1142 XFillRectangle (dpy, win, w->sb.backgroundGC,
1143 y + above + shadowT, x+shadowT,
1144 ss-2*shadowT, width-2*shadowT);
1145 redraw_dimple (w, dpy, win, y + above, x, ss, width);
1148 /* trough below slider */
1152 XClearArea (dpy, win, x, y + above + ss, width, below, False);
1154 XClearArea (dpy, win, y + above + ss, x, below, width, False);
1161 redraw_up_arrow (XlwScrollBarWidget w, Boolean armed, Boolean clear_behind)
1163 Display *dpy = XtDisplay ((Widget) w);
1164 Window win = XtWindow ((Widget) w);
1166 int x = widget_x (w);
1167 int y = widget_y (w);
1168 int width = widget_w (w);
1169 int height = widget_h (w);
1170 int shadowT = w->sb.shadowThickness;
1171 int arrow_height = arrow_h (w);
1173 GC bg = w->sb.backgroundGC;
1174 GC shine = armed ? w->sb.bottomShadowGC : w->sb.topShadowGC;
1175 GC shadow = armed ? w->sb.topShadowGC : w->sb.bottomShadowGC;
1179 if (arrow_same_end (w))
1180 y += height - 2 * arrow_height;
1182 XClearArea (dpy, win, x, y, width, arrow_height + 1, False);
1183 draw_arrow_up (dpy, win, bg, shine, shadow,
1184 x + (width - arrow_height)/2, y,
1185 arrow_height, arrow_height, shadowT);
1189 if (arrow_same_end (w))
1190 y += height - 2 * arrow_height;
1192 XClearArea (dpy, win, y, x, arrow_height + 1, height, False);
1193 draw_arrow_left (dpy, win, bg, shine, shadow,
1194 y, x + (width - arrow_height)/2,
1195 arrow_height, arrow_height, shadowT);
1200 redraw_down_arrow (XlwScrollBarWidget w, Boolean armed, Boolean clear_behind)
1202 Display *dpy = XtDisplay ((Widget) w);
1203 Window win = XtWindow ((Widget) w);
1205 int x = widget_x (w);
1206 int y = widget_y (w);
1207 int width = widget_w (w);
1208 int height = widget_h (w);
1209 int shadowT = w->sb.shadowThickness;
1210 int arrow_height = arrow_h (w);
1212 GC bg = w->sb.backgroundGC;
1213 GC shine = armed ? w->sb.bottomShadowGC : w->sb.topShadowGC;
1214 GC shadow = armed ? w->sb.topShadowGC : w->sb.bottomShadowGC;
1219 XClearArea (dpy, win, x, y + height - arrow_height, width,
1220 arrow_height + 1, False);
1221 draw_arrow_down (dpy, win, bg, shine, shadow,
1222 x + (width - arrow_height)/2,
1223 y + height - arrow_height + 1,
1224 arrow_height, arrow_height, shadowT);
1229 XClearArea (dpy, win, y + height - arrow_height, x,
1230 arrow_height + 1, height, False);
1231 draw_arrow_right (dpy, win, bg, shine, shadow,
1232 y + height - arrow_height + 1,
1233 x + (width - arrow_height)/2,
1234 arrow_height, arrow_height, shadowT);
1239 redraw_everything (XlwScrollBarWidget w, Region region, Boolean behind_arrows)
1241 Display *dpy = XtDisplay ((Widget) w);
1242 Window win = XtWindow ((Widget) w);
1244 if (w->sb.showArrows)
1248 redraw_up_arrow (w, False, behind_arrows);
1249 redraw_down_arrow (w, False, behind_arrows);
1253 int x = widget_x (w);
1254 int y = widget_y (w);
1255 int width = widget_w (w);
1256 int height = widget_h (w);
1257 int arrow_height = arrow_h (w);
1260 if (arrow_same_end (w))
1263 ay = y + height - arrow_height - arrow_height;
1265 ax = x + height - arrow_height - arrow_height;
1267 if (XRectInRegion (region, ax, ay, width, width))
1268 redraw_up_arrow (w, False, behind_arrows);
1271 ay = y + height - arrow_height;
1273 ax = x + height - arrow_height;
1274 if (XRectInRegion (region, ax, ay, width, width))
1275 redraw_down_arrow (w, False, behind_arrows);
1279 draw_shadows (dpy, win, w->sb.bottomShadowGC, w->sb.topShadowGC, 0, 0,
1280 w->core.width, w->core.height, w->sb.shadowThickness);
1282 draw_slider (w, w->sb.above, w->sb.ss, w->sb.below);
1285 /*-------------------------- Method Functions ---------------------------*/
1288 Initialize (Widget treq, Widget tnew, ArgList args, Cardinal *num_args)
1290 XlwScrollBarWidget request = (XlwScrollBarWidget) treq;
1291 XlwScrollBarWidget w = (XlwScrollBarWidget) tnew;
1292 Display *dpy = XtDisplay ((Widget) w);
1293 Window win = RootWindowOfScreen (DefaultScreenOfDisplay (dpy));
1295 if (request->core.width == 0) w->core.width += (VERT (w) ? 12 : 25);
1296 if (request->core.height == 0) w->core.height += (VERT (w) ? 25 : 12);
1304 w->sb.armed = ARM_NONE;
1305 w->sb.forced_scroll = FORCED_SCROLL_NONE;
1307 if (w->sb.shadowThickness > 5) w->sb.shadowThickness = 5;
1310 XCreatePixmapFromBitmapData (dpy, win, (char *) gray_bits, gray_width,
1311 gray_height, 1, 0, 1);
1313 make_trough_pixel (w);
1315 make_shadow_pixels (w);
1317 w->sb.backgroundGC =
1318 get_gc (w, w->core.background_pixel, w->core.background_pixel, None);
1320 get_gc (w, w->sb.topShadowColor, w->core.background_pixel,
1321 w->sb.topShadowPixmap);
1322 w->sb.bottomShadowGC =
1323 get_gc (w, w->sb.bottomShadowColor, w->core.background_pixel,
1324 w->sb.bottomShadowPixmap);
1326 w->sb.fullRedrawNext = True;
1328 w->sb.timerActive = False;
1332 Destroy (Widget widget)
1334 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1335 Display *dpy = XtDisplay ((Widget) w);
1337 XtReleaseGC (widget, w->sb.bottomShadowGC);
1338 XtReleaseGC (widget, w->sb.topShadowGC);
1339 XtReleaseGC (widget, w->sb.backgroundGC);
1341 XFreePixmap (dpy, w->sb.grayPixmap);
1343 if (w->sb.timerActive)
1345 XtRemoveTimeOut (w->sb.timerId);
1346 w->sb.timerActive = False; /* Should be a no-op, but you never know */
1351 Realize (Widget widget, XtValueMask *valuemask, XSetWindowAttributes *attr)
1353 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1354 Display *dpy = XtDisplay ((Widget) w);
1356 XSetWindowAttributes win_attr;
1358 (*coreClassRec.core_class.realize)(widget, valuemask, attr);
1360 win = XtWindow ((Widget) w);
1362 seg_pixel_sizes (w, &w->sb.above, &w->sb.ss, &w->sb.below);
1364 XSetWindowBackground (dpy, win, w->sb.troughColor);
1366 /* Change bit gravity so widget is not cleared on resize */
1367 win_attr.bit_gravity = NorthWestGravity;
1368 XChangeWindowAttributes (dpy, win, CWBitGravity , &win_attr);
1373 Resize (Widget widget)
1375 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1376 Display *dpy = XtDisplay ((Widget) w);
1377 Window win = XtWindow ((Widget) w);
1379 if (XtIsRealized (widget))
1381 DBUG (fprintf (stderr, "Resize = %08lx\n", w));
1383 seg_pixel_sizes (w, &w->sb.above, &w->sb.ss, &w->sb.below);
1385 /* redraw_everything (w, NULL, True); */
1387 w->sb.fullRedrawNext = True;
1388 /* Force expose event */
1389 XClearArea (dpy, win, widget_x (w), widget_y (w), 1, 1, True);
1392 if (w->sb.timerActive)
1394 XtRemoveTimeOut (w->sb.timerId);
1395 w->sb.timerActive = False;
1400 Redisplay (Widget widget, XEvent *event, Region region)
1402 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1404 DBUG (fprintf (stderr, "Redisplay = %08lx\n", w));
1406 if (XtIsRealized (widget))
1408 if (w->sb.fullRedrawNext)
1409 redraw_everything (w, NULL, True);
1411 redraw_everything (w, region, False);
1412 w->sb.fullRedrawNext = False;
1417 SetValues (Widget current, Widget request, Widget neww,
1418 ArgList args, Cardinal *num_args)
1420 XlwScrollBarWidget cur = (XlwScrollBarWidget) current;
1421 XlwScrollBarWidget w = (XlwScrollBarWidget) neww;
1422 Boolean do_redisplay = False;
1424 if (cur->sb.troughColor != w->sb.troughColor)
1426 if (XtIsRealized ((Widget) w))
1428 XSetWindowBackground (XtDisplay((Widget) w), XtWindow ((Widget) w),
1430 do_redisplay = True;
1434 if (cur->core.background_pixel != w->core.background_pixel)
1436 XtReleaseGC ((Widget)cur, cur->sb.backgroundGC);
1437 w->sb.backgroundGC =
1438 get_gc (w, w->core.background_pixel, w->core.background_pixel, None);
1439 do_redisplay = True;
1442 if (cur->sb.topShadowColor != w->sb.topShadowColor ||
1443 cur->sb.topShadowPixmap != w->sb.topShadowPixmap)
1445 XtReleaseGC ((Widget)cur, cur->sb.topShadowGC);
1447 get_gc (w, w->sb.topShadowColor, w->core.background_pixel,
1448 w->sb.topShadowPixmap);
1449 do_redisplay = True;
1452 if (cur->sb.bottomShadowColor != w->sb.bottomShadowColor ||
1453 cur->sb.bottomShadowPixmap != w->sb.bottomShadowPixmap)
1455 XtReleaseGC ((Widget)cur, cur->sb.bottomShadowGC);
1456 w->sb.bottomShadowGC =
1457 get_gc (w, w->sb.bottomShadowColor, w->core.background_pixel,
1458 w->sb.bottomShadowPixmap);
1459 do_redisplay = True;
1462 if (cur->sb.orientation != w->sb.orientation)
1463 do_redisplay = True;
1466 if (cur->sb.minimum != w->sb.minimum ||
1467 cur->sb.maximum != w->sb.maximum ||
1468 cur->sb.sliderSize != w->sb.sliderSize ||
1469 cur->sb.value != w->sb.value ||
1470 cur->sb.pageIncrement != w->sb.pageIncrement ||
1471 cur->sb.increment != w->sb.increment)
1474 if (XtIsRealized ((Widget) w))
1476 seg_pixel_sizes (w, &w->sb.above, &w->sb.ss, &w->sb.below);
1477 draw_slider (w, w->sb.above, w->sb.ss, w->sb.below);
1481 if (w->sb.shadowThickness > 5) w->sb.shadowThickness = 5;
1483 return do_redisplay;
1487 XlwScrollBarGetValues (Widget widget, int *value, int *sliderSize,
1488 int *increment, int *pageIncrement)
1490 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1492 if (w && XtClass ((Widget) w) == xlwScrollBarWidgetClass)
1494 if (value) *value = w->sb.value;
1495 if (sliderSize) *sliderSize = w->sb.sliderSize;
1496 if (increment) *increment = w->sb.increment;
1497 if (pageIncrement) *pageIncrement = w->sb.pageIncrement;
1502 XlwScrollBarSetValues (Widget widget, int value, int sliderSize,
1503 int increment, int pageIncrement, Boolean notify)
1505 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1507 if (w && XtClass ((Widget) w) == xlwScrollBarWidgetClass &&
1508 (w->sb.value != value ||
1509 w->sb.sliderSize != sliderSize ||
1510 w->sb.increment != increment ||
1511 w->sb.pageIncrement != pageIncrement))
1513 int last_value = w->sb.value;
1515 w->sb.value = value;
1516 w->sb.sliderSize = sliderSize;
1517 w->sb.increment = increment;
1518 w->sb.pageIncrement = pageIncrement;
1522 if (XtIsRealized (widget))
1524 seg_pixel_sizes (w, &w->sb.above, &w->sb.ss, &w->sb.below);
1525 draw_slider (w, w->sb.above, w->sb.ss, w->sb.below);
1527 if (w->sb.value != last_value && notify)
1528 call_callbacks (w, XmCR_VALUE_CHANGED, w->sb.value, 0, NULL);
1533 /*-------------------------- Action Functions ---------------------------*/
1536 timer (XtPointer data, XtIntervalId *id)
1538 XlwScrollBarWidget w = (XlwScrollBarWidget) data;
1539 w->sb.timerActive = False;
1541 if (w->sb.armed != ARM_NONE)
1543 int last_value = w->sb.value;
1546 switch (w->sb.armed)
1549 decrement_value (w, w->sb.pageIncrement);
1550 reason = XmCR_PAGE_DECREMENT;
1553 increment_value (w, w->sb.pageIncrement);
1554 reason = XmCR_PAGE_INCREMENT;
1557 decrement_value (w, w->sb.increment);
1558 reason = XmCR_DECREMENT;
1561 increment_value (w, w->sb.increment);
1562 reason = XmCR_INCREMENT;
1570 if (last_value != w->sb.value)
1572 seg_pixel_sizes (w, &w->sb.above, &w->sb.ss, &w->sb.below);
1573 draw_slider (w, w->sb.above, w->sb.ss, w->sb.below);
1575 call_callbacks (w, reason, w->sb.value, 0, NULL);
1578 XtAppAddTimeOut (XtWidgetToApplicationContext ((Widget) w),
1579 (unsigned long) w->sb.repeatDelay,
1580 timer, (XtPointer) w);
1581 w->sb.timerActive = True;
1587 what_button (XlwScrollBarWidget w, int mouse_x, int mouse_y)
1589 int width = widget_w (w);
1590 int height = widget_h (w);
1591 int arrow_height = arrow_h (w);
1593 mouse_x -= widget_x (w);
1594 mouse_y -= widget_y (w);
1596 if (mouse_x < 0 || mouse_x >= width ||
1597 mouse_y < 0 || mouse_y >= height)
1600 if (w->sb.showArrows)
1602 if (mouse_y >= (height -= arrow_height))
1603 return BUTTON_DOWN_ARROW;
1605 if (arrow_same_end (w))
1607 if (mouse_y >= (height -= arrow_height))
1608 return BUTTON_UP_ARROW;
1611 if ( (mouse_y -= arrow_height) < 0)
1612 return BUTTON_UP_ARROW;
1615 if ( (mouse_y -= w->sb.above) < 0)
1616 return BUTTON_TROUGH_ABOVE;
1618 if ( (mouse_y -= w->sb.ss) < 0)
1619 return BUTTON_SLIDER;
1621 return BUTTON_TROUGH_BELOW;
1625 Select (Widget widget, XEvent *event, String *parms, Cardinal *num_parms)
1627 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1628 button_where sb_button;
1630 int mouse_x = event_x (w, event);
1631 int mouse_y = event_y (w, event);
1633 int last_value = w->sb.savedValue = w->sb.value;
1634 int reason = XmCR_NONE;
1636 XtGrabKeyboard ((Widget) w, False, GrabModeAsync, GrabModeAsync,
1637 event->xbutton.time);
1639 sb_button = what_button (w, mouse_x, mouse_y);
1641 if (w->sb.forced_scroll != FORCED_SCROLL_NONE)
1645 case BUTTON_TROUGH_ABOVE:
1646 case BUTTON_TROUGH_BELOW:
1648 sb_button= BUTTON_NONE; /* cause next switch to fall through */
1649 if (w->sb.forced_scroll == FORCED_SCROLL_UPLEFT)
1651 decrement_value (w, w->sb.pageIncrement);
1652 w->sb.armed = ARM_PAGEUP;
1653 reason = XmCR_PAGE_DECREMENT;
1656 else if (w->sb.forced_scroll == FORCED_SCROLL_DOWNRIGHT)
1658 increment_value (w, w->sb.pageIncrement);
1659 w->sb.armed = ARM_PAGEDOWN;
1660 reason = XmCR_PAGE_INCREMENT;
1671 case BUTTON_TROUGH_ABOVE:
1672 decrement_value (w, w->sb.pageIncrement);
1673 w->sb.armed = ARM_PAGEUP;
1674 reason = XmCR_PAGE_DECREMENT;
1676 case BUTTON_TROUGH_BELOW:
1677 increment_value (w, w->sb.pageIncrement);
1678 w->sb.armed = ARM_PAGEDOWN;
1679 reason = XmCR_PAGE_INCREMENT;
1682 w->sb.lastY = mouse_y;
1683 w->sb.armed = ARM_SLIDER;
1684 draw_slider (w, w->sb.above, w->sb.ss, w->sb.below);
1686 case BUTTON_UP_ARROW:
1687 if (event->xbutton.state & ControlMask)
1689 w->sb.value = w->sb.minimum;
1690 reason = XmCR_TO_TOP;
1694 decrement_value (w, w->sb.increment);
1695 reason = XmCR_DECREMENT;
1697 w->sb.armed = ARM_UP;
1698 redraw_up_arrow (w, True, False);
1700 case BUTTON_DOWN_ARROW:
1701 if (event->xbutton.state & ControlMask)
1703 w->sb.value = w->sb.maximum;
1704 reason = XmCR_TO_BOTTOM;
1708 increment_value (w, w->sb.increment);
1709 reason = XmCR_INCREMENT;
1711 w->sb.armed = ARM_DOWN;
1712 redraw_down_arrow (w, True, False);
1720 if (last_value != w->sb.value)
1722 seg_pixel_sizes (w, &w->sb.above, &w->sb.ss, &w->sb.below);
1723 draw_slider (w, w->sb.above, w->sb.ss, w->sb.below);
1725 call_callbacks (w, reason, w->sb.value, mouse_y, event);
1727 if (w->sb.timerActive)
1728 XtRemoveTimeOut (w->sb.timerId);
1731 XtAppAddTimeOut (XtWidgetToApplicationContext ((Widget) w),
1732 (unsigned long) w->sb.initialDelay,
1733 timer, (XtPointer) w);
1734 w->sb.timerActive = True;
1741 PageDownOrRight (Widget widget, XEvent *event, String *parms, Cardinal *num_parms)
1743 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1744 w->sb.forced_scroll = FORCED_SCROLL_DOWNRIGHT;
1745 Select (widget, event, parms, num_parms);
1746 w->sb.forced_scroll = FORCED_SCROLL_NONE;
1750 PageUpOrLeft (Widget widget, XEvent *event, String *parms, Cardinal *num_parms)
1752 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1753 w->sb.forced_scroll = FORCED_SCROLL_UPLEFT;
1754 Select (widget, event, parms, num_parms);
1755 w->sb.forced_scroll = FORCED_SCROLL_NONE;
1759 Drag (Widget widget, XEvent *event, String *parms, Cardinal *num_parms)
1761 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1763 if (w->sb.armed == ARM_SLIDER)
1765 int mouse_y = event_y (w, event);
1766 int diff = mouse_y - w->sb.lastY;
1768 if (diff < -(w->sb.above)) /* up */
1770 mouse_y -= (diff + w->sb.above);
1771 diff = -(w->sb.above);
1773 else if (diff > w->sb.below) /* down */
1775 mouse_y -= (diff - w->sb.below);
1781 w->sb.above += diff;
1782 w->sb.below -= diff;
1784 draw_slider (w, w->sb.above, w->sb.ss, w->sb.below);
1786 w->sb.lastY = mouse_y;
1788 w->sb.value = value_from_pixel (w, w->sb.above);
1792 call_callbacks (w, XmCR_DRAG, w->sb.value, event_y (w, event), event);
1799 Release (Widget widget, XEvent *event, String *parms, Cardinal *num_parms)
1801 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1803 switch (w->sb.armed)
1806 call_callbacks (w, XmCR_VALUE_CHANGED, w->sb.value, event_y (w, event), event);
1807 w->sb.armed = ARM_NONE;
1808 draw_slider (w, w->sb.above, w->sb.ss, w->sb.below);
1811 redraw_up_arrow (w, False, False);
1814 redraw_down_arrow (w, False, False);
1820 XtUngrabKeyboard ((Widget) w, event->xbutton.time);
1822 w->sb.armed = ARM_NONE;
1826 Jump (Widget widget, XEvent *event, String *parms, Cardinal *num_parms)
1828 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1831 int mouse_x = event_x (w, event);
1832 int mouse_y = event_y (w, event);
1834 int scroll_region_y = widget_y (w);
1835 int scroll_region_h = widget_h (w);
1837 if (w->sb.showArrows)
1839 int arrow_height = arrow_h (w);
1840 scroll_region_h -= 2 * arrow_height;
1841 if (!arrow_same_end (w))
1842 scroll_region_y += arrow_height;
1845 XtGrabKeyboard ((Widget) w, False, GrabModeAsync, GrabModeAsync,
1846 event->xbutton.time);
1848 switch (what_button (w, mouse_x, mouse_y))
1850 case BUTTON_TROUGH_ABOVE:
1851 case BUTTON_TROUGH_BELOW:
1853 w->sb.savedValue = w->sb.value;
1855 last_value = w->sb.value;
1857 w->sb.above = mouse_y - (w->sb.ss / 2) - scroll_region_y;
1858 if (w->sb.above < 0)
1860 else if (w->sb.above + w->sb.ss > scroll_region_h)
1861 w->sb.above = scroll_region_h - w->sb.ss;
1863 w->sb.below = scroll_region_h - w->sb.ss - w->sb.above;
1865 w->sb.armed = ARM_SLIDER;
1866 draw_slider (w, w->sb.above, w->sb.ss, w->sb.below);
1868 w->sb.value = value_from_pixel (w, w->sb.above);
1872 w->sb.lastY = mouse_y;
1874 if (w->sb.value != last_value)
1875 call_callbacks (w, XmCR_DRAG, w->sb.value, mouse_y, event);
1885 Abort (Widget widget, XEvent *event, String *parms, Cardinal *num_parms)
1887 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1889 if (w->sb.armed != ARM_NONE)
1891 if (w->sb.value != w->sb.savedValue)
1893 w->sb.value = w->sb.savedValue;
1895 seg_pixel_sizes (w, &w->sb.above, &w->sb.ss, &w->sb.below);
1896 draw_slider (w, w->sb.above, w->sb.ss, w->sb.below);
1898 call_callbacks (w, XmCR_VALUE_CHANGED, w->sb.value,
1899 event_y (w, event), event);
1902 switch (w->sb.armed)
1904 case ARM_UP: redraw_up_arrow (w, False, False); break;
1905 case ARM_DOWN: redraw_down_arrow (w, False, False); break;
1906 default: ; /* Do nothing */
1909 w->sb.armed = ARM_NONE;
1911 XtUngrabKeyboard ((Widget) w, event->xbutton.time);