1 using System;
2 using System.Collections;
3 using System.Windows;
4
5 namespace FileExplorer
6 {
7 PathBar* MakePathBar(ImageList* imageList)
8 {
9 PathBar* pathBar = new PathBar(PathBarCreateParams(PathBarControlCreateParams(ControlCreateParams().Defaults())),
10 EmptyViewControlCreateParams(ControlCreateParams().SetDock(Dock.left).SetSize(Size(100, 0))),
11 ParentPathSelectorCreateParams(ParentPathSelectorControlCreateParams(ControlCreateParams().Defaults()), imageList),
12 PathDividerCreateParams(PathDividerControlCreateParams(ControlCreateParams().Defaults())),
13 PathViewCreateParams(PathViewControlCreateParams(ControlCreateParams().SetDock(Dock.fill))));
14 return pathBar;
15 }
16
17 public class MainWindow : Window
18 {
19 public MainWindow(const string& caption) : base(
20 WindowCreateParams(WindowControlCreateParams(
21 ControlCreateParams().SetWindowClassBackgroundColor(SystemColor.COLOR_WINDOW).SetBackgroundColor(Color.White()),
22 caption)).Defaults())
23 {
24 imageList.AddImage("computer.bitmap");
25 imageList.AddImage("hard_drive.bitmap");
26 imageList.AddImage("folder_closed.bitmap");
27 imageList.AddImage("folder_opened.bitmap");
28 imageList.AddImage("file.bitmap");
29 imageList.AddImage("up.arrow.bitmap");
30 logicalDrives = GetLogicalDrives();
31 computer.Reset(new ComputerNode());
32 computer->SetMainWindow(this);
33 for (const string& driveName : logicalDrives)
34 {
35 UniquePtr<DriveNode> drivePtr(new DriveNode(driveName));
36 DriveNode* drive = drivePtr.Get();
37 drive->SetRootDirectory(new DirectoryNode(driveName + "/"));
38 computer->AddDrive(drivePtr.Release());
39 }
40 UniquePtr<PathBar> pathBarPtr(MakePathBar(&imageList));
41 pathBar = pathBarPtr.Get();
42 pathBar->GetParentPathSelector()->ClickEvent().AddHandler(ParentPathSelectorClick);
43 pathBar->GetPathView()->PathComponentSelectedEvent().AddHandler(PathComponentSelected);
44 UniquePtr<BorderedControl> borderedControl = new BorderedControl(BorderedControlCreateParams(BorderedControlControlCreateParams(
45 ControlCreateParams().SetSize(Size(0, 32)).SetDock(Dock.top), pathBar), pathBarPtr.Release()).
46 SetNormalBorderColor(DefaultPathBarFrameColor()));
47 AddChild(borderedControl.Release());
48 UniquePtr<SplitContainer> splitContainerPtr(new SplitContainer(Orientation.horizontal, 500, Point(), Size(), Dock.fill, Anchors.none));
49 splitContainer = splitContainerPtr.Get();
50 AddChild(splitContainerPtr.Release());
51 UniquePtr<DirectoryTreeView> directoryTreeViewPtr(new DirectoryTreeView(&imageList, computer.Get()));
52 directoryTreeView = directoryTreeViewPtr.Get();
53 directoryTreeView->SetMainWindow(this);
54 directoryTreeView->SizeChangedEvent().AddHandler(DirectoryTreeViewSizeChanged);
55 splitContainer->Pane1Container()->AddChild(directoryTreeViewPtr.Release());
56 UniquePtr<ContentView> contentViewPtr(new ContentView(&imageList));
57 contentView = contentViewPtr.Get();
58 contentView->SetMainWindow(this);
59 contentView->SizeChangedEvent().AddHandler(ContentViewSizeChanged);
60 splitContainer->Pane2Container()->AddChild(contentViewPtr.Release());
61 setSplitterDistance = true;
62 }
63 public inline nothrow ContentView* GetContentView() const
64 {
65 return contentView;
66 }
67 public inline nothrow DirectoryTreeView* GetDirectoryTreeView() const
68 {
69 return directoryTreeView;
70 }
71 public inline nothrow PathBar* GetPathBar() const
72 {
73 return pathBar;
74 }
75 public nothrow void AddListViewEventHandlers(ListView* listView)
76 {
77 listView->ItemClickEvent().AddHandler(ListViewItemClick);
78 listView->ItemRightClickEvent().AddHandler(ListViewItemRightClick);
79 listView->ItemDoubleClickEvent().AddHandler(ListViewItemDoubleClick);
80 }
81 protected override void OnShown()
82 {
83 base->OnShown();
84 if (setSplitterDistance)
85 {
86 setSplitterDistance = false;
87 Size sz = GetSize();
88 splitContainer->SetSplitterDistance(cast<int>(sz.w * 1.0 / 3.0));
89 }
90 }
91 protected override void OnKeyDown(KeyEventArgs& args)
92 {
93 try
94 {
95 base->OnKeyDown(args);
96 if (!args.handled)
97 {
98 switch (args.key)
99 {
100 case Keys.escape:
101 {
102 RemoveContextMenu();
103 args.handled = true;
104 break;
105 }
106 }
107 }
108 }
109 catch (const Exception& ex)
110 {
111 MessageBox.Show(ex.Message());
112 }
113 }
114 protected override void MouseUpNotification(MouseEventArgs& args)
115 {
116 try
117 {
118 if (args.buttons == MouseButtons.lbutton)
119 {
120 RemoveContextMenu();
121 }
122 }
123 catch (const Exception& ex)
124 {
125 MessageBox.Show(ex.Message());
126 }
127 }
128 private void ListViewItemClick(ListViewItemEventArgs& args)
129 {
130 if (args.item != null)
131 {
132 ListView* view = args.item->View();
133 if (view != null)
134 {
135 view->SetSelectedItem(args.item);
136 }
137 }
138 }
139 private void ListViewItemRightClick(ListViewItemEventArgs& args)
140 {
141 if (args.item != null)
142 {
143 ListView* view = args.view;
144 if (view != null)
145 {
146 view->SetSelectedItem(args.item);
147 void* data = args.item->Data();
148 if (data != null)
149 {
150 Node* node = cast<Node*>(data);
151 UniquePtr<ContextMenu> contextMenu(new ContextMenu());
152 node->AddMenuItems(contextMenu.Get(), false);
153 if (contextMenu->HasMenuItems())
154 {
155 view->TranslateContentLocation(args.location);
156 Point screenLoc = view->ClientToScreen(args.location);
157 ShowContextMenu(contextMenu.Release(), screenLoc);
158 }
159 }
160 }
161 }
162 }
163 private void ListViewItemDoubleClick(ListViewItemEventArgs& args)
164 {
165 if (args.item != null && args.item->Data() != null)
166 {
167 Node* node = cast<Node*>(args.item->Data());
168 node->ExecuteDefaultAction();
169 }
170 }
171 private void DirectoryTreeViewSizeChanged(uint windowState)
172 {
173 Size sz = directoryTreeView->GetSize();
174 pathBar->SetEmptyViewWidth(sz.w);
175 }
176 private void ContentViewSizeChanged(uint windowState)
177 {
178 Size sz = contentView->GetSize();
179 pathBar->SetPathViewMaxWidth(sz.w);
180 }
181 private void ParentPathSelectorClick()
182 {
183 Node* currentNode = pathBar->CurrentNode();
184 if (currentNode == null)
185 {
186 currentNode = directoryTreeView->SelectedNode();
187 }
188 if (currentNode != null)
189 {
190 currentNode = currentNode->Parent();
191 if (currentNode != null)
192 {
193 if (currentNode->IsRootDirectory())
194 {
195 currentNode = currentNode->Parent();
196 }
197 }
198 }
199 if (currentNode != null)
200 {
201 currentNode->Open();
202 }
203 }
204 private void PathComponentSelected(PathComponentEventArgs& args)
205 {
206 PathComponent* pathComponent = args.pathComponent;
207 if (pathComponent != null)
208 {
209 void* data = pathComponent->Data();
210 if (data != null)
211 {
212 Node* node = cast<Node*>(data);
213 node->Open();
214 }
215 }
216 }
217 private bool setSplitterDistance;
218 private ImageList imageList;
219 private List<string> logicalDrives;
220 private UniquePtr<ComputerNode> computer;
221 private PathBar* pathBar;
222 private SplitContainer* splitContainer;
223 private DirectoryTreeView* directoryTreeView;
224 private ContentView* contentView;
225 }
226 }