BoxLayout.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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: %zu\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. for (size_t i = 0; i < m_entries.size(); ++i) {
  55. auto& entry = m_entries[i];
  56. if (entry.type == Entry::Type::Spacer) {
  57. ++number_of_visible_entries;
  58. }
  59. if (!entry.widget)
  60. continue;
  61. if (!entry.widget->is_visible())
  62. continue;
  63. ++number_of_visible_entries;
  64. if (entry.widget && entry.widget->size_policy(orientation()) == SizePolicy::Fixed) {
  65. if (should_log) {
  66. dbgprintf("BoxLayout: Subtracting for fixed %s{%p}, size: %s\n", entry.widget->class_name(), entry.widget.ptr(), entry.widget->preferred_size().to_string().characters());
  67. dbgprintf("BoxLayout: Available size before: %s\n", available_size.to_string().characters());
  68. }
  69. available_size -= entry.widget->preferred_size();
  70. if (should_log)
  71. dbgprintf("BoxLayout: Available size after: %s\n", available_size.to_string().characters());
  72. ++number_of_entries_with_fixed_size;
  73. }
  74. available_size -= { spacing(), spacing() };
  75. }
  76. available_size += { spacing(), spacing() };
  77. available_size -= { margins().left() + margins().right(), margins().top() + margins().bottom() };
  78. if (should_log)
  79. dbgprintf("BoxLayout: Number of visible: %d/%zu\n", number_of_visible_entries, m_entries.size());
  80. int number_of_entries_with_automatic_size = number_of_visible_entries - number_of_entries_with_fixed_size;
  81. if (should_log)
  82. 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);
  83. Gfx::IntSize automatic_size;
  84. int remaining_size = 0;
  85. int number_of_entries_with_automatic_size_remaining = number_of_entries_with_automatic_size;
  86. if (number_of_entries_with_automatic_size) {
  87. if (m_orientation == Orientation::Horizontal) {
  88. automatic_size.set_width(available_size.width() / number_of_entries_with_automatic_size);
  89. automatic_size.set_height(widget.height());
  90. remaining_size = available_size.width();
  91. } else {
  92. automatic_size.set_width(widget.width());
  93. automatic_size.set_height(available_size.height() / number_of_entries_with_automatic_size);
  94. remaining_size = available_size.height();
  95. }
  96. }
  97. if (should_log)
  98. dbgprintf("BoxLayout: automatic_size=%s\n", automatic_size.to_string().characters());
  99. int current_x = margins().left();
  100. int current_y = margins().top();
  101. for (size_t i = 0; i < m_entries.size(); ++i) {
  102. auto& entry = m_entries[i];
  103. if (entry.type == Entry::Type::Spacer) {
  104. current_x += automatic_size.width();
  105. current_y += automatic_size.height();
  106. continue;
  107. }
  108. if (!entry.widget)
  109. continue;
  110. if (!entry.widget->is_visible())
  111. continue;
  112. Gfx::IntRect rect(current_x, current_y, 0, 0);
  113. if (entry.layout) {
  114. // FIXME: Implement recursive layout.
  115. ASSERT_NOT_REACHED();
  116. }
  117. ASSERT(entry.widget);
  118. if (entry.widget->size_policy(Orientation::Vertical) == SizePolicy::Fixed) {
  119. rect.set_width(widget.width());
  120. rect.set_height(entry.widget->preferred_size().height());
  121. } else {
  122. if (orientation() == Orientation::Horizontal)
  123. rect.set_height(widget.height());
  124. else
  125. rect.set_height(remaining_size / number_of_entries_with_automatic_size_remaining);
  126. }
  127. if (entry.widget->size_policy(Orientation::Horizontal) == SizePolicy::Fixed) {
  128. rect.set_width(entry.widget->preferred_size().width());
  129. rect.set_height(widget.height());
  130. } else {
  131. if (orientation() == Orientation::Horizontal)
  132. rect.set_width(remaining_size / number_of_entries_with_automatic_size_remaining);
  133. else
  134. rect.set_width(widget.width());
  135. }
  136. if (orientation() == Orientation::Horizontal) {
  137. if (entry.widget->size_policy(Orientation::Vertical) == SizePolicy::Fill)
  138. rect.set_height(widget.height() - margins().top() - margins().bottom());
  139. } else {
  140. if (entry.widget->size_policy(Orientation::Horizontal) == SizePolicy::Fill)
  141. rect.set_width(widget.width() - margins().left() - margins().right());
  142. }
  143. // Apply min/max constraints to filled widgets.
  144. if (entry.widget->size_policy(Orientation::Horizontal) == SizePolicy::Fill) {
  145. if (entry.widget->min_size().width() >= 0)
  146. rect.set_width(max(entry.widget->min_size().width(), rect.width()));
  147. if (entry.widget->max_size().width() >= 0)
  148. rect.set_width(min(entry.widget->max_size().width(), rect.width()));
  149. }
  150. if (entry.widget->size_policy(Orientation::Vertical) == SizePolicy::Fill) {
  151. if (entry.widget->min_size().height() >= 0)
  152. rect.set_height(max(entry.widget->min_size().height(), rect.height()));
  153. if (entry.widget->max_size().height() >= 0)
  154. rect.set_height(min(entry.widget->max_size().height(), rect.height()));
  155. }
  156. if (orientation() == Orientation::Horizontal)
  157. rect.center_vertically_within(widget.rect());
  158. else
  159. rect.center_horizontally_within(widget.rect());
  160. if (should_log)
  161. dbgprintf("BoxLayout: apply, %s{%p} <- %s\n", entry.widget->class_name(), entry.widget.ptr(), rect.to_string().characters());
  162. entry.widget->set_relative_rect(rect);
  163. if (orientation() == Orientation::Horizontal) {
  164. if (entry.widget->size_policy(Orientation::Horizontal) == SizePolicy::Fill) {
  165. remaining_size -= rect.width();
  166. --number_of_entries_with_automatic_size_remaining;
  167. }
  168. current_x += rect.width() + spacing();
  169. } else {
  170. if (entry.widget->size_policy(Orientation::Vertical) == SizePolicy::Fill) {
  171. remaining_size -= rect.height();
  172. --number_of_entries_with_automatic_size_remaining;
  173. }
  174. current_y += rect.height() + spacing();
  175. }
  176. }
  177. }
  178. }