Window.h 11 KB

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