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.

1370 lines
42KB

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