-
Notifications
You must be signed in to change notification settings - Fork 7
/
pgitems.cpp
747 lines (632 loc) · 15.3 KB
/
pgitems.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
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
/*
* nana::propgrid_items Implementation
*
* part of Nana Creator (https://github.com/besh81/nana-creator)
* Author: besh81
*/
#include <iostream>
#include <algorithm>
#include "pgitems.h"
#define INHERITED_COLOR "[inherited]"
namespace nana
{
class internal_lock_guard
{
public:
internal_lock_guard(bool* flag, bool lock_as_true = true)
{
flag_ = flag;
*flag_ = lock_as_true;
}
~internal_lock_guard()
{
*flag_ = !(*flag_);
}
private:
bool* flag_;
};
// color helper functions
nana::color to_color(const std::string& s)
{
bool inherited;
return to_color(s, inherited);
}
bool is_color_inherited(const std::string& s)
{
return s.find(INHERITED_COLOR) == 0 ? true : false;
}
nana::color to_color(const std::string& s, bool& inherited)
{
std::stringstream ss(s);
std::string item;
std::vector<int> items;
inherited = false;
try
{
while(getline(ss, item, ','))
{
if(item == INHERITED_COLOR)
inherited = true;
else
items.push_back(item.empty() ? 0 : std::stoi(item));
}
}
catch(...)
{
// reset
items.clear();
}
for(size_t i = 0; i < 3; ++i)
{
if(i >= items.size())
items.push_back(0);
else
items[i] = std::max(0, std::min(items[i], 255));
}
return nana::color(items[0], items[1], items[2]);
}
nana::color to_color(const std::string& r, const std::string& g, const std::string& b)
{
bool inherited;
return to_color(r + "," + g + "," + b, inherited);
}
std::string to_string(const nana::color& c, bool inherited)
{
return (inherited ? INHERITED_COLOR "," : "") + std::to_string(int(c.r())) + "," + std::to_string(int(c.g())) + "," + std::to_string(int(c.b()));
}
/// class pg_string
void pg_string::value(const std::string& value)
{
internal_lock_guard evt_lock(&evt_emit_, false);
txt_.caption(value);
pgitem::value(value);
}
void pg_string::enabled(bool state)
{
pgitem::enabled(state);
txt_.enabled(en_);
}
void pg_string::set_accept(std::function<bool(wchar_t)> f)
{
txt_.set_accept(f);
}
void pg_string::editable(bool editable)
{
txt_.editable(editable);
}
bool pg_string::editable()
{
return txt_.editable();
}
void pg_string::tooltip( const std::string& help_text )
{
txt_.tooltip( help_text );
}
void pg_string::create(window wd)
{
txt_.create(wd);
txt_.multi_lines(false);
txt_.caption(value_);
txt_.events().click.connect_front([this](const nana::arg_click& arg)
{
scroll();
});
txt_.events().dbl_click([this](const nana::arg_mouse& arg)
{
txt_.select(true);
});
txt_.events().key_press([this](const nana::arg_keyboard& arg)
{
if(arg.key == nana::keyboard::enter)
{
pgitem::value(txt_.caption());
emit_event();
}
});
txt_.events().focus([this](const nana::arg_focus& arg)
{
if( !arg.getting )
{
// just lost focus, so capture the value left by the user, if changed
if( txt_.caption() != pgitem::value() )
{
pgitem::value( txt_.caption() );
emit_event();
}
}
});
}
void pg_string::draw_value(paint::graphics* graph, rectangle rect, const int txtoff, color bgcolor, color fgcolor) const
{
txt_.move(rect.x + PG_BORDER_X, PG_BORDER_Y);
txt_.size(nana::size(rect.width - 2* PG_BORDER_X, size_ - 2* PG_BORDER_Y));
}
/// class pg_string end
/// class pg_string_int
void pg_string_int::value(int value)
{
pg_string::value(std::to_string(value));
}
int pg_string_int::to_int() const
{
int int_val = -1;
try
{
int_val = std::stoi(pgitem::value());
}
catch(...)
{
}
return int_val;
}
void pg_string_int::range(int min, int max)
{
if(min >= max)
{
range_ = false;
return;
}
range_ = true;
min_ = min;
max_ = max;
value(std::clamp(to_int(), min_, max_));
}
void pg_string_int::create(window wd)
{
pg_string::create(wd);
txt_.events().key_press.connect_front([this](const nana::arg_keyboard& arg)
{
if(arg.key == nana::keyboard::enter)
{
int int_val = -1;
try
{
int_val = std::stoi(txt_.caption());
if(range_)
int_val = std::clamp(int_val, min_, max_);
}
catch(...)
{
txt_.caption(value_);
arg.stop_propagation();
return;
}
pg_string::value(std::to_string(int_val));
emit_event();
arg.stop_propagation();
}
});
txt_.events().focus([this](const nana::arg_focus& arg)
{
if( !arg.getting )
{
// just lost focus, so capture the value left by the user, if changed
if( txt_.caption() != pgitem::value() )
{
pgitem::value( txt_.caption() );
emit_event();
}
}
});
txt_.set_accept([](wchar_t c) -> bool
{
return (isdigit(c) || c == '-' || c == nana::keyboard::cancel || c == nana::keyboard::backspace) ? true : false;
});
}
/// class pg_string_int end
/// class pg_string_uint
void pg_string_uint::value(unsigned value)
{
pg_string::value(std::to_string(value));
}
unsigned pg_string_uint::to_uint() const
{
unsigned u_val = -1;
try
{
u_val = std::stoul(pgitem::value());
}
catch(...)
{
}
return u_val;
}
void pg_string_uint::range(unsigned min, unsigned max)
{
if(min >= max)
{
range_ = false;
return;
}
range_ = true;
min_ = min;
max_ = max;
value(std::clamp(to_uint(), min_, max_));
}
void pg_string_uint::create(window wd)
{
pg_string::create(wd);
txt_.events().key_press.connect_front([this](const nana::arg_keyboard& arg)
{
if(arg.key == nana::keyboard::enter)
{
unsigned u_val = -1;
try
{
u_val = std::stoul(txt_.caption());
if(range_)
u_val = std::clamp(u_val, min_, max_);
}
catch(...)
{
txt_.caption(value_);
arg.stop_propagation();
return;
}
pg_string::value(std::to_string(u_val));
emit_event();
arg.stop_propagation();
}
});
txt_.set_accept([](wchar_t c) -> bool
{
return (isdigit(c) || c == nana::keyboard::cancel || c == nana::keyboard::backspace) ? true : false;
});
}
/// class pg_string_uint end
/// class pg_string_button
#define STRING_BUTTON_SIZE 30
void pg_string_button::value(const std::string& value)
{
value_ = value;
internal_lock_guard evt_lock(&evt_emit_, false);
txt_.caption(value);
}
void pg_string_button::enabled(bool state)
{
pgitem::enabled(state);
txt_.enabled(en_);
btn_.enabled(en_);
}
void pg_string_button::set_accept(std::function<bool(wchar_t)> f)
{
txt_.set_accept(f);
}
void pg_string_button::set_button_click(std::function<void(const nana::arg_click&)> f)
{
btn_.events().click.connect_front(f);
}
void pg_string_button::editable(bool editable)
{
txt_.editable(editable);
}
bool pg_string_button::editable() const
{
return txt_.editable();
}
void pg_string_button::tooltip( const std::string& help_text )
{
txt_.tooltip( help_text );
}
void pg_string_button::create(window wd)
{
txt_.create(wd);
txt_.multi_lines(false);
txt_.caption(value_);
txt_.set_accept([this](wchar_t c) -> bool
{
return (((c == nana::keyboard::cancel || c == nana::keyboard::backspace) && txt_.selected()) || c == nana::keyboard::enter) ? true : false;
});
txt_.events().click.connect_front([this](const nana::arg_click& arg)
{
txt_.select(true);
});
txt_.events().dbl_click([this](const nana::arg_mouse& arg)
{
txt_.select(true);
});
txt_.events().key_press([this](const nana::arg_keyboard& arg)
{
if(arg.key == nana::keyboard::enter && txt_.editable())
{
pgitem::value(txt_.caption());
emit_event();
}
});
//button
btn_.create(wd);
btn_.caption(btn_label_);
}
void pg_string_button::draw_value(paint::graphics* graph, rectangle rect, const int txtoff, color bgcolor, color fgcolor) const
{
txt_.move(rect.x + PG_BORDER_X, PG_BORDER_Y);
txt_.size(nana::size(rect.width - 2 * PG_BORDER_X - STRING_BUTTON_SIZE, size_ - 2 * PG_BORDER_Y));
btn_.move(rect.x + PG_BORDER_X + txt_.size().width, PG_BORDER_Y);
btn_.size(nana::size(STRING_BUTTON_SIZE, size_ - 2 * PG_BORDER_Y));
}
/// class pg_string_button end
/// class pg_choice
void pg_choice::value(const std::string& value_)
{
try
{
option(std::stoul(pgitem::value()));
}
catch(...)
{
}
}
void pg_choice::enabled(bool state)
{
pgitem::enabled(state);
cmb_.enabled(en_);
}
void pg_choice::option(std::size_t value)
{
internal_lock_guard evt_lock(&evt_emit_, false);
if(value < cmb_.the_number_of_options())
{
cmb_.option(value);
pgitem::value(std::to_string(value));
}
}
std::size_t pg_choice::option() const
{
std::size_t val = nana::npos;
try
{
val = std::stoul(pgitem::value());
}
catch(...)
{
}
return val;
}
void pg_choice::push_back(const std::string& option)
{
cmb_.push_back(option);
}
void pg_choice::tooltip( const std::string& help_text )
{
cmb_.tooltip( help_text );
}
void pg_choice::set( const std::vector< std::string >& vs )
{
for( auto& s : vs )
{
cmb_.push_back( s );
}
}
void pg_choice::create(window wd)
{
cmb_.create(wd);
cmb_.events().click.connect_front([this](const nana::arg_click& arg)
{
scroll();
});
cmb_.events().selected([this](const nana::arg_combox& arg)
{
if(!evt_emit_)
return;
option(arg.widget.option());
emit_event();
});
}
void pg_choice::draw_value(paint::graphics* graph, rectangle rect, const int txtoff, color bgcolor, color fgcolor) const
{
cmb_.move(rect.x + PG_BORDER_X, PG_BORDER_Y);
cmb_.size(nana::size(rect.width - 2 * PG_BORDER_X, size_ - 2 * PG_BORDER_Y));
}
/// class pg_choice end
/// class pg_check
void pg_check::value(const std::string& value)
{
internal_lock_guard evt_lock(&evt_emit_, false);
if(value == "true" || value == "T" || value == "t" || value == "1")
{
chk_.check(true);
pgitem::value("true");
}
else if(value == "false" || value == "F" || value == "f" || value == "0")
{
chk_.check(false);
pgitem::value("false");
}
}
void pg_check::enabled(bool state)
{
pgitem::enabled(state);
chk_.enabled(en_);
}
void pg_check::tooltip( const std::string& help_text )
{
chk_.tooltip( help_text );
}
void pg_check::create(window wd)
{
chk_.create(wd);
value(value_); // set the initial value passed in ctr
chk_.events().click.connect_front([this](const nana::arg_click& arg)
{
scroll();
});
chk_.events().checked([this](const nana::arg_checkbox& arg)
{
if(!evt_emit_)
return;
check(arg.widget->checked());
emit_event();
});
}
void pg_check::draw_value(paint::graphics* graph, rectangle rect, const int txtoff, color bgcolor, color fgcolor) const
{
chk_.bgcolor(bgcolor);
chk_.move(rect.x + PG_BORDER_X, PG_BORDER_Y);
auto chksize = size_ - 2 * PG_BORDER_Y;
chk_.size(nana::size(chksize, chksize));
}
/// class pg_check end
/// class pg_color
void pg_color::value(const std::string& value)
{
internal_lock_guard evt_lock(&evt_emit_, false);
color_ = nana::to_color(value);
rgb_[0].caption(std::to_string(static_cast<int>(color_.r())));
rgb_[1].caption(std::to_string(static_cast<int>(color_.g())));
rgb_[2].caption(std::to_string(static_cast<int>(color_.b())));
if(show_inherited_)
{
inherited_ = is_color_inherited(value);
menu_.checked(2, inherited_);
for(auto & i : rgb_)
i.enabled(en_ && !inherited_);
}
pgitem::value(nana::to_string(color_, show_inherited_ ? inherited_ : false));
}
void pg_color::enabled(bool state)
{
pgitem::enabled(state);
for(auto & i : rgb_)
i.enabled(en_ && !inherited_);
}
void pg_color::value(::nana::color value)
{
pg_color::value(nana::to_string(value, show_inherited_ ? inherited_ : false));
}
void pg_color::inherited(bool value)
{
if(show_inherited_)
pg_color::value(nana::to_string(color_, value));
}
unsigned pg_color::size() const
{
return expand_ ? 2 * size_ : size_;
}
void pg_color::create(window wd)
{
// ibox context menu
menu_.append_splitter();
// 2. Inherited
menu_.append("Inherited", [this](const nana::menu::item_proxy& ip)
{
pg_color::value(nana::to_string(color_, ip.checked()));
printf(ip.checked() ? "[X]\n" : "[ ]\n");
emit_event();
});
menu_.enabled(2, show_inherited_);
menu_.check_style(2, nana::menu::checks::highlight);
// colorbox
colorbox_.create(wd);
colorbox_.editable(false);
colorbox_.events().mouse_down.connect_front([this](const nana::arg_mouse& arg)
{
if(!en_) // only if enabled
return;
if(arg.left_button)
{
expand_ = !expand_;
scroll();
update();
}
});
// textboxes
for(auto & i : rgb_)
{
i.create(wd);
i.multi_lines(false);
i.events().click.connect_front([this](const nana::arg_click& arg)
{
scroll();
});
i.events().dbl_click([this, &i](const nana::arg_mouse& arg)
{
i.select(true);
});
i.events().key_press([this](const nana::arg_keyboard& arg)
{
if(arg.key == nana::keyboard::enter)
{
pg_color::value(nana::to_string(nana::to_color(rgb_[0].caption(), rgb_[1].caption(), rgb_[2].caption()), show_inherited_ ? inherited_ : false));
emit_event();
}
});
i.set_accept([](wchar_t c) -> bool
{
return (isdigit(c) || c == nana::keyboard::cancel || c == nana::keyboard::backspace) ? true : false;
});
}
pg_color::value(value_);
}
void pg_color::draw(paint::graphics* graph, rectangle area, unsigned labelw, unsigned valuew, unsigned iboxw, const int txtoff, color bgcolor, color fgcolor) const
{
// background
graph->rectangle(area, true, bgcolor);
// draw label
if(labelw)
{
auto rect = area;
rect.width = labelw;
draw_label(graph, rect, txtoff, bgcolor, fgcolor);
}
// draw value - colorbox
if(valuew)
{
auto rect = area;
rect.x += labelw;
rect.width = valuew;
colorbox_.move(rect.x + PG_BORDER_X, PG_BORDER_Y);
colorbox_.size(nana::size(rect.width - 2 * PG_BORDER_X, size_ - 2 * PG_BORDER_Y));
colorbox_.bgcolor(color_);
}
// draw interaction-box
if(iboxw)
{
auto rect = area;
rect.x += labelw + valuew;
rect.width = iboxw;
draw_ibox(graph, rect, bgcolor, fgcolor);
}
// draw expanded
if(!expand_)
{
for(auto & i : rgb_)
API::show_window(i, false);
return;
}
if(!en_)
fgcolor = colors::gray; //TODO: scheme
for(auto & i : rgb_)
API::show_window(i, true);
// colorbox R G B
int availw = labelw + valuew;
if(availw)
{
auto txtsize = graph->text_extent_size("R");
int x = 75;
int y = size_ + static_cast<int>(size_ - txtsize.height) / 2;
int ctrly = size_;
const int offset = 8;
availw -= x;
const unsigned txtw = txtsize.width + 5;
const unsigned ctrlw = (availw - (3 * txtw) - (2 * offset)) / 3;
// R
graph->string(point{ area.x + x, area.y + y }, "R", fgcolor);
x += txtw;
rgb_[0].move(x, ctrly);
rgb_[0].size(nana::size(ctrlw, size_ - 2 * PG_BORDER_Y));
x += ctrlw + offset;
// G
graph->string(point{ area.x + x, area.y + y }, "G", fgcolor);
x += txtw;
rgb_[1].move(x, ctrly);
rgb_[1].size(nana::size(ctrlw, size_ - 2 * PG_BORDER_Y));
x += ctrlw + offset;
// B
graph->string(point{ area.x + x, area.y + y }, "B", fgcolor);
x += txtw;
rgb_[2].move(x, ctrly);
rgb_[2].size(nana::size(ctrlw, size_ - 2 * PG_BORDER_Y));
}
}
/// class pg_color end
}//end namespace nana