DISTRHO Plugin Framework
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.

206 lines
6.1KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2025 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef DGL_EVENT_HANDLERS_HPP_INCLUDED
  17. #define DGL_EVENT_HANDLERS_HPP_INCLUDED
  18. #include "Widget.hpp"
  19. START_NAMESPACE_DGL
  20. /* NOTE none of these classes get assigned to a widget automatically
  21. Manual plugging into Widget events is needed, like so:
  22. ```
  23. bool onMouse(const MouseEvent& ev) override
  24. {
  25. return ButtonEventHandler::mouseEvent(ev);
  26. }
  27. ```
  28. */
  29. // --------------------------------------------------------------------------------------------------------------------
  30. class ButtonEventHandler
  31. {
  32. public:
  33. enum State {
  34. kButtonStateDefault = 0x0,
  35. kButtonStateHover = 0x1,
  36. kButtonStateActive = 0x2,
  37. kButtonStateActiveHover = kButtonStateActive|kButtonStateHover
  38. };
  39. class Callback
  40. {
  41. public:
  42. virtual ~Callback() {}
  43. virtual void buttonClicked(SubWidget* widget, int button) = 0;
  44. };
  45. explicit ButtonEventHandler(SubWidget* self);
  46. virtual ~ButtonEventHandler();
  47. bool isActive() noexcept;
  48. void setActive(bool active, bool sendCallback) noexcept;
  49. bool isChecked() const noexcept;
  50. void setChecked(bool checked, bool sendCallback) noexcept;
  51. bool isCheckable() const noexcept;
  52. void setCheckable(bool checkable) noexcept;
  53. bool isEnabled() const noexcept;
  54. void setEnabled(bool enabled) noexcept;
  55. Point<double> getLastClickPosition() const noexcept;
  56. Point<double> getLastMotionPosition() const noexcept;
  57. void setCallback(Callback* callback) noexcept;
  58. bool mouseEvent(const Widget::MouseEvent& ev);
  59. bool motionEvent(const Widget::MotionEvent& ev);
  60. protected:
  61. State getState() const noexcept;
  62. void clearState() noexcept;
  63. virtual void stateChanged(State state, State oldState);
  64. void setInternalCallback(Callback* callback) noexcept;
  65. void triggerUserCallback(SubWidget* widget, int button);
  66. private:
  67. struct PrivateData;
  68. PrivateData* const pData;
  69. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ButtonEventHandler)
  70. };
  71. // --------------------------------------------------------------------------------------------------------------------
  72. class KnobEventHandler
  73. {
  74. public:
  75. enum Orientation {
  76. Horizontal,
  77. Vertical,
  78. Both
  79. };
  80. // NOTE hover not implemented yet
  81. enum State {
  82. kKnobStateDefault = 0x0,
  83. kKnobStateHover = 0x1,
  84. kKnobStateDragging = 0x2,
  85. kKnobStateDraggingHover = kKnobStateDragging|kKnobStateHover
  86. };
  87. class Callback
  88. {
  89. public:
  90. virtual ~Callback() {}
  91. virtual void knobDragStarted(SubWidget* widget) = 0;
  92. virtual void knobDragFinished(SubWidget* widget) = 0;
  93. virtual void knobValueChanged(SubWidget* widget, float value) = 0;
  94. virtual void knobDoubleClicked(SubWidget*) {};
  95. };
  96. explicit KnobEventHandler(SubWidget* self);
  97. explicit KnobEventHandler(SubWidget* self, const KnobEventHandler& other);
  98. KnobEventHandler& operator=(const KnobEventHandler& other);
  99. virtual ~KnobEventHandler();
  100. bool isEnabled() const noexcept;
  101. void setEnabled(bool enabled) noexcept;
  102. // if setStep(1) has been called before, this returns true
  103. bool isInteger() const noexcept;
  104. // returns raw value, is assumed to be scaled if using log
  105. float getValue() const noexcept;
  106. // NOTE: value is assumed to be scaled if using log
  107. virtual bool setValue(float value, bool sendCallback = false) noexcept;
  108. // returns 0-1 ranged value, already with log scale as needed
  109. float getNormalizedValue() const noexcept;
  110. float getDefault() const noexcept;
  111. // NOTE: value is assumed to be scaled if using log
  112. void setDefault(float def) noexcept;
  113. // NOTE: value is assumed to be scaled if using log
  114. void setRange(float min, float max) noexcept;
  115. void setStep(float step) noexcept;
  116. void setUsingLogScale(bool yesNo) noexcept;
  117. Orientation getOrientation() const noexcept;
  118. void setOrientation(Orientation orientation) noexcept;
  119. void setCallback(Callback* callback) noexcept;
  120. // default 200, higher means slower
  121. void setMouseDeceleration(float accel) noexcept;
  122. bool mouseEvent(const Widget::MouseEvent& ev, double scaleFactor = 1.0);
  123. bool motionEvent(const Widget::MotionEvent& ev, double scaleFactor = 1.0);
  124. bool scrollEvent(const Widget::ScrollEvent& ev);
  125. protected:
  126. State getState() const noexcept;
  127. private:
  128. struct PrivateData;
  129. PrivateData* const pData;
  130. /* not for use */
  131. #ifdef DISTRHO_PROPER_CPP11_SUPPORT
  132. KnobEventHandler(KnobEventHandler& other) = delete;
  133. KnobEventHandler(const KnobEventHandler& other) = delete;
  134. #else
  135. KnobEventHandler(KnobEventHandler& other);
  136. KnobEventHandler(const KnobEventHandler& other);
  137. #endif
  138. DISTRHO_LEAK_DETECTOR(KnobEventHandler)
  139. };
  140. // --------------------------------------------------------------------------------------------------------------------
  141. class SliderEventHandler
  142. {
  143. public:
  144. explicit SliderEventHandler(SubWidget* self);
  145. virtual ~SliderEventHandler();
  146. private:
  147. struct PrivateData;
  148. PrivateData* const pData;
  149. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(SliderEventHandler)
  150. };
  151. // --------------------------------------------------------------------------------------------------------------------
  152. END_NAMESPACE_DGL
  153. #endif // DGL_EVENT_HANDLERS_HPP_INCLUDED