summaryrefslogtreecommitdiff
path: root/xc/lib/Xaw/Scrollbar.c
blob: 3e204a45081c9351af8a56165e1f22b6b40f3a55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
/* $TOG: Scrollbar.c /main/73 1998/02/06 12:48:56 kaleb $ */

/***********************************************************

Copyright 1987, 1988, 1994, 1998  The Open Group

All Rights Reserved.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.


Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in 
supporting documentation, and that the name of Digital not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.  

DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

******************************************************************/
/* $XFree86: xc/lib/Xaw/Scrollbar.c,v 1.9 1999/06/06 08:48:08 dawes Exp $ */

#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#include <X11/Xmu/Drawing.h>
#include <X11/Xaw/ScrollbarP.h>
#include <X11/Xaw/XawInit.h>
#include "Private.h"

#define NoButton	-1
#define PICKLENGTH(widget, x, y)					\
(((widget)->scrollbar.orientation == XtorientHorizontal) ? (x) : (y))

/*
 * Class Methods
 */
static void XawScrollbarClassInitialize(void);
static void XawScrollbarDestroy(Widget);
static void XawScrollbarInitialize(Widget, Widget, ArgList, Cardinal*_args);
static void XawScrollbarRealize(Widget, Mask*, XSetWindowAttributes*);
static void XawScrollbarRedisplay(Widget, XEvent*, Region);
static void XawScrollbarResize(Widget);
static Boolean XawScrollbarSetValues(Widget, Widget, Widget,
				     ArgList, Cardinal*);

/*
 * Prototypes
 */
static Boolean CompareEvents(XEvent*, XEvent*);
static void CreateGC(Widget);
static float FloatInRange(float, float, float);
static float FractionLoc(ScrollbarWidget, int, int);
static void ExtractPosition(XEvent*, Position*, Position*);
static int InRange(int, int, int);
static void FillArea(ScrollbarWidget, int, int, int);
static Bool LookAhead(Widget, XEvent*);
static void PaintThumb(ScrollbarWidget);
static Bool PeekNotifyEvent(Display*, XEvent*, char*);
static void SetDimensions(ScrollbarWidget);

/*
 * Actions
 */
static void EndScroll(Widget, XEvent*, String*, Cardinal*);
static void MoveThumb(Widget, XEvent*, String*, Cardinal*);
static void NotifyScroll(Widget, XEvent*, String*, Cardinal*);
static void NotifyThumb(Widget, XEvent*, String*, Cardinal*);
static void StartScroll(Widget, XEvent*, String*, Cardinal*);

/*
 * Initialization
 */
static char defaultTranslations[] =
"<Btn1Down>:"	"StartScroll(Forward)\n"
"<Btn2Down>:"	"StartScroll(Continuous) MoveThumb() NotifyThumb()\n"
"<Btn3Down>:"	"StartScroll(Backward)\n"
"<Btn2Motion>:"	"MoveThumb() NotifyThumb()\n"
"<BtnUp>:"	"NotifyScroll(Proportional) EndScroll()\n";

static float floatZero = 0.0;

#define Offset(field) XtOffsetOf(ScrollbarRec, field)

