radiobox.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include <rtgui/dc.h>
  2. #include <rtgui/rtgui_theme.h>
  3. #include <rtgui/widgets/radiobox.h>
  4. #define RTGUI_RADIOBOX_DEFAULT_WIDTH 100
  5. #define RTGUI_RADIOBOX_DEFAULT_HEIGHT 20
  6. static void _rtgui_radiobox_constructor(rtgui_radiobox_t *radiobox)
  7. {
  8. rtgui_rect_t rect = {0, 0, RTGUI_RADIOBOX_DEFAULT_WIDTH, RTGUI_RADIOBOX_DEFAULT_HEIGHT};
  9. /* init widget and set event handler */
  10. RTGUI_WIDGET(radiobox)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  11. RTGUI_WIDGET_TEXTALIGN(RTGUI_WIDGET(radiobox)) = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_CENTER_VERTICAL;
  12. rtgui_widget_set_rect(RTGUI_WIDGET(radiobox), &rect);
  13. rtgui_widget_set_event_handler(RTGUI_WIDGET(radiobox), rtgui_radiobox_event_handler);
  14. /* set proper of control */
  15. radiobox->items = RT_NULL;
  16. radiobox->item_count = 0;
  17. radiobox->item_selection = -1;
  18. radiobox->orient = RTGUI_HORIZONTAL;
  19. }
  20. rtgui_type_t *rtgui_radiobox_type_get(void)
  21. {
  22. static rtgui_type_t *radiobox_type = RT_NULL;
  23. if (!radiobox_type)
  24. {
  25. radiobox_type = rtgui_type_create("radiobox", RTGUI_WIDGET_TYPE,
  26. sizeof(rtgui_radiobox_t), RTGUI_CONSTRUCTOR(_rtgui_radiobox_constructor), RT_NULL);
  27. }
  28. return radiobox_type;
  29. }
  30. static void rtgui_radiobox_onmouse(struct rtgui_radiobox* radiobox, struct rtgui_event_mouse* event)
  31. {
  32. RT_ASSERT(radiobox != RT_NULL);
  33. RT_ASSERT(event != RT_NULL);
  34. /* widget is hide, return */
  35. if (RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(radiobox)) ||
  36. !RTGUI_WIDGET_IS_ENABLE(RTGUI_WIDGET(radiobox))) return;
  37. if (event->button & RTGUI_MOUSE_BUTTON_DOWN &&
  38. event->button & RTGUI_MOUSE_BUTTON_LEFT)
  39. {
  40. int bord_size;
  41. struct rtgui_rect rect;
  42. /* focus widgets */
  43. rtgui_widget_focus(RTGUI_WIDGET(radiobox));
  44. /* get widget physical rect */
  45. rtgui_widget_get_rect(RTGUI_WIDGET(radiobox), &rect);
  46. rtgui_widget_rect_to_device(RTGUI_WIDGET(radiobox), &rect);
  47. /* get board size */
  48. if (radiobox->orient == RTGUI_VERTICAL)
  49. bord_size = radiobox->item_size;
  50. else
  51. {
  52. struct rtgui_rect bord_rect;
  53. rtgui_font_get_metrics(RTGUI_WIDGET_FONT(RTGUI_WIDGET(radiobox)), "H", &bord_rect);
  54. bord_size = rtgui_rect_height(bord_rect);
  55. }
  56. rtgui_rect_inflate(&rect, - bord_size);
  57. if (rtgui_rect_contains_point(&rect, event->x, event->y) != RT_EOK) return;
  58. if (radiobox->orient == RTGUI_VERTICAL)
  59. {
  60. int delta_y = event->y - rect.y1;
  61. rtgui_radiobox_set_selection(radiobox, delta_y / radiobox->item_size);
  62. }
  63. else
  64. {
  65. int delta_x = event->x - rect.x1;
  66. rtgui_radiobox_set_selection(radiobox, delta_x / radiobox->item_size);
  67. }
  68. }
  69. }
  70. rt_bool_t rtgui_radiobox_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  71. {
  72. struct rtgui_radiobox* radiobox = (struct rtgui_radiobox*)widget;
  73. switch (event->type)
  74. {
  75. case RTGUI_EVENT_PAINT:
  76. #ifndef RTGUI_USING_SMALL_SIZE
  77. if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
  78. else
  79. #endif
  80. {
  81. rtgui_theme_draw_radiobox(radiobox);
  82. }
  83. break;
  84. case RTGUI_EVENT_KBD:
  85. if (RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(radiobox))) return RT_FALSE;
  86. #ifndef RTGUI_USING_SMALL_SIZE
  87. if (widget->on_key != RT_NULL) widget->on_key(widget, event);
  88. else
  89. #endif
  90. {
  91. struct rtgui_event_kbd *e = (struct rtgui_event_kbd*)event;
  92. /* set focused */
  93. rtgui_widget_focus(RTGUI_WIDGET(radiobox));
  94. if (!(RTGUI_KBD_IS_UP(e))) return RT_FALSE;
  95. if (radiobox->orient == RTGUI_VERTICAL)
  96. {
  97. if (e->key == RTGUIK_UP)
  98. {
  99. if (radiobox->item_selection > 0)
  100. rtgui_radiobox_set_selection(radiobox, radiobox->item_selection - 1);
  101. }
  102. else if (e->key == RTGUIK_DOWN)
  103. {
  104. if (radiobox->item_selection < radiobox->item_count - 1)
  105. rtgui_radiobox_set_selection(radiobox, radiobox->item_selection + 1);
  106. }
  107. }
  108. else
  109. {
  110. if (e->key == RTGUIK_LEFT)
  111. {
  112. if (radiobox->item_selection > 0)
  113. rtgui_radiobox_set_selection(radiobox, radiobox->item_selection - 1);
  114. }
  115. else if (e->key == RTGUIK_RIGHT)
  116. {
  117. if (radiobox->item_selection < radiobox->item_count - 1)
  118. rtgui_radiobox_set_selection(radiobox, radiobox->item_selection + 1);
  119. }
  120. }
  121. }
  122. break;
  123. case RTGUI_EVENT_MOUSE_BUTTON:
  124. #ifndef RTGUI_USING_SMALL_SIZE
  125. if (widget->on_mouseclick != RT_NULL) widget->on_mouseclick(widget, event);
  126. else
  127. #endif
  128. {
  129. rtgui_radiobox_onmouse(radiobox, (struct rtgui_event_mouse*)event);
  130. }
  131. break;
  132. }
  133. return RT_FALSE;
  134. }
  135. struct rtgui_radiobox* rtgui_radiobox_create(const char* label, int orient, char** radio_items, int number)
  136. {
  137. struct rtgui_radiobox* radiobox;
  138. radiobox = (struct rtgui_radiobox*) rtgui_widget_create (RTGUI_RADIOBOX_TYPE);
  139. if (radiobox != RT_NULL)
  140. {
  141. rt_uint8_t board_size;
  142. struct rtgui_rect rect;
  143. radiobox->items = radio_items;
  144. radiobox->item_count = number;
  145. radiobox->item_selection = -1;
  146. radiobox->text = rt_strdup(label);
  147. /* set proper of control */
  148. rtgui_radiobox_set_orientation(radiobox, orient);
  149. rtgui_font_get_metrics(RTGUI_WIDGET_FONT(RTGUI_WIDGET(radiobox)), "H", &rect);
  150. board_size = rtgui_rect_height(rect);
  151. if (orient == RTGUI_VERTICAL)
  152. {
  153. radiobox->item_size = board_size;
  154. }
  155. else
  156. {
  157. int index;
  158. struct rtgui_font* font;
  159. struct rtgui_rect rect;
  160. /* set init item size */
  161. radiobox->item_size = 0;
  162. font = RTGUI_WIDGET_FONT(RTGUI_WIDGET(radiobox));
  163. for (index = 0; index < number; index ++)
  164. {
  165. rtgui_font_get_metrics(font, radio_items[index], &rect);
  166. if ( (board_size + 3 + rtgui_rect_width(rect)) > radiobox->item_size)
  167. radiobox->item_size = board_size + 3 + rtgui_rect_width(rect);
  168. }
  169. }
  170. if (radiobox->item_size < RADIO_BOX_H + 2)
  171. radiobox->item_size = RADIO_BOX_H + 2;
  172. }
  173. return radiobox;
  174. }
  175. void rtgui_radiobox_set_orientation(struct rtgui_radiobox* radiobox, int orientation)
  176. {
  177. RT_ASSERT(radiobox != RT_NULL);
  178. /* set orientation */
  179. radiobox->orient = orientation;
  180. #ifndef RTGUI_USING_SMALL_SIZE
  181. if (radiobox->orient == RTGUI_HORIZONTAL)
  182. {
  183. /* HORIZONTAL */
  184. rtgui_widget_set_miniheight(RTGUI_WIDGET(radiobox), RTGUI_RADIOBOX_DEFAULT_HEIGHT);
  185. rtgui_widget_set_miniwidth(RTGUI_WIDGET(radiobox), RTGUI_RADIOBOX_DEFAULT_WIDTH);
  186. }
  187. else
  188. {
  189. /* VERTICAL */
  190. rtgui_widget_set_miniwidth(RTGUI_WIDGET(radiobox), RTGUI_RADIOBOX_DEFAULT_HEIGHT);
  191. rtgui_widget_set_miniheight(RTGUI_WIDGET(radiobox), RTGUI_RADIOBOX_DEFAULT_WIDTH);
  192. }
  193. #endif
  194. }
  195. void rtgui_radiobox_set_selection(struct rtgui_radiobox* radiobox, int selection)
  196. {
  197. rt_uint16_t old_item;
  198. if (selection == radiobox->item_selection) return;
  199. old_item = radiobox->item_selection;
  200. if (selection >= 0 && selection < radiobox->item_count)
  201. {
  202. radiobox->item_selection = selection;
  203. }
  204. /* update radiobox widget */
  205. rtgui_theme_draw_radiobutton(radiobox, old_item);
  206. rtgui_theme_draw_radiobutton(radiobox, radiobox->item_selection);
  207. }
  208. int rtgui_radiobox_get_selection(struct rtgui_radiobox* radiobox)
  209. {
  210. return radiobox->item_selection;
  211. }