container_image_button.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * File : container_image_button.h
  3. * COPYRIGHT (C) 2012-2017, Shanghai Real-Thread Technology Co., Ltd
  4. *
  5. * Change Logs:
  6. * Date Author Notes
  7. * 2017-11-05 realthread the first version
  8. */
  9. #pragma once
  10. #include "pm_container.h"
  11. namespace Persimmon
  12. {
  13. class ContainerImageButton: public Container //Derived from container, container button
  14. {
  15. public:
  16. ContainerImageButton(const Rect &rect);
  17. virtual ~ContainerImageButton();
  18. void setNorDc(struct rtgui_dc *nor) //Setting the initial background
  19. {
  20. if (this->nor)
  21. rtgui_dc_destory(this->nor);
  22. this->nor = nor;
  23. }
  24. void setSelDc(struct rtgui_dc *sel) //Setting press background
  25. {
  26. if (this->sel)
  27. rtgui_dc_destory(this->sel);
  28. this->sel = sel;
  29. }
  30. void setSelectDc(struct rtgui_dc *select) //Pictures that are covered when pressed
  31. {
  32. if (this->select)
  33. rtgui_dc_destory(this->select);
  34. this->select = select;
  35. }
  36. Signal<void> clicked; //Click event
  37. virtual bool handleGestureEvent(struct rtgui_event_gesture *, const struct rtgui_gesture *); //Touch gesture event
  38. virtual void render(struct rtgui_dc* dc, const Point &dcPoint = Point(),
  39. const Rect &srcRect = Rect(),
  40. RenderFlag flags = DrawNormal)
  41. {
  42. if (nor && selectState == false)
  43. {
  44. setBack(nor);
  45. }
  46. if (sel && selectState)
  47. {
  48. setBack(sel);
  49. }
  50. Container::render(dc, dcPoint, srcRect, flags);
  51. if (select && selectState)
  52. {
  53. struct rtgui_point dc_point = { 0, 0 };
  54. Rect rect;
  55. /* get widget rect */
  56. rect = getRect();
  57. rect.move(dcPoint);
  58. select->engine->blit(select, &dc_point, dc, rect.getRect());
  59. }
  60. }
  61. private:
  62. Widget* setBack(struct rtgui_dc* bg)
  63. {
  64. if (backgroundDc != nor && backgroundDc != sel)
  65. rtgui_dc_destory(backgroundDc);
  66. backgroundDc = bg;
  67. return this;
  68. }
  69. bool selectState;
  70. struct rtgui_dc *select;
  71. struct rtgui_dc *nor, *sel;
  72. };
  73. }