Serenity Operating System
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
27#pragma once
28
29#include <LibGUI/Widget.h>
30
31namespace GUI {
32
33class TabWidget : public Widget {
34 C_OBJECT(TabWidget)
35public:
36 enum TabPosition {
37 Top,
38 Bottom,
39 };
40
41 virtual ~TabWidget() override;
42
43 TabPosition tab_position() const { return m_tab_position; }
44 void set_tab_position(TabPosition);
45
46 int active_tab_index() const;
47
48 Widget* active_widget() { return m_active_widget.ptr(); }
49 const Widget* active_widget() const { return m_active_widget.ptr(); }
50 void set_active_widget(Widget*);
51
52 int bar_height() const { return 21; }
53 int container_padding() const { return 2; }
54
55 void add_widget(const StringView&, Widget&);
56
57 template<class T, class... Args>
58 inline NonnullRefPtr<T> add_tab(const StringView& title, Args&&... args)
59 {
60 auto t = T::construct(forward<Args>(args)...);
61 add_widget(title, *t);
62 return t;
63 }
64
65protected:
66 TabWidget();
67
68 virtual void paint_event(PaintEvent&) override;
69 virtual void child_event(Core::ChildEvent&) override;
70 virtual void resize_event(ResizeEvent&) override;
71 virtual void mousedown_event(MouseEvent&) override;
72 virtual void mousemove_event(MouseEvent&) override;
73 virtual void leave_event(Core::Event&) override;
74
75private:
76 Gfx::Rect child_rect_for_size(const Gfx::Size&) const;
77 Gfx::Rect button_rect(int index) const;
78 Gfx::Rect bar_rect() const;
79 Gfx::Rect container_rect() const;
80 void update_bar();
81
82 RefPtr<Widget> m_active_widget;
83
84 struct TabData {
85 Gfx::Rect rect(const Gfx::Font&) const;
86 int width(const Gfx::Font&) const;
87 String title;
88 Widget* widget { nullptr };
89 };
90 Vector<TabData> m_tabs;
91 TabPosition m_tab_position { TabPosition::Top };
92 int m_hovered_tab_index { -1 };
93};
94
95}