HeaderView.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 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. #include <LibGfx/Orientation.h>
  29. namespace GUI {
  30. class HeaderView final : public Widget {
  31. C_OBJECT(HeaderView);
  32. public:
  33. virtual ~HeaderView() override;
  34. Gfx::Orientation orientation() const { return m_orientation; }
  35. Model* model();
  36. const Model* model() const;
  37. void set_section_size(int section, int size);
  38. int section_size(int section) const;
  39. Gfx::TextAlignment section_alignment(int section) const;
  40. void set_section_alignment(int section, Gfx::TextAlignment);
  41. bool is_section_visible(int section) const;
  42. void set_section_visible(int section, bool);
  43. int section_count() const;
  44. Gfx::IntRect section_rect(int section) const;
  45. private:
  46. HeaderView(AbstractTableView&, Gfx::Orientation);
  47. virtual void paint_event(PaintEvent&) override;
  48. virtual void mousedown_event(MouseEvent&) override;
  49. virtual void mousemove_event(MouseEvent&) override;
  50. virtual void mouseup_event(MouseEvent&) override;
  51. virtual void context_menu_event(ContextMenuEvent&) override;
  52. virtual void leave_event(Core::Event&) override;
  53. int horizontal_padding() const { return 5; }
  54. Gfx::IntRect section_resize_grabbable_rect(int) const;
  55. void paint_horizontal(Painter&);
  56. void paint_vertical(Painter&);
  57. Menu& ensure_context_menu();
  58. RefPtr<Menu> m_context_menu;
  59. AbstractTableView& m_table_view;
  60. Gfx::Orientation m_orientation { Gfx::Orientation::Horizontal };
  61. struct SectionData {
  62. int size { 0 };
  63. bool has_initialized_size { false };
  64. bool visibility { true };
  65. RefPtr<Action> visibility_action;
  66. Gfx::TextAlignment alignment { Gfx::TextAlignment::CenterLeft };
  67. };
  68. SectionData& section_data(int section) const;
  69. void set_hovered_section(int);
  70. mutable Vector<SectionData> m_section_data;
  71. bool m_in_section_resize { false };
  72. Gfx::IntPoint m_section_resize_origin;
  73. int m_section_resize_original_width { 0 };
  74. int m_resizing_section { -1 };
  75. int m_pressed_section { -1 };
  76. bool m_pressed_section_is_pressed { false };
  77. int m_hovered_section { -1 };
  78. };
  79. }