Window.h 10 KB

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