Window.h 7.9 KB

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