notebook.c 8.9 KB

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