| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |  * modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 1. Redistributions of source code must retain the above copyright notice, this | 
					
						
							|  |  |  |  *    list of conditions and the following disclaimer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | 
					
						
							|  |  |  |  *    this list of conditions and the following disclaimer in the documentation | 
					
						
							|  |  |  |  *    and/or other materials provided with the distribution. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
					
						
							|  |  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
					
						
							|  |  |  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
					
						
							|  |  |  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
					
						
							|  |  |  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
					
						
							|  |  |  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
					
						
							|  |  |  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
					
						
							|  |  |  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 20:33:02 +01:00
										 |  |  | #include <LibGUI/BoxLayout.h>
 | 
					
						
							|  |  |  | #include <LibGUI/Painter.h>
 | 
					
						
							|  |  |  | #include <LibGUI/TabWidget.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-14 23:53:11 +01:00
										 |  |  | #include <LibGfx/Font.h>
 | 
					
						
							|  |  |  | #include <LibGfx/Palette.h>
 | 
					
						
							|  |  |  | #include <LibGfx/StylePainter.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | namespace GUI { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-23 12:07:13 +01:00
										 |  |  | TabWidget::TabWidget() | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | TabWidget::~TabWidget() | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-23 12:23:48 +01:00
										 |  |  | void TabWidget::add_widget(const StringView& title, Widget& widget) | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-23 12:23:48 +01:00
										 |  |  |     m_tabs.append({ title, &widget }); | 
					
						
							|  |  |  |     add_child(widget); | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-06 01:02:24 +03:00
										 |  |  | void TabWidget::remove_widget(Widget& widget) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_tabs.remove_first_matching([&widget](auto& entry) { return &widget == entry.widget; }); | 
					
						
							|  |  |  |     remove_child(widget); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void TabWidget::set_active_widget(Widget* widget) | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (widget == m_active_widget) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_active_widget) | 
					
						
							|  |  |  |         m_active_widget->set_visible(false); | 
					
						
							|  |  |  |     m_active_widget = widget; | 
					
						
							|  |  |  |     if (m_active_widget) { | 
					
						
							|  |  |  |         m_active_widget->set_relative_rect(child_rect_for_size(size())); | 
					
						
							| 
									
										
										
										
											2020-04-24 14:16:29 +02:00
										 |  |  |         m_active_widget->set_focus(true); | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  |         m_active_widget->set_visible(true); | 
					
						
							| 
									
										
										
										
											2020-04-06 01:02:24 +03:00
										 |  |  |         deferred_invoke([this](auto&) { | 
					
						
							|  |  |  |             if (on_change) | 
					
						
							|  |  |  |                 on_change(*m_active_widget); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 14:01:20 +02:00
										 |  |  |     update_bar(); | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void TabWidget::resize_event(ResizeEvent& event) | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (!m_active_widget) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     m_active_widget->set_relative_rect(child_rect_for_size(event.size())); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 13:02:38 +01:00
										 |  |  | Gfx::Rect TabWidget::child_rect_for_size(const Gfx::Size& size) const | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-06 13:02:38 +01:00
										 |  |  |     Gfx::Rect rect; | 
					
						
							| 
									
										
										
										
											2019-07-28 17:39:04 +02:00
										 |  |  |     switch (m_tab_position) { | 
					
						
							|  |  |  |     case TabPosition::Top: | 
					
						
							| 
									
										
										
										
											2019-10-23 19:51:09 +02:00
										 |  |  |         rect = { { container_padding(), bar_height() + container_padding() }, { size.width() - container_padding() * 2, size.height() - bar_height() - container_padding() * 2 } }; | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2019-07-28 17:39:04 +02:00
										 |  |  |     case TabPosition::Bottom: | 
					
						
							| 
									
										
										
										
											2019-10-23 19:51:09 +02:00
										 |  |  |         rect = { { container_padding(), container_padding() }, { size.width() - container_padding() * 2, size.height() - bar_height() - container_padding() * 2 } }; | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2019-07-28 17:39:04 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-23 19:51:09 +02:00
										 |  |  |     if (rect.is_empty()) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  |     return rect; | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void TabWidget::child_event(Core::ChildEvent& event) | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     if (!event.child() || !Core::is<Widget>(*event.child())) | 
					
						
							|  |  |  |         return Widget::child_event(event); | 
					
						
							|  |  |  |     auto& child = Core::to<Widget>(*event.child()); | 
					
						
							|  |  |  |     if (event.type() == Event::ChildAdded) { | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  |         if (!m_active_widget) | 
					
						
							|  |  |  |             set_active_widget(&child); | 
					
						
							|  |  |  |         else if (m_active_widget != &child) | 
					
						
							|  |  |  |             child.set_visible(false); | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     } else if (event.type() == Event::ChildRemoved) { | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  |         if (m_active_widget == &child) { | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |             Widget* new_active_widget = nullptr; | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |             for_each_child_widget([&](auto& new_child) { | 
					
						
							| 
									
										
										
										
											2019-05-27 03:52:33 +02:00
										 |  |  |                 new_active_widget = &new_child; | 
					
						
							| 
									
										
										
										
											2019-06-07 17:13:23 +02:00
										 |  |  |                 return IterationDecision::Break; | 
					
						
							| 
									
										
										
										
											2019-05-27 03:52:33 +02:00
										 |  |  |             }); | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  |             set_active_widget(new_active_widget); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     Widget::child_event(event); | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 13:02:38 +01:00
										 |  |  | Gfx::Rect TabWidget::bar_rect() const | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-07-28 17:39:04 +02:00
										 |  |  |     switch (m_tab_position) { | 
					
						
							|  |  |  |     case TabPosition::Top: | 
					
						
							|  |  |  |         return { 0, 0, width(), bar_height() }; | 
					
						
							|  |  |  |     case TabPosition::Bottom: | 
					
						
							|  |  |  |         return { 0, height() - bar_height(), width(), bar_height() }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     ASSERT_NOT_REACHED(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 13:02:38 +01:00
										 |  |  | Gfx::Rect TabWidget::container_rect() const | 
					
						
							| 
									
										
										
										
											2019-07-28 17:39:04 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     switch (m_tab_position) { | 
					
						
							|  |  |  |     case TabPosition::Top: | 
					
						
							|  |  |  |         return { 0, bar_height(), width(), height() - bar_height() }; | 
					
						
							|  |  |  |     case TabPosition::Bottom: | 
					
						
							|  |  |  |         return { 0, 0, width(), height() - bar_height() }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     ASSERT_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void TabWidget::paint_event(PaintEvent& event) | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     Painter painter(*this); | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  |     painter.add_clip_rect(event.rect()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-28 17:39:04 +02:00
										 |  |  |     auto container_rect = this->container_rect(); | 
					
						
							| 
									
										
										
										
											2019-05-05 15:03:34 +02:00
										 |  |  |     auto padding_rect = container_rect; | 
					
						
							|  |  |  |     for (int i = 0; i < container_padding(); ++i) { | 
					
						
							| 
									
										
										
										
											2019-12-24 20:57:54 +01:00
										 |  |  |         painter.draw_rect(padding_rect, palette().button()); | 
					
						
							| 
									
										
										
										
											2019-05-05 15:03:34 +02:00
										 |  |  |         padding_rect.shrink(2, 2); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  |     Gfx::StylePainter::paint_frame(painter, container_rect, palette(), Gfx::FrameShape::Container, Gfx::FrameShadow::Raised, 2); | 
					
						
							| 
									
										
										
										
											2019-05-05 15:03:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-25 14:49:47 +01:00
										 |  |  |     for (size_t i = 0; i < m_tabs.size(); ++i) { | 
					
						
							| 
									
										
										
										
											2019-05-05 14:39:37 +02:00
										 |  |  |         if (m_tabs[i].widget == m_active_widget) | 
					
						
							|  |  |  |             continue; | 
					
						
							| 
									
										
										
										
											2020-02-25 14:49:47 +01:00
										 |  |  |         bool hovered = static_cast<int>(i) == m_hovered_tab_index; | 
					
						
							| 
									
										
										
										
											2019-05-05 14:39:37 +02:00
										 |  |  |         auto button_rect = this->button_rect(i); | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  |         Gfx::StylePainter::paint_tab_button(painter, button_rect, palette(), false, hovered, m_tabs[i].widget->is_enabled()); | 
					
						
							|  |  |  |         painter.draw_text(button_rect.translated(0, 1), m_tabs[i].title, Gfx::TextAlignment::Center, palette().button_text()); | 
					
						
							| 
									
										
										
										
											2019-05-05 14:39:37 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-25 14:49:47 +01:00
										 |  |  |     for (size_t i = 0; i < m_tabs.size(); ++i) { | 
					
						
							| 
									
										
										
										
											2019-05-05 14:39:37 +02:00
										 |  |  |         if (m_tabs[i].widget != m_active_widget) | 
					
						
							|  |  |  |             continue; | 
					
						
							| 
									
										
										
										
											2020-02-25 14:49:47 +01:00
										 |  |  |         bool hovered = static_cast<int>(i) == m_hovered_tab_index; | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  |         auto button_rect = this->button_rect(i); | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  |         Gfx::StylePainter::paint_tab_button(painter, button_rect, palette(), true, hovered, m_tabs[i].widget->is_enabled()); | 
					
						
							|  |  |  |         painter.draw_text(button_rect.translated(0, 1), m_tabs[i].title, Gfx::TextAlignment::Center, palette().button_text()); | 
					
						
							| 
									
										
										
										
											2019-12-24 20:57:54 +01:00
										 |  |  |         painter.draw_line(button_rect.bottom_left().translated(1, 1), button_rect.bottom_right().translated(-1, 1), palette().button()); | 
					
						
							| 
									
										
										
										
											2019-05-05 14:39:37 +02:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 13:02:38 +01:00
										 |  |  | Gfx::Rect TabWidget::button_rect(int index) const | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-05 14:39:37 +02:00
										 |  |  |     int x_offset = 2; | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  |     for (int i = 0; i < index; ++i) | 
					
						
							|  |  |  |         x_offset += m_tabs[i].width(font()); | 
					
						
							| 
									
										
										
										
											2020-02-06 13:02:38 +01:00
										 |  |  |     Gfx::Rect rect { x_offset, 0, m_tabs[index].width(font()), bar_height() }; | 
					
						
							| 
									
										
										
										
											2019-05-05 14:39:37 +02:00
										 |  |  |     if (m_tabs[index].widget != m_active_widget) { | 
					
						
							|  |  |  |         rect.move_by(0, 2); | 
					
						
							|  |  |  |         rect.set_height(rect.height() - 2); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         rect.move_by(-2, 0); | 
					
						
							|  |  |  |         rect.set_width(rect.width() + 4); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-07-28 17:39:04 +02:00
										 |  |  |     rect.move_by(bar_rect().location()); | 
					
						
							| 
									
										
										
										
											2019-05-05 14:39:37 +02:00
										 |  |  |     return rect; | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | int TabWidget::TabData::width(const Gfx::Font& font) const | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     return 16 + font.width(title); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void TabWidget::mousedown_event(MouseEvent& event) | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-25 14:49:47 +01:00
										 |  |  |     for (size_t i = 0; i < m_tabs.size(); ++i) { | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  |         auto button_rect = this->button_rect(i); | 
					
						
							|  |  |  |         if (!button_rect.contains(event.position())) | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         set_active_widget(m_tabs[i].widget); | 
					
						
							| 
									
										
										
										
											2019-05-07 14:01:20 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void TabWidget::mousemove_event(MouseEvent& event) | 
					
						
							| 
									
										
										
										
											2019-05-07 14:01:20 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     int hovered_tab = -1; | 
					
						
							| 
									
										
										
										
											2020-02-25 14:49:47 +01:00
										 |  |  |     for (size_t i = 0; i < m_tabs.size(); ++i) { | 
					
						
							| 
									
										
										
										
											2019-05-07 14:01:20 +02:00
										 |  |  |         auto button_rect = this->button_rect(i); | 
					
						
							|  |  |  |         if (!button_rect.contains(event.position())) | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         hovered_tab = i; | 
					
						
							|  |  |  |         if (m_tabs[i].widget == m_active_widget) | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (hovered_tab == m_hovered_tab_index) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     m_hovered_tab_index = hovered_tab; | 
					
						
							|  |  |  |     update_bar(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void TabWidget::leave_event(Core::Event&) | 
					
						
							| 
									
										
										
										
											2019-05-07 14:01:20 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_hovered_tab_index != -1) { | 
					
						
							|  |  |  |         m_hovered_tab_index = -1; | 
					
						
							|  |  |  |         update_bar(); | 
					
						
							| 
									
										
										
										
											2019-05-05 01:31:02 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-05-07 14:01:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void TabWidget::update_bar() | 
					
						
							| 
									
										
										
										
											2019-05-07 14:01:20 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     auto invalidation_rect = bar_rect(); | 
					
						
							|  |  |  |     invalidation_rect.set_height(invalidation_rect.height() + 1); | 
					
						
							|  |  |  |     update(invalidation_rect); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-07-28 17:39:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void TabWidget::set_tab_position(TabPosition tab_position) | 
					
						
							| 
									
										
										
										
											2019-07-28 17:39:04 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_tab_position == tab_position) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     m_tab_position = tab_position; | 
					
						
							|  |  |  |     if (m_active_widget) | 
					
						
							|  |  |  |         m_active_widget->set_relative_rect(child_rect_for_size(size())); | 
					
						
							|  |  |  |     update(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-09-08 00:13:26 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | int TabWidget::active_tab_index() const | 
					
						
							| 
									
										
										
										
											2019-09-08 00:13:26 +10:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-25 14:49:47 +01:00
										 |  |  |     for (size_t i = 0; i < m_tabs.size(); i++) { | 
					
						
							| 
									
										
										
										
											2019-09-07 16:57:26 +02:00
										 |  |  |         if (m_tabs.at(i).widget == m_active_widget) | 
					
						
							| 
									
										
										
										
											2019-09-08 00:13:26 +10:00
										 |  |  |             return i; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return -1; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-04-23 21:13:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | void TabWidget::set_tab_title(Widget& tab, const StringView& title) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     for (auto& t : m_tabs) { | 
					
						
							|  |  |  |         if (t.widget == &tab) { | 
					
						
							|  |  |  |             if (t.title != title) { | 
					
						
							|  |  |  |                 t.title = title; | 
					
						
							|  |  |  |                 update(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-23 21:43:08 +02:00
										 |  |  | void TabWidget::activate_next_tab() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (m_tabs.size() <= 1) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     int index = active_tab_index(); | 
					
						
							|  |  |  |     ++index; | 
					
						
							|  |  |  |     if (index >= (int)m_tabs.size()) | 
					
						
							|  |  |  |         index = 0; | 
					
						
							|  |  |  |     set_active_widget(m_tabs.at(index).widget); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TabWidget::activate_previous_tab() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (m_tabs.size() <= 1) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     int index = active_tab_index(); | 
					
						
							|  |  |  |     --index; | 
					
						
							|  |  |  |     if (index < 0) | 
					
						
							|  |  |  |         index = m_tabs.size() - 1; | 
					
						
							|  |  |  |     set_active_widget(m_tabs.at(index).widget); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | } |