Serenity Operating System
at hosted 349 lines 12 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#pragma once 28 29#include <AK/String.h> 30#include <LibCore/Object.h> 31#include <LibGUI/Event.h> 32#include <LibGUI/Forward.h> 33#include <LibGfx/Color.h> 34#include <LibGfx/Forward.h> 35#include <LibGfx/Orientation.h> 36#include <LibGfx/Rect.h> 37 38#define REGISTER_WIDGET(class_name) \ 39 extern WidgetClassRegistration registration_##class_name; \ 40 WidgetClassRegistration registration_##class_name(#class_name, []() { return class_name::construct(); }); 41 42template<> 43inline bool Core::is<GUI::Widget>(const Core::Object& object) 44{ 45 return object.is_widget(); 46} 47 48namespace GUI { 49 50enum class SizePolicy { 51 Fixed, 52 Fill 53}; 54inline const char* to_string(SizePolicy policy) 55{ 56 switch (policy) { 57 case SizePolicy::Fixed: 58 return "SizePolicy::Fixed"; 59 case SizePolicy::Fill: 60 return "SizePolicy::Fill"; 61 } 62 return "SizePolicy::(Invalid)"; 63} 64 65enum class HorizontalDirection { 66 Left, 67 Right 68}; 69enum class VerticalDirection { 70 Up, 71 Down 72}; 73 74class WidgetClassRegistration { 75 AK_MAKE_NONCOPYABLE(WidgetClassRegistration) 76 AK_MAKE_NONMOVABLE(WidgetClassRegistration) 77public: 78 WidgetClassRegistration(const String& class_name, Function<NonnullRefPtr<Widget>()> factory); 79 ~WidgetClassRegistration(); 80 81 String class_name() const { return m_class_name; } 82 NonnullRefPtr<Widget> construct() const { return m_factory(); } 83 84 static void for_each(Function<void(const WidgetClassRegistration&)>); 85 static const WidgetClassRegistration* find(const String& class_name); 86 87private: 88 String m_class_name; 89 Function<NonnullRefPtr<Widget>()> m_factory; 90}; 91 92class Widget : public Core::Object { 93 C_OBJECT(Widget) 94public: 95 virtual ~Widget() override; 96 97 Layout* layout() { return m_layout.ptr(); } 98 const Layout* layout() const { return m_layout.ptr(); } 99 void set_layout(NonnullRefPtr<Layout>); 100 101 template<class T, class... Args> 102 inline T& set_layout(Args&&... args) 103 { 104 auto layout = T::construct(forward<Args>(args)...); 105 set_layout(*layout); 106 return layout; 107 } 108 109 SizePolicy horizontal_size_policy() const { return m_horizontal_size_policy; } 110 SizePolicy vertical_size_policy() const { return m_vertical_size_policy; } 111 SizePolicy size_policy(Orientation orientation) { return orientation == Orientation::Horizontal ? m_horizontal_size_policy : m_vertical_size_policy; } 112 void set_size_policy(SizePolicy horizontal_policy, SizePolicy vertical_policy); 113 void set_size_policy(Orientation, SizePolicy); 114 115 Gfx::Size preferred_size() const { return m_preferred_size; } 116 void set_preferred_size(const Gfx::Size&); 117 void set_preferred_size(int width, int height) { set_preferred_size({ width, height }); } 118 119 bool has_tooltip() const { return !m_tooltip.is_empty(); } 120 String tooltip() const { return m_tooltip; } 121 void set_tooltip(const StringView& tooltip) { m_tooltip = tooltip; } 122 123 bool is_enabled() const { return m_enabled; } 124 void set_enabled(bool); 125 126 bool updates_enabled() const { return m_updates_enabled; } 127 void set_updates_enabled(bool); 128 129 virtual void event(Core::Event&) override; 130 131 // This is called after children have been painted. 132 virtual void second_paint_event(PaintEvent&); 133 134 Gfx::Rect relative_rect() const { return m_relative_rect; } 135 Gfx::Point relative_position() const { return m_relative_rect.location(); } 136 137 Gfx::Rect window_relative_rect() const; 138 Gfx::Rect screen_relative_rect() const; 139 140 int x() const { return m_relative_rect.x(); } 141 int y() const { return m_relative_rect.y(); } 142 int width() const { return m_relative_rect.width(); } 143 int height() const { return m_relative_rect.height(); } 144 int length(Orientation orientation) const { return orientation == Orientation::Vertical ? height() : width(); } 145 146 Gfx::Rect rect() const { return { 0, 0, width(), height() }; } 147 Gfx::Size size() const { return m_relative_rect.size(); } 148 149 void update(); 150 void update(const Gfx::Rect&); 151 152 virtual bool accepts_focus() const { return false; } 153 154 bool is_focused() const; 155 void set_focus(bool); 156 157 enum class ShouldRespectGreediness { No = 0, 158 Yes }; 159 struct HitTestResult { 160 Widget* widget { nullptr }; 161 Gfx::Point local_position; 162 }; 163 HitTestResult hit_test(const Gfx::Point&, ShouldRespectGreediness = ShouldRespectGreediness::Yes); 164 Widget* child_at(const Gfx::Point&) const; 165 166 void set_relative_rect(const Gfx::Rect&); 167 void set_relative_rect(int x, int y, int width, int height) { set_relative_rect({ x, y, width, height }); } 168 169 void set_x(int x) { set_relative_rect(x, y(), width(), height()); } 170 void set_y(int y) { set_relative_rect(x(), y, width(), height()); } 171 void set_width(int width) { set_relative_rect(x(), y(), width, height()); } 172 void set_height(int height) { set_relative_rect(x(), y(), width(), height); } 173 174 void move_to(const Gfx::Point& point) { set_relative_rect({ point, relative_rect().size() }); } 175 void move_to(int x, int y) { move_to({ x, y }); } 176 void resize(const Gfx::Size& size) { set_relative_rect({ relative_rect().location(), size }); } 177 void resize(int width, int height) { resize({ width, height }); } 178 179 void move_by(int x, int y) { move_by({ x, y }); } 180 void move_by(const Gfx::Point& delta) { set_relative_rect({ relative_position().translated(delta), size() }); } 181 182 Gfx::ColorRole background_role() const { return m_background_role; } 183 void set_background_role(Gfx::ColorRole); 184 185 Gfx::ColorRole foreground_role() const { return m_foreground_role; } 186 void set_foreground_role(Gfx::ColorRole); 187 188 Color background_color() const { return m_background_color; } 189 Color foreground_color() const { return m_foreground_color; } 190 191 void set_background_color(Color color) { m_background_color = color; } 192 void set_foreground_color(Color color) { m_foreground_color = color; } 193 194 void set_backcolor(const StringView&); 195 void set_forecolor(const StringView&); 196 197 void set_autofill(bool b) { set_fill_with_background_color(b); } 198 199 Window* window() 200 { 201 if (auto* pw = parent_widget()) 202 return pw->window(); 203 return m_window; 204 } 205 206 const Window* window() const 207 { 208 if (auto* pw = parent_widget()) 209 return pw->window(); 210 return m_window; 211 } 212 213 void set_window(Window*); 214 215 Widget* parent_widget(); 216 const Widget* parent_widget() const; 217 218 void set_fill_with_background_color(bool b) { m_fill_with_background_color = b; } 219 bool fill_with_background_color() const { return m_fill_with_background_color; } 220 221 const Gfx::Font& font() const { return *m_font; } 222 void set_font(const Gfx::Font*); 223 void set_font(const Gfx::Font& font) { set_font(&font); } 224 225 void set_global_cursor_tracking(bool); 226 bool global_cursor_tracking() const; 227 228 void notify_layout_changed(Badge<Layout>); 229 void invalidate_layout(); 230 231 bool is_visible() const { return m_visible; } 232 void set_visible(bool); 233 234 bool spans_entire_window_horizontally() const; 235 236 bool is_greedy_for_hits() const { return m_greedy_for_hits; } 237 void set_greedy_for_hits(bool b) { m_greedy_for_hits = b; } 238 239 void move_to_front(); 240 void move_to_back(); 241 242 bool is_frontmost() const; 243 bool is_backmost() const; 244 245 Action* action_for_key_event(const KeyEvent&); 246 247 template<typename Callback> 248 void for_each_child_widget(Callback callback) 249 { 250 for_each_child([&](auto& child) { 251 if (Core::is<Widget>(child)) 252 return callback(Core::to<Widget>(child)); 253 return IterationDecision::Continue; 254 }); 255 } 256 257 Vector<Widget*> child_widgets() const; 258 259 virtual bool is_radio_button() const { return false; } 260 virtual bool is_abstract_button() const { return false; } 261 262 virtual void save_to(AK::JsonObject&) override; 263 264 void do_layout(); 265 266 Gfx::Palette palette() const; 267 void set_palette(const Gfx::Palette&); 268 269protected: 270 Widget(); 271 272 virtual void custom_layout() {} 273 virtual void did_change_font() {} 274 virtual void did_layout() {} 275 virtual void paint_event(PaintEvent&); 276 virtual void resize_event(ResizeEvent&); 277 virtual void show_event(ShowEvent&); 278 virtual void hide_event(HideEvent&); 279 virtual void keydown_event(KeyEvent&); 280 virtual void keyup_event(KeyEvent&); 281 virtual void mousemove_event(MouseEvent&); 282 virtual void mousedown_event(MouseEvent&); 283 virtual void mouseup_event(MouseEvent&); 284 virtual void mousewheel_event(MouseEvent&); 285 virtual void doubleclick_event(MouseEvent&); 286 virtual void context_menu_event(ContextMenuEvent&); 287 virtual void focusin_event(Core::Event&); 288 virtual void focusout_event(Core::Event&); 289 virtual void enter_event(Core::Event&); 290 virtual void leave_event(Core::Event&); 291 virtual void child_event(Core::ChildEvent&) override; 292 virtual void change_event(Event&); 293 virtual void drag_move_event(DragEvent&); 294 virtual void drop_event(DropEvent&); 295 virtual void theme_change_event(ThemeChangeEvent&); 296 297 virtual void did_begin_inspection() override; 298 virtual void did_end_inspection() override; 299 300 virtual bool set_property(const StringView& name, const JsonValue& value) override; 301 302private: 303 void handle_paint_event(PaintEvent&); 304 void handle_resize_event(ResizeEvent&); 305 void handle_mousedown_event(MouseEvent&); 306 void handle_mousedoubleclick_event(MouseEvent&); 307 void handle_mouseup_event(MouseEvent&); 308 void handle_enter_event(Core::Event&); 309 void handle_leave_event(Core::Event&); 310 void focus_previous_widget(); 311 void focus_next_widget(); 312 313 Window* m_window { nullptr }; 314 RefPtr<Layout> m_layout; 315 316 Gfx::Rect m_relative_rect; 317 Gfx::ColorRole m_background_role; 318 Gfx::ColorRole m_foreground_role; 319 Color m_background_color; 320 Color m_foreground_color; 321 NonnullRefPtr<Gfx::Font> m_font; 322 String m_tooltip; 323 324 SizePolicy m_horizontal_size_policy { SizePolicy::Fill }; 325 SizePolicy m_vertical_size_policy { SizePolicy::Fill }; 326 Gfx::Size m_preferred_size; 327 328 bool m_fill_with_background_color { false }; 329 bool m_visible { true }; 330 bool m_greedy_for_hits { false }; 331 bool m_enabled { true }; 332 bool m_updates_enabled { true }; 333 334 NonnullRefPtr<Gfx::PaletteImpl> m_palette; 335}; 336 337inline Widget* Widget::parent_widget() 338{ 339 if (parent() && Core::is<Widget>(*parent())) 340 return &Core::to<Widget>(*parent()); 341 return nullptr; 342} 343inline const Widget* Widget::parent_widget() const 344{ 345 if (parent() && Core::is<Widget>(*parent())) 346 return &Core::to<const Widget>(*parent()); 347 return nullptr; 348} 349}