/
button.cpp
530 lines (444 loc) · 12.3 KB
/
button.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
/*
* A Button Implementation
* Nana C++ Library(http://www.nanapro.org)
* Copyright(C) 2003-2018 Jinhao(cnjinhao@hotmail.com)
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*
* @file: nana/gui/widgets/button.cpp
*/
#include <nana/gui/widgets/button.hpp>
#include <nana/gui/detail/widget_content_measurer_interface.hpp>
#include <nana/paint/text_renderer.hpp>
namespace nana{ namespace drawerbase
{
namespace button
{
class trigger::measurer
: public dev::widget_content_measurer_interface
{
public:
measurer(trigger* t)
: trigger_{ t }
{}
std::optional<size> measure(graph_reference graph, unsigned limit_pixels, bool /*limit_width*/) const override
{
//Button doesn't provide a support of vfit and hfit
if (limit_pixels)
return{};
wchar_t shortkey;
return graph.text_extent_size(API::transform_shortkey_text(trigger_->wdg_->caption(), shortkey, nullptr));
}
size extension() const override
{
return { 14, 10};
}
private:
trigger * trigger_;
};
//trigger
//@brief: draw the button
trigger::trigger()
{
attr_.e_state = element_state::normal;
attr_.omitted = attr_.focused = attr_.pushed = attr_.enable_pushed = attr_.keep_pressed = false;
attr_.focus_color = true;
attr_.icon = nullptr;
measurer_.reset(new measurer{this});
}
trigger::~trigger()
{
delete attr_.icon;
}
void trigger::attached(widget_reference widget, graph_reference graph)
{
graph_ = &graph;
wdg_ = &widget;
window wd = widget;
API::dev::enable_space_click(widget, true);
API::tabstop(wd);
API::effects_edge_nimbus(wd, effects::edge_nimbus::active);
API::effects_edge_nimbus(wd, effects::edge_nimbus::over);
API::dev::set_measurer(widget, measurer_.get());
}
bool trigger::enable_pushed(bool eb)
{
attr_.enable_pushed = eb;
return((eb == false) && pushed(false));
}
bool trigger::pushed(bool pshd)
{
if(pshd != attr_.pushed)
{
attr_.pushed = pshd;
if(false == pshd)
{
if (API::find_window(API::cursor_position()) == wdg_->handle())
attr_.e_state = element_state::hovered;
else
attr_.e_state = element_state::normal;
}
else
attr_.e_state = element_state::pressed;
return true;
}
return false;
}
bool trigger::pushed() const
{
return attr_.pushed;
}
void trigger::omitted(bool om)
{
attr_.omitted = om;
}
bool trigger::focus_color(bool eb)
{
if(eb != attr_.focus_color)
{
attr_.focus_color = eb;
return true;
}
return false;
}
element::cite_bground & trigger::cite()
{
return cite_;
}
void trigger::refresh(graph_reference graph)
{
bool eb = wdg_->enabled();;
attr_.bgcolor = wdg_->bgcolor();
attr_.fgcolor = wdg_->fgcolor();
element_state e_state = attr_.e_state;
if (eb)
{
if (attr_.focused)
{
if (element_state::normal == e_state)
e_state = element_state::focus_normal;
else if (element_state::hovered == e_state)
e_state = element_state::focus_hovered;
}
}
else
e_state = element_state::disabled;
if (false == cite_.draw(graph, attr_.bgcolor, attr_.fgcolor, ::nana::rectangle{ graph.size() }, e_state))
{
if (API::is_transparent_background(*wdg_))
API::dev::copy_transparent_background(*wdg_, graph);
else
_m_draw_background(graph);
_m_draw_border(graph);
}
_m_draw_title(graph, eb);
}
void trigger::mouse_enter(graph_reference graph, const arg_mouse&)
{
attr_.e_state = (attr_.pushed || attr_.keep_pressed ? element_state::pressed : element_state::hovered);
refresh(graph);
API::dev::lazy_refresh();
}
void trigger::mouse_leave(graph_reference graph, const arg_mouse&)
{
if(attr_.enable_pushed && attr_.pushed)
return;
attr_.e_state = element_state::normal;
refresh(graph);
API::dev::lazy_refresh();
}
void trigger::mouse_down(graph_reference graph, const arg_mouse& arg)
{
if (::nana::mouse::left_button != arg.button)
return;
_m_press(graph, true);
}
void trigger::mouse_up(graph_reference graph, const arg_mouse& arg)
{
if (::nana::mouse::left_button != arg.button)
return;
_m_press(graph, false);
}
void trigger::key_press(graph_reference, const arg_keyboard& arg)
{
bool ch_tabstop_next;
switch(arg.key)
{
case keyboard::os_arrow_left: case keyboard::os_arrow_up:
ch_tabstop_next = false;
break;
case keyboard::os_arrow_right: case keyboard::os_arrow_down:
ch_tabstop_next = true;
break;
default:
return;
}
API::move_tabstop(*wdg_, ch_tabstop_next);
}
void trigger::focus(graph_reference graph, const arg_focus& arg)
{
attr_.focused = arg.getting;
refresh(graph);
API::dev::lazy_refresh();
}
void trigger::_m_draw_title(graph_reference graph, bool enabled)
{
wchar_t shortkey;
std::string::size_type shortkey_pos;
std::string mbstr = API::transform_shortkey_text(wdg_->caption(), shortkey, &shortkey_pos);
std::wstring str = to_wstring(mbstr);
nana::size ts = graph.text_extent_size(str);
nana::size gsize = graph.size();
nana::size icon_sz;
if(attr_.icon)
{
icon_sz = attr_.icon->size();
icon_sz.width += 5;
}
nana::point pos{
static_cast<int>(gsize.width - 1 - ts.width) >> 1, static_cast<int>(gsize.height - 1 - ts.height) >> 1
};
if(pos.x < static_cast<int>(icon_sz.width))
pos.x = static_cast<int>(icon_sz.width);
unsigned omitted_pixels = gsize.width - icon_sz.width;
std::size_t txtlen = str.size();
const auto txtptr = str.c_str();
if(ts.width)
{
nana::paint::text_renderer tr(graph);
if(enabled)
{
if (element_state::pressed == attr_.e_state)
{
++pos.x;
++pos.y;
}
auto text_color = (attr_.focus_color && attr_.focused ? ::nana::color(colors::blue) : attr_.fgcolor);
graph.palette(true, text_color);
if (attr_.omitted)
tr.render(pos, txtptr, txtlen, omitted_pixels, paint::text_renderer::mode::truncate_with_ellipsis);
else
#ifdef _nana_std_has_string_view
graph.bidi_string(pos, { txtptr, txtlen });
#else
graph.bidi_string(pos, txtptr, txtlen);
#endif
API::dev::draw_shortkey_underline(graph, mbstr, shortkey, shortkey_pos, pos, text_color);
}
else
{
graph.palette(true, color{ colors::white });
if(attr_.omitted)
{
tr.render(point{ pos.x + 1, pos.y + 1 }, txtptr, txtlen, omitted_pixels, paint::text_renderer::mode::truncate_with_ellipsis);
graph.palette(true, color{ colors::gray });
tr.render(pos, txtptr, txtlen, omitted_pixels, paint::text_renderer::mode::truncate_with_ellipsis);
}
else
{
#ifdef _nana_std_has_string_view
graph.bidi_string(point{ pos.x + 1, pos.y + 1 }, { txtptr, txtlen });
graph.palette(true, color{ colors::gray });
graph.bidi_string(pos, { txtptr, txtlen });
#else
graph.bidi_string(point{ pos.x + 1, pos.y + 1 }, txtptr, txtlen);
graph.palette(true, color{ colors::gray });
graph.bidi_string(pos, txtptr, txtlen);
#endif
}
}
}
if(attr_.icon)
attr_.icon->paste(graph, point{ 3, static_cast<int>(gsize.height - icon_sz.height) / 2 });
}
void trigger::_m_draw_background(graph_reference graph)
{
nana::rectangle r(graph.size());
r.pare_off(1);
auto from = attr_.bgcolor.blend(colors::white, 0.8);
auto to = attr_.bgcolor.blend(colors::black, 0.05);
if (element_state::pressed == attr_.e_state)
{
r.x = r.y = 2;
std::swap(from, to);
}
graph.gradual_rectangle(r, from, to, true);
}
void trigger::_m_draw_border(graph_reference graph)
{
nana::rectangle r(graph.size());
::nana::color lt(static_cast<color_rgb>(0x7f7f7f)), rb(static_cast<color_rgb>(0x707070));
graph.frame_rectangle(r, lt, lt, rb, rb);
graph.palette(false, colors::button_face);
paint::draw draw(graph);
draw.corner(r, 1);
graph.palette(false, static_cast<color_rgb>(0x919191));
draw.corner(r.pare_off(1), 1);
if (element_state::pressed == attr_.e_state)
graph.rectangle(r, false, static_cast<color_rgb>(0xc3c3c3));
}
void trigger::_m_press(graph_reference graph, bool is_pressed)
{
if (is_pressed)
{
if (attr_.e_state == element_state::pressed)
return;
attr_.e_state = element_state::pressed;
attr_.keep_pressed = true;
wdg_->set_capture(true);
}
else
{
wdg_->release_capture();
attr_.keep_pressed = false;
if (attr_.enable_pushed && (false == attr_.pushed))
{
attr_.pushed = true;
return;
}
if (element_state::pressed == attr_.e_state)
attr_.e_state = element_state::hovered;
else
attr_.e_state = element_state::normal;
attr_.pushed = false;
}
refresh(graph);
API::dev::lazy_refresh();
}
void trigger::emit_click()
{
arg_click arg;
arg.window_handle = wdg_->handle();
arg.mouse_args = nullptr;
API::emit_event(event_code::click, arg.window_handle, arg);
}
void trigger::icon(const nana::paint::image& img)
{
if(img.empty())
{
delete attr_.icon;
attr_.icon = nullptr;
return;
}
if(nullptr == attr_.icon)
attr_.icon = new paint::image;
*attr_.icon = img;
}
//end class trigger
}//end namespace button
}//end namespace drawerbase
//button
//@brief: Define a button widget and it provides the interfaces to be operational
button::button(){}
button::button(window wd, bool visible)
{
create(wd, rectangle(), visible);
}
button::button(window wd, const std::string& text, bool visible)
{
create(wd, rectangle(), visible);
caption(text);
}
button::button(window wd, const char* text, bool visible)
{
create(wd, rectangle(), visible);
caption(text);
}
button::button(window wd, const rectangle& r, bool visible)
{
create(wd, r, visible);
}
button& button::icon(const nana::paint::image& img)
{
internal_scope_guard isg;
get_drawer_trigger().icon(img);
API::refresh_window(handle());
return *this;
}
button& button::enable_pushed(bool eb)
{
internal_scope_guard isg;
if(get_drawer_trigger().enable_pushed(eb))
API::refresh_window(handle());
return *this;
}
bool button::pushed() const
{
return get_drawer_trigger().pushed();
}
button& button::pushed(bool psd)
{
internal_scope_guard isg;
if(get_drawer_trigger().pushed(psd))
API::refresh_window(handle());
return *this;
}
button& button::omitted(bool om)
{
internal_scope_guard isg;
get_drawer_trigger().omitted(om);
API::refresh_window(handle());
return *this;
}
button& button::enable_focus_color(bool eb)
{
internal_scope_guard lock;
if(get_drawer_trigger().focus_color(eb))
API::refresh_window(handle());
return *this;
}
button& button::set_bground(const pat::cloneable<element::element_interface>& rv)
{
internal_scope_guard lock;
get_drawer_trigger().cite().set(rv);
return *this;
}
button& button::set_bground(const std::string& name)
{
internal_scope_guard lock;
get_drawer_trigger().cite().set(name.data());
return *this;
}
button& button::transparent(bool enabled)
{
if (enabled)
API::effects_bground(*this, effects::bground_transparent(0), 0.0);
else
API::effects_bground_remove(*this);
return *this;
}
bool button::transparent() const
{
return API::is_transparent_background(*this);
}
button& button::edge_effects(bool enable)
{
if (enable)
{
API::effects_edge_nimbus(*this, effects::edge_nimbus::active);
API::effects_edge_nimbus(*this, effects::edge_nimbus::over);
}
else
API::effects_edge_nimbus(*this, effects::edge_nimbus::none);
return *this;
}
void button::_m_complete_creation()
{
events().shortkey.connect_unignorable([this](const arg_keyboard&)
{
get_drawer_trigger().emit_click();
});
}
void button::_m_caption(native_string_type&& text)
{
API::unregister_shortkey(handle());
wchar_t shortkey;
API::transform_shortkey_text(to_utf8(text), shortkey, nullptr);
if (shortkey)
API::register_shortkey(handle(), shortkey);
base_type::_m_caption(std::move(text));
}
//end class button
}//end namespace nana