notebook.c 9.2 KB

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