Window.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. };
  52. class Window : public Core::Object {
  53. C_OBJECT(Window)
  54. public:
  55. virtual ~Window() override;
  56. static Window* from_window_id(int);
  57. bool is_modal() const { return m_modal; }
  58. void set_modal(bool);
  59. bool is_fullscreen() const { return m_fullscreen; }
  60. void set_fullscreen(bool);
  61. bool is_maximized() const;
  62. bool is_frameless() const { return m_frameless; }
  63. void set_frameless(bool frameless) { m_frameless = frameless; }
  64. bool is_resizable() const { return m_resizable; }
  65. void set_resizable(bool resizable) { m_resizable = resizable; }
  66. bool is_minimizable() const { return m_minimizable; }
  67. void set_minimizable(bool minimizable) { m_minimizable = minimizable; }
  68. void set_double_buffering_enabled(bool);
  69. void set_has_alpha_channel(bool);
  70. void set_opacity(float);
  71. void set_window_type(WindowType);
  72. int window_id() const { return m_window_id; }
  73. String title() const;
  74. void set_title(const StringView&);
  75. Color background_color() const { return m_background_color; }
  76. void set_background_color(Color color) { m_background_color = color; }
  77. enum class CloseRequestDecision {
  78. StayOpen,
  79. Close,
  80. };
  81. Function<CloseRequestDecision()> on_close_request;
  82. int x() const { return rect().x(); }
  83. int y() const { return rect().y(); }
  84. int width() const { return rect().width(); }
  85. int height() const { return rect().height(); }
  86. Gfx::IntRect rect() const;
  87. Gfx::IntSize size() const { return rect().size(); }
  88. void set_rect(const Gfx::IntRect&);
  89. void set_rect(int x, int y, int width, int height) { set_rect({ x, y, width, height }); }
  90. Gfx::IntPoint position() const { return rect().location(); }
  91. void move_to(int x, int y) { move_to({ x, y }); }
  92. void move_to(const Gfx::IntPoint& point) { set_rect({ point, size() }); }
  93. void resize(int width, int height) { resize({ width, height }); }
  94. void resize(const Gfx::IntSize& size) { set_rect({ position(), size }); }
  95. virtual void event(Core::Event&) override;
  96. bool is_visible() const;
  97. bool is_active() const { return m_is_active; }
  98. void show();
  99. void hide();
  100. virtual void close();
  101. void move_to_front();
  102. void start_wm_resize();
  103. Widget* main_widget() { return m_main_widget; }
  104. const Widget* main_widget() const { return m_main_widget; }
  105. void set_main_widget(Widget*);
  106. template<class T, class... Args>
  107. inline T& set_main_widget(Args&&... args)
  108. {
  109. auto widget = T::construct(forward<Args>(args)...);
  110. set_main_widget(widget.ptr());
  111. return *widget;
  112. }
  113. Widget* focused_widget() { return m_focused_widget; }
  114. const Widget* focused_widget() const { return m_focused_widget; }
  115. void set_focused_widget(Widget*);
  116. void update();
  117. void update(const Gfx::IntRect&);
  118. void set_global_cursor_tracking_widget(Widget*);
  119. Widget* global_cursor_tracking_widget() { return m_global_cursor_tracking_widget.ptr(); }
  120. const Widget* global_cursor_tracking_widget() const { return m_global_cursor_tracking_widget.ptr(); }
  121. void set_automatic_cursor_tracking_widget(Widget*);
  122. Widget* automatic_cursor_tracking_widget() { return m_automatic_cursor_tracking_widget.ptr(); }
  123. const Widget* automatic_cursor_tracking_widget() const { return m_automatic_cursor_tracking_widget.ptr(); }
  124. Widget* hovered_widget() { return m_hovered_widget.ptr(); }
  125. const Widget* hovered_widget() const { return m_hovered_widget.ptr(); }
  126. void set_hovered_widget(Widget*);
  127. Gfx::Bitmap* front_bitmap() { return m_front_bitmap.ptr(); }
  128. Gfx::Bitmap* back_bitmap() { return m_back_bitmap.ptr(); }
  129. Gfx::IntSize size_increment() const { return m_size_increment; }
  130. void set_size_increment(const Gfx::IntSize&);
  131. Gfx::IntSize base_size() const { return m_base_size; }
  132. void set_base_size(const Gfx::IntSize&);
  133. void set_override_cursor(StandardCursor);
  134. void set_override_cursor(const Gfx::Bitmap&);
  135. void set_icon(const Gfx::Bitmap*);
  136. void apply_icon();
  137. const Gfx::Bitmap* icon() const { return m_icon.ptr(); }
  138. Vector<Widget*> focusable_widgets() const;
  139. virtual void save_to(AK::JsonObject&) override;
  140. void schedule_relayout();
  141. static void for_each_window(Badge<WindowServerConnection>, Function<void(Window&)>);
  142. static void update_all_windows(Badge<WindowServerConnection>);
  143. void notify_state_changed(Badge<WindowServerConnection>, bool minimized, bool occluded);
  144. virtual bool is_visible_for_timer_purposes() const override { return m_visible_for_timer_purposes; }
  145. Action* action_for_key_event(const KeyEvent&);
  146. void did_add_widget(Badge<Widget>, Widget&);
  147. void did_remove_widget(Badge<Widget>, Widget&);
  148. Window* find_parent_window();
  149. void set_progress(int);
  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 server_did_destroy();
  156. RefPtr<Gfx::Bitmap> create_backing_bitmap(const Gfx::IntSize&);
  157. RefPtr<Gfx::Bitmap> create_shared_bitmap(Gfx::BitmapFormat, const Gfx::IntSize&);
  158. void set_current_backing_bitmap(Gfx::Bitmap&, bool flush_immediately = false);
  159. void flip(const Vector<Gfx::IntRect, 32>& dirty_rects);
  160. void force_update();
  161. RefPtr<Gfx::Bitmap> m_front_bitmap;
  162. RefPtr<Gfx::Bitmap> m_back_bitmap;
  163. RefPtr<Gfx::Bitmap> m_icon;
  164. RefPtr<Gfx::Bitmap> m_custom_cursor;
  165. int m_window_id { 0 };
  166. float m_opacity_when_windowless { 1.0f };
  167. RefPtr<Widget> m_main_widget;
  168. WeakPtr<Widget> m_focused_widget;
  169. WeakPtr<Widget> m_global_cursor_tracking_widget;
  170. WeakPtr<Widget> m_automatic_cursor_tracking_widget;
  171. WeakPtr<Widget> m_hovered_widget;
  172. Gfx::IntRect m_rect_when_windowless;
  173. String m_title_when_windowless;
  174. Vector<Gfx::IntRect, 32> m_pending_paint_event_rects;
  175. Gfx::IntSize m_size_increment;
  176. Gfx::IntSize m_base_size;
  177. Color m_background_color { Color::WarmGray };
  178. WindowType m_window_type { WindowType::Normal };
  179. StandardCursor m_override_cursor { StandardCursor::None };
  180. bool m_is_active { false };
  181. bool m_has_alpha_channel { false };
  182. bool m_double_buffering_enabled { true };
  183. bool m_modal { false };
  184. bool m_resizable { true };
  185. bool m_minimizable { true };
  186. bool m_fullscreen { false };
  187. bool m_frameless { false };
  188. bool m_layout_pending { false };
  189. bool m_visible_for_timer_purposes { true };
  190. bool m_visible { false };
  191. };
  192. }
  193. template<>
  194. inline bool Core::is<GUI::Window>(const Core::Object& object)
  195. {
  196. return object.is_window();
  197. }