radiobox.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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_set_rect(RTGUI_WIDGET(radiobox), &rect);
  12. rtgui_widget_set_event_handler(RTGUI_WIDGET(radiobox), rtgui_radiobox_event_handler);
  13. /* set proper of control */
  14. radiobox->items = RT_NULL;
  15. radiobox->item_count = 0;
  16. radiobox->item_selection = -1;
  17. radiobox->orient = RTGUI_HORIZONTAL;
  18. }
  19. rtgui_type_t *rtgui_radiobox_type_get(void)
  20. {
  21. static rtgui_type_t *radiobox_type = RT_NULL;
  22. if (!radiobox_type)
  23. {
  24. radiobox_type = rtgui_type_create("radiobox", RTGUI_WIDGET_TYPE,
  25. sizeof(rtgui_radiobox_t), RTGUI_CONSTRUCTOR(_rtgui_radiobox_constructor), RT_NULL);
  26. }
  27. return radiobox_type;
  28. }
  29. static void rtgui_radiobox_onmouse(struct rtgui_radiobox* radiobox, struct rtgui_event_mouse* event)
  30. {
  31. RT_ASSERT(radiobox != RT_NULL);
  32. RT_ASSERT(event != RT_NULL);
  33. if (event->button & RTGUI_MOUSE_BUTTON_DOWN &&
  34. event->button & RTGUI_MOUSE_BUTTON_LEFT)
  35. {
  36. int bord_size;
  37. struct rtgui_rect rect;
  38. /* focus widgets */
  39. rtgui_widget_focus(RTGUI_WIDGET(radiobox));
  40. /* get widget rect */
  41. rtgui_widget_get_rect(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_widget* widget, struct rtgui_event* event)
  66. {
  67. struct rtgui_radiobox* radiobox = (struct rtgui_radiobox*)widget;
  68. switch (event->type)
  69. {
  70. case RTGUI_EVENT_PAINT:
  71. #ifndef RTGUI_USING_SMALL_SIZE
  72. if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
  73. else
  74. #endif
  75. {
  76. rtgui_theme_draw_radiobox(radiobox);
  77. }
  78. break;
  79. case RTGUI_EVENT_KBD:
  80. #ifndef RTGUI_USING_SMALL_SIZE
  81. if (widget->on_key != RT_NULL) widget->on_key(widget, event);
  82. else
  83. #endif
  84. {
  85. struct rtgui_event_kbd *e = (struct rtgui_event_kbd*)event;
  86. /* set focused */
  87. rtgui_widget_focus(RTGUI_WIDGET(radiobox));
  88. if (!(RTGUI_KBD_IS_UP(e))) return RT_FALSE;
  89. if (radiobox->orient == RTGUI_VERTICAL)
  90. {
  91. if (e->key == RTGUIK_UP)
  92. {
  93. if (radiobox->item_selection > 0)
  94. rtgui_radiobox_set_selection(radiobox, radiobox->item_selection - 1);
  95. }
  96. else if (e->key == RTGUIK_DOWN)
  97. {
  98. if (radiobox->item_selection < radiobox->item_count - 1)
  99. rtgui_radiobox_set_selection(radiobox, radiobox->item_selection + 1);
  100. }
  101. }
  102. else
  103. {
  104. if (e->key == RTGUIK_LEFT)
  105. {
  106. if (radiobox->item_selection > 0)
  107. rtgui_radiobox_set_selection(radiobox, radiobox->item_selection - 1);
  108. }
  109. else if (e->key == RTGUIK_RIGHT)
  110. {
  111. if (radiobox->item_selection < radiobox->item_count - 1)
  112. rtgui_radiobox_set_selection(radiobox, radiobox->item_selection + 1);
  113. }
  114. }
  115. }
  116. break;
  117. case RTGUI_EVENT_MOUSE_BUTTON:
  118. #ifndef RTGUI_USING_SMALL_SIZE
  119. if (widget->on_mouseclick != RT_NULL) widget->on_mouseclick(widget, event);
  120. else
  121. #endif
  122. {
  123. rtgui_radiobox_onmouse(radiobox, (struct rtgui_event_mouse*)event);
  124. }
  125. break;
  126. }
  127. return RT_FALSE;
  128. }
  129. struct rtgui_radiobox* rtgui_radiobox_create(const char* label, int orient, char** radio_items, int number)
  130. {
  131. struct rtgui_radiobox* radiobox;
  132. radiobox = (struct rtgui_radiobox*) rtgui_widget_create (RTGUI_RADIOBOX_TYPE);
  133. if (radiobox != RT_NULL)
  134. {
  135. rt_uint8_t board_size;
  136. struct rtgui_rect rect;
  137. radiobox->items = radio_items;
  138. radiobox->item_count = number;
  139. radiobox->item_selection = -1;
  140. radiobox->text = rt_strdup(label);
  141. /* set proper of control */
  142. rtgui_radiobox_set_orientation(radiobox, orient);
  143. rtgui_font_get_metrics(RTGUI_WIDGET_FONT(RTGUI_WIDGET(radiobox)), "H", &rect);
  144. board_size = rtgui_rect_height(rect);
  145. if (orient == RTGUI_VERTICAL)
  146. {
  147. radiobox->item_size = board_size;
  148. }
  149. else
  150. {
  151. int index;
  152. struct rtgui_font* font;
  153. struct rtgui_rect rect;
  154. /* set init item size */
  155. radiobox->item_size = 0;
  156. font = RTGUI_WIDGET_FONT(RTGUI_WIDGET(radiobox));
  157. for (index = 0; index < number; index ++)
  158. {
  159. rtgui_font_get_metrics(font, radio_items[index], &rect);
  160. if (rtgui_rect_width(rect) > radiobox->item_size)
  161. radiobox->item_size = board_size + 3 + rtgui_rect_width(rect);
  162. }
  163. }
  164. }
  165. return radiobox;
  166. }
  167. void rtgui_radiobox_set_orientation(struct rtgui_radiobox* radiobox, int orientation)
  168. {
  169. RT_ASSERT(radiobox != RT_NULL);
  170. /* set orientation */
  171. radiobox->orient = orientation;
  172. #ifndef RTGUI_USING_SMALL_SIZE
  173. if (radiobox->orient == RTGUI_HORIZONTAL)
  174. {
  175. /* HORIZONTAL */
  176. rtgui_widget_set_miniheight(RTGUI_WIDGET(radiobox), RTGUI_RADIOBOX_DEFAULT_HEIGHT);
  177. rtgui_widget_set_miniwidth(RTGUI_WIDGET(radiobox), RTGUI_RADIOBOX_DEFAULT_WIDTH);
  178. }
  179. else
  180. {
  181. /* VERTICAL */
  182. rtgui_widget_set_miniwidth(RTGUI_WIDGET(radiobox), RTGUI_RADIOBOX_DEFAULT_HEIGHT);
  183. rtgui_widget_set_miniheight(RTGUI_WIDGET(radiobox), RTGUI_RADIOBOX_DEFAULT_WIDTH);
  184. }
  185. #endif
  186. }
  187. void rtgui_radiobox_set_selection(struct rtgui_radiobox* radiobox, int selection)
  188. {
  189. if (selection >= 0 && selection < radiobox->item_count)
  190. {
  191. radiobox->item_selection = selection;
  192. }
  193. /* update radiobox widget */
  194. rtgui_theme_draw_radiobox(radiobox);
  195. }
  196. int rtgui_radiobox_get_selection(struct rtgui_radiobox* radiobox)
  197. {
  198. return radiobox->item_selection;
  199. }