2020-01-18 11:38:21 +03: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.
|
|
|
|
*/
|
|
|
|
|
2019-01-20 06:49:48 +03:00
|
|
|
#include <AK/Assertions.h>
|
2019-08-18 21:39:46 +03:00
|
|
|
#include <AK/JsonObject.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/Action.h>
|
|
|
|
#include <LibGUI/Application.h>
|
|
|
|
#include <LibGUI/Button.h>
|
|
|
|
#include <LibGUI/CheckBox.h>
|
|
|
|
#include <LibGUI/Event.h>
|
|
|
|
#include <LibGUI/GroupBox.h>
|
|
|
|
#include <LibGUI/Label.h>
|
|
|
|
#include <LibGUI/Layout.h>
|
|
|
|
#include <LibGUI/Menu.h>
|
|
|
|
#include <LibGUI/Painter.h>
|
|
|
|
#include <LibGUI/RadioButton.h>
|
|
|
|
#include <LibGUI/ScrollBar.h>
|
|
|
|
#include <LibGUI/Slider.h>
|
|
|
|
#include <LibGUI/SpinBox.h>
|
|
|
|
#include <LibGUI/TextBox.h>
|
|
|
|
#include <LibGUI/Widget.h>
|
|
|
|
#include <LibGUI/Window.h>
|
|
|
|
#include <LibGUI/WindowServerConnection.h>
|
2020-02-13 23:43:32 +03:00
|
|
|
#include <LibGfx/Bitmap.h>
|
2020-02-15 02:24:14 +03:00
|
|
|
#include <LibGfx/Font.h>
|
2020-02-13 23:43:32 +03:00
|
|
|
#include <LibGfx/Palette.h>
|
2019-12-24 22:57:54 +03:00
|
|
|
#include <unistd.h>
|
2019-11-10 12:58:03 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
namespace GUI {
|
|
|
|
|
|
|
|
REGISTER_GWIDGET(Button)
|
|
|
|
REGISTER_GWIDGET(CheckBox)
|
|
|
|
REGISTER_GWIDGET(GroupBox)
|
|
|
|
REGISTER_GWIDGET(Label)
|
|
|
|
REGISTER_GWIDGET(RadioButton)
|
|
|
|
REGISTER_GWIDGET(ScrollBar)
|
|
|
|
REGISTER_GWIDGET(Slider)
|
|
|
|
REGISTER_GWIDGET(SpinBox)
|
|
|
|
REGISTER_GWIDGET(TextBox)
|
|
|
|
REGISTER_GWIDGET(Widget)
|
|
|
|
|
|
|
|
static HashMap<String, WidgetClassRegistration*>& widget_classes()
|
|
|
|
{
|
|
|
|
static HashMap<String, WidgetClassRegistration*>* map;
|
2019-11-10 12:58:03 +03:00
|
|
|
if (!map)
|
2020-02-02 17:07:41 +03:00
|
|
|
map = new HashMap<String, WidgetClassRegistration*>;
|
2019-11-10 12:58:03 +03:00
|
|
|
return *map;
|
|
|
|
}
|
|
|
|
|
2020-02-23 14:07:13 +03:00
|
|
|
WidgetClassRegistration::WidgetClassRegistration(const String& class_name, Function<NonnullRefPtr<Widget>()> factory)
|
2019-11-10 12:58:03 +03:00
|
|
|
: m_class_name(class_name)
|
|
|
|
, m_factory(move(factory))
|
|
|
|
{
|
|
|
|
widget_classes().set(class_name, this);
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
WidgetClassRegistration::~WidgetClassRegistration()
|
2019-11-10 12:58:03 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void WidgetClassRegistration::for_each(Function<void(const WidgetClassRegistration&)> callback)
|
2019-11-10 12:58:03 +03:00
|
|
|
{
|
|
|
|
for (auto& it : widget_classes()) {
|
|
|
|
callback(*it.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
const WidgetClassRegistration* WidgetClassRegistration::find(const String& class_name)
|
2019-11-10 12:58:03 +03:00
|
|
|
{
|
|
|
|
return widget_classes().get(class_name).value_or(nullptr);
|
|
|
|
}
|
|
|
|
|
2020-02-23 14:07:13 +03:00
|
|
|
Widget::Widget()
|
|
|
|
: Core::Object(nullptr, true)
|
2020-02-15 01:53:11 +03:00
|
|
|
, m_background_role(Gfx::ColorRole::Window)
|
|
|
|
, m_foreground_role(Gfx::ColorRole::WindowText)
|
2020-02-06 13:56:38 +03:00
|
|
|
, m_font(Gfx::Font::default_font())
|
2020-02-02 17:07:41 +03:00
|
|
|
, m_palette(Application::the().palette().impl())
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
Widget::~Widget()
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::child_event(Core::ChildEvent& event)
|
2019-03-16 01:24:40 +03:00
|
|
|
{
|
2020-02-02 17:07:41 +03:00
|
|
|
if (event.type() == Event::ChildAdded) {
|
|
|
|
if (event.child() && Core::is<Widget>(*event.child()) && layout()) {
|
2019-11-05 22:41:27 +03:00
|
|
|
if (event.insertion_before_child() && event.insertion_before_child()->is_widget())
|
2020-02-02 17:07:41 +03:00
|
|
|
layout()->insert_widget_before(Core::to<Widget>(*event.child()), Core::to<Widget>(*event.insertion_before_child()));
|
2019-11-05 22:41:27 +03:00
|
|
|
else
|
2020-02-02 17:07:41 +03:00
|
|
|
layout()->add_widget(Core::to<Widget>(*event.child()));
|
2019-11-05 22:41:27 +03:00
|
|
|
}
|
2019-03-16 01:24:40 +03:00
|
|
|
}
|
2020-02-02 17:07:41 +03:00
|
|
|
if (event.type() == Event::ChildRemoved) {
|
2019-04-06 22:15:13 +03:00
|
|
|
if (layout()) {
|
2020-02-02 17:07:41 +03:00
|
|
|
if (event.child() && Core::is<Widget>(*event.child()))
|
|
|
|
layout()->remove_widget(Core::to<Widget>(*event.child()));
|
2019-04-06 22:15:13 +03:00
|
|
|
else
|
|
|
|
invalidate_layout();
|
|
|
|
}
|
2019-04-19 00:25:30 +03:00
|
|
|
update();
|
2019-04-04 02:44:35 +03:00
|
|
|
}
|
2020-02-02 14:34:39 +03:00
|
|
|
return Core::Object::child_event(event);
|
2019-03-16 01:24:40 +03:00
|
|
|
}
|
|
|
|
|
2020-02-06 13:56:38 +03:00
|
|
|
void Widget::set_relative_rect(const Gfx::Rect& a_rect)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
2019-11-10 14:53:05 +03:00
|
|
|
// Get rid of negative width/height values.
|
2020-02-06 15:02:38 +03:00
|
|
|
Gfx::Rect rect = {
|
2019-11-10 14:53:05 +03:00
|
|
|
a_rect.x(),
|
|
|
|
a_rect.y(),
|
|
|
|
max(a_rect.width(), 0),
|
|
|
|
max(a_rect.height(), 0)
|
|
|
|
};
|
2019-10-23 20:51:09 +03:00
|
|
|
|
2019-01-25 01:40:12 +03:00
|
|
|
if (rect == m_relative_rect)
|
|
|
|
return;
|
2019-04-16 23:59:27 +03:00
|
|
|
|
|
|
|
auto old_rect = m_relative_rect;
|
|
|
|
|
2019-02-20 23:59:13 +03:00
|
|
|
bool size_changed = m_relative_rect.size() != rect.size();
|
2019-01-21 02:46:08 +03:00
|
|
|
m_relative_rect = rect;
|
2019-02-20 23:59:13 +03:00
|
|
|
|
|
|
|
if (size_changed) {
|
2020-02-02 17:07:41 +03:00
|
|
|
ResizeEvent resize_event(m_relative_rect.size(), rect.size());
|
2019-02-20 23:59:13 +03:00
|
|
|
event(resize_event);
|
|
|
|
}
|
|
|
|
|
2019-04-16 23:59:27 +03:00
|
|
|
if (auto* parent = parent_widget())
|
|
|
|
parent->update(old_rect);
|
2019-01-25 01:40:12 +03:00
|
|
|
update();
|
2019-01-20 06:49:48 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::event(Core::Event& event)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
|
|
|
switch (event.type()) {
|
2020-02-02 17:07:41 +03:00
|
|
|
case Event::Paint:
|
|
|
|
return handle_paint_event(static_cast<PaintEvent&>(event));
|
|
|
|
case Event::Resize:
|
|
|
|
return handle_resize_event(static_cast<ResizeEvent&>(event));
|
|
|
|
case Event::FocusIn:
|
2019-01-26 13:24:16 +03:00
|
|
|
return focusin_event(event);
|
2020-02-02 17:07:41 +03:00
|
|
|
case Event::FocusOut:
|
2019-02-04 12:34:56 +03:00
|
|
|
return focusout_event(event);
|
2020-02-02 17:07:41 +03:00
|
|
|
case Event::Show:
|
|
|
|
return show_event(static_cast<ShowEvent&>(event));
|
|
|
|
case Event::Hide:
|
|
|
|
return hide_event(static_cast<HideEvent&>(event));
|
|
|
|
case Event::KeyDown:
|
|
|
|
return keydown_event(static_cast<KeyEvent&>(event));
|
|
|
|
case Event::KeyUp:
|
|
|
|
return keyup_event(static_cast<KeyEvent&>(event));
|
|
|
|
case Event::MouseMove:
|
|
|
|
return mousemove_event(static_cast<MouseEvent&>(event));
|
|
|
|
case Event::MouseDown:
|
|
|
|
return handle_mousedown_event(static_cast<MouseEvent&>(event));
|
|
|
|
case Event::MouseDoubleClick:
|
|
|
|
return handle_mousedoubleclick_event(static_cast<MouseEvent&>(event));
|
|
|
|
case Event::MouseUp:
|
|
|
|
return handle_mouseup_event(static_cast<MouseEvent&>(event));
|
|
|
|
case Event::MouseWheel:
|
|
|
|
return mousewheel_event(static_cast<MouseEvent&>(event));
|
2020-02-13 23:43:32 +03:00
|
|
|
case Event::DragMove:
|
|
|
|
return drag_move_event(static_cast<DragEvent&>(event));
|
2020-02-02 17:07:41 +03:00
|
|
|
case Event::Drop:
|
|
|
|
return drop_event(static_cast<DropEvent&>(event));
|
|
|
|
case Event::Enter:
|
2019-04-08 19:58:44 +03:00
|
|
|
return handle_enter_event(event);
|
2020-02-02 17:07:41 +03:00
|
|
|
case Event::Leave:
|
2019-04-08 19:58:44 +03:00
|
|
|
return handle_leave_event(event);
|
2020-02-02 17:07:41 +03:00
|
|
|
case Event::EnabledChange:
|
|
|
|
return change_event(static_cast<Event&>(event));
|
2019-01-20 06:49:48 +03:00
|
|
|
default:
|
2020-02-02 14:34:39 +03:00
|
|
|
return Core::Object::event(event);
|
2019-01-20 06:49:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::handle_paint_event(PaintEvent& event)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
2019-03-25 15:58:30 +03:00
|
|
|
ASSERT(is_visible());
|
2019-01-21 02:46:08 +03:00
|
|
|
if (fill_with_background_color()) {
|
2020-02-02 17:07:41 +03:00
|
|
|
Painter painter(*this);
|
2019-12-24 22:57:54 +03:00
|
|
|
painter.fill_rect(event.rect(), palette().color(background_role()));
|
2019-02-20 04:39:46 +03:00
|
|
|
} else {
|
|
|
|
#ifdef DEBUG_WIDGET_UNDERDRAW
|
|
|
|
// FIXME: This is a bit broken.
|
|
|
|
// If the widget is not opaque, let's not mess it up with debugging color.
|
2020-02-02 17:07:41 +03:00
|
|
|
Painter painter(*this);
|
2019-02-20 04:39:46 +03:00
|
|
|
painter.fill_rect(rect(), Color::Red);
|
|
|
|
#endif
|
2019-01-20 06:49:48 +03:00
|
|
|
}
|
2019-02-09 13:19:38 +03:00
|
|
|
paint_event(event);
|
2019-06-07 12:46:02 +03:00
|
|
|
for_each_child_widget([&](auto& child) {
|
2019-05-27 04:52:33 +03:00
|
|
|
if (!child.is_visible())
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
if (child.relative_rect().intersects(event.rect())) {
|
2020-02-02 17:07:41 +03:00
|
|
|
PaintEvent local_event(event.rect().intersected(child.relative_rect()).translated(-child.relative_position()));
|
2019-09-20 21:37:31 +03:00
|
|
|
child.dispatch_event(local_event, this);
|
2019-02-02 10:05:14 +03:00
|
|
|
}
|
2019-05-27 04:52:33 +03:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
2019-04-11 04:34:37 +03:00
|
|
|
second_paint_event(event);
|
2019-01-20 06:49:48 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::set_layout(OwnPtr<Layout>&& layout)
|
2019-02-10 13:07:13 +03:00
|
|
|
{
|
|
|
|
if (m_layout)
|
2019-05-31 16:44:04 +03:00
|
|
|
m_layout->notify_disowned({}, *this);
|
2019-02-10 13:07:13 +03:00
|
|
|
m_layout = move(layout);
|
|
|
|
if (m_layout) {
|
2019-05-31 16:44:04 +03:00
|
|
|
m_layout->notify_adopted({}, *this);
|
2019-02-10 13:07:13 +03:00
|
|
|
do_layout();
|
|
|
|
} else {
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::do_layout()
|
2019-02-10 13:07:13 +03:00
|
|
|
{
|
2019-10-26 13:27:01 +03:00
|
|
|
for_each_child_widget([&](auto& child) {
|
|
|
|
child.do_layout();
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
2019-09-01 21:51:20 +03:00
|
|
|
custom_layout();
|
2019-02-10 13:07:13 +03:00
|
|
|
if (!m_layout)
|
|
|
|
return;
|
|
|
|
m_layout->run(*this);
|
2020-02-11 13:26:25 +03:00
|
|
|
did_layout();
|
2019-02-10 13:07:13 +03:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::notify_layout_changed(Badge<Layout>)
|
2019-02-10 13:07:13 +03:00
|
|
|
{
|
2019-04-18 23:57:24 +03:00
|
|
|
invalidate_layout();
|
2019-02-10 13:07:13 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::handle_resize_event(ResizeEvent& event)
|
2019-02-10 13:07:13 +03:00
|
|
|
{
|
2019-12-30 02:26:19 +03:00
|
|
|
resize_event(event);
|
2019-09-01 21:51:20 +03:00
|
|
|
do_layout();
|
2019-02-10 13:07:13 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::handle_mouseup_event(MouseEvent& event)
|
2019-03-25 03:42:15 +03:00
|
|
|
{
|
|
|
|
mouseup_event(event);
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::handle_mousedown_event(MouseEvent& event)
|
2019-04-01 23:03:32 +03:00
|
|
|
{
|
|
|
|
if (accepts_focus())
|
|
|
|
set_focus(true);
|
|
|
|
mousedown_event(event);
|
2020-02-02 17:07:41 +03:00
|
|
|
if (event.button() == MouseButton::Right) {
|
|
|
|
ContextMenuEvent c_event(event.position(), screen_relative_rect().location().translated(event.position()));
|
2019-04-18 05:12:27 +03:00
|
|
|
context_menu_event(c_event);
|
|
|
|
}
|
2019-04-01 23:03:32 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::handle_mousedoubleclick_event(MouseEvent& event)
|
2019-05-15 23:17:09 +03:00
|
|
|
{
|
|
|
|
doubleclick_event(event);
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::handle_enter_event(Core::Event& event)
|
2019-04-08 19:58:44 +03:00
|
|
|
{
|
|
|
|
if (has_tooltip())
|
2020-02-02 17:07:41 +03:00
|
|
|
Application::the().show_tooltip(m_tooltip, screen_relative_rect().center().translated(0, height() / 2));
|
2019-04-08 19:58:44 +03:00
|
|
|
enter_event(event);
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::handle_leave_event(Core::Event& event)
|
2019-04-08 19:58:44 +03:00
|
|
|
{
|
2020-02-02 17:07:41 +03:00
|
|
|
Application::the().hide_tooltip();
|
2019-04-08 19:58:44 +03:00
|
|
|
leave_event(event);
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::click_event(MouseEvent&)
|
2019-03-25 03:42:15 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::doubleclick_event(MouseEvent&)
|
2019-03-25 03:42:15 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::resize_event(ResizeEvent&)
|
2019-02-09 13:19:38 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::paint_event(PaintEvent&)
|
2019-02-09 13:19:38 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::second_paint_event(PaintEvent&)
|
2019-04-11 04:34:37 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::show_event(ShowEvent&)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::hide_event(HideEvent&)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::keydown_event(KeyEvent& event)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
2019-05-15 03:39:58 +03:00
|
|
|
if (!event.alt() && !event.ctrl() && !event.logo() && event.key() == KeyCode::Key_Tab) {
|
|
|
|
if (event.shift())
|
|
|
|
focus_previous_widget();
|
|
|
|
else
|
|
|
|
focus_next_widget();
|
2019-09-20 21:37:31 +03:00
|
|
|
event.accept();
|
|
|
|
return;
|
2019-05-15 03:39:58 +03:00
|
|
|
}
|
2019-09-20 21:37:31 +03:00
|
|
|
event.ignore();
|
2019-01-20 06:49:48 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::keyup_event(KeyEvent&)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::mousedown_event(MouseEvent&)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::mouseup_event(MouseEvent&)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::mousemove_event(MouseEvent&)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::mousewheel_event(MouseEvent&)
|
2019-05-13 20:52:57 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::context_menu_event(ContextMenuEvent&)
|
2019-04-18 05:12:27 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::focusin_event(Core::Event&)
|
2019-01-26 13:24:16 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::focusout_event(Core::Event&)
|
2019-01-26 13:24:16 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::enter_event(Core::Event&)
|
2019-02-20 12:12:19 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::leave_event(Core::Event&)
|
2019-02-20 12:12:19 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::change_event(Event&)
|
2019-05-25 14:40:57 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-13 23:43:32 +03:00
|
|
|
void Widget::drag_move_event(DragEvent& event)
|
|
|
|
{
|
|
|
|
dbg() << class_name() << "{" << this << "} DRAG MOVE position: " << event.position() << ", data_type: '" << event.data_type() << "'";
|
|
|
|
event.ignore();
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::drop_event(DropEvent& event)
|
2019-12-08 18:50:23 +03:00
|
|
|
{
|
|
|
|
dbg() << class_name() << "{" << this << "} DROP position: " << event.position() << ", text: '" << event.text() << "'";
|
2019-12-19 22:07:58 +03:00
|
|
|
event.ignore();
|
2019-12-08 18:50:23 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::update()
|
2019-02-10 16:28:39 +03:00
|
|
|
{
|
2019-05-02 04:46:37 +03:00
|
|
|
if (rect().is_empty())
|
|
|
|
return;
|
2019-02-10 16:28:39 +03:00
|
|
|
update(rect());
|
|
|
|
}
|
|
|
|
|
2020-02-06 13:56:38 +03:00
|
|
|
void Widget::update(const Gfx::Rect& rect)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
2019-03-25 15:58:30 +03:00
|
|
|
if (!is_visible())
|
|
|
|
return;
|
2019-05-02 04:46:37 +03:00
|
|
|
|
2019-05-02 05:19:59 +03:00
|
|
|
if (!updates_enabled())
|
|
|
|
return;
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
Window* window = m_window;
|
|
|
|
Widget* parent = parent_widget();
|
2019-05-02 04:46:37 +03:00
|
|
|
while (parent) {
|
|
|
|
if (!parent->updates_enabled())
|
|
|
|
return;
|
|
|
|
window = parent->m_window;
|
|
|
|
parent = parent->parent_widget();
|
|
|
|
}
|
|
|
|
if (window)
|
|
|
|
window->update(rect.translated(window_relative_rect().location()));
|
2019-02-09 16:30:05 +03:00
|
|
|
}
|
|
|
|
|
2020-02-06 15:02:38 +03:00
|
|
|
Gfx::Rect Widget::window_relative_rect() const
|
2019-02-09 16:30:05 +03:00
|
|
|
{
|
|
|
|
auto rect = relative_rect();
|
|
|
|
for (auto* parent = parent_widget(); parent; parent = parent->parent_widget()) {
|
|
|
|
rect.move_by(parent->relative_position());
|
|
|
|
}
|
|
|
|
return rect;
|
2019-01-20 06:49:48 +03:00
|
|
|
}
|
|
|
|
|
2020-02-06 15:02:38 +03:00
|
|
|
Gfx::Rect Widget::screen_relative_rect() const
|
2019-04-08 19:58:44 +03:00
|
|
|
{
|
|
|
|
return window_relative_rect().translated(window()->position());
|
|
|
|
}
|
|
|
|
|
2020-02-06 13:56:38 +03:00
|
|
|
Widget* Widget::child_at(const Gfx::Point& point) const
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
2019-04-10 03:08:32 +03:00
|
|
|
for (int i = children().size() - 1; i >= 0; --i) {
|
2020-02-02 17:07:41 +03:00
|
|
|
if (!Core::is<Widget>(children()[i]))
|
2019-03-15 18:45:27 +03:00
|
|
|
continue;
|
2020-02-02 17:07:41 +03:00
|
|
|
auto& child = Core::to<Widget>(children()[i]);
|
2019-03-15 18:45:27 +03:00
|
|
|
if (!child.is_visible())
|
|
|
|
continue;
|
2019-04-16 04:47:55 +03:00
|
|
|
if (child.relative_rect().contains(point))
|
2020-02-02 17:07:41 +03:00
|
|
|
return const_cast<Widget*>(&child);
|
2019-01-20 06:49:48 +03:00
|
|
|
}
|
2019-04-16 04:47:55 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-02-06 13:56:38 +03:00
|
|
|
Widget::HitTestResult Widget::hit_test(const Gfx::Point& position, ShouldRespectGreediness should_respect_greediness)
|
2019-04-16 04:47:55 +03:00
|
|
|
{
|
2019-09-17 21:58:13 +03:00
|
|
|
if (should_respect_greediness == ShouldRespectGreediness::Yes && is_greedy_for_hits())
|
2019-04-16 14:25:00 +03:00
|
|
|
return { this, position };
|
|
|
|
if (auto* child = child_at(position))
|
|
|
|
return child->hit_test(position - child->relative_position());
|
|
|
|
return { this, position };
|
2019-01-20 06:49:48 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::set_window(Window* window)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
|
|
|
if (m_window == window)
|
|
|
|
return;
|
|
|
|
m_window = window;
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
bool Widget::is_focused() const
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
2019-01-26 13:24:16 +03:00
|
|
|
auto* win = window();
|
|
|
|
if (!win)
|
|
|
|
return false;
|
2019-01-26 23:58:43 +03:00
|
|
|
if (!win->is_active())
|
|
|
|
return false;
|
2019-01-26 13:24:16 +03:00
|
|
|
return win->focused_widget() == this;
|
2019-01-20 06:49:48 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::set_focus(bool focus)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
2019-01-26 13:24:16 +03:00
|
|
|
auto* win = window();
|
|
|
|
if (!win)
|
2019-01-20 06:49:48 +03:00
|
|
|
return;
|
2019-01-26 13:24:16 +03:00
|
|
|
if (focus) {
|
|
|
|
win->set_focused_widget(this);
|
|
|
|
} else {
|
|
|
|
if (win->focused_widget() == this)
|
|
|
|
win->set_focused_widget(nullptr);
|
|
|
|
}
|
2019-01-20 06:49:48 +03:00
|
|
|
}
|
|
|
|
|
2020-02-06 13:56:38 +03:00
|
|
|
void Widget::set_font(const Gfx::Font* font)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
2019-09-01 13:26:35 +03:00
|
|
|
if (m_font.ptr() == font)
|
|
|
|
return;
|
|
|
|
|
2019-01-20 06:49:48 +03:00
|
|
|
if (!font)
|
2020-02-06 13:56:38 +03:00
|
|
|
m_font = Gfx::Font::default_font();
|
2019-01-20 06:49:48 +03:00
|
|
|
else
|
2019-09-01 13:26:35 +03:00
|
|
|
m_font = *font;
|
|
|
|
|
|
|
|
did_change_font();
|
2019-02-12 12:08:35 +03:00
|
|
|
update();
|
2019-01-20 06:49:48 +03:00
|
|
|
}
|
2019-01-27 10:48:34 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::set_global_cursor_tracking(bool enabled)
|
2019-01-27 10:48:34 +03:00
|
|
|
{
|
|
|
|
auto* win = window();
|
|
|
|
if (!win)
|
|
|
|
return;
|
|
|
|
win->set_global_cursor_tracking_widget(enabled ? this : nullptr);
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
bool Widget::global_cursor_tracking() const
|
2019-01-27 10:48:34 +03:00
|
|
|
{
|
|
|
|
auto* win = window();
|
|
|
|
if (!win)
|
|
|
|
return false;
|
|
|
|
return win->global_cursor_tracking_widget() == this;
|
|
|
|
}
|
2019-02-10 13:07:13 +03:00
|
|
|
|
2020-02-06 13:56:38 +03:00
|
|
|
void Widget::set_preferred_size(const Gfx::Size& size)
|
2019-02-10 13:07:13 +03:00
|
|
|
{
|
|
|
|
if (m_preferred_size == size)
|
|
|
|
return;
|
|
|
|
m_preferred_size = size;
|
|
|
|
invalidate_layout();
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::set_size_policy(Orientation orientation, SizePolicy policy)
|
2019-07-27 10:34:28 +03:00
|
|
|
{
|
|
|
|
if (orientation == Orientation::Horizontal)
|
|
|
|
set_size_policy(policy, m_vertical_size_policy);
|
|
|
|
else
|
|
|
|
set_size_policy(m_horizontal_size_policy, policy);
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::set_size_policy(SizePolicy horizontal_policy, SizePolicy vertical_policy)
|
2019-02-10 13:07:13 +03:00
|
|
|
{
|
|
|
|
if (m_horizontal_size_policy == horizontal_policy && m_vertical_size_policy == vertical_policy)
|
|
|
|
return;
|
|
|
|
m_horizontal_size_policy = horizontal_policy;
|
|
|
|
m_vertical_size_policy = vertical_policy;
|
|
|
|
invalidate_layout();
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::invalidate_layout()
|
2019-02-10 13:07:13 +03:00
|
|
|
{
|
2019-10-26 13:27:01 +03:00
|
|
|
if (window())
|
|
|
|
window()->schedule_relayout();
|
2019-02-10 13:07:13 +03:00
|
|
|
}
|
2019-03-15 18:12:06 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::set_visible(bool visible)
|
2019-03-15 18:12:06 +03:00
|
|
|
{
|
|
|
|
if (visible == m_visible)
|
|
|
|
return;
|
|
|
|
m_visible = visible;
|
|
|
|
if (auto* parent = parent_widget())
|
|
|
|
parent->invalidate_layout();
|
|
|
|
if (m_visible)
|
|
|
|
update();
|
2019-10-02 21:24:03 +03:00
|
|
|
|
|
|
|
if (m_visible) {
|
2020-02-02 17:07:41 +03:00
|
|
|
ShowEvent e;
|
2019-10-02 21:24:03 +03:00
|
|
|
event(e);
|
|
|
|
} else {
|
2020-02-02 17:07:41 +03:00
|
|
|
HideEvent e;
|
2019-10-02 21:24:03 +03:00
|
|
|
event(e);
|
|
|
|
}
|
2019-03-15 18:12:06 +03:00
|
|
|
}
|
2019-03-29 04:20:22 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
bool Widget::spans_entire_window_horizontally() const
|
2019-03-29 04:20:22 +03:00
|
|
|
{
|
|
|
|
auto* w = window();
|
|
|
|
if (!w)
|
|
|
|
return false;
|
|
|
|
auto* main_widget = w->main_widget();
|
|
|
|
if (!main_widget)
|
|
|
|
return false;
|
|
|
|
if (main_widget == this)
|
|
|
|
return true;
|
|
|
|
auto wrr = window_relative_rect();
|
|
|
|
return wrr.left() == main_widget->rect().left() && wrr.right() == main_widget->rect().right();
|
|
|
|
}
|
2019-04-12 03:51:16 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::set_enabled(bool enabled)
|
2019-04-12 03:51:16 +03:00
|
|
|
{
|
|
|
|
if (m_enabled == enabled)
|
|
|
|
return;
|
|
|
|
m_enabled = enabled;
|
2020-02-02 17:07:41 +03:00
|
|
|
Event e(Event::EnabledChange);
|
2019-05-25 14:40:57 +03:00
|
|
|
event(e);
|
2019-04-12 03:51:16 +03:00
|
|
|
update();
|
|
|
|
}
|
2019-04-12 18:10:30 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::move_to_front()
|
2019-04-16 04:47:55 +03:00
|
|
|
{
|
|
|
|
auto* parent = parent_widget();
|
|
|
|
if (!parent)
|
|
|
|
return;
|
|
|
|
if (parent->children().size() == 1)
|
|
|
|
return;
|
2019-06-07 12:46:02 +03:00
|
|
|
parent->children().remove_first_matching([this](auto& entry) {
|
2019-04-16 05:01:14 +03:00
|
|
|
return entry == this;
|
|
|
|
});
|
2019-09-22 01:17:53 +03:00
|
|
|
parent->children().append(*this);
|
2019-04-16 05:01:14 +03:00
|
|
|
parent->update();
|
2019-04-16 04:47:55 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::move_to_back()
|
2019-04-16 04:47:55 +03:00
|
|
|
{
|
|
|
|
auto* parent = parent_widget();
|
|
|
|
if (!parent)
|
|
|
|
return;
|
|
|
|
if (parent->children().size() == 1)
|
|
|
|
return;
|
2019-06-07 12:46:02 +03:00
|
|
|
parent->children().remove_first_matching([this](auto& entry) {
|
2019-04-16 05:01:14 +03:00
|
|
|
return entry == this;
|
|
|
|
});
|
2019-09-22 01:17:53 +03:00
|
|
|
parent->children().prepend(*this);
|
2019-04-16 05:01:14 +03:00
|
|
|
parent->update();
|
2019-04-16 04:47:55 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
bool Widget::is_frontmost() const
|
2019-04-16 04:47:55 +03:00
|
|
|
{
|
|
|
|
auto* parent = parent_widget();
|
|
|
|
if (!parent)
|
|
|
|
return true;
|
2019-09-22 01:17:53 +03:00
|
|
|
return &parent->children().last() == this;
|
2019-04-16 04:47:55 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
bool Widget::is_backmost() const
|
2019-04-16 04:47:55 +03:00
|
|
|
{
|
|
|
|
auto* parent = parent_widget();
|
|
|
|
if (!parent)
|
|
|
|
return true;
|
2019-09-22 01:17:53 +03:00
|
|
|
return &parent->children().first() == this;
|
2019-04-12 18:10:30 +03:00
|
|
|
}
|
2019-04-20 22:56:56 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
Action* Widget::action_for_key_event(const KeyEvent& event)
|
2019-04-20 22:56:56 +03:00
|
|
|
{
|
2020-02-02 17:07:41 +03:00
|
|
|
Shortcut shortcut(event.modifiers(), (KeyCode)event.key());
|
|
|
|
Action* found_action = nullptr;
|
|
|
|
for_each_child_of_type<Action>([&](auto& action) {
|
2020-02-02 03:57:57 +03:00
|
|
|
if (action.shortcut() == shortcut) {
|
|
|
|
found_action = &action;
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
return found_action;
|
2019-04-20 22:56:56 +03:00
|
|
|
}
|
2019-05-02 04:46:37 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::set_updates_enabled(bool enabled)
|
2019-05-02 04:46:37 +03:00
|
|
|
{
|
|
|
|
if (m_updates_enabled == enabled)
|
|
|
|
return;
|
|
|
|
m_updates_enabled = enabled;
|
|
|
|
if (enabled)
|
|
|
|
update();
|
|
|
|
}
|
2019-05-15 03:39:58 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::focus_previous_widget()
|
2019-05-15 03:39:58 +03:00
|
|
|
{
|
|
|
|
auto focusable_widgets = window()->focusable_widgets();
|
|
|
|
for (int i = focusable_widgets.size() - 1; i >= 0; --i) {
|
|
|
|
if (focusable_widgets[i] != this)
|
|
|
|
continue;
|
|
|
|
if (i > 0)
|
|
|
|
focusable_widgets[i - 1]->set_focus(true);
|
|
|
|
else
|
|
|
|
focusable_widgets.last()->set_focus(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::focus_next_widget()
|
2019-05-15 03:39:58 +03:00
|
|
|
{
|
|
|
|
auto focusable_widgets = window()->focusable_widgets();
|
|
|
|
for (int i = 0; i < focusable_widgets.size(); ++i) {
|
|
|
|
if (focusable_widgets[i] != this)
|
|
|
|
continue;
|
|
|
|
if (i < focusable_widgets.size() - 1)
|
|
|
|
focusable_widgets[i + 1]->set_focus(true);
|
|
|
|
else
|
|
|
|
focusable_widgets.first()->set_focus(true);
|
|
|
|
}
|
|
|
|
}
|
2019-08-03 12:35:10 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::set_backcolor(const StringView& color_string)
|
2019-08-03 12:35:10 +03:00
|
|
|
{
|
|
|
|
auto color = Color::from_string(color_string);
|
|
|
|
if (!color.has_value())
|
|
|
|
return;
|
|
|
|
set_background_color(color.value());
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::set_forecolor(const StringView& color_string)
|
2019-08-03 12:35:10 +03:00
|
|
|
{
|
|
|
|
auto color = Color::from_string(color_string);
|
|
|
|
if (!color.has_value())
|
|
|
|
return;
|
|
|
|
set_foreground_color(color.value());
|
|
|
|
}
|
2019-08-18 21:39:46 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::save_to(AK::JsonObject& json)
|
2019-08-18 21:39:46 +03:00
|
|
|
{
|
|
|
|
json.set("relative_rect", relative_rect().to_string());
|
|
|
|
json.set("fill_with_background_color", fill_with_background_color());
|
|
|
|
json.set("tooltip", tooltip());
|
|
|
|
json.set("visible", is_visible());
|
|
|
|
json.set("focused", is_focused());
|
|
|
|
json.set("enabled", is_enabled());
|
|
|
|
json.set("background_color", background_color().to_string());
|
|
|
|
json.set("foreground_color", foreground_color().to_string());
|
|
|
|
json.set("preferred_size", preferred_size().to_string());
|
|
|
|
json.set("size_policy", String::format("[%s,%s]", to_string(horizontal_size_policy()), to_string(vertical_size_policy())));
|
2020-02-02 14:34:39 +03:00
|
|
|
Core::Object::save_to(json);
|
2019-08-18 21:39:46 +03:00
|
|
|
}
|
2019-11-11 21:12:32 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
Vector<Widget*> Widget::child_widgets() const
|
2019-11-11 21:12:32 +03:00
|
|
|
{
|
2020-02-02 17:07:41 +03:00
|
|
|
Vector<Widget*> widgets;
|
2019-11-11 21:12:32 +03:00
|
|
|
widgets.ensure_capacity(children().size());
|
2020-02-02 17:07:41 +03:00
|
|
|
for (auto& child : const_cast<Widget*>(this)->children()) {
|
2019-11-11 21:12:32 +03:00
|
|
|
if (child.is_widget())
|
2020-02-02 17:07:41 +03:00
|
|
|
widgets.append(static_cast<Widget*>(&child));
|
2019-11-11 21:12:32 +03:00
|
|
|
}
|
|
|
|
return widgets;
|
|
|
|
}
|
2019-12-24 22:57:54 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void Widget::set_palette(const Palette& palette)
|
2019-12-24 22:57:54 +03:00
|
|
|
{
|
2019-12-29 02:47:49 +03:00
|
|
|
m_palette = palette.impl();
|
2019-12-24 22:57:54 +03:00
|
|
|
}
|
2020-02-02 17:07:41 +03:00
|
|
|
|
2020-02-15 01:53:11 +03:00
|
|
|
void Widget::set_background_role(ColorRole role)
|
|
|
|
{
|
|
|
|
m_background_role = role;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::set_foreground_role(ColorRole role)
|
|
|
|
{
|
|
|
|
m_foreground_role = role;
|
|
|
|
}
|
|
|
|
|
|
|
|
Gfx::Palette Widget::palette() const
|
|
|
|
{
|
|
|
|
return Gfx::Palette(*m_palette);
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
}
|