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 "VBWidget.h"
30#include <AK/NonnullRefPtrVector.h>
31#include <LibGUI/Widget.h>
32
33class VBForm : public GUI::Widget {
34 C_OBJECT(VBForm)
35 friend class VBWidget;
36
37public:
38 virtual ~VBForm() override;
39
40 static VBForm* current();
41
42 String name() const { return m_name; }
43 void set_name(const String& name) { m_name = name; }
44
45 bool is_selected(const VBWidget&) const;
46 VBWidget* widget_at(const Gfx::Point&);
47
48 void set_should_snap_to_grip(bool snap) { m_should_snap_to_grid = snap; }
49 bool should_snap_to_grid() const { return m_should_snap_to_grid; }
50
51 void insert_widget(VBWidgetType);
52
53 Function<void(VBWidget*)> on_widget_selected;
54
55 void load_from_file(const String& path);
56 void write_to_file(const String& path);
57 void dump();
58
59protected:
60 virtual void paint_event(GUI::PaintEvent&) override;
61 virtual void second_paint_event(GUI::PaintEvent&) override;
62 virtual void mousedown_event(GUI::MouseEvent&) override;
63 virtual void mousemove_event(GUI::MouseEvent&) override;
64 virtual void mouseup_event(GUI::MouseEvent&) override;
65 virtual void context_menu_event(GUI::ContextMenuEvent&) override;
66 virtual void keydown_event(GUI::KeyEvent&) override;
67
68private:
69 explicit VBForm(const String& name);
70
71 void grabber_mousedown_event(GUI::MouseEvent&, Direction grabber);
72 void set_single_selected_widget(VBWidget*);
73 void add_to_selection(VBWidget&);
74 void remove_from_selection(VBWidget&);
75 void delete_selected_widgets();
76 template<typename Callback>
77 void for_each_selected_widget(Callback);
78 void set_cursor_type_from_grabber(Direction grabber);
79
80 VBWidget* single_selected_widget();
81
82 String m_name;
83 int m_grid_size { 5 };
84 bool m_should_snap_to_grid { true };
85 NonnullRefPtrVector<VBWidget> m_widgets;
86 HashMap<GUI::Widget*, VBWidget*> m_gwidget_map;
87 HashTable<VBWidget*> m_selected_widgets;
88 Gfx::Point m_transform_event_origin;
89 Gfx::Point m_next_insertion_position;
90 Direction m_resize_direction { Direction::None };
91 Direction m_mouse_direction_type { Direction::None };
92 RefPtr<GUI::Menu> m_context_menu;
93};