TabWidget.cpp 9.0 KB

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