| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | #include "GWidget.h"
 | 
					
						
							|  |  |  | #include "GEvent.h"
 | 
					
						
							|  |  |  | #include "GEventLoop.h"
 | 
					
						
							|  |  |  | #include "GWindow.h"
 | 
					
						
							| 
									
										
										
										
											2019-02-10 11:07:13 +01:00
										 |  |  | #include <LibGUI/GLayout.h>
 | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | #include <AK/Assertions.h>
 | 
					
						
							|  |  |  | #include <SharedGraphics/GraphicsBitmap.h>
 | 
					
						
							| 
									
										
										
										
											2019-03-28 17:19:56 +01:00
										 |  |  | #include <LibGUI/GPainter.h>
 | 
					
						
							| 
									
										
										
										
											2019-04-08 18:58:44 +02:00
										 |  |  | #include <LibGUI/GApplication.h>
 | 
					
						
							| 
									
										
										
										
											2019-04-12 17:10:30 +02:00
										 |  |  | #include <LibGUI/GMenu.h>
 | 
					
						
							| 
									
										
										
										
											2019-02-17 00:13:47 +01:00
										 |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | GWidget::GWidget(GWidget* parent) | 
					
						
							| 
									
										
										
										
											2019-04-10 17:01:54 +02:00
										 |  |  |     : CObject(parent) | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-01-21 00:46:08 +01:00
										 |  |  |     set_font(nullptr); | 
					
						
							| 
									
										
										
										
											2019-02-04 11:51:42 +01:00
										 |  |  |     m_background_color = Color::LightGray; | 
					
						
							| 
									
										
										
										
											2019-01-21 00:46:08 +01:00
										 |  |  |     m_foreground_color = Color::Black; | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GWidget::~GWidget() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 16:56:55 +02:00
										 |  |  | void GWidget::child_event(CChildEvent& event) | 
					
						
							| 
									
										
										
										
											2019-03-15 23:24:40 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if (event.type() == GEvent::ChildAdded) { | 
					
						
							|  |  |  |         if (event.child() && event.child()->is_widget() && layout()) | 
					
						
							|  |  |  |             layout()->add_widget(static_cast<GWidget&>(*event.child())); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-04 01:44:35 +02:00
										 |  |  |     if (event.type() == GEvent::ChildRemoved) { | 
					
						
							| 
									
										
										
										
											2019-04-06 21:15:13 +02:00
										 |  |  |         if (layout()) { | 
					
						
							|  |  |  |             if (event.child() && event.child()->is_widget()) | 
					
						
							|  |  |  |                 layout()->remove_widget(static_cast<GWidget&>(*event.child())); | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |                 invalidate_layout(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-04-04 01:44:35 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-10 17:01:54 +02:00
										 |  |  |     return CObject::child_event(event); | 
					
						
							| 
									
										
										
										
											2019-03-15 23:24:40 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-24 23:40:12 +01:00
										 |  |  | void GWidget::set_relative_rect(const Rect& rect) | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-01-24 23:40:12 +01:00
										 |  |  |     if (rect == m_relative_rect) | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-04-16 22:59:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     auto old_rect = m_relative_rect; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-20 21:59:13 +01:00
										 |  |  |     bool size_changed = m_relative_rect.size() != rect.size(); | 
					
						
							| 
									
										
										
										
											2019-01-21 00:46:08 +01:00
										 |  |  |     m_relative_rect = rect; | 
					
						
							| 
									
										
										
										
											2019-02-20 21:59:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (size_changed) { | 
					
						
							|  |  |  |         GResizeEvent resize_event(m_relative_rect.size(), rect.size()); | 
					
						
							|  |  |  |         event(resize_event); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-16 22:59:27 +02:00
										 |  |  |     if (auto* parent = parent_widget()) | 
					
						
							|  |  |  |         parent->update(old_rect); | 
					
						
							| 
									
										
										
										
											2019-01-24 23:40:12 +01:00
										 |  |  |     update(); | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 16:56:55 +02:00
										 |  |  | void GWidget::event(CEvent& event) | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     switch (event.type()) { | 
					
						
							|  |  |  |     case GEvent::Paint: | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  |         return handle_paint_event(static_cast<GPaintEvent&>(event)); | 
					
						
							|  |  |  |     case GEvent::Resize: | 
					
						
							| 
									
										
										
										
											2019-02-10 11:07:13 +01:00
										 |  |  |         return handle_resize_event(static_cast<GResizeEvent&>(event)); | 
					
						
							| 
									
										
										
										
											2019-01-26 11:24:16 +01:00
										 |  |  |     case GEvent::FocusIn: | 
					
						
							|  |  |  |         return focusin_event(event); | 
					
						
							| 
									
										
										
										
											2019-02-04 10:34:56 +01:00
										 |  |  |     case GEvent::FocusOut: | 
					
						
							|  |  |  |         return focusout_event(event); | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  |     case GEvent::Show: | 
					
						
							| 
									
										
										
										
											2019-01-21 00:46:08 +01:00
										 |  |  |         return show_event(static_cast<GShowEvent&>(event)); | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  |     case GEvent::Hide: | 
					
						
							| 
									
										
										
										
											2019-01-21 00:46:08 +01:00
										 |  |  |         return hide_event(static_cast<GHideEvent&>(event)); | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  |     case GEvent::KeyDown: | 
					
						
							| 
									
										
										
										
											2019-01-21 00:46:08 +01:00
										 |  |  |         return keydown_event(static_cast<GKeyEvent&>(event)); | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  |     case GEvent::KeyUp: | 
					
						
							| 
									
										
										
										
											2019-01-21 00:46:08 +01:00
										 |  |  |         return keyup_event(static_cast<GKeyEvent&>(event)); | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  |     case GEvent::MouseMove: | 
					
						
							| 
									
										
										
										
											2019-01-21 00:46:08 +01:00
										 |  |  |         return mousemove_event(static_cast<GMouseEvent&>(event)); | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  |     case GEvent::MouseDown: | 
					
						
							| 
									
										
										
										
											2019-04-01 22:03:32 +02:00
										 |  |  |         return handle_mousedown_event(static_cast<GMouseEvent&>(event)); | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  |     case GEvent::MouseUp: | 
					
						
							| 
									
										
										
										
											2019-03-25 01:42:15 +01:00
										 |  |  |         return handle_mouseup_event(static_cast<GMouseEvent&>(event)); | 
					
						
							| 
									
										
										
										
											2019-02-20 10:12:19 +01:00
										 |  |  |     case GEvent::Enter: | 
					
						
							| 
									
										
										
										
											2019-04-08 18:58:44 +02:00
										 |  |  |         return handle_enter_event(event); | 
					
						
							| 
									
										
										
										
											2019-02-20 10:12:19 +01:00
										 |  |  |     case GEvent::Leave: | 
					
						
							| 
									
										
										
										
											2019-04-08 18:58:44 +02:00
										 |  |  |         return handle_leave_event(event); | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  |     default: | 
					
						
							| 
									
										
										
										
											2019-04-10 17:01:54 +02:00
										 |  |  |         return CObject::event(event); | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  | void GWidget::handle_paint_event(GPaintEvent& event) | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-03-25 13:58:30 +01:00
										 |  |  |     ASSERT(is_visible()); | 
					
						
							| 
									
										
										
										
											2019-01-21 00:46:08 +01:00
										 |  |  |     if (fill_with_background_color()) { | 
					
						
							| 
									
										
										
										
											2019-03-28 17:19:56 +01:00
										 |  |  |         GPainter painter(*this); | 
					
						
							| 
									
										
										
										
											2019-01-21 00:46:08 +01:00
										 |  |  |         painter.fill_rect(event.rect(), background_color()); | 
					
						
							| 
									
										
										
										
											2019-02-20 02:39:46 +01: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 17:19:56 +01:00
										 |  |  |         GPainter painter(*this); | 
					
						
							| 
									
										
										
										
											2019-02-20 02:39:46 +01:00
										 |  |  |         painter.fill_rect(rect(), Color::Red); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  |     paint_event(event); | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  |     for (auto* ch : children()) { | 
					
						
							|  |  |  |         auto* child = (GWidget*)ch; | 
					
						
							| 
									
										
										
										
											2019-03-15 16:12:06 +01:00
										 |  |  |         if (!child->is_visible()) | 
					
						
							|  |  |  |             continue; | 
					
						
							| 
									
										
										
										
											2019-02-02 08:05:14 +01:00
										 |  |  |         if (child->relative_rect().intersects(event.rect())) { | 
					
						
							| 
									
										
										
										
											2019-04-16 13:58:02 +02:00
										 |  |  |             GPaintEvent local_event(event.rect().intersected(child->relative_rect()).translated(-child->relative_position())); | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  |             child->event(local_event); | 
					
						
							| 
									
										
										
										
											2019-02-02 08:05:14 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-11 03:34:37 +02:00
										 |  |  |     second_paint_event(event); | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 11:07:13 +01:00
										 |  |  | 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); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-25 01:42:15 +01:00
										 |  |  | 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.
 | 
					
						
							| 
									
										
										
										
											2019-04-12 17:10:30 +02:00
										 |  |  |     if (m_click_clock.is_valid()) { | 
					
						
							|  |  |  |         int elapsed_since_last_click = m_click_clock.elapsed(); | 
					
						
							| 
									
										
										
										
											2019-04-13 16:15:47 +02:00
										 |  |  | #if 0
 | 
					
						
							| 
									
										
										
										
											2019-04-12 17:10:30 +02:00
										 |  |  |         dbgprintf("Click clock elapsed: %d\n", m_click_clock.elapsed()); | 
					
						
							| 
									
										
										
										
											2019-04-13 16:15:47 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-04-12 17:10:30 +02:00
										 |  |  |         if (elapsed_since_last_click < 250) { | 
					
						
							|  |  |  |             doubleclick_event(event); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             m_click_clock.start(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-25 01:42:15 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-01 22:03:32 +02:00
										 |  |  | void GWidget::handle_mousedown_event(GMouseEvent& event) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (accepts_focus()) | 
					
						
							|  |  |  |         set_focus(true); | 
					
						
							| 
									
										
										
										
											2019-04-12 17:10:30 +02:00
										 |  |  |     if (event.button() == GMouseButton::Right) { | 
					
						
							|  |  |  |         if (m_context_menu) { | 
					
						
							| 
									
										
										
										
											2019-04-16 03:47:55 +02:00
										 |  |  |             if (m_context_menu_mode == ContextMenuMode::PassthroughMouseEvent) | 
					
						
							|  |  |  |                 mousedown_event(event); | 
					
						
							| 
									
										
										
										
											2019-04-12 17:10:30 +02:00
										 |  |  |             m_context_menu->popup(screen_relative_rect().location().translated(event.position())); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     // FIXME: Maybe the click clock should be per-button.
 | 
					
						
							| 
									
										
										
										
											2019-04-01 22:03:32 +02:00
										 |  |  |     if (!m_click_clock.is_valid()) | 
					
						
							|  |  |  |         m_click_clock.start(); | 
					
						
							|  |  |  |     mousedown_event(event); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 16:56:55 +02:00
										 |  |  | void GWidget::handle_enter_event(CEvent& event) | 
					
						
							| 
									
										
										
										
											2019-04-08 18:58:44 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (has_tooltip()) | 
					
						
							|  |  |  |         GApplication::the().show_tooltip(m_tooltip, screen_relative_rect().center().translated(0, height() / 2)); | 
					
						
							|  |  |  |     enter_event(event); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 16:56:55 +02:00
										 |  |  | void GWidget::handle_leave_event(CEvent& event) | 
					
						
							| 
									
										
										
										
											2019-04-08 18:58:44 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     GApplication::the().hide_tooltip(); | 
					
						
							|  |  |  |     leave_event(event); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-25 01:42:15 +01:00
										 |  |  | void GWidget::click_event(GMouseEvent&) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GWidget::doubleclick_event(GMouseEvent&) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-09 11:19:38 +01:00
										 |  |  | void GWidget::resize_event(GResizeEvent&) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GWidget::paint_event(GPaintEvent&) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 03:34:37 +02:00
										 |  |  | void GWidget::second_paint_event(GPaintEvent&) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-21 00:46:08 +01:00
										 |  |  | void GWidget::show_event(GShowEvent&) | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-21 00:46:08 +01:00
										 |  |  | void GWidget::hide_event(GHideEvent&) | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-21 00:46:08 +01:00
										 |  |  | void GWidget::keydown_event(GKeyEvent&) | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-21 00:46:08 +01:00
										 |  |  | void GWidget::keyup_event(GKeyEvent&) | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-21 00:46:08 +01:00
										 |  |  | void GWidget::mousedown_event(GMouseEvent&) | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-21 00:46:08 +01:00
										 |  |  | void GWidget::mouseup_event(GMouseEvent&) | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-21 00:46:08 +01:00
										 |  |  | void GWidget::mousemove_event(GMouseEvent&) | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 16:56:55 +02:00
										 |  |  | void GWidget::focusin_event(CEvent&) | 
					
						
							| 
									
										
										
										
											2019-01-26 11:24:16 +01:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 16:56:55 +02:00
										 |  |  | void GWidget::focusout_event(CEvent&) | 
					
						
							| 
									
										
										
										
											2019-01-26 11:24:16 +01:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 16:56:55 +02:00
										 |  |  | void GWidget::enter_event(CEvent&) | 
					
						
							| 
									
										
										
										
											2019-02-20 10:12:19 +01:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 16:56:55 +02:00
										 |  |  | void GWidget::leave_event(CEvent&) | 
					
						
							| 
									
										
										
										
											2019-02-20 10:12:19 +01:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | void GWidget::update() | 
					
						
							| 
									
										
										
										
											2019-02-10 14:28:39 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     update(rect()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GWidget::update(const Rect& rect) | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-03-25 13:58:30 +01:00
										 |  |  |     if (!is_visible()) | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  |     auto* w = window(); | 
					
						
							|  |  |  |     if (!w) | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-02-10 14:28:39 +01:00
										 |  |  |     w->update(rect.translated(window_relative_rect().location())); | 
					
						
							| 
									
										
										
										
											2019-02-09 14:30:05 +01: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 04:49:48 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-08 18:58:44 +02:00
										 |  |  | Rect GWidget::screen_relative_rect() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return window_relative_rect().translated(window()->position()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-16 13:25:00 +02:00
										 |  |  | GWidget* GWidget::child_at(const Point& point) const | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-10 02:08:32 +02:00
										 |  |  |     for (int i = children().size() - 1; i >= 0; --i) { | 
					
						
							|  |  |  |         if (!children()[i]->is_widget()) | 
					
						
							| 
									
										
										
										
											2019-03-15 16:45:27 +01:00
										 |  |  |             continue; | 
					
						
							| 
									
										
										
										
											2019-04-10 02:08:32 +02:00
										 |  |  |         auto& child = *(GWidget*)children()[i]; | 
					
						
							| 
									
										
										
										
											2019-03-15 16:45:27 +01:00
										 |  |  |         if (!child.is_visible()) | 
					
						
							|  |  |  |             continue; | 
					
						
							| 
									
										
										
										
											2019-04-16 03:47:55 +02:00
										 |  |  |         if (child.relative_rect().contains(point)) | 
					
						
							|  |  |  |             return &child; | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-16 03:47:55 +02:00
										 |  |  |     return nullptr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-16 13:25:00 +02:00
										 |  |  | GWidget::HitTestResult GWidget::hit_test(const Point& position) | 
					
						
							| 
									
										
										
										
											2019-04-16 03:47:55 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (is_greedy_for_hits()) | 
					
						
							| 
									
										
										
										
											2019-04-16 13:25:00 +02:00
										 |  |  |         return { this, position }; | 
					
						
							|  |  |  |     if (auto* child = child_at(position)) | 
					
						
							|  |  |  |         return child->hit_test(position - child->relative_position()); | 
					
						
							|  |  |  |     return { this, position }; | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-20 07:03:38 +01:00
										 |  |  | void GWidget::set_window(GWindow* window) | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_window == window) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     m_window = window; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-21 00:46:08 +01:00
										 |  |  | bool GWidget::is_focused() const | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-01-26 11:24:16 +01:00
										 |  |  |     auto* win = window(); | 
					
						
							|  |  |  |     if (!win) | 
					
						
							|  |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2019-01-26 21:58:43 +01:00
										 |  |  |     if (!win->is_active()) | 
					
						
							|  |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2019-01-26 11:24:16 +01:00
										 |  |  |     return win->focused_widget() == this; | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-21 00:46:08 +01:00
										 |  |  | void GWidget::set_focus(bool focus) | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-01-26 11:24:16 +01:00
										 |  |  |     auto* win = window(); | 
					
						
							|  |  |  |     if (!win) | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-01-26 11:24:16 +01:00
										 |  |  |     if (focus) { | 
					
						
							|  |  |  |         win->set_focused_widget(this); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         if (win->focused_widget() == this) | 
					
						
							|  |  |  |             win->set_focused_widget(nullptr); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-21 00:46:08 +01:00
										 |  |  | void GWidget::set_font(RetainPtr<Font>&& font) | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if (!font) | 
					
						
							|  |  |  |         m_font = Font::default_font(); | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         m_font = move(font); | 
					
						
							| 
									
										
										
										
											2019-02-12 10:08:35 +01:00
										 |  |  |     update(); | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-01-27 08:48:34 +01: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 11:07:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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; | 
					
						
							| 
									
										
										
										
											2019-03-25 04:25:25 +01:00
										 |  |  |     do_layout(); | 
					
						
							| 
									
										
										
										
											2019-02-10 11:07:13 +01:00
										 |  |  |     w->main_widget()->do_layout(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-03-15 16:12:06 +01: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-03-29 02:20:22 +01: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 02:51:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | void GWidget::set_enabled(bool enabled) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (m_enabled == enabled) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     m_enabled = enabled; | 
					
						
							|  |  |  |     update(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-04-12 17:10:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-16 03:47:55 +02:00
										 |  |  | void GWidget::set_context_menu(OwnPtr<GMenu>&& context_menu, ContextMenuMode mode) | 
					
						
							| 
									
										
										
										
											2019-04-12 17:10:30 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     // FIXME: Support switching context menus.
 | 
					
						
							|  |  |  |     ASSERT(!m_context_menu); | 
					
						
							|  |  |  |     m_context_menu = move(context_menu); | 
					
						
							| 
									
										
										
										
											2019-04-16 03:47:55 +02:00
										 |  |  |     m_context_menu_mode = mode; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GWidget::move_to_front() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto* parent = parent_widget(); | 
					
						
							|  |  |  |     if (!parent) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     if (parent->children().size() == 1) | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-04-16 04:01:14 +02:00
										 |  |  |     parent->children().remove_first_matching([this] (auto& entry) { | 
					
						
							|  |  |  |         return entry == this; | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-04-16 03:47:55 +02:00
										 |  |  |     parent->children().append(this); | 
					
						
							| 
									
										
										
										
											2019-04-16 04:01:14 +02:00
										 |  |  |     parent->update(); | 
					
						
							| 
									
										
										
										
											2019-04-16 03:47:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GWidget::move_to_back() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto* parent = parent_widget(); | 
					
						
							|  |  |  |     if (!parent) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     if (parent->children().size() == 1) | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-04-16 04:01:14 +02:00
										 |  |  |     parent->children().remove_first_matching([this] (auto& entry) { | 
					
						
							|  |  |  |         return entry == this; | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-04-16 03:47:55 +02:00
										 |  |  |     parent->children().prepend(this); | 
					
						
							| 
									
										
										
										
											2019-04-16 04:01:14 +02:00
										 |  |  |     parent->update(); | 
					
						
							| 
									
										
										
										
											2019-04-16 03:47:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool GWidget::is_frontmost() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto* parent = parent_widget(); | 
					
						
							|  |  |  |     if (!parent) | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     return parent->children().last() == this; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool GWidget::is_backmost() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto* parent = parent_widget(); | 
					
						
							|  |  |  |     if (!parent) | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     return parent->children().first() == this; | 
					
						
							| 
									
										
										
										
											2019-04-12 17:10:30 +02:00
										 |  |  | } |