BoxLayout.cpp 7.0 KB

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