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.

1395 lines
43KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-7 by Raw Material Software ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the
  7. GNU General Public License, as published by the Free Software Foundation;
  8. either version 2 of the License, or (at your option) any later version.
  9. JUCE is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with JUCE; if not, visit www.gnu.org/licenses or write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. ------------------------------------------------------------------------------
  18. If you'd like to release a closed-source product which uses JUCE, commercial
  19. licenses are also available: visit www.rawmaterialsoftware.com/juce for
  20. more information.
  21. ==============================================================================
  22. */
  23. #include "../../../../juce_core/basics/juce_StandardHeader.h"
  24. BEGIN_JUCE_NAMESPACE
  25. #include "juce_Slider.h"
  26. #include "../lookandfeel/juce_LookAndFeel.h"
  27. #include "../menus/juce_PopupMenu.h"
  28. #include "../juce_Desktop.h"
  29. #include "../special/juce_BubbleComponent.h"
  30. #include "../../../../juce_core/text/juce_LocalisedStrings.h"
  31. //==============================================================================
  32. class SliderPopupDisplayComponent : public BubbleComponent
  33. {
  34. public:
  35. //==============================================================================
  36. SliderPopupDisplayComponent (Slider* const owner_)
  37. : owner (owner_),
  38. font (15.0f, Font::bold)
  39. {
  40. setAlwaysOnTop (true);
  41. }
  42. ~SliderPopupDisplayComponent()
  43. {
  44. }
  45. void paintContent (Graphics& g, int w, int h)
  46. {
  47. g.setFont (font);
  48. g.setColour (Colours::black);
  49. g.drawFittedText (text, 0, 0, w, h, Justification::centred, 1);
  50. }
  51. void getContentSize (int& w, int& h)
  52. {
  53. w = font.getStringWidth (text) + 18;
  54. h = (int) (font.getHeight() * 1.6f);
  55. }
  56. void updatePosition (const String& newText)
  57. {
  58. if (text != newText)
  59. {
  60. text = newText;
  61. repaint();
  62. }
  63. BubbleComponent::setPosition (owner);
  64. }
  65. //==============================================================================
  66. juce_UseDebuggingNewOperator
  67. private:
  68. Slider* owner;
  69. Font font;
  70. String text;
  71. SliderPopupDisplayComponent (const SliderPopupDisplayComponent&);
  72. const SliderPopupDisplayComponent& operator= (const SliderPopupDisplayComponent&);
  73. };
  74. //==============================================================================
  75. Slider::Slider (const String& name)
  76. : Component (name),
  77. listeners (2),
  78. currentValue (0.0),
  79. valueMin (0.0),
  80. valueMax (0.0),
  81. minimum (0),
  82. maximum (10),
  83. interval (0),
  84. skewFactor (1.0),
  85. velocityModeSensitivity (1.0),
  86. velocityModeOffset (0.0),
  87. velocityModeThreshold (1),
  88. rotaryStart (float_Pi * 1.2f),
  89. rotaryEnd (float_Pi * 2.8f),
  90. numDecimalPlaces (7),
  91. sliderRegionStart (0),
  92. sliderRegionSize (1),
  93. sliderBeingDragged (-1),
  94. pixelsForFullDragExtent (250),
  95. style (LinearHorizontal),
  96. textBoxPos (TextBoxLeft),
  97. textBoxWidth (80),
  98. textBoxHeight (20),
  99. incDecButtonMode (incDecButtonsNotDraggable),
  100. editableText (true),
  101. doubleClickToValue (false),
  102. isVelocityBased (false),
  103. userKeyOverridesVelocity (true),
  104. rotaryStop (true),
  105. incDecButtonsSideBySide (false),
  106. sendChangeOnlyOnRelease (false),
  107. popupDisplayEnabled (false),
  108. menuEnabled (false),
  109. menuShown (false),
  110. scrollWheelEnabled (true),
  111. snapsToMousePos (true),
  112. valueBox (0),
  113. incButton (0),
  114. decButton (0),
  115. popupDisplay (0),
  116. parentForPopupDisplay (0)
  117. {
  118. setWantsKeyboardFocus (false);
  119. setRepaintsOnMouseActivity (true);
  120. lookAndFeelChanged();
  121. updateText();
  122. }
  123. Slider::~Slider()
  124. {
  125. deleteAndZero (popupDisplay);
  126. deleteAllChildren();
  127. }
  128. //==============================================================================
  129. void Slider::handleAsyncUpdate()
  130. {
  131. cancelPendingUpdate();
  132. for (int i = listeners.size(); --i >= 0;)
  133. {
  134. ((SliderListener*) listeners.getUnchecked (i))->sliderValueChanged (this);
  135. i = jmin (i, listeners.size());
  136. }
  137. }
  138. void Slider::sendDragStart()
  139. {
  140. startedDragging();
  141. for (int i = listeners.size(); --i >= 0;)
  142. {
  143. ((SliderListener*) listeners.getUnchecked (i))->sliderDragStarted (this);
  144. i = jmin (i, listeners.size());
  145. }
  146. }
  147. void Slider::sendDragEnd()
  148. {
  149. stoppedDragging();
  150. sliderBeingDragged = -1;
  151. for (int i = listeners.size(); --i >= 0;)
  152. {
  153. ((SliderListener*) listeners.getUnchecked (i))->sliderDragEnded (this);
  154. i = jmin (i, listeners.size());
  155. }
  156. }
  157. void Slider::addListener (SliderListener* const listener) throw()
  158. {
  159. jassert (listener != 0);
  160. if (listener != 0)
  161. listeners.add (listener);
  162. }
  163. void Slider::removeListener (SliderListener* const listener) throw()
  164. {
  165. listeners.removeValue (listener);
  166. }
  167. //==============================================================================
  168. void Slider::setSliderStyle (const SliderStyle newStyle)
  169. {
  170. if (style != newStyle)
  171. {
  172. style = newStyle;
  173. repaint();
  174. lookAndFeelChanged();
  175. }
  176. }
  177. void Slider::setRotaryParameters (const float startAngleRadians,
  178. const float endAngleRadians,
  179. const bool stopAtEnd)
  180. {
  181. // make sure the values are sensible..
  182. jassert (rotaryStart >= 0 && rotaryEnd >= 0);
  183. jassert (rotaryStart < float_Pi * 4.0f && rotaryEnd < float_Pi * 4.0f);
  184. jassert (rotaryStart < rotaryEnd);
  185. rotaryStart = startAngleRadians;
  186. rotaryEnd = endAngleRadians;
  187. rotaryStop = stopAtEnd;
  188. }
  189. void Slider::setVelocityBasedMode (const bool velBased) throw()
  190. {
  191. isVelocityBased = velBased;
  192. }
  193. void Slider::setVelocityModeParameters (const double sensitivity,
  194. const int threshold,
  195. const double offset,
  196. const bool userCanPressKeyToSwapMode) throw()
  197. {
  198. jassert (threshold >= 0);
  199. jassert (sensitivity > 0);
  200. jassert (offset >= 0);
  201. velocityModeSensitivity = sensitivity;
  202. velocityModeOffset = offset;
  203. velocityModeThreshold = threshold;
  204. userKeyOverridesVelocity = userCanPressKeyToSwapMode;
  205. }
  206. void Slider::setSkewFactor (const double factor) throw()
  207. {
  208. skewFactor = factor;
  209. }
  210. void Slider::setSkewFactorFromMidPoint (const double sliderValueToShowAtMidPoint) throw()
  211. {
  212. if (maximum > minimum)
  213. skewFactor = log (0.5) / log ((sliderValueToShowAtMidPoint - minimum)
  214. / (maximum - minimum));
  215. }
  216. void Slider::setMouseDragSensitivity (const int distanceForFullScaleDrag)
  217. {
  218. jassert (distanceForFullScaleDrag > 0);
  219. pixelsForFullDragExtent = distanceForFullScaleDrag;
  220. }
  221. void Slider::setIncDecButtonsMode (const IncDecButtonMode mode)
  222. {
  223. if (incDecButtonMode != mode)
  224. {
  225. incDecButtonMode = mode;
  226. lookAndFeelChanged();
  227. }
  228. }
  229. void Slider::setTextBoxStyle (const TextEntryBoxPosition newPosition,
  230. const bool isReadOnly,
  231. const int textEntryBoxWidth,
  232. const int textEntryBoxHeight)
  233. {
  234. textBoxPos = newPosition;
  235. editableText = ! isReadOnly;
  236. textBoxWidth = textEntryBoxWidth;
  237. textBoxHeight = textEntryBoxHeight;
  238. repaint();
  239. lookAndFeelChanged();
  240. }
  241. void Slider::setTextBoxIsEditable (const bool shouldBeEditable) throw()
  242. {
  243. editableText = shouldBeEditable;
  244. if (valueBox != 0)
  245. valueBox->setEditable (shouldBeEditable && isEnabled());
  246. }
  247. void Slider::showTextBox()
  248. {
  249. jassert (editableText); // this should probably be avoided in read-only sliders.
  250. if (valueBox != 0)
  251. valueBox->showEditor();
  252. }
  253. void Slider::hideTextBox (const bool discardCurrentEditorContents)
  254. {
  255. if (valueBox != 0)
  256. {
  257. valueBox->hideEditor (discardCurrentEditorContents);
  258. if (discardCurrentEditorContents)
  259. updateText();
  260. }
  261. }
  262. void Slider::setChangeNotificationOnlyOnRelease (const bool onlyNotifyOnRelease) throw()
  263. {
  264. sendChangeOnlyOnRelease = onlyNotifyOnRelease;
  265. }
  266. void Slider::setSliderSnapsToMousePosition (const bool shouldSnapToMouse) throw()
  267. {
  268. snapsToMousePos = shouldSnapToMouse;
  269. }
  270. void Slider::setPopupDisplayEnabled (const bool enabled,
  271. Component* const parentComponentToUse) throw()
  272. {
  273. popupDisplayEnabled = enabled;
  274. parentForPopupDisplay = parentComponentToUse;
  275. }
  276. //==============================================================================
  277. void Slider::colourChanged()
  278. {
  279. lookAndFeelChanged();
  280. }
  281. void Slider::lookAndFeelChanged()
  282. {
  283. const String previousTextBoxContent (valueBox != 0 ? valueBox->getText()
  284. : getTextFromValue (currentValue));
  285. deleteAllChildren();
  286. valueBox = 0;
  287. LookAndFeel& lf = getLookAndFeel();
  288. if (textBoxPos != NoTextBox)
  289. {
  290. addAndMakeVisible (valueBox = getLookAndFeel().createSliderTextBox (*this));
  291. valueBox->setWantsKeyboardFocus (false);
  292. valueBox->setText (previousTextBoxContent, false);
  293. valueBox->setEditable (editableText && isEnabled());
  294. valueBox->addListener (this);
  295. if (style == LinearBar)
  296. valueBox->addMouseListener (this, false);
  297. valueBox->setTooltip (getTooltip());
  298. }
  299. if (style == IncDecButtons)
  300. {
  301. addAndMakeVisible (incButton = lf.createSliderButton (true));
  302. incButton->addButtonListener (this);
  303. addAndMakeVisible (decButton = lf.createSliderButton (false));
  304. decButton->addButtonListener (this);
  305. if (incDecButtonMode != incDecButtonsNotDraggable)
  306. {
  307. incButton->addMouseListener (this, false);
  308. decButton->addMouseListener (this, false);
  309. }
  310. else
  311. {
  312. incButton->setRepeatSpeed (300, 100, 20);
  313. incButton->addMouseListener (decButton, false);
  314. decButton->setRepeatSpeed (300, 100, 20);
  315. decButton->addMouseListener (incButton, false);
  316. }
  317. }
  318. setComponentEffect (lf.getSliderEffect());
  319. resized();
  320. repaint();
  321. }
  322. //==============================================================================
  323. void Slider::setRange (const double newMin,
  324. const double newMax,
  325. const double newInt)
  326. {
  327. if (minimum != newMin
  328. || maximum != newMax
  329. || interval != newInt)
  330. {
  331. minimum = newMin;
  332. maximum = newMax;
  333. interval = newInt;
  334. // figure out the number of DPs needed to display all values at this
  335. // interval setting.
  336. numDecimalPlaces = 7;
  337. if (newInt != 0)
  338. {
  339. int v = abs ((int) (newInt * 10000000));
  340. while ((v % 10) == 0)
  341. {
  342. --numDecimalPlaces;
  343. v /= 10;
  344. }
  345. }
  346. // keep the current values inside the new range..
  347. if (style != TwoValueHorizontal && style != TwoValueVertical)
  348. {
  349. setValue (currentValue, false, false);
  350. }
  351. else
  352. {
  353. setMinValue (getMinValue(), false, false);
  354. setMaxValue (getMaxValue(), false, false);
  355. }
  356. updateText();
  357. }
  358. }
  359. void Slider::triggerChangeMessage (const bool synchronous)
  360. {
  361. if (synchronous)
  362. handleAsyncUpdate();
  363. else
  364. triggerAsyncUpdate();
  365. valueChanged();
  366. }
  367. double Slider::getValue() const throw()
  368. {
  369. // for a two-value style slider, you should use the getMinValue() and getMaxValue()
  370. // methods to get the two values.
  371. jassert (style != TwoValueHorizontal && style != TwoValueVertical);
  372. return currentValue;
  373. }
  374. void Slider::setValue (double newValue,
  375. const bool sendUpdateMessage,
  376. const bool sendMessageSynchronously)
  377. {
  378. // for a two-value style slider, you should use the setMinValue() and setMaxValue()
  379. // methods to set the two values.
  380. jassert (style != TwoValueHorizontal && style != TwoValueVertical);
  381. newValue = constrainedValue (newValue);
  382. if (style == ThreeValueHorizontal || style == ThreeValueVertical)
  383. {
  384. jassert (valueMin <= valueMax);
  385. newValue = jlimit (valueMin, valueMax, newValue);
  386. }
  387. if (currentValue != newValue)
  388. {
  389. if (valueBox != 0)
  390. valueBox->hideEditor (true);
  391. currentValue = newValue;
  392. updateText();
  393. repaint();
  394. if (popupDisplay != 0)
  395. {
  396. ((SliderPopupDisplayComponent*) popupDisplay)->updatePosition (getTextFromValue (currentValue));
  397. popupDisplay->repaint();
  398. }
  399. if (sendUpdateMessage)
  400. triggerChangeMessage (sendMessageSynchronously);
  401. }
  402. }
  403. double Slider::getMinValue() const throw()
  404. {
  405. // The minimum value only applies to sliders that are in two- or three-value mode.
  406. jassert (style == TwoValueHorizontal || style == TwoValueVertical
  407. || style == ThreeValueHorizontal || style == ThreeValueVertical);
  408. return valueMin;
  409. }
  410. double Slider::getMaxValue() const throw()
  411. {
  412. // The maximum value only applies to sliders that are in two- or three-value mode.
  413. jassert (style == TwoValueHorizontal || style == TwoValueVertical
  414. || style == ThreeValueHorizontal || style == ThreeValueVertical);
  415. return valueMax;
  416. }
  417. void Slider::setMinValue (double newValue, const bool sendUpdateMessage, const bool sendMessageSynchronously)
  418. {
  419. // The minimum value only applies to sliders that are in two- or three-value mode.
  420. jassert (style == TwoValueHorizontal || style == TwoValueVertical
  421. || style == ThreeValueHorizontal || style == ThreeValueVertical);
  422. newValue = constrainedValue (newValue);
  423. if (style == TwoValueHorizontal || style == TwoValueVertical)
  424. newValue = jmin (valueMax, newValue);
  425. else
  426. newValue = jmin (currentValue, newValue);
  427. if (valueMin != newValue)
  428. {
  429. valueMin = newValue;
  430. repaint();
  431. if (popupDisplay != 0)
  432. {
  433. ((SliderPopupDisplayComponent*) popupDisplay)->updatePosition (getTextFromValue (valueMin));
  434. popupDisplay->repaint();
  435. }
  436. if (sendUpdateMessage)
  437. triggerChangeMessage (sendMessageSynchronously);
  438. }
  439. }
  440. void Slider::setMaxValue (double newValue, const bool sendUpdateMessage, const bool sendMessageSynchronously)
  441. {
  442. // The maximum value only applies to sliders that are in two- or three-value mode.
  443. jassert (style == TwoValueHorizontal || style == TwoValueVertical
  444. || style == ThreeValueHorizontal || style == ThreeValueVertical);
  445. newValue = constrainedValue (newValue);
  446. if (style == TwoValueHorizontal || style == TwoValueVertical)
  447. newValue = jmax (valueMin, newValue);
  448. else
  449. newValue = jmax (currentValue, newValue);
  450. if (valueMax != newValue)
  451. {
  452. valueMax = newValue;
  453. repaint();
  454. if (popupDisplay != 0)
  455. {
  456. ((SliderPopupDisplayComponent*) popupDisplay)->updatePosition (getTextFromValue (valueMax));
  457. popupDisplay->repaint();
  458. }
  459. if (sendUpdateMessage)
  460. triggerChangeMessage (sendMessageSynchronously);
  461. }
  462. }
  463. void Slider::setDoubleClickReturnValue (const bool isDoubleClickEnabled,
  464. const double valueToSetOnDoubleClick) throw()
  465. {
  466. doubleClickToValue = isDoubleClickEnabled;
  467. doubleClickReturnValue = valueToSetOnDoubleClick;
  468. }
  469. double Slider::getDoubleClickReturnValue (bool& isEnabled_) const throw()
  470. {
  471. isEnabled_ = doubleClickToValue;
  472. return doubleClickReturnValue;
  473. }
  474. void Slider::updateText()
  475. {
  476. if (valueBox != 0)
  477. valueBox->setText (getTextFromValue (currentValue), false);
  478. }
  479. void Slider::setTextValueSuffix (const String& suffix)
  480. {
  481. if (textSuffix != suffix)
  482. {
  483. textSuffix = suffix;
  484. updateText();
  485. }
  486. }
  487. const String Slider::getTextFromValue (double v)
  488. {
  489. if (numDecimalPlaces > 0)
  490. return String (v, numDecimalPlaces) + textSuffix;
  491. else
  492. return String (roundDoubleToInt (v)) + textSuffix;
  493. }
  494. double Slider::getValueFromText (const String& text)
  495. {
  496. String t (text.trimStart());
  497. if (t.endsWith (textSuffix))
  498. t = t.substring (0, t.length() - textSuffix.length());
  499. while (t.startsWithChar (T('+')))
  500. t = t.substring (1).trimStart();
  501. return t.initialSectionContainingOnly (T("0123456789.,-"))
  502. .getDoubleValue();
  503. }
  504. double Slider::proportionOfLengthToValue (double proportion)
  505. {
  506. if (skewFactor != 1.0 && proportion > 0.0)
  507. proportion = exp (log (proportion) / skewFactor);
  508. return minimum + (maximum - minimum) * proportion;
  509. }
  510. double Slider::valueToProportionOfLength (double value)
  511. {
  512. const double n = (value - minimum) / (maximum - minimum);
  513. return skewFactor == 1.0 ? n : pow (n, skewFactor);
  514. }
  515. double Slider::snapValue (double attemptedValue, const bool)
  516. {
  517. return attemptedValue;
  518. }
  519. //==============================================================================
  520. void Slider::startedDragging()
  521. {
  522. }
  523. void Slider::stoppedDragging()
  524. {
  525. }
  526. void Slider::valueChanged()
  527. {
  528. }
  529. //==============================================================================
  530. void Slider::enablementChanged()
  531. {
  532. repaint();
  533. }
  534. void Slider::setPopupMenuEnabled (const bool menuEnabled_) throw()
  535. {
  536. menuEnabled = menuEnabled_;
  537. }
  538. void Slider::setScrollWheelEnabled (const bool enabled) throw()
  539. {
  540. scrollWheelEnabled = enabled;
  541. }
  542. //==============================================================================
  543. void Slider::labelTextChanged (Label* label)
  544. {
  545. const double newValue = snapValue (getValueFromText (label->getText()), false);
  546. if (getValue() != newValue)
  547. {
  548. sendDragStart();
  549. setValue (newValue, true, true);
  550. sendDragEnd();
  551. }
  552. updateText(); // force a clean-up of the text, needed in case setValue() hasn't done this.
  553. }
  554. void Slider::buttonClicked (Button* button)
  555. {
  556. if (style == IncDecButtons)
  557. {
  558. sendDragStart();
  559. if (button == incButton)
  560. setValue (snapValue (getValue() + interval, false), true, true);
  561. else if (button == decButton)
  562. setValue (snapValue (getValue() - interval, false), true, true);
  563. sendDragEnd();
  564. }
  565. }
  566. //==============================================================================
  567. double Slider::constrainedValue (double value) const throw()
  568. {
  569. if (interval > 0)
  570. value = minimum + interval * floor ((value - minimum) / interval + 0.5);
  571. if (value <= minimum || maximum <= minimum)
  572. value = minimum;
  573. else if (value >= maximum)
  574. value = maximum;
  575. return value;
  576. }
  577. float Slider::getLinearSliderPos (const double value)
  578. {
  579. double sliderPosProportional;
  580. if (maximum > minimum)
  581. {
  582. if (value < minimum)
  583. {
  584. sliderPosProportional = 0.0;
  585. }
  586. else if (value > maximum)
  587. {
  588. sliderPosProportional = 1.0;
  589. }
  590. else
  591. {
  592. sliderPosProportional = valueToProportionOfLength (value);
  593. jassert (sliderPosProportional >= 0 && sliderPosProportional <= 1.0);
  594. }
  595. }
  596. else
  597. {
  598. sliderPosProportional = 0.5;
  599. }
  600. if (isVertical() || style == IncDecButtons)
  601. sliderPosProportional = 1.0 - sliderPosProportional;
  602. return (float) (sliderRegionStart + sliderPosProportional * sliderRegionSize);
  603. }
  604. bool Slider::isHorizontal() const throw()
  605. {
  606. return style == LinearHorizontal
  607. || style == LinearBar
  608. || style == TwoValueHorizontal
  609. || style == ThreeValueHorizontal;
  610. }
  611. bool Slider::isVertical() const throw()
  612. {
  613. return style == LinearVertical
  614. || style == TwoValueVertical
  615. || style == ThreeValueVertical;
  616. }
  617. bool Slider::incDecDragDirectionIsHorizontal() const throw()
  618. {
  619. return incDecButtonMode == incDecButtonsDraggable_Horizontal
  620. || (incDecButtonMode == incDecButtonsDraggable_AutoDirection && incDecButtonsSideBySide);
  621. }
  622. float Slider::getPositionOfValue (const double value)
  623. {
  624. if (isHorizontal() || isVertical())
  625. {
  626. return getLinearSliderPos (value);
  627. }
  628. else
  629. {
  630. jassertfalse // not a valid call on a slider that doesn't work linearly!
  631. return 0.0f;
  632. }
  633. }
  634. //==============================================================================
  635. void Slider::paint (Graphics& g)
  636. {
  637. if (style != IncDecButtons)
  638. {
  639. if (style == Rotary || style == RotaryHorizontalDrag || style == RotaryVerticalDrag)
  640. {
  641. const float sliderPos = (float) valueToProportionOfLength (currentValue);
  642. jassert (sliderPos >= 0 && sliderPos <= 1.0f);
  643. getLookAndFeel().drawRotarySlider (g,
  644. sliderRect.getX(),
  645. sliderRect.getY(),
  646. sliderRect.getWidth(),
  647. sliderRect.getHeight(),
  648. sliderPos,
  649. rotaryStart, rotaryEnd,
  650. *this);
  651. }
  652. else
  653. {
  654. getLookAndFeel().drawLinearSlider (g,
  655. sliderRect.getX(),
  656. sliderRect.getY(),
  657. sliderRect.getWidth(),
  658. sliderRect.getHeight(),
  659. getLinearSliderPos (currentValue),
  660. getLinearSliderPos (valueMin),
  661. getLinearSliderPos (valueMax),
  662. style,
  663. *this);
  664. }
  665. if (style == LinearBar && valueBox == 0)
  666. {
  667. g.setColour (findColour (Slider::textBoxOutlineColourId));
  668. g.drawRect (0, 0, getWidth(), getHeight(), 1);
  669. }
  670. }
  671. }
  672. void Slider::resized()
  673. {
  674. int minXSpace = 0;
  675. int minYSpace = 0;
  676. if (textBoxPos == TextBoxLeft || textBoxPos == TextBoxRight)
  677. minXSpace = 30;
  678. else
  679. minYSpace = 15;
  680. const int tbw = jmax (0, jmin (textBoxWidth, getWidth() - minXSpace));
  681. const int tbh = jmax (0, jmin (textBoxHeight, getHeight() - minYSpace));
  682. if (style == LinearBar)
  683. {
  684. if (valueBox != 0)
  685. valueBox->setBounds (0, 0, getWidth(), getHeight());
  686. }
  687. else
  688. {
  689. if (textBoxPos == NoTextBox)
  690. {
  691. sliderRect.setBounds (0, 0, getWidth(), getHeight());
  692. }
  693. else if (textBoxPos == TextBoxLeft)
  694. {
  695. valueBox->setBounds (0, (getHeight() - tbh) / 2, tbw, tbh);
  696. sliderRect.setBounds (tbw, 0, getWidth() - tbw, getHeight());
  697. }
  698. else if (textBoxPos == TextBoxRight)
  699. {
  700. valueBox->setBounds (getWidth() - tbw, (getHeight() - tbh) / 2, tbw, tbh);
  701. sliderRect.setBounds (0, 0, getWidth() - tbw, getHeight());
  702. }
  703. else if (textBoxPos == TextBoxAbove)
  704. {
  705. valueBox->setBounds ((getWidth() - tbw) / 2, 0, tbw, tbh);
  706. sliderRect.setBounds (0, tbh, getWidth(), getHeight() - tbh);
  707. }
  708. else if (textBoxPos == TextBoxBelow)
  709. {
  710. valueBox->setBounds ((getWidth() - tbw) / 2, getHeight() - tbh, tbw, tbh);
  711. sliderRect.setBounds (0, 0, getWidth(), getHeight() - tbh);
  712. }
  713. }
  714. const int indent = getLookAndFeel().getSliderThumbRadius (*this);
  715. if (style == LinearBar)
  716. {
  717. const int barIndent = 1;
  718. sliderRegionStart = barIndent;
  719. sliderRegionSize = getWidth() - barIndent * 2;
  720. sliderRect.setBounds (sliderRegionStart, barIndent,
  721. sliderRegionSize, getHeight() - barIndent * 2);
  722. }
  723. else if (isHorizontal())
  724. {
  725. sliderRegionStart = sliderRect.getX() + indent;
  726. sliderRegionSize = jmax (1, sliderRect.getWidth() - indent * 2);
  727. sliderRect.setBounds (sliderRegionStart, sliderRect.getY(),
  728. sliderRegionSize, sliderRect.getHeight());
  729. }
  730. else if (isVertical())
  731. {
  732. sliderRegionStart = sliderRect.getY() + indent;
  733. sliderRegionSize = jmax (1, sliderRect.getHeight() - indent * 2);
  734. sliderRect.setBounds (sliderRect.getX(), sliderRegionStart,
  735. sliderRect.getWidth(), sliderRegionSize);
  736. }
  737. else
  738. {
  739. sliderRegionStart = 0;
  740. sliderRegionSize = 100;
  741. }
  742. if (style == IncDecButtons)
  743. {
  744. Rectangle buttonRect (sliderRect);
  745. if (textBoxPos == TextBoxLeft || textBoxPos == TextBoxRight)
  746. buttonRect.expand (-2, 0);
  747. else
  748. buttonRect.expand (0, -2);
  749. incDecButtonsSideBySide = buttonRect.getWidth() > buttonRect.getHeight();
  750. if (incDecButtonsSideBySide)
  751. {
  752. decButton->setBounds (buttonRect.getX(),
  753. buttonRect.getY(),
  754. buttonRect.getWidth() / 2,
  755. buttonRect.getHeight());
  756. decButton->setConnectedEdges (Button::ConnectedOnRight);
  757. incButton->setBounds (buttonRect.getCentreX(),
  758. buttonRect.getY(),
  759. buttonRect.getWidth() / 2,
  760. buttonRect.getHeight());
  761. incButton->setConnectedEdges (Button::ConnectedOnLeft);
  762. }
  763. else
  764. {
  765. incButton->setBounds (buttonRect.getX(),
  766. buttonRect.getY(),
  767. buttonRect.getWidth(),
  768. buttonRect.getHeight() / 2);
  769. incButton->setConnectedEdges (Button::ConnectedOnBottom);
  770. decButton->setBounds (buttonRect.getX(),
  771. buttonRect.getCentreY(),
  772. buttonRect.getWidth(),
  773. buttonRect.getHeight() / 2);
  774. decButton->setConnectedEdges (Button::ConnectedOnTop);
  775. }
  776. }
  777. }
  778. void Slider::focusOfChildComponentChanged (FocusChangeType)
  779. {
  780. repaint();
  781. }
  782. void Slider::mouseDown (const MouseEvent& e)
  783. {
  784. mouseWasHidden = false;
  785. incDecDragged = false;
  786. if (isEnabled())
  787. {
  788. if (e.mods.isPopupMenu() && menuEnabled)
  789. {
  790. menuShown = true;
  791. PopupMenu m;
  792. m.addItem (1, TRANS ("velocity-sensitive mode"), true, isVelocityBased);
  793. m.addSeparator();
  794. if (style == Rotary || style == RotaryHorizontalDrag || style == RotaryVerticalDrag)
  795. {
  796. PopupMenu rotaryMenu;
  797. rotaryMenu.addItem (2, TRANS ("use circular dragging"), true, style == Rotary);
  798. rotaryMenu.addItem (3, TRANS ("use left-right dragging"), true, style == RotaryHorizontalDrag);
  799. rotaryMenu.addItem (4, TRANS ("use up-down dragging"), true, style == RotaryVerticalDrag);
  800. m.addSubMenu (TRANS ("rotary mode"), rotaryMenu);
  801. }
  802. const int r = m.show();
  803. if (r == 1)
  804. {
  805. setVelocityBasedMode (! isVelocityBased);
  806. }
  807. else if (r == 2)
  808. {
  809. setSliderStyle (Rotary);
  810. }
  811. else if (r == 3)
  812. {
  813. setSliderStyle (RotaryHorizontalDrag);
  814. }
  815. else if (r == 4)
  816. {
  817. setSliderStyle (RotaryVerticalDrag);
  818. }
  819. }
  820. else if (maximum > minimum)
  821. {
  822. menuShown = false;
  823. if (valueBox != 0)
  824. valueBox->hideEditor (true);
  825. sliderBeingDragged = 0;
  826. if (style == TwoValueHorizontal
  827. || style == TwoValueVertical
  828. || style == ThreeValueHorizontal
  829. || style == ThreeValueVertical)
  830. {
  831. const float mousePos = (float) (isVertical() ? e.y : e.x);
  832. const float normalPosDistance = fabsf (getLinearSliderPos (currentValue) - mousePos);
  833. const float minPosDistance = fabsf (getLinearSliderPos (valueMin) - 0.1f - mousePos);
  834. const float maxPosDistance = fabsf (getLinearSliderPos (valueMax) + 0.1f - mousePos);
  835. if (style == TwoValueHorizontal || style == TwoValueVertical)
  836. {
  837. if (maxPosDistance <= minPosDistance)
  838. sliderBeingDragged = 2;
  839. else
  840. sliderBeingDragged = 1;
  841. }
  842. else if (style == ThreeValueHorizontal || style == ThreeValueVertical)
  843. {
  844. if (normalPosDistance >= minPosDistance && maxPosDistance >= minPosDistance)
  845. sliderBeingDragged = 1;
  846. else if (normalPosDistance >= maxPosDistance)
  847. sliderBeingDragged = 2;
  848. }
  849. }
  850. minMaxDiff = valueMax - valueMin;
  851. mouseXWhenLastDragged = e.x;
  852. mouseYWhenLastDragged = e.y;
  853. lastAngle = rotaryStart + (rotaryEnd - rotaryStart)
  854. * valueToProportionOfLength (currentValue);
  855. if (sliderBeingDragged == 2)
  856. valueWhenLastDragged = valueMax;
  857. else if (sliderBeingDragged == 1)
  858. valueWhenLastDragged = valueMin;
  859. else
  860. valueWhenLastDragged = currentValue;
  861. valueOnMouseDown = valueWhenLastDragged;
  862. if (popupDisplayEnabled)
  863. {
  864. SliderPopupDisplayComponent* const popup = new SliderPopupDisplayComponent (this);
  865. popupDisplay = popup;
  866. if (parentForPopupDisplay != 0)
  867. {
  868. parentForPopupDisplay->addChildComponent (popup);
  869. }
  870. else
  871. {
  872. popup->addToDesktop (0);
  873. }
  874. popup->setVisible (true);
  875. }
  876. sendDragStart();
  877. mouseDrag (e);
  878. }
  879. }
  880. }
  881. void Slider::mouseUp (const MouseEvent&)
  882. {
  883. if (isEnabled()
  884. && (! menuShown)
  885. && (maximum > minimum)
  886. && (style != IncDecButtons || incDecDragged))
  887. {
  888. restoreMouseIfHidden();
  889. if (sendChangeOnlyOnRelease && valueOnMouseDown != currentValue)
  890. triggerChangeMessage (false);
  891. sendDragEnd();
  892. deleteAndZero (popupDisplay);
  893. if (style == IncDecButtons)
  894. {
  895. incButton->setState (Button::buttonNormal);
  896. decButton->setState (Button::buttonNormal);
  897. }
  898. }
  899. }
  900. void Slider::restoreMouseIfHidden()
  901. {
  902. if (mouseWasHidden)
  903. {
  904. mouseWasHidden = false;
  905. Component* c = Component::getComponentUnderMouse();
  906. if (c == 0)
  907. c = this;
  908. c->enableUnboundedMouseMovement (false);
  909. const double pos = (sliderBeingDragged == 2) ? getMaxValue()
  910. : ((sliderBeingDragged == 1) ? getMinValue()
  911. : currentValue);
  912. if (style == RotaryHorizontalDrag || style == RotaryVerticalDrag)
  913. {
  914. int x, y, downX, downY;
  915. Desktop::getMousePosition (x, y);
  916. Desktop::getLastMouseDownPosition (downX, downY);
  917. if (style == RotaryHorizontalDrag)
  918. {
  919. const double posDiff = valueToProportionOfLength (pos) - valueToProportionOfLength (valueOnMouseDown);
  920. x = roundDoubleToInt (pixelsForFullDragExtent * posDiff + downX);
  921. y = downY;
  922. }
  923. else
  924. {
  925. const double posDiff = valueToProportionOfLength (valueOnMouseDown) - valueToProportionOfLength (pos);
  926. x = downX;
  927. y = roundDoubleToInt (pixelsForFullDragExtent * posDiff + downY);
  928. }
  929. Desktop::setMousePosition (x, y);
  930. }
  931. else
  932. {
  933. const int pixelPos = (int) getLinearSliderPos (pos);
  934. int x = isHorizontal() ? pixelPos : (getWidth() / 2);
  935. int y = isVertical() ? pixelPos : (getHeight() / 2);
  936. relativePositionToGlobal (x, y);
  937. Desktop::setMousePosition (x, y);
  938. }
  939. }
  940. }
  941. void Slider::modifierKeysChanged (const ModifierKeys& modifiers)
  942. {
  943. if (isEnabled()
  944. && style != IncDecButtons
  945. && style != Rotary
  946. && isVelocityBased == modifiers.isAnyModifierKeyDown())
  947. {
  948. restoreMouseIfHidden();
  949. }
  950. }
  951. static double smallestAngleBetween (double a1, double a2)
  952. {
  953. return jmin (fabs (a1 - a2),
  954. fabs (a1 + double_Pi * 2.0 - a2),
  955. fabs (a2 + double_Pi * 2.0 - a1));
  956. }
  957. void Slider::mouseDrag (const MouseEvent& e)
  958. {
  959. if (isEnabled()
  960. && (! menuShown)
  961. && (maximum > minimum))
  962. {
  963. if (style == Rotary)
  964. {
  965. int dx = e.x - sliderRect.getCentreX();
  966. int dy = e.y - sliderRect.getCentreY();
  967. if (dx * dx + dy * dy > 25)
  968. {
  969. double angle = atan2 ((double) dx, (double) -dy);
  970. while (angle < 0.0)
  971. angle += double_Pi * 2.0;
  972. if (rotaryStop && ! e.mouseWasClicked())
  973. {
  974. if (fabs (angle - lastAngle) > double_Pi)
  975. {
  976. if (angle >= lastAngle)
  977. angle -= double_Pi * 2.0;
  978. else
  979. angle += double_Pi * 2.0;
  980. }
  981. if (angle >= lastAngle)
  982. angle = jmin (angle, (double) jmax (rotaryStart, rotaryEnd));
  983. else
  984. angle = jmax (angle, (double) jmin (rotaryStart, rotaryEnd));
  985. }
  986. else
  987. {
  988. while (angle < rotaryStart)
  989. angle += double_Pi * 2.0;
  990. if (angle > rotaryEnd)
  991. {
  992. if (smallestAngleBetween (angle, rotaryStart) <= smallestAngleBetween (angle, rotaryEnd))
  993. angle = rotaryStart;
  994. else
  995. angle = rotaryEnd;
  996. }
  997. }
  998. const double proportion = (angle - rotaryStart) / (rotaryEnd - rotaryStart);
  999. valueWhenLastDragged = proportionOfLengthToValue (jlimit (0.0, 1.0, proportion));
  1000. lastAngle = angle;
  1001. }
  1002. }
  1003. else
  1004. {
  1005. if (style == LinearBar && e.mouseWasClicked()
  1006. && valueBox != 0 && valueBox->isEditable())
  1007. return;
  1008. if (style == IncDecButtons)
  1009. {
  1010. if (! incDecDragged)
  1011. incDecDragged = e.getDistanceFromDragStart() > 10 && ! e.mouseWasClicked();
  1012. if (! incDecDragged)
  1013. return;
  1014. }
  1015. if ((isVelocityBased == (userKeyOverridesVelocity ? e.mods.testFlags (ModifierKeys::ctrlModifier | ModifierKeys::commandModifier | ModifierKeys::altModifier)
  1016. : false))
  1017. || ((maximum - minimum) / sliderRegionSize < interval))
  1018. {
  1019. const int mousePos = (isHorizontal() || style == RotaryHorizontalDrag) ? e.x : e.y;
  1020. double scaledMousePos = (mousePos - sliderRegionStart) / (double) sliderRegionSize;
  1021. if (style == RotaryHorizontalDrag
  1022. || style == RotaryVerticalDrag
  1023. || style == IncDecButtons
  1024. || ((style == LinearHorizontal || style == LinearVertical || style == LinearBar)
  1025. && ! snapsToMousePos))
  1026. {
  1027. const int mouseDiff = (style == RotaryHorizontalDrag
  1028. || style == LinearHorizontal
  1029. || style == LinearBar
  1030. || (style == IncDecButtons && incDecDragDirectionIsHorizontal()))
  1031. ? e.getDistanceFromDragStartX()
  1032. : -e.getDistanceFromDragStartY();
  1033. double newPos = valueToProportionOfLength (valueOnMouseDown)
  1034. + mouseDiff * (1.0 / pixelsForFullDragExtent);
  1035. valueWhenLastDragged = proportionOfLengthToValue (jlimit (0.0, 1.0, newPos));
  1036. if (style == IncDecButtons)
  1037. {
  1038. incButton->setState (mouseDiff < 0 ? Button::buttonNormal : Button::buttonDown);
  1039. decButton->setState (mouseDiff > 0 ? Button::buttonNormal : Button::buttonDown);
  1040. }
  1041. }
  1042. else
  1043. {
  1044. if (isVertical())
  1045. scaledMousePos = 1.0 - scaledMousePos;
  1046. valueWhenLastDragged = proportionOfLengthToValue (jlimit (0.0, 1.0, scaledMousePos));
  1047. }
  1048. }
  1049. else
  1050. {
  1051. const int mouseDiff = (isHorizontal() || style == RotaryHorizontalDrag
  1052. || (style == IncDecButtons && incDecDragDirectionIsHorizontal()))
  1053. ? e.x - mouseXWhenLastDragged
  1054. : e.y - mouseYWhenLastDragged;
  1055. const double maxSpeed = jmax (200, sliderRegionSize);
  1056. double speed = jlimit (0.0, maxSpeed, (double) abs (mouseDiff));
  1057. if (speed != 0)
  1058. {
  1059. speed = 0.2 * velocityModeSensitivity
  1060. * (1.0 + sin (double_Pi * (1.5 + jmin (0.5, velocityModeOffset
  1061. + jmax (0.0, (double) (speed - velocityModeThreshold))
  1062. / maxSpeed))));
  1063. if (mouseDiff < 0)
  1064. speed = -speed;
  1065. if (isVertical() || style == RotaryVerticalDrag
  1066. || (style == IncDecButtons && ! incDecDragDirectionIsHorizontal()))
  1067. speed = -speed;
  1068. const double currentPos = valueToProportionOfLength (valueWhenLastDragged);
  1069. valueWhenLastDragged = proportionOfLengthToValue (jlimit (0.0, 1.0, currentPos + speed));
  1070. e.originalComponent->enableUnboundedMouseMovement (true, false);
  1071. mouseWasHidden = true;
  1072. }
  1073. }
  1074. }
  1075. valueWhenLastDragged = jlimit (minimum, maximum, valueWhenLastDragged);
  1076. if (sliderBeingDragged == 0)
  1077. {
  1078. setValue (snapValue (valueWhenLastDragged, true),
  1079. ! sendChangeOnlyOnRelease, true);
  1080. }
  1081. else if (sliderBeingDragged == 1)
  1082. {
  1083. setMinValue (snapValue (valueWhenLastDragged, true),
  1084. ! sendChangeOnlyOnRelease, false);
  1085. if (e.mods.isShiftDown())
  1086. setMaxValue (getMinValue() + minMaxDiff, false);
  1087. else
  1088. minMaxDiff = valueMax - valueMin;
  1089. }
  1090. else
  1091. {
  1092. jassert (sliderBeingDragged == 2);
  1093. setMaxValue (snapValue (valueWhenLastDragged, true),
  1094. ! sendChangeOnlyOnRelease, false);
  1095. if (e.mods.isShiftDown())
  1096. setMinValue (getMaxValue() - minMaxDiff, false);
  1097. else
  1098. minMaxDiff = valueMax - valueMin;
  1099. }
  1100. mouseXWhenLastDragged = e.x;
  1101. mouseYWhenLastDragged = e.y;
  1102. }
  1103. }
  1104. void Slider::mouseDoubleClick (const MouseEvent&)
  1105. {
  1106. if (doubleClickToValue
  1107. && isEnabled()
  1108. && style != IncDecButtons
  1109. && minimum <= doubleClickReturnValue
  1110. && maximum >= doubleClickReturnValue)
  1111. {
  1112. sendDragStart();
  1113. setValue (doubleClickReturnValue, true, true);
  1114. sendDragEnd();
  1115. }
  1116. }
  1117. void Slider::mouseWheelMove (const MouseEvent& e, float wheelIncrementX, float wheelIncrementY)
  1118. {
  1119. if (scrollWheelEnabled && isEnabled()
  1120. && style != TwoValueHorizontal
  1121. && style != TwoValueVertical)
  1122. {
  1123. if (maximum > minimum && ! isMouseButtonDownAnywhere())
  1124. {
  1125. if (valueBox != 0)
  1126. valueBox->hideEditor (false);
  1127. const double proportionDelta = (wheelIncrementX != 0 ? -wheelIncrementX : wheelIncrementY) * 0.15f;
  1128. const double currentPos = valueToProportionOfLength (currentValue);
  1129. const double newValue = proportionOfLengthToValue (jlimit (0.0, 1.0, currentPos + proportionDelta));
  1130. double delta = (newValue != currentValue)
  1131. ? jmax (fabs (newValue - currentValue), interval) : 0;
  1132. if (currentValue > newValue)
  1133. delta = -delta;
  1134. sendDragStart();
  1135. setValue (snapValue (currentValue + delta, false), true, true);
  1136. sendDragEnd();
  1137. }
  1138. }
  1139. else
  1140. {
  1141. Component::mouseWheelMove (e, wheelIncrementX, wheelIncrementY);
  1142. }
  1143. }
  1144. void SliderListener::sliderDragStarted (Slider*)
  1145. {
  1146. }
  1147. void SliderListener::sliderDragEnded (Slider*)
  1148. {
  1149. }
  1150. END_JUCE_NAMESPACE