Audio plugin host https://kx.studio/carla
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.

juce_Slider.cpp 57KB

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