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.

132 lines
3.6KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2019-2021 Jean Pierre Cimalando <jp-dev@inbox.ru>
  4. * Copyright (C) 2012-2023 Filipe Coelho <falktx@falktx.com>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  7. * or without fee is hereby granted, provided that the above copyright notice and this
  8. * permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  11. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  12. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  13. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  15. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #pragma once
  18. #include "Cairo.hpp"
  19. START_NAMESPACE_DGL
  20. // -----------------------------------------------------------------------
  21. class DemoWidgetClickable : public CairoSubWidget,
  22. public ButtonEventHandler,
  23. public ButtonEventHandler::Callback
  24. {
  25. public:
  26. class Callback
  27. {
  28. public:
  29. virtual ~Callback() {}
  30. virtual void demoWidgetClicked(DemoWidgetClickable* widget, uint8_t colorId) = 0;
  31. };
  32. explicit DemoWidgetClickable(SubWidget* const parent)
  33. : CairoSubWidget(parent),
  34. ButtonEventHandler(this),
  35. fCallback(nullptr),
  36. fColorId(0)
  37. {
  38. ButtonEventHandler::setCallback(this);
  39. }
  40. explicit DemoWidgetClickable(TopLevelWidget* const parent)
  41. : CairoSubWidget(parent),
  42. ButtonEventHandler(this),
  43. fCallback(nullptr),
  44. fColorId(0)
  45. {
  46. ButtonEventHandler::setCallback(this);
  47. }
  48. void setCallback(Callback* const callback) noexcept
  49. {
  50. fCallback = callback;
  51. }
  52. uint8_t getColorId() const noexcept
  53. {
  54. return fColorId;
  55. }
  56. void setColorId(uint8_t colorId) noexcept
  57. {
  58. fColorId = colorId;
  59. repaint();
  60. }
  61. protected:
  62. void onCairoDisplay(const CairoGraphicsContext& context) override
  63. {
  64. cairo_t* const cr = context.handle;
  65. const Size<uint> sz = getSize();
  66. const int w = sz.getWidth();
  67. const int h = sz.getHeight();
  68. switch (fColorId)
  69. {
  70. case 0:
  71. cairo_set_source_rgb(cr, 0.75, 0.0, 0.0);
  72. break;
  73. case 1:
  74. cairo_set_source_rgb(cr, 0.0, 0.75, 0.0);
  75. break;
  76. case 2:
  77. cairo_set_source_rgb(cr, 0.0, 0.0, 0.75);
  78. break;
  79. }
  80. cairo_rectangle(cr, 0, 0, w, h);
  81. cairo_fill(cr);
  82. cairo_set_source_rgb(cr, 0.9, 0.9, 0.9);
  83. cairo_new_path(cr);
  84. cairo_move_to(cr, 0.25 * w, 0.25 * h);
  85. cairo_line_to(cr, 0.75 * w, 0.75 * h);
  86. cairo_stroke(cr);
  87. cairo_new_path(cr);
  88. cairo_move_to(cr, 0.75 * w, 0.25 * h);
  89. cairo_line_to(cr, 0.25 * w, 0.75 * h);
  90. cairo_stroke(cr);
  91. }
  92. bool onMouse(const MouseEvent& event) override
  93. {
  94. return ButtonEventHandler::mouseEvent(event);
  95. }
  96. void buttonClicked(SubWidget*, int) override
  97. {
  98. fColorId = (fColorId + 1) % 3;
  99. repaint();
  100. if (fCallback != nullptr)
  101. fCallback->demoWidgetClicked(this, fColorId);
  102. }
  103. private:
  104. Callback* fCallback;
  105. uint8_t fColorId;
  106. };
  107. // -----------------------------------------------------------------------
  108. END_NAMESPACE_DGL
  109. using DGL_NAMESPACE::DemoWidgetClickable;