GBoxLayout.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include <LibGUI/GBoxLayout.h>
  2. #include <LibGUI/GWidget.h>
  3. //#define GBOXLAYOUT_DEBUG
  4. GBoxLayout::GBoxLayout(Orientation orientation)
  5. : m_orientation(orientation)
  6. {
  7. }
  8. GBoxLayout::~GBoxLayout()
  9. {
  10. }
  11. #if 0
  12. Size GLayout::compute_preferred_size() const
  13. {
  14. }
  15. static Size compute_preferred_size(GLayout::Entry& entry)
  16. {
  17. if (entry.layout)
  18. return entry.layout->compute_preferred_size();
  19. else {
  20. return entry.widget->preferred_size();
  21. }
  22. }
  23. #endif
  24. void GBoxLayout::run(GWidget& widget)
  25. {
  26. if (m_entries.is_empty())
  27. return;
  28. Size available_size = widget.size();
  29. int number_of_entries_with_fixed_size = 0;
  30. for (auto& entry : m_entries) {
  31. if (entry.widget && entry.widget->size_policy(orientation()) == SizePolicy::Fixed) {
  32. available_size -= entry.widget->preferred_size();
  33. ++number_of_entries_with_fixed_size;
  34. }
  35. }
  36. int number_of_entries_with_automatic_size = m_entries.size() - number_of_entries_with_fixed_size;
  37. #ifdef GBOXLAYOUT_DEBUG
  38. dbgprintf("GBoxLayout: available_size=%d, fixed=%d, fill=%d\n", available_size.height(), number_of_entries_with_fixed_size, number_of_entries_with_automatic_size);
  39. #endif
  40. Size automatic_size;
  41. if (number_of_entries_with_automatic_size) {
  42. if (m_orientation == Orientation::Horizontal) {
  43. automatic_size.set_width(available_size.width() / number_of_entries_with_automatic_size);
  44. automatic_size.set_height(widget.height());
  45. } else {
  46. automatic_size.set_width(widget.width());
  47. automatic_size.set_height(available_size.height() / number_of_entries_with_automatic_size);
  48. }
  49. }
  50. #ifdef GBOXLAYOUT_DEBUG
  51. dbgprintf("GBoxLayout: automatic_size=%s\n", automatic_size.to_string().characters());
  52. #endif
  53. // FIXME: We should also respect the bottom and right margins.
  54. int current_x = margins().left();
  55. int current_y = margins().top();
  56. for (auto& entry : m_entries) {
  57. Rect rect(current_x, current_y, 0, 0);
  58. if (entry.layout) {
  59. // FIXME: Implement recursive layout.
  60. ASSERT_NOT_REACHED();
  61. }
  62. ASSERT(entry.widget);
  63. rect.set_size(automatic_size);
  64. if (entry.widget->size_policy(Orientation::Vertical) == SizePolicy::Fixed)
  65. rect.set_height(entry.widget->preferred_size().height());
  66. if (entry.widget->size_policy(Orientation::Horizontal) == SizePolicy::Fixed)
  67. rect.set_width(entry.widget->preferred_size().height());
  68. #ifdef GBOXLAYOUT_DEBUG
  69. dbgprintf("GBoxLayout: apply, %s{%p} <- %s\n", entry.widget->class_name(), entry.widget.ptr(), rect.to_string().characters());
  70. #endif
  71. entry.widget->set_relative_rect(rect);
  72. if (orientation() == Orientation::Horizontal)
  73. current_x += rect.width() + spacing();
  74. else
  75. current_y += rect.height() + spacing();
  76. }
  77. }