Layout.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include <AK/Badge.h>
  27. #include <LibGUI/Layout.h>
  28. #include <LibGUI/Widget.h>
  29. namespace GUI {
  30. Layout::Layout()
  31. {
  32. }
  33. Layout::~Layout()
  34. {
  35. }
  36. void Layout::notify_adopted(Badge<Widget>, Widget& widget)
  37. {
  38. if (m_owner == &widget)
  39. return;
  40. m_owner = widget.make_weak_ptr();
  41. }
  42. void Layout::notify_disowned(Badge<Widget>, Widget& widget)
  43. {
  44. ASSERT(m_owner == &widget);
  45. m_owner.clear();
  46. }
  47. void Layout::add_entry(Entry&& entry)
  48. {
  49. m_entries.append(move(entry));
  50. if (m_owner)
  51. m_owner->notify_layout_changed({});
  52. }
  53. void Layout::add_spacer()
  54. {
  55. Entry entry;
  56. entry.type = Entry::Type::Spacer;
  57. add_entry(move(entry));
  58. }
  59. void Layout::add_layout(OwnPtr<Layout>&& layout)
  60. {
  61. Entry entry;
  62. entry.type = Entry::Type::Layout;
  63. entry.layout = move(layout);
  64. add_entry(move(entry));
  65. }
  66. void Layout::add_widget(Widget& widget)
  67. {
  68. Entry entry;
  69. entry.type = Entry::Type::Widget;
  70. entry.widget = widget.make_weak_ptr();
  71. add_entry(move(entry));
  72. }
  73. void Layout::insert_widget_before(Widget& widget, Widget& before_widget)
  74. {
  75. Entry entry;
  76. entry.type = Entry::Type::Widget;
  77. entry.widget = widget.make_weak_ptr();
  78. m_entries.insert_before_matching(move(entry), [&](auto& existing_entry) {
  79. return existing_entry.type == Entry::Type::Widget && existing_entry.widget.ptr() == &before_widget;
  80. });
  81. if (m_owner)
  82. m_owner->notify_layout_changed({});
  83. }
  84. void Layout::remove_widget(Widget& widget)
  85. {
  86. m_entries.remove_first_matching([&](auto& entry) {
  87. return entry.widget == &widget;
  88. });
  89. if (m_owner)
  90. m_owner->notify_layout_changed({});
  91. }
  92. void Layout::set_spacing(int spacing)
  93. {
  94. if (m_spacing == spacing)
  95. return;
  96. m_spacing = spacing;
  97. if (m_owner)
  98. m_owner->notify_layout_changed({});
  99. }
  100. void Layout::set_margins(const Margins& margins)
  101. {
  102. if (m_margins == margins)
  103. return;
  104. m_margins = margins;
  105. if (m_owner)
  106. m_owner->notify_layout_changed({});
  107. }
  108. }