popupMenuWin32.cpp 20 KB

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