notebook.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 && index != notebook->current)
  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. }
  96. }
  97. else
  98. {
  99. /* handle on page */
  100. if (notebook->childs[notebook->current].widget->event_handler != RT_NULL)
  101. notebook->childs[notebook->current].widget->event_handler(
  102. notebook->childs[notebook->current].widget,
  103. &(emouse->parent));
  104. }
  105. }
  106. static void _rtgui_notebook_get_page_rect(rtgui_notebook_t *notebook, struct rtgui_rect* rect)
  107. {
  108. RT_ASSERT(notebook != RT_NULL);
  109. RT_ASSERT(rect != RT_NULL);
  110. rtgui_widget_get_rect(RTGUI_WIDGET(notebook), rect);
  111. if (notebook->flag == RTGUI_NOTEBOOK_NOTAB) return;
  112. else if (notebook->flag == RTGUI_NOTEBOOK_TOP)
  113. rect->y1 = rect->y1 + 25;
  114. else if (notebook->flag == RTGUI_NOTEBOOK_BOTTOM)
  115. rect->y2 = rect->y2 - 25;
  116. }
  117. static void _rtgui_notebook_get_bar_rect(rtgui_notebook_t *notebook, struct rtgui_rect* rect)
  118. {
  119. RT_ASSERT(notebook != RT_NULL);
  120. RT_ASSERT(rect != RT_NULL);
  121. rtgui_widget_get_rect(RTGUI_WIDGET(notebook), rect);
  122. if (notebook->flag == RTGUI_NOTEBOOK_NOTAB) return;
  123. else if (notebook->flag == RTGUI_NOTEBOOK_TOP)
  124. rect->y2 = rect->y1 + 25;
  125. else if (notebook->flag == RTGUI_NOTEBOOK_BOTTOM)
  126. rect->y1 = rect->y2 - 25;
  127. }
  128. DEFINE_CLASS_TYPE(notebook, "notebook",
  129. RTGUI_CONTAINER_TYPE,
  130. _rtgui_notebook_constructor,
  131. _rtgui_notebook_destructor,
  132. sizeof(struct rtgui_notebook));
  133. rtgui_notebook_tab_t *tabs;
  134. struct rtgui_notebook *_notebook;
  135. rtgui_notebook_t* rtgui_notebook_create(const rtgui_rect_t* rect, rt_uint8_t style)
  136. {
  137. struct rtgui_notebook* notebook;
  138. notebook = (struct rtgui_notebook*) rtgui_widget_create (RTGUI_NOTEBOOK_TYPE);
  139. if (notebook != RT_NULL)
  140. {
  141. notebook->flag = style;
  142. rtgui_widget_set_rect(RTGUI_WIDGET(notebook), rect);
  143. }
  144. _notebook = notebook;
  145. return notebook;
  146. }
  147. void rtgui_notebook_destroy(rtgui_notebook_t* notebook)
  148. {
  149. rtgui_widget_destroy(RTGUI_WIDGET(notebook));
  150. }
  151. void rtgui_notebook_add(rtgui_notebook_t* notebook, const char* label, rtgui_widget_t* child)
  152. {
  153. rtgui_rect_t rect;
  154. RT_ASSERT(notebook != RT_NULL);
  155. notebook->count += 1;
  156. notebook->childs = (struct rtgui_notebook_tab*) rtgui_realloc(notebook->childs,
  157. sizeof(struct rtgui_notebook_tab) * notebook->count);
  158. notebook->childs[notebook->count - 1].title = rt_strdup(label);
  159. notebook->childs[notebook->count - 1].widget = child;
  160. tabs = notebook->childs;
  161. /* set parent */
  162. rtgui_widget_set_parent(child, RTGUI_WIDGET(notebook));
  163. _rtgui_notebook_get_page_rect(notebook, &rect);
  164. rtgui_widget_rect_to_device(RTGUI_WIDGET(notebook), &rect);
  165. rtgui_widget_set_rect(child, &rect);
  166. }
  167. void rtgui_notebook_remove(rtgui_notebook_t* notebook, rt_uint16_t index)
  168. {
  169. struct rtgui_notebook_tab tab;
  170. RT_ASSERT(notebook != RT_NULL);
  171. if (index < notebook->count)
  172. {
  173. if (notebook->count == 1)
  174. {
  175. tab = notebook->childs[0];
  176. rtgui_free(notebook->childs);
  177. notebook->childs = RT_NULL;
  178. notebook->count = 0;
  179. }
  180. else
  181. {
  182. tab = notebook->childs[index];
  183. for (;index < notebook->count - 1; index ++)
  184. {
  185. notebook->childs[index] = notebook->childs[index + 1];
  186. }
  187. notebook->count -= 1;
  188. notebook->childs = (struct rtgui_notebook_tab*) rtgui_realloc(notebook->childs,
  189. sizeof(struct rtgui_notebook_tab) * notebook->count);
  190. }
  191. rtgui_widget_destroy(tab.widget);
  192. rtgui_free(tab.title);
  193. if (notebook->current == index)
  194. {
  195. /* update tab */
  196. if (notebook->current > notebook->count - 1)
  197. notebook->current = notebook->count - 1;
  198. rtgui_widget_update(RTGUI_WIDGET(notebook));
  199. }
  200. }
  201. }
  202. int rtgui_notebook_get_count(rtgui_notebook_t* notebook)
  203. {
  204. return notebook->count;
  205. }
  206. rtgui_widget_t* rtgui_notebook_get_current(rtgui_notebook_t* notebook)
  207. {
  208. RT_ASSERT(notebook != RT_NULL);
  209. if (notebook->current != RTGUI_NOT_FOUND)
  210. return notebook->childs[notebook->current].widget;
  211. return RT_NULL;
  212. }
  213. void rtgui_notebook_set_current(rtgui_notebook_t* notebook, rtgui_widget_t* widget)
  214. {
  215. rt_int16_t index;
  216. RT_ASSERT(notebook != RT_NULL);
  217. for (index = 0; index < notebook->count; index ++)
  218. {
  219. if (widget == notebook->childs[index].widget)
  220. {
  221. rtgui_notebook_set_current_by_index(notebook, index);
  222. return;
  223. }
  224. }
  225. }
  226. void rtgui_notebook_set_current_by_index(rtgui_notebook_t* notebook, rt_uint16_t index)
  227. {
  228. RT_ASSERT(notebook != RT_NULL);
  229. if ((index < notebook->count) && (notebook->current != index))
  230. {
  231. if (notebook->current != RTGUI_NOT_FOUND)
  232. rtgui_widget_hide(notebook->childs[notebook->current].widget);
  233. notebook->current = index;
  234. rtgui_widget_show(notebook->childs[notebook->current].widget);
  235. rtgui_widget_update(notebook->childs[notebook->current].widget);
  236. }
  237. }
  238. rtgui_widget_t* rtgui_notebook_get_index(rtgui_notebook_t* notebook, rt_uint16_t index)
  239. {
  240. RT_ASSERT(notebook != RT_NULL);
  241. if (index < notebook->count)
  242. return notebook->childs[index].widget;
  243. return RT_NULL;
  244. }
  245. rt_bool_t rtgui_notebook_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  246. {
  247. struct rtgui_notebook* notebook;
  248. notebook = RTGUI_NOTEBOOK(widget);
  249. if (event->type == RTGUI_EVENT_PAINT)
  250. {
  251. _rtgui_notebook_ondraw(notebook);
  252. }
  253. else if (event->type == RTGUI_EVENT_MOUSE_BUTTON)
  254. {
  255. _rtgui_notebook_onmouse(notebook, (struct rtgui_event_mouse*)event);
  256. }
  257. else if (event->type == RTGUI_EVENT_KBD)
  258. {
  259. if (notebook->current != RTGUI_NOT_FOUND)
  260. {
  261. if (notebook->childs[notebook->current].widget->event_handler != RT_NULL)
  262. return notebook->childs[notebook->current].widget->event_handler(notebook->childs[notebook->current].widget,
  263. event);
  264. }
  265. }
  266. else
  267. {
  268. /* use parent event handler */
  269. return rtgui_widget_event_handler(widget, event);
  270. }
  271. return RT_FALSE;
  272. }