-
Notifications
You must be signed in to change notification settings - Fork 0
/
GCLed.cpp
366 lines (238 loc) · 7.68 KB
/
GCLed.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
Copyright (C) 2019 BrerDawg
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
//GCLed.cpp
//---- v1.01 10-09-07
//---- v1.02 20-09-14 //modified to be a subclass of Fl_Widget (used to be Fl_Window), this was done so it could placed in a pack or scroll widget, as in pref.cpp
//modified callback for use in pref.cpp, changed 'int (*pCallback)( Fl_Widget *w, int e )', to: void (*pCallback)( Fl_Widget *w, void* e );
//added outline when control has focus
//added set_col_from_str(..)
//---- v1.03 2017-oct-30 //modified/nullyfied 'if ( e & FL_LEAVE )' code, it no longer sets 'dont_pass_on = 1', so handle() no
//longer returns 1 for FL_LEAVE messages, without this the parent app was blocked from receiving menu keybrd shortcuts like: "FL_CTRL + 'c'"
//---- v1.04 2018-oct-30 //added 'id' 'id2' to help identify this obj when multiple instances are in use
//---- v1.05 2020-mar-18 //moded all non space keydowns to pass onto parent
//---- v1.06 2020-sep-19 //chaged 'if ( e & FL_ENTER )' to 'if ( e == FL_ENTER )' as this was stopping a parent 'FL_Scroll' scrolling via mousewheel when mouse inside 'GCLed'
//---- v1.07 2020-oct-04 //added 'set_mouse_button_changed_callback()' and public 'left_button' 'middle_button' 'right_button' state flags
#include "GCLed.h"
using namespace std;
/*
//EXAMPLE CODE callback using 'set_mouse_button_changed_callback()'
//shown is a 4 state 4 coloured led
void cb_led_chrd_rec(Fl_Widget* w, void*v)
{
GCLed *wdj = (GCLed*)w;
int which = (intptr_t)v;
int z = wdj->GetColIndex();
int z2 = z;
bool gone_into_play = 0;
bool gone_into_record = 0;
bool gone_into_record_stop = 0;
if( wdj->left_button )
{
if( z == 0 ) { z2 = 1; gone_into_play = 1; }
if( z == 1 ) z2 = 0;
if( z == 2 ) z2 = 0;
if( z == 3 ) { z2 = 0; gone_into_record_stop = 1; }
}
if( wdj->middle_button )
{
if( z == 1 ) z2 = 0;
if( z == 2 ) z2 = 0;
if( z == 3 ) { z2 = 0; gone_into_record_stop = 1; }
}
if( wdj->right_button )
{
if( z == 0 ) z2 = 2;
if( z == 1 ) z2 = 2;
if( z == 2 ) z2 = 3; gone_into_record = 1;
if( z == 3 ) { z2 = 0; gone_into_record_stop = 1; }
}
if( gone_into_play )
{
do_something( 0 );
}
if( gone_into_record )
{
do_something( 1 );
}
if( gone_into_record_stop )
{
do_something( 2 );
}
wdj->ChangeCol( z2 ); //change led colour
}
*/
GCLed::GCLed( int xx, int yy, int ww, int hh, const char *label ) : Fl_Widget( xx, yy, ww, hh, label )
{
has_focus = 0;
iIndex=0;
led_style = cn_gcled_style_square;
p_mouse_button_changed_cb = 0;
id = 0;
id2 = 0;
left_button = middle_button = right_button = 0;
for(int i=0;i<cnMaxColIndex;i++)
{
iR[i]=iG[i]=iB[i]=0;
}
}
GCLed::~GCLed()
{
}
void GCLed::draw()
{
//Fl_Window::draw();
if( led_style == cn_gcled_style_square )
{
fl_line_style( FL_SOLID );
fl_color( 0, 0, 0 );
fl_rect( x() + 2, y() + 2, w() - 4, h() - 4 ); //draw outline
fl_rectf( x() + 3, y() + 3, w() - 6, h() - 6, iR[ iIndex ], iG[ iIndex ], iB[ iIndex ] ); //draw fill col
fl_color( 0, 0, 0 );
//draw focus
if( !has_focus ) fl_color( parent()->color() );
fl_line_style( FL_DOT );
fl_rect( x(), y(), w(), h() );
fl_line_style( FL_SOLID );
}
if( led_style == cn_gcled_style_round )
{
fl_line_style( FL_SOLID );
fl_color( 0, 0, 0 );
fl_arc( x() + 2, y() + 2, w() - 4, h() - 4, 0, 360 ); //draw outline
fl_color( iR[ iIndex ], iG[ iIndex ], iB[ iIndex ] );
fl_pie( x() + 3, y() + 3, w() - 6, h() - 6, 0, 360 ); //draw fill col
//draw focus
fl_color( 0, 0, 0 );
if( !has_focus ) fl_color( parent()->color() );
fl_line_style( FL_DOT );
fl_arc( x(), y(), w(), h(), 0, 360 );
fl_line_style( FL_SOLID );
}
fl_color( 0, 0, 0 );
}
int GCLed::handle( int e )
{
bool need_redraw = 0;
bool dont_pass_on = 0;
if ( e == FL_ENTER ) //v1.06, was 'e & FL_ENTER'
{
dont_pass_on = 1; //need this for tooltip
}
if ( e & FL_LEAVE ) //see v1.03 mods, this code does nothing now
{
// dont_pass_on = 1; //need this for tooltip
}
//return Fl_Widget::handle(e);
if ( e == FL_FOCUS )
{
has_focus = 1; //need this for focus rectangle to be drawn
dont_pass_on = 1;
need_redraw = 1;
}
if ( e == FL_UNFOCUS )
{
has_focus = 0; //need this for focus rectangle to be drawn
dont_pass_on = 1;
need_redraw = 1;
}
if ( e == FL_PUSH )
{
if( Fl::event_button() == 1 ) left_button = 1;
if( Fl::event_button() == 2 ) middle_button = 1;
if( Fl::event_button() == 3 ) right_button = 1;
take_focus();
has_focus = 1;
if( p_mouse_button_changed_cb ) p_mouse_button_changed_cb( cb_mouse_button_changed_obj, cb_mouse_button_changed_args );
do_callback();
dont_pass_on = 1;
need_redraw = 1;
}
if ( e == FL_RELEASE )
{
if( Fl::event_button() == 1 ) left_button = 0;
if( Fl::event_button() == 2 ) middle_button = 0;
if( Fl::event_button() == 3 ) right_button = 0;
if( p_mouse_button_changed_cb ) p_mouse_button_changed_cb( cb_mouse_button_changed_obj, cb_mouse_button_changed_args );
dont_pass_on = 1;
need_redraw = 1;
}
if ( e == FL_KEYDOWN )
{
int key = Fl::event_key();
if( key == ' ' )
{
do_callback();
dont_pass_on = 1; //v1.05
}
}
if( need_redraw )
{
redraw();
}
if( dont_pass_on ) return 1;
return Fl_Widget::handle(e);
}
int GCLed::SetColIndex(int iIndex,int iRin,int iGin,int iBin)
{
if( iIndex >= cnMaxColIndex ) return 0;
iR[iIndex]=iRin;
iG[iIndex]=iGin;
iB[iIndex]=iBin;
return 1;
}
//load colour indexes from a string like this: "255 0 0, 0 255 0, 0 0 255"
//note that each rgb colour set is seperated from next set by a comma, there MUST be a space between primary colour values within each set
int GCLed::set_col_from_str( string rbg_list, char delimiter )
{
string s1;
mystr m1;
vector<string> vstr;
int colours[ 3 ];
m1 = rbg_list;
m1.LoadVectorStrings( vstr, delimiter ); //extract each colour set
for( int i = 0; i < vstr.size(); i++ )
{
if( i >= cnMaxColIndex ) break; //ensure we dont pass storage limit
m1 = vstr[ i ];
//printf( "vstr[ i ]: %s\n", vstr[ i ].c_str() );
int j = m1.LoadArrayInts( colours, 3 , ' ' ); //extract primary colour values
if( j == 3 )
{
iR[ i ] = colours[ 0 ]; //store colours
iG[ i ] = colours[ 1 ];
iB[ i ] = colours[ 2 ];
}
}
return 1;
}
int GCLed::GetColIndex()
{
if(iIndex>=cnMaxColIndex) return -1;
return iIndex;
}
bool GCLed::ChangeCol( int iIn )
{
if(iIn>=cnMaxColIndex) return 0;
iIndex=iIn;
redraw();
return 1;
}
//e.g: use public var 'left_button' to determine state
void GCLed::set_mouse_button_changed_callback( void (*p_cb)( void*, void* ), void *obj_in, void *args_in )
{
p_mouse_button_changed_cb = p_cb;
cb_mouse_button_changed_obj = obj_in;
cb_mouse_button_changed_args = args_in;
}