DISTRHO Plugin Framework
DistrhoUI.hpp
1 /*
2  * DISTRHO Plugin Framework (DPF)
3  * Copyright (C) 2012-2023 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 DISTRHO_UI_HPP_INCLUDED
18 #define DISTRHO_UI_HPP_INCLUDED
19 
20 #include "DistrhoDetails.hpp"
21 #include "extra/LeakDetector.hpp"
22 #include "src/DistrhoPluginChecks.h"
23 
24 #ifdef DGL_CAIRO
25 # include "Cairo.hpp"
26 #endif
27 #ifdef DGL_OPENGL
28 # include "OpenGL.hpp"
29 #endif
30 #ifdef DGL_VULKAN
31 # include "Vulkan.hpp"
32 #endif
33 
34 #if DISTRHO_PLUGIN_HAS_EXTERNAL_UI
35 # include "../dgl/Base.hpp"
36 # include "extra/ExternalWindow.hpp"
37 typedef DISTRHO_NAMESPACE::ExternalWindow UIWidget;
38 #elif DISTRHO_UI_USE_CUSTOM
39 # include DISTRHO_UI_CUSTOM_INCLUDE_PATH
40 typedef DISTRHO_UI_CUSTOM_WIDGET_TYPE UIWidget;
41 #elif DISTRHO_UI_USE_CAIRO
42 # include "../dgl/Cairo.hpp"
43 typedef DGL_NAMESPACE::CairoTopLevelWidget UIWidget;
44 #elif DISTRHO_UI_USE_NANOVG
45 # include "../dgl/NanoVG.hpp"
46 typedef DGL_NAMESPACE::NanoTopLevelWidget UIWidget;
47 #else
48 # include "../dgl/TopLevelWidget.hpp"
49 typedef DGL_NAMESPACE::TopLevelWidget UIWidget;
50 #endif
51 
52 #if DISTRHO_UI_FILE_BROWSER
53 # include "extra/FileBrowserDialog.hpp"
54 #endif
55 #if !DISTRHO_PLUGIN_HAS_EXTERNAL_UI
56 # include <vector>
57 #endif
58 
60 
61 class PluginWindow;
62 
63 /* ------------------------------------------------------------------------------------------------------------
64  * DPF UI */
65 
66 /**
67  @addtogroup MainClasses
68  @{
69  */
70 
71 /**
72  DPF UI class from where UI instances are created.
73 
74  @note You must call setSize during construction,
75  @TODO Detailed information about this class.
76  */
77 class UI : public UIWidget
78 {
79 public:
80  /**
81  UI class constructor.
82  The UI should be initialized to a default state that matches the plugin side.
83 
84  When @a automaticallyScale is set to true, DPF will automatically scale up the UI
85  to fit the host/desktop scale factor.@n
86  It assumes aspect ratio is meant to be kept.
87  Manually call setGeometryConstraints instead if keeping UI aspect ratio is not required.
88  */
89  UI(uint width = 0, uint height = 0, bool automaticallyScaleAndSetAsMinimumSize = false);
90 
91  /**
92  Destructor.
93  */
94  ~UI() override;
95 
96  /* --------------------------------------------------------------------------------------------------------
97  * Host state */
98 
99  /**
100  Check if this UI window is resizable (by the user or window manager).
101  There are situations where an UI supports resizing but the plugin host does not, so this could return false.
102 
103  You might want to add a resize handle for such cases, so the user is still allowed to resize the window.
104  (programatically resizing a window is always possible, but the same is not true for the window manager)
105  */
106  bool isResizable() const noexcept;
107 
108  /**
109  Get the color used for UI background (i.e. window color) in RGBA format.
110  Returns 0 by default, in case of error or lack of host support.
111 
112  The following example code can be use to extract individual colors:
113  ```
114  const int red = (bgColor >> 24) & 0xff;
115  const int green = (bgColor >> 16) & 0xff;
116  const int blue = (bgColor >> 8) & 0xff;
117  ```
118  */
119  uint getBackgroundColor() const noexcept;
120 
121  /**
122  Get the color used for UI foreground (i.e. text color) in RGBA format.
123  Returns 0xffffffff by default, in case of error or lack of host support.
124 
125  The following example code can be use to extract individual colors:
126  ```
127  const int red = (fgColor >> 24) & 0xff;
128  const int green = (fgColor >> 16) & 0xff;
129  const int blue = (fgColor >> 8) & 0xff;
130  ```
131  */
132  uint getForegroundColor() const noexcept;
133 
134  /**
135  Get the current sample rate used in plugin processing.
136  @see sampleRateChanged(double)
137  */
138  double getSampleRate() const noexcept;
139 
140  /**
141  Get the bundle path where the UI resides.@n
142  Can return null if the UI is not available in a bundle (if it is a single binary).
143  @see getBinaryFilename
144  */
145  const char* getBundlePath() const noexcept;
146 
147  /**
148  editParameter.
149 
150  Touch/pressed-down event.
151  Lets the host know the user is tweaking a parameter.
152  Required in some hosts to record automation.
153  */
154  void editParameter(uint32_t index, bool started);
155 
156  /**
157  setParameterValue.
158 
159  Change a parameter value in the Plugin.
160  */
161  void setParameterValue(uint32_t index, float value);
162 
163 #if DISTRHO_PLUGIN_WANT_STATE
164  /**
165  setState.
166  @TODO Document this.
167  */
168  void setState(const char* key, const char* value);
169 
170  /**
171  Request a new file from the host, matching the properties of a state key.@n
172  This will use the native host file browser if available, otherwise a DPF built-in file browser is used.@n
173  Response will be sent asynchronously to stateChanged, with the matching key and the new file as the value.@n
174  It is not possible to know if the action was cancelled by the user.
175 
176  @return Success if a file-browser was opened, otherwise false.
177  @note You cannot request more than one file at a time.
178  */
179  bool requestStateFile(const char* key);
180 #endif
181 
182 #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
183  /**
184  Send a single MIDI note from the UI to the plugin DSP side.@n
185  A note with zero velocity will be sent as note-off (MIDI 0x80), otherwise note-on (MIDI 0x90).
186  */
187  void sendNote(uint8_t channel, uint8_t note, uint8_t velocity);
188 #endif
189 
190 #if DISTRHO_UI_FILE_BROWSER
191  /**
192  Open a file browser dialog with this window as transient parent.@n
193  A few options can be specified to setup the dialog.
194 
195  If a path is selected, onFileSelected() will be called with the user chosen path.
196  If the user cancels or does not pick a file, onFileSelected() will be called with nullptr as filename.
197 
198  This function does not block the event loop.
199 
200  @note This is exactly the same API as provided by the Window class,
201  but redeclared here so that non-embed/DGL based UIs can still use file browser related functions.
202  */
203  bool openFileBrowser(const DISTRHO_NAMESPACE::FileBrowserOptions& options = FileBrowserOptions());
204 #endif
205 
206 #if DISTRHO_PLUGIN_WANT_DIRECT_ACCESS
207  /* --------------------------------------------------------------------------------------------------------
208  * Direct DSP access - DO NOT USE THIS UNLESS STRICTLY NECESSARY!! */
209 
210  /**
211  getPluginInstancePointer.
212  @TODO Document this.
213  */
214  void* getPluginInstancePointer() const noexcept;
215 #endif
216 
217 #if DISTRHO_PLUGIN_HAS_EXTERNAL_UI
218  /* --------------------------------------------------------------------------------------------------------
219  * External UI helpers */
220 
221  /**
222  Get the bundle path that will be used for the next UI.
223  @note: This function is only valid during createUI(),
224  it will return null when called from anywhere else.
225  */
226  static const char* getNextBundlePath() noexcept;
227 
228  /**
229  Get the scale factor that will be used for the next UI.
230  @note: This function is only valid during createUI(),
231  it will return 1.0 when called from anywhere else.
232  */
233  static double getNextScaleFactor() noexcept;
234 
235 # if DISTRHO_PLUGIN_HAS_EMBED_UI
236  /**
237  Get the Window Id that will be used for the next created window.
238  @note: This function is only valid during createUI(),
239  it will return 0 when called from anywhere else.
240  */
241  static uintptr_t getNextWindowId() noexcept;
242 # endif
243 #endif
244 
245 protected:
246  /* --------------------------------------------------------------------------------------------------------
247  * DSP/Plugin Callbacks */
248 
249  /**
250  A parameter has changed on the plugin side.@n
251  This is called by the host to inform the UI about parameter changes.
252  */
253  virtual void parameterChanged(uint32_t index, float value) = 0;
254 
255 #if DISTRHO_PLUGIN_WANT_PROGRAMS
256  /**
257  A program has been loaded on the plugin side.@n
258  This is called by the host to inform the UI about program changes.
259  */
260  virtual void programLoaded(uint32_t index) = 0;
261 #endif
262 
263 #if DISTRHO_PLUGIN_WANT_STATE
264  /**
265  A state has changed on the plugin side.@n
266  This is called by the host to inform the UI about state changes.
267  */
268  virtual void stateChanged(const char* key, const char* value) = 0;
269 #endif
270 
271  /* --------------------------------------------------------------------------------------------------------
272  * DSP/Plugin Callbacks (optional) */
273 
274  /**
275  Optional callback to inform the UI about a sample rate change on the plugin side.
276  @see getSampleRate()
277  */
278  virtual void sampleRateChanged(double newSampleRate);
279 
280  /* --------------------------------------------------------------------------------------------------------
281  * UI Callbacks (optional) */
282 
283  /**
284  UI idle function, called to give idle time to the plugin UI directly from the host.
285  This is called right after OS event handling and Window idle events (within the same cycle).
286  There are no guarantees in terms of timing.
287  @see addIdleCallback(IdleCallback*, uint).
288  */
289  virtual void uiIdle() {}
290 
291  /**
292  Window scale factor function, called when the scale factor changes.
293  This function is for plugin UIs to be able to override Window::onScaleFactorChanged(double).
294 
295  The default implementation does nothing.
296  WARNING function needs a proper name
297  */
298  virtual void uiScaleFactorChanged(double scaleFactor);
299 
300 #if !DISTRHO_PLUGIN_HAS_EXTERNAL_UI
301  /**
302  Get the types available for the data in a clipboard.
303  Must only be called within the context of uiClipboardDataOffer.
304  */
305  std::vector<DGL_NAMESPACE::ClipboardDataOffer> getClipboardDataOfferTypes();
306 
307  /**
308  Window clipboard data offer function, called when clipboard has data present, possibly with several datatypes.
309  While handling this event, the data types can be investigated with getClipboardDataOfferTypes() to decide whether to accept the offer.
310 
311  Reimplement and return a non-zero id to accept the clipboard data offer for a particular type.
312  UIs must ignore any type they do not recognize.
313 
314  The default implementation accepts the "text/plain" mimetype.
315  */
316  virtual uint32_t uiClipboardDataOffer();
317 
318  /**
319  Windows focus function, called when the window gains or loses the keyboard focus.
320  This function is for plugin UIs to be able to override Window::onFocus(bool, CrossingMode).
321 
322  The default implementation does nothing.
323  */
324  virtual void uiFocus(bool focus, DGL_NAMESPACE::CrossingMode mode);
325 
326  /**
327  Window reshape function, called when the window is resized.
328  This function is for plugin UIs to be able to override Window::onReshape(uint, uint).
329 
330  The plugin UI size will be set right after this function.
331  The default implementation sets up the drawing context where necessary.
332 
333  You should almost never need to override this function.
334  The most common exception is custom OpenGL setup, but only really needed for custom OpenGL drawing code.
335  */
336  virtual void uiReshape(uint width, uint height);
337 #endif // !DISTRHO_PLUGIN_HAS_EXTERNAL_UI
338 
339 #if DISTRHO_UI_FILE_BROWSER
340  /**
341  Window file selected function, called when a path is selected by the user, as triggered by openFileBrowser().
342  This function is for plugin UIs to be able to override Window::onFileSelected(const char*).
343 
344  This action happens after the user confirms the action, so the file browser dialog will be closed at this point.
345  The default implementation does nothing.
346 
347  If you need to use files as plugin state, please setup and use states with kStateIsFilenamePath instead.
348  */
349  virtual void uiFileBrowserSelected(const char* filename);
350 #endif
351 
352  /* --------------------------------------------------------------------------------------------------------
353  * UI Resize Handling, internal */
354 
355 #if DISTRHO_PLUGIN_HAS_EXTERNAL_UI
356  /**
357  External Window resize function, called when the window is resized.
358  This is overriden here so the host knows when the UI is resized by you.
359  @see ExternalWindow::sizeChanged(uint,uint)
360  */
361  void sizeChanged(uint width, uint height) override;
362 #else
363  /**
364  Widget resize function, called when the widget is resized.
365  This is overriden here so the host knows when the UI is resized by you.
366  @see Widget::onResize(const ResizeEvent&)
367  */
368  void onResize(const ResizeEvent& ev) override;
369 #endif
370 
371  // -------------------------------------------------------------------------------------------------------
372 
373 private:
374  struct PrivateData;
375  PrivateData* const uiData;
376  friend class PluginWindow;
377  friend class UIExporter;
378 #if !DISTRHO_PLUGIN_HAS_EXTERNAL_UI
379  /** @internal */
380  void requestSizeChange(uint width, uint height) override;
381 #endif
382 
383  DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(UI)
384 };
385 
386 /** @} */
387 
388 /* ------------------------------------------------------------------------------------------------------------
389  * Create UI, entry point */
390 
391 /**
392  @addtogroup EntryPoints
393  @{
394  */
395 
396 /**
397  createUI.
398  @TODO Document this.
399  */
400 extern UI* createUI();
401 
402 /** @} */
403 
404 // -----------------------------------------------------------------------------------------------------------
405 
407 
408 #endif // DISTRHO_UI_HPP_INCLUDED
Definition: DistrhoUI.hpp:78
void * getPluginInstancePointer() const noexcept
virtual void stateChanged(const char *key, const char *value)=0
void editParameter(uint32_t index, bool started)
virtual void programLoaded(uint32_t index)=0
const char * getBundlePath() const noexcept
uint getBackgroundColor() const noexcept
void sizeChanged(uint width, uint height) override
static uintptr_t getNextWindowId() noexcept
bool requestStateFile(const char *key)
uint getForegroundColor() const noexcept
UI(uint width=0, uint height=0, bool automaticallyScaleAndSetAsMinimumSize=false)
bool isResizable() const noexcept
void setParameterValue(uint32_t index, float value)
double getSampleRate() const noexcept
~UI() override
void sendNote(uint8_t channel, uint8_t note, uint8_t velocity)
static double getNextScaleFactor() noexcept
static const char * getNextBundlePath() noexcept
virtual void parameterChanged(uint32_t index, float value)=0
void setState(const char *key, const char *value)
virtual void sampleRateChanged(double newSampleRate)
virtual void uiScaleFactorChanged(double scaleFactor)
virtual void uiIdle()
Definition: DistrhoUI.hpp:289
UI * createUI()
#define END_NAMESPACE_DISTRHO
Definition: DistrhoInfo.hpp:949
#define START_NAMESPACE_DISTRHO
Definition: DistrhoInfo.hpp:943
#define DISTRHO_UI_CUSTOM_WIDGET_TYPE
Definition: DistrhoInfo.hpp:626
Definition: FileBrowserDialogImpl.hpp:33