Window.h 7.7 KB

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