Window.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/DeprecatedString.h>
  8. #include <AK/Error.h>
  9. #include <AK/Function.h>
  10. #include <AK/NonnullOwnPtr.h>
  11. #include <AK/OwnPtr.h>
  12. #include <AK/Variant.h>
  13. #include <AK/WeakPtr.h>
  14. #include <LibGUI/FocusSource.h>
  15. #include <LibGUI/Forward.h>
  16. #include <LibGUI/Object.h>
  17. #include <LibGUI/ResizeDirection.h>
  18. #include <LibGUI/WindowMode.h>
  19. #include <LibGUI/WindowType.h>
  20. #include <LibGfx/Forward.h>
  21. #include <LibGfx/Rect.h>
  22. #include <LibGfx/StandardCursor.h>
  23. namespace GUI {
  24. class WindowBackingStore;
  25. class Window : public GUI::Object {
  26. C_OBJECT(Window)
  27. public:
  28. virtual ~Window() override;
  29. static Window* from_window_id(int);
  30. bool is_modified() const;
  31. void set_modified(bool);
  32. bool is_modal() const { return m_window_mode != WindowMode::Modeless; }
  33. bool is_blocking() const { return m_window_mode == WindowMode::Blocking; }
  34. bool is_popup() const { return m_window_type == WindowType::Popup; }
  35. bool is_autocomplete() const { return m_window_type == WindowType::Autocomplete; }
  36. bool is_fullscreen() const { return m_fullscreen; }
  37. void set_fullscreen(bool);
  38. bool is_maximized() const { return m_maximized; }
  39. void set_maximized(bool);
  40. bool is_minimized() const { return m_minimized; }
  41. void set_minimized(bool);
  42. bool is_frameless() const { return m_frameless; }
  43. void set_frameless(bool);
  44. void set_forced_shadow(bool);
  45. bool is_resizable() const { return m_resizable; }
  46. void set_resizable(bool resizable) { m_resizable = resizable; }
  47. bool is_obeying_widget_min_size() { return m_obey_widget_min_size; }
  48. void set_obey_widget_min_size(bool);
  49. bool is_auto_shrinking() const { return m_auto_shrink; }
  50. void set_auto_shrink(bool);
  51. bool is_minimizable() const { return m_minimizable; }
  52. void set_minimizable(bool minimizable) { m_minimizable = minimizable; }
  53. bool is_closeable() const { return m_closeable; }
  54. void set_closeable(bool closeable) { m_closeable = closeable; }
  55. void set_double_buffering_enabled(bool);
  56. void set_has_alpha_channel(bool);
  57. bool has_alpha_channel() const { return m_has_alpha_channel; }
  58. void set_alpha_hit_threshold(float);
  59. float alpha_hit_threshold() const { return m_alpha_hit_threshold; }
  60. WindowType window_type() const { return m_window_type; }
  61. void set_window_type(WindowType);
  62. WindowMode window_mode() const { return m_window_mode; }
  63. void set_window_mode(WindowMode);
  64. int window_id() const { return m_window_id; }
  65. void make_window_manager(unsigned event_mask);
  66. DeprecatedString title() const;
  67. void set_title(DeprecatedString);
  68. enum class CloseRequestDecision {
  69. StayOpen,
  70. Close,
  71. };
  72. Function<void()> on_font_change;
  73. Function<void()> on_close;
  74. Function<CloseRequestDecision()> on_close_request;
  75. Function<void(bool is_preempted)> on_input_preemption_change;
  76. Function<void(bool is_active_window)> on_active_window_change;
  77. int x() const { return rect().x(); }
  78. int y() const { return rect().y(); }
  79. int width() const { return rect().width(); }
  80. int height() const { return rect().height(); }
  81. Gfx::IntRect rect() const;
  82. Gfx::IntRect floating_rect() const;
  83. Gfx::IntRect applet_rect_on_screen() const;
  84. Gfx::IntSize size() const { return rect().size(); }
  85. void set_rect(Gfx::IntRect const&);
  86. void set_rect(int x, int y, int width, int height) { set_rect({ x, y, width, height }); }
  87. Gfx::IntPoint position() const { return rect().location(); }
  88. Gfx::IntSize minimum_size() const;
  89. void set_minimum_size(Gfx::IntSize);
  90. void set_minimum_size(int width, int height) { set_minimum_size({ width, height }); }
  91. void move_to(int x, int y) { move_to({ x, y }); }
  92. void move_to(Gfx::IntPoint point) { set_rect({ point, size() }); }
  93. void resize(int width, int height) { resize({ width, height }); }
  94. void resize(Gfx::IntSize size) { set_rect({ position(), size }); }
  95. void center_on_screen();
  96. void constrain_to_desktop();
  97. void center_within(Window const&);
  98. void center_within(Gfx::IntRect const&);
  99. virtual void event(Core::Event&) override;
  100. bool is_visible() const;
  101. bool is_active() const;
  102. bool is_focusable() const { return is_active() || is_popup() || is_autocomplete(); }
  103. void show();
  104. void hide();
  105. virtual void close();
  106. void move_to_front();
  107. void start_interactive_resize(ResizeDirection resize_direction);
  108. Widget* main_widget() { return m_main_widget; }
  109. Widget const* main_widget() const { return m_main_widget; }
  110. void set_main_widget(Widget*);
  111. template<class T, class... Args>
  112. inline NonnullRefPtr<T> set_main_widget(Args&&... args)
  113. {
  114. auto widget = T::construct(forward<Args>(args)...);
  115. set_main_widget(widget.ptr());
  116. return widget;
  117. }
  118. Widget* default_return_key_widget() { return m_default_return_key_widget; }
  119. Widget const* default_return_key_widget() const { return m_default_return_key_widget; }
  120. void set_default_return_key_widget(Widget*);
  121. Widget* focused_widget() { return m_focused_widget; }
  122. Widget const* focused_widget() const { return m_focused_widget; }
  123. void set_focused_widget(Widget*, FocusSource = FocusSource::Programmatic);
  124. void update();
  125. void update(Gfx::IntRect const&);
  126. void set_automatic_cursor_tracking_widget(Widget*);
  127. Widget* automatic_cursor_tracking_widget() { return m_automatic_cursor_tracking_widget.ptr(); }
  128. Widget const* automatic_cursor_tracking_widget() const { return m_automatic_cursor_tracking_widget.ptr(); }
  129. Widget* hovered_widget() { return m_hovered_widget.ptr(); }
  130. Widget const* hovered_widget() const { return m_hovered_widget.ptr(); }
  131. void set_hovered_widget(Widget*);
  132. Gfx::Bitmap* back_bitmap();
  133. Gfx::IntSize size_increment() const { return m_size_increment; }
  134. void set_size_increment(Gfx::IntSize);
  135. Gfx::IntSize base_size() const { return m_base_size; }
  136. void set_base_size(Gfx::IntSize);
  137. Optional<Gfx::IntSize> const& resize_aspect_ratio() const { return m_resize_aspect_ratio; }
  138. void set_resize_aspect_ratio(int width, int height) { set_resize_aspect_ratio(Gfx::IntSize(width, height)); }
  139. void set_no_resize_aspect_ratio() { set_resize_aspect_ratio({}); }
  140. void set_resize_aspect_ratio(Optional<Gfx::IntSize> const& ratio);
  141. void set_cursor(Gfx::StandardCursor);
  142. void set_cursor(NonnullRefPtr<Gfx::Bitmap const>);
  143. void set_icon(Gfx::Bitmap const*);
  144. void apply_icon();
  145. Gfx::Bitmap const* icon() const { return m_icon.ptr(); }
  146. Vector<Widget&> focusable_widgets(FocusSource) const;
  147. void schedule_relayout();
  148. void refresh_system_theme();
  149. static void for_each_window(Badge<ConnectionToWindowServer>, Function<void(Window&)>);
  150. static void update_all_windows(Badge<ConnectionToWindowServer>);
  151. void notify_state_changed(Badge<ConnectionToWindowServer>, bool minimized, bool maximized, bool occluded);
  152. virtual bool is_visible_for_timer_purposes() const override { return m_visible_for_timer_purposes; }
  153. Action* action_for_shortcut(Shortcut const&);
  154. void did_add_widget(Badge<Widget>, Widget&);
  155. void did_remove_widget(Badge<Widget>, Widget&);
  156. Window* find_parent_window();
  157. void set_progress(Optional<int>);
  158. void update_cursor(Badge<Widget>) { update_cursor(); }
  159. void did_disable_focused_widget(Badge<Widget>);
  160. [[nodiscard]] NonnullRefPtr<Menu> add_menu(String name);
  161. void add_menu(NonnullRefPtr<Menu> menu);
  162. void flash_menubar_menu_for(MenuItem const&);
  163. void flush_pending_paints_immediately();
  164. Menubar& menubar() { return *m_menubar; }
  165. Menubar const& menubar() const { return *m_menubar; }
  166. void set_blocks_emoji_input(bool b) { m_blocks_emoji_input = b; }
  167. bool blocks_emoji_input() const { return m_blocks_emoji_input; }
  168. void set_always_on_top(bool always_on_top = true);
  169. enum class ShortcutPropagationBoundary {
  170. Window,
  171. Application,
  172. };
  173. void propagate_shortcuts(KeyEvent& event, Widget* widget, ShortcutPropagationBoundary = ShortcutPropagationBoundary::Application);
  174. void restore_size_and_position(StringView domain, StringView group = "Window"sv, Optional<Gfx::IntSize> fallback_size = {}, Optional<Gfx::IntPoint> fallback_position = {});
  175. void save_size_and_position(StringView domain, StringView group = "Window"sv) const;
  176. void save_size_and_position_on_close(StringView domain, StringView group = "Window"sv);
  177. protected:
  178. Window(Core::EventReceiver* parent = nullptr);
  179. virtual void wm_event(WMEvent&);
  180. virtual void screen_rects_change_event(ScreenRectsChangeEvent&);
  181. virtual void applet_area_rect_change_event(AppletAreaRectChangeEvent&);
  182. virtual void enter_event(Core::Event&);
  183. virtual void leave_event(Core::Event&);
  184. private:
  185. void update_min_size();
  186. void update_cursor();
  187. void focus_a_widget_if_possible(FocusSource);
  188. void handle_drop_event(DropEvent&);
  189. void handle_mouse_event(MouseEvent&);
  190. void handle_multi_paint_event(MultiPaintEvent&);
  191. void handle_key_event(KeyEvent&);
  192. void handle_resize_event(ResizeEvent&);
  193. void handle_input_preemption_event(Core::Event&);
  194. void handle_became_active_or_inactive_event(Core::Event&);
  195. void handle_close_request();
  196. void handle_theme_change_event(ThemeChangeEvent&);
  197. void handle_fonts_change_event(FontsChangeEvent&);
  198. void handle_screen_rects_change_event(ScreenRectsChangeEvent&);
  199. void handle_applet_area_rect_change_event(AppletAreaRectChangeEvent&);
  200. void handle_drag_move_event(DragEvent&);
  201. void handle_entered_event(Core::Event&);
  202. void handle_left_event(Core::Event&);
  203. void server_did_destroy();
  204. ErrorOr<NonnullOwnPtr<WindowBackingStore>> create_backing_store(Gfx::IntSize);
  205. Gfx::IntSize backing_store_size(Gfx::IntSize) const;
  206. void set_current_backing_store(WindowBackingStore&, bool flush_immediately = false) const;
  207. void flip(Vector<Gfx::IntRect, 32> const& dirty_rects);
  208. void force_update();
  209. WeakPtr<Widget> m_previously_focused_widget;
  210. OwnPtr<WindowBackingStore> m_front_store;
  211. OwnPtr<WindowBackingStore> m_back_store;
  212. NonnullRefPtr<Menubar> m_menubar;
  213. RefPtr<Gfx::Bitmap const> m_icon;
  214. int m_window_id { 0 };
  215. float m_alpha_hit_threshold { 0.0f };
  216. RefPtr<Widget> m_main_widget;
  217. WeakPtr<Widget> m_default_return_key_widget;
  218. WeakPtr<Widget> m_focused_widget;
  219. WeakPtr<Widget> m_automatic_cursor_tracking_widget;
  220. WeakPtr<Widget> m_hovered_widget;
  221. Gfx::IntRect m_rect_when_windowless;
  222. Gfx::IntSize m_minimum_size_when_windowless { 0, 0 };
  223. Gfx::IntRect m_floating_rect;
  224. DeprecatedString m_title_when_windowless;
  225. Vector<Gfx::IntRect, 32> m_pending_paint_event_rects;
  226. Gfx::IntSize m_size_increment;
  227. Gfx::IntSize m_base_size;
  228. WindowType m_window_type { WindowType::Normal };
  229. WindowMode m_window_mode { WindowMode::Modeless };
  230. AK::Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap const>> m_cursor { Gfx::StandardCursor::None };
  231. AK::Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap const>> m_effective_cursor { Gfx::StandardCursor::None };
  232. bool m_has_alpha_channel { false };
  233. bool m_double_buffering_enabled { true };
  234. bool m_resizable { true };
  235. bool m_obey_widget_min_size { true };
  236. Optional<Gfx::IntSize> m_resize_aspect_ratio {};
  237. bool m_minimizable { true };
  238. bool m_closeable { true };
  239. bool m_maximized { false };
  240. bool m_minimized { false };
  241. bool m_fullscreen { false };
  242. bool m_frameless { false };
  243. bool m_forced_shadow { false };
  244. bool m_layout_pending { false };
  245. bool m_visible_for_timer_purposes { true };
  246. bool m_visible { false };
  247. bool m_moved_by_client { false };
  248. bool m_blocks_emoji_input { false };
  249. bool m_resizing { false };
  250. bool m_auto_shrink { false };
  251. bool m_save_size_and_position_on_close { false };
  252. StringView m_save_domain;
  253. StringView m_save_group;
  254. };
  255. }
  256. template<>
  257. struct AK::Formatter<GUI::Window> : Formatter<Core::EventReceiver> {
  258. };