DISTRHO Plugin Framework
ImageBaseWidgets.hpp
1 /*
2  * DISTRHO Plugin Framework (DPF)
3  * Copyright (C) 2012-2021 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 
17 #ifndef DGL_IMAGE_BASE_WIDGETS_HPP_INCLUDED
18 #define DGL_IMAGE_BASE_WIDGETS_HPP_INCLUDED
19 
20 #include "EventHandlers.hpp"
21 #include "StandaloneWindow.hpp"
22 #include "SubWidget.hpp"
23 
24 START_NAMESPACE_DGL
25 
26 // --------------------------------------------------------------------------------------------------------------------
27 
28 /**
29  DGL Image About Window class.
30 
31  This is a Window attached (transient) to another Window that simply shows an Image as its content.
32  It is typically used for "about this project" style pop-up Windows.
33 
34  Pressing 'Esc' or clicking anywhere on the window will automatically close it.
35 
36  @see CairoImageAboutWindow, OpenGLImageAboutWindow, Window::runAsModal(bool)
37  */
38 template <class ImageType>
40 {
41 public:
42  /**
43  Constructor taking an existing Window as the parent transient window and an optional image.
44  If @a image is valid, the about window size will match the image size.
45  */
46  explicit ImageBaseAboutWindow(Window& transientParentWindow, const ImageType& image = ImageType());
47 
48  /**
49  Constructor taking a top-level-widget's Window as the parent transient window and an optional image.
50  If @a image is valid, the about window size will match the image size.
51  */
52  explicit ImageBaseAboutWindow(TopLevelWidget* topLevelWidget, const ImageType& image = ImageType());
53 
54  /**
55  Set a new image to use as background for this window.
56  Window size will adjust to match the image size.
57  */
58  void setImage(const ImageType& image);
59 
60 protected:
61  void onDisplay() override;
62  bool onKeyboard(const KeyboardEvent&) override;
63  bool onMouse(const MouseEvent&) override;
64 
65 private:
66  ImageType img;
67 
68  DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ImageBaseAboutWindow)
69 };
70 
71 // --------------------------------------------------------------------------------------------------------------------
72 
73 /**
74  DGL Image Button class.
75 
76  This is a typical button, where the drawing comes from a pregenerated set of images.
77  The button can be under "normal", "hover" and "down" states, with one separate image possible for each.
78 
79  The event logic for this button comes from the ButtonEventHandler class.
80 
81  @see CairoImageButton, OpenGLImageButton
82  */
83 template <class ImageType>
84 class ImageBaseButton : public SubWidget,
85  public ButtonEventHandler
86 {
87 public:
88  class Callback
89  {
90  public:
91  virtual ~Callback() {}
92  virtual void imageButtonClicked(ImageBaseButton* imageButton, int button) = 0;
93  };
94 
95  explicit ImageBaseButton(Widget* parentWidget, const ImageType& image);
96  explicit ImageBaseButton(Widget* parentWidget, const ImageType& imageNormal, const ImageType& imageDown);
97  explicit ImageBaseButton(Widget* parentWidget, const ImageType& imageNormal, const ImageType& imageHover, const ImageType& imageDown);
98 
99  ~ImageBaseButton() override;
100 
101  void setCallback(Callback* callback) noexcept;
102 
103 protected:
104  void onDisplay() override;
105  bool onMouse(const MouseEvent&) override;
106  bool onMotion(const MotionEvent&) override;
107 
108 private:
109  struct PrivateData;
110  PrivateData* const pData;
111 
112  DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ImageBaseButton)
113 };
114 
115 // --------------------------------------------------------------------------------------------------------------------
116 
117 /**
118  DGL Image Knob class.
119 
120  This is a typical knob/dial, where the drawing comes from a pregenerated image "filmstrip".
121  The knob's "filmstrip" image can be either horizontal or vertical,
122  with the number of steps automatically based on the largest value (ie, horizontal if width>height, vertical if height>width).
123  There are no different images for "hover" or "down" states.
124 
125  The event logic for this knob comes from the KnobEventHandler class.
126 
127  @see CairoImageKnob, OpenGLImageKnob
128  */
129 template <class ImageType>
130 class ImageBaseKnob : public SubWidget,
131  public KnobEventHandler
132 {
133 public:
134  class Callback
135  {
136  public:
137  virtual ~Callback() {}
138  virtual void imageKnobDragStarted(ImageBaseKnob* imageKnob) = 0;
139  virtual void imageKnobDragFinished(ImageBaseKnob* imageKnob) = 0;
140  virtual void imageKnobValueChanged(ImageBaseKnob* imageKnob, float value) = 0;
141  };
142 
143  explicit ImageBaseKnob(Widget* parentWidget, const ImageType& image, Orientation orientation = Vertical) noexcept;
144  explicit ImageBaseKnob(const ImageBaseKnob& imageKnob);
145  ImageBaseKnob& operator=(const ImageBaseKnob& imageKnob);
146  ~ImageBaseKnob() override;
147 
148  void setCallback(Callback* callback) noexcept;
149  void setImageLayerCount(uint count) noexcept;
150  void setRotationAngle(int angle);
151  bool setValue(float value, bool sendCallback = false) noexcept override;
152 
153 protected:
154  void onDisplay() override;
155  bool onMouse(const MouseEvent&) override;
156  bool onMotion(const MotionEvent&) override;
157  bool onScroll(const ScrollEvent&) override;
158 
159 private:
160  struct PrivateData;
161  PrivateData* const pData;
162 
163  DISTRHO_LEAK_DETECTOR(ImageBaseKnob)
164 };
165 
166 // --------------------------------------------------------------------------------------------------------------------
167 
168 // note set range and step before setting the value
169 
170 template <class ImageType>
172 {
173 public:
174  class Callback
175  {
176  public:
177  virtual ~Callback() {}
178  virtual void imageSliderDragStarted(ImageBaseSlider* imageSlider) = 0;
179  virtual void imageSliderDragFinished(ImageBaseSlider* imageSlider) = 0;
180  virtual void imageSliderValueChanged(ImageBaseSlider* imageSlider, float value) = 0;
181  };
182 
183  explicit ImageBaseSlider(Widget* parentWidget, const ImageType& image) noexcept;
184  ~ImageBaseSlider() override;
185 
186  float getValue() const noexcept;
187  void setValue(float value, bool sendCallback = false) noexcept;
188  void setDefault(float def) noexcept;
189 
190  void setStartPos(const Point<int>& startPos) noexcept;
191  void setStartPos(int x, int y) noexcept;
192  void setEndPos(const Point<int>& endPos) noexcept;
193  void setEndPos(int x, int y) noexcept;
194 
195  void setCheckable(bool checkable) noexcept;
196  void setInverted(bool inverted) noexcept;
197  void setRange(float min, float max) noexcept;
198  void setStep(float step) noexcept;
199 
200  void setCallback(Callback* callback) noexcept;
201 
202 protected:
203  void onDisplay() override;
204  bool onMouse(const MouseEvent&) override;
205  bool onMotion(const MotionEvent&) override;
206 
207 private:
208  struct PrivateData;
209  PrivateData* const pData;
210 
211  // these should not be used
212  void setAbsoluteX(int) const noexcept {}
213  void setAbsoluteY(int) const noexcept {}
214  void setAbsolutePos(int, int) const noexcept {}
215  void setAbsolutePos(const Point<int>&) const noexcept {}
216 
217  DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ImageBaseSlider)
218 };
219 
220 // --------------------------------------------------------------------------------------------------------------------
221 
222 template <class ImageType>
224 {
225 public:
226  class Callback
227  {
228  public:
229  virtual ~Callback() {}
230  virtual void imageSwitchClicked(ImageBaseSwitch* imageSwitch, bool down) = 0;
231  };
232 
233  explicit ImageBaseSwitch(Widget* parentWidget, const ImageType& imageNormal, const ImageType& imageDown) noexcept;
234  explicit ImageBaseSwitch(const ImageBaseSwitch& imageSwitch) noexcept;
235  ImageBaseSwitch& operator=(const ImageBaseSwitch& imageSwitch) noexcept;
236  ~ImageBaseSwitch() override;
237 
238  bool isDown() const noexcept;
239  void setDown(bool down) noexcept;
240 
241  void setCallback(Callback* callback) noexcept;
242 
243 protected:
244  void onDisplay() override;
245  bool onMouse(const MouseEvent&) override;
246 
247 private:
248  struct PrivateData;
249  PrivateData* const pData;
250 
251  DISTRHO_LEAK_DETECTOR(ImageBaseSwitch)
252 };
253 
254 // --------------------------------------------------------------------------------------------------------------------
255 
256 END_NAMESPACE_DGL
257 
258 #endif // DGL_IMAGE_BASE_WIDGETS_HPP_INCLUDED
Definition: EventHandlers.hpp:38
Definition: ImageBaseWidgets.hpp:40
void setImage(const ImageType &image)
bool onKeyboard(const KeyboardEvent &) override
void onDisplay() override
ImageBaseAboutWindow(TopLevelWidget *topLevelWidget, const ImageType &image=ImageType())
bool onMouse(const MouseEvent &) override
ImageBaseAboutWindow(Window &transientParentWindow, const ImageType &image=ImageType())
Definition: ImageBaseWidgets.hpp:89
Definition: ImageBaseWidgets.hpp:86
void onDisplay() override
bool onMotion(const MotionEvent &) override
bool onMouse(const MouseEvent &) override
Definition: ImageBaseWidgets.hpp:135
Definition: ImageBaseWidgets.hpp:132
bool onMouse(const MouseEvent &) override
void onDisplay() override
bool onMotion(const MotionEvent &) override
bool onScroll(const ScrollEvent &) override
Definition: ImageBaseWidgets.hpp:175
Definition: ImageBaseWidgets.hpp:172
Definition: ImageBaseWidgets.hpp:227
Definition: ImageBaseWidgets.hpp:224
Definition: EventHandlers.hpp:93
Definition: Geometry.hpp:41
Definition: StandaloneWindow.hpp:29
Definition: SubWidget.hpp:40
void setAbsolutePos(int x, int y) noexcept
void setAbsoluteY(int y) noexcept
void setAbsoluteX(int x) noexcept
Definition: TopLevelWidget.hpp:47
Definition: Widget.hpp:56
Definition: Window.hpp:63
Definition: Widget.hpp:92
Definition: Widget.hpp:184
Definition: Widget.hpp:161
Definition: Widget.hpp:208