static XtResource resources[] = {
  {
    XtNlength,
    XtCLength,
    XtRDimension,
    sizeof(Dimension),
    Offset(scrollbar.length),
    XtRImmediate,
    (XtPointer)1
  },
  {
    XtNthickness,
    XtCThickness,
    XtRDimension,
    sizeof(Dimension),
    Offset(scrollbar.thickness),
    XtRImmediate,
    (XtPointer)14
  },
  {
    XtNorientation,
    XtCOrientation,
    XtROrientation,
    sizeof(XtOrientation),
    Offset(scrollbar.orientation),
    XtRImmediate,
    (XtPointer)XtorientVertical
  },
  {
    XtNscrollProc,
    XtCCallback,
    XtRCallback,
    sizeof(XtPointer),
    Offset(scrollbar.scrollProc),
    XtRCallback,
    NULL
  },
  {
    XtNthumbProc,
    XtCCallback,
    XtRCallback,
    sizeof(XtPointer),
    Offset(scrollbar.thumbProc),
    XtRCallback,
    NULL
  },
  {
    XtNjumpProc,
    XtCCallback,
    XtRCallback,
    sizeof(XtPointer),
    Offset(scrollbar.jumpProc),
    XtRCallback,
    NULL
  },
  {
    XtNthumb,
    XtCThumb,
    XtRBitmap,
    sizeof(Pixmap),
    Offset(scrollbar.thumb),
    XtRImmediate,
    (XtPointer)XtUnspecifiedPixmap
  },
  {
    XtNforeground,
    XtCForeground,
    XtRPixel,
    sizeof(Pixel),
    Offset(scrollbar.foreground),
    XtRString,
    XtDefaultForeground
  },
  {
    XtNshown,
    XtCShown,
    XtRFloat,
    sizeof(float),
    Offset(scrollbar.shown),
    XtRFloat,
    (XtPointer)&floatZero
  },
  {
    XtNtopOfThumb,
    XtCTopOfThumb,
    XtRFloat,
    sizeof(float),
    Offset(scrollbar.top),
    XtRFloat,
    (XtPointer)&floatZero
  },
  {
    XtNscrollVCursor,
    XtCCursor,
    XtRCursor,
    sizeof(Cursor),
    Offset(scrollbar.verCursor),
    XtRString,
    "sb_v_double_arrow"
  },
  {
    XtNscrollHCursor,
    XtCCursor,
    XtRCursor,
    sizeof(Cursor),
    Offset(scrollbar.horCursor),
    XtRString,
    "sb_h_double_arrow"
  },
  {
    XtNscrollUCursor,
    XtCCursor,
    XtRCursor,
    sizeof(Cursor),
    Offset(scrollbar.upCursor),
    XtRString,
    "sb_up_arrow"
  },
  {
    XtNscrollDCursor,
    XtCCursor,
    XtRCursor,
    sizeof(Cursor),
    Offset(scrollbar.downCursor),
    XtRString,
    "sb_down_arrow"
  },
  {
    XtNscrollLCursor,
    XtCCursor,
    XtRCursor,
    sizeof(Cursor),
    Offset(scrollbar.leftCursor),
    XtRString,
    "sb_left_arrow"
  },
  {
    XtNscrollRCursor,
    XtCCursor,
    XtRCursor,
    sizeof(Cursor),
    Offset(scrollbar.rightCursor),
    XtRString,
    "sb_right_arrow"
  },
  {
    XtNminimumThumb,
    XtCMinimumThumb,
    XtRDimension,
    sizeof(Dimension),
    Offset(scrollbar.min_thumb),
    XtRImmediate,
    (XtPointer)7
  },
};
#undef Offset

static XtActionsRec actions[] = {
	{"StartScroll",		StartScroll},
	{"MoveThumb",		MoveThumb},
	{"NotifyThumb",		NotifyThumb},
	{"NotifyScroll",	NotifyScroll},
	{"EndScroll",		EndScroll},
};

#define Superclass (&simpleClassRec)
ScrollbarClassRec scrollbarClassRec = {
  /* core */
  {
    (WidgetClass)&simpleClassRec,	/* superclass */
    "Scrollbar",			/* class_name */
    sizeof(ScrollbarRec),		/* widget_size */
    XawScrollbarClassInitialize,	/* class_initialize */
    NULL,				/* class_part_init */
    False,				/* class_inited */
    XawScrollbarInitialize,		/* initialize */
    NULL,				/* initialize_hook */
    XawScrollbarRealize,		/* realize */
    actions,				/* actions */
    XtNumber(actions),			/* num_actions */
    resources,				/* resources */
    XtNumber(resources),		/* num_resources */
    NULLQUARK,				/* xrm_class */
    True,				/* compress_motion */
    True,				/* compress_exposure */
    True,				/* compress_enterleave */
    False,				/* visible_interest */
    XawScrollbarDestroy,		/* destroy */
    XawScrollbarResize,			/* resize */
    XawScrollbarRedisplay,		/* expose */
    XawScrollbarSetValues,		/* set_values */
    NULL,				/* set_values_hook */
    XtInheritSetValuesAlmost,		/* set_values_almost */
    NULL,				/* get_values_hook */
    NULL,				/* accept_focus */
    XtVersion,				/* version */
    NULL,				/* callback_private */
    defaultTranslations,		/* tm_table */
    XtInheritQueryGeometry,		/* query_geometry */
    XtInheritDisplayAccelerator,	/* display_accelerator */
    NULL,				/* extension */
  },
  /* simple */
  {
    XtInheritChangeSensitive,		/* change_sensitive */
  },
  /* scrollbar */
  {
    NULL,				/* extension */
  },
};

