2009-11-24 07:34:58 +08:00
|
|
|
/*
|
|
|
|
* 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>
|
2012-04-18 23:06:12 +08:00
|
|
|
#include <rtgui/rtgui_server.h>
|
2012-07-20 20:37:56 +08:00
|
|
|
#include <rtgui/rtgui_app.h>
|
2009-11-24 07:34:58 +08:00
|
|
|
|
|
|
|
#include <rtgui/widgets/window.h>
|
|
|
|
#include <rtgui/widgets/button.h>
|
|
|
|
|
|
|
|
static void _rtgui_win_constructor(rtgui_win_t *win)
|
|
|
|
{
|
2012-04-18 23:06:12 +08:00
|
|
|
RTGUI_WIDGET(win)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
|
|
|
|
win->parent_window = RT_NULL;
|
2009-11-24 07:34:58 +08:00
|
|
|
/* init window attribute */
|
2012-04-18 23:06:12 +08:00
|
|
|
win->on_activate = RT_NULL;
|
|
|
|
win->on_deactivate = RT_NULL;
|
|
|
|
win->on_close = RT_NULL;
|
|
|
|
win->on_key = RT_NULL;
|
|
|
|
win->title = RT_NULL;
|
|
|
|
win->modal_code = RTGUI_MODAL_OK;
|
|
|
|
|
|
|
|
/* initialize last mouse event handled widget */
|
|
|
|
win->last_mevent_widget = RT_NULL;
|
|
|
|
win->focused_widget = RT_NULL;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
|
|
|
/* set window hide */
|
|
|
|
RTGUI_WIDGET_HIDE(RTGUI_WIDGET(win));
|
|
|
|
|
|
|
|
/* set window style */
|
|
|
|
win->style = RTGUI_WIN_STYLE_DEFAULT;
|
2012-04-18 23:06:12 +08:00
|
|
|
|
|
|
|
win->flag = RTGUI_WIN_FLAG_INIT;
|
|
|
|
|
|
|
|
rtgui_object_set_event_handler(RTGUI_OBJECT(win), rtgui_win_event_handler);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
|
|
|
/* init user data */
|
|
|
|
win->user_data = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _rtgui_win_destructor(rtgui_win_t* win)
|
|
|
|
{
|
|
|
|
struct rtgui_event_win_destroy edestroy;
|
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
if (win->flag & RTGUI_WIN_FLAG_CONNECTED)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
|
|
|
/* destroy in server */
|
|
|
|
RTGUI_EVENT_WIN_DESTROY_INIT(&edestroy);
|
|
|
|
edestroy.wid = win;
|
2012-04-18 23:06:12 +08:00
|
|
|
if (rtgui_server_post_event_sync(RTGUI_EVENT(&edestroy),
|
2009-11-24 07:34:58 +08:00
|
|
|
sizeof(struct rtgui_event_win_destroy)) != RT_EOK)
|
|
|
|
{
|
|
|
|
/* destroy in server failed */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* release field */
|
|
|
|
rt_free(win->title);
|
|
|
|
}
|
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
static rt_bool_t _rtgui_win_create_in_server(struct rtgui_win *win)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-04-18 23:06:12 +08:00
|
|
|
if (!(win->flag & RTGUI_WIN_FLAG_CONNECTED))
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
|
|
|
struct rtgui_event_win_create ecreate;
|
|
|
|
RTGUI_EVENT_WIN_CREATE_INIT(&ecreate);
|
|
|
|
|
|
|
|
/* send win create event to server */
|
2012-04-18 23:06:12 +08:00
|
|
|
ecreate.parent_window = win->parent_window;
|
|
|
|
ecreate.wid = win;
|
|
|
|
ecreate.parent.user = win->style;
|
2010-02-08 07:42:49 +08:00
|
|
|
#ifndef RTGUI_USING_SMALL_SIZE
|
2012-04-18 23:06:12 +08:00
|
|
|
ecreate.extent = RTGUI_WIDGET(win)->extent;
|
2009-11-24 07:34:58 +08:00
|
|
|
rt_strncpy((char*)ecreate.title, (char*)win->title, RTGUI_NAME_MAX);
|
2010-02-08 07:42:49 +08:00
|
|
|
#endif
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
if (rtgui_server_post_event_sync(RTGUI_EVENT(&ecreate),
|
|
|
|
sizeof(struct rtgui_event_win_create)
|
|
|
|
) != RT_EOK)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
|
|
|
rt_kprintf("create win: %s failed\n", win->title);
|
|
|
|
return RT_FALSE;
|
|
|
|
}
|
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
win->flag |= RTGUI_WIN_FLAG_CONNECTED;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return RT_TRUE;
|
|
|
|
}
|
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
DEFINE_CLASS_TYPE(win, "win",
|
2011-05-13 18:53:51 +08:00
|
|
|
RTGUI_TOPLEVEL_TYPE,
|
|
|
|
_rtgui_win_constructor,
|
|
|
|
_rtgui_win_destructor,
|
|
|
|
sizeof(struct rtgui_win));
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
rtgui_win_t* rtgui_win_create(struct rtgui_win* parent_window,
|
|
|
|
const char* title,
|
|
|
|
rtgui_rect_t *rect,
|
|
|
|
rt_uint16_t style)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
|
|
|
struct rtgui_win* win;
|
|
|
|
|
|
|
|
/* allocate win memory */
|
2012-04-18 23:06:12 +08:00
|
|
|
win = RTGUI_WIN(rtgui_widget_create(RTGUI_WIN_TYPE));
|
|
|
|
if (win == RT_NULL)
|
|
|
|
return RT_NULL;
|
|
|
|
|
|
|
|
/* set parent toplevel */
|
|
|
|
win->parent_window = parent_window;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
/* set title, rect and style */
|
|
|
|
if (title != RT_NULL)
|
|
|
|
win->title = rt_strdup(title);
|
|
|
|
else
|
|
|
|
win->title = RT_NULL;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
rtgui_widget_set_rect(RTGUI_WIDGET(win), rect);
|
|
|
|
win->style = style;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
if (_rtgui_win_create_in_server(win) == RT_FALSE)
|
|
|
|
{
|
|
|
|
goto __on_err;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
return win;
|
2012-04-18 23:06:12 +08:00
|
|
|
|
|
|
|
__on_err:
|
|
|
|
rtgui_widget_destroy(RTGUI_WIDGET(win));
|
|
|
|
return RT_NULL;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
static rt_bool_t _rtgui_win_deal_close(struct rtgui_win *win,
|
2012-07-20 20:37:56 +08:00
|
|
|
struct rtgui_event *event,
|
|
|
|
rt_bool_t force_close)
|
2012-04-18 23:06:12 +08:00
|
|
|
{
|
|
|
|
if (win->on_close != RT_NULL)
|
|
|
|
{
|
2012-07-20 20:37:56 +08:00
|
|
|
if ((win->on_close(RTGUI_OBJECT(win), event) == RT_FALSE) && !force_close)
|
2012-04-18 23:06:12 +08:00
|
|
|
return RT_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
rtgui_win_hiden(win);
|
|
|
|
|
|
|
|
win->flag |= RTGUI_WIN_FLAG_CLOSED;
|
|
|
|
|
|
|
|
if (win->flag & RTGUI_WIN_FLAG_MODAL)
|
|
|
|
{
|
|
|
|
rtgui_win_end_modal(win, RTGUI_MODAL_CANCEL);
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
2012-04-18 23:06:12 +08:00
|
|
|
else if (win->style & RTGUI_WIN_STYLE_DESTROY_ON_CLOSE)
|
|
|
|
{
|
|
|
|
rtgui_win_destroy(win);
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_TRUE;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
|
2012-07-20 20:37:56 +08:00
|
|
|
void rtgui_win_destroy(struct rtgui_win* win)
|
|
|
|
{
|
|
|
|
/* close the window first if it's not. */
|
|
|
|
if (!(win->flag & RTGUI_WIN_FLAG_CLOSED))
|
|
|
|
{
|
|
|
|
struct rtgui_event_win_close eclose;
|
|
|
|
|
|
|
|
RTGUI_EVENT_WIN_CLOSE_INIT(&eclose);
|
|
|
|
eclose.wid = win;
|
|
|
|
|
|
|
|
if (win->style & RTGUI_WIN_STYLE_DESTROY_ON_CLOSE)
|
|
|
|
{
|
|
|
|
_rtgui_win_deal_close(win,
|
|
|
|
(struct rtgui_event*)&eclose,
|
|
|
|
RT_TRUE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
_rtgui_win_deal_close(win,
|
|
|
|
(struct rtgui_event*)&eclose,
|
|
|
|
RT_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (win->flag & RTGUI_WIN_FLAG_MODAL)
|
|
|
|
{
|
|
|
|
/* set the RTGUI_WIN_STYLE_DESTROY_ON_CLOSE flag so the window will be
|
|
|
|
* destroyed after the event_loop */
|
|
|
|
win->style |= RTGUI_WIN_STYLE_DESTROY_ON_CLOSE;
|
|
|
|
rtgui_win_end_modal(win, RTGUI_MODAL_CANCEL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rtgui_widget_destroy(RTGUI_WIDGET(win));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
/* send a close event to myself to get a consistent behavior */
|
|
|
|
rt_bool_t rtgui_win_close(struct rtgui_win* win)
|
2010-05-02 20:26:19 +08:00
|
|
|
{
|
2012-04-18 23:06:12 +08:00
|
|
|
struct rtgui_event_win_close eclose;
|
|
|
|
|
|
|
|
RTGUI_EVENT_WIN_CLOSE_INIT(&eclose);
|
|
|
|
eclose.wid = win;
|
|
|
|
return _rtgui_win_deal_close(win,
|
2012-07-20 20:37:56 +08:00
|
|
|
(struct rtgui_event*)&eclose,
|
|
|
|
RT_FALSE);
|
2010-05-02 20:26:19 +08:00
|
|
|
}
|
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
rt_base_t rtgui_win_show(struct rtgui_win* win, rt_bool_t is_modal)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-04-18 23:06:12 +08:00
|
|
|
rt_base_t exit_code = -1;
|
2012-07-20 20:37:56 +08:00
|
|
|
struct rtgui_app *app;
|
|
|
|
struct rtgui_event_win_show eshow;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-07-20 20:37:56 +08:00
|
|
|
app = rtgui_app_self();
|
2012-04-18 23:06:12 +08:00
|
|
|
RTGUI_EVENT_WIN_SHOW_INIT(&eshow);
|
|
|
|
eshow.wid = win;
|
|
|
|
|
|
|
|
if (win == RT_NULL)
|
|
|
|
return exit_code;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
|
|
|
/* if it does not register into server, create it in server */
|
2012-04-18 23:06:12 +08:00
|
|
|
if (!(win->flag & RTGUI_WIN_FLAG_CONNECTED))
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
|
|
|
if (_rtgui_win_create_in_server(win) == RT_FALSE)
|
2012-04-18 23:06:12 +08:00
|
|
|
return exit_code;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
|
2012-07-20 20:37:56 +08:00
|
|
|
/* set window unhidden before notify the server */
|
|
|
|
rtgui_widget_show(RTGUI_WIDGET(win));
|
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
if (rtgui_server_post_event_sync(RTGUI_EVENT(&eshow),
|
2012-07-20 20:37:56 +08:00
|
|
|
sizeof(struct rtgui_event_win_show)) != RT_EOK)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-07-20 20:37:56 +08:00
|
|
|
/* It could not be shown if a parent window is hidden. */
|
|
|
|
rtgui_widget_hide(RTGUI_WIDGET(win));
|
2012-04-18 23:06:12 +08:00
|
|
|
return exit_code;
|
|
|
|
}
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
if (win->focused_widget == RT_NULL)
|
|
|
|
rtgui_widget_focus(RTGUI_WIDGET(win));
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-07-20 20:37:56 +08:00
|
|
|
/* set main window */
|
|
|
|
if (app->main_object == RT_NULL)
|
|
|
|
rtgui_app_set_main_win(win);
|
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
if (is_modal == RT_TRUE)
|
|
|
|
{
|
2012-07-20 20:37:56 +08:00
|
|
|
struct rtgui_app *app;
|
2012-04-18 23:06:12 +08:00
|
|
|
struct rtgui_event_win_modal_enter emodal;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
RTGUI_EVENT_WIN_MODAL_ENTER_INIT(&emodal);
|
|
|
|
emodal.wid = win;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-07-20 20:37:56 +08:00
|
|
|
app = rtgui_app_self();
|
2012-04-18 23:06:12 +08:00
|
|
|
RT_ASSERT(app != RT_NULL);
|
|
|
|
|
|
|
|
win->flag |= RTGUI_WIN_FLAG_MODAL;
|
|
|
|
|
|
|
|
if (rtgui_server_post_event_sync((struct rtgui_event*)&emodal,
|
|
|
|
sizeof(emodal)) != RT_EOK)
|
|
|
|
return exit_code;
|
|
|
|
|
|
|
|
app->modal_object = RTGUI_OBJECT(win);
|
|
|
|
|
2012-07-20 20:37:56 +08:00
|
|
|
exit_code = rtgui_app_run(app);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
app->modal_object = RT_NULL;
|
|
|
|
win->flag &= ~RTGUI_WIN_FLAG_MODAL;
|
|
|
|
|
|
|
|
if (win->style & RTGUI_WIN_STYLE_DESTROY_ON_CLOSE)
|
|
|
|
{
|
|
|
|
rtgui_win_destroy(win);
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
2012-04-18 23:06:12 +08:00
|
|
|
}
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
return exit_code;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void rtgui_win_end_modal(struct rtgui_win* win, rtgui_modal_code_t modal_code)
|
|
|
|
{
|
2012-04-18 23:06:12 +08:00
|
|
|
if (win == RT_NULL || !(win->flag & RTGUI_WIN_FLAG_MODAL))
|
|
|
|
return;
|
2010-02-13 00:02:00 +08:00
|
|
|
|
2012-07-20 20:37:56 +08:00
|
|
|
rtgui_app_exit(rtgui_app_self(), modal_code);
|
2010-02-13 00:02:00 +08:00
|
|
|
|
|
|
|
/* remove modal mode */
|
2012-04-18 23:06:12 +08:00
|
|
|
win->flag &= ~RTGUI_WIN_FLAG_MODAL;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void rtgui_win_hiden(struct rtgui_win* win)
|
|
|
|
{
|
|
|
|
RT_ASSERT(win != RT_NULL);
|
|
|
|
|
|
|
|
if (!RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(win)) &&
|
2012-04-18 23:06:12 +08:00
|
|
|
win->flag & RTGUI_WIN_FLAG_CONNECTED)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
|
|
|
/* send hidden message to server */
|
|
|
|
struct rtgui_event_win_hide ehide;
|
|
|
|
RTGUI_EVENT_WIN_HIDE_INIT(&ehide);
|
|
|
|
ehide.wid = win;
|
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
if (rtgui_server_post_event_sync(RTGUI_EVENT(&ehide),
|
2009-11-24 07:34:58 +08:00
|
|
|
sizeof(struct rtgui_event_win_hide)) != RT_EOK)
|
|
|
|
{
|
|
|
|
rt_kprintf("hide win: %s failed\n", win->title);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-20 20:37:56 +08:00
|
|
|
rtgui_widget_hide(RTGUI_WIDGET(win));
|
2012-04-18 23:06:12 +08:00
|
|
|
win->flag &= ~RTGUI_WIN_FLAG_ACTIVATE;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-20 20:37:56 +08:00
|
|
|
rt_err_t rtgui_win_activate(struct rtgui_win *win)
|
|
|
|
{
|
|
|
|
struct rtgui_event_win_activate eact;
|
|
|
|
RTGUI_EVENT_WIN_ACTIVATE_INIT(&eact);
|
|
|
|
eact.wid = win;
|
|
|
|
|
|
|
|
return rtgui_server_post_event_sync(RTGUI_EVENT(&eact),
|
|
|
|
sizeof(eact));
|
|
|
|
}
|
|
|
|
|
2009-11-24 07:34:58 +08:00
|
|
|
rt_bool_t rtgui_win_is_activated(struct rtgui_win* win)
|
|
|
|
{
|
|
|
|
RT_ASSERT(win != RT_NULL);
|
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
if (win->flag & RTGUI_WIN_FLAG_ACTIVATE) return RT_TRUE;
|
2010-12-22 07:50:25 +08:00
|
|
|
|
|
|
|
return RT_FALSE;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void rtgui_win_move(struct rtgui_win* win, int x, int y)
|
|
|
|
{
|
|
|
|
struct rtgui_event_win_move emove;
|
|
|
|
RTGUI_EVENT_WIN_MOVE_INIT(&emove);
|
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
if (win == RT_NULL)
|
|
|
|
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);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
if (win->flag & RTGUI_WIN_FLAG_CONNECTED)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
|
|
|
/* set win hide firstly */
|
2012-07-20 20:37:56 +08:00
|
|
|
rtgui_widget_hide(RTGUI_WIDGET(win));
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
emove.wid = win;
|
2009-11-24 07:34:58 +08:00
|
|
|
emove.x = x;
|
|
|
|
emove.y = y;
|
2012-04-18 23:06:12 +08:00
|
|
|
if (rtgui_server_post_event_sync(RTGUI_EVENT(&emove),
|
2009-11-24 07:34:58 +08:00
|
|
|
sizeof(struct rtgui_event_win_move)) != RT_EOK)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set window visible */
|
2012-07-20 20:37:56 +08:00
|
|
|
rtgui_widget_show(RTGUI_WIDGET(win));
|
2009-11-24 07:34:58 +08:00
|
|
|
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));
|
2012-04-18 23:06:12 +08:00
|
|
|
if (dc == RT_NULL)
|
|
|
|
return RT_FALSE;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
|
|
|
/* 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;
|
2012-04-18 23:06:12 +08:00
|
|
|
rtgui_container_dispatch_event(RTGUI_CONTAINER(win),
|
|
|
|
(rtgui_event_t*)&event);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
|
|
|
rtgui_dc_end_drawing(dc);
|
|
|
|
|
|
|
|
return RT_FALSE;
|
|
|
|
}
|
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
rt_bool_t rtgui_win_event_handler(struct rtgui_object* object, struct rtgui_event* event)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-04-18 23:06:12 +08:00
|
|
|
struct rtgui_win* win;
|
|
|
|
|
|
|
|
RT_ASSERT(object != RT_NULL);
|
|
|
|
RT_ASSERT(event != RT_NULL);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
win = RTGUI_WIN(object);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
|
|
|
switch (event->type)
|
|
|
|
{
|
|
|
|
case RTGUI_EVENT_WIN_SHOW:
|
|
|
|
rtgui_win_show(win, RT_FALSE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RTGUI_EVENT_WIN_HIDE:
|
|
|
|
rtgui_win_hiden(win);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RTGUI_EVENT_WIN_CLOSE:
|
2012-07-20 20:37:56 +08:00
|
|
|
_rtgui_win_deal_close(win, event, RT_FALSE);
|
2012-04-18 23:06:12 +08:00
|
|
|
/* don't broadcast WIN_CLOSE event to others */
|
2009-11-24 07:34:58 +08:00
|
|
|
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)))
|
|
|
|
{
|
2010-12-22 07:50:25 +08:00
|
|
|
/* activate a hide window */
|
|
|
|
return RT_TRUE;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
win->flag |= RTGUI_WIN_FLAG_ACTIVATE;
|
2010-01-04 08:05:01 +08:00
|
|
|
#ifndef RTGUI_USING_SMALL_SIZE
|
2012-04-18 23:06:12 +08:00
|
|
|
if (RTGUI_WIDGET(object)->on_draw != RT_NULL)
|
|
|
|
RTGUI_WIDGET(object)->on_draw(object, event);
|
|
|
|
else
|
2010-01-04 08:05:01 +08:00
|
|
|
#endif
|
2010-05-02 20:26:19 +08:00
|
|
|
rtgui_widget_update(RTGUI_WIDGET(win));
|
2009-11-24 07:34:58 +08:00
|
|
|
|
|
|
|
if (win->on_activate != RT_NULL)
|
|
|
|
{
|
2012-04-18 23:06:12 +08:00
|
|
|
win->on_activate(RTGUI_OBJECT(object), event);
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RTGUI_EVENT_WIN_DEACTIVATE:
|
2012-04-18 23:06:12 +08:00
|
|
|
if (win->flag & RTGUI_WIN_FLAG_MODAL)
|
2010-02-13 00:02:00 +08:00
|
|
|
{
|
2012-04-18 23:06:12 +08:00
|
|
|
/* FIXME: make modal concept clear and easy. See the comment of
|
|
|
|
* rtgui_topwin_modal_enter. */
|
|
|
|
/* There are various reason that a modal window got deactivated:
|
|
|
|
* 1, it has child windows and the user activate one of them.
|
|
|
|
* 2, the application has more than one root window and the
|
|
|
|
* user switched to one of the others.
|
|
|
|
*
|
|
|
|
* In any of the cases, we have nothing to do here.
|
|
|
|
*/
|
2010-02-13 00:02:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-18 23:06:12 +08:00
|
|
|
win->flag &= ~RTGUI_WIN_FLAG_ACTIVATE;
|
2010-01-04 08:05:01 +08:00
|
|
|
#ifndef RTGUI_USING_SMALL_SIZE
|
2012-04-18 23:06:12 +08:00
|
|
|
if (RTGUI_WIDGET(object)->on_draw != RT_NULL)
|
|
|
|
RTGUI_WIDGET(object)->on_draw(object, event);
|
|
|
|
else
|
2010-01-04 08:05:01 +08:00
|
|
|
#endif
|
2012-04-18 23:06:12 +08:00
|
|
|
rtgui_widget_update(RTGUI_WIDGET(win));
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2010-02-13 00:02:00 +08:00
|
|
|
if (win->on_deactivate != RT_NULL)
|
|
|
|
{
|
2012-04-18 23:06:12 +08:00
|
|
|
win->on_deactivate(RTGUI_OBJECT(object), event);
|
2010-02-13 00:02:00 +08:00
|
|
|
}
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RTGUI_EVENT_PAINT:
|
2010-01-04 08:05:01 +08:00
|
|
|
#ifndef RTGUI_USING_SMALL_SIZE
|
2012-04-18 23:06:12 +08:00
|
|
|
if (RTGUI_WIDGET(object)->on_draw != RT_NULL)
|
|
|
|
RTGUI_WIDGET(object)->on_draw(object, event);
|
2010-01-04 08:05:01 +08:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
rtgui_win_ondraw(win);
|
2009-11-24 07:34:58 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RTGUI_EVENT_MOUSE_BUTTON:
|
2011-06-29 17:57:28 +08:00
|
|
|
/* check whether has widget which handled mouse event before */
|
2012-04-18 23:06:12 +08:00
|
|
|
if (win->last_mevent_widget != RT_NULL)
|
2011-06-29 17:57:28 +08:00
|
|
|
{
|
2012-04-18 23:06:12 +08:00
|
|
|
RTGUI_OBJECT(win->last_mevent_widget)->event_handler(
|
|
|
|
RTGUI_OBJECT(win->last_mevent_widget),
|
|
|
|
event);
|
2011-06-29 17:57:28 +08:00
|
|
|
|
|
|
|
/* clean last mouse event handled widget */
|
2012-04-18 23:06:12 +08:00
|
|
|
win->last_mevent_widget = RT_NULL;
|
2011-06-29 17:57:28 +08:00
|
|
|
}
|
2012-04-18 23:06:12 +08:00
|
|
|
else if (rtgui_container_dispatch_mouse_event(RTGUI_CONTAINER(win),
|
2010-02-13 00:02:00 +08:00
|
|
|
(struct rtgui_event_mouse*)event) == RT_FALSE)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2010-01-04 08:05:01 +08:00
|
|
|
#ifndef RTGUI_USING_SMALL_SIZE
|
2012-04-18 23:06:12 +08:00
|
|
|
if (RTGUI_WIDGET(object)->on_mouseclick != RT_NULL)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-04-18 23:06:12 +08:00
|
|
|
return RTGUI_WIDGET(object)->on_mouseclick(object, event);
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
2010-01-04 08:05:01 +08:00
|
|
|
#endif
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RTGUI_EVENT_MOUSE_MOTION:
|
|
|
|
#if 0
|
|
|
|
if (rtgui_widget_dispatch_mouse_event(widget,
|
|
|
|
(struct rtgui_event_mouse*)event) == RT_FALSE)
|
|
|
|
{
|
2010-01-04 08:05:01 +08:00
|
|
|
#ifndef RTGUI_USING_SMALL_SIZE
|
2009-11-24 07:34:58 +08:00
|
|
|
/* handle event in current widget */
|
|
|
|
if (widget->on_mousemotion != RT_NULL)
|
|
|
|
{
|
|
|
|
return widget->on_mousemotion(widget, event);
|
|
|
|
}
|
2010-01-04 08:05:01 +08:00
|
|
|
#endif
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
else return RT_TRUE;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
case RTGUI_EVENT_KBD:
|
|
|
|
/* we should dispatch key event firstly */
|
|
|
|
if (!(win->flag & RTGUI_WIN_FLAG_HANDLE_KEY))
|
2010-02-13 00:02:00 +08:00
|
|
|
{
|
2012-07-20 20:37:56 +08:00
|
|
|
struct rtgui_widget *widget;
|
2012-04-18 23:06:12 +08:00
|
|
|
rt_bool_t res = RT_FALSE;
|
|
|
|
/* we should dispatch the key event just once. Once entered the
|
|
|
|
* dispatch mode, we should swtich to key handling mode. */
|
|
|
|
win->flag |= RTGUI_WIN_FLAG_HANDLE_KEY;
|
|
|
|
|
|
|
|
/* dispatch the key event */
|
2012-07-20 20:37:56 +08:00
|
|
|
for (widget = win->focused_widget;
|
|
|
|
widget && !res;
|
|
|
|
widget = widget->parent)
|
|
|
|
{
|
|
|
|
if (RTGUI_OBJECT(widget)->event_handler != RT_NULL)
|
|
|
|
res = RTGUI_OBJECT(widget)->event_handler(
|
|
|
|
RTGUI_OBJECT(widget), event);
|
|
|
|
}
|
2012-04-18 23:06:12 +08:00
|
|
|
|
|
|
|
win->flag &= ~RTGUI_WIN_FLAG_HANDLE_KEY;
|
|
|
|
return res;
|
2010-02-13 00:02:00 +08:00
|
|
|
}
|
2012-04-18 23:06:12 +08:00
|
|
|
else
|
2009-11-26 17:28:06 +08:00
|
|
|
{
|
2012-04-18 23:06:12 +08:00
|
|
|
/* in key handling mode(it may reach here in
|
|
|
|
* win->focused_widget->event_handler call) */
|
|
|
|
if (win->on_key != RT_NULL)
|
|
|
|
return win->on_key(RTGUI_OBJECT(win), event);
|
2009-11-26 17:28:06 +08:00
|
|
|
}
|
2012-04-18 23:06:12 +08:00
|
|
|
break;
|
2009-11-26 17:28:06 +08:00
|
|
|
|
2009-11-24 07:34:58 +08:00
|
|
|
default:
|
|
|
|
/* call parent event handler */
|
2012-04-18 23:06:12 +08:00
|
|
|
return rtgui_toplevel_event_handler(object, event);
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return RT_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
if (win->flag & RTGUI_WIN_FLAG_CONNECTED)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
|
|
|
/* set window resize event to server */
|
|
|
|
RTGUI_EVENT_WIN_RESIZE_INIT(&event);
|
|
|
|
event.wid = win;
|
|
|
|
event.rect = *rect;
|
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
rtgui_server_post_event(&(event.parent), sizeof(struct rtgui_event_win_resize));
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
void rtgui_win_set_onkey(rtgui_win_t* win, rtgui_event_handler_ptr handler)
|
|
|
|
{
|
|
|
|
if (win != RT_NULL)
|
|
|
|
{
|
|
|
|
win->on_key = handler;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-24 07:34:58 +08:00
|
|
|
void rtgui_win_set_title(rtgui_win_t* win, const char *title)
|
|
|
|
{
|
|
|
|
/* send title to server */
|
2012-04-18 23:06:12 +08:00
|
|
|
if (win->flag & RTGUI_WIN_FLAG_CONNECTED)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|