From 5b91286805b21d39b9d0321dd515dc2bcd7ff8b3 Mon Sep 17 00:00:00 2001 From: "bernard.xiong@gmail.com" Date: Wed, 26 May 2010 23:58:47 +0000 Subject: [PATCH] add radiobutton drawing. git-svn-id: https://rt-thread.googlecode.com/svn/trunk@737 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- components/rtgui/common/rtgui_theme.c | 88 +++++++++++++++++++- components/rtgui/include/rtgui/rtgui_theme.h | 1 + components/rtgui/widgets/radiobox.c | 8 +- 3 files changed, 94 insertions(+), 3 deletions(-) diff --git a/components/rtgui/common/rtgui_theme.c b/components/rtgui/common/rtgui_theme.c index 42760cf060..646ee9b87a 100644 --- a/components/rtgui/common/rtgui_theme.c +++ b/components/rtgui/common/rtgui_theme.c @@ -484,6 +484,90 @@ static const rt_uint8_t radio_checked_byte[] = 0x40, 0x20, 0x30, 0xc0, 0x0f, 0x00, }; +void rtgui_theme_draw_radiobutton(struct rtgui_radiobox* radiobox, rt_uint16_t item) +{ + struct rtgui_dc* dc; + struct rtgui_rect rect, item_rect; + rt_size_t item_size, bord_size; + + /* begin drawing */ + dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(radiobox)); + if (dc == RT_NULL) return; + /* get widget rect */ + rtgui_widget_get_rect(RTGUI_WIDGET(radiobox), &rect); + + item_size = radiobox->item_size; + /* get board size */ + if (radiobox->orient == RTGUI_VERTICAL) + bord_size = item_size; + else + { + rtgui_font_get_metrics(RTGUI_DC_FONT(dc), "H", &item_rect); + bord_size = rtgui_rect_height(item_rect); + } + + item_rect = rect; + rtgui_rect_inflate(&item_rect, - bord_size); + if (radiobox->orient == RTGUI_VERTICAL) + { + /* set the first text rect */ + item_rect.y1 += item * item_size; + item_rect.y2 = item_rect.y1 + item_size; + + /* draw radio */ + if (radiobox->item_selection == item) + { + if (RTGUI_WIDGET_IS_FOCUSED(RTGUI_WIDGET(radiobox))) + rtgui_dc_draw_focus_rect(dc, &item_rect); + + rtgui_dc_draw_word(dc, item_rect.x1, item_rect.y1 + (item_size - RADIO_BOX_H) / 2, + RADIO_BOX_H, radio_checked_byte); + } + else + { + item_rect.x2 += 1; item_rect.y2 += 1; + rtgui_dc_fill_rect(dc, &item_rect); + item_rect.x2 -= 1; item_rect.y2 -= 1; + rtgui_dc_draw_word(dc, item_rect.x1, item_rect.y1 + (item_size - RADIO_BOX_H) / 2, + RADIO_BOX_H, radio_unchecked_byte); + } + + /* draw text */ + item_rect.x1 += item_size + 3; + rtgui_dc_draw_text(dc, radiobox->items[item], &item_rect); + } + else + { + item_rect.x1 += item * item_size; + + /* set the first text rect */ + item_rect.x2 = item_rect.x1 + item_size; + item_rect.y2 = item_rect.y1 + bord_size; + + /* draw radio */ + if (radiobox->item_selection == item) + { + if (RTGUI_WIDGET_IS_FOCUSED(RTGUI_WIDGET(radiobox))) + rtgui_dc_draw_focus_rect(dc, &item_rect); + rtgui_dc_draw_word(dc, item_rect.x1, item_rect.y1, RADIO_BOX_H, radio_checked_byte); + } + else + { + item_rect.x2 += 1; item_rect.y2 += 1; + rtgui_dc_fill_rect(dc, &item_rect); + item_rect.x2 -= 1; item_rect.y2 -= 1; + rtgui_dc_draw_word(dc, item_rect.x1, item_rect.y1, RADIO_BOX_H, radio_unchecked_byte); + } + + /* draw text */ + item_rect.x1 += bord_size + 3; + rtgui_dc_draw_text(dc, radiobox->items[item], &item_rect); + } + + /* end drawing */ + rtgui_dc_end_drawing(dc); +} + void rtgui_theme_draw_radiobox(struct rtgui_radiobox* radiobox) { struct rtgui_dc* dc; @@ -590,8 +674,8 @@ void rtgui_theme_draw_radiobox(struct rtgui_radiobox* radiobox) /* draw radio */ if (radiobox->item_selection == index) { - rtgui_dc_draw_focus_rect(dc, &item_rect); - + if (RTGUI_WIDGET_IS_FOCUSED(RTGUI_WIDGET(radiobox))) + rtgui_dc_draw_focus_rect(dc, &item_rect); rtgui_dc_draw_word(dc, item_rect.x1, item_rect.y1, RADIO_BOX_H, radio_checked_byte); } else diff --git a/components/rtgui/include/rtgui/rtgui_theme.h b/components/rtgui/include/rtgui/rtgui_theme.h index 20912149bd..507d0f3059 100644 --- a/components/rtgui/include/rtgui/rtgui_theme.h +++ b/components/rtgui/include/rtgui/rtgui_theme.h @@ -45,6 +45,7 @@ void rtgui_theme_draw_label(rtgui_label_t* label); void rtgui_theme_draw_textbox(rtgui_textbox_t* box); void rtgui_theme_draw_iconbox(rtgui_iconbox_t* iconbox); void rtgui_theme_draw_checkbox(rtgui_checkbox_t* checkbox); +void rtgui_theme_draw_radiobutton(struct rtgui_radiobox* radiobox, rt_uint16_t item); void rtgui_theme_draw_radiobox(struct rtgui_radiobox* radiobox); void rtgui_theme_draw_slider(struct rtgui_slider* slider); void rtgui_theme_draw_progressbar(struct rtgui_progressbar* bar); diff --git a/components/rtgui/widgets/radiobox.c b/components/rtgui/widgets/radiobox.c index babf9f9ce1..5fbb79f0a6 100644 --- a/components/rtgui/widgets/radiobox.c +++ b/components/rtgui/widgets/radiobox.c @@ -224,13 +224,19 @@ void rtgui_radiobox_set_orientation(struct rtgui_radiobox* radiobox, int orienta 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_radiobox(radiobox); + rtgui_theme_draw_radiobutton(radiobox, old_item); + rtgui_theme_draw_radiobutton(radiobox, radiobox->item_selection); } int rtgui_radiobox_get_selection(struct rtgui_radiobox* radiobox)