WidgetClass scrollbarWidgetClass = (WidgetClass)&scrollbarClassRec;

/*
 * Implementation
 */
static void
XawScrollbarClassInitialize(void)
{
    XawInitializeWidgetSet();
    XtAddConverter(XtRString, XtROrientation, XmuCvtStringToOrientation,
		   NULL, 0);
    XtSetTypeConverter(XtROrientation, XtRString, XmuCvtOrientationToString,
		       NULL, 0, XtCacheNone, NULL);
}

/*
 * Make sure the first number is within the range specified by the other
 * two numbers.
 */
static int
InRange(int num, int small, int big)
{
    return ((num < small) ? small : ((num > big) ? big : num));
}

/*
 * Same as above, but for floating numbers
 */
static float
FloatInRange(float num, float small, float big)
{
    return ((num < small) ? small : ((num > big) ? big : num));
}

/* Fill the area specified by top and bottom with the given pattern */
static float
FractionLoc(ScrollbarWidget w, int x, int y)
{
    float   result;

    result = PICKLENGTH(w, x / (float)XtWidth(w), y / (float)XtHeight(w));

    return (FloatInRange(result, 0.0, 1.0));
}

static void
FillArea(ScrollbarWidget w, int top, int bottom, int thumb)
{
    Dimension length;

    top = XawMax(1, top);
    if (w->scrollbar.orientation == XtorientHorizontal) 
    bottom = XawMin(bottom, XtWidth(w) - 1);
    else
    bottom = XawMin(bottom, XtHeight(w) - 1);

    if (bottom <= top)
	return;

    length = bottom - top;

    switch(thumb) {
	/* Fill the new Thumb location */
	case 1:
	    if (w->scrollbar.orientation == XtorientHorizontal) 
		XFillRectangle(XtDisplay(w), XtWindow(w), w->scrollbar.gc,
			       top, 1, length, XtHeight(w) - 2);
	    else
		XFillRectangle(XtDisplay(w), XtWindow(w), w->scrollbar.gc,
			       1, top, XtWidth(w) - 2, length);
	    break;
	/* Clear the old Thumb location */
	case 0:
	    if (w->scrollbar.orientation == XtorientHorizontal) 
		XClearArea(XtDisplay(w), XtWindow(w),
			   top, 1, length, XtHeight(w) - 2, False);
	    else
		XClearArea(XtDisplay(w), XtWindow(w),
			   1, top, XtWidth(w) - 2, length, False);
	    break;
    }
}


/* Paint the thumb in the area specified by w->top and
   w->shown.  The old area is erased.  The painting and
   erasing is done cleverly so that no flickering will occur. */
static void
PaintThumb(ScrollbarWidget w)
{
    Position oldtop, oldbot, newtop, newbot;

    oldtop = w->scrollbar.topLoc;
    oldbot = oldtop + w->scrollbar.shownLength;
    newtop = w->scrollbar.length * w->scrollbar.top;
    newbot = newtop + (int)(w->scrollbar.length * w->scrollbar.shown);
    if (newbot < newtop + (int)w->scrollbar.min_thumb) 
	newbot = newtop + w->scrollbar.min_thumb;
    w->scrollbar.topLoc = newtop;
    w->scrollbar.shownLength = newbot - newtop;

    if (XtIsRealized((Widget)w)) {
	if (newtop < oldtop)
	    FillArea(w, newtop, XawMin(newbot, oldtop), 1);
	if (newtop > oldtop)
	    FillArea(w, oldtop, XawMin(newtop, oldbot), 0);
	if (newbot < oldbot)
	    FillArea(w, XawMax(newbot, oldtop), oldbot, 0);
	if (newbot > oldbot)
	    FillArea(w, XawMax(newtop, oldbot), newbot, 1);
    }
}

static void
SetDimensions(ScrollbarWidget w)
{
    if (w->scrollbar.orientation == XtorientVertical) {
	w->scrollbar.length = XtHeight(w);
	w->scrollbar.thickness = XtWidth(w);
    }
    else {
	w->scrollbar.length = XtWidth(w);
	w->scrollbar.thickness = XtHeight(w);
    }
}

