combobox.c 7.4 KB

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