2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
|
*
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-03-05 09:21:46 +01:00
|
|
|
#include <AK/JsonObject.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/BoxLayout.h>
|
2020-02-16 09:17:49 +01:00
|
|
|
#include <LibGUI/Widget.h>
|
|
|
|
|
#include <LibGfx/Orientation.h>
|
2019-03-15 23:24:40 +01:00
|
|
|
#include <stdio.h>
|
2019-02-10 11:07:13 +01:00
|
|
|
|
2019-03-15 16:23:12 +01:00
|
|
|
//#define GBOXLAYOUT_DEBUG
|
2019-02-11 08:27:13 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
|
|
|
|
|
|
|
|
|
BoxLayout::BoxLayout(Orientation orientation)
|
2019-02-10 11:07:13 +01:00
|
|
|
: m_orientation(orientation)
|
|
|
|
|
{
|
2020-09-15 21:33:37 +02:00
|
|
|
register_property(
|
|
|
|
|
"orientation", [this] { return m_orientation == Gfx::Orientation::Vertical ? "Vertical" : "Horizontal"; }, nullptr);
|
2019-02-10 11:07:13 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void BoxLayout::run(Widget& widget)
|
2019-02-10 11:07:13 +01:00
|
|
|
{
|
|
|
|
|
if (m_entries.is_empty())
|
|
|
|
|
return;
|
|
|
|
|
|
2020-12-30 01:23:32 +01:00
|
|
|
struct Item {
|
|
|
|
|
Widget* widget { nullptr };
|
|
|
|
|
int min_size { -1 };
|
|
|
|
|
int max_size { -1 };
|
|
|
|
|
int size { 0 };
|
|
|
|
|
bool final { false };
|
|
|
|
|
};
|
2019-03-15 16:12:06 +01:00
|
|
|
|
2020-12-30 01:23:32 +01:00
|
|
|
Vector<Item, 32> items;
|
2019-03-15 23:24:40 +01:00
|
|
|
|
2020-02-25 14:49:47 +01:00
|
|
|
for (size_t i = 0; i < m_entries.size(); ++i) {
|
2020-02-12 14:03:15 +01:00
|
|
|
auto& entry = m_entries[i];
|
2019-05-09 03:06:20 +02:00
|
|
|
if (entry.type == Entry::Type::Spacer) {
|
2020-12-30 01:23:32 +01:00
|
|
|
items.append(Item { nullptr, -1, -1 });
|
|
|
|
|
continue;
|
2019-05-09 03:06:20 +02:00
|
|
|
}
|
2019-04-04 01:44:35 +02:00
|
|
|
if (!entry.widget)
|
|
|
|
|
continue;
|
2019-03-15 16:12:06 +01:00
|
|
|
if (!entry.widget->is_visible())
|
|
|
|
|
continue;
|
2020-12-30 01:23:32 +01:00
|
|
|
auto min_size = entry.widget->min_size();
|
|
|
|
|
auto max_size = entry.widget->max_size();
|
|
|
|
|
items.append(Item { entry.widget.ptr(), min_size.primary_size_for_orientation(orientation()), max_size.primary_size_for_orientation(orientation()) });
|
2019-02-10 11:07:13 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-30 01:23:32 +01:00
|
|
|
if (items.is_empty())
|
|
|
|
|
return;
|
2019-03-15 23:24:40 +01:00
|
|
|
|
2020-12-30 01:23:32 +01:00
|
|
|
int available_size = widget.size().primary_size_for_orientation(orientation()) - spacing() * (items.size() - 1);
|
|
|
|
|
int unfinished_items = items.size();
|
|
|
|
|
|
|
|
|
|
if (orientation() == Gfx::Orientation::Horizontal)
|
|
|
|
|
available_size -= margins().left() + margins().right();
|
|
|
|
|
else
|
|
|
|
|
available_size -= margins().top() + margins().bottom();
|
|
|
|
|
|
|
|
|
|
// Pass 1: Set all items to their minimum size.
|
|
|
|
|
for (auto& item : items) {
|
|
|
|
|
item.size = 0;
|
|
|
|
|
if (item.min_size >= 0)
|
|
|
|
|
item.size = item.min_size;
|
|
|
|
|
available_size -= item.size;
|
|
|
|
|
|
|
|
|
|
if (item.min_size >= 0 && item.max_size >= 0 && item.min_size == item.max_size) {
|
|
|
|
|
// Fixed-size items finish immediately in the first pass.
|
|
|
|
|
item.final = true;
|
|
|
|
|
--unfinished_items;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-10 11:07:13 +01:00
|
|
|
|
2020-12-30 01:23:32 +01:00
|
|
|
// Pass 2: Distribute remaining available size evenly, respecting each item's maximum size.
|
|
|
|
|
while (unfinished_items && available_size > 0) {
|
|
|
|
|
int slice = available_size / unfinished_items;
|
|
|
|
|
available_size = 0;
|
2019-02-10 11:07:13 +01:00
|
|
|
|
2020-12-30 01:23:32 +01:00
|
|
|
for (auto& item : items) {
|
|
|
|
|
if (item.final)
|
|
|
|
|
continue;
|
2020-12-29 18:22:51 +01:00
|
|
|
|
2020-12-30 01:23:32 +01:00
|
|
|
int item_size_with_full_slice = item.size + slice;
|
|
|
|
|
item.size = item_size_with_full_slice;
|
2019-02-10 11:07:13 +01:00
|
|
|
|
2020-12-30 01:23:32 +01:00
|
|
|
if (item.max_size >= 0)
|
|
|
|
|
item.size = min(item.max_size, item_size_with_full_slice);
|
2020-02-12 14:03:15 +01:00
|
|
|
|
2020-12-30 01:23:32 +01:00
|
|
|
// If the slice was more than we needed, return remained to available_size.
|
|
|
|
|
int remainder_to_give_back = item_size_with_full_slice - item.size;
|
|
|
|
|
available_size += remainder_to_give_back;
|
2020-02-12 14:03:15 +01:00
|
|
|
|
2020-12-30 01:23:32 +01:00
|
|
|
if (item.max_size >= 0 && item.size == item.max_size) {
|
|
|
|
|
// We've hit the item's max size. Don't give it any more space.
|
|
|
|
|
item.final = true;
|
|
|
|
|
--unfinished_items;
|
|
|
|
|
}
|
2019-02-20 02:39:46 +01:00
|
|
|
}
|
2019-02-10 11:07:13 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-30 01:23:32 +01:00
|
|
|
// Pass 3: Place the widgets.
|
2019-02-20 09:04:28 +01:00
|
|
|
int current_x = margins().left();
|
|
|
|
|
int current_y = margins().top();
|
|
|
|
|
|
2020-12-30 01:23:32 +01:00
|
|
|
for (auto& item : items) {
|
|
|
|
|
Gfx::IntRect rect { current_x, current_y, 0, 0 };
|
2019-05-09 03:06:20 +02:00
|
|
|
|
2020-12-30 01:23:32 +01:00
|
|
|
rect.set_primary_size_for_orientation(orientation(), item.size);
|
2019-03-09 21:09:29 +01:00
|
|
|
|
2020-12-30 01:23:32 +01:00
|
|
|
if (item.widget) {
|
|
|
|
|
int secondary = widget.size().secondary_size_for_orientation(orientation());
|
|
|
|
|
if (orientation() == Gfx::Orientation::Horizontal)
|
|
|
|
|
secondary -= margins().top() + margins().bottom();
|
2020-12-29 18:22:51 +01:00
|
|
|
else
|
2020-12-30 01:23:32 +01:00
|
|
|
secondary -= margins().left() + margins().right();
|
2019-03-07 23:01:36 +01:00
|
|
|
|
2020-12-30 01:23:32 +01:00
|
|
|
int min_secondary = item.widget->min_size().secondary_size_for_orientation(orientation());
|
|
|
|
|
int max_secondary = item.widget->max_size().secondary_size_for_orientation(orientation());
|
|
|
|
|
if (min_secondary >= 0)
|
|
|
|
|
secondary = max(secondary, min_secondary);
|
|
|
|
|
if (max_secondary >= 0)
|
|
|
|
|
secondary = min(secondary, max_secondary);
|
2019-02-10 11:07:13 +01:00
|
|
|
|
2020-12-30 01:23:32 +01:00
|
|
|
rect.set_secondary_size_for_orientation(orientation(), secondary);
|
2020-12-29 18:22:51 +01:00
|
|
|
|
2020-12-30 01:23:32 +01:00
|
|
|
if (orientation() == Gfx::Orientation::Horizontal)
|
|
|
|
|
rect.center_vertically_within(widget.rect());
|
|
|
|
|
else
|
|
|
|
|
rect.center_horizontally_within(widget.rect());
|
2020-12-29 18:22:51 +01:00
|
|
|
|
2020-12-30 01:23:32 +01:00
|
|
|
item.widget->set_relative_rect(rect);
|
|
|
|
|
}
|
2019-02-10 11:07:13 +01:00
|
|
|
|
2020-12-30 01:23:32 +01:00
|
|
|
if (orientation() == Gfx::Orientation::Horizontal)
|
2019-02-20 09:04:28 +01:00
|
|
|
current_x += rect.width() + spacing();
|
2020-12-30 01:23:32 +01:00
|
|
|
else
|
2019-02-20 09:04:28 +01:00
|
|
|
current_y += rect.height() + spacing();
|
2019-02-10 11:07:13 +01:00
|
|
|
}
|
|
|
|
|
}
|
2020-12-30 01:23:32 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
}
|