radiobox.c 7.1 KB

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