Serenity Operating System
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
27#pragma once
28
29#include "VBWidgetType.h"
30#include <AK/Function.h>
31#include <AK/HashMap.h>
32#include <AK/NonnullOwnPtrVector.h>
33#include <AK/NonnullRefPtr.h>
34#include <AK/RefCounted.h>
35#include <AK/Weakable.h>
36#include <LibGfx/Rect.h>
37#include <LibGUI/Widget.h>
38
39class VBForm;
40class VBProperty;
41class VBWidgetPropertyModel;
42
43enum class Direction {
44 None,
45 Left,
46 UpLeft,
47 Up,
48 UpRight,
49 Right,
50 DownRight,
51 Down,
52 DownLeft
53};
54template<typename Callback>
55inline void for_each_direction(Callback callback)
56{
57 callback(Direction::Left);
58 callback(Direction::UpLeft);
59 callback(Direction::Up);
60 callback(Direction::UpRight);
61 callback(Direction::Right);
62 callback(Direction::DownRight);
63 callback(Direction::Down);
64 callback(Direction::DownLeft);
65}
66
67class VBWidget : public RefCounted<VBWidget>
68 , public Weakable<VBWidget> {
69 friend class VBWidgetPropertyModel;
70
71public:
72 static NonnullRefPtr<VBWidget> create(VBWidgetType type, VBForm& form, VBWidget* parent) { return adopt(*new VBWidget(type, form, parent)); }
73 ~VBWidget();
74
75 bool is_selected() const;
76
77 Gfx::Rect rect() const;
78 void set_rect(const Gfx::Rect&);
79
80 Gfx::Rect grabber_rect(Direction) const;
81 Direction grabber_at(const Gfx::Point&) const;
82
83 GUI::Widget* gwidget() { return m_gwidget; }
84
85 VBProperty& property(const String&);
86
87 void for_each_property(Function<void(VBProperty&)>);
88
89 VBWidgetPropertyModel& property_model() { return *m_property_model; }
90
91 void setup_properties();
92 void synchronize_properties();
93
94 void property_did_change();
95
96 Gfx::Rect transform_origin_rect() const { return m_transform_origin_rect; }
97 void capture_transform_origin_rect();
98
99 bool is_in_layout() const;
100
101private:
102 VBWidget(VBWidgetType, VBForm&, VBWidget* parent);
103
104 void add_property(const String& name, Function<GUI::Variant(const GUI::Widget&)>&& getter, Function<void(GUI::Widget&, const GUI::Variant&)>&& setter);
105
106 VBWidgetType m_type { VBWidgetType::None };
107 VBForm& m_form;
108 RefPtr<GUI::Widget> m_gwidget;
109 NonnullOwnPtrVector<VBProperty> m_properties;
110 NonnullRefPtr<VBWidgetPropertyModel> m_property_model;
111 Gfx::Rect m_transform_origin_rect;
112};