static void
XawScrollbarDestroy(Widget w)
{
    ScrollbarWidget sbw = (ScrollbarWidget)w;
    
    XtReleaseGC(w, sbw->scrollbar.gc);
}

static void
CreateGC(Widget w)
{
    ScrollbarWidget sbw = (ScrollbarWidget)w;
    XGCValues gcValues;
    XtGCMask mask;
    unsigned int depth = 1;

    if (sbw->scrollbar.thumb == XtUnspecifiedPixmap)
	sbw->scrollbar.thumb = XmuCreateStippledPixmap(XtScreen(w),
						       (Pixel)1, (Pixel)0,
						       depth);
    else if (sbw->scrollbar.thumb != None) {
	Window root;
	int x, y;
	unsigned int width, height, bw;

	XGetGeometry(XtDisplay(w), sbw->scrollbar.thumb, &root, &x, &y,
		     &width, &height, &bw, &depth);
    }

    gcValues.foreground = sbw->scrollbar.foreground;
    gcValues.background = sbw->core.background_pixel;
    mask = GCForeground | GCBackground;

    if (sbw->scrollbar.thumb != None) {
	if (depth == 1) {
	    gcValues.fill_style = FillOpaqueStippled;
	    gcValues.stipple = sbw->scrollbar.thumb;
	    mask |= GCFillStyle | GCStipple;
	}
	else {
	    gcValues.fill_style = FillTiled;
	    gcValues.tile = sbw->scrollbar.thumb;
	    mask |= GCFillStyle | GCTile;
	}
    }
    sbw->scrollbar.gc = XtGetGC(w, mask, &gcValues);
}

/* ARGSUSED */
static void
XawScrollbarInitialize(Widget request, Widget cnew,
		       ArgList args, Cardinal *num_args)
{
    ScrollbarWidget w = (ScrollbarWidget)cnew;

    CreateGC(cnew);

    if (XtWidth(w) == 0)
	XtWidth(w) = w->scrollbar.orientation == XtorientVertical ?
			w->scrollbar.thickness : w->scrollbar.length;

    if (XtHeight(w) == 0)
	XtHeight(w) = w->scrollbar.orientation == XtorientHorizontal ?
			w->scrollbar.thickness : w->scrollbar.length;

    SetDimensions(w);
    w->scrollbar.direction = 0;
    w->scrollbar.topLoc = 0;
    w->scrollbar.shownLength = w->scrollbar.min_thumb;
}

static void
XawScrollbarRealize(Widget gw, Mask *valueMask,
		    XSetWindowAttributes *attributes)
{
    ScrollbarWidget w = (ScrollbarWidget)gw;

    w->scrollbar.inactiveCursor = w->scrollbar.orientation == XtorientVertical ?
		w->scrollbar.verCursor : w->scrollbar.horCursor;

    XtVaSetValues(gw, XtNcursor, w->scrollbar.inactiveCursor, NULL);

    /* 
     * The Simple widget actually stuffs the value in the valuemask
     */
    (*scrollbarWidgetClass->core_class.superclass->core_class.realize)
	(gw, valueMask, attributes);
}

/*ARGSUSED*/
static Boolean 
XawScrollbarSetValues(Widget current, Widget request, Widget desired,
		      ArgList args, Cardinal *num_args)
{
    ScrollbarWidget w = (ScrollbarWidget)current;
    ScrollbarWidget dw = (ScrollbarWidget)desired;
    Boolean redraw = False;

    /*
     * If these values are outside the acceptable range ignore them...
     */
    if (dw->scrollbar.top < 0.0 || dw->scrollbar.top > 1.0)
	dw->scrollbar.top = w->scrollbar.top;

    if (dw->scrollbar.shown < 0.0 || dw->scrollbar.shown > 1.0)
	dw->scrollbar.shown = w->scrollbar.shown;

    if (XtIsRealized (desired)) {
	if (w->scrollbar.foreground != dw->scrollbar.foreground ||
	    w->core.background_pixel != dw->core.background_pixel ||
	    w->scrollbar.thumb != dw->scrollbar.thumb) {
	    XtReleaseGC((Widget)dw, w->scrollbar.gc);
	    CreateGC((Widget)dw);
	    redraw = True;
	}
	if (w->scrollbar.top != dw->scrollbar.top ||
	    w->scrollbar.shown != dw->scrollbar.shown)
	    redraw = True;
    }

    return (redraw);
}

