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.

130 lines
3.5KB

  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. #ifndef DGL_COMMON_HPP_INCLUDED
  17. #define DGL_COMMON_HPP_INCLUDED
  18. #include "../ImageWidgets.hpp"
  19. #include "../NanoWidgets.hpp"
  20. START_NAMESPACE_DGL
  21. // -----------------------------------------------------------------------
  22. struct ButtonImpl {
  23. enum State {
  24. kStateNormal = 0,
  25. kStateHover,
  26. kStateDown
  27. };
  28. int button;
  29. int state;
  30. Widget* self;
  31. BlendishButton::Callback* callback_b;
  32. ImageButton::Callback* callback_i;
  33. ButtonImpl(Widget* const s) noexcept
  34. : button(-1),
  35. state(kStateNormal),
  36. self(s),
  37. callback_b(nullptr),
  38. callback_i(nullptr) {}
  39. bool onMouse(const Widget::MouseEvent& ev)
  40. {
  41. // button was released, handle it now
  42. if (button != -1 && ! ev.press)
  43. {
  44. DISTRHO_SAFE_ASSERT(state == kStateDown);
  45. // release button
  46. const int button2 = button;
  47. button = -1;
  48. // cursor was moved outside the button bounds, ignore click
  49. if (! self->contains(ev.pos))
  50. {
  51. state = kStateNormal;
  52. self->repaint();
  53. return true;
  54. }
  55. // still on bounds, register click
  56. state = kStateHover;
  57. self->repaint();
  58. if (callback_b != nullptr)
  59. callback_b->blendishButtonClicked((BlendishButton*)self, button2);
  60. if (callback_i != nullptr)
  61. callback_i->imageButtonClicked((ImageButton*)self, button2);
  62. return true;
  63. }
  64. // button was pressed, wait for release
  65. if (ev.press && self->contains(ev.pos))
  66. {
  67. button = ev.button;
  68. state = kStateDown;
  69. self->repaint();
  70. return true;
  71. }
  72. return false;
  73. }
  74. bool onMotion(const Widget::MotionEvent& ev)
  75. {
  76. // keep pressed
  77. if (button != -1)
  78. return true;
  79. if (self->contains(ev.pos))
  80. {
  81. // check if entering hover
  82. if (state == kStateNormal)
  83. {
  84. state = kStateHover;
  85. self->repaint();
  86. return true;
  87. }
  88. }
  89. else
  90. {
  91. // check if exiting hover
  92. if (state == kStateHover)
  93. {
  94. state = kStateNormal;
  95. self->repaint();
  96. return true;
  97. }
  98. }
  99. return false;
  100. }
  101. DISTRHO_PREVENT_HEAP_ALLOCATION
  102. DISTRHO_DECLARE_NON_COPY_STRUCT(ButtonImpl)
  103. };
  104. // -----------------------------------------------------------------------
  105. END_NAMESPACE_DGL
  106. #endif // DGL_APP_PRIVATE_DATA_HPP_INCLUDED