2 // "$Id: Fl_Positioner.cxx 7903 2010-11-28 21:06:39Z matt $"
4 // Positioner widget for the Fast Light Tool Kit (FLTK).
6 // Copyright 1998-2010 by Bill Spitzak and others.
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
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 // Library General Public License for more details.
18 // You should have received a copy of the GNU Library 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
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
29 // The positioner widget from Forms, gives 2D input
30 // Written by: Mark Overmars
33 #include <FL/Fl_Positioner.H>
34 #include <FL/fl_draw.H>
36 static double flinear(double val
, double smin
, double smax
, double gmin
, double gmax
)
38 if (smin
== smax
) return gmax
;
39 else return gmin
+ (gmax
- gmin
) * (val
- smin
) / (smax
- smin
);
42 void Fl_Positioner::draw(int X
, int Y
, int W
, int H
) {
47 int xx
= int(flinear(xvalue(), xmin
, xmax
, x1
, x1
+w1
-1)+.5);
48 int yy
= int(flinear(yvalue(), ymin
, ymax
, y1
, y1
+h1
-1)+.5);
49 draw_box(box(), X
, Y
, W
, H
, color());
50 fl_color(selection_color());
51 fl_xyline(x1
, yy
, x1
+w1
);
52 fl_yxline(xx
, y1
, y1
+h1
);
55 void Fl_Positioner::draw() {
56 draw(x(), y(), w(), h());
60 /** Returns the current position in x and y.*/
61 int Fl_Positioner::value(double X
, double Y
) {
63 if (X
== xvalue_
&& Y
== yvalue_
) return 0;
64 xvalue_
= X
; yvalue_
= Y
;
69 /** Sets the X axis coordinate.*/
70 int Fl_Positioner::xvalue(double X
) {
71 return(value(X
, yvalue_
));
74 /** Sets the Y axis coordinate.*/
75 int Fl_Positioner::yvalue(double Y
) {
76 return(value(xvalue_
, Y
));
79 int Fl_Positioner::handle(int event
, int X
, int Y
, int W
, int H
) {
86 double w1
= W
- 2 * 4;
87 double h1
= H
- 2 * 4;
88 double xx
= flinear(Fl::event_x(), x1
, x1
+w1
-1.0, xmin
, xmax
);
89 if (xstep_
) xx
= int(xx
/xstep_
+0.5) * xstep_
;
91 if (xx
< xmin
) xx
= xmin
;
92 if (xx
> xmax
) xx
= xmax
;
94 if (xx
> xmin
) xx
= xmin
;
95 if (xx
< xmax
) xx
= xmax
;
97 double yy
= flinear(Fl::event_y(), y1
, y1
+h1
-1.0, ymin
, ymax
);
98 if (ystep_
) yy
= int(yy
/ystep_
+0.5) * ystep_
;
100 if (yy
< ymin
) yy
= ymin
;
101 if (yy
> ymax
) yy
= ymax
;
103 if (yy
> ymin
) yy
= ymin
;
104 if (yy
< ymax
) yy
= ymax
;
106 if (xx
!= xvalue_
|| yy
!= yvalue_
) {
107 xvalue_
= xx
; yvalue_
= yy
;
111 if (!(when() & FL_WHEN_CHANGED
||
112 (when() & FL_WHEN_RELEASE
&& event
== FL_RELEASE
))) return 1;
113 if (changed() || when()&FL_WHEN_NOT_CHANGED
) {
114 if (event
== FL_RELEASE
) clear_changed();
123 int Fl_Positioner::handle(int e
) {
124 return handle(e
, x(), y(), w(), h());
128 Creates a new Fl_Positioner widget using the given position,
129 size, and label string. The default boxtype is FL_NO_BOX.
131 Fl_Positioner::Fl_Positioner(int X
, int Y
, int W
, int H
, const char* l
)
132 : Fl_Widget(X
, Y
, W
, H
, l
) {
134 selection_color(FL_RED
);
135 align(FL_ALIGN_BOTTOM
);
136 when(FL_WHEN_CHANGED
);
139 xvalue_
= yvalue_
= .5;
143 /** Sets the X axis bounds.*/
144 void Fl_Positioner::xbounds(double a
, double b
) {
145 if (a
!= xmin
|| b
!= xmax
) {
151 /** Sets the Y axis bounds.*/
152 void Fl_Positioner::ybounds(double a
, double b
) {
153 if (a
!= ymin
|| b
!= ymax
) {
160 // End of "$Id: Fl_Positioner.cxx 7903 2010-11-28 21:06:39Z matt $".