TabWidget.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/BoxLayout.h>
  27. #include <LibGUI/Painter.h>
  28. #include <LibGUI/TabWidget.h>
  29. #include <LibGfx/Font.h>
  30. #include <LibGfx/Palette.h>
  31. #include <LibGfx/StylePainter.h>
  32. namespace GUI {
  33. TabWidget::TabWidget()
  34. {
  35. }
  36. TabWidget::~TabWidget()
  37. {
  38. }
  39. void TabWidget::add_widget(const StringView& title, Widget& widget)
  40. {
  41. m_tabs.append({ title, &widget });
  42. add_child(widget);
  43. }
  44. void TabWidget::remove_widget(Widget& widget)
  45. {
  46. m_tabs.remove_first_matching([&widget](auto& entry) { return &widget == entry.widget; });
  47. remove_child(widget);
  48. }
  49. void TabWidget::set_active_widget(Widget* widget)
  50. {
  51. if (widget == m_active_widget)
  52. return;
  53. if (m_active_widget)
  54. m_active_widget->set_visible(false);
  55. m_active_widget = widget;
  56. if (m_active_widget) {
  57. m_active_widget->set_relative_rect(child_rect_for_size(size()));
  58. m_active_widget->set_visible(true);
  59. deferred_invoke([this](auto&) {
  60. if (on_change)
  61. on_change(*m_active_widget);
  62. });
  63. }
  64. update_bar();
  65. }
  66. void TabWidget::resize_event(ResizeEvent& event)
  67. {
  68. if (!m_active_widget)
  69. return;
  70. m_active_widget->set_relative_rect(child_rect_for_size(event.size()));
  71. }
  72. Gfx::Rect TabWidget::child_rect_for_size(const Gfx::Size& size) const
  73. {
  74. Gfx::Rect rect;
  75. switch (m_tab_position) {
  76. case TabPosition::Top:
  77. rect = { { container_padding(), bar_height() + container_padding() }, { size.width() - container_padding() * 2, size.height() - bar_height() - container_padding() * 2 } };
  78. break;
  79. case TabPosition::Bottom:
  80. rect = { { container_padding(), container_padding() }, { size.width() - container_padding() * 2, size.height() - bar_height() - container_padding() * 2 } };
  81. break;
  82. }
  83. if (rect.is_empty())
  84. return {};
  85. return rect;
  86. }
  87. void TabWidget::child_event(Core::ChildEvent& event)
  88. {
  89. if (!event.child() || !Core::is<Widget>(*event.child()))
  90. return Widget::child_event(event);
  91. auto& child = Core::to<Widget>(*event.child());
  92. if (event.type() == Event::ChildAdded) {
  93. if (!m_active_widget)
  94. set_active_widget(&child);
  95. else if (m_active_widget != &child)
  96. child.set_visible(false);
  97. } else if (event.type() == Event::ChildRemoved) {
  98. if (m_active_widget == &child) {
  99. Widget* new_active_widget = nullptr;
  100. for_each_child_widget([&](auto& new_child) {
  101. new_active_widget = &new_child;
  102. return IterationDecision::Break;
  103. });
  104. set_active_widget(new_active_widget);
  105. }
  106. }
  107. Widget::child_event(event);
  108. }
  109. Gfx::Rect TabWidget::bar_rect() const
  110. {
  111. switch (m_tab_position) {
  112. case TabPosition::Top:
  113. return { 0, 0, width(), bar_height() };
  114. case TabPosition::Bottom:
  115. return { 0, height() - bar_height(), width(), bar_height() };
  116. }
  117. ASSERT_NOT_REACHED();
  118. }
  119. Gfx::Rect TabWidget::container_rect() const
  120. {
  121. switch (m_tab_position) {
  122. case TabPosition::Top:
  123. return { 0, bar_height(), width(), height() - bar_height() };
  124. case TabPosition::Bottom:
  125. return { 0, 0, width(), height() - bar_height() };
  126. }
  127. ASSERT_NOT_REACHED();
  128. }
  129. void TabWidget::paint_event(PaintEvent& event)
  130. {
  131. Painter painter(*this);
  132. painter.add_clip_rect(event.rect());
  133. auto container_rect = this->container_rect();
  134. auto padding_rect = container_rect;
  135. for (int i = 0; i < container_padding(); ++i) {
  136. painter.draw_rect(padding_rect, palette().button());
  137. padding_rect.shrink(2, 2);
  138. }
  139. Gfx::StylePainter::paint_frame(painter, container_rect, palette(), Gfx::FrameShape::Container, Gfx::FrameShadow::Raised, 2);
  140. for (size_t i = 0; i < m_tabs.size(); ++i) {
  141. if (m_tabs[i].widget == m_active_widget)
  142. continue;
  143. bool hovered = static_cast<int>(i) == m_hovered_tab_index;
  144. auto button_rect = this->button_rect(i);
  145. Gfx::StylePainter::paint_tab_button(painter, button_rect, palette(), false, hovered, m_tabs[i].widget->is_enabled());
  146. painter.draw_text(button_rect.translated(0, 1), m_tabs[i].title, Gfx::TextAlignment::Center, palette().button_text());
  147. }
  148. for (size_t i = 0; i < m_tabs.size(); ++i) {
  149. if (m_tabs[i].widget != m_active_widget)
  150. continue;
  151. bool hovered = static_cast<int>(i) == m_hovered_tab_index;
  152. auto button_rect = this->button_rect(i);
  153. Gfx::StylePainter::paint_tab_button(painter, button_rect, palette(), true, hovered, m_tabs[i].widget->is_enabled());
  154. painter.draw_text(button_rect.translated(0, 1), m_tabs[i].title, Gfx::TextAlignment::Center, palette().button_text());
  155. painter.draw_line(button_rect.bottom_left().translated(1, 1), button_rect.bottom_right().translated(-1, 1), palette().button());
  156. break;
  157. }
  158. }
  159. Gfx::Rect TabWidget::button_rect(int index) const
  160. {
  161. int x_offset = 2;
  162. for (int i = 0; i < index; ++i)
  163. x_offset += m_tabs[i].width(font());
  164. Gfx::Rect rect { x_offset, 0, m_tabs[index].width(font()), bar_height() };
  165. if (m_tabs[index].widget != m_active_widget) {
  166. rect.move_by(0, 2);
  167. rect.set_height(rect.height() - 2);
  168. } else {
  169. rect.move_by(-2, 0);
  170. rect.set_width(rect.width() + 4);
  171. }
  172. rect.move_by(bar_rect().location());
  173. return rect;
  174. }
  175. int TabWidget::TabData::width(const Gfx::Font& font) const
  176. {
  177. return 16 + font.width(title);
  178. }
  179. void TabWidget::mousedown_event(MouseEvent& event)
  180. {
  181. for (size_t i = 0; i < m_tabs.size(); ++i) {
  182. auto button_rect = this->button_rect(i);
  183. if (!button_rect.contains(event.position()))
  184. continue;
  185. set_active_widget(m_tabs[i].widget);
  186. return;
  187. }
  188. }
  189. void TabWidget::mousemove_event(MouseEvent& event)
  190. {
  191. int hovered_tab = -1;
  192. for (size_t i = 0; i < m_tabs.size(); ++i) {
  193. auto button_rect = this->button_rect(i);
  194. if (!button_rect.contains(event.position()))
  195. continue;
  196. hovered_tab = i;
  197. if (m_tabs[i].widget == m_active_widget)
  198. break;
  199. }
  200. if (hovered_tab == m_hovered_tab_index)
  201. return;
  202. m_hovered_tab_index = hovered_tab;
  203. update_bar();
  204. }
  205. void TabWidget::leave_event(Core::Event&)
  206. {
  207. if (m_hovered_tab_index != -1) {
  208. m_hovered_tab_index = -1;
  209. update_bar();
  210. }
  211. }
  212. void TabWidget::update_bar()
  213. {
  214. auto invalidation_rect = bar_rect();
  215. invalidation_rect.set_height(invalidation_rect.height() + 1);
  216. update(invalidation_rect);
  217. }
  218. void TabWidget::set_tab_position(TabPosition tab_position)
  219. {
  220. if (m_tab_position == tab_position)
  221. return;
  222. m_tab_position = tab_position;
  223. if (m_active_widget)
  224. m_active_widget->set_relative_rect(child_rect_for_size(size()));
  225. update();
  226. }
  227. int TabWidget::active_tab_index() const
  228. {
  229. for (size_t i = 0; i < m_tabs.size(); i++) {
  230. if (m_tabs.at(i).widget == m_active_widget)
  231. return i;
  232. }
  233. return -1;
  234. }
  235. }