Layout.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 <LibGUI/UIDimensions.h>
  14. #include <LibGfx/Forward.h>
  15. namespace Core {
  16. namespace Registration {
  17. extern Core::ObjectClassRegistration registration_Layout;
  18. }
  19. }
  20. #define REGISTER_LAYOUT(namespace_, class_name) \
  21. namespace Core { \
  22. namespace Registration { \
  23. Core::ObjectClassRegistration registration_##class_name( \
  24. #namespace_ "::" #class_name##sv, []() { return static_ptr_cast<Core::Object>(namespace_::class_name::construct()); }, &registration_Layout); \
  25. } \
  26. }
  27. namespace GUI {
  28. class Layout : public Core::Object {
  29. C_OBJECT_ABSTRACT(Layout);
  30. public:
  31. virtual ~Layout();
  32. void add_widget(Widget&);
  33. void insert_widget_before(Widget& widget, Widget& before_widget);
  34. void add_layout(OwnPtr<Layout>&&);
  35. void add_spacer();
  36. ErrorOr<void> try_add_widget(Widget&);
  37. ErrorOr<void> try_insert_widget_before(Widget& widget, Widget& before_widget);
  38. ErrorOr<void> try_add_spacer();
  39. void remove_widget(Widget&);
  40. virtual void run(Widget&) = 0;
  41. virtual UISize preferred_size() const = 0;
  42. virtual UISize min_size() const = 0;
  43. void notify_adopted(Badge<Widget>, Widget&);
  44. void notify_disowned(Badge<Widget>, Widget&);
  45. Margins const& margins() const { return m_margins; }
  46. void set_margins(Margins const&);
  47. int spacing() const { return m_spacing; }
  48. void set_spacing(int);
  49. protected:
  50. Layout();
  51. struct Entry {
  52. enum class Type {
  53. Invalid = 0,
  54. Widget,
  55. Layout,
  56. Spacer,
  57. };
  58. Type type { Type::Invalid };
  59. WeakPtr<Widget> widget {};
  60. OwnPtr<Layout> layout {};
  61. };
  62. void add_entry(Entry&&);
  63. ErrorOr<void> try_add_entry(Entry&&);
  64. WeakPtr<Widget> m_owner;
  65. Vector<Entry> m_entries;
  66. Margins m_margins;
  67. int m_spacing { 3 };
  68. };
  69. }