2019-02-10 10:07:13 +00:00
|
|
|
#include <LibGUI/GBoxLayout.h>
|
|
|
|
#include <LibGUI/GWidget.h>
|
2019-03-15 22:24:40 +00:00
|
|
|
#include <stdio.h>
|
2019-02-10 10:07:13 +00:00
|
|
|
|
2019-03-15 15:23:12 +00:00
|
|
|
//#define GBOXLAYOUT_DEBUG
|
2019-02-11 07:27:13 +00:00
|
|
|
|
2019-02-10 10:07:13 +00:00
|
|
|
GBoxLayout::GBoxLayout(Orientation orientation)
|
|
|
|
: m_orientation(orientation)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
GBoxLayout::~GBoxLayout()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void GBoxLayout::run(GWidget& widget)
|
|
|
|
{
|
2019-03-15 22:24:40 +00:00
|
|
|
bool should_log = false;
|
|
|
|
#ifdef GBOXLAYOUT_DEBUG
|
|
|
|
should_log = true;
|
|
|
|
#endif
|
|
|
|
if (should_log)
|
2019-03-25 03:24:20 +00:00
|
|
|
dbgprintf("GBoxLayout: running layout on %s{%p}, entry count: %d\n", widget.class_name(), &widget, m_entries.size());
|
2019-03-18 23:01:02 +00:00
|
|
|
|
2019-02-10 10:07:13 +00:00
|
|
|
if (m_entries.is_empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Size available_size = widget.size();
|
|
|
|
int number_of_entries_with_fixed_size = 0;
|
|
|
|
|
2019-03-15 15:12:06 +00:00
|
|
|
int number_of_visible_entries = 0;
|
|
|
|
|
2019-03-15 22:24:40 +00:00
|
|
|
if (should_log)
|
2019-03-25 03:24:20 +00:00
|
|
|
dbgprintf("GBoxLayout: Starting with available size: %s\n", available_size.to_string().characters());
|
2019-03-15 22:24:40 +00:00
|
|
|
|
2019-02-10 10:07:13 +00:00
|
|
|
for (auto& entry : m_entries) {
|
2019-05-09 01:06:20 +00:00
|
|
|
if (entry.type == Entry::Type::Spacer) {
|
|
|
|
++number_of_visible_entries;
|
|
|
|
}
|
2019-04-03 23:44:35 +00:00
|
|
|
if (!entry.widget)
|
|
|
|
continue;
|
|
|
|
|
2019-03-15 15:12:06 +00:00
|
|
|
if (!entry.widget->is_visible())
|
|
|
|
continue;
|
|
|
|
++number_of_visible_entries;
|
2019-02-10 10:07:13 +00:00
|
|
|
if (entry.widget && entry.widget->size_policy(orientation()) == SizePolicy::Fixed) {
|
2019-03-15 22:24:40 +00:00
|
|
|
if (should_log) {
|
2019-03-25 03:24:20 +00:00
|
|
|
dbgprintf("GBoxLayout: Subtracting for fixed %s{%p}, size: %s\n", entry.widget->class_name(), entry.widget.ptr(), entry.widget->preferred_size().to_string().characters());
|
|
|
|
dbgprintf("GBoxLayout: Available size before: %s\n", available_size.to_string().characters());
|
2019-03-15 22:24:40 +00:00
|
|
|
}
|
2019-02-10 10:07:13 +00:00
|
|
|
available_size -= entry.widget->preferred_size();
|
2019-03-15 22:24:40 +00:00
|
|
|
if (should_log)
|
2019-03-25 03:24:20 +00:00
|
|
|
dbgprintf("GBoxLayout: Available size after: %s\n", available_size.to_string().characters());
|
2019-02-10 10:07:13 +00:00
|
|
|
++number_of_entries_with_fixed_size;
|
|
|
|
}
|
2019-03-19 02:00:42 +00:00
|
|
|
available_size -= { spacing(), spacing() };
|
2019-02-10 10:07:13 +00:00
|
|
|
}
|
|
|
|
|
2019-03-19 02:00:42 +00:00
|
|
|
available_size += { spacing(), spacing() };
|
|
|
|
|
|
|
|
available_size -= { margins().left() + margins().right(), margins().top() + margins().bottom() };
|
|
|
|
|
2019-03-15 22:24:40 +00:00
|
|
|
if (should_log)
|
2019-03-25 03:24:20 +00:00
|
|
|
dbgprintf("GBoxLayout: Number of visible: %d/%d\n", number_of_visible_entries, m_entries.size());
|
2019-03-15 22:24:40 +00:00
|
|
|
|
2019-03-15 15:12:06 +00:00
|
|
|
int number_of_entries_with_automatic_size = number_of_visible_entries - number_of_entries_with_fixed_size;
|
2019-02-10 10:07:13 +00:00
|
|
|
|
2019-03-15 22:24:40 +00:00
|
|
|
if (should_log)
|
2019-03-25 03:24:20 +00:00
|
|
|
dbgprintf("GBoxLayout: 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);
|
2019-02-10 10:07:13 +00:00
|
|
|
|
|
|
|
Size automatic_size;
|
|
|
|
|
2019-02-20 01:39:46 +00:00
|
|
|
if (number_of_entries_with_automatic_size) {
|
|
|
|
if (m_orientation == Orientation::Horizontal) {
|
|
|
|
automatic_size.set_width(available_size.width() / number_of_entries_with_automatic_size);
|
|
|
|
automatic_size.set_height(widget.height());
|
|
|
|
} else {
|
|
|
|
automatic_size.set_width(widget.width());
|
|
|
|
automatic_size.set_height(available_size.height() / number_of_entries_with_automatic_size);
|
|
|
|
}
|
2019-02-10 10:07:13 +00:00
|
|
|
}
|
|
|
|
|
2019-03-15 22:24:40 +00:00
|
|
|
if (should_log)
|
2019-03-25 03:24:20 +00:00
|
|
|
dbgprintf("GBoxLayout: automatic_size=%s\n", automatic_size.to_string().characters());
|
2019-02-10 10:07:13 +00:00
|
|
|
|
2019-02-20 08:04:28 +00:00
|
|
|
int current_x = margins().left();
|
|
|
|
int current_y = margins().top();
|
|
|
|
|
|
|
|
for (auto& entry : m_entries) {
|
2019-05-09 01:06:20 +00:00
|
|
|
if (entry.type == Entry::Type::Spacer) {
|
|
|
|
current_x += automatic_size.width();
|
|
|
|
current_y += automatic_size.height();
|
|
|
|
}
|
|
|
|
|
2019-04-03 23:44:35 +00:00
|
|
|
if (!entry.widget)
|
|
|
|
continue;
|
2019-03-15 15:12:06 +00:00
|
|
|
if (!entry.widget->is_visible())
|
|
|
|
continue;
|
2019-02-10 10:07:13 +00:00
|
|
|
Rect rect(current_x, current_y, 0, 0);
|
|
|
|
if (entry.layout) {
|
|
|
|
// FIXME: Implement recursive layout.
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
ASSERT(entry.widget);
|
2019-03-25 03:24:20 +00:00
|
|
|
rect.set_size(automatic_size);
|
2019-03-09 20:09:29 +00:00
|
|
|
|
2019-02-20 08:04:28 +00:00
|
|
|
if (entry.widget->size_policy(Orientation::Vertical) == SizePolicy::Fixed)
|
|
|
|
rect.set_height(entry.widget->preferred_size().height());
|
2019-03-09 20:09:29 +00:00
|
|
|
|
2019-02-20 08:04:28 +00:00
|
|
|
if (entry.widget->size_policy(Orientation::Horizontal) == SizePolicy::Fixed)
|
2019-03-07 22:01:36 +00:00
|
|
|
rect.set_width(entry.widget->preferred_size().width());
|
|
|
|
|
2019-03-09 20:09:29 +00:00
|
|
|
if (orientation() == Orientation::Horizontal) {
|
|
|
|
if (entry.widget->size_policy(Orientation::Vertical) == SizePolicy::Fill)
|
|
|
|
rect.set_height(widget.height() - margins().top() - margins().bottom());
|
2019-03-07 22:01:36 +00:00
|
|
|
rect.center_vertically_within(widget.rect());
|
2019-03-09 20:09:29 +00:00
|
|
|
} else {
|
|
|
|
if (entry.widget->size_policy(Orientation::Horizontal) == SizePolicy::Fill)
|
|
|
|
rect.set_width(widget.width() - margins().left() - margins().right());
|
2019-03-07 22:01:36 +00:00
|
|
|
rect.center_horizontally_within(widget.rect());
|
2019-03-09 20:09:29 +00:00
|
|
|
}
|
2019-02-10 10:07:13 +00:00
|
|
|
|
2019-03-15 22:24:40 +00:00
|
|
|
if (should_log)
|
2019-03-25 03:24:20 +00:00
|
|
|
dbgprintf("GBoxLayout: apply, %s{%p} <- %s\n", entry.widget->class_name(), entry.widget.ptr(), rect.to_string().characters());
|
2019-02-10 10:07:13 +00:00
|
|
|
entry.widget->set_relative_rect(rect);
|
|
|
|
|
|
|
|
if (orientation() == Orientation::Horizontal)
|
2019-02-20 08:04:28 +00:00
|
|
|
current_x += rect.width() + spacing();
|
2019-02-10 10:07:13 +00:00
|
|
|
else
|
2019-02-20 08:04:28 +00:00
|
|
|
current_y += rect.height() + spacing();
|
2019-02-10 10:07:13 +00:00
|
|
|
}
|
|
|
|
}
|