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.

210 lines
5.7KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2020 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. #include "DistrhoUIInternal.hpp"
  17. #if !DISTRHO_PLUGIN_HAS_EXTERNAL_UI
  18. # include "src/WidgetPrivateData.hpp"
  19. #endif
  20. START_NAMESPACE_DISTRHO
  21. /* ------------------------------------------------------------------------------------------------------------
  22. * Static data, see DistrhoUIInternal.hpp */
  23. double d_lastUiSampleRate = 0.0;
  24. void* d_lastUiDspPtr = nullptr;
  25. #if DISTRHO_PLUGIN_HAS_EXTERNAL_UI
  26. const char* g_nextBundlePath = nullptr;
  27. double g_nextScaleFactor = 1.0;
  28. uintptr_t g_nextWindowId = 0;
  29. #else
  30. Window* d_lastUiWindow = nullptr;
  31. #endif
  32. /* ------------------------------------------------------------------------------------------------------------
  33. * UI */
  34. #if DISTRHO_PLUGIN_HAS_EXTERNAL_UI
  35. UI::UI(uint width, uint height)
  36. : UIWidget(width, height),
  37. pData(new PrivateData()) {}
  38. #else
  39. UI::UI(uint width, uint height)
  40. : UIWidget(*d_lastUiWindow),
  41. pData(new PrivateData())
  42. {
  43. ((UIWidget*)this)->pData->needsFullViewport = false;
  44. if (width > 0 && height > 0)
  45. setSize(width, height);
  46. }
  47. #endif
  48. UI::~UI()
  49. {
  50. delete pData;
  51. }
  52. #if DISTRHO_UI_USER_RESIZABLE && !DISTRHO_PLUGIN_HAS_EXTERNAL_UI
  53. void UI::setGeometryConstraints(uint minWidth, uint minHeight, bool keepAspectRatio, bool automaticallyScale)
  54. {
  55. DISTRHO_SAFE_ASSERT_RETURN(minWidth > 0,);
  56. DISTRHO_SAFE_ASSERT_RETURN(minHeight > 0,);
  57. pData->automaticallyScale = automaticallyScale;
  58. pData->minWidth = minWidth;
  59. pData->minHeight = minHeight;
  60. Window& window(getParentWindow());
  61. const double uiScaleFactor = window.getScaling();
  62. window.setGeometryConstraints(minWidth * uiScaleFactor, minHeight * uiScaleFactor, keepAspectRatio);
  63. if (d_isNotZero(uiScaleFactor - 1.0))
  64. setSize(getWidth() * uiScaleFactor, getHeight() * uiScaleFactor);
  65. }
  66. #endif
  67. /* ------------------------------------------------------------------------------------------------------------
  68. * Host state */
  69. uint UI::getBackgroundColor() const noexcept
  70. {
  71. return pData->bgColor;
  72. }
  73. uint UI::getForegroundColor() const noexcept
  74. {
  75. return pData->fgColor;
  76. }
  77. double UI::getSampleRate() const noexcept
  78. {
  79. return pData->sampleRate;
  80. }
  81. void UI::editParameter(uint32_t index, bool started)
  82. {
  83. pData->editParamCallback(index + pData->parameterOffset, started);
  84. }
  85. void UI::setParameterValue(uint32_t index, float value)
  86. {
  87. pData->setParamCallback(index + pData->parameterOffset, value);
  88. }
  89. #if DISTRHO_PLUGIN_WANT_STATE
  90. void UI::setState(const char* key, const char* value)
  91. {
  92. pData->setStateCallback(key, value);
  93. }
  94. #endif
  95. #if DISTRHO_PLUGIN_WANT_STATEFILES
  96. bool UI::requestStateFile(const char* key)
  97. {
  98. return pData->fileRequestCallback(key);
  99. }
  100. #endif
  101. #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
  102. void UI::sendNote(uint8_t channel, uint8_t note, uint8_t velocity)
  103. {
  104. pData->sendNoteCallback(channel, note, velocity);
  105. }
  106. #endif
  107. #if DISTRHO_PLUGIN_WANT_DIRECT_ACCESS
  108. /* ------------------------------------------------------------------------------------------------------------
  109. * Direct DSP access */
  110. void* UI::getPluginInstancePointer() const noexcept
  111. {
  112. return pData->dspPtr;
  113. }
  114. #endif
  115. #if DISTRHO_PLUGIN_HAS_EXTERNAL_UI
  116. /* ------------------------------------------------------------------------------------------------------------
  117. * External UI helpers */
  118. const char* UI::getNextBundlePath() noexcept
  119. {
  120. return g_nextBundlePath;
  121. }
  122. double UI::getNextScaleFactor() noexcept
  123. {
  124. return g_nextScaleFactor;
  125. }
  126. # if DISTRHO_PLUGIN_HAS_EMBED_UI
  127. uintptr_t UI::getNextWindowId() noexcept
  128. {
  129. return g_nextWindowId;
  130. }
  131. # endif
  132. #endif
  133. /* ------------------------------------------------------------------------------------------------------------
  134. * DSP/Plugin Callbacks (optional) */
  135. void UI::sampleRateChanged(double) {}
  136. #if !DISTRHO_PLUGIN_HAS_EXTERNAL_UI
  137. /* ------------------------------------------------------------------------------------------------------------
  138. * UI Callbacks (optional) */
  139. # ifndef DGL_FILE_BROWSER_DISABLED
  140. void UI::uiFileBrowserSelected(const char*)
  141. {
  142. }
  143. # endif
  144. void UI::uiReshape(uint width, uint height)
  145. {
  146. #ifdef DGL_OPENGL
  147. glEnable(GL_BLEND);
  148. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  149. glMatrixMode(GL_PROJECTION);
  150. glLoadIdentity();
  151. glOrtho(0.0, static_cast<GLdouble>(width), static_cast<GLdouble>(height), 0.0, 0.0, 1.0);
  152. glViewport(0, 0, static_cast<GLsizei>(width), static_cast<GLsizei>(height));
  153. glMatrixMode(GL_MODELVIEW);
  154. glLoadIdentity();
  155. #else
  156. // unused
  157. (void)width;
  158. (void)height;
  159. #endif
  160. }
  161. /* ------------------------------------------------------------------------------------------------------------
  162. * UI Resize Handling, internal */
  163. void UI::onResize(const ResizeEvent& ev)
  164. {
  165. if (pData->resizeInProgress)
  166. return;
  167. pData->setSizeCallback(ev.size.getWidth(), ev.size.getHeight());
  168. }
  169. #endif // !DISTRHO_PLUGIN_HAS_EXTERNAL_UI
  170. // -----------------------------------------------------------------------------------------------------------
  171. END_NAMESPACE_DISTRHO