2020-01-18 08:38:21 +00: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.
|
|
|
|
*/
|
|
|
|
|
2019-05-04 23:31:02 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-02-06 19:33:02 +00:00
|
|
|
#include <LibGUI/Widget.h>
|
2019-05-04 23:31:02 +00:00
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
namespace GUI {
|
|
|
|
|
|
|
|
class TabWidget : public Widget {
|
|
|
|
C_OBJECT(TabWidget)
|
2019-05-04 23:31:02 +00:00
|
|
|
public:
|
2019-07-28 15:39:04 +00:00
|
|
|
enum TabPosition {
|
|
|
|
Top,
|
|
|
|
Bottom,
|
|
|
|
};
|
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
virtual ~TabWidget() override;
|
2019-05-04 23:31:02 +00:00
|
|
|
|
2019-07-28 15:39:04 +00:00
|
|
|
TabPosition tab_position() const { return m_tab_position; }
|
|
|
|
void set_tab_position(TabPosition);
|
2019-09-07 14:57:26 +00:00
|
|
|
|
|
|
|
int active_tab_index() const;
|
2019-07-28 15:39:04 +00:00
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
Widget* active_widget() { return m_active_widget.ptr(); }
|
|
|
|
const Widget* active_widget() const { return m_active_widget.ptr(); }
|
|
|
|
void set_active_widget(Widget*);
|
2021-01-08 00:01:23 +00:00
|
|
|
void set_tab_index(int);
|
2019-05-04 23:31:02 +00:00
|
|
|
|
2020-05-18 21:44:08 +00:00
|
|
|
int bar_height() const { return m_bar_visible ? 21 : 0; }
|
2020-04-24 18:30:32 +00:00
|
|
|
|
|
|
|
int container_padding() const { return m_container_padding; }
|
|
|
|
void set_container_padding(int padding) { m_container_padding = padding; }
|
2019-05-04 23:31:02 +00:00
|
|
|
|
2020-02-23 11:23:48 +00:00
|
|
|
void add_widget(const StringView&, Widget&);
|
2020-04-05 22:02:24 +00:00
|
|
|
void remove_widget(Widget&);
|
2020-02-23 11:23:48 +00:00
|
|
|
|
|
|
|
template<class T, class... Args>
|
2020-04-04 09:10:07 +00:00
|
|
|
T& add_tab(const StringView& title, Args&&... args)
|
2020-02-23 11:23:48 +00:00
|
|
|
{
|
|
|
|
auto t = T::construct(forward<Args>(args)...);
|
|
|
|
add_widget(title, *t);
|
2020-04-04 09:10:07 +00:00
|
|
|
return *t;
|
2020-02-23 11:23:48 +00:00
|
|
|
}
|
2019-05-04 23:31:02 +00:00
|
|
|
|
2020-04-05 22:02:24 +00:00
|
|
|
void remove_tab(Widget& tab) { remove_widget(tab); }
|
|
|
|
|
2020-04-23 19:13:47 +00:00
|
|
|
void set_tab_title(Widget& tab, const StringView& title);
|
2020-04-24 20:27:46 +00:00
|
|
|
void set_tab_icon(Widget& tab, const Gfx::Bitmap*);
|
2020-04-23 19:13:47 +00:00
|
|
|
|
2020-04-23 19:43:08 +00:00
|
|
|
void activate_next_tab();
|
|
|
|
void activate_previous_tab();
|
|
|
|
|
2020-04-24 20:27:46 +00:00
|
|
|
void set_text_alignment(Gfx::TextAlignment alignment) { m_text_alignment = alignment; }
|
|
|
|
Gfx::TextAlignment text_alignment() const { return m_text_alignment; }
|
|
|
|
|
2020-09-15 19:33:37 +00:00
|
|
|
bool uniform_tabs() const { return m_uniform_tabs; }
|
2020-04-24 20:36:25 +00:00
|
|
|
void set_uniform_tabs(bool uniform_tabs) { m_uniform_tabs = uniform_tabs; }
|
|
|
|
int uniform_tab_width() const;
|
|
|
|
|
2020-05-18 21:44:08 +00:00
|
|
|
void set_bar_visible(bool bar_visible);
|
|
|
|
bool is_bar_visible() const { return m_bar_visible; };
|
|
|
|
|
2020-04-23 19:13:47 +00:00
|
|
|
Function<void(Widget&)> on_change;
|
2020-05-07 03:03:03 +00:00
|
|
|
Function<void(Widget&)> on_middle_click;
|
2020-05-18 22:36:02 +00:00
|
|
|
Function<void(Widget&, const ContextMenuEvent&)> on_context_menu_request;
|
2020-04-05 22:02:24 +00:00
|
|
|
|
2019-05-04 23:31:02 +00:00
|
|
|
protected:
|
2020-02-23 11:07:13 +00:00
|
|
|
TabWidget();
|
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
virtual void paint_event(PaintEvent&) override;
|
2020-02-02 11:34:39 +00:00
|
|
|
virtual void child_event(Core::ChildEvent&) override;
|
2020-02-02 14:07:41 +00:00
|
|
|
virtual void resize_event(ResizeEvent&) override;
|
|
|
|
virtual void mousedown_event(MouseEvent&) override;
|
|
|
|
virtual void mousemove_event(MouseEvent&) override;
|
2020-02-02 11:34:39 +00:00
|
|
|
virtual void leave_event(Core::Event&) override;
|
2020-04-30 07:04:03 +00:00
|
|
|
virtual void keydown_event(KeyEvent&) override;
|
2020-05-18 22:36:02 +00:00
|
|
|
virtual void context_menu_event(ContextMenuEvent&) override;
|
2019-05-04 23:31:02 +00:00
|
|
|
|
|
|
|
private:
|
2020-06-10 08:57:59 +00:00
|
|
|
Gfx::IntRect child_rect_for_size(const Gfx::IntSize&) const;
|
|
|
|
Gfx::IntRect button_rect(int index) const;
|
|
|
|
Gfx::IntRect bar_rect() const;
|
|
|
|
Gfx::IntRect container_rect() const;
|
2019-05-07 12:01:20 +00:00
|
|
|
void update_bar();
|
2020-10-30 15:30:33 +00:00
|
|
|
void update_focus_policy();
|
2019-05-04 23:31:02 +00:00
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
RefPtr<Widget> m_active_widget;
|
2019-05-04 23:31:02 +00:00
|
|
|
|
|
|
|
struct TabData {
|
2020-02-06 10:56:38 +00:00
|
|
|
int width(const Gfx::Font&) const;
|
2019-05-04 23:31:02 +00:00
|
|
|
String title;
|
2020-04-24 20:27:46 +00:00
|
|
|
RefPtr<Gfx::Bitmap> icon;
|
2020-02-02 14:07:41 +00:00
|
|
|
Widget* widget { nullptr };
|
2019-05-04 23:31:02 +00:00
|
|
|
};
|
|
|
|
Vector<TabData> m_tabs;
|
2019-07-28 15:39:04 +00:00
|
|
|
TabPosition m_tab_position { TabPosition::Top };
|
2019-05-07 12:01:20 +00:00
|
|
|
int m_hovered_tab_index { -1 };
|
2020-04-24 18:30:32 +00:00
|
|
|
int m_container_padding { 2 };
|
2020-04-24 20:27:46 +00:00
|
|
|
Gfx::TextAlignment m_text_alignment { Gfx::TextAlignment::Center };
|
2020-04-24 20:36:25 +00:00
|
|
|
bool m_uniform_tabs { false };
|
2020-05-18 21:44:08 +00:00
|
|
|
bool m_bar_visible { true };
|
2019-05-04 23:31:02 +00:00
|
|
|
};
|
2020-02-02 14:07:41 +00:00
|
|
|
|
|
|
|
}
|