combobox.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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(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. 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. RT_ASSERT(event != RT_NULL);
  33. widget = RTGUI_WIDGET(object);
  34. list = RTGUI_LISTBOX(widget);
  35. pd_win = RTGUI_WIN(rtgui_widget_get_toplevel(widget));
  36. combo = RTGUI_COMBOBOX(pd_win->user_data);
  37. combo->current_item = list->current_item;
  38. if (combo->on_selected != RT_NULL)
  39. combo->on_selected(RTGUI_OBJECT(combo), RT_NULL);
  40. rtgui_win_hiden(pd_win);
  41. rtgui_widget_update(RTGUI_WIDGET(combo));
  42. return RT_FALSE;
  43. }
  44. rt_bool_t rtgui_combobox_pdwin_ondeactive(struct rtgui_object* object, struct rtgui_event* event)
  45. {
  46. rtgui_win_hiden(RTGUI_WIN(object));
  47. return RT_TRUE;
  48. }
  49. DEFINE_CLASS_TYPE(combobox, "combobox",
  50. RTGUI_WIDGET_TYPE,
  51. _rtgui_combobox_constructor,
  52. _rtgui_combobox_destructor,
  53. sizeof(struct rtgui_combobox));
  54. rtgui_combobox_t *rtgui_combobox_create(struct rtgui_listbox_item* items, rt_uint16_t count, struct rtgui_rect* rect)
  55. {
  56. rtgui_combobox_t *box;
  57. box = (rtgui_combobox_t*)rtgui_widget_create(RTGUI_COMBOBOX_TYPE);
  58. box->items_count = count;
  59. box->items = items;
  60. rtgui_widget_set_rect(RTGUI_WIDGET(box), rect);
  61. box->pd_win = RT_NULL;
  62. return box;
  63. }
  64. void rtgui_combobox_destroy(rtgui_combobox_t* box)
  65. {
  66. rtgui_widget_destroy(RTGUI_WIDGET(box));
  67. }
  68. static void rtgui_combobox_ondraw(struct rtgui_combobox* box)
  69. {
  70. /* draw button */
  71. rtgui_color_t bc;
  72. struct rtgui_dc* dc;
  73. struct rtgui_rect rect, r;
  74. /* begin drawing */
  75. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(box));
  76. if (dc == RT_NULL) return;
  77. bc = RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(box));
  78. /* get widget rect */
  79. rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect);
  80. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(box)) = white;
  81. /* fill widget rect with background color */
  82. rtgui_dc_fill_rect(dc, &rect);
  83. rtgui_dc_draw_rect(dc, &rect);
  84. /* draw current item */
  85. if (box->current_item < box->items_count)
  86. {
  87. rect.x1 += 5;
  88. rtgui_dc_draw_text(dc, box->items[box->current_item].name, &rect);
  89. }
  90. /* restore background color */
  91. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(box)) = bc;
  92. /* draw pull down button */
  93. rect.x1 = rect.x2 - RTGUI_COMBOBOX_BUTTON_WIDTH;
  94. rtgui_rect_inflate(&rect, -1);
  95. rtgui_dc_fill_rect(dc, &rect);
  96. if (box->pd_pressed == RT_TRUE) rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_SUNKEN);
  97. else rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_RAISE);
  98. r.x1 = 0; r.y1 = 0; r.x2 = 8; r.y2 = 4;
  99. rtgui_rect_moveto_align(&rect, &r, RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL);
  100. rtgui_dc_draw_byte(dc, r.x1, r.y1, 4, down_arrow);
  101. /* end drawing */
  102. rtgui_dc_end_drawing(dc);
  103. return;
  104. }
  105. static rt_bool_t rtgui_combobox_onmouse_button(struct rtgui_combobox* box, struct rtgui_event_mouse* event)
  106. {
  107. struct rtgui_rect rect;
  108. /* get widget rect */
  109. rect = RTGUI_WIDGET(box)->extent;
  110. /* move to the pull down button */
  111. rect.x1 = rect.x2 - RTGUI_COMBOBOX_BUTTON_WIDTH;
  112. if (rtgui_rect_contains_point(&rect, event->x, event->y) == RT_EOK)
  113. {
  114. /* handle mouse button on pull down button */
  115. if (event->button & RTGUI_MOUSE_BUTTON_LEFT &&
  116. event->button & RTGUI_MOUSE_BUTTON_DOWN)
  117. {
  118. box->pd_pressed = RT_TRUE;
  119. rtgui_widget_update(RTGUI_WIDGET(box));
  120. }
  121. else if (event->button & RTGUI_MOUSE_BUTTON_LEFT &&
  122. event->button & RTGUI_MOUSE_BUTTON_UP)
  123. {
  124. box->pd_pressed = RT_FALSE;
  125. rtgui_widget_update(RTGUI_WIDGET(box));
  126. /* pop pull down window */
  127. if (box->pd_win == RT_NULL)
  128. {
  129. rtgui_listbox_t *list;
  130. /* create pull down window */
  131. rect = RTGUI_WIDGET(box)->extent;
  132. rect.y1 = rect.y2;
  133. rect.y2 = rect.y1 + 5 * (2 + rtgui_theme_get_selected_height());
  134. box->pd_win = rtgui_win_create(RT_NULL, "combo", &rect, RTGUI_WIN_STYLE_NO_TITLE);
  135. rtgui_win_set_ondeactivate(RTGUI_WIN(box->pd_win), rtgui_combobox_pulldown_hide);
  136. /* set user data to parent combobox */
  137. box->pd_win->user_data = (rt_uint32_t)box;
  138. /* create list box */
  139. rtgui_rect_inflate(&rect, -1);
  140. list = rtgui_listbox_create(box->items, box->items_count, &rect);
  141. rtgui_container_add_child(RTGUI_CONTAINER(box->pd_win), RTGUI_WIDGET(list));
  142. rtgui_widget_focus(RTGUI_WIDGET(list));
  143. rtgui_listbox_set_onitem(list, rtgui_combobox_pdwin_onitem);
  144. rtgui_win_set_ondeactivate(box->pd_win, rtgui_combobox_pdwin_ondeactive);
  145. }
  146. /* show combo box pull down window */
  147. rtgui_win_show(RTGUI_WIN(box->pd_win), RT_FALSE);
  148. }
  149. return RT_TRUE;
  150. }
  151. return RT_FALSE;
  152. }
  153. rt_bool_t rtgui_combobox_event_handler(struct rtgui_object* object, struct rtgui_event* event)
  154. {
  155. struct rtgui_combobox *box;
  156. RT_ASSERT(object != RT_NULL);
  157. RT_ASSERT(event != RT_NULL);
  158. box = RTGUI_COMBOBOX(object);
  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_event_focused* focused;
  174. focused = (struct rtgui_event_focused*) event;
  175. if (focused->widget != RT_NULL)
  176. {
  177. /* hide pull down window */
  178. rtgui_win_hiden(RTGUI_WIN(box->pd_win));
  179. rtgui_combobox_ondraw(box);
  180. }
  181. }
  182. break;
  183. }
  184. return RT_FALSE;
  185. }
  186. static rt_bool_t rtgui_combobox_pulldown_hide(struct rtgui_object* object, struct rtgui_event* event)
  187. {
  188. struct rtgui_widget *widget;
  189. struct rtgui_combobox *box;
  190. RT_ASSERT(object != RT_NULL);
  191. RT_ASSERT(event != RT_NULL);
  192. widget = RTGUI_WIDGET(object);
  193. box = RTGUI_COMBOBOX(object);
  194. if (widget == RT_NULL) return RT_TRUE;
  195. box = (struct rtgui_combobox*) (((struct rtgui_win*)widget)->user_data);
  196. if (box == RT_NULL) return RT_TRUE;
  197. /* hide pull down window */
  198. rtgui_win_hiden(RTGUI_WIN(box->pd_win));
  199. /* clear pull down button state */
  200. box->pd_pressed = RT_FALSE;
  201. rtgui_widget_update(RTGUI_WIDGET(box));
  202. return RT_TRUE;
  203. }
  204. struct rtgui_listbox_item* rtgui_combox_get_select(struct rtgui_combobox* box)
  205. {
  206. if ((box != RT_NULL) && (box->current_item < box->items_count))
  207. {
  208. return &(box->items[box->current_item]);
  209. }
  210. return RT_NULL;
  211. }
  212. void rtgui_combobox_set_onselected(struct rtgui_combobox* box, rtgui_event_handler_ptr func)
  213. {
  214. box->on_selected = func;
  215. }