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 --------------------*/
489 #ifndef XmUNSPECIFIED_PIXMAP
490 #define XmUNSPECIFIED_PIXMAP 2
492 #endif /* NEED_MOTIF */
495 get_gc (XlwScrollBarWidget w, Pixel fg, Pixel bg, Pixmap pm)
500 if (pm == w->sb.grayPixmap)
502 /* If we're using the gray pixmap, guarantee white on black ...
503 * otherwise, we could end up with something odd like grey on white
504 * when we're on a color display that ran out of color cells
507 fg = WhitePixelOfScreen (DefaultScreenOfDisplay (XtDisplay (w)));
508 bg = BlackPixelOfScreen (DefaultScreenOfDisplay (XtDisplay (w)));
511 values.foreground = fg;
512 values.background = bg;
513 values.fill_style = FillOpaqueStippled;
515 /* mask = GCForeground | GCBackground |
516 (pm == None ? 0 : GCStipple | GCFillStyle); gtb */
518 if (pm != None && pm != 0 && pm != XmUNSPECIFIED_PIXMAP)
521 values.stipple = None;
524 #endif /* NEED_MOTIF */
525 mask = GCForeground | GCBackground |
526 (values.stipple == None ? 0 : GCStipple | GCFillStyle);
528 return XtGetGC((Widget) w, mask, &values);
531 /* Replacement for XAllocColor() that tries to return the nearest
532 available color if the colormap is full. From FSF Emacs. */
535 allocate_nearest_color (Display *display, Colormap screen_colormap,
538 int status = XAllocColor (display, screen_colormap, color_def);
543 /* If we got to this point, the colormap is full, so we're
544 going to try to get the next closest color.
545 The algorithm used is a least-squares matching, which is
546 what X uses for closest color matching with StaticColor visuals. */
549 unsigned long nearest_delta = ULONG_MAX;
551 int no_cells = XDisplayCells (display, XDefaultScreen (display));
552 /* Don't use alloca here because lwlib doesn't have the
553 necessary configuration information that src does. */
554 XColor *cells = (XColor *) malloc (sizeof (XColor) * no_cells);
556 for (x = 0; x < no_cells; x++)
559 XQueryColors (display, screen_colormap, cells, no_cells);
561 for (nearest = 0, x = 0; x < no_cells; x++)
563 long dred = (color_def->red >> 8) - (cells[x].red >> 8);
564 long dgreen = (color_def->green >> 8) - (cells[x].green >> 8);
565 long dblue = (color_def->blue >> 8) - (cells[x].blue >> 8);
566 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
568 if (delta < nearest_delta)
571 nearest_delta = delta;
574 color_def->red = cells[nearest].red;
575 color_def->green = cells[nearest].green;
576 color_def->blue = cells[nearest].blue;
578 return XAllocColor (display, screen_colormap, color_def);
583 make_shadow_pixels (XlwScrollBarWidget w)
585 Display *dpy = XtDisplay((Widget) w);
586 Colormap cmap = w->core.colormap;
588 int top_frobbed, bottom_frobbed;
591 top_frobbed = bottom_frobbed = 0;
593 bg = w->core.background_pixel;
594 fg = w->sb.foreground;
596 if (w->sb.topShadowColor == (Pixel)~0) w->sb.topShadowColor = bg;
597 if (w->sb.bottomShadowColor == (Pixel)~0) w->sb.bottomShadowColor = fg;
599 if (w->sb.topShadowColor == bg || w->sb.topShadowColor == fg)
602 XQueryColor (dpy, cmap, &topc);
603 /* don't overflow/wrap! */
604 topc.red = MINL(65535, topc.red * 1.2);
605 topc.green = MINL(65535, topc.green * 1.2);
606 topc.blue = MINL(65535, topc.blue * 1.2);
607 if (allocate_nearest_color (dpy, cmap, &topc))
609 if (topc.pixel == bg)
611 XFreeColors (dpy, cmap, &topc.pixel, 1, 0);
612 topc.red = MINL(65535, topc.red + 0x8000);
613 topc.green = MINL(65535, topc.green + 0x8000);
614 topc.blue = MINL(65535, topc.blue + 0x8000);
615 if (allocate_nearest_color (dpy, cmap, &topc))
617 w->sb.topShadowColor = topc.pixel;
622 w->sb.topShadowColor = topc.pixel;
629 if (w->sb.bottomShadowColor == fg || w->sb.bottomShadowColor == bg)
632 XQueryColor (dpy, cmap, &botc);
633 botc.red = (botc.red * 3) / 5;
634 botc.green = (botc.green * 3) / 5;
635 botc.blue = (botc.blue * 3) / 5;
636 if (allocate_nearest_color (dpy, cmap, &botc))
638 if (botc.pixel == bg)
640 XFreeColors (dpy, cmap, &botc.pixel, 1, 0);
641 botc.red = MINL(65535, botc.red + 0x4000);
642 botc.green = MINL(65535, botc.green + 0x4000);
643 botc.blue = MINL(65535, botc.blue + 0x4000);
644 if (allocate_nearest_color (dpy, cmap, &botc))
646 w->sb.bottomShadowColor = botc.pixel;
651 w->sb.bottomShadowColor = botc.pixel;
657 if (top_frobbed && bottom_frobbed)
659 int top_avg = ((topc.red / 3) + (topc.green / 3) + (topc.blue / 3));
660 int bot_avg = ((botc.red / 3) + (botc.green / 3) + (botc.blue / 3));
661 if (bot_avg > top_avg)
663 Pixel tmp = w->sb.topShadowColor;
664 w->sb.topShadowColor = w->sb.bottomShadowColor;
665 w->sb.bottomShadowColor = tmp;
667 else if (topc.pixel == botc.pixel)
669 if (botc.pixel == bg)
670 w->sb.topShadowColor = bg;
672 w->sb.bottomShadowColor = fg;
676 if (w->sb.topShadowColor == w->core.background_pixel ||
677 w->sb.bottomShadowColor == w->core.background_pixel)
679 /* Assume we're in mono. This code should be okay even if we're
680 * really in color but just short on color cells -- We want the
681 * following behavior, which has been empirically determined to
682 * work well for all fg/bg combinations in mono: If the trough
683 * and slider are BOTH black, then use a white top shadow and a
684 * grey bottom shadow, otherwise use a grey top shadow and a
685 * black bottom shadow.
688 Pixel white = WhitePixelOfScreen (DefaultScreenOfDisplay (XtDisplay (w)));
689 Pixel black = BlackPixelOfScreen (DefaultScreenOfDisplay (XtDisplay (w)));
691 /* Note: core.background_pixel is the color of the slider ... */
693 if (w->core.background_pixel == black &&
694 w->sb.troughColor == black)
696 w->sb.topShadowColor = white;
697 w->sb.bottomShadowPixmap = w->sb.grayPixmap;
699 w->sb.topShadowPixmap = w->sb.grayPixmap;
700 w->sb.bottomShadowColor = black;
706 make_trough_pixel (XlwScrollBarWidget w)
708 Display *dpy = XtDisplay((Widget) w);
709 Colormap cmap = w->core.colormap;
712 if (w->sb.troughColor == (Pixel)~0) w->sb.troughColor = w->core.background_pixel;
714 if (w->sb.troughColor == w->core.background_pixel)
716 troughC.pixel = w->core.background_pixel;
717 XQueryColor (dpy, cmap, &troughC);
718 troughC.red = (troughC.red * 4) / 5;
719 troughC.green = (troughC.green * 4) / 5;
720 troughC.blue = (troughC.blue * 4) / 5;
721 if (allocate_nearest_color (dpy, cmap, &troughC))
722 w->sb.troughColor = troughC.pixel;
726 /*-------------------------- Draw 3D Border -----------------------------*/
728 draw_shadows (Display *dpy, Drawable d, GC shine_gc, GC shadow_gc,
729 int x, int y, int width, int height, int shadowT)
731 XSegment shine[10], shadow[10];
734 if (shadowT > (width / 2)) shadowT = (width / 2);
735 if (shadowT > (height / 2)) shadowT = (height / 2);
736 if (shadowT <= 0) return;
738 for (i = 0; i < shadowT; i++)
742 shine[i].y2 = shine[i].y1 = y + i;
743 shine[i].x2 = x + width - i - 1;
745 shine[i + shadowT].x2 = shine[i + shadowT].x1 = x + i;
746 shine[i + shadowT].y1 = y + shadowT;
747 shine[i + shadowT].y2 = y + height - i - 1;
749 /* Bottom segments */
750 shadow[i].x1 = x + i;
751 shadow[i].y2 = shadow[i].y1 = y + height - i - 1;
752 shadow[i].x2 = x + width - 1 ;
754 shadow[i + shadowT].x2 = shadow[i + shadowT].x1 = x + width - i - 1;
755 shadow[i + shadowT].y1 = y + i + 1;
756 shadow[i + shadowT].y2 = y + height - 1 ;
759 XDrawSegments (dpy, d, shine_gc, shine, shadowT * 2);
760 XDrawSegments (dpy, d, shadow_gc, shadow, shadowT * 2);
763 /*------------------ Draw 3D Arrows: left, up, down, right --------------*/
765 make_vert_seg (XSegment *seg, int x1, int y1, int x2, int y2, int shadowT)
769 for (i=0; i<shadowT; i++, seg++)
780 make_hor_seg (XSegment *seg, int x1, int y1, int x2, int y2, int shadowT)
784 for (i=0; i<shadowT; i++, seg++)
795 draw_arrow_up (Display *dpy, Drawable win, GC bgGC, GC shineGC, GC shadowGC,
796 int x, int y, int width, int height, int shadowT)
798 XSegment shine[10], shadow[10];
804 if (shadowT > (width / 2)) shadowT = (width / 2);
805 if (shadowT > (height / 2)) shadowT = (height / 2);
806 if (shadowT < 0) shadowT = 0;
809 make_vert_seg (shine,
810 x, y + height - shadowT - 1,
811 x + mid, y, shadowT);
813 make_vert_seg (shadow,
814 x, y + height - shadowT - 1,
815 x + width - 1, y + height - shadowT - 1, shadowT);
816 make_vert_seg (shadow + shadowT,
818 x + width - 1, y + height - shadowT - 1, shadowT);
821 triangle[0].y = y + height - 1;
822 triangle[1].x = x + mid;
824 triangle[2].x = x + width - 1;
825 triangle[2].y = y + height - 1;
827 XFillPolygon (dpy, win, bgGC, triangle, 3, Convex, ArcChord);
829 XDrawSegments (dpy, win, shadowGC, shadow, shadowT * 2);
830 XDrawSegments (dpy, win, shineGC, shine, shadowT);
834 draw_arrow_left (Display *dpy, Drawable win, GC bgGC, GC shineGC, GC shadowGC,
835 int x, int y, int width, int height, int shadowT)
837 XSegment shine[10], shadow[10];
842 if (shadowT > (width / 2)) shadowT = (width / 2);
843 if (shadowT > (height / 2)) shadowT = (height / 2);
844 if (shadowT < 0) shadowT = 0;
849 x + width - shadowT - 1, y, shadowT);
851 make_hor_seg (shadow,
853 x + width - shadowT - 1, y + height - 1, shadowT);
854 make_hor_seg (shadow + shadowT,
855 x + width - shadowT - 1, y,
856 x + width - shadowT - 1, y + height - 1, shadowT);
858 triangle[0].x = x + width - 1;
859 triangle[0].y = y + height - 1;
861 triangle[1].y = y + mid;
862 triangle[2].x = x + width - 1;
865 XFillPolygon (dpy, win, bgGC, triangle, 3, Convex, ArcChord);
867 XDrawSegments (dpy, win, shadowGC, shadow, shadowT * 2);
868 XDrawSegments (dpy, win, shineGC, shine, shadowT);
872 draw_arrow_down (Display *dpy, Drawable win, GC bgGC, GC shineGC, GC shadowGC,
873 int x, int y, int width, int height, int shadowT)
875 XSegment shine[10], shadow[10];
881 if (shadowT > (width / 2)) shadowT = (width / 2);
882 if (shadowT > (height / 2)) shadowT = (height / 2);
883 if (shadowT < 0) shadowT = 0;
886 make_vert_seg (shine,
888 x + mid, y + height - shadowT - 1, shadowT);
889 make_vert_seg (shine + shadowT,
891 x + width - 1, y, shadowT);
893 make_vert_seg (shadow,
895 x + mid, y + height - shadowT - 1, shadowT);
899 triangle[1].x = x + mid;
900 triangle[1].y = y + height - 1;
901 triangle[2].x = x + width - 1;
904 XFillPolygon (dpy, win, bgGC, triangle, 3, Convex, ArcChord);
906 XDrawSegments (dpy, win, shadowGC, shadow, shadowT);
907 XDrawSegments (dpy, win, shineGC, shine, shadowT * 2);
911 draw_arrow_right (Display *dpy, Drawable win, GC bgGC, GC shineGC, GC shadowGC,
912 int x, int y, int width, int height, int shadowT)
914 XSegment shine[10], shadow[10];
920 if (shadowT > (width / 2)) shadowT = (width / 2);
921 if (shadowT > (height / 2)) shadowT = (height / 2);
922 if (shadowT < 0) shadowT = 0;
927 x + width - shadowT - 1, y + mid, shadowT);
928 make_hor_seg (shine + shadowT,
930 x, y + height - 1, shadowT);
932 make_hor_seg (shadow,
934 x + width - shadowT - 1, y + mid, shadowT);
936 triangle[0].x = x + 1;
937 triangle[0].y = y + height - 1;
938 triangle[1].x = x + width - 1;
939 triangle[1].y = y + mid;
940 triangle[2].x = x + 1;
943 XFillPolygon (dpy, win, bgGC, triangle, 3, Convex, ArcChord);
945 XDrawSegments (dpy, win, shadowGC, shadow, shadowT);
946 XDrawSegments (dpy, win, shineGC, shine, shadowT * 2);
950 draw_dimple (Display *dpy, Drawable win, GC shine, GC shadow,
951 int x, int y, int width, int height)
953 XDrawArc (dpy, win, shine, x, y, width, height, 46*64, 180*64);
954 XDrawArc (dpy, win, shadow, x, y, width, height, 45*64, -179*64);
957 /*------- Scrollbar values -> pixels, pixels -> scrollbar values --------*/
960 seg_pixel_sizes (XlwScrollBarWidget w, int *above_return,
961 int *ss_return, int *below_return)
963 float total, height, fuz;
964 int value, above, ss, below;
966 height = widget_h (w);
967 if (w->sb.showArrows) height -= (2 * arrow_h (w));
969 value = w->sb.value - w->sb.minimum;
971 total = w->sb.maximum - w->sb.minimum;
974 ss = (int) ((height * w->sb.sliderSize + fuz) / total);
975 above = (int) ((height * value + fuz) / total);
976 below = (int) ((height) - (ss + above));
978 /* Don't let slider get smaller than SS_MIN */
981 /* add a percent amount for integer rounding */
982 float tmp = ((((float) (SS_MIN - ss) * (float) value)) / total) + 0.5;
986 below = (int) ((height) - (ss + above));
991 below = (int) (height - ss);
995 above = (int) (height - ss);
1006 *above_return = above;
1008 *below_return = below;
1014 verify_values (XlwScrollBarWidget w)
1016 int total = w->sb.maximum - w->sb.minimum;
1018 if (w->sb.sliderSize > total)
1019 w->sb.sliderSize = total;
1021 if (w->sb.pageIncrement > total)
1022 w->sb.pageIncrement = total;
1024 if (w->sb.increment > total)
1025 w->sb.increment = total;
1027 if (w->sb.value < w->sb.minimum)
1028 w->sb.value = w->sb.minimum;
1030 if (w->sb.value > w->sb.maximum)
1031 w->sb.value = w->sb.maximum;
1033 if (w->sb.sliderSize > w->sb.maximum - w->sb.value)
1034 w->sb.sliderSize = w->sb.maximum - w->sb.value;
1038 value_from_pixel (XlwScrollBarWidget w, int above)
1040 float total, height, fuz;
1043 height = widget_h (w);
1044 if (w->sb.showArrows)
1045 height -= (2 * arrow_h (w));
1047 total = w->sb.maximum - w->sb.minimum;
1050 ss = (int) ((height * w->sb.sliderSize + (total / 2)) / total);
1054 /* add a percent amount for integer rounding */
1055 above += (int) ((((SS_MIN - ss) * above) + fuz) / height);
1059 /* Prevent SIGFPE's that would occur if we don't truncate the value. */
1060 float floatval = w->sb.minimum + ((float)(above * total + fuz) / height);
1061 if (floatval >= (float) INT_MAX)
1063 else if (floatval <= (float) INT_MIN)
1066 value = (int) floatval;
1074 redraw_dimple (XlwScrollBarWidget w, Display *dpy, Window win,
1075 int x, int y, int width, int height)
1077 if (SLIDER_DIMPLE == slider_style (w))
1080 int slider_p = (w->sb.armed == ARM_SLIDER);
1081 GC shine = slider_p ? w->sb.bottomShadowGC : w->sb.topShadowGC;
1082 GC shadow = slider_p ? w->sb.topShadowGC : w->sb.bottomShadowGC;
1083 int shadowT = w->sb.shadowThickness;
1088 height -= 2*shadowT;
1090 size = (width < height ? width : height) * 3 / 4;
1092 if (size%2 != (width < height ? width : height)%2) size--;
1094 DBUG (fprintf (stderr, "%d %d\n",
1095 x + (width / 2) - (size / 2) - 2*shadowT,
1096 width - size - shadowT));
1098 draw_dimple (dpy, win, shine, shadow,
1099 x + (width / 2) - (size / 2),
1100 y + (height / 2) - (size / 2),
1106 draw_slider (XlwScrollBarWidget w, int above, int ss, int below)
1108 Display *dpy = XtDisplay ((Widget) w);
1109 Window win = XtWindow ((Widget) w);
1111 int x = widget_x (w);
1112 int y = widget_y (w);
1113 int width = widget_w (w);
1114 int height = widget_h (w);
1115 int shadowT = w->sb.shadowThickness;
1116 int vert_p = VERT (w);
1118 if (shadowT > (width / 2)) shadowT = (width / 2);
1119 if (shadowT > (height / 2)) shadowT = (height / 2);
1120 if (shadowT < 0) shadowT = 0;
1122 if (w->sb.showArrows && !arrow_same_end (w))
1125 /* trough above slider */
1129 XClearArea (dpy, win, x, y, width, above, False);
1131 XClearArea (dpy, win, y, x, above, width, False);
1137 draw_shadows (dpy, win, w->sb.topShadowGC, w->sb.bottomShadowGC,
1138 x, y + above, width, ss, shadowT);
1139 XFillRectangle (dpy, win, w->sb.backgroundGC,
1140 x+shadowT, y + above + shadowT,
1141 width-2*shadowT, ss-2*shadowT);
1142 redraw_dimple (w, dpy, win, x, y + above, width, ss);
1146 draw_shadows (dpy, win, w->sb.topShadowGC, w->sb.bottomShadowGC,
1147 y + above, x, ss, width, shadowT);
1148 XFillRectangle (dpy, win, w->sb.backgroundGC,
1149 y + above + shadowT, x+shadowT,
1150 ss-2*shadowT, width-2*shadowT);
1151 redraw_dimple (w, dpy, win, y + above, x, ss, width);
1154 /* trough below slider */
1158 XClearArea (dpy, win, x, y + above + ss, width, below, False);
1160 XClearArea (dpy, win, y + above + ss, x, below, width, False);
1167 redraw_up_arrow (XlwScrollBarWidget w, Boolean armed, Boolean clear_behind)
1169 Display *dpy = XtDisplay ((Widget) w);
1170 Window win = XtWindow ((Widget) w);
1172 int x = widget_x (w);
1173 int y = widget_y (w);
1174 int width = widget_w (w);
1175 int height = widget_h (w);
1176 int shadowT = w->sb.shadowThickness;
1177 int arrow_height = arrow_h (w);
1179 GC bg = w->sb.backgroundGC;
1180 GC shine = armed ? w->sb.bottomShadowGC : w->sb.topShadowGC;
1181 GC shadow = armed ? w->sb.topShadowGC : w->sb.bottomShadowGC;
1185 if (arrow_same_end (w))
1186 y += height - 2 * arrow_height;
1188 XClearArea (dpy, win, x, y, width, arrow_height + 1, False);
1189 draw_arrow_up (dpy, win, bg, shine, shadow,
1190 x + (width - arrow_height)/2, y,
1191 arrow_height, arrow_height, shadowT);
1195 if (arrow_same_end (w))
1196 y += height - 2 * arrow_height;
1198 XClearArea (dpy, win, y, x, arrow_height + 1, height, False);
1199 draw_arrow_left (dpy, win, bg, shine, shadow,
1200 y, x + (width - arrow_height)/2,
1201 arrow_height, arrow_height, shadowT);
1206 redraw_down_arrow (XlwScrollBarWidget w, Boolean armed, Boolean clear_behind)
1208 Display *dpy = XtDisplay ((Widget) w);
1209 Window win = XtWindow ((Widget) w);
1211 int x = widget_x (w);
1212 int y = widget_y (w);
1213 int width = widget_w (w);
1214 int height = widget_h (w);
1215 int shadowT = w->sb.shadowThickness;
1216 int arrow_height = arrow_h (w);
1218 GC bg = w->sb.backgroundGC;
1219 GC shine = armed ? w->sb.bottomShadowGC : w->sb.topShadowGC;
1220 GC shadow = armed ? w->sb.topShadowGC : w->sb.bottomShadowGC;
1225 XClearArea (dpy, win, x, y + height - arrow_height, width,
1226 arrow_height + 1, False);
1227 draw_arrow_down (dpy, win, bg, shine, shadow,
1228 x + (width - arrow_height)/2,
1229 y + height - arrow_height + 1,
1230 arrow_height, arrow_height, shadowT);
1235 XClearArea (dpy, win, y + height - arrow_height, x,
1236 arrow_height + 1, height, False);
1237 draw_arrow_right (dpy, win, bg, shine, shadow,
1238 y + height - arrow_height + 1,
1239 x + (width - arrow_height)/2,
1240 arrow_height, arrow_height, shadowT);
1245 redraw_everything (XlwScrollBarWidget w, Region region, Boolean behind_arrows)
1247 Display *dpy = XtDisplay ((Widget) w);
1248 Window win = XtWindow ((Widget) w);
1250 if (w->sb.showArrows)
1254 redraw_up_arrow (w, False, behind_arrows);
1255 redraw_down_arrow (w, False, behind_arrows);
1259 int x = widget_x (w);
1260 int y = widget_y (w);
1261 int width = widget_w (w);
1262 int height = widget_h (w);
1263 int arrow_height = arrow_h (w);
1266 if (arrow_same_end (w))
1269 ay = y + height - arrow_height - arrow_height;
1271 ax = x + height - arrow_height - arrow_height;
1273 if (XRectInRegion (region, ax, ay, width, width))
1274 redraw_up_arrow (w, False, behind_arrows);
1277 ay = y + height - arrow_height;
1279 ax = x + height - arrow_height;
1280 if (XRectInRegion (region, ax, ay, width, width))
1281 redraw_down_arrow (w, False, behind_arrows);
1285 draw_shadows (dpy, win, w->sb.bottomShadowGC, w->sb.topShadowGC, 0, 0,
1286 w->core.width, w->core.height, w->sb.shadowThickness);
1288 draw_slider (w, w->sb.above, w->sb.ss, w->sb.below);
1291 /*-------------------------- Method Functions ---------------------------*/
1294 Initialize (Widget treq, Widget tnew, ArgList args, Cardinal *num_args)
1296 XlwScrollBarWidget request = (XlwScrollBarWidget) treq;
1297 XlwScrollBarWidget w = (XlwScrollBarWidget) tnew;
1298 Display *dpy = XtDisplay ((Widget) w);
1299 Window win = RootWindowOfScreen (DefaultScreenOfDisplay (dpy));
1301 if (request->core.width == 0) w->core.width += (VERT (w) ? 12 : 25);
1302 if (request->core.height == 0) w->core.height += (VERT (w) ? 25 : 12);
1310 w->sb.armed = ARM_NONE;
1311 w->sb.forced_scroll = FORCED_SCROLL_NONE;
1313 if (w->sb.shadowThickness > 5) w->sb.shadowThickness = 5;
1316 XCreatePixmapFromBitmapData (dpy, win, (char *) gray_bits, gray_width,
1317 gray_height, 1, 0, 1);
1319 make_trough_pixel (w);
1321 make_shadow_pixels (w);
1323 w->sb.backgroundGC =
1324 get_gc (w, w->core.background_pixel, w->core.background_pixel, None);
1326 get_gc (w, w->sb.topShadowColor, w->core.background_pixel,
1327 w->sb.topShadowPixmap);
1328 w->sb.bottomShadowGC =
1329 get_gc (w, w->sb.bottomShadowColor, w->core.background_pixel,
1330 w->sb.bottomShadowPixmap);
1332 w->sb.fullRedrawNext = True;
1334 w->sb.timerActive = False;
1338 Destroy (Widget widget)
1340 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1341 Display *dpy = XtDisplay ((Widget) w);
1343 XtReleaseGC (widget, w->sb.bottomShadowGC);
1344 XtReleaseGC (widget, w->sb.topShadowGC);
1345 XtReleaseGC (widget, w->sb.backgroundGC);
1347 XFreePixmap (dpy, w->sb.grayPixmap);
1349 if (w->sb.timerActive)
1351 XtRemoveTimeOut (w->sb.timerId);
1352 w->sb.timerActive = False; /* Should be a no-op, but you never know */
1357 Realize (Widget widget, XtValueMask *valuemask, XSetWindowAttributes *attr)
1359 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1360 Display *dpy = XtDisplay ((Widget) w);
1362 XSetWindowAttributes win_attr;
1364 (*coreClassRec.core_class.realize)(widget, valuemask, attr);
1366 win = XtWindow ((Widget) w);
1368 seg_pixel_sizes (w, &w->sb.above, &w->sb.ss, &w->sb.below);
1370 XSetWindowBackground (dpy, win, w->sb.troughColor);
1372 /* Change bit gravity so widget is not cleared on resize */
1373 win_attr.bit_gravity = NorthWestGravity;
1374 XChangeWindowAttributes (dpy, win, CWBitGravity , &win_attr);
1379 Resize (Widget widget)
1381 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1382 Display *dpy = XtDisplay ((Widget) w);
1383 Window win = XtWindow ((Widget) w);
1385 if (XtIsRealized (widget))
1387 DBUG (fprintf (stderr, "Resize = %08lx\n", w));
1389 seg_pixel_sizes (w, &w->sb.above, &w->sb.ss, &w->sb.below);
1391 /* redraw_everything (w, NULL, True); */
1393 w->sb.fullRedrawNext = True;
1394 /* Force expose event */
1395 XClearArea (dpy, win, widget_x (w), widget_y (w), 1, 1, True);
1398 if (w->sb.timerActive)
1400 XtRemoveTimeOut (w->sb.timerId);
1401 w->sb.timerActive = False;
1406 Redisplay (Widget widget, XEvent *event, Region region)
1408 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1410 DBUG (fprintf (stderr, "Redisplay = %08lx\n", w));
1412 if (XtIsRealized (widget))
1414 if (w->sb.fullRedrawNext)
1415 redraw_everything (w, NULL, True);
1417 redraw_everything (w, region, False);
1418 w->sb.fullRedrawNext = False;
1423 SetValues (Widget current, Widget request, Widget neww,
1424 ArgList args, Cardinal *num_args)
1426 XlwScrollBarWidget cur = (XlwScrollBarWidget) current;
1427 XlwScrollBarWidget w = (XlwScrollBarWidget) neww;
1428 Boolean do_redisplay = False;
1430 if (cur->sb.troughColor != w->sb.troughColor)
1432 if (XtIsRealized ((Widget) w))
1434 XSetWindowBackground (XtDisplay((Widget) w), XtWindow ((Widget) w),
1436 do_redisplay = True;
1440 if (cur->core.background_pixel != w->core.background_pixel)
1442 XtReleaseGC ((Widget)cur, cur->sb.backgroundGC);
1443 w->sb.backgroundGC =
1444 get_gc (w, w->core.background_pixel, w->core.background_pixel, None);
1445 do_redisplay = True;
1448 if (cur->sb.topShadowColor != w->sb.topShadowColor ||
1449 cur->sb.topShadowPixmap != w->sb.topShadowPixmap)
1451 XtReleaseGC ((Widget)cur, cur->sb.topShadowGC);
1453 get_gc (w, w->sb.topShadowColor, w->core.background_pixel,
1454 w->sb.topShadowPixmap);
1455 do_redisplay = True;
1458 if (cur->sb.bottomShadowColor != w->sb.bottomShadowColor ||
1459 cur->sb.bottomShadowPixmap != w->sb.bottomShadowPixmap)
1461 XtReleaseGC ((Widget)cur, cur->sb.bottomShadowGC);
1462 w->sb.bottomShadowGC =
1463 get_gc (w, w->sb.bottomShadowColor, w->core.background_pixel,
1464 w->sb.bottomShadowPixmap);
1465 do_redisplay = True;
1468 if (cur->sb.orientation != w->sb.orientation)
1469 do_redisplay = True;
1472 if (cur->sb.minimum != w->sb.minimum ||
1473 cur->sb.maximum != w->sb.maximum ||
1474 cur->sb.sliderSize != w->sb.sliderSize ||
1475 cur->sb.value != w->sb.value ||
1476 cur->sb.pageIncrement != w->sb.pageIncrement ||
1477 cur->sb.increment != w->sb.increment)
1480 if (XtIsRealized ((Widget) w))
1482 seg_pixel_sizes (w, &w->sb.above, &w->sb.ss, &w->sb.below);
1483 draw_slider (w, w->sb.above, w->sb.ss, w->sb.below);
1487 if (w->sb.shadowThickness > 5) w->sb.shadowThickness = 5;
1489 return do_redisplay;
1493 XlwScrollBarGetValues (Widget widget, int *value, int *sliderSize,
1494 int *increment, int *pageIncrement)
1496 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1498 if (w && XtClass ((Widget) w) == xlwScrollBarWidgetClass)
1500 if (value) *value = w->sb.value;
1501 if (sliderSize) *sliderSize = w->sb.sliderSize;
1502 if (increment) *increment = w->sb.increment;
1503 if (pageIncrement) *pageIncrement = w->sb.pageIncrement;
1508 XlwScrollBarSetValues (Widget widget, int value, int sliderSize,
1509 int increment, int pageIncrement, Boolean notify)
1511 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1513 if (w && XtClass ((Widget) w) == xlwScrollBarWidgetClass &&
1514 (w->sb.value != value ||
1515 w->sb.sliderSize != sliderSize ||
1516 w->sb.increment != increment ||
1517 w->sb.pageIncrement != pageIncrement))
1519 int last_value = w->sb.value;
1521 w->sb.value = value;
1522 w->sb.sliderSize = sliderSize;
1523 w->sb.increment = increment;
1524 w->sb.pageIncrement = pageIncrement;
1528 if (XtIsRealized (widget))
1530 seg_pixel_sizes (w, &w->sb.above, &w->sb.ss, &w->sb.below);
1531 draw_slider (w, w->sb.above, w->sb.ss, w->sb.below);
1533 if (w->sb.value != last_value && notify)
1534 call_callbacks (w, XmCR_VALUE_CHANGED, w->sb.value, 0, NULL);
1539 /*-------------------------- Action Functions ---------------------------*/
1542 timer (XtPointer data, XtIntervalId *id)
1544 XlwScrollBarWidget w = (XlwScrollBarWidget) data;
1545 w->sb.timerActive = False;
1547 if (w->sb.armed != ARM_NONE)
1549 int last_value = w->sb.value;
1552 switch (w->sb.armed)
1555 decrement_value (w, w->sb.pageIncrement);
1556 reason = XmCR_PAGE_DECREMENT;
1559 increment_value (w, w->sb.pageIncrement);
1560 reason = XmCR_PAGE_INCREMENT;
1563 decrement_value (w, w->sb.increment);
1564 reason = XmCR_DECREMENT;
1567 increment_value (w, w->sb.increment);
1568 reason = XmCR_INCREMENT;
1576 if (last_value != w->sb.value)
1578 seg_pixel_sizes (w, &w->sb.above, &w->sb.ss, &w->sb.below);
1579 draw_slider (w, w->sb.above, w->sb.ss, w->sb.below);
1581 call_callbacks (w, reason, w->sb.value, 0, NULL);
1584 XtAppAddTimeOut (XtWidgetToApplicationContext ((Widget) w),
1585 (unsigned long) w->sb.repeatDelay,
1586 timer, (XtPointer) w);
1587 w->sb.timerActive = True;
1593 what_button (XlwScrollBarWidget w, int mouse_x, int mouse_y)
1595 int width = widget_w (w);
1596 int height = widget_h (w);
1597 int arrow_height = arrow_h (w);
1599 mouse_x -= widget_x (w);
1600 mouse_y -= widget_y (w);
1602 if (mouse_x < 0 || mouse_x >= width ||
1603 mouse_y < 0 || mouse_y >= height)
1606 if (w->sb.showArrows)
1608 if (mouse_y >= (height -= arrow_height))
1609 return BUTTON_DOWN_ARROW;
1611 if (arrow_same_end (w))
1613 if (mouse_y >= (height -= arrow_height))
1614 return BUTTON_UP_ARROW;
1617 if ( (mouse_y -= arrow_height) < 0)
1618 return BUTTON_UP_ARROW;
1621 if ( (mouse_y -= w->sb.above) < 0)
1622 return BUTTON_TROUGH_ABOVE;
1624 if ( (mouse_y -= w->sb.ss) < 0)
1625 return BUTTON_SLIDER;
1627 return BUTTON_TROUGH_BELOW;
1631 Select (Widget widget, XEvent *event, String *parms, Cardinal *num_parms)
1633 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1634 button_where sb_button;
1636 int mouse_x = event_x (w, event);
1637 int mouse_y = event_y (w, event);
1639 int last_value = w->sb.savedValue = w->sb.value;
1640 int reason = XmCR_NONE;
1642 XtGrabKeyboard ((Widget) w, False, GrabModeAsync, GrabModeAsync,
1643 event->xbutton.time);
1645 sb_button = what_button (w, mouse_x, mouse_y);
1647 if (w->sb.forced_scroll != FORCED_SCROLL_NONE)
1651 case BUTTON_TROUGH_ABOVE:
1652 case BUTTON_TROUGH_BELOW:
1654 sb_button= BUTTON_NONE; /* cause next switch to fall through */
1655 if (w->sb.forced_scroll == FORCED_SCROLL_UPLEFT)
1657 decrement_value (w, w->sb.pageIncrement);
1658 w->sb.armed = ARM_PAGEUP;
1659 reason = XmCR_PAGE_DECREMENT;
1662 else if (w->sb.forced_scroll == FORCED_SCROLL_DOWNRIGHT)
1664 increment_value (w, w->sb.pageIncrement);
1665 w->sb.armed = ARM_PAGEDOWN;
1666 reason = XmCR_PAGE_INCREMENT;
1677 case BUTTON_TROUGH_ABOVE:
1678 decrement_value (w, w->sb.pageIncrement);
1679 w->sb.armed = ARM_PAGEUP;
1680 reason = XmCR_PAGE_DECREMENT;
1682 case BUTTON_TROUGH_BELOW:
1683 increment_value (w, w->sb.pageIncrement);
1684 w->sb.armed = ARM_PAGEDOWN;
1685 reason = XmCR_PAGE_INCREMENT;
1688 w->sb.lastY = mouse_y;
1689 w->sb.armed = ARM_SLIDER;
1690 draw_slider (w, w->sb.above, w->sb.ss, w->sb.below);
1692 case BUTTON_UP_ARROW:
1693 if (event->xbutton.state & ControlMask)
1695 w->sb.value = w->sb.minimum;
1696 reason = XmCR_TO_TOP;
1700 decrement_value (w, w->sb.increment);
1701 reason = XmCR_DECREMENT;
1703 w->sb.armed = ARM_UP;
1704 redraw_up_arrow (w, True, False);
1706 case BUTTON_DOWN_ARROW:
1707 if (event->xbutton.state & ControlMask)
1709 w->sb.value = w->sb.maximum;
1710 reason = XmCR_TO_BOTTOM;
1714 increment_value (w, w->sb.increment);
1715 reason = XmCR_INCREMENT;
1717 w->sb.armed = ARM_DOWN;
1718 redraw_down_arrow (w, True, False);
1726 if (last_value != w->sb.value)
1728 seg_pixel_sizes (w, &w->sb.above, &w->sb.ss, &w->sb.below);
1729 draw_slider (w, w->sb.above, w->sb.ss, w->sb.below);
1731 call_callbacks (w, reason, w->sb.value, mouse_y, event);
1733 if (w->sb.timerActive)
1734 XtRemoveTimeOut (w->sb.timerId);
1737 XtAppAddTimeOut (XtWidgetToApplicationContext ((Widget) w),
1738 (unsigned long) w->sb.initialDelay,
1739 timer, (XtPointer) w);
1740 w->sb.timerActive = True;
1747 PageDownOrRight (Widget widget, XEvent *event, String *parms, Cardinal *num_parms)
1749 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1750 w->sb.forced_scroll = FORCED_SCROLL_DOWNRIGHT;
1751 Select (widget, event, parms, num_parms);
1752 w->sb.forced_scroll = FORCED_SCROLL_NONE;
1756 PageUpOrLeft (Widget widget, XEvent *event, String *parms, Cardinal *num_parms)
1758 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1759 w->sb.forced_scroll = FORCED_SCROLL_UPLEFT;
1760 Select (widget, event, parms, num_parms);
1761 w->sb.forced_scroll = FORCED_SCROLL_NONE;
1765 Drag (Widget widget, XEvent *event, String *parms, Cardinal *num_parms)
1767 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1769 if (w->sb.armed == ARM_SLIDER)
1771 int mouse_y = event_y (w, event);
1772 int diff = mouse_y - w->sb.lastY;
1774 if (diff < -(w->sb.above)) /* up */
1776 mouse_y -= (diff + w->sb.above);
1777 diff = -(w->sb.above);
1779 else if (diff > w->sb.below) /* down */
1781 mouse_y -= (diff - w->sb.below);
1787 w->sb.above += diff;
1788 w->sb.below -= diff;
1790 draw_slider (w, w->sb.above, w->sb.ss, w->sb.below);
1792 w->sb.lastY = mouse_y;
1794 w->sb.value = value_from_pixel (w, w->sb.above);
1798 call_callbacks (w, XmCR_DRAG, w->sb.value, event_y (w, event), event);
1805 Release (Widget widget, XEvent *event, String *parms, Cardinal *num_parms)
1807 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1809 switch (w->sb.armed)
1812 call_callbacks (w, XmCR_VALUE_CHANGED, w->sb.value, event_y (w, event), event);
1813 w->sb.armed = ARM_NONE;
1814 draw_slider (w, w->sb.above, w->sb.ss, w->sb.below);
1817 redraw_up_arrow (w, False, False);
1820 redraw_down_arrow (w, False, False);
1826 XtUngrabKeyboard ((Widget) w, event->xbutton.time);
1828 w->sb.armed = ARM_NONE;
1832 Jump (Widget widget, XEvent *event, String *parms, Cardinal *num_parms)
1834 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1837 int mouse_x = event_x (w, event);
1838 int mouse_y = event_y (w, event);
1840 int scroll_region_y = widget_y (w);
1841 int scroll_region_h = widget_h (w);
1843 if (w->sb.showArrows)
1845 int arrow_height = arrow_h (w);
1846 scroll_region_h -= 2 * arrow_height;
1847 if (!arrow_same_end (w))
1848 scroll_region_y += arrow_height;
1851 XtGrabKeyboard ((Widget) w, False, GrabModeAsync, GrabModeAsync,
1852 event->xbutton.time);
1854 switch (what_button (w, mouse_x, mouse_y))
1856 case BUTTON_TROUGH_ABOVE:
1857 case BUTTON_TROUGH_BELOW:
1859 w->sb.savedValue = w->sb.value;
1861 last_value = w->sb.value;
1863 w->sb.above = mouse_y - (w->sb.ss / 2) - scroll_region_y;
1864 if (w->sb.above < 0)
1866 else if (w->sb.above + w->sb.ss > scroll_region_h)
1867 w->sb.above = scroll_region_h - w->sb.ss;
1869 w->sb.below = scroll_region_h - w->sb.ss - w->sb.above;
1871 w->sb.armed = ARM_SLIDER;
1872 draw_slider (w, w->sb.above, w->sb.ss, w->sb.below);
1874 w->sb.value = value_from_pixel (w, w->sb.above);
1878 w->sb.lastY = mouse_y;
1880 if (w->sb.value != last_value)
1881 call_callbacks (w, XmCR_DRAG, w->sb.value, mouse_y, event);
1891 Abort (Widget widget, XEvent *event, String *parms, Cardinal *num_parms)
1893 XlwScrollBarWidget w = (XlwScrollBarWidget) widget;
1895 if (w->sb.armed != ARM_NONE)
1897 if (w->sb.value != w->sb.savedValue)
1899 w->sb.value = w->sb.savedValue;
1901 seg_pixel_sizes (w, &w->sb.above, &w->sb.ss, &w->sb.below);
1902 draw_slider (w, w->sb.above, w->sb.ss, w->sb.below);
1904 call_callbacks (w, XmCR_VALUE_CHANGED, w->sb.value,
1905 event_y (w, event), event);
1908 switch (w->sb.armed)
1910 case ARM_UP: redraw_up_arrow (w, False, False); break;
1911 case ARM_DOWN: redraw_down_arrow (w, False, False); break;
1912 default: ; /* Do nothing */
1915 w->sb.armed = ARM_NONE;
1917 XtUngrabKeyboard ((Widget) w, event->xbutton.time);