-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_gui_fltk.cpp
More file actions
426 lines (336 loc) · 10.5 KB
/
simple_gui_fltk.cpp
File metadata and controls
426 lines (336 loc) · 10.5 KB
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#include "simple_gui_fltk.h"
#include <math.h>
void simple_gui_slider_cb(Fl_Widget* w, void* user_data)
{
*((double*)user_data) = ((Fl_Hor_Value_Slider*)w)->value();
}
void simple_gui_checkbox_cb(Fl_Widget* w, void* user_data)
{
*((bool*)user_data) = ((Fl_Check_Button*)w)->value();
}
void simple_gui_button_cb(Fl_Widget* w, void* user_data)
{
// cout << "button pressed..\n";
// execute std:: function
(*((std::function<void()>*)user_data))();
}
bool Simple_gui::check_valid()
{
if (window == nullptr)
{
std::cerr << "Simple_gui: first call setup()" << std::endl;
return false;
}
return true;
}
void Simple_gui::setup(int x, int y, int w, int h, const char* title)
{
wx = x;
wy = y;
ww = w;
wh = h;
window = std::make_shared<Fl_Window>(wx, wy, ww, wh, title);
buttton_functions.reserve(1000); // avoid reallocations to keep pointers constant
/*
sliders.clear();
slider_value_refs.clear();
check_buttons.clear();
check_buttons_value_refs.clear();
buttton_functions.clear();
// widged placement cursor pos
x = 10;
y = 10;
current_box = nullptr;
*/
}
void Simple_gui::show()
{
if (window != nullptr)
{
window->show();
Fl::check();
// the true position and size might deviate from the requested position due to window manager interference.
wx = window->x();
wy = window->y();
ww = window->w();
wh = window->h();
}
}
void Simple_gui::hide()
{
if (window != nullptr)
{
window->hide();
}
}
void Simple_gui::finish()
{
box_and_group_adjust_size();
if (window)
{
if (current_group)
{
current_group->end();
window->add(current_group);
}
window->end();
}
}
void Simple_gui::update_widgets()
{
for (int i = 0; i < slider_value_refs.size(); i++)
{
sliders[i]->value(*slider_value_refs[i]);
}
for (int i = 0; i < check_buttons_value_refs.size(); i++)
{
check_buttons[i]->value(*check_buttons_value_refs[i]);
}
}
void Simple_gui::add_separator_box(const char* label)
{
if (!check_valid()) { return; }
if (current_group)
{
box_and_group_adjust_size();
current_group->end();
window->add(current_group);
}
// spacing between group boxes
if (current_box)
{
cy += 10;
}
// create a group, such that radio buttons work properly within the box
current_group = new Fl_Group(5, cy, ww - 10, 8025); // make to group very large and adjust size later
//window->add(current_group);
// generate a box
current_box = new Fl_Box(5, cy, ww - 10, w_height + 15, label);
current_box->box(FL_BORDER_BOX);
current_box->align(FL_ALIGN_LEFT | FL_ALIGN_INSIDE | FL_ALIGN_TOP);
window->add(current_box);
if (std::string(label) != "")
{
cy += 15; // spacing for the label string
}
cy+= 5;
}
Fl_Hor_Value_Slider* Simple_gui::add_slider(const char* label, double& val, double min_val, double max_val, double step, const char* tooltip)
{
if (!check_valid()) { return nullptr; }
// auto guess max value (to be 10^x)
if (val >= max_val)
{
// TODO: fix for negative values
max_val = pow(10, ceil(log10(val)));
}
// first, generate a slider somewhere
auto slider = new Fl_Hor_Value_Slider(10, cy, 50, w_height, label);
slider->align(FL_ALIGN_LEFT);
slider->bounds(min_val, max_val);
slider->value(val);
slider->step(step);
slider->callback(simple_gui_slider_cb, &val);
if (tooltip != nullptr) { slider->copy_tooltip(tooltip); }
sliders.push_back(slider);
slider_value_refs.push_back(&val);
cy += w_height + 2;// round(widget_height / 5);
// now, measure label width and reposition all sliders
int label_w = 0, label_h = 0;
slider->measure_label(label_w, label_h);
if (label_w > max_slider_label_width)
{
max_slider_label_width = label_w;
}
int sw = ww - 20 - max_slider_label_width; // slider width
for (auto s : sliders)
{
int sy = s->y();
s->resize(10 + max_slider_label_width, sy, sw, w_height);
}
box_and_group_adjust_size();
return slider;
}
Fl_Check_Button* Simple_gui::add_checkbox(const char* label, bool& val, const char* tooltip)
{
if (!check_valid()) { return nullptr; }
if (!current_group) { add_separator_box(""); }
const int col_width = (ww - 20) / num_cols;
int pos_x = 10 + cur_col * col_width;
auto button = new Fl_Check_Button(pos_x, cy, 100, w_height, label);
button->value(val);
button->callback(simple_gui_checkbox_cb, &val);
if (tooltip != nullptr) { button->tooltip(tooltip); }
check_buttons.push_back(button);
check_buttons_value_refs.push_back(&val);
cur_col++;
// go to next line
if (cur_col == num_cols)
{
cy += w_height + 2;
cur_col = 0;
}
box_and_group_adjust_size();
current_group->add(button);
return button;
}
/*
Fl_Radio_Round_Button* ba = Fl_Radio_Round_Button(20, 20, 180, 20, "Button A");
Fl_Radio_Round_Button* bb = Fl_Radio_Round_Button(20, 50, 180, 20, "Button B");
Fl_Radio_Round_Button* bc = Fl_Radio_Round_Button(20, 80, 180, 20, "Button C");
*/
// explicit instantiation
template Fl_Button* Simple_gui::add_button_helper<Fl_Button>(const char* label, std::function<void()> func, const char* tooltip);
template Fl_Radio_Round_Button* Simple_gui::add_button_helper<Fl_Radio_Round_Button>(const char* label, std::function<void()> func, const char* tooltip);
template Fl_Light_Button* Simple_gui::add_button_helper<Fl_Light_Button>(const char* label, std::function<void()> func, const char* tooltip);
template<class T> T* Simple_gui::add_button_helper(const char* label, std::function<void()> func, const char* tooltip)
{
if (!check_valid()) { return nullptr; }
if (!current_group) { add_separator_box(""); }
if (buttton_functions.size() < buttton_functions.capacity())
{
buttton_functions.push_back(func);
}
else
{
std::cerr << "maximum number of allowed buttons reached.\n";
return nullptr;
}
const int col_width = (ww - 20) / num_cols;
int pos_x = 10 + cur_col * col_width;
auto button = new T(pos_x, cy, 100, w_height, label);
// fit button size to label width
int label_w = 0, label_h = 0;
button->measure_label(label_w, label_h);
button->resize(pos_x, cy, label_w + 20, w_height);
//button->callback(simple_gui_button_cb, (void*)&(func));
button->callback(simple_gui_button_cb, (void*)&(buttton_functions.back()));
if (tooltip != nullptr) { button->tooltip(tooltip); }
current_group->add(button);
cur_col++;
// go to next line
if (cur_col == num_cols)
{
cy += w_height + 2;
cur_col = 0;
}
return button;
}
Fl_Light_Button* Simple_gui::add_toggle_button(const char* label, bool& state, const char* tooltip)
{
auto button = add_button_helper<Fl_Light_Button>(label, []() {}, tooltip);
buttton_functions.back() = [b = button, &state]()
{
state = false;
if (int(b->value()) == 1) { state = true; }
//std::cerr << "\n toggle button" << int(b->value());
};
return button;
}
void Simple_gui::box_and_group_adjust_size()
{
// fix the situation where the user wanted to have e.g. 3 columns, but placed just 2 widgets
if (cur_col < num_cols && cur_col > 0)
{
cy += w_height + 2;
cur_col = 0;
}
if (current_group)
{
current_group->resizable(NULL); // do not change the size and position of child widgets
current_group->resize(current_group->x(), current_group->y(), current_group->w(), cy - current_group->y() + 5);
}
if (current_box)
{
current_box->size(current_box->w(), cy - current_box->y() + 5);
}
}
#ifdef __TEST_THIS_MODULE__
//#pragma comment ( lib, "comctl32.lib")
//#pragma comment ( lib, "ws2_32.lib")
#ifdef _DEBUG
#pragma comment(lib, "fltk14/bin/lib/Debug/fltkd.lib")
#else
#pragma comment(lib, "fltk14/bin/lib/Release/fltk.lib")
#endif
#ifdef _WIN32
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "comctl32.lib")
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "gdi32.lib")
#pragma comment(lib, "winspool.lib")
#pragma comment(lib, "shell32.lib")
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "oleaut32.lib")
#pragma comment(lib, "uuid.lib")
#pragma comment(lib, "comdlg32.lib")
#pragma comment(lib, "advapi32.lib")
#endif
void test_module_simple_gui()
{
/*
auto w = new Fl_Window(50, 50, 500, 500, "hallo");
auto slider = new Fl_Hor_Value_Slider(10, 10, 400, 25, "hallo");
slider->align(FL_ALIGN_LEFT);
slider->bounds(0, 100);
slider->value(50);
slider->step(1);
//slider->callback(simple_gui_slider_cb, &val);
const char* tooltip = "nice slider!";
if (tooltip != nullptr) { slider->tooltip(tooltip); }
w->end();
w->show();
while(true) { Fl::check(); }
return;
//*/
using namespace std;
// first, create a gui window:
Simple_gui sg(1000, 100, 480, 480);
/*
Simple_gui sg_local(50, 50, 150, 100, "Record and Stream");
sg_local.add_button("stop streaming", [&]() { run = false; });
sg_local.add_button("quit program", [&]() { exit(EXIT_SUCCESS); });
sg_local.finish();
*/
// easiest is to add a button:
sg.num_columns(1);
sg.add_button("print hello world!", []() {cout << "Hello world!\n"; }, "prints hello world!");
// now, adding some sliders is also easy:
double params[3]{ 10,110, 0.2 };
sg.add_slider("contrast:", params[0], 0, 100,1, "slider for contrast setting");
sg.add_slider("blur:", params[1], 0, 100,1, "slider for blur");
sg.add_slider("window size:", params[2], 640, 1920, 1, "slider for window size");
// you cann add a box to visually separate and group widgets:
sg.add_separator_box("some checkboxes:");
bool options[4]{ true, false, true, true };
sg.add_checkbox("first option 1", options[0], "tooltip checkbox 1");
sg.add_checkbox("second option 2", options[1], "tooltip checkbox 2");
sg.add_checkbox("third option 3", options[2], "tooltip checkbox 3");
sg.add_separator_box("more checkboxes:");
bool options2[3]{ true, false, true };
sg.num_columns(3);
sg.add_checkbox("checkbox column 1", options[0],"tooltip checkbox 1");
sg.add_checkbox("checkbox column 2", options[1], "tooltip checkbox 2");
sg.add_checkbox("checkbox column 3", options[2], "tooltip checkbox 3");
// a bit more complicated - using radio buttons
sg.add_separator_box("a radio button group:");
int my_val = 0;
auto cb_func = [&](int val) { my_val = val; std::cout << "my value set to:" << val << std::endl; };
sg.num_columns(1);
sg.add_radio_button("radio button 1", [&]() {cb_func(0); },"radio button 1");
// pre-select this radio buttton
auto b = sg.add_radio_button("radio button 2", [&]() {cb_func(1); }, "radio button 2"); if (b) { b->value(true); }
sg.add_radio_button("radio button 3", [&]() {cb_func(2); }, "radio button 3");
// finish gui creation
sg.finish();
sg.show();
while (Fl::check())
{
std::cout << "p1 = " << params[0] << "\tp2 = " << params[1] << "\tp3 = " << params[2] << std::endl;
Fl::wait();
}
}
#endif