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.

510 lines
20KB

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