window.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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_STYLE_DEFAULT (RTGUI_WIN_STYLE_CLOSEBOX | RTGUI_WIN_STYLE_MINIBOX)
  52. #define WINTITLE_HEIGHT 20
  53. #define WINTITLE_CB_WIDTH 16
  54. #define WINTITLE_CB_HEIGHT 16
  55. #define WINTITLE_BORDER_SIZE 2
  56. enum rtgui_win_flag
  57. {
  58. RTGUI_WIN_FLAG_INIT = 0x00, /* init state */
  59. RTGUI_WIN_FLAG_MODAL = 0x01, /* modal mode window */
  60. RTGUI_WIN_FLAG_CLOSED = 0x02, /* window is closed */
  61. RTGUI_WIN_FLAG_ACTIVATE = 0x04, /* window is activated */
  62. RTGUI_WIN_FLAG_UNDER_MODAL = 0x08, /* window is under modal
  63. show(modaled by other) */
  64. RTGUI_WIN_FLAG_CONNECTED = 0x10, /* connected to server */
  65. /* window is event_key dispatcher(dispatch it to the focused widget in
  66. * current window) _and_ a key handler(it should be able to handle keys
  67. * such as ESC). Both of dispatching and handling are in the same
  68. * function(rtgui_win_event_handler). So we have to distinguish between the
  69. * two modes.
  70. *
  71. * If this flag is set, we are in key-handling mode.
  72. */
  73. RTGUI_WIN_FLAG_HANDLE_KEY = 0x20,
  74. RTGUI_WIN_FLAG_CB_PRESSED = 0x40,
  75. };
  76. struct rtgui_win
  77. {
  78. /* inherit from container */
  79. rtgui_container_t parent;
  80. /* update count */
  81. rt_base_t update;
  82. /* drawing count */
  83. rt_base_t drawing;
  84. struct rtgui_rect drawing_rect;
  85. /* parent window. RT_NULL if the window is a top level window */
  86. struct rtgui_win *parent_window;
  87. struct rtgui_region outer_clip;
  88. struct rtgui_rect outer_extent;
  89. /* the widget that will grab the focus in current window */
  90. struct rtgui_widget *focused_widget;
  91. /* which app I belong */
  92. struct rtgui_app *app;
  93. /* window style */
  94. rt_uint16_t style;
  95. /* window state flag */
  96. enum rtgui_win_flag flag;
  97. rtgui_modal_code_t modal_code;
  98. /* last mouse event handled widget */
  99. rtgui_widget_t *last_mevent_widget;
  100. /* window title */
  101. char *title;
  102. struct rtgui_wintitle *_title_wgt;
  103. /* call back */
  104. rt_bool_t (*on_activate)(struct rtgui_object *widget, struct rtgui_event *event);
  105. rt_bool_t (*on_deactivate)(struct rtgui_object *widget, struct rtgui_event *event);
  106. rt_bool_t (*on_close)(struct rtgui_object *widget, struct rtgui_event *event);
  107. /* the key is sent to the focused widget by default. If the focused widget
  108. * and all of it's parents didn't handle the key event, it will be handled
  109. * by @func on_key
  110. *
  111. * If you want to handle key event on your own, it's better to overload
  112. * this function other than handle EVENT_KBD in event_handler.
  113. */
  114. rt_bool_t (*on_key)(struct rtgui_object *widget, struct rtgui_event *event);
  115. /* reserved user data */
  116. void *user_data;
  117. /* Private data. */
  118. rt_base_t (*_do_show)(struct rtgui_win *win);
  119. /* app ref_count */
  120. rt_uint16_t app_ref_count;
  121. /* win magic flag, magic value is 0xA5A55A5A */
  122. rt_uint32_t magic;
  123. };
  124. rtgui_win_t *rtgui_win_create(struct rtgui_win *parent_window, const char *title,
  125. rtgui_rect_t *rect, rt_uint16_t style);
  126. rtgui_win_t *rtgui_mainwin_create(struct rtgui_win *parent_window, const char *title, rt_uint16_t style);
  127. void rtgui_win_destroy(rtgui_win_t *win);
  128. int rtgui_win_init(struct rtgui_win *win, struct rtgui_win *parent_window,
  129. const char *title,
  130. rtgui_rect_t *rect,
  131. rt_uint16_t style);
  132. int rtgui_win_fini(struct rtgui_win* win);
  133. /** Close window.
  134. *
  135. * @param win the window you want to close
  136. *
  137. * @return RT_TRUE if the window is closed. RT_FALSE if not. If the onclose
  138. * callback returns RT_FALSE, the window won't be closed.
  139. *
  140. * \sa rtgui_win_set_onclose .
  141. */
  142. rt_bool_t rtgui_win_close(struct rtgui_win *win);
  143. rt_base_t rtgui_win_show(struct rtgui_win *win, rt_bool_t is_modal);
  144. rt_base_t rtgui_win_do_show(struct rtgui_win *win);
  145. rt_base_t rtgui_win_enter_modal(struct rtgui_win *win);
  146. void rtgui_win_hide(rtgui_win_t *win);
  147. void rtgui_win_end_modal(rtgui_win_t *win, rtgui_modal_code_t modal_code);
  148. rt_err_t rtgui_win_activate(struct rtgui_win *win);
  149. rt_bool_t rtgui_win_is_activated(struct rtgui_win *win);
  150. void rtgui_win_move(struct rtgui_win *win, int x, int y);
  151. /* reset extent of window */
  152. void rtgui_win_set_rect(rtgui_win_t *win, rtgui_rect_t *rect);
  153. void rtgui_win_update_clip(struct rtgui_win *win);
  154. void rtgui_win_set_onactivate(rtgui_win_t *win, rtgui_event_handler_ptr handler);
  155. void rtgui_win_set_ondeactivate(rtgui_win_t *win, rtgui_event_handler_ptr handler);
  156. void rtgui_win_set_onclose(rtgui_win_t *win, rtgui_event_handler_ptr handler);
  157. void rtgui_win_set_onkey(rtgui_win_t *win, rtgui_event_handler_ptr handler);
  158. rt_bool_t rtgui_win_event_handler(struct rtgui_object *win, struct rtgui_event *event);
  159. void rtgui_win_event_loop(rtgui_win_t *wnd);
  160. void rtgui_win_set_title(rtgui_win_t *win, const char *title);
  161. char *rtgui_win_get_title(rtgui_win_t *win);
  162. struct rtgui_dc *rtgui_win_get_drawing(rtgui_win_t * win);
  163. struct rtgui_win* rtgui_win_get_topmost_shown(void);
  164. struct rtgui_win* rtgui_win_get_next_shown(void);
  165. void rtgui_theme_draw_win(struct rtgui_wintitle *wint);
  166. #ifdef __cplusplus
  167. }
  168. #endif
  169. #endif