Layout.cpp 3.3 KB

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