Window.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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/Forward.h>
  32. #include <LibGUI/WindowType.h>
  33. #include <LibGfx/Color.h>
  34. #include <LibGfx/Forward.h>
  35. #include <LibGfx/Rect.h>
  36. namespace GUI {
  37. enum class StandardCursor {
  38. None = 0,
  39. Arrow,
  40. IBeam,
  41. ResizeHorizontal,
  42. ResizeVertical,
  43. ResizeDiagonalTLBR,
  44. ResizeDiagonalBLTR,
  45. ResizeColumn,
  46. ResizeRow,
  47. Hand,
  48. Help,
  49. Drag,
  50. Move,
  51. Wait,
  52. };
  53. class Window : public Core::Object {
  54. C_OBJECT(Window)
  55. public:
  56. virtual ~Window() override;
  57. static Window* from_window_id(int);
  58. bool is_modal() const { return m_modal; }
  59. void set_modal(bool);
  60. bool is_fullscreen() const { return m_fullscreen; }
  61. void set_fullscreen(bool);
  62. bool is_maximized() const;
  63. bool is_frameless() const { return m_frameless; }
  64. void set_frameless(bool frameless) { m_frameless = frameless; }
  65. bool is_resizable() const { return m_resizable; }
  66. void set_resizable(bool resizable) { m_resizable = resizable; }
  67. bool is_minimizable() const { return m_minimizable; }
  68. void set_minimizable(bool minimizable) { m_minimizable = minimizable; }
  69. void set_double_buffering_enabled(bool);
  70. void set_has_alpha_channel(bool);
  71. void set_opacity(float);
  72. void set_window_type(WindowType);
  73. int window_id() const { return m_window_id; }
  74. String title() const;
  75. void set_title(const StringView&);
  76. Color background_color() const { return m_background_color; }
  77. void set_background_color(Color color) { m_background_color = color; }
  78. enum class CloseRequestDecision {
  79. StayOpen,
  80. Close,
  81. };
  82. Function<CloseRequestDecision()> on_close_request;
  83. Function<void(bool is_active_input)> on_active_input_change;
  84. Function<void(const bool is_active)> on_activity_change;
  85. int x() const { return rect().x(); }
  86. int y() const { return rect().y(); }
  87. int width() const { return rect().width(); }
  88. int height() const { return rect().height(); }
  89. Gfx::IntRect rect() const;
  90. Gfx::IntRect rect_in_menubar() const;
  91. Gfx::IntSize size() const { return rect().size(); }
  92. void set_rect(const Gfx::IntRect&);
  93. void set_rect(int x, int y, int width, int height) { set_rect({ x, y, width, height }); }
  94. Gfx::IntPoint position() const { return rect().location(); }
  95. void move_to(int x, int y) { move_to({ x, y }); }
  96. void move_to(const Gfx::IntPoint& point) { set_rect({ point, size() }); }
  97. void resize(int width, int height) { resize({ width, height }); }
  98. void resize(const Gfx::IntSize& size) { set_rect({ position(), size }); }
  99. virtual void event(Core::Event&) override;
  100. bool is_visible() const;
  101. bool is_active() const { return m_is_active; }
  102. bool is_active_input() const { return m_is_active_input; }
  103. bool is_accessory() const { return m_accessory; }
  104. void set_accessory(bool accessory) { m_accessory = accessory; }
  105. void show();
  106. void hide();
  107. virtual void close();
  108. void move_to_front();
  109. void start_wm_resize();
  110. Widget* main_widget() { return m_main_widget; }
  111. const Widget* main_widget() const { return m_main_widget; }
  112. void set_main_widget(Widget*);
  113. template<class T, class... Args>
  114. inline T& set_main_widget(Args&&... args)
  115. {
  116. auto widget = T::construct(forward<Args>(args)...);
  117. set_main_widget(widget.ptr());
  118. return *widget;
  119. }
  120. Widget* focused_widget() { return m_focused_widget; }
  121. const Widget* focused_widget() const { return m_focused_widget; }
  122. void set_focused_widget(Widget*);
  123. void update();
  124. void update(const Gfx::IntRect&);
  125. void set_global_cursor_tracking_widget(Widget*);
  126. Widget* global_cursor_tracking_widget() { return m_global_cursor_tracking_widget.ptr(); }
  127. const Widget* global_cursor_tracking_widget() const { return m_global_cursor_tracking_widget.ptr(); }
  128. void set_automatic_cursor_tracking_widget(Widget*);
  129. Widget* automatic_cursor_tracking_widget() { return m_automatic_cursor_tracking_widget.ptr(); }
  130. const Widget* automatic_cursor_tracking_widget() const { return m_automatic_cursor_tracking_widget.ptr(); }
  131. Widget* hovered_widget() { return m_hovered_widget.ptr(); }
  132. const Widget* hovered_widget() const { return m_hovered_widget.ptr(); }
  133. void set_hovered_widget(Widget*);
  134. Gfx::Bitmap* front_bitmap() { return m_front_bitmap.ptr(); }
  135. Gfx::Bitmap* back_bitmap() { return m_back_bitmap.ptr(); }
  136. Gfx::IntSize size_increment() const { return m_size_increment; }
  137. void set_size_increment(const Gfx::IntSize&);
  138. Gfx::IntSize base_size() const { return m_base_size; }
  139. void set_base_size(const Gfx::IntSize&);
  140. void set_override_cursor(StandardCursor);
  141. void set_override_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() const;
  146. virtual void save_to(AK::JsonObject&) override;
  147. void schedule_relayout();
  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. protected:
  158. Window(Core::Object* parent = nullptr);
  159. virtual void wm_event(WMEvent&);
  160. private:
  161. virtual bool is_window() const override final { return true; }
  162. void handle_drop_event(DropEvent&);
  163. void handle_mouse_event(MouseEvent&);
  164. void handle_multi_paint_event(MultiPaintEvent&);
  165. void handle_key_event(KeyEvent&);
  166. void handle_resize_event(ResizeEvent&);
  167. void handle_input_entered_or_left_event(Core::Event&);
  168. void handle_became_active_or_inactive_event(Core::Event&);
  169. void handle_close_request();
  170. void handle_theme_change_event(ThemeChangeEvent&);
  171. void handle_drag_move_event(DragEvent&);
  172. void handle_left_event();
  173. void server_did_destroy();
  174. RefPtr<Gfx::Bitmap> create_backing_bitmap(const Gfx::IntSize&);
  175. RefPtr<Gfx::Bitmap> create_shared_bitmap(Gfx::BitmapFormat, const Gfx::IntSize&);
  176. void set_current_backing_bitmap(Gfx::Bitmap&, bool flush_immediately = false);
  177. void flip(const Vector<Gfx::IntRect, 32>& dirty_rects);
  178. void force_update();
  179. RefPtr<Gfx::Bitmap> m_front_bitmap;
  180. RefPtr<Gfx::Bitmap> m_back_bitmap;
  181. RefPtr<Gfx::Bitmap> m_icon;
  182. RefPtr<Gfx::Bitmap> m_custom_cursor;
  183. int m_window_id { 0 };
  184. float m_opacity_when_windowless { 1.0f };
  185. RefPtr<Widget> m_main_widget;
  186. WeakPtr<Widget> m_focused_widget;
  187. WeakPtr<Widget> m_global_cursor_tracking_widget;
  188. WeakPtr<Widget> m_automatic_cursor_tracking_widget;
  189. WeakPtr<Widget> m_hovered_widget;
  190. Gfx::IntRect m_rect_when_windowless;
  191. String m_title_when_windowless;
  192. Vector<Gfx::IntRect, 32> m_pending_paint_event_rects;
  193. Gfx::IntSize m_size_increment;
  194. Gfx::IntSize m_base_size;
  195. Color m_background_color { Color::WarmGray };
  196. WindowType m_window_type { WindowType::Normal };
  197. StandardCursor m_override_cursor { StandardCursor::None };
  198. bool m_is_active { false };
  199. bool m_is_active_input { false };
  200. bool m_has_alpha_channel { false };
  201. bool m_double_buffering_enabled { true };
  202. bool m_modal { false };
  203. bool m_resizable { true };
  204. bool m_minimizable { true };
  205. bool m_fullscreen { false };
  206. bool m_frameless { false };
  207. bool m_layout_pending { false };
  208. bool m_visible_for_timer_purposes { true };
  209. bool m_visible { false };
  210. bool m_accessory { false };
  211. };
  212. }
  213. template<>
  214. inline bool Core::is<GUI::Window>(const Core::Object& object)
  215. {
  216. return object.is_window();
  217. }