static void
XawScrollbarResize(Widget gw)
{
    /* ForgetGravity has taken care of background, but thumb may
     * have to move as a result of the new size. */
    SetDimensions((ScrollbarWidget)gw);
    XawScrollbarRedisplay(gw, NULL, NULL);
}

/*ARGSUSED*/
static void
XawScrollbarRedisplay(Widget gw, XEvent *event, Region region)
{
    ScrollbarWidget w = (ScrollbarWidget)gw;
    int x, y;
    unsigned int width, height;

    if (Superclass->core_class.expose)
	(*Superclass->core_class.expose)(gw, event, region);

    if (w->scrollbar.orientation == XtorientHorizontal) {
	x = w->scrollbar.topLoc;
	y = 1;
	width = w->scrollbar.shownLength;
	height = XtHeight(w) - 2;
    }
    else {
	x = 1;
	y = w->scrollbar.topLoc;
	width = XtWidth(w) - 2;
	height = w->scrollbar.shownLength;
    }

    if (region == NULL ||
	XRectInRegion(region, x, y, width, height) != RectangleOut) {
	/* Forces entire thumb to be painted */
	w->scrollbar.topLoc = -(w->scrollbar.length + 1);
	PaintThumb(w);
    }
}

/*ARGSUSED*/
static void
StartScroll(Widget gw, XEvent *event, String *params, Cardinal *num_params)
{
    ScrollbarWidget w = (ScrollbarWidget)gw;
    Cursor cursor;
    char direction;

    if (w->scrollbar.direction != 0)	/* if we're already scrolling */
	return;
    if (*num_params > 0)
	direction = *params[0];
    else
	direction = 'C';

    w->scrollbar.direction = direction;

    switch(direction) {
	case 'B':
	case 'b':
	    cursor = w->scrollbar.orientation == XtorientVertical ?
		w->scrollbar.downCursor : w->scrollbar.rightCursor;
	    break;
	case 'F':
	case 'f':
	    cursor = w->scrollbar.orientation == XtorientVertical ?
		w->scrollbar.upCursor : w->scrollbar.leftCursor;
	    break;
	case 'C':
	case 'c':
	     cursor = w->scrollbar.orientation == XtorientVertical ?
		w->scrollbar.rightCursor : w->scrollbar.upCursor;
	     break;
	default:
	     return;	/* invalid invocation */
    }

    XtVaSetValues(gw, XtNcursor, cursor, NULL);

    XFlush(XtDisplay(w));
}

static Boolean
CompareEvents(XEvent *oldEvent, XEvent *newEvent)
{
#define Check(field) if (newEvent->field != oldEvent->field) return (False)

    Check(xany.display);
    Check(xany.type);
    Check(xany.window);

    switch(newEvent->type) {
	case MotionNotify:
	    Check(xmotion.state);
	    break;
	case ButtonPress:
	case ButtonRelease:
	    Check(xbutton.state);
	    Check(xbutton.button);
	    break;
	case KeyPress:
	case KeyRelease:
	    Check(xkey.state);
	    Check(xkey.keycode);
	    break;
	case EnterNotify:
	case LeaveNotify:
	    Check(xcrossing.mode);
	    Check(xcrossing.detail);
	    Check(xcrossing.state);
	    break;
    }
#undef Check

    return (True);
}

struct EventData {
    XEvent *oldEvent;
    int count;
};

static Bool
PeekNotifyEvent(Display *dpy, XEvent *event, char *args)
{
    struct EventData *eventData = (struct EventData*)args;

    return (++eventData->count == QLength(dpy)	/* since PeekIf blocks */
	    || CompareEvents(event, eventData->oldEvent));
}

static Bool
LookAhead(Widget w, XEvent *event)
{
    XEvent newEvent;
    struct EventData eventData;

    if (QLength(XtDisplay(w)) == 0)
	return (False);

    eventData.count = 0;
    eventData.oldEvent = event;

    XPeekIfEvent(XtDisplay(w), &newEvent, PeekNotifyEvent, (char*)&eventData);

    if (CompareEvents(event, &newEvent))
	return (True);

    return (False);
}

