Window.h 9.9 KB

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