window.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. /* drawing count */
  81. rt_base_t drawing;
  82. struct rtgui_rect drawing_rect;
  83. /* parent window. RT_NULL if the window is a top level window */
  84. struct rtgui_win *parent_window;
  85. struct rtgui_region outer_clip;
  86. struct rtgui_rect outer_extent;
  87. /* the widget that will grab the focus in current window */
  88. struct rtgui_widget *focused_widget;
  89. /* which app I belong */
  90. struct rtgui_app *app;
  91. /* window style */
  92. rt_uint16_t style;
  93. /* window state flag */
  94. enum rtgui_win_flag flag;
  95. rtgui_modal_code_t modal_code;
  96. /* last mouse event handled widget */
  97. rtgui_widget_t *last_mevent_widget;
  98. /* window title */
  99. char *title;
  100. struct rtgui_wintitle *_title_wgt;
  101. /* call back */
  102. rt_bool_t (*on_activate)(struct rtgui_object *widget, struct rtgui_event *event);
  103. rt_bool_t (*on_deactivate)(struct rtgui_object *widget, struct rtgui_event *event);
  104. rt_bool_t (*on_close)(struct rtgui_object *widget, struct rtgui_event *event);
  105. /* the key is sent to the focused widget by default. If the focused widget
  106. * and all of it's parents didn't handle the key event, it will be handled
  107. * by @func on_key
  108. *
  109. * If you want to handle key event on your own, it's better to overload
  110. * this function other than handle EVENT_KBD in event_handler.
  111. */
  112. rt_bool_t (*on_key)(struct rtgui_object *widget, struct rtgui_event *event);
  113. /* reserved user data */
  114. void *user_data;
  115. /* Private data. */
  116. rt_base_t (*_do_show)(struct rtgui_win *win);
  117. };
  118. rtgui_win_t *rtgui_win_create(struct rtgui_win *parent_window, const char *title,
  119. rtgui_rect_t *rect, rt_uint16_t style);
  120. rtgui_win_t *rtgui_mainwin_create(struct rtgui_win *parent_window, const char *title, rt_uint16_t style);
  121. void rtgui_win_destroy(rtgui_win_t *win);
  122. int rtgui_win_init(struct rtgui_win *win, struct rtgui_win *parent_window,
  123. const char *title,
  124. rtgui_rect_t *rect,
  125. rt_uint16_t style);
  126. int rtgui_win_fini(struct rtgui_win* win);
  127. /** Close window.
  128. *
  129. * @param win the window you want to close
  130. *
  131. * @return RT_TRUE if the window is closed. RT_FALSE if not. If the onclose
  132. * callback returns RT_FALSE, the window won't be closed.
  133. *
  134. * \sa rtgui_win_set_onclose .
  135. */
  136. rt_bool_t rtgui_win_close(struct rtgui_win *win);
  137. rt_base_t rtgui_win_show(struct rtgui_win *win, rt_bool_t is_modal);
  138. rt_base_t rtgui_win_do_show(struct rtgui_win *win);
  139. rt_base_t rtgui_win_enter_modal(struct rtgui_win *win);
  140. void rtgui_win_hide(rtgui_win_t *win);
  141. void rtgui_win_end_modal(rtgui_win_t *win, rtgui_modal_code_t modal_code);
  142. rt_err_t rtgui_win_activate(struct rtgui_win *win);
  143. rt_bool_t rtgui_win_is_activated(struct rtgui_win *win);
  144. void rtgui_win_move(struct rtgui_win *win, int x, int y);
  145. /* reset extent of window */
  146. void rtgui_win_set_rect(rtgui_win_t *win, rtgui_rect_t *rect);
  147. void rtgui_win_update_clip(struct rtgui_win *win);
  148. void rtgui_win_set_onactivate(rtgui_win_t *win, rtgui_event_handler_ptr handler);
  149. void rtgui_win_set_ondeactivate(rtgui_win_t *win, rtgui_event_handler_ptr handler);
  150. void rtgui_win_set_onclose(rtgui_win_t *win, rtgui_event_handler_ptr handler);
  151. void rtgui_win_set_onkey(rtgui_win_t *win, rtgui_event_handler_ptr handler);
  152. rt_bool_t rtgui_win_event_handler(struct rtgui_object *win, struct rtgui_event *event);
  153. void rtgui_win_event_loop(rtgui_win_t *wnd);
  154. void rtgui_win_set_title(rtgui_win_t *win, const char *title);
  155. char *rtgui_win_get_title(rtgui_win_t *win);
  156. struct rtgui_dc *rtgui_win_get_drawing(rtgui_win_t * win);
  157. struct rtgui_win* rtgui_win_get_topmost_shown(void);
  158. struct rtgui_win* rtgui_win_get_next_shown(void);
  159. void rtgui_theme_draw_win(struct rtgui_wintitle *wint);
  160. #ifdef __cplusplus
  161. }
  162. #endif
  163. #endif