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.

517 lines
20KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. #ifndef JUCE_BUTTON_H_INCLUDED
  18. #define JUCE_BUTTON_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. A base class for buttons.
  22. This contains all the logic for button behaviours such as enabling/disabling,
  23. responding to shortcut keystrokes, auto-repeating when held down, toggle-buttons
  24. and radio groups, etc.
  25. @see TextButton, DrawableButton, ToggleButton
  26. */
  27. class JUCE_API Button : public Component,
  28. public SettableTooltipClient
  29. {
  30. protected:
  31. //==============================================================================
  32. /** Creates a button.
  33. @param buttonName the text to put in the button (the component's name is also
  34. initially set to this string, but these can be changed later
  35. using the setName() and setButtonText() methods)
  36. */
  37. explicit Button (const String& buttonName);
  38. public:
  39. /** Destructor. */
  40. virtual ~Button();
  41. //==============================================================================
  42. /** Changes the button's text.
  43. @see getButtonText
  44. */
  45. void setButtonText (const String& newText);
  46. /** Returns the text displayed in the button.
  47. @see setButtonText
  48. */
  49. const String& getButtonText() const { return text; }
  50. //==============================================================================
  51. /** Returns true if the button is currently being held down.
  52. @see isOver
  53. */
  54. bool isDown() const noexcept;
  55. /** Returns true if the mouse is currently over the button.
  56. This will be also be true if the button is being held down.
  57. @see isDown
  58. */
  59. bool isOver() const noexcept;
  60. //==============================================================================
  61. /** A button has an on/off state associated with it, and this changes that.
  62. By default buttons are 'off' and for simple buttons that you click to perform
  63. an action you won't change this. Toggle buttons, however will want to
  64. change their state when turned on or off.
  65. @param shouldBeOn whether to set the button's toggle state to be on or
  66. off. If it's a member of a button group, this will
  67. always try to turn it on, and to turn off any other
  68. buttons in the group
  69. @param notification determines the behaviour if the value changes - this
  70. can invoke a synchronous call to clicked(), but
  71. sendNotificationAsync is not supported
  72. @see getToggleState, setRadioGroupId
  73. */
  74. void setToggleState (bool shouldBeOn, NotificationType notification);
  75. /** Returns true if the button is 'on'.
  76. By default buttons are 'off' and for simple buttons that you click to perform
  77. an action you won't change this. Toggle buttons, however will want to
  78. change their state when turned on or off.
  79. @see setToggleState
  80. */
  81. bool getToggleState() const noexcept { return isOn.getValue(); }
  82. /** Returns the Value object that represents the botton's toggle state.
  83. You can use this Value object to connect the button's state to external values or setters,
  84. either by taking a copy of the Value, or by using Value::referTo() to make it point to
  85. your own Value object.
  86. @see getToggleState, Value
  87. */
  88. Value& getToggleStateValue() noexcept { return isOn; }
  89. /** This tells the button to automatically flip the toggle state when
  90. the button is clicked.
  91. If set to true, then before the clicked() callback occurs, the toggle-state
  92. of the button is flipped.
  93. */
  94. void setClickingTogglesState (bool shouldAutoToggleOnClick) noexcept;
  95. /** Returns true if this button is set to be an automatic toggle-button.
  96. This returns the last value that was passed to setClickingTogglesState().
  97. */
  98. bool getClickingTogglesState() const noexcept;
  99. //==============================================================================
  100. /** Enables the button to act as a member of a mutually-exclusive group
  101. of 'radio buttons'.
  102. If the group ID is set to a non-zero number, then this button will
  103. act as part of a group of buttons with the same ID, only one of
  104. which can be 'on' at the same time. Note that when it's part of
  105. a group, clicking a toggle-button that's 'on' won't turn it off.
  106. To find other buttons with the same ID, this button will search through
  107. its sibling components for ToggleButtons, so all the buttons for a
  108. particular group must be placed inside the same parent component.
  109. Set the group ID back to zero if you want it to act as a normal toggle
  110. button again.
  111. The notification argument lets you specify how other buttons should react
  112. to being turned on or off in response to this call.
  113. @see getRadioGroupId
  114. */
  115. void setRadioGroupId (int newGroupId, NotificationType notification = sendNotification);
  116. /** Returns the ID of the group to which this button belongs.
  117. (See setRadioGroupId() for an explanation of this).
  118. */
  119. int getRadioGroupId() const noexcept { return radioGroupId; }
  120. //==============================================================================
  121. /**
  122. Used to receive callbacks when a button is clicked.
  123. @see Button::addListener, Button::removeListener
  124. */
  125. class JUCE_API Listener
  126. {
  127. public:
  128. /** Destructor. */
  129. virtual ~Listener() {}
  130. /** Called when the button is clicked. */
  131. virtual void buttonClicked (Button*) = 0;
  132. /** Called when the button's state changes. */
  133. virtual void buttonStateChanged (Button*) {}
  134. };
  135. /** Registers a listener to receive events when this button's state changes.
  136. If the listener is already registered, this will not register it again.
  137. @see removeListener
  138. */
  139. void addListener (Listener* newListener);
  140. /** Removes a previously-registered button listener
  141. @see addListener
  142. */
  143. void removeListener (Listener* listener);
  144. //==============================================================================
  145. /** Causes the button to act as if it's been clicked.
  146. This will asynchronously make the button draw itself going down and up, and
  147. will then call back the clicked() method as if mouse was clicked on it.
  148. @see clicked
  149. */
  150. virtual void triggerClick();
  151. //==============================================================================
  152. /** Sets a command ID for this button to automatically invoke when it's clicked.
  153. When the button is pressed, it will use the given manager to trigger the
  154. command ID.
  155. Obviously be careful that the ApplicationCommandManager doesn't get deleted
  156. before this button is. To disable the command triggering, call this method and
  157. pass nullptr as the command manager.
  158. If generateTooltip is true, then the button's tooltip will be automatically
  159. generated based on the name of this command and its current shortcut key.
  160. @see addShortcut, getCommandID
  161. */
  162. void setCommandToTrigger (ApplicationCommandManager* commandManagerToUse,
  163. CommandID commandID,
  164. bool generateTooltip);
  165. /** Returns the command ID that was set by setCommandToTrigger(). */
  166. CommandID getCommandID() const noexcept { return commandID; }
  167. //==============================================================================
  168. /** Assigns a shortcut key to trigger the button.
  169. The button registers itself with its top-level parent component for keypresses.
  170. Note that a different way of linking buttons to keypresses is by using the
  171. setCommandToTrigger() method to invoke a command.
  172. @see clearShortcuts
  173. */
  174. void addShortcut (const KeyPress&);
  175. /** Removes all key shortcuts that had been set for this button.
  176. @see addShortcut
  177. */
  178. void clearShortcuts();
  179. /** Returns true if the given keypress is a shortcut for this button.
  180. @see addShortcut
  181. */
  182. bool isRegisteredForShortcut (const KeyPress&) const;
  183. //==============================================================================
  184. /** Sets an auto-repeat speed for the button when it is held down.
  185. (Auto-repeat is disabled by default).
  186. @param initialDelayInMillisecs how long to wait after the mouse is pressed before
  187. triggering the next click. If this is zero, auto-repeat
  188. is disabled
  189. @param repeatDelayInMillisecs the frequently subsequent repeated clicks should be
  190. triggered
  191. @param minimumDelayInMillisecs if this is greater than 0, the auto-repeat speed will
  192. get faster, the longer the button is held down, up to the
  193. minimum interval specified here
  194. */
  195. void setRepeatSpeed (int initialDelayInMillisecs,
  196. int repeatDelayInMillisecs,
  197. int minimumDelayInMillisecs = -1) noexcept;
  198. /** Sets whether the button click should happen when the mouse is pressed or released.
  199. By default the button is only considered to have been clicked when the mouse is
  200. released, but setting this to true will make it call the clicked() method as soon
  201. as the button is pressed.
  202. This is useful if the button is being used to show a pop-up menu, as it allows
  203. the click to be used as a drag onto the menu.
  204. */
  205. void setTriggeredOnMouseDown (bool isTriggeredOnMouseDown) noexcept;
  206. /** Returns the number of milliseconds since the last time the button
  207. went into the 'down' state.
  208. */
  209. uint32 getMillisecondsSinceButtonDown() const noexcept;
  210. //==============================================================================
  211. /** Sets the tooltip for this button.
  212. @see TooltipClient, TooltipWindow
  213. */
  214. void setTooltip (const String& newTooltip) override;
  215. /** Returns the tooltip set by setTooltip(), or the description corresponding to
  216. the currently mapped command if one is enabled (see setCommandToTrigger).
  217. */
  218. String getTooltip() override;
  219. //==============================================================================
  220. /** A combination of these flags are used by setConnectedEdges(). */
  221. enum ConnectedEdgeFlags
  222. {
  223. ConnectedOnLeft = 1,
  224. ConnectedOnRight = 2,
  225. ConnectedOnTop = 4,
  226. ConnectedOnBottom = 8
  227. };
  228. /** Hints about which edges of the button might be connected to adjoining buttons.
  229. The value passed in is a bitwise combination of any of the values in the
  230. ConnectedEdgeFlags enum.
  231. E.g. if you are placing two buttons adjacent to each other, you could use this to
  232. indicate which edges are touching, and the LookAndFeel might choose to drawn them
  233. without rounded corners on the edges that connect. It's only a hint, so the
  234. LookAndFeel can choose to ignore it if it's not relevent for this type of
  235. button.
  236. */
  237. void setConnectedEdges (int connectedEdgeFlags);
  238. /** Returns the set of flags passed into setConnectedEdges(). */
  239. int getConnectedEdgeFlags() const noexcept { return connectedEdgeFlags; }
  240. /** Indicates whether the button adjoins another one on its left edge.
  241. @see setConnectedEdges
  242. */
  243. bool isConnectedOnLeft() const noexcept { return (connectedEdgeFlags & ConnectedOnLeft) != 0; }
  244. /** Indicates whether the button adjoins another one on its right edge.
  245. @see setConnectedEdges
  246. */
  247. bool isConnectedOnRight() const noexcept { return (connectedEdgeFlags & ConnectedOnRight) != 0; }
  248. /** Indicates whether the button adjoins another one on its top edge.
  249. @see setConnectedEdges
  250. */
  251. bool isConnectedOnTop() const noexcept { return (connectedEdgeFlags & ConnectedOnTop) != 0; }
  252. /** Indicates whether the button adjoins another one on its bottom edge.
  253. @see setConnectedEdges
  254. */
  255. bool isConnectedOnBottom() const noexcept { return (connectedEdgeFlags & ConnectedOnBottom) != 0; }
  256. //==============================================================================
  257. /** Used by setState(). */
  258. enum ButtonState
  259. {
  260. buttonNormal,
  261. buttonOver,
  262. buttonDown
  263. };
  264. /** Can be used to force the button into a particular state.
  265. This only changes the button's appearance, it won't trigger a click, or stop any mouse-clicks
  266. from happening.
  267. The state that you set here will only last until it is automatically changed when the mouse
  268. enters or exits the button, or the mouse-button is pressed or released.
  269. */
  270. void setState (ButtonState newState);
  271. // This method's parameters have changed - see the new version.
  272. JUCE_DEPRECATED (void setToggleState (bool, bool));
  273. //==============================================================================
  274. /** This abstract base class is implemented by LookAndFeel classes to provide
  275. button-drawing functionality.
  276. */
  277. struct JUCE_API LookAndFeelMethods
  278. {
  279. virtual ~LookAndFeelMethods() {}
  280. virtual void drawButtonBackground (Graphics&, Button&, const Colour& backgroundColour,
  281. bool isMouseOverButton, bool isButtonDown) = 0;
  282. virtual Font getTextButtonFont (TextButton&, int buttonHeight) = 0;
  283. virtual int getTextButtonWidthToFitText (TextButton&, int buttonHeight) = 0;
  284. /** Draws the text for a TextButton. */
  285. virtual void drawButtonText (Graphics&, TextButton&, bool isMouseOverButton, bool isButtonDown) = 0;
  286. /** Draws the contents of a standard ToggleButton. */
  287. virtual void drawToggleButton (Graphics&, ToggleButton&, bool isMouseOverButton, bool isButtonDown) = 0;
  288. virtual void changeToggleButtonWidthToFitText (ToggleButton&) = 0;
  289. virtual void drawTickBox (Graphics&, Component&, float x, float y, float w, float h,
  290. bool ticked, bool isEnabled, bool isMouseOverButton, bool isButtonDown) = 0;
  291. virtual void drawDrawableButton (Graphics&, DrawableButton&, bool isMouseOverButton, bool isButtonDown) = 0;
  292. private:
  293. #if JUCE_CATCH_DEPRECATED_CODE_MISUSE
  294. // These method have been deprecated: see their replacements above.
  295. virtual int getTextButtonFont (TextButton&) { return 0; }
  296. virtual int changeTextButtonWidthToFitText (TextButton&, int) { return 0; }
  297. #endif
  298. };
  299. protected:
  300. //==============================================================================
  301. /** This method is called when the button has been clicked.
  302. Subclasses can override this to perform whatever they actions they need
  303. to do.
  304. Alternatively, a ButtonListener can be added to the button, and these listeners
  305. will be called when the click occurs.
  306. @see triggerClick
  307. */
  308. virtual void clicked();
  309. /** This method is called when the button has been clicked.
  310. By default it just calls clicked(), but you might want to override it to handle
  311. things like clicking when a modifier key is pressed, etc.
  312. @see ModifierKeys
  313. */
  314. virtual void clicked (const ModifierKeys& modifiers);
  315. /** Subclasses should override this to actually paint the button's contents.
  316. It's better to use this than the paint method, because it gives you information
  317. about the over/down state of the button.
  318. @param g the graphics context to use
  319. @param isMouseOverButton true if the button is either in the 'over' or
  320. 'down' state
  321. @param isButtonDown true if the button should be drawn in the 'down' position
  322. */
  323. virtual void paintButton (Graphics& g,
  324. bool isMouseOverButton,
  325. bool isButtonDown) = 0;
  326. /** Called when the button's up/down/over state changes.
  327. Subclasses can override this if they need to do something special when the button
  328. goes up or down.
  329. @see isDown, isOver
  330. */
  331. virtual void buttonStateChanged();
  332. //==============================================================================
  333. /** @internal */
  334. virtual void internalClickCallback (const ModifierKeys&);
  335. /** @internal */
  336. void handleCommandMessage (int commandId) override;
  337. /** @internal */
  338. void mouseEnter (const MouseEvent&) override;
  339. /** @internal */
  340. void mouseExit (const MouseEvent&) override;
  341. /** @internal */
  342. void mouseDown (const MouseEvent&) override;
  343. /** @internal */
  344. void mouseDrag (const MouseEvent&) override;
  345. /** @internal */
  346. void mouseUp (const MouseEvent&) override;
  347. /** @internal */
  348. bool keyPressed (const KeyPress&) override;
  349. /** @internal */
  350. using Component::keyStateChanged;
  351. /** @internal */
  352. void paint (Graphics&) override;
  353. /** @internal */
  354. void parentHierarchyChanged() override;
  355. /** @internal */
  356. void visibilityChanged() override;
  357. /** @internal */
  358. void focusGained (FocusChangeType) override;
  359. /** @internal */
  360. void focusLost (FocusChangeType) override;
  361. /** @internal */
  362. void enablementChanged() override;
  363. private:
  364. //==============================================================================
  365. Array<KeyPress> shortcuts;
  366. WeakReference<Component> keySource;
  367. String text;
  368. ListenerList<Listener> buttonListeners;
  369. class CallbackHelper;
  370. friend class CallbackHelper;
  371. friend struct ContainerDeletePolicy<CallbackHelper>;
  372. ScopedPointer<CallbackHelper> callbackHelper;
  373. uint32 buttonPressTime, lastRepeatTime;
  374. ApplicationCommandManager* commandManagerToUse;
  375. int autoRepeatDelay, autoRepeatSpeed, autoRepeatMinimumDelay;
  376. int radioGroupId, connectedEdgeFlags;
  377. CommandID commandID;
  378. ButtonState buttonState;
  379. Value isOn;
  380. bool lastToggleState;
  381. bool clickTogglesState;
  382. bool needsToRelease;
  383. bool needsRepainting;
  384. bool isKeyDown;
  385. bool triggerOnMouseDown;
  386. bool generateTooltip;
  387. void repeatTimerCallback();
  388. bool keyStateChangedCallback();
  389. void applicationCommandListChangeCallback();
  390. ButtonState updateState();
  391. ButtonState updateState (bool isOver, bool isDown);
  392. bool isShortcutPressed() const;
  393. void turnOffOtherButtonsInGroup (NotificationType);
  394. void flashButtonState();
  395. void sendClickMessage (const ModifierKeys&);
  396. void sendStateMessage();
  397. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Button)
  398. };
  399. #ifndef DOXYGEN
  400. /** This typedef is just for compatibility with old code and VC6 - newer code should use Button::Listener instead. */
  401. typedef Button::Listener ButtonListener;
  402. #endif
  403. #endif // JUCE_BUTTON_H_INCLUDED