-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathmacmenu.cpp
More file actions
1759 lines (1398 loc) · 47.6 KB
/
macmenu.cpp
File metadata and controls
1759 lines (1398 loc) · 47.6 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
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
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "common/system.h"
#include "common/stack.h"
#include "common/keyboard.h"
#include "common/macresman.h"
#include "common/formats/winexe_pe.h"
#include "common/unicode-bidi.h"
#include "common/timer.h"
#include "graphics/primitives.h"
#include "graphics/font.h"
#include "graphics/fonts/ttf.h"
#include "graphics/macgui/macfontmanager.h"
#include "graphics/macgui/macwindowmanager.h"
#include "graphics/macgui/macwindow.h"
#include "graphics/macgui/macmenu.h"
#define SCROLL_DELAY 100000
namespace Graphics {
enum {
kMenuHeight = 20,
kMenuLeftMargin = 7,
kMenuSpacing = 13,
kMenuPadding = 16,
kMenuDropdownPadding = 14,
kMenuDropdownItemHeight = 16,
kMenuItemHeight = 20,
kMenuWin95LeftDropdownPadding = 34,
kMenuWin95RightDropdownPadding = 57,
kMenuWin95DropdownItemHeight = 20
};
enum {
kMenuHighLevel = -1
};
enum {
kGrayed = 1,
kInactive = 2,
kPopUp = 16,
kMenuBarBreak = 32,
kMenuBreak = 64,
kEndMenu = 128
};
struct MacMenuSubMenu;
MacMenuSubMenu::~MacMenuSubMenu() {
for (uint i = 0; i < items.size(); i++)
delete items[i];
}
void MacMenuSubMenu::enableAllItems() {
for (uint i = 0; i < items.size(); ++i) {
items[i]->enabled = true;
if (items[i]->submenu) {
items[i]->submenu->enableAllItems();
}
}
}
MacMenu::MacMenu(int id, const Common::Rect &bounds, MacWindowManager *wm)
: BaseMacWindow(id, false, wm) {
_loadedFont = NULL;
_font = getMenuFont();
_align = kTextAlignLeft;
_type = MacWindowConstants::kWindowMenu;
_screen.create(bounds.width(), bounds.height(), _wm->_pixelformat);
_bbox.left = 0;
_bbox.top = 0;
_bbox.right = _screen.w;
_bbox.bottom = kMenuHeight;
_dimensionsDirty = true;
if (_wm->_mode & kWMModeWin95 && !(_wm->_mode & kWMModeForceMacFontsInWin95)) {
_menuDropdownItemHeight = kMenuWin95DropdownItemHeight;
_menuLeftDropdownPadding = kMenuWin95LeftDropdownPadding;
_menuRightDropdownPadding = kMenuWin95RightDropdownPadding;
} else {
_menuDropdownItemHeight = kMenuDropdownItemHeight;
_menuLeftDropdownPadding = kMenuDropdownPadding;
_menuRightDropdownPadding = kMenuDropdownPadding;
}
if (_wm->_mode & kWMModeAutohideMenu)
_isVisible = false;
else
_isVisible = true;
_activeItem = -1;
_activeSubItem = -1;
_lastActiveItem = -1;
_lastActiveSubItem = -1;
_selectedItem = -1;
_ccallback = NULL;
_unicodeccallback = NULL;
_cdata = NULL;
_scrollTimerActive = false;
_scrollDirection = 0;
_isModal = false;
_tempSurface.create(_screen.w, _font->getFontHeight(), _wm->_pixelformat);
}
MacMenu::~MacMenu() {
for (uint i = 0; i < _items.size(); i++)
delete _items[i];
delete _loadedFont;
}
Common::StringArray *MacMenu::readMenuFromResource(Common::SeekableReadStream *res) {
res->skip(10);
int enableFlags = res->readUint32BE();
Common::String menuName = res->readPascalString();
Common::String menuItem = res->readPascalString();
int menuItemNumber = 1;
Common::String menu;
byte itemData[4];
while (!menuItem.empty()) {
if (!menu.empty()) {
menu += ';';
}
if ((enableFlags & (1 << menuItemNumber)) == 0) {
menu += '(';
}
menu += menuItem;
res->read(itemData, 4);
static const char styles[] = {'B', 'I', 'U', 'O', 'S', 'C', 'E', 0};
for (int i = 0; styles[i] != 0; i++) {
if ((itemData[3] & (1 << i)) != 0) {
menu += '<';
menu += styles[i];
}
}
if (itemData[1] != 0) {
menu += '/';
menu += (char)itemData[1];
}
menuItem = res->readPascalString();
menuItemNumber++;
}
Common::StringArray *result = new Common::StringArray;
result->push_back(menuName);
result->push_back(menu);
debug(4, "menuName: %s", menuName.c_str());
debug(4, "menu: %s", menu.c_str());
return result;
}
static Common::U32String readUnicodeString(Common::SeekableReadStream *stream) {
Common::U32String strData;
uint16 wchar;
while ((wchar = stream->readUint16LE()) != '\0') {
strData += wchar;
}
return strData;
}
void MacMenu::setAlignment(Graphics::TextAlign align) {
_align = align;
}
MacMenu *MacMenu::createMenuFromPEexe(Common::PEResources *exe, MacWindowManager *wm) {
Common::SeekableReadStream *menuData = exe->getResource(Common::kWinMenu, 128);
if (!menuData)
return nullptr;
menuData->readUint16LE(); // wVersion
menuData->readUint16LE(); // cbHeaderSize
MacMenu *menu = wm->addMenu();
Common::Stack<MacMenuSubMenu *> menus;
Common::Stack<bool> popups;
int depth = 0;
bool lastPopUp = false;
while (depth >= 0) {
uint16 flags = menuData->readUint16LE();
if (flags & kPopUp) {
if (depth == 0) {
menu->addMenuItem(nullptr, readUnicodeString(menuData));
} else {
Common::U32String name = readUnicodeString(menuData);
menu->addMenuItem(menus.top(), name, -1, 0, 0, !(flags & kGrayed));
}
MacMenuSubMenu *submenu = menu->addSubMenu(menus.size() ? menus.top() : nullptr);
menus.push(submenu);
popups.push(lastPopUp);
lastPopUp = (flags & kEndMenu) != 0;
depth++;
} else {
int action = menuData->readUint16LE();
Common::U32String name = readUnicodeString(menuData);
if (depth > 0) {
menu->addMenuItem(menus.top(), name, action, 0, 0, !(flags & kGrayed));
}
if (flags & kEndMenu) {
menus.pop();
depth--;
if (lastPopUp) {
depth--;
if (menus.size())
menus.pop();
}
lastPopUp = popups.pop();
}
}
}
menu->processTabs();
delete menuData;
if (gDebugLevel > 5)
menu->printMenu();
if (wm->_mode & Graphics::kWMModeWin95)
menu->enableAllMenus();
return menu;
}
void MacMenu::printMenu(int level, MacMenuSubMenu *submenu) {
if (!level) {
for (uint i = 0; i < _items.size(); i++) {
debug("0. %s --> %d", _items[i]->unicode ? convertFromU32String(_items[i]->unicodeText).c_str() : _items[i]->text.c_str(), _items[i]->action);
if (_items[i]->submenu != nullptr)
printMenu(level + 1, _items[i]->submenu);
}
} else {
for (uint i = 0; i < submenu->items.size(); i++) {
debugN("%d. ", level);
for (int j = 0; j < level; j++)
debugN(" ");
debug("%s --> %d", submenu->items[i]->unicode ? convertFromU32String(submenu->items[i]->unicodeText).c_str() : submenu->items[i]->text.c_str(), submenu->items[i]->action);
if (submenu->items[i]->submenu != nullptr)
printMenu(level + 1, submenu->items[i]->submenu);
}
}
}
int MacMenu::numberOfMenus() {
return _items.size();
}
MacMenuItem *MacMenu::getMenuItem(const Common::String &menuId) {
MacMenuItem *menu = nullptr;
for (uint i = 0; i < _items.size(); i++) {
// TODO: support unicode text menu
// didn't support unicode item finding yet
if (!_items[i]->unicode) {
if (_items[i]->text.equalsIgnoreCase(menuId)) {
menu = _items[i];
break;
}
}
}
return menu;
}
MacMenuItem *MacMenu::getMenuItem(int menuId) {
MacMenuItem *menu = nullptr;
if ((uint)menuId < _items.size())
menu = _items[menuId];
return menu;
}
MacMenuItem *MacMenu::getSubMenuItem(MacMenuItem *menu, const Common::String &itemId) {
if (!menu) {
warning("MacMenu::getSubMenuItem: can not find given menu");
return nullptr;
}
if (!menu->submenu) {
warning("MacMenu::getSubMenuItem: menu %s doesn't have submenu", menu->text.c_str());
return nullptr;
}
for (uint i = 0; i < menu->submenu->items.size(); i++) {
if (!menu->submenu->items[i]->unicode) {
if (menu->submenu->items[i]->text.equalsIgnoreCase(itemId))
return menu->submenu->items[i];
}
}
warning("MacMenu::getSubMenuItem: menu %s doesn't have item with id %s", menu->text.c_str(), itemId.c_str());
return nullptr;
}
MacMenuItem *MacMenu::getSubMenuItem(MacMenuItem *menu, int itemId) {
if (!menu) {
warning("MacMenu::getSubMenuItem: menuId out of bounds");
return nullptr;
}
if (!menu->submenu) {
warning("MacMenu::getSubMenuItem: menu %s doesn't have submenu", menu->text.c_str());
return nullptr;
}
if ((uint)itemId < menu->submenu->items.size())
return menu->submenu->items[itemId];
warning("MacMenu::getSubMenuItem: itemId %d out of bounds in menu %s", itemId, menu->text.c_str());
return nullptr;
}
int MacMenu::numberOfMenuItems(MacMenuItem *menu) {
if (!menu) {
warning("MacMenu::numberOfMenuItems: can not find menu");
return 0;
}
if (!menu->submenu) {
warning("MacMenu::numberOfMenuItems: menu with id %s has no submenu", menu->text.c_str());
return 0;
}
return menu->submenu->items.size();
}
void MacMenu::addStaticMenus(const MacMenuData *data) {
MacMenuItem *about = new MacMenuItem(_wm->_fontMan->hasBuiltInFonts() ? "\xa9" : "\xf0"); // (c) Symbol as the most resembling apple
_items.push_back(about);
for (int i = 0; data[i].menunum; i++) {
const MacMenuData *m = &data[i];
MacMenuSubMenu *submenu = getSubmenu(nullptr, m->menunum);
if (m->menunum == kMenuHighLevel) {
addMenuItem(nullptr, m->title);
continue;
}
if (submenu == nullptr)
submenu = addSubMenu(nullptr, m->menunum);
addMenuItem(submenu, m->title, m->action, 0, m->shortcut, m->enabled);
}
calcDimensions();
}
MacMenuSubMenu *MacMenu::addSubMenu(MacMenuSubMenu *submenu, int index) {
_dimensionsDirty = true;
if (submenu == nullptr) {
if (index == -1)
index = _items.size() - 1;
if (_items[index]->submenu != nullptr)
warning("Overwritting submenu #%d", index);
return (_items[index]->submenu = new MacMenuSubMenu());
} else {
if (index == -1)
index = submenu->items.size() - 1;
if (submenu->items[index]->submenu != nullptr)
warning("Overwritting submenu #%d", index);
return (submenu->items[index]->submenu = new MacMenuSubMenu());
}
}
MacMenuSubMenu *MacMenu::getSubmenu(MacMenuSubMenu *submenu, int index) {
if (submenu == nullptr) {
if (index < 0 || (uint)index >= _items.size())
return nullptr;
return _items[index]->submenu;
} else {
if (index < 0 || (uint)index >= submenu->items.size())
return nullptr;
return submenu->items[index]->submenu;
}
}
int MacMenu::addMenuItem(MacMenuSubMenu *submenu, const Common::String &text, int action, int style, char shortcut, bool enabled, bool checked) {
_dimensionsDirty = true;
if (submenu == nullptr) {
MacMenuItem *i = new MacMenuItem(text);
_items.push_back(i);
return _items.size() - 1;
}
_dimensionsDirty = true;
submenu->items.push_back(new MacMenuItem(text, action, style, shortcut, -1, enabled, checked));
return submenu->items.size() - 1;
}
int MacMenu::addMenuItem(MacMenuSubMenu *submenu, const Common::U32String &text, int action, int style, char shortcut, bool enabled, bool checked) {
_dimensionsDirty = true;
Common::U32String amp("&");
Common::U32String res;
int shortcutPos = -1;
for (uint i = 0; i < text.size(); i++)
if (text[i] == amp[0]) {
if (i < text.size() - 1) {
if ((text[i + 1] & 0xff) != '&') {
shortcut = text[i + 1] & 0xff;
shortcutPos = i;
} else {
res += text[i];
}
}
} else {
res += text[i];
}
if (submenu == nullptr) {
_items.push_back(new MacMenuItem(res, -1, 0, shortcut, shortcutPos));
return _items.size() - 1;
}
submenu->items.push_back(new MacMenuItem(res, action, style, shortcut, shortcutPos, enabled, checked));
return submenu->items.size() - 1;
}
void MacMenu::insertMenuItem(MacMenuSubMenu *submenu, const Common::String &text, uint pos, int action, int style, char shortcut, bool enabled, bool checked) {
_dimensionsDirty = true;
if (submenu == nullptr) {
if (pos >= _items.size()) {
_dimensionsDirty = false;
} else {
MacMenuItem *i = new MacMenuItem(text);
_items.insert(_items.begin() + pos, i);
}
return;
}
if (pos >= submenu->items.size()) {
_dimensionsDirty = false;
return;
}
_dimensionsDirty = true;
submenu->items.insert(submenu->items.begin() + pos, new MacMenuItem(text, action, style, shortcut, -1, enabled, checked));
}
void MacMenu::insertMenuItem(MacMenuSubMenu *submenu, const Common::U32String &text, uint pos, int action, int style, char shortcut, bool enabled, bool checked) {
_dimensionsDirty = true;
Common::U32String amp("&");
Common::U32String res;
int shortcutPos = -1;
for (uint i = 0; i < text.size(); i++)
if (text[i] == amp[0]) {
if (i < text.size() - 1) {
if ((text[i + 1] & 0xff) != '&') {
shortcut = text[i + 1] & 0xff;
shortcutPos = i;
} else {
res += text[i];
}
}
} else {
res += text[i];
}
if (submenu == nullptr) {
if (pos >= _items.size()) {
_dimensionsDirty = false;
} else {
_items.insert(_items.begin() + pos, new MacMenuItem(res, -1, 0, shortcut, shortcutPos));
}
return;
}
if (pos >= submenu->items.size()) {
_dimensionsDirty = false;
return;
}
submenu->items.insert(submenu->items.begin() + pos, new MacMenuItem(res, action, style, shortcut, shortcutPos, enabled, checked));
}
void MacMenu::removeMenuItem(MacMenuSubMenu *submenu, uint pos) {
_dimensionsDirty = true;
if (submenu == nullptr) {
if (pos >= _items.size()) {
_dimensionsDirty = false;
} else {
delete _items.remove_at(pos);
}
return;
}
if (pos >= submenu->items.size()) {
_dimensionsDirty = false;
return;
}
delete submenu->items.remove_at(pos);
}
void MacMenu::calcDimensions() {
// Calculate menu dimensions
int y = 1;
int x = 18;
for (uint i = 0; i < _items.size(); i++) {
int w = _items[i]->unicode ? _font->getStringWidth(_items[i]->unicodeText) : _font->getStringWidth(_items[i]->text);
if (_items[i]->bbox.bottom == 0) {
_items[i]->bbox.left = x - kMenuLeftMargin;
_items[i]->bbox.top = y;
_items[i]->bbox.right = x + w + kMenuSpacing - kMenuLeftMargin;
_items[i]->bbox.bottom = y + _font->getFontHeight() + (_wm->_fontMan->hasBuiltInFonts() ? 3 : 2);
}
calcSubMenuBounds(_items[i]->submenu, _items[i]->bbox.left - 1, _items[i]->bbox.bottom + 1);
if (_align == kTextAlignRight) {
int right = _items[i]->bbox.right;
int width = right - _items[i]->bbox.left;
_items[i]->bbox.left = _bbox.right + _bbox.left - right;
_items[i]->bbox.right = _items[i]->bbox.left + width;
}
x += w + kMenuSpacing;
}
_dimensionsDirty = false;
}
void MacMenu::loadMenuResource(Common::MacResManager *resFork, uint16 id) {
Common::SeekableReadStream *res = resFork->getResource(MKTAG('M', 'E', 'N', 'U'), id);
assert(res);
uint16 menuID = res->readUint16BE();
/* uint16 width = */ res->readUint16BE();
/* uint16 height = */ res->readUint16BE();
/* uint16 resourceID = */ res->readUint16BE();
/* uint16 placeholder = */ res->readUint16BE();
uint32 initialState = res->readUint32BE();
Common::String menuTitle = res->readPascalString();
if (!menuTitle.empty()) {
addMenuItem(nullptr, menuTitle);
MacMenuSubMenu *submenu = addSubMenu(nullptr);
initialState >>= 1;
// Read submenu items
int action = menuID << 16;
while (true) {
Common::String subMenuTitle = res->readPascalString();
if (subMenuTitle.empty())
break;
/* uint8 icon = */ res->readByte();
uint8 key = res->readByte();
/* uint8 mark = */ res->readByte();
uint8 style = res->readByte();
addMenuItem(submenu, subMenuTitle, action++, style, key, initialState & 1);
initialState >>= 1;
}
}
delete res;
}
void MacMenu::loadMenuBarResource(Common::MacResManager *resFork, uint16 id) {
Common::SeekableReadStream *res = resFork->getResource(MKTAG('M', 'B', 'A', 'R'), id);
assert(res);
uint16 count = res->readUint16BE();
for (int i = 0; i < count; i++) {
loadMenuResource(resFork, res->readUint16BE());
}
}
void MacMenu::setCheckMark(MacMenuItem *menuItem, bool checkMark) {
if (menuItem) {
menuItem->checked = checkMark;
_contentIsDirty = true;
}
}
void MacMenu::setEnabled(MacMenuItem *menuItem, bool enabled) {
if (menuItem) {
menuItem->enabled = enabled;
_contentIsDirty = true;
}
}
void MacMenu::setName(MacMenuItem *menuItem, const Common::String &name) {
if (menuItem) {
menuItem->text = name;
_contentIsDirty = true;
}
}
void MacMenu::setAction(MacMenuItem *menuItem, int actionId) {
if (menuItem) {
menuItem->action = actionId;
}
}
bool MacMenu::getCheckMark(MacMenuItem *menuItem) {
return menuItem ? menuItem->checked : false;
}
bool MacMenu::getEnabled(MacMenuItem *menuItem) {
return menuItem ? menuItem->enabled : false;
}
Common::String MacMenu::getName(MacMenuItem *menuItem) {
return menuItem ? menuItem->text : Common::String();
}
int MacMenu::getAction(MacMenuItem *menuItem) {
return menuItem ? menuItem->action : 0;
}
void MacMenu::clearSubMenu(int id) {
MacMenuItem *menu = _items[id];
if (menu->submenu == nullptr)
return;
for (uint j = 0; j < menu->submenu->items.size(); j++)
delete menu->submenu->items[j];
menu->submenu->items.clear();
menu->submenu->highlight = -1;
}
void MacMenu::createSubMenuFromString(int id, const char *str, int commandId) {
clearSubMenu(id);
Common::String string(str);
Common::String item;
MacMenuSubMenu *submenu = getSubmenu(nullptr, id);
if (submenu == nullptr)
submenu = addSubMenu(nullptr, id);
for (uint i = 0; i < string.size(); i++) {
while (i < string.size() && (string[i] != ';' && string[i] != '\r')) // Read token, consume \r for popup menu (MacPopUp)
item += string[i++];
if (item.lastChar() == ']') { // we have command id
item.deleteLastChar();
const char *p = strrchr(item.c_str(), '[');
p++;
if (p == NULL) {
error("MacMenu::createSubMenuFromString(): Malformed menu item: '%s', bad format for actionId", item.c_str());
}
commandId = atoi(p);
item = Common::String(item.c_str(), p - 1);
}
if (item == "(-") {
addMenuItem(submenu, NULL, 0);
} else {
bool enabled = true;
bool checked = false;
int style = 0;
char shortcut = 0;
const char *shortPtr = strrchr(item.c_str(), '/');
if (shortPtr != NULL) {
if (strlen(shortPtr) >= 2) {
shortcut = shortPtr[1];
item.deleteChar(shortPtr - item.c_str());
item.deleteChar(shortPtr - item.c_str());
} else {
// if we only have one / without shortcut key, we skip it.
item.deleteChar(shortPtr - item.c_str());
// error("MacMenu::createSubMenuFromString(): Unexpected shortcut: '%s', item '%s' in menu '%s'", shortPtr, item.c_str(), string.c_str());
}
}
while (item.size() >= 2 && item[item.size() - 2] == '<') {
char c = item.lastChar();
if (c == 'B') {
style |= kMacFontBold;
} else if (c == 'I') {
style |= kMacFontItalic;
} else if (c == 'U') {
style |= kMacFontUnderline;
} else if (c == 'O') {
style |= kMacFontOutline;
} else if (c == 'S') {
style |= kMacFontShadow;
} else if (c == 'C') {
style |= kMacFontCondense;
} else if (c == 'E') {
style |= kMacFontExtend;
}
item.deleteLastChar();
item.deleteLastChar();
}
Common::String tmpitem(item);
tmpitem.trim();
// is that any places locate a parenthese will disable the item, or only the first char and last char counts.
if (tmpitem.size() > 0 && (tmpitem[0] == '(' || tmpitem.lastChar() == '(')) {
enabled = false;
for (uint j = 0; j < item.size(); j++)
if (item[j] == '(') {
item.deleteChar(j);
break;
}
} else if (tmpitem.size() >= 2 && tmpitem[0] == '!' && (uint8)tmpitem[1] == 195) {
// this is the !√ situation, we need to set item checked, 195 represent √ in director
checked = true;
item = item.substr(2, Common::String::npos);
}
addMenuItem(submenu, item, commandId, style, shortcut, enabled, checked);
}
item.clear();
}
}
const Font *MacMenu::getMenuFont(int slant) {
#ifdef USE_FREETYPE2
if (_wm->_mode & kWMModeWin95 && !(_wm->_mode & kWMModeForceMacFontsInWin95)) {
if (!_loadedFont) {
_loadedFont = Graphics::loadTTFFontFromArchive("ms_sans_serif.ttf", 16);
if (_loadedFont)
return _loadedFont;
} else {
return _loadedFont;
}
// If font was not loaded, fallback
}
#endif
return _wm->_fontMan->getFont(Graphics::MacFont(kMacFontSystem, 12, slant));
}
Common::CodePage MacMenu::getMenuEncoding() const {
#ifdef USE_FREETYPE2
if (_wm->_mode & kWMModeWin95 && !(_wm->_mode & kWMModeForceMacFontsInWin95)) {
return Common::CodePage::kUtf8;
}
#endif
return _wm->_fontMan->getFontEncoding(kMacFontSystem);
}
const Common::String MacMenu::getAcceleratorString(MacMenuItem *item, const char *prefix) {
if (item->shortcut == 0)
return Common::String();
return Common::String::format("%s%c%c", prefix, (_wm->_fontMan->hasBuiltInFonts() ? '^' : '\x11'), item->shortcut);
}
void MacMenu::processTabs() {
for (uint i = 0; i < _items.size(); i++)
processSubmenuTabs(_items[i]->submenu);
}
void MacMenu::processSubmenuTabs(MacMenuSubMenu *submenu) {
if (submenu == nullptr)
return;
for (uint i = 0; i < submenu->items.size(); i++) {
MacMenuSubMenu *menu = submenu->items[i]->submenu;
if (menu != nullptr)
processSubmenuTabs(menu);
}
int maxWidth = 0;
bool haveTabs = false;
Common::U32String tabSymbol("\t");
// First, we replace \t with two spaces, and thus, obtain
// the widest string
for (uint i = 0; i < submenu->items.size(); i++) {
MacMenuItem *item = submenu->items[i];
if (item->unicodeText.empty())
continue;
Common::U32String res(item->unicodeText);
uint32 pos = item->unicodeText.find(tabSymbol);
if (pos != Common::U32String::npos) {
// Sanity check
if (pos == 0 || pos >= item->unicodeText.size())
error("Malformed menu: tab position");
if (item->unicodeText.find(tabSymbol, pos + 1) != Common::U32String::npos)
error("Malformed menu: extra tab");
haveTabs = true;
Common::U32String start = item->unicodeText.substr(0, pos);
Common::U32String end = item->unicodeText.substr(pos + 1);
res = start;
res += Common::U32String(" ");
res += end;
}
int width = _font->getStringWidth(res);
if (width > maxWidth) {
maxWidth = width;
}
}
if (!haveTabs)
return;
// Now expand each tab to the relevant width
// And yes, right edge is going to be uneven
for (uint i = 0; i < submenu->items.size(); i++) {
MacMenuItem *item = submenu->items[i];
if (item->unicodeText.empty())
continue;
uint32 pos = item->unicodeText.find(tabSymbol);
if (pos == Common::U32String::npos)
continue;
Common::U32String start = item->unicodeText.substr(0, pos);
Common::U32String end = item->unicodeText.substr(pos + 1);
Common::U32String res;
Common::U32String spaces(" ");
int width;
do {
res = start;
res += spaces;
res += end;
width = _font->getStringWidth(res);
spaces += spaces[0];
} while (width < maxWidth);
item->unicodeText = res;
}
}
int MacMenu::calcSubMenuWidth(MacMenuSubMenu *submenu) {
int maxWidth = 0;
if (submenu == nullptr)
return maxWidth;
for (uint i = 0; i < submenu->items.size(); i++) {
MacMenuItem *item = submenu->items[i];
if (!item->text.empty()) {
Common::String text(item->text);
Common::String acceleratorText(getAcceleratorString(item, " "));
const Font *font = getMenuFont(item->style);
int width = font->getStringWidth(text);
if (!acceleratorText.empty()) {
width += _font->getStringWidth(acceleratorText);
}
if (item->submenu != nullptr) { // If we're drawing triangle
width += _font->getStringWidth(" ");
}
if (width > maxWidth) {
maxWidth = width;
}
} else if (!item->unicodeText.empty()) {
Common::U32String text(item->unicodeText);
if (item->submenu != nullptr) // If we're drawing triangle
text += Common::U32String(" ");
int width = _font->getStringWidth(text);
if (width > maxWidth) {
maxWidth = width;
}
}
}
return maxWidth > 225 ? 225 : maxWidth;
}
void MacMenu::calcSubMenuBounds(MacMenuSubMenu *submenu, int x, int y) {
if (submenu == nullptr)
return;
// TODO: cache maxWidth
int maxWidth = calcSubMenuWidth(submenu);
int x1 = x;
int y1 = y;
int x2 = x1 + maxWidth + _menuLeftDropdownPadding + _menuRightDropdownPadding - 4;
int y2 = y1 + submenu->items.size() * _menuDropdownItemHeight + 2;
y2 = MIN(y2, y1 + ((_screen.h - y1) / _menuDropdownItemHeight) * _menuDropdownItemHeight + 2);
submenu->bbox.left = x1;
submenu->bbox.top = y1;
submenu->bbox.right = x2;
submenu->bbox.bottom = y2;
if (_align == kTextAlignRight) {
submenu->bbox.left = _bbox.right + _bbox.left - x2;
submenu->bbox.right = submenu->bbox.left + x2 - x1;
}
for (uint i = 0; i < submenu->items.size(); i++) {
MacMenuSubMenu *menu = submenu->items[i]->submenu;
if (menu != nullptr)
calcSubMenuBounds(menu, x2 - 4, y1 + i * _menuDropdownItemHeight + 1);
}
}
template <typename T>
static void drawMenuPattern(ManagedSurface &srcSurf, ManagedSurface &destSurf, const byte *pattern, int x, int y, int width, uint32 colorKey) {
// I am lazy to extend drawString() with plotProc as a parameter, so
// fake it here
for (int ii = 0; ii < srcSurf.h; ii++) {
const T *src = (const T *)srcSurf.getBasePtr(0, ii);
T *dst = (T *)destSurf.getBasePtr(x, y + ii);
byte pat = pattern[ii % 8];
for (int j = 0; j < width; j++) {
if (*src != colorKey && (pat & (1 << (7 - (x + j) % 8))))
*dst = *src;
src++;
dst++;
}
}
}
template <typename T>
static void drawMenuDelimiter(ManagedSurface &srcSurf, Common::Rect *r, int y, uint32 black, uint32 white) {
bool flip = r->left & 2;