2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2021-05-01 13:22:29 -03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2018-10-11 20:03:22 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
2020-02-14 18:29:06 -03:00
|
|
|
#include <AK/Function.h>
|
2021-05-19 09:35:34 -03:00
|
|
|
#include <AK/OwnPtr.h>
|
2019-12-23 16:24:26 -03:00
|
|
|
#include <AK/String.h>
|
2019-05-28 06:53:16 -03:00
|
|
|
#include <AK/WeakPtr.h>
|
2020-02-06 11:04:03 -03:00
|
|
|
#include <LibCore/Object.h>
|
2020-08-14 14:56:40 -03:00
|
|
|
#include <LibGUI/FocusSource.h>
|
2020-02-16 05:17:49 -03:00
|
|
|
#include <LibGUI/Forward.h>
|
2020-02-14 18:29:06 -03:00
|
|
|
#include <LibGUI/WindowType.h>
|
2020-02-14 19:02:47 -03:00
|
|
|
#include <LibGfx/Color.h>
|
|
|
|
|
#include <LibGfx/Forward.h>
|
2020-02-06 08:04:00 -03:00
|
|
|
#include <LibGfx/Rect.h>
|
2020-09-10 14:25:13 -03:00
|
|
|
#include <LibGfx/StandardCursor.h>
|
2018-10-11 20:03:22 -03:00
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
namespace GUI {
|
2019-01-20 02:48:43 -02:00
|
|
|
|
2021-01-15 05:38:12 -03:00
|
|
|
class WindowBackingStore;
|
|
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
class Window : public Core::Object {
|
|
|
|
|
C_OBJECT(Window)
|
2018-10-11 20:03:22 -03:00
|
|
|
public:
|
2020-02-02 11:07:41 -03:00
|
|
|
virtual ~Window() override;
|
2018-10-11 20:03:22 -03:00
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
static Window* from_window_id(int);
|
2019-01-20 02:48:43 -02:00
|
|
|
|
2021-05-01 13:22:29 -03:00
|
|
|
bool is_modified() const;
|
|
|
|
|
void set_modified(bool);
|
|
|
|
|
|
2019-03-18 20:01:02 -03:00
|
|
|
bool is_modal() const { return m_modal; }
|
|
|
|
|
void set_modal(bool);
|
|
|
|
|
|
2019-05-17 16:33:44 -03:00
|
|
|
bool is_fullscreen() const { return m_fullscreen; }
|
2019-09-16 13:38:42 -03:00
|
|
|
void set_fullscreen(bool);
|
2019-05-17 16:33:44 -03:00
|
|
|
|
2020-06-18 07:31:29 -03:00
|
|
|
bool is_maximized() const;
|
2021-06-06 16:50:06 -03:00
|
|
|
void set_maximized(bool);
|
2020-06-18 07:31:29 -03:00
|
|
|
|
2020-05-01 18:26:32 -03:00
|
|
|
bool is_frameless() const { return m_frameless; }
|
2021-02-21 03:10:21 -03:00
|
|
|
void set_frameless(bool);
|
2020-05-01 18:26:32 -03:00
|
|
|
|
2021-07-04 18:10:53 -03:00
|
|
|
void set_forced_shadow(bool);
|
|
|
|
|
|
2019-04-20 16:24:02 -03:00
|
|
|
bool is_resizable() const { return m_resizable; }
|
|
|
|
|
void set_resizable(bool resizable) { m_resizable = resizable; }
|
|
|
|
|
|
2020-01-04 08:12:02 -03:00
|
|
|
bool is_minimizable() const { return m_minimizable; }
|
|
|
|
|
void set_minimizable(bool minimizable) { m_minimizable = minimizable; }
|
|
|
|
|
|
2019-03-17 00:23:54 -03:00
|
|
|
void set_double_buffering_enabled(bool);
|
2019-02-18 21:42:53 -03:00
|
|
|
void set_has_alpha_channel(bool);
|
2021-02-11 21:32:03 -03:00
|
|
|
bool has_alpha_channel() const { return m_has_alpha_channel; }
|
2019-02-18 21:42:53 -03:00
|
|
|
void set_opacity(float);
|
2021-02-11 21:32:03 -03:00
|
|
|
float opacity() const { return m_opacity_when_windowless; }
|
2020-08-13 16:11:41 -03:00
|
|
|
|
2021-02-14 20:42:37 -03:00
|
|
|
void set_alpha_hit_threshold(float);
|
|
|
|
|
float alpha_hit_threshold() const { return m_alpha_hit_threshold; }
|
|
|
|
|
|
2020-08-13 16:11:41 -03:00
|
|
|
WindowType window_type() const { return m_window_type; }
|
2020-02-02 11:07:41 -03:00
|
|
|
void set_window_type(WindowType);
|
2019-02-18 21:42:53 -03:00
|
|
|
|
2019-01-14 11:21:51 -02:00
|
|
|
int window_id() const { return m_window_id; }
|
|
|
|
|
|
2021-04-14 18:38:53 -03:00
|
|
|
void make_window_manager(unsigned event_mask);
|
|
|
|
|
|
2019-01-26 02:20:32 -02:00
|
|
|
String title() const;
|
2021-04-16 15:06:43 -03:00
|
|
|
void set_title(String);
|
2018-10-11 20:03:22 -03:00
|
|
|
|
2019-04-10 09:29:47 -03:00
|
|
|
Color background_color() const { return m_background_color; }
|
|
|
|
|
void set_background_color(Color color) { m_background_color = color; }
|
|
|
|
|
|
2019-08-27 15:35:37 -03:00
|
|
|
enum class CloseRequestDecision {
|
|
|
|
|
StayOpen,
|
|
|
|
|
Close,
|
|
|
|
|
};
|
|
|
|
|
|
2021-01-01 20:32:26 -03:00
|
|
|
Function<void()> on_close;
|
2019-08-27 15:35:37 -03:00
|
|
|
Function<CloseRequestDecision()> on_close_request;
|
2020-07-14 22:17:00 -03:00
|
|
|
Function<void(bool is_active_input)> on_active_input_change;
|
2021-08-04 19:21:39 -03:00
|
|
|
Function<void(bool is_active_window)> on_active_window_change;
|
2019-08-27 15:35:37 -03:00
|
|
|
|
2019-01-24 20:40:12 -02:00
|
|
|
int x() const { return rect().x(); }
|
|
|
|
|
int y() const { return rect().y(); }
|
|
|
|
|
int width() const { return rect().width(); }
|
|
|
|
|
int height() const { return rect().height(); }
|
2018-10-11 20:03:22 -03:00
|
|
|
|
2020-06-10 05:57:59 -03:00
|
|
|
Gfx::IntRect rect() const;
|
2021-03-30 18:30:50 -03:00
|
|
|
Gfx::IntRect applet_rect_on_screen() const;
|
2020-06-10 05:57:59 -03:00
|
|
|
Gfx::IntSize size() const { return rect().size(); }
|
|
|
|
|
void set_rect(const Gfx::IntRect&);
|
2019-02-07 21:14:37 -02:00
|
|
|
void set_rect(int x, int y, int width, int height) { set_rect({ x, y, width, height }); }
|
2018-10-11 20:03:22 -03:00
|
|
|
|
2020-06-10 05:57:59 -03:00
|
|
|
Gfx::IntPoint position() const { return rect().location(); }
|
2018-10-11 21:24:05 -03:00
|
|
|
|
2021-02-15 10:59:43 -03:00
|
|
|
Gfx::IntSize minimum_size() const;
|
|
|
|
|
void set_minimum_size(const Gfx::IntSize&);
|
|
|
|
|
void set_minimum_size(int width, int height) { set_minimum_size({ width, height }); }
|
|
|
|
|
|
2019-02-11 03:09:54 -02:00
|
|
|
void move_to(int x, int y) { move_to({ x, y }); }
|
2020-06-10 05:57:59 -03:00
|
|
|
void move_to(const Gfx::IntPoint& point) { set_rect({ point, size() }); }
|
2019-02-11 03:09:54 -02:00
|
|
|
|
2019-04-26 12:33:20 -03:00
|
|
|
void resize(int width, int height) { resize({ width, height }); }
|
2020-06-10 05:57:59 -03:00
|
|
|
void resize(const Gfx::IntSize& size) { set_rect({ position(), size }); }
|
2019-04-26 12:33:20 -03:00
|
|
|
|
2020-08-15 11:32:11 -03:00
|
|
|
void center_on_screen();
|
2020-12-30 09:41:53 -03:00
|
|
|
void center_within(const Window&);
|
2020-08-15 11:32:11 -03:00
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
virtual void event(Core::Event&) override;
|
2018-10-11 20:26:20 -03:00
|
|
|
|
2019-01-16 10:49:44 -02:00
|
|
|
bool is_visible() const;
|
2021-01-05 11:43:59 -03:00
|
|
|
bool is_active() const;
|
2020-07-14 22:17:00 -03:00
|
|
|
bool is_active_input() const { return m_is_active_input; }
|
|
|
|
|
|
|
|
|
|
bool is_accessory() const { return m_accessory; }
|
|
|
|
|
void set_accessory(bool accessory) { m_accessory = accessory; }
|
2018-10-13 20:23:01 -03:00
|
|
|
|
2019-01-30 17:03:52 -02:00
|
|
|
void show();
|
|
|
|
|
void hide();
|
2019-07-26 11:13:59 -03:00
|
|
|
virtual void close();
|
2019-06-01 15:10:37 -03:00
|
|
|
void move_to_front();
|
2018-10-13 19:21:42 -03:00
|
|
|
|
2021-02-01 07:23:54 -03:00
|
|
|
void start_interactive_resize();
|
2019-05-02 20:38:24 -03:00
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
Widget* main_widget() { return m_main_widget; }
|
|
|
|
|
const Widget* main_widget() const { return m_main_widget; }
|
|
|
|
|
void set_main_widget(Widget*);
|
2019-01-26 08:24:16 -02:00
|
|
|
|
2020-03-03 17:42:48 -03:00
|
|
|
template<class T, class... Args>
|
|
|
|
|
inline T& set_main_widget(Args&&... args)
|
|
|
|
|
{
|
|
|
|
|
auto widget = T::construct(forward<Args>(args)...);
|
|
|
|
|
set_main_widget(widget.ptr());
|
|
|
|
|
return *widget;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
Widget* focused_widget() { return m_focused_widget; }
|
|
|
|
|
const Widget* focused_widget() const { return m_focused_widget; }
|
2020-08-14 14:56:40 -03:00
|
|
|
void set_focused_widget(Widget*, FocusSource = FocusSource::Programmatic);
|
2019-01-20 02:48:43 -02:00
|
|
|
|
2019-12-27 07:34:40 -03:00
|
|
|
void update();
|
2020-06-10 05:57:59 -03:00
|
|
|
void update(const Gfx::IntRect&);
|
2019-01-20 02:48:43 -02:00
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
void set_automatic_cursor_tracking_widget(Widget*);
|
|
|
|
|
Widget* automatic_cursor_tracking_widget() { return m_automatic_cursor_tracking_widget.ptr(); }
|
|
|
|
|
const Widget* automatic_cursor_tracking_widget() const { return m_automatic_cursor_tracking_widget.ptr(); }
|
2019-03-24 11:01:56 -03:00
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
Widget* hovered_widget() { return m_hovered_widget.ptr(); }
|
|
|
|
|
const Widget* hovered_widget() const { return m_hovered_widget.ptr(); }
|
|
|
|
|
void set_hovered_widget(Widget*);
|
2019-02-20 06:12:19 -03:00
|
|
|
|
2021-01-15 05:38:12 -03:00
|
|
|
Gfx::Bitmap* back_bitmap();
|
2019-02-20 17:59:13 -03:00
|
|
|
|
2020-06-10 05:57:59 -03:00
|
|
|
Gfx::IntSize size_increment() const { return m_size_increment; }
|
|
|
|
|
void set_size_increment(const Gfx::IntSize&);
|
|
|
|
|
Gfx::IntSize base_size() const { return m_base_size; }
|
|
|
|
|
void set_base_size(const Gfx::IntSize&);
|
2020-08-21 17:19:10 -03:00
|
|
|
const Optional<Gfx::IntSize>& resize_aspect_ratio() const { return m_resize_aspect_ratio; }
|
|
|
|
|
void set_resize_aspect_ratio(int width, int height) { set_resize_aspect_ratio(Gfx::IntSize(width, height)); }
|
|
|
|
|
void set_no_resize_aspect_ratio() { set_resize_aspect_ratio({}); }
|
|
|
|
|
void set_resize_aspect_ratio(const Optional<Gfx::IntSize>& ratio);
|
2019-02-20 20:21:23 -03:00
|
|
|
|
2020-09-10 15:55:39 -03:00
|
|
|
void set_cursor(Gfx::StandardCursor);
|
|
|
|
|
void set_cursor(const Gfx::Bitmap&);
|
2019-03-31 18:52:02 -03:00
|
|
|
|
2020-02-06 07:56:38 -03:00
|
|
|
void set_icon(const Gfx::Bitmap*);
|
2019-08-02 16:02:56 -03:00
|
|
|
void apply_icon();
|
2020-02-06 07:56:38 -03:00
|
|
|
const Gfx::Bitmap* icon() const { return m_icon.ptr(); }
|
2019-07-28 05:18:49 -03:00
|
|
|
|
2021-06-08 12:06:27 -03:00
|
|
|
Vector<Widget&> focusable_widgets(FocusSource) const;
|
2019-05-14 21:39:58 -03:00
|
|
|
|
2019-10-26 07:27:01 -03:00
|
|
|
void schedule_relayout();
|
|
|
|
|
|
2021-01-16 01:56:41 -03:00
|
|
|
void refresh_system_theme();
|
|
|
|
|
|
2020-03-16 08:36:21 -03:00
|
|
|
static void for_each_window(Badge<WindowServerConnection>, Function<void(Window&)>);
|
2020-02-02 11:07:41 -03:00
|
|
|
static void update_all_windows(Badge<WindowServerConnection>);
|
|
|
|
|
void notify_state_changed(Badge<WindowServerConnection>, bool minimized, bool occluded);
|
2019-12-23 16:24:26 -03:00
|
|
|
|
2019-12-29 11:58:07 -03:00
|
|
|
virtual bool is_visible_for_timer_purposes() const override { return m_visible_for_timer_purposes; }
|
|
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
Action* action_for_key_event(const KeyEvent&);
|
2020-02-01 21:57:57 -03:00
|
|
|
|
2020-05-21 12:26:09 -03:00
|
|
|
void did_add_widget(Badge<Widget>, Widget&);
|
|
|
|
|
void did_remove_widget(Badge<Widget>, Widget&);
|
2020-03-05 11:30:11 -03:00
|
|
|
|
2020-05-01 17:59:38 -03:00
|
|
|
Window* find_parent_window();
|
|
|
|
|
|
2021-05-02 05:42:25 -03:00
|
|
|
void set_progress(Optional<int>);
|
2020-05-30 17:08:26 -03:00
|
|
|
|
2020-09-11 09:17:35 -03:00
|
|
|
void update_cursor(Badge<Widget>) { update_cursor(); }
|
|
|
|
|
|
2020-12-27 20:33:46 -03:00
|
|
|
void did_disable_focused_widget(Badge<Widget>);
|
|
|
|
|
|
2021-07-21 16:21:03 -03:00
|
|
|
Menu& add_menu(String name);
|
2021-03-25 17:01:19 -03:00
|
|
|
|
2019-04-03 16:03:12 -03:00
|
|
|
protected:
|
2020-02-02 11:07:41 -03:00
|
|
|
Window(Core::Object* parent = nullptr);
|
|
|
|
|
virtual void wm_event(WMEvent&);
|
2021-06-13 09:16:06 -03:00
|
|
|
virtual void screen_rects_change_event(ScreenRectsChangeEvent&);
|
2021-08-24 09:01:01 -03:00
|
|
|
virtual void applet_area_rect_change_event(AppletAreaRectChangeEvent&);
|
2019-04-03 16:03:12 -03:00
|
|
|
|
2021-07-25 18:07:55 -03:00
|
|
|
virtual void enter_event(Core::Event&);
|
|
|
|
|
virtual void leave_event(Core::Event&);
|
|
|
|
|
|
2019-03-20 18:31:21 -03:00
|
|
|
private:
|
2020-09-11 09:17:35 -03:00
|
|
|
void update_cursor();
|
2020-10-30 12:57:51 -03:00
|
|
|
void focus_a_widget_if_possible(FocusSource);
|
2020-09-11 09:17:35 -03:00
|
|
|
|
2020-07-23 20:03:24 -03:00
|
|
|
void handle_drop_event(DropEvent&);
|
|
|
|
|
void handle_mouse_event(MouseEvent&);
|
|
|
|
|
void handle_multi_paint_event(MultiPaintEvent&);
|
|
|
|
|
void handle_key_event(KeyEvent&);
|
|
|
|
|
void handle_resize_event(ResizeEvent&);
|
|
|
|
|
void handle_input_entered_or_left_event(Core::Event&);
|
|
|
|
|
void handle_became_active_or_inactive_event(Core::Event&);
|
|
|
|
|
void handle_close_request();
|
|
|
|
|
void handle_theme_change_event(ThemeChangeEvent&);
|
2021-07-12 04:57:34 -03:00
|
|
|
void handle_fonts_change_event(FontsChangeEvent&);
|
2021-06-13 09:16:06 -03:00
|
|
|
void handle_screen_rects_change_event(ScreenRectsChangeEvent&);
|
2021-08-24 09:01:01 -03:00
|
|
|
void handle_applet_area_rect_change_event(AppletAreaRectChangeEvent&);
|
2020-07-23 20:03:24 -03:00
|
|
|
void handle_drag_move_event(DragEvent&);
|
2021-07-25 18:07:55 -03:00
|
|
|
void handle_entered_event(Core::Event&);
|
|
|
|
|
void handle_left_event(Core::Event&);
|
2020-07-23 20:03:24 -03:00
|
|
|
|
2020-05-01 18:53:05 -03:00
|
|
|
void server_did_destroy();
|
|
|
|
|
|
2021-01-15 05:38:12 -03:00
|
|
|
OwnPtr<WindowBackingStore> create_backing_store(const Gfx::IntSize&);
|
|
|
|
|
void set_current_backing_store(WindowBackingStore&, bool flush_immediately = false);
|
2020-06-10 05:57:59 -03:00
|
|
|
void flip(const Vector<Gfx::IntRect, 32>& dirty_rects);
|
2020-02-19 12:45:06 -03:00
|
|
|
void force_update();
|
2019-03-17 00:23:54 -03:00
|
|
|
|
2021-06-02 01:08:47 -03:00
|
|
|
WeakPtr<Widget> m_previously_focused_widget;
|
|
|
|
|
|
2021-01-15 05:38:12 -03:00
|
|
|
OwnPtr<WindowBackingStore> m_front_store;
|
|
|
|
|
OwnPtr<WindowBackingStore> m_back_store;
|
|
|
|
|
|
2021-07-29 07:14:12 -03:00
|
|
|
NonnullRefPtr<Menubar> m_menubar;
|
2021-03-25 17:01:19 -03:00
|
|
|
|
2020-02-06 07:56:38 -03:00
|
|
|
RefPtr<Gfx::Bitmap> m_icon;
|
2020-05-15 22:04:09 -03:00
|
|
|
RefPtr<Gfx::Bitmap> m_custom_cursor;
|
2019-01-30 17:03:52 -02:00
|
|
|
int m_window_id { 0 };
|
2019-02-18 21:42:53 -03:00
|
|
|
float m_opacity_when_windowless { 1.0f };
|
2021-02-14 20:42:37 -03:00
|
|
|
float m_alpha_hit_threshold { 0.0f };
|
2020-02-02 11:07:41 -03:00
|
|
|
RefPtr<Widget> m_main_widget;
|
|
|
|
|
WeakPtr<Widget> m_focused_widget;
|
|
|
|
|
WeakPtr<Widget> m_automatic_cursor_tracking_widget;
|
|
|
|
|
WeakPtr<Widget> m_hovered_widget;
|
2020-06-10 05:57:59 -03:00
|
|
|
Gfx::IntRect m_rect_when_windowless;
|
2021-02-15 10:59:43 -03:00
|
|
|
Gfx::IntSize m_minimum_size_when_windowless { 50, 50 };
|
|
|
|
|
bool m_minimum_size_modified { false };
|
2019-01-30 17:03:52 -02:00
|
|
|
String m_title_when_windowless;
|
2020-06-10 05:57:59 -03:00
|
|
|
Vector<Gfx::IntRect, 32> m_pending_paint_event_rects;
|
|
|
|
|
Gfx::IntSize m_size_increment;
|
|
|
|
|
Gfx::IntSize m_base_size;
|
2019-06-30 04:23:16 -03:00
|
|
|
Color m_background_color { Color::WarmGray };
|
2020-02-02 11:07:41 -03:00
|
|
|
WindowType m_window_type { WindowType::Normal };
|
2020-09-10 15:55:39 -03:00
|
|
|
Gfx::StandardCursor m_cursor { Gfx::StandardCursor::None };
|
2020-09-11 09:17:35 -03:00
|
|
|
Gfx::StandardCursor m_effective_cursor { Gfx::StandardCursor::None };
|
2020-07-14 22:17:00 -03:00
|
|
|
bool m_is_active_input { false };
|
2019-02-18 21:42:53 -03:00
|
|
|
bool m_has_alpha_channel { false };
|
2019-03-17 00:23:54 -03:00
|
|
|
bool m_double_buffering_enabled { true };
|
2019-03-18 20:01:02 -03:00
|
|
|
bool m_modal { false };
|
2019-03-18 20:52:39 -03:00
|
|
|
bool m_resizable { true };
|
2020-08-21 17:19:10 -03:00
|
|
|
Optional<Gfx::IntSize> m_resize_aspect_ratio {};
|
2020-01-04 08:12:02 -03:00
|
|
|
bool m_minimizable { true };
|
2019-05-17 16:33:44 -03:00
|
|
|
bool m_fullscreen { false };
|
2020-05-01 18:26:32 -03:00
|
|
|
bool m_frameless { false };
|
2021-07-04 18:10:53 -03:00
|
|
|
bool m_forced_shadow { false };
|
2019-10-26 07:27:01 -03:00
|
|
|
bool m_layout_pending { false };
|
2019-12-29 11:58:07 -03:00
|
|
|
bool m_visible_for_timer_purposes { true };
|
2020-03-12 08:32:28 -03:00
|
|
|
bool m_visible { false };
|
2020-07-14 22:17:00 -03:00
|
|
|
bool m_accessory { false };
|
2020-07-30 22:52:45 -03:00
|
|
|
bool m_moved_by_client { false };
|
2018-10-11 20:03:22 -03:00
|
|
|
};
|
2020-02-01 21:57:57 -03:00
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
}
|
2021-01-16 11:51:56 -03:00
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
struct AK::Formatter<GUI::Window> : Formatter<Core::Object> {
|
|
|
|
|
};
|