home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xaw / Scrollbar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-04  |  20.8 KB  |  750 lines

  1. /* $XConsortium: Scrollbar.c,v 1.69 91/05/04 23:07:32 keith Exp $ */
  2.  
  3. /***********************************************************
  4. Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts,
  5. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  6.  
  7.                         All Rights Reserved
  8.  
  9. Permission to use, copy, modify, and distribute this software and its 
  10. documentation for any purpose and without fee is hereby granted, 
  11. provided that the above copyright notice appear in all copies and that
  12. both that copyright notice and this permission notice appear in 
  13. supporting documentation, and that the names of Digital or MIT not be
  14. used in advertising or publicity pertaining to distribution of the
  15. software without specific, written prior permission.  
  16.  
  17. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  18. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  19. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  20. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  22. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  23. SOFTWARE.
  24.  
  25. ******************************************************************/
  26.  
  27. /* ScrollBar.c */
  28. /* created by weissman, Mon Jul  7 13:20:03 1986 */
  29. /* converted by swick, Thu Aug 27 1987 */
  30.  
  31. #include <X11/IntrinsicP.h>
  32. #include <X11/StringDefs.h>
  33.  
  34. #include <X11/Xaw/XawInit.h>
  35. #include <X11/Xaw/ScrollbarP.h>
  36. #include <X11/Xmu/Drawing.h>
  37.  
  38. /* Private definitions. */
  39.  
  40. static char defaultTranslations[] =
  41.     "<Btn1Down>:   StartScroll(Forward) \n\
  42.      <Btn2Down>:   StartScroll(Continuous) MoveThumb() NotifyThumb() \n\
  43.      <Btn3Down>:   StartScroll(Backward) \n\
  44.      <Btn2Motion>: MoveThumb() NotifyThumb() \n\
  45.      <BtnUp>:      NotifyScroll(Proportional) EndScroll()";
  46.  
  47. #ifdef bogusScrollKeys
  48.     /* examples */
  49.     "<KeyPress>f:  StartScroll(Forward) NotifyScroll(FullLength) EndScroll()"
  50.     "<KeyPress>b:  StartScroll(Backward) NotifyScroll(FullLength) EndScroll()"
  51. #endif
  52.  
  53. static float floatZero = 0.0;
  54.  
  55. #define Offset(field) XtOffsetOf(ScrollbarRec, field)
  56.  
  57. static XtResource resources[] = {
  58.   {XtNlength, XtCLength, XtRDimension, sizeof(Dimension),
  59.        Offset(scrollbar.length), XtRImmediate, (XtPointer) 1},
  60.   {XtNthickness, XtCThickness, XtRDimension, sizeof(Dimension),
  61.        Offset(scrollbar.thickness), XtRImmediate, (XtPointer) 14},
  62.   {XtNorientation, XtCOrientation, XtROrientation, sizeof(XtOrientation),
  63.       Offset(scrollbar.orientation), XtRImmediate, (XtPointer) XtorientVertical},
  64.   {XtNscrollProc, XtCCallback, XtRCallback, sizeof(XtPointer),
  65.        Offset(scrollbar.scrollProc), XtRCallback, NULL},
  66.   {XtNthumbProc, XtCCallback, XtRCallback, sizeof(XtPointer),
  67.        Offset(scrollbar.thumbProc), XtRCallback, NULL},
  68.   {XtNjumpProc, XtCCallback, XtRCallback, sizeof(XtPointer),
  69.        Offset(scrollbar.jumpProc), XtRCallback, NULL},
  70.   {XtNthumb, XtCThumb, XtRBitmap, sizeof(Pixmap),
  71.        Offset(scrollbar.thumb), XtRImmediate, (XtPointer) XtUnspecifiedPixmap},
  72.   {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
  73.        Offset(scrollbar.foreground), XtRString, XtDefaultForeground},
  74.   {XtNshown, XtCShown, XtRFloat, sizeof(float),
  75.        Offset(scrollbar.shown), XtRFloat, (XtPointer)&floatZero},
  76.   {XtNtopOfThumb, XtCTopOfThumb, XtRFloat, sizeof(float),
  77.        Offset(scrollbar.top), XtRFloat, (XtPointer)&floatZero},
  78.   {XtNscrollVCursor, XtCCursor, XtRCursor, sizeof(Cursor),
  79.        Offset(scrollbar.verCursor), XtRString, "sb_v_double_arrow"},
  80.   {XtNscrollHCursor, XtCCursor, XtRCursor, sizeof(Cursor),
  81.        Offset(scrollbar.horCursor), XtRString, "sb_h_double_arrow"},
  82.   {XtNscrollUCursor, XtCCursor, XtRCursor, sizeof(Cursor),
  83.        Offset(scrollbar.upCursor), XtRString, "sb_up_arrow"},
  84.   {XtNscrollDCursor, XtCCursor, XtRCursor, sizeof(Cursor),
  85.        Offset(scrollbar.downCursor), XtRString, "sb_down_arrow"},
  86.   {XtNscrollLCursor, XtCCursor, XtRCursor, sizeof(Cursor),
  87.        Offset(scrollbar.leftCursor), XtRString, "sb_left_arrow"},
  88.   {XtNscrollRCursor, XtCCursor, XtRCursor, sizeof(Cursor),
  89.        Offset(scrollbar.rightCursor), XtRString, "sb_right_arrow"},
  90.   {XtNminimumThumb, XtCMinimumThumb, XtRDimension, sizeof(Dimension),
  91.        Offset(scrollbar.min_thumb), XtRImmediate, (XtPointer) 7},
  92. };
  93. #undef Offset
  94.  
  95. static void ClassInitialize();
  96. static void Initialize();
  97. static void Destroy();
  98. static void Realize();
  99. static void Resize();
  100. static void Redisplay();
  101. static Boolean SetValues();
  102.  
  103. static void StartScroll();
  104. static void MoveThumb();
  105. static void NotifyThumb();
  106. static void NotifyScroll();
  107. static void EndScroll();
  108.  
  109. static XtActionsRec actions[] = {
  110.     {"StartScroll",        StartScroll},
  111.     {"MoveThumb",        MoveThumb},
  112.     {"NotifyThumb",        NotifyThumb},
  113.     {"NotifyScroll",    NotifyScroll},
  114.     {"EndScroll",        EndScroll},
  115. };
  116.  
  117.  
  118. ScrollbarClassRec scrollbarClassRec = {
  119.   { /* core fields */
  120.     /* superclass       */      (WidgetClass) &simpleClassRec,
  121.     /* class_name       */      "Scrollbar",
  122.     /* size             */      sizeof(ScrollbarRec),
  123.     /* class_initialize    */    ClassInitialize,
  124.     /* class_part_init  */    NULL,
  125.     /* class_inited    */    FALSE,
  126.     /* initialize       */      Initialize,
  127.     /* initialize_hook  */    NULL,
  128.     /* realize          */      Realize,
  129.     /* actions          */      actions,
  130.     /* num_actions    */    XtNumber(actions),
  131.     /* resources        */      resources,
  132.     /* num_resources    */      XtNumber(resources),
  133.     /* xrm_class        */      NULLQUARK,
  134.     /* compress_motion    */    TRUE,
  135.     /* compress_exposure*/    TRUE,
  136.     /* compress_enterleave*/    TRUE,
  137.     /* visible_interest */      FALSE,
  138.     /* destroy          */      Destroy,
  139.     /* resize           */      Resize,
  140.     /* expose           */      Redisplay,
  141.     /* set_values       */      SetValues,
  142.     /* set_values_hook  */    NULL,
  143.     /* set_values_almost */    XtInheritSetValuesAlmost,
  144.     /* get_values_hook  */    NULL,
  145.     /* accept_focus     */      NULL,
  146.     /* version          */    XtVersion,
  147.     /* callback_private */      NULL,
  148.     /* tm_table         */      defaultTranslations,
  149.     /* query_geometry    */    XtInheritQueryGeometry,
  150.     /* display_accelerator*/    XtInheritDisplayAccelerator,
  151.     /* extension        */    NULL
  152.   },
  153.   { /* simple fields */
  154.     /* change_sensitive         */      XtInheritChangeSensitive
  155.   },
  156.   { /* scrollbar fields */
  157.     /* ignore                   */      0
  158.   }
  159.  
  160. };
  161.  
  162. WidgetClass scrollbarWidgetClass = (WidgetClass)&scrollbarClassRec;
  163.  
  164. #define NoButton -1
  165. #define PICKLENGTH(widget, x, y) \
  166.     ((widget->scrollbar.orientation == XtorientHorizontal) ? x : y)
  167. #define MIN(x,y)    ((x) < (y) ? (x) : (y))
  168. #define MAX(x,y)    ((x) > (y) ? (x) : (y))
  169.  
  170. static void ClassInitialize()
  171. {
  172.     XawInitializeWidgetSet();
  173.     XtAddConverter( XtRString, XtROrientation, XmuCvtStringToOrientation,
  174.             NULL, (Cardinal)0 );
  175. }
  176.  
  177. /*
  178.  * Make sure the first number is within the range specified by the other
  179.  * two numbers.
  180.  */
  181.  
  182. static int InRange(num, small, big)
  183. int num, small, big;
  184. {
  185.     return (num < small) ? small : ((num > big) ? big : num);
  186. }
  187.  
  188. /*
  189.  * Same as above, but for floating numbers. 
  190.  */
  191.  
  192. static float FloatInRange(num, small, big)
  193. float num, small, big;
  194. {
  195.     return (num < small) ? small : ((num > big) ? big : num);
  196. }
  197.  
  198.  
  199. /* Fill the area specified by top and bottom with the given pattern. */
  200. static float FractionLoc(w, x, y)
  201.   ScrollbarWidget w;
  202.   int x, y;
  203. {
  204.     float   result;
  205.  
  206.     result = PICKLENGTH(w, x / (float)w->core.width,
  207.                 y / (float)w->core.height);
  208.     return FloatInRange(result, 0.0, 1.0);
  209. }
  210.  
  211.  
  212. static FillArea(w, top, bottom, thumb)
  213.   ScrollbarWidget w;
  214.   Position top, bottom;
  215.   int thumb;
  216. {
  217.     Dimension length = bottom-top;
  218.  
  219.     if (bottom < 0) return;
  220.  
  221.     switch(thumb) {
  222.     /* Fill the new Thumb location */
  223.       case 1:
  224.     if (w->scrollbar.orientation == XtorientHorizontal) 
  225.         XFillRectangle(XtDisplay(w), XtWindow(w),
  226.                w->scrollbar.gc, top, 1, length,
  227.                w->core.height-2);
  228.     
  229.     else XFillRectangle(XtDisplay(w), XtWindow(w), w->scrollbar.gc,
  230.                 1, top, w->core.width-2, length);
  231.  
  232.     break;
  233.     /* Clear the old Thumb location */
  234.       case 0:
  235.     if (w->scrollbar.orientation == XtorientHorizontal) 
  236.         XClearArea(XtDisplay(w), XtWindow(w), top, 1,
  237.                length, w->core.height-2, FALSE);
  238.     
  239.     else XClearArea(XtDisplay(w), XtWindow(w), 1,
  240.             top, w->core.width-2, length, FALSE);
  241.  
  242.     }  
  243. }
  244.  
  245.  
  246. /* Paint the thumb in the area specified by w->top and
  247.    w->shown.  The old area is erased.  The painting and
  248.    erasing is done cleverly so that no flickering will occur. */
  249.  
  250. static void PaintThumb( w )
  251.   ScrollbarWidget w;
  252. {
  253.     Position oldtop, oldbot, newtop, newbot;
  254.  
  255.     oldtop = w->scrollbar.topLoc;
  256.     oldbot = oldtop + w->scrollbar.shownLength;
  257.     newtop = w->scrollbar.length * w->scrollbar.top;
  258.     newbot = newtop + (int)(w->scrollbar.length * w->scrollbar.shown);
  259.     if (newbot < newtop + (int)w->scrollbar.min_thumb) 
  260.       newbot = newtop + w->scrollbar.min_thumb;
  261.     w->scrollbar.topLoc = newtop;
  262.     w->scrollbar.shownLength = newbot - newtop;
  263.  
  264.     if (XtIsRealized((Widget)w)) {
  265.     if (newtop < oldtop) FillArea(w, newtop, MIN(newbot, oldtop), 1);
  266.     if (newtop > oldtop) FillArea(w, oldtop, MIN(newtop, oldbot), 0);
  267.     if (newbot < oldbot) FillArea(w, MAX(newbot, oldtop), oldbot, 0);
  268.     if (newbot > oldbot) FillArea(w, MAX(newtop, oldbot), newbot, 1);
  269.     }
  270. }
  271.  
  272.  
  273. static void SetDimensions(w)
  274.     ScrollbarWidget w;
  275. {
  276.     if (w->scrollbar.orientation == XtorientVertical) {
  277.     w->scrollbar.length = w->core.height;
  278.     w->scrollbar.thickness = w->core.width;
  279.     }
  280.     else {
  281.     w->scrollbar.length = w->core.width;
  282.     w->scrollbar.thickness = w->core.height;
  283.     }
  284. }
  285.  
  286. /*    Function Name: Destroy
  287.  *    Description: Called as the scrollbar is going away...
  288.  *    Arguments: w - the scrollbar.
  289.  *    Returns: nonw
  290.  */
  291.  
  292. static void
  293. Destroy(w)
  294. Widget w;
  295. {
  296.     ScrollbarWidget sbw = (ScrollbarWidget) w;
  297.     
  298.     XtReleaseGC(w, sbw->scrollbar.gc);
  299. }
  300.  
  301. /*    Function Name: CreateGC
  302.  *    Description: Creates the GC.
  303.  *    Arguments: w - the scrollbar widget.
  304.  *    Returns: none. 
  305.  */
  306.  
  307. static void
  308. CreateGC(w)
  309. Widget w;
  310. {
  311.     ScrollbarWidget sbw = (ScrollbarWidget) w;
  312.     XGCValues gcValues;
  313.     XtGCMask mask;
  314.     unsigned int depth = 1;
  315.  
  316.     if (sbw->scrollbar.thumb == XtUnspecifiedPixmap) {
  317.         sbw->scrollbar.thumb = XmuCreateStippledPixmap (XtScreen(w), 
  318.                             (Pixel) 1, (Pixel) 0,
  319.                             depth);
  320.     }
  321.     else if (sbw->scrollbar.thumb != None) {
  322.     Window root;
  323.     int x, y;
  324.     unsigned int width, height, bw;
  325.     if (XGetGeometry(XtDisplay(w), sbw->scrollbar.thumb, &root, &x, &y,
  326.              &width, &height, &bw, &depth) == 0) {
  327.         XtAppError(XtWidgetToApplicationContext(w),
  328.            "Scrollbar Widget: Could not get geometry of thumb pixmap.");
  329.     }
  330.     }
  331.  
  332.     gcValues.foreground = sbw->scrollbar.foreground;
  333.     gcValues.background = sbw->core.background_pixel;
  334.     mask = GCForeground | GCBackground;
  335.  
  336.     if (sbw->scrollbar.thumb != None) {
  337.     if (depth == 1) {
  338.         gcValues.fill_style = FillOpaqueStippled;
  339.         gcValues.stipple = sbw->scrollbar.thumb;
  340.         mask |= GCFillStyle | GCStipple;
  341.     }
  342.     else {
  343.         gcValues.fill_style = FillTiled;
  344.         gcValues.tile = sbw->scrollbar.thumb;
  345.         mask |= GCFillStyle | GCTile;
  346.     }
  347.     }
  348.     sbw->scrollbar.gc = XtGetGC( w, mask, &gcValues);
  349. }
  350.  
  351. /* ARGSUSED */
  352. static void Initialize( request, new )
  353.    Widget request;        /* what the client asked for */
  354.    Widget new;            /* what we're going to give him */
  355. {
  356.     ScrollbarWidget w = (ScrollbarWidget) new;
  357.  
  358.     CreateGC(new);
  359.  
  360.     if (w->core.width == 0)
  361.     w->core.width = (w->scrollbar.orientation == XtorientVertical)
  362.         ? w->scrollbar.thickness : w->scrollbar.length;
  363.  
  364.     if (w->core.height == 0)
  365.     w->core.height = (w->scrollbar.orientation == XtorientHorizontal)
  366.         ? w->scrollbar.thickness : w->scrollbar.length;
  367.  
  368.     SetDimensions( w );
  369.     w->scrollbar.direction = 0;
  370.     w->scrollbar.topLoc = 0;
  371.     w->scrollbar.shownLength = w->scrollbar.min_thumb;
  372. }
  373.  
  374. static void Realize( gw, valueMask, attributes )
  375.    Widget gw;
  376.    Mask *valueMask;
  377.    XSetWindowAttributes *attributes;
  378. {
  379.     ScrollbarWidget w = (ScrollbarWidget) gw;
  380.  
  381.     w->scrollbar.inactiveCursor =
  382.       (w->scrollbar.orientation == XtorientVertical)
  383.     ? w->scrollbar.verCursor
  384.     : w->scrollbar.horCursor;
  385.  
  386.     XtVaSetValues(gw, XtNcursor, w->scrollbar.inactiveCursor, NULL);
  387.  
  388.     /* 
  389.      * The Simple widget actually stuffs the value in the valuemask. 
  390.      */
  391.     
  392.     (*scrollbarWidgetClass->core_class.superclass->core_class.realize)
  393.     (gw, valueMask, attributes);
  394. }
  395.  
  396. /* ARGSUSED */
  397. static Boolean 
  398. SetValues( current, request, desired )
  399. Widget current,        /* what I am */
  400.        request,        /* what he wants me to be */
  401.        desired;        /* what I will become */
  402. {
  403.     ScrollbarWidget w = (ScrollbarWidget) current;
  404.     ScrollbarWidget dw = (ScrollbarWidget) desired;
  405.     Boolean redraw = FALSE;
  406.  
  407. /*
  408.  * If these values are outside the acceptable range ignore them...
  409.  */
  410.  
  411.     if (dw->scrollbar.top < 0.0 || dw->scrollbar.top > 1.0)
  412.         dw->scrollbar.top = w->scrollbar.top;
  413.  
  414.     if (dw->scrollbar.shown < 0.0 || dw->scrollbar.shown > 1.0)
  415.         dw->scrollbar.shown = w->scrollbar.shown;
  416.  
  417. /*
  418.  * Change colors and stuff...
  419.  */
  420.  
  421.     if ( XtIsRealized (desired) ) {
  422.     if ( (w->scrollbar.foreground != dw->scrollbar.foreground) ||
  423.         (w->core.background_pixel != dw->core.background_pixel) ||
  424.         (w->scrollbar.thumb != dw->scrollbar.thumb) ) 
  425.     {
  426.         XtReleaseGC((Widget)dw, w->scrollbar.gc);
  427.         CreateGC( (Widget) dw);
  428.         redraw = TRUE;
  429.     }
  430.     if (w->scrollbar.top != dw->scrollbar.top ||
  431.         w->scrollbar.shown != dw->scrollbar.shown)
  432.         redraw = TRUE;
  433.     }
  434.  
  435.     return( redraw );
  436. }
  437.  
  438. static void Resize( gw )
  439.    Widget gw;
  440. {
  441.     /* ForgetGravity has taken care of background, but thumb may
  442.      * have to move as a result of the new size. */
  443.     SetDimensions( (ScrollbarWidget)gw );
  444.     Redisplay( gw, (XEvent*)NULL, (Region)NULL );
  445. }
  446.  
  447.  
  448. /* ARGSUSED */
  449. static void Redisplay( gw, event, region )
  450.    Widget gw;
  451.    XEvent *event;
  452.    Region region;
  453. {
  454.     ScrollbarWidget w = (ScrollbarWidget) gw;
  455.     int x, y;
  456.     unsigned int width, height;
  457.  
  458.     if (w->scrollbar.orientation == XtorientHorizontal) {
  459.     x = w->scrollbar.topLoc;
  460.     y = 1;
  461.     width = w->scrollbar.shownLength;
  462.     height = w->core.height - 2;
  463.     } else {
  464.     x = 1;
  465.     y = w->scrollbar.topLoc;
  466.     width = w->core.width - 2;
  467.     height = w->scrollbar.shownLength;
  468.     }
  469.  
  470.     if ( (region == NULL) ||
  471.      (XRectInRegion(region, x, y, width, height) != RectangleOut) ) {
  472.     /* Forces entire thumb to be painted. */
  473.     w->scrollbar.topLoc = -(w->scrollbar.length + 1);
  474.     PaintThumb( w ); 
  475.     }
  476. }
  477.  
  478.  
  479. /* ARGSUSED */
  480. static void StartScroll( gw, event, params, num_params )
  481.   Widget gw;
  482.   XEvent *event;
  483.   String *params;        /* direction: Back|Forward|Smooth */
  484.   Cardinal *num_params;        /* we only support 1 */
  485. {
  486.     ScrollbarWidget w = (ScrollbarWidget) gw;
  487.     Cursor cursor;
  488.     char direction;
  489.  
  490.     if (w->scrollbar.direction != 0) return; /* if we're already scrolling */
  491.     if (*num_params > 0) direction = *params[0];
  492.     else         direction = 'C';
  493.  
  494.     w->scrollbar.direction = direction;
  495.  
  496.     switch( direction ) {
  497.     case 'B':
  498.     case 'b':    cursor = (w->scrollbar.orientation == XtorientVertical)
  499.                    ? w->scrollbar.downCursor
  500.                    : w->scrollbar.rightCursor; break;
  501.  
  502.     case 'F':
  503.     case 'f':    cursor = (w->scrollbar.orientation == XtorientVertical)
  504.                    ? w->scrollbar.upCursor
  505.                    : w->scrollbar.leftCursor; break;
  506.  
  507.     case 'C':
  508.     case 'c':    cursor = (w->scrollbar.orientation == XtorientVertical)
  509.                    ? w->scrollbar.rightCursor
  510.                    : w->scrollbar.upCursor; break;
  511.  
  512.     default:    return; /* invalid invocation */
  513.     }
  514.  
  515.     XtVaSetValues(gw, XtNcursor, cursor, NULL);
  516.  
  517.     XFlush(XtDisplay(w));
  518. }
  519.  
  520.  
  521. static Boolean CompareEvents( oldEvent, newEvent )
  522.     XEvent *oldEvent, *newEvent;
  523. {
  524. #define Check(field) if (newEvent->field != oldEvent->field) return False;
  525.  
  526.     Check(xany.display);
  527.     Check(xany.type);
  528.     Check(xany.window);
  529.  
  530.     switch( newEvent->type ) {
  531.       case MotionNotify:
  532.         Check(xmotion.state); break;
  533.       case ButtonPress:
  534.       case ButtonRelease:
  535.         Check(xbutton.state);
  536.         Check(xbutton.button); break;
  537.       case KeyPress:
  538.       case KeyRelease:
  539.         Check(xkey.state);
  540.         Check(xkey.keycode); break;
  541.       case EnterNotify:
  542.       case LeaveNotify:
  543.         Check(xcrossing.mode);
  544.         Check(xcrossing.detail);
  545.         Check(xcrossing.state); break;
  546.     }
  547. #undef Check
  548.  
  549.     return True;
  550. }
  551.  
  552. struct EventData {
  553.     XEvent *oldEvent;
  554.     int count;
  555. };
  556.  
  557. static Bool PeekNotifyEvent( dpy, event, args )
  558.     Display *dpy;
  559.     XEvent *event;
  560.     char *args;
  561. {
  562.     struct EventData *eventData = (struct EventData*)args;
  563.  
  564.     return ((++eventData->count == QLength(dpy)) /* since PeekIf blocks */
  565.         || CompareEvents(event, eventData->oldEvent));
  566. }
  567.  
  568.  
  569. static Boolean LookAhead( w, event )
  570.     Widget w;
  571.     XEvent *event;
  572. {
  573.     XEvent newEvent;
  574.     struct EventData eventData;
  575.  
  576.     if (QLength(XtDisplay(w)) == 0) return False;
  577.  
  578.     eventData.count = 0;
  579.     eventData.oldEvent = event;
  580.  
  581.     XPeekIfEvent(XtDisplay(w), &newEvent, PeekNotifyEvent, (char*)&eventData);
  582.  
  583.     if (CompareEvents(event, &newEvent))
  584.     return True;
  585.     else
  586.     return False;
  587. }
  588.  
  589.  
  590. static void ExtractPosition( event, x, y )
  591.     XEvent *event;
  592.     Position *x, *y;        /* RETURN */
  593. {
  594.     switch( event->type ) {
  595.       case MotionNotify:
  596.         *x = event->xmotion.x;     *y = event->xmotion.y;      break;
  597.       case ButtonPress:
  598.       case ButtonRelease:
  599.         *x = event->xbutton.x;   *y = event->xbutton.y;   break;
  600.       case KeyPress:
  601.       case KeyRelease:
  602.         *x = event->xkey.x;      *y = event->xkey.y;      break;
  603.       case EnterNotify:
  604.       case LeaveNotify:
  605.         *x = event->xcrossing.x; *y = event->xcrossing.y; break;
  606.       default:
  607.         *x = 0; *y = 0;
  608.     }
  609. }
  610.  
  611. static void NotifyScroll( gw, event, params, num_params   )
  612.    Widget gw;
  613.    XEvent *event;
  614.    String *params;        /* style: Proportional|FullLength */
  615.    Cardinal *num_params;    /* we only support 1 */
  616. {
  617.     ScrollbarWidget w = (ScrollbarWidget) gw;
  618.     int call_data;
  619.     char style;
  620.     Position x, y;
  621.  
  622.     if (w->scrollbar.direction == 0) return; /* if no StartScroll */
  623.  
  624.     if (LookAhead(gw, event)) return;
  625.  
  626.     if (*num_params > 0) style = *params[0];
  627.     else         style = 'P';
  628.  
  629.     switch( style ) {
  630.         case 'P':    /* Proportional */
  631.         case 'p':    ExtractPosition( event, &x, &y );
  632.              call_data = InRange( PICKLENGTH( w, x, y ),
  633.                       0,
  634.                       (int) w->scrollbar.length); break;
  635.  
  636.         case 'F':    /* FullLength */
  637.         case 'f':    call_data = w->scrollbar.length; break;
  638.     }
  639.  
  640.     switch( w->scrollbar.direction ) {
  641.         case 'B':
  642.         case 'b':    call_data = -call_data;
  643.                /* fall through */
  644.         case 'F':
  645.     case 'f':    XtCallCallbacks( gw, XtNscrollProc, (XtPointer)call_data);
  646.                  break;
  647.  
  648.         case 'C':
  649.     case 'c':    /* NotifyThumb has already called the thumbProc(s) */
  650.              break;
  651.     }
  652. }
  653.  
  654. /* ARGSUSED */
  655. static void EndScroll(gw, event, params, num_params )
  656.    Widget gw;
  657.    XEvent *event;        /* unused */
  658.    String *params;        /* unused */
  659.    Cardinal *num_params;    /* unused */
  660. {
  661.     ScrollbarWidget w = (ScrollbarWidget) gw;
  662.  
  663.     XtVaSetValues(gw, XtNcursor, w->scrollbar.inactiveCursor, NULL);
  664.     XFlush(XtDisplay(w));    /* make sure it get propogated. */
  665.  
  666.     w->scrollbar.direction = 0;
  667. }
  668.  
  669.  
  670. /* ARGSUSED */
  671. static void MoveThumb( gw, event, params, num_params )
  672.    Widget gw;
  673.    XEvent *event;
  674.    String *params;        /* unused */
  675.    Cardinal *num_params;    /* unused */
  676. {
  677.     ScrollbarWidget w = (ScrollbarWidget) gw;
  678.     Position x, y;
  679.  
  680.     if (w->scrollbar.direction == 0) return; /* if no StartScroll */
  681.  
  682.     if (LookAhead(gw, event)) return;
  683.  
  684.     if (!event->xmotion.same_screen) return;
  685.  
  686.     ExtractPosition( event, &x, &y );
  687.     w->scrollbar.top = FractionLoc(w, x, y);
  688.     PaintThumb(w);
  689.     XFlush(XtDisplay(w));    /* re-draw it before Notifying */
  690. }
  691.  
  692.  
  693. /* ARGSUSED */
  694. static void NotifyThumb( gw, event, params, num_params )
  695.    Widget gw;
  696.    XEvent *event;
  697.    String *params;        /* unused */
  698.    Cardinal *num_params;    /* unused */
  699. {
  700.     register ScrollbarWidget w = (ScrollbarWidget) gw;
  701.  
  702.     if (w->scrollbar.direction == 0) return; /* if no StartScroll */
  703.  
  704.     if (LookAhead(gw, event)) return;
  705.  
  706.     /* thumbProc is not pretty, but is necessary for backwards
  707.        compatibility on those architectures for which it work{s,ed};
  708.        the intent is to pass a (truncated) float by value. */
  709.     XtCallCallbacks( gw, XtNthumbProc, *(XtPointer*)&w->scrollbar.top);
  710.     XtCallCallbacks( gw, XtNjumpProc, (XtPointer)&w->scrollbar.top);
  711. }
  712.  
  713.  
  714.  
  715. /************************************************************
  716.  *
  717.  *  Public routines. 
  718.  *
  719.  ************************************************************/
  720.  
  721. /* Set the scroll bar to the given location. */
  722.  
  723. #if NeedFunctionPrototypes
  724. void XawScrollbarSetThumb(Widget gw,
  725. #if NeedWidePrototypes
  726.               double top, double shown)
  727. #else
  728.               float top, float shown)
  729. #endif
  730. #else
  731. void XawScrollbarSetThumb( gw, top, shown )
  732. Widget gw;
  733. float top, shown;
  734. #endif
  735. {
  736.     ScrollbarWidget w = (ScrollbarWidget)gw;
  737.  
  738.     if (w->scrollbar.direction == 'c') return; /* if still thumbing */
  739.  
  740.     w->scrollbar.top = (top > 1.0) ? 1.0 :
  741.                (top >= 0.0) ? top :
  742.                w->scrollbar.top;
  743.  
  744.     w->scrollbar.shown = (shown > 1.0) ? 1.0 :
  745.              (shown >= 0.0) ? shown :
  746.                  w->scrollbar.shown;
  747.  
  748.     PaintThumb( w );
  749. }
  750.