1 | /* |
---|
2 | * $Id: ScrollBar.c,v 1.2 1999-01-22 23:16:57 ghudson Exp $ |
---|
3 | * |
---|
4 | * Copyright 1990, 1991 by the Massachusetts Institute of Technology. |
---|
5 | * |
---|
6 | * For copying and distribution information, please see the file |
---|
7 | * <mit-copyright.h>. |
---|
8 | * |
---|
9 | */ |
---|
10 | |
---|
11 | #if (!defined(lint)) && (!defined(SABER)) |
---|
12 | static char rcsid[] = |
---|
13 | "$Id: ScrollBar.c,v 1.2 1999-01-22 23:16:57 ghudson Exp $"; |
---|
14 | #endif |
---|
15 | |
---|
16 | #include "mit-copyright.h" |
---|
17 | #include <stdio.h> |
---|
18 | #include "Jets.h" |
---|
19 | #include "ScrollBar.h" |
---|
20 | #include <X11/cursorfont.h> |
---|
21 | |
---|
22 | #define offset(field) XjOffset(ScrollBarJet,field) |
---|
23 | |
---|
24 | static XjResource resources[] = { |
---|
25 | { XjNchangeProc, XjCChangeProc, XjRCallback, sizeof(XjCallback *), |
---|
26 | offset(scrollBar.changeProc), XjRString, NULL }, |
---|
27 | { XjNx, XjCX, XjRInt, sizeof(int), |
---|
28 | offset(core.x), XjRString, XjInheritValue }, |
---|
29 | { XjNy, XjCY, XjRInt, sizeof(int), |
---|
30 | offset(core.y), XjRString, XjInheritValue }, |
---|
31 | { XjNwidth, XjCWidth, XjRInt, sizeof(int), |
---|
32 | offset(core.width), XjRString, "15" }, |
---|
33 | { XjNheight, XjCHeight, XjRInt, sizeof(int), |
---|
34 | offset(core.height), XjRString, "100" }, |
---|
35 | { XjNborderWidth, XjCBorderWidth, XjRInt, sizeof(int), |
---|
36 | offset(scrollBar.borderWidth), XjRString, "1" }, |
---|
37 | { XjNborderThickness, XjCBorderThickness, XjRInt, sizeof(int), |
---|
38 | offset(scrollBar.borderThickness), XjRString, "1" }, |
---|
39 | { XjNborderColor, XjCBorderColor, XjRColor, sizeof(int), |
---|
40 | offset(scrollBar.borderColor), XjRColor, (caddr_t)-1 }, |
---|
41 | { XjNpadding, XjCPadding, XjRInt, sizeof(int), |
---|
42 | offset(scrollBar.padding), XjRString, "5" }, |
---|
43 | { XjNreverseVideo, XjCReverseVideo, XjRBoolean, sizeof(Boolean), |
---|
44 | offset(scrollBar.reverseVideo), XjRBoolean, (caddr_t) False }, |
---|
45 | { XjNforeground, XjCForeground, XjRColor, sizeof(int), |
---|
46 | offset(scrollBar.foreground), XjRString, XjDefaultForeground }, |
---|
47 | { XjNbackground, XjCBackground, XjRColor, sizeof(int), |
---|
48 | offset(scrollBar.background), XjRString, XjDefaultBackground }, |
---|
49 | { XjNthumb, XjCThumb, XjRPixmap, sizeof(XjPixmap *), |
---|
50 | offset(scrollBar.thumb), XjRString, NULL }, |
---|
51 | { XjNverticalCursorCode, XjCCursorCode, XjRInt, sizeof(int), |
---|
52 | offset(scrollBar.vertCursorCode), XjRInt, |
---|
53 | (caddr_t) XC_sb_v_double_arrow }, |
---|
54 | { XjNhorizontalCursorCode, XjCCursorCode, XjRInt, sizeof(int), |
---|
55 | offset(scrollBar.horizCursorCode), XjRInt, |
---|
56 | (caddr_t) XC_sb_h_double_arrow }, |
---|
57 | { XjNupCursorCode, XjCCursorCode, XjRInt, sizeof(int), |
---|
58 | offset(scrollBar.upCursorCode), XjRInt, (caddr_t) XC_sb_up_arrow }, |
---|
59 | { XjNdownCursorCode, XjCCursorCode, XjRInt, sizeof(int), |
---|
60 | offset(scrollBar.downCursorCode), XjRInt, (caddr_t) XC_sb_down_arrow }, |
---|
61 | { XjNleftCursorCode, XjCCursorCode, XjRInt, sizeof(int), |
---|
62 | offset(scrollBar.leftCursorCode), XjRInt, (caddr_t) XC_sb_left_arrow }, |
---|
63 | { XjNrightCursorCode, XjCCursorCode, XjRInt, sizeof(int), |
---|
64 | offset(scrollBar.rightCursorCode), XjRInt, (caddr_t) XC_sb_right_arrow }, |
---|
65 | { XjNorientation, XjCOrientation, XjROrientation, sizeof(int), |
---|
66 | offset(scrollBar.orientation), XjRString, XjVertical }, |
---|
67 | { XjNshowArrows, XjCShowArrows, XjRBoolean, sizeof(Boolean), |
---|
68 | offset(scrollBar.showArrows), XjRBoolean, (caddr_t) False }, |
---|
69 | }; |
---|
70 | |
---|
71 | #undef offset |
---|
72 | |
---|
73 | static Boolean event_handler(); |
---|
74 | static void realize(), resize(), querySize(), |
---|
75 | move(), destroy(), initialize(); |
---|
76 | |
---|
77 | ScrollBarClassRec scrollBarClassRec = { |
---|
78 | { |
---|
79 | /* class name */ "ScrollBar", |
---|
80 | /* jet size */ sizeof(ScrollBarRec), |
---|
81 | /* classInitialize */ NULL, |
---|
82 | /* classInitialized? */ 1, |
---|
83 | /* initialize */ initialize, |
---|
84 | /* prerealize */ NULL, |
---|
85 | /* realize */ realize, |
---|
86 | /* event */ event_handler, |
---|
87 | /* expose */ NULL, |
---|
88 | /* querySize */ querySize, |
---|
89 | /* move */ move, |
---|
90 | /* resize */ resize, |
---|
91 | /* destroy */ destroy, |
---|
92 | /* resources */ resources, |
---|
93 | /* number of 'em */ XjNumber(resources) |
---|
94 | } |
---|
95 | }; |
---|
96 | |
---|
97 | JetClass scrollBarJetClass = (JetClass)&scrollBarClassRec; |
---|
98 | |
---|
99 | static void calcPos(me) |
---|
100 | ScrollBarJet me; |
---|
101 | { |
---|
102 | int range, pixrange, visible, pixvisible; |
---|
103 | |
---|
104 | range = me->scrollBar.maximum - me->scrollBar.minimum + 1; |
---|
105 | visible = MIN(me->scrollBar.visible, range); |
---|
106 | |
---|
107 | /* |
---|
108 | want to scale range into pixrange, but it may not be the |
---|
109 | transformation we really expect due to the minimum size |
---|
110 | constraint on the scrollbar thumb. So, the mapping we |
---|
111 | want is: |
---|
112 | (a.) normalize |
---|
113 | (b.) 0 -> 0 |
---|
114 | (c.) (range - 1) - visible + 1 -> height - pixvisible |
---|
115 | */ |
---|
116 | |
---|
117 | if (me->scrollBar.orientation == Vertical) |
---|
118 | { |
---|
119 | pixrange = me->core.height - 2 * me->scrollBar.borderWidth; |
---|
120 | pixvisible = me->scrollBar.thumbHeight; |
---|
121 | me->scrollBar.thumbY = me->core.y + me->scrollBar.borderWidth; |
---|
122 | |
---|
123 | if (range - visible != 0) |
---|
124 | me->scrollBar.thumbY += |
---|
125 | (me->scrollBar.current - me->scrollBar.minimum) * |
---|
126 | (pixrange - pixvisible) / (range - visible); |
---|
127 | } |
---|
128 | else |
---|
129 | { |
---|
130 | pixrange = me->core.width - 2 * me->scrollBar.borderWidth; |
---|
131 | pixvisible = me->scrollBar.thumbWidth; |
---|
132 | me->scrollBar.thumbX = me->core.x + me->scrollBar.borderWidth; |
---|
133 | |
---|
134 | if (range - visible != 0) |
---|
135 | me->scrollBar.thumbX += |
---|
136 | (me->scrollBar.current - me->scrollBar.minimum) * |
---|
137 | (pixrange - pixvisible) / (range - visible); |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | static int calcCurrent(me, button, thumbX, thumbY) |
---|
142 | ScrollBarJet me; |
---|
143 | unsigned int button; |
---|
144 | int thumbX, thumbY; |
---|
145 | { |
---|
146 | int range, pixrange, visible, pixvisible; |
---|
147 | int current, increment; |
---|
148 | |
---|
149 | range = me->scrollBar.maximum - me->scrollBar.minimum + 1; |
---|
150 | visible = MIN(me->scrollBar.visible, range); |
---|
151 | |
---|
152 | if (me->scrollBar.orientation == Vertical) |
---|
153 | { |
---|
154 | pixrange = me->core.height - 2 * me->scrollBar.borderWidth; |
---|
155 | pixvisible = me->scrollBar.thumbHeight; |
---|
156 | } |
---|
157 | else |
---|
158 | { |
---|
159 | pixrange = me->core.width - 2 * me->scrollBar.borderWidth; |
---|
160 | pixvisible = me->scrollBar.thumbWidth; |
---|
161 | } |
---|
162 | if (pixrange < 1) |
---|
163 | pixrange = 1; |
---|
164 | |
---|
165 | switch(button) |
---|
166 | { |
---|
167 | case Button1: |
---|
168 | case Button3: |
---|
169 | current = me->scrollBar.current; |
---|
170 | if (me->scrollBar.orientation == Vertical) |
---|
171 | increment = 1 + visible * |
---|
172 | (thumbY - me->core.y - me->scrollBar.borderWidth) |
---|
173 | / pixrange; |
---|
174 | else |
---|
175 | increment = 1 + visible * |
---|
176 | (thumbX - me->core.x - me->scrollBar.borderWidth) |
---|
177 | / pixrange; |
---|
178 | |
---|
179 | if (button == Button1) |
---|
180 | current += increment; |
---|
181 | else |
---|
182 | current -= increment; |
---|
183 | break; |
---|
184 | |
---|
185 | case Button2: |
---|
186 | default: |
---|
187 | current = me->scrollBar.minimum; |
---|
188 | if (pixrange - pixvisible != 0) |
---|
189 | { |
---|
190 | if (me->scrollBar.orientation == Vertical) |
---|
191 | increment = (thumbY - me->core.y - me->scrollBar.borderWidth) * |
---|
192 | (range - visible) / (pixrange - pixvisible); |
---|
193 | else |
---|
194 | increment = (thumbX - me->core.x - me->scrollBar.borderWidth) * |
---|
195 | (range - visible) / (pixrange - pixvisible); |
---|
196 | current += increment; |
---|
197 | } |
---|
198 | break; |
---|
199 | } |
---|
200 | |
---|
201 | return current; |
---|
202 | } |
---|
203 | |
---|
204 | static void initSizes(me) |
---|
205 | ScrollBarJet me; |
---|
206 | { |
---|
207 | int range, pixrange, visible; |
---|
208 | int size; |
---|
209 | |
---|
210 | /* range of possible values */ |
---|
211 | range = me->scrollBar.maximum - me->scrollBar.minimum + 1; |
---|
212 | if (me->scrollBar.orientation == Vertical) |
---|
213 | pixrange = me->core.height - 2 * me->scrollBar.borderWidth; |
---|
214 | else |
---|
215 | pixrange = me->core.width - 2 * me->scrollBar.borderWidth; |
---|
216 | |
---|
217 | /* how much of range can be seen */ |
---|
218 | visible = MIN(me->scrollBar.visible, range); |
---|
219 | |
---|
220 | size = MAX(pixrange * visible / range, 5); |
---|
221 | |
---|
222 | if (me->scrollBar.orientation == Vertical) |
---|
223 | { |
---|
224 | me->scrollBar.thumbHeight = size; |
---|
225 | me->scrollBar.thumbWidth = (me->core.width |
---|
226 | - 2 * me->scrollBar.borderWidth); |
---|
227 | me->scrollBar.thumbX = me->core.x + me->scrollBar.borderWidth; |
---|
228 | } |
---|
229 | else |
---|
230 | { |
---|
231 | me->scrollBar.thumbWidth = size; |
---|
232 | me->scrollBar.thumbHeight = (me->core.height |
---|
233 | - 2 * me->scrollBar.borderWidth); |
---|
234 | me->scrollBar.thumbY = me->core.y + me->scrollBar.borderWidth; |
---|
235 | } |
---|
236 | } |
---|
237 | |
---|
238 | static void initialize(me) |
---|
239 | ScrollBarJet me; |
---|
240 | { |
---|
241 | me->scrollBar.realized = 0; |
---|
242 | |
---|
243 | me->scrollBar.inside = 0; |
---|
244 | me->scrollBar.selected = 0; |
---|
245 | me->scrollBar.pressed = 0; |
---|
246 | me->scrollBar.minimum = 0; |
---|
247 | me->scrollBar.maximum = 10; |
---|
248 | me->scrollBar.current = 5; |
---|
249 | me->scrollBar.visible = 2; |
---|
250 | } |
---|
251 | |
---|
252 | /* |
---|
253 | * Things are currently broken screenwise. |
---|
254 | * It will be fun to fix later. :) |
---|
255 | */ |
---|
256 | static void realize(me) |
---|
257 | ScrollBarJet me; |
---|
258 | { |
---|
259 | unsigned long valuemask; |
---|
260 | XGCValues values; |
---|
261 | int swap; |
---|
262 | |
---|
263 | if (me->scrollBar.reverseVideo) |
---|
264 | { |
---|
265 | swap = me->scrollBar.foreground; |
---|
266 | me->scrollBar.foreground = me->scrollBar.background; |
---|
267 | me->scrollBar.background = swap; |
---|
268 | } |
---|
269 | |
---|
270 | values.function = GXcopy; |
---|
271 | values.graphics_exposures = False; |
---|
272 | |
---|
273 | if (me->scrollBar.borderThickness != 0) |
---|
274 | { |
---|
275 | values.foreground = (me->scrollBar.borderColor == -1) ? |
---|
276 | me->scrollBar.foreground : me->scrollBar.borderColor; |
---|
277 | values.line_width = me->scrollBar.borderThickness; |
---|
278 | valuemask = ( GCForeground | GCLineWidth | GCFunction |
---|
279 | | GCGraphicsExposures ); |
---|
280 | |
---|
281 | me->scrollBar.line_gc = XjCreateGC(me->core.display, |
---|
282 | me->core.window, |
---|
283 | valuemask, |
---|
284 | &values); |
---|
285 | } |
---|
286 | |
---|
287 | values.foreground = (me->scrollBar.borderColor == -1) ? |
---|
288 | me->scrollBar.foreground : me->scrollBar.borderColor; |
---|
289 | values.background = me->scrollBar.foreground; |
---|
290 | values.line_width = 0; |
---|
291 | valuemask = ( GCForeground | GCBackground | GCLineWidth |
---|
292 | | GCFunction | GCGraphicsExposures ); |
---|
293 | |
---|
294 | me->scrollBar.bg_gc = XjCreateGC(me->core.display, |
---|
295 | me->core.window, |
---|
296 | valuemask, |
---|
297 | &values); |
---|
298 | |
---|
299 | values.foreground = me->scrollBar.background; |
---|
300 | |
---|
301 | me->scrollBar.fg_gc = XjCreateGC(me->core.display, |
---|
302 | me->core.window, |
---|
303 | valuemask, |
---|
304 | &values); |
---|
305 | |
---|
306 | valuemask = ( GCForeground | GCBackground | GCFunction |
---|
307 | | GCGraphicsExposures ); |
---|
308 | |
---|
309 | me->scrollBar.thumb_gc = XjCreateGC(me->core.display, |
---|
310 | me->core.window, |
---|
311 | valuemask, |
---|
312 | &values); |
---|
313 | |
---|
314 | if (me->scrollBar.thumb != NULL) |
---|
315 | { /* this is a mighty annoying pain in the rear */ |
---|
316 | /* destroy p... check also Window.c */ |
---|
317 | Pixmap p; |
---|
318 | |
---|
319 | p = XjCreatePixmap(me->core.display, |
---|
320 | me->core.window, |
---|
321 | me->scrollBar.thumb->width, |
---|
322 | me->scrollBar.thumb->height, |
---|
323 | DefaultDepth(me->core.display, /* wrong... sigh. */ |
---|
324 | DefaultScreen(me->core.display))); |
---|
325 | |
---|
326 | XCopyPlane(me->core.display, |
---|
327 | me->scrollBar.thumb->pixmap, |
---|
328 | p, |
---|
329 | me->scrollBar.thumb_gc, |
---|
330 | 0, 0, |
---|
331 | me->scrollBar.thumb->width, |
---|
332 | me->scrollBar.thumb->height, |
---|
333 | 0, 0, 1); |
---|
334 | |
---|
335 | values.fill_style = FillTiled; |
---|
336 | values.tile = p; |
---|
337 | valuemask = GCTile | GCFillStyle; |
---|
338 | |
---|
339 | XChangeGC(me->core.display, |
---|
340 | me->scrollBar.thumb_gc, |
---|
341 | valuemask, |
---|
342 | &values); |
---|
343 | } |
---|
344 | |
---|
345 | if (me->scrollBar.upCursorCode != -1) |
---|
346 | me->scrollBar.upCursor = XjCreateFontCursor(me->core.display, |
---|
347 | me->scrollBar.upCursorCode); |
---|
348 | else |
---|
349 | me->scrollBar.upCursor = (Cursor) NULL; |
---|
350 | |
---|
351 | if (me->scrollBar.downCursorCode != -1) |
---|
352 | me->scrollBar.downCursor = XjCreateFontCursor(me->core.display, |
---|
353 | me->scrollBar.downCursorCode); |
---|
354 | else |
---|
355 | me->scrollBar.downCursor = (Cursor) NULL; |
---|
356 | |
---|
357 | if (me->scrollBar.leftCursorCode != -1) |
---|
358 | me->scrollBar.leftCursor = XjCreateFontCursor(me->core.display, |
---|
359 | me->scrollBar.leftCursorCode); |
---|
360 | else |
---|
361 | me->scrollBar.leftCursor = (Cursor) NULL; |
---|
362 | |
---|
363 | if (me->scrollBar.rightCursorCode != -1) |
---|
364 | me->scrollBar.rightCursor = XjCreateFontCursor(me->core.display, |
---|
365 | me->scrollBar.rightCursorCode); |
---|
366 | else |
---|
367 | me->scrollBar.rightCursor = (Cursor) NULL; |
---|
368 | |
---|
369 | if (me->scrollBar.vertCursorCode != -1) |
---|
370 | me->scrollBar.vertCursor = XjCreateFontCursor(me->core.display, |
---|
371 | me->scrollBar.vertCursorCode); |
---|
372 | else |
---|
373 | me->scrollBar.vertCursor = (Cursor) NULL; |
---|
374 | |
---|
375 | if (me->scrollBar.horizCursorCode != -1) |
---|
376 | me->scrollBar.horizCursor = XjCreateFontCursor(me->core.display, |
---|
377 | me->scrollBar.horizCursorCode); |
---|
378 | else |
---|
379 | me->scrollBar.horizCursor = (Cursor) NULL; |
---|
380 | |
---|
381 | |
---|
382 | XDefineCursor(me->core.display, me->core.window, |
---|
383 | (me->scrollBar.orientation == Vertical) |
---|
384 | ? me->scrollBar.vertCursor |
---|
385 | : me->scrollBar.horizCursor); |
---|
386 | |
---|
387 | /* |
---|
388 | * Usurp events for this window |
---|
389 | */ |
---|
390 | XjRegisterWindow(me->core.window, (Jet) me); |
---|
391 | XjSelectInput(me->core.display, me->core.window, |
---|
392 | ExposureMask | StructureNotifyMask | |
---|
393 | ButtonPressMask | ButtonReleaseMask | Button2MotionMask); |
---|
394 | |
---|
395 | me->scrollBar.realized = 1; |
---|
396 | |
---|
397 | initSizes(me); |
---|
398 | calcPos(me); |
---|
399 | } |
---|
400 | |
---|
401 | static void destroy(me) |
---|
402 | ScrollBarJet me; |
---|
403 | { |
---|
404 | XjFreeGC(me->core.display, me->scrollBar.thumb_gc); |
---|
405 | if (me->scrollBar.borderThickness != 0) |
---|
406 | XjFreeGC(me->core.display, me->scrollBar.line_gc); |
---|
407 | if (me->scrollBar.vertCursor != (Cursor) NULL) |
---|
408 | XjFreeCursor(me->core.display, me->scrollBar.vertCursor); |
---|
409 | if (me->scrollBar.horizCursor != (Cursor) NULL) |
---|
410 | XjFreeCursor(me->core.display, me->scrollBar.horizCursor); |
---|
411 | if (me->scrollBar.upCursor != (Cursor) NULL) |
---|
412 | XjFreeCursor(me->core.display, me->scrollBar.upCursor); |
---|
413 | if (me->scrollBar.downCursor != (Cursor) NULL) |
---|
414 | XjFreeCursor(me->core.display, me->scrollBar.downCursor); |
---|
415 | if (me->scrollBar.rightCursor != (Cursor) NULL) |
---|
416 | XjFreeCursor(me->core.display, me->scrollBar.rightCursor); |
---|
417 | if (me->scrollBar.leftCursor != (Cursor) NULL) |
---|
418 | XjFreeCursor(me->core.display, me->scrollBar.leftCursor); |
---|
419 | |
---|
420 | XjUnregisterWindow(me->core.window, (Jet) me); |
---|
421 | } |
---|
422 | |
---|
423 | static void querySize(me, size) |
---|
424 | ScrollBarJet me; |
---|
425 | XjSize *size; |
---|
426 | { |
---|
427 | size->width = me->core.width; |
---|
428 | size->height = me->core.height; |
---|
429 | } |
---|
430 | |
---|
431 | static void move(me, x, y) |
---|
432 | ScrollBarJet me; |
---|
433 | int x, y; |
---|
434 | { |
---|
435 | me->core.x = x; |
---|
436 | me->core.y = y; |
---|
437 | |
---|
438 | if (me->core.child != NULL) |
---|
439 | XjMove(me->core.child, |
---|
440 | me->core.x + me->scrollBar.borderWidth + |
---|
441 | me->scrollBar.borderThickness + me->scrollBar.padding, |
---|
442 | me->core.y + me->scrollBar.borderWidth + |
---|
443 | me->scrollBar.borderThickness + me->scrollBar.padding); |
---|
444 | } |
---|
445 | |
---|
446 | static void resize(me, size) |
---|
447 | ScrollBarJet me; |
---|
448 | XjSize *size; |
---|
449 | { |
---|
450 | me->core.width = size->width; |
---|
451 | me->core.height = size->height; |
---|
452 | |
---|
453 | if (me->scrollBar.realized) |
---|
454 | { |
---|
455 | initSizes(me); |
---|
456 | calcPos(me); |
---|
457 | } |
---|
458 | } |
---|
459 | |
---|
460 | /* |
---|
461 | * clearAround clears the area around the scrollbar's thumb if the thumb |
---|
462 | * is "warped" somewhere with SetScrollBar. |
---|
463 | */ |
---|
464 | static void clearAround(me) |
---|
465 | ScrollBarJet me; |
---|
466 | { |
---|
467 | int x1, x2, y1, y2, w1, w2, h1, h2; |
---|
468 | |
---|
469 | if (me->scrollBar.orientation == Vertical) |
---|
470 | { |
---|
471 | x1 = x2 = me->scrollBar.thumbX; |
---|
472 | y1 = me->core.y; |
---|
473 | y2 = me->scrollBar.thumbY + me->scrollBar.thumbHeight; |
---|
474 | w1 = w2 = me->scrollBar.thumbWidth; |
---|
475 | h1 = me->scrollBar.thumbY - me->core.y; |
---|
476 | h2 = me->core.height - me->scrollBar.thumbY + me->scrollBar.thumbHeight; |
---|
477 | } |
---|
478 | else |
---|
479 | { |
---|
480 | x1 = me->core.x; |
---|
481 | x2 = me->scrollBar.thumbX + me->scrollBar.thumbWidth; |
---|
482 | y1 = y2 = me->scrollBar.thumbY; |
---|
483 | w1 = me->scrollBar.thumbX - me->core.x; |
---|
484 | w2 = me->core.width - me->scrollBar.thumbX + me->scrollBar.thumbWidth; |
---|
485 | h1 = h2 = me->scrollBar.thumbHeight; |
---|
486 | } |
---|
487 | |
---|
488 | XClearArea(me->core.display, me->core.window, x1, y1, w1, h1, False); |
---|
489 | |
---|
490 | XClearArea(me->core.display, me->core.window, x2, y2, w2, h2, False); |
---|
491 | } |
---|
492 | |
---|
493 | /* |
---|
494 | * drawThumb draws the thumb at its current position (whenever there is |
---|
495 | * an expose or if the thumb moves. |
---|
496 | */ |
---|
497 | static void drawThumb(me) |
---|
498 | ScrollBarJet me; |
---|
499 | { |
---|
500 | if (me->scrollBar.borderThickness != 0) |
---|
501 | { |
---|
502 | XDrawRectangle(me->core.display, |
---|
503 | me->core.window, |
---|
504 | me->scrollBar.line_gc, |
---|
505 | me->scrollBar.thumbX + me->scrollBar.borderThickness/2, |
---|
506 | me->scrollBar.thumbY + me->scrollBar.borderThickness/2, |
---|
507 | me->scrollBar.thumbWidth - |
---|
508 | 1 - me->scrollBar.borderThickness / 2, |
---|
509 | me->scrollBar.thumbHeight - |
---|
510 | 1 - me->scrollBar.borderThickness / 2); |
---|
511 | } |
---|
512 | |
---|
513 | XFillRectangle(me->core.display, |
---|
514 | me->core.window, |
---|
515 | me->scrollBar.thumb_gc, |
---|
516 | me->scrollBar.thumbX + me->scrollBar.borderThickness, |
---|
517 | me->scrollBar.thumbY + me->scrollBar.borderThickness, |
---|
518 | me->scrollBar.thumbWidth - |
---|
519 | (2 * me->scrollBar.borderThickness), |
---|
520 | me->scrollBar.thumbHeight - |
---|
521 | (2 * me->scrollBar.borderThickness)); |
---|
522 | } |
---|
523 | |
---|
524 | |
---|
525 | static void clear(me) |
---|
526 | ScrollBarJet me; |
---|
527 | { |
---|
528 | XClearWindow(me->core.display, me->core.window); |
---|
529 | } |
---|
530 | |
---|
531 | |
---|
532 | static void moveThumb(me, current) |
---|
533 | ScrollBarJet me; |
---|
534 | int current; |
---|
535 | { |
---|
536 | int oldPos; |
---|
537 | int range, visible; |
---|
538 | |
---|
539 | range = me->scrollBar.maximum - me->scrollBar.minimum + 1; |
---|
540 | visible = MIN(me->scrollBar.visible, range); |
---|
541 | |
---|
542 | if (current < me->scrollBar.minimum) |
---|
543 | current = me->scrollBar.minimum; |
---|
544 | else |
---|
545 | if (current > (me->scrollBar.maximum - visible + 1)) |
---|
546 | current = me->scrollBar.maximum - visible + 1; |
---|
547 | |
---|
548 | if (current == me->scrollBar.current) |
---|
549 | return; |
---|
550 | |
---|
551 | oldPos = (me->scrollBar.orientation == Vertical) |
---|
552 | ? me->scrollBar.thumbY |
---|
553 | : me->scrollBar.thumbX; |
---|
554 | |
---|
555 | me->scrollBar.current = current; |
---|
556 | calcPos(me); |
---|
557 | drawThumb(me); |
---|
558 | |
---|
559 | if (me->scrollBar.orientation == Vertical) |
---|
560 | { |
---|
561 | if (oldPos < me->scrollBar.thumbY) |
---|
562 | XClearArea(me->core.display, |
---|
563 | me->core.window, |
---|
564 | me->scrollBar.thumbX, |
---|
565 | oldPos, |
---|
566 | me->scrollBar.thumbWidth, |
---|
567 | me->scrollBar.thumbY - oldPos, |
---|
568 | False); |
---|
569 | else |
---|
570 | XClearArea(me->core.display, |
---|
571 | me->core.window, |
---|
572 | me->scrollBar.thumbX, |
---|
573 | me->scrollBar.thumbY + me->scrollBar.thumbHeight, |
---|
574 | me->scrollBar.thumbWidth, |
---|
575 | oldPos - me->scrollBar.thumbY, |
---|
576 | False); |
---|
577 | } |
---|
578 | else |
---|
579 | { |
---|
580 | if (oldPos < me->scrollBar.thumbX) |
---|
581 | XClearArea(me->core.display, |
---|
582 | me->core.window, |
---|
583 | oldPos, |
---|
584 | me->scrollBar.thumbY, |
---|
585 | me->scrollBar.thumbX - oldPos, |
---|
586 | me->scrollBar.thumbHeight, |
---|
587 | False); |
---|
588 | else |
---|
589 | XClearArea(me->core.display, |
---|
590 | me->core.window, |
---|
591 | me->scrollBar.thumbX + me->scrollBar.thumbWidth, |
---|
592 | me->scrollBar.thumbY, |
---|
593 | oldPos - me->scrollBar.thumbX, |
---|
594 | me->scrollBar.thumbHeight, |
---|
595 | False); |
---|
596 | } |
---|
597 | |
---|
598 | XjCallCallbacks((caddr_t) me, me->scrollBar.changeProc, NULL); |
---|
599 | } |
---|
600 | /*==========*/ |
---|
601 | static Boolean event_handler(me, event) |
---|
602 | ScrollBarJet me; |
---|
603 | XEvent *event; |
---|
604 | { |
---|
605 | XEvent tmp; |
---|
606 | int current; |
---|
607 | |
---|
608 | switch(event->type) |
---|
609 | { |
---|
610 | case GraphicsExpose: |
---|
611 | case Expose: |
---|
612 | if (event->xexpose.count != 0) |
---|
613 | break; |
---|
614 | |
---|
615 | drawThumb(me); |
---|
616 | break; |
---|
617 | |
---|
618 | case ConfigureNotify: |
---|
619 | if (event->xconfigure.width != me->core.width || |
---|
620 | event->xconfigure.height != me->core.height) |
---|
621 | { |
---|
622 | XjSize size; |
---|
623 | |
---|
624 | size.width = event->xconfigure.width; |
---|
625 | size.height = event->xconfigure.height; |
---|
626 | XjResize((Jet) me, &size); |
---|
627 | clear(me); |
---|
628 | drawThumb(me); |
---|
629 | } |
---|
630 | break; |
---|
631 | |
---|
632 | case ButtonPress: |
---|
633 | switch(event->xbutton.button) |
---|
634 | { |
---|
635 | case Button1: |
---|
636 | XDefineCursor(me->core.display, me->core.window, |
---|
637 | (me->scrollBar.orientation == Vertical) |
---|
638 | ? me->scrollBar.upCursor |
---|
639 | : me->scrollBar.leftCursor); |
---|
640 | break; |
---|
641 | |
---|
642 | case Button2: |
---|
643 | current = calcCurrent(me, Button2, |
---|
644 | event->xbutton.x, event->xbutton.y); |
---|
645 | XDefineCursor(me->core.display, me->core.window, |
---|
646 | (me->scrollBar.orientation == Vertical) |
---|
647 | ? me->scrollBar.leftCursor |
---|
648 | : me->scrollBar.upCursor); |
---|
649 | moveThumb(me, current); |
---|
650 | break; |
---|
651 | |
---|
652 | case Button3: |
---|
653 | XDefineCursor(me->core.display, me->core.window, |
---|
654 | (me->scrollBar.orientation == Vertical) |
---|
655 | ? me->scrollBar.downCursor |
---|
656 | : me->scrollBar.rightCursor); |
---|
657 | break; |
---|
658 | } |
---|
659 | break; |
---|
660 | |
---|
661 | case MotionNotify: |
---|
662 | if (XPending(me->core.display)) |
---|
663 | { |
---|
664 | XPeekEvent(me->core.display, &tmp); |
---|
665 | if (tmp.type == MotionNotify && tmp.xany.window == me->core.window) |
---|
666 | break; /* ignore this event */ |
---|
667 | } |
---|
668 | |
---|
669 | current = calcCurrent(me, Button2, event->xmotion.x, event->xmotion.y); |
---|
670 | |
---|
671 | moveThumb(me, current); |
---|
672 | break; |
---|
673 | |
---|
674 | case ButtonRelease: |
---|
675 | current = calcCurrent(me, event->xbutton.button, |
---|
676 | event->xbutton.x, event->xbutton.y); |
---|
677 | moveThumb(me, current); |
---|
678 | XDefineCursor(me->core.display, me->core.window, |
---|
679 | (me->scrollBar.orientation == Vertical) |
---|
680 | ? me->scrollBar.vertCursor |
---|
681 | : me->scrollBar.horizCursor); |
---|
682 | break; |
---|
683 | |
---|
684 | default: |
---|
685 | return False; |
---|
686 | } |
---|
687 | return True; |
---|
688 | } |
---|
689 | |
---|
690 | void SetScrollBar(me, min, max, visible, value) |
---|
691 | ScrollBarJet me; |
---|
692 | int min, max, visible, value; |
---|
693 | { |
---|
694 | if (min != -1) |
---|
695 | me->scrollBar.minimum = min; |
---|
696 | if (max != -1) |
---|
697 | me->scrollBar.maximum = max; |
---|
698 | if (visible != -1) |
---|
699 | me->scrollBar.visible = visible; |
---|
700 | if (value != -1) |
---|
701 | me->scrollBar.current = value; |
---|
702 | |
---|
703 | if (me->scrollBar.realized) |
---|
704 | { |
---|
705 | initSizes(me); |
---|
706 | calcPos(me); |
---|
707 | clearAround(me); |
---|
708 | drawThumb(me); |
---|
709 | } |
---|
710 | } |
---|
711 | |
---|
712 | int GetScrollBarValue(me) |
---|
713 | ScrollBarJet me; |
---|
714 | { |
---|
715 | return me->scrollBar.current; |
---|
716 | } |
---|