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.

1442 lines
46KB

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