TabWidget.cpp 8.0 KB

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