ScummVM API documentation
macmenu.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef GRAPHICS_MACGUI_MACMENU_H
23 #define GRAPHICS_MACGUI_MACMENU_H
24 
25 #include "common/str-array.h"
26 #include "graphics/macgui/macfontmanager.h"
27 #include "graphics/macgui/macwindow.h"
28 #include "graphics/font.h"
29 
30 namespace Common {
31 class U32String;
32 class MacResManager;
33 class PEResources;
34 }
35 
36 namespace Graphics {
37 
38 struct MacMenuItem;
39 struct MacMenuSubMenu;
40 typedef Common::Array<MacMenuItem *> ItemArray;
41 
43  ItemArray items;
44  Common::Rect bbox;
45  int highlight;
46  int visStart; // Visible start
47  int visEnd; // Visible end
48  int scroll;
49 
50  MacMenuSubMenu() : highlight(-1), visStart(0), visEnd(0), scroll(0) {}
51 
52  ~MacMenuSubMenu();
53 
54  void enableAllItems();
55 
56  int ytoItem(int y, int itemHeight) { return MIN<int>((y - bbox.top) / itemHeight, items.size() - 1); }
57 };
58 
59 struct MacMenuItem {
60  Common::String text;
61  Common::U32String unicodeText;
62  bool unicode;
63  int action;
64  int style;
65  char shortcut;
66  int shortcutPos;
67  bool enabled;
68  bool checked;
69  Common::Rect bbox;
70 
71  MacMenuSubMenu *submenu;
72 
73  MacMenuItem(const Common::String &t, int a = -1, int s = 0, char sh = 0, int sp = -1, bool e = true, bool c = false) :
74  text(t), unicode(false), action(a), style(s), shortcut(sh),
75  shortcutPos(sp), enabled(e), submenu(nullptr), checked(c) {}
76  MacMenuItem(const Common::U32String &t, int a = -1, int s = 0, char sh = 0, int sp = -1, bool e = true, bool c = false) :
77  unicodeText(t), unicode(true), action(a), style(s), shortcut(sh),
78  shortcutPos(sp), enabled(e), submenu(nullptr), checked(c) {}
79 
80  ~MacMenuItem() {
81  if (submenu)
82  delete submenu;
83  }
84 };
85 
86 struct MacMenuData {
87  int menunum;
88  const char *title;
89  int action;
90  byte shortcut;
91  bool enabled;
92 };
93 
94 class MacMenu : public BaseMacWindow {
95 public:
96  MacMenu(int id, const Common::Rect &bounds, MacWindowManager *wm);
97  ~MacMenu();
98 
99  ManagedSurface *getBorderSurface() override { return nullptr; }
100  const Common::Rect &getInnerDimensions() override { return _dims; }
101  bool isDirty() override { return _contentIsDirty || _dimensionsDirty; }
102 
103  static Common::StringArray *readMenuFromResource(Common::SeekableReadStream *res);
104  static MacMenu *createMenuFromPEexe(Common::PEResources *exe, MacWindowManager *wm);
105 
106  void setAlignment(Graphics::TextAlign align);
107 
108  void setCommandsCallback(void (*callback)(int, Common::String &, void *), void *data) { _ccallback = callback; _cdata = data; }
109  void setCommandsCallback(void (*callback)(int, Common::U32String &, void *), void *data) { _unicodeccallback = callback; _cdata = data; }
110 
111  void addStaticMenus(const MacMenuData *data);
112  void calcDimensions();
113 
114  int numberOfMenus();
115  int numberOfMenuItems(MacMenuItem *menu);
116 
117  MacMenuItem *getMenuItem(int menuId);
118  MacMenuItem *getMenuItem(const Common::String &menuId);
119  MacMenuItem *getSubMenuItem(MacMenuItem *menu, int itemId);
120  MacMenuItem *getSubMenuItem(MacMenuItem *menu, const Common::String &itemId);
121 
122  MacMenuSubMenu *addSubMenu(MacMenuSubMenu *submenu, int index = -1);
123  int addMenuItem(MacMenuSubMenu *submenu, const Common::String &text, int action = -1, int style = 0, char shortcut = 0, bool enabled = true, bool checked = false);
124  int addMenuItem(MacMenuSubMenu *submenu, const Common::U32String &text, int action = 0, int style = 0, char shortcut = 0, bool enabled = true, bool checked = false);
125  void insertMenuItem(MacMenuSubMenu *submenu, const Common::String &text, uint pos, int action = -1, int style = 0, char shortcut = 0, bool enabled = true, bool checked = false);
126  void insertMenuItem(MacMenuSubMenu *submenu, const Common::U32String &text, uint pos, int action = 0, int style = 0, char shortcut = 0, bool enabled = true, bool checked = false);
127  void removeMenuItem(MacMenuSubMenu *submenu, uint pos);
128  void loadMenuResource(Common::MacResManager *resFork, uint16 id);
129  void loadMenuBarResource(Common::MacResManager *resFork, uint16 id);
130  void createSubMenuFromString(int id, const char *string, int commandId);
131  void clearSubMenu(int id);
132 
133  MacMenuSubMenu *getSubmenu(MacMenuSubMenu *submenu, int index);
134 
135  bool draw(ManagedSurface *g, bool forceRedraw = false) override;
136  void eventLoop();
137  bool mouseClick(int x, int y);
138  bool draw(bool forceRedraw = false) override { return false; }
139  void blit(ManagedSurface *g, Common::Rect &dest) override {}
140 
141  bool processEvent(Common::Event &event) override;
142 
143  void enableCommand(int menunum, int action, bool state);
144  void enableCommand(const char *menuitem, const char *menuaction, bool state);
145  void enableCommand(const Common::U32String &menuitem, const Common::U32String &menuaction, bool state);
146  void enableAllMenus();
147  void disableAllMenus();
148 
149  bool isVisible() { return _isVisible; }
150  void setVisible(bool visible, bool silent = false) override { _isVisible = visible; _contentIsDirty = true; }
151 
152  void printMenu(int level = 0, MacMenuSubMenu *submenu = nullptr);
153 
154  virtual void closeMenu();
155 
156  bool checkIntersects(Common::Rect &rect);
157 
158  // macmenuItem operations
159  void setCheckMark(MacMenuItem *menuItem, bool checkMark);
160  bool getCheckMark(MacMenuItem *menuItem);
161 
162  void setEnabled(MacMenuItem *menuItem, bool enabled);
163  bool getEnabled(MacMenuItem *menuItem);
164 
165  void setName(MacMenuItem *menuItem, const Common::String &name);
166  Common::String getName(MacMenuItem *menuItem);
167 
168  void setAction(MacMenuItem *menuItem, int actionId);
169  int getAction(MacMenuItem *menuItem);
170 
171  int getLastSelectedMenuItem() { return _lastActiveItem; };
172  int getLastSelectedSubmenuItem() { return _lastActiveSubItem; };
173 
174  void renderSubmenu(MacMenuSubMenu *menu, bool recursive = true);
175 
176  int getScrollDirection() { return _scrollDirection; }
177 
178  int getDropdownItemHeight() { return _menuDropdownItemHeight; }
179 
181 
182 protected:
183  Common::Rect _bbox;
184  ManagedSurface _screen;
185  ItemArray _items;
186  bool _isVisible;
187  bool _dimensionsDirty;
188  int _menuDropdownItemHeight;
189 
190  int _activeItem;
191  int _activeSubItem;
192 
193  bool _isModal;
194 
195  void calcSubMenuBounds(MacMenuSubMenu *menu, int x, int y);
196 
197 private:
198  ManagedSurface _tempSurface;
199  TextAlign _align;
200  int _menuLeftDropdownPadding;
201  int _menuRightDropdownPadding;
202 
203 private:
204  bool checkCallback(bool unicode = false);
205  const Font *getMenuFont(int slant = kMacFontRegular);
206  const Common::String getAcceleratorString(MacMenuItem *item, const char *prefix);
207  void processTabs();
208  void processSubmenuTabs(MacMenuSubMenu *submenu);
209 
210  int calcSubMenuWidth(MacMenuSubMenu *menu);
211 
212  bool keyEvent(Common::Event &event);
213  bool mouseRelease(int x, int y);
214  bool mouseMove(int x, int y);
215 
216  bool processMenuShortCut(uint16 ascii);
217 
218  void drawSubMenuArrow(ManagedSurface *dst, int x, int y, int color);
219  bool contains(int x, int y);
220 
221  void drawScrollArrow(int arrowX, int arrowY, int direction);
222 
223  MacMenuItem *findMenuItem(const Common::String &menuId, const Common::String &itemId);
224  MacMenuItem *findMenuItem(int menuId, int itemId);
225 
226 
227  const Font *_font;
228  Font *_loadedFont;
229 
230  int _lastActiveItem;
231  int _lastActiveSubItem;
232 
233  bool _scrollTimerActive;
234  int _scrollDirection;
235 
236  void (*_ccallback)(int action, Common::String &text, void *data);
237  void (*_unicodeccallback)(int action, Common::U32String &text, void *data);
238  void *_cdata;
239 };
240 
241 } // End of namespace Graphics
242 
243 #endif
Definition: managed_surface.h:51
Definition: macmenu.h:86
Definition: macresman.h:125
Definition: str.h:59
Definition: font.h:82
Definition: winexe_pe.h:48
TextAlign
Definition: font.h:48
bool draw(bool forceRedraw=false) override
Definition: macmenu.h:138
ManagedSurface * getBorderSurface() override
Definition: macmenu.h:99
Definition: rect.h:144
Definition: stream.h:745
Definition: macwindowmanager.h:148
Definition: macmenu.h:42
Definition: ustr.h:57
Definition: events.h:198
Definition: algorithm.h:29
Definition: formatinfo.h:28
void setVisible(bool visible, bool silent=false) override
Definition: macmenu.h:150
bool isDirty() override
Definition: macmenu.h:101
Definition: macmenu.h:94
size_type size() const
Definition: array.h:275
Definition: macmenu.h:59
Definition: macwindow.h:73
const Common::Rect & getInnerDimensions() override
Definition: macmenu.h:100