combobox.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #include <rtgui/dc.h>
  2. #include <rtgui/rtgui_theme.h>
  3. #include <rtgui/widgets/combobox.h>
  4. static rt_bool_t rtgui_combobox_pulldown_hide(struct rtgui_object *object, struct rtgui_event *event);
  5. const static rt_uint8_t down_arrow[] = {0xff, 0x7e, 0x3c, 0x18};
  6. static void _rtgui_combobox_constructor(rtgui_combobox_t *box)
  7. {
  8. rtgui_rect_t rect = {0, 0, RTGUI_COMBOBOX_WIDTH, RTGUI_COMBOBOX_HEIGHT};
  9. /* init widget and set event handler */
  10. rtgui_object_set_event_handler(RTGUI_OBJECT(box), rtgui_combobox_event_handler);
  11. rtgui_widget_set_rect(RTGUI_WIDGET(box), &rect);
  12. RTGUI_WIDGET_TEXTALIGN(box) = RTGUI_ALIGN_CENTER_VERTICAL;
  13. box->pd_pressed = RT_FALSE;
  14. box->current_item = 0;
  15. box->on_selected = RT_NULL;
  16. box->pd_win = RT_NULL;
  17. }
  18. static void _rtgui_combobox_destructor(rtgui_combobox_t *box)
  19. {
  20. /* destroy pull down window */
  21. rtgui_win_destroy(box->pd_win);
  22. /* reset box field */
  23. box->pd_win = RT_NULL;
  24. }
  25. rt_bool_t rtgui_combobox_pdwin_onitem(struct rtgui_object *object, struct rtgui_event *event)
  26. {
  27. struct rtgui_widget *widget;
  28. rtgui_win_t *pd_win;
  29. rtgui_combobox_t *combo;
  30. rtgui_listbox_t *list;
  31. RT_ASSERT(object != RT_NULL);
  32. widget = RTGUI_WIDGET(object);
  33. list = RTGUI_LISTBOX(widget);
  34. pd_win = RTGUI_WIN(rtgui_widget_get_toplevel(widget));
  35. combo = RTGUI_COMBOBOX(pd_win->user_data);
  36. combo->current_item = list->current_item;
  37. if (combo->on_selected != RT_NULL)
  38. combo->on_selected(RTGUI_OBJECT(combo), RT_NULL);
  39. rtgui_win_hide(pd_win);
  40. rtgui_widget_update(RTGUI_WIDGET(combo));
  41. return RT_FALSE;
  42. }
  43. rt_bool_t rtgui_combobox_pdwin_ondeactive(struct rtgui_object *object, struct rtgui_event *event)
  44. {
  45. rtgui_win_hide(RTGUI_WIN(object));
  46. return RT_TRUE;
  47. }
  48. DEFINE_CLASS_TYPE(combobox, "combobox",
  49. RTGUI_WIDGET_TYPE,
  50. _rtgui_combobox_constructor,
  51. _rtgui_combobox_destructor,
  52. sizeof(struct rtgui_combobox));
  53. rtgui_combobox_t *rtgui_combobox_create(struct rtgui_listbox_item *items, rt_uint16_t count, struct rtgui_rect *rect)
  54. {
  55. rtgui_combobox_t *box;
  56. box = (rtgui_combobox_t *)rtgui_widget_create(RTGUI_COMBOBOX_TYPE);
  57. box->items_count = count;
  58. box->items = items;
  59. rtgui_widget_set_rect(RTGUI_WIDGET(box), rect);
  60. box->pd_win = RT_NULL;
  61. return box;
  62. }
  63. void rtgui_combobox_destroy(rtgui_combobox_t *box)
  64. {
  65. rtgui_widget_destroy(RTGUI_WIDGET(box));
  66. }
  67. static void rtgui_combobox_ondraw(struct rtgui_combobox *box)
  68. {
  69. /* draw button */
  70. rtgui_color_t bc;
  71. struct rtgui_dc *dc;
  72. struct rtgui_rect rect, r;
  73. /* begin drawing */
  74. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(box));
  75. if (dc == RT_NULL) return;
  76. bc = RTGUI_WIDGET_BACKGROUND(box);
  77. /* get widget rect */
  78. rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect);
  79. RTGUI_WIDGET_BACKGROUND(box) = white;
  80. /* fill widget rect with background color */
  81. rtgui_dc_fill_rect(dc, &rect);
  82. rtgui_dc_draw_rect(dc, &rect);
  83. /* draw current item */
  84. if (box->current_item < box->items_count)
  85. {
  86. rect.x1 += 5;
  87. rtgui_dc_draw_text(dc, box->items[box->current_item].name, &rect);
  88. }
  89. /* restore background color */
  90. RTGUI_WIDGET_BACKGROUND(box) = bc;
  91. /* draw pull down button */
  92. rect.x1 = rect.x2 - RTGUI_COMBOBOX_BUTTON_WIDTH;
  93. rtgui_rect_inflate(&rect, -1);
  94. rtgui_dc_fill_rect(dc, &rect);
  95. if (box->pd_pressed == RT_TRUE) rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_SUNKEN);
  96. else rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_RAISE);
  97. r.x1 = 0;
  98. r.y1 = 0;
  99. r.x2 = 8;
  100. r.y2 = 4;
  101. rtgui_rect_moveto_align(&rect, &r, RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL);
  102. rtgui_dc_draw_byte(dc, r.x1, r.y1, 4, down_arrow);
  103. /* end drawing */
  104. rtgui_dc_end_drawing(dc);
  105. return;
  106. }
  107. static rt_bool_t rtgui_combobox_onmouse_button(struct rtgui_combobox *box, struct rtgui_event_mouse *event)
  108. {
  109. struct rtgui_rect rect;
  110. /* get widget rect */
  111. rect = RTGUI_WIDGET(box)->extent;
  112. /* move to the pull down button */
  113. rect.x1 = rect.x2 - RTGUI_COMBOBOX_BUTTON_WIDTH;
  114. if (rtgui_rect_contains_point(&rect, event->x, event->y) == RT_EOK)
  115. {
  116. /* handle mouse button on pull down button */
  117. if (event->button & RTGUI_MOUSE_BUTTON_LEFT &&
  118. event->button & RTGUI_MOUSE_BUTTON_DOWN)
  119. {
  120. box->pd_pressed = RT_TRUE;
  121. rtgui_widget_update(RTGUI_WIDGET(box));
  122. }
  123. else if (event->button & RTGUI_MOUSE_BUTTON_LEFT &&
  124. event->button & RTGUI_MOUSE_BUTTON_UP)
  125. {
  126. box->pd_pressed = RT_FALSE;
  127. rtgui_widget_update(RTGUI_WIDGET(box));
  128. /* pop pull down window */
  129. if (box->pd_win == RT_NULL)
  130. {
  131. rtgui_listbox_t *list;
  132. /* create pull down window */
  133. rect = RTGUI_WIDGET(box)->extent;
  134. rect.y1 = rect.y2;
  135. /* give it 5 pixels margin, or the last item won't get shown */
  136. rect.y2 = rect.y1 + box->items_count * (2 + rtgui_theme_get_selected_height()) + 5;
  137. box->pd_win = rtgui_win_create(RT_NULL, "combo", &rect, RTGUI_WIN_STYLE_NO_TITLE);
  138. rtgui_win_set_ondeactivate(RTGUI_WIN(box->pd_win), rtgui_combobox_pulldown_hide);
  139. /* set user data to parent combobox */
  140. box->pd_win->user_data = (rt_uint32_t)box;
  141. /* create list box */
  142. rtgui_rect_inflate(&rect, -1);
  143. list = rtgui_listbox_create(box->items, box->items_count, &rect);
  144. rtgui_container_add_child(RTGUI_CONTAINER(box->pd_win), RTGUI_WIDGET(list));
  145. rtgui_widget_focus(RTGUI_WIDGET(list));
  146. rtgui_listbox_set_onitem(list, rtgui_combobox_pdwin_onitem);
  147. rtgui_win_set_ondeactivate(box->pd_win, rtgui_combobox_pdwin_ondeactive);
  148. }
  149. /* show combo box pull down window */
  150. rtgui_win_show(RTGUI_WIN(box->pd_win), RT_FALSE);
  151. }
  152. return RT_TRUE;
  153. }
  154. return RT_FALSE;
  155. }
  156. rt_bool_t rtgui_combobox_event_handler(struct rtgui_object *object, struct rtgui_event *event)
  157. {
  158. struct rtgui_combobox *box;
  159. RTGUI_WIDGET_EVENT_HANDLER_PREPARE
  160. box = RTGUI_COMBOBOX(object);
  161. switch (event->type)
  162. {
  163. case RTGUI_EVENT_PAINT:
  164. #ifndef RTGUI_USING_SMALL_SIZE
  165. if (widget->on_draw != RT_NULL)
  166. widget->on_draw(RTGUI_OBJECT(widget), event);
  167. else
  168. #endif
  169. rtgui_combobox_ondraw(box);
  170. break;
  171. case RTGUI_EVENT_MOUSE_BUTTON:
  172. return rtgui_combobox_onmouse_button(box, (struct rtgui_event_mouse *)event);
  173. case RTGUI_EVENT_FOCUSED:
  174. {
  175. /* item focused */
  176. struct rtgui_event_focused *focused;
  177. focused = (struct rtgui_event_focused *) event;
  178. if (focused->widget != RT_NULL)
  179. {
  180. /* hide pull down window */
  181. rtgui_win_hide(RTGUI_WIN(box->pd_win));
  182. rtgui_combobox_ondraw(box);
  183. }
  184. }
  185. break;
  186. default:
  187. return rtgui_widget_event_handler(object, event);
  188. }
  189. return RT_FALSE;
  190. }
  191. static rt_bool_t rtgui_combobox_pulldown_hide(struct rtgui_object *object, struct rtgui_event *event)
  192. {
  193. struct rtgui_widget *widget;
  194. struct rtgui_combobox *box;
  195. RT_ASSERT(object != RT_NULL);
  196. RT_ASSERT(event != RT_NULL);
  197. widget = RTGUI_WIDGET(object);
  198. box = RTGUI_COMBOBOX(object);
  199. if (widget == RT_NULL) return RT_TRUE;
  200. box = (struct rtgui_combobox *)(((struct rtgui_win *)widget)->user_data);
  201. if (box == RT_NULL) return RT_TRUE;
  202. /* hide pull down window */
  203. rtgui_win_hide(RTGUI_WIN(box->pd_win));
  204. /* clear pull down button state */
  205. box->pd_pressed = RT_FALSE;
  206. rtgui_widget_update(RTGUI_WIDGET(box));
  207. return RT_TRUE;
  208. }
  209. struct rtgui_listbox_item *rtgui_combox_get_select(struct rtgui_combobox *box)
  210. {
  211. if ((box != RT_NULL) && (box->current_item < box->items_count))
  212. {
  213. return &(box->items[box->current_item]);
  214. }
  215. return RT_NULL;
  216. }
  217. void rtgui_combobox_set_onselected(struct rtgui_combobox *box, rtgui_event_handler_ptr func)
  218. {
  219. box->on_selected = func;
  220. }