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.

102 lines
2.9KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com>
  4. * Copyright (C) 2019-2021 Jean Pierre Cimalando <jp-dev@inbox.ru>
  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. {
  23. public:
  24. explicit DemoWidgetClickable(SubWidget* const parent)
  25. : CairoSubWidget(parent),
  26. colorId(0) {}
  27. explicit DemoWidgetClickable(TopLevelWidget* const parent)
  28. : CairoSubWidget(parent),
  29. colorId(0) {}
  30. protected:
  31. void onCairoDisplay(const CairoGraphicsContext& context) override
  32. {
  33. cairo_t* const cr = context.handle;
  34. const Size<uint> sz = getSize();
  35. const int w = sz.getWidth();
  36. const int h = sz.getHeight();
  37. switch (colorId)
  38. {
  39. case 0:
  40. cairo_set_source_rgb(cr, 0.75, 0.0, 0.0);
  41. break;
  42. case 1:
  43. cairo_set_source_rgb(cr, 0.0, 0.75, 0.0);
  44. break;
  45. case 2:
  46. cairo_set_source_rgb(cr, 0.0, 0.0, 0.75);
  47. break;
  48. }
  49. cairo_rectangle(cr, 0, 0, w, h);
  50. cairo_fill(cr);
  51. cairo_set_source_rgb(cr, 0.9, 0.9, 0.9);
  52. cairo_new_path(cr);
  53. cairo_move_to(cr, 0.25 * w, 0.25 * h);
  54. cairo_line_to(cr, 0.75 * w, 0.75 * h);
  55. cairo_stroke(cr);
  56. cairo_new_path(cr);
  57. cairo_move_to(cr, 0.75 * w, 0.25 * h);
  58. cairo_line_to(cr, 0.25 * w, 0.75 * h);
  59. cairo_stroke(cr);
  60. }
  61. bool onMouse(const MouseEvent& event) override
  62. {
  63. if (event.press)
  64. {
  65. const int w = getWidth();
  66. const int h = getHeight();
  67. const int mx = event.pos.getX();
  68. const int my = event.pos.getY();
  69. // inside
  70. if (mx >= 0 && my >= 0 && mx < w && my < h)
  71. {
  72. colorId = (colorId + 1) % 3;
  73. repaint();
  74. }
  75. }
  76. return CairoSubWidget::onMouse(event);
  77. }
  78. private:
  79. uint colorId;
  80. };
  81. // -----------------------------------------------------------------------
  82. END_NAMESPACE_DGL
  83. using DGL_NAMESPACE::DemoWidgetClickable;