TabWidget.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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(Widget* parent)
  34. : Widget(parent)
  35. {
  36. }
  37. TabWidget::~TabWidget()
  38. {
  39. }
  40. void TabWidget::add_widget(const StringView& title, Widget* widget)
  41. {
  42. m_tabs.append({ title, widget });
  43. add_child(*widget);
  44. }
  45. void TabWidget::set_active_widget(Widget* widget)
  46. {
  47. if (widget == m_active_widget)
  48. return;
  49. if (m_active_widget)
  50. m_active_widget->set_visible(false);
  51. m_active_widget = widget;
  52. if (m_active_widget) {
  53. m_active_widget->set_relative_rect(child_rect_for_size(size()));
  54. m_active_widget->set_visible(true);
  55. }
  56. update_bar();
  57. }
  58. void TabWidget::resize_event(ResizeEvent& event)
  59. {
  60. if (!m_active_widget)
  61. return;
  62. m_active_widget->set_relative_rect(child_rect_for_size(event.size()));
  63. }
  64. Gfx::Rect TabWidget::child_rect_for_size(const Gfx::Size& size) const
  65. {
  66. Gfx::Rect rect;
  67. switch (m_tab_position) {
  68. case TabPosition::Top:
  69. rect = { { container_padding(), bar_height() + container_padding() }, { size.width() - container_padding() * 2, size.height() - bar_height() - container_padding() * 2 } };
  70. break;
  71. case TabPosition::Bottom:
  72. rect = { { container_padding(), container_padding() }, { size.width() - container_padding() * 2, size.height() - bar_height() - container_padding() * 2 } };
  73. break;
  74. }
  75. if (rect.is_empty())
  76. return {};
  77. return rect;
  78. }
  79. void TabWidget::child_event(Core::ChildEvent& event)
  80. {
  81. if (!event.child() || !Core::is<Widget>(*event.child()))
  82. return Widget::child_event(event);
  83. auto& child = Core::to<Widget>(*event.child());
  84. if (event.type() == Event::ChildAdded) {
  85. if (!m_active_widget)
  86. set_active_widget(&child);
  87. else if (m_active_widget != &child)
  88. child.set_visible(false);
  89. } else if (event.type() == Event::ChildRemoved) {
  90. if (m_active_widget == &child) {
  91. Widget* new_active_widget = nullptr;
  92. for_each_child_widget([&](auto& new_child) {
  93. new_active_widget = &new_child;
  94. return IterationDecision::Break;
  95. });
  96. set_active_widget(new_active_widget);
  97. }
  98. }
  99. Widget::child_event(event);
  100. }
  101. Gfx::Rect TabWidget::bar_rect() const
  102. {
  103. switch (m_tab_position) {
  104. case TabPosition::Top:
  105. return { 0, 0, width(), bar_height() };
  106. case TabPosition::Bottom:
  107. return { 0, height() - bar_height(), width(), bar_height() };
  108. }
  109. ASSERT_NOT_REACHED();
  110. }
  111. Gfx::Rect TabWidget::container_rect() const
  112. {
  113. switch (m_tab_position) {
  114. case TabPosition::Top:
  115. return { 0, bar_height(), width(), height() - bar_height() };
  116. case TabPosition::Bottom:
  117. return { 0, 0, width(), height() - bar_height() };
  118. }
  119. ASSERT_NOT_REACHED();
  120. }
  121. void TabWidget::paint_event(PaintEvent& event)
  122. {
  123. Painter painter(*this);
  124. painter.add_clip_rect(event.rect());
  125. auto container_rect = this->container_rect();
  126. auto padding_rect = container_rect;
  127. for (int i = 0; i < container_padding(); ++i) {
  128. painter.draw_rect(padding_rect, palette().button());
  129. padding_rect.shrink(2, 2);
  130. }
  131. Gfx::StylePainter::paint_frame(painter, container_rect, palette(), Gfx::FrameShape::Container, Gfx::FrameShadow::Raised, 2);
  132. for (int i = 0; i < m_tabs.size(); ++i) {
  133. if (m_tabs[i].widget == m_active_widget)
  134. continue;
  135. bool hovered = i == m_hovered_tab_index;
  136. auto button_rect = this->button_rect(i);
  137. Gfx::StylePainter::paint_tab_button(painter, button_rect, palette(), false, hovered, m_tabs[i].widget->is_enabled());
  138. painter.draw_text(button_rect.translated(0, 1), m_tabs[i].title, Gfx::TextAlignment::Center, palette().button_text());
  139. }
  140. for (int i = 0; i < m_tabs.size(); ++i) {
  141. if (m_tabs[i].widget != m_active_widget)
  142. continue;
  143. bool hovered = i == m_hovered_tab_index;
  144. auto button_rect = this->button_rect(i);
  145. Gfx::StylePainter::paint_tab_button(painter, button_rect, palette(), true, 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. painter.draw_line(button_rect.bottom_left().translated(1, 1), button_rect.bottom_right().translated(-1, 1), palette().button());
  148. break;
  149. }
  150. }
  151. Gfx::Rect TabWidget::button_rect(int index) const
  152. {
  153. int x_offset = 2;
  154. for (int i = 0; i < index; ++i)
  155. x_offset += m_tabs[i].width(font());
  156. Gfx::Rect rect { x_offset, 0, m_tabs[index].width(font()), bar_height() };
  157. if (m_tabs[index].widget != m_active_widget) {
  158. rect.move_by(0, 2);
  159. rect.set_height(rect.height() - 2);
  160. } else {
  161. rect.move_by(-2, 0);
  162. rect.set_width(rect.width() + 4);
  163. }
  164. rect.move_by(bar_rect().location());
  165. return rect;
  166. }
  167. int TabWidget::TabData::width(const Gfx::Font& font) const
  168. {
  169. return 16 + font.width(title);
  170. }
  171. void TabWidget::mousedown_event(MouseEvent& event)
  172. {
  173. for (int i = 0; i < m_tabs.size(); ++i) {
  174. auto button_rect = this->button_rect(i);
  175. if (!button_rect.contains(event.position()))
  176. continue;
  177. set_active_widget(m_tabs[i].widget);
  178. return;
  179. }
  180. }
  181. void TabWidget::mousemove_event(MouseEvent& event)
  182. {
  183. int hovered_tab = -1;
  184. for (int i = 0; i < m_tabs.size(); ++i) {
  185. auto button_rect = this->button_rect(i);
  186. if (!button_rect.contains(event.position()))
  187. continue;
  188. hovered_tab = i;
  189. if (m_tabs[i].widget == m_active_widget)
  190. break;
  191. }
  192. if (hovered_tab == m_hovered_tab_index)
  193. return;
  194. m_hovered_tab_index = hovered_tab;
  195. update_bar();
  196. }
  197. void TabWidget::leave_event(Core::Event&)
  198. {
  199. if (m_hovered_tab_index != -1) {
  200. m_hovered_tab_index = -1;
  201. update_bar();
  202. }
  203. }
  204. void TabWidget::update_bar()
  205. {
  206. auto invalidation_rect = bar_rect();
  207. invalidation_rect.set_height(invalidation_rect.height() + 1);
  208. update(invalidation_rect);
  209. }
  210. void TabWidget::set_tab_position(TabPosition tab_position)
  211. {
  212. if (m_tab_position == tab_position)
  213. return;
  214. m_tab_position = tab_position;
  215. if (m_active_widget)
  216. m_active_widget->set_relative_rect(child_rect_for_size(size()));
  217. update();
  218. }
  219. int TabWidget::active_tab_index() const
  220. {
  221. for (int i = 0; i < m_tabs.size(); i++) {
  222. if (m_tabs.at(i).widget == m_active_widget)
  223. return i;
  224. }
  225. return -1;
  226. }
  227. }