mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 15:12:45 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			434 lines
		
	
	
	
		
			9.9 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			434 lines
		
	
	
	
		
			9.9 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| #include "GWidget.h"
 | |
| #include "GEvent.h"
 | |
| #include "GEventLoop.h"
 | |
| #include "GWindow.h"
 | |
| #include <LibGUI/GLayout.h>
 | |
| #include <AK/Assertions.h>
 | |
| #include <SharedGraphics/GraphicsBitmap.h>
 | |
| #include <LibGUI/GPainter.h>
 | |
| #include <LibGUI/GApplication.h>
 | |
| #include <LibGUI/GMenu.h>
 | |
| #include <unistd.h>
 | |
| 
 | |
| GWidget::GWidget(GWidget* parent)
 | |
|     : CObject(parent)
 | |
| {
 | |
|     set_font(nullptr);
 | |
|     m_background_color = Color::LightGray;
 | |
|     m_foreground_color = Color::Black;
 | |
| }
 | |
| 
 | |
| GWidget::~GWidget()
 | |
| {
 | |
| }
 | |
| 
 | |
| void GWidget::child_event(CChildEvent& event)
 | |
| {
 | |
|     if (event.type() == GEvent::ChildAdded) {
 | |
|         if (event.child() && event.child()->is_widget() && layout())
 | |
|             layout()->add_widget(static_cast<GWidget&>(*event.child()));
 | |
|     }
 | |
|     if (event.type() == GEvent::ChildRemoved) {
 | |
|         if (layout()) {
 | |
|             if (event.child() && event.child()->is_widget())
 | |
|                 layout()->remove_widget(static_cast<GWidget&>(*event.child()));
 | |
|             else
 | |
|                 invalidate_layout();
 | |
|         }
 | |
|     }
 | |
|     return CObject::child_event(event);
 | |
| }
 | |
| 
 | |
| void GWidget::set_relative_rect(const Rect& rect)
 | |
| {
 | |
|     if (rect == m_relative_rect)
 | |
|         return;
 | |
|     bool size_changed = m_relative_rect.size() != rect.size();
 | |
|     m_relative_rect = rect;
 | |
| 
 | |
|     if (size_changed) {
 | |
|         GResizeEvent resize_event(m_relative_rect.size(), rect.size());
 | |
|         event(resize_event);
 | |
|     }
 | |
| 
 | |
|     update();
 | |
| }
 | |
| 
 | |
| void GWidget::event(CEvent& event)
 | |
| {
 | |
|     switch (event.type()) {
 | |
|     case GEvent::Paint:
 | |
|         return handle_paint_event(static_cast<GPaintEvent&>(event));
 | |
|     case GEvent::Resize:
 | |
|         return handle_resize_event(static_cast<GResizeEvent&>(event));
 | |
|     case GEvent::FocusIn:
 | |
|         return focusin_event(event);
 | |
|     case GEvent::FocusOut:
 | |
|         return focusout_event(event);
 | |
|     case GEvent::Show:
 | |
|         return show_event(static_cast<GShowEvent&>(event));
 | |
|     case GEvent::Hide:
 | |
|         return hide_event(static_cast<GHideEvent&>(event));
 | |
|     case GEvent::KeyDown:
 | |
|         return keydown_event(static_cast<GKeyEvent&>(event));
 | |
|     case GEvent::KeyUp:
 | |
|         return keyup_event(static_cast<GKeyEvent&>(event));
 | |
|     case GEvent::MouseMove:
 | |
|         return mousemove_event(static_cast<GMouseEvent&>(event));
 | |
|     case GEvent::MouseDown:
 | |
|         return handle_mousedown_event(static_cast<GMouseEvent&>(event));
 | |
|     case GEvent::MouseUp:
 | |
|         return handle_mouseup_event(static_cast<GMouseEvent&>(event));
 | |
|     case GEvent::Enter:
 | |
|         return handle_enter_event(event);
 | |
|     case GEvent::Leave:
 | |
|         return handle_leave_event(event);
 | |
|     default:
 | |
|         return CObject::event(event);
 | |
|     }
 | |
| }
 | |
| 
 | |
| void GWidget::handle_paint_event(GPaintEvent& event)
 | |
| {
 | |
|     ASSERT(is_visible());
 | |
|     if (fill_with_background_color()) {
 | |
|         GPainter painter(*this);
 | |
|         painter.fill_rect(event.rect(), background_color());
 | |
|     } 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.
 | |
|         GPainter painter(*this);
 | |
|         painter.fill_rect(rect(), Color::Red);
 | |
| #endif
 | |
|     }
 | |
|     paint_event(event);
 | |
