Audio plugin host https://kx.studio/carla
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.

145 lines
3.9KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2015 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 "../NanoWidgets.hpp"
  17. #include "Common.hpp"
  18. #define BLENDISH_IMPLEMENTATION
  19. #include "nanovg/nanovg.h"
  20. #include "oui-blendish/blendish.h"
  21. #include "oui-blendish/blendish_resources.h"
  22. START_NAMESPACE_DGL
  23. // -----------------------------------------------------------------------
  24. static void registerBlendishResourcesIfNeeded(NVGcontext* const context)
  25. {
  26. if (nvgFindFont(context, "__dpf_blendish__") >= 0)
  27. return;
  28. using namespace blendish_resources;
  29. bndSetFont(nvgCreateFontMem(context, "__dpf_blendish__", (const uchar*)dejavusans_ttf, dejavusans_ttf_size, 0));
  30. bndSetIconImage(nvgCreateImageMem(context, 0, (const uchar*)blender_icons16_png, blender_icons16_png_size));
  31. }
  32. // -----------------------------------------------------------------------
  33. struct BlendishButton::PrivateData {
  34. ButtonImpl impl;
  35. int iconId;
  36. DISTRHO_NAMESPACE::String text;
  37. PrivateData(Widget* const s, const char* const t, const int i) noexcept
  38. : impl(s),
  39. iconId(i),
  40. text(t) {}
  41. DISTRHO_DECLARE_NON_COPY_STRUCT(PrivateData)
  42. };
  43. // -----------------------------------------------------------------------
  44. BlendishButton::BlendishButton(Window& parent, const char* text, int iconId)
  45. : NanoWidget(parent),
  46. pData(new PrivateData(this, text, iconId))
  47. {
  48. registerBlendishResourcesIfNeeded(getContext());
  49. _updateBounds();
  50. }
  51. BlendishButton::BlendishButton(NanoWidget* widget, const char* text, int iconId)
  52. : NanoWidget(widget),
  53. pData(new PrivateData(this, text, iconId))
  54. {
  55. registerBlendishResourcesIfNeeded(getContext());
  56. _updateBounds();
  57. }
  58. BlendishButton::~BlendishButton()
  59. {
  60. delete pData;
  61. }
  62. int BlendishButton::getIconId() const noexcept
  63. {
  64. return pData->iconId;
  65. }
  66. void BlendishButton::setIconId(int iconId) noexcept
  67. {
  68. if (pData->iconId == iconId)
  69. return;
  70. pData->iconId = iconId;
  71. _updateBounds();
  72. repaint();
  73. }
  74. const char* BlendishButton::getText() const noexcept
  75. {
  76. return pData->text;
  77. }
  78. void BlendishButton::setText(const char* text) noexcept
  79. {
  80. if (pData->text == text)
  81. return;
  82. pData->text = text;
  83. _updateBounds();
  84. repaint();
  85. }
  86. void BlendishButton::setCallback(Callback* callback) noexcept
  87. {
  88. pData->impl.callback_b = callback;
  89. }
  90. void BlendishButton::onNanoDisplay()
  91. {
  92. bndToolButton(getContext(),
  93. getAbsoluteX(), getAbsoluteY(), getWidth(), getHeight(),
  94. 0, static_cast<BNDwidgetState>(pData->impl.state), pData->iconId, pData->text);
  95. }
  96. bool BlendishButton::onMouse(const MouseEvent& ev)
  97. {
  98. return pData->impl.onMouse(ev);
  99. }
  100. bool BlendishButton::onMotion(const MotionEvent& ev)
  101. {
  102. return pData->impl.onMotion(ev);
  103. }
  104. void BlendishButton::_updateBounds()
  105. {
  106. const float width = bndLabelWidth (getContext(), pData->iconId, pData->text);
  107. const float height = bndLabelHeight(getContext(), pData->iconId, pData->text, width);
  108. setSize(width, height);
  109. }
  110. // -----------------------------------------------------------------------
  111. END_NAMESPACE_DGL
  112. #include "oui-blendish/blendish_resources.cpp"
  113. // -----------------------------------------------------------------------