1 /********************************************************************************
2 * *
3 * S l i d e r W i d g e t *
4 * *
5 *********************************************************************************
6 * Copyright (C) 1997,2005 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Lesser General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2.1 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with this library; if not, write to the Free Software *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
21 *********************************************************************************
22 * $Id: FXSlider.cpp,v 1.59 2005/01/16 16:06:07 fox Exp $ *
23 ********************************************************************************/
24 #include "xincs.h"
25 #include "fxver.h"
26 #include "fxdefs.h"
27 #include "FXHash.h"
28 #include "FXThread.h"
29 #include "FXStream.h"
30 #include "FXString.h"
31 #include "FXSize.h"
32 #include "FXPoint.h"
33 #include "FXRectangle.h"
34 #include "FXRegistry.h"
35 #include "FXApp.h"
36 #include "FXDCWindow.h"
37 #include "FXSlider.h"
38
39
40
41
42 /*
43 Notes:
44 - Maybe add bindings for arrow keys for value changes.
45 */
46
47 #define TICKSIZE 4 // Length of ticks
48 #define OVERHANG 4 // Default amount of overhang
49 #define MINOVERHANG 3 // Minimal amount of overhang
50 #define HEADINSIDEBAR 20 // Default for inside bar head size
51 #define HEADOVERHANGING 9 // Default for overhanging head size
52
53 #define SLIDER_MASK (SLIDER_VERTICAL|SLIDER_ARROW_UP|SLIDER_ARROW_DOWN|SLIDER_INSIDE_BAR|SLIDER_TICKS_TOP|SLIDER_TICKS_BOTTOM)
54
55 using namespace FX;
56
57 /*******************************************************************************/
58
59 namespace FX {
60
61 // Map
62 FXDEFMAP(FXSlider) FXSliderMap[]={
63 FXMAPFUNC(SEL_PAINT,0,FXSlider::onPaint),
64 FXMAPFUNC(SEL_MOTION,0,FXSlider::onMotion),
65 FXMAPFUNC(SEL_MOUSEWHEEL,0,FXSlider::onMouseWheel),
66 FXMAPFUNC(SEL_LEFTBUTTONPRESS,0,FXSlider::onLeftBtnPress),
67 FXMAPFUNC(SEL_LEFTBUTTONRELEASE,0,FXSlider::onLeftBtnRelease),
68 FXMAPFUNC(SEL_MIDDLEBUTTONPRESS,0,FXSlider::onMiddleBtnPress),
69 FXMAPFUNC(SEL_MIDDLEBUTTONRELEASE,0,FXSlider::onMiddleBtnRelease),
70 FXMAPFUNC(SEL_UNGRABBED,0,FXSlider::onUngrabbed),
71 FXMAPFUNC(SEL_QUERY_TIP,0,FXSlider::onQueryTip),
72 FXMAPFUNC(SEL_QUERY_HELP,0,FXSlider::onQueryHelp),
73 FXMAPFUNC(SEL_TIMEOUT,FXSlider::ID_AUTOSLIDE,FXSlider::onAutoSlide),
74 FXMAPFUNC(SEL_COMMAND,FXSlider::ID_SETVALUE,FXSlider::onCmdSetValue),
75 FXMAPFUNC(SEL_COMMAND,FXSlider::ID_SETINTVALUE,FXSlider::onCmdSetIntValue),
76 FXMAPFUNC(SEL_COMMAND,FXSlider::ID_SETREALVALUE,FXSlider::onCmdSetRealValue),
77 FXMAPFUNC(SEL_COMMAND,FXSlider::ID_GETINTVALUE,FXSlider::onCmdGetIntValue),
78 FXMAPFUNC(SEL_COMMAND,FXSlider::ID_GETREALVALUE,FXSlider::onCmdGetRealValue),
79 FXMAPFUNC(SEL_COMMAND,FXSlider::ID_SETINTRANGE,FXSlider::onCmdSetIntRange),
80 FXMAPFUNC(SEL_COMMAND,FXSlider::ID_GETINTRANGE,FXSlider::onCmdGetIntRange),
81 FXMAPFUNC(SEL_COMMAND,FXSlider::ID_SETREALRANGE,FXSlider::onCmdSetRealRange),
82 FXMAPFUNC(SEL_COMMAND,FXSlider::ID_GETREALRANGE,FXSlider::onCmdGetRealRange),
83 FXMAPFUNC(SEL_COMMAND,FXSlider::ID_SETHELPSTRING,FXSlider::onCmdSetHelp),
84 FXMAPFUNC(SEL_COMMAND,FXSlider::ID_GETHELPSTRING,FXSlider::onCmdGetHelp),
85 FXMAPFUNC(SEL_COMMAND,FXSlider::ID_SETTIPSTRING,FXSlider::onCmdSetTip),
86 FXMAPFUNC(SEL_COMMAND,FXSlider::ID_GETTIPSTRING,FXSlider::onCmdGetTip),
87 };
88
89
90 // Object implementation
FXIMPLEMENT(FXSlider,FXFrame,FXSliderMap,ARRAYNUMBER (FXSliderMap))91 FXIMPLEMENT(FXSlider,FXFrame,FXSliderMap,ARRAYNUMBER(FXSliderMap))
92
93
94 // Make a slider
95 FXSlider::FXSlider(){
96 flags|=FLAG_ENABLED;
97 headpos=0;
98 dragpoint=0;
99 }
100
101
102 // Make a slider
FXSlider(FXComposite * p,FXObject * tgt,FXSelector sel,FXuint opts,FXint x,FXint y,FXint w,FXint h,FXint pl,FXint pr,FXint pt,FXint pb)103 FXSlider::FXSlider(FXComposite* p,FXObject* tgt,FXSelector sel,FXuint opts,FXint x,FXint y,FXint w,FXint h,FXint pl,FXint pr,FXint pt,FXint pb):
104 FXFrame(p,opts,x,y,w,h,pl,pr,pt,pb){
105 flags|=FLAG_ENABLED;
106 slotColor=getApp()->getBackColor();
107 baseColor=getApp()->getBaseColor();
108 hiliteColor=getApp()->getHiliteColor();
109 shadowColor=getApp()->getShadowColor();
110 borderColor=getApp()->getBorderColor();
111 target=tgt;
112 message=sel;
113 range[0]=0;
114 range[1]=100;
115 pos=50;
116 incr=1;
117 delta=0;
118 headpos=0;
119 dragpoint=0;
120 headsize=(options&SLIDER_INSIDE_BAR)?HEADINSIDEBAR:HEADOVERHANGING;
121 slotsize=5;
122 }
123
124
125 // Enable the window
enable()126 void FXSlider::enable(){
127 if(!(flags&FLAG_ENABLED)){
128 FXFrame::enable();
129 update();
130 }
131 }
132
133
134 // Disable the window
disable()135 void FXSlider::disable(){
136 if(flags&FLAG_ENABLED){
137 FXFrame::disable();
138 update();
139 }
140 }
141
142
143 // Get default size
getDefaultWidth()144 FXint FXSlider::getDefaultWidth(){
145 FXint w;
146 if(options&SLIDER_VERTICAL){
147 if(options&SLIDER_INSIDE_BAR) w=4+headsize/2;
148 else if(options&(SLIDER_ARROW_LEFT|SLIDER_ARROW_RIGHT)) w=slotsize+MINOVERHANG*2+headsize/2;
149 else w=slotsize+MINOVERHANG*2;
150 if(options&SLIDER_TICKS_LEFT) w+=TICKSIZE;
151 if(options&SLIDER_TICKS_RIGHT) w+=TICKSIZE;
152 }
153 else{
154 w=headsize+4;
155 }
156 return w+padleft+padright+(border<<1);
157 }
158
159
getDefaultHeight()160 FXint FXSlider::getDefaultHeight(){
161 FXint h;
162 if(options&SLIDER_VERTICAL){
163 h=headsize+4;
164 }
165 else{
166 if(options&SLIDER_INSIDE_BAR) h=4+headsize/2;
167 else if(options&(SLIDER_ARROW_UP|SLIDER_ARROW_DOWN)) h=slotsize+2*MINOVERHANG+headsize/2;
168 else h=slotsize+MINOVERHANG*2;
169 if(options&SLIDER_TICKS_TOP) h+=TICKSIZE;
170 if(options&SLIDER_TICKS_BOTTOM) h+=TICKSIZE;
171 }
172 return h+padtop+padbottom+(border<<1);
173 }
174
175
176 // Layout changed; even though the position is still
177 // the same, the head may have to be moved.
layout()178 void FXSlider::layout(){
179 setValue(pos);
180 flags&=~FLAG_DIRTY;
181 }
182
183
184 // Set help using a message
onCmdSetHelp(FXObject *,FXSelector,void * ptr)185 long FXSlider::onCmdSetHelp(FXObject*,FXSelector,void* ptr){
186 setHelpText(*((FXString*)ptr));
187 return 1;
188 }
189
190
191 // Get help using a message
onCmdGetHelp(FXObject *,FXSelector,void * ptr)192 long FXSlider::onCmdGetHelp(FXObject*,FXSelector,void* ptr){
193 *((FXString*)ptr)=getHelpText();
194 return 1;
195 }
196
197
198 // Set tip using a message
onCmdSetTip(FXObject *,FXSelector,void * ptr)199 long FXSlider::onCmdSetTip(FXObject*,FXSelector,void* ptr){
200 setTipText(*((FXString*)ptr));
201 return 1;
202 }
203
204
205 // Get tip using a message
onCmdGetTip(FXObject *,FXSelector,void * ptr)206 long FXSlider::onCmdGetTip(FXObject*,FXSelector,void* ptr){
207 *((FXString*)ptr)=getTipText();
208 return 1;
209 }
210
211
212 // We were asked about tip text
onQueryTip(FXObject * sender,FXSelector sel,void * ptr)213 long FXSlider::onQueryTip(FXObject* sender,FXSelector sel,void* ptr){
214 if(FXWindow::onQueryTip(sender,sel,ptr)) return 1;
215 if((flags&FLAG_TIP) && !tip.empty()){
216 sender->handle(this,FXSEL(SEL_COMMAND,ID_SETSTRINGVALUE),(void*)&tip);
217 return 1;
218 }
219 return 0;
220 }
221
222
223 // We were asked about status text
onQueryHelp(FXObject * sender,FXSelector sel,void * ptr)224 long FXSlider::onQueryHelp(FXObject* sender,FXSelector sel,void* ptr){
225 if(FXWindow::onQueryHelp(sender,sel,ptr)) return 1;
226 if((flags&FLAG_HELP) && !help.empty()){
227 sender->handle(this,FXSEL(SEL_COMMAND,ID_SETSTRINGVALUE),(void*)&help);
228 return 1;
229 }
230 return 0;
231 }
232
233
234 // Update value from a message
onCmdSetValue(FXObject *,FXSelector,void * ptr)235 long FXSlider::onCmdSetValue(FXObject*,FXSelector,void* ptr){
236 setValue((FXint)(FXival)ptr);
237 return 1;
238 }
239
240
241 // Update value from a message
onCmdSetIntValue(FXObject *,FXSelector,void * ptr)242 long FXSlider::onCmdSetIntValue(FXObject*,FXSelector,void* ptr){
243 setValue(*((FXint*)ptr));
244 return 1;
245 }
246
247
248 // Update value from a message
onCmdSetRealValue(FXObject *,FXSelector,void * ptr)249 long FXSlider::onCmdSetRealValue(FXObject*,FXSelector,void* ptr){
250 setValue((FXint)*((FXdouble*)ptr));
251 return 1;
252 }
253
254
255 // Obtain value from text field
onCmdGetIntValue(FXObject *,FXSelector,void * ptr)256 long FXSlider::onCmdGetIntValue(FXObject*,FXSelector,void* ptr){
257 *((FXint*)ptr)=getValue();
258 return 1;
259 }
260
261
262 // Obtain value with a message
onCmdGetRealValue(FXObject *,FXSelector,void * ptr)263 long FXSlider::onCmdGetRealValue(FXObject*,FXSelector,void* ptr){
264 *((FXdouble*)ptr)=(FXdouble)getValue();
265 return 1;
266 }
267
268
269 // Update range from a message
onCmdSetIntRange(FXObject *,FXSelector,void * ptr)270 long FXSlider::onCmdSetIntRange(FXObject*,FXSelector,void* ptr){
271 setRange(((FXint*)ptr)[0],((FXint*)ptr)[1]);
272 return 1;
273 }
274
275
276 // Get range with a message
onCmdGetIntRange(FXObject *,FXSelector,void * ptr)277 long FXSlider::onCmdGetIntRange(FXObject*,FXSelector,void* ptr){
278 ((FXint*)ptr)[0]=range[0];
279 ((FXint*)ptr)[1]=range[1];
280 return 1;
281 }
282
283
284 // Update range from a message
onCmdSetRealRange(FXObject *,FXSelector,void * ptr)285 long FXSlider::onCmdSetRealRange(FXObject*,FXSelector,void* ptr){
286 setRange((FXint)((FXdouble*)ptr)[0],(FXint)((FXdouble*)ptr)[1]);
287 return 1;
288 }
289
290
291 // Get range with a message
onCmdGetRealRange(FXObject *,FXSelector,void * ptr)292 long FXSlider::onCmdGetRealRange(FXObject*,FXSelector,void* ptr){
293 ((FXdouble*)ptr)[0]=(FXdouble)range[0];
294 ((FXdouble*)ptr)[1]=(FXdouble)range[1];
295 return 1;
296 }
297
298
299 // Pressed LEFT button
onLeftBtnPress(FXObject *,FXSelector,void * ptr)300 long FXSlider::onLeftBtnPress(FXObject*,FXSelector,void* ptr){
301 register FXEvent *event=(FXEvent*)ptr;
302 register FXint p=pos;
303 flags&=~FLAG_TIP;
304 if(isEnabled()){
305 grab();
306 getApp()->removeTimeout(this,ID_AUTOSLIDE);
307 if(target && target->tryHandle(this,FXSEL(SEL_LEFTBUTTONPRESS,message),ptr)) return 1;
308 flags&=~FLAG_UPDATE;
309 if(options&SLIDER_VERTICAL){
310 if(event->win_y<headpos){
311 getApp()->addTimeout(this,ID_AUTOSLIDE,getApp()->getScrollDelay(),(void*)(FXival)incr);
312 p=pos+incr;
313 }
314 else if(event->win_y>(headpos+headsize)){
315 getApp()->addTimeout(this,ID_AUTOSLIDE,getApp()->getScrollDelay(),(void*)(FXival)-incr);
316 p=pos-incr;
317 }
318 else{
319 dragpoint=event->win_y-headpos;
320 flags|=FLAG_PRESSED;
321 }
322 }
323 else{
324 if(event->win_x<headpos){
325 getApp()->addTimeout(this,ID_AUTOSLIDE,getApp()->getScrollDelay(),(void*)(FXival)-incr);
326 p=pos-incr;
327 }
328 else if(event->win_x>(headpos+headsize)){
329 getApp()->addTimeout(this,ID_AUTOSLIDE,getApp()->getScrollDelay(),(void*)(FXival)incr);
330 p=pos+incr;
331 }
332 else{
333 dragpoint=event->win_x-headpos;
334 flags|=FLAG_PRESSED;
335 }
336 }
337 if(p<range[0]) p=range[0];
338 if(p>range[1]) p=range[1];
339 if(p!=pos){
340 setValue(p);
341 flags|=FLAG_CHANGED;
342 if(target) target->tryHandle(this,FXSEL(SEL_CHANGED,message),(void*)(FXival)pos);
343 }
344 return 1;
345 }
346 return 0;
347 }
348
349
350 // Released Left button
onLeftBtnRelease(FXObject *,FXSelector,void * ptr)351 long FXSlider::onLeftBtnRelease(FXObject*,FXSelector,void* ptr){
352 register FXuint flgs=flags;
353 if(isEnabled()){
354 ungrab();
355 getApp()->removeTimeout(this,ID_AUTOSLIDE);
356 setValue(pos); // Hop to exact position
357 flags&=~FLAG_PRESSED;
358 flags&=~FLAG_CHANGED;
359 flags|=FLAG_UPDATE;
360 if(target && target->tryHandle(this,FXSEL(SEL_LEFTBUTTONRELEASE,message),ptr)) return 1;
361 if(flgs&FLAG_CHANGED){
362 if(target) target->tryHandle(this,FXSEL(SEL_COMMAND,message),(void*)(FXival)pos);
363 }
364 return 1;
365 }
366 return 0;
367 }
368
369
370 // Moving
onMotion(FXObject *,FXSelector,void * ptr)371 long FXSlider::onMotion(FXObject*,FXSelector,void* ptr){
372 register FXEvent *event=(FXEvent*)ptr;
373 register FXint xx,yy,ww,hh,lo,hi,p,h,travel;
374 if(!isEnabled()) return 0;
375 if(flags&FLAG_PRESSED){
376 yy=border+padtop+2;
377 xx=border+padleft+2;
378 hh=height-(border<<1)-padtop-padbottom-4;
379 ww=width-(border<<1)-padleft-padright-4;
380 if(options&SLIDER_VERTICAL){
381 h=event->win_y-dragpoint;
382 travel=hh-headsize;
383 if(h<yy) h=yy;
384 if(h>yy+travel) h=yy+travel;
385 if(h!=headpos){
386 FXMINMAX(lo,hi,headpos,h);
387 headpos=h;
388 update(border,lo-1,width-(border<<1),hi+headsize+2-lo);
389 }
390 if(travel>0)
391 p=range[0]+((range[1]-range[0])*(yy+travel-h)+travel/2)/travel; // Use rounding!!
392 else
393 p=range[0];
394 }
395 else{
396 h=event->win_x-dragpoint;
397 travel=ww-headsize;
398 if(h<xx) h=xx;
399 if(h>xx+travel) h=xx+travel;
400 if(h!=headpos){
401 FXMINMAX(lo,hi,headpos,h);
402 headpos=h;
403 update(lo-1,border,hi+headsize+2-lo,height-(border<<1));
404 }
405 if(travel>0)
406 p=range[0]+((range[1]-range[0])*(h-xx)+travel/2)/travel; // Use rounding!!
407 else
408 p=range[0];
409 }
410 if(p<range[0]) p=range[0];
411 if(p>range[1]) p=range[1];
412 if(pos!=p){
413 pos=p;
414 flags|=FLAG_CHANGED;
415 if(target) target->tryHandle(this,FXSEL(SEL_CHANGED,message),(void*)(FXival)pos);
416 }
417 return 1;
418 }
419 return 0;
420 }
421
422
423 // Pressed middle or right
onMiddleBtnPress(FXObject *,FXSelector,void * ptr)424 long FXSlider::onMiddleBtnPress(FXObject*,FXSelector,void* ptr){
425 register FXEvent *event=(FXEvent*)ptr;
426 register FXint xx,yy,ww,hh,lo,hi,p,h,travel;
427 flags&=~FLAG_TIP;
428 if(isEnabled()){
429 grab();
430 if(target && target->tryHandle(this,FXSEL(SEL_MIDDLEBUTTONPRESS,message),ptr)) return 1;
431 dragpoint=headsize/2;
432 yy=border+padtop+2;
433 xx=border+padleft+2;
434 hh=height-(border<<1)-padtop-padbottom-4;
435 ww=width-(border<<1)-padleft-padright-4;
436 flags|=FLAG_PRESSED;
437 flags&=~FLAG_UPDATE;
438 if(options&SLIDER_VERTICAL){
439 h=event->win_y-dragpoint;
440 travel=hh-headsize;
441 if(h<yy) h=yy;
442 if(h>yy+travel) h=yy+travel;
443 if(h!=headpos){
444 FXMINMAX(lo,hi,headpos,h);
445 headpos=h;
446 update(border,lo-1,width-(border<<1),hi+headsize+2-lo);
447 }
448 if(travel>0)
449 p=range[0]+((range[1]-range[0])*(yy+travel-h)+travel/2)/travel; // Use rounding!!
450 else
451 p=range[0];
452 }
453 else{
454 h=event->win_x-dragpoint;
455 travel=ww-headsize;
456 if(h<xx) h=xx;
457 if(h>xx+travel) h=xx+travel;
458 if(h!=headpos){
459 FXMINMAX(lo,hi,headpos,h);
460 headpos=h;
461 update(lo-1,border,hi+headsize+2-lo,height-(border<<1));
462 }
463 if(travel>0)
464 p=range[0]+((range[1]-range[0])*(h-xx)+travel/2)/travel; // Use rounding!!
465 else
466 p=range[0];
467 }
468 if(p<range[0]) p=range[0];
469 if(p>range[1]) p=range[1];
470 if(p!=pos){
471 pos=p;
472 flags|=FLAG_CHANGED;
473 if(target) target->tryHandle(this,FXSEL(SEL_CHANGED,message),(void*)(FXival)pos);
474 }
475 return 1;
476 }
477 return 0;
478 }
479
480
481 // Released middle button
onMiddleBtnRelease(FXObject *,FXSelector,void * ptr)482 long FXSlider::onMiddleBtnRelease(FXObject*,FXSelector,void* ptr){
483 register FXuint flgs=flags;
484 if(isEnabled()){
485 ungrab();
486 getApp()->removeTimeout(this,ID_AUTOSLIDE);
487 flags&=~FLAG_PRESSED;
488 flags&=~FLAG_CHANGED;
489 flags|=FLAG_UPDATE;
490 setValue(pos); // Hop to exact position
491 if(target && target->tryHandle(this,FXSEL(SEL_MIDDLEBUTTONRELEASE,message),ptr)) return 1;
492 if(flgs&FLAG_CHANGED){
493 if(target) target->tryHandle(this,FXSEL(SEL_COMMAND,message),(void*)(FXival)pos);
494 }
495 return 1;
496 }
497 return 0;
498 }
499
500
501 // Mouse wheel
onMouseWheel(FXObject *,FXSelector,void * ptr)502 long FXSlider::onMouseWheel(FXObject*,FXSelector,void* ptr){
503 register FXEvent *event=(FXEvent*)ptr;
504 register FXint p=pos+(event->code*incr)/120;
505 if(p<range[0]) p=range[0];
506 if(p>range[1]) p=range[1];
507 if(pos!=p){
508 setValue(p);
509 if(target) target->tryHandle(this,FXSEL(SEL_COMMAND,message),(void*)(FXival)pos);
510 }
511 return 1;
512 }
513
514
515 // The widget lost the grab for some reason
onUngrabbed(FXObject * sender,FXSelector sel,void * ptr)516 long FXSlider::onUngrabbed(FXObject* sender,FXSelector sel,void* ptr){
517 FXFrame::onUngrabbed(sender,sel,ptr);
518 getApp()->removeTimeout(this,ID_AUTOSLIDE);
519 flags&=~FLAG_PRESSED;
520 flags&=~FLAG_CHANGED;
521 flags|=FLAG_UPDATE;
522 return 1;
523 }
524
525
526 // Automatically move slider while holding down mouse
onAutoSlide(FXObject *,FXSelector,void * ptr)527 long FXSlider::onAutoSlide(FXObject*,FXSelector,void* ptr){
528 register FXint inc=(FXint)(FXival)ptr;
529 register FXint p=pos+inc;
530 if(p<=range[0]){
531 p=range[0];
532 }
533 else if(p>=range[1]){
534 p=range[1];
535 }
536 else{
537 getApp()->addTimeout(this,ID_AUTOSLIDE,getApp()->getScrollSpeed(),(void*)(FXival)inc);
538 }
539 if(p!=pos){
540 setValue(p);
541 flags|=FLAG_CHANGED;
542 if(target) target->tryHandle(this,FXSEL(SEL_CHANGED,message),(void*)(FXival)pos);
543 return 1;
544 }
545 return 0;
546 }
547
548
549 // Draw horizontal ticks
drawHorzTicks(FXDCWindow & dc,FXint,FXint y,FXint,FXint)550 void FXSlider::drawHorzTicks(FXDCWindow& dc,FXint,FXint y,FXint,FXint){
551 register FXint interval=range[1]-range[0];
552 register FXint travel,offset,v,d,p;
553 if(0<interval){
554 d=delta;
555 if(d<=0) d=incr;
556 dc.setForeground(FXRGB(0,0,0));
557 travel=width-(border<<1)-padleft-padright-headsize-4;
558 offset=border+padleft+2+headsize/2;
559 for(v=range[0]; v<=range[1]; v+=d){
560 p=offset+(travel*(v-range[0]))/interval;
561 dc.fillRectangle(p,y,1,TICKSIZE);
562 }
563 }
564 }
565
566
567 // Draw vertical ticks
drawVertTicks(FXDCWindow & dc,FXint x,FXint,FXint,FXint)568 void FXSlider::drawVertTicks(FXDCWindow& dc,FXint x,FXint,FXint,FXint){
569 register FXint interval=range[1]-range[0];
570 register FXint travel,offset,v,d,p;
571 if(0<interval){
572 d=delta;
573 if(d<=0) d=incr;
574 dc.setForeground(FXRGB(0,0,0));
575 travel=height-(border<<1)-padtop-padbottom-headsize-4;
576 offset=height-border-padbottom-2-headsize/2;
577 for(v=range[0]; v<=range[1]; v+=d){
578 p=offset-(travel*(v-range[0]))/interval;
579 dc.fillRectangle(x,p,TICKSIZE,1);
580 }
581 }
582 }
583
584
585 // Draw slider head
drawSliderHead(FXDCWindow & dc,FXint x,FXint y,FXint w,FXint h)586 void FXSlider::drawSliderHead(FXDCWindow& dc,FXint x,FXint y,FXint w,FXint h){
587 FXint m;
588 dc.setForeground(baseColor);
589 dc.fillRectangle(x,y,w,h);
590 if(options&SLIDER_VERTICAL){
591 m=(h>>1);
592 if(options&SLIDER_ARROW_LEFT){
593 dc.setForeground(hiliteColor);
594 dc.drawLine(x+m,y,x+w-1,y);
595 dc.drawLine(x,y+m,x+m,y);
596 dc.setForeground(shadowColor);
597 dc.drawLine(x+1,y+h-m-1,x+m+1,y+h-1);
598 dc.drawLine(x+m,y+h-2,x+w-1,y+h-2);
599 dc.drawLine(x+w-2,y+1,x+w-2,y+h-1);
600 dc.setForeground(borderColor);
601 dc.drawLine(x,y+h-m-1,x+m,y+h-1);
602 dc.drawLine(x+w-1,y+h-1,x+w-1,y);
603 dc.fillRectangle(x+m,y+h-1,w-m,1);
604 }
605 else if(options&SLIDER_ARROW_RIGHT){
606 dc.setForeground(hiliteColor);
607 dc.drawLine(x,y,x+w-m-1,y);
608 dc.drawLine(x,y+1,x,y+h-1);
609 dc.drawLine(x+w-1,y+m,x+w-m-1,y);
610 #ifndef WIN32
611 dc.setForeground(shadowColor);
612 dc.drawLine(x+w-2,y+h-m-1,x+w-m-2,y+h-1);
613 dc.drawLine(x+1,y+h-2,x+w-m-1,y+h-2);
614 dc.setForeground(borderColor);
615 dc.drawLine(x+w-1,y+h-m-1,x+w-m-1,y+h-1);
616 dc.drawLine(x,y+h-1,x+w-m-1,y+h-1);
617 #else
618 dc.setForeground(shadowColor);
619 dc.drawLine(x+w-1,y+h-m-2,x+w-m-2,y+h-1);
620 dc.drawLine(x+1,y+h-2,x+w-m-1,y+h-2);
621 dc.setForeground(borderColor);
622 dc.drawLine(x+w,y+h-m-2,x+w-m-1,y+h-1);
623 dc.drawLine(x,y+h-1,x+w-m-1,y+h-1);
624 #endif
625 }
626 else if(options&SLIDER_INSIDE_BAR){
627 drawDoubleRaisedRectangle(dc,x,y,w,h);
628 dc.setForeground(shadowColor);
629 dc.drawLine(x+1,y+m-1,x+w-2,y+m-1);
630 dc.setForeground(hiliteColor);
631 dc.drawLine(x+1,y+m,x+w-2,y+m);
632 }
633 else{
634 drawDoubleRaisedRectangle(dc,x,y,w,h);
635 }
636 }
637 else{
638 m=(w>>1);
639 if(options&SLIDER_ARROW_UP){
640 dc.setForeground(hiliteColor);
641 dc.drawLine(x,y+m,x+m,y);
642 dc.drawLine(x,y+m,x,y+h-1);
643 dc.setForeground(shadowColor);
644 dc.drawLine(x+w-1,y+m+1,x+w-m-1,y+1);
645 dc.drawLine(x+w-2,y+m+1,x+w-2,y+h-1);
646 dc.drawLine(x+1,y+h-2,x+w-2,y+h-2);
647 dc.setForeground(borderColor);
648 dc.drawLine(x+w-1,y+m,x+w-m-1,y);
649 dc.drawLine(x+w-1,y+m,x+w-1,y+h-1);
650 dc.fillRectangle(x,y+h-1,w,1);
651 }
652 else if(options&SLIDER_ARROW_DOWN){
653 dc.setForeground(hiliteColor);
654 dc.drawLine(x,y,x+w-1,y);
655 dc.drawLine(x,y+1,x,y+h-m-1);
656 dc.drawLine(x,y+h-m-1,x+m,y+h-1);
657 dc.setForeground(shadowColor);
658 dc.drawLine(x+w-2,y+1,x+w-2,y+h-m-1);
659 dc.drawLine(x+w-1,y+h-m-2,x+w-m-1,y+h-2);
660 dc.setForeground(borderColor);
661 dc.drawLine(x+w-1,y+h-m-1,x+w-m-1,y+h-1);
662 dc.fillRectangle(x+w-1,y,1,h-m);
663 }
664 else if(options&SLIDER_INSIDE_BAR){
665 drawDoubleRaisedRectangle(dc,x,y,w,h);
666 dc.setForeground(shadowColor);
667 dc.drawLine(x+m-1,y+1,x+m-1,y+h-2);
668 dc.setForeground(hiliteColor);
669 dc.drawLine(x+m,y+1,x+m,y+h-1);
670 }
671 else{
672 drawDoubleRaisedRectangle(dc,x,y,w,h);
673 }
674 }
675 }
676
677
678 // Handle repaint
onPaint(FXObject *,FXSelector,void * ptr)679 long FXSlider::onPaint(FXObject*,FXSelector,void* ptr){
680 FXEvent *event=(FXEvent*)ptr;
681 FXint tx,ty,hhs=headsize/2;
682 FXint xx,yy,ww,hh;
683 FXDCWindow dc(this,event);
684
685 // Repaint background
686 dc.setForeground(backColor);
687 dc.fillRectangle(0,0,width,height);
688
689 // Repaint border
690 drawFrame(dc,0,0,width,height);
691
692 // Slot placement
693 xx=border+padleft;
694 yy=border+padtop;
695 ww=width-(border<<1)-padleft-padright;
696 hh=height-(border<<1)-padtop-padbottom;
697 FXASSERT(range[0]<=pos && pos<=range[1]);
698
699 // Draw the slot
700 if(options&SLIDER_VERTICAL){
701
702 // Adjust slot placement for tickmarks
703 if(options&SLIDER_TICKS_LEFT){ xx+=TICKSIZE; ww-=TICKSIZE; }
704 if(options&SLIDER_TICKS_RIGHT){ ww-=TICKSIZE; }
705
706 // Draw slider
707 if(options&SLIDER_INSIDE_BAR){
708 drawDoubleSunkenRectangle(dc,xx,yy,ww,hh);
709 dc.setStipple(STIPPLE_GRAY);
710 dc.setForeground(slotColor);
711 dc.setBackground(baseColor);
712 dc.setFillStyle(FILL_OPAQUESTIPPLED);
713 dc.fillRectangle(xx+2,yy+2,ww-4,hh-4);
714 dc.setFillStyle(FILL_SOLID);
715 if(options&SLIDER_TICKS_LEFT) drawVertTicks(dc,border+padleft,yy,ww,hh);
716 if(options&SLIDER_TICKS_RIGHT) drawVertTicks(dc,width-padright-border-TICKSIZE,yy,ww,hh);
717 if(isEnabled()) drawSliderHead(dc,xx+2,headpos,ww-4,headsize);
718 }
719 else{
720 if(options&SLIDER_ARROW_LEFT) tx=xx+hhs+(ww-slotsize-hhs)/2;
721 else if(options&SLIDER_ARROW_RIGHT) tx=xx+(ww-slotsize-hhs)/2;
722 else tx=xx+(ww-slotsize)/2;
723 drawDoubleSunkenRectangle(dc,tx,yy,slotsize,hh);
724 dc.setForeground(slotColor);
725 dc.fillRectangle(tx+2,yy+2,slotsize-4,hh-4);
726 if(options&SLIDER_TICKS_LEFT) drawVertTicks(dc,border+padleft,yy,ww,hh);
727 if(options&SLIDER_TICKS_RIGHT) drawVertTicks(dc,width-padright-border-TICKSIZE,yy,ww,hh);
728 if(isEnabled()) drawSliderHead(dc,xx,headpos,ww,headsize);
729 }
730 }
731 else{
732
733 // Adjust slot placement for tickmarks
734 if(options&SLIDER_TICKS_TOP){ yy+=TICKSIZE; hh-=TICKSIZE; }
735 if(options&SLIDER_TICKS_BOTTOM){ hh-=TICKSIZE; }
736
737 // Draw slider
738 if(options&SLIDER_INSIDE_BAR){
739 drawDoubleSunkenRectangle(dc,xx,yy,ww,hh);
740 dc.setForeground(slotColor);
741 dc.setStipple(STIPPLE_GRAY);
742 dc.setForeground(slotColor);
743 dc.setBackground(baseColor);
744 dc.setFillStyle(FILL_OPAQUESTIPPLED);
745 dc.fillRectangle(xx+2,yy+2,ww-4,hh-4);
746 dc.setFillStyle(FILL_SOLID);
747 if(options&SLIDER_TICKS_TOP) drawHorzTicks(dc,xx,border+padtop,ww,hh);
748 if(options&SLIDER_TICKS_BOTTOM) drawHorzTicks(dc,xx,height-border-padbottom-TICKSIZE,ww,hh);
749 if(isEnabled()) drawSliderHead(dc,headpos,yy+2,headsize,hh-4);
750 }
751 else{
752 if(options&SLIDER_ARROW_UP) ty=yy+hhs+(hh-slotsize-hhs)/2;
753 else if(options&SLIDER_ARROW_DOWN) ty=yy+(hh-slotsize-hhs)/2;
754 else ty=yy+(hh-slotsize)/2;
755 drawDoubleSunkenRectangle(dc,xx,ty,ww,slotsize);
756 dc.setForeground(slotColor);
757 dc.fillRectangle(xx+2,ty+2,ww-4,slotsize-4);
758 if(options&SLIDER_TICKS_TOP) drawHorzTicks(dc,xx,border+padtop,ww,hh);
759 if(options&SLIDER_TICKS_BOTTOM) drawHorzTicks(dc,xx,height-border-padbottom-TICKSIZE,ww,hh);
760 if(isEnabled()) drawSliderHead(dc,headpos,yy,headsize,hh);
761 }
762 }
763 return 1;
764 }
765
766
767 // Set slider range; this also revalidates the position,
768 // and possibly moves the head [even if the position was still OK,
769 // the head might still have to be moved to the exact position].
setRange(FXint lo,FXint hi)770 void FXSlider::setRange(FXint lo,FXint hi){
771 if(lo>hi){ fxerror("%s::setRange: trying to set negative range.\n",getClassName()); }
772 if(range[0]!=lo || range[1]!=hi){
773 range[0]=lo;
774 range[1]=hi;
775 setValue(pos);
776 }
777 }
778
779
780 // Set position; this should always cause the head to reflect
781 // the exact [discrete] value representing pos, even if several
782 // head positions may represent the same position!
783 // Also, the minimal amount is repainted, as one sometimes as very
784 // large/wide sliders.
setValue(FXint p)785 void FXSlider::setValue(FXint p){
786 register FXint interval=range[1]-range[0];
787 register FXint travel,lo,hi,h;
788 if(p<range[0]) p=range[0];
789 if(p>range[1]) p=range[1];
790 if(options&SLIDER_VERTICAL){
791 travel=height-(border<<1)-padtop-padbottom-headsize-4;
792 h=height-border-padbottom-2-headsize;
793 if(0<interval) h-=(travel*(p-range[0]))/interval;
794 if(h!=headpos){
795 FXMINMAX(lo,hi,headpos,h);
796 headpos=h;
797 update(border,lo-1,width-(border<<1),hi+headsize+2-lo);
798 }
799 }
800 else{
801 travel=width-(border<<1)-padleft-padright-headsize-4;
802 h=border+padleft+2;
803 if(0<interval) h+=(travel*(p-range[0]))/interval;
804 if(h!=headpos){
805 FXMINMAX(lo,hi,headpos,h);
806 headpos=h;
807 update(lo-1,border,hi+headsize+2-lo,height-(border<<1));
808 }
809 }
810 pos=p;
811 }
812
813
814 // Get slider options
getSliderStyle() const815 FXuint FXSlider::getSliderStyle() const {
816 return (options&SLIDER_MASK);
817 }
818
819
820 // Set slider options
setSliderStyle(FXuint style)821 void FXSlider::setSliderStyle(FXuint style){
822 register FXuint opts=(options&~SLIDER_MASK) | (style&SLIDER_MASK);
823 if(options!=opts){
824 headsize=(opts&SLIDER_INSIDE_BAR)?HEADINSIDEBAR:HEADOVERHANGING;
825 options=opts;
826 recalc();
827 update();
828 }
829 }
830
831
832 // Set head size
setHeadSize(FXint hs)833 void FXSlider::setHeadSize(FXint hs){
834 if(headsize!=hs){
835 headsize=hs;
836 recalc();
837 update();
838 }
839 }
840
841
842 // Set slot size
setSlotSize(FXint bs)843 void FXSlider::setSlotSize(FXint bs){
844 if(slotsize!=bs){
845 slotsize=bs;
846 recalc();
847 update();
848 }
849 }
850
851
852 // Set increment
setIncrement(FXint inc)853 void FXSlider::setIncrement(FXint inc){
854 incr=inc;
855 }
856
857
858 // Set slot color
setSlotColor(FXColor clr)859 void FXSlider::setSlotColor(FXColor clr){
860 if(slotColor!=clr){
861 slotColor=clr;
862 update();
863 }
864 }
865
866
867 // Change the delta between ticks
setTickDelta(FXint dist)868 void FXSlider::setTickDelta(FXint dist){
869 if(dist<0) dist=0;
870 if(delta!=dist){
871 delta=dist;
872 if(options&(SLIDER_TICKS_TOP|SLIDER_TICKS_BOTTOM)){
873 recalc();
874 }
875 }
876 }
877
878
879 // Save object to stream
save(FXStream & store) const880 void FXSlider::save(FXStream& store) const {
881 FXFrame::save(store);
882 store << range[0] << range[1];
883 store << pos;
884 store << incr;
885 store << delta;
886 store << slotColor;
887 store << headsize;
888 store << slotsize;
889 store << help;
890 store << tip;
891 }
892
893
894 // Load object from stream
load(FXStream & store)895 void FXSlider::load(FXStream& store){
896 FXFrame::load(store);
897 store >> range[0] >> range[1];
898 store >> pos;
899 store >> incr;
900 store >> delta;
901 store >> slotColor;
902 store >> headsize;
903 store >> slotsize;
904 store >> help;
905 store >> tip;
906 }
907
908
909 // Delete
~FXSlider()910 FXSlider::~FXSlider(){
911 getApp()->removeTimeout(this,ID_AUTOSLIDE);
912 }
913
914 }
915