notebook.c 8.7 KB

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