popupMenu.cpp
Engine/source/platform/menus/popupMenu.cpp
Classes:
class
Event class used to remove popup menus from the event notification in a safe way.
Public Variables
Public Functions
ConsoleDocClass(PopupMenu , "@brief <a href="/coding/class/classpopupmenu/">PopupMenu</a> represents a system <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">menu.\n\n</a>" "You can add menu items <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the menu, but there is no torque object associated " "with these menu items, they exist only in a platform specific <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">manner.\n\n</a>" " @note Internal use <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">only\n\n</a>" " @internal" )
DefineConsoleMethod(PopupMenu , checkItem , void , (S32 pos, bool checked) , "(pos, checked)" )
DefineConsoleMethod(PopupMenu , enableItem , void , (S32 pos, bool enabled) , "(pos, enabled)" )
DefineConsoleMethod(PopupMenu , getItemCount , S32 , () , "()" )
DefineConsoleMethod(PopupMenu , isItemChecked , bool , (S32 pos) , "(pos)" )
DefineConsoleMethod(PopupMenu , removeFromMenuBar , void , () , "()" )
DefineConsoleMethod(PopupMenu , removeItem , void , (S32 pos) , "(pos)" )
Detailed Description
Public Variables
U32 sMaxPopupGUID
Public Functions
ConsoleDocClass(PopupMenu , "@brief <a href="/coding/class/classpopupmenu/">PopupMenu</a> represents a system <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">menu.\n\n</a>" "You can add menu items <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the menu, but there is no torque object associated " "with these menu items, they exist only in a platform specific <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">manner.\n\n</a>" " @note Internal use <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">only\n\n</a>" " @internal" )
DefineConsoleMethod(PopupMenu , attachToMenuBar , void , (const char *canvasName, S32 pos, const char *title) , "(GuiCanvas, pos, title)" )
DefineConsoleMethod(PopupMenu , checkItem , void , (S32 pos, bool checked) , "(pos, checked)" )
DefineConsoleMethod(PopupMenu , checkRadioItem , void , (S32 firstPos, S32 lastPos, S32 checkPos) , "(firstPos, lastPos, checkPos)" )
DefineConsoleMethod(PopupMenu , enableItem , void , (S32 pos, bool enabled) , "(pos, enabled)" )
DefineConsoleMethod(PopupMenu , getItemCount , S32 , () , "()" )
DefineConsoleMethod(PopupMenu , insertItem , S32 , (S32 pos, const char *title, const char *accelerator, const char *cmd) , ("", "", "") , "(pos[, title][, accelerator][, cmd])" )
DefineConsoleMethod(PopupMenu , insertSubMenu , S32 , (S32 pos, String title, String subMenu) , "(pos, title, subMenu)" )
DefineConsoleMethod(PopupMenu , isItemChecked , bool , (S32 pos) , "(pos)" )
DefineConsoleMethod(PopupMenu , removeFromMenuBar , void , () , "()" )
DefineConsoleMethod(PopupMenu , removeItem , void , (S32 pos) , "(pos)" )
DefineConsoleMethod(PopupMenu , setItem , bool , (S32 pos, const char *title, const char *accelerator, const char *cmd) , ("") , "(pos, title[, accelerator][, cmd])" )
DefineConsoleMethod(PopupMenu , showPopup , void , (const char *canvasName, S32 x, S32 y) , (-1, -1) , "(Canvas,[x, y])" )
IMPLEMENT_CONOBJECT(PopupMenu )
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "platform/menus/popupMenu.h" 25#include "console/consoleTypes.h" 26#include "console/engineAPI.h" 27#include "gui/core/guiCanvas.h" 28#include "core/util/safeDelete.h" 29 30static U32 sMaxPopupGUID = 0; 31PopupMenuEvent PopupMenu::smPopupMenuEvent; 32bool PopupMenu::smSelectionEventHandled = false; 33 34/// Event class used to remove popup menus from the event notification in a safe way 35class PopUpNotifyRemoveEvent : public SimEvent 36{ 37public: 38 void process(SimObject *object) 39 { 40 PopupMenu::smPopupMenuEvent.remove((PopupMenu *)object, &PopupMenu::handleSelectEvent); 41 } 42}; 43 44//----------------------------------------------------------------------------- 45// Constructor/Destructor 46//----------------------------------------------------------------------------- 47 48PopupMenu::PopupMenu() : mCanvas(NULL) 49{ 50 createPlatformPopupMenuData(); 51 52 mSubmenus = new SimSet; 53 mSubmenus->registerObject(); 54 55 mBarTitle = StringTable->EmptyString(); 56 mIsPopup = false; 57 58 mPopupGUID = sMaxPopupGUID++; 59} 60 61PopupMenu::~PopupMenu() 62{ 63 // This searches the menu bar so is safe to call for menus 64 // that aren't on it, since nothing will happen. 65 removeFromMenuBar(); 66 67 SimSet::iterator i; 68 while((i = mSubmenus->begin()) != mSubmenus->end()) 69 { 70 (*i)->deleteObject(); 71 } 72 73 mSubmenus->deleteObject(); 74 deletePlatformPopupMenuData(); 75 76 PopupMenu::smPopupMenuEvent.remove(this, &PopupMenu::handleSelectEvent); 77} 78 79IMPLEMENT_CONOBJECT(PopupMenu); 80 81ConsoleDocClass( PopupMenu, 82 "@brief PopupMenu represents a system menu.\n\n" 83 "You can add menu items to the menu, but there is no torque object associated " 84 "with these menu items, they exist only in a platform specific manner.\n\n" 85 "@note Internal use only\n\n" 86 "@internal" 87); 88 89 90//----------------------------------------------------------------------------- 91 92void PopupMenu::initPersistFields() 93{ 94 addField("isPopup", TypeBool, Offset(mIsPopup, PopupMenu), "true if this is a pop-up/context menu. defaults to false."); 95 addField("barTitle", TypeCaseString, Offset(mBarTitle, PopupMenu), "the title of this menu when attached to a menu bar"); 96 97 Parent::initPersistFields(); 98} 99 100//----------------------------------------------------------------------------- 101 102bool PopupMenu::onAdd() 103{ 104 if(! Parent::onAdd()) 105 return false; 106 107 createPlatformMenu(); 108 109 Con::executef(this, "onAdd"); 110 return true; 111} 112 113void PopupMenu::onRemove() 114{ 115 Con::executef(this, "onRemove"); 116 117 Parent::onRemove(); 118} 119 120//----------------------------------------------------------------------------- 121 122void PopupMenu::onMenuSelect() 123{ 124 Con::executef(this, "onMenuSelect"); 125} 126 127//----------------------------------------------------------------------------- 128 129void PopupMenu::handleSelectEvent(U32 popID, U32 command) 130{ 131 if (popID == mPopupGUID && canHandleID(command)) 132 if (handleSelect(command)) 133 smSelectionEventHandled = true; 134} 135 136//----------------------------------------------------------------------------- 137 138void PopupMenu::onAttachToMenuBar(GuiCanvas *canvas, S32 pos, const char *title) 139{ 140 mCanvas = canvas; 141 142 // Attached menus must be notified of menu events 143 smPopupMenuEvent.notify(this, &PopupMenu::handleSelectEvent); 144 145 // Pass on to sub menus 146 for(SimSet::iterator i = mSubmenus->begin();i != mSubmenus->end();++i) 147 { 148 PopupMenu *mnu = dynamic_cast<PopupMenu *>(*i); 149 if(mnu == NULL) 150 continue; 151 152 mnu->onAttachToMenuBar(canvas, pos, title); 153 } 154 155 // Call script 156 if(isProperlyAdded()) 157 Con::executef(this, "onAttachToMenuBar", Con::getIntArg(canvas ? canvas->getId() : 0), Con::getIntArg(pos), title); 158} 159 160void PopupMenu::onRemoveFromMenuBar(GuiCanvas *canvas) 161{ 162 mCanvas = NULL; 163 164 // We are no longer interested in select events, remove ourselves from the notification list in a safe way 165 Sim::postCurrentEvent(this, new PopUpNotifyRemoveEvent()); 166 167 // Pass on to sub menus 168 for(SimSet::iterator i = mSubmenus->begin();i != mSubmenus->end();++i) 169 { 170 PopupMenu *mnu = dynamic_cast<PopupMenu *>(*i); 171 if(mnu == NULL) 172 continue; 173 174 mnu->onRemoveFromMenuBar(canvas); 175 } 176 177 // Call script 178 if(isProperlyAdded()) 179 Con::executef(this, "onRemoveFromMenuBar", Con::getIntArg(canvas ? canvas->getId() : 0)); 180} 181 182//----------------------------------------------------------------------------- 183 184bool PopupMenu::onMessageReceived(StringTableEntry queue, const char* event, const char* data) 185{ 186 return Con::executef(this, "onMessageReceived", queue, event, data); 187} 188 189 190bool PopupMenu::onMessageObjectReceived(StringTableEntry queue, Message *msg ) 191{ 192 return Con::executef(this, "onMessageReceived", queue, Con::getIntArg(msg->getId())); 193} 194 195//----------------------------------------------------------------------------- 196// Console Methods 197//----------------------------------------------------------------------------- 198 199DefineConsoleMethod(PopupMenu, insertItem, S32, (S32 pos, const char * title, const char * accelerator, const char* cmd), ("", "", ""), "(pos[, title][, accelerator][, cmd])") 200{ 201 return object->insertItem(pos, title, accelerator, cmd); 202} 203 204DefineConsoleMethod(PopupMenu, removeItem, void, (S32 pos), , "(pos)") 205{ 206 object->removeItem(pos); 207} 208 209DefineConsoleMethod(PopupMenu, insertSubMenu, S32, (S32 pos, String title, String subMenu), , "(pos, title, subMenu)") 210{ 211 PopupMenu *mnu = dynamic_cast<PopupMenu *>(Sim::findObject(subMenu)); 212 if(mnu == NULL) 213 { 214 Con::errorf("PopupMenu::insertSubMenu - Invalid PopupMenu object specified for submenu"); 215 return -1; 216 } 217 return object->insertSubMenu(pos, title, mnu); 218} 219 220DefineConsoleMethod(PopupMenu, setItem, bool, (S32 pos, const char * title, const char * accelerator, const char *cmd), (""), "(pos, title[, accelerator][, cmd])") 221{ 222 return object->setItem(pos, title, accelerator, cmd); 223} 224 225//----------------------------------------------------------------------------- 226 227DefineConsoleMethod(PopupMenu, enableItem, void, (S32 pos, bool enabled), , "(pos, enabled)") 228{ 229 object->enableItem(pos, enabled); 230} 231 232DefineConsoleMethod(PopupMenu, checkItem, void, (S32 pos, bool checked), , "(pos, checked)") 233{ 234 object->checkItem(pos, checked); 235} 236 237DefineConsoleMethod(PopupMenu, checkRadioItem, void, (S32 firstPos, S32 lastPos, S32 checkPos), , "(firstPos, lastPos, checkPos)") 238{ 239 object->checkRadioItem(firstPos, lastPos, checkPos); 240} 241 242DefineConsoleMethod(PopupMenu, isItemChecked, bool, (S32 pos), , "(pos)") 243{ 244 return object->isItemChecked(pos); 245} 246 247DefineConsoleMethod(PopupMenu, getItemCount, S32, (), , "()") 248{ 249 return object->getItemCount(); 250} 251 252//----------------------------------------------------------------------------- 253 254DefineConsoleMethod(PopupMenu, attachToMenuBar, void, (const char * canvasName, S32 pos, const char * title), , "(GuiCanvas, pos, title)") 255{ 256 object->attachToMenuBar(dynamic_cast<GuiCanvas*>(Sim::findObject(canvasName)), pos, title); 257} 258 259DefineConsoleMethod(PopupMenu, removeFromMenuBar, void, (), , "()") 260{ 261 object->removeFromMenuBar(); 262} 263 264//----------------------------------------------------------------------------- 265 266DefineConsoleMethod(PopupMenu, showPopup, void, (const char * canvasName, S32 x, S32 y), ( -1, -1), "(Canvas,[x, y])") 267{ 268 GuiCanvas *pCanvas = dynamic_cast<GuiCanvas*>(Sim::findObject(canvasName)); 269 object->showPopup(pCanvas, x, y); 270} 271
