notebook.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #include <rtgui/dc.h>
  2. #include <rtgui/rtgui.h>
  3. #include <rtgui/rtgui_system.h>
  4. #include <rtgui/widgets/notebook.h>
  5. #define RTGUI_NOTEBOOK_TAB_WIDTH 80
  6. static void _rtgui_notebook_get_bar_rect(rtgui_notebook_t *notebook, struct rtgui_rect* rect);
  7. static void _rtgui_notebook_get_page_rect(rtgui_notebook_t *notebook, struct rtgui_rect* rect);
  8. static void _rtgui_notebook_constructor(rtgui_notebook_t *notebook)
  9. {
  10. notebook->flag = 0;
  11. notebook->childs = RT_NULL;
  12. notebook->count = 0;
  13. notebook->current = RTGUI_NOT_FOUND;
  14. RTGUI_WIDGET(notebook)->gc.textalign = RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL;
  15. rtgui_widget_set_event_handler(RTGUI_WIDGET(notebook), rtgui_notebook_event_handler);
  16. }
  17. static void _rtgui_notebook_destructor(rtgui_notebook_t *notebook)
  18. {
  19. int index;
  20. if (notebook->childs != RT_NULL)
  21. {
  22. for (index = 0; index < notebook->count; index ++)
  23. {
  24. rtgui_widget_destroy(notebook->childs[index].widget);
  25. rt_free(notebook->childs[index].title);
  26. }
  27. rtgui_free(notebook->childs);
  28. }
  29. }
  30. /* Draw tab bars of @param notebook. @param dc should be initialized and
  31. * finished outside this function. Don't pass @param notebook or @param dc as
  32. * RT_NULL, it should be checked outside.
  33. */
  34. static void _rtgui_notebook_draw_bar(struct rtgui_notebook *notebook,
  35. struct rtgui_dc *dc)
  36. {
  37. struct rtgui_rect rect;
  38. int index;
  39. RT_ASSERT((notebook != RT_NULL) && (dc != RT_NULL));
  40. _rtgui_notebook_get_bar_rect(notebook, &rect);
  41. rtgui_dc_fill_rect(dc, &rect);
  42. rect.x2 = rect.x1 + RTGUI_NOTEBOOK_TAB_WIDTH;
  43. /* draw tab bar */
  44. for (index = 0; index < notebook->count; index++)
  45. {
  46. if (notebook->current == index)
  47. rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_SUNKEN);
  48. else
  49. rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_BOX);
  50. rtgui_dc_draw_text(dc, notebook->childs[index].title, &rect);
  51. rect.x1 += RTGUI_NOTEBOOK_TAB_WIDTH;
  52. rect.x2 += RTGUI_NOTEBOOK_TAB_WIDTH;
  53. }
  54. }
  55. static void _rtgui_notebook_ondraw(rtgui_notebook_t *notebook)
  56. {
  57. struct rtgui_dc* dc;
  58. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(notebook));
  59. if (dc == RT_NULL) return;
  60. if (notebook->count == 0)
  61. {
  62. rtgui_rect_t rect;
  63. rtgui_widget_get_rect(RTGUI_WIDGET(notebook), &rect);
  64. rtgui_dc_fill_rect(dc, &rect);
  65. }
  66. else
  67. {
  68. if (notebook->current == RTGUI_NOT_FOUND)
  69. notebook->current = 0;
  70. _rtgui_notebook_draw_bar(notebook, dc);
  71. /* draw current tab */
  72. rtgui_widget_update(notebook->childs[notebook->current].widget);
  73. }
  74. rtgui_dc_end_drawing(dc);
  75. }
  76. static void _rtgui_notebook_onmouse(rtgui_notebook_t *notebook, struct rtgui_event_mouse* emouse)
  77. {
  78. rtgui_rect_t rect;
  79. /* handle notebook bar */
  80. _rtgui_notebook_get_bar_rect(notebook, &rect);
  81. rtgui_widget_rect_to_device(RTGUI_WIDGET(notebook), &rect);
  82. if (rtgui_rect_contains_point(&rect, emouse->x, emouse->y) == RT_EOK)
  83. {
  84. int index;
  85. struct rtgui_dc* dc;
  86. index = (emouse->x - rect.x1) / RTGUI_NOTEBOOK_TAB_WIDTH;
  87. if (index < notebook->count)
  88. {
  89. /* update tab bar */
  90. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(notebook));
  91. if (dc == RT_NULL) return;
  92. rtgui_notebook_set_current_by_index(notebook, index);
  93. _rtgui_notebook_draw_bar(notebook, dc);
  94. rtgui_dc_end_drawing(dc);
  95. return;
  96. }
  97. }
  98. /* handle on page */
  99. if (notebook->childs[notebook->current].widget->event_handler != RT_NULL)
  100. notebook->childs[notebook->current].widget->event_handler(notebook->childs[notebook->current].widget,
  101. &(emouse->parent));
  102. }
  103. static void _rtgui_notebook_get_page_rect(rtgui_notebook_t *notebook, struct rtgui_rect* rect)
  104. {
  105. RT_ASSERT(notebook != RT_NULL);
  106. RT_ASSERT(rect != RT_NULL);
  107. rtgui_widget_get_rect(RTGUI_WIDGET(notebook), rect);
  108. if (notebook->flag == RTGUI_NOTEBOOK_NOTAB) return;
  109. else if (notebook->flag == RTGUI_NOTEBOOK_TOP)
  110. rect->y1 = rect->y1 + 25;
  111. else if (notebook->flag == RTGUI_NOTEBOOK_BOTTOM)
  112. rect->y2 = rect->y2 - 25;
  113. }
  114. static void _rtgui_notebook_get_bar_rect(rtgui_notebook_t *notebook, struct rtgui_rect* rect)
  115. {
  116. RT_ASSERT(notebook != RT_NULL);
  117. RT_ASSERT(rect != RT_NULL);
  118. rtgui_widget_get_rect(RTGUI_WIDGET(notebook), rect);
  119. if (notebook->flag == RTGUI_NOTEBOOK_NOTAB) return;
  120. else if (notebook->flag == RTGUI_NOTEBOOK_TOP)
  121. rect->y2 = rect->y1 + 25;
  122. else if (notebook->flag == RTGUI_NOTEBOOK_BOTTOM)
  123. rect->y1 = rect->y2 - 25;
  124. }
  125. DEFINE_CLASS_TYPE(notebook, "notebook",
  126. RTGUI_CONTAINER_TYPE,
  127. _rtgui_notebook_constructor,
  128. _rtgui_notebook_destructor,
  129. sizeof(struct rtgui_notebook));
  130. rtgui_notebook_tab_t *tabs;
  131. struct rtgui_notebook *_notebook;
  132. rtgui_notebook_t* rtgui_notebook_create(const rtgui_rect_t* rect, rt_uint8_t style)
  133. {
  134. struct rtgui_notebook* notebook;
  135. notebook = (struct rtgui_notebook*) rtgui_widget_create (RTGUI_NOTEBOOK_TYPE);
  136. if (notebook != RT_NULL)
  137. {
  138. notebook->flag = style;
  139. rtgui_widget_set_rect(RTGUI_WIDGET(notebook), rect);
  140. }
  141. _notebook = notebook;
  142. return notebook;
  143. }
  144. void rtgui_notebook_destroy(rtgui_notebook_t* notebook)
  145. {
  146. rtgui_widget_destroy(RTGUI_WIDGET(notebook));
  147. }
  148. void rtgui_notebook_add(rtgui_notebook_t* notebook, const char* label, rtgui_widget_t* child)
  149. {
  150. rtgui_rect_t rect;
  151. RT_ASSERT(notebook != RT_NULL);
  152. notebook->count += 1;
  153. notebook->childs = (struct rtgui_notebook_tab*) rtgui_realloc(notebook->childs,
  154. sizeof(struct rtgui_notebook_tab) * notebook->count);
  155. notebook->childs[notebook->count - 1].title = rt_strdup(label);
  156. notebook->childs[notebook->count - 1].widget = child;
  157. tabs = notebook->childs;
  158. /* set parent */
  159. rtgui_widget_set_parent(child, RTGUI_WIDGET(notebook));
  160. _rtgui_notebook_get_page_rect(notebook, &rect);
  161. rtgui_widget_rect_to_device(RTGUI_WIDGET(notebook), &rect);
  162. rtgui_widget_set_rect(child, &rect);
  163. }
  164. void rtgui_notebook_remove(rtgui_notebook_t* notebook, rt_uint16_t index)
  165. {
  166. struct rtgui_notebook_tab tab;
  167. RT_ASSERT(notebook != RT_NULL);
  168. if (index < notebook->count)
  169. {
  170. if (notebook->count == 1)
  171. {
  172. tab = notebook->childs[0];
  173. rtgui_free(notebook->childs);
  174. notebook->childs = RT_NULL;
  175. notebook->count = 0;
  176. }
  177. else
  178. {
  179. tab = notebook->childs[index];
  180. for (;index < notebook->count - 1; index ++)
  181. {
  182. notebook->childs[index] = notebook->childs[index + 1];
  183. }
  184. notebook->count -= 1;
  185. notebook->childs = (struct rtgui_notebook_tab*) rtgui_realloc(notebook->childs,
  186. sizeof(struct rtgui_notebook_tab) * notebook->count);
  187. }
  188. rtgui_widget_destroy(tab.widget);
  189. rtgui_free(tab.title);
  190. if (notebook->current == index)
  191. {
  192. /* update tab */
  193. if (notebook->current > notebook->count - 1)
  194. notebook->current = notebook->count - 1;
  195. rtgui_widget_update(RTGUI_WIDGET(notebook));
  196. }
  197. }
  198. }
  199. int rtgui_notebook_get_count(rtgui_notebook_t* notebook)
  200. {
  201. return notebook->count;
  202. }
  203. rtgui_widget_t* rtgui_notebook_get_current(rtgui_notebook_t* notebook)
  204. {
  205. RT_ASSERT(notebook != RT_NULL);
  206. if (notebook->current != RTGUI_NOT_FOUND)
  207. return notebook->childs[notebook->current].widget;
  208. return RT_NULL;
  209. }
  210. void rtgui_notebook_set_current(rtgui_notebook_t* notebook, rtgui_widget_t* widget)
  211. {
  212. rt_int16_t index;
  213. RT_ASSERT(notebook != RT_NULL);
  214. for (index = 0; index < notebook->count; index ++)
  215. {
  216. if (widget == notebook->childs[index].widget)
  217. {
  218. rtgui_notebook_set_current_by_index(notebook, index);
  219. return;
  220. }
  221. }
  222. }
  223. void rtgui_notebook_set_current_by_index(rtgui_notebook_t* notebook, rt_uint16_t index)
  224. {
  225. RT_ASSERT(notebook != RT_NULL);
  226. if ((index < notebook->count) && (notebook->current != index))
  227. {
  228. if (notebook->current != RTGUI_NOT_FOUND)
  229. rtgui_widget_hide(notebook->childs[notebook->current].widget);
  230. notebook->current = index;
  231. rtgui_widget_show(notebook->childs[notebook->current].widget);
  232. rtgui_widget_update(notebook->childs[notebook->current].widget);
  233. }
  234. }
  235. rtgui_widget_t* rtgui_notebook_get_index(rtgui_notebook_t* notebook, rt_uint16_t index)
  236. {
  237. RT_ASSERT(notebook != RT_NULL);
  238. if (index < notebook->count)
  239. return notebook->childs[index].widget;
  240. return RT_NULL;
  241. }
  242. rt_bool_t rtgui_notebook_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  243. {
  244. struct rtgui_notebook* notebook;
  245. notebook = RTGUI_NOTEBOOK(widget);
  246. if (event->type == RTGUI_EVENT_PAINT)
  247. {
  248. _rtgui_notebook_ondraw(notebook);
  249. }
  250. else if (event->type == RTGUI_EVENT_MOUSE_BUTTON)
  251. {
  252. _rtgui_notebook_onmouse(notebook, (struct rtgui_event_mouse*)event);
  253. }
  254. else if (event->type == RTGUI_EVENT_KBD)
  255. {
  256. if (notebook->current != RTGUI_NOT_FOUND)
  257. {
  258. if (notebook->childs[notebook->current].widget->event_handler != RT_NULL)
  259. return notebook->childs[notebook->current].widget->event_handler(notebook->childs[notebook->current].widget,
  260. event);
  261. }
  262. }
  263. else
  264. {
  265. /* use parent event handler */
  266. return rtgui_widget_event_handler(widget, event);
  267. }
  268. return RT_FALSE;
  269. }