1 #include "FormView.hpp"
2
FormView()3 FormView::FormView()
4 {
5 TransparentBackPaint();
6
7 for (int i = 0; i < 4; ++i)
8 for (int j = 0; j < 4; ++j)
9 for (int k = 0; k < 4; ++k)
10 _colors << Color(i * 64 - 1, j * 64 - 1, k * 64 - 1);
11
12 _objectResize = false;
13 _frameResize = false;
14
15 _showInfo = 0;
16 _leftCur = 0;
17 _topCur = 0;
18 _rightCur = 0;
19 _bottomCur = 0;
20
21 SetHAlign(-1);
22 SetVAlign(-1);
23
24 _Layout = -1;
25 _container = NULL;
26
27 SetFont(StdFont());
28
29 _toolTop[0] = FormViewImg::ToolTop();
30 _toolTop[1] = FormViewImg::ToolTopH();
31 _toolTop[2] = FormViewImg::ToolTopP();
32 _toolLeft[0] = FormViewImg::ToolLeft();
33 _toolLeft[1] = FormViewImg::ToolLeftH();
34 _toolLeft[2] = FormViewImg::ToolLeftP();
35 _toolRight[0] = FormViewImg::ToolRight();
36 _toolRight[1] = FormViewImg::ToolRightH();
37 _toolRight[2] = FormViewImg::ToolRightP();
38 _toolBottom[0] = FormViewImg::ToolBottom();
39 _toolBottom[1] = FormViewImg::ToolBottomH();
40 _toolBottom[2] = FormViewImg::ToolBottomP();
41
42 _cursor = OverrideCursor(Image());
43 OverrideCursor(_cursor);
44
45 SetBool("View.Coloring", false);
46 SetBool("Grid.Visible", true);
47 SetBool("Grid.Binding", true);
48 SetNumber("Grid.CX", 10);
49 SetNumber("Grid.CY", 10);
50 }
51
New()52 void FormView::New()
53 {
54 _Layouts.Clear();
55 _Layouts.Add().Set("Form.Name", t_("Default"));
56 SelectLayout(0);
57
58 WhenUpdateLayouts();
59 }
60
Xmlize(XmlIO xml)61 void FormView::Xmlize(XmlIO xml)
62 {
63 for (int i = 0; i < GetLayoutCount(); ++i)
64 GetLayout(i)->Compression(IsCompressed());
65 xml("layouts", GetLayouts());
66 }
67
SaveAll(const String & file,bool compression)68 bool FormView::SaveAll(const String& file, bool compression)
69 {
70 if (compression)
71 {
72 String s = StoreAsXML(*this, "form");
73 return SaveFile(file, ZCompress(s));
74 }
75 return StoreAsXMLFile(*this, "form", file);
76 }
77
LoadAll(const String & file,bool compression)78 bool FormView::LoadAll(const String& file, bool compression)
79 {
80 if (compression)
81 {
82 String s;
83 s = ZDecompress(LoadFile(file));
84 return LoadFromXML(*this, s);
85 }
86 return LoadFromXMLFile(*this, file);
87 }
88
Layout()89 void FormView::Layout()
90 {
91 FormLayout *l = GetCurrentLayout();
92 if (!l) return;
93
94 SetPageRect(
95 Rect(Point(GetPageRect().left, GetPageRect().top),
96 Size(l->GetNumber("Form.Width", 400),
97 l->GetNumber("Form.Height", 300)))
98 );
99 }
100
GetObject(int id)101 FormObject* FormView::GetObject(int id)
102 {
103 if (!IsLayout())
104 return NULL;
105
106 if (id < 0 || id >= GetObjectCount())
107 return NULL;
108
109 return &((*GetObjects())[id]);
110 }
111
ObjectFromPt(Point p)112 int FormView::ObjectFromPt(Point p)
113 {
114 if (!IsLayout()) return -1;
115
116 for (int i = GetObjectCount() - 1; i >= 0; --i)
117 if (Zoom(Offseted( (*GetObjects())[i].GetRect() )).Contains(p))
118 return i;
119 return -1;
120 }
121
IsSelected(int id)122 bool FormView::IsSelected(int id)
123 {
124 if (!IsLayout())
125 return false;
126
127 if (id < 0 || id >= GetObjectCount())
128 return false;
129
130 return (*GetObjects())[id].GetState() == FormObject::SELECTED;
131 }
132
GetSelected()133 Vector<int> FormView::GetSelected()
134 {
135 Vector<int> result;
136
137 if (!IsLayout())
138 return result;
139
140 for (int i = GetObjects()->GetCount() - 1; i >= 0; --i)
141 if ((*GetObjects())[i].GetState() == FormObject::SELECTED) result << i;
142 return result;
143 }
144
GetSelectedCount()145 int FormView::GetSelectedCount()
146 {
147 if (!IsLayout())
148 return 0;
149
150 int count = 0;
151 for (int i = GetObjects()->GetCount() - 1; i >= 0; --i)
152 if ((*GetObjects())[i].GetState() == FormObject::SELECTED) count++;
153 return count;
154 }
155
GetObjectsRect()156 Rect FormView::GetObjectsRect()
157 {
158 if (!IsLayout() || !GetObjectCount())
159 return Offseted(Rect(Point(0, 0), GetGridSize()));
160
161 Rect r;
162
163 for (int i = 0; i < GetObjectCount(); i++)
164 {
165 FormObject *pI = GetObject(i);
166 if (!pI) continue;
167
168 Rect s = pI->GetRect();
169 if (i == 0) r = s;
170
171 if (r.top > s.top) r.top = s.top;
172 if (r.left > s.left) r.left = s.left;
173 if (r.right < s.right) r.right = s.right;
174 if (r.bottom < s.bottom) r.bottom = s.bottom;
175 }
176
177 return Offseted(r);
178 }
179
GetSelectionRect()180 Rect FormView::GetSelectionRect()
181 {
182 Rect r;
183 if (!IsLayout()) return Offseted(r);
184 Vector<int> sel = GetSelected();
185
186 for (int i = 0; i < sel.GetCount(); i++)
187 {
188 FormObject *pI = GetObject( sel[i] );
189 if (!pI) continue;
190
191 Rect s = pI->GetRect();
192 if (i == 0) r = s;
193
194 if (r.top > s.top) r.top = s.top;
195 if (r.left > s.left) r.left = s.left;
196 if (r.right < s.right) r.right = s.right;
197 if (r.bottom < s.bottom) r.bottom = s.bottom;
198 }
199
200 return Offseted(r);
201 }
202
AddToSelection(int id)203 void FormView::AddToSelection(int id)
204 {
205 if (!IsLayout())
206 return;
207
208 FormObject *pI = GetObject(id);
209 if (!pI) return;
210
211 pI->SetState(FormObject::SELECTED);
212 Refresh();
213 }
214
ToggleSelection(int id)215 void FormView::ToggleSelection(int id)
216 {
217 if (!IsLayout())
218 return;
219
220 FormObject *pI = GetObject(id);
221 if (!pI) return;
222
223 pI->GetState() == FormObject::SELECTED
224 ? pI->SetState(FormObject::NONE)
225 : pI->SetState(FormObject::SELECTED);
226
227 Refresh();
228 }
229
ClearSelection()230 void FormView::ClearSelection()
231 {
232 if (!IsLayout())
233 return;
234
235 for (int i = GetObjects()->GetCount() - 1; i >= 0; --i)
236 (*GetObjects())[i].SetState(FormObject::NONE);
237 WhenUpdate();
238 Refresh();
239 }
240
SelectAllInRect(Rect r,bool add,bool toggle)241 void FormView::SelectAllInRect(Rect r, bool add, bool toggle)
242 {
243 if (!IsLayout())
244 return;
245
246 if (!add) ClearSelection();
247
248 for (int i = GetObjectCount() - 1; i >= 0; --i)
249 if (r.Contains(Zoom(Offseted(GetObject(i)->GetRect()))))
250 toggle ? ToggleSelection(i) : AddToSelection(i);
251
252 Refresh();
253 }
254
HasLayout(const String & layout)255 int FormView::HasLayout(const String& layout)
256 {
257 for (int i = 0; i < _Layouts.GetCount(); ++i)
258 if (_Layouts[i].Get("Form.Name") == layout)
259 return i;
260 return -1;
261 }
262