summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGUI/AbstractZoomPanWidget.h
blob: 612438981a546c7052e9574e8332ec440703a4fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * Copyright (c) 2022, Mustafa Quraish <mustafa@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <LibGUI/Frame.h>
#include <LibGfx/Point.h>
#include <LibGfx/Rect.h>

namespace GUI {

class AbstractZoomPanWidget : public GUI::Frame {
    C_OBJECT(AbstractZoomPanWidget);

public:
    void set_scale(float scale);
    float scale() const { return m_scale; }
    void set_scale_bounds(float min_scale, float max_scale);

    void scale_by(float amount);
    void scale_centered(float new_scale, Gfx::IntPoint const& center);

    bool is_panning() const { return m_is_panning; }
    void start_panning(Gfx::IntPoint const& position);
    void stop_panning();

    void pan_to(Gfx::IntPoint const& position);

    // Should be overridden by derived classes if they want updates.
    virtual void handle_relayout(Gfx::IntRect const&) { update(); }
    void relayout();

    Gfx::FloatPoint frame_to_content_position(Gfx::IntPoint const& frame_position) const;
    Gfx::FloatRect frame_to_content_rect(Gfx::IntRect const& frame_rect) const;
    Gfx::FloatPoint content_to_frame_position(Gfx::IntPoint const& content_position) const;
    Gfx::FloatRect content_to_frame_rect(Gfx::IntRect const& content_rect) const;

    virtual void mousewheel_event(GUI::MouseEvent& event) override;
    virtual void mousedown_event(GUI::MouseEvent& event) override;
    virtual void resize_event(GUI::ResizeEvent& event) override;
    virtual void mousemove_event(GUI::MouseEvent& event) override;
    virtual void mouseup_event(GUI::MouseEvent& event) override;

    void set_original_rect(Gfx::IntRect const& rect) { m_original_rect = rect; }
    void set_content_rect(Gfx::IntRect const& content_rect);
    void set_origin(Gfx::FloatPoint const& origin) { m_origin = origin; }

    void reset_view();

    Gfx::IntRect content_rect() const { return m_content_rect; }

    Function<void(float)> on_scale_change;

    enum class FitType {
        Width,
        Height,
        Both
    };
    void fit_content_to_rect(Gfx::IntRect const& rect, FitType = FitType::Both);
    void fit_content_to_view(FitType fit_type = FitType::Both)
    {
        fit_content_to_rect(rect(), fit_type);
    }

private:
    Gfx::IntRect m_original_rect;
    Gfx::IntRect m_content_rect;

    Gfx::IntPoint m_pan_mouse_pos;
    Gfx::FloatPoint m_origin;
    Gfx::FloatPoint m_pan_start;
    bool m_is_panning { false };

    float m_min_scale { 0.1f };
    float m_max_scale { 10.0f };
    float m_scale { 1.0f };

    AK::Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap>> m_saved_cursor { Gfx::StandardCursor::None };
};

}