1 /*
2  * Author: Harry van Haaren 2013
3  *         harryhaaren@gmail.com
4  *
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 
20 #ifndef AVTK_BUTTON_H
21 #define AVTK_BUTTON_H
22 
23 #include <ntk/FL/Fl_Button.H>
24 
25 namespace Avtk
26 {
27 
28 class Button : public Fl_Button
29 {
30 public:
Button(int _x,int _y,int _w,int _h,const char * _label)31 	Button(int _x, int _y, int _w, int _h, const char *_label):
32 		Fl_Button(_x, _y, _w, _h)
33 	{
34 		copy_label(_label);
35 		x = _x;
36 		y = _y;
37 		w = _w;
38 		h = _h;
39 
40 		_r = 1.0;
41 		_g = 0.48;
42 		_b = 0.0;
43 
44 		_bgr = 0.11;
45 		_bgg = 0.11;
46 		_bgb = 0.11;
47 
48 		highlight = false;
49 		mouseOver = false;
50 
51 		greyedOut = false;
52 	}
53 
setGreyOut(bool g)54 	void setGreyOut( bool g )
55 	{
56 		greyedOut = g;
57 	}
58 
59 	bool greyedOut;
60 
61 	bool mouseOver;
62 	bool highlight;
63 	int x, y, w, h;
64 
65 	float _r, _g, _b;       // foreground colour
66 	float _bgr, _bgg, _bgb; // background colour
67 	float _outr, _outg, _outb; // outline colour
68 
setColor(float r,float g,float b)69 	void setColor(float r, float g, float b)
70 	{
71 		_r = r;
72 		_g = g;
73 		_b = b;
74 	}
75 
setBgColor(float r,float g,float b)76 	void setBgColor(float r, float g, float b)
77 	{
78 		_bgr = r;
79 		_bgg = g;
80 		_bgb = b;
81 	}
82 
setHighlight(bool b)83 	void setHighlight(bool b)
84 	{
85 		highlight = b;
86 		redraw();
87 	}
88 
draw()89 	void draw()
90 	{
91 		if (damage() & FL_DAMAGE_ALL) {
92 			cairo_t *cr = Fl::cairo_cc();
93 
94 			cairo_save( cr );
95 
96 			cairo_rectangle( cr, x+1, y+1, w-2, h-2 );
97 			if ( !greyedOut )
98 				cairo_set_source_rgb( cr, _bgr, _bgg, _bgb );
99 			else {
100 				float grey = (_bgr + _bgg + _bgb) / 3;
101 				cairo_set_source_rgb( cr, grey, grey, grey );
102 			}
103 			cairo_fill_preserve(cr);
104 
105 			cairo_set_line_width(cr, 1.3);
106 			cairo_rectangle( cr, x+1, y+1, w-2, h-2 );
107 
108 			if ( highlight && !greyedOut ) {
109 				cairo_set_source_rgba(cr, _r, _g, _b, 0.4);
110 				cairo_fill_preserve(cr);
111 			}
112 
113 			float alpha = 0.6;
114 			if (mouseOver)
115 				alpha = 1;
116 
117 			if ( !greyedOut )
118 				cairo_set_source_rgba(cr,  _r, _g, _b, alpha);
119 			else {
120 				float grey = (_r + _g + _b) / 3;
121 				cairo_set_source_rgb( cr, grey, grey, grey );
122 			}
123 
124 			if ( highlight && !greyedOut )
125 				cairo_set_line_width(cr, 2.2);
126 			cairo_stroke(cr);
127 
128 			cairo_restore( cr );
129 
130 			draw_label();
131 		}
132 	}
133 
resize(int X,int Y,int W,int H)134 	void resize(int X, int Y, int W, int H)
135 	{
136 		Fl_Widget::resize(X,Y,W,H);
137 		x = X;
138 		y = Y;
139 		w = W;
140 		h = H;
141 		redraw();
142 	}
143 
handle(int event)144 	int handle(int event)
145 	{
146 		switch(event) {
147 		case FL_PUSH:
148 			highlight = 1;
149 			redraw();
150 			return 1;
151 		case FL_DRAG: {
152 			int t = Fl::event_inside(this);
153 			if (t != highlight) {
154 				highlight = t;
155 				redraw();
156 			}
157 		}
158 		return 1;
159 		case FL_ENTER:
160 			mouseOver = true;
161 			redraw();
162 			return 1;
163 		case FL_LEAVE:
164 			mouseOver = false;
165 			redraw();
166 			return 1;
167 		case FL_RELEASE:
168 			if (highlight) {
169 				highlight = 0;
170 				redraw();
171 				do_callback();
172 			}
173 			return 1;
174 		case FL_SHORTCUT:
175 			if ( test_shortcut() ) {
176 				do_callback();
177 				return 1;
178 			}
179 			return 0;
180 		default:
181 			return Fl_Widget::handle(event);
182 		}
183 	}
184 };
185 
186 } // Avtk
187 
188 #endif // AVTK_BUTTON_H
189 
190