-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrowser.cpp
850 lines (759 loc) · 24.3 KB
/
Browser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
/* Copyright (c) 2002-2012 Croteam Ltd.
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as published by
the Free Software Foundation
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
// Browser.cpp : implementation file
//
#include "StdH.h"
#include "Browser.h"
#ifdef _DEBUG
#undef new
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
////////////////////////////////////////////////////////////////////
// CBrowser Construction/Destruction
CBrowser::CBrowser(void)
{
// set default number of subdirectories (none)
for( INDEX i=0; i<DIRECTORY_SHORTCT_CT; i++)
{
m_aiSubDirectoriesCt[ i] = 0;
}
}
BOOL CBrowser::Create( CWnd* pParentWnd, UINT nIDTemplate,
UINT nStyle, UINT nID, BOOL bChange)
{
if(!CDialogBar::Create(pParentWnd,nIDTemplate,nStyle,nID))
{
return FALSE;
}
m_bVirtualTreeChanged = FALSE;
m_TreeHeight = CLOSED_TREE;
m_Size = m_sizeDefault;
m_BrowseWindow.AttachToControl( this);
m_BrowseWindow.SetBrowserPtr( this);
m_TreeCtrl.SetBrowserPtr( this);
m_TreeCtrl.SubclassDlgItem(IDC_VIRTUALTREE, this);
m_TreeCtrl.DragAcceptFiles();
m_IconsImageList.Create( IDB_DIRECTORY_ICONS, 16, 1, CLR_NONE);
m_TreeCtrl.SetImageList( &m_IconsImageList, TVSIL_NORMAL);
return TRUE;
}
////////////////////////////////////////////////////////////////////
// Overloaded functions
CSize CBrowser::CalcDynamicLayout(int nLength, DWORD nMode)
{
CSize csResult;
// Return default if it is being docked or floated
if ((nMode & LM_VERTDOCK) || (nMode & LM_HORZDOCK))
{
if (nMode & LM_STRETCH) // if not docked stretch to fit
{
csResult = CSize((nMode & LM_HORZ) ? 32767 : m_Size.cx,
(nMode & LM_HORZ) ? m_Size.cy : 32767);
}
else
{
csResult = m_Size;
}
}
else if (nMode & LM_MRUWIDTH)
{
csResult = m_Size;
}
// In all other cases, accept the dynamic length
else
{
if (nMode & LM_LENGTHY)
{
csResult = CSize( m_Size.cx, m_Size.cy = nLength);
}
else
{
csResult = CSize( m_Size.cx = nLength, m_Size.cy);
}
}
CRect NewBrowserPos;
CRect NewTreePos;
ULONG ulNewTreeHeight = m_TreeHeight;
if( csResult.cy < (m_TreeHeight + V_BORDER*2 + 10) )
{
ulNewTreeHeight = csResult.cy - +V_BORDER*2 - 10;
}
// First we calculate and set browsing window position
NewBrowserPos = CRect( H_BORDER,
V_BORDER + ulNewTreeHeight,
csResult.cx - H_BORDER,
csResult.cy - V_BORDER);
m_BrowseWindow.MoveWindow( NewBrowserPos);
// Finaly we set new virtual tree control size
NewTreePos = CRect( H_BORDER,
V_BORDER,
csResult.cx - H_BORDER,
V_BORDER + ulNewTreeHeight);
CWnd *pwndTree = GetDlgItem( IDC_VIRTUALTREE);
pwndTree->MoveWindow( NewTreePos);
m_boxBrowseWnd = PIXaabbox2D( PIX2D(NewBrowserPos.TopLeft().x, NewBrowserPos.TopLeft().y),
PIX2D(NewBrowserPos.BottomRight().x, NewBrowserPos.BottomRight().y) );
m_BrowseWindow.m_BrowseWndWidth = NewBrowserPos.Width();
m_BrowseWindow.m_BrowseWndHeight = NewBrowserPos.Height();
m_boxTreeWnd = PIXaabbox2D( PIX2D(NewTreePos.TopLeft().x, NewTreePos.TopLeft().y),
PIX2D(NewTreePos.BottomRight().x, NewTreePos.BottomRight().y) );
return csResult;
}
BEGIN_MESSAGE_MAP(CBrowser, CDialogBar)
//{{AFX_MSG_MAP(CBrowser)
ON_COMMAND(ID_CREATE_DIRECTORY, OnCreateDirectory)
ON_COMMAND(ID_DELETE_DIRECTORY, OnDeleteDirectory)
ON_COMMAND(ID_SAVE_VIRTUAL_TREE, OnSaveVirtualTree)
ON_COMMAND(ID_LOAD_VIRTUAL_TREE, OnLoadVirtualTree)
ON_COMMAND(ID_RENAME_DIRECTORY, OnRenameDirectory)
ON_WM_CONTEXTMENU()
ON_COMMAND(ID_SAVE_AS_VIRTUAL_TREE, OnSaveAsVirtualTree)
ON_COMMAND(ID_IMPORT_VIRTUAL_TREE, OnImportVirtualTree)
ON_COMMAND(ID_EXPORT_VIRTUAL_TREE, OnExportVirtualTree)
ON_UPDATE_COMMAND_UI(ID_IMPORT_VIRTUAL_TREE, OnUpdateImportVirtualTree)
ON_UPDATE_COMMAND_UI(ID_EXPORT_VIRTUAL_TREE, OnUpdateExportVirtualTree)
ON_COMMAND(ID_DUMP_VT, OnDumpVt)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////
// CBrowser message handlers
/////////////////////////////////////////////////////////////////////
void CBrowser::AddDirectoryRecursiv(CVirtualTreeNode *pOneDirectory, HTREEITEM hParent)
{
// Insert one entry into virtual directory tree
HTREEITEM InsertedDir;
InsertedDir = m_TreeCtrl.InsertItem( 0, _T(""), 0, 0, TVIS_SELECTED, TVIF_STATE, 0,
hParent, TVI_SORT );
pOneDirectory->vtn_Handle = (ULONG) InsertedDir;
m_TreeCtrl.SetItemData( InsertedDir, (ULONG)(pOneDirectory));
m_TreeCtrl.SetItemText( InsertedDir, CString(pOneDirectory->vtn_strName));
m_TreeCtrl.SetItemImage( InsertedDir, pOneDirectory->vtn_itIconType,
pOneDirectory->vtn_itIconType + NO_OF_ICONS);
// Now add this directory's subdirectories recursively
FOREACHINLIST( CVirtualTreeNode, vtn_lnInDirectory, pOneDirectory->vtn_lhChildren, it)
{
CVirtualTreeNode &vtn=*it;
if( vtn.vtn_bIsDirectory)
{
AddDirectoryRecursiv( &vtn, InsertedDir);
}
}
}
void CBrowser::OnCreateDirectory()
{
CDlgCreateVirtualDirectory dlg;
if( dlg.DoModal() == IDOK)
{
CloseSelectedDirectory();
CVirtualTreeNode *pvtnCurrent;
// Is this root directory ?
if( m_TreeCtrl.GetCount() == 0)
{
// Set data in instanciated CVirtualTreeNode because it is Root
m_VirtualTree.vtn_bIsDirectory = TRUE;
m_VirtualTree.vtn_itIconType = dlg.m_iSelectedIconType;
m_VirtualTree.vtn_strName = MfcStringToCT(dlg.m_strDirectoryName);
// Root's parent is NULL
m_VirtualTree.vnt_pvtnParent = NULL;
pvtnCurrent = &m_VirtualTree;
}
else
{
// Allocate new CVirtualTreeNode to carry new directory data
CVirtualTreeNode *pvtnNewDir = new CVirtualTreeNode();
pvtnCurrent = pvtnNewDir;
pvtnNewDir->vtn_bIsDirectory = TRUE;
pvtnNewDir->vtn_itIconType = dlg.m_iSelectedIconType;
pvtnNewDir->vtn_strName = MfcStringToCT(dlg.m_strDirectoryName);
// Get ptr to parent CVirtualTreeNode
HTREEITEM pSelectedItem = m_TreeCtrl.GetSelectedItem();
if( pSelectedItem == NULL) return;
pvtnNewDir->vnt_pvtnParent = (CVirtualTreeNode *)m_TreeCtrl.GetItemData( pSelectedItem);
// And add this new directory into its list
pvtnNewDir->vnt_pvtnParent->vtn_lhChildren.AddTail( pvtnNewDir->vtn_lnInDirectory);
}
m_bVirtualTreeChanged = TRUE;
m_TreeCtrl.DeleteAllItems();
AddDirectoryRecursiv( &m_VirtualTree, TVI_ROOT); // Fill CTreeCtrl using recursion
m_TreeCtrl.SortChildren( NULL);
// Now select it
m_TreeCtrl.SelectItem( (HTREEITEM) pvtnCurrent->vtn_Handle);
OpenSelectedDirectory();
}
m_TreeCtrl.SetFocus();
}
void CBrowser::OnUpdateVirtualTreeControl(void)
{
m_bVirtualTreeChanged = TRUE;
m_TreeCtrl.DeleteAllItems();
AddDirectoryRecursiv( &m_VirtualTree, TVI_ROOT);
m_TreeCtrl.SortChildren( NULL);
// Now select it
m_TreeCtrl.SelectItem( (HTREEITEM) m_VirtualTree.vtn_Handle);
OpenSelectedDirectory();
m_TreeCtrl.SetFocus();
}
void CBrowser::OnDeleteDirectory()
{
if( m_TreeCtrl.GetCount() == 0)
return;
if( ::MessageBoxA( this->m_hWnd, "Do You really want to delete directory and all its subdirectories?",
"Warning !", MB_YESNO | MB_ICONWARNING | MB_DEFBUTTON1 |
MB_TASKMODAL | MB_TOPMOST) != IDYES)
{
m_TreeCtrl.SetFocus();
return;
}
CloseSelectedDirectory();
DeleteDirectory();
OpenSelectedDirectory();
}
void CBrowser::DeleteDirectory(void)
{
if( m_TreeCtrl.GetCount() == 0)
return;
CVirtualTreeNode *pvtnParent = NULL;
HTREEITEM pSelectedItem = m_TreeCtrl.GetSelectedItem();
if( pSelectedItem == NULL) return;
CVirtualTreeNode *pVTN = (CVirtualTreeNode *)m_TreeCtrl.GetItemData( pSelectedItem);
// Delete all subdirectories
FORDELETELIST( CVirtualTreeNode, vtn_lnInDirectory, pVTN->vtn_lhChildren, litDel)
{
delete &litDel.Current();
}
// If this is not root directory, remove it from list and delete it
if( pVTN->vnt_pvtnParent != NULL)
{
pvtnParent = pVTN->vnt_pvtnParent;
pVTN->vtn_lnInDirectory.Remove();
delete pVTN;
}
m_TreeCtrl.DeleteAllItems();
// If it wasn't root directory, fill TreeCtrl with data
if( pvtnParent != NULL)
{
AddDirectoryRecursiv( &m_VirtualTree, TVI_ROOT); // Fill CTreeCtrl using recursion
m_TreeCtrl.SortChildren( NULL);
HTREEITEM NewActiveDir = (HTREEITEM) pvtnParent->vtn_Handle;
if( m_TreeCtrl.ItemHasChildren(NewActiveDir) )
{
NewActiveDir = m_TreeCtrl.GetChildItem( NewActiveDir);
}
m_TreeCtrl.SelectItem( NewActiveDir);
}
m_bVirtualTreeChanged = TRUE;
m_TreeCtrl.SetFocus();
}
CVirtualTreeNode *CBrowser::FindItemDirectory(CVirtualTreeNode *pvtnCurrentDirectory,
CTFileName fnItemFileName)
{
// Now search this directory recursivly
FOREACHINLIST( CVirtualTreeNode, vtn_lnInDirectory, pvtnCurrentDirectory->vtn_lhChildren, it)
{
if( it->vtn_bIsDirectory)
{
CVirtualTreeNode *pvtnResult = FindItemDirectory( &it.Current(), fnItemFileName);
if( pvtnResult != NULL) return pvtnResult;
}
else
{
if( it->vtn_fnItem == fnItemFileName)
{
// deselect all items in directory
FOREACHINLIST( CVirtualTreeNode, vtn_lnInDirectory, pvtnCurrentDirectory->vtn_lhChildren, it2)
{
it2->vtn_bSelected = FALSE;
}
// selected requested item
it->vtn_bSelected = TRUE;
return pvtnCurrentDirectory;
}
}
}
return NULL;
}
void CBrowser::SelectItemDirectory( CTFileName fnItemFileName)
{
CVirtualTreeNode *pvtnItemDirectory = FindItemDirectory( &m_VirtualTree, fnItemFileName);
if( pvtnItemDirectory != NULL)
{
CloseSelectedDirectory();
// Now select it
m_TreeCtrl.SelectItem( (HTREEITEM) pvtnItemDirectory->vtn_Handle);
OpenSelectedDirectory();
}
}
/*
* Separate subdirectory names along current path
*/
INDEX CBrowser::GetSelectedDirectory( CTString strArray[])
{
INDEX iSubDirsCt = 0;
HTREEITEM pItem = m_TreeCtrl.GetSelectedItem();
if( pItem == NULL) return -1;
FOREVER
{
CTString strItemName = MfcStringToCT(m_TreeCtrl.GetItemText(pItem));
strArray[ iSubDirsCt] = strItemName;
iSubDirsCt ++;
if( m_TreeCtrl.GetParentItem(pItem) == NULL)
break;
pItem = m_TreeCtrl.GetParentItem(pItem);
}
return iSubDirsCt;
}
HTREEITEM CBrowser::GetVirtualDirectoryItem( CTString strArray[], INDEX iSubDirsCt)
{
INDEX j;
// pick up root item ptr if path finding fails, it will be selected
HTREEITEM pRootItem = m_TreeCtrl.GetRootItem();
HTREEITEM pItem = pRootItem;
// loop all directories starting from root and search for selected subdirectory name
for( j=iSubDirsCt-1; j>=0; j--)
{
BOOL bSucess;
HTREEITEM pStartItem = pItem;
FOREVER
{
if( pItem == NULL)
{
bSucess = FALSE;
break;
}
// get current directory's (starting from root) first subdirectory
CTString strItemName = MfcStringToCT(m_TreeCtrl.GetItemText(pItem));
// is this subdirectory's name same as one in list of selected along given path
if( strArray[ j] == strItemName)
{
// subdirectory found, if it has children or selected subdirectories counter reached 0
if( m_TreeCtrl.ItemHasChildren( pItem) || (j==0) )
{
// mark that we found current subdirectory
bSucess = TRUE;
// if counter didn't reached 0
if( j!=0)
{
// it becomes current and we will try to find his selected subdirectory
pItem = m_TreeCtrl.GetChildItem( pItem);
}
break;
}
// we have no more children and counter is not 0 which means
else
{
// that we didn't succeeded
bSucess = FALSE;
break;
}
}
// this directory's name is not one we are searching for
else
{
// so skip to next item in current directory
pItem = m_TreeCtrl.GetNextItem( pItem, TVGN_NEXT);
// if we have wrapped back to start of items list, we didn't find any dir with
// selected name
if( pStartItem == pItem)
{
// so we weren't successful
bSucess = FALSE;
break;
}
}
}
// directory search finished, if selected subdirectory wasn't found
if( !bSucess)
{
// select root item
pItem = pRootItem;
// stop searching
break;
}
}
return pItem;
}
/*
* Selects subdirectory using given path
*/
void CBrowser::SelectVirtualDirectory( CTString strArray[], INDEX iSubDirsCt)
{
// get virtual directory item
HTREEITEM pItem = GetVirtualDirectoryItem( strArray, iSubDirsCt);
// select last found directory
m_TreeCtrl.SelectItem( pItem);
// make it visible
m_TreeCtrl.EnsureVisible( pItem);
}
CTFileName CBrowser::GetIOFileName(CTString strTitle, BOOL bSave)
{
// You can't save with no directories
if( bSave && m_TreeCtrl.GetCount()==0)
return CTString("");
CWorldEditorApp *pApp = (CWorldEditorApp *)AfxGetApp();
CMainFrame* pMainFrame = STATIC_DOWNCAST(CMainFrame, AfxGetMainWnd());
char chrChoosedFileName[ 256];
OPENFILENAMEA ofnSaveVirtualTree;
const char *pFilters = "Virtual tree files (*.vrt)\0*.vrt\0\0";
strcpy( chrChoosedFileName, pMainFrame->m_fnLastVirtualTree.FileName());
strcat( chrChoosedFileName, pMainFrame->m_fnLastVirtualTree.FileExt());
memset( &ofnSaveVirtualTree, 0, sizeof( OPENFILENAME));
ofnSaveVirtualTree.lStructSize = sizeof(OPENFILENAME);
ofnSaveVirtualTree.lpstrFilter = pFilters;
ofnSaveVirtualTree.lpstrFile = chrChoosedFileName;
ofnSaveVirtualTree.nMaxFile = 256;
char strInitDir[ 256];
strcpy(strInitDir, IDir::AppPath() + pMainFrame->m_fnLastVirtualTree.FileDir());
ofnSaveVirtualTree.lpstrInitialDir = strInitDir;
ofnSaveVirtualTree.lpstrTitle = strTitle;
ofnSaveVirtualTree.Flags = OFN_EXPLORER | OFN_ENABLEHOOK;
ofnSaveVirtualTree.lpstrDefExt = "vrt";
ofnSaveVirtualTree.hwndOwner = pMainFrame->m_hWnd;
BOOL bResult=FALSE;
if( bSave)
{
bResult=GetSaveFileNameA( &ofnSaveVirtualTree);
}
else
{
bResult=GetOpenFileNameA( &ofnSaveVirtualTree);
}
if( !bResult)
{
m_TreeCtrl.SetFocus();
return CTString("");
}
CTFileName fnTree = CTString(chrChoosedFileName);
try
{
fnTree.RemoveApplicationPath_t();
}
catch( char *str_err)
{
AfxMessageBox( CString(str_err));
return CTString("");
}
return fnTree;
}
void CBrowser::SaveVirtualTree(CTFileName fnSave, CVirtualTreeNode *pvtn)
{
ASSERT(pvtn!=NULL);
if(pvtn==NULL) return;
CMainFrame* pMainFrame = STATIC_DOWNCAST(CMainFrame, AfxGetMainWnd());
CTFileStream File;
try
{
File.Create_t( fnSave, CTStream::CM_BINARY);
File.WriteID_t( CChunkID("VRTT"));
File.WriteID_t( CChunkID(VIRTUAL_TREE_VERSION));
pvtn->Write_t( &File); // This will recursivly save tree beneath given tree node
// Now we will try to save selected item's path
File.WriteID_t( CChunkID("SELC"));
// write ID of virtual tree buffer
File.WriteID_t( CChunkID("VTBF"));
// obtain selected directpry's path
CTString strArray[ 32];
INDEX iSubDirsCt = GetSelectedDirectory( strArray);
// write count of sub-directories
File << iSubDirsCt;
// write names of all subdirectories along path into file
INDEX i=0;
for( ; i<iSubDirsCt; i++)
{
File << strArray[ i];
}
// now save all of virtual tree buffers
for( i=0; i<DIRECTORY_SHORTCT_CT; i++)
{
// write ID of virtual tree buffer
File.WriteID_t( CChunkID("VTBF"));
// write count of sub-directories
INDEX iSubDirsCt = m_aiSubDirectoriesCt[ i];
File << iSubDirsCt;
// write names of all subdirectories along path into file
for( INDEX j=0; j<iSubDirsCt; j++)
{
File << m_astrVTreeBuffer[i][j];
}
}
// save marker for end of virtual tree
File.WriteID_t( CChunkID("END_"));
// close the file
File.Close();
}
catch( char *str_err)
{
AfxMessageBox( CString(str_err));
return;
}
m_TreeCtrl.SetFocus();
m_bVirtualTreeChanged = FALSE;
}
void CBrowser::OnSaveVirtualTree()
{
CMainFrame* pMainFrame = STATIC_DOWNCAST(CMainFrame, AfxGetMainWnd());
// save whole virtual tree
SaveVirtualTree(pMainFrame->m_fnLastVirtualTree, &m_VirtualTree);
}
void CBrowser::OnSaveAsVirtualTree()
{
CMainFrame* pMainFrame = STATIC_DOWNCAST(CMainFrame, AfxGetMainWnd());
CTFileName fnSave=GetIOFileName("Save virtual tree to file", TRUE);
if( fnSave!="")
{
// remember choosed file name as last used
pMainFrame->m_fnLastVirtualTree = fnSave;
// save virtual tree
SaveVirtualTree(fnSave, &m_VirtualTree);
}
}
void CBrowser::OnLoadVirtualTree()
{
CMainFrame* pMainFrame = STATIC_DOWNCAST(CMainFrame, AfxGetMainWnd());
if( m_bVirtualTreeChanged)
{
if( ::MessageBoxA( this->m_hWnd, "Current virtual directory not saved. Do You want to continue anyway?",
"Warning !", MB_YESNO | MB_ICONWARNING | MB_DEFBUTTON1 |
MB_TASKMODAL | MB_TOPMOST) != IDYES)
{
m_TreeCtrl.SetFocus();
return;
}
}
CTFileName fnOpen=GetIOFileName("Load virtual tree", FALSE);
if( fnOpen!="")
{
// remember choosed file name as last used
pMainFrame->m_fnLastVirtualTree = fnOpen;
// open virtual tree branch
OnLoadVirtualTreeInternal(fnOpen, NULL);
}
}
void CBrowser::OnImportVirtualTree()
{
CTFileName fnImport=GetIOFileName("Import virtual tree", FALSE);
if( fnImport!="")
{
HTREEITEM pSelectedItem = m_TreeCtrl.GetSelectedItem();
if( pSelectedItem == NULL) return;
CVirtualTreeNode *pVTN = (CVirtualTreeNode *)m_TreeCtrl.GetItemData( pSelectedItem);
// open virtual tree branch
OnLoadVirtualTreeInternal(fnImport, pVTN);
}
}
void CBrowser::OnExportVirtualTree()
{
HTREEITEM pSelectedItem = m_TreeCtrl.GetSelectedItem();
if( pSelectedItem == NULL) return;
CMainFrame* pMainFrame = STATIC_DOWNCAST(CMainFrame, AfxGetMainWnd());
CTFileName fnExport=GetIOFileName("Export virtual tree", TRUE);
if( fnExport!="")
{
CVirtualTreeNode *pVTN = (CVirtualTreeNode *)m_TreeCtrl.GetItemData( pSelectedItem);
// save virtual tree branch
SaveVirtualTree(fnExport, pVTN);
}
}
void CBrowser::OnUpdateImportVirtualTree(CCmdUI* pCmdUI)
{
HTREEITEM pSelectedItem = m_TreeCtrl.GetSelectedItem();
pCmdUI->Enable( pSelectedItem != NULL);
}
void CBrowser::OnUpdateExportVirtualTree(CCmdUI* pCmdUI)
{
HTREEITEM pSelectedItem = m_TreeCtrl.GetSelectedItem();
pCmdUI->Enable( pSelectedItem != NULL);
}
void CBrowser::OnLoadVirtualTreeInternal(CTFileName fnVirtulTree, CVirtualTreeNode *pvtnRoot)
{
CloseSelectedDirectory();
try
{
LoadVirtualTree_t( fnVirtulTree, pvtnRoot);
}
catch( char *strError)
{
AfxMessageBox( CString(strError));
FORDELETELIST( CVirtualTreeNode, vtn_lnInDirectory, m_VirtualTree.vtn_lhChildren, litDel)
{
delete &litDel.Current();
}
return;
}
m_TreeCtrl.SetFocus();
m_bVirtualTreeChanged = FALSE;
OpenSelectedDirectory();
}
void CBrowser::LoadVirtualTree_t( CTFileName fnVirtulTree, CVirtualTreeNode *pvtnRoot)
{
if( pvtnRoot==NULL)
{
// If current tree is not empty, delete it
if( m_TreeCtrl.GetCount() != 0)
{
FORDELETELIST( CVirtualTreeNode, vtn_lnInDirectory, m_VirtualTree.vtn_lhChildren, litDel)
{
delete &litDel.Current();
}
m_TreeCtrl.SelectItem( m_TreeCtrl.GetRootItem());
DeleteDirectory();
}
}
CTFileStream File;
File.Open_t( fnVirtulTree, CTStream::OM_READ);
if( !(CChunkID("VRTT") == File.GetID_t()))
{
throw( "This is not valid virtual tree file");
}
if( !(CChunkID(VIRTUAL_TREE_VERSION) == File.GetID_t()))
{
throw( "Invalid version of virtual tree file.");
}
if( pvtnRoot==NULL)
{
m_VirtualTree.Read_t( &File, NULL);
}
else
{
CVirtualTreeNode *pVTNNew = new CVirtualTreeNode;
pVTNNew->Read_t( &File, pvtnRoot);
}
// delete all items
m_TreeCtrl.DeleteAllItems();
AddDirectoryRecursiv( &m_VirtualTree, TVI_ROOT); // Fill CTreeCtrl using recursion
if( pvtnRoot!=NULL)
{
// don't read rest of data
File.Close();
return;
}
// Now we will try to load selected directory definition
File.ExpectID_t( CChunkID("SELC"));
// expect ID for virtual tree buffer
File.ExpectID_t( CChunkID("VTBF"));
INDEX iSubDirsCt;
// read count of sub-directories
File >> iSubDirsCt;
// limited number of sub directories
ASSERT( iSubDirsCt <= 32);
// we will load selected directories from root up to finaly selected
CTString strArray[ 32];
// load as many subdirectory names as count says
INDEX i=0;
for( ; i<iSubDirsCt; i++)
{
File >> strArray[ i];
}
// try to select directory
SelectVirtualDirectory( strArray, iSubDirsCt);
// now read all four virtual tree buffers
for( i=0; i<DIRECTORY_SHORTCT_CT; i++)
{
// expect ID for virtual tree buffer
File.ExpectID_t( CChunkID("VTBF"));
// read count of sub-directories
File >> iSubDirsCt;
// store it in array of counters
m_aiSubDirectoriesCt[ i] = iSubDirsCt;
// limited number of sub directories
ASSERT( iSubDirsCt <= 32);
// load as many subdirectory names as count says
for( INDEX j=0; j<iSubDirsCt; j++)
{
File >> m_astrVTreeBuffer[i][j];
}
}
File.ExpectID_t( CChunkID("END_"));
File.Close();
}
void CBrowser::OnRenameDirectory()
{
if( m_TreeCtrl.GetCount() != 0)
{
HTREEITEM pSelectedItem = m_TreeCtrl.GetSelectedItem();
if( pSelectedItem == NULL) return;
CVirtualTreeNode *pVTN = (CVirtualTreeNode *)m_TreeCtrl.GetItemData( pSelectedItem);
CDlgCreateVirtualDirectory dlg( pVTN->vtn_strName, CTString("Rename virtual directory") );
if( dlg.DoModal() == IDOK)
{
pVTN->vtn_itIconType = dlg.m_iSelectedIconType;
pVTN->vtn_strName = MfcStringToCT(dlg.m_strDirectoryName);
m_TreeCtrl.SetItemImage( pSelectedItem, pVTN->vtn_itIconType,
pVTN->vtn_itIconType + NO_OF_ICONS);
m_TreeCtrl.SetItemText( pSelectedItem, CString(pVTN->vtn_strName));
m_bVirtualTreeChanged = TRUE;
}
m_TreeCtrl.SetFocus();
}
}
void CBrowser::OnContextMenu(CWnd* pWnd, CPoint point)
{
CRect rectBrowser;
GetWindowRect( &rectBrowser);
CPoint ptInBrowser = CPoint( point.x - rectBrowser.TopLeft().x,
point.y - rectBrowser.TopLeft().y);
PIXaabbox2D boxPoint = PIXaabbox2D( PIX2D(ptInBrowser.x, ptInBrowser.y) );
if( (m_boxBrowseWnd & boxPoint) == boxPoint)
{
m_BrowseWindow.OnContextMenu( point);
}
else if( (m_boxTreeWnd & boxPoint) == boxPoint)
{
m_TreeCtrl.OnContextMenu( point);
}
}
CVirtualTreeNode *CBrowser::GetSelectedDirectory(void)
{
if( m_TreeCtrl.GetCount() != 0)
{
HTREEITEM pSelectedItem = m_TreeCtrl.GetSelectedItem();
if( pSelectedItem!=NULL)
{
return (CVirtualTreeNode *)m_TreeCtrl.GetItemData( pSelectedItem);
}
}
return NULL;
}
void CBrowser::OpenSelectedDirectory(void)
{
CVirtualTreeNode *pVTN = GetSelectedDirectory();
if( pVTN != NULL)
{
m_BrowseWindow.OpenDirectory( pVTN);
}
}
void CBrowser::CloseSelectedDirectory(void)
{
CVirtualTreeNode *pVTN = GetSelectedDirectory();
if( pVTN != NULL)
{
m_BrowseWindow.CloseDirectory( pVTN);
}
}
void CBrowser::OnDumpVt()
{
CTFileName fnName = _EngineGUI.FileRequester( "Dump virtual tree as ...",
"Text file\0*.txt\0" FILTER_ALL FILTER_END, "Dump virtual tree directory", "VirtualTrees\\");
if( fnName == "") return;
CTFileStream FileDump;
try
{
FileDump.Create_t( fnName, CTStream::CM_TEXT);
m_VirtualTree.Dump(&FileDump);
FileDump.Close();
}
catch( char *str_err)
{
AfxMessageBox( CString(str_err));
}
}