Window.h 11 KB

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