diff --git a/graphics/macgui/macmenu.cpp b/graphics/macgui/macmenu.cpp index c3369eed518..a9825fdb986 100644 --- a/graphics/macgui/macmenu.cpp +++ b/graphics/macgui/macmenu.cpp @@ -114,6 +114,8 @@ MacMenu::MacMenu(int id, const Common::Rect &bounds, MacWindowManager *wm) _activeItem = -1; _activeSubItem = -1; + _lastActiveItem = -1; + _lastActiveSubItem = -1; _ccallback = NULL; _unicodeccallback = NULL; @@ -685,7 +687,7 @@ void MacMenu::createSubMenuFromString(int id, const char *str, int commandId) { submenu = addSubMenu(nullptr, id); for (uint i = 0; i < string.size(); i++) { - while (i < string.size() && string[i] != ';') // Read token + 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 @@ -1455,6 +1457,10 @@ bool MacMenu::mouseRelease(int x, int y) { } } + // Set last active items and subitems before leaving! + _lastActiveItem = _activeItem; + _lastActiveSubItem = _activeSubItem; + // if the mode is not win95, or the click position is outside of the menu, then we close it if (!(_wm->_mode & kWMModeWin95) || !contains(x, y) || haveCallBack) closeMenu(); diff --git a/graphics/macgui/macmenu.h b/graphics/macgui/macmenu.h index 5603ef2ec8f..24c18430638 100644 --- a/graphics/macgui/macmenu.h +++ b/graphics/macgui/macmenu.h @@ -165,6 +165,8 @@ public: void setAction(MacMenuItem *menuItem, int actionId); int getAction(MacMenuItem *menuItem); + int getLastSelectedMenuItem() { return _lastActiveItem; }; + int getLastSelectedSubmenuItem() { return _lastActiveSubItem; }; protected: Common::Rect _bbox; @@ -175,6 +177,7 @@ protected: Common::Array _menustack; void renderSubmenu(MacMenuSubMenu *menu, bool recursive = true); + void calcSubMenuBounds(MacMenuSubMenu *menu, int x, int y); private: ManagedSurface _tempSurface; @@ -191,7 +194,6 @@ private: void processSubmenuTabs(MacMenuSubMenu *submenu); int calcSubMenuWidth(MacMenuSubMenu *menu); - void calcSubMenuBounds(MacMenuSubMenu *menu, int x, int y); bool keyEvent(Common::Event &event); bool mouseRelease(int x, int y); @@ -212,6 +214,9 @@ private: int _activeItem; int _activeSubItem; + int _lastActiveItem; + int _lastActiveSubItem; + void (*_ccallback)(int action, Common::String &text, void *data); void (*_unicodeccallback)(int action, Common::U32String &text, void *data); void *_cdata; diff --git a/graphics/macgui/macpopupmenu.cpp b/graphics/macgui/macpopupmenu.cpp new file mode 100644 index 00000000000..5ee790ff100 --- /dev/null +++ b/graphics/macgui/macpopupmenu.cpp @@ -0,0 +1,95 @@ +/* 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 . + * + */ + +#include "graphics/macgui/macpopupmenu.h" +#include "graphics/macgui/macwindowmanager.h" + +namespace Graphics { + +MacPopUp::MacPopUp(int id, const Common::Rect &bounds, MacWindowManager *wm, const char *string) : MacMenu(id, bounds, wm) { + _menuItemId = addMenuItem(nullptr, ""); + createSubMenuFromString(0, string, 0); + wm->addMenu(id, this); + _menuId = id; +} + +bool MacPopUp::draw(ManagedSurface *g, bool forceRedraw) { + + if (!_isVisible) + return false; + + if (_dimensionsDirty) + calcSubMenuBounds(_items[0]->submenu, _mouseX, _mouseY); + + if (!_contentIsDirty && !forceRedraw) + return false; + _contentIsDirty = false; + + _screen.clear(_wm->_colorGreen); + renderSubmenu(_items[0]->submenu, false); + + if (g) + g->transBlitFrom(_screen, _wm->_colorGreen); + + if (!(_wm->_mode & kWMModalMenuMode) && g) + g_system->copyRectToScreen(g->getPixels(), g->pitch, 0, 0, g->w, g->h); + + return true; +} + +uint32 MacPopUp::drawAndSelectMenu(int x, int y, int item) { + _mouseX = x; + _mouseY = y; + + // If menu is not active, then activate it! + if (!_active) + _wm->activateMenu(); + setActive(true); + + _contentIsDirty = true; // Set true to force refresh menu open changes + + // Push our submenu to stack + _menustack.clear(); + _menustack.push_back(_items[0]->submenu); + + // Display menu and update according to events + this->draw(_wm->_screen); + eventLoop(); + + // Close menu + closeMenu(); + + int activeSubItem = getLastSelectedSubmenuItem(); + if (activeSubItem == -1) + return item; + + // Return one indexed item! + return activeSubItem + 1; +} + +Common::String MacPopUp::getItemText(int item) { + // Convert 1-indexed item to 0 indexed + item = item - 1; + MacMenuItem *menu = getMenuItem(_menuItemId); + MacMenuItem *submenu = getSubMenuItem(menu, item); + return getName(submenu); +} +} // end of namespace Graphics diff --git a/graphics/macgui/macpopupmenu.h b/graphics/macgui/macpopupmenu.h new file mode 100644 index 00000000000..68ff4da18a6 --- /dev/null +++ b/graphics/macgui/macpopupmenu.h @@ -0,0 +1,48 @@ +/* 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 . + * + */ + +#ifndef GRAPHICS_MACGUI_MACPOPUPMENU_H +#define GRAPHICS_MACGUI_MACPOPUPMENU_H + +#include "graphics/macgui/macmenu.h" + +namespace Graphics { + +class MacMenu; +class MacWindowManager; + +class MacPopUp : public MacMenu { +public: + MacPopUp(int id, const Common::Rect &bounds, MacWindowManager *wm, const char *string); + uint32 drawAndSelectMenu(int x, int y, int item); + Common::String getItemText(int item); + + bool draw(ManagedSurface *g, bool forceRedraw = false) override; +private: + int _mouseX; + int _mouseY; + int _menuItemId; + int _menuId; + + bool mouseClicked(int x, int y); +}; +} // End of namespace Graphics +#endif diff --git a/graphics/module.mk b/graphics/module.mk index f8528e1c923..0d12dc92c88 100644 --- a/graphics/module.mk +++ b/graphics/module.mk @@ -27,6 +27,7 @@ MODULE_OBJS := \ macgui/macdialog.o \ macgui/macfontmanager.o \ macgui/macmenu.o \ + macgui/macpopupmenu.o \ macgui/mactext.o \ macgui/mactextwindow.o \ macgui/macwidget.o \