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.

1430 lines
45KB

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