Layout.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. void remove_widget(Widget&);
  36. virtual void run(Widget&) = 0;
  37. virtual Gfx::IntSize preferred_size() const = 0;
  38. void notify_adopted(Badge<Widget>, Widget&);
  39. void notify_disowned(Badge<Widget>, Widget&);
  40. const Margins& margins() const { return m_margins; }
  41. void set_margins(const Margins&);
  42. int spacing() const { return m_spacing; }
  43. void set_spacing(int);
  44. protected:
  45. Layout();
  46. struct Entry {
  47. enum class Type {
  48. Invalid = 0,
  49. Widget,
  50. Layout,
  51. Spacer,
  52. };
  53. Type type { Type::Invalid };
  54. WeakPtr<Widget> widget;
  55. OwnPtr<Layout> layout;
  56. };
  57. void add_entry(Entry&&);
  58. WeakPtr<Widget> m_owner;
  59. Vector<Entry> m_entries;
  60. Margins m_margins;
  61. int m_spacing { 3 };
  62. };
  63. }