diff options
Diffstat (limited to 'components/rtgui/widgets')
31 files changed, 0 insertions, 12343 deletions
diff --git a/components/rtgui/widgets/box.c b/components/rtgui/widgets/box.c deleted file mode 100644 index 448f47ea36..0000000000 --- a/components/rtgui/widgets/box.c +++ /dev/null @@ -1,288 +0,0 @@ -/* - * File : box.c - * This file is part of 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-16 Bernard first version - */ -#include <rtgui/dc.h> -#include <rtgui/widgets/box.h> - -static void _rtgui_box_constructor(rtgui_box_t *box) -{ - /* init widget and set event handler */ - rtgui_object_set_event_handler(RTGUI_OBJECT(box), RT_NULL); - - /* set proper of control */ - box->orient = RTGUI_HORIZONTAL; - box->border_size = RTGUI_BORDER_DEFAULT_WIDTH; - box->container = RT_NULL; -} - -DEFINE_CLASS_TYPE(box, "box", - RTGUI_OBJECT_TYPE, - _rtgui_box_constructor, - RT_NULL, - sizeof(struct rtgui_box)); - -struct rtgui_box *rtgui_box_create(int orientation, int border_size) -{ - struct rtgui_box *box; - - box = (struct rtgui_box *) rtgui_object_create(RTGUI_BOX_TYPE); - if (box != RT_NULL) - { - box->orient = orientation; - box->border_size = border_size; - } - - return box; -} -RTM_EXPORT(rtgui_box_create); - -void rtgui_box_destroy(struct rtgui_box *box) -{ - rtgui_object_destroy(RTGUI_OBJECT(box)); -} -RTM_EXPORT(rtgui_box_destroy); - -static void rtgui_box_layout_vertical(struct rtgui_box *box, struct rtgui_rect *extent) -{ - rtgui_list_t *node; - rt_int32_t box_width; - rt_int32_t space_count; - rt_int32_t next_x, next_y; - rt_int32_t total_height, space_height; - struct rtgui_event_resize size_event; - - /* prepare the resize event */ - RTGUI_EVENT_RESIZE_INIT(&size_event); - - /* find spaces */ - space_count = 0; - total_height = 0; - space_height = 0; - - rtgui_list_foreach(node, &(box->container->children)) - { - rtgui_widget_t *widget = rtgui_list_entry(node, struct rtgui_widget, sibling); - if (widget->align & RTGUI_ALIGN_STRETCH) space_count ++; - else total_height += widget->min_height; - } - - /* calculate the height for each spaces */ - if (space_count != 0) - { - space_height = (rtgui_rect_height(*extent) - total_height - (box->border_size << 1)) / space_count; - } - - /* init (x, y) and box width */ - next_x = extent->x1 + box->border_size; - next_y = extent->y1 + box->border_size; - box_width = rtgui_rect_width(*extent) - (box->border_size << 1); - - /* layout each widget */ - rtgui_list_foreach(node, &(box->container->children)) - { - struct rtgui_rect *rect; - rtgui_widget_t *widget = rtgui_list_entry(node, struct rtgui_widget, sibling); - - /* get extent of widget */ - rect = &(widget->extent); - - /* reset rect */ - rtgui_rect_moveto(rect, -rect->x1, -rect->y1); - rect->x2 = widget->min_width; - rect->y2 = widget->min_height; - - /* left in default */ - rtgui_rect_moveto(rect, next_x, next_y); - - if (widget->align & RTGUI_ALIGN_EXPAND) - { - /* expand on horizontal */ - rect->x2 = rect->x1 + (rt_int16_t)box_width; - } - if (widget->align & RTGUI_ALIGN_CENTER_VERTICAL) - { - /* center */ - rt_uint32_t mid; - - mid = box_width - rtgui_rect_width(*rect); - mid = mid / 2; - - rect->x1 = next_x + mid; - rect->x2 = next_x + box_width - mid; - } - else if (widget->align & RTGUI_ALIGN_RIGHT) - { - /* right */ - rect->x1 = next_x + box_width - rtgui_rect_width(*rect); - rect->x2 = next_x + box_width; - } - - if (widget->align & RTGUI_ALIGN_STRETCH) - { - rect->y2 = rect->y1 + space_height; - } - - /* process resize event */ - size_event.x = rect->x1; - size_event.y = rect->y1; - size_event.w = rect->x2 - rect->x1; - size_event.h = rect->y2 - rect->y1; - RTGUI_OBJECT(widget)->event_handler(RTGUI_OBJECT(widget), - &size_event.parent); - - /* point to next height */ - next_y = rect->y2 + box->border_size; - } -} - -static void rtgui_box_layout_horizontal(struct rtgui_box *box, struct rtgui_rect *extent) -{ - rtgui_list_t *node; - rt_int32_t box_height; - rt_int32_t space_count; - rt_int32_t next_x, next_y; - rt_int32_t total_width, space_width; - struct rtgui_event_resize size_event; - - /* prepare the resize event */ - RTGUI_EVENT_RESIZE_INIT(&size_event); - - /* find spaces */ - space_count = 0; - total_width = 0; - space_width = 0; - - rtgui_list_foreach(node, &(box->container->children)) - { - rtgui_widget_t *widget = rtgui_list_entry(node, struct rtgui_widget, sibling); - if (widget->align & RTGUI_ALIGN_STRETCH) space_count ++; - else total_width += widget->min_width; - } - - if (space_count != 0) - { - /* calculate the height for each spaces */ - space_width = (rtgui_rect_width(*extent) - total_width) / space_count; - } - - /* init (x, y) and box height */ - next_x = extent->x1 + box->border_size; - next_y = extent->y1 + box->border_size; - box_height = rtgui_rect_height(*extent) - (box->border_size << 1); - - /* layout each widget */ - rtgui_list_foreach(node, &(box->container->children)) - { - rtgui_rect_t *rect; - rtgui_widget_t *widget = rtgui_list_entry(node, struct rtgui_widget, sibling); - - /* get extent of widget */ - rect = &(widget->extent); - - /* reset rect */ - rtgui_rect_moveto(rect, -rect->x1, -rect->y1); - rect->x2 = widget->min_width; - rect->y2 = widget->min_height; - - /* top in default */ - rtgui_rect_moveto(rect, next_x, next_y); - - if (widget->align & RTGUI_ALIGN_EXPAND) - { - /* expand on vertical */ - rect->y2 = rect->y1 + box_height; - } - if (widget->align & RTGUI_ALIGN_CENTER_HORIZONTAL) - { - /* center */ - rt_uint32_t mid; - - mid = box_height - rtgui_rect_height(*rect); - mid = mid / 2; - - rect->y1 = next_y + mid; - rect->y2 = next_y + box_height - mid; - } - else if (widget->align & RTGUI_ALIGN_RIGHT) - { - /* right */ - rect->y1 = next_y + box_height - rtgui_rect_height(*rect); - rect->y2 = next_y + box_height; - } - - if (widget->align & RTGUI_ALIGN_STRETCH) - { - rect->x2 = rect->x1 + space_width; - } - - /* process resize event */ - size_event.x = rect->x1; - size_event.y = rect->y1; - size_event.w = rect->x2 - rect->x1; - size_event.h = rect->y2 - rect->y1; - RTGUI_OBJECT(widget)->event_handler(RTGUI_OBJECT(widget), - &size_event.parent); - - /* point to next width */ - next_x = rect->x2 + box->border_size; - } -} - -void rtgui_box_layout(rtgui_box_t *box) -{ - struct rtgui_rect extent; - RT_ASSERT(box != RT_NULL); - - if (box->container == RT_NULL) return; - - rtgui_widget_get_extent(RTGUI_WIDGET(box->container), &extent); - if (box->orient & RTGUI_VERTICAL) - { - rtgui_box_layout_vertical(box, &extent); - } - else - { - rtgui_box_layout_horizontal(box, &extent); - } - - /* update box and its children clip */ - if (!RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(box->container))) - { - rtgui_widget_update_clip(RTGUI_WIDGET(box->container)); - } -} -RTM_EXPORT(rtgui_box_layout); - -void rtgui_box_layout_rect(rtgui_box_t *box, struct rtgui_rect *rect) -{ - RT_ASSERT(box != RT_NULL); - - if (box->container == RT_NULL) return; - - if (box->orient & RTGUI_VERTICAL) - { - rtgui_box_layout_vertical(box, rect); - } - else - { - rtgui_box_layout_horizontal(box, rect); - } - - /* update box and its children clip */ - if (!RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(box->container))) - { - rtgui_widget_update_clip(RTGUI_WIDGET(box->container)); - } -} -RTM_EXPORT(rtgui_box_layout_rect); - diff --git a/components/rtgui/widgets/button.c b/components/rtgui/widgets/button.c deleted file mode 100644 index ceca58f008..0000000000 --- a/components/rtgui/widgets/button.c +++ /dev/null @@ -1,305 +0,0 @@ -/* - * File : button.c - * This file is part of 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-16 Bernard first version - */ -#include <rtgui/dc.h> -#include <rtgui/rtgui_theme.h> -#include <rtgui/widgets/button.h> -#include <rtgui/widgets/window.h> - -static rt_bool_t rtgui_button_onunfocus(struct rtgui_object *object, rtgui_event_t *event); - -static void _rtgui_button_constructor(rtgui_button_t *button) -{ - /* init widget and set event handler */ - RTGUI_WIDGET(button)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE; - rtgui_object_set_event_handler(RTGUI_OBJECT(button), rtgui_button_event_handler); - rtgui_widget_set_onunfocus(RTGUI_WIDGET(button), rtgui_button_onunfocus); - /* un-press button */ - button->flag = 0; - - /* set flag and on_button event handler */ - button->pressed_image = RT_NULL; - button->unpressed_image = RT_NULL; - button->on_button = RT_NULL; - - /* set gc */ - RTGUI_WIDGET_FOREGROUND(button) = default_foreground; - RTGUI_WIDGET_BACKGROUND(button) = RTGUI_RGB(212, 208, 200); - RTGUI_WIDGET_TEXTALIGN(button) = RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL; -} - -static void _rtgui_button_destructor(rtgui_button_t *button) -{ - if (button->pressed_image != RT_NULL) - { - rtgui_image_destroy(button->pressed_image); - button->pressed_image = RT_NULL; - } - - if (button->unpressed_image != RT_NULL) - { - rtgui_image_destroy(button->unpressed_image); - button->unpressed_image = RT_NULL; - } -} - -DEFINE_CLASS_TYPE(button, "button", - RTGUI_LABEL_TYPE, - _rtgui_button_constructor, - _rtgui_button_destructor, - sizeof(struct rtgui_button)); - -rt_bool_t rtgui_button_event_handler(struct rtgui_object *object, struct rtgui_event *event) -{ - struct rtgui_widget *widget; - struct rtgui_button *btn; - - RT_ASSERT(object != RT_NULL); - RT_ASSERT(event != RT_NULL); - - widget = RTGUI_WIDGET(object); - btn = RTGUI_BUTTON(widget); - switch (event->type) - { - case RTGUI_EVENT_PAINT: - rtgui_theme_draw_button(btn); - break; - - case RTGUI_EVENT_KBD: - { - struct rtgui_event_kbd *ekbd = (struct rtgui_event_kbd *) event; - - if (RTGUI_WIDGET_IS_HIDE(widget)) return RT_FALSE; - if ((ekbd->key == RTGUIK_RETURN) || (ekbd->key == RTGUIK_SPACE)) - { - if (RTGUI_KBD_IS_DOWN(ekbd)) - { - btn->flag |= RTGUI_BUTTON_FLAG_PRESS; - } - else - { - btn->flag &= ~RTGUI_BUTTON_FLAG_PRESS; - } - - /* draw button */ - rtgui_theme_draw_button(btn); - - if ((btn->flag & RTGUI_BUTTON_FLAG_PRESS) && (btn->on_button != RT_NULL)) - { - /* call on button handler */ - btn->on_button(RTGUI_OBJECT(widget), event); - } - } - } - break; - - case RTGUI_EVENT_MOUSE_BUTTON: - if (RTGUI_WIDGET_IS_HIDE(widget)) - return RT_FALSE; - { - struct rtgui_event_mouse *emouse = (struct rtgui_event_mouse *)event; - - /* it's not this widget event, clean status */ - if (rtgui_rect_contains_point(&(RTGUI_WIDGET(btn)->extent), - emouse->x, emouse->y) != RT_EOK) - { - btn->flag &= ~RTGUI_BUTTON_FLAG_PRESS; - /* draw button */ - rtgui_theme_draw_button(btn); - - break; - } - - if (btn->flag & RTGUI_BUTTON_TYPE_PUSH) - { - /* it's a push button */ - if (emouse->button & RTGUI_MOUSE_BUTTON_UP) - { - if (btn->flag & RTGUI_BUTTON_FLAG_PRESS) - { - btn->flag &= ~RTGUI_BUTTON_FLAG_PRESS; - } - else - { - btn->flag |= RTGUI_BUTTON_FLAG_PRESS; - } - - /* draw button */ - rtgui_theme_draw_button(btn); - - if (btn->on_button != RT_NULL) - { - /* call on button handler */ - btn->on_button(RTGUI_OBJECT(widget), event); - } - -#ifndef RTGUI_USING_SMALL_SIZE - /* invokes call back */ - if (widget->on_mouseclick != RT_NULL && - emouse->button & RTGUI_MOUSE_BUTTON_UP) - return widget->on_mouseclick(RTGUI_OBJECT(widget), event); -#endif - } - } - else - { - if (emouse->button & RTGUI_MOUSE_BUTTON_LEFT) - { - /* set the last mouse event handled widget */ - struct rtgui_win *win; - /* need callback */ - rt_bool_t need_cb = RT_FALSE; - - /* we need to decide whether the callback will be invoked - * before the flag has changed. Moreover, we cannot invoke - * it directly here, because the button might be destroyed - * in the callback. If that happens, program will crash on - * the following code. We need to make sure that the - * callbacks are invoke at the very last step. */ - if ((btn->flag & RTGUI_BUTTON_FLAG_PRESS) - && (emouse->button & RTGUI_MOUSE_BUTTON_UP)) - { - need_cb = RT_TRUE; - } - - /* if the button will handle the mouse up event here, it - * should not be the last_mevent_widget. Take care that - * don't overwrite other widgets. */ - win = RTGUI_WIN(RTGUI_WIDGET(btn)->toplevel); - if (emouse->button & RTGUI_MOUSE_BUTTON_DOWN) - { - btn->flag |= RTGUI_BUTTON_FLAG_PRESS; - win->last_mevent_widget = RTGUI_WIDGET(btn); - } - else - { - btn->flag &= ~RTGUI_BUTTON_FLAG_PRESS; - if (win->last_mevent_widget == RTGUI_WIDGET(btn)) - win->last_mevent_widget = RT_NULL; - } - - /* draw button */ - rtgui_theme_draw_button(btn); - - if (need_cb) - { - if (btn->on_button) - btn->on_button(RTGUI_OBJECT(widget), event); -#ifndef RTGUI_USING_SMALL_SIZE - if (widget->on_mouseclick) - return widget->on_mouseclick(RTGUI_OBJECT(widget), event); -#endif - } - } - } - - return RT_TRUE; - } - default: - return rtgui_widget_event_handler(object, event); - } - - return RT_FALSE; -} -RTM_EXPORT(rtgui_button_event_handler); - -rtgui_button_t *rtgui_button_create(const char *text) -{ - struct rtgui_button *btn; - - btn = (struct rtgui_button *) rtgui_widget_create(RTGUI_BUTTON_TYPE); - if (btn != RT_NULL) - { - rtgui_rect_t rect; - - /* set default rect */ - rtgui_font_get_metrics(rtgui_font_default(), text, &rect); - rect.x2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1); - rect.y2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1); - rtgui_widget_set_rect(RTGUI_WIDGET(btn), &rect); - rtgui_label_set_text(RTGUI_LABEL(btn), text); - } - - return btn; -} -RTM_EXPORT(rtgui_button_create); - -rtgui_button_t *rtgui_pushbutton_create(const char *text) -{ - rtgui_button_t *btn; - - btn = rtgui_button_create(text); - if (btn != RT_NULL) btn->flag |= RTGUI_BUTTON_TYPE_PUSH; - - return btn; -} -RTM_EXPORT(rtgui_pushbutton_create); - -void rtgui_button_destroy(rtgui_button_t *btn) -{ - rtgui_widget_destroy(RTGUI_WIDGET(btn)); -} -RTM_EXPORT(rtgui_button_destroy); - -void rtgui_button_set_pressed_image(rtgui_button_t *btn, rtgui_image_t *image) -{ - RT_ASSERT(btn != RT_NULL); - - btn->pressed_image = image; -} -RTM_EXPORT(rtgui_button_set_pressed_image); - -void rtgui_button_set_unpressed_image(rtgui_button_t *btn, rtgui_image_t *image) -{ - RT_ASSERT(btn != RT_NULL); - - btn->unpressed_image = image; -} -RTM_EXPORT(rtgui_button_set_unpressed_image); - -void rtgui_button_set_onbutton(rtgui_button_t *btn, rtgui_onbutton_func_t func) -{ - RT_ASSERT(btn != RT_NULL); - - btn->on_button = func; -} -RTM_EXPORT(rtgui_button_set_onbutton); - -static rt_bool_t rtgui_button_onunfocus(struct rtgui_object *object, rtgui_event_t *event) -{ - rtgui_rect_t rect; - rtgui_widget_t *widget; - struct rtgui_dc *dc; - - RT_ASSERT(object); - widget = RTGUI_WIDGET(object); - - dc = rtgui_dc_begin_drawing(widget); - if (dc == RT_NULL) return RT_FALSE; - - rtgui_widget_get_rect(widget, &rect); - - if (!RTGUI_WIDGET_IS_FOCUSED(widget)) - { - /* only clear focus rect */ - rtgui_color_t color; - rtgui_rect_inflate(&rect, -2); - color = RTGUI_DC_FC(dc); - RTGUI_DC_FC(dc) = RTGUI_DC_BC(dc); - rtgui_dc_draw_focus_rect(dc, &rect); - RTGUI_DC_FC(dc) = color; - } - - rtgui_dc_end_drawing(dc); - return RT_TRUE; -} diff --git a/components/rtgui/widgets/checkbox.c b/components/rtgui/widgets/checkbox.c deleted file mode 100644 index 4f0c6a49bc..0000000000 --- a/components/rtgui/widgets/checkbox.c +++ /dev/null @@ -1,152 +0,0 @@ -#include <rtgui/dc.h> -#include <rtgui/rtgui_theme.h> -#include <rtgui/widgets/checkbox.h> - -static void _rtgui_checkbox_constructor(rtgui_checkbox_t *box) -{ - /* init widget and set event handler */ - RTGUI_WIDGET(box)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE; - rtgui_object_set_event_handler(RTGUI_OBJECT(box), rtgui_checkbox_event_handler); - - /* set status */ - box->status_down = RTGUI_CHECKBOX_STATUS_UNCHECKED; - box->on_button = RT_NULL; - - /* set default gc */ - RTGUI_WIDGET_TEXTALIGN(box) = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_CENTER_VERTICAL; -} - -DEFINE_CLASS_TYPE(checkbox, "checkbox", - RTGUI_LABEL_TYPE, - _rtgui_checkbox_constructor, - RT_NULL, - sizeof(struct rtgui_checkbox)); - -void rtgui_checkbox_set_onbutton(rtgui_checkbox_t *checkbox, rtgui_onbutton_func_t func) -{ - RT_ASSERT(checkbox != RT_NULL); - - checkbox->on_button = func; -} - -rt_bool_t rtgui_checkbox_event_handler(struct rtgui_object *object, struct rtgui_event *event) -{ - struct rtgui_checkbox *box; - - RTGUI_WIDGET_EVENT_HANDLER_PREPARE - - box = RTGUI_CHECKBOX(object); - - switch (event->type) - { - case RTGUI_EVENT_PAINT: -#ifndef RTGUI_USING_SMALL_SIZE - if (widget->on_draw != RT_NULL) - { - return widget->on_draw(RTGUI_OBJECT(widget), event); - } - else -#endif - rtgui_theme_draw_checkbox(box); - break; - - case RTGUI_EVENT_MOUSE_BUTTON: - { - if (RTGUI_WIDGET_IS_ENABLE(widget) && !RTGUI_WIDGET_IS_HIDE(widget)) - { - struct rtgui_event_mouse *emouse = (struct rtgui_event_mouse *)event; - if (emouse->button & RTGUI_MOUSE_BUTTON_LEFT && - emouse->button & RTGUI_MOUSE_BUTTON_UP) - { - /* set focus */ - rtgui_widget_focus(widget); - - if (box->status_down & RTGUI_CHECKBOX_STATUS_UNCHECKED) - { - /* check it */ - box->status_down = RTGUI_CHECKBOX_STATUS_CHECKED; - } - else - { - /* un-check it */ - box->status_down = RTGUI_CHECKBOX_STATUS_UNCHECKED; - } - } - - /* draw checkbox */ - rtgui_theme_draw_checkbox(box); - -#ifndef RTGUI_USING_SMALL_SIZE - /* call user callback */ - if (widget->on_mouseclick != RT_NULL) - { - return widget->on_mouseclick(RTGUI_OBJECT(widget), event); - } -#endif - if (box->on_button != RT_NULL) - { - box->on_button(RTGUI_OBJECT(widget), event); - return RT_TRUE; - } - } - - return RT_TRUE; - } - default: - return rtgui_widget_event_handler(object, event); - } - - return RT_FALSE; -} - -struct rtgui_checkbox *rtgui_checkbox_create(const char *text, rt_bool_t checked) -{ - struct rtgui_checkbox *box; - - box = (struct rtgui_checkbox *) rtgui_widget_create(RTGUI_CHECKBOX_TYPE); - if (box != RT_NULL) - { - rtgui_rect_t rect; - - /* set default rect */ - rtgui_font_get_metrics(rtgui_font_default(), text, &rect); - rect.x2 += RTGUI_BORDER_DEFAULT_WIDTH + 5 + (RTGUI_BORDER_DEFAULT_WIDTH << 1); - rect.y2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1); - - rtgui_widget_set_rect(RTGUI_WIDGET(box), &rect); - rtgui_label_set_text(RTGUI_LABEL(box), text); - - if (checked == RT_TRUE) - box->status_down = RTGUI_CHECKBOX_STATUS_CHECKED; - else - box->status_down = RTGUI_CHECKBOX_STATUS_UNCHECKED; - } - - return box; -} - -void rtgui_checkbox_destroy(rtgui_checkbox_t *box) -{ - rtgui_widget_destroy(RTGUI_WIDGET(box)); -} - -void rtgui_checkbox_set_checked(rtgui_checkbox_t *checkbox, rt_bool_t checked) -{ - RT_ASSERT(checkbox != RT_NULL); - if (checked == RT_TRUE) - checkbox->status_down = RTGUI_CHECKBOX_STATUS_CHECKED; - else - checkbox->status_down = RTGUI_CHECKBOX_STATUS_UNCHECKED; - -} - -rt_bool_t rtgui_checkbox_get_checked(rtgui_checkbox_t *checkbox) -{ - RT_ASSERT(checkbox != RT_NULL); - - if (checkbox->status_down == RTGUI_CHECKBOX_STATUS_CHECKED) - return RT_TRUE; - - return RT_FALSE; -} - diff --git a/components/rtgui/widgets/combobox.c b/components/rtgui/widgets/combobox.c deleted file mode 100644 index ece6cb2e1b..0000000000 --- a/components/rtgui/widgets/combobox.c +++ /dev/null @@ -1,280 +0,0 @@ -#include <rtgui/dc.h> -#include <rtgui/rtgui_theme.h> -#include <rtgui/widgets/combobox.h> - -static rt_bool_t rtgui_combobox_pulldown_hide(struct rtgui_object *object, struct rtgui_event *event); -const static rt_uint8_t down_arrow[] = {0xff, 0x7e, 0x3c, 0x18}; - -static void _rtgui_combobox_constructor(rtgui_combobox_t *box) -{ - rtgui_rect_t rect = {0, 0, RTGUI_COMBOBOX_WIDTH, RTGUI_COMBOBOX_HEIGHT}; - - /* init widget and set event handler */ - rtgui_object_set_event_handler(RTGUI_OBJECT(box), rtgui_combobox_event_handler); - rtgui_widget_set_rect(RTGUI_WIDGET(box), &rect); - - RTGUI_WIDGET_TEXTALIGN(box) = RTGUI_ALIGN_CENTER_VERTICAL; - - box->pd_pressed = RT_FALSE; - box->current_item = 0; - box->on_selected = RT_NULL; - box->pd_win = RT_NULL; -} - -static void _rtgui_combobox_destructor(rtgui_combobox_t *box) -{ - /* destroy pull down window */ - rtgui_win_destroy(box->pd_win); - - /* reset box field */ - box->pd_win = RT_NULL; -} - -rt_bool_t rtgui_combobox_pdwin_onitem(struct rtgui_object *object, struct rtgui_event *event) -{ - struct rtgui_widget *widget; - rtgui_win_t *pd_win; - rtgui_combobox_t *combo; - rtgui_listbox_t *list; - - RT_ASSERT(object != RT_NULL); - - widget = RTGUI_WIDGET(object); - list = RTGUI_LISTBOX(widget); - pd_win = RTGUI_WIN(rtgui_widget_get_toplevel(widget)); - combo = RTGUI_COMBOBOX(pd_win->user_data); - combo->current_item = list->current_item; - - if (combo->on_selected != RT_NULL) - combo->on_selected(RTGUI_OBJECT(combo), RT_NULL); - - rtgui_win_hide(pd_win); - rtgui_widget_update(RTGUI_WIDGET(combo)); - - return RT_FALSE; -} - -rt_bool_t rtgui_combobox_pdwin_ondeactive(struct rtgui_object *object, struct rtgui_event *event) -{ - rtgui_win_hide(RTGUI_WIN(object)); - return RT_TRUE; -} - -DEFINE_CLASS_TYPE(combobox, "combobox", - RTGUI_WIDGET_TYPE, - _rtgui_combobox_constructor, - _rtgui_combobox_destructor, - sizeof(struct rtgui_combobox)); - -rtgui_combobox_t *rtgui_combobox_create(struct rtgui_listbox_item *items, rt_uint16_t count, struct rtgui_rect *rect) -{ - rtgui_combobox_t *box; - - box = (rtgui_combobox_t *)rtgui_widget_create(RTGUI_COMBOBOX_TYPE); - box->items_count = count; - box->items = items; - rtgui_widget_set_rect(RTGUI_WIDGET(box), rect); - - box->pd_win = RT_NULL; - - return box; -} - -void rtgui_combobox_destroy(rtgui_combobox_t *box) -{ - rtgui_widget_destroy(RTGUI_WIDGET(box)); -} - -static void rtgui_combobox_ondraw(struct rtgui_combobox *box) -{ - /* draw button */ - rtgui_color_t bc; - struct rtgui_dc *dc; - struct rtgui_rect rect, r; - - /* begin drawing */ - dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(box)); - if (dc == RT_NULL) return; - - bc = RTGUI_WIDGET_BACKGROUND(box); - - /* get widget rect */ - rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect); - RTGUI_WIDGET_BACKGROUND(box) = white; - - /* fill widget rect with background color */ - rtgui_dc_fill_rect(dc, &rect); - rtgui_dc_draw_rect(dc, &rect); - - /* draw current item */ - if (box->current_item < box->items_count) - { - rect.x1 += 5; - rtgui_dc_draw_text(dc, box->items[box->current_item].name, &rect); - } - - /* restore background color */ - RTGUI_WIDGET_BACKGROUND(box) = bc; - - /* draw pull down button */ - rect.x1 = rect.x2 - RTGUI_COMBOBOX_BUTTON_WIDTH; - rtgui_rect_inflate(&rect, -1); - rtgui_dc_fill_rect(dc, &rect); - if (box->pd_pressed == RT_TRUE) rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_SUNKEN); - else rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_RAISE); - - r.x1 = 0; - r.y1 = 0; - r.x2 = 8; - r.y2 = 4; - rtgui_rect_moveto_align(&rect, &r, RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL); - rtgui_dc_draw_byte(dc, r.x1, r.y1, 4, down_arrow); - - /* end drawing */ - rtgui_dc_end_drawing(dc); - return; -} - -static rt_bool_t rtgui_combobox_onmouse_button(struct rtgui_combobox *box, struct rtgui_event_mouse *event) -{ - struct rtgui_rect rect; - - /* get widget rect */ - rect = RTGUI_WIDGET(box)->extent; - - /* move to the pull down button */ - rect.x1 = rect.x2 - RTGUI_COMBOBOX_BUTTON_WIDTH; - if (rtgui_rect_contains_point(&rect, event->x, event->y) == RT_EOK) - { - /* handle mouse button on pull down button */ - if (event->button & RTGUI_MOUSE_BUTTON_LEFT && - event->button & RTGUI_MOUSE_BUTTON_DOWN) - { - box->pd_pressed = RT_TRUE; - rtgui_widget_update(RTGUI_WIDGET(box)); - } - else if (event->button & RTGUI_MOUSE_BUTTON_LEFT && - event->button & RTGUI_MOUSE_BUTTON_UP) - { - box->pd_pressed = RT_FALSE; - rtgui_widget_update(RTGUI_WIDGET(box)); - - /* pop pull down window */ - if (box->pd_win == RT_NULL) - { - rtgui_listbox_t *list; - - /* create pull down window */ - rect = RTGUI_WIDGET(box)->extent; - rect.y1 = rect.y2; - /* give it 5 pixels margin, or the last item won't get shown */ - rect.y2 = rect.y1 + box->items_count * (2 + rtgui_theme_get_selected_height()) + 5; - box->pd_win = rtgui_win_create(RT_NULL, "combo", &rect, RTGUI_WIN_STYLE_NO_TITLE); - rtgui_win_set_ondeactivate(RTGUI_WIN(box->pd_win), rtgui_combobox_pulldown_hide); - /* set user data to parent combobox */ - box->pd_win->user_data = (rt_uint32_t)box; - - /* create list box */ - rtgui_rect_inflate(&rect, -1); - list = rtgui_listbox_create(box->items, box->items_count, &rect); - rtgui_container_add_child(RTGUI_CONTAINER(box->pd_win), RTGUI_WIDGET(list)); - rtgui_widget_focus(RTGUI_WIDGET(list)); - - rtgui_listbox_set_onitem(list, rtgui_combobox_pdwin_onitem); - rtgui_win_set_ondeactivate(box->pd_win, rtgui_combobox_pdwin_ondeactive); - } - - /* show combo box pull down window */ - rtgui_win_show(RTGUI_WIN(box->pd_win), RT_FALSE); - } - - return RT_TRUE; - } - - return RT_FALSE; -} - -rt_bool_t rtgui_combobox_event_handler(struct rtgui_object *object, struct rtgui_event *event) -{ - struct rtgui_combobox *box; - RTGUI_WIDGET_EVENT_HANDLER_PREPARE - - box = RTGUI_COMBOBOX(object); - - switch (event->type) - { - case RTGUI_EVENT_PAINT: -#ifndef RTGUI_USING_SMALL_SIZE - if (widget->on_draw != RT_NULL) - widget->on_draw(RTGUI_OBJECT(widget), event); - else -#endif - rtgui_combobox_ondraw(box); - - break; - - case RTGUI_EVENT_MOUSE_BUTTON: - return rtgui_combobox_onmouse_button(box, (struct rtgui_event_mouse *)event); - - case RTGUI_EVENT_FOCUSED: - { - /* item focused */ - struct rtgui_event_focused *focused; - - focused = (struct rtgui_event_focused *) event; - - if (focused->widget != RT_NULL) - { - /* hide pull down window */ - rtgui_win_hide(RTGUI_WIN(box->pd_win)); - rtgui_combobox_ondraw(box); - } - } - break; - default: - return rtgui_widget_event_handler(object, event); - } - - return RT_FALSE; -} - -static rt_bool_t rtgui_combobox_pulldown_hide(struct rtgui_object *object, struct rtgui_event *event) -{ - struct rtgui_widget *widget; - struct rtgui_combobox *box; - - RT_ASSERT(object != RT_NULL); - RT_ASSERT(event != RT_NULL); - - widget = RTGUI_WIDGET(object); - box = RTGUI_COMBOBOX(object); - - if (widget == RT_NULL) return RT_TRUE; - - box = (struct rtgui_combobox *)(((struct rtgui_win *)widget)->user_data); - if (box == RT_NULL) return RT_TRUE; - - /* hide pull down window */ - rtgui_win_hide(RTGUI_WIN(box->pd_win)); - - /* clear pull down button state */ - box->pd_pressed = RT_FALSE; - rtgui_widget_update(RTGUI_WIDGET(box)); - - return RT_TRUE; -} - -struct rtgui_listbox_item *rtgui_combox_get_select(struct rtgui_combobox *box) -{ - if ((box != RT_NULL) && (box->current_item < box->items_count)) - { - return &(box->items[box->current_item]); - } - - return RT_NULL; -} - -void rtgui_combobox_set_onselected(struct rtgui_combobox *box, rtgui_event_handler_ptr func) -{ - box->on_selected = func; -} diff --git a/components/rtgui/widgets/container.c b/components/rtgui/widgets/container.c deleted file mode 100644 index 7014102cd2..0000000000 --- a/components/rtgui/widgets/container.c +++ /dev/null @@ -1,323 +0,0 @@ -/* - * File : container.c - * This file is part of 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-16 Bernard first version - * 2010-09-24 Bernard fix container destroy issue - */ -#include <rtgui/dc.h> -#include <rtgui/rtgui_system.h> -#include <rtgui/rtgui_app.h> -#include <rtgui/widgets/container.h> -#include <rtgui/widgets/window.h> - -static void _rtgui_container_constructor(rtgui_container_t *container) -{ - /* init container */ - rtgui_object_set_event_handler(RTGUI_OBJECT(container), - rtgui_container_event_handler); - - rtgui_list_init(&(container->children)); - container->layout_box = RT_NULL; - - RTGUI_WIDGET(container)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE; -} - -static void _rtgui_container_destructor(rtgui_container_t *container) -{ - rtgui_container_destroy_children(container); - - if (container->layout_box != RT_NULL) - rtgui_object_destroy(RTGUI_OBJECT(container->layout_box)); -} - -DEFINE_CLASS_TYPE(container, "container", - RTGUI_WIDGET_TYPE, - _rtgui_container_constructor, - _rtgui_container_destructor, - sizeof(struct rtgui_container)); -RTM_EXPORT(_rtgui_container); - -rt_bool_t rtgui_container_dispatch_event(rtgui_container_t *container, rtgui_event_t *event) -{ - /* handle in child widget */ - struct rtgui_list_node *node; - - rtgui_list_foreach(node, &(container->children)) - { - struct rtgui_widget *w; - w = rtgui_list_entry(node, struct rtgui_widget, sibling); - - if (RTGUI_OBJECT(w)->event_handler && - RTGUI_OBJECT(w)->event_handler(RTGUI_OBJECT(w), event) == RT_TRUE) - return RT_TRUE; - } - - return RT_FALSE; -} -RTM_EXPORT(rtgui_container_dispatch_event); - -/* broadcast means that the return value of event handlers will be ignored. The - * events will always reach every child.*/ -rt_bool_t rtgui_container_broadcast_event(struct rtgui_container *container, struct rtgui_event *event) -{ - struct rtgui_list_node *node; - - rtgui_list_foreach(node, &(container->children)) - { - struct rtgui_widget *w; - w = rtgui_list_entry(node, struct rtgui_widget, sibling); - - if (RTGUI_OBJECT(w)->event_handler) - RTGUI_OBJECT(w)->event_handler(RTGUI_OBJECT(w), event); - } - - return RT_FALSE; -} -RTM_EXPORT(rtgui_container_broadcast_event); - -rt_bool_t rtgui_container_dispatch_mouse_event(rtgui_container_t *container, struct rtgui_event_mouse *event) -{ - /* handle in child widget */ - struct rtgui_list_node *node; - struct rtgui_widget *old_focus; - - old_focus = RTGUI_WIDGET(container)->toplevel->focused_widget; - - rtgui_list_foreach(node, &(container->children)) - { - struct rtgui_widget *w; - w = rtgui_list_entry(node, struct rtgui_widget, sibling); - if (rtgui_rect_contains_point(&(w->extent), - event->x, event->y) == RT_EOK) - { - if ((old_focus != w) && RTGUI_WIDGET_IS_FOCUSABLE(w)) - rtgui_widget_focus(w); - if (RTGUI_OBJECT(w)->event_handler && - RTGUI_OBJECT(w)->event_handler(RTGUI_OBJECT(w), - (rtgui_event_t *)event) == RT_TRUE) - return RT_TRUE; - } - } - - return RT_FALSE; -} -RTM_EXPORT(rtgui_container_dispatch_mouse_event); - -rt_bool_t rtgui_container_event_handler(struct rtgui_object *object, struct rtgui_event *event) -{ - struct rtgui_container *container; - struct rtgui_widget *widget; - - RT_ASSERT(object != RT_NULL); - RT_ASSERT(event != RT_NULL); - - container = RTGUI_CONTAINER(object); - widget = RTGUI_WIDGET(object); - - switch (event->type) - { - case RTGUI_EVENT_PAINT: - { - struct rtgui_dc *dc; - struct rtgui_rect rect; - - dc = rtgui_dc_begin_drawing(widget); - if (dc == RT_NULL) - return RT_FALSE; - rtgui_widget_get_rect(widget, &rect); - - /* fill container with background */ - rtgui_dc_fill_rect(dc, &rect); - - /* paint on each child */ - rtgui_container_dispatch_event(container, event); - - rtgui_dc_end_drawing(dc); - } - break; - - case RTGUI_EVENT_KBD: - break; - - case RTGUI_EVENT_MOUSE_BUTTON: - case RTGUI_EVENT_MOUSE_MOTION: - /* handle in child widget */ - return rtgui_container_dispatch_mouse_event(container, - (struct rtgui_event_mouse *)event); - - case RTGUI_EVENT_SHOW: - rtgui_widget_onshow(RTGUI_OBJECT(container), event); - rtgui_container_dispatch_event(container, event); - break; - case RTGUI_EVENT_HIDE: - rtgui_widget_onhide(RTGUI_OBJECT(container), event); - rtgui_container_dispatch_event(container, event); - break; - case RTGUI_EVENT_COMMAND: - rtgui_container_dispatch_event(container, event); - break; - - case RTGUI_EVENT_UPDATE_TOPLVL: - /* call parent handler */ - rtgui_widget_onupdate_toplvl(object, event); - /* update the children */ - rtgui_container_broadcast_event(container, event); - break; - - case RTGUI_EVENT_RESIZE: - /* re-layout container */ - rtgui_container_layout(container); - break; - - default: - /* call parent widget event handler */ - return rtgui_widget_event_handler(RTGUI_OBJECT(widget), event); - } - - return RT_FALSE; -} -RTM_EXPORT(rtgui_container_event_handler); - -rtgui_container_t *rtgui_container_create(void) -{ - struct rtgui_container *container; - - /* allocate container */ - container = (struct rtgui_container *) rtgui_widget_create(RTGUI_CONTAINER_TYPE); - return container; -} -RTM_EXPORT(rtgui_container_create); - -void rtgui_container_destroy(rtgui_container_t *container) -{ - rtgui_widget_destroy(RTGUI_WIDGET(container)); -} -RTM_EXPORT(rtgui_container_destroy); - -/* - * This function will add a child to a container widget - * Note: this function will not change the widget layout - * the layout is the responsibility of layout widget, such as box. - */ -void rtgui_container_add_child(rtgui_container_t *container, rtgui_widget_t *child) -{ - RT_ASSERT(container != RT_NULL); - RT_ASSERT(child != RT_NULL); - - /* set parent and toplevel widget */ - child->parent = RTGUI_WIDGET(container); - /* put widget to parent's children list */ - rtgui_list_append(&(container->children), &(child->sibling)); - - /* update children toplevel */ - if (RTGUI_WIDGET(container)->toplevel != RT_NULL && - RTGUI_IS_WIN(RTGUI_WIDGET(container)->toplevel)) - { - struct rtgui_event_update_toplvl eup; - RTGUI_EVENT_UPDATE_TOPLVL_INIT(&eup); - eup.toplvl = RTGUI_WIDGET(container)->toplevel; - rtgui_container_broadcast_event(container, (struct rtgui_event *)&eup); - } -} -RTM_EXPORT(rtgui_container_add_child); - -/* remove a child to widget */ -void rtgui_container_remove_child(rtgui_container_t *container, rtgui_widget_t *child) -{ - RT_ASSERT(container != RT_NULL); - RT_ASSERT(child != RT_NULL); - - rtgui_widget_unfocus(child); - - /* remove widget from parent's children list */ - rtgui_list_remove(&(container->children), &(child->sibling)); - - /* set parent and toplevel widget */ - child->parent = RT_NULL; - child->toplevel = RT_NULL; -} -RTM_EXPORT(rtgui_container_remove_child); - -/* destroy all children of container */ -void rtgui_container_destroy_children(rtgui_container_t *container) -{ - struct rtgui_list_node *node; - - if (container == RT_NULL) - return; - - node = container->children.next; - while (node != RT_NULL) - { - rtgui_widget_t *child = rtgui_list_entry(node, rtgui_widget_t, sibling); - - if (RTGUI_IS_CONTAINER(child)) - { - /* break parent firstly */ - child->parent = RT_NULL; - - /* destroy children of child */ - rtgui_container_destroy_children(RTGUI_CONTAINER(child)); - } - - /* remove widget from parent's children list */ - rtgui_list_remove(&(container->children), &(child->sibling)); - - /* set parent and toplevel widget */ - child->parent = RT_NULL; - - /* destroy object and remove from parent */ - rtgui_object_destroy(RTGUI_OBJECT(child)); - - node = container->children.next; - } - - container->children.next = RT_NULL; - - /* update widget clip */ - rtgui_win_update_clip(RTGUI_WIN(RTGUI_WIDGET(container)->toplevel)); -} -RTM_EXPORT(rtgui_container_destroy_children); - -rtgui_widget_t *rtgui_container_get_first_child(rtgui_container_t *container) -{ - rtgui_widget_t *child = RT_NULL; - - RT_ASSERT(container != RT_NULL); - - if (container->children.next != RT_NULL) - { - child = rtgui_list_entry(container->children.next, rtgui_widget_t, sibling); - } - - return child; -} -RTM_EXPORT(rtgui_container_get_first_child); - -void rtgui_container_set_box(rtgui_container_t *container, struct rtgui_box *box) -{ - if (container == RT_NULL || box == RT_NULL) - return; - - container->layout_box = box; - box->container = container; -} -RTM_EXPORT(rtgui_container_set_box); - -void rtgui_container_layout(struct rtgui_container *container) -{ - if (container == RT_NULL || container->layout_box == RT_NULL) - return; - - rtgui_box_layout(container->layout_box); -} -RTM_EXPORT(rtgui_container_layout); - diff --git a/components/rtgui/widgets/digfont.c b/components/rtgui/widgets/digfont.c deleted file mode 100644 index 0d081f94ad..0000000000 --- a/components/rtgui/widgets/digfont.c +++ /dev/null @@ -1,309 +0,0 @@ -/* - * File : digfont.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, 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 - * 2012-12-21 pife first version - */ - -#include <rtthread.h> -#include <rtgui/rtgui.h> -#include <rtgui/dc.h> -#include <rtgui/widgets/digfont.h> - -#define DIGFONT_V 1 -#define DIGFONT_H 0 - -int rtgui_digitfont_create(struct rtgui_digitfont * font) -{ - int i; - int half; - int space; - int seg35_y; - int hight; - struct rtgui_digitfont_data * data, *databuf; - struct rtgui_digitfont_data temp, temp2;; - - data= rt_malloc(sizeof(struct rtgui_digitfont_data) * 7 * font->seg1_nr); - if (data== RT_NULL) - { - rt_kprintf("out of memory.\n"); - goto __err; - } - databuf = font->data = data; - - data = &temp; - half = font->seg1_nr >> 1; - space = font->seg1_hspace; - seg35_y = font->seg1_vspace + font->seg2_len + (half<<1) - font->seg1_vspace; - hight = (font->seg1_vspace << 1) + font->seg2_len + font->seg3_len + (half << 1) - 1; - - //seg 1 - data->type = DIGFONT_H; - data->x = font->seg1_hspace; - data->y = 0; - data->len = font->seg1_len; - - *databuf++ = *data; - for(i=1; i<font->seg1_nr; i++) - { - data->len -= 2; - data->x ++; - data->y ++; - *databuf++ = *data; - } - - //seg 2 - data->type = DIGFONT_V; - data->x = (font->seg1_hspace << 1) + font->seg1_len -1; - data->y = font->seg1_vspace ; - data->len = font->seg2_len; - *databuf++ = *data; - for(i=1; i<font->seg1_nr; i++) - { - if (i >= half) - { - data->len -= 2; - } - data->x --; - data->y ++; - *databuf++ = *data; - } - - //seg 3 - data->type = DIGFONT_V; - data->x = (font->seg1_hspace << 1) + font->seg1_len -1; - data->y = seg35_y; - data->len = font->seg3_len; - *databuf++ = *data; - temp2 = *data; - for(i=1; i<font->seg1_nr; i++) - { - if (i < half) - data->y --; - else - { - data->len -= 2; - data->y ++; - } - - data->x --; - *databuf++ = *data; - } - - // seg4 - data->type = DIGFONT_H; - data->x = font->seg1_hspace; - data->y = hight -1; - data->len = font->seg1_len; - *databuf++ = *data; - for(i=1; i<font->seg1_nr; i++) - { - data->len -= 2; - data->x ++; - data->y --; - *databuf++ = *data; - } - - //seg5 - data->type = DIGFONT_V; - data->x = 0; - data->y = seg35_y; - data->len = font->seg3_len; - *databuf++ = *data; - - for(i=1; i<font->seg1_nr; i++) - { - if (i < half) - data->y --; - else - { - data->len -= 2; - data->y ++; - } - - data->x ++; - *databuf++ = *data; - } - - //seg 6 - data->type = DIGFONT_V; - data->x = 0; - data->y = font->seg1_vspace ; - data->len = font->seg2_len; - *databuf++ = *data; - for(i=1; i<font->seg1_nr; i++) - { - if (i >= half) - { - data->len -= 2; - } - data->x ++; - data->y ++; - *databuf++ = *data; - } - - //seg7 - data->type = DIGFONT_H; - data->x = font->seg1_hspace + half - 1; - data->y = font->seg1_vspace + font->seg2_len + half -1; - data->len = (font->seg1_hspace << 1) + font->seg1_len - \ - ((font->seg1_hspace + half -1) << 1); - *databuf++ = *data; - temp2 = *data; - for(i=0; i<half; i++) - { - data->x ++; - data->y --; - data->len -= 2; - *databuf++ = *data; - } - - *data = temp2; - for (i=0; i<half; i++) - { - data->x ++; - data->y ++; - data->len -= 2; - *databuf++ = *data; - } - - return 0; - -__err: - return -1; -} - -int rtgui_dc_draw_digitfont(struct rtgui_dc *dc, struct rtgui_digitfont * font, rtgui_rect_t * rect) -{ - int i, size; - struct rtgui_digitfont_data * data; - - size = font->seg1_nr * 7; - data = font->data; - - for(i=0; i<size; i++) - { - if (data->type == DIGFONT_H) - rtgui_dc_draw_hline(dc, rect->x1 + data->x, \ - rect->x1 + data->x + data->len, rect->y1 + data->y); - else - rtgui_dc_draw_vline(dc, rect->x1 + data->x, rect->y1 + data->y, \ - rect->y1 + data->y + data->len); - - data++; - } -} - -const char digtube_code_table[] = -{ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,}; - -int rtgui_dc_draw_digitfont_code(struct rtgui_dc *dc, struct rtgui_digitfont * font, rtgui_rect_t * rect, char code) -{ - int i,j; - struct rtgui_digitfont_data * data; - rtgui_color_t color; - - data = font->data; - - color = RTGUI_DC_FC(dc); - for(i=0; i<7; i++) - { - if (code & (1 << i)) - RTGUI_DC_FC(dc) = color; - else - RTGUI_DC_FC(dc) = RTGUI_DC_BC(dc); - - for (j=0; j<font->seg1_nr; j++) - { - if (data->type == DIGFONT_H) - rtgui_dc_draw_hline(dc, rect->x1 + data->x, \ - rect->x1 + data->x + data->len, rect->y1 + data->y); - else - rtgui_dc_draw_vline(dc, rect->x1 + data->x, rect->y1 + data->y, \ - rect->y1 + data->y + data->len); - - data++; - } - } - RTGUI_DC_FC(dc) = color; - - return 0; -} - -int rtgui_get_digfont_metrics(struct rtgui_digitfont * font, rtgui_rect_t * rect) -{ - int half = font->seg1_nr >> 1; - rect->x1 = 0; - rect->y1 = 0; - rect->x2 = (font->seg1_hspace << 1) + font->seg1_len; - rect->y2 = (font->seg1_vspace << 1) + font->seg2_len + font->seg3_len + (half << 1) - 1; - return 0; -} - -/* debug */ -struct rtgui_digitfont digitfont_40 = -{ - 34, //int seg1_len; - 3, //int seg1_hspace; - 1, //int seg1_vpace; - 9, //int seg1_nr; //9 - 30, //int seg2_len; - 30, //int seg3_len; - RT_NULL, -}; - -#ifdef _WIN32 -#include <windows.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -static char fontbuf[80]; -int digfont_file(char * name, struct rtgui_digitfont * font) -{ - int i; - int size; - FILE * file; - struct rtgui_digitfont_data * data; - - if (font->data == RT_NULL) - rtgui_digitfont_create(font); - - /* create a file to store data */ - file = fopen(name, "wb + "); - if (file == RT_NULL) - { - rt_kprintf("open < % s > failed.\n", name); - return -1; - } - data = font->data; - size = font->seg1_nr * 7; - - for( i=0; i<size; i++) - { - if ((i % (font->seg1_nr)) == 0) - { - sprintf(fontbuf, "\n/* seg <%d> */\n", i/font->seg1_nr + 1); - fwrite(fontbuf, strlen(fontbuf), 1, file); - } - - sprintf(fontbuf, " { % 2d, % 2d, % 2d, % 2d}, \n", data->x, data->y, data->len, data->type); - fwrite(fontbuf, strlen(fontbuf), 1, file); - data ++; - } - fclose(file); - - return 0; -} -#if defined(RT_USING_FINSH) -#include <finsh.h> -FINSH_FUNCTION_EXPORT(digfont_file, create the font file of digit font) -#endif - -#endif
\ No newline at end of file diff --git a/components/rtgui/widgets/digtube.c b/components/rtgui/widgets/digtube.c deleted file mode 100644 index e5ce5484ea..0000000000 --- a/components/rtgui/widgets/digtube.c +++ /dev/null @@ -1,174 +0,0 @@ -/* - * File : digfont.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, 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 - * 2012-12-21 pife first version - */ - -#include <rtgui/dc.h> -#include <rtgui/widgets/digtube.h> - -static void _rtgui_digtube_constructor(struct rtgui_digtube * digtube) -{ - RTGUI_WIDGET_TEXTALIGN(digtube) = RTGUI_ALIGN_CENTER; - - /* init widget and set event handler */ - rtgui_object_set_event_handler(RTGUI_OBJECT(digtube), rtgui_digtube_event_handler); -} - -static void _rtgui_digtube_destructor(struct rtgui_digtube *digtube) -{ -#ifndef RTGUI_DIGTUBE_USE_CONST_FONT - /* release font memory */ - rt_free(digtube->digitfont.data); - digtube->digitfont.data = RT_NULL; -#endif -} - -DEFINE_CLASS_TYPE(digtube, "digtube", - RTGUI_WIDGET_TYPE, - _rtgui_digtube_constructor, - _rtgui_digtube_destructor, - sizeof(struct rtgui_digtube)); - -rt_bool_t rtgui_digtube_event_handler(struct rtgui_object *object, struct rtgui_event *event) -{ - struct rtgui_digtube *digtube; - struct rtgui_dc *dc; - rtgui_rect_t rect; - rtgui_rect_t text_rect; - rtgui_color_t color; - char * disbuf; - char tempbuf[8]; - int i; - - RTGUI_WIDGET_EVENT_HANDLER_PREPARE - - digtube = RTGUI_DIGTUBE(object); - switch (event->type) - { - case RTGUI_EVENT_PAINT: - - dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(object)); - if (dc == RT_NULL) - break; - rtgui_widget_get_rect(RTGUI_WIDGET(object), &rect); - rtgui_dc_fill_rect(dc, &rect); - - if (! (digtube->tube_style & RTGUI_DIGTUBE_STYLE_NOBACKFONT)) - { - color = RTGUI_DC_BC(dc); - RTGUI_DC_BC(dc) = digtube->digit_bc; - } - - if (digtube->tube_style & RTGUI_DIGTUBE_STYLE_DISCODES) - disbuf = (char *) (digtube->value); - else - { - const char * format = - digtube->tube_style & RTGUI_DIGTUBE_STYLE_DISHEXNUM ? - "%7x" : "%7d"; - - disbuf = &tempbuf[0]; - rt_snprintf(disbuf, 8, format, digtube->value); - - /* */ - for (i=0; i<7; i++) - { - if (disbuf[i] == ' ') - disbuf[i] = 0; - else - { - disbuf[i] = (disbuf[i] >= '0' && disbuf[i] <= '9') ? disbuf[i] - '0': - disbuf[i] - 'a' + 10; - - disbuf[i] = digtube_code_table[disbuf[i]]; - } - } - - disbuf = tempbuf + 7 - digtube->tube_count; - } - - text_rect.x1 = 0; - text_rect.y1 = 0; - text_rect.x2 = (digtube->digit_width + digtube->digit_space) * digtube->tube_count - -digtube->digit_space; - text_rect.y2 = digtube->digit_hight; - - rtgui_rect_moveto_align(&rect, &text_rect, RTGUI_DC_TEXTALIGN(dc)); - for (i=0; i<digtube->tube_count; i++) - { - rtgui_dc_draw_digitfont_code(dc, &digtube->digitfont, &text_rect, disbuf[i]); - text_rect.x1 += digtube->digit_width + digtube->digit_space; - } - - if (! (digtube->tube_style & RTGUI_DIGTUBE_STYLE_NOBACKFONT)) - RTGUI_DC_BC(dc) = color; - rtgui_dc_end_drawing(dc); - break; - default: - return rtgui_widget_event_handler(object, event); - } - - return RT_FALSE; -} - -rtgui_digtube_t *rtgui_digtube_create( - struct rtgui_digitfont * digitfont, - int count, - void * value, - int style) -{ - struct rtgui_digtube *digtube; - rtgui_rect_t rect; - - RT_ASSERT(count <= 7 && count > 0) - - digtube = (struct rtgui_digtube *) rtgui_widget_create(RTGUI_DIGTUBE_TYPE); - if (digtube == RT_NULL) - return RT_NULL; - - /* set field */ - if (digitfont == RT_NULL) - digitfont = &digitfont_40; - - rt_memcpy(& digtube->digitfont, digitfont, sizeof(struct rtgui_digitfont)); - -#ifndef RTGUI_DIGTUBE_USE_CONST_FONT - if (digtube->digitfont.data == RT_NULL) - rtgui_digitfont_create(& digtube->digitfont); -#endif - - /* set default rect */ - rtgui_get_digfont_metrics(&digtube->digitfont, &rect); - digtube->digit_width = rect.x2; - digtube->digit_hight = rect.y2; - digtube->tube_count = count; - digtube->digit_space = RTGUI_DIGTUBE_DEFAULT_SPACE; - - rect.x2 = (rect.x2 + digtube->digit_space) * count - digtube->digit_space; - rect.y2 = rect.y2; - - RTGUI_WIDGET_BACKGROUND(digtube) = rtgui_theme_default_bc(); - RTGUI_WIDGET_FOREGROUND(digtube) = RTGUI_DIGTUBE_DEFAULT_FC; - rtgui_widget_set_rect(RTGUI_WIDGET(digtube), &rect); - - /* set display value */ - digtube->digit_bc = RTGUI_DIGTUBE_DEFAULT_DIGIT_BC; - digtube->value = value; - digtube->tube_style = style; - - return digtube; -} - -void rtgui_digtube_destroy(rtgui_digtube_t *digtube) -{ - rtgui_widget_destroy(RTGUI_WIDGET(digtube)); -}
\ No newline at end of file diff --git a/components/rtgui/widgets/edit.c b/components/rtgui/widgets/edit.c deleted file mode 100644 index d8e1effe3d..0000000000 --- a/components/rtgui/widgets/edit.c +++ /dev/null @@ -1,1959 +0,0 @@ -/* - * File : edit.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, 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 - * 2012-06-04 amsl firist version. - * 2012-08-09 amsl beta 0.1 - */ -#include <rtgui/dc.h> -#include <rtgui/widgets/edit.h> -#include <rtgui/widgets/scrollbar.h> -#include <rtgui/rtgui_system.h> -#include <rtgui/filerw.h> - -extern int isprint(unsigned char ch); /* Quote from shell.c */ -static void rtgui_edit_draw_caret(struct rtgui_edit *edit); -static void rtgui_edit_timeout(struct rtgui_timer *timer, void *parameter); -static rt_bool_t rtgui_edit_onfocus(struct rtgui_object *object, rtgui_event_t *event); -static rt_bool_t rtgui_edit_onunfocus(struct rtgui_object *object, rtgui_event_t *event); -#ifdef RTGUI_EDIT_USING_SCROLL -static rt_bool_t rtgui_edit_hscroll_handle(struct rtgui_widget *widget, rtgui_event_t *event); -static rt_bool_t rtgui_edit_vscroll_handle(struct rtgui_widget *widget, rtgui_event_t *event); -#endif - -void _rtgui_edit_constructor(struct rtgui_edit *edit) -{ - rtgui_rect_t font_rect; - RTGUI_WIDGET_FLAG(edit) |= RTGUI_WIDGET_FLAG_FOCUSABLE; - - rtgui_object_set_event_handler(RTGUI_OBJECT(edit), rtgui_edit_event_handler); - rtgui_widget_set_onfocus(RTGUI_WIDGET(edit), rtgui_edit_onfocus); - rtgui_widget_set_onunfocus(RTGUI_WIDGET(edit), rtgui_edit_onunfocus); - - RTGUI_WIDGET_FOREGROUND(edit) = black; - RTGUI_WIDGET_BACKGROUND(edit) = white; - /* set default text align */ - RTGUI_WIDGET_TEXTALIGN(edit) = RTGUI_ALIGN_CENTER_VERTICAL; - rtgui_widget_set_border(RTGUI_WIDGET(edit), RTGUI_BORDER_SUNKEN); - /* set proper of control */ - edit->caret_timer = RT_NULL; - edit->caret = RT_NULL; - - edit->tabsize = 4; - edit->margin = 1; - edit->max_rows = edit->max_cols = 0; - edit->visual.x = edit->visual.y = 0; - edit->upleft.x = edit->upleft.y = 0; - edit->row_per_page = edit->col_per_page = 0; - - edit->update_buf = RT_NULL; - edit->flag = RTGUI_EDIT_NONE; -#ifdef RTGUI_EDIT_USING_SCROLL - edit->flag |= RTGUI_EDIT_VSCROLL; - edit->flag |= RTGUI_EDIT_HSCROLL; -#endif - /* allocate default line buffer */ - edit->bzsize = 16; - - rtgui_font_get_metrics(RTGUI_WIDGET_FONT(edit), "H", &font_rect); - edit->font_width = rtgui_rect_width(font_rect); - edit->font_height = rtgui_rect_height(font_rect); - - edit->dbl_buf = rtgui_dc_buffer_create(edit->font_width * 2 + 1, edit->font_height + 1); - - edit->head = RT_NULL; - edit->tail = RT_NULL; - edit->first_line = RT_NULL; -#ifdef RTGUI_EDIT_USING_SCROLL - edit->hscroll = RT_NULL; - edit->vscroll = RT_NULL; -#endif -} - -void _rtgui_edit_deconstructor(struct rtgui_edit *edit) -{ - if (edit->max_rows > 0) - { - while (edit->max_rows > 0) - rtgui_edit_delete_line(edit, edit->head); - edit->max_rows = 0; - } - if (edit->caret_timer != RT_NULL) - rtgui_timer_destory(edit->caret_timer); - edit->caret_timer = RT_NULL; - - if (edit->caret != RT_NULL) - rtgui_free(edit->caret); - edit->caret = RT_NULL; - if (edit->update_buf != RT_NULL) - rtgui_free(edit->update_buf); - - rtgui_dc_destory(edit->dbl_buf); -} - -DEFINE_CLASS_TYPE(edit, "edit", - RTGUI_CONTAINER_TYPE, - _rtgui_edit_constructor, - _rtgui_edit_deconstructor, - sizeof(struct rtgui_edit)); - -#ifdef RTGUI_EDIT_USING_SCROLL -void rtgui_edit_adjust_scroll(rtgui_scrollbar_t *bar) -{ - struct rtgui_edit *edit; - - RT_ASSERT(bar != RT_NULL); - - if (bar->widget_link != RT_NULL) - { - rtgui_rect_t rect; - rt_uint32_t _left = 0, _top = 0, _width = RTGUI_DEFAULT_SB_WIDTH, _len = 0; - - edit = bar->widget_link; - rtgui_widget_get_rect(edit, &rect); - rtgui_widget_rect_to_device(edit, &rect); - if (bar->orient == RTGUI_HORIZONTAL) - { - if (RTGUI_WIDGET_IS_HIDE(edit->hscroll)) - { - if (edit->max_rows > edit->row_per_page) - { - RTGUI_WIDGET_SHOW(edit->hscroll); - rtgui_scrollbar_set_line_step(edit->hscroll, 1); - rtgui_scrollbar_set_page_step(edit->hscroll, edit->row_per_page); - rtgui_scrollbar_set_range(edit->hscroll, edit->max_rows); - } - else - RTGUI_WIDGET_HIDE(edit->vscroll); - rtgui_widget_update_clip(RTGUI_WIDGET(edit)); - } - else - { - _left = RTGUI_WIDGET_BORDER(edit); - _top = rtgui_rect_height(rect) - RTGUI_WIDGET_BORDER(edit) - _width; - _len = rtgui_rect_width(rect) - RTGUI_WIDGET_BORDER(edit) * 2; - - if (!RTGUI_WIDGET_IS_HIDE(edit->vscroll)) - _len -= _width; - rect.x1 += _left; - rect.y1 += _top; - rect.x2 = rect.x1 + _len; - rect.y2 = rect.y1 + _width; - } - } - else if (bar->orient == RTGUI_VERTICAL) - { - _left = rtgui_rect_width(rect) - RTGUI_WIDGET_BORDER(edit) - _width; - _top = RTGUI_WIDGET_BORDER(edit); - _len = rtgui_rect_height(rect) - RTGUI_WIDGET_BORDER(edit) * 2; - - if (!RTGUI_WIDGET_IS_HIDE(edit->hscroll)) - _len -= _width; - rect.x1 += _left; - rect.y1 += _top; - rect.x2 = rect.x1 + _width; - rect.y2 = rect.y1 + _len; - } - rtgui_widget_set_rect(bar, &rect); - } -} -RTM_EXPORT(rtgui_edit_adjust_scroll); -#endif - -struct rtgui_edit *rtgui_edit_create(struct rtgui_container *container, int left, int top, int w, int h) -{ - struct rtgui_edit *edit; - - RT_ASSERT(container != RT_NULL); - - edit = (struct rtgui_edit *)rtgui_widget_create(RTGUI_EDIT_TYPE); - if (edit != RT_NULL) - { - rtgui_rect_t rect; - int effe; - rtgui_widget_get_rect(RTGUI_WIDGET(container), &rect); - rtgui_widget_rect_to_device(RTGUI_WIDGET(container), &rect); - rect.x1 += left; - rect.y1 += top; - rect.x2 = rect.x1 + w; - rect.y2 = rect.y1 + h; - rtgui_widget_set_rect(RTGUI_WIDGET(edit), &rect); - rtgui_container_add_child(container, RTGUI_WIDGET(edit)); - - /* set character number */ - edit->item_height = edit->font_height; /* the same height */ - effe = h - (edit->margin + RTGUI_WIDGET_BORDER(edit)) * 2; - edit->row_per_page = effe / edit->item_height; - if (effe % edit->item_height) - edit->row_per_page += 1; - - effe = w - (edit->margin + RTGUI_WIDGET_BORDER(edit)) * 2; - edit->col_per_page = effe / edit->font_width; - if (effe % edit->font_width) - edit->col_per_page += 1; - edit->update_buf = rtgui_malloc(edit->col_per_page + 1); - -#ifdef RTGUI_EDIT_USING_SCROLL - if (edit->hscroll == RT_NULL && edit->flag & RTGUI_EDIT_HSCROLL) - { - /* create horizontal scrollbar */ - rt_uint32_t _left, _top, _width = RTGUI_DEFAULT_SB_WIDTH, _len; - _left = RTGUI_WIDGET_BORDER(edit); - _top = rtgui_rect_height(rect) - RTGUI_WIDGET_BORDER(edit) - _width; - _len = rtgui_rect_width(rect) - RTGUI_WIDGET_BORDER(edit) * 2; - if (edit->max_rows > edit->row_per_page) _len -= _width; - - edit->hscroll = rtgui_scrollbar_create(edit, _left, _top, _width, _len, RTGUI_HORIZONTAL); - - if (edit->hscroll != RT_NULL) - { - edit->hscroll->widget_link = (pvoid)edit; - edit->hscroll->on_scroll = rtgui_edit_hscroll_handle; - RTGUI_WIDGET_HIDE(edit->hscroll); - } - } - if (edit->vscroll == RT_NULL && edit->flag & RTGUI_EDIT_VSCROLL) - { - /* create vertical scrollbar */ - rt_uint32_t _left, _top, _width = RTGUI_DEFAULT_SB_WIDTH, _len; - _left = rtgui_rect_width(rect) - RTGUI_WIDGET_BORDER(edit) - _width; - _top = RTGUI_WIDGET_BORDER(edit); - _len = rtgui_rect_height(rect) - RTGUI_WIDGET_BORDER(edit) * 2; - if (edit->max_cols > edit->col_per_page) _len -= _width; - - edit->vscroll = rtgui_scrollbar_create(edit, _left, _top, _width, _len, RTGUI_VERTICAL); - - if (edit->vscroll != RT_NULL) - { - edit->vscroll->widget_link = (pvoid)edit; - edit->vscroll->on_scroll = rtgui_edit_vscroll_handle; - RTGUI_WIDGET_HIDE(edit->vscroll); - } - } -#endif - } - - return edit; -} -RTM_EXPORT(rtgui_edit_create); - -void rtgui_edit_destroy(struct rtgui_edit *edit) -{ - rtgui_widget_destroy(RTGUI_WIDGET(edit)); -} -RTM_EXPORT(rtgui_edit_destroy); - -/** - * calc line buffer alloc length - * - * @param n a standard buffer value, please use edit->bzsize - * @param m given a reference value - * - * @return get a proper standard values - */ -rt_inline rt_int16_t rtgui_edit_alloc_len(rt_int16_t n, rt_int16_t m) -{ - if (n > m) return n; -#ifndef RTGUI_USING_SMALL_SIZE - return rtgui_edit_alloc_len(n * 2, m); -#else - return rtgui_edit_alloc_len(n + 16, m); -#endif -} - -/** - * please use it to replace rt_strlen - * especially in reading the source file. - */ -rt_inline rt_int16_t rtgui_edit_line_strlen(const char *s) -{ - const char *sc; - /* ascii text end of 0x0A or 0x0D-0x0A*/ - for (sc = s; *sc != 0x0D && *sc != 0x0A && *sc != 0x00; ++sc); - return sc - s; -} - -rt_bool_t rtgui_edit_append_line(struct rtgui_edit *edit, const char *text) -{ - rt_int16_t len; - struct edit_line *line, *node; - - RT_ASSERT(edit != RT_NULL); - - line = rtgui_malloc(sizeof(struct edit_line)); - if (line == RT_NULL) return RT_FALSE; - - len = rtgui_edit_line_strlen(text); - line->zsize = rtgui_edit_alloc_len(edit->bzsize, len + 1); - line->text = rtgui_malloc(line->zsize); - rt_memcpy(line->text, text, len); - *(line->text + len) = '\0'; - line->len = rtgui_edit_line_strlen(line->text); - - line->next = RT_NULL; - edit->max_rows++; - if (edit->max_cols < len) edit->max_cols = len; - - node = edit->head; - if (node == RT_NULL) - { - edit->head = line; - edit->tail = line; - line->prev = RT_NULL; - edit->first_line = line; - return RT_TRUE; - } - while (node->next != RT_NULL) node = node->next; - /* to tail item on to queue */ - node->next = line; - line->prev = node; - /* re-fixed position tail */ - edit->tail = line; - - return RT_TRUE; -} -RTM_EXPORT(rtgui_edit_append_line); - -rt_bool_t rtgui_edit_insert_line(struct rtgui_edit *edit, struct edit_line *p, char *text) -{ - rt_int16_t len; - struct edit_line *line; - - RT_ASSERT(edit != RT_NULL); - RT_ASSERT(p != RT_NULL); - - if (p->next == RT_NULL) - { - rtgui_edit_append_line(edit, text); - return RT_TRUE; - } - - line = rtgui_malloc(sizeof(struct edit_line)); - if (line == RT_NULL) return RT_FALSE; - - line->prev = p; - line->next = p->next; - p->next = line; - if (line->next != RT_NULL) - { - line->next->prev = line; - } - - len = rtgui_edit_line_strlen(text); - line->zsize = rtgui_edit_alloc_len(edit->bzsize, len + 1); - - line->text = rtgui_malloc(line->zsize); - rt_memset(line->text, 0, line->zsize); - rt_memcpy(line->text, text, len); - *(line->text + len) = '\0'; - - edit->max_rows ++; - line->len = rtgui_edit_line_strlen(line->text); - - return RT_TRUE; -} -RTM_EXPORT(rtgui_edit_insert_line); - -rt_bool_t rtgui_edit_delete_line(struct rtgui_edit *edit, struct edit_line *line) -{ - RT_ASSERT(edit != RT_NULL); - RT_ASSERT(line != RT_NULL); - - if (edit->max_rows == 0) return RT_FALSE; - - if (line->prev == RT_NULL) - { - if (line->next == RT_NULL) - { - /* only one item */ - edit->head = RT_NULL; - edit->tail = RT_NULL; - } - else - { - /* first item */ - line->next->prev = RT_NULL; - edit->head = line->next; - } - } - else - { - if (line->next == RT_NULL) - { - /* last item */ - line->prev->next = RT_NULL; - edit->tail = line->prev; - } - else - { - /* middle item */ - line->prev->next = line->next; - line->next->prev = line->prev; - } - } - - if (edit->max_rows > 0)edit->max_rows--; - if (line->text) - { - rtgui_free(line->text); - line->text = RT_NULL; - } - rtgui_free(line); - line = RT_NULL; - - return RT_TRUE; -} -RTM_EXPORT(rtgui_edit_delete_line); - -rt_bool_t rtgui_edit_connect_line(struct rtgui_edit *edit, struct edit_line *line, struct edit_line *connect) -{ - rt_int16_t len1, len2; - - RT_ASSERT(edit != RT_NULL); - RT_ASSERT(line != RT_NULL); - RT_ASSERT(connect != RT_NULL); - - len1 = rtgui_edit_line_strlen(line->text); - len2 = rtgui_edit_line_strlen(connect->text); - - line->zsize = rtgui_edit_alloc_len(edit->bzsize, len1 + len2 + 1); - line->text = rt_realloc(line->text, line->zsize); - rt_memcpy(line->text + len1, connect->text, len2); - *(line->text + len1 + len2) = '\0'; - - line->len = rtgui_edit_line_strlen(line->text); - return RT_TRUE; -} -RTM_EXPORT(rtgui_edit_connect_line); - -static void rtgui_edit_get_caret_rect(struct rtgui_edit *edit, rtgui_rect_t *rect, rtgui_point_t visual) -{ - RT_ASSERT(edit != RT_NULL); - - rtgui_widget_get_rect(RTGUI_WIDGET(edit), rect); - - rect->x1 += visual.x * edit->font_width + RTGUI_WIDGET_BORDER(edit) + edit->margin; - rect->x2 = rect->x1 + 1; /* caret width: 1 */ - rect->y1 += visual.y * edit->item_height + RTGUI_WIDGET_BORDER(edit) + edit->margin; - if ((rect->y1 + edit->font_height) < (rect->y2 - RTGUI_WIDGET_BORDER(edit) - edit->margin)) - rect->y2 = rect->y1 + edit->font_height; - else - rect->y2 = rect->y2 - RTGUI_WIDGET_BORDER(edit) - edit->margin; -} - -static void rtgui_edit_init_caret(struct rtgui_edit *edit, rtgui_point_t visual) -{ - struct rtgui_graphic_driver *hw_driver = rtgui_graphic_driver_get_default(); - int x, y; - rtgui_color_t color; - rtgui_rect_t rect; - int ofs = 0; - - RT_ASSERT(edit != RT_NULL); - if (!RTGUI_WIDGET_IS_FOCUSED(edit)) return; - - rtgui_edit_get_caret_rect(edit, &edit->caret_rect, visual); - rect = edit->caret_rect; - rtgui_widget_rect_to_device(RTGUI_WIDGET(edit), &rect); - - if (edit->caret == RT_NULL) - edit->caret = (rtgui_color_t *)rtgui_malloc(rtgui_rect_width(rect) * rtgui_rect_height(rect) * sizeof(rtgui_color_t)); - rtgui_timer_stop(edit->caret_timer); - - for (x = rect.x1; x < rect.x2; x++) - { - for (y = rect.y1; y < rect.y2; y++) - { - hw_driver->ops->get_pixel(&color, x, y); - *(edit->caret + ofs++) = color; - } - } - - rtgui_timer_start(edit->caret_timer); -} - -/* draw caret */ -static void rtgui_edit_draw_caret(struct rtgui_edit *edit) -{ - int x, y; - rtgui_color_t color; - rtgui_rect_t rect; - int ofs = 0; - struct rtgui_dc *dc; - - RT_ASSERT(edit != RT_NULL); - if (edit->caret == RT_NULL) return; - - dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(edit)); - if (dc == RT_NULL)return; - - rect = edit->caret_rect; - - for (x = rect.x1; x < rect.x2; x++) - { - for (y = rect.y1; y < rect.y2; y++) - { - color = *(edit->caret + ofs); - ofs++; - if (edit->flag & RTGUI_EDIT_CARET) - { - color = ~color; - rtgui_dc_draw_color_point(dc, x, y, color); - } - else - { - rtgui_dc_draw_color_point(dc, x, y, color); - } - } - } - - rtgui_dc_end_drawing(dc); -} - -static void rtgui_edit_timeout(struct rtgui_timer *timer, void *parameter) -{ - struct rtgui_edit *edit; - - edit = RTGUI_EDIT(parameter); - /* set caret flag */ - if (edit->flag & RTGUI_EDIT_CARET) - edit->flag &= ~RTGUI_EDIT_CARET; - else - edit->flag |= RTGUI_EDIT_CARET; - - rtgui_edit_draw_caret(edit); - - return; -} - -struct edit_line *rtgui_edit_get_line_by_index(struct rtgui_edit *edit, rt_uint32_t index) -{ - struct edit_line *line = RT_NULL; - - RT_ASSERT(edit != RT_NULL); - - if (edit->head != RT_NULL) - { - int i = 0; - line = edit->first_line; - while (line) - { - if ((edit->upleft.y + i++) == index) - break; - line = line->next; - } - } - return line; -} -RTM_EXPORT(rtgui_edit_get_line_by_index); - -rt_uint32_t rtgui_edit_get_index_by_line(struct rtgui_edit *edit, struct edit_line *line) -{ - rt_uint32_t index = 0; - struct edit_line *tmp; - - RT_ASSERT(edit != RT_NULL); - RT_ASSERT(line != RT_NULL); - - if (edit->head != RT_NULL) - { - tmp = edit->first_line; - index = edit->upleft.y; - while (tmp) - { - if (tmp == line) - break; - index ++; - tmp = tmp->next; - } - } - return index; -} -RTM_EXPORT(rtgui_edit_get_index_by_line); - -enum -{ - EDIT_IDENT_DIR_BOTH, - EDIT_IDENT_DIR_LEFT, - EDIT_IDENT_DIR_RIGHT -}; -/* -* identify a byte is double byte -* @param dir set direction. -* @param *p record the position of the effective. -* @return RT_TRUE is Got it, else not found. -*/ -static rt_bool_t identify_double_byte(struct rtgui_edit *edit, struct edit_line *line, - rt_uint32_t dir, rt_int16_t *p) -{ - int index, effe_nums; - - RT_ASSERT(edit != RT_NULL); - RT_ASSERT(line != RT_NULL); - - if (dir == EDIT_IDENT_DIR_BOTH) - { - } - else if (dir == EDIT_IDENT_DIR_LEFT) - { - if (edit->upleft.x == 0 && edit->visual.x == 0) - return RT_FALSE; - index = edit->upleft.x + edit->visual.x; - effe_nums = 0; - while (index--) - { - if ((rt_uint8_t)(*(line->text + index)) >= 0x80) - effe_nums ++; - else - break; - } - if (effe_nums > 0) - { - *p = 2 - effe_nums % 2; - return RT_TRUE; - } - } - else if (dir == EDIT_IDENT_DIR_RIGHT) - { - if (edit->upleft.x + edit->visual.x == line->len) - return RT_FALSE; - index = edit->upleft.x + edit->visual.x; - effe_nums = 0; - while (index < line->len) - { - if ((rt_uint8_t)(*(line->text + index)) >= 0x80) - effe_nums ++; - else - break; - index ++; - } - if (effe_nums > 0) - { - *p = 2 - effe_nums % 2; - return RT_TRUE; - } - } - return RT_FALSE; -} - -static void rtgui_edit_onmouse(struct rtgui_edit *edit, struct rtgui_event_mouse *emouse) -{ - rtgui_rect_t rect; - - RT_ASSERT(edit != RT_NULL); - RT_ASSERT(emouse != RT_NULL); - - rtgui_widget_get_rect(RTGUI_WIDGET(edit), &rect); - if ((rtgui_region_contains_point(&(RTGUI_WIDGET(edit)->clip), emouse->x, emouse->y, &rect) == RT_EOK)) - { - rt_uint16_t x, y; - - /* multiline text */ - x = (emouse->x - rect.x1) / (edit->font_width); - y = (emouse->y - rect.y1) / (edit->item_height); - if ((x < edit->col_per_page) && (y < edit->row_per_page)) - { - if (emouse->button & RTGUI_MOUSE_BUTTON_DOWN) - { - struct edit_line *line; - rt_int16_t tmp_pos = 0; - - edit->visual.x = x; - edit->visual.y = y; - - line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->visual.y); - if (line == RT_NULL) - return; - - if (edit->visual.x > line->len) - edit->visual.x = line->len; - if (edit->upleft.x > 0) - { - if (edit->upleft.x >= line->len) - edit->upleft.x = 0; - else - edit->visual.x -= edit->upleft.x; - rtgui_edit_ondraw(edit); - } - if (identify_double_byte(edit, line, EDIT_IDENT_DIR_LEFT, &tmp_pos)) - edit->visual.x -= (2 - tmp_pos); - if (edit->flag & RTGUI_EDIT_CARET) - { - if (edit->caret_timer != RT_NULL) - rtgui_timer_stop(edit->caret_timer); - - edit->flag &= ~RTGUI_EDIT_CARET; - rtgui_edit_draw_caret(edit); - - if (edit->caret_timer != RT_NULL) - rtgui_timer_start(edit->caret_timer); - } - - /* set widget focus */ - rtgui_widget_focus(RTGUI_WIDGET(edit)); - - if (RTGUI_WIDGET_IS_FOCUSED(edit)) - { - rtgui_edit_init_caret(edit, edit->visual); - edit->flag |= RTGUI_EDIT_CARET; - rtgui_edit_draw_caret(edit); - } - } - else if (emouse->button & RTGUI_MOUSE_BUTTON_UP) - { - /* please add codes at here. */ - } -#ifdef RTGUI_EDIT_USING_SCROLL - if (edit->vscroll && !RTGUI_WIDGET_IS_HIDE(edit)) - { - if (!RTGUI_WIDGET_IS_HIDE(edit->vscroll)) - rtgui_scrollbar_set_value(edit->vscroll, edit->upleft.y); - } - if (edit->hscroll && !RTGUI_WIDGET_IS_HIDE(edit)) - { - if (!RTGUI_WIDGET_IS_HIDE(edit->hscroll)) - rtgui_scrollbar_set_value(edit->hscroll, edit->upleft.x); - } -#endif - } - } -} - -rt_inline rt_uint16_t query_shift_code(rt_uint16_t key) -{ - if (key >= RTGUIK_a && key <= RTGUIK_z) - return (key - ('a' - 'A')); - else - { - switch (key) - { - case '1': - return '!'; - case '2': - return '@'; - case '3': - return '#'; - case '4': - return '$'; - case '5': - return '%'; - case '6': - return '^'; - case '7': - return '&'; - case '8': - return '*'; - case '9': - return '('; - case '0': - return ')'; - case '-': - return '_'; - case '=': - return '+'; - case '\\': - return '|'; - case ';': - return ':'; - case '\'': - return '"'; - case ',': - return '<'; - case '.': - return '>'; - case '/': - return '?'; - case '`': - return '~'; - } - } - return key; -} - -rt_inline rt_uint16_t query_caps_code(rt_uint16_t key) -{ - if (key >= RTGUIK_a && key <= RTGUIK_z) - return (key - ('a' - 'A')); - return key; -} - -rt_inline rt_bool_t is_small_keyboard(rt_uint16_t *key) -{ - if (*key >= RTGUIK_KP0 && *key <= RTGUIK_KP9) - { - *key = *key - (RTGUIK_KP0 - RTGUIK_0); - return RT_TRUE; - } - else if (*key == RTGUIK_KP_PERIOD) - { - *key = '.'; - return RT_TRUE; - } - else if (*key == RTGUIK_KP_DIVIDE) - { - *key = '/'; - return RT_TRUE; - } - else if (*key == RTGUIK_KP_MULTIPLY) - { - *key = '*'; - return RT_TRUE; - } - else if (*key == RTGUIK_KP_MINUS) - { - *key = '-'; - return RT_TRUE; - } - else if (*key == RTGUIK_KP_PLUS) - { - *key = '+'; - return RT_TRUE; - } - else if (*key == RTGUIK_KP_ENTER) - { - *key = RTGUIK_RETURN; - return RT_TRUE; - } - return RT_FALSE; -} - -void kbd_event_set_key(struct rtgui_event_kbd *ekbd, rt_uint16_t key) -{ - RTGUI_EVENT_KBD_INIT(ekbd); - ekbd->mod = RTGUI_KMOD_NONE; - ekbd->unicode = 0; - - ekbd->key = key; - ekbd->type = RTGUI_KEYDOWN; -} - -static rt_bool_t rtgui_edit_onkey(struct rtgui_object *object, rtgui_event_t *event) -{ - enum { EDIT_NONE, EDIT_ONDRAW, EDIT_UPDATE }; - struct rtgui_edit *edit = RTGUI_EDIT(object); - struct rtgui_event_kbd *ekbd = (struct rtgui_event_kbd *)event; - struct edit_line *line = RT_NULL; - rt_bool_t update_type = EDIT_NONE; - - RT_ASSERT(edit != RT_NULL); - RT_ASSERT(ekbd != RT_NULL); - - if (RTGUI_KBD_IS_UP(ekbd)) - { - /* reset function key */ - if (ekbd->key == RTGUIK_RCTRL || ekbd->key == RTGUIK_LCTRL) - edit->flag &= ~RTGUI_EDIT_CTRL; - else if (ekbd->key == RTGUIK_RALT || ekbd->key == RTGUIK_LALT) - edit->flag &= ~RTGUI_EDIT_ALT; - else if (ekbd->key == RTGUIK_RSHIFT || ekbd->key == RTGUIK_LSHIFT) - edit->flag &= ~RTGUI_EDIT_SHIFT; - else if (ekbd->key == RTGUIK_CAPSLOCK) - edit->flag &= ~RTGUI_EDIT_CAPSLOCK; - else if (ekbd->key == RTGUIK_NUMLOCK) - edit->flag &= ~RTGUI_EDIT_NUMLOCK; - return RT_TRUE; - } - - line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->visual.y); - if (line == RT_NULL) - return RT_FALSE; - - /* rt_kprintf("key=%04X ",ekbd->key); */ - if (ekbd->key == RTGUIK_RCTRL || ekbd->key == RTGUIK_LCTRL) - { - /* use CTRL key */ - edit->flag |= RTGUI_EDIT_CTRL; - return RT_FALSE; - } - else if (ekbd->key == RTGUIK_RALT || ekbd->key == RTGUIK_LALT) - { - /* use ALT key */ - edit->flag |= RTGUI_EDIT_ALT; - return RT_FALSE; - } - else if (ekbd->key == RTGUIK_RSHIFT || ekbd->key == RTGUIK_LSHIFT) - { - /* use SHIFT key */ - edit->flag |= RTGUI_EDIT_SHIFT; - return RT_FALSE; - } - else if (ekbd->key == RTGUIK_CAPSLOCK) - { - edit->flag |= RTGUI_EDIT_CAPSLOCK; - return RT_FALSE; - } - else if (ekbd->key == RTGUIK_NUMLOCK) - { - edit->flag |= RTGUI_EDIT_NUMLOCK; - return RT_FALSE; - } - else if (ekbd->key == RTGUIK_DELETE) - { - /* delete latter character */ - int ofs = edit->upleft.x + edit->visual.x; - if (ofs > line->len - 1 || (ofs == 0 && line->len == 0)) - { - /* will the next line marges into the current line */ - struct edit_line *next_line = line->next; - if (next_line != RT_NULL) - { - struct edit_line *update_end_line; - - update_type = EDIT_UPDATE; - edit->update.start = edit->visual; - - rtgui_edit_connect_line(edit, line, next_line); - rtgui_edit_delete_line(edit, next_line); - - if (edit->max_rows - edit->upleft.y > edit->row_per_page) - { - update_end_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->row_per_page); - if (update_end_line != RT_NULL) - { - edit->update.end.x = edit->col_per_page; - edit->update.end.y = edit->upleft.y + edit->row_per_page; - } - } - else - { - int update_end_index = rtgui_edit_get_index_by_line(edit, edit->tail); - edit->update.end.x = edit->col_per_page; - edit->update.end.y = update_end_index + 1; - } - } - line->len = rtgui_edit_line_strlen(line->text); - goto _edit_exit; - } - else if (ofs == line->len - 1) - { - line->text[ofs] = '\0'; - } - else - { - char *c; - rt_int16_t tmp_pos = 1; - identify_double_byte(edit, line, EDIT_IDENT_DIR_RIGHT, &tmp_pos); - /* remove character */ - for (c = &line->text[ofs]; c[tmp_pos] != '\0'; c++) - *c = c[tmp_pos]; - *c = '\0'; - } - update_type = EDIT_UPDATE; - edit->update.start = edit->visual; - edit->update.end.x = line->len - edit->upleft.x; - if (edit->update.end.x > edit->col_per_page) - edit->update.end.x = edit->col_per_page; - edit->update.end.y = edit->visual.y; - } - else if (ekbd->key == RTGUIK_BACKSPACE) - { - if (edit->visual.x == 0) - { - /* incorporated into prev line */ - struct rtgui_event_kbd event_kbd; - struct edit_line *prev_line = line->prev; - if (prev_line != RT_NULL) - { - struct edit_line *update_end_line; - - update_type = EDIT_UPDATE; - edit->visual.x = prev_line->len; - - rtgui_edit_connect_line(edit, prev_line, line); - kbd_event_set_key(&event_kbd, RTGUIK_UP); - rtgui_edit_onkey(object, (rtgui_event_t *)&event_kbd); - rtgui_edit_delete_line(edit, line); - - edit->update.start = edit->visual; /* update.start.y is changed */ - if (edit->max_rows - edit->upleft.y > edit->row_per_page) - { - update_end_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->row_per_page); - if (update_end_line != RT_NULL) - { - edit->update.end.x = edit->col_per_page; - edit->update.end.y = edit->upleft.y + edit->row_per_page; - } - } - else - { - int update_end_index = rtgui_edit_get_index_by_line(edit, edit->tail); - edit->update.end.x = edit->col_per_page; - edit->update.end.y = update_end_index + 1; - } - } - goto _edit_exit; - } - - /* delete front character */ - if (edit->visual.x == line->len) - { - rt_int16_t tmp_pos = 1; - identify_double_byte(edit, line, EDIT_IDENT_DIR_LEFT, &tmp_pos); - line->text[edit->visual.x - tmp_pos] = '\0'; - edit->visual.x -= tmp_pos; - } - else if (edit->visual.x != 0) - { - /* remove current character */ - char *c; - rt_int16_t tmp_pos = 1; - identify_double_byte(edit, line, EDIT_IDENT_DIR_LEFT, &tmp_pos); - /* remove character */ - for (c = &line->text[edit->visual.x - tmp_pos]; c[tmp_pos] != '\0'; c++) - { - *c = c[tmp_pos]; - } - *c = '\0'; - edit->visual.x -= tmp_pos; - } - /* adjusted line buffer length */ - if (rtgui_edit_alloc_len(edit->bzsize, line->len + 2) < line->zsize) - { - line->zsize = rtgui_edit_alloc_len(edit->bzsize, line->len + 1); - line->text = rt_realloc(line->text, line->zsize); - } - update_type = EDIT_UPDATE; - edit->update.start = edit->visual; - edit->update.end.x = line->len; - edit->update.end.y = edit->visual.y; - } - else if (ekbd->key == RTGUIK_UP) - { - /* move to prev line */ - struct edit_line *prev_line; - if (edit->visual.y > 0) - edit->visual.y --; - else - { - /* change first row */ - if (edit->upleft.y > 0) - { - edit->upleft.y --; - if (edit->first_line->prev != RT_NULL) - edit->first_line = edit->first_line->prev; - update_type = EDIT_ONDRAW; - } - } - - /* The position of the recount X */ - prev_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->visual.y); - if (prev_line == RT_NULL) - return RT_FALSE; - - if (edit->upleft.x > 0) - { - if (prev_line->len <= edit->upleft.x) - { - if (prev_line->len <= edit->col_per_page) - { - edit->upleft.x = 0; - edit->visual.x = prev_line->len; - } - else - { - edit->upleft.x = prev_line->len - (edit->col_per_page - 1); - edit->visual.x = edit->col_per_page - 1; - } - update_type = EDIT_ONDRAW; - } - else if (prev_line->len - edit->upleft.x < edit->col_per_page) - { - if (edit->visual.x > prev_line->len - edit->upleft.x) - edit->visual.x = prev_line->len - edit->upleft.x; - else - { - rt_int16_t tmp_pos = 0; - if (identify_double_byte(edit, prev_line, EDIT_IDENT_DIR_LEFT, &tmp_pos)) - edit->visual.x -= (2 - tmp_pos); - } - } - } - else if (edit->visual.x > prev_line->len) - edit->visual.x = prev_line->len; - else if (prev_line->len >= 2) - { - rt_int16_t tmp_pos = 0; - if (identify_double_byte(edit, prev_line, EDIT_IDENT_DIR_LEFT, &tmp_pos)) - edit->visual.x -= (2 - tmp_pos); - } - -#ifdef RTGUI_EDIT_USING_SCROLL - /* update vscroll */ - if (edit->vscroll && !RTGUI_WIDGET_IS_HIDE(edit)) - { - if (!RTGUI_WIDGET_IS_HIDE(edit->vscroll)) - rtgui_scrollbar_set_value(edit->vscroll, edit->upleft.y); - } -#endif - } - else if (ekbd->key == RTGUIK_DOWN) - { - struct edit_line *tail_line, *next_line; - tail_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->visual.y); - if (tail_line != RT_NULL) - { - /* it is tail line */ - if (tail_line == edit->tail) return RT_FALSE; - } - /* move to next line */ - if (edit->visual.y < edit->row_per_page - 2) - { - edit->visual.y ++; - } - else if (edit->visual.y + edit->upleft.y < edit->max_rows - 1) - { - /* change first row */ - edit->upleft.y++; - if (edit->first_line->next != RT_NULL) - edit->first_line = edit->first_line->next; - update_type = EDIT_ONDRAW; - } - - /* adjust next line end position */ - next_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->visual.y); - if (next_line == RT_NULL) - return RT_FALSE; - - if (edit->upleft.x > 0) - { - if (next_line->len <= edit->upleft.x) - { - if (next_line->len <= edit->col_per_page) - { - edit->upleft.x = 0; - edit->visual.x = next_line->len; - } - else - { - edit->upleft.x = next_line->len - (edit->col_per_page - 1); - edit->visual.x = edit->col_per_page - 1; - } - update_type = EDIT_ONDRAW; - } - else if (next_line->len - edit->upleft.x < edit->col_per_page) - { - if (edit->visual.x > next_line->len - edit->upleft.x) - edit->visual.x = next_line->len - edit->upleft.x; - else - { - rt_int16_t tmp_pos = 0; - if (identify_double_byte(edit, next_line, EDIT_IDENT_DIR_LEFT, &tmp_pos)) - edit->visual.x -= (2 - tmp_pos); - } - } - } - else if (edit->visual.x > next_line->len) - edit->visual.x = next_line->len; - else if (next_line->len >= 2) - { - rt_int16_t tmp_pos = 0; - if (identify_double_byte(edit, next_line, EDIT_IDENT_DIR_LEFT, &tmp_pos)) - edit->visual.x -= (2 - tmp_pos); - } - -#ifdef RTGUI_EDIT_USING_SCROLL - /* update vscroll */ - if (edit->vscroll && !RTGUI_WIDGET_IS_HIDE(edit)) - { - if (!RTGUI_WIDGET_IS_HIDE(edit->vscroll)) - rtgui_scrollbar_set_value(edit->vscroll, edit->upleft.y); - } -#endif - } - else if (ekbd->key == RTGUIK_LEFT) - { - /* move to prev char */ - if (edit->visual.x > 0) - { - rt_int16_t tmp_pos = 1; - identify_double_byte(edit, line, EDIT_IDENT_DIR_LEFT, &tmp_pos); - edit->visual.x -= tmp_pos; - if (edit->visual.x == -1) - { - edit->visual.x = 0; - edit->upleft.x --; - update_type = EDIT_ONDRAW; - } - } - else - { - if (edit->upleft.x > 0) - { - rt_int16_t tmp_pos = 1; - identify_double_byte(edit, line, EDIT_IDENT_DIR_LEFT, &tmp_pos); - edit->upleft.x -= tmp_pos; - update_type = EDIT_ONDRAW; - } - else - { - struct rtgui_event_kbd event_kbd; - struct edit_line *first_line; - first_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->visual.y); - if (first_line != RT_NULL) - { - /* it is head line */ - if (first_line == edit->head) return RT_FALSE; - } - /* move the caret to the prev line end */ - kbd_event_set_key(&event_kbd, RTGUIK_UP); - rtgui_edit_onkey(object, (rtgui_event_t *)&event_kbd); - kbd_event_set_key(&event_kbd, RTGUIK_END); - rtgui_edit_onkey(object, (rtgui_event_t *)&event_kbd); - } - } - } - else if (ekbd->key == RTGUIK_RIGHT) - { - /* move to next char */ - if (line->len >= edit->col_per_page) - { - if (edit->upleft.x + edit->col_per_page <= line->len) - { - if (edit->visual.x < edit->col_per_page - 1) - { - rt_int16_t tmp_pos = 1; - identify_double_byte(edit, line, EDIT_IDENT_DIR_RIGHT, &tmp_pos); - edit->visual.x += tmp_pos; - } - else if (edit->visual.x == edit->col_per_page - 1) - { - if (edit->upleft.x + edit->col_per_page < line->len) - edit->upleft.x ++; - else - edit->upleft.x = line->len - edit->col_per_page + 1; - update_type = EDIT_ONDRAW; - } - } - else - { - struct rtgui_event_kbd event_kbd; - /* move to next head */ - kbd_event_set_key(&event_kbd, RTGUIK_DOWN); - rtgui_edit_onkey(object, (rtgui_event_t *)&event_kbd); - kbd_event_set_key(&event_kbd, RTGUIK_HOME); - rtgui_edit_onkey(object, (rtgui_event_t *)&event_kbd); - } - } - else - { - if (edit->visual.x < line->len) - { - rt_int16_t tmp_pos = 1; - identify_double_byte(edit, line, EDIT_IDENT_DIR_RIGHT, &tmp_pos); - edit->visual.x += tmp_pos; - } - else - { - struct rtgui_event_kbd event_kbd; - struct edit_line *tail_line; - tail_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->visual.y); - if (tail_line != RT_NULL) - { - /* it is tail line */ - if (tail_line == edit->tail) return RT_FALSE; - } - /* move the caret to the next line head */ - kbd_event_set_key(&event_kbd, RTGUIK_DOWN); - rtgui_edit_onkey(object, (rtgui_event_t *)&event_kbd); - kbd_event_set_key(&event_kbd, RTGUIK_HOME); - rtgui_edit_onkey(object, (rtgui_event_t *)&event_kbd); - } - } - } - else if (ekbd->key == RTGUIK_HOME) - { - /* move cursor to line head */ - edit->visual.x = 0; - if (edit->upleft.x > 0) - { - edit->upleft.x = 0; - update_type = EDIT_ONDRAW; - } - } - else if (ekbd->key == RTGUIK_END) - { - /* move cursor to line tail */ - if (line->len >= edit->col_per_page) - { - edit->visual.x = edit->col_per_page - 1; - edit->upleft.x = line->len - (edit->col_per_page - 1); - update_type = EDIT_ONDRAW; - } - else - edit->visual.x = line->len; - } - else if (ekbd->key == RTGUIK_TAB) - { - int space_nums; - struct rtgui_event_kbd event_kbd; - /* using spaces to replace TAB */ - space_nums = edit->tabsize - (edit->upleft.x + edit->visual.x) % edit->tabsize; - while (space_nums--) - { - kbd_event_set_key(&event_kbd, RTGUIK_SPACE); - rtgui_edit_onkey(object, (rtgui_event_t *)&event_kbd); - } - } - else if (ekbd->key == RTGUIK_RETURN) - { - struct edit_line *update_end_line; - struct rtgui_event_kbd event_kbd; - - /* insert a new line buffer */ - rtgui_edit_insert_line(edit, line, line->text + edit->upleft.x + edit->visual.x); - line->text[edit->upleft.x + edit->visual.x] = '\0'; - line->len = rtgui_edit_line_strlen(line->text); - - if ((edit->max_rows - edit->upleft.y) < edit->row_per_page) - { - int update_end_index = rtgui_edit_get_index_by_line(edit, edit->tail); - update_type = EDIT_UPDATE; - edit->update.start = edit->visual; - edit->update.end.x = edit->tail->len; - edit->update.end.y = update_end_index; - } - else - { - /* nothing */ - /* it will be adjusted upleft.y when entering DOWN case */ - } - - /* move the caret to the next line head */ - kbd_event_set_key(&event_kbd, RTGUIK_DOWN); - rtgui_edit_onkey(object, (rtgui_event_t *)&event_kbd); - kbd_event_set_key(&event_kbd, RTGUIK_HOME); - rtgui_edit_onkey(object, (rtgui_event_t *)&event_kbd); - /* adjust update line end position */ - if ((edit->max_rows - edit->upleft.y) >= edit->row_per_page) - { - update_type = EDIT_UPDATE; - edit->update.start = edit->visual; - edit->update.start.y -= 1; - update_end_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->row_per_page - 1); - - if (update_end_line != RT_NULL) - { - edit->update.end.x = update_end_line->len; - edit->update.end.y = edit->upleft.y + edit->row_per_page; - } - } - } - else - { - if (isprint((unsigned char)ekbd->key)) - { - /* it's may print character */ - update_type = EDIT_UPDATE; - edit->update.start = edit->visual; - - if (edit->flag & RTGUI_EDIT_SHIFT) - ekbd->key = query_shift_code(ekbd->key); - if (edit->flag & RTGUI_EDIT_CAPSLOCK) - ekbd->key = query_caps_code(ekbd->key); - - if (line->len < line->zsize - 1) - { - int ofs = edit->upleft.x + edit->visual.x; - if (edit->visual.x >= edit->col_per_page - 1) - { - edit->upleft.x ++; - update_type = EDIT_ONDRAW; - } - - if (ofs < line->len) - { - char *c; - for (c = &line->text[line->len]; c != &line->text[ofs]; c--) - *c = *(c - 1); - } - line->text[ofs] = (char)ekbd->key; - if (edit->visual.x < edit->col_per_page - 1) - edit->visual.x ++; - line->text[line->len + 1] = '\0'; - line->len = rtgui_edit_line_strlen(line->text); - edit->update.end.x = line->len; - if (edit->update.end.x > edit->col_per_page) - edit->update.end.x = edit->col_per_page; - edit->update.end.y = edit->visual.y; - } - else - { - /* adjust line buffer's zone size */ - line->zsize = rtgui_edit_alloc_len(edit->bzsize, line->len + 1); - line->text = rt_realloc(line->text, line->zsize); - rtgui_edit_onkey(object, event); /* reentry */ - } - } - else - { - /* Is small keyboard ? */ - if (edit->flag & RTGUI_EDIT_NUMLOCK) - { - if (is_small_keyboard(&ekbd->key)) - rtgui_edit_onkey(object, event); - /* small keyboard another value reserved */ - } - } - } - line->len = rtgui_edit_line_strlen(line->text); - -_edit_exit: - if (edit->flag & RTGUI_EDIT_CARET) - { - if (edit->caret_timer != RT_NULL) - rtgui_timer_stop(edit->caret_timer); - - edit->flag &= ~RTGUI_EDIT_CARET; - rtgui_edit_draw_caret(edit);/* refresh it */ - if (edit->caret_timer != RT_NULL) - rtgui_timer_start(edit->caret_timer); - } - - /* re-draw edit widget */ - if (update_type == EDIT_ONDRAW) - rtgui_edit_ondraw(edit); - else if (update_type == EDIT_UPDATE) - rtgui_edit_update(edit); - - if (RTGUI_WIDGET_IS_FOCUSED(edit)) - { - rtgui_edit_init_caret(edit, edit->visual); - edit->flag |= RTGUI_EDIT_CARET; - rtgui_edit_draw_caret(edit); - } - return RT_TRUE; -} - -static rt_bool_t rtgui_edit_onfocus(struct rtgui_object *object, rtgui_event_t *event) -{ - struct rtgui_edit *edit = RTGUI_EDIT(object); - - edit->caret_timer = rtgui_timer_create(50, RT_TIMER_FLAG_PERIODIC, - rtgui_edit_timeout, (void *)edit); - /* set caret to show */ - edit->flag |= RTGUI_EDIT_CARET; - /* start caret timer */ - if (edit->caret_timer != RT_NULL) - rtgui_timer_start(edit->caret_timer); - - return RT_TRUE; -} - -static rt_bool_t rtgui_edit_onunfocus(struct rtgui_object *object, rtgui_event_t *event) -{ - struct rtgui_edit *edit = RTGUI_EDIT(object); - - /* stop caret timer */ - if (edit->caret_timer != RT_NULL) - { - rtgui_timer_stop(edit->caret_timer); - rtgui_timer_destory(edit->caret_timer); - } - /* set caret to hide */ - edit->flag &= ~RTGUI_EDIT_CARET; - rtgui_edit_draw_caret(edit); - - return RT_TRUE; -} - -#ifdef RTGUI_EDIT_USING_SCROLL -static rt_bool_t rtgui_edit_hscroll_handle(struct rtgui_widget *widget, rtgui_event_t *event) -{ - struct rtgui_edit *edit = RTGUI_EDIT(widget); - - /* adjust first display row when dragging */ - edit->upleft.y = edit->hscroll->value; - - rtgui_edit_ondraw(edit); - - return RT_TRUE; -} - -static rt_bool_t rtgui_edit_vscroll_handle(struct rtgui_widget *widget, rtgui_event_t *event) -{ - struct rtgui_edit *edit = RTGUI_EDIT(widget); - - /* adjust first display row when dragging */ - edit->upleft.x = edit->vscroll->value; - - rtgui_edit_ondraw(edit); - - return RT_TRUE; -} -#endif - -/* local area update */ -void rtgui_edit_update(struct rtgui_edit *edit) -{ - rt_int16_t i, cpy_len = 0, prev_len; - rtgui_rect_t rect, r; - struct rtgui_dc *dc; - char *src; - - RT_ASSERT(edit != RT_NULL); - - dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(edit)); - if (dc == RT_NULL) return; - - rtgui_widget_get_rect(RTGUI_WIDGET(edit), &rect); - rtgui_rect_inflate(&rect, -(edit->margin + RTGUI_WIDGET_BORDER(edit))); - - if (edit->update_buf == RT_NULL) - { - /* try again allocate */ - edit->update_buf = rtgui_malloc(edit->col_per_page + 1); - } - - prev_len = edit->col_per_page; - for (i = edit->update.start.y; i <= edit->update.end.y; i++) - { - struct edit_line *line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + i); - - if (i > edit->upleft.y + edit->row_per_page) break; - if (line == RT_NULL) - { - /* when use "RTGUIK_BACKSPACE" backspace forward, - * the last line is blank, that only fill background. */ - r.x1 = rect.x1; - r.x2 = rect.x1 + prev_len * edit->font_width; - r.y1 = rect.y1 + i * edit->font_height; - r.y2 = r.y1 + edit->font_height; - if (r.x2 > rect.x2) r.x2 = rect.x2; - if (r.y2 > rect.y2) r.y2 = rect.y2; - rtgui_dc_fill_rect(dc, &r); - break; - } - - if (edit->update.start.y == edit->update.end.y) - { - r.x1 = rect.x1 + edit->update.start.x * edit->font_width; - r.x2 = rect.x1 + edit->update.end.x * edit->font_width; - r.y1 = rect.y1 + i * edit->font_height; - r.y2 = r.y1 + edit->font_height; - cpy_len = edit->update.end.x - edit->update.start.x; - src = line->text + edit->update.start.x + edit->upleft.x; - } - else - { - if (i == edit->update.start.y) - { - r.x1 = rect.x1 + edit->update.start.x * edit->font_width; - r.x2 = rect.x2; - r.y1 = rect.y1 + i * edit->font_height; - r.y2 = r.y1 + edit->font_height; - cpy_len = line->len - edit->update.start.x - edit->upleft.x; - if (cpy_len > (edit->col_per_page - edit->update.start.x)) - cpy_len = edit->col_per_page - edit->update.start.x; - src = line->text + edit->update.start.x + edit->upleft.x; - } - else if (i == edit->update.end.y) - { - r.x1 = rect.x1; - r.x2 = rect.x1 + edit->update.end.x * edit->font_width; - cpy_len = edit->update.end.x; - r.y1 = rect.y1 + i * edit->font_height; - r.y2 = r.y1 + edit->font_height; - src = line->text + edit->upleft.x; - } - else - { - r.x1 = rect.x1; - r.x2 = rect.x2; - r.y1 = rect.y1 + i * edit->font_height; - r.y2 = r.y1 + edit->font_height; - cpy_len = line->len - edit->upleft.x; - if (cpy_len > edit->col_per_page) - cpy_len = edit->col_per_page; - src = line->text + edit->upleft.x; - } - } - if (r.y1 > rect.y2) break; - if (r.x2 > rect.x2) r.x2 = rect.x2; - if (r.y2 > rect.y2) r.y2 = rect.y2; - - rt_memcpy(edit->update_buf, src, cpy_len); - *(edit->update_buf + cpy_len) = '\0'; - - rtgui_dc_fill_rect(dc, &r); - rtgui_dc_draw_text(dc, edit->update_buf, &r); - prev_len = line->len; - } - - rtgui_dc_end_drawing(dc); -} - -void rtgui_edit_ondraw(struct rtgui_edit *edit) -{ - rtgui_rect_t rect, r; - struct rtgui_dc *dc; -#ifdef RTGUI_EDIT_USING_SCROLL - int hscroll_flag = 0; - int vscroll_flag = 0; -#endif - - RT_ASSERT(edit != RT_NULL); - - dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(edit)); - if (dc == RT_NULL) return; - - /* get widget rect */ - rtgui_widget_get_rect(RTGUI_WIDGET(edit), &rect); - rtgui_rect_inflate(&rect, -RTGUI_WIDGET_BORDER(edit)); - - /* fill widget rect with edit background color */ - RTGUI_DC_BC(dc) = RTGUI_WIDGET_BACKGROUND(edit); - rtgui_dc_fill_rect(dc, &rect); - - rtgui_rect_inflate(&rect, RTGUI_WIDGET_BORDER(edit)); - /* draw border */ - rtgui_dc_draw_border(dc, &rect, RTGUI_WIDGET_BORDER_STYLE(edit)); - - rtgui_rect_inflate(&rect, -(edit->margin + RTGUI_WIDGET_BORDER(edit))); - -#ifdef RTGUI_EDIT_USING_SCROLL - if (edit->vscroll && !RTGUI_WIDGET_IS_HIDE(edit->vscroll)) - { - rect.x2 = rect.x2 - rtgui_rect_width(edit->vscroll->parent.extent); - } - if (edit->hscroll && !RTGUI_WIDGET_IS_HIDE(edit->hscroll)) - { - rect.y2 = rect.y2 - rtgui_rect_height(edit->hscroll->parent.extent); - } -#endif - r = rect; - - /* draw text */ - if (edit->head != RT_NULL) - { - struct edit_line *line = edit->first_line; - int num = 0; - - rect.y2 = rect.y1 + edit->item_height; - while (line) - { - rt_int16_t tmp_pos = 0, ofs; - char *str = line->text + edit->upleft.x; - - if (edit->upleft.x < line->len) - { - rtgui_point_t p = edit->visual; /* backup */ - edit->visual.x = 0; - identify_double_byte(edit, line, EDIT_IDENT_DIR_LEFT, &tmp_pos); - ofs = tmp_pos % 2; - if (ofs == 1) - { - /* use dc_buffer draw the left half of double byte */ - char dbl_bmp[3]; - rtgui_point_t pot = {0}; - rtgui_rect_t r = {0}; - - pot.x = edit->font_width; - r.x2 = edit->font_width * 2; - r.y2 = edit->font_height; - dbl_bmp[0] = *(str - 1); - dbl_bmp[1] = *str; - dbl_bmp[2] = '\0'; - RTGUI_DC_BC(edit->dbl_buf) = RTGUI_WIDGET_BACKGROUND(edit); - rtgui_dc_fill_rect(edit->dbl_buf, &r); - RTGUI_DC_FC(edit->dbl_buf) = RTGUI_WIDGET_FOREGROUND(edit); - rtgui_dc_draw_text(edit->dbl_buf, dbl_bmp, &r); - rtgui_dc_blit(edit->dbl_buf, &pot, dc, &rect); - } - rect.x1 += ofs * edit->font_width; - rtgui_dc_draw_text(dc, line->text + edit->upleft.x + ofs, &rect); - rect.x1 -= ofs * edit->font_width; - edit->visual = p; /* restore */ - } - - line = line->next; - - rect.y1 += edit->item_height; - if ((rect.y1 + edit->item_height) < r.y2) - rect.y2 = rect.y1 + edit->item_height; - else - rect.y2 = r.y2; - - if (num++ >= edit->row_per_page) - break; - } - } - -#ifdef RTGUI_EDIT_USING_SCROLL - if (edit->hscroll && !RTGUI_WIDGET_IS_HIDE(edit->hscroll)) - { - hscroll_flag = 1; - rtgui_scrollbar_ondraw(edit->hscroll); - } - if (edit->vscroll && !RTGUI_WIDGET_IS_HIDE(edit->vscroll)) - { - vscroll_flag = 1; - rtgui_scrollbar_ondraw(edit->vscroll); - } - - if (hscroll_flag && vscroll_flag) - { - rtgui_color_t _bc; - rtgui_widget_get_rect(RTGUI_WIDGET(edit), &rect); - rect.x1 = rect.x2 - RTGUI_WIDGET_BORDER(edit); - rect.y1 = rect.y2 - RTGUI_WIDGET_BORDER(edit); - _bc = RTGUI_DC_BC(dc); - RTGUI_DC_BC(dc) = default_background; - rtgui_dc_fill_rect(dc, &rect); - RTGUI_DC_BC(dc) = _bc; - } -#endif - rtgui_dc_end_drawing(dc); -} - -/* set edit text */ -void rtgui_edit_set_text(struct rtgui_edit *edit, const char *text) -{ - const char *begin, *ptr; -#ifdef RTGUI_EDIT_USING_SCROLL - int hscroll_flag = 0; - int vscroll_flag = 0; -#endif - - RT_ASSERT(edit != RT_NULL); - - while (edit->max_rows > 0) - rtgui_edit_delete_line(edit, edit->head); - edit->max_rows = 0; - - begin = text; - for (ptr = begin; *ptr != '\0'; ptr++) - { - if (*ptr == 0x0A) - { - /* unix style */ - rtgui_edit_append_line(edit, begin); - begin = ptr + 1; - } - else if (*ptr == 0x0D && *(ptr + 1) == 0x0A) - { - /* windows style */ - rtgui_edit_append_line(edit, begin); - begin = ptr + 2; - } - } - if (*ptr == '\0') - { - if (begin < ptr) - rtgui_edit_append_line(edit, begin); - } - -#ifdef RTGUI_EDIT_USING_SCROLL - if (edit->hscroll != RT_NULL) - { - if (edit->max_cols > edit->col_per_page) - { - RTGUI_WIDGET_SHOW(edit->hscroll); - rtgui_scrollbar_set_line_step(edit->hscroll, 1); - rtgui_scrollbar_set_page_step(edit->hscroll, edit->col_per_page); - rtgui_scrollbar_set_range(edit->hscroll, edit->max_cols); - hscroll_flag = 1; - } - else - { - RTGUI_WIDGET_HIDE(edit->hscroll); - } - } - if (edit->vscroll != RT_NULL) - { - if (edit->max_rows > edit->row_per_page) - { - RTGUI_WIDGET_SHOW(edit->vscroll); - rtgui_scrollbar_set_line_step(edit->vscroll, 1); - rtgui_scrollbar_set_page_step(edit->vscroll, edit->row_per_page); - rtgui_scrollbar_set_range(edit->vscroll, edit->max_rows); - vscroll_flag = 1; - } - else - { - RTGUI_WIDGET_HIDE(edit->vscroll); - } - } - - if (edit->hscroll != RT_NULL && !RTGUI_WIDGET_IS_HIDE(edit->hscroll)) - { - rtgui_edit_adjust_scroll(edit->hscroll); - } - if (edit->vscroll != RT_NULL && !RTGUI_WIDGET_IS_HIDE(edit->vscroll)) - { - rtgui_edit_adjust_scroll(edit->vscroll); - } - - if (hscroll_flag || vscroll_flag) - { - rtgui_widget_update_clip(RTGUI_WIDGET(edit)); - } -#endif -} - -rt_bool_t rtgui_edit_event_handler(struct rtgui_object *object, rtgui_event_t *event) -{ - rtgui_widget_t *widget = RTGUI_WIDGET(object); - struct rtgui_edit *edit = RTGUI_EDIT(object); - - switch (event->type) - { - case RTGUI_EVENT_PAINT: -#ifndef RTGUI_USING_SMALL_SIZE - if (widget->on_draw != RT_NULL) - widget->on_draw(object, event); - else -#endif - rtgui_edit_ondraw(edit); - break; - - case RTGUI_EVENT_MOUSE_BUTTON: -#ifndef RTGUI_USING_SMALL_SIZE - if (widget->on_mouseclick != RT_NULL) - widget->on_mouseclick(object, event); - else -#endif - rtgui_edit_onmouse(edit, (struct rtgui_event_mouse *)event); - return RT_TRUE; - - case RTGUI_EVENT_KBD: -#ifndef RTGUI_USING_SMALL_SIZE - if (widget->on_key != RT_NULL) - widget->on_key(object, event); - else -#endif - rtgui_edit_onkey(object, event); - return RT_TRUE; - - default: - return rtgui_widget_event_handler(object, event); - } - - return RT_FALSE; -} - -rtgui_point_t rtgui_edit_get_current_point(struct rtgui_edit *edit) -{ - rtgui_point_t p; - - RT_ASSERT(edit != RT_NULL); - - p.x = edit->upleft.x + edit->visual.x; - p.y = edit->upleft.y + edit->visual.y; - - return p; -} - -rt_uint32_t rtgui_edit_get_mem_consume(struct rtgui_edit *edit) -{ - rt_uint32_t mem_size; - struct edit_line *line; - - mem_size = sizeof(struct rtgui_edit); - mem_size += edit->col_per_page + 1; /* update_buf */ - if (edit->head != RT_NULL) - { - line = edit->head; - while (line) - { - mem_size += line->zsize; - mem_size += sizeof(struct edit_line); - line = line->next; - } - } - - return mem_size; -} - -#ifdef RTGUI_USING_DFS_FILERW -/** - * File access component, General File Access Interface - */ -rt_bool_t rtgui_edit_readin_file(struct rtgui_edit *edit, const char *filename) -{ - struct rtgui_filerw *filerw; - int num = 0, read_bytes, size , len = 0; - char *text , ch; - - filerw = rtgui_filerw_create_file(filename, "rb"); - if (filerw == RT_NULL) return RT_FALSE; - /** - * If it was in the debug of the win32, If document encode is UTF-8 or Unicode, - * Will read to garbled code when using the function read documents. - * You can Change of the document contains the source code for ANSI. - */ - while (edit->max_rows > 0) - rtgui_edit_delete_line(edit, edit->head); - edit->max_rows = 0; - - size = edit->bzsize; - text = rtgui_malloc(size); - if (text == RT_NULL) return RT_FALSE; - - do - { - if ((read_bytes = rtgui_filerw_read(filerw, &ch, 1, 1)) > 0) - { - /* rt_kprintf("ch=%02X ",ch); DEBUG */ - if (num >= size - 1) - text = rt_realloc(text, rtgui_edit_alloc_len(size, num)); - if (ch == 0x09) //Tab - { - len = edit->tabsize - num % edit->tabsize; - while (len--) - *(text + num++) = ' '; - } - else - *(text + num++) = ch; - if (ch == 0x0A) - { - rtgui_edit_append_line(edit, text); - num = 0; - } - - } - else if (num > 0) - { - /* last line does not exist the end operator */ - *(text + num) = '\0'; - rtgui_edit_append_line(edit, text); - } - } - while (read_bytes); - - rtgui_filerw_close(filerw); - rtgui_free(text); - rtgui_edit_ondraw(edit); - - return RT_TRUE; -} - -rt_bool_t rtgui_edit_saveas_file(struct rtgui_edit *edit, const char *filename) -{ - struct rtgui_filerw *filerw; - char ch_tailed = 0x0A; - struct edit_line *line; - - filerw = rtgui_filerw_create_file(filename, "wb"); - if (filerw == RT_NULL) return RT_FALSE; - - line = edit->head; - while (line) - { - rtgui_filerw_write(filerw, line->text, line->len, 1); - if (line != edit->tail) - rtgui_filerw_write(filerw, &ch_tailed, 1, 1); - line = line->next; - } - - rtgui_filerw_close(filerw); - - return RT_TRUE; -} -#endif diff --git a/components/rtgui/widgets/filelist_view.c b/components/rtgui/widgets/filelist_view.c deleted file mode 100644 index 78d05c68d0..0000000000 --- a/components/rtgui/widgets/filelist_view.c +++ /dev/null @@ -1,861 +0,0 @@ -/* - * File : filelist_view.c - * This file is part of RTGUI in RT-Thread RTOS - * COPYRIGHT (C) 2010, 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 - * 2010-01-06 Bernard first version - */ - -#include <rtgui/rtgui_object.h> -#include <rtgui/rtgui_system.h> -#include <rtgui/rtgui_theme.h> -#include <rtgui/rtgui_app.h> - -#include <rtgui/list.h> -#include <rtgui/image.h> -#include <rtgui/widgets/container.h> -#include <rtgui/widgets/filelist_view.h> -#include <rtgui/widgets/listbox.h> -#include <rtgui/widgets/window.h> - -#if defined(RTGUI_USING_DFS_FILERW) -#ifdef _WIN32_NATIVE -#include <io.h> -#include <dirent.h> -#include <sys/stat.h> -#define PATH_SEPARATOR '\\' -#else -#include <dfs_posix.h> -#define PATH_SEPARATOR '/' -#endif - -#include <string.h> - -#define RTGUI_FILELIST_MARGIN 5 - -const static char *file_xpm[] = -{ - "16 16 21 1", - " c None", - ". c #999999", - "+ c #818181", - "@ c #FFFFFF", - "# c #ECECEC", - "$ c #EAEAEA", - "% c #EBEBEB", - "& c #EDEDED", - "* c #F0F0F0", - "= c #C4C4C4", - "- c #C5C5C5", - "; c #C6C6C6", - "> c #C7C7C7", - ", c #EEEEEE", - "' c #EDEDE5", - ") c #EDEDE6", - "! c #EFEFEF", - "~ c #C8C8C8", - "{ c #F1F1F1", - "] c #F2F2F2", - "^ c #959595", - ".++++++++++++ ", - "+@@@@@@@@@@@@+ ", - "+@#$$%%%##&*@+ ", - "+@$=--;;;;>*@+ ", - "+@$%%###&&,*@+ ", - "+@%-;;;;;;>*@+ ", - "+@%%##&&'#,*@+ ", - "+@%;;;;,,),*@+ ", - "+@##&&,,!!!*@+ ", - "+@#;;;>>~~~*@+ ", - "+@#&,,!!*{{{@+ ", - "+@&;>>~~~{{]@+ ", - "+@&&,!!**{]]@+ ", - "+@@@@@@@@@@@@+ ", - "^++++++++++++^ ", - " " -}; - -const static char *folder_xpm[] = -{ - "16 16 121 2", - " c None", - ". c #D9B434", - "+ c #E1C25E", - "@ c #E2C360", - "# c #E2C35F", - "$ c #DBB63C", - "% c #DAB336", - "& c #FEFEFD", - "* c #FFFFFE", - "= c #FFFEFE", - "- c #FFFEFD", - "; c #FBF7EA", - "> c #E4C76B", - ", c #E3C76B", - "' c #E6CD79", - ") c #E5CA74", - "! c #DAAF35", - "~ c #FEFCF7", - "{ c #F8E48E", - "] c #F5DE91", - "^ c #F5E09F", - "/ c #F6E1AC", - "( c #FEFBEF", - "_ c #FEFDF4", - ": c #FEFCF3", - "< c #FEFCF1", - "[ c #FEFBEE", - "} c #FFFDFA", - "| c #DAAF36", - "1 c #DAAA36", - "2 c #FDFAF1", - "3 c #F5DE94", - "4 c #F4DC93", - "5 c #F2D581", - "6 c #EDCA6A", - "7 c #EACB6C", - "8 c #EFD385", - "9 c #EFD280", - "0 c #EFD07A", - "a c #EECF76", - "b c #EECF72", - "c c #FBF7E9", - "d c #DAAE34", - "e c #DAAB35", - "f c #FBF6E8", - "g c #EFD494", - "h c #EECE88", - "i c #E9C173", - "j c #F6E9C9", - "k c #FEFCF2", - "l c #FEFCF0", - "m c #DAAB36", - "n c #DAA637", - "o c #FFFDF8", - "p c #FFFDF6", - "q c #FFFCF5", - "r c #FCF6D8", - "s c #F8E694", - "t c #F7E385", - "u c #F6DF76", - "v c #F5DB68", - "w c #F4D85C", - "x c #FCF4D7", - "y c #DAA435", - "z c #DAA136", - "A c #FEFCF6", - "B c #FCF2C8", - "C c #FBEFB9", - "D c #FAECAC", - "E c #F9E89C", - "F c #F7E38B", - "G c #F6E07C", - "H c #F6DC6C", - "I c #F5D95D", - "J c #F4D64F", - "K c #F3D344", - "L c #FCF3D0", - "M c #DA9F35", - "N c #DA9A36", - "O c #FDFAF2", - "P c #FAEDB3", - "Q c #F9E9A4", - "R c #F8E695", - "S c #F7E285", - "T c #F6DE76", - "U c #F5DB65", - "V c #F4D757", - "W c #F3D449", - "X c #F2D13B", - "Y c #F1CE30", - "Z c #FBF2CC", - "` c #DA9835", - " . c #DA9435", - ".. c #FEFAEF", - "+. c #F9E9A1", - "@. c #F8E591", - "#. c #F7E181", - "$. c #F6DE72", - "%. c #F5DA63", - "&. c #F4D754", - "*. c #F3D347", - "=. c #F2D039", - "-. c #F1CD2E", - ";. c #F0CB26", - ">. c #FBF2CA", - ",. c #D98E33", - "'. c #FAF0DC", - "). c #F4DDA7", - "!. c #F4DB9E", - "~. c #F3DA96", - "{. c #F3D88E", - "]. c #F3D786", - "^. c #F2D47F", - "/. c #F2D379", - "(. c #F1D272", - "_. c #F1D06C", - ":. c #F1CF69", - "<. c #F8EAC2", - "[. c #D8882D", - "}. c #D8872D", - "|. c #D8862C", - " ", - " ", - " ", - " . + @ @ @ # $ ", - " % & * = - * ; > , , , ' ) ", - " ! ~ { ] ^ / ( _ : < ( [ } | ", - " 1 2 3 4 5 6 7 8 9 0 a b c d ", - " e f g h i j k : k l ( [ * m ", - " n * o p q : r s t u v w x y ", - " z A B C D E F G H I J K L M ", - " N O P Q R S T U V W X Y Z ` ", - " ...+.@.#.$.%.&.*.=.-.;.>. . ", - " ,.'.).!.~.{.].^./.(._.:.<.,. ", - " [.}.[.[.[.[.[.[.[.[.}.[.|. ", - " ", - " " -}; - -/* image for file and folder */ -static rtgui_image_t *file_image, *folder_image; -static struct rtgui_listbox_item items[] = -{ -#ifdef RTGUI_USING_FONTHZ - {"���ː`����", RT_NULL}, - {"���v�`����", RT_NULL}, - {"�X��", RT_NULL} -#else - {"Open folder", RT_NULL}, - {"Select folder", RT_NULL}, - {"Cancel", RT_NULL} -#endif -}; -static void rtgui_filelist_view_clear(rtgui_filelist_view_t *view); - -static rt_bool_t rtgui_filelist_view_on_folder_item(rtgui_object_t *object, struct rtgui_event *event) -{ - rtgui_win_t *menu; - rtgui_listbox_t *listbox; - rtgui_filelist_view_t *view; - - listbox = RTGUI_LISTBOX(object); - menu = RTGUI_WIN(rtgui_widget_get_toplevel(RTGUI_WIDGET(object))); - view = RTGUI_FILELIST_VIEW(menu->user_data); - - /* hide window */ - rtgui_win_hide(menu); - - switch (listbox->current_item) - { - case 0: - { - char *dir_ptr; - - /* destroy menu window */ - rtgui_win_destroy(menu); - - dir_ptr = (char *) rtgui_malloc(256); - rtgui_filelist_view_get_fullpath(view, dir_ptr, 256); - rtgui_filelist_view_set_directory(view, dir_ptr); - rtgui_free(dir_ptr); - } - break; - case 1: - /* destroy menu window */ - rtgui_win_destroy(menu); - break; - - default: - /* destroy menu window */ - rtgui_win_destroy(menu); - break; - } - - return RT_TRUE; -} - -static rt_bool_t rtgui_filelist_view_on_menu_deactivate(rtgui_object_t *object, struct rtgui_event *event) -{ - rtgui_win_t *menu; - menu = RTGUI_WIN(rtgui_widget_get_toplevel(RTGUI_WIDGET(object))); - - /* destroy menu window */ - rtgui_win_destroy(menu); - - return RT_TRUE; -} - -static void rtgui_filelist_view_menu_pop(rtgui_widget_t *parent) -{ - rtgui_win_t *menu; - rtgui_listbox_t *listbox; - rtgui_rect_t screen, rect = {0, 0, 140, 85}; - - rtgui_graphic_driver_get_rect(rtgui_graphic_driver_get_default(), &screen); - rtgui_rect_moveto_align(&screen, &rect, RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL); - - menu = rtgui_win_create(RTGUI_WIN(rtgui_widget_get_toplevel(parent)), - "Folder Menu", &rect, RTGUI_WIN_STYLE_DEFAULT); - if (menu != RT_NULL) - { - /* set user data on menu window */ - menu->user_data = (rt_uint32_t)parent; - - rtgui_win_set_ondeactivate(menu, rtgui_filelist_view_on_menu_deactivate); - - listbox = rtgui_listbox_create(items, sizeof(items) / sizeof(items[0]), &rect); - rtgui_listbox_set_onitem(listbox, rtgui_filelist_view_on_folder_item); - rtgui_container_add_child(RTGUI_CONTAINER(menu), RTGUI_WIDGET(listbox)); - rtgui_listbox_set_current_item(listbox, 0); - rtgui_win_show(menu, RT_TRUE); - } -} - -static void _rtgui_filelist_view_constructor(struct rtgui_filelist_view *view) -{ - /* default rect */ - struct rtgui_rect rect = {0, 0, 200, 200}; - - /* set default widget rect and set event handler */ - rtgui_object_set_event_handler(RTGUI_OBJECT(view), rtgui_filelist_view_event_handler); - rtgui_widget_set_rect(RTGUI_WIDGET(view), &rect); - - RTGUI_WIDGET(view)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE; - - view->current_item = 0; - view->items_count = 0; - view->page_items = 0; - - view->current_directory = RT_NULL; - view->pattern = RT_NULL; - RTGUI_WIDGET_BACKGROUND(view) = white; - RTGUI_WIDGET_TEXTALIGN(view) = RTGUI_ALIGN_CENTER_VERTICAL; - - file_image = rtgui_image_create_from_mem("xpm", - (rt_uint8_t *)file_xpm, sizeof(file_xpm), RT_TRUE); - folder_image = rtgui_image_create_from_mem("xpm", - (rt_uint8_t *)folder_xpm, sizeof(folder_xpm), RT_TRUE); -} - -static void _rtgui_filelist_view_destructor(struct rtgui_filelist_view *view) -{ - /* delete all file items */ - rtgui_filelist_view_clear(view); - /* delete current directory and pattern */ - if (view->current_directory != RT_NULL) - { - rt_free(view->current_directory); - view->current_directory = RT_NULL; - } - if (view->pattern != RT_NULL) - { - rt_free(view->pattern); - view->pattern = RT_NULL; - } - - /* delete image */ - rtgui_image_destroy(file_image); - rtgui_image_destroy(folder_image); -} - -DEFINE_CLASS_TYPE(filelist, "filelist", - RTGUI_CONTAINER_TYPE, - _rtgui_filelist_view_constructor, - _rtgui_filelist_view_destructor, - sizeof(struct rtgui_filelist_view)); - -void rtgui_filelist_view_ondraw(struct rtgui_filelist_view *view) -{ - struct rtgui_dc *dc; - rt_uint16_t page_index, index; - struct rtgui_file_item *item; - struct rtgui_rect rect, item_rect, image_rect; - - dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(view)); - if (dc == RT_NULL) return; - - rtgui_widget_get_rect(RTGUI_WIDGET(view), &rect); - rtgui_dc_fill_rect(dc, &rect); - - /* get item base rect */ - item_rect = rect; - item_rect.y1 += 1; - item_rect.y2 = item_rect.y1 + (1 + rtgui_theme_get_selected_height()); - - /* get image base rect */ - image_rect.x1 = RTGUI_FILELIST_MARGIN; - image_rect.y1 = 0; - image_rect.x2 = RTGUI_FILELIST_MARGIN + file_image->w; - image_rect.y2 = file_image->h; - rtgui_rect_moveto_align(&item_rect, &image_rect, RTGUI_ALIGN_CENTER_VERTICAL); - - /* get current page */ - page_index = (view->current_item / view->page_items) * view->page_items; - for (index = 0; index < view->page_items; index ++) - { - if (page_index + index >= view->items_count) break; - - item = &(view->items[page_index + index]); - - if (page_index + index == view->current_item) - { - rtgui_theme_draw_selected(dc, &item_rect); - } - else - { - /* draw background */ - rtgui_dc_fill_rect(dc, &item_rect); - } - - /* draw item */ - if (item->type == RTGUI_FITEM_FILE) - rtgui_image_blit(file_image, dc, &image_rect); - else - rtgui_image_blit(folder_image, dc, &image_rect); - - /* draw text */ - item_rect.x1 += RTGUI_FILELIST_MARGIN + file_image->w + 2; - rtgui_dc_draw_text(dc, item->name, &item_rect); - item_rect.x1 -= RTGUI_FILELIST_MARGIN + file_image->w + 2; - - /* move to next item position */ - item_rect.y1 += (rtgui_theme_get_selected_height() + 1); - item_rect.y2 += (rtgui_theme_get_selected_height() + 1); - - image_rect.y1 += (rtgui_theme_get_selected_height() + 1); - image_rect.y2 += (rtgui_theme_get_selected_height() + 1); - } - - rtgui_dc_end_drawing(dc); -} - -void rtgui_filelist_view_update_current(struct rtgui_filelist_view *view, rt_uint16_t old_item) -{ - struct rtgui_dc *dc; - struct rtgui_file_item *item; - rtgui_rect_t rect, item_rect, image_rect; - - if (old_item / view->page_items != view->current_item / view->page_items) - { - /* it's not a same page, update all */ - rtgui_widget_update(RTGUI_WIDGET(view)); - return; - } - - dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(view)); - if (dc == RT_NULL) return; - - rtgui_widget_get_rect(RTGUI_WIDGET(view), &rect); - - /* get old item rect */ - item_rect = rect; - item_rect.y1 += 1; - item_rect.y1 += (old_item % view->page_items) * (1 + rtgui_theme_get_selected_height()); - item_rect.y2 = item_rect.y1 + (1 + rtgui_theme_get_selected_height()); - - /* get image rect */ - image_rect.x1 = RTGUI_FILELIST_MARGIN; - image_rect.y1 = 0; - image_rect.x2 = RTGUI_FILELIST_MARGIN + file_image->w; - image_rect.y2 = file_image->h; - rtgui_rect_moveto_align(&item_rect, &image_rect, RTGUI_ALIGN_CENTER_VERTICAL); - - /* draw old item */ - rtgui_dc_fill_rect(dc, &item_rect); - - item = &(view->items[old_item]); - if (item->type == RTGUI_FITEM_FILE) /* draw item image */ - rtgui_image_blit(file_image, dc, &image_rect); - else - rtgui_image_blit(folder_image, dc, &image_rect); - - item_rect.x1 += RTGUI_FILELIST_MARGIN + file_image->w + 2; - rtgui_dc_draw_text(dc, item->name, &item_rect); - - /* draw current item */ - item_rect = rect; - item_rect.y1 += 1; - item_rect.y1 += (view->current_item % view->page_items) * (1 + rtgui_theme_get_selected_height()); - item_rect.y2 = item_rect.y1 + (1 + rtgui_theme_get_selected_height()); - - rtgui_theme_draw_selected(dc, &item_rect); - - /* get image base rect */ - image_rect.x1 = RTGUI_FILELIST_MARGIN; - image_rect.y1 = 0; - image_rect.x2 = RTGUI_FILELIST_MARGIN + file_image->w; - image_rect.y2 = file_image->h; - rtgui_rect_moveto_align(&item_rect, &image_rect, RTGUI_ALIGN_CENTER_VERTICAL); - - item = &(view->items[view->current_item]); - if (item->type == RTGUI_FITEM_FILE) /* draw item image */ - rtgui_image_blit(file_image, dc, &image_rect); - else - rtgui_image_blit(folder_image, dc, &image_rect); - - item_rect.x1 += RTGUI_FILELIST_MARGIN + file_image->w + 2; - rtgui_dc_draw_text(dc, item->name, &item_rect); - - rtgui_dc_end_drawing(dc); -} - -static void rtgui_filelist_view_onenturn(struct rtgui_filelist_view *view) -{ - if (view->items[view->current_item].type == RTGUI_FITEM_DIR) - { - char new_path[64]; - - if (strcmp(view->items[view->current_item].name, ".") == 0) return ; - if (strcmp(view->items[view->current_item].name, "..") == 0) - { - char *ptr; - ptr = strrchr(view->current_directory, PATH_SEPARATOR); - - if (ptr == RT_NULL) return ; - if (ptr == &(view->current_directory[0])) - { - /* it's root directory */ - new_path[0] = PATH_SEPARATOR; - new_path[1] = '\0'; - } - else - { - strncpy(new_path, view->current_directory, ptr - view->current_directory + 1); - new_path[ptr - view->current_directory] = '\0'; - } - } - else if (view->current_item == 0 && -#ifdef _WIN32_NATIVE - (view->current_directory[1] == ':') && (view->current_directory[2] == '\\') -#else - (view->current_directory[0] == '/') && (view->current_directory[1] == '\0') -#endif - ) - { - rtgui_filelist_view_destroy(view); - - return ; - } - else - { - rtgui_filelist_view_menu_pop(RTGUI_WIDGET(view)); - return ; - } - rtgui_filelist_view_set_directory(view, new_path); - } -} - -rt_bool_t rtgui_filelist_view_event_handler(struct rtgui_object *object, struct rtgui_event *event) -{ - struct rtgui_widget *widget; - struct rtgui_filelist_view *view = RT_NULL; - - widget = RTGUI_WIDGET(object); - view = RTGUI_FILELIST_VIEW(widget); - switch (event->type) - { - case RTGUI_EVENT_PAINT: - rtgui_filelist_view_ondraw(view); - return RT_FALSE; - - case RTGUI_EVENT_RESIZE: - { - struct rtgui_event_resize *resize; - - resize = (struct rtgui_event_resize *)event; - - /* recalculate page items */ - if (file_image != RT_NULL) - view->page_items = resize->h / (1 + rtgui_theme_get_selected_height()); - else - view->page_items = resize->h / (2 + 14); - } - break; - - case RTGUI_EVENT_MOUSE_BUTTON: - { - rtgui_rect_t rect; - struct rtgui_event_mouse *emouse; - - emouse = (struct rtgui_event_mouse *)event; - - /* calculate selected item */ - - /* get physical extent information */ - rtgui_widget_get_rect(widget, &rect); - rtgui_widget_rect_to_device(widget, &rect); - - if (rtgui_rect_contains_point(&rect, emouse->x, emouse->y) == RT_EOK) - { - rt_uint16_t index; - rt_uint16_t current_page; - rt_uint16_t old_item; - - index = (emouse->y - rect.y1) / (2 + rtgui_theme_get_selected_height()); - - /* get current page */ - current_page = view->current_item / view->page_items; - old_item = view->current_item; - - if (index + current_page * view->page_items < view->items_count) - { - /* set selected item */ - view->current_item = index + current_page * view->page_items; - if (emouse->button & RTGUI_MOUSE_BUTTON_DOWN) - { - rtgui_filelist_view_update_current(view, old_item); - } - else - { - /* up event */ - rtgui_filelist_view_onenturn(view); - } - } - - return RT_TRUE; - } - } - break; - - case RTGUI_EVENT_KBD: - { - struct rtgui_event_kbd *ekbd = (struct rtgui_event_kbd *)event; - if (ekbd->type == RTGUI_KEYDOWN) - { - rt_uint16_t old_item; - - old_item = view->current_item; - switch (ekbd->key) - { - case RTGUIK_UP: - if (view->current_item > 0) - view->current_item --; - rtgui_filelist_view_update_current(view, old_item); - return RT_TRUE; - - case RTGUIK_DOWN: - if (view->current_item < view->items_count - 1) - view->current_item ++; - rtgui_filelist_view_update_current(view, old_item); - return RT_TRUE; - - case RTGUIK_LEFT: - if (view->current_item - view->page_items >= 0) - view->current_item -= view->page_items; - rtgui_filelist_view_update_current(view, old_item); - return RT_TRUE; - - case RTGUIK_RIGHT: - if (view->current_item + view->page_items < view->items_count - 1) - view->current_item += view->page_items; - rtgui_filelist_view_update_current(view, old_item); - return RT_TRUE; - - case RTGUIK_RETURN: - rtgui_filelist_view_onenturn(view); - return RT_TRUE; - - default: - break; - } - } - } - return RT_FALSE; - } - - /* use view event handler */ - return rtgui_container_event_handler(object, event); -} - -rtgui_filelist_view_t *rtgui_filelist_view_create(const char *directory, - const char *pattern, - const rtgui_rect_t *rect) -{ - struct rtgui_filelist_view *view = RT_NULL; - - /* create a new view */ - view = (struct rtgui_filelist_view *) rtgui_widget_create(RTGUI_FILELIST_VIEW_TYPE); - if (view != RT_NULL) - { - rtgui_widget_set_rect(RTGUI_WIDGET(view), rect); - view->items = RT_NULL; - view->pattern = rt_strdup(pattern); - view->page_items = rtgui_rect_height(*rect) / (1 + rtgui_theme_get_selected_height()); - rtgui_filelist_view_set_directory(view, directory); - } - - return view; -} -RTM_EXPORT(rtgui_filelist_view_create); - -void rtgui_filelist_view_destroy(rtgui_filelist_view_t *view) -{ - /* destroy view */ - rtgui_widget_destroy(RTGUI_WIDGET(view)); -} -RTM_EXPORT(rtgui_filelist_view_destroy); - - -/* clear all file items */ -static void rtgui_filelist_view_clear(rtgui_filelist_view_t *view) -{ - rt_uint32_t index; - struct rtgui_file_item *item; - - for (index = 0; index < view->items_count; index ++) - { - item = &(view->items[index]); - - /* release item name */ - rt_free(item->name); - item->name = RT_NULL; - } - - /* release items */ - rtgui_free(view->items); - view->items = RT_NULL; - - view->items_count = 0; - view->current_item = 0; -} -RTM_EXPORT(rtgui_filelist_view_clear); - -void rtgui_filelist_view_set_directory(rtgui_filelist_view_t *view, const char *directory) -{ - struct rtgui_file_item *item; - - RT_ASSERT(view != RT_NULL); - - /* clear file items firstly */ - rtgui_filelist_view_clear(view); - if (directory != RT_NULL) - { - DIR *dir; - struct stat s; - char *fullpath; - rt_uint32_t index; - struct dirent *dirent; - - view->items_count = 0; - dir = opendir(directory); - if (dir == RT_NULL) goto __return; - - /* current directory exists, set it */ - if (view->current_directory != RT_NULL) rt_free(view->current_directory); - view->current_directory = rt_strdup(directory); - - do - { - dirent = readdir(dir); - if (dirent == RT_NULL) break; - - if (strcmp(dirent->d_name, ".") == 0) continue; - if (strcmp(dirent->d_name, "..") == 0) continue; - - view->items_count ++; - } - while (dirent != RT_NULL); - closedir(dir); - - view->items_count ++; /* root directory for [x] exit, others for .. */ - - view->items = (struct rtgui_file_item *) rtgui_malloc(sizeof(struct rtgui_file_item) * view->items_count); - if (view->items == RT_NULL) return; /* no memory */ - - index = 0; - if (directory[0] == '/' && directory[1] != '\0') - { - item = &(view->items[0]); - - /* add .. directory */ - item->name = rt_strdup(".."); - item->type = RTGUI_FITEM_DIR; - item->size = 0; - - index ++; - } - else - { - item = &(view->items[0]); - - /* add .. directory */ -#ifdef RTGUI_USING_FONTHZ - item->name = rt_strdup("�˳��ļ����"); -#else - item->name = rt_strdup(".."); -#endif - item->type = RTGUI_FITEM_DIR; - item->size = 0; - - index ++; - } - - /* reopen directory */ - dir = opendir(directory); - fullpath = rtgui_malloc(256); - while (index < view->items_count) - { - dirent = readdir(dir); - if (dirent == RT_NULL) break; - - if (strcmp(dirent->d_name, ".") == 0) continue; - if (strcmp(dirent->d_name, "..") == 0) continue; - - item = &(view->items[index]); - item->name = rt_strdup(dirent->d_name); - - rt_memset(&s, 0, sizeof(struct stat)); - - /* build full path for the file */ - if (directory[strlen(directory) - 1] != PATH_SEPARATOR) - rt_snprintf(fullpath, 256, "%s%c%s", directory, PATH_SEPARATOR, dirent->d_name); - else - rt_snprintf(fullpath, 256, "%s%s", directory, dirent->d_name); - - stat(fullpath, &s); - if (s.st_mode & S_IFDIR) - { - item->type = RTGUI_FITEM_DIR; - item->size = 0; - } - else - { - item->type = RTGUI_FITEM_FILE; - item->size = s.st_size; - } - - index ++; - } - rtgui_free(fullpath); - closedir(dir); - } - - view->current_item = 0; - -__return: - /* update view */ - rtgui_widget_update(RTGUI_WIDGET(view)); -} -RTM_EXPORT(rtgui_filelist_view_set_directory); - -void rtgui_filelist_view_get_fullpath(rtgui_filelist_view_t *view, char *path, rt_size_t len) -{ - RT_ASSERT(view != RT_NULL); - - if (view->current_directory[strlen(view->current_directory) - 1] != PATH_SEPARATOR) - rt_snprintf(path, len, "%s%c%s", view->current_directory, PATH_SEPARATOR, - view->items[view->current_item].name); - else - rt_snprintf(path, len, "%s%s", view->current_directory, - view->items[view->current_item].name); -} -RTM_EXPORT(rtgui_filelist_view_get_fullpath); - -#endif diff --git a/components/rtgui/widgets/groupbox.c b/components/rtgui/widgets/groupbox.c deleted file mode 100644 index 2e648c2997..0000000000 --- a/components/rtgui/widgets/groupbox.c +++ /dev/null @@ -1,155 +0,0 @@ -/* - * File : groupbox.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, 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 - * 2012-07-29 Bernard first version - */ -#include <rtgui/dc.h> -#include <rtgui/rtgui_system.h> -#include <rtgui/rtgui_theme.h> -#include <rtgui/widgets/groupbox.h> - -static void _rtgui_groupbox_constructor(rtgui_groupbox_t *box) -{ - /* init widget and set event handler */ - rtgui_object_set_event_handler(RTGUI_OBJECT(box), rtgui_groupbox_event_handler); - - /* set field */ - box->box = RT_NULL; - box->label = RT_NULL; - box->selected = RT_NULL; - - box->on_selected = RT_NULL; -} - -DEFINE_CLASS_TYPE(groupbox, "groupbox", - RTGUI_PANEL_TYPE, - _rtgui_groupbox_constructor, - RT_NULL, - sizeof(struct rtgui_groupbox)); - -rt_bool_t rtgui_groupbox_event_handler(struct rtgui_object *object, struct rtgui_event *event) -{ - struct rtgui_groupbox *box; - - box = RTGUI_GROUPBOX(object); - switch (event->type) - { - case RTGUI_EVENT_PAINT: - { - rtgui_panel_event_handler(RTGUI_OBJECT(box), event); - - /* dispatch paint event to child */ - rtgui_container_dispatch_event(RTGUI_CONTAINER(box), event); - } - break; - default: - return rtgui_container_event_handler(object, event); - } - - return RT_FALSE; -} - -rtgui_groupbox_t *rtgui_groupbox_create(const char *label, struct rtgui_rect *rect, - int style, widget_select_t select_func) -{ - struct rtgui_groupbox *box; - - RT_ASSERT(select_func != RT_NULL); - - box = (struct rtgui_groupbox *) rtgui_widget_create(RTGUI_GROUPBOX_TYPE); - if (box != RT_NULL) - { - rtgui_widget_set_rect(RTGUI_WIDGET(box), rect); - - if (label != RT_NULL) - { - box->label = rt_strdup(label); - } - - /* create layout box */ - box->box = rtgui_box_create(style, RTGUI_WIDGET_DEFAULT_MARGIN + 1); - rtgui_container_set_box(RTGUI_CONTAINER(box), box->box); - - rtgui_panel_set_border(RTGUI_PANEL(box), RTGUI_BORDER_NONE); - - box->select_func = select_func; - } - - return box; -} - -void rtgui_groupbox_destroy(rtgui_groupbox_t *groupbox) -{ - rtgui_object_destroy(RTGUI_OBJECT(groupbox)); -} - -void rtgui_groupbox_select_widget(struct rtgui_groupbox *box, struct rtgui_widget *widget) -{ - struct rtgui_event event; - - RT_ASSERT(box != RT_NULL); - RT_ASSERT(widget != RT_NULL); - - if (box->selected != widget) - { - if (box->selected != RT_NULL) - { - box->select_func(box->selected, RT_FALSE); - if (box->on_selected != RT_NULL) - { - RTGUI_EVENT_INIT(&event, RTGUI_EVENT_UNSELECTED); - box->on_selected(RTGUI_OBJECT(widget), &event); - } - rtgui_widget_update(widget); - } - box->selected = widget; - } - - box->select_func(box->selected, RT_TRUE); - - if (box->on_selected != RT_NULL) - { - RTGUI_EVENT_INIT(&event, RTGUI_EVENT_SELECTED); - box->on_selected(RTGUI_OBJECT(widget), &event); - } -} - -struct rtgui_widget *rtgui_groupbox_get_selected(struct rtgui_groupbox *box) -{ - RT_ASSERT(box != RT_NULL); - - return box->selected; -} - -void rtgui_groupbox_add_widget(struct rtgui_groupbox *box, struct rtgui_widget *widget) -{ - widget->user_data = (rt_uint32_t)box; - rtgui_container_add_child(RTGUI_CONTAINER(box), widget); - RTGUI_WIDGET_ALIGN(widget) = RTGUI_ALIGN_CENTER; - RTGUI_WIDGET_BACKGROUND(widget) = RTGUI_WIDGET_BACKGROUND(box); -} - -void rtgui_groupbox_layout(struct rtgui_groupbox *box) -{ - if (RTGUI_PANEL(box)->border_style != RTGUI_BORDER_NONE) - { - rtgui_box_layout(box->box); - } - else - { - struct rtgui_rect extent; - - RT_ASSERT(box != RT_NULL); - rtgui_widget_get_extent(RTGUI_WIDGET(box), &extent); - rtgui_rect_inflate(&extent, -RTGUI_WIDGET_DEFAULT_MARGIN); - rtgui_box_layout_rect(box->box, &extent); - } -} diff --git a/components/rtgui/widgets/iconbox.c b/components/rtgui/widgets/iconbox.c deleted file mode 100644 index a5807347fd..0000000000 --- a/components/rtgui/widgets/iconbox.c +++ /dev/null @@ -1,186 +0,0 @@ -/* - * File : iconbox.c - * This file is part of 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-16 Bernard first version - */ -#include <rtgui/dc.h> -#include <rtgui/widgets/iconbox.h> -#include <rtgui/rtgui_theme.h> - -static void _rtgui_iconbox_constructor(rtgui_iconbox_t *iconbox) -{ - /* init widget and set event handler */ - RTGUI_WIDGET(iconbox)->flag |= (RTGUI_WIDGET_FLAG_TRANSPARENT | RTGUI_WIDGET_FLAG_FOCUSABLE); - rtgui_object_set_event_handler(RTGUI_OBJECT(iconbox), rtgui_iconbox_event_handler); - - /* set proper of control */ - iconbox->image = RT_NULL; - iconbox->selected = RT_FALSE; - iconbox->text = RT_NULL; - iconbox->text_position = RTGUI_ICONBOX_TEXT_BELOW; -} - -static void _rtgui_iconbox_destructor(rtgui_iconbox_t *iconbox) -{ - if (iconbox->image != RT_NULL) - { - rtgui_image_destroy(iconbox->image); - iconbox->image = RT_NULL; - } - - if (iconbox->text != RT_NULL) - { - rt_free(iconbox->text); - iconbox->text = RT_NULL; - } -} - -DEFINE_CLASS_TYPE(iconbox, "iconbox", - RTGUI_WIDGET_TYPE, - _rtgui_iconbox_constructor, - _rtgui_iconbox_destructor, - sizeof(struct rtgui_iconbox)); - -rt_bool_t rtgui_iconbox_event_handler(struct rtgui_object *object, struct rtgui_event *event) -{ - struct rtgui_iconbox *iconbox; - - iconbox = RTGUI_ICONBOX(object); - - switch (event->type) - { - case RTGUI_EVENT_PAINT: - rtgui_theme_draw_iconbox(iconbox); - break; - - case RTGUI_EVENT_MOUSE_BUTTON: - if (RTGUI_WIDGET_IS_HIDE(object)) return RT_FALSE; - - { - struct rtgui_event_mouse *emouse = (struct rtgui_event_mouse *)event; - - /* it's not this widget event, clean status */ - if (rtgui_rect_contains_point(&(RTGUI_WIDGET(iconbox)->extent), - emouse->x, emouse->y) != RT_EOK) - { - if (iconbox->selected != RT_TRUE) - { - rtgui_iconbox_set_selected(iconbox, RT_TRUE); - rtgui_widget_focus(RTGUI_WIDGET(iconbox)); - } - break; - } - } - return RT_TRUE; - - default: - return rtgui_widget_event_handler(object, event); - } - - return RT_FALSE; -} - -struct rtgui_iconbox *rtgui_iconbox_create(struct rtgui_image *image, - const char *text, - int position) -{ - struct rtgui_iconbox *iconbox; - - iconbox = (struct rtgui_iconbox *)rtgui_widget_create(RTGUI_ICONBOX_TYPE); - if (iconbox != RT_NULL) - { - rtgui_rect_t rect = {0, 0, 0, 0}, text_rect; - - rect.x2 = image->w; - rect.y2 = image->h; - - /* get text rect */ - rtgui_font_get_metrics(rtgui_font_default(), text, &text_rect); - if (position == RTGUI_ICONBOX_TEXT_BELOW) - { - rect.y2 += RTGUI_WIDGET_DEFAULT_MARGIN; - if (text_rect.x2 > rect.x2) - { - rect.x2 = text_rect.x2; - } - rect.y2 += text_rect.y2; - } - else if (position == RTGUI_ICONBOX_TEXT_RIGHT) - { - rect.x2 += RTGUI_WIDGET_DEFAULT_MARGIN; - if (text_rect.y2 > rect.y2) - { - rect.y2 = text_rect.y2; - } - rect.x2 += text_rect.x2; - } - - /* set widget rect */ - rtgui_widget_set_rect(RTGUI_WIDGET(iconbox), &rect); - - /* set image and text position */ - iconbox->image = image; - iconbox->text = (char *)rt_strdup((const char *)text); - iconbox->text_position = position; - } - - return iconbox; -} - -void rtgui_iconbox_destroy(struct rtgui_iconbox *iconbox) -{ - rtgui_widget_destroy(RTGUI_WIDGET(iconbox)); -} - -void rtgui_iconbox_set_text_position(struct rtgui_iconbox *iconbox, int position) -{ - struct rtgui_rect rect = {0, 0, 0, 0}, text_rect; - - RT_ASSERT(iconbox != RT_NULL); - - iconbox->text_position = position; - - /* set mini width and height */ - rect.x2 = iconbox->image->w; - rect.y2 = iconbox->image->h; - - /* get text rect */ - if (iconbox->text != RT_NULL) - { - rtgui_font_get_metrics(rtgui_font_default(), - iconbox->text, &text_rect); - if (position == RTGUI_ICONBOX_TEXT_BELOW) - { - rect.y2 += RTGUI_WIDGET_DEFAULT_MARGIN; - if (text_rect.x2 > rect.x2) - { - rect.x2 = text_rect.x2; - } - rect.y2 += text_rect.y2; - } - else if (position == RTGUI_ICONBOX_TEXT_RIGHT) - { - rect.x2 += RTGUI_WIDGET_DEFAULT_MARGIN; - if (text_rect.y2 > rect.y2) - { - rect.y2 = text_rect.y2; - } - rect.x2 += text_rect.x2; - } - } -} - -void rtgui_iconbox_set_selected(struct rtgui_iconbox *iconbox, rt_bool_t selected) -{ - RT_ASSERT(iconbox != RT_NULL); - - iconbox->selected = selected; -} diff --git a/components/rtgui/widgets/label.c b/components/rtgui/widgets/label.c deleted file mode 100644 index 2465879e45..0000000000 --- a/components/rtgui/widgets/label.c +++ /dev/null @@ -1,122 +0,0 @@ -/* - * File : label.c - * This file is part of 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-16 Bernard first version - */ -#include <rtgui/dc.h> -#include <rtgui/widgets/label.h> -#include <rtgui/rtgui_system.h> -#include <rtgui/rtgui_theme.h> - -static void _rtgui_label_constructor(rtgui_label_t *label) -{ - RTGUI_WIDGET_TEXTALIGN(label) = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_CENTER_VERTICAL; - /* init widget and set event handler */ - rtgui_object_set_event_handler(RTGUI_OBJECT(label), rtgui_label_event_handler); - - /* set field */ - label->text = RT_NULL; -} - -static void _rtgui_label_destructor(rtgui_label_t *label) -{ - /* release text memory */ - if (label->text) - rt_free(label->text); - label->text = RT_NULL; -} - -DEFINE_CLASS_TYPE(label, "label", - RTGUI_WIDGET_TYPE, - _rtgui_label_constructor, - _rtgui_label_destructor, - sizeof(struct rtgui_label)); - -rt_bool_t rtgui_label_event_handler(struct rtgui_object *object, struct rtgui_event *event) -{ - struct rtgui_label *label; - RTGUI_WIDGET_EVENT_HANDLER_PREPARE - - label = RTGUI_LABEL(object); - switch (event->type) - { - case RTGUI_EVENT_PAINT: - rtgui_theme_draw_label(label); - break; - default: - return rtgui_widget_event_handler(object, event); - } - - return RT_FALSE; -} -RTM_EXPORT(rtgui_label_event_handler); - -rtgui_label_t *rtgui_label_create(const char *text) -{ - struct rtgui_label *label; - - label = (struct rtgui_label *) rtgui_widget_create(RTGUI_LABEL_TYPE); - if (label != RT_NULL) - { - rtgui_rect_t rect; - - /* set default rect */ - rtgui_font_get_metrics(rtgui_font_default(), text, &rect); - rect.x2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1); - rect.y2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1); - rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect); - - /* set text */ - label->text = (char *)rt_strdup((const char *)text); - } - - return label; -} -RTM_EXPORT(rtgui_label_create); - -void rtgui_label_destroy(rtgui_label_t *label) -{ - rtgui_widget_destroy(RTGUI_WIDGET(label)); -} -RTM_EXPORT(rtgui_label_destroy); - -char *rtgui_label_get_text(rtgui_label_t *label) -{ - RT_ASSERT(label != RT_NULL); - - return label->text; -} -RTM_EXPORT(rtgui_label_get_text); - -void rtgui_label_set_text(rtgui_label_t *label, const char *text) -{ - RT_ASSERT(label != RT_NULL); - - if (label->text != RT_NULL) - { - /* it's a same text string */ - if (rt_strcmp(text, label->text) == 0) - return; - - /* release old text memory */ - rt_free(label->text); - } - - if (text != RT_NULL) - label->text = (char *)rt_strdup((const char *)text); - else - label->text = RT_NULL; - - /* update widget */ - rtgui_theme_draw_label(label); -} -RTM_EXPORT(rtgui_label_set_text); - diff --git a/components/rtgui/widgets/list_view.c b/components/rtgui/widgets/list_view.c deleted file mode 100644 index d45be89e5d..0000000000 --- a/components/rtgui/widgets/list_view.c +++ /dev/null @@ -1,655 +0,0 @@ -/* - * File : list_view.c - * This file is part of RTGUI in RT-Thread RTOS - * COPYRIGHT (C) 2010, 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 - * 2010-01-06 Bernard first version - */ - -#include <rtgui/rtgui_theme.h> -#include <rtgui/widgets/list_view.h> - -#define LIST_MARGIN 5 - -static void _rtgui_list_view_constructor(struct rtgui_list_view *view) -{ - /* default rect */ - struct rtgui_rect rect = {0, 0, 200, 200}; - - /* set default widget rect and set event handler */ - rtgui_object_set_event_handler(RTGUI_OBJECT(view), rtgui_list_view_event_handler); - rtgui_widget_set_rect(RTGUI_WIDGET(view), &rect); - - RTGUI_WIDGET(view)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE; - - view->flag = RTGUI_LIST_VIEW_LIST; - view->current_item = 0; - view->items_count = 0; - view->page_items = 0; - - RTGUI_WIDGET_BACKGROUND(view) = white; - RTGUI_WIDGET_TEXTALIGN(view) = RTGUI_ALIGN_CENTER_VERTICAL; -} - -DEFINE_CLASS_TYPE(listview, "listview", - RTGUI_CONTAINER_TYPE, - _rtgui_list_view_constructor, - RT_NULL, - sizeof(struct rtgui_list_view)); - -static void rtgui_list_view_onicondraw(struct rtgui_list_view *view, struct rtgui_dc *dc) -{ - struct rtgui_rect rect, item_rect, drawing_rect; - rt_ubase_t c, r, item_index; /* col and row index */ - rt_ubase_t item_width, item_height; - rtgui_image_t *image; - - if (view->items_count == 0) return; - - rtgui_widget_get_rect(RTGUI_WIDGET(view), &rect); - item_index = (view->current_item / view->page_items) * view->page_items; - - item_width = (rtgui_rect_width(rect) - 2 * LIST_MARGIN) / view->col_items; - item_height = (rtgui_rect_height(rect) - 4) / view->row_items; - image = view->items[0].image; - - for (r = 0; r < view->row_items; r ++) - { - for (c = 0; c < view->col_items; c ++) - { - if (item_index < view->items_count) - { - item_rect.y1 = rect.y1 + LIST_MARGIN + r * item_height; - item_rect.x1 = rect.x1 + LIST_MARGIN + c * item_width; - item_rect.x2 = item_rect.x1 + item_width; - item_rect.y2 = item_rect.y1 + item_height; - - if (item_index == view->current_item) - { - rtgui_theme_draw_selected(dc, &item_rect); - } - - drawing_rect.x1 = drawing_rect.y1 = 0; - drawing_rect.x2 = image->w; - drawing_rect.y2 = image->h; - rtgui_rect_moveto_align(&item_rect, &drawing_rect, RTGUI_ALIGN_CENTER_HORIZONTAL); - drawing_rect.y1 += 5; - drawing_rect.y2 += 5; - rtgui_image_blit(view->items[item_index].image, dc, &drawing_rect); - - item_rect.y1 = drawing_rect.y2 + LIST_MARGIN; - item_rect.x1 += 3; - item_rect.x2 -= 3; - rtgui_font_get_metrics(RTGUI_WIDGET_FONT(view), view->items[item_index].name, - &drawing_rect); - rtgui_rect_moveto_align(&item_rect, &drawing_rect, RTGUI_ALIGN_CENTER_HORIZONTAL); - rtgui_dc_draw_text(dc, view->items[item_index].name, &drawing_rect); - - item_index ++; - } - else break; - } - } -} - -static void rtgui_list_view_update_icon(struct rtgui_list_view *view, rt_int16_t old_item) -{ - struct rtgui_rect rect, item_rect, drawing_rect; - rt_ubase_t c, r; /* col and row index */ - rt_ubase_t item_width, item_height; - rtgui_image_t *image; - struct rtgui_dc *dc; - - if ((view->items_count == 0) || - (old_item == view->current_item)) - return; - - if (old_item / view->page_items != view->current_item / view->page_items) - { - /* it's not a same page, update all */ - rtgui_widget_update(RTGUI_WIDGET(view)); - return; - } - - dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(view)); - if (dc == RT_NULL) return; - - rtgui_widget_get_rect(RTGUI_WIDGET(view), &rect); - - item_width = (rtgui_rect_width(rect) - 2 * LIST_MARGIN) / view->col_items; - item_height = (rtgui_rect_height(rect) - 4) / view->row_items; - image = view->items[0].image; - - /* update old item */ - r = (old_item % view->page_items) / view->col_items; - c = (old_item % view->page_items) % view->col_items; - item_rect.y1 = rect.y1 + LIST_MARGIN + r * item_height; - item_rect.x1 = rect.x1 + LIST_MARGIN + c * item_width; - item_rect.x2 = item_rect.x1 + item_width; - item_rect.y2 = item_rect.y1 + item_height; - rtgui_dc_fill_rect(dc, &item_rect); - - /* draw image */ - drawing_rect.x1 = drawing_rect.y1 = 0; - drawing_rect.x2 = image->w; - drawing_rect.y2 = image->h; - rtgui_rect_moveto_align(&item_rect, &drawing_rect, RTGUI_ALIGN_CENTER_HORIZONTAL); - drawing_rect.y1 += 5; - drawing_rect.y2 += 5; - rtgui_image_blit(view->items[old_item].image, dc, &drawing_rect); - - /* draw text */ - item_rect.y1 = drawing_rect.y2 + LIST_MARGIN; - item_rect.x1 += 3; - item_rect.x2 -= 3; - rtgui_font_get_metrics(RTGUI_WIDGET_FONT(view), view->items[old_item].name, - &drawing_rect); - rtgui_rect_moveto_align(&item_rect, &drawing_rect, RTGUI_ALIGN_CENTER_HORIZONTAL); - rtgui_dc_draw_text(dc, view->items[old_item].name, &drawing_rect); - - /* update new item as selected */ - r = (view->current_item % view->page_items) / view->col_items; - c = (view->current_item % view->page_items) % view->col_items; - item_rect.y1 = rect.y1 + LIST_MARGIN + r * item_height; - item_rect.x1 = rect.x1 + LIST_MARGIN + c * item_width; - item_rect.x2 = item_rect.x1 + item_width; - item_rect.y2 = item_rect.y1 + item_height; - rtgui_theme_draw_selected(dc, &item_rect); - - /* draw image */ - drawing_rect.x1 = 0; - drawing_rect.y1 = 3; - drawing_rect.x2 = image->w; - drawing_rect.y2 = 3 + image->h; - - rtgui_rect_moveto_align(&item_rect, &drawing_rect, RTGUI_ALIGN_CENTER_HORIZONTAL); - rtgui_image_blit(view->items[view->current_item].image, dc, &drawing_rect); - - /* draw text */ - item_rect.y1 = drawing_rect.y2 + LIST_MARGIN; - item_rect.x1 += 3; - item_rect.x2 -= 3; - rtgui_font_get_metrics(RTGUI_WIDGET_FONT(view), - view->items[view->current_item].name, - &drawing_rect); - rtgui_rect_moveto_align(&item_rect, &drawing_rect, RTGUI_ALIGN_CENTER_HORIZONTAL); - rtgui_dc_draw_text(dc, view->items[view->current_item].name, &drawing_rect); - - rtgui_dc_end_drawing(dc); -} - -static void rtgui_list_view_onlistdraw(struct rtgui_list_view *view, struct rtgui_dc *dc) -{ - rt_ubase_t index, page_index; - rtgui_rect_t rect, item_rect, image_rect; - const struct rtgui_list_item *item; - - rtgui_widget_get_rect(RTGUI_WIDGET(view), &rect); - - /* get item base rect */ - item_rect = rect; - item_rect.y1 += 2; - item_rect.y2 = item_rect.y1 + (2 + rtgui_theme_get_selected_height()); - - /* get current page */ - page_index = (view->current_item / view->page_items) * view->page_items; - for (index = 0; index < view->page_items; index ++) - { - if (page_index + index >= view->items_count) break; - - item = &(view->items[page_index + index]); - - if (page_index + index == view->current_item) - { - rtgui_theme_draw_selected(dc, &item_rect); - } - item_rect.x1 += LIST_MARGIN; - - if (item->image != RT_NULL) - { - /* blit on center */ - image_rect.x1 = 0; - image_rect.y1 = 0; - image_rect.x2 = item->image->w; - image_rect.y2 = item->image->h; - - rtgui_rect_moveto_align(&item_rect, &image_rect, RTGUI_ALIGN_CENTER_VERTICAL); - rtgui_image_blit(item->image, dc, &image_rect); - item_rect.x1 += item->image->w + 2; - } - /* draw text */ - rtgui_dc_draw_text(dc, item->name, &item_rect); - - if (item->image != RT_NULL) - item_rect.x1 -= (item->image->w + 2); - item_rect.x1 -= LIST_MARGIN; - - /* move to next item position */ - item_rect.y1 += (rtgui_theme_get_selected_height() + 2); - item_rect.y2 += (rtgui_theme_get_selected_height() + 2); - } -} - -void rtgui_list_view_update_list(struct rtgui_list_view *view, rt_int16_t old_item) -{ - struct rtgui_dc *dc; - const struct rtgui_list_item *item; - rtgui_rect_t rect, item_rect; - - if (old_item / view->page_items != view->current_item / view->page_items) - { - /* it's not a same page, update all */ - rtgui_widget_update(RTGUI_WIDGET(view)); - return; - } - - dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(view)); - if (dc == RT_NULL) return; - - rtgui_widget_get_rect(RTGUI_WIDGET(view), &rect); - - /* get old item's rect and draw old item */ - item_rect.x1 = rect.x1; - item_rect.x2 = rect.x2; - item_rect.y1 = rect.y1 + 2; - item_rect.y1 += (old_item % view->page_items) * (2 + rtgui_theme_get_selected_height()); - item_rect.y2 = item_rect.y1 + (2 + rtgui_theme_get_selected_height()); - rtgui_dc_fill_rect(dc, &item_rect); - - item_rect.x1 += LIST_MARGIN; - - item = &(view->items[old_item]); - if (item->image != RT_NULL) - { - struct rtgui_rect image_rect; - - /* blit on center */ - image_rect.x1 = 0; - image_rect.y1 = 0; - image_rect.x2 = item->image->w; - image_rect.y2 = item->image->h; - - rtgui_rect_moveto_align(&item_rect, &image_rect, RTGUI_ALIGN_CENTER_VERTICAL); - rtgui_image_blit(item->image, dc, &image_rect); - item_rect.x1 += item->image->w + 2; - } - rtgui_dc_draw_text(dc, item->name, &item_rect); - - /* draw current item */ - item_rect = rect; - /* get current item's rect */ - item_rect.y1 += 2; - item_rect.y1 += (view->current_item % view->page_items) * (2 + rtgui_theme_get_selected_height()); - item_rect.y2 = item_rect.y1 + (2 + rtgui_theme_get_selected_height()); - - /* draw current item */ - rtgui_theme_draw_selected(dc, &item_rect); - - item_rect.x1 += LIST_MARGIN; - - item = &(view->items[view->current_item]); - if (item->image != RT_NULL) - { - struct rtgui_rect image_rect; - - /* blit on center */ - image_rect.x1 = 0; - image_rect.y1 = 0; - image_rect.x2 = item->image->w; - image_rect.y2 = item->image->h; - - rtgui_rect_moveto_align(&item_rect, &image_rect, RTGUI_ALIGN_CENTER_VERTICAL); - rtgui_image_blit(item->image, dc, &image_rect); - item_rect.x1 += (item->image->w + 2); - } - rtgui_dc_draw_text(dc, item->name, &item_rect); - - rtgui_dc_end_drawing(dc); -} -RTM_EXPORT(rtgui_list_view_update_list); - -void rtgui_list_view_ondraw(struct rtgui_list_view *view) -{ - struct rtgui_rect rect; - struct rtgui_dc *dc; - - dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(view)); - if (dc == RT_NULL) return; - - rtgui_widget_get_rect(RTGUI_WIDGET(view), &rect); - rtgui_dc_fill_rect(dc, &rect); - - switch (view->flag) - { - case RTGUI_LIST_VIEW_LIST: - rtgui_list_view_onlistdraw(view, dc); - break; - - case RTGUI_LIST_VIEW_ICON: - rtgui_list_view_onicondraw(view, dc); - break; - } - - rtgui_dc_end_drawing(dc); -} -RTM_EXPORT(rtgui_list_view_ondraw); - -static rt_bool_t rtgui_list_view_onmouse(struct rtgui_list_view *view, struct rtgui_event_mouse *emouse) -{ - rtgui_rect_t rect; - - /* calculate selected item */ - - /* get physical extent information */ - rtgui_widget_get_rect(RTGUI_WIDGET(view), &rect); - rtgui_widget_rect_to_device(RTGUI_WIDGET(view), &rect); - - if (rtgui_rect_contains_point(&rect, emouse->x, emouse->y) == RT_EOK) - { - rt_uint16_t index; - rt_uint16_t old_item; - - /* get old item */ - old_item = view->current_item; - - switch (view->flag) - { - case RTGUI_LIST_VIEW_LIST: - index = (emouse->y - rect.y1) / (2 + rtgui_theme_get_selected_height()); - - if ((index < view->items_count) && (index < view->page_items)) - { - /* set selected item */ - view->current_item = (view->current_item / view->page_items) * view->page_items + index; - if (emouse->button & RTGUI_MOUSE_BUTTON_DOWN) - { - /* down event */ - rtgui_list_view_update_list(view, old_item); - } - else - { - /* up event */ - if (view->items[view->current_item].action != RT_NULL) - { - view->items[view->current_item].action(RTGUI_WIDGET(view), - view->items[view->current_item].parameter); - } - } - } - break; - - case RTGUI_LIST_VIEW_ICON: - { - rt_uint16_t x, y; - rt_uint16_t item_height, item_width; - rt_ubase_t current_page; - - item_width = (rtgui_rect_width(rect) - 2 * LIST_MARGIN) / view->col_items; - item_height = (rtgui_rect_height(rect) - 4) / view->row_items; - x = emouse->x - rect.x1; - y = emouse->y - rect.y1; - - index = (y / item_height * view->col_items) + x / item_width; - current_page = view->current_item / view->page_items; - - if ((index + (current_page * view->page_items) < view->items_count)) - { - if (emouse->button & RTGUI_MOUSE_BUTTON_DOWN) - { - view->current_item = index + (current_page * view->page_items); - - /* down event */ - rtgui_list_view_update_icon(view, old_item); - } - else - { - /* up event */ - if (view->items[view->current_item].action != RT_NULL) - { - view->items[view->current_item].action(RTGUI_WIDGET(view), - view->items[view->current_item].parameter); - } - } - } - } - break; - - case RTGUI_LIST_VIEW_REPORT: - break; - } - - return RT_TRUE; - } - - return RT_FALSE; -} - -rt_bool_t rtgui_list_view_event_handler(struct rtgui_object *widget, struct rtgui_event *event) -{ - struct rtgui_list_view *view = RT_NULL; - - view = RTGUI_LIST_VIEW(widget); - switch (event->type) - { - case RTGUI_EVENT_PAINT: - rtgui_list_view_ondraw(view); - return RT_FALSE; - - case RTGUI_EVENT_RESIZE: - { - struct rtgui_event_resize *resize; - - resize = (struct rtgui_event_resize *)event; - - /* recalculate page items */ - view->page_items = resize->h / (2 + rtgui_theme_get_selected_height()); - } - break; - - case RTGUI_EVENT_MOUSE_BUTTON: - { - struct rtgui_event_mouse *emouse; - - emouse = (struct rtgui_event_mouse *)event; - return rtgui_list_view_onmouse(view, emouse); - } - - case RTGUI_EVENT_KBD: - { - struct rtgui_event_kbd *ekbd = (struct rtgui_event_kbd *)event; - if (ekbd->type == RTGUI_KEYDOWN) - { - rt_uint16_t old_item; - - if (view->current_item == -1) - { - /* set a initial item */ - if ((view->items_count > 0) && - (ekbd->key == RTGUIK_UP || ekbd->key == RTGUIK_DOWN)) - { - view->current_item = 0; - if (view->flag == RTGUI_LIST_VIEW_LIST) - rtgui_list_view_update_list(view, -1); - else - rtgui_list_view_update_icon(view, -1); - break; - } - else return RT_FALSE; - } - - old_item = view->current_item; - switch (ekbd->key) - { - case RTGUIK_LEFT: - if (view->flag == RTGUI_LIST_VIEW_LIST) - { - if (view->current_item - view->page_items >= 0) - view->current_item -= view->page_items; - - rtgui_list_view_update_list(view, old_item); - } - else if (view->flag == RTGUI_LIST_VIEW_ICON) - { - if (view->current_item > 0) - view->current_item --; - rtgui_list_view_update_icon(view, old_item); - } - return RT_FALSE; - - case RTGUIK_UP: - if (view->flag == RTGUI_LIST_VIEW_LIST) - { - if (view->current_item > 0) - view->current_item --; - rtgui_list_view_update_list(view, old_item); - } - else if (view->flag == RTGUI_LIST_VIEW_ICON) - { - if (view->current_item >= view->col_items) - view->current_item -= view->col_items; - else - view->current_item = 0; - - rtgui_list_view_update_icon(view, old_item); - } - return RT_FALSE; - - case RTGUIK_RIGHT: - if (view->flag == RTGUI_LIST_VIEW_LIST) - { - if (view->current_item + view->page_items < view->items_count - 1) - view->current_item += view->page_items; - - rtgui_list_view_update_list(view, old_item); - } - else if (view->flag == RTGUI_LIST_VIEW_ICON) - { - if (view->current_item < view->items_count - 1) - view->current_item ++; - - rtgui_list_view_update_icon(view, old_item); - } - return RT_FALSE; - - case RTGUIK_DOWN: - if (view->flag == RTGUI_LIST_VIEW_LIST) - { - if (view->current_item < view->items_count - 1) - view->current_item ++; - - rtgui_list_view_update_list(view, old_item); - } - else if (view->flag == RTGUI_LIST_VIEW_ICON) - { - if (view->current_item + view->col_items <= (view->items_count - 1)) - view->current_item += view->col_items; - else - view->current_item = view->items_count - 1; - - rtgui_list_view_update_icon(view, old_item); - } - return RT_FALSE; - - case RTGUIK_RETURN: - if (view->items[view->current_item].action != RT_NULL) - { - view->items[view->current_item].action(RTGUI_WIDGET(view), - view->items[view->current_item].parameter); - } - return RT_FALSE; - - default: - break; - } - } - } - return RT_FALSE; - - default: - - /* use view event handler */ - return rtgui_container_event_handler(widget, event); - } - - return RT_FALSE; -} -RTM_EXPORT(rtgui_list_view_event_handler); - -static void rtgui_list_view_calc(struct rtgui_list_view *view) -{ - rtgui_rect_t rect; - rt_uint32_t image_width, image_height; - rt_ubase_t text_width, text_height; - rt_ubase_t item_width, item_height; - - if (view->items_count == 0) return; - - /* get image width and height */ - if (view->items[0].image != RT_NULL) - { - image_width = view->items[0].image->w; - image_height = view->items[0].image->h; - } - else - { - image_width = 0; - image_height = 0; - } - - rtgui_font_get_metrics(RTGUI_WIDGET_FONT(view), "HHHHHH", &rect); - - text_height = rtgui_rect_height(rect); - text_width = rtgui_rect_width(rect); - - rtgui_widget_get_rect(RTGUI_WIDGET(view), &rect); - - item_width = (image_width + LIST_MARGIN); - if (item_width < (text_width + LIST_MARGIN)) item_width = text_width + LIST_MARGIN; - item_height = image_height + 8 + text_height + LIST_MARGIN; - if (item_width > item_height) item_height = item_width; - else item_width = item_height; - - view->row_items = (rtgui_rect_height(rect) - 2 * LIST_MARGIN) / item_height; - view->col_items = (rtgui_rect_width(rect) - 2 * LIST_MARGIN) / item_width; - view->page_items = view->row_items * view->col_items; -} - -rtgui_list_view_t *rtgui_list_view_create(const struct rtgui_list_item *items, rt_uint16_t count, - rtgui_rect_t *rect, rt_uint16_t flag) -{ - struct rtgui_list_view *view = RT_NULL; - - view = (struct rtgui_list_view *) rtgui_widget_create(RTGUI_LIST_VIEW_TYPE); - if (view != RT_NULL) - { - view->items = items; - view->items_count = count; - - view->flag = flag; - rtgui_widget_set_rect(RTGUI_WIDGET(view), rect); - - if (flag == RTGUI_LIST_VIEW_LIST) - view->page_items = rtgui_rect_height(*rect) / (2 + rtgui_theme_get_selected_height()); - else if ((flag == RTGUI_LIST_VIEW_ICON) && (count > 0)) - { - rtgui_list_view_calc(view); - } - } - - return view; -} -RTM_EXPORT(rtgui_list_view_create); - -void rtgui_list_view_destroy(rtgui_list_view_t *view) -{ - /* destroy view */ - rtgui_widget_destroy(RTGUI_WIDGET(view)); -} -RTM_EXPORT(rtgui_list_view_destroy); - diff --git a/components/rtgui/widgets/listbox.c b/components/rtgui/widgets/listbox.c deleted file mode 100644 index 901624e021..0000000000 --- a/components/rtgui/widgets/listbox.c +++ /dev/null @@ -1,436 +0,0 @@ -/* - * File : listbox.c - * This file is part of RTGUI in RT-Thread RTOS - * COPYRIGHT (C) 2010, 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 - * 2010-01-06 Bernard first version - */ - -#include <rtgui/rtgui_theme.h> -#include <rtgui/widgets/listbox.h> - -#define LIST_MARGIN 5 -static rt_bool_t rtgui_listbox_onunfocus(struct rtgui_object *object, rtgui_event_t *event); - -static void _rtgui_listbox_constructor(struct rtgui_listbox *box) -{ - /* set default widget rect and set event handler */ - rtgui_object_set_event_handler(RTGUI_OBJECT(box), rtgui_listbox_event_handler); - rtgui_widget_set_onunfocus(RTGUI_WIDGET(box), rtgui_listbox_onunfocus); - RTGUI_WIDGET(box)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE; - - box->current_item = -1; - box->items_count = 0; - box->page_items = 1; - box->on_item = 0; - - RTGUI_WIDGET_BACKGROUND(box) = white; - RTGUI_WIDGET_TEXTALIGN(box) = RTGUI_ALIGN_CENTER_VERTICAL; -} - -DEFINE_CLASS_TYPE(listbox, "listbox", - RTGUI_WIDGET_TYPE, - _rtgui_listbox_constructor, - RT_NULL, - sizeof(struct rtgui_listbox)); - -void rtgui_listbox_ondraw(struct rtgui_listbox *box) -{ - struct rtgui_dc *dc; - rt_uint16_t page_index, index; - const struct rtgui_listbox_item *item; - struct rtgui_rect rect, item_rect, image_rect; - - dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(box)); - if (dc == RT_NULL) return; - - rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect); - rtgui_dc_fill_rect(dc, &rect); - - rect.x2 -= 1; - rect.y2 -= 1; - /* draw focused border */ - if (RTGUI_WIDGET_IS_FOCUSED(box)) - rtgui_dc_draw_focus_rect(dc, &rect); - - /* get item base rect */ - item_rect = rect; - item_rect.x1 += 1; - item_rect.x2 -= 1; - item_rect.y1 += 2; - item_rect.y2 = item_rect.y1 + (2 + rtgui_theme_get_selected_height()); - - /* get current page */ - if (box->current_item == -1) - page_index = 0; - else - page_index = (box->current_item / box->page_items) * box->page_items; - - for (index = 0; index < box->page_items; index ++) - { - if (page_index + index >= box->items_count) break; - - item = &(box->items[page_index + index]); - - if (page_index + index == box->current_item) - { - rtgui_theme_draw_selected(dc, &item_rect); - } - item_rect.x1 += LIST_MARGIN; - - if (item->image != RT_NULL) - { - rtgui_image_get_rect(item->image, &image_rect); - rtgui_rect_moveto_align(&item_rect, &image_rect, RTGUI_ALIGN_CENTER_VERTICAL); - - rtgui_image_blit(item->image, dc, &image_rect); - item_rect.x1 += item->image->w + 2; - } - /* draw text */ - rtgui_dc_draw_text(dc, item->name, &item_rect); - - if (item->image != RT_NULL) - item_rect.x1 -= (item->image->w + 2); - item_rect.x1 -= LIST_MARGIN; - - /* move to next item position */ - item_rect.y1 += (rtgui_theme_get_selected_height() + 2); - item_rect.y2 += (rtgui_theme_get_selected_height() + 2); - } - rtgui_dc_end_drawing(dc); -} -RTM_EXPORT(rtgui_listbox_ondraw); - -static void rtgui_listbox_update_current(struct rtgui_listbox *box, rt_int16_t old_item) -{ - struct rtgui_dc *dc; - const struct rtgui_listbox_item *item; - rtgui_rect_t rect, item_rect, image_rect; - - if ((old_item == -1) || (old_item / box->page_items != box->current_item / box->page_items)) - { - /* it's not a same page, update all */ - rtgui_widget_update(RTGUI_WIDGET(box)); - return; - } - - dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(box)); - if (dc == RT_NULL) return; - - rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect); - rect.x2 -= 1; - rect.y2 -= 1; - - item_rect = rect; - /* get old item's rect */ - item_rect.x1 += 1; - item_rect.x2 -= 1; - item_rect.y1 += 2; - item_rect.y1 += (old_item % box->page_items) * (2 + rtgui_theme_get_selected_height()); - item_rect.y2 = item_rect.y1 + (2 + rtgui_theme_get_selected_height()); - - /* draw old item */ - rtgui_dc_fill_rect(dc, &item_rect); - - item_rect.x1 += LIST_MARGIN; - - item = &(box->items[old_item]); - if (item->image != RT_NULL) - { - rtgui_image_get_rect(item->image, &image_rect); - rtgui_rect_moveto_align(&item_rect, &image_rect, RTGUI_ALIGN_CENTER_VERTICAL); - - rtgui_image_blit(item->image, dc, &image_rect); - item_rect.x1 += item->image->w + 2; - } - rtgui_dc_draw_text(dc, item->name, &item_rect); - - /* draw current item */ - item_rect = rect; - /* get current item's rect */ - item_rect.x1 += 1; - item_rect.x2 -= 1; - item_rect.y1 += 2; - item_rect.y1 += (box->current_item % box->page_items) * (2 + rtgui_theme_get_selected_height()); - item_rect.y2 = item_rect.y1 + (2 + rtgui_theme_get_selected_height()); - - /* draw current item */ - rtgui_theme_draw_selected(dc, &item_rect); - - item_rect.x1 += LIST_MARGIN; - - item = &(box->items[box->current_item]); - if (item->image != RT_NULL) - { - rtgui_image_get_rect(item->image, &image_rect); - rtgui_rect_moveto_align(&item_rect, &image_rect, RTGUI_ALIGN_CENTER_VERTICAL); - - rtgui_image_blit(item->image, dc, &image_rect); - item_rect.x1 += (item->image->w + 2); - } - rtgui_dc_draw_text(dc, item->name, &item_rect); - - rtgui_dc_end_drawing(dc); -} - -rt_bool_t rtgui_listbox_event_handler(struct rtgui_object *object, struct rtgui_event *event) -{ - struct rtgui_listbox *box; - RTGUI_WIDGET_EVENT_HANDLER_PREPARE - - box = RTGUI_LISTBOX(object); - switch (event->type) - { - case RTGUI_EVENT_PAINT: - rtgui_listbox_ondraw(box); - return RT_FALSE; - - case RTGUI_EVENT_RESIZE: - { - struct rtgui_event_resize *resize; - - resize = (struct rtgui_event_resize *)event; - - /* recalculate page items */ - box->page_items = resize->h / (2 + rtgui_theme_get_selected_height()); - } - break; - - case RTGUI_EVENT_MOUSE_BUTTON: - { - rtgui_rect_t rect; - struct rtgui_event_mouse *emouse; - - emouse = (struct rtgui_event_mouse *)event; - - /* calculate selected item */ - - /* get physical extent information */ - rtgui_widget_get_rect(widget, &rect); - rtgui_widget_rect_to_device(widget, &rect); - - if ((rtgui_rect_contains_point(&rect, emouse->x, emouse->y) == RT_EOK) && (box->items_count > 0)) - { - rt_uint16_t index; - index = (emouse->y - rect.y1) / (2 + rtgui_theme_get_selected_height()); - - /* set focus */ - rtgui_widget_focus(widget); - { - struct rtgui_rect rect; - struct rtgui_dc *dc; - - dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(box)); - if (dc != RT_NULL) - { - /* get widget rect */ - rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect); - /* update focus border */ - rect.x2 -= 1; - rect.y2 -= 1; - /* draw focused border */ - if (RTGUI_WIDGET_IS_FOCUSED(box)) - rtgui_dc_draw_focus_rect(dc, &rect); - rtgui_dc_end_drawing(dc); - } - } - - if ((index < box->items_count) && (index < box->page_items)) - { - rt_uint16_t old_item; - - old_item = box->current_item; - - /* set selected item */ - box->current_item = (box->current_item / box->page_items) * box->page_items + index; - if (emouse->button & RTGUI_MOUSE_BUTTON_DOWN) - { - /* down event */ - rtgui_listbox_update_current(box, old_item); - } - else - { - /* up event */ - if (box->on_item != RT_NULL) - { - box->on_item(RTGUI_OBJECT(box), RT_NULL); - } - } - } - } - - return RT_TRUE; - } - - case RTGUI_EVENT_KBD: - { - struct rtgui_event_kbd *ekbd = (struct rtgui_event_kbd *)event; - if ((ekbd->type == RTGUI_KEYDOWN) && (box->items_count > 0)) - { - rt_int16_t old_item; - - if (box->current_item == -1) - { - /* set a initial item */ - if ((box->items_count > 0) && - (ekbd->key == RTGUIK_UP || ekbd->key == RTGUIK_DOWN)) - { - box->current_item = 0; - rtgui_listbox_update_current(box, -1); - return RT_TRUE; - } - else - return RT_FALSE; - } - old_item = box->current_item; - - switch (ekbd->key) - { - case RTGUIK_LEFT: - if (box->current_item - box->page_items >= 0) - box->current_item -= box->page_items; - rtgui_listbox_update_current(box, old_item); - return RT_TRUE; - - case RTGUIK_UP: - if (box->current_item > 0) - box->current_item --; - rtgui_listbox_update_current(box, old_item); - return RT_TRUE; - - case RTGUIK_RIGHT: - if (box->current_item + box->page_items < box->items_count - 1) - box->current_item += box->page_items; - rtgui_listbox_update_current(box, old_item); - return RT_TRUE; - - case RTGUIK_DOWN: - if (box->current_item < box->items_count - 1) - box->current_item ++; - rtgui_listbox_update_current(box, old_item); - return RT_TRUE; - - case RTGUIK_RETURN: - if (box->on_item != RT_NULL) - box->on_item(RTGUI_OBJECT(box), RT_NULL); - return RT_TRUE; - - default: - break; - } - } - } - return RT_FALSE; - default: - /* use box event handler */ - return rtgui_widget_event_handler(RTGUI_OBJECT(widget), event); - } - - return RT_FALSE; -} -RTM_EXPORT(rtgui_listbox_event_handler); - -rtgui_listbox_t *rtgui_listbox_create(const struct rtgui_listbox_item *items, rt_uint16_t count, rtgui_rect_t *rect) -{ - struct rtgui_listbox *box = RT_NULL; - - box = (struct rtgui_listbox *) rtgui_widget_create(RTGUI_LISTBOX_TYPE); - if (box != RT_NULL) - { - box->items = items; - box->items_count = count; - - box->page_items = rtgui_rect_height(*rect) / (2 + rtgui_theme_get_selected_height()); - if (box->page_items == 0) box->page_items = 1; - rtgui_widget_set_rect(RTGUI_WIDGET(box), rect); - } - - return box; -} -RTM_EXPORT(rtgui_listbox_create); - -void rtgui_listbox_destroy(rtgui_listbox_t *box) -{ - /* destroy box */ - rtgui_widget_destroy(RTGUI_WIDGET(box)); -} -RTM_EXPORT(rtgui_listbox_destroy); - -void rtgui_listbox_set_onitem(rtgui_listbox_t *box, rtgui_event_handler_ptr func) -{ - RT_ASSERT(box != RT_NULL); - - box->on_item = func; -} -RTM_EXPORT(rtgui_listbox_set_onitem); - -void rtgui_listbox_set_items(rtgui_listbox_t *box, struct rtgui_listbox_item *items, rt_uint16_t count) -{ - rtgui_rect_t rect; - - box->items = items; - box->items_count = count; - box->current_item = -1; - - rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect); - box->page_items = rtgui_rect_height(rect) / (2 + rtgui_theme_get_selected_height()); - - rtgui_widget_update(RTGUI_WIDGET(box)); -} -RTM_EXPORT(rtgui_listbox_set_items); - -void rtgui_listbox_set_current_item(rtgui_listbox_t *box, int index) -{ - RT_ASSERT(box != RT_NULL); - - if (index >= box->items_count) - return; - - if (index != box->current_item) - { - int old_item; - - old_item = box->current_item; - box->current_item = index; - rtgui_listbox_update_current(box, old_item); - } - if (box->on_item) - box->on_item(RTGUI_OBJECT(box), RT_NULL); -} -RTM_EXPORT(rtgui_listbox_set_current_item); - -static rt_bool_t rtgui_listbox_onunfocus(struct rtgui_object *object, rtgui_event_t *event) -{ - rtgui_rect_t rect; - rtgui_widget_t *widget; - struct rtgui_dc *dc; - - RT_ASSERT(object); - widget = RTGUI_WIDGET(object); - - dc = rtgui_dc_begin_drawing(widget); - if (dc == RT_NULL) return RT_FALSE; - - rtgui_widget_get_rect(widget, &rect); - - if (!RTGUI_WIDGET_IS_FOCUSED(widget)) - { - /* only clear focus rect */ - rtgui_color_t color; - rect.x2 -= 1; - rect.y2 -= 1; - color = RTGUI_DC_FC(dc); - RTGUI_DC_FC(dc) = RTGUI_DC_BC(dc); - rtgui_dc_draw_focus_rect(dc, &rect); - RTGUI_DC_FC(dc) = color; - } - - rtgui_dc_end_drawing(dc); - return RT_TRUE; -} diff --git a/components/rtgui/widgets/listctrl.c b/components/rtgui/widgets/listctrl.c deleted file mode 100644 index ee74bb2615..0000000000 --- a/components/rtgui/widgets/listctrl.c +++ /dev/null @@ -1,472 +0,0 @@ -/* - * File : listctrl.c - * This file is part of RTGUI in RT-Thread RTOS - * COPYRIGHT (C) 2010, 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 - * 2010-01-06 Bernard first version - */ - -#include <rtgui/rtgui_theme.h> -#include <rtgui/widgets/listctrl.h> - -static void rtgui_listctrl_update_current(struct rtgui_listctrl *ctrl, rt_uint16_t old_item); - -static void _rtgui_listctrl_constructor(struct rtgui_listctrl *ctrl) -{ - /* set default widget rect and set event handler */ - rtgui_object_set_event_handler(RTGUI_OBJECT(ctrl), rtgui_listctrl_event_handler); - - RTGUI_WIDGET(ctrl)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE; - - ctrl->current_item = -1; - ctrl->item_height = rtgui_theme_get_selected_height(); - ctrl->items_count = 0; - ctrl->page_items = 0; - ctrl->on_item = 0; - ctrl->on_item_draw = RT_NULL; - - RTGUI_WIDGET_BACKGROUND(ctrl) = white; - RTGUI_WIDGET_TEXTALIGN(ctrl) = RTGUI_ALIGN_CENTER_VERTICAL; -} - -DEFINE_CLASS_TYPE(listctrl, "listctrl", - RTGUI_WIDGET_TYPE, - _rtgui_listctrl_constructor, - RT_NULL, - sizeof(struct rtgui_listctrl)); - -static void _rtgui_listctrl_get_rect(struct rtgui_listctrl *ctrl, rtgui_rect_t *rect) -{ - rtgui_widget_get_rect(RTGUI_WIDGET(ctrl), rect); - if (ctrl->items_count > rtgui_rect_height(*rect) / ctrl->item_height) - { - rect->x2 = rect->x2 - 8; - } -} - -static void _rtgui_listctrl_get_scrollbar_rect(struct rtgui_listctrl *ctrl, rtgui_rect_t *rect) -{ - rtgui_widget_get_rect(RTGUI_WIDGET(ctrl), rect); - if (ctrl->items_count > rtgui_rect_height(*rect) / ctrl->item_height) - { - rect->x1 = rect->x2 - 8; - } - else - { - /* no scrollbar */ - rt_memset(rect, 0, sizeof(rtgui_rect_t)); - } -} - -static void _rtgui_listctrl_scrollbar_ondraw(struct rtgui_listctrl *ctrl, struct rtgui_dc *dc) -{ - rtgui_rect_t rect; - rt_uint32_t height, y1; - - /* get scrollbar rect */ - _rtgui_listctrl_get_scrollbar_rect(ctrl, &rect); - if (rtgui_rect_is_empty(&rect) == RT_TRUE) return; - - rtgui_dc_fill_rect(dc, &rect); - - height = rtgui_rect_height(rect); - height = height / ((ctrl->items_count + (ctrl->page_items - 1)) / ctrl->page_items); - y1 = (ctrl->current_item / ctrl->page_items) * height; - - rect.y1 = rect.y1 + y1; - rect.y2 = rect.y1 + height; - rect.x1 -= 3; - rtgui_theme_draw_selected(dc, &rect); -} - -static void _rtgui_listctrl_scrollbar_onmouse(struct rtgui_listctrl *ctrl, struct rtgui_event_mouse *mouse) -{ - rtgui_rect_t rect; - rt_uint32_t height, y1; - rt_uint16_t old_item; - - /* get scrollbar rect */ - _rtgui_listctrl_get_scrollbar_rect(ctrl, &rect); - height = rtgui_rect_height(rect); - height = height / ((ctrl->items_count + (ctrl->page_items - 1)) / ctrl->page_items); - y1 = (ctrl->current_item / ctrl->page_items) * height; - - rect.y1 = rect.y1 + y1; - rect.y2 = rect.y1 + height; - rtgui_widget_rect_to_device(RTGUI_WIDGET(ctrl), &rect); - - old_item = ctrl->current_item; - if (mouse->y < rect.y1) - { - if (ctrl->current_item - ctrl->page_items >= 0) - rtgui_listctrl_set_current_item(ctrl, ctrl->current_item - ctrl->page_items); - else - rtgui_listctrl_update_current(ctrl, old_item); - } - else if (mouse->y > rect.y2) - { - rt_uint16_t new_item; - if (ctrl->current_item + ctrl->page_items < ctrl->items_count - 1) - new_item = ctrl->current_item + ctrl->page_items; - else - new_item = ((ctrl->current_item / ctrl->page_items) + 1) * ctrl->page_items; - rtgui_listctrl_set_current_item(ctrl, new_item); - } -} - -static void _rtgui_listctrl_ondraw(struct rtgui_listctrl *ctrl) -{ - struct rtgui_rect rect, item_rect; - struct rtgui_dc *dc; - rt_uint16_t page_index, index; - - dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(ctrl)); - if (dc == RT_NULL) return; - - _rtgui_listctrl_get_rect(ctrl, &rect); - rtgui_dc_fill_rect(dc, &rect); - - rect.x2 -= 1; - rect.y2 -= 1; - - /* get item base rect */ - item_rect = rect; - item_rect.x1 += 1; - item_rect.x2 -= 1; - item_rect.y1 += 2; - item_rect.y2 = item_rect.y1 + (2 + ctrl->item_height); - - /* get current page */ - page_index = (ctrl->current_item / ctrl->page_items) * ctrl->page_items; - for (index = 0; index < ctrl->page_items; index ++) - { - if (page_index + index >= ctrl->items_count) break; - - if (page_index + index == ctrl->current_item) - { - rtgui_theme_draw_selected(dc, &item_rect); - } - - if (ctrl->on_item_draw != RT_NULL) - { - ctrl->on_item_draw(ctrl, dc, &item_rect, page_index + index); - } - - /* move to next item position */ - item_rect.y1 += (ctrl->item_height + 2); - item_rect.y2 += (ctrl->item_height + 2); - } - - /* draw scrollbar */ - _rtgui_listctrl_scrollbar_ondraw(ctrl, dc); - rtgui_dc_end_drawing(dc); -} - -static void rtgui_listctrl_update_current(struct rtgui_listctrl *ctrl, rt_uint16_t old_item) -{ - struct rtgui_dc *dc; - rtgui_rect_t rect, item_rect; - - if (old_item / ctrl->page_items != ctrl->current_item / ctrl->page_items) - { - /* it's not a same page, update all */ - rtgui_widget_update(RTGUI_WIDGET(ctrl)); - return; - } - - dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(ctrl)); - if (dc == RT_NULL) return; - - _rtgui_listctrl_get_rect(ctrl, &rect); - rect.x2 -= 1; - rect.y2 -= 1; - - item_rect = rect; - /* get old item's rect */ - item_rect.x1 += 1; - item_rect.x2 -= 1; - item_rect.y1 += 2; - item_rect.y1 += (old_item % ctrl->page_items) * (2 + ctrl->item_height); - item_rect.y2 = item_rect.y1 + (2 + ctrl->item_height); - - /* draw old item */ - rtgui_dc_fill_rect(dc, &item_rect); - if (ctrl->on_item_draw != RT_NULL) - ctrl->on_item_draw(ctrl, dc, &item_rect, old_item); - - /* draw current item */ - item_rect = rect; - /* get current item's rect */ - item_rect.x1 += 1; - item_rect.x2 -= 1; - item_rect.y1 += 2; - item_rect.y1 += (ctrl->current_item % ctrl->page_items) * (2 + ctrl->item_height); - item_rect.y2 = item_rect.y1 + (2 + ctrl->item_height); - - /* draw current item */ - rtgui_theme_draw_selected(dc, &item_rect); - if (ctrl->on_item_draw != RT_NULL) - ctrl->on_item_draw(ctrl, dc, &item_rect, ctrl->current_item); - - rtgui_dc_end_drawing(dc); -} - -rt_bool_t rtgui_listctrl_event_handler(struct rtgui_object *object, struct rtgui_event *event) -{ - struct rtgui_listctrl *ctrl; - RTGUI_WIDGET_EVENT_HANDLER_PREPARE - - ctrl = RTGUI_LISTCTRL(object); - switch (event->type) - { - case RTGUI_EVENT_PAINT: - _rtgui_listctrl_ondraw(ctrl); - return RT_FALSE; - - case RTGUI_EVENT_RESIZE: - { - struct rtgui_event_resize *resize; - - resize = (struct rtgui_event_resize *)event; - - /* recalculate page items */ - ctrl->page_items = resize->h / (2 + ctrl->item_height); - } - break; - - case RTGUI_EVENT_MOUSE_BUTTON: - { - rtgui_rect_t rect; - struct rtgui_event_mouse *emouse; - - emouse = (struct rtgui_event_mouse *)event; - - /* get scrollbar rect */ - _rtgui_listctrl_get_scrollbar_rect(ctrl, &rect); - rtgui_widget_rect_to_device(RTGUI_WIDGET(ctrl), &rect); - if (rtgui_rect_contains_point(&rect, emouse->x, emouse->y) == RT_EOK) - { - _rtgui_listctrl_scrollbar_onmouse(ctrl, emouse); - return RT_TRUE; - } - - /* calculate selected item */ - - /* get physical extent information */ - _rtgui_listctrl_get_rect(ctrl, &rect); - rtgui_widget_rect_to_device(widget, &rect); - - if ((rtgui_rect_contains_point(&rect, emouse->x, emouse->y) == RT_EOK) && - (ctrl->items_count > 0)) - { - rt_uint16_t index; - index = (emouse->y - rect.y1) / (2 + ctrl->item_height); - - /* set focus */ - rtgui_widget_focus(widget); - { - struct rtgui_rect rect; - struct rtgui_dc *dc; - - dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(ctrl)); - if (dc != RT_NULL) - { - /* get widget rect */ - rtgui_widget_get_rect(RTGUI_WIDGET(ctrl), &rect); - /* update focus border */ - rect.x2 -= 1; - rect.y2 -= 1; - rtgui_dc_end_drawing(dc); - } - } - - if ((index < ctrl->page_items) && - (ctrl->current_item / ctrl->page_items)* ctrl->page_items + index < ctrl->items_count) - { - if (emouse->button & RTGUI_MOUSE_BUTTON_DOWN) - { - rtgui_listctrl_set_current_item(ctrl, - (ctrl->current_item / ctrl->page_items) * ctrl->page_items + index); - } - } - } - - return RT_TRUE; - } - - case RTGUI_EVENT_KBD: - { - struct rtgui_event_kbd *ekbd = (struct rtgui_event_kbd *)event; - if ((ekbd->type == RTGUI_KEYDOWN) && (ctrl->items_count > 0)) - { - switch (ekbd->key) - { - case RTGUIK_LEFT: - if (ctrl->current_item - ctrl->page_items >= 0) - rtgui_listctrl_set_current_item(ctrl, ctrl->current_item - ctrl->page_items); - return RT_TRUE; - - case RTGUIK_UP: - if (ctrl->current_item > 0) - rtgui_listctrl_set_current_item(ctrl, ctrl->current_item-1); - return RT_TRUE; - - case RTGUIK_RIGHT: - { - rt_uint16_t new_item = ctrl->items_count; - if (ctrl->current_item + ctrl->page_items < ctrl->items_count - 1) - new_item = ctrl->current_item + ctrl->page_items; - else - { - if ((((ctrl->current_item / ctrl->page_items) + 1) * ctrl->page_items) - < ctrl->items_count - 1) - new_item = ((ctrl->current_item / ctrl->page_items) + 1) * ctrl->page_items; - } - rtgui_listctrl_set_current_item(ctrl, new_item); - } - return RT_TRUE; - - case RTGUIK_DOWN: - if (ctrl->current_item < ctrl->items_count - 1) - rtgui_listctrl_set_current_item(ctrl, ctrl->current_item+1); - return RT_TRUE; - - case RTGUIK_RETURN: - if (ctrl->on_item != RT_NULL) - { - return ctrl->on_item(RTGUI_OBJECT(ctrl), RT_NULL); - } - return RT_FALSE; - - default: - break; - } - } - } - return RT_FALSE; - default: - /* use ctrl event handler */ - return rtgui_widget_event_handler(RTGUI_OBJECT(widget), event); - } - - return RT_FALSE; -} -RTM_EXPORT(rtgui_listctrl_event_handler); - -rtgui_listctrl_t *rtgui_listctrl_create(void *items, rt_uint16_t count, rtgui_rect_t *rect, - rtgui_onitem_draw_t ondraw) -{ - struct rtgui_listctrl *ctrl = RT_NULL; - - ctrl = (struct rtgui_listctrl *) rtgui_widget_create(RTGUI_LISTCTRL_TYPE); - if (ctrl != RT_NULL) - { - ctrl->items = items; - ctrl->items_count = count; - ctrl->on_item_draw = ondraw; - - ctrl->page_items = rtgui_rect_height(*rect) / (2 + ctrl->item_height); - rtgui_widget_set_rect(RTGUI_WIDGET(ctrl), rect); - } - - return ctrl; -} -RTM_EXPORT(rtgui_listctrl_create); - -void rtgui_listctrl_destroy(rtgui_listctrl_t *ctrl) -{ - /* destroy ctrl */ - rtgui_widget_destroy(RTGUI_WIDGET(ctrl)); -} -RTM_EXPORT(rtgui_listctrl_destroy); - -void rtgui_listctrl_set_onitem(rtgui_listctrl_t *ctrl, rtgui_event_handler_ptr func) -{ - RT_ASSERT(ctrl != RT_NULL); - - ctrl->on_item = func; -} -RTM_EXPORT(rtgui_listctrl_set_onitem); - -void rtgui_listctrl_set_items(rtgui_listctrl_t *ctrl, void *items, rt_uint16_t count) -{ - rtgui_rect_t rect; - - ctrl->items = items; - ctrl->items_count = count; - ctrl->current_item = 0; - - rtgui_widget_get_rect(RTGUI_WIDGET(ctrl), &rect); - ctrl->page_items = rtgui_rect_height(rect) / (2 + ctrl->item_height); - - rtgui_widget_update(RTGUI_WIDGET(ctrl)); -} -RTM_EXPORT(rtgui_listctrl_set_items); - -/** - * @brief set the selected(current) item of listctrl widget. - * - * If the index is greater than items_count, it will have no effect. Otherwise, - * the item on @param index will be selected and the on_item will be invoked if - * it has one. - * - * @fixme set current to the items which is not in current page won't update - * the scrollbar. - */ -void rtgui_listctrl_set_current_item(struct rtgui_listctrl *ctrl, rt_uint16_t index) -{ - RT_ASSERT(ctrl); - - if (index >= ctrl->items_count) - return; - - if (index != ctrl->current_item) - { - rt_uint16_t old_item = ctrl->current_item; - ctrl->current_item = index; - rtgui_listctrl_update_current(ctrl, old_item); - } - if (ctrl->on_item != RT_NULL) - { - ctrl->on_item(RTGUI_OBJECT(ctrl), RT_NULL); - } -} -RTM_EXPORT(rtgui_listctrl_set_current_item); - -rt_bool_t rtgui_listctrl_get_item_rect(rtgui_listctrl_t *ctrl, rt_uint16_t item, rtgui_rect_t *item_rect) -{ - if (item < ctrl->items_count) - { - rt_uint16_t index; - - /* check whether this item in current page */ - index = (ctrl->current_item / ctrl->page_items) * ctrl->page_items; - if (index > item || index + ctrl->page_items <= item) return RT_FALSE; - - rtgui_widget_get_extent(RTGUI_WIDGET(ctrl), item_rect); - item_rect->y1 -= 2; - item_rect->y1 += (item % ctrl->page_items) * (2 + ctrl->item_height); - item_rect->y2 = item_rect->y1 + (2 + ctrl->item_height); - return RT_TRUE; - } - - return RT_FALSE; -} -RTM_EXPORT(rtgui_listctrl_get_item_rect); - -void rtgui_listctrl_set_itemheight(struct rtgui_listctrl *ctrl, int height) -{ - RT_ASSERT(ctrl != RT_NULL); - if (height <= 0) return; - - ctrl->item_height = height; - ctrl->page_items = rtgui_rect_height(RTGUI_WIDGET(ctrl)->extent) / (2 + ctrl->item_height); -} -RTM_EXPORT(rtgui_listctrl_set_itemheight); - diff --git a/components/rtgui/widgets/menu.c b/components/rtgui/widgets/menu.c deleted file mode 100644 index ce6f719120..0000000000 --- a/components/rtgui/widgets/menu.c +++ /dev/null @@ -1,294 +0,0 @@ -#include <rtgui/dc.h> -#include <rtgui/widgets/menu.h> -#include <rtgui/rtgui_theme.h> - -static rt_bool_t rtgui_menu_on_deactivate(struct rtgui_object *object, rtgui_event_t *event); -const static rt_uint8_t right_arrow[] = {0x80, 0xc0, 0xe0, 0xf0, 0xe0, 0xc0, 0x80}; - -static void _rtgui_menu_constructor(rtgui_menu_t *menu) -{ - /* set window style */ - RTGUI_WIN(menu)->style = RTGUI_WIN_STYLE_NO_TITLE; - - /* set deactivate handler */ - rtgui_win_set_ondeactivate(RTGUI_WIN(menu), rtgui_menu_on_deactivate); - - /* set proper of control */ - menu->parent_menu = RT_NULL; - menu->sub_menu = RT_NULL; - - menu->items = RT_NULL; - menu->items_count = 0; - menu->items_list = RT_NULL; - - menu->on_menupop = RT_NULL; - menu->on_menuhide = RT_NULL; -} - -static void _rtgui_menu_destructor(rtgui_menu_t *menu) -{ - if (menu->sub_menu != RT_NULL) - { - rtgui_menu_destroy(menu->sub_menu); - menu->sub_menu = RT_NULL; - } - - rtgui_listctrl_destroy(menu->items_list); - menu->items_list = RT_NULL; -} - -static rt_bool_t _rtgui_menu_onitem(struct rtgui_object *object, struct rtgui_event *event) -{ - struct rtgui_menu *menu; - - /* event will be NULL, don't check it. */ - RT_ASSERT(object); - - /* get menu */ - menu = RTGUI_MENU(rtgui_widget_get_toplevel(RTGUI_WIDGET(object))); - if (menu->items[menu->items_list->current_item].type == RTGUI_ITEM_SUBMENU) - { - const rtgui_menu_item_t *items; - rt_uint16_t count; - rtgui_rect_t item_rect; - - items = (rtgui_menu_item_t *)menu->items[menu->items_list->current_item].submenu; - count = menu->items[menu->items_list->current_item].submenu_count; - if (menu->sub_menu != RT_NULL) - { - if (menu->sub_menu->items == items) - { - if (!RTGUI_WIDGET_IS_HIDE(menu->sub_menu)) - { - /* hide this sub menu */ - rtgui_win_hide(RTGUI_WIN(menu->sub_menu)); - return RT_FALSE; - } - - /* show this sub menu */ - rtgui_listctrl_get_item_rect(menu->items_list, menu->items_list->current_item, &item_rect); - rtgui_menu_pop(menu->sub_menu, item_rect.x2, item_rect.y1); - return RT_FALSE; - } - - /* delete sub menu */ - rtgui_menu_destroy(menu->sub_menu); - menu->sub_menu = RT_NULL; - } - - /* create sub menu */ - menu->sub_menu = rtgui_menu_create("submenu", menu, items, count); - - rtgui_listctrl_get_item_rect(menu->items_list, menu->items_list->current_item, &item_rect); - rtgui_menu_pop(menu->sub_menu, item_rect.x2 + 5, item_rect.y1); - } - else /* other menu item */ - { - /* invoke action */ - if (menu->items[menu->items_list->current_item].on_menuaction != RT_NULL) - menu->items[menu->items_list->current_item].on_menuaction(RTGUI_OBJECT(menu), RT_NULL); - - /* hide sub-menu */ - if (menu->sub_menu != RT_NULL) - { - rtgui_menu_hide(menu->sub_menu); - } - rtgui_menu_hide(menu); - } - return RT_FALSE; -} - -static void _rtgui_menu_item_ondraw(struct rtgui_listctrl *list, - struct rtgui_dc *dc, - rtgui_rect_t *rect, - rt_uint16_t index) -{ - rtgui_rect_t item_rect; - struct rtgui_menu_item *item; - - item_rect = *rect; - item_rect.x1 += 5; - - /* re-fill item */ - if (list->current_item == index) - { - rtgui_color_t bc; - - bc = RTGUI_WIDGET_BACKGROUND(list); - RTGUI_WIDGET_BACKGROUND(list) = blue; - rtgui_dc_fill_rect(dc, rect); - RTGUI_WIDGET_BACKGROUND(list) = bc; - } - - /* get menu item */ - item = (rtgui_menu_item_t *)list->items; - item = &item[index]; - - if (item->type == RTGUI_ITEM_SUBMENU) - { - rtgui_rect_t r = {0, 0, 8, 8}; - rtgui_dc_draw_text(dc, item->label, &item_rect); - item_rect.x1 = item_rect.x2 - 16; - item_rect.x2 -= 8; - rtgui_rect_moveto_align(&item_rect, &r, RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL); - rtgui_dc_draw_byte(dc, r.x1, r.y1, 8, right_arrow); - } - else if (item->type == RTGUI_ITEM_SEPARATOR) - { - rtgui_dc_draw_horizontal_line(dc, item_rect.x1, item_rect.x2, (item_rect.y2 + item_rect.y1) / 2); - } - else if (item->type == RTGUI_ITEM_CHECK) - { - /* not support right now */ - } - else - { - /* normal menu item */ - rtgui_dc_draw_text(dc, item->label, &item_rect); - if (item->image != RT_NULL) - rtgui_image_blit(item->image, dc, &item_rect); - } -} - -DEFINE_CLASS_TYPE(menu, "menu", - RTGUI_WIN_TYPE, - _rtgui_menu_constructor, - _rtgui_menu_destructor, - sizeof(struct rtgui_menu)); - -static rt_bool_t rtgui_menu_on_deactivate(struct rtgui_object *object, rtgui_event_t *event) -{ - rtgui_menu_t *menu; - RTGUI_WIDGET_EVENT_HANDLER_PREPARE - - menu = RTGUI_MENU(object); - if (menu->parent_menu != RT_NULL) - { - /* whether click on parent menu */ - if (rtgui_win_is_activated(RTGUI_WIN(menu->parent_menu)) == RT_TRUE && - menu->parent_menu->items[menu->parent_menu->items_list->current_item].submenu - == (struct rtgui_menu_item_t *)menu->items) - return RT_TRUE; - } - - /* submenu is activate */ - if (menu->items[menu->items_list->current_item].type == RTGUI_ITEM_SUBMENU) - { - /* if sub menu activated, not hide menu. But we cannot use the - * activated flag as criteria since the old window is deactivated - * before the new window got activated. But the window will be shown in - * this context, so use 'is not hide'. */ - if (menu->sub_menu != RT_NULL && - !RTGUI_WIDGET_IS_HIDE(menu->sub_menu)) - return RT_TRUE; - } - - rtgui_win_hide(RTGUI_WIN(menu)); - if (menu->on_menuhide != RT_NULL) - { - menu->on_menuhide(RTGUI_OBJECT(menu), RT_NULL); - } - - /* un-select item */ - menu->items_list->current_item = -1; - - /* if it's a submenu, try to hide parent menu */ - if (menu->parent_menu != RT_NULL && - rtgui_win_is_activated(RTGUI_WIN(menu->parent_menu)) == RT_FALSE) - { - rtgui_menu_on_deactivate(RTGUI_OBJECT(menu->parent_menu), event); - } - - return RT_TRUE; -} - -struct rtgui_menu *rtgui_menu_create(const char *title, struct rtgui_menu *parent_menu, - const struct rtgui_menu_item *items, rt_uint16_t count) -{ - rtgui_rect_t rect = {0, 0, 100, 100}; - struct rtgui_menu *menu; - - menu = (struct rtgui_menu *) rtgui_widget_create(RTGUI_MENU_TYPE); - if (menu != RT_NULL) - { - rtgui_win_set_title(RTGUI_WIN(menu), title); - menu->parent_menu = parent_menu; - menu->items = items; - menu->items_count = count; - - rtgui_widget_set_rect(RTGUI_WIDGET(menu), &rect); - rtgui_rect_inflate(&rect, -1); - /* create menu item list */ - menu->items_list = rtgui_listctrl_create((void*)items, count, &rect, _rtgui_menu_item_ondraw); - RTGUI_WIDGET_BACKGROUND(menu->items_list) = rtgui_theme_default_bc(); - rtgui_container_add_child(RTGUI_CONTAINER(menu), RTGUI_WIDGET(menu->items_list)); - rtgui_listctrl_set_onitem(menu->items_list, _rtgui_menu_onitem); - } - - return menu; -} - -void rtgui_menu_destroy(struct rtgui_menu *menu) -{ - rtgui_widget_destroy(RTGUI_WIDGET(menu)); -} - -void rtgui_menu_set_onmenupop(struct rtgui_menu *menu, rtgui_event_handler_ptr handler) -{ - if (menu == RT_NULL) return; - - menu->on_menupop = handler; -} - -void rtgui_menu_set_onmenuhide(struct rtgui_menu *menu, rtgui_event_handler_ptr handler) -{ - if (menu == RT_NULL) return; - - menu->on_menuhide = handler; -} - -void rtgui_menu_pop(struct rtgui_menu *menu, int x, int y) -{ - rtgui_rect_t rect; - struct rtgui_event_resize eresize; - - if (menu == RT_NULL) - return; - - /* set window extent */ - rect.x1 = 0; - rect.y1 = 0; - rect.x2 = 100; - rect.y2 = menu->items_count * (rtgui_theme_get_selected_height() + 2) + 5; - rtgui_rect_moveto(&rect, x, y); - rtgui_win_set_rect(RTGUI_WIN(menu), &rect); - rtgui_rect_inflate(&rect, -1); - rtgui_widget_set_rect(RTGUI_WIDGET(menu->items_list), &rect); - - eresize.parent.type = RTGUI_EVENT_RESIZE; - eresize.x = rect.x1; - eresize.y = rect.y1; - eresize.h = rect.y2 - rect.y1; - eresize.w = rect.x2 - rect.x1; - rtgui_listctrl_event_handler(RTGUI_OBJECT(menu->items_list), &(eresize.parent)); - - /* on menu pop handler */ - if (menu->on_menupop != RT_NULL) - { - menu->on_menupop(RTGUI_OBJECT(menu), RT_NULL); - } - - /* show menu window */ - rtgui_win_show(RTGUI_WIN(menu), RT_FALSE); -} - -void rtgui_menu_hide(struct rtgui_menu *menu) -{ - rtgui_win_hide(RTGUI_WIN(menu)); - /* un-select item */ - menu->items_list->current_item = -1; - - if (menu->parent_menu != RT_NULL) - rtgui_menu_hide(menu->parent_menu); -} - diff --git a/components/rtgui/widgets/mv_view.c b/components/rtgui/widgets/mv_view.c deleted file mode 100644 index dbc5f91f6d..0000000000 --- a/components/rtgui/widgets/mv_view.c +++ /dev/null @@ -1,81 +0,0 @@ -/* - * File : mv_view.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2012, 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 - * 2012-09-17 Grissiom first version - */ - -#include <rtgui/rtgui.h> -#include <rtgui/rtgui_mv_model.h> -#include <rtgui/widgets/mv_view.h> - -static void _rtgui_mv_view_constructor(struct rtgui_mv_view *view) -{ - view->model_number = 0; - view->model = RT_NULL; -} - -static void _rtgui_mv_view_destructor(struct rtgui_mv_view *view) -{ - if (view->model_number == 1) - { - rtgui_mv_model_remove_view(view->model, view); - } - else if (view->model_number > 1) - { - void **model_arr = *(void ** *)view->model; - int i; - - for (i = 0; i < view->model_number; i++) - { - rtgui_mv_model_remove_view(model_arr[i], view); - } - } -} - -DEFINE_CLASS_TYPE(mv_view, "mv_view", - RTGUI_WIDGET_TYPE, - _rtgui_mv_view_constructor, - _rtgui_mv_view_destructor, - sizeof(struct rtgui_mv_view)); - -struct rtgui_mv_view *rtgui_mv_view_create(void) -{ - return RTGUI_MV_VIEW(rtgui_widget_create(RTGUI_MV_VIEW_TYPE)); -} - -void rtgui_mv_view_destroy(struct rtgui_mv_view *view) -{ - rtgui_widget_destroy(RTGUI_WIDGET(view)); -} - -struct rtgui_mv_model *rtgui_mv_view_foreach_in_model(struct rtgui_mv_view *view, rt_uint32_t *iter) -{ - struct rtgui_mv_model *model; - - RT_ASSERT(view); - - if (*iter >= view->model_number) - return RT_NULL; - - if (view->model_number == 1) - { - model = view->model; - } - else - { - struct rtgui_mv_model **model_array = view->model; - model = model_array[*iter]; - } - - (*iter)++; - return model; -} - diff --git a/components/rtgui/widgets/notebook.c b/components/rtgui/widgets/notebook.c deleted file mode 100644 index f3a6da232c..0000000000 --- a/components/rtgui/widgets/notebook.c +++ /dev/null @@ -1,634 +0,0 @@ -#include <rtgui/dc.h> -#include <rtgui/rtgui.h> -#include <rtgui/rtgui_system.h> -#include <rtgui/widgets/notebook.h> -#include <rtgui/widgets/window.h> -#include <rtgui/image.h> - -#define RTGUI_NOTEBOOK_TAB_DEFAULT_WIDTH 80 -#define RTGUI_NOTEBOOK_TAB_DEFAULT_HEIGHT 25 - -struct rtgui_notebook_tab -{ -#ifdef RTGUI_USING_NOTEBOOK_IMAGE - struct rtgui_image *pressed_image; - struct rtgui_image *unpressed_image; -#endif - - struct rtgui_widget *widget; - char *title; -}; - -static void _rtgui_notebook_get_bar_rect(struct rtgui_notebook *notebook, struct rtgui_rect *rect); -static void _rtgui_notebook_get_page_rect(struct rtgui_notebook *notebook, struct rtgui_rect *rect); - -static void _rtgui_notebook_constructor(struct rtgui_notebook *notebook) -{ - notebook->flag = 0; - notebook->childs = RT_NULL; - notebook->count = 0; - notebook->current = 0; - - notebook->tab_h = RTGUI_NOTEBOOK_TAB_DEFAULT_HEIGHT; - notebook->tab_w = RTGUI_NOTEBOOK_TAB_DEFAULT_WIDTH; - - RTGUI_WIDGET(notebook)->gc.textalign = RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL; - rtgui_object_set_event_handler(RTGUI_OBJECT(notebook), rtgui_notebook_event_handler); -} - -static void _rtgui_notebook_destructor(struct rtgui_notebook *notebook) -{ - int index; - - if (notebook->childs != RT_NULL) - { - for (index = 0; index < notebook->count; index ++) - { - rtgui_widget_destroy(notebook->childs[index].widget); - rt_free(notebook->childs[index].title); - } - - rtgui_free(notebook->childs); - } -} - -DEFINE_CLASS_TYPE(notebook, "notebook", - RTGUI_WIDGET_TYPE, - _rtgui_notebook_constructor, - _rtgui_notebook_destructor, - sizeof(struct rtgui_notebook)); - -/* Draw tab bars of @param notebook. @param dc should be initialized and - * finished outside this function. Don't pass @param notebook or @param dc as - * RT_NULL, it should be checked outside. - */ -static void _rtgui_notebook_draw_bar(struct rtgui_notebook *notebook, - struct rtgui_dc *dc) -{ - int index; - struct rtgui_rect rect; - struct rtgui_rect text_rect; -#ifdef RTGUI_USING_NOTEBOOK_IMAGE - struct rtgui_image *image = RT_NULL; - struct rtgui_rect image_rect; -#endif - - RT_ASSERT((notebook != RT_NULL) && (dc != RT_NULL)); - - if (notebook->flag == RTGUI_NOTEBOOK_NOTAB) - return; - - _rtgui_notebook_get_bar_rect(notebook, &rect); - rtgui_dc_fill_rect(dc, &rect); - - if (notebook->flag == RTGUI_NOTEBOOK_TOP || - notebook->flag == RTGUI_NOTEBOOK_BOTTOM) - { - rect.x2 = rect.x1 + notebook->tab_w; - /* draw tab bar */ - for (index = 0; index < notebook->count; index++) - { - if (notebook->current == index) - { -#ifdef RTGUI_USING_NOTEBOOK_IMAGE - if (notebook->childs[index].pressed_image != RT_NULL) - image = notebook->childs[index].pressed_image; - else -#endif - rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_SUNKEN); - } - else - { -#ifdef RTGUI_USING_NOTEBOOK_IMAGE - if (notebook->childs[index].unpressed_image != RT_NULL) - image = notebook->childs[index].unpressed_image; - else -#endif - rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_BOX); - } - - rtgui_font_get_metrics(RTGUI_WIDGET_FONT(notebook), - notebook->childs[index].title, &text_rect); - rtgui_rect_moveto_align(&rect, &text_rect, RTGUI_ALIGN_CENTER); - -#ifdef RTGUI_USING_NOTEBOOK_IMAGE - if (image != RT_NULL) - { - image_rect.x1 = 0; - image_rect.y1 = RTGUI_WIDGET_DEFAULT_MARGIN; - image_rect.x2 = image_rect.x1 + image->w; - image_rect.y2 = image_rect.y1 + image->h; - rtgui_rect_moveto_align(&rect, &image_rect, RTGUI_ALIGN_CENTER_HORIZONTAL); - - rtgui_image_blit(image, dc, &image_rect); - } - if (image != RT_NULL) - { - int text_height = text_rect.y2 - text_rect.y1; - - text_rect.y1 = image_rect.y2 + RTGUI_WIDGET_DEFAULT_MARGIN; - text_rect.y2 = text_rect.y1 + text_height; - } - image = RT_NULL; -#endif - - rtgui_dc_draw_text(dc, notebook->childs[index].title, &text_rect); - - /* move to next tab */ - rect.x1 = rect.x2; - rect.x2 = rect.x1 + notebook->tab_w; - } - } - else - { - rect.y2 = rect.y1 + notebook->tab_h; - /* draw tab bar */ - for (index = 0; index < notebook->count; index++) - { - if (notebook->current == index) - { -#ifdef RTGUI_USING_NOTEBOOK_IMAGE - if (notebook->childs[index].pressed_image != RT_NULL) - image = notebook->childs[index].pressed_image; - else -#endif - rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_SUNKEN); - } - else - { -#ifdef RTGUI_USING_NOTEBOOK_IMAGE - if (notebook->childs[index].unpressed_image != RT_NULL) - image = notebook->childs[index].unpressed_image; - else -#endif - rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_BOX); - } - - rtgui_font_get_metrics(RTGUI_WIDGET_FONT(notebook), - notebook->childs[index].title, &text_rect); - rtgui_rect_moveto_align(&rect, &text_rect, RTGUI_ALIGN_CENTER); -#ifdef RTGUI_USING_NOTEBOOK_IMAGE - if (image != RT_NULL) - { - image_rect.x1 = 0; - image_rect.y1 = RTGUI_WIDGET_DEFAULT_MARGIN; - image_rect.x2 = image->w; - image_rect.y2 = image_rect.y1 + image->h; - rtgui_rect_moveto_align(&rect, &image_rect, RTGUI_ALIGN_CENTER_HORIZONTAL); - - rtgui_image_blit(image, dc, &image_rect); - } - - if (image != RT_NULL) - { - int text_height = text_rect.y2 - text_rect.y1; - - text_rect.y1 = image_rect.y2 + RTGUI_WIDGET_DEFAULT_MARGIN; - text_rect.y2 = text_rect.y1 + text_height; - } - image = RT_NULL; -#endif - rtgui_dc_draw_text(dc, notebook->childs[index].title, &text_rect); - - /* move to next tab */ - rect.y1 = rect.y2; - rect.y2 = rect.y1 + notebook->tab_h; - } - - } -} - -static void _rtgui_notebook_ondraw(struct rtgui_notebook *notebook) -{ - struct rtgui_dc *dc; - - dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(notebook)); - if (dc == RT_NULL) return; - - if (notebook->count == 0) - { - rtgui_rect_t rect; - rtgui_widget_get_rect(RTGUI_WIDGET(notebook), &rect); - rtgui_dc_fill_rect(dc, &rect); - } - else - { - if (notebook->current == RTGUI_NOT_FOUND) - notebook->current = 0; - - _rtgui_notebook_draw_bar(notebook, dc); - - /* draw current tab */ - rtgui_widget_update(notebook->childs[notebook->current].widget); - } - rtgui_dc_end_drawing(dc); -} - -static void _rtgui_notebook_onmouse(struct rtgui_notebook *notebook, struct rtgui_event_mouse *emouse) -{ - rtgui_rect_t rect; - - /* handle notebook bar */ - _rtgui_notebook_get_bar_rect(notebook, &rect); - rtgui_widget_rect_to_device(RTGUI_WIDGET(notebook), &rect); - if (rtgui_rect_contains_point(&rect, emouse->x, emouse->y) == RT_EOK) - { - int index; - struct rtgui_dc *dc; - - if (notebook->flag == RTGUI_NOTEBOOK_BOTTOM || notebook->flag == RTGUI_NOTEBOOK_TOP) - { - index = (emouse->x - rect.x1) / notebook->tab_w; - if (index < notebook->count && index != notebook->current) - { - /* update tab bar */ - dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(notebook)); - if (dc == RT_NULL) return; - - rtgui_notebook_set_current_by_index(notebook, index); - - _rtgui_notebook_draw_bar(notebook, dc); - - rtgui_dc_end_drawing(dc); - } - } - else - { - index = (emouse->y - rect.y1) / notebook->tab_h; - if (index < notebook->count && index != notebook->current) - { - /* update tab bar */ - dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(notebook)); - if (dc == RT_NULL) return; - - rtgui_notebook_set_current_by_index(notebook, index); - _rtgui_notebook_draw_bar(notebook, dc); - - rtgui_dc_end_drawing(dc); - } - } - } - else - { - /* handle on page */ - if (RTGUI_OBJECT(notebook->childs[notebook->current].widget)->event_handler != RT_NULL) - RTGUI_OBJECT(notebook->childs[notebook->current].widget)->event_handler( - RTGUI_OBJECT(notebook->childs[notebook->current].widget), - &(emouse->parent)); - } -} - -static void _rtgui_notebook_get_page_rect(struct rtgui_notebook *notebook, struct rtgui_rect *rect) -{ - struct rtgui_rect tab_rect; - - RT_ASSERT(notebook != RT_NULL); - RT_ASSERT(rect != RT_NULL); - - _rtgui_notebook_get_bar_rect(notebook, &tab_rect); - rtgui_widget_get_rect(RTGUI_WIDGET(notebook), rect); - - if (notebook->flag == RTGUI_NOTEBOOK_NOTAB) - return; - else if (notebook->flag == RTGUI_NOTEBOOK_TOP) - { - rect->y1 = tab_rect.y2; - } - else if (notebook->flag == RTGUI_NOTEBOOK_BOTTOM) - { - rect->y2 = tab_rect.y1; - } - else if (notebook->flag == RTGUI_NOTEBOOK_LEFT) - { - rect->x1 = tab_rect.x2; - } - else if (notebook->flag == RTGUI_NOTEBOOK_RIGHT) - { - rect->x2 = tab_rect.x1; - } -} - -static void _rtgui_notebook_get_bar_rect(struct rtgui_notebook *notebook, struct rtgui_rect *rect) -{ - RT_ASSERT(notebook != RT_NULL); - RT_ASSERT(rect != RT_NULL); - - rtgui_widget_get_rect(RTGUI_WIDGET(notebook), rect); - if (notebook->flag == RTGUI_NOTEBOOK_NOTAB) - { - rect->x1 = rect->y1 = rect->x2 = rect->y2 = 0; - } - else if (notebook->flag == RTGUI_NOTEBOOK_TOP) - { - rect->y2 = rect->y1 + notebook->tab_h; - } - else if (notebook->flag == RTGUI_NOTEBOOK_BOTTOM) - { - rect->y1 = rect->y2 - notebook->tab_h; - } - else if (notebook->flag == RTGUI_NOTEBOOK_LEFT) - { - rect->x2 = rect->x1 + notebook->tab_w; - } - else if (notebook->flag == RTGUI_NOTEBOOK_RIGHT) - { - rect->x1 = rect->x2 - notebook->tab_w; - } -} - -struct rtgui_notebook *rtgui_notebook_create(const rtgui_rect_t *rect, rt_uint8_t style) -{ - struct rtgui_notebook *notebook; - - notebook = (struct rtgui_notebook *) rtgui_widget_create(RTGUI_NOTEBOOK_TYPE); - if (notebook != RT_NULL) - { - notebook->flag = style; - rtgui_widget_set_rect(RTGUI_WIDGET(notebook), rect); - } - - return notebook; -} - -void rtgui_notebook_destroy(struct rtgui_notebook *notebook) -{ - rtgui_widget_destroy(RTGUI_WIDGET(notebook)); -} - -void rtgui_notebook_add(struct rtgui_notebook *notebook, const char *label, struct rtgui_widget *child) -{ - rtgui_rect_t rect; - RT_ASSERT(notebook != RT_NULL); - - notebook->count += 1; - notebook->childs = (struct rtgui_notebook_tab *) - rtgui_realloc(notebook->childs, - sizeof(struct rtgui_notebook_tab) * notebook->count); - - notebook->childs[notebook->count - 1].title = rt_strdup(label); - notebook->childs[notebook->count - 1].widget = child; -#ifdef RTGUI_USING_NOTEBOOK_IMAGE - notebook->childs[notebook->count - 1].pressed_image = RT_NULL; - notebook->childs[notebook->count - 1].unpressed_image = RT_NULL; -#endif - - /* set parent */ - rtgui_widget_set_parent(child, RTGUI_WIDGET(notebook)); - - if (notebook->count - 1 != notebook->current) - rtgui_widget_hide(child); - - _rtgui_notebook_get_page_rect(notebook, &rect); - rtgui_widget_rect_to_device(RTGUI_WIDGET(notebook), &rect); - rtgui_widget_set_rect(child, &rect); - - if (RTGUI_WIDGET(notebook)->toplevel != RT_NULL && - RTGUI_IS_WIN(RTGUI_WIDGET(notebook)->toplevel)) - { - struct rtgui_event_update_toplvl eup; - RTGUI_EVENT_UPDATE_TOPLVL_INIT(&eup); - eup.toplvl = RTGUI_WIDGET(notebook)->toplevel; - if (RTGUI_OBJECT(child)->event_handler) - RTGUI_OBJECT(child)->event_handler(RTGUI_OBJECT(child), (struct rtgui_event *)&eup); - } -} - -#ifdef RTGUI_USING_NOTEBOOK_IMAGE -void rtgui_notebook_add_image(struct rtgui_notebook *notebook, const char *label, struct rtgui_widget *child, - struct rtgui_image *pressed_image, struct rtgui_image *unpressed_image) -{ - rtgui_rect_t rect; - RT_ASSERT(notebook != RT_NULL); - - notebook->count += 1; - notebook->childs = (struct rtgui_notebook_tab *) - rtgui_realloc(notebook->childs, - sizeof(struct rtgui_notebook_tab) * notebook->count); - - notebook->childs[notebook->count - 1].title = rt_strdup(label); - notebook->childs[notebook->count - 1].widget = child; - notebook->childs[notebook->count - 1].pressed_image = pressed_image; - notebook->childs[notebook->count - 1].unpressed_image = unpressed_image; - - /* set parent */ - rtgui_widget_set_parent(child, RTGUI_WIDGET(notebook)); - - _rtgui_notebook_get_page_rect(notebook, &rect); - rtgui_widget_rect_to_device(RTGUI_WIDGET(notebook), &rect); - rtgui_widget_set_rect(child, &rect); - - if (notebook->count - 1 != notebook->current) - rtgui_widget_hide(child); - - if (RTGUI_WIDGET(notebook)->toplevel != RT_NULL && - RTGUI_IS_WIN(RTGUI_WIDGET(notebook)->toplevel)) - { - struct rtgui_event_update_toplvl eup; - RTGUI_EVENT_UPDATE_TOPLVL_INIT(&eup); - eup.toplvl = RTGUI_WIDGET(notebook)->toplevel; - if (RTGUI_OBJECT(child)->event_handler) - RTGUI_OBJECT(child)->event_handler(RTGUI_OBJECT(child), (struct rtgui_event *)&eup); - } - - return; -} -#endif - -void rtgui_notebook_remove(struct rtgui_notebook *notebook, rt_uint16_t index) -{ - struct rtgui_notebook_tab tab; - rt_bool_t need_update = RT_FALSE; - - RT_ASSERT(notebook != RT_NULL); - - if (index < notebook->count) - { - if (notebook->count == 1) - { - tab = notebook->childs[0]; - rtgui_free(notebook->childs); - notebook->childs = RT_NULL; - notebook->count = 0; - } - else - { - if (notebook->current == index) - need_update = RT_TRUE; - - tab = notebook->childs[index]; - for (; index < notebook->count - 1; index++) - { - notebook->childs[index] = notebook->childs[index + 1]; - } - - notebook->count -= 1; - notebook->childs = (struct rtgui_notebook_tab *) rtgui_realloc(notebook->childs, - sizeof(struct rtgui_notebook_tab) * notebook->count); - } - - rt_free(tab.title); - - if (need_update) - { - if (notebook->current > notebook->count - 1) - notebook->current = notebook->count - 1; - - rtgui_widget_hide(tab.widget); - rtgui_widget_show(notebook->childs[notebook->current].widget); - rtgui_widget_update(RTGUI_WIDGET(notebook)); - rtgui_widget_set_parent(tab.widget, RT_NULL); - } - } -} - -int rtgui_notebook_get_count(struct rtgui_notebook *notebook) -{ - RT_ASSERT(notebook != RT_NULL); - return notebook->count; -} - -void rtgui_notebook_get_client_rect(struct rtgui_notebook *notebook, struct rtgui_rect *rect) -{ - _rtgui_notebook_get_page_rect(notebook, rect); - rtgui_rect_moveto(rect, 0, 0); -} - -struct rtgui_widget *rtgui_notebook_get_current(struct rtgui_notebook *notebook) -{ - RT_ASSERT(notebook != RT_NULL); - if (notebook->current != RTGUI_NOT_FOUND) - return notebook->childs[notebook->current].widget; - - return RT_NULL; -} - -rt_int16_t rtgui_notebook_get_current_index(struct rtgui_notebook *notebook) -{ - RT_ASSERT(notebook != RT_NULL); - return notebook->current; -} - -void rtgui_notebook_set_current(struct rtgui_notebook *notebook, struct rtgui_widget *widget) -{ - rt_int16_t index; - - RT_ASSERT(notebook != RT_NULL); - - for (index = 0; index < notebook->count; index ++) - { - if (widget == notebook->childs[index].widget) - { - rtgui_notebook_set_current_by_index(notebook, index); - return; - } - } -} - -void rtgui_notebook_set_current_by_index(struct rtgui_notebook *notebook, rt_uint16_t index) -{ - RT_ASSERT(notebook != RT_NULL); - - if ((index < notebook->count) && (notebook->current != index)) - { - struct rtgui_widget *widget; - - if (notebook->current != RTGUI_NOT_FOUND) - rtgui_widget_hide(notebook->childs[notebook->current].widget); - - notebook->current = index; - widget = notebook->childs[notebook->current].widget; - rtgui_widget_show(widget); - rtgui_widget_update_clip(widget); - /* the whole notebook need an update */ - rtgui_widget_update(RTGUI_WIDGET(notebook)); - rtgui_widget_focus(widget); - } -} - -struct rtgui_widget *rtgui_notebook_get_widget_at(struct rtgui_notebook *notebook, rt_uint16_t index) -{ - RT_ASSERT(notebook != RT_NULL); - if (index < notebook->count) - return notebook->childs[index].widget; - - return RT_NULL; -} - -static rt_bool_t _rtgui_notebook_current_widget_handle(struct rtgui_notebook *notebook, - struct rtgui_event *event) -{ - struct rtgui_widget *widget = rtgui_notebook_get_current(notebook); - if (widget && widget != RTGUI_WIDGET(notebook)) - return RTGUI_OBJECT(widget)->event_handler(RTGUI_OBJECT(widget), event); - else - return RT_FALSE; -} - -static void _rtgui_notebook_all_widget_handle(struct rtgui_notebook *notebook, - struct rtgui_event *event) -{ - struct rtgui_object *object; - int i; - for (i = 0; i < notebook->count; i++) - { - object = RTGUI_OBJECT(notebook->childs[i].widget); - if (object->event_handler) - object->event_handler(object, event); - } -} - -rt_bool_t rtgui_notebook_event_handler(struct rtgui_object *object, struct rtgui_event *event) -{ - int page_index; - rtgui_rect_t rect; - struct rtgui_notebook *notebook; - - RT_ASSERT(object != RT_NULL); - RT_ASSERT(event != RT_NULL); - - notebook = RTGUI_NOTEBOOK(object); - - switch (event->type) - { - case RTGUI_EVENT_PAINT: - _rtgui_notebook_ondraw(notebook); - break; - case RTGUI_EVENT_MOUSE_BUTTON: - _rtgui_notebook_onmouse(notebook, (struct rtgui_event_mouse *)event); - break; - case RTGUI_EVENT_SHOW: - /* show myself */ - rtgui_widget_onshow(object, event); - /* show the tab widget */ - return _rtgui_notebook_current_widget_handle(notebook, event); - case RTGUI_EVENT_HIDE: - /* hide myself */ - rtgui_widget_onhide(object, event); - /* hide the tab widget */ - return _rtgui_notebook_current_widget_handle(notebook, event); - case RTGUI_EVENT_KBD: - return _rtgui_notebook_current_widget_handle(notebook, event); - case RTGUI_EVENT_UPDATE_TOPLVL: - /* update myself */ - rtgui_widget_onupdate_toplvl(object, event); - /* update all the widgets in myself */ - _rtgui_notebook_all_widget_handle(notebook, event); - return RT_FALSE; - - case RTGUI_EVENT_RESIZE: - /* re-size page widget */ - _rtgui_notebook_get_page_rect(notebook, &rect); - rtgui_widget_rect_to_device(RTGUI_WIDGET(notebook), &rect); - for (page_index = 0; page_index < notebook->count; page_index ++) - { - rtgui_widget_set_rect(notebook->childs[page_index].widget, &rect); - } - break; - - default: - /* use parent event handler */ - return rtgui_widget_event_handler(object, event); - } - - return RT_FALSE; -} diff --git a/components/rtgui/widgets/panel.c b/components/rtgui/widgets/panel.c deleted file mode 100644 index 466835f745..0000000000 --- a/components/rtgui/widgets/panel.c +++ /dev/null @@ -1,89 +0,0 @@ -/* - * File : panel.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2012, 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 - * 2012-07-07 Bernard implement panel as a widget - */ -#include <rtgui/dc.h> -#include <rtgui/widgets/panel.h> -#include <rtgui/rtgui_system.h> -#include <rtgui/rtgui_theme.h> - -static void _rtgui_panel_constructor(rtgui_panel_t *panel) -{ - /* init widget and set event handler */ - rtgui_object_set_event_handler(RTGUI_OBJECT(panel), rtgui_panel_event_handler); - - /* set field */ - panel->border_style = RTGUI_BORDER_NONE; -} - -DEFINE_CLASS_TYPE(panel, "panel", - RTGUI_CONTAINER_TYPE, - _rtgui_panel_constructor, - RT_NULL, - sizeof(struct rtgui_panel)); - -rt_bool_t rtgui_panel_event_handler(struct rtgui_object *object, struct rtgui_event *event) -{ - struct rtgui_panel *panel; - - panel = RTGUI_PANEL(object); - switch (event->type) - { - case RTGUI_EVENT_PAINT: - { - struct rtgui_dc *dc; - struct rtgui_rect rect; - - rtgui_widget_get_rect(RTGUI_WIDGET(object), &rect); - - dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(object)); - if (dc == RT_NULL) return RT_FALSE; - - rtgui_dc_fill_rect(dc, &rect); - // rtgui_rect_inflate(&rect, RTGUI_WIDGET(panel)->); - rtgui_dc_draw_border(dc, &rect, panel->border_style); - - /* paint on each child */ - rtgui_container_dispatch_event(RTGUI_CONTAINER(panel), event); - - rtgui_dc_end_drawing(dc); - } - break; - - default: - return rtgui_container_event_handler(object, event); - } - - return RT_FALSE; -} - -rtgui_panel_t *rtgui_panel_create(int border_style) -{ - struct rtgui_panel *panel; - - panel = (struct rtgui_panel *) rtgui_widget_create(RTGUI_PANEL_TYPE); - if (panel != RT_NULL) - { - rtgui_rect_t rect = {0, 0, 100, 100}; - rtgui_widget_set_rect(RTGUI_WIDGET(panel), &rect); - - panel->border_style = border_style; - } - - return panel; -} - -void rtgui_panel_destroy(rtgui_panel_t *panel) -{ - rtgui_object_destroy(RTGUI_OBJECT(panel)); -} - diff --git a/components/rtgui/widgets/plot.c b/components/rtgui/widgets/plot.c deleted file mode 100644 index 9c3018a5c3..0000000000 --- a/components/rtgui/widgets/plot.c +++ /dev/null @@ -1,287 +0,0 @@ -/* - * File : plot.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2012, 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 - * 2012-09-03 Grissiom first version - */ - -#include <rtgui/rtgui_system.h> -#include <rtgui/dc.h> -#include <rtgui/widgets/plot.h> -#include <rtgui/widgets/plot_curve.h> - -static void _rtgui_plot_constructor(struct rtgui_plot *plot) -{ - plot->base_x = plot->base_y = 0; - plot->ptype = RTGUI_PLOT_TYPE_SCAN; - plot->scale_x = plot->scale_y = 1; - - rtgui_object_set_event_handler(RTGUI_OBJECT(plot), rtgui_plot_event_handler); -} - -static void _rtgui_plot_destructor(struct rtgui_plot *plot) -{ -} - -DEFINE_CLASS_TYPE(plot, "plot", - RTGUI_MV_VIEW_TYPE, - _rtgui_plot_constructor, - _rtgui_plot_destructor, - sizeof(struct rtgui_plot)); - -struct rtgui_plot *rtgui_plot_create(void) -{ - struct rtgui_plot *plot; - - plot = RTGUI_PLOT(rtgui_widget_create(RTGUI_PLOT_TYPE)); - - return plot; -} -RTM_EXPORT(rtgui_plot_create); - -void rtgui_plot_destroy(struct rtgui_plot *plot) -{ - rtgui_mv_view_destroy(RTGUI_MV_VIEW(plot)); -} -RTM_EXPORT(rtgui_plot_destroy); - -void rtgui_plot_set_base(struct rtgui_plot *plot, - rtgui_plot_curve_dtype x, rtgui_plot_curve_dtype y) -{ - plot->base_x = x; - plot->base_y = y; -} -RTM_EXPORT(rtgui_plot_set_base); - -rt_inline int _rtgui_plot_curve_calc_x(struct rtgui_plot *plot, rtgui_plot_curve_dtype x) -{ - return (x - plot->base_x) / plot->scale_x; -} - -rt_inline int _rtgui_plot_curve_calc_y(struct rtgui_plot *plot, rtgui_plot_curve_dtype y, rt_uint16_t height) -{ - return height - (y - plot->base_y) / plot->scale_y; -} - -static void _rtgui_plot_curve_onpaint( - struct rtgui_dc *dc, - struct rtgui_plot *plot, - struct rtgui_plot_curve *curve, - rt_uint16_t start_idx, - rt_uint16_t stop_idx) -{ - struct rtgui_rect rect; - rt_uint16_t height; - int last_x, last_y; - rtgui_color_t old_color; - rtgui_plot_curve_dtype *x_data, *y_data; - - rtgui_dc_get_rect(dc, &rect); - height = rtgui_rect_height(rect); - - old_color = RTGUI_DC_FC(dc); - RTGUI_DC_FC(dc) = curve->color; - - x_data = rtgui_plot_curve_get_x(curve); - y_data = rtgui_plot_curve_get_y(curve); - if (x_data) - { - rt_size_t i; - - last_x = _rtgui_plot_curve_calc_x(plot, x_data[start_idx]); - last_y = _rtgui_plot_curve_calc_y(plot, y_data[start_idx], height); - for (i = start_idx + 1; i < stop_idx; i++) - { - int cur_x = _rtgui_plot_curve_calc_x(plot, x_data[i]); - int cur_y = _rtgui_plot_curve_calc_y(plot, y_data[i], height); - rtgui_dc_draw_line(dc, - last_x, last_y, - cur_x, cur_y); - last_x = cur_x; - last_y = cur_y; - } - } - else - { - rt_size_t i; - - last_x = _rtgui_plot_curve_calc_x(plot, start_idx); - last_y = _rtgui_plot_curve_calc_y(plot, y_data[start_idx], height); - for (i = start_idx + 1; i < stop_idx; i++) - { - int cur_x = _rtgui_plot_curve_calc_x(plot, i); - int cur_y = _rtgui_plot_curve_calc_y(plot, y_data[i], height); - rtgui_dc_draw_line(dc, - last_x, last_y, - cur_x, cur_y); - last_x = cur_x; - last_y = cur_y; - } - } - RTGUI_DC_FC(dc) = old_color; -} - -static void _rtgui_plot_draw_curve(struct rtgui_plot *plot, struct rtgui_event *event) -{ - int i; - struct rtgui_dc *dc; - - dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(plot)); - if (dc == RT_NULL) - return; - - if (RTGUI_MV_VIEW(plot)->model_number == 1) - { - _rtgui_plot_curve_onpaint(dc, plot, - RTGUI_PLOT_CURVE(RTGUI_MV_VIEW(plot)->model), - 0, RTGUI_MV_MODEL(RTGUI_MV_VIEW(plot)->model)->length); - } - else - { - void **curve_array = (void **)RTGUI_MV_VIEW(plot)->model; - for (i = 0; i < RTGUI_MV_VIEW(plot)->model_number; i++) - { - _rtgui_plot_curve_onpaint(dc, plot, - RTGUI_PLOT_CURVE(curve_array[i]), - 0, RTGUI_MV_MODEL(curve_array[i])->length); - } - } - - rtgui_dc_end_drawing(dc); -} - -static void _rtgui_plot_update_scale(struct rtgui_plot *plot) -{ - struct rtgui_plot_curve *curve; - struct rtgui_rect rect; - rtgui_plot_curve_dtype max_x = 0; - rtgui_plot_curve_dtype min_x = 0; - rtgui_plot_curve_dtype max_y = 0; - rtgui_plot_curve_dtype min_y = 0; - rt_uint32_t iter = 0; - - rtgui_widget_get_rect(RTGUI_WIDGET(plot), &rect); - - curve = RTGUI_PLOT_CURVE( - rtgui_mv_view_foreach_in_model(RTGUI_MV_VIEW(plot), &iter)); - max_x = curve->max_x; - min_x = curve->min_x; - max_y = curve->max_y; - min_y = curve->min_y; - - while (curve) - { - if (curve->max_x > max_x) - max_x = curve->max_x; - if (curve->min_x < min_x) - min_x = curve->min_x; - if (curve->max_y > max_y) - max_y = curve->max_y; - if (curve->min_y < min_y) - min_y = curve->min_y; - - curve = RTGUI_PLOT_CURVE( - rtgui_mv_view_foreach_in_model(RTGUI_MV_VIEW(plot), &iter)); - } - - plot->scale_x = (max_x - min_x + rtgui_rect_width(rect)) / rtgui_rect_width(rect); - plot->scale_y = (max_y - min_y + rtgui_rect_height(rect)) / rtgui_rect_height(rect); -} - -rt_bool_t rtgui_plot_ondraw(struct rtgui_plot *plot, struct rtgui_event *event) -{ - struct rtgui_dc *dc; - struct rtgui_rect rect; - - dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(plot)); - if (dc == RT_NULL) - return RT_FALSE; - - rtgui_widget_get_rect(RTGUI_WIDGET(plot), &rect); - - rtgui_dc_fill_rect(dc, &rect); - - _rtgui_plot_draw_curve(plot, event); - - rtgui_dc_end_drawing(dc); - - return RT_FALSE; -} - -rt_bool_t rtgui_plot_onmvmodel(struct rtgui_plot *plot, struct rtgui_event *event) -{ - struct rtgui_event_mv_model *emodel = (struct rtgui_event_mv_model *)event; - - RT_ASSERT(plot); - RT_ASSERT(event); - - switch (plot->ptype) - { - case RTGUI_PLOT_TYPE_SCAN: - _rtgui_plot_update_scale(plot); - rtgui_plot_ondraw(plot, event); - case RTGUI_PLOT_TYPE_INCREMENTAL: - { - rt_uint16_t old_sc_x = plot->scale_x; - rt_uint16_t old_sc_y = plot->scale_y; - - _rtgui_plot_update_scale(plot); - - if (old_sc_x != plot->scale_x || old_sc_y != plot->scale_y) - { - /* we need to repaint the whole widget as the scale changed. */ - rtgui_plot_ondraw(plot, event); - } - else - { - /* get dc for _rtgui_plot_curve_onpaint */ - struct rtgui_dc *dc; - dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(plot)); - if (dc == RT_NULL) - return RT_FALSE; - - /* only draw the newly recieved data */ - _rtgui_plot_curve_onpaint(dc, plot, - RTGUI_PLOT_CURVE(emodel->model), - emodel->first_data_changed_idx, - emodel->last_data_changed_idx + 1); - - rtgui_dc_end_drawing(dc); - } - } - return RT_TRUE; - default: - RT_ASSERT(0); - } - - return RT_TRUE; -} - -rt_bool_t rtgui_plot_event_handler(struct rtgui_object *object, struct rtgui_event *event) -{ - struct rtgui_plot *plot; - - RTGUI_WIDGET_EVENT_HANDLER_PREPARE; - - plot = RTGUI_PLOT(object); - - switch (event->type) - { - case RTGUI_EVENT_PAINT: - _rtgui_plot_update_scale(RTGUI_PLOT(object)); - return rtgui_plot_ondraw(plot, event); - case RTGUI_EVENT_MV_MODEL: - return rtgui_plot_onmvmodel(plot, event); - default: - return rtgui_widget_event_handler(object, event); - } -} -RTM_EXPORT(rtgui_plot_event_handler); - diff --git a/components/rtgui/widgets/plot_curve.c b/components/rtgui/widgets/plot_curve.c deleted file mode 100644 index 11b620da65..0000000000 --- a/components/rtgui/widgets/plot_curve.c +++ /dev/null @@ -1,87 +0,0 @@ -/* - * File : plot.h - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2012, 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 - * 2012-09-03 Grissiom first version - */ - -#include <rtgui/color.h> -#include <rtgui/rtgui_mv_model.h> -#include <rtgui/widgets/widget.h> -#include <rtgui/widgets/plot_curve.h> - -static void _rtgui_plot_curve_constructor(struct rtgui_plot_curve *curve) -{ - curve->color = red; - curve->max_x = curve->min_x = curve->max_y = curve->min_y = 0; - - /* init widget and set event handler */ - rtgui_object_set_event_handler(RTGUI_OBJECT(curve), RT_NULL); -} - -static void _rtgui_plot_curve_destructor(struct rtgui_plot_curve *curve) -{ - /* nothing to do so far. */ -} - -DEFINE_CLASS_TYPE(plot_curve, "plot_curve", - RTGUI_MV_MODEL_TYPE, - _rtgui_plot_curve_constructor, - _rtgui_plot_curve_destructor, - sizeof(struct rtgui_plot_curve)); - -struct rtgui_plot_curve *rtgui_plot_curve_create(void) -{ - struct rtgui_plot_curve *curve; - - curve = RTGUI_PLOT_CURVE(rtgui_object_create(RTGUI_PLOT_CURVE_TYPE)); - if (curve == RT_NULL) - return RT_NULL; - - if (rtgui_mv_model_set_dimension(RTGUI_MV_MODEL(curve), 2) != RT_EOK) - { - rtgui_object_destroy(RTGUI_OBJECT(curve)); - return RT_NULL; - } - - return curve; -} -RTM_EXPORT(rtgui_plot_curve_create); - -void rtgui_plot_curve_destroy(struct rtgui_plot_curve *curve) -{ - rtgui_mv_model_destroy(RTGUI_MV_MODEL(curve)); -} -RTM_EXPORT(rtgui_plot_curve_destroy); - -void rtgui_plot_curve_set_x(struct rtgui_plot_curve *curve, void *p) -{ - rtgui_mv_model_set_data(RTGUI_MV_MODEL(curve), 0, p); -} -RTM_EXPORT(rtgui_plot_curve_set_x); - -void *rtgui_plot_curve_get_x(struct rtgui_plot_curve *curve) -{ - return rtgui_mv_model_get_data(RTGUI_MV_MODEL(curve), 0); -} -RTM_EXPORT(rtgui_plot_curve_get_x); - -void rtgui_plot_curve_set_y(struct rtgui_plot_curve *curve, void *p) -{ - rtgui_mv_model_set_data(RTGUI_MV_MODEL(curve), 1, p); -} -RTM_EXPORT(rtgui_plot_curve_set_y); - -void *rtgui_plot_curve_get_y(struct rtgui_plot_curve *curve) -{ - return rtgui_mv_model_get_data(RTGUI_MV_MODEL(curve), 1); -} -RTM_EXPORT(rtgui_plot_curve_get_y); - diff --git a/components/rtgui/widgets/progressbar.c b/components/rtgui/widgets/progressbar.c deleted file mode 100644 index c140342c72..0000000000 --- a/components/rtgui/widgets/progressbar.c +++ /dev/null @@ -1,106 +0,0 @@ -#include <rtgui/dc.h> -#include <rtgui/rtgui_theme.h> -#include <rtgui/widgets/progressbar.h> - -#define RTGUI_PROGRESSBAR_DEFAULT_RANGE 100 - -static void _rtgui_progressbar_constructor(rtgui_progressbar_t *bar) -{ - rtgui_rect_t rect = {0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT}; - - rtgui_object_set_event_handler(RTGUI_OBJECT(bar), rtgui_progressbar_event_handler); - rtgui_widget_set_rect(RTGUI_WIDGET(bar), &rect); - - bar->orient = RTGUI_HORIZONTAL; - bar->range = RTGUI_PROGRESSBAR_DEFAULT_RANGE; - bar->position = 0; - - /* set gc */ - RTGUI_WIDGET_TEXTALIGN(bar) = RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL; -} - -DEFINE_CLASS_TYPE(progressbar, "progressbar", - RTGUI_WIDGET_TYPE, - _rtgui_progressbar_constructor, - RT_NULL, - sizeof(struct rtgui_progressbar)); - -rt_bool_t rtgui_progressbar_event_handler(struct rtgui_object *object, - struct rtgui_event *event) -{ - struct rtgui_progressbar *bar; - RTGUI_WIDGET_EVENT_HANDLER_PREPARE - - bar = RTGUI_PROGRESSBAR(object); - - switch (event->type) - { - case RTGUI_EVENT_PAINT: - rtgui_theme_draw_progressbar(bar); - break; - default: - return rtgui_widget_event_handler(object, event); - } - - return RT_FALSE; -} - -struct rtgui_progressbar *rtgui_progressbar_create(int orientation, int range, - rtgui_rect_t *r) -{ - struct rtgui_progressbar *bar; - - bar = (struct rtgui_progressbar *) rtgui_widget_create(RTGUI_PROGRESSBAR_TYPE); - if (bar != RT_NULL) - { - if (r != RT_NULL) - rtgui_widget_set_rect(RTGUI_WIDGET(bar), r); - - bar->orient = orientation; - bar->range = range; - } - - return bar; -} - -void rtgui_progressbar_destroy(struct rtgui_progressbar *bar) -{ - rtgui_widget_destroy(RTGUI_WIDGET(bar)); -} - -void rtgui_progressbar_set_value(struct rtgui_progressbar *bar, int value) -{ - RT_ASSERT(bar != RT_NULL); - - if (!RTGUI_WIDGET_IS_ENABLE(bar)) return; - - bar->position = value; - - rtgui_theme_draw_progressbar(bar); - return; -} - -int rtgui_progressbar_get_value(struct rtgui_progressbar *bar) -{ - RT_ASSERT(bar != RT_NULL); - - return bar->position; -} - -void rtgui_progressbar_set_range(struct rtgui_progressbar *bar, int range) -{ - RT_ASSERT(bar != RT_NULL); - - bar->range = range; - - rtgui_theme_draw_progressbar(bar); - return; -} - -int rtgui_progressbar_get_range(struct rtgui_progressbar *bar) -{ - RT_ASSERT(bar != RT_NULL); - - return bar->range; -} - diff --git a/components/rtgui/widgets/radiobox.c b/components/rtgui/widgets/radiobox.c deleted file mode 100644 index 4eeb620221..0000000000 --- a/components/rtgui/widgets/radiobox.c +++ /dev/null @@ -1,263 +0,0 @@ -#include <rtgui/dc.h> -#include <rtgui/rtgui_theme.h> -#include <rtgui/widgets/radiobox.h> - -#define RTGUI_RADIOBOX_DEFAULT_WIDTH 100 -#define RTGUI_RADIOBOX_DEFAULT_HEIGHT 20 - -static void _rtgui_radiobox_constructor(rtgui_radiobox_t *radiobox) -{ - rtgui_rect_t rect = {0, 0, RTGUI_RADIOBOX_DEFAULT_WIDTH, RTGUI_RADIOBOX_DEFAULT_HEIGHT}; - - /* init widget and set event handler */ - RTGUI_WIDGET(radiobox)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE; - RTGUI_WIDGET_TEXTALIGN(radiobox) = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_CENTER_VERTICAL; - rtgui_widget_set_rect(RTGUI_WIDGET(radiobox), &rect); - rtgui_object_set_event_handler(RTGUI_OBJECT(radiobox), rtgui_radiobox_event_handler); - - /* set proper of control */ - radiobox->items = RT_NULL; - radiobox->item_count = 0; - radiobox->item_selection = -1; - radiobox->orient = RTGUI_HORIZONTAL; -} - -DEFINE_CLASS_TYPE(radiobox, "radiobox", - RTGUI_WIDGET_TYPE, - _rtgui_radiobox_constructor, - RT_NULL, - sizeof(struct rtgui_radiobox)); - -static void rtgui_radiobox_onmouse(struct rtgui_radiobox *radiobox, struct rtgui_event_mouse *event) -{ - RT_ASSERT(radiobox != RT_NULL); - RT_ASSERT(event != RT_NULL); - - /* widget is hide, return */ - if (RTGUI_WIDGET_IS_HIDE(radiobox) || - !RTGUI_WIDGET_IS_ENABLE(radiobox)) return; - - if (event->button & RTGUI_MOUSE_BUTTON_DOWN && - event->button & RTGUI_MOUSE_BUTTON_LEFT) - { - int bord_size; - struct rtgui_rect rect; - - /* focus widgets */ - rtgui_widget_focus(RTGUI_WIDGET(radiobox)); - - /* get widget physical rect */ - rtgui_widget_get_rect(RTGUI_WIDGET(radiobox), &rect); - rtgui_widget_rect_to_device(RTGUI_WIDGET(radiobox), &rect); - - /* get board size */ - if (radiobox->orient == RTGUI_VERTICAL) - bord_size = radiobox->item_size; - else - { - struct rtgui_rect bord_rect; - - rtgui_font_get_metrics(RTGUI_WIDGET_FONT(radiobox), "H", &bord_rect); - bord_size = rtgui_rect_height(bord_rect); - } - rtgui_rect_inflate(&rect, - bord_size); - if (rtgui_rect_contains_point(&rect, event->x, event->y) != RT_EOK) return; - - if (radiobox->orient == RTGUI_VERTICAL) - { - int delta_y = event->y - rect.y1; - rtgui_radiobox_set_selection(radiobox, delta_y / radiobox->item_size); - } - else - { - int delta_x = event->x - rect.x1; - rtgui_radiobox_set_selection(radiobox, delta_x / radiobox->item_size); - } - } -} - -rt_bool_t rtgui_radiobox_event_handler(struct rtgui_object *object, struct rtgui_event *event) -{ - struct rtgui_radiobox *radiobox; - RTGUI_WIDGET_EVENT_HANDLER_PREPARE - - radiobox = RTGUI_RADIOBOX(object); - switch (event->type) - { - case RTGUI_EVENT_PAINT: -#ifndef RTGUI_USING_SMALL_SIZE - if (widget->on_draw != RT_NULL) - widget->on_draw(RTGUI_OBJECT(widget), event); - else -#endif - { - rtgui_theme_draw_radiobox(radiobox); - } - - break; - - case RTGUI_EVENT_KBD: - if (RTGUI_WIDGET_IS_HIDE(radiobox)) return RT_FALSE; - -#ifndef RTGUI_USING_SMALL_SIZE - if (widget->on_key != RT_NULL) - return widget->on_key(RTGUI_OBJECT(widget), event); - else -#endif - { - struct rtgui_event_kbd *e = (struct rtgui_event_kbd *)event; - - /* set focused */ - rtgui_widget_focus(RTGUI_WIDGET(radiobox)); - if (!(RTGUI_KBD_IS_UP(e))) return RT_FALSE; - - if (radiobox->orient == RTGUI_VERTICAL) - { - if (e->key == RTGUIK_UP) - { - if (radiobox->item_selection > 0) - { - rtgui_radiobox_set_selection(radiobox, radiobox->item_selection - 1); - return RT_TRUE; - } - } - else if (e->key == RTGUIK_DOWN) - { - if (radiobox->item_selection < radiobox->item_count - 1) - { - rtgui_radiobox_set_selection(radiobox, radiobox->item_selection + 1); - return RT_TRUE; - } - } - } - else - { - if (e->key == RTGUIK_LEFT) - { - if (radiobox->item_selection > 0) - { - rtgui_radiobox_set_selection(radiobox, radiobox->item_selection - 1); - return RT_TRUE; - } - } - else if (e->key == RTGUIK_RIGHT) - { - if (radiobox->item_selection < radiobox->item_count - 1) - { - rtgui_radiobox_set_selection(radiobox, radiobox->item_selection + 1); - return RT_TRUE; - } - } - } - } - break; - - case RTGUI_EVENT_MOUSE_BUTTON: -#ifndef RTGUI_USING_SMALL_SIZE - if (widget->on_mouseclick != RT_NULL) - widget->on_mouseclick(RTGUI_OBJECT(widget), event); - else -#endif - { - rtgui_radiobox_onmouse(radiobox, (struct rtgui_event_mouse *)event); - } - break; - default: - return rtgui_widget_event_handler(object, event); - } - - return RT_FALSE; -} - -struct rtgui_radiobox *rtgui_radiobox_create(const char *label, int orient, char **radio_items, int number) -{ - struct rtgui_radiobox *radiobox; - - radiobox = (struct rtgui_radiobox *) rtgui_widget_create(RTGUI_RADIOBOX_TYPE); - if (radiobox != RT_NULL) - { - rt_uint8_t board_size; - struct rtgui_rect rect; - - radiobox->items = radio_items; - radiobox->item_count = number; - radiobox->item_selection = -1; - radiobox->text = rt_strdup(label); - - /* set proper of control */ - rtgui_radiobox_set_orientation(radiobox, orient); - rtgui_font_get_metrics(RTGUI_WIDGET_FONT(radiobox), "H", &rect); - board_size = rtgui_rect_height(rect); - - if (orient == RTGUI_VERTICAL) - { - radiobox->item_size = board_size; - } - else - { - int index; - struct rtgui_font *font; - struct rtgui_rect rect; - - /* set init item size */ - radiobox->item_size = 0; - - font = RTGUI_WIDGET_FONT(radiobox); - for (index = 0; index < number; index ++) - { - rtgui_font_get_metrics(font, radio_items[index], &rect); - if ((board_size + 3 + rtgui_rect_width(rect)) > radiobox->item_size) - radiobox->item_size = board_size + 3 + rtgui_rect_width(rect); - } - } - - if (radiobox->item_size < RADIO_BOX_H + 2) - radiobox->item_size = RADIO_BOX_H + 2; - } - - return radiobox; -} - -void rtgui_radiobox_set_orientation(struct rtgui_radiobox *radiobox, int orientation) -{ - RT_ASSERT(radiobox != RT_NULL); - - /* set orientation */ - radiobox->orient = orientation; -#ifndef RTGUI_USING_SMALL_SIZE - if (radiobox->orient == RTGUI_HORIZONTAL) - { - /* HORIZONTAL */ - rtgui_widget_set_minheight(RTGUI_WIDGET(radiobox), RTGUI_RADIOBOX_DEFAULT_HEIGHT); - rtgui_widget_set_minwidth(RTGUI_WIDGET(radiobox), RTGUI_RADIOBOX_DEFAULT_WIDTH); - } - else - { - /* VERTICAL */ - rtgui_widget_set_minwidth(RTGUI_WIDGET(radiobox), RTGUI_RADIOBOX_DEFAULT_HEIGHT); - rtgui_widget_set_minheight(RTGUI_WIDGET(radiobox), RTGUI_RADIOBOX_DEFAULT_WIDTH); - } -#endif -} - -void rtgui_radiobox_set_selection(struct rtgui_radiobox *radiobox, int selection) -{ - rt_uint16_t old_item; - - if (selection == radiobox->item_selection) return; - - old_item = radiobox->item_selection; - if (selection >= 0 && selection < radiobox->item_count) - { - radiobox->item_selection = selection; - } - - /* update radiobox widget */ - rtgui_theme_draw_radiobutton(radiobox, old_item); - rtgui_theme_draw_radiobutton(radiobox, radiobox->item_selection); -} - -int rtgui_radiobox_get_selection(struct rtgui_radiobox *radiobox) -{ - return radiobox->item_selection; -} diff --git a/components/rtgui/widgets/scrollbar.c b/components/rtgui/widgets/scrollbar.c deleted file mode 100644 index 9b67c7210c..0000000000 --- a/components/rtgui/widgets/scrollbar.c +++ /dev/null @@ -1,790 +0,0 @@ -/* -* File : scrollbar.c -* This file is part of 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 -* -*/ -#include <stdlib.h> -#include <rtgui/dc.h> -#include <rtgui/widgets/scrollbar.h> - -static void _rtgui_scrollbar_constructor(rtgui_scrollbar_t *bar) -{ - /* set event handler */ - rtgui_object_set_event_handler(RTGUI_OBJECT(bar), rtgui_scrollbar_event_handler); - - bar->status = 0; - bar->value = 0; - - bar->thumb_w = 16; - bar->thumb_len = 16; - - bar->widget_link = RT_NULL; - bar->on_scroll = RT_NULL; - - bar->orient = RTGUI_HORIZONTAL; - /* set gc */ - RTGUI_WIDGET_TEXTALIGN(bar) = RTGUI_ALIGN_CENTER; -} - -static void _rtgui_scrollbar_destructor(rtgui_scrollbar_t *bar) -{ - -} - -rt_uint32_t _rtgui_scrollbar_get_length(rtgui_scrollbar_t *bar) -{ - rtgui_rect_t rect; - rt_uint32_t result; - - rtgui_widget_get_rect(RTGUI_WIDGET(bar), &rect); - - if(bar->orient & RTGUI_VERTICAL) - result = rtgui_rect_height(rect) - 2*rtgui_rect_width(rect) - bar->thumb_len; - else - result = rtgui_rect_width(rect) - 2*rtgui_rect_height(rect) - bar->thumb_len; - - return result; -} - -rt_uint32_t get_scrollbar_pos(rtgui_scrollbar_t* bar) -{ - double pos,ftmp; - rt_uint32_t result; - - /* calculate thumb position */ - pos = bar->value; - ftmp = _rtgui_scrollbar_get_length(bar); - pos *= ftmp; - ftmp = bar->count; - pos /= ftmp; - - result = pos; - return result; -} - -DEFINE_CLASS_TYPE(scrollbar, "scrollbar", - RTGUI_WIDGET_TYPE, - _rtgui_scrollbar_constructor, - _rtgui_scrollbar_destructor, - sizeof(struct rtgui_scrollbar)); - -rtgui_scrollbar_t* rtgui_scrollbar_create(rtgui_container_t *container,int left,int top,int w,int len,int orient) -{ - rtgui_scrollbar_t* bar; - - RT_ASSERT(container != RT_NULL); - - bar = (rtgui_scrollbar_t *)rtgui_widget_create(RTGUI_SCROLLBAR_TYPE); - if(bar != RT_NULL) - { - rtgui_rect_t rect; - - rtgui_widget_get_rect(RTGUI_WIDGET(container), &rect); - rtgui_widget_rect_to_device(RTGUI_WIDGET(container),&rect); - rect.x1 += left; - rect.y1 += top; - - bar->thumb_w = w; - if(orient == RTGUI_VERTICAL) - { - rect.x2 = rect.x1+w; - rect.y2 = rect.y1+len; - } - else - { - rect.x2 = rect.x1+len; - rect.y2 = rect.y1+w; - } - - rtgui_widget_set_rect(RTGUI_WIDGET(bar), &rect); - - bar->orient = orient; - - rtgui_container_add_child(container, RTGUI_WIDGET(bar)); - } - - return bar; -} - -void rtgui_scrollbar_destroy(rtgui_scrollbar_t* bar) -{ - rtgui_widget_destroy(RTGUI_WIDGET(bar)); -} - -const static rt_uint8_t _up_arrow[] = {0x10, 0x38, 0x7C, 0xFE}; -const static rt_uint8_t _down_arrow[] = {0xFE,0x7C, 0x38, 0x10}; -const static rt_uint8_t _left_arrow[] = {0x10, 0x30, 0x70, 0xF0, 0x70, 0x30, 0x10}; -const static rt_uint8_t _right_arrow[] = {0x80, 0xC0, 0xE0, 0xF0, 0xE0, 0xC0, 0x80}; - -void rtgui_scrollbar_ondraw(rtgui_scrollbar_t* bar) -{ - /* draw scroll bar */ - rtgui_rect_t rect, btn_rect, thum_rect, arrow_rect; - struct rtgui_dc* dc; - rtgui_color_t bc; - - RT_ASSERT(bar != RT_NULL); - - /* begin drawing */ - dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(bar)); - if(dc == RT_NULL)return; - - bc = RTGUI_DC_BC(dc); - /* begin drawing */ - rtgui_widget_get_rect(RTGUI_WIDGET(bar), &rect); - RTGUI_DC_BC(dc) = white; - rtgui_dc_fill_rect(dc,&rect); - RTGUI_DC_BC(dc) = bc; - - if(bar->orient == RTGUI_VERTICAL) - { - btn_rect = rect; - btn_rect.y2 = btn_rect.y1 + (rect.x2 - rect.x1); - rtgui_dc_fill_rect(dc,&btn_rect); - /* draw up button */ - if(bar->status & SBS_UPARROW) - rtgui_dc_draw_border(dc, &btn_rect, RTGUI_BORDER_SUNKEN); - else - rtgui_dc_draw_border(dc, &btn_rect, RTGUI_BORDER_RAISE); - /* draw up arrow */ - arrow_rect.x1 = 0; arrow_rect.y1 = 0; - arrow_rect.x2 = 7; arrow_rect.y2 = 4; - rtgui_rect_moveto_align(&btn_rect, &arrow_rect, RTGUI_ALIGN_CENTER); - rtgui_dc_draw_byte(dc, arrow_rect.x1, arrow_rect.y1, rtgui_rect_height(arrow_rect), _up_arrow); - - /* draw thumb */ - rtgui_scrollbar_get_thumb_rect(bar, &thum_rect); - rtgui_dc_fill_rect(dc,&thum_rect); - rtgui_dc_draw_border(dc, &thum_rect, RTGUI_BORDER_RAISE); - - /* draw down button */ - btn_rect.y1 = rect.y2 - (rect.x2 - rect.x1); - btn_rect.y2 = rect.y2; - rtgui_dc_fill_rect(dc,&btn_rect); - if(bar->status & SBS_DOWNARROW) - rtgui_dc_draw_border(dc, &btn_rect, RTGUI_BORDER_SUNKEN); - else - rtgui_dc_draw_border(dc, &btn_rect, RTGUI_BORDER_RAISE); - /* draw down arrow */ - arrow_rect.x1 = 0; - arrow_rect.y1 = 0; - arrow_rect.x2 = 7; - arrow_rect.y2 = 4; - rtgui_rect_moveto_align(&btn_rect, &arrow_rect, RTGUI_ALIGN_CENTER); - rtgui_dc_draw_byte(dc, arrow_rect.x1, arrow_rect.y1, rtgui_rect_height(arrow_rect), _down_arrow); - } - else - { - btn_rect = rect; - btn_rect.x2 = btn_rect.x1 + (rect.y2 - rect.y1); - rtgui_dc_fill_rect(dc,&btn_rect); - /* draw left button */ - if(bar->status & SBS_LEFTARROW) - rtgui_dc_draw_border(dc, &btn_rect, RTGUI_BORDER_SUNKEN); - else - rtgui_dc_draw_border(dc, &btn_rect, RTGUI_BORDER_RAISE); - arrow_rect.x1 = 0; - arrow_rect.y1 = 0; - arrow_rect.x2 = 4; - arrow_rect.y2 = 7; - rtgui_rect_moveto_align(&btn_rect, &arrow_rect, RTGUI_ALIGN_CENTER); - rtgui_dc_draw_byte(dc, arrow_rect.x1, arrow_rect.y1, rtgui_rect_height(arrow_rect), _left_arrow); - - /* draw thumb */ - if(RTGUI_WIDGET_IS_ENABLE(bar)) - { - rtgui_scrollbar_get_thumb_rect(bar, &thum_rect); - rtgui_dc_fill_rect(dc,&thum_rect); - rtgui_dc_draw_border(dc, &thum_rect, RTGUI_BORDER_RAISE); - } - - btn_rect.x1 = rect.x2 - (rect.y2-rect.y1); - btn_rect.x2 = rect.x2; - rtgui_dc_fill_rect(dc,&btn_rect); - /* draw right button */ - if(bar->status & SBS_RIGHTARROW) - rtgui_dc_draw_border(dc, &btn_rect, RTGUI_BORDER_SUNKEN); - else - rtgui_dc_draw_border(dc, &btn_rect, RTGUI_BORDER_RAISE); - arrow_rect.x1 = 0; - arrow_rect.y1 = 0; - arrow_rect.x2 = 4; - arrow_rect.y2 = 7; - rtgui_rect_moveto_align(&btn_rect, &arrow_rect, RTGUI_ALIGN_CENTER); - rtgui_dc_draw_byte(dc, arrow_rect.x1, arrow_rect.y1, rtgui_rect_height(arrow_rect), _right_arrow); - - } - - rtgui_dc_end_drawing(dc); -} - -void rtgui_scrollbar_get_thumb_rect(rtgui_scrollbar_t *bar, rtgui_rect_t *erect) -{ - rtgui_rect_t rect; - - rtgui_widget_get_rect(RTGUI_WIDGET(bar), &rect); - if(bar->orient & RTGUI_VERTICAL) - { - /* vertical scroll bar */ - erect->x1 = rect.x1; - erect->x2 = rect.x2; - erect->y1 = rect.y1 + rtgui_rect_width(rect) + get_scrollbar_pos(bar); - erect->y2 = erect->y1 + bar->thumb_len; - } - else - { - /* horizontal scroll bar */ - erect->x1 = rect.x1 + rtgui_rect_height(rect) + get_scrollbar_pos(bar); - erect->x2 = erect->x1 + bar->thumb_len; - erect->y1 = rect.y1; - erect->y2 = rect.y2; - } -} - -static rtgui_point_t sbar_mouse_move_size; - -static void _rtgui_scrollbar_on_mouseclick(rtgui_scrollbar_t *bar, rtgui_event_t * event) -{ - rtgui_rect_t btn_rect, bar_rect,rect; - rt_uint32_t pos; - struct rtgui_event_mouse *mouse = (struct rtgui_event_mouse*)event; - - RT_ASSERT(bar != RT_NULL); - - /* get value */ - pos = get_scrollbar_pos(bar); - - rtgui_widget_get_rect(RTGUI_WIDGET(bar), &rect); - rtgui_widget_rect_to_device(RTGUI_WIDGET(bar),&rect); - - if(bar->orient == RTGUI_VERTICAL) - { - /* arrange vertical */ - /* get up arrow button rect */ - btn_rect.x1 = rect.x1; - btn_rect.x2 = rect.x2; - btn_rect.y1 = rect.y1; - btn_rect.y2 = rect.y1 + rtgui_rect_width(rect); - - if(rtgui_rect_contains_point(&btn_rect, mouse->x, mouse->y) == RT_EOK) - { - if((mouse->button & RTGUI_MOUSE_BUTTON_LEFT) && (mouse->button & RTGUI_MOUSE_BUTTON_DOWN)) - { - bar->status |= SBS_UPARROW; - if(bar->value==0) - { - rtgui_scrollbar_ondraw(bar); - return; - } - /* line step */ - bar->value -= bar->line_step; - if(bar->value < 0) bar->value = 0; - } - else if(mouse->button & RTGUI_MOUSE_BUTTON_UP) - { - bar->status = 0; - } - goto __exit; - } - /* click on the thumb chunk, be going to dragging */ - rtgui_scrollbar_get_thumb_rect(bar, &bar_rect); - rtgui_widget_rect_to_device(RTGUI_WIDGET(bar),&bar_rect); - if(rtgui_rect_contains_point(&bar_rect, mouse->x, mouse->y) == RT_EOK) - { - /* on thumb */ - if((mouse->button & RTGUI_MOUSE_BUTTON_LEFT) && (mouse->button & RTGUI_MOUSE_BUTTON_DOWN)) - { - /* changed status into drag */ - bar->status |= SBS_VERTTHUMB; - sbar_mouse_move_size.x = mouse->x; - sbar_mouse_move_size.y = mouse->y; - } - else if(mouse->button & RTGUI_MOUSE_BUTTON_UP) - { - bar->status = 0; - } - goto __exit; - } - else - { - /* click on space without thumb */ - /* get bar rect */ - bar_rect.y1 = rect.y1 + rtgui_rect_width(rect); - bar_rect.y2 = rect.y2 - rtgui_rect_width(rect); - if(rtgui_rect_contains_point(&bar_rect, mouse->x, mouse->y) == RT_EOK) - { - if((mouse->button & RTGUI_MOUSE_BUTTON_LEFT) && (mouse->button & RTGUI_MOUSE_BUTTON_DOWN)) - { - /* page step */ - if(mouse->y < bar_rect.y1 + pos) - { - bar->status |= SBS_UPSPACE; - bar->value -= bar->page_step; - if(bar->value < 0) bar->value = 0; - } - else if(mouse->y > pos + bar->thumb_len) - { - bar->status |= SBS_DOWNSPACE; - bar->value += bar->page_step; - if(bar->value > bar->count) bar->value = bar->count; - } - } - goto __exit; - } - } - /* likewise foregoing */ - /* get down arrow button rect */ - bar_rect.x1 = rect.x1; - bar_rect.x2 = rect.x2; - btn_rect.y1 = rect.y2 - rtgui_rect_width(rect); - btn_rect.y2 = rect.y2; - if(rtgui_rect_contains_point(&btn_rect, mouse->x, mouse->y) == RT_EOK) - { - if((mouse->button & RTGUI_MOUSE_BUTTON_LEFT) && (mouse->button & RTGUI_MOUSE_BUTTON_DOWN)) - { - bar->status |= SBS_DOWNARROW; - if(bar->value==bar->count) - { - rtgui_scrollbar_ondraw(bar); - return; - } - /* line step */ - bar->value += bar->line_step; - if(bar->value > bar->count) bar->value = bar->count; - } - else if(mouse->button & RTGUI_MOUSE_BUTTON_UP) - bar->status = 0; - goto __exit; - } - } - else - { - /* get left arrow button rect */ - btn_rect.x1 = rect.x1; - btn_rect.x2 = rect.x1 + rtgui_rect_height(rect); - btn_rect.y1 = rect.y1; - btn_rect.y2 = rect.y2; - if(rtgui_rect_contains_point(&btn_rect, mouse->x, mouse->y) == RT_EOK) - { - if((mouse->button & RTGUI_MOUSE_BUTTON_LEFT) && (mouse->button & RTGUI_MOUSE_BUTTON_DOWN)) - { - bar->status |= SBS_LEFTARROW; - if(bar->value==0) - { - rtgui_scrollbar_ondraw(bar); - return; - } - /* line step */ - bar->value -= bar->line_step; - if(bar->value < 0) bar->value = 0; - } - else if(mouse->button & RTGUI_MOUSE_BUTTON_UP) - bar->status = 0; - goto __exit; - } - - rtgui_scrollbar_get_thumb_rect(bar, &bar_rect); - rtgui_widget_rect_to_device(RTGUI_WIDGET(bar),&bar_rect); - if(rtgui_rect_contains_point(&bar_rect, mouse->x, mouse->y) == RT_EOK) - { - /* on the thumb */ - if((mouse->button & RTGUI_MOUSE_BUTTON_LEFT) && (mouse->button & RTGUI_MOUSE_BUTTON_DOWN)) - { - bar->status |= SBS_HORZTHUMB; - sbar_mouse_move_size.x = mouse->x; - sbar_mouse_move_size.y = mouse->y; - } - else if(mouse->button & RTGUI_MOUSE_BUTTON_UP) - { - bar->status = 0; - } - goto __exit; - } - else - { - /* get bar rect */ - bar_rect.x1 = rect.x1 + rtgui_rect_height(rect); - bar_rect.x2 = rect.x2 - rtgui_rect_height(rect); - bar_rect.y1 = rect.y1; - bar_rect.y2 = rect.y2; - if(rtgui_rect_contains_point(&bar_rect, mouse->x, mouse->y) == RT_EOK) - { - if((mouse->button & RTGUI_MOUSE_BUTTON_LEFT) && (mouse->button & RTGUI_MOUSE_BUTTON_DOWN)) - { - /* page step */ - if(mouse->x < bar_rect.x1 + pos) - { - bar->status |= SBS_LEFTSPACE; - bar->value -= bar->page_step; - if(bar->value < 0) bar->value = 0; - } - else if(mouse->x > pos + bar->thumb_len) - { - bar->status |= SBS_RIGHTSPACE; - bar->value += bar->page_step; - if(bar->value > bar->count) bar->value = bar->count; - } - } - else if(mouse->button & RTGUI_MOUSE_BUTTON_UP) - { - bar->status = 0; - } - goto __exit; - } - } - /* get right arrow button rect */ - btn_rect.x1 = rect.x2 - rtgui_rect_height(rect); - btn_rect.x2 = rect.x2; - bar_rect.y1 = rect.y1; - bar_rect.y2 = rect.y2; - if(rtgui_rect_contains_point(&btn_rect, mouse->x, mouse->y) == RT_EOK) - { - if((mouse->button & RTGUI_MOUSE_BUTTON_LEFT) && (mouse->button & RTGUI_MOUSE_BUTTON_DOWN)) - { - bar->status |= SBS_RIGHTARROW; - if(bar->value==bar->count) - { - rtgui_scrollbar_ondraw(bar); - return; - } - /* line step */ - bar->value += bar->line_step; - if(bar->value > bar->count) bar->value = bar->count; - } - else if(mouse->button & RTGUI_MOUSE_BUTTON_UP) - bar->status = 0; - goto __exit; - } - } - -__exit: - rtgui_scrollbar_ondraw(bar); - if((mouse->button & RTGUI_MOUSE_BUTTON_LEFT) && (mouse->button & RTGUI_MOUSE_BUTTON_DOWN)) - { - if(bar->widget_link != RT_NULL && bar->on_scroll != RT_NULL) - { - rtgui_widget_focus(bar->widget_link); - bar->on_scroll(RTGUI_OBJECT(bar->widget_link), event); - } - } -} - -/* thumb chunk activity */ -static void _rtgui_scrollbar_on_mousemotion(rtgui_scrollbar_t *bar, rtgui_event_t * event) -{ - float tmppos; - rt_uint32_t pos; - struct rtgui_event_mouse *mouse = (struct rtgui_event_mouse*)event; -rt_kprintf("sbar mouse motion.\n"); - tmppos = _rtgui_scrollbar_get_length(bar); - tmppos /= bar->count; - pos = (rt_uint32_t)tmppos; - - if(bar->orient == RTGUI_VERTICAL) - { - if(bar->status & SBS_VERTTHUMB) - { - /* from then on mouseclick */ - if((mouse->y-sbar_mouse_move_size.y) > 3) - { - bar->status |= SBS_DOWNTHUMB; - } - else if((mouse->y-sbar_mouse_move_size.y) < -3) - { - bar->status |= SBS_UPTHUMB; - } - else bar->status &= ~(SBS_UPTHUMB|SBS_DOWNTHUMB); - - if(abs(mouse->y-sbar_mouse_move_size.y) >= pos) - { - int step = abs(mouse->y-sbar_mouse_move_size.y)/pos; - sbar_mouse_move_size.y = mouse->y; - - if(bar->status & SBS_UPTHUMB) - { - bar->value -= step; - if(bar->value < 0) bar->value = 0; - } - else if(bar->status & SBS_DOWNTHUMB) - { - bar->value += step; - if(bar->value > bar->count) bar->value = bar->count; - } - goto __exit; - } - } - else if(bar->status & SBS_UPARROW) - { - /* on-going push down uparrow button */ - if(bar->value==0)return; - bar->value -= bar->line_step; - if(bar->value < 0) bar->value = 0; - goto __exit; - } - else if(bar->status & SBS_DOWNARROW) - { - /* on-going push down downarrow button */ - if(bar->value==bar->count)return; - bar->value += bar->line_step; - if(bar->value > bar->count) bar->value = bar->count; - goto __exit; - } - /*else if(bar->status & SBS_UPSPACE) - { - bar->value -= bar->page_step; - if(bar->value < 0) bar->value = 0; - goto __exit; - } - else if(bar->status & SBS_DOWNSPACE) - { - bar->value += bar->page_step; - if(bar->value > bar->count) bar->value = bar->count; - goto __exit; - }*/ - return; - } - else - { - if(bar->status & SBS_HORZTHUMB) - {rt_kprintf("HORZTHUMB, move event\n"); - if((mouse->x-sbar_mouse_move_size.x) > 5) - { - bar->status |= SBS_RIGHTTHUMB; - } - else if((mouse->x-sbar_mouse_move_size.x) < -5) - { - bar->status |= SBS_LEFTTHUMB; - } - if(abs(mouse->x-sbar_mouse_move_size.x) > pos) - { - int step = abs(mouse->x-sbar_mouse_move_size.x)/pos; - sbar_mouse_move_size.x = mouse->x; - if(bar->status & SBS_LEFTTHUMB) - { - bar->value -= step; - if(bar->value < 0) bar->value = 0; - } - else if(bar->status & SBS_RIGHTTHUMB) - { - bar->value += step; - if(bar->value > bar->count) bar->value = bar->count; - } - goto __exit; - } - } - else if(bar->status & SBS_LEFTARROW) - { - if(bar->value==0)return; - bar->value -= bar->line_step; - if(bar->value < 0) bar->value = 0; - goto __exit; - } - else if(bar->status & SBS_RIGHTARROW) - { - if(bar->value==bar->count)return; - bar->value += bar->line_step; - if(bar->value > bar->count) bar->value = bar->count; - goto __exit; - } - /*else if(bar->status & SBS_LEFTSPACE) - { - bar->value -= bar->page_step; - if(bar->value < bar->min) bar->value = bar->min; - goto __exit; - } - else if(bar->status & SBS_RIGHTSPACE) - { - bar->value += bar->page_step; - if(bar->value > bar->count-1) bar->value = bar->count-1; - goto __exit; - }*/ - return; - } - -__exit: - rtgui_scrollbar_ondraw(bar); - if(bar->widget_link != RT_NULL && bar->on_scroll != RT_NULL) - { - rtgui_widget_focus(bar->widget_link); - bar->on_scroll(RTGUI_OBJECT(bar->widget_link), event); - } -} - - -rt_bool_t rtgui_scrollbar_event_handler(rtgui_object_t *object, rtgui_event_t *event) -{ - rtgui_widget_t *widget = RTGUI_WIDGET(object); - rtgui_scrollbar_t* bar = RTGUI_SCROLLBAR(object); - - switch(event->type) - { - case RTGUI_EVENT_PAINT: -#ifndef RTGUI_USING_SMALL_SIZE - if(widget->on_draw != RT_NULL) - widget->on_draw(object, event); - else -#endif - { - if(!RTGUI_WIDGET_IS_HIDE(bar)) - rtgui_scrollbar_ondraw(bar); - } - break; - - case RTGUI_EVENT_MOUSE_BUTTON: - if(RTGUI_WIDGET_IS_ENABLE(widget)) - { -#ifndef RTGUI_USING_SMALL_SIZE - if(widget->on_mouseclick != RT_NULL) - { - widget->on_mouseclick(object, event); - } - else -#endif - { - _rtgui_scrollbar_on_mouseclick(bar, event); - } - } - break; - case RTGUI_EVENT_MOUSE_MOTION: - if(RTGUI_WIDGET_IS_ENABLE(widget)) - { - _rtgui_scrollbar_on_mousemotion(bar, event); - } - - default: - return rtgui_widget_event_handler(object, event); - } - - return RT_FALSE; -} - -void rtgui_scrollbar_set_orientation(rtgui_scrollbar_t* bar, int orient) -{ - RT_ASSERT(bar != RT_NULL); - - bar->orient = orient; -} - -/* get active area length */ -rt_uint32_t get_sbar_active_len(rtgui_scrollbar_t *bar) -{ - rtgui_rect_t rect; - - rtgui_widget_get_rect(RTGUI_WIDGET(bar), &rect); - - if(bar->orient & RTGUI_VERTICAL) - return rtgui_rect_height(rect) - 2*rtgui_rect_width(rect); - else - return rtgui_rect_width(rect) - 2*rtgui_rect_height(rect); -} - -void rtgui_scrollbar_set_thumbbar_len(rtgui_scrollbar_t* bar) -{ - double size=0; - rt_uint32_t len,w; - - RT_ASSERT(bar != RT_NULL); - - /* Make sure called rtgui_scrollbar_set_range(),before you use under code. */ - size = bar->page_step; - size /= bar->count+bar->page_step; - size *= get_sbar_active_len(bar); - len = (rt_uint32_t)size ; - - w = bar->thumb_w; - - if(len < w/2) len = w/2; - - bar->thumb_len = len; -} - -/* -* please use them with below step: -* 1.SetLineStep();2.SetPageStep();3.SetRange(); -*/ - -void rtgui_scrollbar_set_line_step(rtgui_scrollbar_t* bar, int step) -{ - RT_ASSERT(bar != RT_NULL); - - bar->line_step = step; -} - -void rtgui_scrollbar_set_page_step(rtgui_scrollbar_t* bar, int step) -{ - RT_ASSERT(bar != RT_NULL); - - bar->page_step = step; -} - -void rtgui_scrollbar_set_range(rtgui_scrollbar_t* bar, int count) -{ - RT_ASSERT(bar != RT_NULL); - - /* disable or enable scrollbar */ - if(bar->page_step >= count) - { - /* disable bar */ - RTGUI_WIDGET_DISABLE(bar); - } - else - { - /* enable bar */ - RTGUI_WIDGET_ENABLE(bar); - } - - /* thumb step count = item_count-item_per_page */ - bar->count = (rt_int16_t)(count-bar->page_step); - rtgui_scrollbar_set_thumbbar_len(bar); -} - -/* use VALUE change be binding widget's frist item. */ -void rtgui_scrollbar_set_value(rtgui_scrollbar_t* bar, rt_int16_t value) -{ - RT_ASSERT(bar != RT_NULL); - - bar->value = value; - - if(bar->value < 0) bar->value = 0; - - rtgui_widget_update(RTGUI_WIDGET(bar)); -} - -void rtgui_scrollbar_set_onscroll(rtgui_scrollbar_t* bar, rtgui_event_handler_ptr handler) -{ - if(bar == RT_NULL || handler == RT_NULL) return; - - bar->on_scroll = handler; -} - -void rtgui_scrollbar_hide(rtgui_scrollbar_t* bar) -{ - rtgui_rect_t rect; - struct rtgui_dc* dc; - - RT_ASSERT(bar != RT_NULL); - - /* begin drawing */ - dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(bar)); - if(dc == RT_NULL)return; - - RTGUI_WIDGET_HIDE(bar); - - /* begin drawing */ - rtgui_widget_get_rect(RTGUI_WIDGET(bar), &rect); - if((RTGUI_WIDGET(bar))->parent != RT_NULL) - RTGUI_DC_BC(dc) = RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(bar)->parent); - else - RTGUI_DC_BC(dc) = RTGUI_RGB(225, 228, 220); - rtgui_dc_fill_rect(dc,&rect); - - rtgui_dc_end_drawing(dc); -} diff --git a/components/rtgui/widgets/slider.c b/components/rtgui/widgets/slider.c deleted file mode 100644 index 01ef2a340b..0000000000 --- a/components/rtgui/widgets/slider.c +++ /dev/null @@ -1,304 +0,0 @@ -/* - * File : slider.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2010, 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-16 Bernard first version - * 2010-09-10 Bernard fix hide issue - */ -#include <rtgui/dc.h> -#include <rtgui/rtgui_theme.h> -#include <rtgui/widgets/slider.h> - -#define RTGUI_SLIDER_DEFAULT_WIDTH 100 -#define RTGUI_SLIDER_DEFAULT_HEIGHT 20 -#define RTGUI_SLIDER_DEFAULT_MIN 0 -#define RTGUI_SLIDER_DEFAULT_MAX 100 - -static rt_bool_t rtgui_slider_onunfocus(struct rtgui_object *object, rtgui_event_t *event); - -static void _rtgui_slider_constructor(rtgui_slider_t *slider) -{ - rtgui_rect_t rect = {0, 0, RTGUI_SLIDER_DEFAULT_WIDTH, RTGUI_SLIDER_DEFAULT_HEIGHT}; - - /* init widget and set event handler */ - RTGUI_WIDGET(slider)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE; - rtgui_widget_set_rect(RTGUI_WIDGET(slider), &rect); - rtgui_object_set_event_handler(RTGUI_OBJECT(slider), rtgui_slider_event_handler); - rtgui_widget_set_onunfocus(RTGUI_WIDGET(slider), rtgui_slider_onunfocus); - /* set proper of control */ - slider->min = RTGUI_SLIDER_DEFAULT_MIN; - slider->max = RTGUI_SLIDER_DEFAULT_MAX; - slider->value = RTGUI_SLIDER_DEFAULT_MIN; - slider->orient = RTGUI_HORIZONTAL; - - slider->ticks = 10; - slider->thumb_width = 8; - slider->on_changed = RT_NULL; -} - -DEFINE_CLASS_TYPE(slider, "slider", - RTGUI_WIDGET_TYPE, - _rtgui_slider_constructor, - RT_NULL, - sizeof(struct rtgui_slider)); - -static void rtgui_slider_onmouse(struct rtgui_slider *slider, struct rtgui_event_mouse *event) -{ - RT_ASSERT(slider != RT_NULL); - RT_ASSERT(event != RT_NULL); - - if (event->button & RTGUI_MOUSE_BUTTON_DOWN && - event->button & RTGUI_MOUSE_BUTTON_LEFT) - { - int sel; - int range = slider->max - slider->min; - int x0, xsize; - int x; - x0 = 1 + slider->thumb_width / 2; - - if (slider->orient == RTGUI_VERTICAL) - { - x = event->y - RTGUI_WIDGET(slider)->extent.y1; - x -= x0; - xsize = rtgui_rect_height(RTGUI_WIDGET(slider)->extent) - 2 * x0; - } - else - { - x = event->x - RTGUI_WIDGET(slider)->extent.x1; - x -= x0; - xsize = rtgui_rect_width(RTGUI_WIDGET(slider)->extent) - 2 * x0; - } - - if (x <= 0) - { - sel = slider->min; - } - else if (x >= xsize) - { - sel = slider->max; - } - else - { - sel = ((range * x) + xsize / 2) / xsize; - sel += slider->min; - } - - rtgui_widget_focus(RTGUI_WIDGET(slider)); - rtgui_slider_set_value(slider, sel); - if (slider->on_changed != RT_NULL) /* invoke callback function */ - slider->on_changed(RTGUI_WIDGET(slider), RT_NULL); - } -} - -static rt_bool_t rtgui_slider_onkey(struct rtgui_slider *slider, struct rtgui_event_kbd *event) -{ - RT_ASSERT(slider != RT_NULL); - RT_ASSERT(event != RT_NULL); - - if (!(RTGUI_KBD_IS_UP(event))) - return RT_TRUE; - - if (slider->orient == RTGUI_HORIZONTAL) - { - if (event->key == RTGUIK_RIGHT) - { - if (slider->value > slider->min) - slider->value++; - } - else if (event->key == RTGUIK_LEFT) - { - if (slider->value < slider->max) - slider->value--; - } - } - else - { - if (event->key == RTGUIK_UP) - { - if (slider->value > slider->min) - slider->value--; - } - else if (event->key == RTGUIK_DOWN) - { - if (slider->value < slider->max) - slider->value++; - } - } - - /* update widget */ - rtgui_widget_update(RTGUI_WIDGET(slider)); - if (slider->on_changed != RT_NULL) /* invoke callback function */ - slider->on_changed(RTGUI_WIDGET(slider), RT_NULL); - - return RT_TRUE; -} - -rt_bool_t rtgui_slider_event_handler(struct rtgui_object *object, struct rtgui_event *event) -{ - struct rtgui_widget *widget; - struct rtgui_slider *slider; - - RT_ASSERT(object != RT_NULL); - RT_ASSERT(event != RT_NULL); - - widget = RTGUI_WIDGET(object); - slider = RTGUI_SLIDER(object); - - switch (event->type) - { - case RTGUI_EVENT_PAINT: -#ifndef RTGUI_USING_SMALL_SIZE - if (widget->on_draw != RT_NULL) - widget->on_draw(RTGUI_OBJECT(widget), event); - else -#endif - { - rtgui_theme_draw_slider(slider); - } - - break; - - case RTGUI_EVENT_KBD: - if (!RTGUI_WIDGET_IS_ENABLE(widget) || RTGUI_WIDGET_IS_HIDE(widget)) return RT_FALSE; - -#ifndef RTGUI_USING_SMALL_SIZE - if (widget->on_key != RT_NULL) - return widget->on_key(RTGUI_OBJECT(widget), event); - else -#endif - return rtgui_slider_onkey(slider, (struct rtgui_event_kbd *)event); - - case RTGUI_EVENT_MOUSE_BUTTON: - if (!RTGUI_WIDGET_IS_ENABLE(widget) || RTGUI_WIDGET_IS_HIDE(widget)) return RT_FALSE; - -#ifndef RTGUI_USING_SMALL_SIZE - if (widget->on_mouseclick != RT_NULL) - widget->on_mouseclick(RTGUI_OBJECT(widget), event); - else -#endif - { - rtgui_slider_onmouse(slider, (struct rtgui_event_mouse *)event); - } - break; - - default: - return rtgui_widget_event_handler(object, event); - } - - return RT_FALSE; -} -RTM_EXPORT(rtgui_slider_event_handler); - -struct rtgui_slider *rtgui_slider_create(rt_size_t min, rt_size_t max, int orient) -{ - struct rtgui_slider *slider; - - slider = (struct rtgui_slider *) rtgui_widget_create(RTGUI_SLIDER_TYPE); - if (slider != RT_NULL) - { - /* set proper of control */ - slider->min = min; - slider->max = max; - slider->value = min; - - slider->ticks = 10; - slider->thumb_width = 8; - - rtgui_slider_set_orientation(slider, orient); - } - - return slider; -} -RTM_EXPORT(rtgui_slider_create); - -void rtgui_slider_set_range(struct rtgui_slider *slider, rt_size_t min, rt_size_t max) -{ - RT_ASSERT(slider != RT_NULL); - - slider->max = max; - slider->min = min; -} -RTM_EXPORT(rtgui_slider_set_range); - -void rtgui_slider_set_value(struct rtgui_slider *slider, rt_size_t value) -{ - RT_ASSERT(slider != RT_NULL); - - if (RTGUI_WIDGET_IS_ENABLE(slider)) - { - if (value < slider->min) value = slider->min; - if (value > slider->max) value = slider->max; - - if (slider->value != value) - { - slider->value = value; - rtgui_theme_draw_slider(slider); - } - } -} -RTM_EXPORT(rtgui_slider_set_value); - -void rtgui_slider_set_orientation(struct rtgui_slider *slider, int orientation) -{ - RT_ASSERT(slider != RT_NULL); - - /* set orientation */ - slider->orient = orientation; -#ifndef RTGUI_USING_SMALL_SIZE - if (slider->orient == RTGUI_HORIZONTAL) - { - /* HORIZONTAL */ - rtgui_widget_set_minheight(RTGUI_WIDGET(slider), RTGUI_SLIDER_DEFAULT_HEIGHT); - rtgui_widget_set_minwidth(RTGUI_WIDGET(slider), RTGUI_SLIDER_DEFAULT_WIDTH); - } - else - { - /* VERTICAL */ - rtgui_widget_set_minwidth(RTGUI_WIDGET(slider), RTGUI_SLIDER_DEFAULT_HEIGHT); - rtgui_widget_set_minheight(RTGUI_WIDGET(slider), RTGUI_SLIDER_DEFAULT_WIDTH); - } -#endif -} -RTM_EXPORT(rtgui_slider_set_orientation); - -rt_size_t rtgui_slider_get_value(struct rtgui_slider *slider) -{ - RT_ASSERT(slider != RT_NULL); - - return slider->value; -} -RTM_EXPORT(rtgui_slider_get_value); - -static rt_bool_t rtgui_slider_onunfocus(struct rtgui_object *object, rtgui_event_t *event) -{ - rtgui_rect_t rect; - rtgui_widget_t *widget; - struct rtgui_dc *dc; - - RT_ASSERT(object); - widget = RTGUI_WIDGET(object); - - dc = rtgui_dc_begin_drawing(widget); - if (dc == RT_NULL) return RT_FALSE; - - rtgui_widget_get_rect(widget, &rect); - - if (!RTGUI_WIDGET_IS_FOCUSED(widget)) - { - /* only clear focus rect */ - rtgui_color_t color = RTGUI_DC_FC(dc); - RTGUI_DC_FC(dc) = RTGUI_DC_BC(dc); - rtgui_dc_draw_focus_rect(dc, &rect); - RTGUI_DC_FC(dc) = color; - } - - rtgui_dc_end_drawing(dc); - return RT_TRUE; -} diff --git a/components/rtgui/widgets/staticline.c b/components/rtgui/widgets/staticline.c deleted file mode 100644 index b3ee043f0d..0000000000 --- a/components/rtgui/widgets/staticline.c +++ /dev/null @@ -1,88 +0,0 @@ -#include <rtgui/dc.h> -#include <rtgui/rtgui_theme.h> -#include <rtgui/widgets/staticline.h> - -static void _rtgui_staticline_constructor(rtgui_staticline_t *staticline) -{ - /* init widget and set event handler */ - rtgui_rect_t rect = {0, 0, 100, 2}; - - rtgui_widget_set_rect(RTGUI_WIDGET(staticline), &rect); - staticline->orient = RTGUI_HORIZONTAL; - - rtgui_object_set_event_handler(RTGUI_OBJECT(staticline), rtgui_staticline_event_handler); -} - - -DEFINE_CLASS_TYPE(staticline, "staticline", - RTGUI_WIDGET_TYPE, - _rtgui_staticline_constructor, - RT_NULL, - sizeof(struct rtgui_staticline)); - -rt_bool_t rtgui_staticline_event_handler(struct rtgui_object *object, struct rtgui_event *event) -{ - struct rtgui_staticline *staticline; - RTGUI_WIDGET_EVENT_HANDLER_PREPARE - - staticline = RTGUI_STATICLINE(object); - switch (event->type) - { - case RTGUI_EVENT_PAINT: -#ifndef RTGUI_USING_SMALL_SIZE - if (widget->on_draw != RT_NULL) - widget->on_draw(RTGUI_OBJECT(widget), event); - else -#endif - rtgui_theme_draw_staticline(staticline); - break; - default: - return rtgui_widget_event_handler(object, event); - } - - return RT_FALSE; -} -RTM_EXPORT(rtgui_staticline_event_handler); - -rtgui_staticline_t *rtgui_staticline_create(int orientation) -{ - rtgui_staticline_t *staticline; - - staticline = (struct rtgui_staticline *) rtgui_widget_create(RTGUI_STATICLINE_TYPE); - if (staticline != RT_NULL) - { - rtgui_staticline_set_orientation(staticline, orientation); - } - - return staticline; -} -RTM_EXPORT(rtgui_staticline_create); - -void rtgui_staticline_destroy(rtgui_staticline_t *staticline) -{ - rtgui_widget_destroy(RTGUI_WIDGET(staticline)); -} -RTM_EXPORT(rtgui_staticline_destroy); - -void rtgui_staticline_set_orientation(rtgui_staticline_t *staticline, int orientation) -{ - RT_ASSERT(staticline != RT_NULL); - - staticline->orient = orientation; -#ifndef RTGUI_USING_SMALL_SIZE - if (orientation == RTGUI_HORIZONTAL) - { - /* HORIZONTAL */ - rtgui_widget_set_minheight(RTGUI_WIDGET(staticline), 2); - rtgui_widget_set_minwidth(RTGUI_WIDGET(staticline), 100); - } - else - { - /* VERTICAL */ - rtgui_widget_set_minwidth(RTGUI_WIDGET(staticline), 2); - rtgui_widget_set_minheight(RTGUI_WIDGET(staticline), 100); - } -#endif -} -RTM_EXPORT(rtgui_staticline_set_orientation); - diff --git a/components/rtgui/widgets/textbox.c b/components/rtgui/widgets/textbox.c deleted file mode 100644 index 6cc46e05f4..0000000000 --- a/components/rtgui/widgets/textbox.c +++ /dev/null @@ -1,691 +0,0 @@ -/* -* File : textbox.c -* This file is part of 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-16 Bernard first version -* 2011-01-224 Bernard fix backspace issue. -* 2012-06-09 asml refactor -* 2012-06-17 Grissiom misc cleanup and merge -*/ -#include <string.h> -#include <rtgui/widgets/textbox.h> -#include <rtgui/widgets/combobox.h> -#include <rtgui/rtgui_system.h> - -#include <ctype.h> - -static void rtgui_textbox_draw_caret(rtgui_textbox_t *box, rt_uint16_t position); -static rt_bool_t rtgui_textbox_onkey(struct rtgui_object *widget, rtgui_event_t *event); -static rt_bool_t rtgui_textbox_onfocus(struct rtgui_object *widget, rtgui_event_t *event); -static rt_bool_t rtgui_textbox_onunfocus(struct rtgui_object *widget, rtgui_event_t *event); - -static void _rtgui_textbox_constructor(rtgui_textbox_t *box) -{ - rtgui_rect_t rect; - - RTGUI_WIDGET_FLAG(RTGUI_WIDGET(box)) |= RTGUI_WIDGET_FLAG_FOCUSABLE; - - rtgui_object_set_event_handler(RTGUI_OBJECT(box), rtgui_textbox_event_handler); - rtgui_widget_set_onfocus(RTGUI_WIDGET(box), rtgui_textbox_onfocus); - rtgui_widget_set_onunfocus(RTGUI_WIDGET(box), rtgui_textbox_onunfocus); -#ifndef RTGUI_USING_SMALL_SIZE - rtgui_widget_set_onkey(RTGUI_WIDGET(box), rtgui_textbox_onkey); -#endif - - RTGUI_WIDGET_FOREGROUND(box) = black; - RTGUI_WIDGET_BACKGROUND(box) = white; - /* set default text align */ - RTGUI_WIDGET_TEXTALIGN(box) = RTGUI_ALIGN_CENTER_VERTICAL; - /* set proper of control */ - box->caret_timer = RT_NULL; - box->caret = RT_NULL; - - box->line = box->line_begin = box->position = 0; - box->flag = RTGUI_TEXTBOX_SINGLE; - /* allocate default line buffer */ - box->text = RT_NULL; - rtgui_textbox_set_mask_char(box, '*'); - - rtgui_font_get_metrics(RTGUI_WIDGET_FONT(box), "H", &rect); - rtgui_widget_set_minheight(RTGUI_WIDGET(box), - rtgui_rect_height(rect) + RTGUI_TEXTBOX_BORDER_WIDTH * 2); - /* at least, we want to display one char. */ - rtgui_widget_set_minwidth(RTGUI_WIDGET(box), - rtgui_rect_width(rect) + RTGUI_TEXTBOX_BORDER_WIDTH * 2 \ - + RTGUI_WIDGET_DEFAULT_MARGIN /* there is a margin in the beginning - of the text. */ - ); - box->font_width = rtgui_rect_width(rect); - box->on_enter = RT_NULL; - box->dis_length = 0; - box->first_pos = 0; -} - -static void _rtgui_textbox_deconstructor(rtgui_textbox_t *textbox) -{ - if (textbox->text != RT_NULL) - { - rtgui_free(textbox->text); - textbox->text = RT_NULL; - } - if (textbox->caret_timer != RT_NULL) - { - rtgui_timer_destory(textbox->caret_timer); - textbox->caret_timer = RT_NULL; - } - if (textbox->caret != RT_NULL) - { - rtgui_free(textbox->caret); - textbox->caret = RT_NULL; - } -} - -DEFINE_CLASS_TYPE(textbox, "textbox", - RTGUI_WIDGET_TYPE, - _rtgui_textbox_constructor, - _rtgui_textbox_deconstructor, - sizeof(struct rtgui_textbox)); - -static void rtgui_textbox_get_caret_rect(rtgui_textbox_t *box, rtgui_rect_t *rect, rt_uint16_t position) -{ - int font_h, box_h; - rtgui_rect_t item_rect; - - RT_ASSERT(box != RT_NULL); - - rtgui_widget_get_rect(RTGUI_WIDGET(box), rect); - - rtgui_font_get_metrics(RTGUI_WIDGET_FONT(box), "H", &item_rect); - font_h = rtgui_rect_height(item_rect); - box_h = rtgui_rect_height(*rect); - - rect->x1 += position * box->font_width + 2; - rect->x2 = rect->x1 + 2; - rect->y1 += (box_h - font_h) / 2; - rect->y2 = rect->y1 + font_h; -} - -static void rtgui_textbox_init_caret(rtgui_textbox_t *box, rt_uint16_t position) -{ - int x, y; - rtgui_color_t color; - rtgui_rect_t rect; - int ofs = 0; - - RT_ASSERT(box != RT_NULL); - - if (!RTGUI_WIDGET_IS_FOCUSED(box)) - return; - - rtgui_textbox_get_caret_rect(box, &box->caret_rect, position); - rect = box->caret_rect; - rtgui_widget_rect_to_device(RTGUI_WIDGET(box), &rect); - - if (box->caret == RT_NULL) - box->caret = rtgui_malloc(rtgui_rect_width(rect) * rtgui_rect_height(rect) * sizeof(rtgui_color_t)); - - for (x = rect.x1; x < rect.x2; x++) - { - for (y = rect.y1; y < rect.y2; y++) - { - rtgui_graphic_driver_get_default()->ops->get_pixel(&color, x, y); - *(box->caret + ofs) = color; - ofs++; - } - } -} - -/* draw caret */ -static void rtgui_textbox_draw_caret(rtgui_textbox_t *box, rt_uint16_t position) -{ - int x, y; - rtgui_color_t color; - rtgui_rect_t rect; - int ofs = 0; - struct rtgui_dc *dc; - - RT_ASSERT(box != RT_NULL); - if (box->caret == RT_NULL) - return; - - dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(box)); - if (dc == RT_NULL) - return; - - rect = box->caret_rect; - - for (x = rect.x1; x < rect.x2; x++) - { - for (y = rect.y1; y < rect.y2; y++) - { - color = *(box->caret + ofs); - ofs++; - if (box->flag & RTGUI_TEXTBOX_CARET_SHOW) - { - color = ~color; - } - rtgui_dc_draw_color_point(dc, x, y, color); - } - } - - rtgui_dc_end_drawing(dc); -} - -static void rtgui_textbox_timeout(rtgui_timer_t *timer, void *parameter) -{ - rtgui_textbox_t *box; - - box = RTGUI_TEXTBOX(parameter); - /* set caret flag */ - if (box->flag & RTGUI_TEXTBOX_CARET_SHOW) - box->flag &= ~RTGUI_TEXTBOX_CARET_SHOW; - else - box->flag |= RTGUI_TEXTBOX_CARET_SHOW; - rtgui_textbox_draw_caret(box, box->position); -} - -static void rtgui_textbox_onmouse(rtgui_textbox_t *box, struct rtgui_event_mouse *event) -{ - rt_size_t length; - rt_uint16_t posbak = box->position; - - RT_ASSERT(box != RT_NULL); - RT_ASSERT(event != RT_NULL); - - length = rt_strlen(box->text); - - if (event->button & RTGUI_MOUSE_BUTTON_LEFT && event->button & RTGUI_MOUSE_BUTTON_DOWN) - { - rt_int32_t x; - /* single line text */ - /* set caret position */ - x = event->x - RTGUI_WIDGET(box)->extent.x1; - if (x < 0) - { - box->position = 0; - } - else if (x > (length - box->first_pos) * box->font_width) - { - box->position = length - box->first_pos; - } - else - { - box->position = x / box->font_width; - } - - if (box->flag & RTGUI_TEXTBOX_CARET_SHOW) - { - if (box->caret_timer != RT_NULL) - rtgui_timer_stop(box->caret_timer); - - box->flag &= ~RTGUI_TEXTBOX_CARET_SHOW; - rtgui_textbox_draw_caret(box, posbak); - - if (box->caret_timer != RT_NULL) - rtgui_timer_start(box->caret_timer); - } - - rtgui_textbox_init_caret(box, box->position); - box->flag |= RTGUI_TEXTBOX_CARET_SHOW; - rtgui_textbox_draw_caret(box, box->position); - } -} - -static rt_bool_t rtgui_textbox_onkey(struct rtgui_object *widget, rtgui_event_t *event) -{ - rtgui_textbox_t *box = RTGUI_TEXTBOX(widget); - struct rtgui_event_kbd *ekbd = (struct rtgui_event_kbd *)event; - rt_size_t length; - rt_uint16_t posbak = box->position; - - RT_ASSERT(box != RT_NULL); - RT_ASSERT(ekbd != RT_NULL); - - /* handle the key at down time and nothing to do with up */ - if (RTGUI_KBD_IS_UP(ekbd)) - return RT_TRUE; - - if (box->dis_length == 0) - { - rtgui_rect_t rect; - - rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect); - - if (box->font_width == 0) - return RT_FALSE; - - box->dis_length = (rtgui_rect_width(rect) - 5) / box->font_width; - } - - length = rt_strlen(box->text); - if (ekbd->key == RTGUIK_DELETE) - { - /* delete latter character */ - if (box->first_pos + box->position == length - 1) - { - box->text[box->first_pos + box->position] = '\0'; - } - else - { - char *c; - - /* remove character */ - for (c = &box->text[box->first_pos + box->position]; c[1] != '\0'; c++) - *c = c[1]; - *c = '\0'; - } - } - else if (ekbd->key == RTGUIK_BACKSPACE) - { - /* delete front character */ - if (box->position == 0) - { - if(box->first_pos > 0) - { - if(box->first_pos > box->dis_length) - { - box->first_pos -= box->dis_length; - box->position = box->dis_length; - } - else - { - box->position = box->first_pos; - box->first_pos = 0; - } - } - } - else if (box->position == length-box->first_pos) - { - box->text[box->first_pos + box->position - 1] = '\0'; - box->position --; - } - else if (box->position != 0) - { - /* remove current character */ - if (box->position != 0) - { - char *c; - - /* remove character */ - for (c = &box->text[box->position - 1]; c[1] != '\0'; c++) - *c = c[1]; - *c = '\0'; - } - box->position --; - } - } - else if (ekbd->key == RTGUIK_LEFT) - { - /* move to prev */ - if (box->position > 0) - { - box->position --; - } - else - { - if(box->first_pos > 0) - box->first_pos -= 1;//DEBUG - } - } - else if (ekbd->key == RTGUIK_RIGHT) - { - /* move to next */ - if (box->first_pos + box->position < length) - { - if(box->position < box->dis_length) - box->position ++; - else - box->first_pos += 1;//DEBUG - } - } - else if (ekbd->key == RTGUIK_HOME) - { - /* move cursor to start */ - box->position = 0; - box->first_pos = 0; - } - else if (ekbd->key == RTGUIK_END) - { - /* move cursor to end */ - if(length > box->dis_length) - { - box->position = box->dis_length; - box->first_pos = length - box->dis_length; - } - else - { - box->position = length; - box->first_pos = 0; - } - } - else if (ekbd->key == RTGUIK_RETURN) - { - if (box->on_enter != RT_NULL) - { - box->on_enter(box, event); - } - } - else if (ekbd->key == RTGUIK_NUMLOCK) - { - /* change numlock state */ - /* - extern void update_number_lock(void); - update_number_lock(); - */ - } - else - { - if (isprint(ekbd->key)) - { - /* it's may print character */ - /* no buffer on this line */ - if (box->flag & RTGUI_TEXTBOX_DIGIT) - { - /* only input digit */ - if (!isdigit(ekbd->key)) - { - /* exception: '.' and '-' */ - if (ekbd->key != '.' && ekbd->key != '-')return RT_FALSE; - if (ekbd->key == '.' && strchr(box->text, '.'))return RT_FALSE; - - if (ekbd->key == '-') - { - if (length + 1 > box->line_length) return RT_FALSE; - - if (strchr(box->text, '-')) - { - char *c; - for (c = &box->text[0]; c != &box->text[length]; c++) - *c = *(c + 1); - box->text[length] = '\0'; - box->position --; - goto _exit; - } - else - { - char *c; - for (c = &box->text[length]; c != &box->text[0]; c--) - *c = *(c - 1); - box->text[0] = '-'; - box->text[length + 1] = '\0'; - box->position ++; - goto _exit; - } - } - } - } - if (length + 1 > box->line_length) return RT_FALSE; - - if (box->first_pos + box->position <= length - 1) - { - char *c; - - for (c = &box->text[length]; c != &box->text[box->first_pos + box->position]; c--) - *c = *(c - 1); - box->text[length + 1] = '\0'; - } - - box->text[box->first_pos + box->position] = ekbd->key; - if(box->position < box->dis_length) - box->position ++; - else - box->first_pos ++; - } - } - -_exit: - if (box->flag & RTGUI_TEXTBOX_CARET_SHOW) - { - if (box->caret_timer != RT_NULL) - rtgui_timer_stop(box->caret_timer); - - box->flag &= ~RTGUI_TEXTBOX_CARET_SHOW; - rtgui_textbox_draw_caret(box, posbak);/* refresh it */ - if (box->caret_timer != RT_NULL) - rtgui_timer_start(box->caret_timer); - } - - /* re-draw text box */ - rtgui_textbox_ondraw(box); - - rtgui_textbox_init_caret(box, box->position); - box->flag |= RTGUI_TEXTBOX_CARET_SHOW; - rtgui_textbox_draw_caret(box, box->position); - - return RT_TRUE; -} - -static rt_bool_t rtgui_textbox_onfocus(struct rtgui_object *widget, rtgui_event_t *event) -{ - rtgui_textbox_t *box = RTGUI_TEXTBOX(widget); - - /* if there is already a timer, don't create another one. */ - if (box->caret_timer == RT_NULL) - { - box->caret_timer = rtgui_timer_create(50, RT_TIMER_FLAG_PERIODIC, rtgui_textbox_timeout, box); - /* set caret to show */ - box->flag |= RTGUI_TEXTBOX_CARET_SHOW; - /* start caret timer */ - if (box->caret_timer != RT_NULL) - rtgui_timer_start(box->caret_timer); - } - - return RT_TRUE; -} - -static rt_bool_t rtgui_textbox_onunfocus(struct rtgui_object *widget, rtgui_event_t *event) -{ - rtgui_textbox_t *box = RTGUI_TEXTBOX(widget); - - /* stop caret timer */ - if (box->caret_timer != RT_NULL) - { - rtgui_timer_stop(box->caret_timer); - rtgui_timer_destory(box->caret_timer); - box->caret_timer = RT_NULL; - } - /* set caret to hide */ - box->flag &= ~RTGUI_TEXTBOX_CARET_SHOW; - rtgui_textbox_draw_caret(box, box->position); - - if (box->on_enter != RT_NULL) - box->on_enter(box, event); - - return RT_TRUE; -} - -rtgui_textbox_t *rtgui_textbox_create(const char *text, rt_uint32_t flag) -{ - rtgui_textbox_t *box; - - box = (struct rtgui_textbox *)rtgui_widget_create(RTGUI_TEXTBOX_TYPE); - if (box != RT_NULL) - { - /* allocate default line buffer */ - rtgui_textbox_set_value(box, text); - box->flag = flag; - } - - return box; -} - -void rtgui_textbox_destroy(rtgui_textbox_t *box) -{ - rtgui_widget_destroy(RTGUI_WIDGET(box)); -} - -void rtgui_textbox_ondraw(rtgui_textbox_t *box) -{ - /* draw button */ - rtgui_rect_t rect; - struct rtgui_dc *dc; - rtgui_color_t fc; - - RT_ASSERT(box != RT_NULL); - - /* begin drawing */ - dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(box)); - if (dc == RT_NULL) - return; - - /* get widget rect */ - rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect); - fc = RTGUI_WIDGET_FOREGROUND(box); - - rtgui_rect_inflate(&rect, -RTGUI_TEXTBOX_BORDER_WIDTH); - - /* fill widget rect with white color */ - RTGUI_WIDGET_BACKGROUND(box) = white; - rtgui_dc_fill_rect(dc, &rect); - - rtgui_rect_inflate(&rect, RTGUI_TEXTBOX_BORDER_WIDTH); - /* draw border */ - RTGUI_WIDGET_FOREGROUND(box) = RTGUI_RGB(123, 158, 189); - rtgui_dc_draw_rect(dc, &rect); - - /* draw text */ - RTGUI_WIDGET_FOREGROUND(box) = fc; - if (box->text != RT_NULL) - { - rect.x1 += RTGUI_WIDGET_DEFAULT_MARGIN; - /* draw single text */ - if (box->flag & RTGUI_TEXTBOX_MASK) - { - /* draw mask char */ - rt_size_t len = rt_strlen(box->text); - if (len > 0) - { - char *text_mask = rtgui_malloc(len + 1); - rt_memset(text_mask, box->mask_char, len + 1); - text_mask[len] = 0; - rtgui_dc_draw_text(dc, text_mask+box->first_pos, &rect); - rtgui_free(text_mask); - } - } - else - { - rtgui_dc_draw_text(dc, box->text+box->first_pos, &rect); - } - } - - rtgui_dc_end_drawing(dc); -} - -/* set textbox text */ -void rtgui_textbox_set_value(rtgui_textbox_t *box, const char *text) -{ - if (box->text != RT_NULL) - { - /* yet exist something */ - /* free the old text */ - rtgui_free(box->text); - box->text = RT_NULL; - } - - /* no something */ - box->line_length = ((rt_strlen(text) + 1) / RTGUI_TEXTBOX_LINE_MAX + 1) * RTGUI_TEXTBOX_LINE_MAX; - - /* allocate line buffer */ - box->text = rtgui_malloc(box->line_length+1); - rt_memset(box->text, 0, box->line_length+1); - - /* copy text */ - rt_memcpy(box->text, text, rt_strlen(text) + 1); - - /* set current position */ - box->position = rt_strlen(text); -} - -const char *rtgui_textbox_get_value(rtgui_textbox_t *box) -{ - return (const char *)box->text; -} - -void rtgui_textbox_set_mask_char(rtgui_textbox_t *box, const char ch) -{ - box->mask_char = ch; -} - -char rtgui_textbox_get_mask_char(rtgui_textbox_t *box) -{ - return box->mask_char; -} - -rt_err_t rtgui_textbox_set_line_length(rtgui_textbox_t *box, rt_size_t length) -{ - char *new_line; - - RT_ASSERT(box != RT_NULL); - - /* invalid length */ - if (length <= 0) - return -RT_ERROR; - - new_line = rtgui_realloc(box->text, length+1); - if (new_line == RT_NULL) - return -RT_ENOMEM; - - if (length < box->line_length) - new_line[length] = '\0'; - - box->line_length = length; - box->text = new_line; - - return RT_EOK; -} - -/* get textbox text area */ -void rtgui_textbox_get_edit_rect(rtgui_textbox_t *box, rtgui_rect_t *rect) -{ - rtgui_widget_get_rect(RTGUI_WIDGET(box), rect); - rtgui_rect_inflate(rect, -1); -} - -rt_bool_t rtgui_textbox_event_handler(struct rtgui_object *object, rtgui_event_t *event) -{ - rtgui_widget_t *widget = RTGUI_WIDGET(object); - rtgui_textbox_t *box = RTGUI_TEXTBOX(object); - - switch (event->type) - { - case RTGUI_EVENT_PAINT: -#ifndef RTGUI_USING_SMALL_SIZE - if (widget->on_draw != RT_NULL) - widget->on_draw(RTGUI_OBJECT(widget), event); - else -#endif - rtgui_textbox_ondraw(box); - break; - - case RTGUI_EVENT_MOUSE_BUTTON: -#ifndef RTGUI_USING_SMALL_SIZE - if (widget->on_mouseclick != RT_NULL) - widget->on_mouseclick(RTGUI_OBJECT(widget), event); - else -#endif - rtgui_textbox_onmouse(box, (struct rtgui_event_mouse *)event); - return RT_TRUE; - - case RTGUI_EVENT_KBD: -#ifndef RTGUI_USING_SMALL_SIZE - if (widget->on_key != RT_NULL) - widget->on_key(RTGUI_OBJECT(widget), event); - else -#endif - rtgui_textbox_onkey(RTGUI_OBJECT(box), (struct rtgui_event *)event); - return RT_TRUE; - - default: - return rtgui_widget_event_handler(RTGUI_OBJECT(widget), event); - } - - return RT_FALSE; -} - diff --git a/components/rtgui/widgets/textview.c b/components/rtgui/widgets/textview.c deleted file mode 100644 index 84465391cc..0000000000 --- a/components/rtgui/widgets/textview.c +++ /dev/null @@ -1,355 +0,0 @@ -/* - * File : textview.c - * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2006 - 2011, 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 - * 2011-03-05 Bernard first version - */ -#include <rtgui/dc.h> -#include <rtgui/rtgui_system.h> -#include <rtgui/widgets/textview.h> - -rt_inline char *_get_line_text(rtgui_textview_t *textview, rt_uint16_t index) -{ - char *line; - if (index < textview->line_count) - { - line = textview->lines + (index * textview->line_width); - return line; - } - - return RT_NULL; -} - -static void _calc_line(rtgui_textview_t *textview, const char *text) -{ - char *line; - const unsigned char *ptr; - rt_ubase_t line_index, line_position; - - if (textview->lines != RT_NULL) - { - rtgui_free(textview->lines); - textview->lines = RT_NULL; - textview->line_count = 0; - } - - /* get line count */ - line_index = 0; - line_position = 0; - ptr = (const unsigned char *)text; - if (*ptr == 0) return; - - while (*ptr != '\0') - { - if (*ptr == '\n') - { - line_index ++; - line_position = 0; - } - else if (*ptr == '\r') - { - ptr ++; - continue; - } - else if (*ptr == '\t') - { - line_position += 4; - if (line_position >= textview->line_width - 1) - { - line_index ++; - line_position = 0; - } - } - else - { - if ((*ptr) >= 0x80) - { - /* fill cjk character */ - if (line_position + 1 >= (textview->line_width - 1)) - { - /* split to next line */ - line_index ++; - line_position = 0; - } - - line_position ++; - line_position ++; - } - else - { - line_position ++; - } - - if (line_position >= textview->line_width - 1) - { - line_index ++; - line_position = 0; - } - } - - ptr ++; - } - - /* set line count */ - textview->line_count = line_index + 1; - - /* allocate lines */ - textview->lines = rtgui_malloc(textview->line_count * textview->line_width); - rt_memset(textview->lines, 0, (textview->line_count * textview->line_width)); - - /* fill lines */ - line_index = 0; - line_position = 0; - ptr = (const unsigned char *)text; - line = _get_line_text(textview, line_index); - while (*ptr) - { - if (*ptr == '\n') - { - line_index ++; - line_position = 0; - line = _get_line_text(textview, line_index); - } - else if (*ptr == '\r') - { - /* ignore '\r' */ - ptr ++; - continue; - } - else if (*ptr == '\t') - { - line[line_position++] = ' '; - line[line_position++] = ' '; - line[line_position++] = ' '; - line[line_position++] = ' '; - if (line_position >= textview->line_width - 1) - { - line_index ++; - line_position = 0; - line = _get_line_text(textview, line_index); - } - } - else - { - if ((*ptr) >= 0x80) - { - /* fill cjk character */ - if (line_position + 1 >= (textview->line_width - 1)) - { - /* split to next line */ - line_index ++; - line_position = 0; - line = _get_line_text(textview, line_index); - } - - line[line_position ++] = *ptr ++; - line[line_position ++] = *ptr; - } - else - { - line[line_position ++] = *ptr; - } - - if (line_position >= textview->line_width - 1) - { - line_index ++; - line_position = 0; - line = _get_line_text(textview, line_index); - } - } - - ptr ++; - } - - textview->line_current = 0; -} - -static void _calc_width(rtgui_textview_t *textview) -{ - rtgui_rect_t rect; - rt_uint16_t width, height; - - width = rtgui_rect_width(RTGUI_WIDGET(textview)->extent) - 6; - height = rtgui_rect_height(RTGUI_WIDGET(textview)->extent); - - rtgui_font_get_metrics(RTGUI_WIDGET_FONT(textview), "W", &rect); - textview->line_width = width / rtgui_rect_width(rect) + 1; - textview->line_page_count = height / (rtgui_rect_height(rect) + 3); - - /* set minimal value */ - if (textview->line_page_count == 0) textview->line_page_count = 1; -} - -static void _draw_textview(rtgui_textview_t *textview) -{ - struct rtgui_dc *dc; - struct rtgui_rect rect, font_rect; - char *line; - rt_ubase_t line_index, item_height; - - rtgui_font_get_metrics(RTGUI_WIDGET_FONT(textview), "W", &font_rect); - item_height = rtgui_rect_height(font_rect) + 3; - - dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(textview)); - if (dc == RT_NULL) return ; - - /* fill rect */ - rtgui_widget_get_rect(RTGUI_WIDGET(textview), &rect); - rtgui_dc_fill_rect(dc, &rect); - - rect.x1 += 3; - rect.x2 -= 3; - - for (line_index = textview->line_current; - (line_index < textview->line_current + textview->line_page_count) && - (line_index < textview->line_count); - line_index ++) - { - line = (char *)_get_line_text(textview, line_index); - rtgui_dc_draw_text(dc, line, &rect); - - rect.y1 += item_height; - } - - rtgui_dc_end_drawing(dc); -} - -static void _rtgui_textview_constructor(rtgui_textview_t *textview) -{ - /* init widget and set event handler */ - rtgui_object_set_event_handler(RTGUI_OBJECT(textview), rtgui_textview_event_handler); - RTGUI_WIDGET(textview)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE; - - /* set field */ - textview->line_count = 0; - textview->lines = RT_NULL; - - textview->line_current = -1; - textview->line_page_count = 1; -} - -static void _rtgui_textview_destructor(rtgui_textview_t *textview) -{ - /* release line memory */ - rtgui_free(textview->lines); - textview->lines = RT_NULL; -} - -DEFINE_CLASS_TYPE(textview, "textview", - RTGUI_WIDGET_TYPE, - _rtgui_textview_constructor, - _rtgui_textview_destructor, - sizeof(struct rtgui_textview)); - -rt_bool_t rtgui_textview_event_handler(struct rtgui_object *object, struct rtgui_event *event) -{ - struct rtgui_textview *textview; - RTGUI_WIDGET_EVENT_HANDLER_PREPARE - - textview = RTGUI_TEXTVIEW(object); - switch (event->type) - { - case RTGUI_EVENT_PAINT: - _draw_textview(textview); - break; - - case RTGUI_EVENT_KBD: - { - struct rtgui_event_kbd *ekbd = (struct rtgui_event_kbd *)event; - if (ekbd->type == RTGUI_KEYDOWN) - { - rt_int16_t line_current_update; - line_current_update = textview->line_current; - if (ekbd->key == RTGUIK_LEFT) - { - if (textview->line_current > textview->line_page_count) - { - line_current_update -= textview->line_page_count; - } - else if (textview->line_current > 0) - { - line_current_update = 0; - } - } - else if (ekbd->key == RTGUIK_RIGHT) - { - if (textview->line_current + textview->line_page_count < textview->line_count - 1) - { - line_current_update += textview->line_page_count; - } - } - else if (ekbd->key == RTGUIK_UP) - { - if (textview->line_current > 0) - { - line_current_update --; - } - } - else if (ekbd->key == RTGUIK_DOWN) - { - if (textview->line_current + textview->line_page_count < textview->line_count - 1) - { - line_current_update ++; - } - } - - if (textview->line_current != line_current_update) - { - textview->line_current = line_current_update; - rtgui_widget_update(widget); - return RT_TRUE; - } - } - break; - } - default: - return rtgui_widget_event_handler(RTGUI_OBJECT(widget), event); - } - - return RT_FALSE; -} - -rtgui_textview_t *rtgui_textview_create(const char *text, const rtgui_rect_t *rect) -{ - struct rtgui_textview *textview; - - textview = (struct rtgui_textview *) rtgui_widget_create(RTGUI_TEXTVIEW_TYPE); - if (textview != RT_NULL) - { - rtgui_widget_set_rect(RTGUI_WIDGET(textview), rect); - - /* calculate line width and line page count */ - _calc_width(textview); - - /* set text */ - _calc_line(textview, text); - } - - return textview; -} - -void rtgui_textview_destroy(rtgui_textview_t *textview) -{ - rtgui_widget_destroy(RTGUI_WIDGET(textview)); -} - -void rtgui_textview_set_text(rtgui_textview_t *textview, const char *text) -{ - RT_ASSERT(textview != RT_NULL); - - /* calculate line width and line page count */ - _calc_width(textview); - - /* set text */ - _calc_line(textview, text); - - /* update widget */ - rtgui_widget_update(RTGUI_WIDGET(textview)); -} diff --git a/components/rtgui/widgets/title.c b/components/rtgui/widgets/title.c deleted file mode 100644 index 840669e7f6..0000000000 --- a/components/rtgui/widgets/title.c +++ /dev/null @@ -1,75 +0,0 @@ -/* - * File : title.c - * This file is part of 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-16 Bernard first version - */ -#include <rtgui/widgets/title.h> -#include <rtgui/rtgui_system.h> - -/* there is no event handler in wintitle but handle the event on topwin of server */ -static void _rtgui_wintitle_constructor(rtgui_wintitle_t *wintitle) -{ - wintitle->title = RT_NULL; - RTGUI_WIDGET(wintitle)->flag = RTGUI_WIDGET_FLAG_DEFAULT; - RTGUI_WIDGET_TEXTALIGN(wintitle) = RTGUI_ALIGN_CENTER_VERTICAL; -} - -static void _rtgui_wintitle_deconstructor(rtgui_wintitle_t *wintitle) -{ - if (wintitle->title != RT_NULL) - rt_free(wintitle->title); - wintitle->title = RT_NULL; -} - -DEFINE_CLASS_TYPE(wintitle, "wintitle", - RTGUI_WIDGET_TYPE, - _rtgui_wintitle_constructor, - _rtgui_wintitle_deconstructor, - sizeof(struct rtgui_wintitle)); - -rtgui_wintitle_t *rtgui_wintitle_create(struct rtgui_win *window, const char *title) -{ - rtgui_wintitle_t *wintitle; - - wintitle = (rtgui_wintitle_t *)rtgui_widget_create(RTGUI_WINTITLE_TYPE); - if (wintitle != RT_NULL) - { - rtgui_wintitle_set_title(wintitle, title); - RTGUI_WIDGET(wintitle)->toplevel = window; - } - - return wintitle; -} - -void rtgui_wintitle_destroy(rtgui_wintitle_t *wintitle) -{ - rtgui_widget_destroy(RTGUI_WIDGET(wintitle)); -} - -void rtgui_wintitle_set_title(rtgui_wintitle_t *wintitle, const char *title) -{ - RT_ASSERT(wintitle != RT_NULL); - - if (wintitle->title != RT_NULL) - { - rt_free(wintitle->title); - } - - if (title != RT_NULL) wintitle->title = (char *)rt_strdup((const char *)title); - else wintitle->title = RT_NULL; -} - -char *rtgui_wintitle_get_title(rtgui_wintitle_t *wintitle) -{ - RT_ASSERT(wintitle != RT_NULL); - - return wintitle->title; -} diff --git a/components/rtgui/widgets/widget.c b/components/rtgui/widgets/widget.c deleted file mode 100644 index 9340a5394a..0000000000 --- a/components/rtgui/widgets/widget.c +++ /dev/null @@ -1,794 +0,0 @@ -/* - * 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 - * 2010-06-26 Bernard add user_data to widget structure - */ - -#include <rtgui/dc_client.h> -#include <rtgui/rtgui_app.h> -#include <rtgui/widgets/widget.h> -#include <rtgui/widgets/window.h> -#include <rtgui/widgets/container.h> -#include <rtgui/widgets/notebook.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.textstyle = RTGUI_TEXTSTYLE_NORMAL; - widget->gc.textalign = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_TOP; - widget->align = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_TOP; - - /* clear the garbage value of extent and clip */ - widget->extent.x1 = widget->extent.y1 = 0; - widget->extent.x2 = widget->extent.y2 = 0; - rtgui_region_init_with_extents(&widget->clip, &widget->extent); - - /* set parent and toplevel root */ - widget->parent = RT_NULL; - widget->toplevel = RT_NULL; - - /* some common event handler */ - widget->on_focus_in = RT_NULL; - widget->on_focus_out = RT_NULL; - -#ifndef RTGUI_USING_SMALL_SIZE - widget->on_draw = RT_NULL; - widget->on_mouseclick = RT_NULL; - widget->on_key = RT_NULL; - widget->on_size = RT_NULL; - widget->on_command = RT_NULL; -#endif - - /* set default event handler */ - rtgui_object_set_event_handler(RTGUI_OBJECT(widget), rtgui_widget_event_handler); - - /* init user data private to 0 */ - widget->user_data = 0; - - /* init clip information */ - rtgui_region_init(&(widget->clip)); - - /* init hardware dc */ - rtgui_dc_client_init(widget); -} - -/* Destroys the widget */ -static void _rtgui_widget_destructor(rtgui_widget_t *widget) -{ - if (widget == RT_NULL) return; - - if (widget->parent != RT_NULL && RTGUI_IS_CONTAINER(widget->parent)) - { - /* 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)); -} - -DEFINE_CLASS_TYPE(widget, "widget", - RTGUI_OBJECT_TYPE, - _rtgui_widget_constructor, - _rtgui_widget_destructor, - sizeof(struct rtgui_widget)); -RTM_EXPORT(_rtgui_widget); - -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; -} -RTM_EXPORT(rtgui_widget_create); - -void rtgui_widget_destroy(rtgui_widget_t *widget) -{ - rtgui_object_destroy(RTGUI_OBJECT(widget)); -} -RTM_EXPORT(rtgui_widget_destroy); - -void rtgui_widget_set_rect(rtgui_widget_t *widget, const rtgui_rect_t *rect) -{ - int delta_x, delta_y; - - if (widget == RT_NULL || rect == RT_NULL) return; - - /* move to a logic position if it's a container widget */ - if (RTGUI_IS_CONTAINER(widget)) - { - delta_x = rect->x1 - widget->extent.x1; - delta_y = rect->y1 - widget->extent.y1; - - rtgui_widget_move_to_logic(widget, delta_x, delta_y); - } - - /* update extent rectangle */ - widget->extent = *rect; - if (RTGUI_IS_CONTAINER(widget)) - { - /* re-do layout */ - rtgui_container_layout(RTGUI_CONTAINER(widget)); - } - - /* reset min width and height */ - widget->min_width = rtgui_rect_width(widget->extent); - widget->min_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); - if ((widget->parent != RT_NULL) && (widget->toplevel != RT_NULL)) - { - /* update widget clip */ - rtgui_widget_update_clip(widget->parent); - } -} -RTM_EXPORT(rtgui_widget_set_rect); - -void rtgui_widget_set_rectangle(rtgui_widget_t *widget, int x, int y, int width, int height) -{ - rtgui_rect_t rect; - - rect.x1 = x; - rect.y1 = y; - rect.x2 = x + width; - rect.y2 = y + height; - - rtgui_widget_set_rect(widget, &rect); -} -RTM_EXPORT(rtgui_widget_set_rectangle); - -void rtgui_widget_set_parent(rtgui_widget_t *widget, rtgui_widget_t *parent) -{ - /* set parent and toplevel widget */ - widget->parent = parent; -} -RTM_EXPORT(rtgui_widget_set_parent); - -void rtgui_widget_get_extent(rtgui_widget_t *widget, rtgui_rect_t *rect) -{ - RT_ASSERT(widget != RT_NULL); - RT_ASSERT(rect != RT_NULL); - - *rect = widget->extent; -} -RTM_EXPORT(rtgui_widget_get_extent); - -void rtgui_widget_set_minsize(rtgui_widget_t *widget, int width, int height) -{ - RT_ASSERT(widget != RT_NULL); - widget->min_width = width; - widget->min_height = height; -} -RTM_EXPORT(rtgui_widget_set_minsize); - -void rtgui_widget_set_minwidth(rtgui_widget_t *widget, int width) -{ - RT_ASSERT(widget != RT_NULL); - - widget->min_width = width; -} -RTM_EXPORT(rtgui_widget_set_minwidth); - -void rtgui_widget_set_minheight(rtgui_widget_t *widget, int height) -{ - RT_ASSERT(widget != RT_NULL); - - widget->min_height = height; -} -RTM_EXPORT(rtgui_widget_set_minheight); - -/* - * 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); - } - } -} -RTM_EXPORT(rtgui_widget_move_to_logic); - -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; - } -} -RTM_EXPORT(rtgui_widget_get_rect); - -/** - * set widget draw style - */ -void rtgui_widget_set_border(rtgui_widget_t *widget, rt_uint32_t style) -{ - RT_ASSERT(widget != RT_NULL); - - widget->border_style = style; - switch (style) - { - case RTGUI_BORDER_NONE: - widget->border = 0; - break; - case RTGUI_BORDER_SIMPLE: - case RTGUI_BORDER_UP: - case RTGUI_BORDER_DOWN: - widget->border = 1; - break; - case RTGUI_BORDER_STATIC: - case RTGUI_BORDER_RAISE: - case RTGUI_BORDER_SUNKEN: - case RTGUI_BORDER_BOX: - case RTGUI_BORDER_EXTRA: - widget->border = 2; - break; - default: - widget->border = 2; - break; - } -} -RTM_EXPORT(rtgui_widget_set_border); - -void rtgui_widget_set_onfocus(rtgui_widget_t *widget, rtgui_event_handler_ptr handler) -{ - RT_ASSERT(widget != RT_NULL); - - widget->on_focus_in = handler; -} -RTM_EXPORT(rtgui_widget_set_onfocus); - -void rtgui_widget_set_onunfocus(rtgui_widget_t *widget, rtgui_event_handler_ptr handler) -{ - RT_ASSERT(widget != RT_NULL); - - widget->on_focus_out = handler; -} -RTM_EXPORT(rtgui_widget_set_onunfocus); - -#ifndef RTGUI_USING_SMALL_SIZE -void rtgui_widget_set_ondraw(rtgui_widget_t *widget, rtgui_event_handler_ptr handler) -{ - RT_ASSERT(widget != RT_NULL); - - widget->on_draw = handler; -} -RTM_EXPORT(rtgui_widget_set_ondraw); - -void rtgui_widget_set_onmouseclick(rtgui_widget_t *widget, rtgui_event_handler_ptr handler) -{ - RT_ASSERT(widget != RT_NULL); - - widget->on_mouseclick = handler; -} -RTM_EXPORT(rtgui_widget_set_onmouseclick); - -void rtgui_widget_set_onkey(rtgui_widget_t *widget, rtgui_event_handler_ptr handler) -{ - RT_ASSERT(widget != RT_NULL); - - widget->on_key = handler; -} -RTM_EXPORT(rtgui_widget_set_onkey); - -void rtgui_widget_set_onsize(rtgui_widget_t *widget, rtgui_event_handler_ptr handler) -{ - RT_ASSERT(widget != RT_NULL); - - widget->on_size = handler; -} -RTM_EXPORT(rtgui_widget_set_onsize); - -void rtgui_widget_set_oncommand(rtgui_widget_t *widget, rtgui_event_handler_ptr handler) -{ - RT_ASSERT(widget != RT_NULL); - - widget->on_command = handler; -} -RTM_EXPORT(rtgui_widget_set_oncommand); -#endif - -/** - * @brief Focuses the widget. The focused widget is the widget which can receive 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) -{ - struct rtgui_widget *old_focus; - - RT_ASSERT(widget != RT_NULL); - - if (!RTGUI_WIDGET_IS_FOCUSABLE(widget) || !RTGUI_WIDGET_IS_ENABLE(widget)) - return; - - old_focus = RTGUI_WIN(widget->toplevel)->focused_widget; - if (old_focus == widget) - return; /* it's the same focused widget */ - - /* unfocused the old widget */ - if (old_focus != RT_NULL) - rtgui_widget_unfocus(old_focus); - - /* set widget as focused */ - widget->flag |= RTGUI_WIDGET_FLAG_FOCUS; - RTGUI_WIN(widget->toplevel)->focused_widget = widget; - - /* invoke on focus in call back */ - if (widget->on_focus_in != RT_NULL) - widget->on_focus_in(RTGUI_OBJECT(widget), RT_NULL); -} -RTM_EXPORT(rtgui_widget_focus); - -/** - * @brief Unfocused the widget - * @param widget a widget - */ -void rtgui_widget_unfocus(rtgui_widget_t *widget) -{ - - RT_ASSERT(widget != RT_NULL); - - if (!widget->toplevel || !RTGUI_WIDGET_IS_FOCUSED(widget)) - return; - - widget->flag &= ~RTGUI_WIDGET_FLAG_FOCUS; - - if (widget->on_focus_out != RT_NULL) - widget->on_focus_out(RTGUI_OBJECT(widget), RT_NULL); - - RTGUI_WIN(widget->toplevel)->focused_widget = RT_NULL; - - /* Ergodic constituent widget, make child loss of focus */ - if (RTGUI_IS_CONTAINER(widget)) - { - rtgui_list_t *node; - rtgui_list_foreach(node, &(RTGUI_CONTAINER(widget)->children)) - { - rtgui_widget_t *child = rtgui_list_entry(node, rtgui_widget_t, sibling); - if (RTGUI_WIDGET_IS_HIDE(child)) continue; - rtgui_widget_unfocus(child); - } - } -} -RTM_EXPORT(rtgui_widget_unfocus); - -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; - } -} -RTM_EXPORT(rtgui_widget_point_to_device); - -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; - } -} -RTM_EXPORT(rtgui_widget_rect_to_device); - -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; - } -} -RTM_EXPORT(rtgui_widget_point_to_logic); - -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; - } -} -RTM_EXPORT(rtgui_widget_rect_to_logic); - -struct rtgui_win *rtgui_widget_get_toplevel(rtgui_widget_t *widget) -{ - rtgui_widget_t *r; - - RT_ASSERT(widget != RT_NULL); - - if (widget->toplevel) - return widget->toplevel; - - rt_kprintf("widget->toplevel not properly set\n"); - r = widget; - /* get the toplevel widget */ - while (r->parent != RT_NULL) - r = r->parent; - - /* set toplevel */ - widget->toplevel = RTGUI_WIN(r); - - return RTGUI_WIN(r); -} -RTM_EXPORT(rtgui_widget_get_toplevel); - -rt_bool_t rtgui_widget_onupdate_toplvl(struct rtgui_object *object, struct rtgui_event *event) -{ - struct rtgui_widget *widget; - struct rtgui_event_update_toplvl *eup; - - RT_ASSERT(object); - RT_ASSERT(event); - - widget = RTGUI_WIDGET(object); - eup = (struct rtgui_event_update_toplvl *)event; - - widget->toplevel = eup->toplvl; - - return RT_FALSE; -} -RTM_EXPORT(rtgui_widget_onupdate_toplvl); - -rt_bool_t rtgui_widget_event_handler(struct rtgui_object *object, rtgui_event_t *event) -{ - RTGUI_WIDGET_EVENT_HANDLER_PREPARE; - - switch (event->type) - { - case RTGUI_EVENT_SHOW: - return rtgui_widget_onshow(object, event); - case RTGUI_EVENT_HIDE: - return rtgui_widget_onhide(object, event); - case RTGUI_EVENT_UPDATE_TOPLVL: - return rtgui_widget_onupdate_toplvl(object, event); -#ifndef RTGUI_USING_SMALL_SIZE - case RTGUI_EVENT_PAINT: - if (widget->on_draw != RT_NULL) - return widget->on_draw(RTGUI_OBJECT(widget), event); - break; - - case RTGUI_EVENT_KBD: - if (widget->on_key != RT_NULL) - return widget->on_key(RTGUI_OBJECT(widget), event); - break; - - case RTGUI_EVENT_MOUSE_BUTTON: - if (widget->on_mouseclick != RT_NULL) - return widget->on_mouseclick(RTGUI_OBJECT(widget), event); - break; - - case RTGUI_EVENT_COMMAND: - if (widget->on_command != RT_NULL) - return widget->on_command(RTGUI_OBJECT(widget), event); - break; - - case RTGUI_EVENT_RESIZE: - if (widget->on_size != RT_NULL) - return widget->on_size(RTGUI_OBJECT(widget), event); - break; -#endif - default: - return rtgui_object_event_handler(object, event); - } - - return RT_FALSE; -} -RTM_EXPORT(rtgui_widget_event_handler); - -/* - * 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, there is no clip to update. */ - if (parent == RT_NULL) - { - return; - } - - /* 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_IS_CONTAINER(parent)) - { - 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 view 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); - } - } - else if (RTGUI_IS_NOTEBOOK(widget)) - { - rtgui_widget_update_clip(rtgui_notebook_get_current(RTGUI_NOTEBOOK(widget))); - } -} -RTM_EXPORT(rtgui_widget_update_clip); - -void rtgui_widget_show(struct rtgui_widget *widget) -{ - struct rtgui_event_show eshow; - RT_ASSERT(widget != RT_NULL); - - if (!RTGUI_WIDGET_IS_HIDE(widget)) - return; - - RTGUI_EVENT_SHOW_INIT(&eshow); - if (RTGUI_OBJECT(widget)->event_handler != RT_NULL) - { - RTGUI_OBJECT(widget)->event_handler( - RTGUI_OBJECT(widget), - &eshow); - } -} -RTM_EXPORT(rtgui_widget_show); - -rt_bool_t rtgui_widget_onshow(struct rtgui_object *object, struct rtgui_event *event) -{ - struct rtgui_widget *widget = RTGUI_WIDGET(object); - - if (!RTGUI_WIDGET_IS_HIDE(object)) - return RT_FALSE; - - RTGUI_WIDGET_UNHIDE(widget); - - return RT_FALSE; -} -RTM_EXPORT(rtgui_widget_onshow); - -rt_bool_t rtgui_widget_onhide(struct rtgui_object *object, struct rtgui_event *event) -{ - struct rtgui_widget *widget = RTGUI_WIDGET(object); - - if (RTGUI_WIDGET_IS_HIDE(object)) - return RT_FALSE; - - /* hide this widget */ - RTGUI_WIDGET_HIDE(widget); - - if (widget->parent != RT_NULL) - { - rtgui_widget_t *parent; - - parent = widget->parent; - /* get the no transparent parent */ - while (parent != RT_NULL && parent->flag & RTGUI_WIDGET_FLAG_TRANSPARENT) - { - parent = parent->parent; - } - - /* union widget rect */ - rtgui_region_union_rect(&(parent->clip), &(parent->clip), &(widget->extent)); - } - - return RT_FALSE; -} -RTM_EXPORT(rtgui_widget_onhide); - -void rtgui_widget_hide(struct rtgui_widget *widget) -{ - struct rtgui_event_hide ehide; - RT_ASSERT(widget != RT_NULL); - - if (RTGUI_WIDGET_IS_HIDE(widget)) - return; - - RTGUI_EVENT_HIDE_INIT(&ehide); - if (RTGUI_OBJECT(widget)->event_handler != RT_NULL) - { - RTGUI_OBJECT(widget)->event_handler( - RTGUI_OBJECT(widget), - &ehide); - } -} -RTM_EXPORT(rtgui_widget_hide); - -rtgui_color_t rtgui_widget_get_parent_foreground(rtgui_widget_t *widget) -{ - rtgui_widget_t *parent; - - /* get parent widget */ - parent = widget->parent; - while (parent->parent != RT_NULL && (RTGUI_WIDGET_FLAG(parent) & RTGUI_WIDGET_FLAG_TRANSPARENT)) - parent = parent->parent; - - /* get parent's color */ - if (parent != RT_NULL) - return RTGUI_WIDGET_FOREGROUND(parent); - - return RTGUI_WIDGET_FOREGROUND(widget); -} -RTM_EXPORT(rtgui_widget_get_parent_foreground); - -rtgui_color_t rtgui_widget_get_parent_background(rtgui_widget_t *widget) -{ - rtgui_widget_t *parent; - - /* get parent widget */ - parent = widget->parent; - while (parent->parent != RT_NULL && (RTGUI_WIDGET_FLAG(parent) & RTGUI_WIDGET_FLAG_TRANSPARENT)) - parent = parent->parent; - - /* get parent's color */ - if (parent != RT_NULL) - return RTGUI_WIDGET_BACKGROUND(parent); - - return RTGUI_WIDGET_BACKGROUND(widget); -} -RTM_EXPORT(rtgui_widget_get_parent_background); - -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 (RTGUI_WIDGET_IS_HIDE(widget)) - return; - - if (RTGUI_OBJECT(widget)->event_handler != RT_NULL) - { - RTGUI_OBJECT(widget)->event_handler( - RTGUI_OBJECT(widget), - &paint.parent); - } -} -RTM_EXPORT(rtgui_widget_update); - -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; -} -RTM_EXPORT(rtgui_widget_get_next_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; -} -RTM_EXPORT(rtgui_widget_get_prev_sibling); - -#ifdef RTGUI_WIDGET_DEBUG -#include <rtgui/widgets/label.h> -#include <rtgui/widgets/button.h> -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_WIN(widget) == RT_TRUE) - rt_kprintf(":%s ", RTGUI_WIN(widget)->title); - else 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 diff --git a/components/rtgui/widgets/window.c b/components/rtgui/widgets/window.c deleted file mode 100644 index 78971a4649..0000000000 --- a/components/rtgui/widgets/window.c +++ /dev/null @@ -1,728 +0,0 @@ -/* - * 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/rtgui_server.h> -#include <rtgui/rtgui_app.h> - -#include <rtgui/widgets/window.h> -#include <rtgui/widgets/button.h> - -static void _rtgui_win_constructor(rtgui_win_t *win) -{ - /* set toplevel to self */ - RTGUI_WIDGET(win)->toplevel = win; - - /* init win property */ - win->drawing = 0; - - /* hide win default */ - RTGUI_WIDGET_HIDE(win); - - RTGUI_WIDGET(win)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE; - win->parent_window = RT_NULL; - win->app = rtgui_app_self(); - /* init window attribute */ - 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; - - /* set window hide */ - RTGUI_WIDGET_HIDE(win); - - /* set window style */ - win->style = RTGUI_WIN_STYLE_DEFAULT; - - win->flag = RTGUI_WIN_FLAG_INIT; - - rtgui_object_set_event_handler(RTGUI_OBJECT(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 (win->flag & RTGUI_WIN_FLAG_CONNECTED) - { - /* destroy in server */ - RTGUI_EVENT_WIN_DESTROY_INIT(&edestroy); - edestroy.wid = win; - if (rtgui_server_post_event_sync(RTGUI_EVENT(&edestroy), - sizeof(struct rtgui_event_win_destroy)) != RT_EOK) - { - /* destroy in server failed */ - return; - } - } - - /* release field */ - if (win->title != RT_NULL) - rt_free(win->title); - /* release external clip info */ - win->drawing = 0; -} - -static rt_bool_t _rtgui_win_create_in_server(struct rtgui_win *win) -{ - if (!(win->flag & RTGUI_WIN_FLAG_CONNECTED)) - { - struct rtgui_event_win_create ecreate; - RTGUI_EVENT_WIN_CREATE_INIT(&ecreate); - - /* send win create event to server */ - ecreate.parent_window = win->parent_window; - ecreate.wid = win; - ecreate.parent.user = win->style; -#ifndef RTGUI_USING_SMALL_SIZE - ecreate.extent = RTGUI_WIDGET(win)->extent; - rt_strncpy((char *)ecreate.title, (char *)win->title, RTGUI_NAME_MAX); -#endif - - if (rtgui_server_post_event_sync(RTGUI_EVENT(&ecreate), - sizeof(struct rtgui_event_win_create) - ) != RT_EOK) - { - rt_kprintf("create win: %s failed\n", win->title); - return RT_FALSE; - } - - win->flag |= RTGUI_WIN_FLAG_CONNECTED; - } - - return RT_TRUE; -} - -DEFINE_CLASS_TYPE(win, "win", - RTGUI_CONTAINER_TYPE, - _rtgui_win_constructor, - _rtgui_win_destructor, - sizeof(struct rtgui_win)); - -rtgui_win_t *rtgui_win_create(struct rtgui_win *parent_window, - const char *title, - rtgui_rect_t *rect, - rt_uint16_t style) -{ - struct rtgui_win *win; - - /* allocate win memory */ - win = RTGUI_WIN(rtgui_widget_create(RTGUI_WIN_TYPE)); - if (win == RT_NULL) - return RT_NULL; - - /* set parent window */ - win->parent_window = parent_window; - - /* 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) - { - goto __on_err; - } - return win; - -__on_err: - rtgui_widget_destroy(RTGUI_WIDGET(win)); - return RT_NULL; -} -RTM_EXPORT(rtgui_win_create); - -rtgui_win_t *rtgui_mainwin_create(struct rtgui_win *parent_window, const char *title, rt_uint16_t style) -{ - struct rtgui_rect rect; - - /* get rect of main window */ - rtgui_get_mainwin_rect(&rect); - - return rtgui_win_create(parent_window, title, &rect, style); -} -RTM_EXPORT(rtgui_mainwin_create); - -static rt_bool_t _rtgui_win_deal_close(struct rtgui_win *win, - struct rtgui_event *event, - rt_bool_t force_close) -{ - if (win->on_close != RT_NULL) - { - if ((win->on_close(RTGUI_OBJECT(win), event) == RT_FALSE) && !force_close) - return RT_FALSE; - } - - rtgui_win_hide(win); - - win->flag |= RTGUI_WIN_FLAG_CLOSED; - - if (win->flag & RTGUI_WIN_FLAG_MODAL) - { - rtgui_win_end_modal(win, RTGUI_MODAL_CANCEL); - } - else if (win->style & RTGUI_WIN_STYLE_DESTROY_ON_CLOSE) - { - rtgui_win_destroy(win); - } - - return RT_TRUE; -} - -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)); - } -} -RTM_EXPORT(rtgui_win_destroy); - -/* send a close event to myself to get a consistent behavior */ -rt_bool_t rtgui_win_close(struct rtgui_win *win) -{ - struct rtgui_event_win_close eclose; - - RTGUI_EVENT_WIN_CLOSE_INIT(&eclose); - eclose.wid = win; - return _rtgui_win_deal_close(win, - (struct rtgui_event *)&eclose, - RT_FALSE); -} -RTM_EXPORT(rtgui_win_close); - -rt_base_t rtgui_win_show(struct rtgui_win *win, rt_bool_t is_modal) -{ - rt_base_t exit_code = -1; - struct rtgui_app *app; - struct rtgui_event_win_show eshow; - - app = win->app; - RTGUI_EVENT_WIN_SHOW_INIT(&eshow); - eshow.wid = win; - - if (win == RT_NULL) - return exit_code; - - /* if it does not register into server, create it in server */ - if (!(win->flag & RTGUI_WIN_FLAG_CONNECTED)) - { - if (_rtgui_win_create_in_server(win) == RT_FALSE) - return exit_code; - } - - /* set window unhidden before notify the server */ - rtgui_widget_show(RTGUI_WIDGET(win)); - - if (rtgui_server_post_event_sync(RTGUI_EVENT(&eshow), - sizeof(struct rtgui_event_win_show)) != RT_EOK) - { - /* It could not be shown if a parent window is hidden. */ - rtgui_widget_hide(RTGUI_WIDGET(win)); - return exit_code; - } - - if (win->focused_widget == RT_NULL) - rtgui_widget_focus(RTGUI_WIDGET(win)); - - /* set main window */ - if (app->main_object == RT_NULL) - rtgui_app_set_main_win(app, win); - - if (is_modal == RT_TRUE) - { - struct rtgui_app *app; - struct rtgui_event_win_modal_enter emodal; - - RTGUI_EVENT_WIN_MODAL_ENTER_INIT(&emodal); - emodal.wid = win; - - app = win->app; - 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); - - exit_code = rtgui_app_run(app); - - app->modal_object = RT_NULL; - win->flag &= ~RTGUI_WIN_FLAG_MODAL; - - if (win->style & RTGUI_WIN_STYLE_DESTROY_ON_CLOSE) - { - rtgui_win_destroy(win); - } - } - - return exit_code; -} -RTM_EXPORT(rtgui_win_show); - -void rtgui_win_end_modal(struct rtgui_win *win, rtgui_modal_code_t modal_code) -{ - if (win == RT_NULL || !(win->flag & RTGUI_WIN_FLAG_MODAL)) - return; - - rtgui_app_exit(win->app, modal_code); - - /* remove modal mode */ - win->flag &= ~RTGUI_WIN_FLAG_MODAL; -} -RTM_EXPORT(rtgui_win_end_modal); - -void rtgui_win_hide(struct rtgui_win *win) -{ - RT_ASSERT(win != RT_NULL); - - if (!RTGUI_WIDGET_IS_HIDE(win) && - win->flag & RTGUI_WIN_FLAG_CONNECTED) - { - /* send hidden message to server */ - struct rtgui_event_win_hide ehide; - RTGUI_EVENT_WIN_HIDE_INIT(&ehide); - ehide.wid = win; - - if (rtgui_server_post_event_sync(RTGUI_EVENT(&ehide), - sizeof(struct rtgui_event_win_hide)) != RT_EOK) - { - rt_kprintf("hide win: %s failed\n", win->title); - return; - } - - rtgui_widget_hide(RTGUI_WIDGET(win)); - win->flag &= ~RTGUI_WIN_FLAG_ACTIVATE; - } -} -RTM_EXPORT(rtgui_win_hide); - -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)); -} -RTM_EXPORT(rtgui_win_activate); - -rt_bool_t rtgui_win_is_activated(struct rtgui_win *win) -{ - RT_ASSERT(win != RT_NULL); - - if (win->flag & RTGUI_WIN_FLAG_ACTIVATE) return RT_TRUE; - - return RT_FALSE; -} -RTM_EXPORT(rtgui_win_is_activated); - -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; - - /* 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); - - if (win->flag & RTGUI_WIN_FLAG_CONNECTED) - { - /* set win hide firstly */ - rtgui_widget_hide(RTGUI_WIDGET(win)); - - emove.wid = win; - emove.x = x; - emove.y = y; - if (rtgui_server_post_event_sync(RTGUI_EVENT(&emove), - sizeof(struct rtgui_event_win_move)) != RT_EOK) - { - return; - } - } - - /* set window visible */ - rtgui_widget_show(RTGUI_WIDGET(win)); - return; -} -RTM_EXPORT(rtgui_win_move); - -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; -} - -void rtgui_win_update_clip(struct rtgui_win *win) -{ - struct rtgui_container *cnt; - struct rtgui_list_node *node; - - if (win == RT_NULL) - return; - - /* update the clip info of each child */ - cnt = RTGUI_CONTAINER(win); - rtgui_list_foreach(node, &(cnt->children)) - { - rtgui_widget_t *child = rtgui_list_entry(node, rtgui_widget_t, sibling); - - rtgui_widget_update_clip(child); - } -} - -rt_bool_t rtgui_win_event_handler(struct rtgui_object *object, struct rtgui_event *event) -{ - struct rtgui_win *win; - - RT_ASSERT(object != RT_NULL); - RT_ASSERT(event != RT_NULL); - - win = RTGUI_WIN(object); - - switch (event->type) - { - case RTGUI_EVENT_WIN_SHOW: - rtgui_win_show(win, RT_FALSE); - break; - - case RTGUI_EVENT_WIN_HIDE: - rtgui_win_hide(win); - break; - - case RTGUI_EVENT_WIN_CLOSE: - _rtgui_win_deal_close(win, event, RT_FALSE); - /* don't broadcast WIN_CLOSE event to others */ - 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(win)) - { - /* activate a hide window */ - return RT_TRUE; - } - - win->flag |= RTGUI_WIN_FLAG_ACTIVATE; - if (win->on_activate != RT_NULL) - { - win->on_activate(RTGUI_OBJECT(object), event); - } - break; - - case RTGUI_EVENT_WIN_DEACTIVATE: - if (win->flag & RTGUI_WIN_FLAG_MODAL) - { - /* 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. - */ - } - else - { - win->flag &= ~RTGUI_WIN_FLAG_ACTIVATE; - if (win->on_deactivate != RT_NULL) - { - win->on_deactivate(RTGUI_OBJECT(object), event); - } - } - break; - - case RTGUI_EVENT_CLIP_INFO: - /* update win clip */ - rtgui_win_update_clip(win); - break; - - case RTGUI_EVENT_PAINT: -#ifndef RTGUI_USING_SMALL_SIZE - if (RTGUI_WIDGET(object)->on_draw != RT_NULL) - RTGUI_WIDGET(object)->on_draw(object, event); - else -#endif - rtgui_win_ondraw(win); - break; - - case RTGUI_EVENT_MOUSE_BUTTON: - { - rt_bool_t res = rtgui_container_dispatch_mouse_event(RTGUI_CONTAINER(win), - (struct rtgui_event_mouse *)event); -#ifndef RTGUI_USING_SMALL_SIZE - if (RTGUI_WIDGET(object)->on_mouseclick != RT_NULL) - { - RTGUI_WIDGET(object)->on_mouseclick(object, event); - } -#endif - /* check whether has widget which handled mouse event before. - * - * Note #1: that the widget should have already received mouse down - * event and we should only feed the mouse up event to it here. - * - * Note #2: the widget is responsible to clean up - * last_mevent_widget on mouse up event(but not overwrite other - * widgets). If not, it will receive two mouse up events. - */ - if (((struct rtgui_event_mouse *)event)->button & RTGUI_MOUSE_BUTTON_UP - && win->last_mevent_widget != RT_NULL) - { - RTGUI_OBJECT(win->last_mevent_widget)->event_handler( - RTGUI_OBJECT(win->last_mevent_widget), - event); - - /* clean last mouse event handled widget */ - win->last_mevent_widget = RT_NULL; - } - - return res; - } - - case RTGUI_EVENT_MOUSE_MOTION: -#if 0 - if (rtgui_widget_dispatch_mouse_event(widget, - (struct rtgui_event_mouse *)event) == RT_FALSE) - { -#ifndef RTGUI_USING_SMALL_SIZE - /* handle event in current widget */ - if (widget->on_mousemotion != RT_NULL) - { - return widget->on_mousemotion(widget, event); - } -#endif - } - else return RT_TRUE; -#endif - break; - - case RTGUI_EVENT_KBD: - /* we should dispatch key event firstly */ - if (!(win->flag & RTGUI_WIN_FLAG_HANDLE_KEY)) - { - struct rtgui_widget *widget; - 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 */ - 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); - } - - win->flag &= ~RTGUI_WIN_FLAG_HANDLE_KEY; - return res; - } - else - { - /* 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); - } - break; - - case RTGUI_EVENT_COMMAND: - if (rtgui_container_dispatch_event(RTGUI_CONTAINER(object), event) != RT_TRUE) - { -#ifndef RTGUI_USING_SMALL_SIZE - if (RTGUI_WIDGET(object)->on_command != RT_NULL) - { - RTGUI_WIDGET(object)->on_command(object, event); - } -#endif - } - else - return RT_TRUE; - break; - - default: - return rtgui_container_event_handler(object, event); - } - - return RT_FALSE; -} -RTM_EXPORT(rtgui_win_event_handler); - -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 (win->flag & RTGUI_WIN_FLAG_CONNECTED) - { - /* set window resize event to server */ - RTGUI_EVENT_WIN_RESIZE_INIT(&event); - event.wid = win; - event.rect = *rect; - - rtgui_server_post_event(&(event.parent), sizeof(struct rtgui_event_win_resize)); - } -} -RTM_EXPORT(rtgui_win_set_rect); - -void rtgui_win_set_onactivate(rtgui_win_t *win, rtgui_event_handler_ptr handler) -{ - if (win != RT_NULL) - { - win->on_activate = handler; - } -} -RTM_EXPORT(rtgui_win_set_onactivate); - -void rtgui_win_set_ondeactivate(rtgui_win_t *win, rtgui_event_handler_ptr handler) -{ - if (win != RT_NULL) - { - win->on_deactivate = handler; - } -} -RTM_EXPORT(rtgui_win_set_ondeactivate); - -void rtgui_win_set_onclose(rtgui_win_t *win, rtgui_event_handler_ptr handler) -{ - if (win != RT_NULL) - { - win->on_close = handler; - } -} -RTM_EXPORT(rtgui_win_set_onclose); - -void rtgui_win_set_onkey(rtgui_win_t *win, rtgui_event_handler_ptr handler) -{ - if (win != RT_NULL) - { - win->on_key = handler; - } -} -RTM_EXPORT(rtgui_win_set_onkey); - -void rtgui_win_set_title(rtgui_win_t *win, const char *title) -{ - /* send title to server */ - if (win->flag & RTGUI_WIN_FLAG_CONNECTED) - { - } - - /* 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); - } -} -RTM_EXPORT(rtgui_win_set_title); - -char *rtgui_win_get_title(rtgui_win_t *win) -{ - RT_ASSERT(win != RT_NULL); - - return win->title; -} -RTM_EXPORT(rtgui_win_get_title); - |
