diff options
| author | bernard.xiong <[email protected]> | 2009-10-04 02:56:38 +0000 |
|---|---|---|
| committer | bernard.xiong <[email protected]> | 2009-10-04 02:56:38 +0000 |
| commit | 90f9d0a2ab733f3930907693fceadba1d01c0df4 (patch) | |
| tree | 2fa88428f28ba4fb071afcd98c585d30176ef674 /rtgui/widgets | |
| parent | e3248a5045bd59b32c0b97a05c771928316f3aff (diff) | |
add basic files of RTGUI
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@69 bbd45198-f89e-11dd-88c7-29a3b14d5316
Diffstat (limited to 'rtgui/widgets')
| -rw-r--r-- | rtgui/widgets/widget.c | 497 | ||||
| -rw-r--r-- | rtgui/widgets/window.c | 467 | ||||
| -rw-r--r-- | rtgui/widgets/workbench.c | 472 |
3 files changed, 1436 insertions, 0 deletions
diff --git a/rtgui/widgets/widget.c b/rtgui/widgets/widget.c new file mode 100644 index 0000000000..4363baf1c1 --- /dev/null +++ b/rtgui/widgets/widget.c @@ -0,0 +1,497 @@ +/* + * File : widget.c + * This file is part of RTGUI in RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2009-10-04 Bernard first version + */ +#include <rtgui/widgets/widget.h> +#include <rtgui/widgets/window.h> +#include <rtgui/widgets/view.h> + +static void _rtgui_widget_constructor(rtgui_widget_t *widget) +{ + if (!widget) return; + + /* set default flag */ + widget->flag = RTGUI_WIDGET_FLAG_DEFAULT; + + /* init list */ + rtgui_list_init(&(widget->sibling)); + + /* init gc */ + widget->gc.foreground = default_foreground; + widget->gc.background = default_background; + widget->gc.font = rtgui_font_default(); + widget->gc.textalign = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_TOP; + widget->align = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_TOP; + + /* set parent and toplevel root */ + widget->parent = RT_NULL; + widget->toplevel = RT_NULL; + + /* some common event handler */ + widget->on_draw = RT_NULL; + widget->on_focus_in = RT_NULL; + widget->on_focus_out = RT_NULL; + widget->on_mouseclick = RT_NULL; + widget->on_key = RT_NULL; + widget->on_size = RT_NULL; + widget->on_command = RT_NULL; + + /* set default event handler */ + widget->event_handler = rtgui_widget_event_handler; + + /* does not set widget extent and only set clip_sync to zero */ + rtgui_region_init(&(widget->clip)); + widget->clip_sync = 0; +} + +/* Destroys the widget */ +static void _rtgui_widget_destructor(rtgui_widget_t *widget) +{ + if (widget == RT_NULL) return; + + if (widget->parent != RT_NULL) + { + /* remove widget from parent's children list */ + rtgui_list_remove(&(RTGUI_CONTAINER(widget->parent)->children), &(widget->sibling)); + + widget->parent = RT_NULL; + } + + /* fini clip region */ + rtgui_region_fini(&(widget->clip)); + widget->clip_sync = 0; +} + +rtgui_type_t *rtgui_widget_type_get(void) +{ + static rtgui_type_t *widget_type = RT_NULL; + + if (!widget_type) + { + widget_type = rtgui_type_create("rtgui_widget", RTGUI_OBJECT_TYPE, + sizeof(rtgui_widget_t), RTGUI_CONSTRUCTOR(_rtgui_widget_constructor), + RTGUI_DESTRUCTOR(_rtgui_widget_destructor)); + } + + return widget_type; +} + +rtgui_widget_t *rtgui_widget_create(rtgui_type_t *widget_type) +{ + struct rtgui_widget* widget; + + widget = RTGUI_WIDGET(rtgui_object_create(widget_type)); + + return widget; +} + +void rtgui_widget_destroy(rtgui_widget_t* widget) +{ + rtgui_object_destroy(RTGUI_OBJECT(widget)); +} + +void rtgui_widget_set_rect(rtgui_widget_t* widget, rtgui_rect_t* rect) +{ + if (widget == RT_NULL || rect == RT_NULL) return; + + widget->extent = *rect;
+
+ /* reset mini width and height */
+ widget->mini_width = rtgui_rect_width(widget->extent);
+ widget->mini_height = rtgui_rect_height(widget->extent); + + /* it's not empty, fini it */ + if (rtgui_region_not_empty(&(widget->clip))) + { + rtgui_region_fini(&(widget->clip)); + } + + /* reset clip info */ + rtgui_region_init_with_extents(&(widget->clip), rect); +} + +void rtgui_widget_set_miniwidth(rtgui_widget_t* widget, int width) +{ + RT_ASSERT(widget != RT_NULL); + + widget->mini_width = width; +} + +void rtgui_widget_set_miniheight(rtgui_widget_t* widget, int height) +{ + RT_ASSERT(widget != RT_NULL); + + widget->mini_height = height; +} + +/* + * This function moves widget and its children to a logic point + */ +void rtgui_widget_move_to_logic(rtgui_widget_t* widget, int dx, int dy) +{ + struct rtgui_list_node* node; + rtgui_widget_t* child; + + if (widget == RT_NULL) return; + + rtgui_rect_moveto(&(widget->extent), dx, dy); + + /* move each child */ + if (RTGUI_IS_CONTAINER(widget)) + { + rtgui_list_foreach(node, &(RTGUI_CONTAINER(widget)->children)) + { + child = rtgui_list_entry(node, rtgui_widget_t, sibling); + + rtgui_widget_move_to_logic(child, dx, dy); + } + } +} + +void rtgui_widget_set_event_handler(rtgui_widget_t* widget, rtgui_event_handler_ptr handler) +{ + RT_ASSERT(widget != RT_NULL); + + widget->event_handler = handler; +} + +void rtgui_widget_get_rect(rtgui_widget_t* widget, rtgui_rect_t *rect) +{ + RT_ASSERT(widget != RT_NULL); + + if (rect != RT_NULL) + { + rect->x1 = rect->y1 = 0; + rect->x2 = widget->extent.x2 - widget->extent.x1; + rect->y2 = widget->extent.y2 - widget->extent.y1; + } +} + +void rtgui_widget_set_ondraw(rtgui_widget_t* widget, rtgui_event_handler_ptr handler) +{ + RT_ASSERT(widget != RT_NULL); + + widget->on_draw = handler; +} + +void rtgui_widget_set_onfocus(rtgui_widget_t* widget, rtgui_event_handler_ptr handler) +{ + RT_ASSERT(widget != RT_NULL); + + widget->on_focus_in = handler; +} + +void rtgui_widget_set_onunfocus(rtgui_widget_t* widget, rtgui_event_handler_ptr handler) +{ + RT_ASSERT(widget != RT_NULL); + + widget->on_focus_out = handler; +} + +void rtgui_widget_set_onmouseclick(rtgui_widget_t* widget, rtgui_event_handler_ptr handler) +{ + RT_ASSERT(widget != RT_NULL); + + widget->on_mouseclick = handler; +} + +void rtgui_widget_set_onkey(rtgui_widget_t* widget, rtgui_event_handler_ptr handler) +{ + RT_ASSERT(widget != RT_NULL); + + widget->on_key = handler; +} + +void rtgui_widget_set_onsize(rtgui_widget_t* widget, rtgui_event_handler_ptr handler) +{ + RT_ASSERT(widget != RT_NULL); + + widget->on_size = handler; +} + +void rtgui_widget_set_oncommand(rtgui_widget_t* widget, rtgui_event_handler_ptr handler) +{ + RT_ASSERT(widget != RT_NULL); + + widget->on_command = handler; +} + +/** + * @brief Focuses the widget. The focused widget is the one which receives the keyboard events + * @param widget a widget + * @note The widget has to be attached to a toplevel widget, otherwise it will have no effect + */ +void rtgui_widget_focus(rtgui_widget_t *widget) +{ + rtgui_widget_t *focused; + + if (!widget || !widget->toplevel || !RTGUI_WIDGET_IS_FOCUSABLE(widget) || !RTGUI_WIDGET_IS_ENABLE(widget)) + return; + + focused = rtgui_toplevel_get_focus(RTGUI_TOPLEVEL(widget->toplevel)); + + if ( focused != RT_NULL && widget == focused) + return; + + if (focused != RT_NULL) + { + rtgui_widget_unfocus(focused); + } + + rtgui_toplevel_set_focus(RTGUI_TOPLEVEL(widget->toplevel), widget); + widget->flag |= RTGUI_WIDGET_FLAG_FOCUS; + + if (widget->on_focus_in) + widget->on_focus_in(widget, RT_NULL); +} + +/** + * @brief Unfocused the widget + * @param widget a widget + */ +void rtgui_widget_unfocus(rtgui_widget_t *widget) +{ + if (!widget || !widget->toplevel || !RTGUI_WIDGET_IS_FOCUS(widget)) + return; + + if (rtgui_toplevel_get_focus(RTGUI_TOPLEVEL(widget->toplevel)) == widget) + { + rtgui_toplevel_set_focus(RTGUI_TOPLEVEL(widget->toplevel), RT_NULL); + } + + widget->flag &= ~RTGUI_WIDGET_FLAG_FOCUS; + + if (widget->on_focus_out) + widget->on_focus_out(widget, RT_NULL); +} + +void rtgui_widget_point_to_device(rtgui_widget_t* widget, rtgui_point_t* point) +{ + RT_ASSERT(widget != RT_NULL); + + if (point != RT_NULL) + { + point->x += widget->extent.x1; + point->y += widget->extent.y1; + } +} + +void rtgui_widget_rect_to_device(rtgui_widget_t* widget, rtgui_rect_t* rect) +{ + RT_ASSERT(widget != RT_NULL); + + if (rect != RT_NULL) + { + rect->x1 += widget->extent.x1; + rect->x2 += widget->extent.x1; + + rect->y1 += widget->extent.y1; + rect->y2 += widget->extent.y1; + } +} + +void rtgui_widget_point_to_logic(rtgui_widget_t* widget, rtgui_point_t* point) +{ + RT_ASSERT(widget != RT_NULL); + + if (point != RT_NULL) + { + point->x -= widget->extent.x1; + point->y -= widget->extent.y1; + } +} + +void rtgui_widget_rect_to_logic(rtgui_widget_t* widget, rtgui_rect_t* rect) +{ + RT_ASSERT(widget != RT_NULL); + + if (rect != RT_NULL) + { + rect->x1 -= widget->extent.x1; + rect->x2 -= widget->extent.x1; + + rect->y1 -= widget->extent.y1; + rect->y2 -= widget->extent.y1; + } +} + +rtgui_widget_t* rtgui_widget_get_toplevel(rtgui_widget_t* widget) +{ + rtgui_widget_t* r; + + RT_ASSERT(widget != RT_NULL); + + if (widget->toplevel) return widget->toplevel; + + r = widget; + /* get the toplevel widget */ + while (r->parent != RT_NULL) r = r->parent; + + /* set toplevel */ + widget->toplevel = r; + + return r; +} + +rt_bool_t rtgui_widget_event_handler(rtgui_widget_t* widget, rtgui_event_t* event) +{ + switch (event->type) + { + case RTGUI_EVENT_PAINT: + if (widget->on_draw != RT_NULL) return widget->on_draw(widget, event); + break; + + case RTGUI_EVENT_KBD: + if (widget->on_key != RT_NULL) return widget->on_key(widget, event); + break; + + case RTGUI_EVENT_MOUSE_BUTTON: + if (widget->on_mouseclick != RT_NULL) return widget->on_mouseclick(widget, event); + break; + + case RTGUI_EVENT_COMMAND: + if (widget->on_command != RT_NULL) return widget->on_command(widget, event); + break; + + case RTGUI_EVENT_RESIZE: + if (widget->on_size != RT_NULL) return widget->on_size(widget, event); + break; + } + + return RT_FALSE; +} + +/* + * This function updates the clip info of widget + */ +void rtgui_widget_update_clip(rtgui_widget_t* widget) +{ + struct rtgui_list_node* node; + rtgui_widget_t *parent; + + /* no widget or widget is hide, no update clip */ + if (widget == RT_NULL || RTGUI_WIDGET_IS_HIDE(widget)) return; + + parent = widget->parent; + /* if there is no parent, do not update clip (please use toplevel widget API) */ + if (parent == RT_NULL) return; + + /* increase clip sync */ + widget->clip_sync ++; + + /* reset clip to extent */ + rtgui_region_reset(&(widget->clip), &(widget->extent)); + + /* limit widget extent in parent extent */ + rtgui_region_intersect(&(widget->clip), &(widget->clip), &(parent->clip)); + + /* get the no transparent parent */ + while (parent != RT_NULL && parent->flag & RTGUI_WIDGET_FLAG_TRANSPARENT) + { + parent = parent->parent; + } + + if (parent != RT_NULL) + { + /* subtract widget clip in parent clip */ + if (!(widget->flag & RTGUI_WIDGET_FLAG_TRANSPARENT)) + { + rtgui_region_subtract_rect(&(parent->clip), &(parent->clip), + &(widget->extent)); + } + } + + /* + * note: since the layout widget introduction, the sibling widget will not + * intersect. + */ + + /* if it's a container object, update the clip info of children */ + if (RTGUI_IS_CONTAINER(widget)) + { + rtgui_widget_t* child; + rtgui_list_foreach(node, &(RTGUI_CONTAINER(widget)->children)) + { + child = rtgui_list_entry(node, rtgui_widget_t, sibling); + + rtgui_widget_update_clip(child); + } + } +} + +void rtgui_widget_show(rtgui_widget_t* widget) +{ + /* there is no parent or the parent is hide, no show at all */ + if (widget->parent == RT_NULL || + RTGUI_WIDGET_IS_HIDE(widget->parent)) return; + + /* update the clip info of widget */ + RTGUI_WIDGET_UNHIDE(widget); + rtgui_widget_update_clip(widget); +} + +void rtgui_widget_hide(rtgui_widget_t* widget) +{ + /* hide this widget */ + RTGUI_WIDGET_HIDE(widget); + + /* update the clip info of widget parent */ + rtgui_widget_update_clip(widget->parent); +} + +void rtgui_widget_update(rtgui_widget_t* widget) +{ + struct rtgui_event_paint paint; + RTGUI_EVENT_PAINT_INIT(&paint); + paint.wid = RT_NULL; + + RT_ASSERT(widget != RT_NULL); + + if (widget->event_handler != RT_NULL) + { + widget->event_handler(widget, &paint.parent); + } +} + +rtgui_widget_t* rtgui_widget_get_next_sibling(rtgui_widget_t* widget) +{ + rtgui_widget_t* sibling = RT_NULL; + + if (widget->sibling.next != RT_NULL) + { + sibling = rtgui_list_entry(widget->sibling.next, rtgui_widget_t, sibling); + } + + return sibling; +} + +rtgui_widget_t* rtgui_widget_get_prev_sibling(rtgui_widget_t* widget) +{ + struct rtgui_list_node* node; + rtgui_widget_t *sibling, *parent;
+ + node = RT_NULL; sibling = RT_NULL; + parent = widget->parent; + if (parent != RT_NULL) + { + rtgui_list_foreach(node, &(RTGUI_CONTAINER(parent)->children)) + { + if (node->next == &(widget->sibling)) + break; + } + } + + if (node != RT_NULL) + sibling = rtgui_list_entry(node, rtgui_widget_t, sibling); + + return sibling; +}
diff --git a/rtgui/widgets/window.c b/rtgui/widgets/window.c new file mode 100644 index 0000000000..d882181e17 --- /dev/null +++ b/rtgui/widgets/window.c @@ -0,0 +1,467 @@ +/*
+ * File : window.c
+ * This file is part of RTGUI in RT-Thread RTOS
+ * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rt-thread.org/license/LICENSE
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2009-10-04 Bernard first version
+ */
+#include <rtgui/dc.h>
+#include <rtgui/color.h>
+#include <rtgui/image.h>
+#include <rtgui/rtgui_system.h>
+ +#include <rtgui/widgets/window.h>
+#include <rtgui/widgets/button.h>
+
+static void _rtgui_win_constructor(rtgui_win_t *win)
+{
+ /* init window attribute */
+ win->on_activate = RT_NULL;
+ win->on_deactivate = RT_NULL;
+ win->on_close = RT_NULL;
+ win->title = RT_NULL;
+
+ /* set window hide */
+ RTGUI_WIDGET_HIDE(RTGUI_WIDGET(win));
+
+ /* set window style */
+ win->style = RTGUI_WIN_STYLE_DEFAULT;
+ rtgui_widget_set_event_handler(RTGUI_WIDGET(win), rtgui_win_event_handler);
+
+ /* init user data */
+ win->user_data = 0;
+}
+
+static void _rtgui_win_destructor(rtgui_win_t* win)
+{
+ struct rtgui_event_win_destroy edestroy;
+
+ if (RTGUI_TOPLEVEL(win)->server != RT_NULL)
+ {
+ /* destroy in server */
+ RTGUI_EVENT_WIN_DESTROY_INIT(&edestroy);
+ edestroy.wid = win;
+ if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(win)->server, RTGUI_EVENT(&edestroy),
+ sizeof(struct rtgui_event_win_destroy)) != RT_EOK)
+ {
+ return;
+ }
+ }
+
+ /* release field */
+ rtgui_free(win->title);
+}
+
+static rt_bool_t _rtgui_win_create_in_server(rtgui_win_t* win)
+{
+ if (RTGUI_TOPLEVEL(win)->server == RT_NULL)
+ {
+ rt_thread_t server;
+ struct rtgui_event_win_create ecreate;
+ RTGUI_EVENT_WIN_CREATE_INIT(&ecreate);
+
+ /* get server thread id */
+ server = rtgui_thread_get_server();
+ if (server == RT_NULL)
+ {
+ rt_kprintf("RTGUI server is not running...\n");
+ return RT_FALSE;
+ }
+
+ /* send win create event to server */
+ ecreate.wid = win;
+ ecreate.extent = RTGUI_WIDGET(win)->extent;
+ ecreate.flag = win->style;
+ ecreate.mask = 0;
+ rt_strncpy((char*)ecreate.title, (char*)win->title, RTGUI_NAME_MAX);
+
+ if (rtgui_thread_send_sync(server, RTGUI_EVENT(&ecreate),
+ sizeof(struct rtgui_event_win_create)) != RT_EOK)
+ {
+ rt_kprintf("create win: %s failed\n", win->title);
+ return RT_FALSE;
+ }
+
+ /* set server */
+ RTGUI_TOPLEVEL(win)->server = server;
+ }
+
+ return RT_TRUE;
+}
+
+rtgui_type_t *rtgui_win_type_get(void)
+{
+ static rtgui_type_t *win_type = RT_NULL;
+
+ if (!win_type)
+ {
+ win_type = rtgui_type_create("win", RTGUI_TOPLEVEL_TYPE,
+ sizeof(rtgui_win_t),
+ RTGUI_CONSTRUCTOR(_rtgui_win_constructor),
+ RTGUI_DESTRUCTOR(_rtgui_win_destructor));
+ }
+
+ return win_type;
+}
+
+rtgui_win_t* rtgui_win_create(const char* title, rtgui_rect_t *rect, rt_uint32_t style)
+{
+ struct rtgui_win* win;
+
+ /* allocate win memory */
+ win = (struct rtgui_win*) rtgui_widget_create (RTGUI_WIN_TYPE);
+ if (win != RT_NULL)
+ {
+ /* set title, rect and style */
+ if (title != RT_NULL) win->title = rt_strdup(title);
+ else win->title = RT_NULL;
+
+ rtgui_widget_set_rect(RTGUI_WIDGET(win), rect);
+ win->style = style;
+
+ if (_rtgui_win_create_in_server(win) == RT_FALSE)
+ {
+ rtgui_widget_destroy(RTGUI_WIDGET(win));
+ return RT_NULL;
+ }
+ }
+
+ return win;
+}
+
+void rtgui_win_destroy(struct rtgui_win* win)
+{
+ rtgui_widget_destroy(RTGUI_WIDGET(win));
+}
+
+void rtgui_win_show(struct rtgui_win* win)
+{
+ RT_ASSERT(win != RT_NULL);
+
+ /* if it does not register into server, create it in server */
+ if (RTGUI_TOPLEVEL(win)->server == RT_NULL)
+ {
+ if (_rtgui_win_create_in_server(win) == RT_FALSE)
+ return;
+ }
+
+ if (RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(win)))
+ {
+ /* send show message to server */
+ struct rtgui_event_win_show eshow;
+ RTGUI_EVENT_WIN_SHOW_INIT(&eshow);
+ eshow.wid = win;
+
+ if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(win)->server, RTGUI_EVENT(&eshow),
+ sizeof(struct rtgui_event_win_show)) != RT_EOK)
+ {
+ /* hide window failed */
+ return;
+ }
+
+ /* set window unhidden */
+ RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(win));
+ }
+ else rtgui_widget_update(RTGUI_WIDGET(win));
+}
+
+void rtgui_win_hiden(struct rtgui_win* win)
+{
+ RT_ASSERT(win != RT_NULL);
+
+ if (!RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(win)) &&
+ RTGUI_TOPLEVEL(win)->server != RT_NULL)
+ {
+ /* send hidden message to server */
+ struct rtgui_event_win_hide ehide;
+ RTGUI_EVENT_WIN_HIDE_INIT(&ehide);
+ ehide.wid = win;
+
+ if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(win)->server, RTGUI_EVENT(&ehide),
+ sizeof(struct rtgui_event_win_hide)) != RT_EOK)
+ {
+ rt_kprintf("hide win: %s failed\n", win->title);
+ return;
+ }
+
+ /* set window hide and deactivated */
+ RTGUI_WIDGET_HIDE(RTGUI_WIDGET(win));
+ win->style &= ~RTGUI_WIN_STYLE_ACTIVATE;
+ }
+}
+
+rt_bool_t rtgui_win_is_activated(struct rtgui_win* win)
+{
+ RT_ASSERT(win != RT_NULL);
+
+ return win->style & RTGUI_WIN_STYLE_ACTIVATE;
+}
+
+void rtgui_win_move(struct rtgui_win* win, int x, int y)
+{
+ struct rtgui_event_win_move emove;
+ RTGUI_EVENT_WIN_MOVE_INIT(&emove);
+
+ if (win == RT_NULL) return;
+
+ if (RTGUI_TOPLEVEL(win)->server != RT_NULL)
+ {
+ /* set win hide firstly */
+ RTGUI_WIDGET_HIDE(RTGUI_WIDGET(win));
+
+ emove.wid = win;
+ emove.x = x;
+ emove.y = y;
+ if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(win)->server, RTGUI_EVENT(&emove),
+ sizeof(struct rtgui_event_win_move)) != RT_EOK)
+ {
+ return;
+ }
+ }
+
+ /* move window to logic position */
+ rtgui_widget_move_to_logic(RTGUI_WIDGET(win),
+ x - RTGUI_WIDGET(win)->extent.x1,
+ y - RTGUI_WIDGET(win)->extent.y1);
+
+ /* set window visible */
+ RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(win));
+
+ /* update window */
+ // rtgui_widget_update(RTGUI_WIDGET(win));
+ return;
+}
+
+static rt_bool_t rtgui_win_ondraw(struct rtgui_win* win)
+{
+ struct rtgui_dc* dc;
+ struct rtgui_rect rect;
+ struct rtgui_event_paint event;
+
+ /* begin drawing */
+ dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(win));
+ if (dc == RT_NULL) return RT_FALSE;
+
+ /* get window rect */
+ rtgui_widget_get_rect(RTGUI_WIDGET(win), &rect);
+ /* fill area */
+ rtgui_dc_fill_rect(dc, &rect);
+
+ /* paint each widget */
+ RTGUI_EVENT_PAINT_INIT(&event);
+ event.wid = RT_NULL;
+ rtgui_container_dispatch_event(RTGUI_CONTAINER(win), (rtgui_event_t*)&event);
+
+ rtgui_dc_end_drawing(dc);
+
+ return RT_FALSE;
+}
+
+rt_bool_t rtgui_win_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
+{
+ struct rtgui_win* win = (struct rtgui_win*)widget;
+
+ RT_ASSERT((win != RT_NULL) && (event != RT_NULL));
+
+ switch (event->type)
+ {
+ case RTGUI_EVENT_WIN_SHOW:
+ rtgui_win_show(win);
+ break;
+
+ case RTGUI_EVENT_WIN_HIDE:
+ rtgui_win_hiden(win);
+ break;
+
+ case RTGUI_EVENT_WIN_CLOSE:
+ if (win->on_close != RT_NULL)
+ {
+ if (win->on_close(widget, event) == RT_FALSE) return RT_TRUE;
+ }
+
+ /* destroy window */
+ rtgui_win_destroy(win);
+
+ /* exit event loop */
+ return RT_TRUE;
+
+ case RTGUI_EVENT_WIN_MOVE:
+ {
+ struct rtgui_event_win_move* emove = (struct rtgui_event_win_move*)event;
+
+ /* move window */
+ rtgui_win_move(win, emove->x, emove->y);
+ }
+ break;
+
+ case RTGUI_EVENT_WIN_ACTIVATE:
+ if (RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(win)))
+ {
+ rt_kprintf("activate window, but window is hide!\n");
+ }
+
+ win->style |= RTGUI_WIN_STYLE_ACTIVATE;
+ if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
+ else rtgui_win_ondraw(win);
+
+ if (win->on_activate != RT_NULL)
+ {
+ win->on_activate(widget, event);
+ }
+ break;
+
+ case RTGUI_EVENT_WIN_DEACTIVATE:
+ win->style &= ~RTGUI_WIN_STYLE_ACTIVATE;
+ if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
+ else rtgui_win_ondraw(win);
+
+ if (win->on_deactivate != RT_NULL)
+ {
+ win->on_deactivate(widget, event);
+ }
+ break;
+
+ case RTGUI_EVENT_PAINT:
+ if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
+ else rtgui_win_ondraw(win);
+ break;
+
+ case RTGUI_EVENT_MOUSE_BUTTON:
+ if (rtgui_container_dispatch_mouse_event(RTGUI_CONTAINER(win), (struct rtgui_event_mouse*)event) == RT_FALSE)
+ {
+ if (widget->on_mouseclick != RT_NULL)
+ {
+ return widget->on_mouseclick(widget, event);
+ }
+ }
+ break;
+
+ case RTGUI_EVENT_MOUSE_MOTION:
+#if 0
+ if (rtgui_widget_dispatch_mouse_event(widget,
+ (struct rtgui_event_mouse*)event) == RT_FALSE)
+ {
+ /* handle event in current widget */
+ if (widget->on_mousemotion != RT_NULL)
+ {
+ return widget->on_mousemotion(widget, event);
+ }
+ }
+ else return RT_TRUE;
+#endif
+ break;
+
+ default:
+ /* call parent event handler */
+ return rtgui_toplevel_event_handler(widget, event);
+ }
+
+ return RT_FALSE;
+}
+
+/* windows event loop */
+void rtgui_win_event_loop(rtgui_win_t* wnd)
+{
+ int quit = 0;
+
+ /* the buffer uses to receive event */
+ char event_buf[256];
+
+ struct rtgui_event* event = (struct rtgui_event*)&event_buf[0];
+
+ while (!quit)
+ {
+ if (rtgui_thread_recv(event, sizeof(event_buf)) == RT_EOK)
+ {
+ if (RTGUI_WIDGET(wnd)->event_handler != RT_NULL)
+ {
+ if (RTGUI_WIDGET(wnd)->event_handler(RTGUI_WIDGET(wnd), event) == RT_TRUE)
+ quit = 1;
+ }
+ }
+ }
+}
+
+void rtgui_win_set_rect(rtgui_win_t* win, rtgui_rect_t* rect)
+{
+ struct rtgui_event_win_resize event;
+
+ if (win == RT_NULL || rect == RT_NULL) return;
+
+ RTGUI_WIDGET(win)->extent = *rect;
+
+ if (RTGUI_TOPLEVEL(win)->server != RT_NULL)
+ {
+ /* set window resize event to server */
+ RTGUI_EVENT_WIN_RESIZE_INIT(&event);
+ event.wid = win;
+ event.rect = *rect;
+
+ rtgui_thread_send(RTGUI_TOPLEVEL(win)->server, &(event.parent), sizeof(struct rtgui_event_win_resize));
+ }
+}
+
+void rtgui_win_set_box(rtgui_win_t* win, rtgui_box_t* box)
+{
+ if (win == RT_NULL || box == RT_NULL) return;
+
+ rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(box));
+ rtgui_widget_set_rect(RTGUI_WIDGET(box), &(RTGUI_WIDGET(win)->extent));
+}
+
+void rtgui_win_set_onactivate(rtgui_win_t* win, rtgui_event_handler_ptr handler)
+{
+ if (win != RT_NULL)
+ {
+ win->on_activate = handler;
+ }
+}
+
+void rtgui_win_set_ondeactivate(rtgui_win_t* win, rtgui_event_handler_ptr handler)
+{
+ if (win != RT_NULL)
+ {
+ win->on_deactivate = handler;
+ }
+}
+
+void rtgui_win_set_onclose(rtgui_win_t* win, rtgui_event_handler_ptr handler)
+{
+ if (win != RT_NULL)
+ {
+ win->on_close = handler;
+ }
+}
+
+void rtgui_win_set_title(rtgui_win_t* win, const char *title)
+{
+ /* send title to server */
+ if (RTGUI_TOPLEVEL(win)->server != RT_NULL)
+ {
+ }
+
+ /* modify in local side */
+ if (win->title != RT_NULL)
+ {
+ rtgui_free(win->title);
+ win->title = RT_NULL;
+ }
+
+ if (title != RT_NULL)
+ {
+ win->title = rt_strdup(title);
+ }
+}
+
+char* rtgui_win_get_title(rtgui_win_t* win)
+{
+ RT_ASSERT(win != RT_NULL);
+
+ return win->title;
+}
diff --git a/rtgui/widgets/workbench.c b/rtgui/widgets/workbench.c new file mode 100644 index 0000000000..795b1378eb --- /dev/null +++ b/rtgui/widgets/workbench.c @@ -0,0 +1,472 @@ +/* + * File : workbench.c + * This file is part of RTGUI in RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2009-10-04 Bernard first version + */ +#include <rtgui/rtgui_system.h> +#include <rtgui/widgets/window.h>
+#include <rtgui/widgets/workbench.h>
+
+static void _rtgui_workbench_constructor(rtgui_workbench_t *workbench)
+{
+ /* set event handler */
+ rtgui_widget_set_event_handler(RTGUI_WIDGET(workbench), rtgui_workbench_event_handler);
+
+ /* set attributes */
+ workbench->panel = RT_NULL;
+ workbench->flag = RTGUI_WORKBENCH_FLAG_DEFAULT;
+}
+
+static void _rtgui_workbench_destructor(rtgui_workbench_t *workbench)
+{
+ RT_ASSERT(workbench != RT_NULL);
+
+ if (RTGUI_TOPLEVEL(workbench)->server != RT_NULL)
+ {
+ struct rtgui_event_panel_detach edetach;
+ RTGUI_EVENT_PANEL_DETACH_INIT(&edetach);
+
+ /* detach from panel */
+ edetach.panel = workbench->panel;
+
+ /* send PANEL DETACH to server */
+ if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(workbench)->server,
+ RTGUI_EVENT(&edetach), sizeof(struct rtgui_event_panel_detach)) != RT_EOK)
+ return;
+
+ RTGUI_TOPLEVEL(workbench)->server = RT_NULL;
+ }
+
+ /* release title */
+ if (workbench->title != RT_NULL)
+ {
+ rtgui_free(workbench->title);
+ }
+}
+
+rtgui_type_t *rtgui_workbench_type_get(void)
+{
+ static rtgui_type_t *workbench_type = RT_NULL;
+
+ if (!workbench_type)
+ {
+ workbench_type = rtgui_type_create("workbench", RTGUI_TOPLEVEL_TYPE,
+ sizeof(rtgui_workbench_t),
+ RTGUI_CONSTRUCTOR(_rtgui_workbench_constructor),
+ RTGUI_DESTRUCTOR(_rtgui_workbench_destructor));
+ }
+
+ return workbench_type;
+}
+
+rtgui_workbench_t *rtgui_workbench_create(const char* panel_name, const unsigned char* title)
+{
+ struct rtgui_workbench* workbench;
+
+ /* the server thread id */
+ rt_thread_t server = rtgui_thread_get_server();
+ if (server == RT_NULL)
+ {
+ rt_kprintf("can't find rtgui server\n");
+ return RT_NULL;
+ }
+
+ /* create workbench */
+ workbench = (rtgui_workbench_t*) rtgui_widget_create (RTGUI_WORKBENCH_TYPE);
+ if (workbench != RT_NULL)
+ {
+ /* the buffer uses to receive event */
+ union
+ {
+ struct rtgui_event_panel_attach ecreate;
+ struct rtgui_event_panel_info epanel;
+
+ char buffer[256]; /* use to recv other information */
+ } event;
+
+ /* set workbench title */
+ workbench->title = (unsigned char*)rt_strdup((char*)title);
+
+ /* create application in server */
+ RTGUI_EVENT_PANEL_ATTACH_INIT(&(event.ecreate));
+
+ /* set the panel name and workbench */
+ rt_strncpy(event.ecreate.panel_name, panel_name, RTGUI_NAME_MAX);
+ event.ecreate.workbench = workbench;
+
+ /* send PANEL ATTACH to server */
+ if (rtgui_thread_send_sync(server,
+ &(event.ecreate.parent), sizeof(struct rtgui_event_panel_attach)) != RTGUI_STATUS_OK)
+ {
+ return RT_NULL;
+ }
+
+ /* get PANEL INFO */
+ rtgui_thread_recv_filter(RTGUI_EVENT_PANEL_INFO, &(event.epanel.parent), sizeof(event));
+
+ /* set panel */
+ workbench->panel = (struct rtgui_panel*)event.epanel.panel;
+
+ /* connected */
+ RTGUI_TOPLEVEL(workbench)->server = server;
+
+ /* set extent of workbench */
+ rtgui_widget_set_rect(RTGUI_WIDGET(workbench), &(event.epanel.extent));
+
+ /* set workbench in thread */
+ rtgui_thread_set_widget(RTGUI_WIDGET(workbench));
+ }
+
+ return workbench;
+}
+
+void rtgui_workbench_destroy(rtgui_workbench_t* workbench)
+{
+ RT_ASSERT(workbench != RT_NULL);
+
+ if (RTGUI_TOPLEVEL(workbench)->server != RT_NULL)
+ {
+ struct rtgui_event_panel_detach edetach;
+ RTGUI_EVENT_PANEL_DETACH_INIT(&edetach);
+
+ /* detach from panel */
+ edetach.panel = workbench->panel;
+
+ /* send PANEL DETACH to server */
+ if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(workbench)->server,
+ RTGUI_EVENT(&edetach), sizeof(struct rtgui_event_panel_detach)) != RT_EOK)
+ return;
+
+ RTGUI_TOPLEVEL(workbench)->server = RT_NULL;
+ }
+
+ rtgui_widget_destroy(RTGUI_WIDGET(workbench));
+}
+
+void rtgui_workbench_set_flag(rtgui_workbench_t* workbench, rt_uint8_t flag)
+{
+ RT_ASSERT(workbench != RT_NULL);
+
+ workbench->flag = flag;
+}
+
+rtgui_view_t *rtgui_workbench_get_current_view(rtgui_workbench_t * workbench)
+{
+ struct rtgui_list_node* node;
+ struct rtgui_widget* view;
+
+ RT_ASSERT(workbench != RT_NULL);
+
+ /* find the first shown view */
+ rtgui_list_foreach(node, &(RTGUI_CONTAINER(workbench)->children))
+ {
+ view = rtgui_list_entry(node, struct rtgui_widget, sibling);
+
+ /* is it a shown view? */
+ if (!RTGUI_WIDGET_IS_HIDE(view))
+ {
+ return (rtgui_view_t*)view;
+ }
+ }
+
+ return RT_NULL;
+}
+
+void rtgui_workbench_event_loop(rtgui_workbench_t* workbench)
+{
+ int quit = 0;
+
+ /* the buffer uses to receive event */
+ char event_buf[256];
+ struct rtgui_event* event = (struct rtgui_event*)&event_buf[0];
+
+ /* show workbench firstly */
+ rtgui_workbench_show(workbench);
+
+ while (!quit)
+ {
+ if (rtgui_thread_recv(event, sizeof(event_buf)) == RT_EOK)
+ {
+ RTGUI_WIDGET(workbench)->event_handler(RTGUI_WIDGET(workbench), event);
+ }
+ }
+}
+
+rt_err_t rtgui_workbench_show(rtgui_workbench_t* workbench)
+{
+ RT_ASSERT(workbench != RT_NULL);
+
+ if (RTGUI_TOPLEVEL(workbench)->server != RT_NULL)
+ {
+ struct rtgui_event_panel_show eraise;
+ RTGUI_EVENT_PANEL_SHOW_INIT(&eraise);
+ eraise.workbench = workbench;
+
+ eraise.panel = workbench->panel;
+ if (rtgui_thread_send_sync(workbench->parent.server, RTGUI_EVENT(&eraise),
+ sizeof(struct rtgui_event_panel_show)) != RT_EOK)
+ return -RT_ERROR;
+
+ RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(workbench));
+ rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(workbench));
+ }
+ else return -RT_ERROR;
+
+ return RT_EOK;
+}
+
+rt_err_t rtgui_workbench_hide(rtgui_workbench_t* workbench)
+{
+ if (RTGUI_TOPLEVEL(workbench)->server != RT_NULL)
+ {
+ struct rtgui_event_panel_hide ehide;
+ RTGUI_EVENT_PANEL_HIDE_INIT(&ehide);
+
+ RT_ASSERT(workbench != RT_NULL);
+ if (workbench->parent.server == RT_NULL) return -RT_ERROR;
+
+ ehide.panel = workbench->panel;
+ if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(workbench)->server, RTGUI_EVENT(&ehide),
+ sizeof(struct rtgui_event_panel_hide)) != RT_EOK)
+ return -RT_ERROR;
+
+ RTGUI_WIDGET_HIDE(RTGUI_WIDGET(workbench));
+ rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(workbench));
+ }
+
+ return RT_EOK;
+}
+
+rt_bool_t rtgui_workbench_event_handler(rtgui_widget_t* widget, rtgui_event_t* event)
+{
+ struct rtgui_workbench* workbench = (struct rtgui_workbench*)widget;
+
+ switch (event->type)
+ {
+ case RTGUI_EVENT_PANEL_DETACH:
+ RTGUI_WIDGET_HIDE(RTGUI_WIDGET(workbench));
+ RTGUI_TOPLEVEL(workbench)->server = RT_NULL;
+ return RT_TRUE;
+
+ case RTGUI_EVENT_PANEL_SHOW:
+ /* show workbench in server */
+ rtgui_workbench_show(workbench);
+ break;
+
+ case RTGUI_EVENT_PANEL_HIDE:
+ /* hide widget */ + RTGUI_WIDGET_HIDE(widget);
+ break;
+
+ case RTGUI_EVENT_MOUSE_MOTION:
+ {
+ struct rtgui_event_mouse* emouse = (struct rtgui_event_mouse*)event;
+ struct rtgui_toplevel* top = RTGUI_TOPLEVEL(emouse->wid);
+
+ /* check the destination window */
+ if (top != RT_NULL && RTGUI_WIDGET(top)->event_handler != RT_NULL)
+ {
+ RTGUI_WIDGET(top)->event_handler(RTGUI_WIDGET(top), event);
+ }
+ else
+ {
+ /* let viewer to handle it */
+ rtgui_view_t* view = rtgui_workbench_get_current_view(workbench);
+ if (view != RT_NULL &&
+ RTGUI_WIDGET(view)->event_handler != RT_NULL)
+ {
+ RTGUI_WIDGET(view)->event_handler(RTGUI_WIDGET(view), event);
+ }
+ }
+ }
+ break;
+
+ case RTGUI_EVENT_MOUSE_BUTTON:
+ {
+ struct rtgui_event_mouse* emouse = (struct rtgui_event_mouse*)event;
+ struct rtgui_toplevel* top = RTGUI_TOPLEVEL(emouse->wid);
+
+ /* check the destination window */
+ if (top != RT_NULL && RTGUI_WIDGET(top)->event_handler != RT_NULL)
+ {
+ RTGUI_WIDGET(top)->event_handler(RTGUI_WIDGET(top), event);
+ }
+ else
+ {
+ /* let viewer to handle it */
+ rtgui_view_t* view = rtgui_workbench_get_current_view(workbench);
+ if (view != RT_NULL &&
+ RTGUI_WIDGET(view)->event_handler != RT_NULL)
+ {
+ RTGUI_WIDGET(view)->event_handler(RTGUI_WIDGET(view), event);
+ }
+ }
+ }
+ break;
+
+ case RTGUI_EVENT_KBD:
+ {
+ struct rtgui_event_kbd* kbd = (struct rtgui_event_kbd*)event;
+ struct rtgui_toplevel* top = RTGUI_TOPLEVEL(kbd->wid);
+
+ /* check the destination window */
+ if (top != RT_NULL && RTGUI_WIDGET(top)->event_handler != RT_NULL)
+ {
+ RTGUI_WIDGET(top)->event_handler(RTGUI_WIDGET(top), event);
+ }
+ else
+ {
+ return rtgui_toplevel_event_handler(widget, event);
+ }
+ }
+ break;
+
+ case RTGUI_EVENT_PAINT:
+ {
+ struct rtgui_event_paint* epaint = (struct rtgui_event_paint*)event;
+ struct rtgui_toplevel* top = RTGUI_TOPLEVEL(epaint->wid);
+
+ /* check the destination window */
+ if (top != RT_NULL && RTGUI_WIDGET(top)->event_handler != RT_NULL)
+ {
+ RTGUI_WIDGET(top)->event_handler(RTGUI_WIDGET(top), event);
+ }
+ else
+ {
+ rtgui_view_t* view;
+
+ /* paint a view */
+ view = rtgui_workbench_get_current_view(workbench);
+ if (view != RT_NULL)
+ {
+ /* remake a paint event */
+ RTGUI_EVENT_PAINT_INIT(epaint);
+ epaint->wid = RT_NULL;
+
+ /* send this event to the view */
+ if (RTGUI_WIDGET(view)->event_handler != RT_NULL)
+ {
+ RTGUI_WIDGET(view)->event_handler(RTGUI_WIDGET(view), event);
+ }
+ }
+ }
+ }
+ break;
+
+ case RTGUI_EVENT_CLIP_INFO:
+ {
+ struct rtgui_event_clip_info* eclip = (struct rtgui_event_clip_info*)event;
+ struct rtgui_widget* dest_widget = RTGUI_WIDGET(eclip->wid);
+
+ if (dest_widget != RT_NULL && dest_widget->event_handler != RT_NULL)
+ {
+ dest_widget->event_handler(dest_widget, event);
+ }
+ else
+ {
+ return rtgui_toplevel_event_handler(widget, event);
+ }
+ }
+ break;
+
+ case RTGUI_EVENT_WIN_CLOSE:
+ case RTGUI_EVENT_WIN_ACTIVATE:
+ case RTGUI_EVENT_WIN_DEACTIVATE:
+ {
+ struct rtgui_event_win* wevent = (struct rtgui_event_win*)event;
+ struct rtgui_widget* dest_widget = RTGUI_WIDGET(wevent->wid);
+ if (dest_widget != RT_NULL && dest_widget->event_handler != RT_NULL)
+ {
+ dest_widget->event_handler(dest_widget, event);
+ }
+ }
+ break;
+
+ case RTGUI_EVENT_WIN_MOVE:
+ {
+ struct rtgui_event_win_move* wevent = (struct rtgui_event_win_move*)event;
+ struct rtgui_toplevel* top = RTGUI_TOPLEVEL(wevent->wid);
+ if (top != RT_NULL && RTGUI_WIDGET(top)->event_handler != RT_NULL)
+ {
+ RTGUI_WIDGET(top)->event_handler(RTGUI_WIDGET(top), event);
+ }
+ }
+ break;
+
+ default:
+ return rtgui_toplevel_event_handler(widget, event);
+ }
+
+ return RT_TRUE;
+}
+
+void rtgui_workbench_add_view(rtgui_workbench_t* workbench, rtgui_view_t* view)
+{
+ rtgui_container_add_child(RTGUI_CONTAINER(workbench), RTGUI_WIDGET(view));
+
+ /* reset view extent */
+ rtgui_widget_set_rect(RTGUI_WIDGET(view), &(RTGUI_WIDGET(workbench)->extent));
+}
+
+void rtgui_workbench_remove_view(rtgui_workbench_t* workbench, rtgui_view_t* view)
+{
+ rtgui_container_remove_child(RTGUI_CONTAINER(workbench), RTGUI_WIDGET(view));
+}
+
+void rtgui_workbench_show_view(rtgui_workbench_t* workbench, rtgui_view_t* view)
+{
+ RT_ASSERT(workbench != RT_NULL);
+ RT_ASSERT(view != RT_NULL);
+
+ if (rtgui_workbench_get_current_view(workbench) == view &&
+ !RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(view))) return;
+
+ /* remove from child list */
+ rtgui_list_remove(&(RTGUI_CONTAINER(workbench)->children), &(RTGUI_WIDGET(view)->sibling));
+
+ /* insert to the head of child list */
+ rtgui_list_insert(&(RTGUI_CONTAINER(workbench)->children), &(RTGUI_WIDGET(view)->sibling));
+
+ /* show view */
+ RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(view));
+
+ /* update workbench clip */
+ rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(workbench));
+
+ if (!RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(workbench)))
+ {
+ rtgui_widget_update(RTGUI_WIDGET(view));
+ }
+}
+
+void rtgui_workbench_hide_view(rtgui_workbench_t* workbench, rtgui_view_t* view)
+{
+ RT_ASSERT(workbench != RT_NULL);
+ RT_ASSERT(view != RT_NULL);
+
+ /* remove from child list */
+ rtgui_list_remove(&(RTGUI_CONTAINER(workbench)->children), &(RTGUI_WIDGET(view)->sibling));
+
+ /* append to the end of child list */
+ rtgui_list_append(&(RTGUI_CONTAINER(workbench)->children), &(RTGUI_WIDGET(view)->sibling));
+
+ /* hide view */
+ RTGUI_WIDGET_HIDE(RTGUI_WIDGET(view));
+
+ /* update workbench clip */
+ rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(workbench));
+
+ if (!RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(workbench)))
+ {
+ view = rtgui_workbench_get_current_view(workbench);
+ rtgui_widget_update(RTGUI_WIDGET(view));
+ }
+}
|
