notebook.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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_constructor(rtgui_notebook_t *notebook)
  6. {
  7. notebook->childs = RT_NULL;
  8. notebook->count = 0;
  9. notebook->current = RTGUI_NOT_FOUND;
  10. }
  11. static void _rtgui_notebook_destructor(rtgui_notebook_t *notebook)
  12. {
  13. int index;
  14. if (notebook->childs != RT_NULL)
  15. {
  16. for (index = 0; index < notebook->count; index ++)
  17. {
  18. rtgui_widget_destroy(notebook->childs[index].widget);
  19. rt_free(notebook->childs[index].title);
  20. }
  21. rtgui_free(notebook->childs);
  22. }
  23. }
  24. static void _rtgui_notebook_ondraw(rtgui_notebook_t *notebook)
  25. {
  26. struct rtgui_dc* dc;
  27. rtgui_rect_t rect;
  28. int index;
  29. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(notebook));
  30. if (dc == RT_NULL) return;
  31. rtgui_widget_get_rect(RTGUI_WIDGET(notebook), &rect);
  32. if (notebook->count == 0)
  33. {
  34. rtgui_dc_fill_rect(dc, &rect);
  35. }
  36. else
  37. {
  38. /* draw tab bar */
  39. for (index = 0; index < notebook->count; index ++)
  40. {
  41. }
  42. /* draw current tab */
  43. rtgui_widget_update(notebook->childs[notebook->current].widget);
  44. }
  45. rtgui_dc_end_drawing(dc);
  46. }
  47. rtgui_type_t *rtgui_notebook_type_get(void)
  48. {
  49. static rtgui_type_t *noteboot_type = RT_NULL;
  50. if (!noteboot_type)
  51. {
  52. noteboot_type = rtgui_type_create("notebook", RTGUI_WIDGET_TYPE,
  53. sizeof(rtgui_notebook_t),
  54. RTGUI_CONSTRUCTOR(_rtgui_notebook_constructor),
  55. RTGUI_DESTRUCTOR(_rtgui_notebook_destructor));
  56. }
  57. return noteboot_type;
  58. }
  59. rtgui_notebook_t* rtgui_notebook_create(const rtgui_rect_t* rect)
  60. {
  61. struct rtgui_notebook* notebook;
  62. notebook = (struct rtgui_notebook*) rtgui_widget_create (RTGUI_NOTEBOOK_TYPE);
  63. if (notebook != RT_NULL)
  64. {
  65. rtgui_widget_set_rect(RTGUI_WIDGET(notebook), rect);
  66. }
  67. return notebook;
  68. }
  69. void rtgui_notebook_destroy(rtgui_notebook_t* notebook)
  70. {
  71. rtgui_widget_destroy(RTGUI_WIDGET(notebook));
  72. }
  73. void rtgui_notebook_add(rtgui_notebook_t* notebook, const char* label, rtgui_widget_t* child)
  74. {
  75. RT_ASSERT(notebook != RT_NULL);
  76. notebook->count += 1;
  77. notebook->childs = (struct rtgui_notebook_tab*) rtgui_realloc(notebook->childs,
  78. sizeof(struct rtgui_notebook_tab) * notebook->count);
  79. notebook->childs[notebook->count - 1].title = rt_strdup(label);
  80. notebook->childs[notebook->count - 1].widget = child;
  81. }
  82. void rtgui_notebook_remove(rtgui_notebook_t* notebook, rt_uint16_t index)
  83. {
  84. struct rtgui_notebook_tab tab;
  85. RT_ASSERT(notebook != RT_NULL);
  86. if (index < notebook->count)
  87. {
  88. if (notebook->count == 1)
  89. {
  90. tab = notebook->childs[0];
  91. rtgui_free(notebook->childs);
  92. notebook->childs = RT_NULL;
  93. notebook->count = 0;
  94. }
  95. else
  96. {
  97. tab = notebook->childs[index];
  98. for (;index < notebook->count - 1; index ++)
  99. {
  100. notebook->childs[index] = notebook->childs[index + 1];
  101. }
  102. notebook->count -= 1;
  103. notebook->childs = (struct rtgui_notebook_tab*) rtgui_realloc(notebook->childs,
  104. sizeof(struct rtgui_notebook_tab) * notebook->count);
  105. }
  106. rtgui_widget_destroy(tab.widget);
  107. rtgui_free(tab.title);
  108. if (notebook->current == index)
  109. {
  110. /* update tab */
  111. if (notebook->current > notebook->count - 1)
  112. notebook->current = notebook->count - 1;
  113. rtgui_widget_update(RTGUI_WIDGET(notebook));
  114. }
  115. }
  116. }
  117. int rtgui_notebook_get_count(rtgui_notebook_t* notebook)
  118. {
  119. return notebook->count;
  120. }
  121. rtgui_widget_t* rtgui_notebook_get_current(rtgui_notebook_t* notebook)
  122. {
  123. RT_ASSERT(notebook != RT_NULL);
  124. if (notebook->current != RTGUI_NOT_FOUND)
  125. return notebook->childs[notebook->current].widget;
  126. return RT_NULL;
  127. }
  128. void rtgui_notebook_set_current(rtgui_notebook_t* notebook, rtgui_widget_t* widget)
  129. {
  130. rt_int16_t index;
  131. RT_ASSERT(notebook != RT_NULL);
  132. for (index = 0; index < notebook->count; index ++)
  133. {
  134. if (widget == notebook->childs[index].widget)
  135. {
  136. rtgui_notebook_set_current_by_index(notebook, index);
  137. return;
  138. }
  139. }
  140. }
  141. void rtgui_notebook_set_current_by_index(rtgui_notebook_t* notebook, rt_uint16_t index)
  142. {
  143. RT_ASSERT(notebook != RT_NULL);
  144. if ((index < notebook->count) && (notebook->current != index))
  145. {
  146. if (notebook->current != RTGUI_NOT_FOUND)
  147. rtgui_widget_hide(notebook->childs[notebook->current].widget);
  148. notebook->current = index;
  149. rtgui_widget_show(notebook->childs[notebook->current].widget);
  150. rtgui_widget_update(notebook->childs[notebook->current].widget);
  151. }
  152. }
  153. rtgui_widget_t* rtgui_notebook_get_index(rtgui_notebook_t* notebook, rt_uint16_t index)
  154. {
  155. RT_ASSERT(notebook != RT_NULL);
  156. if (index < notebook->count)
  157. return notebook->childs[index].widget;
  158. return RT_NULL;
  159. }
  160. rt_bool_t rtgui_notebook_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  161. {
  162. if (event->type == RTGUI_EVENT_PAINT)
  163. {
  164. _rtgui_notebook_ondraw(RTGUI_NOTEBOOK(widget));
  165. }
  166. else
  167. {
  168. /* use parent event handler */
  169. return rtgui_widget_event_handler(widget, event);
  170. }
  171. return RT_FALSE;
  172. }