|     for (auto* ch : children()) {
 | |
|         auto* child = (GWidget*)ch;
 | |
|         if (!child->is_visible())
 | |
|             continue;
 | |
|         if (child->relative_rect().intersects(event.rect())) {
 | |
|             auto local_rect = event.rect();
 | |
|             local_rect.intersect(child->relative_rect());
 | |
|             local_rect.move_by(-child->relative_rect().x(), -child->relative_rect().y());
 | |
|             GPaintEvent local_event(local_rect);
 | |
|             child->event(local_event);
 | |
|         }
 | |
|     }
 | |
|     second_paint_event(event);
 | |
| }
 | |
| 
 | |
| void GWidget::set_layout(OwnPtr<GLayout>&& layout)
 | |
| {
 | |
|     if (m_layout)
 | |
|         m_layout->notify_disowned(Badge<GWidget>(), *this);
 | |
|     m_layout = move(layout);
 | |
|     if (m_layout) {
 | |
|         m_layout->notify_adopted(Badge<GWidget>(), *this);
 | |
|         do_layout();
 | |
|     } else {
 | |
|         update();
 | |
|     }
 | |
| }
 | |
| 
 | |
| void GWidget::do_layout()
 | |
| {
 | |
|     if (!m_layout)
 | |
|         return;
 | |
|     m_layout->run(*this);
 | |
|     update();
 | |
| }
 | |
| 
 | |
| void GWidget::notify_layout_changed(Badge<GLayout>)
 | |
| {
 | |
|     do_layout();
 | |
| }
 | |
| 
 | |
| void GWidget::handle_resize_event(GResizeEvent& event)
 | |
| {
 | |
|     if (layout())
 | |
|         do_layout();
 | |
|     return resize_event(event);
 | |
| }
 | |
| 
 | |
| void GWidget::handle_mouseup_event(GMouseEvent& event)
 | |
