Window.h 9.9 KB

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