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.

1573 lines
57KB

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