forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.cpp
1578 lines (1301 loc) · 41 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) 2018-2023 Igara Studio S.A.
// 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 "base/scoped_value.h"
#include "gfx/size.h"
#include "os/font.h"
#include "ui/display.h"
#include "ui/intern.h"
#include "ui/ui.h"
#include <algorithm>
#include <cctype>
#include <memory>
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;
}
};
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);
static void choose_side(gfx::Rect& bounds,
const gfx::Rect& workarea,
const gfx::Rect& parentBounds)
{
int scale = guiscale();
if (get_multiple_displays())
scale = Manager::getDefault()->display()->scale();
int x_left = parentBounds.x - bounds.w + 1*scale;
int x_right = parentBounds.x2() - 1*scale;
int x, y = bounds.y;
Rect r1(0, 0, bounds.w, bounds.h);
Rect r2(0, 0, bounds.w, bounds.h);
r1.x = x_left = std::clamp(x_left, workarea.x, std::max(workarea.x, workarea.x2()-bounds.w));
r2.x = x_right = std::clamp(x_right, workarea.x, std::max(workarea.x, workarea.x2()-bounds.w));
r1.y = r2.y = y = std::clamp(y, workarea.y, std::max(workarea.y, workarea.y2()-bounds.h));
// Calculate both intersections
const gfx::Rect s1 = r1.createIntersection(parentBounds);
const gfx::Rect s2 = r2.createIntersection(parentBounds);
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 (s2.w*s2.h <= s1.w*s1.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
bounds.x = x;
bounds.y = y;
}
static void add_scrollbars_if_needed(MenuBoxWindow* window,
const gfx::Rect& workarea,
gfx::Rect& bounds)
{
gfx::Rect rc = bounds;
if (rc.x < workarea.x) {
rc.w -= (workarea.x - rc.x);
rc.x = workarea.x;
}
if (rc.x2() > workarea.x2()) {
rc.w = workarea.x2() - rc.x;
}
bool vscrollbarsAdded = false;
if (rc.y < workarea.y) {
rc.h -= (workarea.y - rc.y);
rc.y = workarea.y;
vscrollbarsAdded = true;
}
if (rc.y2() > workarea.y2()) {
rc.h = workarea.y2() - rc.y;
vscrollbarsAdded = true;
}
if (rc == bounds)
return;
MenuBox* menubox = window->menubox();
View* view = new View;
view->InitTheme.connect([view]{ view->noBorderNoChildSpacing(); });
view->initTheme();
if (vscrollbarsAdded) {
int barWidth = view->verticalBar()->getBarWidth();;
if (get_multiple_displays())
barWidth *= window->display()->scale();
rc.w += 2*barWidth;
if (rc.x2() > workarea.x2()) {
rc.x = workarea.x2() - rc.w;
if (rc.x < workarea.x) {
rc.x = workarea.x;
rc.w = workarea.w;
}
}
}
// New bounds
bounds = rc;
window->removeChild(menubox);
view->attachToView(menubox);
window->addChild(view);
}
//////////////////////////////////////////////////////////////////////
// Menu
Menu::Menu()
: Widget(kMenuWidget)
, m_menuitem(nullptr)
{
enableFlags(IGNORE_MOUSE);
initTheme();
}
Menu::~Menu()
{
if (m_menuitem) {
if (m_menuitem->getSubmenu() == this) {
m_menuitem->setSubmenu(nullptr);
}
else {
ASSERT(m_menuitem->getSubmenu() == nullptr);
}
}
}
void Menu::onOpenPopup()
{
OpenPopup();
}
//////////////////////////////////////////////////////////////////////
// MenuBox
MenuBox::MenuBox(WidgetType type)
: Widget(type)
, m_base(nullptr)
{
this->setFocusStop(true);
initTheme();
}
MenuBox::~MenuBox()
{
stopFilteringMouseDown();
}
//////////////////////////////////////////////////////////////////////
// MenuBar
bool MenuBar::m_expandOnMouseover = false;
MenuBar::MenuBar(ProcessTopLevelShortcuts processShortcuts)
: MenuBox(kMenuBarWidget)
, m_processTopLevelShortcuts(processShortcuts == ProcessTopLevelShortcuts::kYes)
{
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 = nullptr;
m_submenu_menubox = nullptr;
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()
{
m_base.reset(new MenuBaseData);
return m_base.get();
}
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(nullptr);
m_submenu = menu;
if (m_submenu) {
ASSERT_VALID_WIDGET(m_submenu);
m_submenu->setOwnerMenuItem(this);
}
}
void MenuItem::openSubmenu()
{
MenuBaseData* base = get_base(this);
if (base && base->is_processing)
return;
if (auto menu = static_cast<Menu*>(parent()))
menu->highlightItem(this, true, true, true);
}
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,
Display* parentDisplay)
{
// Set the owner menu item to nullptr temporarily in case that we
// are re-using a menu from the root menu as popup menu (e.g. like
// "animation_menu", that is used when right-cliking a Play button)
base::ScopedValue<MenuItem*> restoreOwner(m_menuitem, nullptr);
// 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
MenuBoxWindow window;
window.Open.connect([this]{ this->onOpenPopup(); });
// Set the native parent (without this the menubox will appear in
// the same screen of the main window/manager and not the
// parentDisplay's screen)
if (parentDisplay)
window.setParentDisplay(parentDisplay);
MenuBox* menubox = window.menubox();
MenuBaseData* base = menubox->createBase();
base->was_clicked = true;
// Set children
menubox->setMenu(this);
menubox->startFilteringMouseDown();
window.remapWindow();
fit_bounds(parentDisplay,
&window,
gfx::Rect(pos, window.size()),
[&window](const gfx::Rect& workarea,
gfx::Rect& bounds,
std::function<gfx::Rect(Widget*)> getWidgetBounds) {
choose_side(bounds, workarea, gfx::Rect(bounds.x-1, bounds.y, 1, 1));
add_scrollbars_if_needed(&window, workarea, bounds);
});
// Set the focus to the new menubox
Manager* manager = Manager::getDefault();
manager->setFocus(menubox);
// 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();
menubox->stopFilteringMouseDown();
}
Widget* Menu::findItemById(const char* id) const
{
Widget* result = findChild(id);
if (result)
return result;
for (auto child : children()) {
if (child->type() == kMenuItemWidget) {
if (Menu* submenu = static_cast<MenuItem*>(child)->getSubmenu()) {
result = submenu->findItemById(id);
if (result)
return result;
}
}
}
return nullptr;
}
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;
for (auto it=children().begin(),
end=children().end();
it!=end; ) {
auto next = it;
++next;
reqSize = (*it)->sizeHint();
if (parent() &&
parent()->type() == kMenuBarWidget) {
size.w += reqSize.w + ((next != end) ? childSpacing(): 0);
size.h = std::max(size.h, reqSize.h);
}
else {
size.w = std::max(size.w, reqSize.w);
size.h += reqSize.h + ((next != end) ? childSpacing(): 0);
}
it = next;
}
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: {
MenuBaseData* base = get_base(this);
ASSERT(base);
if (!base)
break;
if (!base->was_clicked)
break;
[[fallthrough]];
}
case kMouseDownMessage:
case kDoubleClickMessage:
if (menu && msg->display()) {
ASSERT(menu->parent() == this);
MenuBaseData* base = get_base(this);
ASSERT(base);
if (!base)
break;
if (base->is_processing)
break;
const gfx::Point mousePos = static_cast<MouseMessage*>(msg)->position();
const gfx::Point screenPos = msg->display()->nativeWindow()->pointToScreen(mousePos);
// Get the widget below the mouse cursor
auto mgr = manager();
if (!mgr)
break;
Widget* picked = mgr->pickFromScreenPos(screenPos);
// 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 != nullptr) {
// 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 == nullptr || // 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();
// Change to "return false" if you want to send the click
// to the window after closing all menus.
return true;
}
}
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.
base->was_clicked = false;
}
}
}
else if (!base->was_clicked) {
menu->unhighlightItem();
}
}
}
break;
case kMouseLeaveMessage:
if (menu) {
MenuBaseData* base = get_base(this);
ASSERT(base);
if (!base)
break;
if (base->is_processing)
break;
MenuItem* highlight = menu->getHighlightedItem();
if (highlight && !highlight->hasSubmenuOpened())
menu->unhighlightItem();
}
break;
case kMouseUpMessage:
if (menu) {
MenuBaseData* base = get_base(this);
ASSERT(base);
if (!base)
break;
if (base->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 == nullptr) {
menu->closeAll();
highlight->executeClick();
}
}
break;
case kKeyDownMessage:
if (menu) {
MenuItem* selected;
MenuBaseData* base = get_base(this);
ASSERT(base);
if (!base)
break;
if (base->is_processing)
break;
base->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) &&
static_cast<MenuBar*>(this)->processTopLevelShortcuts())) {
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 = nullptr;
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);
ASSERT(root);
if (!root)
break;
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;
case kMouseWheelMessage: {
View* view = View::getView(this);
if (view) {
auto mouseMsg = static_cast<MouseMessage*>(msg);
gfx::Point scroll = view->viewScroll();
if (mouseMsg->preciseWheel())
scroll += mouseMsg->wheelDelta();
else
scroll += mouseMsg->wheelDelta() * textHeight()*3;
view->setViewScroll(scroll);
}
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);
ASSERT(base);
if (!base)
break;
bool select_first = static_cast<OpenMenuItemMessage*>(msg)->select_first();
ASSERT(base->is_processing);
ASSERT(hasSubmenu());
// New window that will be automatically deleted
auto window = new MenuBoxWindow(this);
window->Close.connect([window]{
window->deferDelete();
});
auto parentDisplay = display();
if (parentDisplay)
window->setParentDisplay(parentDisplay);
MenuBox* menubox = window->menubox();
m_submenu_menubox = menubox;
menubox->setMenu(m_submenu);
window->remapWindow();
fit_bounds(
parentDisplay, window, window->bounds(),
[this, window, parentDisplay](const gfx::Rect& workarea,
gfx::Rect& bounds,
std::function<gfx::Rect(Widget*)> getWidgetBounds){
const gfx::Rect itemBounds = getWidgetBounds(this);
if (inBar()) {
bounds.x = std::clamp(itemBounds.x, workarea.x, std::max(workarea.x, workarea.x2()-bounds.w));
bounds.y = std::max(workarea.y, itemBounds.y2());
}
else {
int scale = guiscale();
if (get_multiple_displays())
scale = parentDisplay->scale();
const gfx::Rect parentBounds = getWidgetBounds(this->window());
bounds.y = itemBounds.y-3*scale;
choose_side(bounds, workarea, parentBounds);
}
add_scrollbars_if_needed(window, workarea, bounds);
});
// Setup the highlight of the new menubox
if (select_first) {
// Select the first child
MenuItem* first_child = nullptr;
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);
ASSERT(base);
if (!base)
break;
ASSERT(base->is_processing);
ASSERT(m_submenu_menubox);
Window* window = m_submenu_menubox->window();
ASSERT(window && window->type() == kWindowWidget);
m_submenu_menubox = nullptr;
// Destroy the window
window->closeWindow(nullptr);