Skip to content

Commit 75c76f6

Browse files
committed
VisualBuilder: Make it possible to insert widgets from the toolbox.
1 parent c71ece7 commit 75c76f6

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

Applications/VisualBuilder/VBForm.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@
22
#include "VBWidget.h"
33
#include <LibGUI/GPainter.h>
44

5+
static VBForm* s_current;
6+
VBForm* VBForm::current()
7+
{
8+
return s_current;
9+
}
10+
511
VBForm::VBForm(const String& name, GWidget* parent)
612
: GWidget(parent)
713
, m_name(name)
814
{
15+
s_current = this;
916
set_fill_with_background_color(true);
1017
set_background_color(Color::LightGray);
1118
set_greedy_for_hits(true);
@@ -23,6 +30,14 @@ VBForm::VBForm(const String& name, GWidget* parent)
2330
m_widgets.append(move(button1));
2431
}
2532

33+
void VBForm::insert_widget(WidgetType type)
34+
{
35+
auto widget = VBWidget::create(type, *this);
36+
widget->set_rect({ m_next_insertion_position, { m_grid_size * 10 + 1, m_grid_size * 5 + 1 } });
37+
m_next_insertion_position.move_by(m_grid_size, m_grid_size);
38+
m_widgets.append(move(widget));
39+
}
40+
2641
VBForm::~VBForm()
2742
{
2843
}

Applications/VisualBuilder/VBForm.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class VBForm : public GWidget {
99
explicit VBForm(const String& name, GWidget* parent = nullptr);
1010
virtual ~VBForm() override;
1111

12+
static VBForm* current();
13+
1214
String name() const { return m_name; }
1315
void set_name(const String& name) { m_name = name; }
1416

@@ -18,6 +20,8 @@ class VBForm : public GWidget {
1820
void set_should_snap_to_grip(bool snap) { m_should_snap_to_grid = snap; }
1921
bool should_snap_to_grid() const { return m_should_snap_to_grid; }
2022

23+
void insert_widget(WidgetType);
24+
2125
protected:
2226
virtual void paint_event(GPaintEvent&) override;
2327
virtual void second_paint_event(GPaintEvent&) override;
@@ -35,5 +39,6 @@ class VBForm : public GWidget {
3539
WeakPtr<VBWidget> m_selected_widget;
3640
Point m_transform_event_origin;
3741
Rect m_transform_widget_origin_rect;
42+
Point m_next_insertion_position;
3843
Direction m_resize_direction { Direction::None };
3944
};

Applications/VisualBuilder/main.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@
22
#include <LibGUI/GWidget.h>
33
#include <LibGUI/GBoxLayout.h>
44
#include <LibGUI/GApplication.h>
5-
#include <LibGUI/GStatusBar.h>
6-
#include <LibGUI/GToolBar.h>
75
#include <LibGUI/GMenuBar.h>
8-
#include <LibGUI/GTextEditor.h>
96
#include <LibGUI/GAction.h>
10-
#include <LibGUI/GFontDatabase.h>
11-
#include <LibCore/CFile.h>
12-
#include <AK/StringBuilder.h>
7+
#include <LibGUI/GButton.h>
138
#include "VBForm.h"
149
#include "VBWidget.h"
1510
#include <unistd.h>
@@ -68,6 +63,26 @@ GWindow* make_toolbox_window()
6863

6964
auto* widget = new GWidget;
7065
widget->set_fill_with_background_color(true);
66+
widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
7167
window->set_main_widget(widget);
68+
69+
auto* button_button = new GButton(widget);
70+
button_button->set_caption("Button");
71+
button_button->on_click = [] (GButton&) {
72+
if (auto* form = VBForm::current())
73+
form->insert_widget(WidgetType::GButton);
74+
};
75+
auto* spinbox_button = new GButton(widget);
76+
spinbox_button->set_caption("SpinBox");
77+
spinbox_button->on_click = [] (GButton&) {
78+
if (auto* form = VBForm::current())
79+
form->insert_widget(WidgetType::GSpinBox);
80+
};
81+
auto* editor_button = new GButton(widget);
82+
editor_button->set_caption("TextEditor");
83+
editor_button->on_click = [] (GButton&) {
84+
if (auto* form = VBForm::current())
85+
form->insert_widget(WidgetType::GTextEditor);
86+
};
7287
return window;
7388
}

0 commit comments

Comments
 (0)