GBoxLayout.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/GBoxLayout.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. for (auto& entry : m_entries) {
  50. if (entry.type == Entry::Type::Spacer) {
  51. ++number_of_visible_entries;
  52. }
  53. if (!entry.widget)
  54. continue;
  55. if (!entry.widget->is_visible())
  56. continue;
  57. ++number_of_visible_entries;
  58. if (entry.widget && entry.widget->size_policy(orientation()) == SizePolicy::Fixed) {
  59. if (should_log) {
  60. dbgprintf("BoxLayout: Subtracting for fixed %s{%p}, size: %s\n", entry.widget->class_name(), entry.widget.ptr(), entry.widget->preferred_size().to_string().characters());
  61. dbgprintf("BoxLayout: Available size before: %s\n", available_size.to_string().characters());
  62. }
  63. available_size -= entry.widget->preferred_size();
  64. if (should_log)
  65. dbgprintf("BoxLayout: Available size after: %s\n", available_size.to_string().characters());
  66. ++number_of_entries_with_fixed_size;
  67. }
  68. available_size -= { spacing(), spacing() };
  69. }
  70. available_size += { spacing(), spacing() };
  71. available_size -= { margins().left() + margins().right(), margins().top() + margins().bottom() };
  72. if (should_log)
  73. dbgprintf("BoxLayout: Number of visible: %d/%d\n", number_of_visible_entries, m_entries.size());
  74. int number_of_entries_with_automatic_size = number_of_visible_entries - number_of_entries_with_fixed_size;
  75. if (should_log)
  76. 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);
  77. Gfx::Size automatic_size;
  78. if (number_of_entries_with_automatic_size) {
  79. if (m_orientation == Orientation::Horizontal) {
  80. automatic_size.set_width(available_size.width() / number_of_entries_with_automatic_size);
  81. automatic_size.set_height(widget.height());
  82. } else {
  83. automatic_size.set_width(widget.width());
  84. automatic_size.set_height(available_size.height() / number_of_entries_with_automatic_size);
  85. }
  86. }
  87. if (should_log)
  88. dbgprintf("BoxLayout: automatic_size=%s\n", automatic_size.to_string().characters());
  89. int current_x = margins().left();
  90. int current_y = margins().top();
  91. for (auto& entry : m_entries) {
  92. if (entry.type == Entry::Type::Spacer) {
  93. current_x += automatic_size.width();
  94. current_y += automatic_size.height();
  95. }
  96. if (!entry.widget)
  97. continue;
  98. if (!entry.widget->is_visible())
  99. continue;
  100. Gfx::Rect rect(current_x, current_y, 0, 0);
  101. if (entry.layout) {
  102. // FIXME: Implement recursive layout.
  103. ASSERT_NOT_REACHED();
  104. }
  105. ASSERT(entry.widget);
  106. rect.set_size(automatic_size);
  107. if (entry.widget->size_policy(Orientation::Vertical) == SizePolicy::Fixed)
  108. rect.set_height(entry.widget->preferred_size().height());
  109. if (entry.widget->size_policy(Orientation::Horizontal) == SizePolicy::Fixed)
  110. rect.set_width(entry.widget->preferred_size().width());
  111. if (orientation() == Orientation::Horizontal) {
  112. if (entry.widget->size_policy(Orientation::Vertical) == SizePolicy::Fill)
  113. rect.set_height(widget.height() - margins().top() - margins().bottom());
  114. rect.center_vertically_within(widget.rect());
  115. } else {
  116. if (entry.widget->size_policy(Orientation::Horizontal) == SizePolicy::Fill)
  117. rect.set_width(widget.width() - margins().left() - margins().right());
  118. rect.center_horizontally_within(widget.rect());
  119. }
  120. if (should_log)
  121. dbgprintf("BoxLayout: apply, %s{%p} <- %s\n", entry.widget->class_name(), entry.widget.ptr(), rect.to_string().characters());
  122. entry.widget->set_relative_rect(rect);
  123. if (orientation() == Orientation::Horizontal)
  124. current_x += rect.width() + spacing();
  125. else
  126. current_y += rect.height() + spacing();
  127. }
  128. }
  129. }