Window.h 8.0 KB

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