DISTRHO Plugin Framework
DistrhoUI.hpp
1 /*
2  * DISTRHO Plugin Framework (DPF)
3  * Copyright (C) 2012-2019 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 "extra/LeakDetector.hpp"
21 #include "src/DistrhoPluginChecks.h"
22 
23 #if DISTRHO_PLUGIN_HAS_EXTERNAL_UI
24 # include "../dgl/Base.hpp"
25 # include "extra/ExternalWindow.hpp"
26 typedef DISTRHO_NAMESPACE::ExternalWindow UIWidget;
27 #elif DISTRHO_UI_USE_NANOVG
28 # include "../dgl/NanoVG.hpp"
29 typedef DGL_NAMESPACE::NanoWidget UIWidget;
30 #else
31 # include "../dgl/Widget.hpp"
32 typedef DGL_NAMESPACE::Widget UIWidget;
33 #endif
34 
35 START_NAMESPACE_DISTRHO
36 
37 /* ------------------------------------------------------------------------------------------------------------
38  * DPF UI */
39 
40 /**
41  @addtogroup MainClasses
42  @{
43  */
44 
45 /**
46  DPF UI class from where UI instances are created.
47 
48  @note You must call setSize during construction,
49  @TODO Detailed information about this class.
50  */
51 class UI : public UIWidget
52 {
53 public:
54  /**
55  UI class constructor.
56  The UI should be initialized to a default state that matches the plugin side.
57  */
58  UI(uint width = 0, uint height = 0);
59 
60  /**
61  Destructor.
62  */
63  virtual ~UI();
64 
65 #if !DISTRHO_PLUGIN_HAS_EXTERNAL_UI
66  /**
67  Set geometry constraints for the UI when resized by the user, and optionally scale UI automatically.
68  @see Window::setGeometryConstraints(uint,uint,bool)
69  @see Window::setScaling(double)
70  */
71  void setGeometryConstraints(uint minWidth, uint minHeight, bool keepAspectRatio, bool automaticallyScale = false);
72 #endif
73 
74  /* --------------------------------------------------------------------------------------------------------
75  * Host state */
76 
77  /**
78  Get the current sample rate used in plugin processing.
79  @see sampleRateChanged(double)
80  */
81  double getSampleRate() const noexcept;
82 
83  /**
84  editParameter.
85  @TODO Document this.
86  */
87  void editParameter(uint32_t index, bool started);
88 
89  /**
90  setParameterValue.
91  @TODO Document this.
92  */
93  void setParameterValue(uint32_t index, float value);
94 
95 #if DISTRHO_PLUGIN_WANT_STATE
96  /**
97  setState.
98  @TODO Document this.
99  */
100  void setState(const char* key, const char* value);
101 #endif
102 
103 #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
104  /**
105  sendNote.
106  @TODO Document this.
107  @note Work in progress. Implemented for DSSI and LV2 formats.
108  */
109  void sendNote(uint8_t channel, uint8_t note, uint8_t velocity);
110 #endif
111 
112 #if DISTRHO_PLUGIN_WANT_DIRECT_ACCESS
113  /* --------------------------------------------------------------------------------------------------------
114  * Direct DSP access - DO NOT USE THIS UNLESS STRICTLY NECESSARY!! */
115 
116  /**
117  getPluginInstancePointer.
118  @TODO Document this.
119  */
120  void* getPluginInstancePointer() const noexcept;
121 #endif
122 
123 #if DISTRHO_PLUGIN_HAS_EXTERNAL_UI
124  /* --------------------------------------------------------------------------------------------------------
125  * External UI helpers */
126 
127  /**
128  Get the bundle path that will be used for the next UI.
129  @note: This function is only valid during createUI(),
130  it will return null when called from anywhere else.
131  */
132  static const char* getNextBundlePath() noexcept;
133 
134 # if DISTRHO_PLUGIN_HAS_EMBED_UI
135  /**
136  Get the Window Id that will be used for the next created window.
137  @note: This function is only valid during createUI(),
138  it will return 0 when called from anywhere else.
139  */
140  static uintptr_t getNextWindowId() noexcept;
141 # endif
142 #endif
143 
144 protected:
145  /* --------------------------------------------------------------------------------------------------------
146  * DSP/Plugin Callbacks */
147 
148  /**
149  A parameter has changed on the plugin side.@n
150  This is called by the host to inform the UI about parameter changes.
151  */
152  virtual void parameterChanged(uint32_t index, float value) = 0;
153 
154 #if DISTRHO_PLUGIN_WANT_PROGRAMS
155  /**
156  A program has been loaded on the plugin side.@n
157  This is called by the host to inform the UI about program changes.
158  */
159  virtual void programLoaded(uint32_t index) = 0;
160 #endif
161 
162 #if DISTRHO_PLUGIN_WANT_STATE
163  /**
164  A state has changed on the plugin side.@n
165  This is called by the host to inform the UI about state changes.
166  */
167  virtual void stateChanged(const char* key, const char* value) = 0;
168 #endif
169 
170  /* --------------------------------------------------------------------------------------------------------
171  * DSP/Plugin Callbacks (optional) */
172 
173  /**
174  Optional callback to inform the UI about a sample rate change on the plugin side.
175  @see getSampleRate()
176  */
177  virtual void sampleRateChanged(double newSampleRate);
178 
179 #if !DISTRHO_PLUGIN_HAS_EXTERNAL_UI
180  /* --------------------------------------------------------------------------------------------------------
181  * UI Callbacks (optional) */
182 
183  /**
184  uiIdle.
185  @TODO Document this.
186  */
187  virtual void uiIdle() {}
188 
189 # ifndef DGL_FILE_BROWSER_DISABLED
190  /**
191  File browser selected function.
192  @see Window::fileBrowserSelected(const char*)
193  */
194  virtual void uiFileBrowserSelected(const char* filename);
195 # endif
196 
197  /**
198  OpenGL window reshape function, called when parent window is resized.
199  You can reimplement this function for a custom OpenGL state.
200  @see Window::onReshape(uint,uint)
201  */
202  virtual void uiReshape(uint width, uint height);
203 
204  /* --------------------------------------------------------------------------------------------------------
205  * UI Resize Handling, internal */
206 
207  /**
208  OpenGL widget resize function, called when the widget is resized.
209  This is overriden here so the host knows when the UI is resized by you.
210  @see Widget::onResize(const ResizeEvent&)
211  */
212  void onResize(const ResizeEvent& ev) override;
213 #endif
214 
215  // -------------------------------------------------------------------------------------------------------
216 
217 private:
218  struct PrivateData;
219  PrivateData* const pData;
220  friend class UIExporter;
221  friend class UIExporterWindow;
222 
223 #if !DISTRHO_PLUGIN_HAS_EXTERNAL_UI
224  // these should not be used
225  void setAbsoluteX(int) const noexcept {}
226  void setAbsoluteY(int) const noexcept {}
227  void setAbsolutePos(int, int) const noexcept {}
228  void setAbsolutePos(const DGL_NAMESPACE::Point<int>&) const noexcept {}
229 #endif
230 
231  DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(UI)
232 };
233 
234 /** @} */
235 
236 /* ------------------------------------------------------------------------------------------------------------
237  * Create UI, entry point */
238 
239 /**
240  @addtogroup EntryPoints
241  @{
242  */
243 
244 /**
245  createUI.
246  @TODO Document this.
247  */
248 extern UI* createUI();
249 
250 /** @} */
251 
252 // -----------------------------------------------------------------------------------------------------------
253 
254 END_NAMESPACE_DISTRHO
255 
256 #endif // DISTRHO_UI_HPP_INCLUDED
void editParameter(uint32_t index, bool started)
virtual void parameterChanged(uint32_t index, float value)=0
static uintptr_t getNextWindowId() noexcept
virtual ~UI()
void setParameterValue(uint32_t index, float value)
void * getPluginInstancePointer() const noexcept
virtual void sampleRateChanged(double newSampleRate)
virtual void programLoaded(uint32_t index)=0
void sendNote(uint8_t channel, uint8_t note, uint8_t velocity)
UI(uint width=0, uint height=0)
static const char * getNextBundlePath() noexcept
UI * createUI()
virtual void stateChanged(const char *key, const char *value)=0
double getSampleRate() const noexcept
void setState(const char *key, const char *value)
Definition: DistrhoUI.hpp:51