widget.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. struct rtgui_gc
  54. {
  55. /* foreground and background color */
  56. rtgui_color_t foreground, background;
  57. /* text align */
  58. rt_base_t textalign;
  59. /* font */
  60. rtgui_font_t* font;
  61. };
  62. typedef struct rtgui_gc rtgui_gc_t;
  63. /*
  64. * the base widget object
  65. */
  66. struct rtgui_widget
  67. {
  68. /* inherit from rtgui_object */
  69. struct rtgui_object object;
  70. /* the parent and root widget */
  71. struct rtgui_widget *parent, *toplevel;
  72. /* the widget children and sibling */
  73. rtgui_list_t sibling;
  74. /* widget flag */
  75. rt_int32_t flag;
  76. /* widget align */
  77. rt_int32_t align;
  78. /* the graphic context of widget */
  79. rtgui_gc_t gc;
  80. /* the widget extent */
  81. rtgui_rect_t extent;
  82. #ifndef RTGUI_USING_SMALL_SIZE
  83. rt_int16_t mini_width, mini_height;
  84. rt_int16_t margin, margin_style;
  85. #endif
  86. /* the rect clip */
  87. rtgui_region_t clip;
  88. #ifndef RTGUI_USING_SMALL_SIZE
  89. rt_uint32_t clip_sync;
  90. #endif
  91. /* the event handler */
  92. rt_bool_t (*event_handler) (struct rtgui_widget* widget, struct rtgui_event* event);
  93. /* call back */
  94. rt_bool_t (*on_focus_in) (struct rtgui_widget* widget, struct rtgui_event* event);
  95. rt_bool_t (*on_focus_out) (struct rtgui_widget* widget, struct rtgui_event* event);
  96. #ifndef RTGUI_USING_SMALL_SIZE
  97. rt_bool_t (*on_draw) (struct rtgui_widget* widget, struct rtgui_event* event);
  98. rt_bool_t (*on_mouseclick) (struct rtgui_widget* widget, struct rtgui_event* event);
  99. rt_bool_t (*on_key) (struct rtgui_widget* widget, struct rtgui_event* event);
  100. rt_bool_t (*on_size) (struct rtgui_widget* widget, struct rtgui_event* event);
  101. rt_bool_t (*on_command) (struct rtgui_widget* widget, struct rtgui_event* event);
  102. #endif
  103. };
  104. typedef struct rtgui_widget rtgui_widget_t;
  105. rtgui_type_t *rtgui_widget_type_get(void);
  106. rtgui_widget_t *rtgui_widget_create(rtgui_type_t *widget_type);
  107. void rtgui_widget_destroy(rtgui_widget_t* widget);
  108. /* set the event handler of widget */
  109. void rtgui_widget_set_event_handler(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
  110. /* widget default event handler */
  111. rt_bool_t rtgui_widget_event_handler(rtgui_widget_t* widget, rtgui_event_t* event);
  112. /* focus and unfocus */
  113. void rtgui_widget_focus(rtgui_widget_t * widget);
  114. void rtgui_widget_unfocus(rtgui_widget_t *widget);
  115. /* event handler for each command */
  116. void rtgui_widget_set_onfocus(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
  117. void rtgui_widget_set_onunfocus(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
  118. #ifndef RTGUI_USING_SMALL_SIZE
  119. void rtgui_widget_set_ondraw(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
  120. void rtgui_widget_set_onmouseclick(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
  121. void rtgui_widget_set_onkey(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
  122. void rtgui_widget_set_onsize(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
  123. void rtgui_widget_set_oncommand(rtgui_widget_t* widget, rtgui_event_handler_ptr handler);
  124. #endif
  125. /* get and set rect of widget */
  126. void rtgui_widget_get_rect(rtgui_widget_t* widget, rtgui_rect_t *rect);
  127. void rtgui_widget_set_rect(rtgui_widget_t* widget, rtgui_rect_t* rect);
  128. #ifndef RTGUI_USING_SMALL_SIZE
  129. void rtgui_widget_set_miniwidth(rtgui_widget_t* widget, int width);
  130. void rtgui_widget_set_miniheight(rtgui_widget_t* widget, int height);
  131. #endif
  132. /* get the physical position of a logic point on widget */
  133. void rtgui_widget_point_to_device(rtgui_widget_t * widget, rtgui_point_t * point);
  134. /* get the physical position of a logic rect on widget */
  135. void rtgui_widget_rect_to_device(rtgui_widget_t * widget, rtgui_rect_t * rect);
  136. /* get the logic position of a physical point on widget */
  137. void rtgui_widget_point_to_logic(rtgui_widget_t* widget, rtgui_point_t * point);
  138. /* get the logic position of a physical rect on widget */
  139. void rtgui_widget_rect_to_logic(rtgui_widget_t* widget, rtgui_rect_t* rect);
  140. /* move widget and its children to a logic point */
  141. void rtgui_widget_move_to_logic(rtgui_widget_t* widget, int dx, int dy);
  142. /* update the clip info of widget */
  143. void rtgui_widget_update_clip(rtgui_widget_t* widget);
  144. /* get the toplevel widget of widget */
  145. rtgui_widget_t* rtgui_widget_get_toplevel(rtgui_widget_t* widget);
  146. void rtgui_widget_show(rtgui_widget_t* widget);
  147. void rtgui_widget_hide(rtgui_widget_t* widget);
  148. void rtgui_widget_update(rtgui_widget_t* widget);
  149. /* get parent color */
  150. rtgui_color_t rtgui_widget_get_parent_foreground(rtgui_widget_t* widget);
  151. rtgui_color_t rtgui_widget_get_parent_background(rtgui_widget_t* widget);
  152. /* get the next sibling of widget */
  153. rtgui_widget_t* rtgui_widget_get_next_sibling(rtgui_widget_t* widget);
  154. /* get the prev sibling of widget */
  155. rtgui_widget_t* rtgui_widget_get_prev_sibling(rtgui_widget_t* widget);
  156. #ifdef __cplusplus
  157. }
  158. #endif
  159. #endif