2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2021-04-17 19:22:07 +03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2020-12-09 14:52:38 +03:00
|
|
|
#include <AK/String.h>
|
|
|
|
#include <AK/StringBuilder.h>
|
2021-04-17 19:22:07 +03:00
|
|
|
#include <LibCore/EventLoop.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/Action.h>
|
|
|
|
#include <LibGUI/ActionGroup.h>
|
2021-04-17 19:22:07 +03:00
|
|
|
#include <LibGUI/Application.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/BoxLayout.h>
|
|
|
|
#include <LibGUI/Button.h>
|
|
|
|
#include <LibGUI/Painter.h>
|
2020-12-30 05:52:27 +03:00
|
|
|
#include <LibGUI/SeparatorWidget.h>
|
2021-04-13 17:18:20 +03:00
|
|
|
#include <LibGUI/Toolbar.h>
|
2020-09-18 10:49:51 +03:00
|
|
|
#include <LibGfx/Palette.h>
|
2019-02-20 04:39:46 +03:00
|
|
|
|
2021-04-13 17:18:20 +03:00
|
|
|
REGISTER_WIDGET(GUI, Toolbar)
|
2021-01-03 02:30:13 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
namespace GUI {
|
|
|
|
|
2021-04-13 17:18:20 +03:00
|
|
|
Toolbar::Toolbar(Orientation orientation, int button_size)
|
2020-12-30 05:52:27 +03:00
|
|
|
: m_orientation(orientation)
|
|
|
|
, m_button_size(button_size)
|
2019-02-20 04:39:46 +03:00
|
|
|
{
|
2020-12-30 05:52:27 +03:00
|
|
|
if (m_orientation == Orientation::Horizontal) {
|
2020-12-30 03:23:32 +03:00
|
|
|
set_fixed_height(button_size + 8);
|
2019-11-08 23:09:34 +03:00
|
|
|
} else {
|
2020-12-30 03:23:32 +03:00
|
|
|
set_fixed_width(button_size + 8);
|
2019-11-08 23:09:34 +03:00
|
|
|
}
|
2020-03-05 11:21:46 +03:00
|
|
|
set_layout<BoxLayout>(orientation);
|
2019-02-20 13:56:28 +03:00
|
|
|
layout()->set_spacing(0);
|
2019-03-27 22:48:23 +03:00
|
|
|
layout()->set_margins({ 2, 2, 2, 2 });
|
2019-02-20 04:39:46 +03:00
|
|
|
}
|
|
|
|
|
2021-04-13 17:18:20 +03:00
|
|
|
Toolbar::~Toolbar()
|
2019-02-20 04:39:46 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-04-13 17:18:20 +03:00
|
|
|
class ToolbarButton final : public Button {
|
|
|
|
C_OBJECT(ToolbarButton);
|
2020-10-30 18:47:00 +03:00
|
|
|
|
|
|
|
public:
|
2021-04-13 17:18:20 +03:00
|
|
|
virtual ~ToolbarButton() override { }
|
2020-10-30 18:47:00 +03:00
|
|
|
|
|
|
|
private:
|
2021-04-13 17:18:20 +03:00
|
|
|
explicit ToolbarButton(Action& action)
|
2020-10-30 18:47:00 +03:00
|
|
|
{
|
|
|
|
if (action.group() && action.group()->is_exclusive())
|
|
|
|
set_exclusive(true);
|
|
|
|
set_action(action);
|
2020-12-09 14:52:38 +03:00
|
|
|
set_tooltip(tooltip(action));
|
2021-10-21 18:56:23 +03:00
|
|
|
set_focus_policy(FocusPolicy::NoFocus);
|
2020-10-30 18:47:00 +03:00
|
|
|
if (action.icon())
|
|
|
|
set_icon(action.icon());
|
|
|
|
else
|
|
|
|
set_text(action.text());
|
2021-04-13 17:18:20 +03:00
|
|
|
set_button_style(Gfx::ButtonStyle::Coolbar);
|
2020-10-30 18:47:00 +03:00
|
|
|
}
|
2020-12-09 14:52:38 +03:00
|
|
|
String tooltip(const Action& action) const
|
|
|
|
{
|
|
|
|
StringBuilder builder;
|
|
|
|
builder.append(action.text());
|
|
|
|
if (action.shortcut().is_valid()) {
|
|
|
|
builder.append(" (");
|
|
|
|
builder.append(action.shortcut().to_string());
|
|
|
|
builder.append(")");
|
|
|
|
}
|
|
|
|
return builder.to_string();
|
|
|
|
}
|
2021-04-17 19:22:07 +03:00
|
|
|
|
|
|
|
virtual void enter_event(Core::Event& event) override
|
|
|
|
{
|
|
|
|
auto* app = Application::the();
|
|
|
|
if (app && action())
|
|
|
|
Core::EventLoop::current().post_event(*app, make<ActionEvent>(ActionEvent::Type::ActionEnter, *action()));
|
|
|
|
return Button::enter_event(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void leave_event(Core::Event& event) override
|
|
|
|
{
|
|
|
|
auto* app = Application::the();
|
|
|
|
if (app && action())
|
|
|
|
Core::EventLoop::current().post_event(*app, make<ActionEvent>(ActionEvent::Type::ActionLeave, *action()));
|
|
|
|
return Button::leave_event(event);
|
|
|
|
}
|
2020-10-30 18:47:00 +03:00
|
|
|
};
|
|
|
|
|
2021-11-24 15:18:52 +03:00
|
|
|
ErrorOr<NonnullRefPtr<GUI::Button>> Toolbar::try_add_action(Action& action)
|
2019-02-20 04:39:46 +03:00
|
|
|
{
|
2021-11-24 15:18:52 +03:00
|
|
|
auto item = TRY(adopt_nonnull_own_or_enomem(new (nothrow) Item));
|
2020-02-02 17:07:41 +03:00
|
|
|
item->type = Item::Type::Action;
|
2019-07-11 16:55:54 +03:00
|
|
|
item->action = action;
|
2019-02-20 04:39:46 +03:00
|
|
|
|
2021-11-24 15:18:52 +03:00
|
|
|
// NOTE: Grow the m_items capacity before potentially adding a child widget.
|
|
|
|
// This avoids having to untangle the child widget in case of allocation failure.
|
|
|
|
TRY(m_items.try_ensure_capacity(m_items.size() + 1));
|
2019-02-20 04:39:46 +03:00
|
|
|
|
2021-11-24 15:18:52 +03:00
|
|
|
auto button = TRY(try_add<ToolbarButton>(action));
|
|
|
|
button->set_fixed_size(m_button_size + 8, m_button_size + 8);
|
|
|
|
|
|
|
|
m_items.unchecked_append(move(item));
|
2021-05-15 23:23:13 +03:00
|
|
|
return button;
|
2019-02-20 04:39:46 +03:00
|
|
|
}
|
|
|
|
|
2021-11-24 15:18:52 +03:00
|
|
|
GUI::Button& Toolbar::add_action(Action& action)
|
|
|
|
{
|
|
|
|
auto button = MUST(try_add_action(action));
|
|
|
|
return *button;
|
|
|
|
}
|
|
|
|
|
2021-11-25 00:23:59 +03:00
|
|
|
ErrorOr<void> Toolbar::try_add_separator()
|
2019-02-20 04:39:46 +03:00
|
|
|
{
|
2021-11-25 00:23:59 +03:00
|
|
|
// NOTE: Grow the m_items capacity before potentially adding a child widget.
|
|
|
|
TRY(m_items.try_ensure_capacity(m_items.size() + 1));
|
|
|
|
|
|
|
|
auto item = TRY(adopt_nonnull_own_or_enomem(new (nothrow) Item));
|
2020-02-02 17:07:41 +03:00
|
|
|
item->type = Item::Type::Separator;
|
2021-12-02 15:53:02 +03:00
|
|
|
(void)TRY(try_add<SeparatorWidget>(m_orientation == Gfx::Orientation::Horizontal ? Gfx::Orientation::Vertical : Gfx::Orientation::Horizontal));
|
2021-11-25 00:23:59 +03:00
|
|
|
m_items.unchecked_append(move(item));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
void Toolbar::add_separator()
|
|
|
|
{
|
|
|
|
MUST(try_add_separator());
|
2019-02-20 04:39:46 +03:00
|
|
|
}
|
|
|
|
|
2021-04-13 17:18:20 +03:00
|
|
|
void Toolbar::paint_event(PaintEvent& event)
|
2019-02-20 04:39:46 +03:00
|
|
|
{
|
2020-02-02 17:07:41 +03:00
|
|
|
Painter painter(*this);
|
2019-03-29 17:01:54 +03:00
|
|
|
painter.add_clip_rect(event.rect());
|
2019-05-10 23:58:52 +03:00
|
|
|
|
2020-09-18 10:49:51 +03:00
|
|
|
painter.fill_rect(event.rect(), palette().button());
|
2019-02-20 04:39:46 +03:00
|
|
|
}
|
2020-02-02 17:07:41 +03:00
|
|
|
|
|
|
|
}
|