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.

108 lines
4.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. A component which lists all menu items and groups them into categories
  24. by their respective parent menus. This kind of component is often used
  25. for so-called "burger" menus in mobile apps.
  26. @see MenuBarModel
  27. @tags{GUI}
  28. */
  29. class BurgerMenuComponent : public Component,
  30. private ListBoxModel,
  31. private MenuBarModel::Listener
  32. {
  33. public:
  34. //==============================================================================
  35. /** Creates a burger menu component.
  36. @param model the model object to use to control this burger menu. You can
  37. set the parameter or pass nullptr into this if you like,
  38. and set the model later using the setModel() method.
  39. @see setModel
  40. */
  41. BurgerMenuComponent (MenuBarModel* model = nullptr);
  42. /** Destructor. */
  43. ~BurgerMenuComponent();
  44. //==============================================================================
  45. /** Changes the model object to use to control the burger menu.
  46. This can be a nullptr, in which case the bar will be empty. This object will not be
  47. owned by the BurgerMenuComponent so it is up to you to manage its lifetime.
  48. Don't delete the object that is passed-in while it's still being used by this MenuBar.
  49. Any submenus in your MenuBarModel will be recursively flattened and added to the
  50. top-level burger menu section.
  51. */
  52. void setModel (MenuBarModel* newModel);
  53. /** Returns the current burger menu model being used. */
  54. MenuBarModel* getModel() const noexcept;
  55. private:
  56. //==============================================================================
  57. struct Row
  58. {
  59. bool isMenuHeader;
  60. int topLevelMenuIndex;
  61. PopupMenu::Item item;
  62. };
  63. void refresh();
  64. void paint (Graphics&) override;
  65. int getNumRows() override;
  66. void paintListBoxItem (int, Graphics&, int, int, bool) override;
  67. void listBoxItemClicked (int, const MouseEvent&) override;
  68. Component* refreshComponentForRow (int, bool, Component*) override;
  69. void resized() override;
  70. void menuBarItemsChanged (MenuBarModel*) override;
  71. void menuCommandInvoked (MenuBarModel*, const ApplicationCommandTarget::InvocationInfo&) override;
  72. void mouseUp (const MouseEvent&) override;
  73. void handleCommandMessage (int) override;
  74. void addMenuBarItemsForMenu (PopupMenu&, int);
  75. static bool hasSubMenu (const PopupMenu::Item&);
  76. //==============================================================================
  77. MenuBarModel* model = nullptr;
  78. ListBox listBox {"BurgerMenuListBox", this};
  79. Array<Row> rows;
  80. int lastRowClicked = -1, inputSourceIndexOfLastClick = -1, topLevelIndexClicked = -1;
  81. //==============================================================================
  82. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BurgerMenuComponent)
  83. };
  84. } // namespace juce