summaryrefslogtreecommitdiff
path: root/LibGUI/GBoxLayout.cpp
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-02-10 11:07:13 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-02-10 11:07:13 +0100
commit2def3d8d3ff3c913919cd64b2bf75b77ee57d1cf (patch)
treeb4e92858629468605876ef0e9c48e9391019843e /LibGUI/GBoxLayout.cpp
parent2cf1dd5b6f10b898f24203c879290d665531a989 (diff)
downloadserenity-2def3d8d3ff3c913919cd64b2bf75b77ee57d1cf.zip
LibGUI: Start adding an automatic widget layout system.
My needs are really quite simple, so I'm just going to add what I need as I go along. The first thing I needed was a simple box layout with widgets being able to say whether they prefer fixed or fill for both their vertical and horizontal sizes. I also made a simple GStatusBar so FileManager can show how many bytes worth of files are in the current directory.
Diffstat (limited to 'LibGUI/GBoxLayout.cpp')
-rw-r--r--LibGUI/GBoxLayout.cpp89
1 files changed, 89 insertions, 0 deletions
diff --git a/LibGUI/GBoxLayout.cpp b/LibGUI/GBoxLayout.cpp
new file mode 100644
index 0000000000..bc9872bf2d
--- /dev/null
+++ b/LibGUI/GBoxLayout.cpp
@@ -0,0 +1,89 @@
+#include <LibGUI/GBoxLayout.h>
+#include <LibGUI/GWidget.h>
+
+GBoxLayout::GBoxLayout(Orientation orientation)
+ : m_orientation(orientation)
+{
+}
+
+GBoxLayout::~GBoxLayout()
+{
+}
+
+#if 0
+Size GLayout::compute_preferred_size() const
+{
+
+}
+
+
+static Size compute_preferred_size(GLayout::Entry& entry)
+{
+ if (entry.layout)
+ return entry.layout->compute_preferred_size();
+ else {
+ return entry.widget->preferred_size();
+ }
+}
+#endif
+
+void GBoxLayout::run(GWidget& widget)
+{
+ if (m_entries.is_empty())
+ return;
+
+ Size available_size = widget.size();
+ int number_of_entries_with_fixed_size = 0;
+
+ for (auto& entry : m_entries) {
+ if (entry.widget && entry.widget->size_policy(orientation()) == SizePolicy::Fixed) {
+ available_size -= entry.widget->preferred_size();
+ ++number_of_entries_with_fixed_size;
+ }
+ }
+
+ int number_of_entries_with_automatic_size = m_entries.size() - number_of_entries_with_fixed_size;
+
+ 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);
+
+ Size 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);
+ }
+
+ dbgprintf("GBoxLayout: automatic_size=%s\n", automatic_size.to_string().characters());
+
+ int current_x = 0;
+ int current_y = 0;
+ for (auto& entry : m_entries) {
+ Rect rect(current_x, current_y, 0, 0);
+ if (entry.layout) {
+ // FIXME: Implement recursive layout.
+ ASSERT_NOT_REACHED();
+ }
+ ASSERT(entry.widget);
+ if (entry.widget->size_policy(orientation()) == SizePolicy::Fixed) {
+ rect.set_size(automatic_size);
+ if (orientation() == Orientation::Vertical) {
+ rect.set_height(entry.widget->preferred_size().height());
+ } else {
+ rect.set_width(entry.widget->preferred_size().height());
+ }
+ } else {
+ rect.set_size(automatic_size);
+ }
+
+ dbgprintf("GBoxLayout: apply, %s{%p} <- %s\n", entry.widget->class_name(), entry.widget.ptr(), rect.to_string().characters());
+ entry.widget->set_relative_rect(rect);
+
+ if (orientation() == Orientation::Horizontal)
+ current_x += rect.width();
+ else
+ current_y += rect.height();
+ }
+}