BoxLayout.cpp 7.3 KB

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