Layout.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/OwnPtr.h>
  8. #include <AK/Vector.h>
  9. #include <AK/WeakPtr.h>
  10. #include <LibCore/Object.h>
  11. #include <LibGUI/Forward.h>
  12. #include <LibGUI/Margins.h>
  13. #include <LibGfx/Forward.h>
  14. namespace Core {
  15. namespace Registration {
  16. extern Core::ObjectClassRegistration registration_Layout;
  17. }
  18. }
  19. #define REGISTER_LAYOUT(namespace_, class_name) \
  20. namespace Core { \
  21. namespace Registration { \
  22. Core::ObjectClassRegistration registration_##class_name( \
  23. #namespace_ "::" #class_name, []() { return static_ptr_cast<Core::Object>(namespace_::class_name::construct()); }, &registration_Layout); \
  24. } \
  25. }
  26. namespace GUI {
  27. class Layout : public Core::Object {
  28. C_OBJECT_ABSTRACT(Layout);
  29. public:
  30. virtual ~Layout();
  31. void add_widget(Widget&);
  32. void insert_widget_before(Widget& widget, Widget& before_widget);
  33. void add_layout(OwnPtr<Layout>&&);
  34. void add_spacer();
  35. ErrorOr<void> try_add_widget(Widget&);
  36. ErrorOr<void> try_insert_widget_before(Widget& widget, Widget& before_widget);
  37. ErrorOr<void> try_add_spacer();
  38. void remove_widget(Widget&);
  39. virtual void run(Widget&) = 0;
  40. virtual Gfx::IntSize preferred_size() const = 0;
  41. void notify_adopted(Badge<Widget>, Widget&);
  42. void notify_disowned(Badge<Widget>, Widget&);
  43. Margins const& margins() const { return m_margins; }
  44. void set_margins(Margins const&);
  45. int spacing() const { return m_spacing; }
  46. void set_spacing(int);
  47. protected:
  48. Layout();
  49. struct Entry {
  50. enum class Type {
  51. Invalid = 0,
  52. Widget,
  53. Layout,
  54. Spacer,
  55. };
  56. Type type { Type::Invalid };
  57. WeakPtr<Widget> widget {};
  58. OwnPtr<Layout> layout {};
  59. };
  60. void add_entry(Entry&&);
  61. ErrorOr<void> try_add_entry(Entry&&);
  62. WeakPtr<Widget> m_owner;
  63. Vector<Entry> m_entries;
  64. Margins m_margins;
  65. int m_spacing { 3 };
  66. };
  67. }