The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

287 lines
8.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. //==============================================================================
  16. struct CustomMenuBarItemHolder : public Component
  17. {
  18. CustomMenuBarItemHolder (const ReferenceCountedObjectPtr<PopupMenu::CustomComponent>& customComponent)
  19. {
  20. setInterceptsMouseClicks (false, true);
  21. update (customComponent);
  22. }
  23. void update (const ReferenceCountedObjectPtr<PopupMenu::CustomComponent>& newComponent)
  24. {
  25. jassert (newComponent != nullptr);
  26. if (newComponent != custom)
  27. {
  28. if (custom != nullptr)
  29. removeChildComponent (custom.get());
  30. custom = newComponent;
  31. addAndMakeVisible (*custom);
  32. resized();
  33. }
  34. }
  35. void resized() override
  36. {
  37. custom->setBounds (getLocalBounds());
  38. }
  39. ReferenceCountedObjectPtr<PopupMenu::CustomComponent> custom;
  40. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CustomMenuBarItemHolder)
  41. };
  42. //==============================================================================
  43. BurgerMenuComponent::BurgerMenuComponent (MenuBarModel* modelToUse)
  44. {
  45. auto& lf = getLookAndFeel();
  46. listBox.setRowHeight (roundToInt (lf.getPopupMenuFont().getHeight() * 2.0f));
  47. listBox.addMouseListener (this, true);
  48. setModel (modelToUse);
  49. addAndMakeVisible (listBox);
  50. }
  51. BurgerMenuComponent::~BurgerMenuComponent()
  52. {
  53. if (model != nullptr)
  54. model->removeListener (this);
  55. }
  56. void BurgerMenuComponent::setModel (MenuBarModel* newModel)
  57. {
  58. if (newModel != model)
  59. {
  60. if (model != nullptr)
  61. model->removeListener (this);
  62. model = newModel;
  63. if (model != nullptr)
  64. model->addListener (this);
  65. refresh();
  66. listBox.updateContent();
  67. }
  68. }
  69. MenuBarModel* BurgerMenuComponent::getModel() const noexcept
  70. {
  71. return model;
  72. }
  73. void BurgerMenuComponent::refresh()
  74. {
  75. lastRowClicked = inputSourceIndexOfLastClick = -1;
  76. rows.clear();
  77. if (model != nullptr)
  78. {
  79. auto menuBarNames = model->getMenuBarNames();
  80. for (auto menuIdx = 0; menuIdx < menuBarNames.size(); ++menuIdx)
  81. {
  82. PopupMenu::Item menuItem;
  83. menuItem.text = menuBarNames[menuIdx];
  84. String ignore;
  85. auto menu = model->getMenuForIndex (menuIdx, ignore);
  86. rows.add (Row { true, menuIdx, menuItem });
  87. addMenuBarItemsForMenu (menu, menuIdx);
  88. }
  89. }
  90. }
  91. void BurgerMenuComponent::addMenuBarItemsForMenu (PopupMenu& menu, int menuIdx)
  92. {
  93. for (PopupMenu::MenuItemIterator it (menu); it.next();)
  94. {
  95. auto& item = it.getItem();
  96. if (item.isSeparator)
  97. continue;
  98. if (hasSubMenu (item))
  99. addMenuBarItemsForMenu (*item.subMenu, menuIdx);
  100. else
  101. rows.add (Row {false, menuIdx, it.getItem()});
  102. }
  103. }
  104. int BurgerMenuComponent::getNumRows()
  105. {
  106. return rows.size();
  107. }
  108. void BurgerMenuComponent::paint (Graphics& g)
  109. {
  110. getLookAndFeel().drawPopupMenuBackground (g, getWidth(), getHeight());
  111. }
  112. void BurgerMenuComponent::paintListBoxItem (int rowIndex, Graphics& g, int w, int h, bool highlight)
  113. {
  114. auto& lf = getLookAndFeel();
  115. Rectangle<int> r (w, h);
  116. auto row = (rowIndex < rows.size() ? rows.getReference (rowIndex)
  117. : Row { true, 0, {} });
  118. g.fillAll (findColour (PopupMenu::backgroundColourId));
  119. if (row.isMenuHeader)
  120. {
  121. lf.drawPopupMenuSectionHeader (g, r.reduced (20, 0), row.item.text);
  122. g.setColour (Colours::grey);
  123. g.fillRect (r.withHeight (1));
  124. }
  125. else
  126. {
  127. auto& item = row.item;
  128. auto* colour = item.colour != Colour() ? &item.colour : nullptr;
  129. if (item.customComponent == nullptr)
  130. lf.drawPopupMenuItem (g, r.reduced (20, 0),
  131. item.isSeparator,
  132. item.isEnabled,
  133. highlight,
  134. item.isTicked,
  135. hasSubMenu (item),
  136. item.text,
  137. item.shortcutKeyDescription,
  138. item.image.get(),
  139. colour);
  140. }
  141. }
  142. bool BurgerMenuComponent::hasSubMenu (const PopupMenu::Item& item)
  143. {
  144. return item.subMenu != nullptr && (item.itemID == 0 || item.subMenu->getNumItems() > 0);
  145. }
  146. void BurgerMenuComponent::listBoxItemClicked (int rowIndex, const MouseEvent& e)
  147. {
  148. auto row = rowIndex < rows.size() ? rows.getReference (rowIndex)
  149. : Row { true, 0, {} };
  150. if (! row.isMenuHeader)
  151. {
  152. lastRowClicked = rowIndex;
  153. inputSourceIndexOfLastClick = e.source.getIndex();
  154. }
  155. }
  156. Component* BurgerMenuComponent::refreshComponentForRow (int rowIndex, bool isRowSelected, Component* existing)
  157. {
  158. auto row = rowIndex < rows.size() ? rows.getReference (rowIndex)
  159. : Row { true, 0, {} };
  160. auto hasCustomComponent = (row.item.customComponent != nullptr);
  161. if (existing == nullptr && hasCustomComponent)
  162. return new CustomMenuBarItemHolder (row.item.customComponent);
  163. if (existing != nullptr)
  164. {
  165. auto* componentToUpdate = dynamic_cast<CustomMenuBarItemHolder*> (existing);
  166. jassert (componentToUpdate != nullptr);
  167. if (hasCustomComponent && componentToUpdate != nullptr)
  168. {
  169. row.item.customComponent->setHighlighted (isRowSelected);
  170. componentToUpdate->update (row.item.customComponent);
  171. }
  172. else
  173. {
  174. delete existing;
  175. existing = nullptr;
  176. }
  177. }
  178. return existing;
  179. }
  180. void BurgerMenuComponent::resized()
  181. {
  182. listBox.setBounds (getLocalBounds());
  183. }
  184. void BurgerMenuComponent::menuBarItemsChanged (MenuBarModel* menuBarModel)
  185. {
  186. setModel (menuBarModel);
  187. }
  188. void BurgerMenuComponent::menuCommandInvoked (MenuBarModel*, const ApplicationCommandTarget::InvocationInfo&)
  189. {
  190. }
  191. void BurgerMenuComponent::mouseUp (const MouseEvent& event)
  192. {
  193. auto rowIndex = listBox.getSelectedRow();
  194. if (rowIndex == lastRowClicked && rowIndex < rows.size()
  195. && event.source.getIndex() == inputSourceIndexOfLastClick)
  196. {
  197. auto& row = rows.getReference (rowIndex);
  198. if (! row.isMenuHeader)
  199. {
  200. listBox.selectRow (-1);
  201. lastRowClicked = -1;
  202. inputSourceIndexOfLastClick = -1;
  203. topLevelIndexClicked = row.topLevelMenuIndex;
  204. auto& item = row.item;
  205. if (auto* managerOfChosenCommand = item.commandManager)
  206. {
  207. ApplicationCommandTarget::InvocationInfo info (item.itemID);
  208. info.invocationMethod = ApplicationCommandTarget::InvocationInfo::fromMenu;
  209. managerOfChosenCommand->invoke (info, true);
  210. }
  211. postCommandMessage (item.itemID);
  212. }
  213. }
  214. }
  215. void BurgerMenuComponent::handleCommandMessage (int commandID)
  216. {
  217. if (model != nullptr)
  218. {
  219. model->menuItemSelected (commandID, topLevelIndexClicked);
  220. topLevelIndexClicked = -1;
  221. refresh();
  222. listBox.updateContent();
  223. }
  224. }
  225. } // namespace juce