BoxLayout.cpp 7.1 KB

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