2019-01-20 06:49:48 +03:00
|
|
|
#include <AK/Assertions.h>
|
2019-08-18 21:39:46 +03:00
|
|
|
#include <AK/JsonObject.h>
|
2019-11-08 13:39:45 +03:00
|
|
|
#include <LibDraw/GraphicsBitmap.h>
|
2019-04-20 22:56:56 +03:00
|
|
|
#include <LibGUI/GAction.h>
|
2019-04-08 19:58:44 +03:00
|
|
|
#include <LibGUI/GApplication.h>
|
2019-11-08 13:39:45 +03:00
|
|
|
#include <LibGUI/GEvent.h>
|
2019-06-07 12:46:02 +03:00
|
|
|
#include <LibGUI/GLayout.h>
|
2019-04-12 18:10:30 +03:00
|
|
|
#include <LibGUI/GMenu.h>
|
2019-06-07 12:46:02 +03:00
|
|
|
#include <LibGUI/GPainter.h>
|
2019-11-08 13:39:45 +03:00
|
|
|
#include <LibGUI/GWidget.h>
|
|
|
|
#include <LibGUI/GWindow.h>
|
|
|
|
#include <LibGUI/GWindowServerConnection.h>
|
2019-02-17 02:13:47 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2019-01-20 06:49:48 +03:00
|
|
|
GWidget::GWidget(GWidget* parent)
|
2019-04-18 23:57:24 +03:00
|
|
|
: CObject(parent, true)
|
2019-09-01 13:26:35 +03:00
|
|
|
, m_font(Font::default_font())
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
2019-06-30 10:23:16 +03:00
|
|
|
m_background_color = Color::WarmGray;
|
2019-01-21 02:46:08 +03:00
|
|
|
m_foreground_color = Color::Black;
|
2019-01-20 06:49:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
GWidget::~GWidget()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-04-10 17:56:55 +03:00
|
|
|
void GWidget::child_event(CChildEvent& event)
|
2019-03-16 01:24:40 +03:00
|
|
|
{
|
|
|
|
if (event.type() == GEvent::ChildAdded) {
|
2019-11-05 22:41:27 +03:00
|
|
|
if (event.child() && is<GWidget>(*event.child()) && layout()) {
|
|
|
|
if (event.insertion_before_child() && event.insertion_before_child()->is_widget())
|
|
|
|
layout()->insert_widget_before(to<GWidget>(*event.child()), to<GWidget>(*event.insertion_before_child()));
|
|
|
|
else
|
|
|
|
layout()->add_widget(to<GWidget>(*event.child()));
|
|
|
|
}
|
2019-03-16 01:24:40 +03:00
|
|
|
}
|
2019-04-04 02:44:35 +03:00
|
|
|
if (event.type() == GEvent::ChildRemoved) {
|
2019-04-06 22:15:13 +03:00
|
|
|
if (layout()) {
|
2019-05-27 05:06:01 +03:00
|
|
|
if (event.child() && is<GWidget>(*event.child()))
|
|
|
|
layout()->remove_widget(to<GWidget>(*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
|
|
|
}
|
2019-04-10 18:01:54 +03:00
|
|
|
return CObject::child_event(event);
|
2019-03-16 01:24:40 +03:00
|
|
|
}
|
|
|
|
|
2019-10-23 21:50:01 +03:00
|
|
|
void GWidget::set_relative_rect(const Rect& a_rect)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
2019-10-23 21:50:01 +03:00
|
|
|
Rect rect = a_rect.is_empty() ? Rect() : a_rect;
|
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) {
|
|
|
|
GResizeEvent resize_event(m_relative_rect.size(), rect.size());
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-04-10 17:56:55 +03:00
|
|
|
void GWidget::event(CEvent& event)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
|
|
|
switch (event.type()) {
|
|
|
|
case GEvent::Paint:
|
2019-02-09 13:19:38 +03:00
|
|
|
return handle_paint_event(static_cast<GPaintEvent&>(event));
|
|
|
|
case GEvent::Resize:
|
2019-02-10 13:07:13 +03:00
|
|
|
return handle_resize_event(static_cast<GResizeEvent&>(event));
|
2019-01-26 13:24:16 +03:00
|
|
|
case GEvent::FocusIn:
|
|
|
|
return focusin_event(event);
|
2019-02-04 12:34:56 +03:00
|
|
|
case GEvent::FocusOut:
|
|
|
|
return focusout_event(event);
|
2019-01-20 06:49:48 +03:00
|
|
|
case GEvent::Show:
|
2019-01-21 02:46:08 +03:00
|
|
|
return show_event(static_cast<GShowEvent&>(event));
|
2019-01-20 06:49:48 +03:00
|
|
|
case GEvent::Hide:
|
2019-01-21 02:46:08 +03:00
|
|
|
return hide_event(static_cast<GHideEvent&>(event));
|
2019-01-20 06:49:48 +03:00
|
|
|
case GEvent::KeyDown:
|
2019-01-21 02:46:08 +03:00
|
|
|
return keydown_event(static_cast<GKeyEvent&>(event));
|
2019-01-20 06:49:48 +03:00
|
|
|
case GEvent::KeyUp:
|
2019-01-21 02:46:08 +03:00
|
|
|
return keyup_event(static_cast<GKeyEvent&>(event));
|
2019-01-20 06:49:48 +03:00
|
|
|
case GEvent::MouseMove:
|
2019-01-21 02:46:08 +03:00
|
|
|
return mousemove_event(static_cast<GMouseEvent&>(event));
|
2019-01-20 06:49:48 +03:00
|
|
|
case GEvent::MouseDown:
|
2019-04-01 23:03:32 +03:00
|
|
|
return handle_mousedown_event(static_cast<GMouseEvent&>(event));
|
2019-05-15 23:17:09 +03:00
|
|
|
case GEvent::MouseDoubleClick:
|
|
|
|
return handle_mousedoubleclick_event(static_cast<GMouseEvent&>(event));
|
2019-01-20 06:49:48 +03:00
|
|
|
case GEvent::MouseUp:
|
2019-03-25 03:42:15 +03:00
|
|
|
return handle_mouseup_event(static_cast<GMouseEvent&>(event));
|
2019-05-13 20:52:57 +03:00
|
|
|
case GEvent::MouseWheel:
|
|
|
|
return mousewheel_event(static_cast<GMouseEvent&>(event));
|
2019-02-20 12:12:19 +03:00
|
|
|
case GEvent::Enter:
|
2019-04-08 19:58:44 +03:00
|
|
|
return handle_enter_event(event);
|
2019-02-20 12:12:19 +03:00
|
|
|
case GEvent::Leave:
|
2019-04-08 19:58:44 +03:00
|
|
|
return handle_leave_event(event);
|
2019-05-25 14:40:57 +03:00
|
|
|
case GEvent::EnabledChange:
|
|
|
|
return change_event(static_cast<GEvent&>(event));
|
2019-01-20 06:49:48 +03:00
|
|
|
default:
|
2019-04-10 18:01:54 +03:00
|
|
|
return CObject::event(event);
|
2019-01-20 06:49:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-09 13:19:38 +03:00
|
|
|
void GWidget::handle_paint_event(GPaintEvent& 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()) {
|
2019-03-28 19:19:56 +03:00
|
|
|
GPainter painter(*this);
|
2019-01-21 02:46:08 +03:00
|
|
|
painter.fill_rect(event.rect(), background_color());
|
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.
|
2019-03-28 19:19:56 +03:00
|
|
|
GPainter 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())) {
|
|
|
|
GPaintEvent 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
|
|
|
}
|
|
|
|
|
2019-02-10 13:07:13 +03:00
|
|
|
void GWidget::set_layout(OwnPtr<GLayout>&& layout)
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GWidget::do_layout()
|
|
|
|
{
|
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);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GWidget::notify_layout_changed(Badge<GLayout>)
|
|
|
|
{
|
2019-04-18 23:57:24 +03:00
|
|
|
invalidate_layout();
|
2019-02-10 13:07:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void GWidget::handle_resize_event(GResizeEvent& event)
|
|
|
|
{
|
2019-09-01 21:51:20 +03:00
|
|
|
do_layout();
|
2019-02-10 13:07:13 +03:00
|
|
|
return resize_event(event);
|
|
|
|
}
|
|
|
|
|
2019-03-25 03:42:15 +03:00
|
|
|
void GWidget::handle_mouseup_event(GMouseEvent& event)
|
|
|
|
{
|
|
|
|
mouseup_event(event);
|
|
|
|
}
|
|
|
|
|
2019-04-01 23:03:32 +03:00
|
|
|
void GWidget::handle_mousedown_event(GMouseEvent& event)
|
|
|
|
{
|
|
|
|
if (accepts_focus())
|
|
|
|
set_focus(true);
|
|
|
|
mousedown_event(event);
|
2019-04-18 05:12:27 +03:00
|
|
|
if (event.button() == GMouseButton::Right) {
|
|
|
|
GContextMenuEvent c_event(event.position(), screen_relative_rect().location().translated(event.position()));
|
|
|
|
context_menu_event(c_event);
|
|
|
|
}
|
2019-04-01 23:03:32 +03:00
|
|
|
}
|
|
|
|
|
2019-05-15 23:17:09 +03:00
|
|
|
void GWidget::handle_mousedoubleclick_event(GMouseEvent& event)
|
|
|
|
{
|
|
|
|
doubleclick_event(event);
|
|
|
|
}
|
|
|
|
|
2019-04-10 17:56:55 +03:00
|
|
|
void GWidget::handle_enter_event(CEvent& event)
|
2019-04-08 19:58:44 +03:00
|
|
|
{
|
|
|
|
if (has_tooltip())
|
|
|
|
GApplication::the().show_tooltip(m_tooltip, screen_relative_rect().center().translated(0, height() / 2));
|
|
|
|
enter_event(event);
|
|
|
|
}
|
|
|
|
|
2019-04-10 17:56:55 +03:00
|
|
|
void GWidget::handle_leave_event(CEvent& event)
|
2019-04-08 19:58:44 +03:00
|
|
|
{
|
|
|
|
GApplication::the().hide_tooltip();
|
|
|
|
leave_event(event);
|
|
|
|
}
|
|
|
|
|
2019-03-25 03:42:15 +03:00
|
|
|
void GWidget::click_event(GMouseEvent&)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void GWidget::doubleclick_event(GMouseEvent&)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-09 13:19:38 +03:00
|
|
|
void GWidget::resize_event(GResizeEvent&)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void GWidget::paint_event(GPaintEvent&)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-04-11 04:34:37 +03:00
|
|
|
void GWidget::second_paint_event(GPaintEvent&)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
void GWidget::show_event(GShowEvent&)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
void GWidget::hide_event(GHideEvent&)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-05-15 03:39:58 +03:00
|
|
|
void GWidget::keydown_event(GKeyEvent& 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
|
|
|
}
|
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
void GWidget::keyup_event(GKeyEvent&)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
void GWidget::mousedown_event(GMouseEvent&)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
void GWidget::mouseup_event(GMouseEvent&)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
void GWidget::mousemove_event(GMouseEvent&)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-05-13 20:52:57 +03:00
|
|
|
void GWidget::mousewheel_event(GMouseEvent&)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-04-18 05:12:27 +03:00
|
|
|
void GWidget::context_menu_event(GContextMenuEvent&)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-04-10 17:56:55 +03:00
|
|
|
void GWidget::focusin_event(CEvent&)
|
2019-01-26 13:24:16 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-04-10 17:56:55 +03:00
|
|
|
void GWidget::focusout_event(CEvent&)
|
2019-01-26 13:24:16 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-04-10 17:56:55 +03:00
|
|
|
void GWidget::enter_event(CEvent&)
|
2019-02-20 12:12:19 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-04-10 17:56:55 +03:00
|
|
|
void GWidget::leave_event(CEvent&)
|
2019-02-20 12:12:19 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-05-25 14:40:57 +03:00
|
|
|
void GWidget::change_event(GEvent&)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-20 06:49:48 +03:00
|
|
|
void GWidget::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());
|
|
|
|
}
|
|
|
|
|
|
|
|
void GWidget::update(const 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;
|
|
|
|
|
2019-05-02 04:46:37 +03:00
|
|
|
GWindow* window = m_window;
|
|
|
|
GWidget* parent = parent_widget();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
Rect GWidget::window_relative_rect() const
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-04-08 19:58:44 +03:00
|
|
|
Rect GWidget::screen_relative_rect() const
|
|
|
|
{
|
|
|
|
return window_relative_rect().translated(window()->position());
|
|
|
|
}
|
|
|
|
|
2019-04-16 14:25:00 +03:00
|
|
|
GWidget* GWidget::child_at(const 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) {
|
2019-09-22 01:17:53 +03:00
|
|
|
if (!is<GWidget>(children()[i]))
|
2019-03-15 18:45:27 +03:00
|
|
|
continue;
|
2019-09-22 01:17:53 +03:00
|
|
|
auto& child = to<GWidget>(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))
|
2019-09-22 01:17:53 +03:00
|
|
|
return const_cast<GWidget*>(&child);
|
2019-01-20 06:49:48 +03:00
|
|
|
}
|
2019-04-16 04:47:55 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-09-17 21:58:13 +03:00
|
|
|
GWidget::HitTestResult GWidget::hit_test(const 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
|
|
|
}
|
|
|
|
|
2019-01-20 09:03:38 +03:00
|
|
|
void GWidget::set_window(GWindow* window)
|
2019-01-20 06:49:48 +03:00
|
|
|
{
|
|
|
|
if (m_window == window)
|
|
|
|
return;
|
|
|
|
m_window = window;
|
|
|
|
}
|
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
bool GWidget::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
|
|
|
}
|
|
|
|
|
2019-01-21 02:46:08 +03:00
|
|
|
void GWidget::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
|
|
|
}
|
|
|
|
|
2019-09-01 13:26:35 +03:00
|
|
|
void GWidget::set_font(const 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)
|
|
|
|
m_font = Font::default_font();
|
|
|
|
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
|
|
|
|
|
|
|
void GWidget::set_global_cursor_tracking(bool enabled)
|
|
|
|
{
|
|
|
|
auto* win = window();
|
|
|
|
if (!win)
|
|
|
|
return;
|
|
|
|
win->set_global_cursor_tracking_widget(enabled ? this : nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GWidget::global_cursor_tracking() const
|
|
|
|
{
|
|
|
|
auto* win = window();
|
|
|
|
if (!win)
|
|
|
|
return false;
|
|
|
|
return win->global_cursor_tracking_widget() == this;
|
|
|
|
}
|
2019-02-10 13:07:13 +03:00
|
|
|
|
|
|
|
void GWidget::set_preferred_size(const Size& size)
|
|
|
|
{
|
|
|
|
if (m_preferred_size == size)
|
|
|
|
return;
|
|
|
|
m_preferred_size = size;
|
|
|
|
invalidate_layout();
|
|
|
|
}
|
|
|
|
|
2019-07-27 10:34:28 +03:00
|
|
|
void GWidget::set_size_policy(Orientation orientation, SizePolicy policy)
|
|
|
|
{
|
|
|
|
if (orientation == Orientation::Horizontal)
|
|
|
|
set_size_policy(policy, m_vertical_size_policy);
|
|
|
|
else
|
|
|
|
set_size_policy(m_horizontal_size_policy, policy);
|
|
|
|
}
|
|
|
|
|
2019-02-10 13:07:13 +03:00
|
|
|
void GWidget::set_size_policy(SizePolicy horizontal_policy, SizePolicy vertical_policy)
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GWidget::invalidate_layout()
|
|
|
|
{
|
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
|
|
|
|
|
|
|
void GWidget::set_visible(bool visible)
|
|
|
|
{
|
|
|
|
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) {
|
|
|
|
GShowEvent e;
|
|
|
|
event(e);
|
|
|
|
} else {
|
|
|
|
GHideEvent e;
|
|
|
|
event(e);
|
|
|
|
}
|
2019-03-15 18:12:06 +03:00
|
|
|
}
|
2019-03-29 04:20:22 +03:00
|
|
|
|
|
|
|
bool GWidget::spans_entire_window_horizontally() const
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
void GWidget::set_enabled(bool enabled)
|
|
|
|
{
|
|
|
|
if (m_enabled == enabled)
|
|
|
|
return;
|
|
|
|
m_enabled = enabled;
|
2019-05-25 14:40:57 +03:00
|
|
|
GEvent e(GEvent::EnabledChange);
|
|
|
|
event(e);
|
2019-04-12 03:51:16 +03:00
|
|
|
update();
|
|
|
|
}
|
2019-04-12 18:10:30 +03:00
|
|
|
|
2019-04-16 04:47:55 +03:00
|
|
|
void GWidget::move_to_front()
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void GWidget::move_to_back()
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
bool GWidget::is_frontmost() const
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
bool GWidget::is_backmost() const
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
GAction* GWidget::action_for_key_event(const GKeyEvent& event)
|
|
|
|
{
|
|
|
|
auto it = m_local_shortcut_actions.find(GShortcut(event.modifiers(), (KeyCode)event.key()));
|
|
|
|
if (it == m_local_shortcut_actions.end())
|
|
|
|
return nullptr;
|
|
|
|
return (*it).value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GWidget::register_local_shortcut_action(Badge<GAction>, GAction& action)
|
|
|
|
{
|
|
|
|
m_local_shortcut_actions.set(action.shortcut(), &action);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GWidget::unregister_local_shortcut_action(Badge<GAction>, GAction& action)
|
|
|
|
{
|
|
|
|
m_local_shortcut_actions.remove(action.shortcut());
|
|
|
|
}
|
2019-05-02 04:46:37 +03:00
|
|
|
|
|
|
|
void GWidget::set_updates_enabled(bool enabled)
|
|
|
|
{
|
|
|
|
if (m_updates_enabled == enabled)
|
|
|
|
return;
|
|
|
|
m_updates_enabled = enabled;
|
|
|
|
if (enabled)
|
|
|
|
update();
|
|
|
|
}
|
2019-05-15 03:39:58 +03:00
|
|
|
|
|
|
|
void GWidget::focus_previous_widget()
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GWidget::focus_next_widget()
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
void GWidget::set_backcolor(const StringView& color_string)
|
|
|
|
{
|
|
|
|
auto color = Color::from_string(color_string);
|
|
|
|
if (!color.has_value())
|
|
|
|
return;
|
|
|
|
set_background_color(color.value());
|
|
|
|
}
|
|
|
|
|
|
|
|
void GWidget::set_forecolor(const StringView& color_string)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
void GWidget::save_to(AK::JsonObject& json)
|
|
|
|
{
|
|
|
|
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())));
|
|
|
|
CObject::save_to(json);
|
|
|
|
}
|