forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.cpp
1309 lines (1069 loc) · 33.2 KB
/
menu.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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Aseprite UI Library
// Copyright (C) 2001-2018 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "ui/menu.h"
#include "gfx/size.h"
#include "she/font.h"
#include "ui/intern.h"
#include "ui/ui.h"
#include <cctype>
static const int kTimeoutToOpenSubmenu = 250;
namespace ui {
using namespace gfx;
//////////////////////////////////////////////////////////////////////
// Internal messages: to move between menus
RegisterMessage kOpenMenuItemMessage;
RegisterMessage kCloseMenuItemMessage;
RegisterMessage kClosePopupMessage;
RegisterMessage kExecuteMenuItemMessage;
class OpenMenuItemMessage : public Message {
public:
OpenMenuItemMessage(bool select_first) :
Message(kOpenMenuItemMessage),
m_select_first(select_first) {
}
// If this value is true, it means that after opening the menu, we
// have to select the first item (i.e. highlighting it).
bool select_first() const { return m_select_first; }
private:
bool m_select_first;
};
class CloseMenuItemMessage : public Message {
public:
CloseMenuItemMessage(bool last_of_close_chain) :
Message(kCloseMenuItemMessage),
m_last_of_close_chain(last_of_close_chain) {
}
// This fields is used to indicate the end of a sequence of
// kOpenMenuItemMessage and kCloseMenuItemMessage messages. If it is true
// the message is the last one of the chain, which means that no
// more kOpenMenuItemMessage or kCloseMenuItemMessage messages are in the queue.
bool last_of_close_chain() const { return m_last_of_close_chain; }
private:
bool m_last_of_close_chain;
};
// Data for the main jmenubar or the first popuped-jmenubox
struct MenuBaseData {
// True when the menu-items must be opened with the cursor movement
bool was_clicked;
// True when there's kOpen/CloseMenuItemMessage messages in queue, to
// avoid start processing another menuitem-request when we're
// already working in one
bool is_processing;
// True when the kMouseDownMessage is being filtered
bool is_filtering;
bool close_all;
MenuBaseData() {
was_clicked = false;
is_filtering = false;
is_processing = false;
close_all = false;
}
};
class CustomizedWindowForMenuBox : public Window
{
public:
CustomizedWindowForMenuBox(MenuBox* menubox)
: Window(WithoutTitleBar, "")
{
setMoveable(false); // Can't move the window
addChild(menubox);
remapWindow();
}
protected:
bool onProcessMessage(Message* msg) override
{
switch (msg->type()) {
case kCloseMessage:
// Delete this window automatically
deferDelete();
break;
}
return Window::onProcessMessage(msg);
}
};
static MenuBox* get_base_menubox(Widget* widget);
static MenuBaseData* get_base(Widget* widget);
static MenuItem* check_for_letter(Menu* menu, const KeyMessage* keymsg);
static MenuItem* find_nextitem(Menu* menu, MenuItem* menuitem);
static MenuItem* find_previtem(Menu* menu, MenuItem* menuitem);
//////////////////////////////////////////////////////////////////////
// Menu
Menu::Menu()
: Widget(kMenuWidget)
, m_menuitem(NULL)
{
initTheme();
}
Menu::~Menu()
{
if (m_menuitem) {
if (m_menuitem->getSubmenu() == this) {
m_menuitem->setSubmenu(NULL);
}
else {
ASSERT(m_menuitem->getSubmenu() == NULL);
}
}
}
//////////////////////////////////////////////////////////////////////
// MenuBox
MenuBox::MenuBox(WidgetType type)
: Widget(type)
, m_base(NULL)
{
this->setFocusStop(true);
initTheme();
}
MenuBox::~MenuBox()
{
stopFilteringMouseDown();
delete m_base;
}
//////////////////////////////////////////////////////////////////////
// MenuBar
bool MenuBar::m_expandOnMouseover = false;
MenuBar::MenuBar()
: MenuBox(kMenuBarWidget)
{
createBase();
}
// static
bool MenuBar::expandOnMouseover()
{
return m_expandOnMouseover;
}
// static
void MenuBar::setExpandOnMouseover(bool state)
{
m_expandOnMouseover = state;
}
//////////////////////////////////////////////////////////////////////
// MenuItem
MenuItem::MenuItem(const std::string& text)
: Widget(kMenuItemWidget)
{
m_highlighted = false;
m_submenu = NULL;
m_submenu_menubox = NULL;
setText(text);
initTheme();
}
MenuItem::~MenuItem()
{
delete m_submenu;
}
Menu* MenuBox::getMenu()
{
if (children().empty())
return nullptr;
else
return static_cast<Menu*>(children().front());
}
MenuBaseData* MenuBox::createBase()
{
delete m_base;
m_base = new MenuBaseData;
return m_base;
}
Menu* MenuItem::getSubmenu()
{
return m_submenu;
}
void MenuBox::setMenu(Menu* menu)
{
if (Menu* oldMenu = getMenu())
removeChild(oldMenu);
if (menu) {
ASSERT_VALID_WIDGET(menu);
addChild(menu);
}
}
void MenuItem::setSubmenu(Menu* menu)
{
if (m_submenu)
m_submenu->setOwnerMenuItem(NULL);
m_submenu = menu;
if (m_submenu) {
ASSERT_VALID_WIDGET(m_submenu);
m_submenu->setOwnerMenuItem(this);
}
}
bool MenuItem::isHighlighted() const
{
return m_highlighted;
}
void MenuItem::setHighlighted(bool state)
{
m_highlighted = state;
}
bool MenuItem::hasSubmenu() const
{
return (m_submenu && !m_submenu->children().empty());
}
void Menu::showPopup(const gfx::Point& pos)
{
// Generally, when we call showPopup() the menu shouldn't contain a
// parent menu-box, because we're filtering kMouseDownMessage to
// close the popup automatically when we click outside the menubox.
// Anyway there is one specific case were a clicked widget might
// call showPopup() when it's clicked the first time, and a second
// click could generate a kDoubleClickMessage which is then
// converted to kMouseDownMessage to finally call showPopup() again.
// In this case, the menu is already in a menubox.
if (parent()) {
static_cast<MenuBox*>(parent())->cancelMenuLoop();
return;
}
// New window and new menu-box
base::UniquePtr<Window> window(new Window(Window::WithoutTitleBar));
MenuBox* menubox = new MenuBox();
MenuBaseData* base = menubox->createBase();
base->was_clicked = true;
window->setMoveable(false); // Can't move the window
// Set children
menubox->setMenu(this);
menubox->startFilteringMouseDown();
window->addChild(menubox);
window->remapWindow();
// Menubox position
window->positionWindow(
MID(0, pos.x, ui::display_w() - window->bounds().w),
MID(0, pos.y, ui::display_h() - window->bounds().h));
// Set the focus to the new menubox
Manager* manager = Manager::getDefault();
manager->setFocus(menubox);
menubox->setFocusMagnet(true);
// Open the window
window->openWindowInForeground();
// Free the keyboard focus if it's in the menu popup, in other case
// it means that the user set the focus to other specific widget
// before we closed the popup.
Widget* focus = manager->getFocus();
if (focus && focus->window() == window)
focus->releaseFocus();
// Fetch the "menu" so it isn't destroyed
menubox->setMenu(nullptr);
menubox->stopFilteringMouseDown();
}
void Menu::onPaint(PaintEvent& ev)
{
theme()->paintMenu(ev);
}
void Menu::onResize(ResizeEvent& ev)
{
setBoundsQuietly(ev.bounds());
Rect cpos = childrenBounds();
bool isBar = (parent()->type() == kMenuBarWidget);
for (auto child : children()) {
Size reqSize = child->sizeHint();
if (isBar)
cpos.w = reqSize.w;
else
cpos.h = reqSize.h;
child->setBounds(cpos);
if (isBar)
cpos.x += cpos.w;
else
cpos.y += cpos.h;
}
}
void Menu::onSizeHint(SizeHintEvent& ev)
{
Size size(0, 0);
Size reqSize;
UI_FOREACH_WIDGET_WITH_END(children(), it, end) {
reqSize = (*it)->sizeHint();
if (parent() &&
parent()->type() == kMenuBarWidget) {
size.w += reqSize.w + ((it+1 != end) ? childSpacing(): 0);
size.h = MAX(size.h, reqSize.h);
}
else {
size.w = MAX(size.w, reqSize.w);
size.h += reqSize.h + ((it+1 != end) ? childSpacing(): 0);
}
}
size.w += border().width();
size.h += border().height();
ev.setSizeHint(size);
}
bool MenuBox::onProcessMessage(Message* msg)
{
Menu* menu = MenuBox::getMenu();
switch (msg->type()) {
case kMouseMoveMessage:
if (!get_base(this)->was_clicked)
break;
// Fall through
case kMouseDownMessage:
case kDoubleClickMessage:
if (menu) {
ASSERT(menu->parent() == this);
if (get_base(this)->is_processing)
break;
gfx::Point mousePos = static_cast<MouseMessage*>(msg)->position();
// Here we catch the filtered messages (menu-bar or the
// popuped menu-box) to detect if the user press outside of
// the widget
if (msg->type() == kMouseDownMessage && m_base != NULL) {
Widget* picked = manager()->pick(mousePos);
// If one of these conditions are accomplished we have to
// close all menus (back to menu-bar or close the popuped
// menubox), this is the place where we control if...
if (picked == NULL || // If the button was clicked nowhere
picked == this || // If the button was clicked in this menubox
// The picked widget isn't from the same tree of menus
(get_base_menubox(picked) != this ||
(this->type() == kMenuBarWidget &&
picked->type() == kMenuWidget))) {
// The user click outside all the menu-box/menu-items, close all
menu->closeAll();
return true;
}
}
// Get the widget below the mouse cursor
Widget* picked = menu->pick(mousePos);
if (picked) {
if ((picked->type() == kMenuItemWidget) &&
!(picked->hasFlags(DISABLED))) {
MenuItem* pickedItem = static_cast<MenuItem*>(picked);
// If the picked menu-item is not highlighted...
if (!pickedItem->isHighlighted()) {
// In menu-bar always open the submenu, in other popup-menus
// open the submenu only if the user does click
bool open_submenu =
(this->type() == kMenuBarWidget) ||
(msg->type() == kMouseDownMessage);
menu->highlightItem(pickedItem, false, open_submenu, false);
}
// If the user pressed in a highlighted menu-item (maybe
// the user was waiting for the timer to open the
// submenu...)
else if (msg->type() == kMouseDownMessage &&
pickedItem->hasSubmenu()) {
pickedItem->stopTimer();
// If the submenu is closed, open it
if (!pickedItem->hasSubmenuOpened())
pickedItem->openSubmenu(false);
else if (pickedItem->inBar()) {
pickedItem->getSubmenu()->closeAll();
// Set this flag to false so the submenu is not open
// again on kMouseMoveMessage.
get_base(this)->was_clicked = false;
}
}
}
else if (!get_base(this)->was_clicked) {
menu->unhighlightItem();
}
}
}
break;
case kMouseLeaveMessage:
if (menu) {
if (get_base(this)->is_processing)
break;
MenuItem* highlight = menu->getHighlightedItem();
if (highlight && !highlight->hasSubmenuOpened())
menu->unhighlightItem();
}
break;
case kMouseUpMessage:
if (menu) {
if (get_base(this)->is_processing)
break;
// The item is highlighted and not opened (and the timer to open the submenu is stopped)
MenuItem* highlight = menu->getHighlightedItem();
if (highlight &&
!highlight->hasSubmenuOpened() &&
highlight->m_submenu_timer == NULL) {
menu->closeAll();
highlight->executeClick();
}
}
break;
case kKeyDownMessage:
if (menu) {
MenuItem* selected;
if (get_base(this)->is_processing)
break;
get_base(this)->was_clicked = false;
// Check for ALT+some underlined letter
if (((this->type() == kMenuBoxWidget) && (msg->modifiers() == kKeyNoneModifier || // <-- Inside menu-boxes we can use letters without Alt modifier pressed
msg->modifiers() == kKeyAltModifier)) ||
((this->type() == kMenuBarWidget) && (msg->modifiers() == kKeyAltModifier))) {
auto keymsg = static_cast<KeyMessage*>(msg);
selected = check_for_letter(menu, keymsg);
if (selected) {
menu->highlightItem(selected, true, true, true);
return true;
}
}
// Highlight movement with keyboard
if (this->hasFocus()) {
MenuItem* highlight = menu->getHighlightedItem();
MenuItem* child_with_submenu_opened = NULL;
bool used = false;
// Search a child with highlight or the submenu opened
for (auto child : menu->children()) {
if (child->type() != kMenuItemWidget)
continue;
if (static_cast<MenuItem*>(child)->hasSubmenuOpened())
child_with_submenu_opened = static_cast<MenuItem*>(child);
}
if (!highlight && child_with_submenu_opened)
highlight = child_with_submenu_opened;
switch (static_cast<KeyMessage*>(msg)->scancode()) {
case kKeyEsc:
// In menu-bar
if (this->type() == kMenuBarWidget) {
if (highlight) {
cancelMenuLoop();
used = true;
}
}
// In menu-boxes
else {
if (child_with_submenu_opened) {
child_with_submenu_opened->closeSubmenu(true);
used = true;
}
// Go to parent
else if (menu->m_menuitem) {
// Just retrogress one parent-level
menu->m_menuitem->closeSubmenu(true);
used = true;
}
}
break;
case kKeyUp:
// In menu-bar
if (this->type() == kMenuBarWidget) {
if (child_with_submenu_opened)
child_with_submenu_opened->closeSubmenu(true);
}
// In menu-boxes
else {
// Go to previous
highlight = find_previtem(menu, highlight);
menu->highlightItem(highlight, false, false, false);
}
used = true;
break;
case kKeyDown:
// In menu-bar
if (this->type() == kMenuBarWidget) {
// Select the active menu
menu->highlightItem(highlight, true, true, true);
}
// In menu-boxes
else {
// Go to next
highlight = find_nextitem(menu, highlight);
menu->highlightItem(highlight, false, false, false);
}
used = true;
break;
case kKeyLeft:
// In menu-bar
if (this->type() == kMenuBarWidget) {
// Go to previous
highlight = find_previtem(menu, highlight);
menu->highlightItem(highlight, false, false, false);
}
// In menu-boxes
else {
// Go to parent
if (menu->m_menuitem) {
Widget* parent = menu->m_menuitem->parent()->parent();
// Go to the previous item in the parent
// If the parent is the menu-bar
if (parent->type() == kMenuBarWidget) {
menu = static_cast<MenuBar*>(parent)->getMenu();
MenuItem* menuitem = find_previtem(menu, menu->getHighlightedItem());
// Go to previous item in the parent
menu->highlightItem(menuitem, false, true, true);
}
// If the parent isn't the menu-bar
else {
// Just retrogress one parent-level
menu->m_menuitem->closeSubmenu(true);
}
}
}
used = true;
break;
case kKeyRight:
// In menu-bar
if (this->type() == kMenuBarWidget) {
// Go to next
highlight = find_nextitem(menu, highlight);
menu->highlightItem(highlight, false, false, false);
}
// In menu-boxes
else {
// Enter in sub-menu
if (highlight && highlight->hasSubmenu()) {
menu->highlightItem(highlight, true, true, true);
}
// Go to parent
else if (menu->m_menuitem) {
// Get the root menu
MenuBox* root = get_base_menubox(this);
menu = root->getMenu();
// Go to the next item in the root
MenuItem* menuitem = find_nextitem(menu, menu->getHighlightedItem());
// Open the sub-menu
menu->highlightItem(menuitem, false, true, true);
}
}
used = true;
break;
case kKeyEnter:
case kKeyEnterPad:
if (highlight)
menu->highlightItem(highlight, true, true, true);
used = true;
break;
}
// Return true if we've already consumed the key.
if (used) {
return true;
}
// If the user presses the ALT key we close everything.
else if (static_cast<KeyMessage*>(msg)->scancode() == kKeyAlt) {
cancelMenuLoop();
}
}
}
break;
default:
if (msg->type() == kClosePopupMessage) {
window()->closeWindow(nullptr);
}
break;
}
return Widget::onProcessMessage(msg);
}
void MenuBox::onResize(ResizeEvent& ev)
{
setBoundsQuietly(ev.bounds());
if (Menu* menu = getMenu())
menu->setBounds(childrenBounds());
}
void MenuBox::onSizeHint(SizeHintEvent& ev)
{
Size size(0, 0);
if (Menu* menu = getMenu())
size = menu->sizeHint();
size.w += border().width();
size.h += border().height();
ev.setSizeHint(size);
}
bool MenuItem::onProcessMessage(Message* msg)
{
switch (msg->type()) {
case kMouseEnterMessage:
// TODO theme specific!!
invalidate();
// When a menu item receives the mouse, start a timer to open the submenu...
if (isEnabled() && hasSubmenu()) {
// Start the timer to open the submenu...
if (!inBar() || MenuBar::expandOnMouseover())
startTimer();
}
break;
case kMouseLeaveMessage:
// Unhighlight this item if its submenu isn't opened
if (isHighlighted() &&
!m_submenu_menubox &&
parent() &&
parent()->type() == kMenuWidget) {
static_cast<Menu*>(parent())->unhighlightItem();
}
// TODO theme specific!!
invalidate();
// Stop timer to open the popup
if (m_submenu_timer)
m_submenu_timer.reset();
break;
default:
if (msg->type() == kOpenMessage) {
validateItem();
}
else if (msg->type() == kOpenMenuItemMessage) {
validateItem();
MenuBaseData* base = get_base(this);
bool select_first = static_cast<OpenMenuItemMessage*>(msg)->select_first();
ASSERT(base != NULL);
ASSERT(base->is_processing);
ASSERT(hasSubmenu());
Rect old_pos = window()->bounds();
old_pos.w -= 1*guiscale();
MenuBox* menubox = new MenuBox();
m_submenu_menubox = menubox;
menubox->setMenu(m_submenu);
// New window and new menu-box
Window* window = new CustomizedWindowForMenuBox(menubox);
// Menubox position
Rect pos = window->bounds();
if (inBar()) {
pos.x = MID(0, bounds().x, ui::display_w()-pos.w);
pos.y = MAX(0, bounds().y2());
}
else {
int x_left = old_pos.x - pos.w;
int x_right = old_pos.x2();
int x, y = bounds().y-3*guiscale();
Rect r1(0, 0, pos.w, pos.h), r2(0, 0, pos.w, pos.h);
r1.x = x_left = MID(0, x_left, ui::display_w()-pos.w);
r2.x = x_right = MID(0, x_right, ui::display_w()-pos.w);
r1.y = r2.y = y = MID(0, y, ui::display_h()-pos.h);
// Calculate both intersections
gfx::Rect s1 = r1.createIntersection(old_pos);
gfx::Rect s2 = r2.createIntersection(old_pos);
if (s2.isEmpty())
x = x_right; // Use the right because there aren't intersection with it
else if (s1.isEmpty())
x = x_left; // Use the left because there are not intersection
else if (r2.w*r2.h <= r1.w*r1.h)
x = x_right; // Use the right because there are less intersection area
else
x = x_left; // Use the left because there are less intersection area
pos.x = x;
pos.y = y;
}
window->positionWindow(pos.x, pos.y);
// Set the focus to the new menubox
menubox->setFocusMagnet(true);
// Setup the highlight of the new menubox
if (select_first) {
// Select the first child
MenuItem* first_child = NULL;
for (auto child : m_submenu->children()) {
if (child->type() != kMenuItemWidget)
continue;
if (child->isEnabled()) {
first_child = static_cast<MenuItem*>(child);
break;
}
}
if (first_child)
m_submenu->highlightItem(first_child, false, false, false);
else
m_submenu->unhighlightItem();
}
else
m_submenu->unhighlightItem();
// Run in background
window->openWindow();
base->is_processing = false;
return true;
}
else if (msg->type() == kCloseMenuItemMessage) {
bool last_of_close_chain = static_cast<CloseMenuItemMessage*>(msg)->last_of_close_chain();
MenuBaseData* base = get_base(this);
Window* window;
ASSERT(base != NULL);
ASSERT(base->is_processing);
MenuBox* menubox = m_submenu_menubox;
m_submenu_menubox = NULL;
ASSERT(menubox != NULL);
window = (Window*)menubox->parent();
ASSERT(window && window->type() == kWindowWidget);
// Fetch the "menu" to avoid destroy it with 'delete'.
menubox->setMenu(NULL);
// Destroy the window
window->closeWindow(NULL);
// Set the focus to this menu-box of this menu-item
if (base->close_all)
manager()->freeFocus();
else
manager()->setFocus(this->parent()->parent());
// Do not call "delete window" here, because it
// (CustomizedWindowForMenuBox) will be deferDelete()d on
// kCloseMessage.
if (last_of_close_chain) {
base->close_all = false;
base->is_processing = false;
}
// Stop timer to open the popup
stopTimer();
return true;
}
else if (msg->type() == kExecuteMenuItemMessage) {
onClick();
return true;
}
break;
case kTimerMessage:
if (static_cast<TimerMessage*>(msg)->timer() == m_submenu_timer.get()) {
MenuBaseData* base = get_base(this);
ASSERT(hasSubmenu());
// Stop timer to open the popup
stopTimer();
// If the submenu is closed, and we are not processing messages, open it
if (m_submenu_menubox == NULL && !base->is_processing)
openSubmenu(false);
}
break;
}
return Widget::onProcessMessage(msg);
}
void MenuItem::onInitTheme(InitThemeEvent& ev)
{
if (m_submenu)
m_submenu->initTheme();
if (m_submenu_menubox)
m_submenu_menubox->initTheme();
Widget::onInitTheme(ev);
}
void MenuItem::onPaint(PaintEvent& ev)
{
theme()->paintMenuItem(ev);
}
void MenuItem::onClick()
{
// Fire new Click() signal.
Click();
}
void MenuItem::onValidate()
{
// Here the user can customize the automatic validation of the menu
// item before it's shown.
}
void MenuItem::onSizeHint(SizeHintEvent& ev)
{
Size size(0, 0);
if (hasText()) {
size.w =
+ textWidth()
+ (inBar() ? childSpacing()/4: childSpacing())
+ border().width();
size.h =
+ textHeight()
+ border().height();
}
ev.setSizeHint(size);
}
// Climbs the hierarchy of menus to get the most-top menubox.
static MenuBox* get_base_menubox(Widget* widget)
{
while (widget) {
ASSERT_VALID_WIDGET(widget);
// We are in a menubox
if (widget->type() == kMenuBoxWidget ||
widget->type() == kMenuBarWidget) {
if (static_cast<MenuBox*>(widget)->getBase()) {
return static_cast<MenuBox*>(widget);
}
else {
Menu* menu = static_cast<MenuBox*>(widget)->getMenu();
ASSERT(menu != NULL);
ASSERT(menu->getOwnerMenuItem() != NULL);
widget = menu->getOwnerMenuItem();
}
}
else {
widget = widget->parent();
}
}
return nullptr;
}
static MenuBaseData* get_base(Widget* widget)
{
MenuBox* menubox = get_base_menubox(widget);
ASSERT(menubox);
if (menubox)
return menubox->getBase();
else
return nullptr;
}
MenuItem* Menu::getHighlightedItem()
{
for (auto child : children()) {
if (child->type() != kMenuItemWidget)
continue;
MenuItem* menuitem = static_cast<MenuItem*>(child);
if (menuitem->isHighlighted())
return menuitem;
}
return NULL;
}
void Menu::highlightItem(MenuItem* menuitem, bool click, bool open_submenu, bool select_first_child)
{
// Find the menuitem with the highlight
for (auto child : children()) {
if (child->type() != kMenuItemWidget)
continue;
if (child != menuitem) {
// Is it?
if (static_cast<MenuItem*>(child)->isHighlighted()) {
static_cast<MenuItem*>(child)->setHighlighted(false);
child->invalidate();
}
}
}
if (menuitem) {
if (!menuitem->isHighlighted()) {