window.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * File : window.h
  3. * This file is part of RT-Thread GUI Engine
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2009-10-04 Bernard first version
  23. * 2010-05-03 Bernard add win close function
  24. */
  25. #ifndef __RTGUI_WINDOW_H__
  26. #define __RTGUI_WINDOW_H__
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. #include <rtgui/rtgui.h>
  31. #include <rtgui/list.h>
  32. #include <rtgui/dc.h>
  33. #include <rtgui/widgets/widget.h>
  34. #include <rtgui/widgets/box.h>
  35. DECLARE_CLASS_TYPE(win);
  36. /** Gets the type of a win */
  37. #define RTGUI_WIN_TYPE (RTGUI_TYPE(win))
  38. /** Casts the object to an rtgui_win */
  39. #define RTGUI_WIN(obj) (RTGUI_OBJECT_CAST((obj), RTGUI_WIN_TYPE, rtgui_win_t))
  40. /** Checks if the object is an rtgui_win */
  41. #define RTGUI_IS_WIN(obj) (RTGUI_OBJECT_CHECK_TYPE((obj), RTGUI_WIN_TYPE))
  42. #define RTGUI_WIN_STYLE_NO_FOCUS 0x0001 /* non-focused window */
  43. #define RTGUI_WIN_STYLE_NO_TITLE 0x0002 /* no title window */
  44. #define RTGUI_WIN_STYLE_NO_BORDER 0x0004 /* no border window */
  45. #define RTGUI_WIN_STYLE_CLOSEBOX 0x0008 /* window has the close button */
  46. #define RTGUI_WIN_STYLE_MINIBOX 0x0010 /* window has the mini button */
  47. #define RTGUI_WIN_STYLE_DESTROY_ON_CLOSE 0x0020 /* window is destroyed when closed */
  48. #define RTGUI_WIN_STYLE_ONTOP 0x0040 /* window is in the top layer */
  49. #define RTGUI_WIN_STYLE_ONBTM 0x0080 /* window is in the bottom layer */
  50. #define RTGUI_WIN_STYLE_MAINWIN 0x0106 /* window is a main window */
  51. #define RTGUI_WIN_MAGIC 0xA5A55A5A /* win magic flag */
  52. #define RTGUI_WIN_STYLE_DEFAULT (RTGUI_WIN_STYLE_CLOSEBOX | RTGUI_WIN_STYLE_MINIBOX)
  53. #define WINTITLE_HEIGHT 20
  54. #define WINTITLE_CB_WIDTH 16
  55. #define WINTITLE_CB_HEIGHT 16
  56. #define WINTITLE_BORDER_SIZE 2
  57. enum rtgui_win_flag
  58. {
  59. RTGUI_WIN_FLAG_INIT = 0x00, /* init state */
  60. RTGUI_WIN_FLAG_MODAL = 0x01, /* modal mode window */
  61. RTGUI_WIN_FLAG_CLOSED = 0x02, /* window is closed */
  62. RTGUI_WIN_FLAG_ACTIVATE = 0x04, /* window is activated */
  63. RTGUI_WIN_FLAG_UNDER_MODAL = 0x08, /* window is under modal
  64. show(modaled by other) */
  65. RTGUI_WIN_FLAG_CONNECTED = 0x10, /* connected to server */
  66. /* window is event_key dispatcher(dispatch it to the focused widget in
  67. * current window) _and_ a key handler(it should be able to handle keys
  68. * such as ESC). Both of dispatching and handling are in the same
  69. * function(rtgui_win_event_handler). So we have to distinguish between the
  70. * two modes.
  71. *
  72. * If this flag is set, we are in key-handling mode.
  73. */
  74. RTGUI_WIN_FLAG_HANDLE_KEY = 0x20,
  75. RTGUI_WIN_FLAG_CB_PRESSED = 0x40,
  76. };
  77. struct rtgui_win
  78. {
  79. /* inherit from container */
  80. rtgui_container_t parent;
  81. /* update count */
  82. rt_base_t update;
  83. /* drawing count */
  84. rt_base_t drawing;
  85. struct rtgui_rect drawing_rect;
  86. /* parent window. RT_NULL if the window is a top level window */
  87. struct rtgui_win *parent_window;
  88. struct rtgui_region outer_clip;
  89. struct rtgui_rect outer_extent;
  90. /* the widget that will grab the focus in current window */
  91. struct rtgui_widget *focused_widget;
  92. /* which app I belong */
  93. struct rtgui_app *app;
  94. /* window style */
  95. rt_uint16_t style;
  96. /* window state flag */
  97. enum rtgui_win_flag flag;
  98. rtgui_modal_code_t modal_code;
  99. /* last mouse event handled widget */
  100. rtgui_widget_t *last_mevent_widget;
  101. /* window title */
  102. char *title;
  103. struct rtgui_wintitle *_title_wgt;
  104. /* call back */
  105. rt_bool_t (*on_activate)(struct rtgui_object *widget, struct rtgui_event *event);
  106. rt_bool_t (*on_deactivate)(struct rtgui_object *widget, struct rtgui_event *event);
  107. rt_bool_t (*on_close)(struct rtgui_object *widget, struct rtgui_event *event);
  108. /* the key is sent to the focused widget by default. If the focused widget
  109. * and all of it's parents didn't handle the key event, it will be handled
  110. * by @func on_key
  111. *
  112. * If you want to handle key event on your own, it's better to overload
  113. * this function other than handle EVENT_KBD in event_handler.
  114. */
  115. rt_bool_t (*on_key)(struct rtgui_object *widget, struct rtgui_event *event);
  116. /* reserved user data */
  117. void *user_data;
  118. /* Private data. */
  119. rt_base_t (*_do_show)(struct rtgui_win *win);
  120. /* app ref_count */
  121. rt_uint16_t app_ref_count;
  122. /* win magic flag, magic value is 0xA5A55A5A */
  123. rt_uint32_t magic;
  124. };
  125. rtgui_win_t *rtgui_win_create(struct rtgui_win *parent_window, const char *title,
  126. rtgui_rect_t *rect, rt_uint16_t style);
  127. rtgui_win_t *rtgui_mainwin_create(struct rtgui_win *parent_window, const char *title, rt_uint16_t style);
  128. void rtgui_win_destroy(rtgui_win_t *win);
  129. int rtgui_win_init(struct rtgui_win *win, struct rtgui_win *parent_window,
  130. const char *title,
  131. rtgui_rect_t *rect,
  132. rt_uint16_t style);
  133. int rtgui_win_fini(struct rtgui_win* win);
  134. /** Close window.
  135. *
  136. * @param win the window you want to close
  137. *
  138. * @return RT_TRUE if the window is closed. RT_FALSE if not. If the onclose
  139. * callback returns RT_FALSE, the window won't be closed.
  140. *
  141. * \sa rtgui_win_set_onclose .
  142. */
  143. rt_bool_t rtgui_win_close(struct rtgui_win *win);
  144. rt_base_t rtgui_win_show(struct rtgui_win *win, rt_bool_t is_modal);
  145. rt_base_t rtgui_win_do_show(struct rtgui_win *win);
  146. rt_base_t rtgui_win_enter_modal(struct rtgui_win *win);
  147. void rtgui_win_hide(rtgui_win_t *win);
  148. void rtgui_win_end_modal(rtgui_win_t *win, rtgui_modal_code_t modal_code);
  149. rt_err_t rtgui_win_activate(struct rtgui_win *win);
  150. rt_bool_t rtgui_win_is_activated(struct rtgui_win *win);
  151. void rtgui_win_move(struct rtgui_win *win, int x, int y);
  152. /* reset extent of window */
  153. void rtgui_win_set_rect(rtgui_win_t *win, rtgui_rect_t *rect);
  154. void rtgui_win_update_clip(struct rtgui_win *win);
  155. void rtgui_win_set_onactivate(rtgui_win_t *win, rtgui_event_handler_ptr handler);
  156. void rtgui_win_set_ondeactivate(rtgui_win_t *win, rtgui_event_handler_ptr handler);
  157. void rtgui_win_set_onclose(rtgui_win_t *win, rtgui_event_handler_ptr handler);
  158. void rtgui_win_set_onkey(rtgui_win_t *win, rtgui_event_handler_ptr handler);
  159. rt_bool_t rtgui_win_event_handler(struct rtgui_object *win, struct rtgui_event *event);
  160. void rtgui_win_event_loop(rtgui_win_t *wnd);
  161. void rtgui_win_set_title(rtgui_win_t *win, const char *title);
  162. char *rtgui_win_get_title(rtgui_win_t *win);
  163. struct rtgui_dc *rtgui_win_get_drawing(rtgui_win_t * win);
  164. struct rtgui_win* rtgui_win_get_topmost_shown(void);
  165. struct rtgui_win* rtgui_win_get_next_shown(void);
  166. void rtgui_theme_draw_win(struct rtgui_wintitle *wint);
  167. #ifdef __cplusplus
  168. }
  169. #endif
  170. #endif