notebook.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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_ASSERT(notebook != RT_NULL);
  191. if (index < notebook->count)
  192. {
  193. if (notebook->count == 1)
  194. {
  195. tab = notebook->childs[0];
  196. rtgui_free(notebook->childs);
  197. notebook->childs = RT_NULL;
  198. notebook->count = 0;
  199. }
  200. else
  201. {
  202. tab = notebook->childs[index];
  203. for (;index < notebook->count - 1; index ++)
  204. {
  205. notebook->childs[index] = notebook->childs[index + 1];
  206. }
  207. notebook->count -= 1;
  208. notebook->childs = (struct rtgui_notebook_tab*) rtgui_realloc(notebook->childs,
  209. sizeof(struct rtgui_notebook_tab) * notebook->count);
  210. }
  211. // FIXME: do we really want to destroy it?
  212. rtgui_widget_destroy(tab.widget);
  213. rtgui_free(tab.title);
  214. if (notebook->current == index)
  215. {
  216. /* update tab */
  217. if (notebook->current > notebook->count - 1)
  218. notebook->current = notebook->count - 1;
  219. rtgui_widget_update(RTGUI_WIDGET(notebook));
  220. }
  221. }
  222. }
  223. int rtgui_notebook_get_count(struct rtgui_notebook* notebook)
  224. {
  225. RT_ASSERT(notebook != RT_NULL);
  226. return notebook->count;
  227. }
  228. struct rtgui_widget* rtgui_notebook_get_current(struct rtgui_notebook* notebook)
  229. {
  230. RT_ASSERT(notebook != RT_NULL);
  231. if (notebook->current != RTGUI_NOT_FOUND)
  232. return notebook->childs[notebook->current].widget;
  233. return RT_NULL;
  234. }
  235. rt_int16_t rtgui_notebook_get_current_index(struct rtgui_notebook* notebook)
  236. {
  237. RT_ASSERT(notebook != RT_NULL);
  238. return notebook->current;
  239. }
  240. void rtgui_notebook_set_current(struct rtgui_notebook* notebook, struct rtgui_widget* widget)
  241. {
  242. rt_int16_t index;
  243. RT_ASSERT(notebook != RT_NULL);
  244. for (index = 0; index < notebook->count; index ++)
  245. {
  246. if (widget == notebook->childs[index].widget)
  247. {
  248. rtgui_notebook_set_current_by_index(notebook, index);
  249. return;
  250. }
  251. }
  252. }
  253. void rtgui_notebook_set_current_by_index(struct rtgui_notebook* notebook, rt_uint16_t index)
  254. {
  255. RT_ASSERT(notebook != RT_NULL);
  256. if ((index < notebook->count) && (notebook->current != index))
  257. {
  258. struct rtgui_widget *widget;
  259. if (notebook->current != RTGUI_NOT_FOUND)
  260. rtgui_widget_hide(notebook->childs[notebook->current].widget);
  261. notebook->current = index;
  262. widget = notebook->childs[notebook->current].widget;
  263. rtgui_widget_show(widget);
  264. rtgui_widget_update(widget);
  265. rtgui_widget_focus(widget);
  266. }
  267. }
  268. struct rtgui_widget* rtgui_notebook_get_widget_at(struct rtgui_notebook* notebook, rt_uint16_t index)
  269. {
  270. RT_ASSERT(notebook != RT_NULL);
  271. if (index < notebook->count)
  272. return notebook->childs[index].widget;
  273. return RT_NULL;
  274. }
  275. static rt_bool_t _rtgui_notebook_current_widget_handle(struct rtgui_notebook *notebook,
  276. struct rtgui_event *event)
  277. {
  278. struct rtgui_widget *widget = rtgui_notebook_get_current(notebook);
  279. if (widget && widget != RTGUI_WIDGET(notebook))
  280. return RTGUI_OBJECT(widget)->event_handler(RTGUI_OBJECT(widget), event);
  281. else
  282. return RT_FALSE;
  283. }
  284. static void _rtgui_notebook_all_widget_handle(struct rtgui_notebook *notebook,
  285. struct rtgui_event *event)
  286. {
  287. struct rtgui_object *object;
  288. int i;
  289. for (i = 0; i < notebook->count; i++)
  290. {
  291. object = RTGUI_OBJECT(notebook->childs[i].widget);
  292. if (object->event_handler)
  293. object->event_handler(object, event);
  294. }
  295. }
  296. rt_bool_t rtgui_notebook_event_handler(struct rtgui_object* object, struct rtgui_event* event)
  297. {
  298. struct rtgui_notebook* notebook;
  299. RT_ASSERT(object != RT_NULL);
  300. RT_ASSERT(event != RT_NULL);
  301. notebook = RTGUI_NOTEBOOK(object);
  302. switch (event->type)
  303. {
  304. case RTGUI_EVENT_PAINT:
  305. _rtgui_notebook_ondraw(notebook);
  306. break;
  307. case RTGUI_EVENT_MOUSE_BUTTON:
  308. _rtgui_notebook_onmouse(notebook, (struct rtgui_event_mouse*)event);
  309. break;
  310. case RTGUI_EVENT_SHOW:
  311. /* show myself */
  312. rtgui_widget_onshow(object, event);
  313. /* show the tab widget */
  314. return _rtgui_notebook_current_widget_handle(notebook, event);
  315. case RTGUI_EVENT_HIDE:
  316. /* hide myself */
  317. rtgui_widget_onhide(object, event);
  318. /* hide the tab widget */
  319. return _rtgui_notebook_current_widget_handle(notebook, event);
  320. case RTGUI_EVENT_KBD:
  321. return _rtgui_notebook_current_widget_handle(notebook, event);
  322. case RTGUI_EVENT_UPDATE_TOPLVL:
  323. /* update myself */
  324. rtgui_widget_onupdate_toplvl(object, event);
  325. /* update all the widgets in myself */
  326. _rtgui_notebook_all_widget_handle(notebook, event);
  327. return RT_FALSE;
  328. default:
  329. /* use parent event handler */
  330. return rtgui_widget_event_handler(object, event);
  331. }
  332. return RT_FALSE;
  333. }