BoxLayout.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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/JsonObject.h>
  27. #include <LibGUI/BoxLayout.h>
  28. #include <LibGUI/Widget.h>
  29. #include <LibGfx/Orientation.h>
  30. #include <stdio.h>
  31. //#define GBOXLAYOUT_DEBUG
  32. namespace GUI {
  33. BoxLayout::BoxLayout(Orientation orientation)
  34. : m_orientation(orientation)
  35. {
  36. register_property(
  37. "orientation", [this] { return m_orientation == Gfx::Orientation::Vertical ? "Vertical" : "Horizontal"; }, nullptr);
  38. }
  39. void BoxLayout::run(Widget& widget)
  40. {
  41. bool should_log = false;
  42. #ifdef GBOXLAYOUT_DEBUG
  43. should_log = true;
  44. #endif
  45. if (should_log)
  46. dbgprintf("BoxLayout: running layout on %s{%p}, entry count: %d\n", widget.class_name(), &widget, m_entries.size());
  47. if (m_entries.is_empty())
  48. return;
  49. Gfx::IntSize available_size = widget.size();
  50. int number_of_entries_with_fixed_size = 0;
  51. int number_of_visible_entries = 0;
  52. if (should_log)
  53. dbgprintf("BoxLayout: Starting with available size: %s\n", available_size.to_string().characters());
  54. Optional<size_t> last_entry_with_automatic_size;
  55. for (size_t i = 0; i < m_entries.size(); ++i) {
  56. auto& entry = m_entries[i];
  57. if (entry.type == Entry::Type::Spacer) {
  58. ++number_of_visible_entries;
  59. }
  60. if (!entry.widget)
  61. continue;
  62. if (!entry.widget->is_visible())
  63. continue;
  64. ++number_of_visible_entries;
  65. if (entry.widget && entry.widget->size_policy(orientation()) == SizePolicy::Fixed) {
  66. if (should_log) {
  67. dbgprintf("BoxLayout: Subtracting for fixed %s{%p}, size: %s\n", entry.widget->class_name(), entry.widget.ptr(), entry.widget->preferred_size().to_string().characters());
  68. dbgprintf("BoxLayout: Available size before: %s\n", available_size.to_string().characters());
  69. }
  70. available_size -= entry.widget->preferred_size();
  71. if (should_log)
  72. dbgprintf("BoxLayout: Available size after: %s\n", available_size.to_string().characters());
  73. ++number_of_entries_with_fixed_size;
  74. } else {
  75. last_entry_with_automatic_size = i;
  76. }
  77. available_size -= { spacing(), spacing() };
  78. }
  79. available_size += { spacing(), spacing() };
  80. available_size -= { margins().left() + margins().right(), margins().top() + margins().bottom() };
  81. if (should_log)
  82. dbgprintf("BoxLayout: Number of visible: %d/%d\n", number_of_visible_entries, m_entries.size());
  83. int number_of_entries_with_automatic_size = number_of_visible_entries - number_of_entries_with_fixed_size;
  84. if (should_log)
  85. dbgprintf("BoxLayout: available_size=%s, fixed=%d, fill=%d\n", available_size.to_string().characters(), number_of_entries_with_fixed_size, number_of_entries_with_automatic_size);
  86. Gfx::IntSize automatic_size;
  87. Gfx::IntSize automatic_size_for_last_entry;
  88. if (number_of_entries_with_automatic_size) {
  89. if (m_orientation == Orientation::Horizontal) {
  90. automatic_size.set_width(available_size.width() / number_of_entries_with_automatic_size);
  91. automatic_size.set_height(widget.height());
  92. automatic_size_for_last_entry.set_width(available_size.width() - (number_of_entries_with_automatic_size - 1) * automatic_size.width());
  93. automatic_size_for_last_entry.set_height(widget.height());
  94. } else {
  95. automatic_size.set_width(widget.width());
  96. automatic_size.set_height(available_size.height() / number_of_entries_with_automatic_size);
  97. automatic_size_for_last_entry.set_width(widget.width());
  98. automatic_size_for_last_entry.set_height(available_size.height() - (number_of_entries_with_automatic_size - 1) * automatic_size.height());
  99. }
  100. }
  101. if (should_log)
  102. dbgprintf("BoxLayout: automatic_size=%s\n", automatic_size.to_string().characters());
  103. int current_x = margins().left();
  104. int current_y = margins().top();
  105. for (size_t i = 0; i < m_entries.size(); ++i) {
  106. auto& entry = m_entries[i];
  107. if (entry.type == Entry::Type::Spacer) {
  108. current_x += automatic_size.width();
  109. current_y += automatic_size.height();
  110. }
  111. if (!entry.widget)
  112. continue;
  113. if (!entry.widget->is_visible())
  114. continue;
  115. Gfx::IntRect rect(current_x, current_y, 0, 0);
  116. if (entry.layout) {
  117. // FIXME: Implement recursive layout.
  118. ASSERT_NOT_REACHED();
  119. }
  120. ASSERT(entry.widget);
  121. if (last_entry_with_automatic_size.has_value() && i == last_entry_with_automatic_size.value()) {
  122. rect.set_size(automatic_size_for_last_entry);
  123. } else {
  124. rect.set_size(automatic_size);
  125. }
  126. if (entry.widget->size_policy(Orientation::Vertical) == SizePolicy::Fixed)
  127. rect.set_height(entry.widget->preferred_size().height());
  128. if (entry.widget->size_policy(Orientation::Horizontal) == SizePolicy::Fixed)
  129. rect.set_width(entry.widget->preferred_size().width());
  130. if (orientation() == Orientation::Horizontal) {
  131. if (entry.widget->size_policy(Orientation::Vertical) == SizePolicy::Fill)
  132. rect.set_height(widget.height() - margins().top() - margins().bottom());
  133. rect.center_vertically_within(widget.rect());
  134. } else {
  135. if (entry.widget->size_policy(Orientation::Horizontal) == SizePolicy::Fill)
  136. rect.set_width(widget.width() - margins().left() - margins().right());
  137. rect.center_horizontally_within(widget.rect());
  138. }
  139. if (should_log)
  140. dbgprintf("BoxLayout: apply, %s{%p} <- %s\n", entry.widget->class_name(), entry.widget.ptr(), rect.to_string().characters());
  141. entry.widget->set_relative_rect(rect);
  142. if (orientation() == Orientation::Horizontal)
  143. current_x += rect.width() + spacing();
  144. else
  145. current_y += rect.height() + spacing();
  146. }
  147. }
  148. }