Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_PopupMenu.cpp 61KB

9 years ago
8 years ago
8 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
8 years ago
8 years ago
8 years ago
9 years ago
9 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. //==============================================================================
  18. namespace PopupMenuSettings
  19. {
  20. const int scrollZone = 24;
  21. const int borderSize = 2;
  22. const int timerInterval = 50;
  23. const int dismissCommandId = 0x6287345f;
  24. const int sectionHeaderID = 0x4734a34f;
  25. static bool menuWasHiddenBecauseOfAppChange = false;
  26. }
  27. class PopupMenu::Item
  28. {
  29. public:
  30. Item() : itemID (0), isActive (true), isSeparator (true), isTicked (false),
  31. usesColour (false), commandManager (nullptr)
  32. {}
  33. Item (const int itemId,
  34. const String& name,
  35. const bool active,
  36. const bool ticked,
  37. Drawable* drawable,
  38. const Colour colour,
  39. const bool useColour,
  40. CustomComponent* const custom,
  41. const PopupMenu* const sub,
  42. ApplicationCommandManager* const manager)
  43. : itemID (itemId), text (name), textColour (colour),
  44. isActive (active), isSeparator (false), isTicked (ticked),
  45. usesColour (useColour), iconDrawable (drawable),
  46. customComp (custom), subMenu (createCopyIfNotNull (sub)), commandManager (manager)
  47. {
  48. if (commandManager != nullptr && itemID != 0)
  49. {
  50. String shortcutKey;
  51. const Array<KeyPress> keyPresses (commandManager->getKeyMappings()
  52. ->getKeyPressesAssignedToCommand (itemID));
  53. for (int i = 0; i < keyPresses.size(); ++i)
  54. {
  55. const String key (keyPresses.getReference(i).getTextDescriptionWithIcons());
  56. if (shortcutKey.isNotEmpty())
  57. shortcutKey << ", ";
  58. if (key.length() == 1 && key[0] < 128)
  59. shortcutKey << "shortcut: '" << key << '\'';
  60. else
  61. shortcutKey << key;
  62. }
  63. shortcutKey = shortcutKey.trim();
  64. if (shortcutKey.isNotEmpty())
  65. text << "<end>" << shortcutKey;
  66. }
  67. }
  68. Item (const Item& other)
  69. : itemID (other.itemID),
  70. text (other.text),
  71. textColour (other.textColour),
  72. isActive (other.isActive),
  73. isSeparator (other.isSeparator),
  74. isTicked (other.isTicked),
  75. usesColour (other.usesColour),
  76. iconDrawable (other.iconDrawable != nullptr ? other.iconDrawable->createCopy() : nullptr),
  77. customComp (other.customComp),
  78. subMenu (createCopyIfNotNull (other.subMenu.get())),
  79. commandManager (other.commandManager)
  80. {}
  81. bool canBeTriggered() const noexcept { return isActive && itemID != 0 && itemID != PopupMenuSettings::sectionHeaderID; }
  82. bool hasActiveSubMenu() const noexcept { return isActive && subMenu != nullptr && subMenu->items.size() > 0; }
  83. //==============================================================================
  84. const int itemID;
  85. String text;
  86. const Colour textColour;
  87. const bool isActive, isSeparator, isTicked, usesColour;
  88. ScopedPointer<Drawable> iconDrawable;
  89. ReferenceCountedObjectPtr<CustomComponent> customComp;
  90. ScopedPointer<PopupMenu> subMenu;
  91. ApplicationCommandManager* const commandManager;
  92. private:
  93. Item& operator= (const Item&);
  94. JUCE_LEAK_DETECTOR (Item)
  95. };
  96. //==============================================================================
  97. struct PopupMenu::HelperClasses
  98. {
  99. class MouseSourceState;
  100. class MenuWindow;
  101. //==============================================================================
  102. class ItemComponent : public Component
  103. {
  104. public:
  105. ItemComponent (const PopupMenu::Item& info, int standardItemHeight, MenuWindow& parent)
  106. : itemInfo (info),
  107. isHighlighted (false)
  108. {
  109. addAndMakeVisible (itemInfo.customComp);
  110. parent.addAndMakeVisible (this);
  111. int itemW = 80;
  112. int itemH = 16;
  113. getIdealSize (itemW, itemH, standardItemHeight);
  114. setSize (itemW, jlimit (2, 600, itemH));
  115. addMouseListener (&parent, false);
  116. }
  117. ~ItemComponent()
  118. {
  119. removeChildComponent (itemInfo.customComp);
  120. }
  121. void getIdealSize (int& idealWidth, int& idealHeight, const int standardItemHeight)
  122. {
  123. if (itemInfo.customComp != nullptr)
  124. itemInfo.customComp->getIdealSize (idealWidth, idealHeight);
  125. else
  126. getLookAndFeel().getIdealPopupMenuItemSize (itemInfo.text,
  127. itemInfo.isSeparator,
  128. standardItemHeight,
  129. idealWidth, idealHeight);
  130. }
  131. void paint (Graphics& g) override
  132. {
  133. if (itemInfo.customComp == nullptr)
  134. {
  135. String mainText (itemInfo.text);
  136. String endText;
  137. const int endIndex = mainText.indexOf ("<end>");
  138. if (endIndex >= 0)
  139. {
  140. endText = mainText.substring (endIndex + 5).trim();
  141. mainText = mainText.substring (0, endIndex);
  142. }
  143. getLookAndFeel()
  144. .drawPopupMenuItem (g, getLocalBounds(),
  145. itemInfo.isSeparator,
  146. itemInfo.isActive,
  147. isHighlighted,
  148. itemInfo.isTicked,
  149. itemInfo.subMenu != nullptr && (itemInfo.itemID == 0 || itemInfo.subMenu->getNumItems() > 0),
  150. mainText, endText,
  151. itemInfo.iconDrawable,
  152. itemInfo.usesColour ? &(itemInfo.textColour) : nullptr);
  153. }
  154. }
  155. void resized() override
  156. {
  157. if (Component* const child = getChildComponent (0))
  158. child->setBounds (getLocalBounds().reduced (2, 0));
  159. }
  160. void setHighlighted (bool shouldBeHighlighted)
  161. {
  162. shouldBeHighlighted = shouldBeHighlighted && itemInfo.isActive;
  163. if (isHighlighted != shouldBeHighlighted)
  164. {
  165. isHighlighted = shouldBeHighlighted;
  166. if (itemInfo.customComp != nullptr)
  167. itemInfo.customComp->setHighlighted (shouldBeHighlighted);
  168. repaint();
  169. }
  170. }
  171. PopupMenu::Item itemInfo;
  172. private:
  173. bool isHighlighted;
  174. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ItemComponent)
  175. };
  176. //==============================================================================
  177. class MenuWindow : public Component
  178. {
  179. public:
  180. MenuWindow (const PopupMenu& menu, MenuWindow* const parentWindow,
  181. const Options& opts,
  182. const bool alignToRectangle,
  183. const bool shouldDismissOnMouseUp,
  184. ApplicationCommandManager** const manager)
  185. : Component ("menu"),
  186. parent (parentWindow),
  187. options (opts),
  188. managerOfChosenCommand (manager),
  189. componentAttachedTo (options.targetComponent),
  190. hasBeenOver (false),
  191. needsToScroll (false),
  192. dismissOnMouseUp (shouldDismissOnMouseUp),
  193. hideOnExit (false),
  194. disableMouseMoves (false),
  195. hasAnyJuceCompHadFocus (false),
  196. numColumns (0),
  197. contentHeight (0),
  198. childYOffset (0),
  199. windowCreationTime (Time::getMillisecondCounter()),
  200. lastFocusedTime (windowCreationTime),
  201. timeEnteredCurrentChildComp (windowCreationTime)
  202. {
  203. setWantsKeyboardFocus (false);
  204. setMouseClickGrabsKeyboardFocus (false);
  205. setAlwaysOnTop (true);
  206. setLookAndFeel (parent != nullptr ? &(parent->getLookAndFeel())
  207. : menu.lookAndFeel.get());
  208. setOpaque (getLookAndFeel().findColour (PopupMenu::backgroundColourId).isOpaque()
  209. || ! Desktop::canUseSemiTransparentWindows());
  210. for (int i = 0; i < menu.items.size(); ++i)
  211. {
  212. PopupMenu::Item* const item = menu.items.getUnchecked(i);
  213. if (i < menu.items.size() - 1 || ! item->isSeparator)
  214. items.add (new ItemComponent (*item, options.standardHeight, *this));
  215. }
  216. calculateWindowPos (options.targetArea, alignToRectangle);
  217. setTopLeftPosition (windowPos.getPosition());
  218. updateYPositions();
  219. if (options.visibleItemID != 0)
  220. {
  221. const int y = options.targetArea.getY() - windowPos.getY();
  222. ensureItemIsVisible (options.visibleItemID,
  223. isPositiveAndBelow (y, windowPos.getHeight()) ? y : -1);
  224. }
  225. resizeToBestWindowPos();
  226. addToDesktop (ComponentPeer::windowIsTemporary
  227. | ComponentPeer::windowIgnoresKeyPresses
  228. | getLookAndFeel().getMenuWindowFlags());
  229. getActiveWindows().add (this);
  230. Desktop::getInstance().addGlobalMouseListener (this);
  231. }
  232. ~MenuWindow()
  233. {
  234. getActiveWindows().removeFirstMatchingValue (this);
  235. Desktop::getInstance().removeGlobalMouseListener (this);
  236. activeSubMenu = nullptr;
  237. items.clear();
  238. }
  239. //==============================================================================
  240. void paint (Graphics& g) override
  241. {
  242. if (isOpaque())
  243. g.fillAll (Colours::white);
  244. getLookAndFeel().drawPopupMenuBackground (g, getWidth(), getHeight());
  245. }
  246. void paintOverChildren (Graphics& g) override
  247. {
  248. if (canScroll())
  249. {
  250. LookAndFeel& lf = getLookAndFeel();
  251. if (isTopScrollZoneActive())
  252. lf.drawPopupMenuUpDownArrow (g, getWidth(), PopupMenuSettings::scrollZone, true);
  253. if (isBottomScrollZoneActive())
  254. {
  255. g.setOrigin (0, getHeight() - PopupMenuSettings::scrollZone);
  256. lf.drawPopupMenuUpDownArrow (g, getWidth(), PopupMenuSettings::scrollZone, false);
  257. }
  258. }
  259. }
  260. //==============================================================================
  261. // hide this and all sub-comps
  262. void hide (const PopupMenu::Item* const item, const bool makeInvisible)
  263. {
  264. if (isVisible())
  265. {
  266. WeakReference<Component> deletionChecker (this);
  267. activeSubMenu = nullptr;
  268. currentChild = nullptr;
  269. if (item != nullptr
  270. && item->commandManager != nullptr
  271. && item->itemID != 0)
  272. {
  273. *managerOfChosenCommand = item->commandManager;
  274. }
  275. exitModalState (item != nullptr ? item->itemID : 0);
  276. if (makeInvisible && (deletionChecker != nullptr))
  277. setVisible (false);
  278. }
  279. }
  280. void dismissMenu (const PopupMenu::Item* const item)
  281. {
  282. if (parent != nullptr)
  283. {
  284. parent->dismissMenu (item);
  285. }
  286. else
  287. {
  288. if (item != nullptr)
  289. {
  290. // need a copy of this on the stack as the one passed in will get deleted during this call
  291. const PopupMenu::Item mi (*item);
  292. hide (&mi, false);
  293. }
  294. else
  295. {
  296. hide (nullptr, false);
  297. }
  298. }
  299. }
  300. //==============================================================================
  301. bool keyPressed (const KeyPress& key) override
  302. {
  303. if (key.isKeyCode (KeyPress::downKey))
  304. {
  305. selectNextItem (1);
  306. }
  307. else if (key.isKeyCode (KeyPress::upKey))
  308. {
  309. selectNextItem (-1);
  310. }
  311. else if (key.isKeyCode (KeyPress::leftKey))
  312. {
  313. if (parent != nullptr)
  314. {
  315. Component::SafePointer<MenuWindow> parentWindow (parent);
  316. ItemComponent* currentChildOfParent = parentWindow->currentChild;
  317. hide (nullptr, true);
  318. if (parentWindow != nullptr)
  319. parentWindow->setCurrentlyHighlightedChild (currentChildOfParent);
  320. disableTimerUntilMouseMoves();
  321. }
  322. else if (componentAttachedTo != nullptr)
  323. {
  324. componentAttachedTo->keyPressed (key);
  325. }
  326. }
  327. else if (key.isKeyCode (KeyPress::rightKey))
  328. {
  329. disableTimerUntilMouseMoves();
  330. if (showSubMenuFor (currentChild))
  331. {
  332. if (isSubMenuVisible())
  333. activeSubMenu->selectNextItem (1);
  334. }
  335. else if (componentAttachedTo != nullptr)
  336. {
  337. componentAttachedTo->keyPressed (key);
  338. }
  339. }
  340. else if (key.isKeyCode (KeyPress::returnKey))
  341. {
  342. triggerCurrentlyHighlightedItem();
  343. }
  344. else if (key.isKeyCode (KeyPress::escapeKey))
  345. {
  346. dismissMenu (nullptr);
  347. }
  348. else
  349. {
  350. return false;
  351. }
  352. return true;
  353. }
  354. void inputAttemptWhenModal() override
  355. {
  356. WeakReference<Component> deletionChecker (this);
  357. for (int i = mouseSourceStates.size(); --i >= 0;)
  358. {
  359. mouseSourceStates.getUnchecked(i)->timerCallback();
  360. if (deletionChecker == nullptr)
  361. return;
  362. }
  363. if (! isOverAnyMenu())
  364. {
  365. if (componentAttachedTo != nullptr)
  366. {
  367. // we want to dismiss the menu, but if we do it synchronously, then
  368. // the mouse-click will be allowed to pass through. That's good, except
  369. // when the user clicks on the button that orginally popped the menu up,
  370. // as they'll expect the menu to go away, and in fact it'll just
  371. // come back. So only dismiss synchronously if they're not on the original
  372. // comp that we're attached to.
  373. const Point<int> mousePos (componentAttachedTo->getMouseXYRelative());
  374. if (componentAttachedTo->reallyContains (mousePos, true))
  375. {
  376. postCommandMessage (PopupMenuSettings::dismissCommandId); // dismiss asynchrounously
  377. return;
  378. }
  379. }
  380. dismissMenu (nullptr);
  381. }
  382. }
  383. void handleCommandMessage (int commandId) override
  384. {
  385. Component::handleCommandMessage (commandId);
  386. if (commandId == PopupMenuSettings::dismissCommandId)
  387. dismissMenu (nullptr);
  388. }
  389. //==============================================================================
  390. void mouseMove (const MouseEvent& e) override { handleMouseEvent (e); }
  391. void mouseDown (const MouseEvent& e) override { handleMouseEvent (e); }
  392. void mouseDrag (const MouseEvent& e) override { handleMouseEvent (e); }
  393. void mouseUp (const MouseEvent& e) override { handleMouseEvent (e); }
  394. void mouseWheelMove (const MouseEvent&, const MouseWheelDetails& wheel) override
  395. {
  396. alterChildYPos (roundToInt (-10.0f * wheel.deltaY * PopupMenuSettings::scrollZone));
  397. }
  398. void handleMouseEvent (const MouseEvent& e)
  399. {
  400. getMouseState (e.source).handleMouseEvent (e);
  401. }
  402. bool windowIsStillValid()
  403. {
  404. if (! isVisible())
  405. return false;
  406. if (componentAttachedTo != options.targetComponent)
  407. {
  408. dismissMenu (nullptr);
  409. return false;
  410. }
  411. if (MenuWindow* currentlyModalWindow = dynamic_cast<MenuWindow*> (Component::getCurrentlyModalComponent()))
  412. if (! treeContains (currentlyModalWindow))
  413. return false;
  414. return true;
  415. }
  416. static Array<MenuWindow*>& getActiveWindows()
  417. {
  418. static Array<MenuWindow*> activeMenuWindows;
  419. return activeMenuWindows;
  420. }
  421. MouseSourceState& getMouseState (MouseInputSource source)
  422. {
  423. for (int i = mouseSourceStates.size(); --i >= 0;)
  424. {
  425. MouseSourceState& ms = *mouseSourceStates.getUnchecked(i);
  426. if (ms.source == source)
  427. return ms;
  428. }
  429. MouseSourceState* ms = new MouseSourceState (*this, source);
  430. mouseSourceStates.add (ms);
  431. return *ms;
  432. }
  433. //==============================================================================
  434. bool isOverAnyMenu() const
  435. {
  436. return parent != nullptr ? parent->isOverAnyMenu()
  437. : isOverChildren();
  438. }
  439. bool isOverChildren() const
  440. {
  441. return isVisible()
  442. && (isAnyMouseOver() || (activeSubMenu != nullptr && activeSubMenu->isOverChildren()));
  443. }
  444. bool isAnyMouseOver() const
  445. {
  446. for (int i = 0; i < mouseSourceStates.size(); ++i)
  447. if (mouseSourceStates.getUnchecked(i)->isOver())
  448. return true;
  449. return false;
  450. }
  451. bool treeContains (const MenuWindow* const window) const noexcept
  452. {
  453. const MenuWindow* mw = this;
  454. while (mw->parent != nullptr)
  455. mw = mw->parent;
  456. while (mw != nullptr)
  457. {
  458. if (mw == window)
  459. return true;
  460. mw = mw->activeSubMenu;
  461. }
  462. return false;
  463. }
  464. bool doesAnyJuceCompHaveFocus()
  465. {
  466. bool anyFocused = Process::isForegroundProcess();
  467. if (anyFocused && Component::getCurrentlyFocusedComponent() == nullptr)
  468. {
  469. // because no component at all may have focus, our test here will
  470. // only be triggered when something has focus and then loses it.
  471. anyFocused = ! hasAnyJuceCompHadFocus;
  472. for (int i = ComponentPeer::getNumPeers(); --i >= 0;)
  473. {
  474. if (ComponentPeer::getPeer (i)->isFocused())
  475. {
  476. anyFocused = true;
  477. hasAnyJuceCompHadFocus = true;
  478. break;
  479. }
  480. }
  481. }
  482. return anyFocused;
  483. }
  484. //==============================================================================
  485. void calculateWindowPos (const Rectangle<int>& target, const bool alignToRectangle)
  486. {
  487. const Rectangle<int> mon (Desktop::getInstance().getDisplays()
  488. .getDisplayContaining (target.getCentre())
  489. #if JUCE_MAC
  490. .userArea);
  491. #else
  492. .totalArea); // on windows, don't stop the menu overlapping the taskbar
  493. #endif
  494. const int maxMenuHeight = mon.getHeight() - 24;
  495. int x, y, widthToUse, heightToUse;
  496. layoutMenuItems (mon.getWidth() - 24, maxMenuHeight, widthToUse, heightToUse);
  497. if (alignToRectangle)
  498. {
  499. x = target.getX();
  500. const int spaceUnder = mon.getHeight() - (target.getBottom() - mon.getY());
  501. const int spaceOver = target.getY() - mon.getY();
  502. if (heightToUse < spaceUnder - 30 || spaceUnder >= spaceOver)
  503. y = target.getBottom();
  504. else
  505. y = target.getY() - heightToUse;
  506. }
  507. else
  508. {
  509. bool tendTowardsRight = target.getCentreX() < mon.getCentreX();
  510. if (parent != nullptr)
  511. {
  512. if (parent->parent != nullptr)
  513. {
  514. const bool parentGoingRight = (parent->getX() + parent->getWidth() / 2
  515. > parent->parent->getX() + parent->parent->getWidth() / 2);
  516. if (parentGoingRight && target.getRight() + widthToUse < mon.getRight() - 4)
  517. tendTowardsRight = true;
  518. else if ((! parentGoingRight) && target.getX() > widthToUse + 4)
  519. tendTowardsRight = false;
  520. }
  521. else if (target.getRight() + widthToUse < mon.getRight() - 32)
  522. {
  523. tendTowardsRight = true;
  524. }
  525. }
  526. const int biggestSpace = jmax (mon.getRight() - target.getRight(),
  527. target.getX() - mon.getX()) - 32;
  528. if (biggestSpace < widthToUse)
  529. {
  530. layoutMenuItems (biggestSpace + target.getWidth() / 3, maxMenuHeight, widthToUse, heightToUse);
  531. if (numColumns > 1)
  532. layoutMenuItems (biggestSpace - 4, maxMenuHeight, widthToUse, heightToUse);
  533. tendTowardsRight = (mon.getRight() - target.getRight()) >= (target.getX() - mon.getX());
  534. }
  535. if (tendTowardsRight)
  536. x = jmin (mon.getRight() - widthToUse - 4, target.getRight());
  537. else
  538. x = jmax (mon.getX() + 4, target.getX() - widthToUse);
  539. y = target.getY();
  540. if (target.getCentreY() > mon.getCentreY())
  541. y = jmax (mon.getY(), target.getBottom() - heightToUse);
  542. }
  543. x = jmax (mon.getX() + 1, jmin (mon.getRight() - (widthToUse + 6), x));
  544. y = jmax (mon.getY() + 1, jmin (mon.getBottom() - (heightToUse + 6), y));
  545. windowPos.setBounds (x, y, widthToUse, heightToUse);
  546. // sets this flag if it's big enough to obscure any of its parent menus
  547. hideOnExit = parent != nullptr
  548. && parent->windowPos.intersects (windowPos.expanded (-4, -4));
  549. }
  550. void layoutMenuItems (const int maxMenuW, const int maxMenuH, int& width, int& height)
  551. {
  552. numColumns = 0;
  553. contentHeight = 0;
  554. int totalW;
  555. const int maximumNumColumns = options.maxColumns > 0 ? options.maxColumns : 7;
  556. do
  557. {
  558. ++numColumns;
  559. totalW = workOutBestSize (maxMenuW);
  560. if (totalW > maxMenuW)
  561. {
  562. numColumns = jmax (1, numColumns - 1);
  563. workOutBestSize (maxMenuW); // to update col widths
  564. break;
  565. }
  566. else if (totalW > maxMenuW / 2 || contentHeight < maxMenuH)
  567. {
  568. break;
  569. }
  570. } while (numColumns < maximumNumColumns);
  571. const int actualH = jmin (contentHeight, maxMenuH);
  572. needsToScroll = contentHeight > actualH;
  573. width = updateYPositions();
  574. height = actualH + PopupMenuSettings::borderSize * 2;
  575. }
  576. int workOutBestSize (const int maxMenuW)
  577. {
  578. int totalW = 0;
  579. contentHeight = 0;
  580. int childNum = 0;
  581. for (int col = 0; col < numColumns; ++col)
  582. {
  583. int colW = options.standardHeight, colH = 0;
  584. const int numChildren = jmin (items.size() - childNum,
  585. (items.size() + numColumns - 1) / numColumns);
  586. for (int i = numChildren; --i >= 0;)
  587. {
  588. colW = jmax (colW, items.getUnchecked (childNum + i)->getWidth());
  589. colH += items.getUnchecked (childNum + i)->getHeight();
  590. }
  591. colW = jmin (maxMenuW / jmax (1, numColumns - 2), colW + PopupMenuSettings::borderSize * 2);
  592. columnWidths.set (col, colW);
  593. totalW += colW;
  594. contentHeight = jmax (contentHeight, colH);
  595. childNum += numChildren;
  596. }
  597. if (totalW < options.minWidth)
  598. {
  599. totalW = options.minWidth;
  600. for (int col = 0; col < numColumns; ++col)
  601. columnWidths.set (0, totalW / numColumns);
  602. }
  603. return totalW;
  604. }
  605. void ensureItemIsVisible (const int itemID, int wantedY)
  606. {
  607. jassert (itemID != 0);
  608. for (int i = items.size(); --i >= 0;)
  609. {
  610. ItemComponent* const m = items.getUnchecked(i);
  611. if (m != nullptr
  612. && m->itemInfo.itemID == itemID
  613. && windowPos.getHeight() > PopupMenuSettings::scrollZone * 4)
  614. {
  615. const int currentY = m->getY();
  616. if (wantedY > 0 || currentY < 0 || m->getBottom() > windowPos.getHeight())
  617. {
  618. if (wantedY < 0)
  619. wantedY = jlimit (PopupMenuSettings::scrollZone,
  620. jmax (PopupMenuSettings::scrollZone,
  621. windowPos.getHeight() - (PopupMenuSettings::scrollZone + m->getHeight())),
  622. currentY);
  623. const Rectangle<int> mon (Desktop::getInstance().getDisplays()
  624. .getDisplayContaining (windowPos.getPosition()).userArea);
  625. int deltaY = wantedY - currentY;
  626. windowPos.setSize (jmin (windowPos.getWidth(), mon.getWidth()),
  627. jmin (windowPos.getHeight(), mon.getHeight()));
  628. const int newY = jlimit (mon.getY(),
  629. mon.getBottom() - windowPos.getHeight(),
  630. windowPos.getY() + deltaY);
  631. deltaY -= newY - windowPos.getY();
  632. childYOffset -= deltaY;
  633. windowPos.setPosition (windowPos.getX(), newY);
  634. updateYPositions();
  635. }
  636. break;
  637. }
  638. }
  639. }
  640. void resizeToBestWindowPos()
  641. {
  642. Rectangle<int> r (windowPos);
  643. if (childYOffset < 0)
  644. {
  645. r = r.withTop (r.getY() - childYOffset);
  646. }
  647. else if (childYOffset > 0)
  648. {
  649. const int spaceAtBottom = r.getHeight() - (contentHeight - childYOffset);
  650. if (spaceAtBottom > 0)
  651. r.setSize (r.getWidth(), r.getHeight() - spaceAtBottom);
  652. }
  653. setBounds (r);
  654. updateYPositions();
  655. }
  656. void alterChildYPos (const int delta)
  657. {
  658. if (canScroll())
  659. {
  660. childYOffset += delta;
  661. if (delta < 0)
  662. childYOffset = jmax (childYOffset, 0);
  663. else if (delta > 0)
  664. childYOffset = jmin (childYOffset,
  665. contentHeight - windowPos.getHeight() + PopupMenuSettings::borderSize);
  666. updateYPositions();
  667. }
  668. else
  669. {
  670. childYOffset = 0;
  671. }
  672. resizeToBestWindowPos();
  673. repaint();
  674. }
  675. int updateYPositions()
  676. {
  677. int x = 0;
  678. int childNum = 0;
  679. for (int col = 0; col < numColumns; ++col)
  680. {
  681. const int numChildren = jmin (items.size() - childNum,
  682. (items.size() + numColumns - 1) / numColumns);
  683. const int colW = columnWidths [col];
  684. int y = PopupMenuSettings::borderSize - (childYOffset + (getY() - windowPos.getY()));
  685. for (int i = 0; i < numChildren; ++i)
  686. {
  687. Component* const c = items.getUnchecked (childNum + i);
  688. c->setBounds (x, y, colW, c->getHeight());
  689. y += c->getHeight();
  690. }
  691. x += colW;
  692. childNum += numChildren;
  693. }
  694. return x;
  695. }
  696. void setCurrentlyHighlightedChild (ItemComponent* const child)
  697. {
  698. if (currentChild != nullptr)
  699. currentChild->setHighlighted (false);
  700. currentChild = child;
  701. if (currentChild != nullptr)
  702. {
  703. currentChild->setHighlighted (true);
  704. timeEnteredCurrentChildComp = Time::getApproximateMillisecondCounter();
  705. }
  706. }
  707. bool isSubMenuVisible() const noexcept { return activeSubMenu != nullptr && activeSubMenu->isVisible(); }
  708. bool showSubMenuFor (ItemComponent* const childComp)
  709. {
  710. activeSubMenu = nullptr;
  711. if (childComp != nullptr
  712. && childComp->itemInfo.hasActiveSubMenu())
  713. {
  714. activeSubMenu = new HelperClasses::MenuWindow (*(childComp->itemInfo.subMenu), this,
  715. options.withTargetScreenArea (childComp->getScreenBounds())
  716. .withMinimumWidth (0)
  717. .withTargetComponent (nullptr),
  718. false, dismissOnMouseUp, managerOfChosenCommand);
  719. activeSubMenu->setVisible (true); // (must be called before enterModalState on Windows to avoid DropShadower confusion)
  720. activeSubMenu->enterModalState (false);
  721. activeSubMenu->toFront (false);
  722. return true;
  723. }
  724. return false;
  725. }
  726. void triggerCurrentlyHighlightedItem()
  727. {
  728. if (currentChild != nullptr
  729. && currentChild->itemInfo.canBeTriggered()
  730. && (currentChild->itemInfo.customComp == nullptr
  731. || currentChild->itemInfo.customComp->isTriggeredAutomatically()))
  732. {
  733. dismissMenu (&currentChild->itemInfo);
  734. }
  735. }
  736. void selectNextItem (const int delta)
  737. {
  738. disableTimerUntilMouseMoves();
  739. int start = jmax (0, items.indexOf (currentChild));
  740. for (int i = items.size(); --i >= 0;)
  741. {
  742. start += delta;
  743. if (ItemComponent* mic = items.getUnchecked ((start + items.size()) % items.size()))
  744. {
  745. if (mic->itemInfo.canBeTriggered() || mic->itemInfo.hasActiveSubMenu())
  746. {
  747. setCurrentlyHighlightedChild (mic);
  748. break;
  749. }
  750. }
  751. }
  752. }
  753. void disableTimerUntilMouseMoves()
  754. {
  755. disableMouseMoves = true;
  756. if (parent != nullptr)
  757. parent->disableTimerUntilMouseMoves();
  758. }
  759. bool canScroll() const noexcept { return childYOffset != 0 || needsToScroll; }
  760. bool isTopScrollZoneActive() const noexcept { return canScroll() && childYOffset > 0; }
  761. bool isBottomScrollZoneActive() const noexcept { return canScroll() && childYOffset < contentHeight - windowPos.getHeight(); }
  762. //==============================================================================
  763. MenuWindow* parent;
  764. const Options options;
  765. OwnedArray<ItemComponent> items;
  766. ApplicationCommandManager** managerOfChosenCommand;
  767. WeakReference<Component> componentAttachedTo;
  768. Rectangle<int> windowPos;
  769. bool hasBeenOver, needsToScroll;
  770. bool dismissOnMouseUp, hideOnExit, disableMouseMoves, hasAnyJuceCompHadFocus;
  771. int numColumns, contentHeight, childYOffset;
  772. Component::SafePointer<ItemComponent> currentChild;
  773. ScopedPointer<MenuWindow> activeSubMenu;
  774. Array<int> columnWidths;
  775. uint32 windowCreationTime, lastFocusedTime, timeEnteredCurrentChildComp;
  776. OwnedArray<MouseSourceState> mouseSourceStates;
  777. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MenuWindow)
  778. };
  779. //==============================================================================
  780. class MouseSourceState : private Timer
  781. {
  782. public:
  783. MouseSourceState (MenuWindow& w, MouseInputSource s)
  784. : window (w), source (s), scrollAcceleration (1.0),
  785. lastScrollTime (Time::getMillisecondCounter()),
  786. lastMouseMoveTime (0), isDown (false)
  787. {
  788. }
  789. void handleMouseEvent (const MouseEvent& e)
  790. {
  791. if (! window.windowIsStillValid())
  792. return;
  793. startTimer (PopupMenuSettings::timerInterval);
  794. handleMousePosition (e.getScreenPosition());
  795. }
  796. void timerCallback() override
  797. {
  798. if (window.windowIsStillValid())
  799. handleMousePosition (source.getScreenPosition().roundToInt());
  800. }
  801. bool isOver() const
  802. {
  803. return window.reallyContains (window.getLocalPoint (nullptr, source.getScreenPosition()).roundToInt(), true);
  804. }
  805. MenuWindow& window;
  806. MouseInputSource source;
  807. private:
  808. Point<int> lastMousePos;
  809. double scrollAcceleration;
  810. uint32 lastScrollTime, lastMouseMoveTime;
  811. bool isDown;
  812. void handleMousePosition (Point<int> globalMousePos)
  813. {
  814. const Point<int> localMousePos (window.getLocalPoint (nullptr, globalMousePos));
  815. const uint32 timeNow = Time::getMillisecondCounter();
  816. if (timeNow > window.timeEnteredCurrentChildComp + 100
  817. && window.reallyContains (localMousePos, true)
  818. && window.currentChild != nullptr
  819. && ! (window.disableMouseMoves || window.isSubMenuVisible()))
  820. {
  821. window.showSubMenuFor (window.currentChild);
  822. }
  823. highlightItemUnderMouse (globalMousePos, localMousePos, timeNow);
  824. const bool overScrollArea = scrollIfNecessary (localMousePos, timeNow);
  825. const bool isOverAny = window.isOverAnyMenu();
  826. if (window.hideOnExit && window.hasBeenOver && ! isOverAny)
  827. window.hide (nullptr, true);
  828. else
  829. checkButtonState (localMousePos, timeNow, isDown, overScrollArea, isOverAny);
  830. }
  831. void checkButtonState (Point<int> localMousePos, const uint32 timeNow,
  832. const bool wasDown, const bool overScrollArea, const bool isOverAny)
  833. {
  834. isDown = window.hasBeenOver
  835. && (ModifierKeys::getCurrentModifiers().isAnyMouseButtonDown()
  836. || ModifierKeys::getCurrentModifiersRealtime().isAnyMouseButtonDown());
  837. if (! window.doesAnyJuceCompHaveFocus())
  838. {
  839. if (timeNow > window.lastFocusedTime + 10)
  840. {
  841. PopupMenuSettings::menuWasHiddenBecauseOfAppChange = true;
  842. window.dismissMenu (nullptr);
  843. // Note: this object may have been deleted by the previous call..
  844. }
  845. }
  846. else if (wasDown && timeNow > window.windowCreationTime + 250
  847. && ! (isDown || overScrollArea))
  848. {
  849. if (window.reallyContains (localMousePos, true))
  850. window.triggerCurrentlyHighlightedItem();
  851. else if ((window.hasBeenOver || ! window.dismissOnMouseUp) && ! isOverAny)
  852. window.dismissMenu (nullptr);
  853. // Note: this object may have been deleted by the previous call..
  854. }
  855. else
  856. {
  857. window.lastFocusedTime = timeNow;
  858. }
  859. }
  860. void highlightItemUnderMouse (Point<int> globalMousePos, Point<int> localMousePos, const uint32 timeNow)
  861. {
  862. if (globalMousePos != lastMousePos || timeNow > lastMouseMoveTime + 350)
  863. {
  864. const bool isMouseOver = window.reallyContains (localMousePos, true);
  865. if (isMouseOver)
  866. window.hasBeenOver = true;
  867. if (lastMousePos.getDistanceFrom (globalMousePos) > 2)
  868. {
  869. lastMouseMoveTime = timeNow;
  870. if (window.disableMouseMoves && isMouseOver)
  871. window.disableMouseMoves = false;
  872. }
  873. if (window.disableMouseMoves || (window.activeSubMenu != nullptr && window.activeSubMenu->isOverChildren()))
  874. return;
  875. const bool isMovingTowardsMenu = isMouseOver && globalMousePos != lastMousePos
  876. && isMovingTowardsSubmenu (globalMousePos);
  877. lastMousePos = globalMousePos;
  878. if (! isMovingTowardsMenu)
  879. {
  880. Component* c = window.getComponentAt (localMousePos);
  881. if (c == &window)
  882. c = nullptr;
  883. ItemComponent* itemUnderMouse = dynamic_cast<ItemComponent*> (c);
  884. if (itemUnderMouse == nullptr && c != nullptr)
  885. itemUnderMouse = c->findParentComponentOfClass<ItemComponent>();
  886. if (itemUnderMouse != window.currentChild
  887. && (isMouseOver || (window.activeSubMenu == nullptr) || ! window.activeSubMenu->isVisible()))
  888. {
  889. if (isMouseOver && (c != nullptr) && (window.activeSubMenu != nullptr))
  890. window.activeSubMenu->hide (nullptr, true);
  891. if (! isMouseOver)
  892. itemUnderMouse = nullptr;
  893. window.setCurrentlyHighlightedChild (itemUnderMouse);
  894. }
  895. }
  896. }
  897. }
  898. bool isMovingTowardsSubmenu (Point<int> newGlobalPos) const
  899. {
  900. if (window.activeSubMenu == nullptr)
  901. return false;
  902. // try to intelligently guess whether the user is moving the mouse towards a currently-open
  903. // submenu. To do this, look at whether the mouse stays inside a triangular region that
  904. // extends from the last mouse pos to the submenu's rectangle..
  905. const Rectangle<int> itemScreenBounds (window.activeSubMenu->getScreenBounds());
  906. float subX = (float) itemScreenBounds.getX();
  907. Point<int> oldGlobalPos (lastMousePos);
  908. if (itemScreenBounds.getX() > window.getX())
  909. {
  910. oldGlobalPos -= Point<int> (2, 0); // to enlarge the triangle a bit, in case the mouse only moves a couple of pixels
  911. }
  912. else
  913. {
  914. oldGlobalPos += Point<int> (2, 0);
  915. subX += itemScreenBounds.getWidth();
  916. }
  917. Path areaTowardsSubMenu;
  918. areaTowardsSubMenu.addTriangle ((float) oldGlobalPos.x, (float) oldGlobalPos.y,
  919. subX, (float) itemScreenBounds.getY(),
  920. subX, (float) itemScreenBounds.getBottom());
  921. return areaTowardsSubMenu.contains (newGlobalPos.toFloat());
  922. }
  923. bool scrollIfNecessary (Point<int> localMousePos, const uint32 timeNow)
  924. {
  925. if (window.canScroll()
  926. && isPositiveAndBelow (localMousePos.x, window.getWidth())
  927. && (isPositiveAndBelow (localMousePos.y, window.getHeight()) || source.isDragging()))
  928. {
  929. if (window.isTopScrollZoneActive() && localMousePos.y < PopupMenuSettings::scrollZone)
  930. return scroll (timeNow, -1);
  931. if (window.isBottomScrollZoneActive() && localMousePos.y > window.getHeight() - PopupMenuSettings::scrollZone)
  932. return scroll (timeNow, 1);
  933. }
  934. scrollAcceleration = 1.0;
  935. return false;
  936. }
  937. bool scroll (const uint32 timeNow, const int direction)
  938. {
  939. if (timeNow > lastScrollTime + 20)
  940. {
  941. scrollAcceleration = jmin (4.0, scrollAcceleration * 1.04);
  942. int amount = 0;
  943. for (int i = 0; i < window.items.size() && amount == 0; ++i)
  944. amount = ((int) scrollAcceleration) * window.items.getUnchecked(i)->getHeight();
  945. window.alterChildYPos (amount * direction);
  946. lastScrollTime = timeNow;
  947. }
  948. return true;
  949. }
  950. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MouseSourceState)
  951. };
  952. //==============================================================================
  953. class NormalComponentWrapper : public PopupMenu::CustomComponent
  954. {
  955. public:
  956. NormalComponentWrapper (Component* const comp, const int w, const int h,
  957. const bool triggerMenuItemAutomaticallyWhenClicked)
  958. : PopupMenu::CustomComponent (triggerMenuItemAutomaticallyWhenClicked),
  959. width (w), height (h)
  960. {
  961. addAndMakeVisible (comp);
  962. }
  963. void getIdealSize (int& idealWidth, int& idealHeight) override
  964. {
  965. idealWidth = width;
  966. idealHeight = height;
  967. }
  968. void resized() override
  969. {
  970. if (Component* const child = getChildComponent(0))
  971. child->setBounds (getLocalBounds());
  972. }
  973. private:
  974. const int width, height;
  975. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NormalComponentWrapper)
  976. };
  977. //==============================================================================
  978. class HeaderItemComponent : public PopupMenu::CustomComponent
  979. {
  980. public:
  981. HeaderItemComponent (const String& name)
  982. : PopupMenu::CustomComponent (false)
  983. {
  984. setName (name);
  985. }
  986. void paint (Graphics& g) override
  987. {
  988. getLookAndFeel().drawPopupMenuSectionHeader (g, getLocalBounds(), getName());
  989. }
  990. void getIdealSize (int& idealWidth, int& idealHeight) override
  991. {
  992. getLookAndFeel().getIdealPopupMenuItemSize (getName(), false, -1, idealWidth, idealHeight);
  993. idealHeight += idealHeight / 2;
  994. idealWidth += idealWidth / 4;
  995. }
  996. private:
  997. JUCE_LEAK_DETECTOR (HeaderItemComponent)
  998. };
  999. };
  1000. //==============================================================================
  1001. PopupMenu::PopupMenu()
  1002. {
  1003. }
  1004. PopupMenu::PopupMenu (const PopupMenu& other)
  1005. : lookAndFeel (other.lookAndFeel)
  1006. {
  1007. items.addCopiesOf (other.items);
  1008. }
  1009. PopupMenu& PopupMenu::operator= (const PopupMenu& other)
  1010. {
  1011. if (this != &other)
  1012. {
  1013. lookAndFeel = other.lookAndFeel;
  1014. clear();
  1015. items.addCopiesOf (other.items);
  1016. }
  1017. return *this;
  1018. }
  1019. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  1020. PopupMenu::PopupMenu (PopupMenu&& other) noexcept
  1021. : lookAndFeel (other.lookAndFeel)
  1022. {
  1023. items.swapWith (other.items);
  1024. }
  1025. PopupMenu& PopupMenu::operator= (PopupMenu&& other) noexcept
  1026. {
  1027. jassert (this != &other); // hopefully the compiler should make this situation impossible!
  1028. items.swapWith (other.items);
  1029. lookAndFeel = other.lookAndFeel;
  1030. return *this;
  1031. }
  1032. #endif
  1033. PopupMenu::~PopupMenu()
  1034. {
  1035. }
  1036. void PopupMenu::clear()
  1037. {
  1038. items.clear();
  1039. }
  1040. void PopupMenu::addItem (int itemResultID, const String& itemText, bool isActive, bool isTicked)
  1041. {
  1042. jassert (itemResultID != 0); // 0 is used as a return value to indicate that the user
  1043. // didn't pick anything, so you shouldn't use it as the id
  1044. // for an item..
  1045. items.add (new Item (itemResultID, itemText, isActive, isTicked, nullptr,
  1046. Colours::black, false, nullptr, nullptr, nullptr));
  1047. }
  1048. static Drawable* createDrawableFromImage (const Image& im)
  1049. {
  1050. if (im.isValid())
  1051. {
  1052. DrawableImage* d = new DrawableImage();
  1053. d->setImage (im);
  1054. return d;
  1055. }
  1056. return nullptr;
  1057. }
  1058. void PopupMenu::addItem (int itemResultID, const String& itemText, bool isActive, bool isTicked, const Image& iconToUse)
  1059. {
  1060. jassert (itemResultID != 0); // 0 is used as a return value to indicate that the user
  1061. // didn't pick anything, so you shouldn't use it as the id
  1062. // for an item..
  1063. items.add (new Item (itemResultID, itemText, isActive, isTicked, createDrawableFromImage (iconToUse),
  1064. Colours::black, false, nullptr, nullptr, nullptr));
  1065. }
  1066. void PopupMenu::addItem (int itemResultID, const String& itemText, bool isActive, bool isTicked, Drawable* iconToUse)
  1067. {
  1068. jassert (itemResultID != 0); // 0 is used as a return value to indicate that the user
  1069. // didn't pick anything, so you shouldn't use it as the id
  1070. // for an item..
  1071. items.add (new Item (itemResultID, itemText, isActive, isTicked, iconToUse,
  1072. Colours::black, false, nullptr, nullptr, nullptr));
  1073. }
  1074. void PopupMenu::addCommandItem (ApplicationCommandManager* commandManager,
  1075. const CommandID commandID,
  1076. const String& displayName,
  1077. Drawable* iconToUse)
  1078. {
  1079. jassert (commandManager != nullptr && commandID != 0);
  1080. if (const ApplicationCommandInfo* const registeredInfo = commandManager->getCommandForID (commandID))
  1081. {
  1082. ApplicationCommandInfo info (*registeredInfo);
  1083. ApplicationCommandTarget* const target = commandManager->getTargetForCommand (commandID, info);
  1084. items.add (new Item (commandID,
  1085. displayName.isNotEmpty() ? displayName
  1086. : info.shortName,
  1087. target != nullptr && (info.flags & ApplicationCommandInfo::isDisabled) == 0,
  1088. (info.flags & ApplicationCommandInfo::isTicked) != 0,
  1089. iconToUse,
  1090. Colours::black,
  1091. false,
  1092. nullptr, nullptr,
  1093. commandManager));
  1094. }
  1095. }
  1096. void PopupMenu::addColouredItem (int itemResultID, const String& itemText, Colour itemTextColour,
  1097. bool isActive, bool isTicked, Drawable* iconToUse)
  1098. {
  1099. jassert (itemResultID != 0); // 0 is used as a return value to indicate that the user
  1100. // didn't pick anything, so you shouldn't use it as the id
  1101. // for an item..
  1102. items.add (new Item (itemResultID, itemText, isActive, isTicked, iconToUse,
  1103. itemTextColour, true, nullptr, nullptr, nullptr));
  1104. }
  1105. void PopupMenu::addColouredItem (int itemResultID, const String& itemText, Colour itemTextColour,
  1106. bool isActive, bool isTicked, const Image& iconToUse)
  1107. {
  1108. addColouredItem (itemResultID, itemText, itemTextColour, isActive, isTicked, createDrawableFromImage (iconToUse));
  1109. }
  1110. void PopupMenu::addCustomItem (int itemID, CustomComponent* cc, const PopupMenu* subMenu)
  1111. {
  1112. jassert (itemID != 0); // 0 is used as a return value to indicate that the user
  1113. // didn't pick anything, so you shouldn't use it as the id
  1114. // for an item..
  1115. items.add (new Item (itemID, String::empty, true, false, nullptr,
  1116. Colours::black, false, cc, subMenu, nullptr));
  1117. }
  1118. void PopupMenu::addCustomItem (int itemResultID, Component* customComponent, int idealWidth, int idealHeight,
  1119. bool triggerMenuItemAutomaticallyWhenClicked, const PopupMenu* subMenu)
  1120. {
  1121. items.add (new Item (itemResultID, String::empty, true, false, nullptr, Colours::black, false,
  1122. new HelperClasses::NormalComponentWrapper (customComponent, idealWidth, idealHeight,
  1123. triggerMenuItemAutomaticallyWhenClicked),
  1124. subMenu, nullptr));
  1125. }
  1126. void PopupMenu::addSubMenu (const String& subMenuName, const PopupMenu& subMenu, bool isActive)
  1127. {
  1128. addSubMenu (subMenuName, subMenu, isActive, nullptr, false, 0);
  1129. }
  1130. void PopupMenu::addSubMenu (const String& subMenuName, const PopupMenu& subMenu, bool isActive,
  1131. const Image& iconToUse, bool isTicked, int itemResultID)
  1132. {
  1133. addSubMenu (subMenuName, subMenu, isActive, createDrawableFromImage (iconToUse), isTicked, itemResultID);
  1134. }
  1135. void PopupMenu::addSubMenu (const String& subMenuName, const PopupMenu& subMenu, bool isActive,
  1136. Drawable* iconToUse, bool isTicked, int itemResultID)
  1137. {
  1138. items.add (new Item (itemResultID, subMenuName, isActive && (itemResultID != 0 || subMenu.getNumItems() > 0), isTicked,
  1139. iconToUse, Colours::black, false, nullptr, &subMenu, nullptr));
  1140. }
  1141. void PopupMenu::addSeparator()
  1142. {
  1143. if (items.size() > 0 && ! items.getLast()->isSeparator)
  1144. items.add (new Item());
  1145. }
  1146. void PopupMenu::addSectionHeader (const String& title)
  1147. {
  1148. addCustomItem (PopupMenuSettings::sectionHeaderID, new HelperClasses::HeaderItemComponent (title));
  1149. }
  1150. //==============================================================================
  1151. PopupMenu::Options::Options()
  1152. : targetComponent (nullptr),
  1153. visibleItemID (0),
  1154. minWidth (0),
  1155. maxColumns (0),
  1156. standardHeight (0)
  1157. {
  1158. targetArea.setPosition (Desktop::getMousePosition());
  1159. }
  1160. PopupMenu::Options PopupMenu::Options::withTargetComponent (Component* comp) const noexcept
  1161. {
  1162. Options o (*this);
  1163. o.targetComponent = comp;
  1164. if (comp != nullptr)
  1165. o.targetArea = comp->getScreenBounds();
  1166. return o;
  1167. }
  1168. PopupMenu::Options PopupMenu::Options::withTargetScreenArea (const Rectangle<int>& area) const noexcept
  1169. {
  1170. Options o (*this);
  1171. o.targetArea = area;
  1172. return o;
  1173. }
  1174. PopupMenu::Options PopupMenu::Options::withMinimumWidth (int w) const noexcept
  1175. {
  1176. Options o (*this);
  1177. o.minWidth = w;
  1178. return o;
  1179. }
  1180. PopupMenu::Options PopupMenu::Options::withMaximumNumColumns (int cols) const noexcept
  1181. {
  1182. Options o (*this);
  1183. o.maxColumns = cols;
  1184. return o;
  1185. }
  1186. PopupMenu::Options PopupMenu::Options::withStandardItemHeight (int height) const noexcept
  1187. {
  1188. Options o (*this);
  1189. o.standardHeight = height;
  1190. return o;
  1191. }
  1192. PopupMenu::Options PopupMenu::Options::withItemThatMustBeVisible (int idOfItemToBeVisible) const noexcept
  1193. {
  1194. Options o (*this);
  1195. o.visibleItemID = idOfItemToBeVisible;
  1196. return o;
  1197. }
  1198. Component* PopupMenu::createWindow (const Options& options,
  1199. ApplicationCommandManager** managerOfChosenCommand) const
  1200. {
  1201. if (items.size() > 0)
  1202. return new HelperClasses::MenuWindow (*this, nullptr, options,
  1203. ! options.targetArea.isEmpty(),
  1204. ModifierKeys::getCurrentModifiers().isAnyMouseButtonDown(),
  1205. managerOfChosenCommand);
  1206. return nullptr;
  1207. }
  1208. //==============================================================================
  1209. // This invokes any command manager commands and deletes the menu window when it is dismissed
  1210. class PopupMenuCompletionCallback : public ModalComponentManager::Callback
  1211. {
  1212. public:
  1213. PopupMenuCompletionCallback()
  1214. : managerOfChosenCommand (nullptr),
  1215. prevFocused (Component::getCurrentlyFocusedComponent()),
  1216. prevTopLevel (prevFocused != nullptr ? prevFocused->getTopLevelComponent() : nullptr)
  1217. {
  1218. PopupMenuSettings::menuWasHiddenBecauseOfAppChange = false;
  1219. }
  1220. void modalStateFinished (int result)
  1221. {
  1222. if (managerOfChosenCommand != nullptr && result != 0)
  1223. {
  1224. ApplicationCommandTarget::InvocationInfo info (result);
  1225. info.invocationMethod = ApplicationCommandTarget::InvocationInfo::fromMenu;
  1226. managerOfChosenCommand->invoke (info, true);
  1227. }
  1228. // (this would be the place to fade out the component, if that's what's required)
  1229. component = nullptr;
  1230. if (! PopupMenuSettings::menuWasHiddenBecauseOfAppChange)
  1231. {
  1232. if (prevTopLevel != nullptr)
  1233. prevTopLevel->toFront (true);
  1234. if (prevFocused != nullptr)
  1235. prevFocused->grabKeyboardFocus();
  1236. }
  1237. }
  1238. ApplicationCommandManager* managerOfChosenCommand;
  1239. ScopedPointer<Component> component;
  1240. WeakReference<Component> prevFocused, prevTopLevel;
  1241. private:
  1242. JUCE_DECLARE_NON_COPYABLE (PopupMenuCompletionCallback)
  1243. };
  1244. int PopupMenu::showWithOptionalCallback (const Options& options, ModalComponentManager::Callback* const userCallback,
  1245. const bool canBeModal)
  1246. {
  1247. ScopedPointer<ModalComponentManager::Callback> userCallbackDeleter (userCallback);
  1248. ScopedPointer<PopupMenuCompletionCallback> callback (new PopupMenuCompletionCallback());
  1249. if (Component* window = createWindow (options, &(callback->managerOfChosenCommand)))
  1250. {
  1251. callback->component = window;
  1252. window->setVisible (true); // (must be called before enterModalState on Windows to avoid DropShadower confusion)
  1253. window->enterModalState (false, userCallbackDeleter.release());
  1254. ModalComponentManager::getInstance()->attachCallback (window, callback.release());
  1255. window->toFront (false); // need to do this after making it modal, or it could
  1256. // be stuck behind other comps that are already modal..
  1257. #if JUCE_MODAL_LOOPS_PERMITTED
  1258. if (userCallback == nullptr && canBeModal)
  1259. return window->runModalLoop();
  1260. #else
  1261. ignoreUnused (canBeModal);
  1262. jassert (! (userCallback == nullptr && canBeModal));
  1263. #endif
  1264. }
  1265. return 0;
  1266. }
  1267. //==============================================================================
  1268. #if JUCE_MODAL_LOOPS_PERMITTED
  1269. int PopupMenu::showMenu (const Options& options)
  1270. {
  1271. return showWithOptionalCallback (options, nullptr, true);
  1272. }
  1273. #endif
  1274. void PopupMenu::showMenuAsync (const Options& options, ModalComponentManager::Callback* userCallback)
  1275. {
  1276. #if ! JUCE_MODAL_LOOPS_PERMITTED
  1277. jassert (userCallback != nullptr);
  1278. #endif
  1279. showWithOptionalCallback (options, userCallback, false);
  1280. }
  1281. //==============================================================================
  1282. #if JUCE_MODAL_LOOPS_PERMITTED
  1283. int PopupMenu::show (const int itemIDThatMustBeVisible,
  1284. const int minimumWidth, const int maximumNumColumns,
  1285. const int standardItemHeight,
  1286. ModalComponentManager::Callback* callback)
  1287. {
  1288. return showWithOptionalCallback (Options().withItemThatMustBeVisible (itemIDThatMustBeVisible)
  1289. .withMinimumWidth (minimumWidth)
  1290. .withMaximumNumColumns (maximumNumColumns)
  1291. .withStandardItemHeight (standardItemHeight),
  1292. callback, true);
  1293. }
  1294. int PopupMenu::showAt (const Rectangle<int>& screenAreaToAttachTo,
  1295. const int itemIDThatMustBeVisible,
  1296. const int minimumWidth, const int maximumNumColumns,
  1297. const int standardItemHeight,
  1298. ModalComponentManager::Callback* callback)
  1299. {
  1300. return showWithOptionalCallback (Options().withTargetScreenArea (screenAreaToAttachTo)
  1301. .withItemThatMustBeVisible (itemIDThatMustBeVisible)
  1302. .withMinimumWidth (minimumWidth)
  1303. .withMaximumNumColumns (maximumNumColumns)
  1304. .withStandardItemHeight (standardItemHeight),
  1305. callback, true);
  1306. }
  1307. int PopupMenu::showAt (Component* componentToAttachTo,
  1308. const int itemIDThatMustBeVisible,
  1309. const int minimumWidth, const int maximumNumColumns,
  1310. const int standardItemHeight,
  1311. ModalComponentManager::Callback* callback)
  1312. {
  1313. Options options (Options().withItemThatMustBeVisible (itemIDThatMustBeVisible)
  1314. .withMinimumWidth (minimumWidth)
  1315. .withMaximumNumColumns (maximumNumColumns)
  1316. .withStandardItemHeight (standardItemHeight));
  1317. if (componentToAttachTo != nullptr)
  1318. options = options.withTargetComponent (componentToAttachTo);
  1319. return showWithOptionalCallback (options, callback, true);
  1320. }
  1321. #endif
  1322. bool JUCE_CALLTYPE PopupMenu::dismissAllActiveMenus()
  1323. {
  1324. const Array<HelperClasses::MenuWindow*>& windows = HelperClasses::MenuWindow::getActiveWindows();
  1325. const int numWindows = windows.size();
  1326. for (int i = numWindows; --i >= 0;)
  1327. if (HelperClasses::MenuWindow* const pmw = windows[i])
  1328. pmw->dismissMenu (nullptr);
  1329. return numWindows > 0;
  1330. }
  1331. //==============================================================================
  1332. int PopupMenu::getNumItems() const noexcept
  1333. {
  1334. int num = 0;
  1335. for (int i = items.size(); --i >= 0;)
  1336. if (! items.getUnchecked(i)->isSeparator)
  1337. ++num;
  1338. return num;
  1339. }
  1340. bool PopupMenu::containsCommandItem (const int commandID) const
  1341. {
  1342. for (int i = items.size(); --i >= 0;)
  1343. {
  1344. const Item& mi = *items.getUnchecked (i);
  1345. if ((mi.itemID == commandID && mi.commandManager != nullptr)
  1346. || (mi.subMenu != nullptr && mi.subMenu->containsCommandItem (commandID)))
  1347. {
  1348. return true;
  1349. }
  1350. }
  1351. return false;
  1352. }
  1353. bool PopupMenu::containsAnyActiveItems() const noexcept
  1354. {
  1355. for (int i = items.size(); --i >= 0;)
  1356. {
  1357. const Item& mi = *items.getUnchecked (i);
  1358. if (mi.subMenu != nullptr)
  1359. {
  1360. if (mi.subMenu->containsAnyActiveItems())
  1361. return true;
  1362. }
  1363. else if (mi.isActive)
  1364. {
  1365. return true;
  1366. }
  1367. }
  1368. return false;
  1369. }
  1370. void PopupMenu::setLookAndFeel (LookAndFeel* const newLookAndFeel)
  1371. {
  1372. lookAndFeel = newLookAndFeel;
  1373. }
  1374. //==============================================================================
  1375. PopupMenu::CustomComponent::CustomComponent (bool autoTrigger)
  1376. : isHighlighted (false),
  1377. triggeredAutomatically (autoTrigger)
  1378. {
  1379. }
  1380. PopupMenu::CustomComponent::~CustomComponent()
  1381. {
  1382. }
  1383. void PopupMenu::CustomComponent::setHighlighted (bool shouldBeHighlighted)
  1384. {
  1385. isHighlighted = shouldBeHighlighted;
  1386. repaint();
  1387. }
  1388. void PopupMenu::CustomComponent::triggerMenuItem()
  1389. {
  1390. if (HelperClasses::ItemComponent* const mic = findParentComponentOfClass<HelperClasses::ItemComponent>())
  1391. {
  1392. if (HelperClasses::MenuWindow* const pmw = mic->findParentComponentOfClass<HelperClasses::MenuWindow>())
  1393. {
  1394. pmw->dismissMenu (&mic->itemInfo);
  1395. }
  1396. else
  1397. {
  1398. // something must have gone wrong with the component hierarchy if this happens..
  1399. jassertfalse;
  1400. }
  1401. }
  1402. else
  1403. {
  1404. // why isn't this component inside a menu? Not much point triggering the item if
  1405. // there's no menu.
  1406. jassertfalse;
  1407. }
  1408. }
  1409. //==============================================================================
  1410. PopupMenu::MenuItemIterator::MenuItemIterator (const PopupMenu& m)
  1411. : subMenu (nullptr),
  1412. itemId (0),
  1413. isSeparator (false),
  1414. isTicked (false),
  1415. isEnabled (false),
  1416. isCustomComponent (false),
  1417. isSectionHeader (false),
  1418. customColour (nullptr),
  1419. menu (m),
  1420. index (0)
  1421. {
  1422. }
  1423. PopupMenu::MenuItemIterator::~MenuItemIterator()
  1424. {
  1425. }
  1426. bool PopupMenu::MenuItemIterator::next()
  1427. {
  1428. if (index >= menu.items.size())
  1429. return false;
  1430. const Item* const item = menu.items.getUnchecked (index);
  1431. ++index;
  1432. if (item->isSeparator && index >= menu.items.size()) // (avoid showing a separator at the end)
  1433. return false;
  1434. itemName = item->customComp != nullptr ? item->customComp->getName() : item->text;
  1435. subMenu = item->subMenu;
  1436. itemId = item->itemID;
  1437. isSeparator = item->isSeparator;
  1438. isTicked = item->isTicked;
  1439. isEnabled = item->isActive;
  1440. isSectionHeader = dynamic_cast<HelperClasses::HeaderItemComponent*> (static_cast<CustomComponent*> (item->customComp)) != nullptr;
  1441. isCustomComponent = (! isSectionHeader) && item->customComp != nullptr;
  1442. customColour = item->usesColour ? &(item->textColour) : nullptr;
  1443. icon = item->iconDrawable;
  1444. commandManager = item->commandManager;
  1445. return true;
  1446. }
  1447. void PopupMenu::MenuItemIterator::addItemTo (PopupMenu& targetMenu)
  1448. {
  1449. targetMenu.items.add (new Item (itemId, itemName, isEnabled, isTicked, icon != nullptr ? icon->createCopy() : nullptr,
  1450. customColour != nullptr ? *customColour : Colours::black,
  1451. customColour != nullptr, nullptr, subMenu, commandManager));
  1452. }