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.

153 lines
4.0KB

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