static void
ExtractPosition(XEvent *event, Position *x, Position *y)
{
    switch(event->type) {
	case MotionNotify:
	    *x = event->xmotion.x;
	    *y = event->xmotion.y;
	    break;
	case ButtonPress:
	case ButtonRelease:
	    *x = event->xbutton.x;
	    *y = event->xbutton.y;
	    break;
	case KeyPress:
	case KeyRelease:
	    *x = event->xkey.x;
	    *y = event->xkey.y;
	    break;
	case EnterNotify:
	case LeaveNotify:
	    *x = event->xcrossing.x;
	    *y = event->xcrossing.y;
	    break;
	default:
	    *x = 0;
	    *y = 0;
	    break;
    }
}

static void
NotifyScroll(Widget gw, XEvent *event, String *params, Cardinal *num_params)
{
    ScrollbarWidget w = (ScrollbarWidget)gw;
    int call_data = 0;
    char style;
    Position x, y;

    if (w->scrollbar.direction == 0)	/* if no StartScroll */
	return;

    if (LookAhead(gw, event))
	return;

    if (*num_params > 0)
	style = *params[0];
    else
	style = 'P';

    switch(style) {
	case 'P':    /* Proportional */
	case 'p':
	    ExtractPosition(event, &x, &y);
	    call_data = InRange(PICKLENGTH(w, x, y), 0, (int)w->scrollbar.length);
	    break;
	case 'F':    /* FullLength */
	case 'f':
	    call_data = w->scrollbar.length;
	    break;
    }

    switch(w->scrollbar.direction) {
	case 'B':
	case 'b':
	    call_data = -call_data;
	    /*FALLTHROUGH*/
	case 'F':
	case 'f':
	    XtCallCallbacks(gw, XtNscrollProc, (XtPointer)call_data);
	    break;
	case 'C':
	case 'c':    /* NotifyThumb has already called the thumbProc(s) */
	    break;
    }
}

/*ARGSUSED*/
static void
EndScroll(Widget gw, XEvent *event, String *params, Cardinal *num_params)
{
    ScrollbarWidget w = (ScrollbarWidget)gw;

    XtVaSetValues(gw, XtNcursor, w->scrollbar.inactiveCursor, NULL);
    XFlush(XtDisplay(w));		/* make sure it get propogated */

    w->scrollbar.direction = 0;
}

/*ARGSUSED*/
static void
MoveThumb(Widget gw, XEvent *event, String *params, Cardinal *num_params)
{
    ScrollbarWidget w = (ScrollbarWidget)gw;
    Position x, y;

    if (w->scrollbar.direction == 0)	/* if no StartScroll */
	return;

    if (LookAhead(gw, event))
	return;

    if (!event->xmotion.same_screen)
	return;

    ExtractPosition(event, &x, &y);
    w->scrollbar.top = FractionLoc(w, x, y);
}

/*ARGSUSED*/
static void
NotifyThumb(Widget gw, XEvent *event, String *params, Cardinal *num_params)
{
    ScrollbarWidget w = (ScrollbarWidget)gw;

    if (w->scrollbar.direction == 0)	/* if no StartScroll */
	return;

    if (LookAhead(gw, event))
	return;

    /* thumbProc is not pretty, but is necessary for backwards
       compatibility on those architectures for which it work{s,ed};
       the intent is to pass a (truncated) float by value. */
    XtCallCallbacks(gw, XtNthumbProc, *(XtPointer*)&w->scrollbar.top);
    XtCallCallbacks(gw, XtNjumpProc, (XtPointer)&w->scrollbar.top);

    PaintThumb(w);
}

/*
 *  Public routines
 */
/* Set the scroll bar to the given location. */
void
XawScrollbarSetThumb(Widget gw,
#if NeedWidePrototypes
		     double top, double shown
#else
		     float top, float shown
#endif
		     )
{
    ScrollbarWidget w = (ScrollbarWidget)gw;

    if (w->scrollbar.direction == 'c')	/* if still thumbing */
	return;

    w->scrollbar.top = top > 1.0 ? 1.0 : top >= 0.0 ? top : w->scrollbar.top;

    w->scrollbar.shown = shown > 1.0 ? 1.0 : shown >= 0.0 ?
				shown : w->scrollbar.shown;
    PaintThumb(w);
}