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