GBoxLayout.cpp 2.6 KB

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