From aa35bccfee9008d03f8a1f7de79655f409a39228 Mon Sep 17 00:00:00 2001 From: "bernard.xiong" Date: Tue, 10 Aug 2010 07:25:08 +0000 Subject: [PATCH] fix view focus bug. git-svn-id: https://rt-thread.googlecode.com/svn/trunk@840 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- .../rtgui/include/rtgui/widgets/widget.h | 3 ++ components/rtgui/widgets/view.c | 9 +++- components/rtgui/widgets/widget.c | 51 ++++++++++++------- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/components/rtgui/include/rtgui/widgets/widget.h b/components/rtgui/include/rtgui/widgets/widget.h index 078f12acd3..719272f272 100644 --- a/components/rtgui/include/rtgui/widgets/widget.h +++ b/components/rtgui/include/rtgui/widgets/widget.h @@ -188,6 +188,9 @@ rtgui_widget_t* rtgui_widget_get_next_sibling(rtgui_widget_t* widget); /* get the prev sibling of widget */ rtgui_widget_t* rtgui_widget_get_prev_sibling(rtgui_widget_t* widget); +/* dump widget information */ +void rtgui_widget_dump(rtgui_widget_t* widget); + #ifdef __cplusplus } #endif diff --git a/components/rtgui/widgets/view.c b/components/rtgui/widgets/view.c index 04cbc79a34..4a7dfe12e6 100644 --- a/components/rtgui/widgets/view.c +++ b/components/rtgui/widgets/view.c @@ -148,8 +148,13 @@ rtgui_modal_code_t rtgui_view_show(rtgui_view_t* view, rt_bool_t is_modal) workbench = RTGUI_WORKBENCH(RTGUI_WIDGET(view)->parent); rtgui_workbench_show_view(workbench, view); - if (RTGUI_WIDGET_IS_FOCUSABLE(RTGUI_WIDGET(view))) - rtgui_widget_focus(RTGUI_WIDGET(view)); + if (RTGUI_CONTAINER(view)->focused != RT_NULL) + rtgui_widget_focus(RTGUI_CONTAINER(view)->focused); + else + { + if (RTGUI_WIDGET_IS_FOCUSABLE(RTGUI_WIDGET(view))) + rtgui_widget_focus(RTGUI_WIDGET(view)); + } view->modal_show = is_modal; if (is_modal == RT_TRUE) diff --git a/components/rtgui/widgets/widget.c b/components/rtgui/widgets/widget.c index e046087287..81ee2d797b 100644 --- a/components/rtgui/widgets/widget.c +++ b/components/rtgui/widgets/widget.c @@ -250,37 +250,31 @@ void rtgui_widget_set_oncommand(rtgui_widget_t* widget, rtgui_event_handler_ptr */ void rtgui_widget_focus(rtgui_widget_t *widget) { - rtgui_widget_t *focused; rtgui_container_t *parent; RT_ASSERT(widget != RT_NULL); - if (!widget->parent || !RTGUI_WIDGET_IS_FOCUSABLE(widget) || !RTGUI_WIDGET_IS_ENABLE(widget)) + if (!widget->parent || !widget->toplevel) return; + if (!RTGUI_WIDGET_IS_FOCUSABLE(widget) || !RTGUI_WIDGET_IS_ENABLE(widget)) return; /* set widget as focused */ widget->flag |= RTGUI_WIDGET_FLAG_FOCUS; - /* get parent container */ - parent = RTGUI_CONTAINER(widget->parent); + /* get root parent container and old focused widget */ + parent = RTGUI_CONTAINER(widget->toplevel); + if (parent->focused == widget) return ; /* it's the same focused widget */ - /* get old focused widget */ - focused = parent->focused; - if (focused == widget) return ; /* it's the same focused widget */ - - if (focused != RT_NULL) - rtgui_widget_unfocus(focused); + /* unfocused the old widget */ + if (parent->focused != RT_NULL) rtgui_widget_unfocus(parent->focused); /* set widget as focused widget in parent link */ - parent->focused = widget; - while (RTGUI_WIDGET(parent)->parent != RT_NULL) + parent = RTGUI_CONTAINER(widget->parent); + do { - parent = RTGUI_CONTAINER(RTGUI_WIDGET(parent)->parent); parent->focused = widget; - - /* if the parent is hide, break it */ - if (RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(parent))) break; - } + parent = RTGUI_CONTAINER(RTGUI_WIDGET(parent)->parent); + } while ((parent != RT_NULL) && !RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(parent))); /* invoke on focus in call back */ if (widget->on_focus_in != RT_NULL) @@ -559,3 +553,26 @@ rtgui_widget_t* rtgui_widget_get_prev_sibling(rtgui_widget_t* widget) return sibling; } + +#ifdef RTGUI_WIDGET_DEBUG +#include +#include +void rtgui_widget_dump(rtgui_widget_t* widget) +{ + struct rtgui_object *obj; + + obj = RTGUI_OBJECT(widget); + rt_kprintf("widget type: %s ", obj->type->name); + + if (RTGUI_IS_VIEW(widget) == RT_TRUE) + rt_kprintf(":%s ", RTGUI_VIEW(widget)->title); + if (RTGUI_IS_WIN(widget) == RT_TRUE) + rt_kprintf(":%s ", RTGUI_WIN(widget)->title); + if ((RTGUI_IS_LABEL(widget) == RT_TRUE) || (RTGUI_IS_BUTTON(widget) == RT_TRUE)) + rt_kprintf(":%s ", RTGUI_LABEL(widget)->text); + + rt_kprintf("extent(%d, %d) - (%d, %d)\n", widget->extent.x1, + widget->extent.y1, widget->extent.x2, widget->extent.y2); + // rtgui_region_dump(&(widget->clip)); +} +#endif