TabWidget.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #pragma once
  27. #include <LibGUI/Widget.h>
  28. namespace GUI {
  29. class TabWidget : public Widget {
  30. C_OBJECT(TabWidget)
  31. public:
  32. enum TabPosition {
  33. Top,
  34. Bottom,
  35. };
  36. virtual ~TabWidget() override;
  37. TabPosition tab_position() const { return m_tab_position; }
  38. void set_tab_position(TabPosition);
  39. int active_tab_index() const;
  40. Widget* active_widget() { return m_active_widget.ptr(); }
  41. const Widget* active_widget() const { return m_active_widget.ptr(); }
  42. void set_active_widget(Widget*);
  43. void set_tab_index(int);
  44. int bar_height() const { return m_bar_visible ? 21 : 0; }
  45. int container_padding() const { return m_container_padding; }
  46. void set_container_padding(int padding) { m_container_padding = padding; }
  47. void add_widget(const StringView&, Widget&);
  48. void remove_widget(Widget&);
  49. template<class T, class... Args>
  50. T& add_tab(const StringView& title, Args&&... args)
  51. {
  52. auto t = T::construct(forward<Args>(args)...);
  53. add_widget(title, *t);
  54. return *t;
  55. }
  56. void remove_tab(Widget& tab) { remove_widget(tab); }
  57. void set_tab_title(Widget& tab, const StringView& title);
  58. void set_tab_icon(Widget& tab, const Gfx::Bitmap*);
  59. void activate_next_tab();
  60. void activate_previous_tab();
  61. void set_text_alignment(Gfx::TextAlignment alignment) { m_text_alignment = alignment; }
  62. Gfx::TextAlignment text_alignment() const { return m_text_alignment; }
  63. bool uniform_tabs() const { return m_uniform_tabs; }
  64. void set_uniform_tabs(bool uniform_tabs) { m_uniform_tabs = uniform_tabs; }
  65. int uniform_tab_width() const;
  66. void set_bar_visible(bool bar_visible);
  67. bool is_bar_visible() const { return m_bar_visible; };
  68. Function<void(size_t)> on_tab_count_change;
  69. Function<void(Widget&)> on_change;
  70. Function<void(Widget&)> on_middle_click;
  71. Function<void(Widget&, const ContextMenuEvent&)> on_context_menu_request;
  72. protected:
  73. TabWidget();
  74. virtual void paint_event(PaintEvent&) override;
  75. virtual void child_event(Core::ChildEvent&) override;
  76. virtual void resize_event(ResizeEvent&) override;
  77. virtual void mousedown_event(MouseEvent&) override;
  78. virtual void mousemove_event(MouseEvent&) override;
  79. virtual void leave_event(Core::Event&) override;
  80. virtual void keydown_event(KeyEvent&) override;
  81. virtual void context_menu_event(ContextMenuEvent&) override;
  82. private:
  83. Gfx::IntRect child_rect_for_size(const Gfx::IntSize&) const;
  84. Gfx::IntRect button_rect(int index) const;
  85. Gfx::IntRect bar_rect() const;
  86. Gfx::IntRect container_rect() const;
  87. void update_bar();
  88. void update_focus_policy();
  89. RefPtr<Widget> m_active_widget;
  90. struct TabData {
  91. int width(const Gfx::Font&) const;
  92. String title;
  93. RefPtr<Gfx::Bitmap> icon;
  94. Widget* widget { nullptr };
  95. };
  96. Vector<TabData> m_tabs;
  97. TabPosition m_tab_position { TabPosition::Top };
  98. int m_hovered_tab_index { -1 };
  99. int m_container_padding { 2 };
  100. Gfx::TextAlignment m_text_alignment { Gfx::TextAlignment::Center };
  101. bool m_uniform_tabs { false };
  102. bool m_bar_visible { true };
  103. };
  104. }