notebook.c 8.9 KB

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