widget.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * File : widget.h
  3. * This file is part of RTGUI in RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2009-10-04 Bernard first version
  13. */
  14. #ifndef __RTGUI_WIDGET_H__
  15. #define __RTGUI_WIDGET_H__
  16. #include <rtgui/rtgui.h>
  17. #include <rtgui/list.h>
  18. #include <rtgui/region.h>
  19. #include <rtgui/event.h>
  20. #include <rtgui/color.h>
  21. #include <rtgui/font.h>
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. #define RTGUI_WIDGET_FLAG_HIDE 0x01
  26. #define RTGUI_WIDGET_FLAG_DISABLE 0x02
  27. #define RTGUI_WIDGET_FLAG_FOCUS 0x04
  28. #define RTGUI_WIDGET_FLAG_TRANSPARENT 0x08
  29. #define RTGUI_WIDGET_FLAG_FOCUSABLE 0x10
  30. #define RTGUI_WIDGET_FLAG_DEFAULT 0x00
  31. #define RTGUI_WIDGET_UNHIDE(w) (w)->flag &= ~RTGUI_WIDGET_FLAG_HIDE
  32. #define RTGUI_WIDGET_HIDE(w) (w)->flag |= RTGUI_WIDGET_FLAG_HIDE
  33. #define RTGUI_WIDGET_IS_HIDE(w) ((w)->flag & RTGUI_WIDGET_FLAG_HIDE)
  34. #define RTGUI_WIDGET_ENABLE(w) (w)->flag &= ~RTGUI_WIDGET_FLAG_DISABLE
  35. #define RTGUI_WIDGET_DISABLE(w) (w)->flag |= RTGUI_WIDGET_FLAG_DISABLE
  36. #define RTGUI_WIDGET_IS_ENABLE(w) !(w->flag & RTGUI_WIDGET_FLAG_DISABLE)
  37. #define RTGUI_WIDGET_UNFOCUS(w) (w)->flag &= ~RTGUI_WIDGET_FLAG_FOCUS
  38. #define RTGUI_WIDGET_FOCUS(w) (w)->flag |= RTGUI_WIDGET_FLAG_FOCUS
  39. #define RTGUI_WIDGET_IS_FOCUSED(w) ((w)->flag & RTGUI_WIDGET_FLAG_FOCUS)
  40. #define RTGUI_WIDGET_IS_FOCUSABLE(w) ((w)->flag & RTGUI_WIDGET_FLAG_FOCUSABLE)
  41. /* get rtgui widget object */
  42. #define RTGUI_WIDGET_FOREGROUND(w) ((w)->gc.foreground)
  43. #define RTGUI_WIDGET_BACKGROUND(w) ((w)->gc.background)
  44. #define RTGUI_WIDGET_TEXTALIGN(w) ((w)->gc.textalign)
  45. #define RTGUI_WIDGET_FONT(w) ((w)->gc.font)
  46. #define RTGUI_WIDGET_FLAG(w) ((w)->flag)
  47. /** Gets the type of a widget */
  48. #define RTGUI_WIDGET_TYPE (rtgui_widget_type_get())
  49. /** Casts the object to a rtgui_widget */
  50. #define RTGUI_WIDGET(obj) (RTGUI_OBJECT_CAST((obj), RTGUI_WIDGET_TYPE, rtgui_widget_t))
  51. /** Check if the object is a rtgui_widget */
  52. #define RTGUI_IS_WIDGET(obj) (RTGUI_OBJECT_CHECK_TYPE((obj), RTGUI_WIDGET_TYPE))
  53. /*
  54. * the base widget object
  55. */
  56. struct rtgui_widget
  57. {
  58. /* inherit from rtgui_object */
  59. struct rtgui_object object;
  60. /* the parent and root widget */
  61. struct rtgui_widget *parent, *toplevel;
  62. /* the widget children and sibling */
  63. rtgui_list_t sibling;
  64. /* widget flag */
  65. rt_int32_t flag;
  66. /* widget align */
  67. rt_int32_t align;
  68. /* the graphic context of widget */
  69. rtgui_gc_t gc;
  70. /* the widget extent */
  71. rtgui_rect_t extent;
  72. #ifndef RTGUI_USING_SMALL_SIZE
  73. rt_int16_t mini_width, mini_height;
  74. rt_int16_t margin, margin_style;
  75. #endif
  76. /* the rect clip */
  77. rtgui_region_t clip;
  78. rt_uint16_t clip_sync;
  79. /* the event handler */
  80. rt_bool_t (*event_handler) (struct rtgui_widget* widget, struct rtgui_event* event);
  81. /* call back */
  82. rt_bool_t (*on_focus_in) (struct rtgui_widget* widget, struct rtgui_event* event);
  83. rt_bool_t (*on_focus_out) (struct rtgui_widget* widget, struct rtgui_event* event);
  84. #ifndef RTGUI_USING_SMALL_SIZE
  85. rt_bool_t (*on_draw) (struct rtgui_widget* widget, struct rtgui_event* event);
  86. rt_bool_t (*on_mouseclick) (struct rtgui_widget* widget, struct rtgui_event* event);
  87. rt_bool_t (*on_key) (struct rtgui_widget* widget, struct rtgui_event* event);
  88. rt_bool_t (*on_size) (struct rtgui_widget* widget, struct rtgui_event* event);
  89. rt_bool_t (*on_command) (struct rtgui_widget* widget, struct rtgui_event* event);
  90. #endif
  91. };
  92. typedef struct rtgui_widget rtgui_widget_t;
  93. rtgui_type_t *rtgui_widget_type_get(void);
  94. rtgui_widget_t *rtgui_widget_create(rtgui_type_t *widget_type);
  95. void rtgui_widget_destroy(rtgui_widget_t* widget);
  96. /* set the event handler of widget */
  97. void rtgui_widget_set_event_handler(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
  98. /* widget default event handler */
  99. rt_bool_t rtgui_widget_event_handler(rtgui_widget_t* widget, rtgui_event_t* event);
  100. /* focus and unfocus */
  101. void rtgui_widget_focus(rtgui_widget_t * widget);
  102. void rtgui_widget_unfocus(rtgui_widget_t *widget);
  103. /* event handler for each command */
  104. void rtgui_widget_set_onfocus(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
  105. void rtgui_widget_set_onunfocus(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
  106. #ifndef RTGUI_USING_SMALL_SIZE
  107. void rtgui_widget_set_ondraw(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
  108. void rtgui_widget_set_onmouseclick(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
  109. void rtgui_widget_set_onkey(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
  110. void rtgui_widget_set_onsize(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
  111. void rtgui_widget_set_oncommand(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
  112. #endif
  113. /* get and set rect of widget */
  114. void rtgui_widget_get_rect(rtgui_widget_t* widget, rtgui_rect_t *rect);
  115. void rtgui_widget_set_rect(rtgui_widget_t* widget, rtgui_rect_t* rect);
  116. #ifndef RTGUI_USING_SMALL_SIZE
  117. void rtgui_widget_set_miniwidth(rtgui_widget_t* widget, int width);
  118. void rtgui_widget_set_miniheight(rtgui_widget_t* widget, int height);
  119. #endif
  120. /* get the physical position of a logic point on widget */
  121. void rtgui_widget_point_to_device(rtgui_widget_t * widget, rtgui_point_t * point);
  122. /* get the physical position of a logic rect on widget */
  123. void rtgui_widget_rect_to_device(rtgui_widget_t * widget, rtgui_rect_t * rect);
  124. /* get the logic position of a physical point on widget */
  125. void rtgui_widget_point_to_logic(rtgui_widget_t* widget, rtgui_point_t * point);
  126. /* get the logic position of a physical rect on widget */
  127. void rtgui_widget_rect_to_logic(rtgui_widget_t* widget, rtgui_rect_t* rect);
  128. /* move widget and its children to a logic point */
  129. void rtgui_widget_move_to_logic(rtgui_widget_t* widget, int dx, int dy);
  130. /* update the clip info of widget */
  131. void rtgui_widget_update_clip(rtgui_widget_t* widget);
  132. /* get the toplevel widget of widget */
  133. rtgui_widget_t* rtgui_widget_get_toplevel(rtgui_widget_t* widget);
  134. void rtgui_widget_show(rtgui_widget_t* widget);
  135. void rtgui_widget_hide(rtgui_widget_t* widget);
  136. void rtgui_widget_update(rtgui_widget_t* widget);
  137. /* get parent color */
  138. rtgui_color_t rtgui_widget_get_parent_foreground(rtgui_widget_t* widget);
  139. rtgui_color_t rtgui_widget_get_parent_background(rtgui_widget_t* widget);
  140. /* get the next sibling of widget */
  141. rtgui_widget_t* rtgui_widget_get_next_sibling(rtgui_widget_t* widget);
  142. /* get the prev sibling of widget */
  143. rtgui_widget_t* rtgui_widget_get_prev_sibling(rtgui_widget_t* widget);
  144. #ifdef __cplusplus
  145. }
  146. #endif
  147. #endif