forked from FNNDSC/KWWidgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkKWDirectoryExplorer.cxx
2741 lines (2325 loc) · 80.4 KB
/
vtkKWDirectoryExplorer.cxx
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
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*=========================================================================
Module: $RCSfile: vtkKWDirectoryExplorer.cxx,v $
Copyright (c) Kitware, Inc.
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkKWDirectoryExplorer.h"
#include "vtkKWApplication.h"
#include "vtkKWEntry.h"
#include "vtkKWOptions.h"
#include "vtkKWEntryWithLabel.h"
#include "vtkKWIcon.h"
#include "vtkKWInternationalization.h"
#include "vtkKWFileBrowserUtilities.h"
#include "vtkKWLabel.h"
#include "vtkKWMenu.h"
#include "vtkKWPushButton.h"
#include "vtkKWPushButtonWithMenu.h"
#include "vtkKWScrollbar.h"
#include "vtkKWSimpleEntryDialog.h"
#include "vtkKWTkUtilities.h"
#include "vtkKWToolbar.h"
#include "vtkKWTree.h"
#include "vtkKWTreeWithScrollbars.h"
#include "vtkDirectory.h"
#include "vtkObjectFactory.h"
#include <vtksys/SystemTools.hxx>
#include <vtksys/ios/sstream>
#include <vtksys/stl/string>
#include <vtksys/stl/list>
#include <vtksys/stl/algorithm>
#include <sys/stat.h>
#include <time.h>
#ifdef _WIN32
#define _WIN32_WINNT 0x0501
#include "vtkWindows.h" //for GetLogicalDrives on Windows
#include <shellapi.h>
#include <shlobj.h>
#endif
//#define _MY_DEBUG
//----------------------------------------------------------------------------
vtkStandardNewMacro( vtkKWDirectoryExplorer );
//vtkCxxRevisionMacro(vtkKWDirectoryExplorer, "$Revision: 1.51 $");
vtkIdType vtkKWDirectoryExplorer::IdCounter = 1;
//----------------------------------------------------------------------------
class vtkKWDirectoryExplorerInternals
{
public:
vtkKWDirectoryExplorerInternals()
{
this->RootNode = "root";
this->IsNavigatingNode = 0;
this->IsOpeningDirectory = 0;
this->TempPath = "";
this->MostRecentDirCurrent = this->MostRecentDirList.begin();
}
// Most recent directories list (history)
typedef vtksys_stl::list<vtksys_stl::string> MostRecentDirContainer;
typedef vtksys_stl::list<vtksys_stl::string>::iterator MostRecentDirIterator;
MostRecentDirContainer MostRecentDirList;
MostRecentDirIterator MostRecentDirCurrent;
const char* RootNode;
int IsNavigatingNode;
int IsOpeningDirectory;
vtksys_stl::string TempPath;
vtksys_stl::string FolderImage;
};
//----------------------------------------------------------------------------
vtkKWDirectoryExplorer::vtkKWDirectoryExplorer()
{
this->MaximumNumberOfDirectoriesInHistory = 20;
this->Internals = new vtkKWDirectoryExplorerInternals;
this->Toolbar = vtkKWToolbar::New();
this->DirectoryTree = vtkKWTreeWithScrollbars::New();
this->CreateFolderButton = vtkKWPushButton::New();
this->BackButton = vtkKWPushButtonWithMenu::New();
this->ForwardButton = vtkKWPushButtonWithMenu::New();
this->UpButton = vtkKWPushButton::New();
this->ContextMenu = NULL;
this->DirectorySelectedCommand = NULL;
this->DirectoryCreatedCommand = NULL;
this->DirectoryDeletedCommand = NULL;
this->DirectoryOpenedCommand = NULL;
this->DirectoryClosedCommand = NULL;
this->DirectoryRenamedCommand = NULL;
}
//----------------------------------------------------------------------------
vtkKWDirectoryExplorer::~vtkKWDirectoryExplorer()
{
this->DirectoryTree->Delete();
this->CreateFolderButton->Delete();
this->BackButton->Delete();
this->ForwardButton->Delete();
this->UpButton->Delete();
this->Toolbar->Delete();
if (this->DirectoryCreatedCommand)
{
delete [] this->DirectoryCreatedCommand;
this->DirectoryCreatedCommand = NULL;
}
if (this->DirectoryDeletedCommand)
{
delete [] this->DirectoryDeletedCommand;
this->DirectoryDeletedCommand = NULL;
}
if (this->DirectoryOpenedCommand )
{
delete [] this->DirectoryOpenedCommand;
this->DirectoryOpenedCommand = NULL;
}
if (this->DirectoryClosedCommand)
{
delete [] this->DirectoryClosedCommand;
this->DirectoryClosedCommand = NULL;
}
if (this->DirectorySelectedCommand)
{
delete [] this->DirectorySelectedCommand;
this->DirectorySelectedCommand = NULL;
}
if (this->DirectoryRenamedCommand)
{
delete [] this->DirectoryRenamedCommand;
this->DirectoryRenamedCommand = NULL;
}
if (this->ContextMenu)
{
this->ContextMenu->Delete();
this->ContextMenu = NULL;
}
if (this->Internals)
{
delete this->Internals;
}
}
//----------------------------------------------------------------------------
void vtkKWDirectoryExplorer::CreateWidget()
{
// Check if already created
if (this->IsCreated())
{
vtkErrorMacro(<< this->GetClassName() << " already created");
return;
}
// Call the superclass to create the whole widget
this->Superclass::CreateWidget();
// Toolbar
this->Toolbar->SetParent(this);
this->Toolbar->Create();
// Go back button
this->BackButton->SetParent(this->Toolbar->GetFrame());
this->BackButton->Create();
this->BackButton->GetPushButton()->SetImageToPredefinedIcon(
vtkKWIcon::IconBrowserBack);
this->BackButton->SetBalloonHelpString("Back to previous directory");
this->BackButton->GetPushButton()->SetCommand(
this, "BackToPreviousDirectoryCallback");
this->BackButton->GetPushButton()->SetConfigurationOptionAsInt(
"-takefocus", 0);
this->Toolbar->AddWidget(this->BackButton);
// Go forward button
this->ForwardButton->SetParent(this->Toolbar->GetFrame());
this->ForwardButton->Create();
this->ForwardButton->GetPushButton()->SetImageToPredefinedIcon(
vtkKWIcon::IconBrowserForward);
this->ForwardButton->SetBalloonHelpString("Go to next directory");
this->ForwardButton->GetPushButton()->SetCommand(
this, "ForwardToNextDirectoryCallback");
this->ForwardButton->GetPushButton()->SetConfigurationOptionAsInt(
"-takefocus", 0);
this->Toolbar->AddWidget(this->ForwardButton);
// Go up button
this->UpButton->SetParent(this->Toolbar->GetFrame());
this->UpButton->Create();
this->UpButton->SetImageToPredefinedIcon(vtkKWIcon::IconBrowserUp);
this->UpButton->SetBalloonHelpString("Go up one directory");
this->UpButton->SetCommand(this, "GoUpDirectoryCallback");
this->UpButton->SetConfigurationOptionAsInt(
"-takefocus", 0);
this->Toolbar->AddWidget(this->UpButton);
// Create folder button
this->CreateFolderButton->SetParent(this->Toolbar->GetFrame());
this->CreateFolderButton->Create();
this->CreateFolderButton->SetImageToPredefinedIcon(
vtkKWIcon::IconFolderNew);
this->CreateFolderButton->SetBalloonHelpString("Create new folder");
this->CreateFolderButton->SetCommand(
this, "CreateNewFolderCallback");
this->CreateFolderButton->SetConfigurationOptionAsInt(
"-takefocus", 0);
this->Toolbar->AddWidget(this->CreateFolderButton);
this->Toolbar->SetToolbarAspectToFlat();
this->Toolbar->SetWidgetsAspectToFlat();
this->Script("pack %s -side top -anchor nw",
this->Toolbar->GetWidgetName());
// Directory Tree
this->DirectoryTree->SetParent(this);
this->DirectoryTree->Create();
this->DirectoryTree->GetVerticalScrollbar()->SetConfigurationOptionAsInt(
"-takefocus", 0);
this->DirectoryTree->GetHorizontalScrollbar()->SetConfigurationOptionAsInt(
"-takefocus", 0);
vtkKWTree *dirtree = this->DirectoryTree->GetWidget();
dirtree->SetBorderWidth(2);
dirtree->SetReliefToGroove();
dirtree->SetSelectionModeToSingle();
dirtree->SelectionFillOn();
dirtree->SetLinesVisibility(0);
dirtree->SetPadX(20);
dirtree->SetWidth(30);
dirtree->SetHeight(15);
dirtree->SetBackgroundColor(1.0, 1.0, 1.0);
dirtree->UseRawNodeUserDataOn();
dirtree->UseRawNodeTextOn();
dirtree->SetOpenCommand(this,"DirectoryOpenedCallback");
dirtree->SetCloseCommand(this, "DirectoryClosedCallback");
dirtree->SetSingleClickOnNodeCommand(this, "SingleClickOnNodeCallback");
dirtree->SetBinding("<Delete>", this, "RemoveSelectedNodeCallback");
dirtree->SetSelectionChangedCommand(this, "DirectorySelectedCallback");
dirtree->SetRightClickOnNodeCommand(this, "RightClickCallback %X %Y");
dirtree->SetBinding("<F2>", this, "RenameCallback");
this->Script(
"pack %s -side top -fill both -expand true -padx 1 -pady 1",
this->DirectoryTree->GetWidgetName());
// Setup the image for the tree node
this->Internals->FolderImage = dirtree->GetWidgetName();
this->Internals->FolderImage.append("_0");
vtkKWIcon *tmpIcon = vtkKWIcon::New();
tmpIcon->SetImage(vtkKWIcon::IconFolderXP);
if (!vtkKWTkUtilities::UpdatePhoto(this->GetApplication(),
this->Internals->FolderImage.c_str(),
tmpIcon->GetData(),
tmpIcon->GetWidth(),
tmpIcon->GetHeight(),
tmpIcon->GetPixelSize()))
{
vtkWarningMacro(
<< "Error updating Tk photo "
<< this->Internals->FolderImage.c_str());
}
tmpIcon->Delete();
// Load root directory the first time we are mapped
this->LoadRootDirectory();
// dirtree->SetBinding("<Map>", this, "LoadRootDirectoryCallback");
//Update the Back/Forward button. Should be disabled
this->Update();
}
//----------------------------------------------------------------------------
void vtkKWDirectoryExplorer::LoadRootDirectoryCallback()
{
this->DirectoryTree->GetWidget()->RemoveBinding(
"<Map>", this, "LoadRootDirectoryCallback");
this->LoadRootDirectory();
}
//----------------------------------------------------------------------------
void vtkKWDirectoryExplorer::LoadRootDirectory()
{
#ifndef _WIN32 // UNIX flavor
vtkIdType dirID = vtkKWDirectoryExplorer::IdCounter++;
vtkKWIcon *tmpIcon = vtkKWIcon::New();
tmpIcon->SetImage(vtkKWIcon::IconFolderXP);
char strDirID[20];
sprintf(strDirID, "%lu", (long unsigned int)dirID);
this->AddDirectoryNode(
this->Internals->RootNode,
strDirID,
"/",
KWFileBrowser_UNIX_ROOT_DIRECTORY,
tmpIcon);
tmpIcon->Delete();
//this->UpdateDirectoryNode(strDirID);
this->OpenDirectoryNode(strDirID, 0, 1);
#else // Windows flavor
vtksys_stl::string name;
vtksys_stl::string realname;
vtksys_stl::string disklabel;
char strVolName[MAX_PATH];
char strFS[MAX_PATH];
DWORD serialnum;
vtkKWIcon *tmpIcon = vtkKWIcon::New();
// Loop over drive letters
DWORD mask;
for (mask = GetLogicalDrives(), name = "A:"; mask; mask >>= 1, name[0]++)
{
// Skip unavailable drives
if (!(mask&1))
{
continue;
}
// Find out drive types
UINT drivetype = GetDriveTypeA(name.c_str());
switch (drivetype)
{
case DRIVE_REMOVABLE:
if (name[0] == 'A' || name[0] == 'B')
{
tmpIcon->SetImage(vtkKWIcon::IconFloppyDrive);
}
else
{
//maybe a zip icon?
tmpIcon->SetImage(vtkKWIcon::IconFloppyDrive);
}
disklabel = "Removable";
break;
case DRIVE_REMOTE:
tmpIcon->SetImage(vtkKWIcon::IconNetDrive);
disklabel = "Network";
break;
case DRIVE_CDROM:
tmpIcon->SetImage(vtkKWIcon::IconCdRom);
disklabel = "CD-ROM";
break;
case DRIVE_RAMDISK:
tmpIcon->SetImage(vtkKWIcon::IconFolderXP);
disklabel = "RAM Disk";
break;
case DRIVE_FIXED:
tmpIcon->SetImage(vtkKWIcon::IconHardDrive);
disklabel = "Local Disk";
break;
case DRIVE_UNKNOWN:
case DRIVE_NO_ROOT_DIR:
default:
tmpIcon->SetImage(vtkKWIcon::IconFolderXP);
disklabel = "Unknown";
break;
}
// if there is an end backslash, remove it.
char* cleaned_name = vtksys::SystemTools::RemoveChars(
name.c_str(), "\\");
if (cleaned_name)
{
name = cleaned_name;
delete [] cleaned_name;
}
else
{
continue;
}
realname = disklabel;
if(drivetype != DRIVE_REMOTE)
{
vtksys_stl::string volPath = name;
if (GetVolumeInformation(volPath.append("\\").c_str(),
strVolName, MAX_PATH, &serialnum, NULL,
NULL, strFS,
MAX_PATH))
{
if (strcmp(strVolName, ""))
{
realname = strVolName;
}
}
}
else
{
DWORD dwUniSz = 1024;
if(!WNetGetConnection(name.c_str(),
strVolName,
&dwUniSz))
{
if (strcmp(strVolName, ""))
{
realname = strVolName;
}
}
}
realname += " (";
realname += name;
realname += ")";
// Add directory node
vtkIdType dirID = vtkKWDirectoryExplorer::IdCounter++;
char strDirID[20];
sprintf(strDirID, "%lu", dirID);
// Be consistent with SystemTools::GetParentDirectory() returning as "C:/"
vtksys_stl::string dirname = name;
dirname += '\\';
this->AddDirectoryNode(this->Internals->RootNode,
strDirID,
realname.c_str(),
dirname.c_str(),
tmpIcon);
}
tmpIcon->Delete();
#endif
}
//----------------------------------------------------------------------------
void vtkKWDirectoryExplorer::AddDirectoryNode(
const char* parentnode,
const char* node,
const char* text,
const char* fullname,
vtkKWIcon *nodeicon)
{
this->DirectoryTree->GetWidget()->AddNode(parentnode, node, text);
this->DirectoryTree->GetWidget()->SetNodeUserData(
node, vtksys::SystemTools::EscapeChars(
fullname, KWFileBrowser_ESCAPE_CHARS).c_str());
this->DirectoryTree->GetWidget()->SetNodeImageToIcon(node, nodeicon);
}
//----------------------------------------------------------------------------
bool vtkKWDirectoryExplorerSortDirPredicate(const char *d1, const char *d2)
{
return vtksys::SystemTools::Strucmp(d1, d2) < 0 ? true : false;
}
//----------------------------------------------------------------------------
void vtkKWDirectoryExplorer::UpdateDirectoryNode(const char* node)
{
#if defined (_MY_DEBUG)
cout << "-----------------UpdateDirectoryNode: " << nodepath << endl;
clock_t start = clock();
#endif
vtkKWTree *dirtree = this->DirectoryTree->GetWidget();
const char *dirtreename = dirtree->GetWidgetName();
// Check if this node has children, if yes,
// check if those directory still there.
vtksys_stl::vector<vtksys_stl::string> children;
vtksys::SystemTools::Split(
dirtree->GetNodeChildren(node), children, ' ');
vtksys_stl::vector<vtksys_stl::string>::iterator node_it, node_end;
int num_children = (int)children.size();
vtksys_stl::list<vtksys_stl::string> children_text;
vtksys_stl::list<vtksys_stl::string>::iterator node_text_it, node_text_end;
node_it = children.begin();
node_end = children.end();
for (; node_it != node_end; node_it++)
{
children_text.push_back(dirtree->GetNodeText((*node_it).c_str()));
}
node_text_end = children_text.end();
#if defined (_MY_DEBUG)
double durationchildren = (double)(clock() - start) / CLOCKS_PER_SEC;
cout << "Get Node children time: " << durationchildren << endl;
clock_t start = clock();
#endif
vtksys_stl::string nodepath = dirtree->GetNodeUserData(node);
vtkDirectory *dir = vtkDirectory::New();
if (!dir->Open(nodepath.c_str()))
{
dir->Delete();
return;
}
int num_files = dir->GetNumberOfFiles();
#if defined (_MY_DEBUG)
double durationopen = (double)(clock() - start) / CLOCKS_PER_SEC;
cout << "Dir open time: " << durationopen << endl;
start = clock();
#endif
// First collect all the dirs
vtksys_stl::vector<const char*> dir_list;
dir_list.reserve(num_files);
bool dotfound = false, dotdotfound = false;
vtksys_stl::string strfilename;
for (int i = 0; i < num_files; i++)
{
const char *filename = dir->GetFile(i);
// skip . and ..
if (!dotfound || !dotdotfound)
{
if (strcmp(filename, ".") == 0)
{
dotfound=true;
continue;
}
else if (strcmp(filename, "..") == 0)
{
dotdotfound = true;
continue;
}
}
// Skip dir that has '\' in the name.
// To fully handle a directory name containing slash,
// we need to modify some methods in vtkDirectory and vtksys::SystemTools.
// Also, the UseRawNodeUserData and UseRawNodeText variables in vtkKWTree
// needs to be considered.
strfilename = filename;
if(strfilename.find("\\") != vtksys_stl::string::npos)
{
vtkWarningMacro(
<< "KWDirectoryExplorer currently does not support dir name containing slash \""
<< filename << "\" under path: " << nodepath.c_str());
continue;
}
if (dir->FileIsDirectory(filename))
{
dir_list.push_back(filename);
}
}
// Sort them, and process one by one
#ifdef _WIN32
// Already sorted on Win32
#else
vtksys_stl::sort(dir_list.begin(), dir_list.end(),
vtkKWDirectoryExplorerSortDirPredicate);
#endif
vtkIdType dirID;
char strDirID[20];
// Have these two flags so that we do not need to do strcmp
// for every file in the directory
vtksys_ios::ostringstream tk_cfgcmd;
vtksys_ios::ostringstream tk_treecmd;
// The following variables is for checking whether a
// directory has sub directories; if yes, then add the
// '+' before the tree node
vtkDirectory *tmpdir = vtkDirectory::New();
vtksys_stl::string tmp_str, tmp_file, tmp_name;
vtksys_stl::string treecmd = dirtreename;
treecmd.append(" insert end ").append(node).append(" ");
vtksys_stl::string fullname = "";
const char* image_name = this->Internals->FolderImage.c_str();
if (!KWFileBrowser_HasTrailingSlash(nodepath.c_str()))
{
nodepath += KWFileBrowser_PATH_SEPARATOR;
}
#if defined (_MY_DEBUG)
clock_t scriptstart = clock();
#endif
vtksys_stl::vector<const char*>::iterator dir_list_it = dir_list.begin();
vtksys_stl::vector<const char*>::iterator dir_list_end = dir_list.end();
int nb_new_dirs = 0;
int nb_dirs_found = 0;
for (; dir_list_it != dir_list_end; dir_list_it++)
{
const char *filename = *dir_list_it;
// if the node already has children, we need to find any
// new directories and add them
bool isadded = false;
if (num_children && nb_dirs_found < num_children)
{
node_text_it = children_text.begin();
for (; node_text_it != node_text_end; node_text_it++)
{
if (strcmp(node_text_it->c_str(), filename) == 0)
{
isadded = true;
nb_dirs_found++;
children_text.erase(node_text_it);
break;
}
}
}
// if this directory is not added yet, add this new directory
if (!isadded)
{
fullname = nodepath;
fullname += filename;
dirID = vtkKWDirectoryExplorer::IdCounter++;
sprintf(strDirID, "%lu", (long unsigned int)dirID);
tk_treecmd << treecmd;
tk_treecmd << strDirID << " -text {"
<< filename << "}" << " -image {"
<< image_name << "}" << " -data \""
<< vtksys::SystemTools::EscapeChars(
fullname.c_str(), KWFileBrowser_ESCAPE_CHARS).c_str()
<< "\"" << endl;
nb_new_dirs++;
#ifdef _WIN32 // disable that on Unix, too slow for NFS
#if defined (_MY_DEBUG)
start = clock();
#endif
#if 1
// Check if this new folder has subfolders.
if (!tmpdir->Open(fullname.c_str()))
{
continue;
}
tmp_str = fullname;
if(!KWFileBrowser_HasTrailingSlash(tmp_str.c_str()))
{
tmp_str += KWFileBrowser_PATH_SEPARATOR;
}
bool dot1found = false;
bool dot2found = false;
for(int j = 0; j < tmpdir->GetNumberOfFiles(); j++)
{
tmp_name = tmp_str;
tmp_file = tmpdir->GetFile(j);
// Skip . and ..
if (!dot1found || !dot2found)
{
if (!dot1found && strcmp(tmp_file.c_str(), ".") == 0)
{
dot1found = true;
continue;
}
if (!dot2found && strcmp(tmp_file.c_str(), "..") == 0)
{
dot2found = true;
continue;
}
}
tmp_name += tmp_file;
struct stat tmp_fs;
if (stat(tmp_name.c_str(), &tmp_fs) != 0)
{
continue;
}
else
{
if (
#if defined( _WIN32 )
tmp_fs.st_mode & _S_IFDIR
#else
(S_ISDIR(tmp_fs.st_mode))
#endif
)
{
tk_cfgcmd << dirtreename << " itemconfigure "
<< strDirID << " -drawcross allways" << endl;
break;
}
}
} //end for
#else // Try differently? Crashs on some Unixes...
vtksys_stl::string tmpNewdir = vtksys::SystemTools::EscapeChars(
fullname.c_str(), KWFileBrowser_ESCAPE_CHARS);
tk_cfgcmd << "set dir \"" << tmpNewdir.c_str() << "\"" <<endl;
tk_cfgcmd << "set contents [glob -nocomplain -directory $dir -type d *]" << endl;
tk_cfgcmd << "if { [llength $contents] } {" << endl;
tk_cfgcmd << dirtreename << " itemconfigure " << strDirID << " -drawcross allways" << endl;
tk_cfgcmd << "} else { " << endl;
tk_cfgcmd << "set newcontents [glob -nocomplain -directory $dir -type d .*]" << endl;
tk_cfgcmd << "set pos [lsearch -exact $newcontents \"" <<tmpNewdir.c_str()<<"/.\"]" <<endl;
tk_cfgcmd << "if { $pos >= 0 } {set newcontents [lreplace $newcontents $pos $pos]}" <<endl;
tk_cfgcmd << "set pos [lsearch -exact $newcontents \"" << tmpNewdir.c_str() << "/..\"]" <<endl;
tk_cfgcmd << "if { $pos >= 0 } {set newcontents [lreplace $newcontents $pos $pos]}" <<endl;
tk_cfgcmd << "if { [llength $newcontents] } {" << endl;
tk_cfgcmd << dirtreename << " itemconfigure " << strDirID << " -drawcross allways }" << endl;
tk_cfgcmd << " }" <<endl;
#endif
#if defined (_MY_DEBUG)
double durationsub = (double)(clock() - start) / CLOCKS_PER_SEC;
cout << tmp_name << "---- Check sub folder time: "
<< durationsub << endl;
#endif
#endif // Win32, do not check for subdirs, too slow for NFS
}//end if (!added)
}//end for
tmpdir->Delete();
#if defined (_MY_DEBUG)
double durationscript = (double)(clock() - scriptstart) / CLOCKS_PER_SEC;
cout << "Creat Script time: " << durationscript << endl;
start = clock();
#endif
// Run add the tree node command if available
if (tk_treecmd.str() != "" && nb_new_dirs > 0)
{
vtkKWTkUtilities::EvaluateSimpleString(
this->GetApplication(), tk_treecmd.str().c_str());
}
// Run the script for adding/removing 'cross' image to tree node
if (tk_cfgcmd.str() != "")
{
vtkKWTkUtilities::EvaluateSimpleString(
this->GetApplication(), tk_cfgcmd.str().c_str());
}
#if defined (_MY_DEBUG)
double durationrun = (double)(clock() - start) / CLOCKS_PER_SEC;
cout << "Run script time: " << durationrun << endl;
start = clock();
#endif
// The node has child nodes already, we need to make sure
// the child node directories are still there,
// otherwise, remove them
if (num_children && nb_dirs_found < num_children)
{
node_it = children.begin();
for (; node_it != node_end; node_it++)
{
if (!vtksys::SystemTools::FileExists(
dirtree->GetNodeUserData((*node_it).c_str())))
{
dirtree->DeleteNode((*node_it).c_str());
}
}
}
dir->Delete();
#if defined (_MY_DEBUG)
double durationclean = (double)(clock() - start) / CLOCKS_PER_SEC;
cout << "Check dir exists time: " << durationclean << endl;
#endif
}
//----------------------------------------------------------------------------
void vtkKWDirectoryExplorer::OpenDirectoryNode(const char* node,
int select,
int opennode)
{
// Change mouse cursor to wait.
vtksys_stl::string node_str = node;
if (!this->DirectoryTree->GetWidget()->HasNode(node_str.c_str()))
{
return;
}
vtkKWTkUtilities::SetTopLevelMouseCursor(this, "watch");
// Set internal flag
this->Internals->IsOpeningDirectory = 1;
// Check/Load all the directories and files under this node
this->UpdateDirectoryNode(node_str.c_str());
// Open the node
if (opennode &&
!this->DirectoryTree->GetWidget()->IsNodeOpen(node_str.c_str()))
{
this->DirectoryTree->GetWidget()->OpenNode(node_str.c_str());
this->DirectoryTree->GetWidget()->DisplayChildNodes(node_str.c_str());
}
// Select the node
if (select)
{
if(this->DirectoryTree->GetWidget()->GetSelectionMode() ==
vtkKWOptions::SelectionModeSingle)
{
this->DirectoryTree->GetWidget()->ClearSelection();
}
this->DirectoryTree->GetWidget()->SelectNode(node_str.c_str());
this->DirectoryTree->GetWidget()->SeeNode(node_str.c_str());
this->InvokeDirectorySelectedCommand(this->GetNthSelectedDirectory(0));
}
// Update dir history list
if (!this->Internals->IsNavigatingNode)
{
this->UpdateMostRecentDirectoryHistory(node_str.c_str());
}
// Update Back/Forward button state
this->Update();
this->Internals->IsOpeningDirectory = 0;
// Set back mouse cursor
vtkKWTkUtilities::SetTopLevelMouseCursor(this, NULL);
}
//----------------------------------------------------------------------------
void vtkKWDirectoryExplorer::Update()
{
if (!this->DirectoryTree->IsCreated())
{
return;
}
this->UpdateEnableState();
vtksys_stl::string callback = "OpenDirectoryNodeCallback ";
// History buttons
if (this->Internals->MostRecentDirList.size() > 1)
{
// First item in the list
if (this->Internals->MostRecentDirCurrent ==
this->Internals->MostRecentDirList.begin())
{
this->ForwardButton->SetEnabled(0);
}
// Last item in the list
else if (strcmp((*this->Internals->MostRecentDirCurrent).c_str(),
this->Internals->MostRecentDirList.back().c_str()) == 0)
{
this->BackButton->SetEnabled(0);
}
}
else
{
this->ForwardButton->SetEnabled(0);
this->BackButton->SetEnabled(0);
}
// History forward/Backward
vtkKWTree *tree = this->DirectoryTree->GetWidget();
if (this->Internals->MostRecentDirCurrent !=
this->Internals->MostRecentDirList.end())
{
// History forward
vtkKWMenu *menu = this->ForwardButton->GetMenu();
menu->DeleteAllItems();
vtkKWDirectoryExplorerInternals::MostRecentDirIterator it =
this->Internals->MostRecentDirCurrent;
vtksys_stl::string menucommand;
vtksys_stl::string menutext;
int offset = -1;
char buff[10];
// Keep the most recent item
if (it != this->Internals->MostRecentDirList.begin())
{
it--;
}
while (it != this->Internals->MostRecentDirList.begin())
{
menutext = tree->GetNodeText((*it).c_str());
menucommand = callback;
menucommand.append((*it).c_str());
sprintf(buff, " %d", offset);
menucommand.append(buff);
menu->AddCommand(menutext.c_str(), this, menucommand.c_str());
offset--;
it--;
}
// Now, the first item of the list
// NOTE: that deserve more comment, what is going on here?
if (tree->HasNode((*it).c_str()))
{
menucommand = callback;
menutext = tree->GetNodeText((*it).c_str());
menucommand.append((*it).c_str());
sprintf(buff, " %d", offset);
menucommand.append(buff);
menu->AddCommand(menutext.c_str(), this, menucommand.c_str());
}