| {
 | |
|     mouseup_event(event);
 | |
| 
 | |
|     if (!rect().contains(event.position()))
 | |
|         return;
 | |
|     // It's a click.. but is it a doubleclick?
 | |
|     // FIXME: This needs improvement.
 | |
|     if (m_click_clock.is_valid()) {
 | |
|         int elapsed_since_last_click = m_click_clock.elapsed();
 | |
| #if 0
 | |
|         dbgprintf("Click clock elapsed: %d\n", m_click_clock.elapsed());
 | |
| #endif
 | |
|         if (elapsed_since_last_click < 250) {
 | |
|             doubleclick_event(event);
 | |
|         } else {
 | |
|             m_click_clock.start();
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 | |
| void GWidget::handle_mousedown_event(GMouseEvent& event)
 | |
| {
 | |
|     if (accepts_focus())
 | |
|         set_focus(true);
 | |
|     if (event.button() == GMouseButton::Right) {
 | |
|         if (m_context_menu) {
 | |
|             m_context_menu->popup(screen_relative_rect().location().translated(event.position()));
 | |
|             return;
 | |
|         }
 | |
|     }
 | |
|     // FIXME: Maybe the click clock should be per-button.
 | |
|     if (!m_click_clock.is_valid())
 | |
|         m_click_clock.start();
 | |
|     mousedown_event(event);
 | |
| }
 | |
| 
 | |
| void GWidget::handle_enter_event(CEvent& event)
 | |
| {
 | |
|     if (has_tooltip())
 | |
|         GApplication::the().show_tooltip(m_tooltip, screen_relative_rect().center().translated(0, height() / 2));
 | |
|     enter_event(event);
 | |
| }
 | |
| 
 | |
| void GWidget::handle_leave_event(CEvent& event)
 | |
| {
 | |
|     GApplication::the().hide_tooltip();
 | |
|     leave_event(event);
 | |
| }
 | |
| 
 | |
| void GWidget::click_event(GMouseEvent&)
 | |
| {
 | |
| }
 | |
| 
 | |
| void GWidget::doubleclick_event(GMouseEvent&)
 | |
| {
 | |
| }
 | |
| 
 | |
| void GWidget::resize_event(GResizeEvent&)
 | |
| {
 | |
| }
 | |
| 
 | |
| void GWidget::paint_event(GPaintEvent&)
 | |
| {
 | |
| }
 | |
| 
 | |
| void GWidget::second_paint_event(GPaintEvent&)
 | |
| {
 | |
| }
 | |
| 
 | |
| void GWidget::show_event(GShowEvent&)
 | |
| {
 | |
| }
 | |
| 
 | |
| void GWidget::hide_event(GHideEvent&)
 | |
| {
 | |
| }
 | |
| 
 | |
| void GWidget::keydown_event(GKeyEvent&)
 | |
| {
 | |
| }
 | |
| 
 | |
| void GWidget::keyup_event(GKeyEvent&)
 | |
| {
 | |
| }
 | |
| 
 | |
| void GWidget::mousedown_event(GMouseEvent&)
 | |
| {
 | |
| }
 | |
| 
 | |
| void GWidget::mouseup_event(GMouseEvent&)
 | |
| {
 | |
| }
 | |
| 
 | |
| void GWidget::mousemove_event(GMouseEvent&)
 | |
| {
 | |
| }
 | |
| 
 | |
| void GWidget::focusin_event(CEvent&)
 | |
| {
 | |
| }
 | |
| 
 | |
| void GWidget::focusout_event(CEvent&)
 | |
| {
 | |
| }
 | |
| 
 | |
| void GWidget::enter_event(CEvent&)
 | |
| {
 | |
| }
 | |
| 
 | |
| void GWidget::leave_event(CEvent&)
 | |
| {
 | |
| }
 | |
| 
 | |
| void GWidget::update()
 | |
| {
 | |
|     update(rect());
 | |
| }
 | |
| 
 | |
| void GWidget::update(const Rect& rect)
 | |
| {
 | |
|     if (!is_visible())
 | |
|         return;
 | |
|     auto* w = window();
 | |
|     if (!w)
 | |
|         return;
 | |
|     w->update(rect.translated(window_relative_rect().location()));
 | |
| }
 | |
| 
 | |
| 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;
 | |
| }
 | |
| 
 | |
| Rect GWidget::screen_relative_rect() const
 | |
| {
 | |
|     return window_relative_rect().translated(window()->position());
 | |
| }
 | |
| 
 | |
| GWidget::HitTestResult GWidget::hit_test(int x, int y)
 | |
| {
 | |
|     if (is_greedy_for_hits())
 | |
|         return { this, x, y };
 | |
|     for (int i = children().size() - 1; i >= 0; --i) {
 | |
|         if (!children()[i]->is_widget())
 | |
|             continue;
 | |
|         auto& child = *(GWidget*)children()[i];
 | |
|         if (!child.is_visible())
 | |
|             continue;
 | |
|         if (child.relative_rect().contains(x, y))
 | |
|             return child.hit_test(x - child.relative_rect().x(), y - child.relative_rect().y());
 | |
|     }
 | |
|     return { this, x, y };
 | |
| }
 | |
| 
 | |
| void GWidget::set_window(GWindow* window)
 | |
| {
 | |
|     if (m_window == window)
 | |
|         return;
 | |
|     m_window = window;
 | |
| }
 | |
| 
 | |
| bool GWidget::is_focused() const
 | |
| {
 | |
|     auto* win = window();
 | |
|     if (!win)
 | |
|         return false;
 | |
|     if (!win->is_active())
 | |
|         return false;
 | |
|     return win->focused_widget() == this;
 | |
| }
 | |
| 
 | |
| void GWidget::set_focus(bool focus)
 | |
| {
 | |
|     auto* win = window();
 | |
|     if (!win)
 | |
|         return;
 | |
|     if (focus) {
 | |
|         win->set_focused_widget(this);
 | |
|     } else {
 | |
|         if (win->focused_widget() == this)
 | |
|             win->set_focused_widget(nullptr);
 | |
|     }
 | |
| }
 | |
| 
 | |
| void GWidget::set_font(RetainPtr<Font>&& font)
 | |
| {
 | |
|     if (!font)
 | |
|         m_font = Font::default_font();
 | |
|     else
 | |
|         m_font = move(font);
 | |
|     update();
 | |
| }
 | |
| 
 | |
| 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;
 | |
| }
 | |
| 
 | |
| void GWidget::set_preferred_size(const Size& size)
 | |
| {
 | |
|     if (m_preferred_size == size)
 | |
|         return;
 | |
|     m_preferred_size = size;
 | |
|     invalidate_layout();
 | |
| }
 | |
| 
 | |
| 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()
 | |
| {
 | |
|     auto* w = window();
 | |
|     if (!w)
 | |
|         return;
 | |
|     if (!w->main_widget())
 | |
|         return;
 | |
|     do_layout();
 | |
|     w->main_widget()->do_layout();
 | |
| }
 | |
| 
 | |
| 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();
 | |
| }
 | |
| 
 | |
| 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();
 | |
| }
 | |
| 
 | |
| void GWidget::set_enabled(bool enabled)
 | |
| {
 | |
|     if (m_enabled == enabled)
 | |
|         return;
 | |
|     m_enabled = enabled;
 | |
|     update();
 | |
| }
 | |
| 
 | |
| void GWidget::set_context_menu(OwnPtr<GMenu>&& context_menu)
 | |
| {
 | |
|     // FIXME: Support switching context menus.
 | |
|     ASSERT(!m_context_menu);
 | |
|     m_context_menu = move(context_menu);
 | |
| }
 | 
