TabWidget.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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/Bitmap.h>
  30. #include <LibGfx/Font.h>
  31. #include <LibGfx/Palette.h>
  32. #include <LibGfx/StylePainter.h>
  33. namespace GUI {
  34. TabWidget::TabWidget()
  35. {
  36. }
  37. TabWidget::~TabWidget()
  38. {
  39. }
  40. void TabWidget::add_widget(const StringView& title, Widget& widget)
  41. {
  42. m_tabs.append({ title, nullptr, &widget });
  43. add_child(widget);
  44. }
  45. void TabWidget::remove_widget(Widget& widget)
  46. {
  47. if (active_widget() == &widget)
  48. activate_next_tab();
  49. m_tabs.remove_first_matching([&widget](auto& entry) { return &widget == entry.widget; });
  50. remove_child(widget);
  51. }
  52. void TabWidget::set_active_widget(Widget* widget)
  53. {
  54. if (widget == m_active_widget)
  55. return;
  56. if (m_active_widget)
  57. m_active_widget->set_visible(false);
  58. m_active_widget = widget;
  59. if (m_active_widget) {
  60. m_active_widget->set_relative_rect(child_rect_for_size(size()));
  61. m_active_widget->set_focus(true);
  62. m_active_widget->set_visible(true);
  63. deferred_invoke([this](auto&) {
  64. if (on_change)
  65. on_change(*m_active_widget);
  66. });
  67. }
  68. update_bar();
  69. }
  70. void TabWidget::resize_event(ResizeEvent& event)
  71. {
  72. if (!m_active_widget)
  73. return;
  74. m_active_widget->set_relative_rect(child_rect_for_size(event.size()));
  75. }
  76. Gfx::IntRect TabWidget::child_rect_for_size(const Gfx::IntSize& size) const
  77. {
  78. Gfx::IntRect rect;
  79. switch (m_tab_position) {
  80. case TabPosition::Top:
  81. rect = { { container_padding(), bar_height() + container_padding() }, { size.width() - container_padding() * 2, size.height() - bar_height() - container_padding() * 2 } };
  82. break;
  83. case TabPosition::Bottom:
  84. rect = { { container_padding(), container_padding() }, { size.width() - container_padding() * 2, size.height() - bar_height() - container_padding() * 2 } };
  85. break;
  86. }
  87. if (rect.is_empty())
  88. return {};
  89. return rect;
  90. }
  91. void TabWidget::child_event(Core::ChildEvent& event)
  92. {
  93. if (!event.child() || !is<Widget>(*event.child()))
  94. return Widget::child_event(event);
  95. auto& child = downcast<Widget>(*event.child());
  96. if (event.type() == Event::ChildAdded) {
  97. if (!m_active_widget)
  98. set_active_widget(&child);
  99. else if (m_active_widget != &child)
  100. child.set_visible(false);
  101. } else if (event.type() == Event::ChildRemoved) {
  102. if (m_active_widget == &child) {
  103. Widget* new_active_widget = nullptr;
  104. for_each_child_widget([&](auto& new_child) {
  105. new_active_widget = &new_child;
  106. return IterationDecision::Break;
  107. });
  108. set_active_widget(new_active_widget);
  109. }
  110. }
  111. Widget::child_event(event);
  112. }
  113. Gfx::IntRect TabWidget::bar_rect() const
  114. {
  115. switch (m_tab_position) {
  116. case TabPosition::Top:
  117. return { 0, 0, width(), bar_height() };
  118. case TabPosition::Bottom:
  119. return { 0, height() - bar_height(), width(), bar_height() };
  120. }
  121. ASSERT_NOT_REACHED();
  122. }
  123. Gfx::IntRect TabWidget::container_rect() const
  124. {
  125. switch (m_tab_position) {
  126. case TabPosition::Top:
  127. return { 0, bar_height(), width(), height() - bar_height() };
  128. case TabPosition::Bottom:
  129. return { 0, 0, width(), height() - bar_height() };
  130. }
  131. ASSERT_NOT_REACHED();
  132. }
  133. void TabWidget::paint_event(PaintEvent& event)
  134. {
  135. if (!m_bar_visible)
  136. return;
  137. Painter painter(*this);
  138. painter.add_clip_rect(event.rect());
  139. auto container_rect = this->container_rect();
  140. auto padding_rect = container_rect;
  141. for (int i = 0; i < container_padding(); ++i) {
  142. painter.draw_rect(padding_rect, palette().button());
  143. padding_rect.shrink(2, 2);
  144. }
  145. if (container_padding() > 0)
  146. Gfx::StylePainter::paint_frame(painter, container_rect, palette(), Gfx::FrameShape::Container, Gfx::FrameShadow::Raised, 2);
  147. auto paint_tab_icon_if_needed = [&](auto& icon, auto& button_rect, auto& text_rect) {
  148. if (!icon)
  149. return;
  150. Gfx::IntRect icon_rect { button_rect.x(), button_rect.y(), 16, 16 };
  151. icon_rect.move_by(4, 3);
  152. painter.draw_scaled_bitmap(icon_rect, *icon, icon->rect());
  153. text_rect.set_x(icon_rect.right() + 1 + 4);
  154. text_rect.intersect(button_rect);
  155. };
  156. for (size_t i = 0; i < m_tabs.size(); ++i) {
  157. if (m_tabs[i].widget == m_active_widget)
  158. continue;
  159. bool hovered = static_cast<int>(i) == m_hovered_tab_index;
  160. auto button_rect = this->button_rect(i);
  161. Gfx::StylePainter::paint_tab_button(painter, button_rect, palette(), false, hovered, m_tabs[i].widget->is_enabled(), m_tab_position == TabPosition::Top);
  162. auto text_rect = button_rect.translated(0, m_tab_position == TabPosition::Top ? 1 : 0);
  163. paint_tab_icon_if_needed(m_tabs[i].icon, button_rect, text_rect);
  164. painter.draw_text(text_rect, m_tabs[i].title, m_text_alignment, palette().button_text(), Gfx::TextElision::Right);
  165. }
  166. for (size_t i = 0; i < m_tabs.size(); ++i) {
  167. if (m_tabs[i].widget != m_active_widget)
  168. continue;
  169. bool hovered = static_cast<int>(i) == m_hovered_tab_index;
  170. auto button_rect = this->button_rect(i);
  171. Gfx::StylePainter::paint_tab_button(painter, button_rect, palette(), true, hovered, m_tabs[i].widget->is_enabled(), m_tab_position == TabPosition::Top);
  172. auto text_rect = button_rect.translated(0, m_tab_position == TabPosition::Top ? 1 : 0);
  173. paint_tab_icon_if_needed(m_tabs[i].icon, button_rect, text_rect);
  174. painter.draw_text(text_rect, m_tabs[i].title, m_text_alignment, palette().button_text(), Gfx::TextElision::Right);
  175. if (m_tab_position == TabPosition::Top) {
  176. painter.draw_line(button_rect.bottom_left().translated(1, 1), button_rect.bottom_right().translated(-1, 1), palette().button());
  177. } else if (m_tab_position == TabPosition::Bottom) {
  178. painter.set_pixel(button_rect.top_left().translated(0, -1), palette().threed_highlight());
  179. painter.set_pixel(button_rect.top_right().translated(-1, -1), palette().threed_shadow1());
  180. painter.draw_line(button_rect.top_left().translated(1, -1), button_rect.top_right().translated(-2, -1), palette().button());
  181. painter.draw_line(button_rect.top_left().translated(1, -2), button_rect.top_right().translated(-2, -2), palette().button());
  182. }
  183. break;
  184. }
  185. }
  186. int TabWidget::uniform_tab_width() const
  187. {
  188. int minimum_tab_width = 24;
  189. int maximum_tab_width = 160;
  190. int total_tab_width = m_tabs.size() * maximum_tab_width;
  191. int tab_width = maximum_tab_width;
  192. if (total_tab_width > width())
  193. tab_width = width() / m_tabs.size();
  194. return max(tab_width, minimum_tab_width);
  195. }
  196. void TabWidget::set_bar_visible(bool bar_visible)
  197. {
  198. m_bar_visible = bar_visible;
  199. if (m_active_widget)
  200. m_active_widget->set_relative_rect(child_rect_for_size(size()));
  201. update_bar();
  202. }
  203. Gfx::IntRect TabWidget::button_rect(int index) const
  204. {
  205. int x_offset = 2;
  206. for (int i = 0; i < index; ++i) {
  207. auto tab_width = m_uniform_tabs ? uniform_tab_width() : m_tabs[i].width(font());
  208. x_offset += tab_width;
  209. }
  210. Gfx::IntRect rect { x_offset, 0, m_uniform_tabs ? uniform_tab_width() : m_tabs[index].width(font()), bar_height() };
  211. if (m_tabs[index].widget != m_active_widget) {
  212. rect.move_by(0, m_tab_position == TabPosition::Top ? 2 : 0);
  213. rect.set_height(rect.height() - 2);
  214. } else {
  215. rect.move_by(-2, 0);
  216. rect.set_width(rect.width() + 4);
  217. }
  218. rect.move_by(bar_rect().location());
  219. return rect;
  220. }
  221. int TabWidget::TabData::width(const Gfx::Font& font) const
  222. {
  223. return 16 + font.width(title) + (icon ? (16 + 4) : 0);
  224. }
  225. void TabWidget::mousedown_event(MouseEvent& event)
  226. {
  227. for (size_t i = 0; i < m_tabs.size(); ++i) {
  228. auto button_rect = this->button_rect(i);
  229. if (!button_rect.contains(event.position()))
  230. continue;
  231. if (event.button() == MouseButton::Left) {
  232. set_active_widget(m_tabs[i].widget);
  233. } else if (event.button() == MouseButton::Middle) {
  234. auto* widget = m_tabs[i].widget;
  235. deferred_invoke([this, widget](auto&) {
  236. if (on_middle_click && widget)
  237. on_middle_click(*widget);
  238. });
  239. }
  240. return;
  241. }
  242. }
  243. void TabWidget::mousemove_event(MouseEvent& event)
  244. {
  245. int hovered_tab = -1;
  246. for (size_t i = 0; i < m_tabs.size(); ++i) {
  247. auto button_rect = this->button_rect(i);
  248. if (!button_rect.contains(event.position()))
  249. continue;
  250. hovered_tab = i;
  251. if (m_tabs[i].widget == m_active_widget)
  252. break;
  253. }
  254. if (hovered_tab == m_hovered_tab_index)
  255. return;
  256. m_hovered_tab_index = hovered_tab;
  257. update_bar();
  258. }
  259. void TabWidget::leave_event(Core::Event&)
  260. {
  261. if (m_hovered_tab_index != -1) {
  262. m_hovered_tab_index = -1;
  263. update_bar();
  264. }
  265. }
  266. void TabWidget::update_bar()
  267. {
  268. auto invalidation_rect = bar_rect();
  269. invalidation_rect.set_height(invalidation_rect.height() + 1);
  270. update(invalidation_rect);
  271. }
  272. void TabWidget::set_tab_position(TabPosition tab_position)
  273. {
  274. if (m_tab_position == tab_position)
  275. return;
  276. m_tab_position = tab_position;
  277. if (m_active_widget)
  278. m_active_widget->set_relative_rect(child_rect_for_size(size()));
  279. update();
  280. }
  281. int TabWidget::active_tab_index() const
  282. {
  283. for (size_t i = 0; i < m_tabs.size(); i++) {
  284. if (m_tabs.at(i).widget == m_active_widget)
  285. return i;
  286. }
  287. return -1;
  288. }
  289. void TabWidget::set_tab_title(Widget& tab, const StringView& title)
  290. {
  291. for (auto& t : m_tabs) {
  292. if (t.widget == &tab) {
  293. if (t.title != title) {
  294. t.title = title;
  295. update();
  296. }
  297. return;
  298. }
  299. }
  300. }
  301. void TabWidget::set_tab_icon(Widget& tab, const Gfx::Bitmap* icon)
  302. {
  303. for (auto& t : m_tabs) {
  304. if (t.widget == &tab) {
  305. t.icon = icon;
  306. update();
  307. return;
  308. }
  309. }
  310. }
  311. void TabWidget::activate_next_tab()
  312. {
  313. if (m_tabs.size() <= 1)
  314. return;
  315. int index = active_tab_index();
  316. ++index;
  317. if (index >= (int)m_tabs.size())
  318. index = 0;
  319. set_active_widget(m_tabs.at(index).widget);
  320. }
  321. void TabWidget::activate_previous_tab()
  322. {
  323. if (m_tabs.size() <= 1)
  324. return;
  325. int index = active_tab_index();
  326. --index;
  327. if (index < 0)
  328. index = m_tabs.size() - 1;
  329. set_active_widget(m_tabs.at(index).widget);
  330. }
  331. void TabWidget::keydown_event(KeyEvent& event)
  332. {
  333. if (event.ctrl() && event.key() == Key_Tab) {
  334. if (event.shift())
  335. activate_previous_tab();
  336. else
  337. activate_next_tab();
  338. event.accept();
  339. return;
  340. }
  341. Widget::keydown_event(event);
  342. }
  343. void TabWidget::context_menu_event(ContextMenuEvent& context_menu_event)
  344. {
  345. for (size_t i = 0; i < m_tabs.size(); ++i) {
  346. auto button_rect = this->button_rect(i);
  347. if (!button_rect.contains(context_menu_event.position()))
  348. continue;
  349. auto* widget = m_tabs[i].widget;
  350. deferred_invoke([this, widget, context_menu_event](auto&) {
  351. if (on_context_menu_request && widget)
  352. on_context_menu_request(*widget, context_menu_event);
  353. });
  354. return;
  355. }
  356. }
  357. }