combobox.c 7.3 KB

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