1998-10-20 00:46:58 +04:00
|
|
|
//
|
2005-02-25 00:55:12 +03:00
|
|
|
// "$Id$"
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
|
|
|
// Scroll bar widget for the Fast Light Tool Kit (FLTK).
|
|
|
|
//
|
2010-11-29 00:06:39 +03:00
|
|
|
// Copyright 1998-2010 by Bill Spitzak and others.
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Library General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Library General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Library General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
// USA.
|
|
|
|
//
|
2005-04-16 04:13:17 +04:00
|
|
|
// Please report all bugs and problems on the following page:
|
|
|
|
//
|
|
|
|
// http://www.fltk.org/str.php
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2008-09-15 12:41:54 +04:00
|
|
|
|
1998-10-06 22:21:25 +04:00
|
|
|
#include <FL/Fl.H>
|
|
|
|
#include <FL/Fl_Scrollbar.H>
|
|
|
|
#include <FL/fl_draw.H>
|
|
|
|
#include <math.h>
|
2006-09-17 18:58:25 +04:00
|
|
|
#include "flstring.h"
|
1998-10-06 22:21:25 +04:00
|
|
|
|
|
|
|
#define INITIALREPEAT .5
|
|
|
|
#define REPEAT .05
|
|
|
|
|
|
|
|
void Fl_Scrollbar::increment_cb() {
|
2010-02-26 23:44:35 +03:00
|
|
|
char inv = maximum()<minimum();
|
|
|
|
int ls = inv ? -linesize_ : linesize_;
|
1999-12-04 15:22:37 +03:00
|
|
|
int i;
|
|
|
|
switch (pushed_) {
|
2010-02-26 23:44:35 +03:00
|
|
|
case 1: // clicked on arrow left
|
|
|
|
i = -ls;
|
|
|
|
break;
|
|
|
|
default: // clicked on arrow right
|
|
|
|
i = ls;
|
|
|
|
break;
|
|
|
|
case 5: // clicked into the box next to the slider on the left
|
|
|
|
i = -(int((maximum()-minimum())*slider_size()/(1.0-slider_size())));
|
|
|
|
if (inv) {
|
|
|
|
if (i<-ls) i = -ls;
|
|
|
|
} else {
|
|
|
|
if (i>-ls) i = -ls; // err
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 6: // clicked into the box next to the slider on the right
|
|
|
|
i = (int((maximum()-minimum())*slider_size()/(1.0-slider_size())));
|
|
|
|
if (inv) {
|
|
|
|
if (i>ls) i = ls;
|
|
|
|
} else {
|
|
|
|
if (i<ls) i = ls; // err
|
|
|
|
}
|
|
|
|
break;
|
1999-12-04 15:22:37 +03:00
|
|
|
}
|
|
|
|
handle_drag(clamp(value() + i));
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Fl_Scrollbar::timeout_cb(void* v) {
|
|
|
|
Fl_Scrollbar* s = (Fl_Scrollbar*)v;
|
|
|
|
s->increment_cb();
|
|
|
|
Fl::add_timeout(REPEAT, timeout_cb, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Fl_Scrollbar::handle(int event) {
|
1999-12-04 15:22:37 +03:00
|
|
|
// area of scrollbar:
|
|
|
|
int area;
|
|
|
|
int X=x(); int Y=y(); int W=w(); int H=h();
|
|
|
|
|
|
|
|
// adjust slider area to be inside the arrow buttons:
|
|
|
|
if (horizontal()) {
|
|
|
|
if (W >= 3*H) {X += H; W -= 2*H;}
|
|
|
|
} else {
|
|
|
|
if (H >= 3*W) {Y += W; H -= 2*W;}
|
|
|
|
}
|
|
|
|
|
|
|
|
// which widget part is highlighted?
|
2000-01-20 09:14:34 +03:00
|
|
|
int relx;
|
|
|
|
int ww;
|
|
|
|
if (horizontal()) {
|
|
|
|
relx = Fl::event_x()-X;
|
|
|
|
ww = W;
|
1999-12-04 15:22:37 +03:00
|
|
|
} else {
|
2000-01-20 09:14:34 +03:00
|
|
|
relx = Fl::event_y()-Y;
|
|
|
|
ww = H;
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
2000-01-20 09:14:34 +03:00
|
|
|
if (relx < 0) area = 1;
|
|
|
|
else if (relx >= ww) area = 2;
|
|
|
|
else {
|
|
|
|
int S = int(slider_size()*ww+.5);
|
|
|
|
int T = (horizontal() ? H : W)/2+1;
|
2005-05-12 17:48:54 +04:00
|
|
|
if (type()==FL_VERT_NICE_SLIDER || type()==FL_HOR_NICE_SLIDER) T += 4;
|
2000-01-20 09:14:34 +03:00
|
|
|
if (S < T) S = T;
|
2000-03-17 12:38:20 +03:00
|
|
|
double val =
|
|
|
|
(maximum()-minimum()) ? (value()-minimum())/(maximum()-minimum()) : 0.5;
|
2000-01-20 09:14:34 +03:00
|
|
|
int sliderx;
|
|
|
|
if (val >= 1.0) sliderx = ww-S;
|
|
|
|
else if (val <= 0.0) sliderx = 0;
|
|
|
|
else sliderx = int(val*(ww-S)+.5);
|
2002-09-28 16:04:12 +04:00
|
|
|
if (Fl::event_button() == FL_MIDDLE_MOUSE) area = 8;
|
|
|
|
else if (relx < sliderx) area = 5;
|
2001-07-30 22:30:39 +04:00
|
|
|
else if (relx >= sliderx+S) area = 6;
|
2000-01-20 09:14:34 +03:00
|
|
|
else area = 8;
|
|
|
|
}
|
|
|
|
|
1998-10-06 22:21:25 +04:00
|
|
|
switch (event) {
|
1999-12-04 15:22:37 +03:00
|
|
|
case FL_ENTER:
|
|
|
|
case FL_LEAVE:
|
|
|
|
return 1;
|
1998-10-06 22:21:25 +04:00
|
|
|
case FL_RELEASE:
|
2000-01-16 07:30:39 +03:00
|
|
|
damage(FL_DAMAGE_ALL);
|
1998-10-06 22:21:25 +04:00
|
|
|
if (pushed_) {
|
|
|
|
Fl::remove_timeout(timeout_cb, this);
|
|
|
|
pushed_ = 0;
|
|
|
|
}
|
|
|
|
handle_release();
|
|
|
|
return 1;
|
|
|
|
case FL_PUSH:
|
1999-12-04 15:22:37 +03:00
|
|
|
if (pushed_) return 1;
|
1999-12-29 06:14:41 +03:00
|
|
|
if (area != 8) pushed_ = area;
|
1998-10-06 22:21:25 +04:00
|
|
|
if (pushed_) {
|
|
|
|
handle_push();
|
|
|
|
Fl::add_timeout(INITIALREPEAT, timeout_cb, this);
|
|
|
|
increment_cb();
|
2000-01-16 07:30:39 +03:00
|
|
|
damage(FL_DAMAGE_ALL);
|
1999-12-04 15:22:37 +03:00
|
|
|
return 1;
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
1999-12-04 15:22:37 +03:00
|
|
|
return Fl_Slider::handle(event, X,Y,W,H);
|
1998-10-06 22:21:25 +04:00
|
|
|
case FL_DRAG:
|
1999-12-04 15:22:37 +03:00
|
|
|
if (pushed_) return 1;
|
|
|
|
return Fl_Slider::handle(event, X,Y,W,H);
|
2001-08-04 16:21:34 +04:00
|
|
|
case FL_MOUSEWHEEL :
|
2005-11-07 11:51:53 +03:00
|
|
|
if (horizontal()) {
|
|
|
|
if (Fl::e_dx==0) return 0;
|
2010-02-26 23:44:35 +03:00
|
|
|
int ls = maximum()>=minimum() ? linesize_ : -linesize_;
|
|
|
|
handle_drag(clamp(value() + ls * Fl::e_dx));
|
2005-11-26 03:47:45 +03:00
|
|
|
return 1;
|
2005-11-07 11:51:53 +03:00
|
|
|
} else {
|
|
|
|
if (Fl::e_dy==0) return 0;
|
2010-02-26 23:44:35 +03:00
|
|
|
int ls = maximum()>=minimum() ? linesize_ : -linesize_;
|
|
|
|
handle_drag(clamp(value() + ls * Fl::e_dy));
|
2005-11-26 03:47:45 +03:00
|
|
|
return 1;
|
2005-11-07 11:51:53 +03:00
|
|
|
}
|
2001-08-05 18:00:15 +04:00
|
|
|
case FL_SHORTCUT:
|
|
|
|
case FL_KEYBOARD: {
|
1998-10-06 22:21:25 +04:00
|
|
|
int v = value();
|
|
|
|
int ls = maximum()>=minimum() ? linesize_ : -linesize_;
|
|
|
|
if (horizontal()) {
|
|
|
|
switch (Fl::event_key()) {
|
|
|
|
case FL_Left:
|
|
|
|
v -= ls;
|
|
|
|
break;
|
|
|
|
case FL_Right:
|
|
|
|
v += ls;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else { // vertical
|
|
|
|
switch (Fl::event_key()) {
|
|
|
|
case FL_Up:
|
|
|
|
v -= ls;
|
|
|
|
break;
|
|
|
|
case FL_Down:
|
|
|
|
v += ls;
|
|
|
|
break;
|
|
|
|
case FL_Page_Up:
|
|
|
|
if (slider_size() >= 1.0) return 0;
|
|
|
|
v -= int((maximum()-minimum())*slider_size()/(1.0-slider_size()));
|
|
|
|
v += ls;
|
|
|
|
break;
|
|
|
|
case FL_Page_Down:
|
|
|
|
if (slider_size() >= 1.0) return 0;
|
|
|
|
v += int((maximum()-minimum())*slider_size()/(1.0-slider_size()));
|
|
|
|
v -= ls;
|
|
|
|
break;
|
|
|
|
case FL_Home:
|
|
|
|
v = int(minimum());
|
|
|
|
break;
|
|
|
|
case FL_End:
|
|
|
|
v = int(maximum());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v = int(clamp(v));
|
|
|
|
if (v != value()) {
|
|
|
|
Fl_Slider::value(v);
|
|
|
|
value_damage();
|
2004-07-27 20:02:21 +04:00
|
|
|
set_changed();
|
1998-10-06 22:21:25 +04:00
|
|
|
do_callback();
|
|
|
|
}
|
|
|
|
return 1;}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fl_Scrollbar::draw() {
|
1999-05-14 13:07:09 +04:00
|
|
|
if (damage()&FL_DAMAGE_ALL) draw_box();
|
|
|
|
int X = x()+Fl::box_dx(box());
|
|
|
|
int Y = y()+Fl::box_dy(box());
|
|
|
|
int W = w()-Fl::box_dw(box());
|
|
|
|
int H = h()-Fl::box_dh(box());
|
1998-10-06 22:21:25 +04:00
|
|
|
if (horizontal()) {
|
1999-05-14 13:07:09 +04:00
|
|
|
if (W < 3*H) {Fl_Slider::draw(X,Y,W,H); return;}
|
|
|
|
Fl_Slider::draw(X+H,Y,W-2*H,H);
|
1998-10-19 21:53:09 +04:00
|
|
|
if (damage()&FL_DAMAGE_ALL) {
|
2002-03-26 00:08:42 +03:00
|
|
|
draw_box((pushed_==1) ? fl_down(slider()) : slider(),
|
1999-05-14 13:07:09 +04:00
|
|
|
X, Y, H, H, selection_color());
|
2002-03-26 00:08:42 +03:00
|
|
|
draw_box((pushed_==2) ? fl_down(slider()) : slider(),
|
2002-08-13 19:42:44 +04:00
|
|
|
X+W-H, Y, H, H, selection_color());
|
1998-10-21 21:53:13 +04:00
|
|
|
if (active_r())
|
|
|
|
fl_color(labelcolor());
|
|
|
|
else
|
2001-10-29 06:44:33 +03:00
|
|
|
fl_color(fl_inactive(labelcolor()));
|
2000-04-11 12:11:56 +04:00
|
|
|
int w1 = (H-4)/3; if (w1 < 1) w1 = 1;
|
|
|
|
int x1 = X+(H-w1-1)/2;
|
2002-08-09 07:17:30 +04:00
|
|
|
int yy1 = Y+(H-2*w1-1)/2;
|
2006-09-17 18:58:25 +04:00
|
|
|
if (Fl::scheme_ && !strcmp(Fl::scheme_, "gtk+")) {
|
|
|
|
fl_polygon(x1, yy1+w1, x1+w1, yy1+2*w1, x1+w1-1, yy1+w1, x1+w1, yy1);
|
|
|
|
x1 += (W-H);
|
|
|
|
fl_polygon(x1, yy1, x1+1, yy1+w1, x1, yy1+2*w1, x1+w1, yy1+w1);
|
|
|
|
} else {
|
|
|
|
fl_polygon(x1, yy1+w1, x1+w1, yy1+2*w1, x1+w1, yy1);
|
|
|
|
x1 += (W-H);
|
|
|
|
fl_polygon(x1, yy1, x1, yy1+2*w1, x1+w1, yy1+w1);
|
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
} else { // vertical
|
1999-05-14 13:07:09 +04:00
|
|
|
if (H < 3*W) {Fl_Slider::draw(X,Y,W,H); return;}
|
|
|
|
Fl_Slider::draw(X,Y+W,W,H-2*W);
|
1998-10-19 21:53:09 +04:00
|
|
|
if (damage()&FL_DAMAGE_ALL) {
|
2002-03-26 00:08:42 +03:00
|
|
|
draw_box((pushed_==1) ? fl_down(slider()) : slider(),
|
1999-05-14 13:07:09 +04:00
|
|
|
X, Y, W, W, selection_color());
|
2002-03-26 00:08:42 +03:00
|
|
|
draw_box((pushed_==2) ? fl_down(slider()) : slider(),
|
1999-05-14 13:07:09 +04:00
|
|
|
X, Y+H-W, W, W, selection_color());
|
1998-10-21 21:53:13 +04:00
|
|
|
if (active_r())
|
|
|
|
fl_color(labelcolor());
|
|
|
|
else
|
2002-10-06 22:37:14 +04:00
|
|
|
fl_color(fl_inactive(labelcolor()));
|
2000-04-11 12:11:56 +04:00
|
|
|
int w1 = (W-4)/3; if (w1 < 1) w1 = 1;
|
|
|
|
int x1 = X+(W-2*w1-1)/2;
|
2002-08-09 07:17:30 +04:00
|
|
|
int yy1 = Y+(W-w1-1)/2;
|
2006-09-17 18:58:25 +04:00
|
|
|
if (Fl::scheme_ && !strcmp(Fl::scheme_, "gtk+")) {
|
|
|
|
fl_polygon(x1, yy1+w1, x1+w1, yy1+w1-1, x1+2*w1, yy1+w1, x1+w1, yy1);
|
|
|
|
yy1 += H-W;
|
|
|
|
fl_polygon(x1, yy1, x1+w1, yy1+1, x1+w1, yy1+w1);
|
|
|
|
fl_polygon(x1+w1, yy1+1, x1+2*w1, yy1, x1+w1, yy1+w1);
|
|
|
|
} else {
|
|
|
|
fl_polygon(x1, yy1+w1, x1+2*w1, yy1+w1, x1+w1, yy1);
|
|
|
|
yy1 += H-W;
|
|
|
|
fl_polygon(x1, yy1, x1+w1, yy1+w1, x1+2*w1, yy1);
|
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-15 12:41:54 +04:00
|
|
|
/**
|
2009-03-14 14:46:43 +03:00
|
|
|
Creates a new Fl_Scrollbar widget with given position, size, and label.
|
|
|
|
You need to do type(FL_HORIZONTAL) if you want a horizontal scrollbar.
|
2008-09-15 12:41:54 +04:00
|
|
|
*/
|
1998-10-06 22:21:25 +04:00
|
|
|
Fl_Scrollbar::Fl_Scrollbar(int X, int Y, int W, int H, const char* L)
|
2008-09-15 12:41:54 +04:00
|
|
|
: Fl_Slider(X, Y, W, H, L) {
|
1998-10-06 22:21:25 +04:00
|
|
|
box(FL_FLAT_BOX);
|
|
|
|
color(FL_DARK2);
|
|
|
|
slider(FL_UP_BOX);
|
|
|
|
linesize_ = 16;
|
|
|
|
pushed_ = 0;
|
|
|
|
step(1);
|
|
|
|
}
|
1998-10-20 00:46:58 +04:00
|
|
|
|
2008-09-15 12:41:54 +04:00
|
|
|
/** Destroys the Scrollbar. */
|
|
|
|
Fl_Scrollbar::~Fl_Scrollbar() {
|
2008-02-25 16:00:53 +03:00
|
|
|
if (pushed_)
|
|
|
|
Fl::remove_timeout(timeout_cb, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
2005-02-25 00:55:12 +03:00
|
|
|
// End of "$Id$".
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|