1 #include "FormView.hpp"
2
CreateObject(Point p,const char * type)3 void FormView::CreateObject(Point p, const char* type)
4 {
5 if (!IsLayout())
6 return;
7
8 p = DeZoom(p);
9
10 Rect pageRect = GetPageRect();
11 Rect objRect;
12
13 if (GetBool("Grid.Binding", true))
14 PointToGrid(p);
15
16 if (AsString(type) == "Button")
17 objRect = Rect(Point(p.x - pageRect.left, p.y - pageRect.top),
18 Size(GetNumber("Grid.CX", 10) * 10, GetNumber("Grid.CY", 10) * 3)
19 );
20 else
21 objRect = Rect(Point(p.x - pageRect.left, p.y - pageRect.top),
22 Size(GetNumber("Grid.CX", 10) * 10, GetNumber("Grid.CY", 10) * 2)
23 );
24
25 pageRect = Deoffseted(GetPageRect());
26 if (objRect.right > pageRect.right) objRect.Offset(- objRect.right + pageRect.right, 0);
27 if (objRect.bottom > pageRect.bottom) objRect.Offset(0, - objRect.bottom + pageRect.bottom);
28
29 FormObject obj(objRect);
30 obj.Set("Type", type);
31
32 if (AsString(type) == "EditField")
33 obj.Set("TextAlign", "Left");
34
35 GetObjects()->Add(obj);
36
37 ClearSelection();
38 AddToSelection(GetObjectCount() - 1);
39
40 Refresh();
41 WhenUpdate();
42 WhenChildSelected(GetSelected());
43 WhenChildCount(GetObjectCount());
44 }
45
SetSprings(dword hAlign,dword vAlign)46 void FormView::SetSprings(dword hAlign, dword vAlign)
47 {
48 if (!IsLayout())
49 return;
50
51 Vector<int> sel = GetSelected();
52 for (int i = 0; i < sel.GetCount(); ++i)
53 {
54 FormObject* pI = GetObject(sel[i]);
55 if (!pI) continue;
56
57 if (hAlign != -1) pI->SetHAlign(_HAlign = hAlign);
58 if (vAlign != -1) pI->SetVAlign(_VAlign = vAlign);
59 }
60
61 WhenUpdate();
62 Refresh();
63 }
64
RemoveSelection()65 void FormView::RemoveSelection()
66 {
67 if (!IsLayout())
68 return;
69
70 Vector<int> sel = GetSelected();
71 Sort(sel);
72
73 for (int i = sel.GetCount() - 1; i >= 0; --i)
74 GetObjects()->Remove(sel[i]);
75
76 Refresh();
77 WhenChildCount(GetObjectCount());
78 }
79
MoveUpObject(int id)80 void FormView::MoveUpObject(int id)
81 {
82 if (!IsLayout())
83 return;
84
85 if (id < 0 || id >= GetObjectCount() - 1)
86 return;
87
88 GetObjects()->Swap(id, id + 1);
89 Refresh();
90 WhenChildZ();
91 }
92
MoveDownObject(int id)93 void FormView::MoveDownObject(int id)
94 {
95 if (!IsLayout())
96 return;
97
98 if (id <= 0 || id >= GetObjectCount())
99 return;
100
101 GetObjects()->Swap(id, id - 1);
102 Refresh();
103 WhenChildZ();
104 }
105
MoveToTopObject(int id)106 void FormView::MoveToTopObject(int id)
107 {
108 if (!IsLayout())
109 return;
110
111 if (id < 0 || id >= GetObjectCount() - 1)
112 return;
113
114 for (int i = id; i < GetObjectCount() - 1; ++i)
115 GetObjects()->Swap(i, i + 1);
116 Refresh();
117 WhenChildZ();
118 }
119
MoveToBottomObject(int id)120 void FormView::MoveToBottomObject(int id)
121 {
122 if (!IsLayout())
123 return;
124
125 if (id <= 0 || id >= GetObjectCount())
126 return;
127
128 for (int i = id; i > 0; --i)
129 GetObjects()->Swap(i, i - 1);
130 Refresh();
131 WhenChildZ();
132 }
133
InvertSelection()134 void FormView::InvertSelection()
135 {
136 if (!IsLayout())
137 return;
138
139 for (int i = 0; i < GetObjectCount(); ++i)
140 {
141 FormObject* p = &(*GetObjects())[i];
142 p->GetState() == FormObject::SELECTED
143 ? p->SetState(FormObject::NONE)
144 : p->SetState(FormObject::SELECTED);
145 }
146 Refresh();
147 }
148
AlignTopSelection()149 void FormView::AlignTopSelection()
150 {
151 if (!IsLayout())
152 return;
153
154 int min = Deoffseted(GetSelectionRect()).top;
155 Vector<int> sel = GetSelected();
156
157 for (int i = 0; i < sel.GetCount(); ++i)
158 {
159 FormObject* p = GetObject(sel[i]);
160 if (!p) continue;
161 p->SetRect( Rect(Point(p->GetRect().left, min), p->GetSize()) );
162 }
163
164 WhenChildPos(sel);
165 }
166
AlignLeftSelection()167 void FormView::AlignLeftSelection()
168 {
169 if (!IsLayout())
170 return;
171
172 int min = Deoffseted(GetSelectionRect()).left;
173 Vector<int> sel = GetSelected();
174
175 for (int i = 0; i < sel.GetCount(); ++i)
176 {
177 FormObject* p = GetObject(sel[i]);
178 if (!p) continue;
179 p->SetRect( Rect(Point(min, p->GetRect().top), p->GetSize()) );
180 }
181
182 WhenChildPos(sel);
183 }
184
AlignRightSelection()185 void FormView::AlignRightSelection()
186 {
187 if (!IsLayout())
188 return;
189
190 int min = Deoffseted(GetSelectionRect()).right;
191 Vector<int> sel = GetSelected();
192
193 for (int i = 0; i < sel.GetCount(); ++i)
194 {
195 FormObject* p = GetObject(sel[i]);
196 if (!p) continue;
197 p->SetRect( Rect(Point(min - p->GetRect().Width(), p->GetRect().top), p->GetSize()) );
198 }
199
200 WhenChildPos(sel);
201 }
202
AlignBottomSelection()203 void FormView::AlignBottomSelection()
204 {
205 if (!IsLayout())
206 return;
207
208 int min = Deoffseted(GetSelectionRect()).bottom;
209 Vector<int> sel = GetSelected();
210
211 for (int i = 0; i < sel.GetCount(); ++i)
212 {
213 FormObject* p = GetObject(sel[i]);
214 if (!p) continue;
215 p->SetRect( Rect(Point(p->GetRect().left, min - p->GetRect().Height()), p->GetSize()) );
216 }
217
218 WhenChildPos(sel);
219 }
220
ToggleOutlineDraw(int obj)221 void FormView::ToggleOutlineDraw(int obj)
222 {
223 if (!IsLayout())
224 return;
225
226 FormObject* pI = GetObject(obj);
227 if (!pI) return;
228
229 pI->SetBool("OutlineDraw", !pI->GetBool("OutlineDraw", false));
230 WhenChildZ();
231 }
232
IsOutlineDraw(int obj)233 bool FormView::IsOutlineDraw(int obj)
234 {
235 if (!IsLayout())
236 return false;
237
238 FormObject* pI = GetObject(obj);
239 if (!pI) return false;
240
241 return pI->GetBool("OutlineDraw", false);
242 }
243