TabWidget.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. int bar_height() const { return 21; }
  44. int container_padding() const { return m_container_padding; }
  45. void set_container_padding(int padding) { m_container_padding = padding; }
  46. void add_widget(const StringView&, Widget&);
  47. void remove_widget(Widget&);
  48. template<class T, class... Args>
  49. T& add_tab(const StringView& title, Args&&... args)
  50. {
  51. auto t = T::construct(forward<Args>(args)...);
  52. add_widget(title, *t);
  53. return *t;
  54. }
  55. void remove_tab(Widget& tab) { remove_widget(tab); }
  56. void set_tab_title(Widget& tab, const StringView& title);
  57. void set_tab_icon(Widget& tab, const Gfx::Bitmap*);
  58. void activate_next_tab();
  59. void activate_previous_tab();
  60. void set_text_alignment(Gfx::TextAlignment alignment) { m_text_alignment = alignment; }
  61. Gfx::TextAlignment text_alignment() const { return m_text_alignment; }
  62. void set_uniform_tabs(bool uniform_tabs) { m_uniform_tabs = uniform_tabs; }
  63. int uniform_tab_width() const;
  64. Function<void(Widget&)> on_change;
  65. protected:
  66. TabWidget();
  67. virtual void paint_event(PaintEvent&) override;
  68. virtual void child_event(Core::ChildEvent&) override;
  69. virtual void resize_event(ResizeEvent&) override;
  70. virtual void mousedown_event(MouseEvent&) override;
  71. virtual void mousemove_event(MouseEvent&) override;
  72. virtual void leave_event(Core::Event&) override;
  73. private:
  74. Gfx::Rect child_rect_for_size(const Gfx::Size&) const;
  75. Gfx::Rect button_rect(int index) const;
  76. Gfx::Rect bar_rect() const;
  77. Gfx::Rect container_rect() const;
  78. void update_bar();
  79. RefPtr<Widget> m_active_widget;
  80. struct TabData {
  81. int width(const Gfx::Font&) const;
  82. String title;
  83. RefPtr<Gfx::Bitmap> icon;
  84. Widget* widget { nullptr };
  85. };
  86. Vector<TabData> m_tabs;
  87. TabPosition m_tab_position { TabPosition::Top };
  88. int m_hovered_tab_index { -1 };
  89. int m_container_padding { 2 };
  90. Gfx::TextAlignment m_text_alignment { Gfx::TextAlignment::Center };
  91. bool m_uniform_tabs { false };
  92. };
  93. }