Window.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright (c) 2018-2021, 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 <LibCore/Object.h>
  15. #include <LibGUI/FocusSource.h>
  16. #include <LibGUI/Forward.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 Core::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_minimizable() const { return m_minimizable; }
  50. void set_minimizable(bool minimizable) { m_minimizable = minimizable; }
  51. bool is_closeable() const { return m_closeable; }
  52. void set_closeable(bool closeable) { m_closeable = closeable; }
  53. void set_double_buffering_enabled(bool);
  54. void set_has_alpha_channel(bool);
  55. bool has_alpha_channel() const { return m_has_alpha_channel; }
  56. void set_opacity(float);
  57. float opacity() const { return m_opacity_when_windowless; }
  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_close;
  73. Function<CloseRequestDecision()> on_close_request;
  74. Function<void(bool is_preempted)> on_input_preemption_change;
  75. Function<void(bool is_active_window)> on_active_window_change;
  76. int x() const { return rect().x(); }
  77. int y() const { return rect().y(); }
  78. int width() const { return rect().width(); }
  79. int height() const { return rect().height(); }
  80. Gfx::IntRect rect() const;
  81. Gfx::IntRect applet_rect_on_screen() const;
  82. Gfx::IntSize size() const { return rect().size(); }
  83. void set_rect(Gfx::IntRect const&);
  84. void set_rect(int x, int y, int width, int height) { set_rect({ x, y, width, height }); }
  85. Gfx::IntPoint position() const { return rect().location(); }
  86. Gfx::IntSize minimum_size() const;
  87. void set_minimum_size(Gfx::IntSize);
  88. void set_minimum_size(int width, int height) { set_minimum_size({ width, height }); }
  89. void move_to(int x, int y) { move_to({ x, y }); }
  90. void move_to(Gfx::IntPoint point) { set_rect({ point, size() }); }
  91. void resize(int width, int height) { resize({ width, height }); }
  92. void resize(Gfx::IntSize size) { set_rect({ position(), size }); }
  93. void center_on_screen();
  94. void center_within(Window const&);
  95. virtual void event(Core::Event&) override;
  96. bool is_visible() const;
  97. bool is_active() const;
  98. bool is_focusable() const { return is_active() || is_popup() || is_autocomplete(); }
  99. void show();
  100. void hide();
  101. virtual void close();
  102. void move_to_front();
  103. void start_interactive_resize(ResizeDirection resize_direction);
  104. Widget* main_widget() { return m_main_widget; }
  105. Widget const* main_widget() const { return m_main_widget; }
  106. void set_main_widget(Widget*);
  107. template<class T, class... Args>
  108. inline ErrorOr<NonnullRefPtr<T>> set_main_widget(Args&&... args)
  109. {
  110. auto widget = TRY(T::try_create(forward<Args>(args)...));
  111. set_main_widget(widget.ptr());
  112. return widget;
  113. }
  114. Widget* default_return_key_widget() { return m_default_return_key_widget; }
  115. Widget const* default_return_key_widget() const { return m_default_return_key_widget; }
  116. void set_default_return_key_widget(Widget*);
  117. Widget* focused_widget() { return m_focused_widget; }
  118. Widget const* focused_widget() const { return m_focused_widget; }
  119. void set_focused_widget(Widget*, FocusSource = FocusSource::Programmatic);
  120. void update();
  121. void update(Gfx::IntRect const&);
  122. void set_automatic_cursor_tracking_widget(Widget*);
  123. Widget* automatic_cursor_tracking_widget() { return m_automatic_cursor_tracking_widget.ptr(); }
  124. Widget const* automatic_cursor_tracking_widget() const { return m_automatic_cursor_tracking_widget.ptr(); }
  125. Widget* hovered_widget() { return m_hovered_widget.ptr(); }
  126. Widget const* hovered_widget() const { return m_hovered_widget.ptr(); }
  127. void set_hovered_widget(Widget*);
  128. Gfx::Bitmap* back_bitmap();
  129. Gfx::IntSize size_increment() const { return m_size_increment; }
  130. void set_size_increment(Gfx::IntSize);
  131. Gfx::IntSize base_size() const { return m_base_size; }
  132. void set_base_size(Gfx::IntSize);
  133. Optional<Gfx::IntSize> const& resize_aspect_ratio() const { return m_resize_aspect_ratio; }
  134. void set_resize_aspect_ratio(int width, int height) { set_resize_aspect_ratio(Gfx::IntSize(width, height)); }
  135. void set_no_resize_aspect_ratio() { set_resize_aspect_ratio({}); }
  136. void set_resize_aspect_ratio(Optional<Gfx::IntSize> const& ratio);
  137. void set_cursor(Gfx::StandardCursor);
  138. void set_cursor(NonnullRefPtr<Gfx::Bitmap>);
  139. void set_icon(Gfx::Bitmap const*);
  140. void apply_icon();
  141. Gfx::Bitmap const* icon() const { return m_icon.ptr(); }
  142. Vector<Widget&> focusable_widgets(FocusSource) const;
  143. void schedule_relayout();
  144. void refresh_system_theme();
  145. static void for_each_window(Badge<ConnectionToWindowServer>, Function<void(Window&)>);
  146. static void update_all_windows(Badge<ConnectionToWindowServer>);
  147. void notify_state_changed(Badge<ConnectionToWindowServer>, bool minimized, bool maximized, bool occluded);
  148. virtual bool is_visible_for_timer_purposes() const override { return m_visible_for_timer_purposes; }
  149. Action* action_for_shortcut(Shortcut const&);
  150. void did_add_widget(Badge<Widget>, Widget&);
  151. void did_remove_widget(Badge<Widget>, Widget&);
  152. Window* find_parent_window();
  153. void set_progress(Optional<int>);
  154. void update_cursor(Badge<Widget>) { update_cursor(); }
  155. void did_disable_focused_widget(Badge<Widget>);
  156. Menu& add_menu(DeprecatedString name);
  157. ErrorOr<NonnullRefPtr<Menu>> try_add_menu(DeprecatedString name);
  158. ErrorOr<void> try_add_menu(NonnullRefPtr<Menu> menu);
  159. void flash_menubar_menu_for(MenuItem const&);
  160. void flush_pending_paints_immediately();
  161. Menubar& menubar() { return *m_menubar; }
  162. Menubar const& menubar() const { return *m_menubar; }
  163. void set_blocks_emoji_input(bool b) { m_blocks_emoji_input = b; }
  164. bool blocks_emoji_input() const { return m_blocks_emoji_input; }
  165. void set_always_on_top(bool always_on_top = true);
  166. void propagate_shortcuts_up_to_application(KeyEvent& event, Widget* widget);
  167. protected:
  168. Window(Core::Object* parent = nullptr);
  169. virtual void wm_event(WMEvent&);
  170. virtual void screen_rects_change_event(ScreenRectsChangeEvent&);
  171. virtual void applet_area_rect_change_event(AppletAreaRectChangeEvent&);
  172. virtual void enter_event(Core::Event&);
  173. virtual void leave_event(Core::Event&);
  174. private:
  175. void update_min_size();
  176. void update_cursor();
  177. void focus_a_widget_if_possible(FocusSource);
  178. void handle_drop_event(DropEvent&);
  179. void handle_mouse_event(MouseEvent&);
  180. void handle_multi_paint_event(MultiPaintEvent&);
  181. void handle_key_event(KeyEvent&);
  182. void handle_resize_event(ResizeEvent&);
  183. void handle_input_preemption_event(Core::Event&);
  184. void handle_became_active_or_inactive_event(Core::Event&);
  185. void handle_close_request();
  186. void handle_theme_change_event(ThemeChangeEvent&);
  187. void handle_fonts_change_event(FontsChangeEvent&);
  188. void handle_screen_rects_change_event(ScreenRectsChangeEvent&);
  189. void handle_applet_area_rect_change_event(AppletAreaRectChangeEvent&);
  190. void handle_drag_move_event(DragEvent&);
  191. void handle_entered_event(Core::Event&);
  192. void handle_left_event(Core::Event&);
  193. void server_did_destroy();
  194. ErrorOr<NonnullOwnPtr<WindowBackingStore>> create_backing_store(Gfx::IntSize);
  195. Gfx::IntSize backing_store_size(Gfx::IntSize) const;
  196. void set_current_backing_store(WindowBackingStore&, bool flush_immediately = false) const;
  197. void flip(Vector<Gfx::IntRect, 32> const& dirty_rects);
  198. void force_update();
  199. bool are_cursors_the_same(AK::Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap>> const&, AK::Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap>> const&) const;
  200. WeakPtr<Widget> m_previously_focused_widget;
  201. OwnPtr<WindowBackingStore> m_front_store;
  202. OwnPtr<WindowBackingStore> m_back_store;
  203. NonnullRefPtr<Menubar> m_menubar;
  204. RefPtr<Gfx::Bitmap> m_icon;
  205. int m_window_id { 0 };
  206. float m_opacity_when_windowless { 1.0f };
  207. float m_alpha_hit_threshold { 0.0f };
  208. RefPtr<Widget> m_main_widget;
  209. WeakPtr<Widget> m_default_return_key_widget;
  210. WeakPtr<Widget> m_focused_widget;
  211. WeakPtr<Widget> m_automatic_cursor_tracking_widget;
  212. WeakPtr<Widget> m_hovered_widget;
  213. Gfx::IntRect m_rect_when_windowless;
  214. Gfx::IntSize m_minimum_size_when_windowless { 0, 0 };
  215. DeprecatedString m_title_when_windowless;
  216. Vector<Gfx::IntRect, 32> m_pending_paint_event_rects;
  217. Gfx::IntSize m_size_increment;
  218. Gfx::IntSize m_base_size;
  219. WindowType m_window_type { WindowType::Normal };
  220. WindowMode m_window_mode { WindowMode::Modeless };
  221. AK::Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap>> m_cursor { Gfx::StandardCursor::None };
  222. AK::Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap>> m_effective_cursor { Gfx::StandardCursor::None };
  223. bool m_has_alpha_channel { false };
  224. bool m_double_buffering_enabled { true };
  225. bool m_resizable { true };
  226. bool m_obey_widget_min_size { true };
  227. Optional<Gfx::IntSize> m_resize_aspect_ratio {};
  228. bool m_minimizable { true };
  229. bool m_closeable { true };
  230. bool m_maximized { false };
  231. bool m_minimized { false };
  232. bool m_fullscreen { false };
  233. bool m_frameless { false };
  234. bool m_forced_shadow { false };
  235. bool m_layout_pending { false };
  236. bool m_visible_for_timer_purposes { true };
  237. bool m_visible { false };
  238. bool m_moved_by_client { false };
  239. bool m_blocks_emoji_input { false };
  240. bool m_resizing { false };
  241. };
  242. }
  243. template<>
  244. struct AK::Formatter<GUI::Window> : Formatter<Core::Object> {
  245. };