2 // "$Id: Fl_Counter.cxx 7903 2010-11-28 21:06:39Z matt $"
4 // Counter 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 #include <FL/Fl_Counter.H>
30 #include <FL/fl_draw.H>
32 void Fl_Counter::draw() {
33 int i
; Fl_Boxtype boxtype
[5];
37 if (boxtype
[0] == FL_UP_BOX
) boxtype
[0] = FL_DOWN_BOX
;
38 if (boxtype
[0] == FL_THIN_UP_BOX
) boxtype
[0] = FL_THIN_DOWN_BOX
;
41 boxtype
[i
] = fl_down(box());
46 if (type() == FL_NORMAL_COUNTER
) {
48 xx
[1] = x(); ww
[1] = W
;
49 xx
[2] = x()+1*W
; ww
[2] = W
;
50 xx
[0] = x()+2*W
; ww
[0] = w()-4*W
;
51 xx
[3] = x()+w()-2*W
; ww
[3] = W
;
52 xx
[4] = x()+w()-1*W
; ww
[4] = W
;
56 xx
[2] = x(); ww
[2] = W
;
57 xx
[0] = x()+W
; ww
[0] = w()-2*W
;
58 xx
[3] = x()+w()-1*W
; ww
[3] = W
;
62 draw_box(boxtype
[0], xx
[0], y(), ww
[0], h(), FL_BACKGROUND2_COLOR
);
63 fl_font(textfont(), textsize());
64 fl_color(active_r() ? textcolor() : fl_inactive(textcolor()));
65 char str
[128]; format(str
);
66 fl_draw(str
, xx
[0], y(), ww
[0], h(), FL_ALIGN_CENTER
);
67 if (Fl::focus() == this) draw_focus(boxtype
[0], xx
[0], y(), ww
[0], h());
68 if (!(damage()&FL_DAMAGE_ALL
)) return; // only need to redraw text
71 selcolor
= labelcolor();
73 selcolor
= fl_inactive(labelcolor());
75 if (type() == FL_NORMAL_COUNTER
) {
76 draw_box(boxtype
[1], xx
[1], y(), ww
[1], h(), color());
77 fl_draw_symbol("@-4<<", xx
[1], y(), ww
[1], h(), selcolor
);
79 draw_box(boxtype
[2], xx
[2], y(), ww
[2], h(), color());
80 fl_draw_symbol("@-4<", xx
[2], y(), ww
[2], h(), selcolor
);
81 draw_box(boxtype
[3], xx
[3], y(), ww
[3], h(), color());
82 fl_draw_symbol("@-4>", xx
[3], y(), ww
[3], h(), selcolor
);
83 if (type() == FL_NORMAL_COUNTER
) {
84 draw_box(boxtype
[4], xx
[4], y(), ww
[4], h(), color());
85 fl_draw_symbol("@-4>>", xx
[4], y(), ww
[4], h(), selcolor
);
89 void Fl_Counter::increment_cb() {
90 if (!mouseobj
) return;
93 case 1: v
-= lstep_
; break;
94 case 2: v
= increment(v
, -1); break;
95 case 3: v
= increment(v
, 1); break;
96 case 4: v
+= lstep_
; break;
98 handle_drag(clamp(round(v
)));
101 #define INITIALREPEAT .5
104 void Fl_Counter::repeat_callback(void* v
) {
105 Fl_Counter
* b
= (Fl_Counter
*)v
;
107 Fl::add_timeout(REPEAT
, repeat_callback
, b
);
112 int Fl_Counter::calc_mouseobj() {
113 if (type() == FL_NORMAL_COUNTER
) {
115 if (Fl::event_inside(x(), y(), W
, h())) return 1;
116 if (Fl::event_inside(x()+W
, y(), W
, h())) return 2;
117 if (Fl::event_inside(x()+w()-2*W
, y(), W
, h())) return 3;
118 if (Fl::event_inside(x()+w()-W
, y(), W
, h())) return 4;
121 if (Fl::event_inside(x(), y(), W
, h())) return 2;
122 if (Fl::event_inside(x()+w()-W
, y(), W
, h())) return 3;
127 int Fl_Counter::handle(int event
) {
132 Fl::remove_timeout(repeat_callback
, this);
139 if (Fl::visible_focus()) Fl::focus(this);
140 { Fl_Widget_Tracker
wp(this);
142 if (wp
.deleted()) return 1;
147 Fl::remove_timeout(repeat_callback
, this);
149 if (i
) Fl::add_timeout(INITIALREPEAT
, repeat_callback
, this);
150 Fl_Widget_Tracker
wp(this);
152 if (wp
.deleted()) return 1;
157 switch (Fl::event_key()) {
159 handle_drag(clamp(increment(value(),-1)));
162 handle_drag(clamp(increment(value(),1)));
167 // break not required because of switch...
168 case FL_FOCUS
: /* FALLTHROUGH */
170 if (Fl::visible_focus()) {
174 case FL_ENTER
: /* FALLTHROUGH */
183 Destroys the valuator.
185 Fl_Counter::~Fl_Counter() {
186 Fl::remove_timeout(repeat_callback
, this);
190 Creates a new Fl_Counter widget using the given position, size, and label
191 string. The default type is FL_NORMAL_COUNTER.
192 \param[in] X, Y, W, H position and size of the widget
193 \param[in] L widget label, default is no label
195 Fl_Counter::Fl_Counter(int X
, int Y
, int W
, int H
, const char* L
)
196 : Fl_Valuator(X
, Y
, W
, H
, L
) {
198 selection_color(FL_INACTIVE_COLOR
); // was FL_BLUE
199 align(FL_ALIGN_BOTTOM
);
200 bounds(-1000000.0, 1000000.0);
201 Fl_Valuator::step(1, 10);
204 textfont_
= FL_HELVETICA
;
205 textsize_
= FL_NORMAL_SIZE
;
206 textcolor_
= FL_FOREGROUND_COLOR
;
210 // End of "$Id: Fl_Counter.cxx 7903 2010-11-28 21:06:39Z matt $".