notebook.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. #include <rtgui/dc.h>
  2. #include <rtgui/rtgui.h>
  3. #include <rtgui/rtgui_system.h>
  4. #include <rtgui/widgets/notebook.h>
  5. #include <rtgui/widgets/window.h>
  6. #define RTGUI_NOTEBOOK_TAB_WIDTH 80
  7. struct rtgui_notebook_tab
  8. {
  9. struct rtgui_widget *widget;
  10. char *title;
  11. };
  12. static void _rtgui_notebook_get_bar_rect(struct rtgui_notebook *notebook, struct rtgui_rect* rect);
  13. static void _rtgui_notebook_get_page_rect(struct rtgui_notebook *notebook, struct rtgui_rect* rect);
  14. static void _rtgui_notebook_constructor(struct rtgui_notebook *notebook)
  15. {
  16. notebook->flag = 0;
  17. notebook->childs = RT_NULL;
  18. notebook->count = 0;
  19. notebook->current = 0;
  20. RTGUI_WIDGET(notebook)->gc.textalign = RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL;
  21. rtgui_object_set_event_handler(RTGUI_OBJECT(notebook), rtgui_notebook_event_handler);
  22. }
  23. static void _rtgui_notebook_destructor(struct rtgui_notebook *notebook)
  24. {
  25. int index;
  26. if (notebook->childs != RT_NULL)
  27. {
  28. for (index = 0; index < notebook->count; index ++)
  29. {
  30. rtgui_widget_destroy(notebook->childs[index].widget);
  31. rt_free(notebook->childs[index].title);
  32. }
  33. rtgui_free(notebook->childs);
  34. }
  35. }
  36. DEFINE_CLASS_TYPE(notebook, "notebook",
  37. RTGUI_WIDGET_TYPE,
  38. _rtgui_notebook_constructor,
  39. _rtgui_notebook_destructor,
  40. sizeof(struct rtgui_notebook));
  41. /* Draw tab bars of @param notebook. @param dc should be initialized and
  42. * finished outside this function. Don't pass @param notebook or @param dc as
  43. * RT_NULL, it should be checked outside.
  44. */
  45. static void _rtgui_notebook_draw_bar(struct rtgui_notebook *notebook,
  46. struct rtgui_dc *dc)
  47. {
  48. struct rtgui_rect rect;
  49. int index;
  50. RT_ASSERT((notebook != RT_NULL) && (dc != RT_NULL));
  51. if (notebook->flag & RTGUI_NOTEBOOK_NOTAB)
  52. return;
  53. _rtgui_notebook_get_bar_rect(notebook, &rect);
  54. rtgui_dc_fill_rect(dc, &rect);
  55. rect.x2 = rect.x1 + RTGUI_NOTEBOOK_TAB_WIDTH;
  56. /* draw tab bar */
  57. for (index = 0; index < notebook->count; index++)
  58. {
  59. if (notebook->current == index)
  60. rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_SUNKEN);
  61. else
  62. rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_BOX);
  63. rtgui_dc_draw_text(dc, notebook->childs[index].title, &rect);
  64. rect.x1 += RTGUI_NOTEBOOK_TAB_WIDTH;
  65. rect.x2 += RTGUI_NOTEBOOK_TAB_WIDTH;
  66. }
  67. }
  68. static void _rtgui_notebook_ondraw(struct rtgui_notebook *notebook)
  69. {
  70. struct rtgui_dc* dc;
  71. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(notebook));
  72. if (dc == RT_NULL) return;
  73. if (notebook->count == 0)
  74. {
  75. rtgui_rect_t rect;
  76. rtgui_widget_get_rect(RTGUI_WIDGET(notebook), &rect);
  77. rtgui_dc_fill_rect(dc, &rect);
  78. }
  79. else
  80. {
  81. if (notebook->current == RTGUI_NOT_FOUND)
  82. notebook->current = 0;
  83. _rtgui_notebook_draw_bar(notebook, dc);
  84. /* draw current tab */
  85. rtgui_widget_update(notebook->childs[notebook->current].widget);
  86. }
  87. rtgui_dc_end_drawing(dc);
  88. }
  89. static void _rtgui_notebook_onmouse(struct rtgui_notebook *notebook, struct rtgui_event_mouse* emouse)
  90. {
  91. rtgui_rect_t rect;
  92. /* handle notebook bar */
  93. _rtgui_notebook_get_bar_rect(notebook, &rect);
  94. rtgui_widget_rect_to_device(RTGUI_WIDGET(notebook), &rect);
  95. if (rtgui_rect_contains_point(&rect, emouse->x, emouse->y) == RT_EOK)
  96. {
  97. int index;
  98. struct rtgui_dc* dc;
  99. index = (emouse->x - rect.x1) / RTGUI_NOTEBOOK_TAB_WIDTH;
  100. if (index < notebook->count && index != notebook->current)
  101. {
  102. /* update tab bar */
  103. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(notebook));
  104. if (dc == RT_NULL) return;
  105. rtgui_notebook_set_current_by_index(notebook, index);
  106. _rtgui_notebook_draw_bar(notebook, dc);
  107. rtgui_dc_end_drawing(dc);
  108. }
  109. }
  110. else
  111. {
  112. /* handle on page */
  113. if (RTGUI_OBJECT(notebook->childs[notebook->current].widget)->event_handler != RT_NULL)
  114. RTGUI_OBJECT(notebook->childs[notebook->current].widget)->event_handler(
  115. RTGUI_OBJECT(notebook->childs[notebook->current].widget),
  116. &(emouse->parent));
  117. }
  118. }
  119. static void _rtgui_notebook_get_page_rect(struct rtgui_notebook *notebook, struct rtgui_rect* rect)
  120. {
  121. RT_ASSERT(notebook != RT_NULL);
  122. RT_ASSERT(rect != RT_NULL);
  123. rtgui_widget_get_rect(RTGUI_WIDGET(notebook), rect);
  124. if (notebook->flag == RTGUI_NOTEBOOK_NOTAB)
  125. return;
  126. else if (notebook->flag == RTGUI_NOTEBOOK_TOP)
  127. rect->y1 = rect->y1 + 25;
  128. else if (notebook->flag == RTGUI_NOTEBOOK_BOTTOM)
  129. rect->y2 = rect->y2 - 25;
  130. }
  131. static void _rtgui_notebook_get_bar_rect(struct rtgui_notebook *notebook, struct rtgui_rect* rect)
  132. {
  133. RT_ASSERT(notebook != RT_NULL);
  134. RT_ASSERT(rect != RT_NULL);
  135. rtgui_widget_get_rect(RTGUI_WIDGET(notebook), rect);
  136. if (notebook->flag == RTGUI_NOTEBOOK_NOTAB)
  137. {
  138. rect->x1 = rect->y1 = rect->x2 = rect->y2 = 0;
  139. }
  140. else if (notebook->flag == RTGUI_NOTEBOOK_TOP)
  141. rect->y2 = rect->y1 + 25;
  142. else if (notebook->flag == RTGUI_NOTEBOOK_BOTTOM)
  143. rect->y1 = rect->y2 - 25;
  144. }
  145. struct rtgui_notebook* rtgui_notebook_create(const rtgui_rect_t* rect, rt_uint8_t style)
  146. {
  147. struct rtgui_notebook* notebook;
  148. notebook = (struct rtgui_notebook*) rtgui_widget_create(RTGUI_NOTEBOOK_TYPE);
  149. if (notebook != RT_NULL)
  150. {
  151. notebook->flag = style;
  152. rtgui_widget_set_rect(RTGUI_WIDGET(notebook), rect);
  153. }
  154. return notebook;
  155. }
  156. void rtgui_notebook_destroy(struct rtgui_notebook* notebook)
  157. {
  158. rtgui_widget_destroy(RTGUI_WIDGET(notebook));
  159. }
  160. void rtgui_notebook_add(struct rtgui_notebook* notebook, const char* label, struct rtgui_widget* child)
  161. {
  162. rtgui_rect_t rect;
  163. RT_ASSERT(notebook != RT_NULL);
  164. notebook->count += 1;
  165. notebook->childs = (struct rtgui_notebook_tab*)
  166. rtgui_realloc(notebook->childs,
  167. sizeof(struct rtgui_notebook_tab) * notebook->count);
  168. notebook->childs[notebook->count - 1].title = rt_strdup(label);
  169. notebook->childs[notebook->count - 1].widget = child;
  170. /* set parent */
  171. rtgui_widget_set_parent(child, RTGUI_WIDGET(notebook));
  172. _rtgui_notebook_get_page_rect(notebook, &rect);
  173. rtgui_widget_rect_to_device(RTGUI_WIDGET(notebook), &rect);
  174. rtgui_widget_set_rect(child, &rect);
  175. if (notebook->count - 1 != notebook->current)
  176. rtgui_widget_hide(child);
  177. if (RTGUI_WIDGET(notebook)->toplevel != RT_NULL &&
  178. RTGUI_IS_TOPLEVEL(RTGUI_WIDGET(notebook)->toplevel))
  179. {
  180. struct rtgui_event_update_toplvl eup;
  181. RTGUI_EVENT_UPDATE_TOPLVL_INIT(&eup);
  182. eup.toplvl = RTGUI_WIDGET(notebook)->toplevel;
  183. if (RTGUI_OBJECT(child)->event_handler)
  184. RTGUI_OBJECT(child)->event_handler(RTGUI_OBJECT(child), (struct rtgui_event*)&eup);
  185. }
  186. }
  187. void rtgui_notebook_remove(struct rtgui_notebook* notebook, rt_uint16_t index)
  188. {
  189. struct rtgui_notebook_tab tab;
  190. rt_bool_t need_update = RT_FALSE;
  191. RT_ASSERT(notebook != RT_NULL);
  192. if (index < notebook->count)
  193. {
  194. if (notebook->count == 1)
  195. {
  196. tab = notebook->childs[0];
  197. rtgui_free(notebook->childs);
  198. notebook->childs = RT_NULL;
  199. notebook->count = 0;
  200. }
  201. else
  202. {
  203. if (notebook->current == index)
  204. need_update = RT_TRUE;
  205. tab = notebook->childs[index];
  206. for (;index < notebook->count - 1; index++)
  207. {
  208. notebook->childs[index] = notebook->childs[index + 1];
  209. }
  210. notebook->count -= 1;
  211. notebook->childs = (struct rtgui_notebook_tab*) rtgui_realloc(notebook->childs,
  212. sizeof(struct rtgui_notebook_tab) * notebook->count);
  213. }
  214. rtgui_free(tab.title);
  215. if (need_update)
  216. {
  217. if (notebook->current > notebook->count - 1)
  218. notebook->current = notebook->count - 1;
  219. rtgui_widget_hide(tab.widget);
  220. rtgui_widget_show(notebook->childs[notebook->current].widget);
  221. rtgui_widget_update(RTGUI_WIDGET(notebook));
  222. rtgui_widget_set_parent(tab.widget, RT_NULL);
  223. }
  224. }
  225. }
  226. int rtgui_notebook_get_count(struct rtgui_notebook* notebook)
  227. {
  228. RT_ASSERT(notebook != RT_NULL);
  229. return notebook->count;
  230. }
  231. struct rtgui_widget* rtgui_notebook_get_current(struct rtgui_notebook* notebook)
  232. {
  233. RT_ASSERT(notebook != RT_NULL);
  234. if (notebook->current != RTGUI_NOT_FOUND)
  235. return notebook->childs[notebook->current].widget;
  236. return RT_NULL;
  237. }
  238. rt_int16_t rtgui_notebook_get_current_index(struct rtgui_notebook* notebook)
  239. {
  240. RT_ASSERT(notebook != RT_NULL);
  241. return notebook->current;
  242. }
  243. void rtgui_notebook_set_current(struct rtgui_notebook* notebook, struct rtgui_widget* widget)
  244. {
  245. rt_int16_t index;
  246. RT_ASSERT(notebook != RT_NULL);
  247. for (index = 0; index < notebook->count; index ++)
  248. {
  249. if (widget == notebook->childs[index].widget)
  250. {
  251. rtgui_notebook_set_current_by_index(notebook, index);
  252. return;
  253. }
  254. }
  255. }
  256. void rtgui_notebook_set_current_by_index(struct rtgui_notebook* notebook, rt_uint16_t index)
  257. {
  258. RT_ASSERT(notebook != RT_NULL);
  259. if ((index < notebook->count) && (notebook->current != index))
  260. {
  261. struct rtgui_widget *widget;
  262. if (notebook->current != RTGUI_NOT_FOUND)
  263. rtgui_widget_hide(notebook->childs[notebook->current].widget);
  264. notebook->current = index;
  265. widget = notebook->childs[notebook->current].widget;
  266. rtgui_widget_show(widget);
  267. rtgui_widget_update(widget);
  268. rtgui_widget_focus(widget);
  269. }
  270. }
  271. struct rtgui_widget* rtgui_notebook_get_widget_at(struct rtgui_notebook* notebook, rt_uint16_t index)
  272. {
  273. RT_ASSERT(notebook != RT_NULL);
  274. if (index < notebook->count)
  275. return notebook->childs[index].widget;
  276. return RT_NULL;
  277. }
  278. static rt_bool_t _rtgui_notebook_current_widget_handle(struct rtgui_notebook *notebook,
  279. struct rtgui_event *event)
  280. {
  281. struct rtgui_widget *widget = rtgui_notebook_get_current(notebook);
  282. if (widget && widget != RTGUI_WIDGET(notebook))
  283. return RTGUI_OBJECT(widget)->event_handler(RTGUI_OBJECT(widget), event);
  284. else
  285. return RT_FALSE;
  286. }
  287. static void _rtgui_notebook_all_widget_handle(struct rtgui_notebook *notebook,
  288. struct rtgui_event *event)
  289. {
  290. struct rtgui_object *object;
  291. int i;
  292. for (i = 0; i < notebook->count; i++)
  293. {
  294. object = RTGUI_OBJECT(notebook->childs[i].widget);
  295. if (object->event_handler)
  296. object->event_handler(object, event);
  297. }
  298. }
  299. rt_bool_t rtgui_notebook_event_handler(struct rtgui_object* object, struct rtgui_event* event)
  300. {
  301. struct rtgui_notebook* notebook;
  302. RT_ASSERT(object != RT_NULL);
  303. RT_ASSERT(event != RT_NULL);
  304. notebook = RTGUI_NOTEBOOK(object);
  305. switch (event->type)
  306. {
  307. case RTGUI_EVENT_PAINT:
  308. _rtgui_notebook_ondraw(notebook);
  309. break;
  310. case RTGUI_EVENT_MOUSE_BUTTON:
  311. _rtgui_notebook_onmouse(notebook, (struct rtgui_event_mouse*)event);
  312. break;
  313. case RTGUI_EVENT_SHOW:
  314. /* show myself */
  315. rtgui_widget_onshow(object, event);
  316. /* show the tab widget */
  317. return _rtgui_notebook_current_widget_handle(notebook, event);
  318. case RTGUI_EVENT_HIDE:
  319. /* hide myself */
  320. rtgui_widget_onhide(object, event);
  321. /* hide the tab widget */
  322. return _rtgui_notebook_current_widget_handle(notebook, event);
  323. case RTGUI_EVENT_KBD:
  324. return _rtgui_notebook_current_widget_handle(notebook, event);
  325. case RTGUI_EVENT_UPDATE_TOPLVL:
  326. /* update myself */
  327. rtgui_widget_onupdate_toplvl(object, event);
  328. /* update all the widgets in myself */
  329. _rtgui_notebook_all_widget_handle(notebook, event);
  330. return RT_FALSE;
  331. default:
  332. /* use parent event handler */
  333. return rtgui_widget_event_handler(object, event);
  334. }
  335. return RT_FALSE;
  336. }