popupMenuWin32.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/menus/popupMenu.h"
  23. #include "platformWin32/platformWin32.h"
  24. #include "console/consoleTypes.h"
  25. #include "gui/core/guiCanvas.h"
  26. #include "windowManager/platformWindowMgr.h"
  27. #include "windowManager/win32/win32Window.h"
  28. #include "core/util/safeDelete.h"
  29. #include "sim/actionMap.h"
  30. #include "platform/platformInput.h"
  31. //////////////////////////////////////////////////////////////////////////
  32. // Platform Menu Data
  33. //////////////////////////////////////////////////////////////////////////
  34. struct PlatformPopupMenuData
  35. {
  36. static U32 mLastPopupMenuID;
  37. static const U32 PopupMenuIDRange;
  38. HMENU mMenu;
  39. U32 mMenuID;
  40. U32 mLastID;
  41. Win32Window::AcceleratorList mAccelerators;
  42. Win32Window::AcceleratorList mDisabledAccelerators;
  43. PlatformPopupMenuData()
  44. {
  45. mMenu = NULL;
  46. mMenuID = mLastPopupMenuID++;
  47. mLastID = 0;
  48. }
  49. ~PlatformPopupMenuData()
  50. {
  51. if(mMenu)
  52. DestroyMenu(mMenu);
  53. }
  54. void insertAccelerator(EventDescriptor &desc, U32 id);
  55. void removeAccelerator(U32 id);
  56. void setAccelleratorEnabled(U32 id, bool enabled);
  57. };
  58. U32 PlatformPopupMenuData::mLastPopupMenuID = 0;
  59. const U32 PlatformPopupMenuData::PopupMenuIDRange = 100;
  60. //////////////////////////////////////////////////////////////////////////
  61. void PlatformPopupMenuData::insertAccelerator(EventDescriptor &desc, U32 id)
  62. {
  63. if(desc.eventType != SI_KEY)
  64. return;
  65. Win32Window::AcceleratorList::iterator i;
  66. for(i = mAccelerators.begin();i != mAccelerators.end();++i)
  67. {
  68. if(i->mID == id)
  69. {
  70. // Update existing entry
  71. i->mDescriptor.eventType = desc.eventType;
  72. i->mDescriptor.eventCode = desc.eventCode;
  73. i->mDescriptor.flags = desc.flags;
  74. return;
  75. }
  76. if(i->mDescriptor.eventType == desc.eventType && i->mDescriptor.eventCode == desc.eventCode && i->mDescriptor.flags == desc.flags)
  77. {
  78. // Already have a matching accelerator, don't add another one
  79. return;
  80. }
  81. }
  82. Win32Window::Accelerator accel;
  83. accel.mDescriptor = desc;
  84. accel.mID = id;
  85. mAccelerators.push_back(accel);
  86. }
  87. void PlatformPopupMenuData::removeAccelerator(U32 id)
  88. {
  89. Win32Window::AcceleratorList::iterator i;
  90. for(i = mAccelerators.begin();i != mAccelerators.end();++i)
  91. {
  92. if(i->mID == id)
  93. {
  94. mAccelerators.erase(i);
  95. return;
  96. }
  97. }
  98. }
  99. void PlatformPopupMenuData::setAccelleratorEnabled( U32 id, bool enabled )
  100. {
  101. Win32Window::AcceleratorList *src = NULL;
  102. Win32Window::AcceleratorList *dst = NULL;
  103. if ( enabled )
  104. {
  105. src = &mDisabledAccelerators;
  106. dst = &mAccelerators;
  107. }
  108. else
  109. {
  110. src = &mAccelerators;
  111. dst = &mDisabledAccelerators;
  112. }
  113. Win32Window::AcceleratorList::iterator i;
  114. for ( i = src->begin(); i != src->end(); ++i )
  115. {
  116. if ( i->mID == id )
  117. {
  118. Win32Window::Accelerator tmp = *i;
  119. src->erase( i );
  120. dst->push_back( tmp );
  121. return;
  122. }
  123. }
  124. }
  125. //////////////////////////////////////////////////////////////////////////
  126. void PopupMenu::createPlatformPopupMenuData()
  127. {
  128. mData = new PlatformPopupMenuData;
  129. }
  130. void PopupMenu::deletePlatformPopupMenuData()
  131. {
  132. SAFE_DELETE(mData);
  133. }
  134. void PopupMenu::createPlatformMenu()
  135. {
  136. mData->mMenu = mIsPopup ? CreatePopupMenu() : CreateMenu();
  137. AssertFatal(mData->mMenu, "Unable to create menu");
  138. MENUINFO mi;
  139. mi.cbSize = sizeof(mi);
  140. mi.fMask = MIM_MENUDATA;
  141. mi.dwMenuData = (ULONG_PTR)this;
  142. SetMenuInfo(mData->mMenu, &mi);
  143. }
  144. //////////////////////////////////////////////////////////////////////////
  145. // Public Methods
  146. //////////////////////////////////////////////////////////////////////////
  147. S32 PopupMenu::insertItem(S32 pos, const char *title, const char* accelerator)
  148. {
  149. Win32Window *pWindow = mCanvas ? dynamic_cast<Win32Window*>(mCanvas->getPlatformWindow()) : NULL;
  150. bool isAttached = isAttachedToMenuBar();
  151. if(isAttached && pWindow == NULL)
  152. return -1;
  153. MENUITEMINFOA mi;
  154. mi.cbSize = sizeof(mi);
  155. mi.fMask = MIIM_ID|MIIM_TYPE;
  156. mi.wID = (mData->mMenuID * PlatformPopupMenuData::PopupMenuIDRange) + mData->mLastID + 1;
  157. mData->mLastID++;
  158. if(title && *title)
  159. mi.fType = MFT_STRING;
  160. else
  161. mi.fType = MFT_SEPARATOR;
  162. char buf[1024];
  163. if(accelerator && *accelerator)
  164. {
  165. dSprintf(buf, sizeof(buf), "%s\t%s", title, accelerator);
  166. if(isAttached)
  167. pWindow->removeAccelerators(mData->mAccelerators);
  168. // Build entry for accelerator table
  169. EventDescriptor accelDesc;
  170. if(ActionMap::createEventDescriptor(accelerator, &accelDesc))
  171. mData->insertAccelerator(accelDesc, mi.wID);
  172. else
  173. Con::errorf("PopupMenu::insertItem - Could not create event descriptor for accelerator \"%s\"", accelerator);
  174. if(isAttached)
  175. pWindow->addAccelerators(mData->mAccelerators);
  176. }
  177. else
  178. dSprintf(buf, sizeof(buf), "%s", title);
  179. mi.dwTypeData = (LPSTR)buf;
  180. if(InsertMenuItemA(mData->mMenu, pos, TRUE, &mi))
  181. {
  182. if(isAttached)
  183. {
  184. HWND hWindow = pWindow->getHWND();
  185. DrawMenuBar(hWindow);
  186. }
  187. return mi.wID;
  188. }
  189. return -1;
  190. }
  191. S32 PopupMenu::insertSubMenu(S32 pos, const char *title, PopupMenu *submenu)
  192. {
  193. Win32Window *pWindow = mCanvas ? dynamic_cast<Win32Window*>(mCanvas->getPlatformWindow()) : NULL;
  194. bool isAttached = isAttachedToMenuBar();
  195. if(isAttached && pWindow == NULL)
  196. return -1;
  197. for(S32 i = 0;i < mSubmenus->size();i++)
  198. {
  199. if(submenu == (*mSubmenus)[i])
  200. {
  201. Con::errorf("PopupMenu::insertSubMenu - Attempting to add submenu twice");
  202. return -1;
  203. }
  204. }
  205. MENUITEMINFOA mi;
  206. mi.cbSize = sizeof(mi);
  207. mi.fMask = MIIM_ID|MIIM_TYPE|MIIM_SUBMENU|MIIM_DATA;
  208. mi.wID = (mData->mMenuID * PlatformPopupMenuData::PopupMenuIDRange) + mData->mLastID + 1;
  209. if(title && *title)
  210. mi.fType = MFT_STRING;
  211. else
  212. mi.fType = MFT_SEPARATOR;
  213. mi.dwTypeData = (LPSTR)title;
  214. mi.hSubMenu = submenu->mData->mMenu;
  215. mi.dwItemData = (ULONG_PTR)submenu;
  216. if(InsertMenuItemA(mData->mMenu, pos, TRUE, &mi))
  217. {
  218. mSubmenus->addObject(submenu);
  219. if(isAttached)
  220. {
  221. pWindow->addAccelerators(submenu->mData->mAccelerators);
  222. HWND hWindow = pWindow->getHWND();
  223. DrawMenuBar(hWindow);
  224. }
  225. return mi.wID;
  226. }
  227. return -1;
  228. }
  229. bool PopupMenu::setItem(S32 pos, const char *title, const char* accelerator)
  230. {
  231. Win32Window *pWindow = mCanvas ? dynamic_cast<Win32Window*>(mCanvas->getPlatformWindow()) : NULL;
  232. bool isAttached = isAttachedToMenuBar();
  233. if(isAttached && pWindow == NULL)
  234. return false;
  235. // Are we out of range?
  236. if ( pos >= getItemCount() )
  237. return false;
  238. MENUITEMINFOA mi;
  239. mi.cbSize = sizeof(mi);
  240. mi.fMask = MIIM_TYPE;
  241. if(title && *title)
  242. mi.fType = MFT_STRING;
  243. else
  244. mi.fType = MFT_SEPARATOR;
  245. char buf[1024];
  246. if(accelerator && *accelerator)
  247. {
  248. dSprintf(buf, sizeof(buf), "%s\t%s", title, accelerator);
  249. if(isAttached)
  250. pWindow->removeAccelerators(mData->mAccelerators);
  251. // Build entry for accelerator table
  252. EventDescriptor accelDesc;
  253. if(ActionMap::createEventDescriptor(accelerator, &accelDesc))
  254. mData->insertAccelerator(accelDesc, pos);
  255. else
  256. Con::errorf("PopupMenu::setItem - Could not create event descriptor for accelerator \"%s\"", accelerator);
  257. if(isAttached)
  258. pWindow->addAccelerators(mData->mAccelerators);
  259. }
  260. else
  261. dSprintf(buf, sizeof(buf), "%s", title);
  262. mi.dwTypeData = (LPSTR)buf;
  263. if(SetMenuItemInfoA(mData->mMenu, pos, TRUE, &mi))
  264. {
  265. if(isAttached)
  266. {
  267. HWND hWindow = pWindow->getHWND();
  268. DrawMenuBar(hWindow);
  269. }
  270. return true;
  271. }
  272. return false;
  273. }
  274. void PopupMenu::removeItem(S32 itemPos)
  275. {
  276. Win32Window *pWindow = mCanvas ? dynamic_cast<Win32Window*>(mCanvas->getPlatformWindow()) : NULL;
  277. bool isAttached = isAttachedToMenuBar();
  278. if(isAttached && pWindow == NULL)
  279. return;
  280. MENUITEMINFOA mi;
  281. mi.cbSize = sizeof(mi);
  282. mi.fMask = MIIM_DATA|MIIM_ID;
  283. if(GetMenuItemInfoA(mData->mMenu, itemPos, TRUE, &mi))
  284. {
  285. bool submenu = false;
  286. // Update list of submenus if this is a submenu
  287. if(mi.fMask & MIIM_DATA)
  288. {
  289. PopupMenu *mnu = (PopupMenu *)mi.dwItemData;
  290. if( mnu != NULL )
  291. {
  292. if(isAttached)
  293. pWindow->removeAccelerators(mnu->mData->mAccelerators);
  294. mSubmenus->removeObject(mnu);
  295. submenu = true;
  296. }
  297. }
  298. if(! submenu)
  299. {
  300. // Update accelerators if this has an accelerator and wasn't a sub menu
  301. for(S32 i = 0;i < mData->mAccelerators.size();++i)
  302. {
  303. if(mData->mAccelerators[i].mID == mi.wID)
  304. {
  305. if(isAttached)
  306. pWindow->removeAccelerators(mData->mAccelerators);
  307. mData->mAccelerators.erase(i);
  308. if(isAttached)
  309. pWindow->addAccelerators(mData->mAccelerators);
  310. break;
  311. }
  312. }
  313. }
  314. }
  315. else
  316. return;
  317. RemoveMenu(mData->mMenu, itemPos, MF_BYPOSITION);
  318. if(isAttached)
  319. {
  320. HWND hWindow = pWindow->getHWND();
  321. DrawMenuBar(hWindow);
  322. }
  323. }
  324. //////////////////////////////////////////////////////////////////////////
  325. void PopupMenu::enableItem( S32 pos, bool enable )
  326. {
  327. U32 flags = enable ? MF_ENABLED : MF_GRAYED;
  328. EnableMenuItem( mData->mMenu, pos, MF_BYPOSITION|flags );
  329. // Update accelerators.
  330. // NOTE: This really DOES need to happen. A disabled menu item
  331. // should not still have an accelerator mapped to it.
  332. //
  333. // Unfortunately, the editors currently only set menu items
  334. // enabled/disabled when the menu itself is selected which means our
  335. // accelerators would be out of synch.
  336. /*
  337. Win32Window *pWindow = mCanvas ? dynamic_cast<Win32Window*>( mCanvas->getPlatformWindow() ) : NULL;
  338. bool isAttached = isAttachedToMenuBar();
  339. if ( isAttached && pWindow == NULL )
  340. return;
  341. MENUITEMINFOA mi;
  342. mi.cbSize = sizeof(mi);
  343. mi.fMask = MIIM_DATA|MIIM_ID;
  344. if ( !GetMenuItemInfoA( mData->mMenu, pos, TRUE, &mi) )
  345. return;
  346. if ( isAttached )
  347. pWindow->removeAccelerators( mData->mAccelerators );
  348. mData->setAccelleratorEnabled( mi.wID, enable );
  349. if ( isAttached )
  350. pWindow->addAccelerators( mData->mAccelerators );
  351. */
  352. }
  353. void PopupMenu::checkItem(S32 pos, bool checked)
  354. {
  355. // U32 flags = checked ? MF_CHECKED : MF_UNCHECKED;
  356. // CheckMenuItem(mData->mMenu, pos, MF_BYPOSITION|flags);
  357. MENUITEMINFOA mi;
  358. mi.cbSize = sizeof(mi);
  359. mi.fMask = MIIM_STATE;
  360. mi.fState = checked ? MFS_CHECKED : MFS_UNCHECKED;
  361. SetMenuItemInfoA(mData->mMenu, pos, TRUE, &mi);
  362. }
  363. void PopupMenu::checkRadioItem(S32 firstPos, S32 lastPos, S32 checkPos)
  364. {
  365. CheckMenuRadioItem(mData->mMenu, firstPos, lastPos, checkPos, MF_BYPOSITION);
  366. }
  367. bool PopupMenu::isItemChecked(S32 pos)
  368. {
  369. MENUITEMINFOA mi;
  370. mi.cbSize = sizeof(mi);
  371. mi.fMask = MIIM_STATE;
  372. if(GetMenuItemInfoA(mData->mMenu, pos, TRUE, &mi) && (mi.fState & MFS_CHECKED))
  373. return true;
  374. return false;
  375. }
  376. U32 PopupMenu::getItemCount()
  377. {
  378. return GetMenuItemCount( mData->mMenu );
  379. }
  380. //////////////////////////////////////////////////////////////////////////
  381. bool PopupMenu::canHandleID(U32 id)
  382. {
  383. for(S32 i = 0;i < mSubmenus->size();i++)
  384. {
  385. PopupMenu *subM = dynamic_cast<PopupMenu *>((*mSubmenus)[i]);
  386. if(subM == NULL)
  387. continue;
  388. if(subM->canHandleID(id))
  389. return true;
  390. }
  391. if(id >= mData->mMenuID * PlatformPopupMenuData::PopupMenuIDRange &&
  392. id < (mData->mMenuID+1) * PlatformPopupMenuData::PopupMenuIDRange)
  393. {
  394. return true;
  395. }
  396. return false;
  397. }
  398. bool PopupMenu::handleSelect(U32 command, const char *text /* = NULL */)
  399. {
  400. // [tom, 8/20/2006] Pass off to a sub menu if it's for them
  401. for(S32 i = 0;i < mSubmenus->size();i++)
  402. {
  403. PopupMenu *subM = dynamic_cast<PopupMenu *>((*mSubmenus)[i]);
  404. if(subM == NULL)
  405. continue;
  406. if(subM->canHandleID(command))
  407. {
  408. return subM->handleSelect(command, text);
  409. }
  410. }
  411. // [tom, 8/21/2006] Cheesey hack to find the position based on ID
  412. char buf[512];
  413. MENUITEMINFOA mi;
  414. mi.cbSize = sizeof(mi);
  415. mi.dwTypeData = NULL;
  416. S32 numItems = GetMenuItemCount(mData->mMenu);
  417. S32 pos = -1;
  418. for(S32 i = 0;i < numItems;i++)
  419. {
  420. mi.fMask = MIIM_ID|MIIM_STRING|MIIM_STATE;
  421. if(GetMenuItemInfoA(mData->mMenu, i, TRUE, &mi))
  422. {
  423. if(mi.wID == command)
  424. {
  425. if(text == NULL)
  426. {
  427. mi.dwTypeData = buf;
  428. mi.cch++;
  429. GetMenuItemInfoA(mData->mMenu, i, TRUE, &mi);
  430. // [tom, 5/11/2007] Don't do anything if the menu item is disabled
  431. if(mi.fState & MFS_DISABLED)
  432. return false;
  433. text = StringTable->insert(mi.dwTypeData);
  434. }
  435. pos = i;
  436. break;
  437. }
  438. }
  439. }
  440. if(pos == -1)
  441. {
  442. Con::errorf("PopupMenu::handleSelect - Could not find menu item position for ID %d ... this shouldn't happen!", command);
  443. return false;
  444. }
  445. // [tom, 8/20/2006] Wasn't handled by a submenu, pass off to script
  446. return dAtob(Con::executef(this, "onSelectItem", Con::getIntArg(pos), text ? text : ""));
  447. }
  448. //////////////////////////////////////////////////////////////////////////
  449. void PopupMenu::showPopup(GuiCanvas *owner, S32 x /* = -1 */, S32 y /* = -1 */)
  450. {
  451. if( owner == NULL )
  452. {
  453. Con::warnf("PopupMenu::showPopup - Invalid canvas supplied!");
  454. return;
  455. }
  456. // [tom, 6/4/2007] showPopup() blocks until the menu is closed by the user,
  457. // so the canvas pointer is not needed beyond the scope of this function
  458. // when working with context menus. Setting mCanvas here will cause undesired
  459. // behavior in relation to the menu bar.
  460. Win32Window *pWindow = dynamic_cast<Win32Window*>(owner->getPlatformWindow());
  461. if(pWindow == NULL)
  462. return;
  463. HWND hWindow = pWindow->getHWND();
  464. POINT p;
  465. if(x == -1 && y == -1)
  466. GetCursorPos(&p);
  467. else
  468. {
  469. p.x = x;
  470. p.y = y;
  471. ClientToScreen(hWindow, &p);
  472. }
  473. winState.renderThreadBlocked = true;
  474. U32 opt = (int)TrackPopupMenu(mData->mMenu, TPM_NONOTIFY|TPM_RETURNCMD, p.x, p.y, 0, hWindow, NULL);
  475. if(opt > 0)
  476. handleSelect(opt, NULL);
  477. winState.renderThreadBlocked = false;
  478. }
  479. //////////////////////////////////////////////////////////////////////////
  480. void PopupMenu::attachToMenuBar(GuiCanvas *owner, S32 pos, const char *title)
  481. {
  482. if(owner == NULL || isAttachedToMenuBar())
  483. return;
  484. // This is set for sub-menus in the onAttachToMenuBar() callback
  485. mCanvas = owner;
  486. Win32Window *pWindow = dynamic_cast<Win32Window*>(owner->getPlatformWindow());
  487. if(pWindow == NULL)
  488. return;
  489. HMENU hWindowMenu = pWindow->getMenuHandle();
  490. if(hWindowMenu == NULL)
  491. {
  492. hWindowMenu = CreateMenu();
  493. if(hWindowMenu)
  494. {
  495. pWindow->setMenuHandle( hWindowMenu);
  496. }
  497. }
  498. MENUITEMINFOA mii;
  499. mii.cbSize = sizeof(MENUITEMINFOA);
  500. mii.fMask = MIIM_STRING|MIIM_DATA;
  501. mii.dwTypeData = (LPSTR)title;
  502. mii.fMask |= MIIM_ID;
  503. mii.wID = mData->mMenuID;
  504. mii.fMask |= MIIM_SUBMENU;
  505. mii.hSubMenu = mData->mMenu;
  506. mii.dwItemData = (ULONG_PTR)this;
  507. InsertMenuItemA(hWindowMenu, pos, TRUE, &mii);
  508. HWND hWindow = pWindow->getHWND();
  509. DrawMenuBar(hWindow);
  510. pWindow->addAccelerators(mData->mAccelerators);
  511. // Add accelerators for sub menus
  512. for(SimSet::iterator i = mSubmenus->begin();i != mSubmenus->end();++i)
  513. {
  514. PopupMenu *submenu = dynamic_cast<PopupMenu *>(*i);
  515. if(submenu == NULL)
  516. continue;
  517. pWindow->addAccelerators(submenu->mData->mAccelerators);
  518. }
  519. onAttachToMenuBar(owner, pos, title);
  520. }
  521. // New version of above for use by MenuBar class. Do not use yet.
  522. void PopupMenu::attachToMenuBar(GuiCanvas *owner, S32 pos)
  523. {
  524. Win32Window *pWindow = dynamic_cast<Win32Window*>(owner->getPlatformWindow());
  525. if(pWindow == NULL)
  526. return;
  527. //When playing a journal, the system menu is not actually shown
  528. if (Journal::IsPlaying())
  529. {
  530. onAttachToMenuBar(owner, pos, mBarTitle);
  531. return;
  532. }
  533. HMENU hWindowMenu = pWindow->getMenuHandle();
  534. MENUITEMINFOA mii;
  535. mii.cbSize = sizeof(MENUITEMINFOA);
  536. mii.fMask = MIIM_STRING|MIIM_DATA;
  537. mii.dwTypeData = (LPSTR)mBarTitle;
  538. mii.fMask |= MIIM_ID;
  539. mii.wID = mData->mMenuID;
  540. mii.fMask |= MIIM_SUBMENU;
  541. mii.hSubMenu = mData->mMenu;
  542. mii.dwItemData = (ULONG_PTR)this;
  543. InsertMenuItemA(hWindowMenu, pos, TRUE, &mii);
  544. pWindow->addAccelerators(mData->mAccelerators);
  545. // Add accelerators for sub menus (have to do this here as it's platform specific)
  546. for(SimSet::iterator i = mSubmenus->begin();i != mSubmenus->end();++i)
  547. {
  548. PopupMenu *submenu = dynamic_cast<PopupMenu *>(*i);
  549. if(submenu == NULL)
  550. continue;
  551. pWindow->addAccelerators(submenu->mData->mAccelerators);
  552. }
  553. onAttachToMenuBar(owner, pos, mBarTitle);
  554. }
  555. void PopupMenu::removeFromMenuBar()
  556. {
  557. S32 pos = getPosOnMenuBar();
  558. if(pos == -1)
  559. return;
  560. Win32Window *pWindow = mCanvas ? dynamic_cast<Win32Window*>(mCanvas->getPlatformWindow()) : NULL;
  561. if(pWindow == NULL)
  562. return;
  563. HMENU hMenuHandle = pWindow->getMenuHandle();
  564. if(!hMenuHandle)
  565. return;
  566. RemoveMenu(hMenuHandle, pos, MF_BYPOSITION);
  567. HWND hWindow = pWindow->getHWND();
  568. DrawMenuBar(hWindow);
  569. pWindow->removeAccelerators(mData->mAccelerators);
  570. // Remove accelerators for sub menus
  571. for(SimSet::iterator i = mSubmenus->begin();i != mSubmenus->end();++i)
  572. {
  573. PopupMenu *submenu = dynamic_cast<PopupMenu *>(*i);
  574. if(submenu == NULL)
  575. continue;
  576. pWindow->removeAccelerators(submenu->mData->mAccelerators);
  577. }
  578. onRemoveFromMenuBar(mCanvas);
  579. }
  580. S32 PopupMenu::getPosOnMenuBar()
  581. {
  582. if(mCanvas == NULL)
  583. return -1;
  584. Win32Window *pWindow = mCanvas ? dynamic_cast<Win32Window*>(mCanvas->getPlatformWindow()) : NULL;
  585. if(pWindow == NULL)
  586. return -1;
  587. HMENU hMenuHandle = pWindow->getMenuHandle();
  588. S32 numItems = GetMenuItemCount(hMenuHandle);
  589. S32 pos = -1;
  590. for(S32 i = 0;i < numItems;i++)
  591. {
  592. MENUITEMINFOA mi;
  593. mi.cbSize = sizeof(mi);
  594. mi.fMask = MIIM_DATA;
  595. if(GetMenuItemInfoA(hMenuHandle, i, TRUE, &mi))
  596. {
  597. if(mi.fMask & MIIM_DATA)
  598. {
  599. PopupMenu *mnu = (PopupMenu *)mi.dwItemData;
  600. if(mnu == this)
  601. {
  602. pos = i;
  603. break;
  604. }
  605. }
  606. }
  607. }
  608. return pos;
  609. }