widget.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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_DEFAULT 0x0000
  26. #define RTGUI_WIDGET_FLAG_SHOWN 0x0001
  27. #define RTGUI_WIDGET_FLAG_DISABLE 0x0002
  28. #define RTGUI_WIDGET_FLAG_FOCUS 0x0004
  29. #define RTGUI_WIDGET_FLAG_TRANSPARENT 0x0008
  30. #define RTGUI_WIDGET_FLAG_FOCUSABLE 0x0010
  31. #define RTGUI_WIDGET_FLAG_DC_VISIBLE 0x0100
  32. /* rtgui widget attribute */
  33. #define RTGUI_WIDGET_FOREGROUND(w) (RTGUI_WIDGET(w)->gc.foreground)
  34. #define RTGUI_WIDGET_BACKGROUND(w) (RTGUI_WIDGET(w)->gc.background)
  35. #define RTGUI_WIDGET_TEXTALIGN(w) (RTGUI_WIDGET(w)->gc.textalign)
  36. #define RTGUI_WIDGET_FONT(w) (RTGUI_WIDGET(w)->gc.font)
  37. #define RTGUI_WIDGET_FLAG(w) (RTGUI_WIDGET(w)->flag)
  38. #define RTGUI_WIDGET_ALIGN(w) (RTGUI_WIDGET(w)->align)
  39. #define RTGUI_WIDGET_BORDER(w) (RTGUI_WIDGET(w)->border)
  40. #define RTGUI_WIDGET_BORDER_STYLE(w) (RTGUI_WIDGET(w)->border_style)
  41. #define RTGUI_WIDGET_UNHIDE(w) RTGUI_WIDGET_FLAG(w) |= RTGUI_WIDGET_FLAG_SHOWN
  42. #define RTGUI_WIDGET_HIDE(w) RTGUI_WIDGET_FLAG(w) &= ~RTGUI_WIDGET_FLAG_SHOWN
  43. #define RTGUI_WIDGET_IS_HIDE(w) (!(RTGUI_WIDGET_FLAG(w) & RTGUI_WIDGET_FLAG_SHOWN))
  44. #define RTGUI_WIDGET_ENABLE(w) RTGUI_WIDGET_FLAG(w) &= ~RTGUI_WIDGET_FLAG_DISABLE
  45. #define RTGUI_WIDGET_DISABLE(w) RTGUI_WIDGET_FLAG(w) |= RTGUI_WIDGET_FLAG_DISABLE
  46. #define RTGUI_WIDGET_IS_ENABLE(w) (!((RTGUI_WIDGET_FLAG(w) & RTGUI_WIDGET_FLAG_DISABLE)))
  47. #define RTGUI_WIDGET_UNFOCUS(w) RTGUI_WIDGET_FLAG(w) &= ~RTGUI_WIDGET_FLAG_FOCUS
  48. #define RTGUI_WIDGET_FOCUS(w) RTGUI_WIDGET_FLAG(w) |= RTGUI_WIDGET_FLAG_FOCUS
  49. #define RTGUI_WIDGET_IS_FOCUSED(w) (RTGUI_WIDGET_FLAG(w) & RTGUI_WIDGET_FLAG_FOCUS)
  50. #define RTGUI_WIDGET_IS_FOCUSABLE(w) (RTGUI_WIDGET_FLAG(w) & RTGUI_WIDGET_FLAG_FOCUSABLE)
  51. #define RTGUI_WIDGET_IS_DC_VISIBLE(w) (RTGUI_WIDGET_FLAG(w) & RTGUI_WIDGET_FLAG_DC_VISIBLE)
  52. #define RTGUI_WIDGET_DC_SET_VISIBLE(w) RTGUI_WIDGET_FLAG(w) |= RTGUI_WIDGET_FLAG_DC_VISIBLE
  53. #define RTGUI_WIDGET_DC_SET_UNVISIBLE(w) RTGUI_WIDGET_FLAG(w) &= ~RTGUI_WIDGET_FLAG_DC_VISIBLE
  54. #define RTGUI_WIDGET_DC(w) ((struct rtgui_dc*)&((w)->dc_type))
  55. DECLARE_CLASS_TYPE(widget);
  56. /** Gets the type of a widget */
  57. #define RTGUI_WIDGET_TYPE (RTGUI_TYPE(widget))
  58. /** Casts the object to a rtgui_widget */
  59. #define RTGUI_WIDGET(obj) (RTGUI_OBJECT_CAST((obj), RTGUI_WIDGET_TYPE, rtgui_widget_t))
  60. /** Check if the object is a rtgui_widget */
  61. #define RTGUI_IS_WIDGET(obj) (RTGUI_OBJECT_CHECK_TYPE((obj), RTGUI_WIDGET_TYPE))
  62. /*
  63. * the base widget object
  64. */
  65. struct rtgui_widget
  66. {
  67. /* inherit from rtgui_object */
  68. struct rtgui_object object;
  69. /* the widget that contains this widget */
  70. struct rtgui_widget *parent;
  71. /* the window that contains this widget */
  72. struct rtgui_win *toplevel;
  73. /* the widget children and sibling */
  74. rtgui_list_t sibling;
  75. /* widget flag */
  76. rt_int32_t flag;
  77. /* hardware device context */
  78. rt_uint32_t dc_type;
  79. const struct rtgui_dc_engine* dc_engine;
  80. /* the graphic context of widget */
  81. rtgui_gc_t gc;
  82. /* the widget extent */
  83. rtgui_rect_t extent;
  84. /* minimal width and height of widget */
  85. rt_int16_t mini_width, mini_height;
  86. /* widget align */
  87. rt_int32_t align;
  88. rt_uint16_t border;
  89. rt_uint16_t border_style;
  90. /* the rect clip */
  91. rtgui_region_t clip;
  92. /* call back */
  93. rt_bool_t (*on_focus_in) (struct rtgui_object* widget, struct rtgui_event* event);
  94. rt_bool_t (*on_focus_out) (struct rtgui_object* widget, struct rtgui_event* event);
  95. /* will be called just before the widget is shown. You can setup your
  96. * widget in this call back. It's return value is ignored. The @param event
  97. * will always be RT_NULL
  98. */
  99. rt_bool_t (*on_show) (struct rtgui_object* widget, struct rtgui_event* event);
  100. /* will be called just before the widget is hiden. You can setup your
  101. * widget in this call back. It's return value is ignored. The @param event
  102. * will always be RT_NULL
  103. */
  104. rt_bool_t (*on_hide) (struct rtgui_object* widget, struct rtgui_event* event);
  105. #ifndef RTGUI_USING_SMALL_SIZE
  106. rt_bool_t (*on_draw) (struct rtgui_object* widget, struct rtgui_event* event);
  107. rt_bool_t (*on_mouseclick) (struct rtgui_object* widget, struct rtgui_event* event);
  108. rt_bool_t (*on_key) (struct rtgui_object* widget, struct rtgui_event* event);
  109. rt_bool_t (*on_size) (struct rtgui_object* widget, struct rtgui_event* event);
  110. rt_bool_t (*on_command) (struct rtgui_object* widget, struct rtgui_event* event);
  111. #endif
  112. /* user private data */
  113. rt_uint32_t user_data;
  114. };
  115. typedef struct rtgui_widget rtgui_widget_t;
  116. rtgui_widget_t *rtgui_widget_create(rtgui_type_t *widget_type);
  117. void rtgui_widget_destroy(rtgui_widget_t* widget);
  118. rt_bool_t rtgui_widget_event_handler(struct rtgui_object* object, rtgui_event_t* event);
  119. /* focus and unfocus */
  120. void rtgui_widget_focus(rtgui_widget_t * widget);
  121. void rtgui_widget_unfocus(rtgui_widget_t *widget);
  122. /* event handler for each command */
  123. void rtgui_widget_set_onfocus(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
  124. void rtgui_widget_set_onunfocus(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
  125. void rtgui_widget_set_onshow(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
  126. void rtgui_widget_set_onhide(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
  127. #ifndef RTGUI_USING_SMALL_SIZE
  128. void rtgui_widget_set_ondraw(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
  129. void rtgui_widget_set_onmouseclick(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
  130. void rtgui_widget_set_onkey(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
  131. void rtgui_widget_set_onsize(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
  132. void rtgui_widget_set_oncommand(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
  133. #endif
  134. /* get and set rect of widget */
  135. void rtgui_widget_get_rect(rtgui_widget_t* widget, rtgui_rect_t *rect);
  136. void rtgui_widget_set_border(rtgui_widget_t* widget, rt_uint32_t style);
  137. void rtgui_widget_set_rect(rtgui_widget_t* widget, const rtgui_rect_t* rect);
  138. void rtgui_widget_set_rectangle(rtgui_widget_t* widget, int x, int y, int width, int height);
  139. void rtgui_widget_get_extent(rtgui_widget_t* widget, rtgui_rect_t *rect);
  140. #ifndef RTGUI_USING_SMALL_SIZE
  141. void rtgui_widget_set_miniwidth(rtgui_widget_t* widget, int width);
  142. void rtgui_widget_set_miniheight(rtgui_widget_t* widget, int height);
  143. #endif
  144. void rtgui_widget_set_parent(rtgui_widget_t* widget, rtgui_widget_t* parent);
  145. /* get the physical position of a logic point on widget */
  146. void rtgui_widget_point_to_device(rtgui_widget_t * widget, rtgui_point_t * point);
  147. /* get the physical position of a logic rect on widget */
  148. void rtgui_widget_rect_to_device(rtgui_widget_t * widget, rtgui_rect_t * rect);
  149. /* get the logic position of a physical point on widget */
  150. void rtgui_widget_point_to_logic(rtgui_widget_t* widget, rtgui_point_t * point);
  151. /* get the logic position of a physical rect on widget */
  152. void rtgui_widget_rect_to_logic(rtgui_widget_t* widget, rtgui_rect_t* rect);
  153. /* move widget and its children to a logic point */
  154. void rtgui_widget_move_to_logic(rtgui_widget_t* widget, int dx, int dy);
  155. /* update the clip info of widget */
  156. void rtgui_widget_update_clip(rtgui_widget_t* widget);
  157. /* get the toplevel widget of widget */
  158. struct rtgui_win* rtgui_widget_get_toplevel(rtgui_widget_t* widget);
  159. rt_bool_t rtgui_widget_onupdate_toplvl(struct rtgui_object *object, struct rtgui_event *event);
  160. void rtgui_widget_show(rtgui_widget_t* widget);
  161. rt_bool_t rtgui_widget_onshow(struct rtgui_object *object, struct rtgui_event *event);
  162. void rtgui_widget_hide(rtgui_widget_t* widget);
  163. rt_bool_t rtgui_widget_onhide(struct rtgui_object *object, struct rtgui_event *event);
  164. void rtgui_widget_update(rtgui_widget_t* widget);
  165. /* get parent color */
  166. rtgui_color_t rtgui_widget_get_parent_foreground(rtgui_widget_t* widget);
  167. rtgui_color_t rtgui_widget_get_parent_background(rtgui_widget_t* widget);
  168. /* get the next sibling of widget */
  169. rtgui_widget_t* rtgui_widget_get_next_sibling(rtgui_widget_t* widget);
  170. /* get the prev sibling of widget */
  171. rtgui_widget_t* rtgui_widget_get_prev_sibling(rtgui_widget_t* widget);
  172. /* dump widget information */
  173. void rtgui_widget_dump(rtgui_widget_t* widget);
  174. #ifdef __cplusplus
  175. }
  176. #endif
  177. #endif