GLayout.cpp 3.3 KB

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