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.

75 lines
1.8KB

  1. // Copyright Jean Pierre Cimalando 2018.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include "DemoWidgetClickable.h"
  6. #include "Cairo.hpp"
  7. #include "Window.hpp"
  8. DemoWidgetClickable::DemoWidgetClickable(Widget *group)
  9. : Widget(group)
  10. {
  11. }
  12. void DemoWidgetClickable::onDisplay()
  13. {
  14. cairo_t *cr = getParentWindow().getGraphicsContext().cairo;
  15. Point<int> pt = getAbsolutePos();
  16. Size<uint> sz = getSize();
  17. int x = pt.getX();
  18. int y = pt.getY();
  19. int w = sz.getWidth();
  20. int h = sz.getHeight();
  21. switch (colorid_) {
  22. case 0:
  23. cairo_set_source_rgb(cr, 0.75, 0.0, 0.0);
  24. break;
  25. case 1:
  26. cairo_set_source_rgb(cr, 0.0, 0.75, 0.0);
  27. break;
  28. case 2:
  29. cairo_set_source_rgb(cr, 0.0, 0.0, 0.75);
  30. break;
  31. }
  32. cairo_rectangle(cr, x, y, w, h);
  33. cairo_fill(cr);
  34. cairo_set_source_rgb(cr, 0.9, 0.9, 0.9);
  35. cairo_new_path(cr);
  36. cairo_move_to(cr, x + 0.25 * w, y + 0.25 * h);
  37. cairo_line_to(cr, x + 0.75 * w, y + 0.75 * h);
  38. cairo_stroke(cr);
  39. cairo_new_path(cr);
  40. cairo_move_to(cr, x + 0.75 * w, y + 0.25 * h);
  41. cairo_line_to(cr, x + 0.25 * w, y + 0.75 * h);
  42. cairo_stroke(cr);
  43. }
  44. bool DemoWidgetClickable::onMouse(const MouseEvent &event)
  45. {
  46. if (event.press) {
  47. Point<int> pos = getAbsolutePos();
  48. Size<uint> size = getSize();
  49. int mx = event.pos.getX();
  50. int my = event.pos.getY();
  51. int px = pos.getX();
  52. int py = pos.getY();
  53. bool inside = mx >= 0 && my >= 0 &&
  54. mx < size.getWidth() && my < size.getHeight();
  55. if (inside) {
  56. colorid_ = (colorid_ + 1) % 3;
  57. repaint();
  58. }
  59. }
  60. return Widget::onMouse(event);
  61. }