1 #include "studiox_includes.h"
2 #include "resource_tree.h"
3
4 #ifdef _DEBUG
5 #define new DEBUG_NEW
6 #endif
7
8 ///////////////////////////////////////////////////////////////////////////////
resource_tree(resource_view * parent)9 resource_tree::resource_tree(resource_view *parent)
10 {
11 mpRoot = NULL;
12 mParentView = parent;
13
14 m_res_info.name = _T("Resources");
15 m_res_info.type = RES_TYPE_HEADER;
16 mpRoot = new resource_item(this, &m_res_info);
17 mpRoot->Open();
18 mpRoot->SetVisible(TRUE);
19 }
20
21 ///////////////////////////////////////////////////////////////////////////////
~resource_tree()22 resource_tree::~resource_tree()
23 {
24 if (mpRoot)
25 {
26 delete mpRoot;
27 }
28 }
29
30
31
32 ///////////////////////////////////////////////////////////////////////////////
Paint(CDC * dc)33 void resource_tree::Paint(CDC *dc)
34 {
35 mpRoot->Paint(dc);
36 }
37
38 ///////////////////////////////////////////////////////////////////////////////
InsertItem(resource_item * parent,res_info * info)39 resource_item *resource_tree::InsertItem(resource_item *parent,
40 res_info *info)
41 {
42 resource_item *item = new resource_item(this, info);
43
44 if (!parent)
45 {
46 parent = mpRoot;
47 }
48
49 parent->Attach(item);
50 return item;
51 }
52
53 ///////////////////////////////////////////////////////////////////////////////
InsertItem(resource_item * parent,CString & string_id,CString & string_val)54 resource_item *resource_tree::InsertItem(resource_item *parent,
55 CString &string_id, CString &string_val)
56 {
57 resource_item *item = new resource_item(this, string_id, string_val);
58 parent->Attach(item);
59 return item;
60 }
61
62 ///////////////////////////////////////////////////////////////////////////////
GetMaxWidth(resource_item * start,CDC * dc)63 INT resource_tree::GetMaxWidth(resource_item* start, CDC* dc)
64 {
65 int width;
66 int max = 0;
67 bool is_folder = FALSE;
68
69 while (start)
70 {
71 width = start->GetWidth(dc);
72
73 if (width > max)
74 {
75 max = width;
76 }
77
78 if (start->IsOpen() && start->First())
79 {
80 width = GetMaxWidth(start->First(), dc);
81
82 if (width > max)
83 {
84 max = width;
85 }
86 }
87
88 start = start->Next();
89 }
90
91 return max;
92 }
93
94 ///////////////////////////////////////////////////////////////////////////////
PositionItems(resource_item * start,int top,int indent,CDC * dc)95 int resource_tree::PositionItems(resource_item *start, int top, int indent, CDC *dc)
96 {
97 CRect size;
98
99 size.top = top;
100 size.left = 0;
101
102 while(start)
103 {
104 size.bottom = size.top + start->GetHeight() - 1;
105
106 if (size.bottom > m_height)
107 {
108 m_height = size.bottom;
109 }
110
111 size.right = size.left + m_width - 1;
112
113 start->SetPos(size);
114 start->SetIndent(indent);
115 top = size.bottom + 1;
116 BOOL IsFolder = FALSE;
117
118 if (start->mpRes)
119 {
120 if (start->mpRes->type == RES_TYPE_FOLDER)
121 {
122 IsFolder = TRUE;
123 }
124 }
125
126 if (IsFolder)
127 {
128 if (start->IsOpen())
129 {
130 top = start->LayoutFolderChildren(dc, top);
131 }
132 }
133 else
134 {
135 if (start->IsOpen() && start->First())
136 {
137 top = PositionItems(start->First(), top, indent + 1, dc);
138 }
139 }
140 size.top = top;
141 start = start->Next();
142 }
143
144 return top;
145 }
146
147 ///////////////////////////////////////////////////////////////////////////////
DeleteAllItems()148 void resource_tree::DeleteAllItems()
149 {
150 resource_item *start = mpRoot;
151 resource_item *next;
152
153 SetCurrentItem(NULL);
154
155 start = start->First();
156
157 while(start)
158 {
159 next = start->Next();
160 start->Detach();
161 delete start;
162 start = next;
163 }
164 }
165
166 ///////////////////////////////////////////////////////////////////////////////
DeleteItem(resource_item * item,BOOL reposition)167 void resource_tree::DeleteItem(resource_item *item, BOOL reposition)
168 {
169 item->Detach();
170 delete item;
171
172 if (reposition)
173 {
174 PositionItems();
175 }
176 }
177
178 ///////////////////////////////////////////////////////////////////////////////
GetViewWidth()179 int resource_tree::GetViewWidth()
180 {
181 CRect size;
182 mParentView->GetClientRect(&size);
183 return size.Width();
184 }
185
186 ///////////////////////////////////////////////////////////////////////////////
PositionItems()187 int resource_tree::PositionItems()
188 {
189 CRect size;
190 CDC *dc = mParentView->GetDC();
191
192 resource_item *start = mpRoot;
193 start = mpRoot;
194 m_height = 0;
195 m_width = GetMaxWidth(start, dc);
196
197 mParentView->GetClientRect(&size);
198 m_parent_right = size.right;
199
200 if (m_width <= m_parent_right)
201 {
202 m_width = m_parent_right;
203 }
204 else
205 {
206 m_width += 10;
207 }
208
209 PositionItems(start, 0, 0, dc);
210 mParentView->ReleaseDC(dc);
211
212 size.right = size.left + m_width;
213 mParentView->OnTreeSizeChange();
214 return 0;
215 }
216
217 ///////////////////////////////////////////////////////////////////////////////
GetHeight(resource_item * start)218 int resource_tree::GetHeight(resource_item *start)
219 {
220 return m_height;
221 }
222
223 ///////////////////////////////////////////////////////////////////////////////
DoesFolderExist(CString & find_name,int resource_id)224 BOOL resource_tree::DoesFolderExist(CString &find_name, int resource_id)
225 {
226 res_info *info;
227
228 resource_item *start = FindFolder(GetRoot(), RES_TYPE_FOLDER, resource_id);
229
230 while (start)
231 {
232 info = start->mpRes;
233
234 if ((find_name == info->name))
235 {
236 return TRUE;
237 }
238
239 start = start->Next();
240 }
241 return FALSE;
242 }
243
244 ///////////////////////////////////////////////////////////////////////////////
CreateUniqueFolderName(CString & seed,int folder_id)245 void resource_tree::CreateUniqueFolderName(CString &seed, int folder_id)
246 {
247 CString out;
248
249 int underscore = seed.ReverseFind('_');
250
251 if (underscore > 0 && underscore > (seed.GetLength() - 3))
252 {
253 if (seed.GetAt(underscore + 1) >= '0' &&
254 seed.GetAt(underscore + 1) <= '9')
255 {
256 seed = seed.Left(underscore);
257 }
258 }
259
260 if (mpRoot)
261 {
262 if (!DoesFolderExist(seed, folder_id))
263 {
264 out = seed;
265 }
266 else
267 {
268 int index = 1;
269 out.Format(_T("%s_%d"), seed, index);
270
271 while (DoesFolderExist(out, folder_id))
272 {
273 index++;
274 out.Format(_T("%s_%d"), seed, index);
275 }
276 }
277 }
278
279 seed = out;
280 }
281
282 ///////////////////////////////////////////////////////////////////////////////
FindItem(resource_item * start,CPoint cp)283 resource_item *resource_tree::FindItem(resource_item *start, CPoint cp)
284 {
285 resource_item *test;
286 CRect rect;
287
288 while(start)
289 {
290 if (start->IsVisible())
291 {
292 //PtInRect: a point on the right or bottom side is considered outside the rectangle.
293 //increate one pixel in bottom and right side so tath a point on right and bottom side
294 //can be considered inside the rectangle
295 rect = start->GetPos();
296 rect.InflateRect(0, 0, 1, 1);
297 if (rect.PtInRect(cp))
298 {
299 return start;
300 }
301
302 // special check for the theme and language switch icon within folder:
303 if ((start->mpRes) && (start->IsOpen()))
304 {
305 if ((start->mpRes->type == RES_TYPE_FOLDER) ||
306 start->mpRes->type == RES_TYPE_GROUP)
307 {
308 if (start->CheckClickCommand(cp))
309 {
310 return start;
311 }
312 }
313 }
314
315 if (start->First() && start->IsOpen())
316 {
317 test = FindItem(start->First(), cp);
318 if (test)
319 {
320 return test;
321 }
322 }
323 }
324 start = start->Next();
325 }
326 return NULL;
327 }
328
329 ///////////////////////////////////////////////////////////////////////////////
FindFolder(resource_item * start,int folder_type,int folder_id)330 resource_item *resource_tree::FindFolder(resource_item *start, int folder_type, int folder_id)
331 {
332 resource_item *test;
333
334 while(start)
335 {
336 if (start->mpRes && start->mpRes->type == folder_type && start->mpRes->folder_id == folder_id)
337 {
338 return start;
339 }
340 if (start ->First())
341 {
342 test = FindFolder(start->First(), folder_type, folder_id);
343 if (test)
344 {
345 return test;
346 }
347 }
348 start = start->Next();
349 }
350 return NULL;
351 }
352
353 ///////////////////////////////////////////////////////////////////////////////
FindItem(resource_item * start,int res_type,CString & res_name)354 resource_item *resource_tree::FindItem(resource_item *start, int res_type, CString &res_name)
355 {
356 resource_item *test;
357
358 while(start)
359 {
360 if (start->mpRes && start->mpRes->type == res_type && start->mpRes->name == res_name)
361 {
362 return start;
363 }
364 if (start ->First())
365 {
366 test = FindItem(start->First(), res_type, res_name);
367 if (test)
368 {
369 return test;
370 }
371 }
372 start = start->Next();
373 }
374 return NULL;
375 }
376
377
378 ///////////////////////////////////////////////////////////////////////////////
FindFolderNamed(resource_item * start,int folder_type,int folder_id,CString & folder_name)379 resource_item *resource_tree::FindFolderNamed(resource_item *start, int folder_type, int folder_id, CString &folder_name)
380 {
381 resource_item *test;
382
383 while (start)
384 {
385 if ((start->mpRes) &&
386 (start->mpRes->type == folder_type) &&
387 (start->mpRes->folder_id == folder_id) &&
388 (start->mpRes->name == folder_name))
389 {
390 return start;
391 }
392 if (start->First())
393 {
394 test = FindFolderNamed(start->First(), folder_type, folder_id, folder_name);
395 if (test)
396 {
397 return test;
398 }
399 }
400 start = start->Next();
401 }
402
403 return NULL;
404 }
405
406 ///////////////////////////////////////////////////////////////////////////////
GetCurrentItem()407 resource_item* resource_tree:: GetCurrentItem()
408 {
409 return mParentView->GetCurrentItem();
410 }
411
412 ///////////////////////////////////////////////////////////////////////////////
SetCurrentItem(resource_item * item)413 void resource_tree::SetCurrentItem(resource_item* item)
414 {
415 mParentView->SetCurrentItem(item);
416 }
417
418