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); } } }
/* 创建用于演示radiobox控件的视图 */ rtgui_container_t* demo_view_radiobox(void) { rtgui_rect_t rect; rtgui_container_t* container; rtgui_radiobox_t* radiobox; /* 先创建一个演示用的视图 */ container = demo_view("RadioBox View"); /* 获得视图的位置信息 */ demo_view_get_rect(container, &rect); rect.x1 += 5; rect.x2 -= 5; rect.y1 += 5; rect.y2 = rect.y1 + 5 * 25; /* 创建一个垂直方向显示的radiobox控件,文本项是radio_item_v数组,共5个项 */ radiobox = rtgui_radiobox_create("Radio Box", RTGUI_VERTICAL, radio_item_v, 5); /* 设置当前选择的数组是第0项 */ rtgui_radiobox_set_selection(radiobox, 0); /* 添加radiobox控件到视图中 */ rtgui_container_add_child(container, RTGUI_WIDGET(radiobox)); /* 设置radiobox控件的位置信息 */ rtgui_widget_set_rect(RTGUI_WIDGET(radiobox), &rect); /* 获得视图的位置信息 */ demo_view_get_rect(container, &rect); rect.x1 += 5; rect.x2 -= 5; rect.y1 += 5 + 5 * 25; rect.y2 = rect.y1 + 60; /* 创建一个水平方向显示的radiobox控件,文本项是radio_item_h数组,共3个项 */ radiobox = rtgui_radiobox_create("Radio Box", RTGUI_HORIZONTAL, radio_item_h, 3); /* 设置当前选择的数组是第0项 */ rtgui_radiobox_set_selection(radiobox, 0); /* 添加radiobox控件到视图中 */ rtgui_container_add_child(container, RTGUI_WIDGET(radiobox)); /* 设置radiobox控件的位置信息 */ rtgui_widget_set_rect(RTGUI_WIDGET(radiobox), &rect); return container; }
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; }