1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2006 Alexander Olk
24 // Alexander Olk alex.olk@googlemail.com
25 // Gert Driesen (drieseng@users.sourceforge.net)
26 // Eric Petit (surfzoid2002@yahoo.fr)
29 // Keyboard shortcuts (DEL, F5, F2)
33 using System.Collections;
34 using System.Collections.Specialized;
35 using System.ComponentModel;
38 using System.Resources;
40 using System.Threading;
43 namespace System.Windows.Forms
46 [DefaultProperty ("FileName")]
47 [DefaultEvent ("FileOk")]
48 public abstract class FileDialog : CommonDialog
50 protected static readonly object EventFileOk = new object ();
51 private static int MaxFileNameItems = 10;
53 internal enum FileDialogType
59 private bool addExtension = true;
60 private bool checkFileExists;
61 private bool checkPathExists = true;
62 private string defaultExt;
63 private bool dereferenceLinks = true;
64 private string[] fileNames;
65 private string filter = "";
66 private int filterIndex = 1;
67 private string initialDirectory;
68 private bool restoreDirectory;
69 private bool showHelp;
71 private bool validateNames = true;
72 private bool auto_upgrade_enable = true;
73 private FileDialogCustomPlacesCollection custom_places;
74 private bool supportMultiDottedExtensions;
75 private bool checkForIllegalChars = true;
76 private Button cancelButton;
77 private ToolBarButton upToolBarButton;
78 private PopupButtonPanel popupButtonPanel;
79 private Button openSaveButton;
80 private Button helpButton;
81 private Label fileTypeLabel;
82 private ToolBarButton menueToolBarButton;
83 private ContextMenu menueToolBarButtonContextMenu;
84 private ToolBar smallButtonToolBar;
85 private DirComboBox dirComboBox;
86 private ComboBox fileNameComboBox;
87 private Label fileNameLabel;
88 private MWFFileView mwfFileView;
89 private MwfFileViewItemComparer file_view_comparer;
90 private Label searchSaveLabel;
91 private ToolBarButton newdirToolBarButton;
92 private ToolBarButton backToolBarButton;
93 private ComboBox fileTypeComboBox;
94 private ImageList imageListTopToolbar;
95 private CheckBox readonlyCheckBox;
97 private bool multiSelect;
99 private string restoreDirectoryString = String.Empty;
101 internal FileDialogType fileDialogType;
103 private bool do_not_call_OnSelectedIndexChangedFileTypeComboBox;
105 private bool showReadOnly;
106 private bool readOnlyChecked;
107 internal bool createPrompt;
108 internal bool overwritePrompt = true;
110 private FileFilter fileFilter;
111 private string[] configFileNames = null;
112 private string lastFolder = String.Empty;
116 private const string filedialog_string = "FileDialog";
117 private const string lastfolder_string = "LastFolder";
118 private const string width_string = "Width";
119 private const string height_string = "Height";
120 private const string filenames_string = "FileNames";
121 private const string x_string = "X";
122 private const string y_string = "Y";
124 private readonly char [] wildcard_chars = new char [] { '*', '?' };
126 private bool disable_form_closed_event;
128 internal FileDialog ()
130 form = new DialogForm (this);
133 Size formConfigSize = Size.Empty;
134 Point formConfigLocation = Point.Empty;
136 object formWidth = MWFConfig.GetValue (filedialog_string, width_string);
138 object formHeight = MWFConfig.GetValue (filedialog_string, height_string);
140 if (formHeight != null && formWidth != null)
141 formConfigSize = new Size ((int)formWidth, (int)formHeight);
143 object formLocationX = MWFConfig.GetValue (filedialog_string, x_string);
144 object formLocationY = MWFConfig.GetValue (filedialog_string, y_string);
146 if (formLocationX != null && formLocationY != null)
147 formConfigLocation = new Point ((int)formLocationX, (int)formLocationY);
149 configFileNames = (string[])MWFConfig.GetValue (filedialog_string, filenames_string);
151 fileTypeComboBox = new ComboBox ();
152 backToolBarButton = new ToolBarButton ();
153 newdirToolBarButton = new ToolBarButton ();
154 searchSaveLabel = new Label ();
155 mwfFileView = new MWFFileView (vfs);
156 fileNameLabel = new Label ();
157 fileNameComboBox = new ComboBox ();
158 dirComboBox = new DirComboBox (vfs);
159 smallButtonToolBar = new ToolBar ();
160 menueToolBarButton = new ToolBarButton ();
161 fileTypeLabel = new Label ();
162 openSaveButton = new Button ();
163 helpButton = new Button ();
164 popupButtonPanel = new PopupButtonPanel ();
165 upToolBarButton = new ToolBarButton ();
166 cancelButton = new Button ();
167 form.CancelButton = cancelButton;
168 imageListTopToolbar = new ImageList ();
169 menueToolBarButtonContextMenu = new ContextMenu ();
170 readonlyCheckBox = new CheckBox ();
172 form.SuspendLayout ();
174 //imageListTopToolbar
175 imageListTopToolbar.ColorDepth = ColorDepth.Depth32Bit;
176 imageListTopToolbar.ImageSize = new Size (16, 16); // 16, 16
177 imageListTopToolbar.Images.Add (ResourceImageLoader.Get ("go-previous.png"));
178 imageListTopToolbar.Images.Add (ResourceImageLoader.Get ("go-top.png"));
179 imageListTopToolbar.Images.Add (ResourceImageLoader.Get ("folder-new.png"));
180 imageListTopToolbar.Images.Add (ResourceImageLoader.Get ("preferences-system-windows.png"));
181 imageListTopToolbar.TransparentColor = Color.Transparent;
184 searchSaveLabel.FlatStyle = FlatStyle.System;
185 searchSaveLabel.Location = new Point (6, 6);
186 searchSaveLabel.Size = new Size (86, 22);
187 searchSaveLabel.TextAlign = ContentAlignment.MiddleRight;
190 dirComboBox.Anchor = ((AnchorStyles)(((AnchorStyles.Top | AnchorStyles.Left) | AnchorStyles.Right)));
191 dirComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
192 dirComboBox.Location = new Point (99, 6);
193 dirComboBox.Size = new Size (261, 22);
194 dirComboBox.TabIndex = 7;
196 // smallButtonToolBar
197 smallButtonToolBar.Anchor = ((AnchorStyles)((AnchorStyles.Top | AnchorStyles.Right)));
198 smallButtonToolBar.Appearance = ToolBarAppearance.Flat;
199 smallButtonToolBar.AutoSize = false;
200 smallButtonToolBar.Buttons.AddRange (new ToolBarButton [] {
204 menueToolBarButton});
205 smallButtonToolBar.ButtonSize = new Size (24, 24); // 21, 16
206 smallButtonToolBar.Divider = false;
207 smallButtonToolBar.Dock = DockStyle.None;
208 smallButtonToolBar.DropDownArrows = true;
209 smallButtonToolBar.ImageList = imageListTopToolbar;
210 smallButtonToolBar.Location = new Point (372, 6);
211 smallButtonToolBar.ShowToolTips = true;
212 smallButtonToolBar.Size = new Size (140, 28);
213 smallButtonToolBar.TabIndex = 8;
214 smallButtonToolBar.TextAlign = ToolBarTextAlign.Right;
217 popupButtonPanel.Dock = DockStyle.None;
218 popupButtonPanel.Anchor = ((AnchorStyles)((((AnchorStyles.Top | AnchorStyles.Bottom) | AnchorStyles.Left))));
219 popupButtonPanel.Location = new Point (6, 35);
220 popupButtonPanel.Size = new Size (87, 338);
221 popupButtonPanel.TabIndex = 9;
224 mwfFileView.Anchor = ((AnchorStyles)((((AnchorStyles.Top | AnchorStyles.Bottom) | AnchorStyles.Left) | AnchorStyles.Right)));
225 mwfFileView.Location = new Point (99, 35);
226 mwfFileView.Size = new Size (450, 283);
227 mwfFileView.MultiSelect = false;
228 mwfFileView.TabIndex = 10;
229 mwfFileView.RegisterSender (dirComboBox);
230 mwfFileView.RegisterSender (popupButtonPanel);
233 fileNameLabel.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Left)));
234 fileNameLabel.FlatStyle = FlatStyle.System;
235 fileNameLabel.Location = new Point (101, 326);
236 fileNameLabel.Size = new Size (70, 21);
237 fileNameLabel.Text = "File name:";
238 fileNameLabel.TextAlign = ContentAlignment.MiddleLeft;
241 fileNameComboBox.Anchor = ((AnchorStyles)(((AnchorStyles.Bottom | AnchorStyles.Left) | AnchorStyles.Right)));
242 fileNameComboBox.Location = new Point (195, 326);
243 fileNameComboBox.Size = new Size (246, 22);
244 fileNameComboBox.TabIndex = 1;
245 fileNameComboBox.MaxDropDownItems = MaxFileNameItems;
246 fileNameComboBox.RestoreContextMenu ();
247 UpdateRecentFiles ();
250 fileTypeLabel.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Left)));
251 fileTypeLabel.FlatStyle = FlatStyle.System;
252 fileTypeLabel.Location = new Point (101, 355);
253 fileTypeLabel.Size = new Size (90, 21);
254 fileTypeLabel.Text = "Files of type:";
255 fileTypeLabel.TextAlign = ContentAlignment.MiddleLeft;
258 fileTypeComboBox.Anchor = ((AnchorStyles)(((AnchorStyles.Bottom | AnchorStyles.Left) | AnchorStyles.Right)));
259 fileTypeComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
260 fileTypeComboBox.Location = new Point (195, 355);
261 fileTypeComboBox.Size = new Size (246, 22);
262 fileTypeComboBox.TabIndex = 2;
265 backToolBarButton.ImageIndex = 0;
266 backToolBarButton.Enabled = false;
267 backToolBarButton.Style = ToolBarButtonStyle.PushButton;
268 mwfFileView.AddControlToEnableDisableByDirStack (backToolBarButton);
271 upToolBarButton.ImageIndex = 1;
272 upToolBarButton.Style = ToolBarButtonStyle.PushButton;
273 mwfFileView.SetFolderUpToolBarButton (upToolBarButton);
275 // newdirToolBarButton
276 newdirToolBarButton.ImageIndex = 2;
277 newdirToolBarButton.Style = ToolBarButtonStyle.PushButton;
279 // menueToolBarButton
280 menueToolBarButton.ImageIndex = 3;
281 menueToolBarButton.DropDownMenu = menueToolBarButtonContextMenu;
282 menueToolBarButton.Style = ToolBarButtonStyle.DropDownButton;
284 // menueToolBarButtonContextMenu
285 menueToolBarButtonContextMenu.MenuItems.AddRange (mwfFileView.ViewMenuItems);
288 openSaveButton.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
289 openSaveButton.FlatStyle = FlatStyle.System;
290 openSaveButton.Location = new Point (474, 326);
291 openSaveButton.Size = new Size (75, 23);
292 openSaveButton.TabIndex = 4;
293 openSaveButton.FlatStyle = FlatStyle.System;
296 cancelButton.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
297 cancelButton.FlatStyle = FlatStyle.System;
298 cancelButton.Location = new Point (474, 353);
299 cancelButton.Size = new Size (75, 23);
300 cancelButton.TabIndex = 5;
301 cancelButton.Text = "Cancel";
302 cancelButton.FlatStyle = FlatStyle.System;
305 helpButton.Anchor = ((AnchorStyles)((AnchorStyles.Bottom | AnchorStyles.Right)));
306 helpButton.FlatStyle = FlatStyle.System;
307 helpButton.Location = new Point (474, 353);
308 helpButton.Size = new Size (75, 23);
309 helpButton.TabIndex = 6;
310 helpButton.Text = "Help";
311 helpButton.FlatStyle = FlatStyle.System;
312 helpButton.Visible = false;
315 readonlyCheckBox.Anchor = ((AnchorStyles)(((AnchorStyles.Bottom | AnchorStyles.Left) | AnchorStyles.Right)));
316 readonlyCheckBox.Text = "Open Readonly";
317 readonlyCheckBox.Location = new Point (195, 350);
318 readonlyCheckBox.Size = new Size (245, 21);
319 readonlyCheckBox.TabIndex = 3;
320 readonlyCheckBox.FlatStyle = FlatStyle.System;
321 readonlyCheckBox.Visible = false;
323 form.SizeGripStyle = SizeGripStyle.Show;
324 form.AcceptButton = openSaveButton;
325 form.MaximizeBox = true;
326 form.MinimizeBox = true;
327 form.FormBorderStyle = FormBorderStyle.Sizable;
328 form.ClientSize = new Size (555, 385);
329 form.MinimumSize = form.Size;
331 form.Controls.Add (smallButtonToolBar);
332 form.Controls.Add (cancelButton);
333 form.Controls.Add (openSaveButton);
334 form.Controls.Add (mwfFileView);
335 form.Controls.Add (fileTypeLabel);
336 form.Controls.Add (fileNameLabel);
337 form.Controls.Add (fileTypeComboBox);
338 form.Controls.Add (fileNameComboBox);
339 form.Controls.Add (dirComboBox);
340 form.Controls.Add (searchSaveLabel);
341 form.Controls.Add (popupButtonPanel);
342 form.Controls.Add (helpButton);
343 form.Controls.Add (readonlyCheckBox);
345 form.ResumeLayout (true);
347 if (formConfigSize != Size.Empty) {
348 form.ClientSize = formConfigSize;
351 if (formConfigLocation != Point.Empty) {
352 form.Location = formConfigLocation;
355 openSaveButton.Click += new EventHandler (OnClickOpenSaveButton);
356 cancelButton.Click += new EventHandler (OnClickCancelButton);
357 helpButton.Click += new EventHandler (OnClickHelpButton);
359 smallButtonToolBar.ButtonClick += new ToolBarButtonClickEventHandler (OnClickSmallButtonToolBar);
361 fileTypeComboBox.SelectedIndexChanged += new EventHandler (OnSelectedIndexChangedFileTypeComboBox);
363 mwfFileView.SelectedFileChanged += new EventHandler (OnSelectedFileChangedFileView);
364 mwfFileView.ForceDialogEnd += new EventHandler (OnForceDialogEndFileView);
365 mwfFileView.SelectedFilesChanged += new EventHandler (OnSelectedFilesChangedFileView);
366 mwfFileView.ColumnClick += new ColumnClickEventHandler(OnColumnClickFileView);
368 dirComboBox.DirectoryChanged += new EventHandler (OnDirectoryChangedDirComboBox);
369 popupButtonPanel.DirectoryChanged += new EventHandler (OnDirectoryChangedPopupButtonPanel);
371 readonlyCheckBox.CheckedChanged += new EventHandler (OnCheckCheckChanged);
372 form.FormClosed += new FormClosedEventHandler (OnFileDialogFormClosed);
373 custom_places = new FileDialogCustomPlacesCollection ();
377 public bool AddExtension {
383 addExtension = value;
387 [MonoTODO ("Stub, value not respected")]
388 [DefaultValue (true)]
389 public bool AutoUpgradeEnabled {
390 get { return auto_upgrade_enable; }
391 set { auto_upgrade_enable = value; }
394 [DefaultValue(false)]
395 public virtual bool CheckFileExists {
397 return checkFileExists;
401 checkFileExists = value;
406 public bool CheckPathExists {
408 return checkPathExists;
412 checkPathExists = value;
416 [MonoTODO ("Stub, collection not used")]
418 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
419 public FileDialogCustomPlacesCollection CustomPlaces {
420 get { return custom_places; }
424 public string DefaultExt {
426 if (defaultExt == null)
431 if (value != null && value.Length > 0) {
432 // remove leading dot
433 if (value [0] == '.')
434 value = value.Substring (1);
440 // in MS.NET it doesn't make a difference if
441 // DerefenceLinks is true or false
442 // if the selected file is a link FileDialog
443 // always returns the link
445 public bool DereferenceLinks {
447 return dereferenceLinks;
451 dereferenceLinks = value;
456 public string FileName {
458 if (fileNames == null || fileNames.Length == 0)
461 if (fileNames [0].Length == 0)
464 // skip check for illegal characters if the filename was set
465 // through FileDialog API
466 if (!checkForIllegalChars)
467 return fileNames [0];
469 // ensure filename contains only valid characters
470 Path.GetFullPath (fileNames [0]);
471 // but return filename as is
472 return fileNames [0];
478 fileNames = new string [1] { value };
480 fileNames = new string [0];
483 // skip check for illegal characters if the filename was set
484 // through FileDialog API
485 checkForIllegalChars = false;
490 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
491 public string[] FileNames {
493 if (fileNames == null || fileNames.Length == 0) {
494 return new string [0];
497 string[] new_filenames = new string [fileNames.Length];
498 fileNames.CopyTo (new_filenames, 0);
500 // skip check for illegal characters if the filename was set
501 // through FileDialog API
502 if (!checkForIllegalChars)
503 return new_filenames;
505 foreach (string fileName in new_filenames) {
506 // ensure filename contains only valid characters
507 Path.GetFullPath (fileName);
509 return new_filenames;
515 public string Filter {
523 if (fileFilter != null)
524 fileFilter.FilterArrayList.Clear ();
526 if (FileFilter.CheckFilter (value)) {
529 fileFilter = new FileFilter (filter);
531 throw new ArgumentException ("The provided filter string"
532 + " is invalid. The filter string should contain a"
533 + " description of the filter, followed by the "
534 + " vertical bar (|) and the filter pattern. The"
535 + " strings for different filtering options should"
536 + " also be separated by the vertical bar. Example:"
537 + " Text files (*.txt)|*.txt|All files (*.*)|*.*");
545 public int FilterIndex {
555 public string InitialDirectory {
557 if (initialDirectory == null)
559 return initialDirectory;
562 initialDirectory = value;
566 [DefaultValue(false)]
567 public bool RestoreDirectory {
569 return restoreDirectory;
573 restoreDirectory = value;
577 [DefaultValue(false)]
578 public bool ShowHelp {
585 ResizeAndRelocateForHelpOrReadOnly ();
589 [DefaultValue(false)]
590 public bool SupportMultiDottedExtensions {
592 return supportMultiDottedExtensions;
596 supportMultiDottedExtensions = value;
602 public string Title {
613 // this one is a hard one ;)
615 // - up to MAX_PATH characters (windef.h) = 260
616 // - no trailing dots or spaces
619 // NTFS/Posix filename:
620 // - up to 32,768 Unicode characters
621 // - trailing periods or spaces
625 public bool ValidateNames {
627 return validateNames;
631 validateNames = value;
635 public override void Reset ()
638 checkFileExists = false;
639 checkPathExists = true;
641 dereferenceLinks = true;
643 Filter = String.Empty;
645 InitialDirectory = null;
646 restoreDirectory = false;
647 SupportMultiDottedExtensions = false;
650 validateNames = true;
655 public override string ToString ()
657 return String.Format("{0}: Title: {1}, FileName: {2}", base.ToString (), Title, FileName);
660 public event CancelEventHandler FileOk {
661 add { Events.AddHandler (EventFileOk, value); }
662 remove { Events.RemoveHandler (EventFileOk, value); }
665 protected virtual IntPtr Instance {
673 // This is just for internal use with MSs version, so it doesn't need to be implemented
674 // as it can't really be accessed anyways
675 protected int Options {
679 internal virtual string DialogTitle {
685 [MonoTODO ("Not implemented, will throw NotImplementedException")]
686 protected override IntPtr HookProc (IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam)
688 throw new NotImplementedException ();
691 protected void OnFileOk (CancelEventArgs e)
693 CancelEventHandler fo = (CancelEventHandler) Events [EventFileOk];
698 private void CleanupOnClose ()
700 mwfFileView.StopThumbnailCreation();
702 WriteConfigValues ();
704 Mime.CleanFileCache ();
706 disable_form_closed_event = true;
709 protected override bool RunDialog (IntPtr hWndOwner)
712 form.Text = DialogTitle;
714 // avoid using the FileNames property to skip the invalid characters
716 string fileName = null;
717 if (fileNames != null && fileNames.Length != 0)
718 fileName = fileNames [0];
720 fileName = string.Empty;
726 SetFileAndDirectory (fileName);
727 fileNameComboBox.Select ();
732 internal virtual bool ShowReadOnly {
734 showReadOnly = value;
735 ResizeAndRelocateForHelpOrReadOnly ();
743 internal virtual bool ReadOnlyChecked {
745 readOnlyChecked = value;
746 readonlyCheckBox.Checked = value;
750 return readOnlyChecked;
754 internal bool BMultiSelect {
757 mwfFileView.MultiSelect = value;
765 internal string OpenSaveButtonText {
767 openSaveButton.Text = value;
771 internal string SearchSaveLabel {
773 searchSaveLabel.Text = value;
777 internal string FileTypeLabel {
779 fileTypeLabel.Text = value;
783 internal string CustomFilter {
785 string fname = fileNameComboBox.Text;
786 if (fname.IndexOfAny (wildcard_chars) == -1)
793 private void SelectFilter ()
795 int filter_to_select = (filterIndex - 1);
797 if (mwfFileView.FilterArrayList == null || mwfFileView.FilterArrayList.Count == 0) {
798 filter_to_select = -1;
800 if (filter_to_select < 0 || filter_to_select >= mwfFileView.FilterArrayList.Count)
801 filter_to_select = 0;
804 do_not_call_OnSelectedIndexChangedFileTypeComboBox = true;
805 fileTypeComboBox.BeginUpdate ();
806 fileTypeComboBox.SelectedIndex = filter_to_select;
807 fileTypeComboBox.EndUpdate ();
808 do_not_call_OnSelectedIndexChangedFileTypeComboBox = false;
809 mwfFileView.FilterIndex = filter_to_select + 1;
812 private void SetFileAndDirectory (string fname)
814 if (fname.Length != 0) {
815 bool rooted = Path.IsPathRooted (fname);
817 mwfFileView.ChangeDirectory (null, lastFolder);
818 fileNameComboBox.Text = fname;
820 string dirname = Path.GetDirectoryName (fname);
821 if (dirname != null && dirname.Length > 0 && Directory.Exists (dirname)) {
822 fileNameComboBox.Text = Path.GetFileName (fname);
823 mwfFileView.ChangeDirectory (null, dirname);
825 fileNameComboBox.Text = fname;
826 mwfFileView.ChangeDirectory (null, lastFolder);
830 mwfFileView.ChangeDirectory (null, lastFolder);
831 fileNameComboBox.Text = null;
835 void OnClickOpenSaveButton (object sender, EventArgs e)
837 // for filenames typed or selected by user, enable check for
838 // illegal characters in filename(s)
839 checkForIllegalChars = true;
841 if (fileDialogType == FileDialogType.OpenFileDialog) {
842 ListView.SelectedListViewItemCollection sl = mwfFileView.SelectedItems;
843 if (sl.Count > 0 && sl [0] != null) {
845 FileViewListViewItem item = sl [0] as FileViewListViewItem;
846 FSEntry fsEntry = item.FSEntry;
848 if ((fsEntry.Attributes & FileAttributes.Directory) == FileAttributes.Directory) {
849 mwfFileView.ChangeDirectory (null, fsEntry.FullName, CustomFilter);
853 foreach (FileViewListViewItem item in sl) {
854 FSEntry fsEntry = item.FSEntry;
855 if ((fsEntry.Attributes & FileAttributes.Directory) == FileAttributes.Directory) {
856 mwfFileView.ChangeDirectory (null, fsEntry.FullName, CustomFilter);
864 // Custom filter, typed by the user, ignoring the stored filters
865 if (fileNameComboBox.Text.IndexOfAny (wildcard_chars) != -1) {
866 mwfFileView.UpdateFileView (fileNameComboBox.Text);
870 ArrayList files = new ArrayList ();
871 FileNamesTokenizer tokenizer = new FileNamesTokenizer (
872 fileNameComboBox.Text, multiSelect);
873 tokenizer.GetNextFile ();
874 while (tokenizer.CurrentToken != TokenType.EOF) {
875 string fileName = tokenizer.TokenText;
876 string internalfullfilename;
878 if (!Path.IsPathRooted (fileName)) {
879 // on unix currentRealFolder for "Recently used files" is null,
880 // because recently used files don't get saved as links in a directory
881 // recently used files get saved in a xml file
882 if (mwfFileView.CurrentRealFolder != null)
883 fileName = Path.Combine (mwfFileView.CurrentRealFolder, fileName);
885 if (mwfFileView.CurrentFSEntry != null) {
886 fileName = mwfFileView.CurrentFSEntry.FullName;
890 FileInfo fileInfo = new FileInfo (fileName);
892 if (fileInfo.Exists || fileDialogType == FileDialogType.SaveFileDialog) {
893 internalfullfilename = fileName;
895 DirectoryInfo dirInfo = new DirectoryInfo (fileName);
896 if (dirInfo.Exists) {
897 mwfFileView.ChangeDirectory (null, dirInfo.FullName, CustomFilter);
898 fileNameComboBox.Text = null;
901 internalfullfilename = fileName;
906 string current_extension = Path.GetExtension (fileName);
907 if (current_extension.Length == 0) {
908 string filter_extension = string.Empty;
910 if (AddFilterExtension (internalfullfilename))
911 filter_extension = GetExtension (internalfullfilename);
913 if (filter_extension.Length == 0 && DefaultExt.Length > 0) {
914 filter_extension = "." + DefaultExt;
916 if (checkFileExists) {
917 // ignore DefaultExt if file not exist
918 if (!File.Exists (internalfullfilename + filter_extension))
919 filter_extension = string.Empty;
923 internalfullfilename += filter_extension;
927 if (checkFileExists) {
928 if (!File.Exists (internalfullfilename)) {
929 string message = "\"" + internalfullfilename + "\" does not exist. Please verify that you have entered the correct file name.";
930 MessageBox.Show (message, openSaveButton.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
935 if (fileDialogType == FileDialogType.SaveFileDialog) {
936 if (overwritePrompt) {
937 if (File.Exists (internalfullfilename)) {
938 string message = "\"" + internalfullfilename + "\" already exists. Do you want to overwrite it?";
939 DialogResult dr = MessageBox.Show (message, openSaveButton.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
940 if (dr == DialogResult.Cancel)
946 if (!File.Exists (internalfullfilename)) {
947 string message = "\"" + internalfullfilename + "\" does not exist. Do you want to create it?";
948 DialogResult dr = MessageBox.Show (message, openSaveButton.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
949 if (dr == DialogResult.Cancel)
955 files.Add (internalfullfilename);
956 tokenizer.GetNextFile ();
959 if (files.Count > 0) {
960 fileNames = new string [files.Count];
961 for (int i = 0; i < files.Count; i++) {
962 string fileName = (string) files [i];
963 fileNames [i] = fileName;
964 mwfFileView.WriteRecentlyUsed (fileName);
966 if (!File.Exists (fileName))
967 // ignore files that do not exist
970 if (fileNameComboBox.Items.IndexOf (fileName) == -1)
971 fileNameComboBox.Items.Insert (0, fileName);
974 // remove items above the maximum items that we want to display
975 while (fileNameComboBox.Items.Count > MaxFileNameItems)
976 fileNameComboBox.Items.RemoveAt (MaxFileNameItems);
978 // If a directory is selected, navigate into it
979 foreach (FileViewListViewItem item in mwfFileView.SelectedItems) {
980 FSEntry fsEntry = item.FSEntry;
982 if ((fsEntry.Attributes & FileAttributes.Directory) == FileAttributes.Directory) {
983 mwfFileView.ChangeDirectory (null, fsEntry.FullName, CustomFilter);
991 if (checkPathExists && mwfFileView.CurrentRealFolder != null) {
992 if (!Directory.Exists (mwfFileView.CurrentRealFolder)) {
993 string message = "\"" + mwfFileView.CurrentRealFolder + "\" does not exist. Please verify that you have entered the correct directory name.";
994 MessageBox.Show (message, openSaveButton.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
996 if (InitialDirectory.Length == 0 || !Directory.Exists (InitialDirectory))
997 mwfFileView.ChangeDirectory (null, lastFolder, CustomFilter);
999 mwfFileView.ChangeDirectory (null, InitialDirectory, CustomFilter);
1004 if (restoreDirectory) {
1005 lastFolder = restoreDirectoryString;
1007 lastFolder = mwfFileView.CurrentFolder;
1010 // update value of FilterIndex with user-selected filter
1011 filterIndex = fileTypeComboBox.SelectedIndex + 1;
1013 CancelEventArgs cancelEventArgs = new CancelEventArgs ();
1015 OnFileOk (cancelEventArgs);
1017 if (cancelEventArgs.Cancel)
1021 form.DialogResult = DialogResult.OK;
1024 bool AddFilterExtension (string fileName)
1026 if (fileDialogType == FileDialogType.OpenFileDialog) {
1027 if (DefaultExt.Length == 0)
1030 if (checkFileExists) {
1031 // if CheckFileExists is true, only add filter extension if
1032 // file with DefaultExt does not exist
1033 string fullFileName = fileName + "." + DefaultExt;
1034 return !File.Exists (fullFileName);
1036 // if CheckFileExists is false, only add filter extension
1037 // if specified file does not exist
1038 return !File.Exists (fileName);
1045 string GetExtension (string fileName)
1047 string filter_extension = String.Empty;
1049 if (fileFilter == null || fileTypeComboBox.SelectedIndex == -1)
1050 return filter_extension;
1052 FilterStruct filterstruct = (FilterStruct) fileFilter.FilterArrayList
1053 [fileTypeComboBox.SelectedIndex];
1055 for (int i = 0; i < filterstruct.filters.Count; i++) {
1056 string extension = filterstruct.filters [i];
1058 if (extension.StartsWith ("*"))
1059 extension = extension.Remove (0, 1);
1061 if (extension.IndexOf ('*') != -1)
1064 if (!supportMultiDottedExtensions) {
1065 int lastdot = extension.LastIndexOf('.');
1067 if (extension.LastIndexOf('.', lastdot - 1) != -1) {
1068 extension = extension.Remove(0, lastdot);
1073 if (!checkFileExists) {
1074 filter_extension = extension;
1078 if (fileDialogType == FileDialogType.SaveFileDialog) {
1079 // when DefaultExt is set, only consider first filter
1080 // extension (and do not check if file exists)
1081 if (DefaultExt.Length > 0) {
1082 filter_extension = extension;
1087 // MSDN: If the CheckFileExists property is true,
1088 // the dialog box adds the first extension from the
1089 // current file filter that matches an existing file
1090 string fullfilename = fileName + extension;
1091 if (File.Exists (fullfilename)) {
1092 filter_extension = extension;
1095 if (fileDialogType == FileDialogType.SaveFileDialog) {
1096 // when DefaultExt is set, only consider first filter
1098 if (DefaultExt.Length > 0) {
1099 filter_extension = extension;
1106 return filter_extension;
1109 void OnClickCancelButton (object sender, EventArgs e)
1111 if (restoreDirectory)
1112 mwfFileView.CurrentFolder = restoreDirectoryString;
1116 form.DialogResult = DialogResult.Cancel;
1119 void OnClickHelpButton (object sender, EventArgs e)
1124 void OnClickSmallButtonToolBar (object sender, ToolBarButtonClickEventArgs e)
1126 if (e.Button == upToolBarButton) {
1127 mwfFileView.OneDirUp (CustomFilter);
1129 if (e.Button == backToolBarButton) {
1130 mwfFileView.PopDir (CustomFilter);
1132 if (e.Button == newdirToolBarButton) {
1133 mwfFileView.CreateNewFolder ();
1137 void OnSelectedIndexChangedFileTypeComboBox (object sender, EventArgs e)
1139 if (do_not_call_OnSelectedIndexChangedFileTypeComboBox) {
1140 do_not_call_OnSelectedIndexChangedFileTypeComboBox = false;
1144 UpdateRecentFiles ();
1146 mwfFileView.FilterIndex = fileTypeComboBox.SelectedIndex + 1;
1149 void OnSelectedFileChangedFileView (object sender, EventArgs e)
1151 fileNameComboBox.Text = mwfFileView.CurrentFSEntry.Name;
1154 void OnSelectedFilesChangedFileView (object sender, EventArgs e)
1156 string selectedFiles = mwfFileView.SelectedFilesString;
1157 if (selectedFiles != null && selectedFiles.Length != 0)
1158 fileNameComboBox.Text = selectedFiles;
1161 void OnForceDialogEndFileView (object sender, EventArgs e)
1163 OnClickOpenSaveButton (this, EventArgs.Empty);
1166 void OnDirectoryChangedDirComboBox (object sender, EventArgs e)
1168 mwfFileView.ChangeDirectory (sender, dirComboBox.CurrentFolder, CustomFilter);
1171 void OnDirectoryChangedPopupButtonPanel (object sender, EventArgs e)
1173 mwfFileView.ChangeDirectory (sender, popupButtonPanel.CurrentFolder, CustomFilter);
1176 void OnCheckCheckChanged (object sender, EventArgs e)
1178 ReadOnlyChecked = readonlyCheckBox.Checked;
1181 void OnFileDialogFormClosed (object sender, FormClosedEventArgs e)
1183 HandleFormClosedEvent (sender);
1186 private void OnColumnClickFileView (object sender, ColumnClickEventArgs e)
1188 if (file_view_comparer == null)
1189 file_view_comparer = new MwfFileViewItemComparer (true);
1191 file_view_comparer.ColumnIndex = e.Column;
1192 file_view_comparer.Ascendent = !file_view_comparer.Ascendent;
1194 if (mwfFileView.ListViewItemSorter == null)
1195 mwfFileView.ListViewItemSorter = file_view_comparer;
1197 mwfFileView.Sort ();
1200 void HandleFormClosedEvent (object sender)
1202 if (!disable_form_closed_event)
1203 OnClickCancelButton (sender, EventArgs.Empty);
1205 disable_form_closed_event = false;
1208 private void UpdateFilters ()
1210 if (fileFilter == null)
1211 fileFilter = new FileFilter ();
1213 ArrayList filters = fileFilter.FilterArrayList;
1215 fileTypeComboBox.BeginUpdate ();
1217 fileTypeComboBox.Items.Clear ();
1219 foreach (FilterStruct fs in filters) {
1220 fileTypeComboBox.Items.Add (fs.filterName);
1223 fileTypeComboBox.EndUpdate ();
1225 mwfFileView.FilterArrayList = filters;
1228 private void UpdateRecentFiles ()
1230 fileNameComboBox.Items.Clear ();
1231 if (configFileNames != null) {
1232 foreach (string configFileName in configFileNames) {
1233 if (configFileName == null || configFileName.Trim ().Length == 0)
1235 // add no more than 10 items
1236 if (fileNameComboBox.Items.Count >= MaxFileNameItems)
1238 fileNameComboBox.Items.Add (configFileName);
1243 private void ResizeAndRelocateForHelpOrReadOnly ()
1245 form.SuspendLayout ();
1247 int fx = form.Size.Width - form.MinimumSize.Width;
1248 int fy = form.Size.Height - form.MinimumSize.Height;
1250 if (!ShowHelp && !ShowReadOnly)
1253 mwfFileView.Size = new Size (450 + fx, 254 + fy);
1254 fileNameLabel.Location = new Point (101, 298 + fy);
1255 fileNameComboBox.Location = new Point (195, 298 + fy);
1256 fileTypeLabel.Location = new Point (101, 326 + fy);
1257 fileTypeComboBox.Location = new Point (195, 326 + fy);
1258 openSaveButton.Location = new Point (474 + fx, 298 + fy);
1259 cancelButton.Location = new Point (474 + fx, 324 + fy);
1260 helpButton.Location = new Point (474 + fx, 353 + fy);
1261 readonlyCheckBox.Location = new Point (195, 350 + fy);
1263 helpButton.Visible = ShowHelp;
1264 readonlyCheckBox.Visible = ShowReadOnly;
1266 form.ResumeLayout ();
1269 private void WriteConfigValues ()
1271 MWFConfig.SetValue (filedialog_string, width_string, form.ClientSize.Width);
1272 MWFConfig.SetValue (filedialog_string, height_string, form.ClientSize.Height);
1273 MWFConfig.SetValue (filedialog_string, x_string, form.Location.X);
1274 MWFConfig.SetValue (filedialog_string, y_string, form.Location.Y);
1276 MWFConfig.SetValue (filedialog_string, lastfolder_string, lastFolder);
1278 string[] fileNameCBItems = new string [fileNameComboBox.Items.Count];
1280 fileNameComboBox.Items.CopyTo (fileNameCBItems, 0);
1282 MWFConfig.SetValue (filedialog_string, filenames_string, fileNameCBItems);
1285 private void ReadConfigValues ()
1287 lastFolder = (string)MWFConfig.GetValue (filedialog_string, lastfolder_string);
1289 if (lastFolder != null && lastFolder.IndexOf ("://") == -1) {
1290 if (!Directory.Exists (lastFolder)) {
1291 lastFolder = MWFVFS.DesktopPrefix;
1295 if (InitialDirectory.Length > 0 && Directory.Exists (InitialDirectory))
1296 lastFolder = InitialDirectory;
1298 if (lastFolder == null || lastFolder.Length == 0)
1299 lastFolder = Environment.CurrentDirectory;
1301 if (RestoreDirectory)
1302 restoreDirectoryString = lastFolder;
1305 class FileNamesTokenizer
1307 public FileNamesTokenizer (string text, bool allowMultiple)
1311 _tokenType = TokenType.BOF;
1312 _allowMultiple = allowMultiple;
1315 public TokenType CurrentToken {
1316 get { return _tokenType; }
1319 public string TokenText {
1320 get { return _tokenText; }
1323 public bool AllowMultiple {
1324 get { return _allowMultiple; }
1327 private int ReadChar ()
1329 if (_position < _text.Length) {
1330 return _text [_position++];
1336 private int PeekChar ()
1338 if (_position < _text.Length) {
1339 return _text [_position];
1345 private void SkipWhitespaceAndQuotes ()
1349 while ((ch = PeekChar ()) != -1) {
1350 if ((char) ch != '"' && !char.IsWhiteSpace ((char) ch))
1356 public void GetNextFile ()
1358 if (_tokenType == TokenType.EOF)
1359 throw new Exception ("");
1363 SkipWhitespaceAndQuotes ();
1365 if (PeekChar () == -1) {
1366 _tokenType = TokenType.EOF;
1370 _tokenType = TokenType.FileName;
1371 StringBuilder sb = new StringBuilder ();
1373 while ((ch = PeekChar ()) != -1) {
1374 if ((char) ch == '"') {
1378 int pos = _position;
1380 SkipWhitespaceAndQuotes ();
1381 if (PeekChar () == -1) {
1385 sb.Append ((char) ch);
1387 sb.Append ((char) ReadChar ());
1391 _tokenText = sb.ToString ();
1394 private readonly bool _allowMultiple;
1395 private int _position;
1396 private readonly string _text;
1397 private TokenType _tokenType;
1398 private string _tokenText;
1401 internal enum TokenType
1410 #region PopupButtonPanel
1411 internal class PopupButtonPanel : Control, IUpdateFolder
1414 internal class PopupButton : Control
1416 internal enum PopupButtonState
1419 private Image image = null;
1420 private PopupButtonState popupButtonState = PopupButtonState.Normal;
1421 private StringFormat text_format = new StringFormat();
1422 private Rectangle text_rect = Rectangle.Empty;
1424 public PopupButton ()
1426 text_format.Alignment = StringAlignment.Center;
1427 text_format.LineAlignment = StringAlignment.Near;
1429 SetStyle (ControlStyles.DoubleBuffer, true);
1430 SetStyle (ControlStyles.AllPaintingInWmPaint, true);
1431 SetStyle (ControlStyles.UserPaint, true);
1432 SetStyle (ControlStyles.Selectable, false);
1435 public Image Image {
1446 public PopupButtonState ButtonState {
1448 popupButtonState = value;
1453 return popupButtonState;
1457 #region UIA Framework Members
1458 internal void PerformClick ()
1460 OnClick (EventArgs.Empty);
1464 protected override void OnPaint (PaintEventArgs pe)
1471 private void Draw (PaintEventArgs pe)
1473 Graphics gr = pe.Graphics;
1475 gr.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (BackColor), ClientRectangle);
1478 if (image != null) {
1479 int i_x = (ClientSize.Width - image.Width) / 2;
1481 gr.DrawImage (image, i_x, i_y);
1484 if (Text != String.Empty) {
1485 if (text_rect == Rectangle.Empty)
1486 text_rect = new Rectangle (0, Height - 30, Width, Height - 30);
1488 gr.DrawString (Text, Font, Brushes.White, text_rect, text_format);
1491 switch (popupButtonState) {
1492 case PopupButtonState.Up:
1493 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.White), 0, 0, ClientSize.Width - 1, 0);
1494 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.White), 0, 0, 0, ClientSize.Height - 1);
1495 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.Black), ClientSize.Width - 1, 0, ClientSize.Width - 1, ClientSize.Height - 1);
1496 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.Black), 0, ClientSize.Height - 1, ClientSize.Width - 1, ClientSize.Height - 1);
1499 case PopupButtonState.Down:
1500 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.Black), 0, 0, ClientSize.Width - 1, 0);
1501 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.Black), 0, 0, 0, ClientSize.Height - 1);
1502 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.White), ClientSize.Width - 1, 0, ClientSize.Width - 1, ClientSize.Height - 1);
1503 gr.DrawLine (ThemeEngine.Current.ResPool.GetPen (Color.White), 0, ClientSize.Height - 1, ClientSize.Width - 1, ClientSize.Height - 1);
1508 protected override void OnMouseEnter (EventArgs e)
1510 if (popupButtonState != PopupButtonState.Down)
1511 popupButtonState = PopupButtonState.Up;
1513 PopupButtonPanel panel = Parent as PopupButtonPanel;
1515 if (panel.focusButton != null && panel.focusButton.ButtonState == PopupButtonState.Up) {
1516 panel.focusButton.ButtonState = PopupButtonState.Normal;
1517 panel.SetFocusButton (null);
1520 base.OnMouseEnter (e);
1523 protected override void OnMouseLeave (EventArgs e)
1525 if (popupButtonState == PopupButtonState.Up)
1526 popupButtonState = PopupButtonState.Normal;
1528 base.OnMouseLeave (e);
1531 protected override void OnClick (EventArgs e)
1533 popupButtonState = PopupButtonState.Down;
1540 private PopupButton recentlyusedButton;
1541 private PopupButton desktopButton;
1542 private PopupButton personalButton;
1543 private PopupButton mycomputerButton;
1544 private PopupButton networkButton;
1546 private PopupButton lastPopupButton = null;
1547 private PopupButton focusButton = null;
1549 private string currentPath;
1551 private int currentFocusIndex;
1553 public PopupButtonPanel ()
1557 BackColor = Color.FromArgb (128, 128, 128);
1558 Size = new Size (85, 336);
1559 InternalBorderStyle = BorderStyle.Fixed3D;
1561 recentlyusedButton = new PopupButton ();
1562 desktopButton = new PopupButton ();
1563 personalButton = new PopupButton ();
1564 mycomputerButton = new PopupButton ();
1565 networkButton = new PopupButton ();
1567 recentlyusedButton.Size = new Size (81, 64);
1568 recentlyusedButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesRecentDocuments, 32);
1569 recentlyusedButton.BackColor = BackColor;
1570 recentlyusedButton.ForeColor = Color.Black;
1571 recentlyusedButton.Location = new Point (2, 2);
1572 recentlyusedButton.Text = "Recently\nused";
1573 recentlyusedButton.Click += new EventHandler (OnClickButton);
1575 desktopButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesDesktop, 32);
1576 desktopButton.BackColor = BackColor;
1577 desktopButton.ForeColor = Color.Black;
1578 desktopButton.Size = new Size (81, 64);
1579 desktopButton.Location = new Point (2, 66);
1580 desktopButton.Text = "Desktop";
1581 desktopButton.Click += new EventHandler (OnClickButton);
1583 personalButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesPersonal, 32);
1584 personalButton.BackColor = BackColor;
1585 personalButton.ForeColor = Color.Black;
1586 personalButton.Size = new Size (81, 64);
1587 personalButton.Location = new Point (2, 130);
1588 personalButton.Text = "Personal";
1589 personalButton.Click += new EventHandler (OnClickButton);
1591 mycomputerButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesMyComputer, 32);
1592 mycomputerButton.BackColor = BackColor;
1593 mycomputerButton.ForeColor = Color.Black;
1594 mycomputerButton.Size = new Size (81, 64);
1595 mycomputerButton.Location = new Point (2, 194);
1596 mycomputerButton.Text = "My Computer";
1597 mycomputerButton.Click += new EventHandler (OnClickButton);
1599 networkButton.Image = ThemeEngine.Current.Images (UIIcon.PlacesMyNetwork, 32);
1600 networkButton.BackColor = BackColor;
1601 networkButton.ForeColor = Color.Black;
1602 networkButton.Size = new Size (81, 64);
1603 networkButton.Location = new Point (2, 258);
1604 networkButton.Text = "My Network";
1605 networkButton.Click += new EventHandler (OnClickButton);
1607 Controls.Add (recentlyusedButton);
1608 Controls.Add (desktopButton);
1609 Controls.Add (personalButton);
1610 Controls.Add (mycomputerButton);
1611 Controls.Add (networkButton);
1613 ResumeLayout (false);
1615 KeyDown += new KeyEventHandler (Key_Down);
1617 SetStyle (ControlStyles.StandardClick, false);
1620 void OnClickButton (object sender, EventArgs e)
1622 if (lastPopupButton != null && lastPopupButton != sender as PopupButton)
1623 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1624 lastPopupButton = sender as PopupButton;
1626 if (sender == recentlyusedButton) {
1627 currentPath = MWFVFS.RecentlyUsedPrefix;
1629 if (sender == desktopButton) {
1630 currentPath = MWFVFS.DesktopPrefix;
1632 if (sender == personalButton) {
1633 currentPath = MWFVFS.PersonalPrefix;
1635 if (sender == mycomputerButton) {
1636 currentPath = MWFVFS.MyComputerPrefix;
1638 if (sender == networkButton) {
1639 currentPath = MWFVFS.MyNetworkPrefix;
1642 EventHandler eh = (EventHandler)(Events [PDirectoryChangedEvent]);
1644 eh (this, EventArgs.Empty);
1647 static object UIAFocusedItemChangedEvent = new object ();
1649 internal event EventHandler UIAFocusedItemChanged {
1650 add { Events.AddHandler (UIAFocusedItemChangedEvent, value); }
1651 remove { Events.RemoveHandler (UIAFocusedItemChangedEvent, value); }
1654 internal void OnUIAFocusedItemChanged ()
1656 EventHandler eh = (EventHandler) Events [UIAFocusedItemChangedEvent];
1658 eh (this, EventArgs.Empty);
1661 internal PopupButton UIAFocusButton {
1667 public string CurrentFolder {
1669 string currentPath = value;
1670 if (currentPath == MWFVFS.RecentlyUsedPrefix) {
1671 if (lastPopupButton != recentlyusedButton) {
1672 if (lastPopupButton != null)
1673 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1674 recentlyusedButton.ButtonState = PopupButton.PopupButtonState.Down;
1675 lastPopupButton = recentlyusedButton;
1678 if (currentPath == MWFVFS.DesktopPrefix) {
1679 if (lastPopupButton != desktopButton) {
1680 if (lastPopupButton != null)
1681 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1682 desktopButton.ButtonState = PopupButton.PopupButtonState.Down;
1683 lastPopupButton = desktopButton;
1686 if (currentPath == MWFVFS.PersonalPrefix) {
1687 if (lastPopupButton != personalButton) {
1688 if (lastPopupButton != null)
1689 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1690 personalButton.ButtonState = PopupButton.PopupButtonState.Down;
1691 lastPopupButton = personalButton;
1694 if (currentPath == MWFVFS.MyComputerPrefix) {
1695 if (lastPopupButton != mycomputerButton) {
1696 if (lastPopupButton != null)
1697 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1698 mycomputerButton.ButtonState = PopupButton.PopupButtonState.Down;
1699 lastPopupButton = mycomputerButton;
1702 if (currentPath == MWFVFS.MyNetworkPrefix) {
1703 if (lastPopupButton != networkButton) {
1704 if (lastPopupButton != null)
1705 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1706 networkButton.ButtonState = PopupButton.PopupButtonState.Down;
1707 lastPopupButton = networkButton;
1710 if (lastPopupButton != null) {
1711 lastPopupButton.ButtonState = PopupButton.PopupButtonState.Normal;
1712 lastPopupButton = null;
1721 protected override void OnGotFocus (EventArgs e)
1723 if (lastPopupButton != recentlyusedButton) {
1724 recentlyusedButton.ButtonState = PopupButton.PopupButtonState.Up;
1725 SetFocusButton (recentlyusedButton);
1727 currentFocusIndex = 0;
1729 base.OnGotFocus (e);
1732 protected override void OnLostFocus (EventArgs e)
1734 if (focusButton != null && focusButton.ButtonState != PopupButton.PopupButtonState.Down)
1735 focusButton.ButtonState = PopupButton.PopupButtonState.Normal;
1736 base.OnLostFocus (e);
1739 protected override bool IsInputKey (Keys key)
1749 return base.IsInputKey (key);
1752 private void Key_Down (object sender, KeyEventArgs e)
1754 bool update_focus = false;
1756 if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Up) {
1757 currentFocusIndex --;
1759 if (currentFocusIndex < 0)
1760 currentFocusIndex = Controls.Count - 1;
1762 update_focus = true;
1764 if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Right) {
1765 currentFocusIndex++;
1767 if (currentFocusIndex == Controls.Count)
1768 currentFocusIndex = 0;
1770 update_focus = true;
1772 if (e.KeyCode == Keys.Enter) {
1773 focusButton.ButtonState = PopupButton.PopupButtonState.Down;
1774 OnClickButton (focusButton, EventArgs.Empty);
1778 PopupButton newfocusButton = Controls [currentFocusIndex] as PopupButton;
1779 if (focusButton != null && focusButton.ButtonState != PopupButton.PopupButtonState.Down)
1780 focusButton.ButtonState = PopupButton.PopupButtonState.Normal;
1781 if (newfocusButton.ButtonState != PopupButton.PopupButtonState.Down)
1782 newfocusButton.ButtonState = PopupButton.PopupButtonState.Up;
1783 SetFocusButton (newfocusButton);
1789 static object PDirectoryChangedEvent = new object ();
1791 public event EventHandler DirectoryChanged {
1792 add { Events.AddHandler (PDirectoryChangedEvent, value); }
1793 remove { Events.RemoveHandler (PDirectoryChangedEvent, value); }
1796 internal void SetFocusButton (PopupButton button)
1798 if (button == focusButton)
1801 focusButton = button;
1802 OnUIAFocusedItemChanged ();
1808 internal class DirComboBox : ComboBox, IUpdateFolder
1810 #region DirComboBoxItem
1811 internal class DirComboBoxItem
1813 private int imageIndex;
1814 private string name;
1815 private string path;
1817 private ImageList imageList;
1819 public DirComboBoxItem (ImageList imageList, int imageIndex, string name, string path, int xPos)
1821 this.imageList = imageList;
1822 this.imageIndex = imageIndex;
1828 public int ImageIndex {
1834 public string Name {
1840 public string Path {
1852 public ImageList ImageList {
1861 #region UIA Framework Members
1862 public override string ToString ()
1870 private ImageList imageList = new ImageList();
1872 private string currentPath;
1874 private Stack folderStack = new Stack();
1876 private static readonly int indent = 6;
1878 private DirComboBoxItem recentlyUsedDirComboboxItem;
1879 private DirComboBoxItem desktopDirComboboxItem;
1880 private DirComboBoxItem personalDirComboboxItem;
1881 private DirComboBoxItem myComputerDirComboboxItem;
1882 private DirComboBoxItem networkDirComboboxItem;
1884 private ArrayList myComputerItems = new ArrayList ();
1886 private DirComboBoxItem mainParentDirComboBoxItem = null;
1887 private DirComboBoxItem real_parent = null;
1891 public DirComboBox (MWFVFS vfs)
1897 DrawMode = DrawMode.OwnerDrawFixed;
1899 imageList.ColorDepth = ColorDepth.Depth32Bit;
1900 imageList.ImageSize = new Size (16, 16);
1901 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesRecentDocuments, 16));
1902 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesDesktop, 16));
1903 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesPersonal, 16));
1904 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesMyComputer, 16));
1905 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.PlacesMyNetwork, 16));
1906 imageList.Images.Add (ThemeEngine.Current.Images (UIIcon.NormalFolder, 16));
1907 imageList.TransparentColor = Color.Transparent;
1909 recentlyUsedDirComboboxItem = new DirComboBoxItem (imageList, 0, "Recently used", MWFVFS.RecentlyUsedPrefix, 0);
1910 desktopDirComboboxItem = new DirComboBoxItem (imageList, 1, "Desktop", MWFVFS.DesktopPrefix, 0);
1911 personalDirComboboxItem = new DirComboBoxItem (imageList, 2, "Personal folder", MWFVFS.PersonalPrefix, indent);
1912 myComputerDirComboboxItem = new DirComboBoxItem (imageList, 3, "My Computer", MWFVFS.MyComputerPrefix, indent);
1913 networkDirComboboxItem = new DirComboBoxItem (imageList, 4, "My Network", MWFVFS.MyNetworkPrefix, indent);
1915 ArrayList al = this.vfs.GetMyComputerContent ();
1917 foreach (FSEntry fsEntry in al) {
1918 myComputerItems.Add (new DirComboBoxItem (MimeIconEngine.LargeIcons, fsEntry.IconIndex, fsEntry.Name, fsEntry.FullName, indent * 2));
1924 mainParentDirComboBoxItem = myComputerDirComboboxItem;
1926 ResumeLayout (false);
1929 public string CurrentFolder {
1931 currentPath = value;
1940 private void CreateComboList ()
1943 DirComboBoxItem selection = null;
1945 if (currentPath == MWFVFS.RecentlyUsedPrefix) {
1946 mainParentDirComboBoxItem = recentlyUsedDirComboboxItem;
1947 selection = recentlyUsedDirComboboxItem;
1948 } else if (currentPath == MWFVFS.DesktopPrefix) {
1949 selection = desktopDirComboboxItem;
1950 mainParentDirComboBoxItem = desktopDirComboboxItem;
1951 } else if (currentPath == MWFVFS.PersonalPrefix) {
1952 selection = personalDirComboboxItem;
1953 mainParentDirComboBoxItem = personalDirComboboxItem;
1954 } else if (currentPath == MWFVFS.MyComputerPrefix) {
1955 selection = myComputerDirComboboxItem;
1956 mainParentDirComboBoxItem = myComputerDirComboboxItem;
1957 } else if (currentPath == MWFVFS.MyNetworkPrefix) {
1958 selection = networkDirComboboxItem;
1959 mainParentDirComboBoxItem = networkDirComboboxItem;
1961 foreach (DirComboBoxItem dci in myComputerItems) {
1962 if (dci.Path == currentPath) {
1963 mainParentDirComboBoxItem = selection = dci;
1973 Items.Add (recentlyUsedDirComboboxItem);
1974 Items.Add (desktopDirComboboxItem);
1975 Items.Add (personalDirComboboxItem);
1976 Items.Add (myComputerDirComboboxItem);
1977 Items.AddRange (myComputerItems);
1978 Items.Add (networkDirComboboxItem);
1980 if (selection == null)
1981 real_parent = CreateFolderStack ();
1983 if (real_parent != null) {
1984 int local_indent = 0;
1986 if (real_parent == desktopDirComboboxItem)
1989 if (real_parent == personalDirComboboxItem || real_parent == networkDirComboboxItem)
1994 selection = AppendToParent (local_indent, real_parent);
1999 if (selection != null)
2000 SelectedItem = selection;
2003 private DirComboBoxItem CreateFolderStack ()
2005 folderStack.Clear ();
2007 DirectoryInfo di = new DirectoryInfo (currentPath);
2009 folderStack.Push (di);
2011 bool ignoreCase = !XplatUI.RunningOnUnix;
2013 while (di.Parent != null) {
2016 if (mainParentDirComboBoxItem != personalDirComboboxItem && string.Compare (di.FullName, ThemeEngine.Current.Places (UIIcon.PlacesDesktop), ignoreCase) == 0)
2017 return desktopDirComboboxItem;
2019 if (mainParentDirComboBoxItem == personalDirComboboxItem) {
2020 if (string.Compare (di.FullName, ThemeEngine.Current.Places (UIIcon.PlacesPersonal), ignoreCase) == 0)
2021 return personalDirComboboxItem;
2023 foreach (DirComboBoxItem dci in myComputerItems) {
2024 if (string.Compare (dci.Path, di.FullName, ignoreCase) == 0) {
2029 folderStack.Push (di);
2035 private DirComboBoxItem AppendToParent (int nr_indents, DirComboBoxItem parentDirComboBoxItem)
2037 DirComboBoxItem selection = null;
2039 int index = Items.IndexOf (parentDirComboBoxItem) + 1;
2041 int xPos = indent * nr_indents;
2043 while (folderStack.Count != 0) {
2044 DirectoryInfo dii = folderStack.Pop () as DirectoryInfo;
2046 DirComboBoxItem dci = new DirComboBoxItem (imageList, 5, dii.Name, dii.FullName, xPos);
2048 Items.Insert (index, dci);
2057 protected override void OnDrawItem (DrawItemEventArgs e)
2062 DirComboBoxItem dcbi = Items [e.Index] as DirComboBoxItem;
2064 Bitmap bmp = new Bitmap (e.Bounds.Width, e.Bounds.Height, e.Graphics);
2065 Graphics gr = Graphics.FromImage (bmp);
2067 Color backColor = e.BackColor;
2068 Color foreColor = e.ForeColor;
2070 int xPos = dcbi.XPos;
2072 if ((e.State & DrawItemState.ComboBoxEdit) != 0)
2075 gr.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (backColor),
2076 new Rectangle (0, 0, bmp.Width, bmp.Height));
2078 if ((e.State & DrawItemState.Selected) == DrawItemState.Selected &&
2079 (!DroppedDown || (e.State & DrawItemState.ComboBoxEdit) != DrawItemState.ComboBoxEdit)) {
2080 foreColor = ThemeEngine.Current.ColorHighlightText;
2082 int w = (int) gr.MeasureString (dcbi.Name, e.Font).Width;
2084 gr.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (ThemeEngine.Current.ColorHighlight),
2085 new Rectangle (xPos + 23, 1, w + 3, e.Bounds.Height - 2));
2086 if ((e.State & DrawItemState.Focus) == DrawItemState.Focus) {
2087 ControlPaint.DrawFocusRectangle (gr, new Rectangle (xPos + 22, 0, w + 5,
2088 e.Bounds.Height), foreColor, ThemeEngine.Current.ColorHighlight);
2092 gr.DrawString (dcbi.Name, e.Font , ThemeEngine.Current.ResPool.GetSolidBrush (foreColor), new Point (24 + xPos, (bmp.Height - e.Font.Height) / 2));
2093 gr.DrawImage (dcbi.ImageList.Images [dcbi.ImageIndex], new Rectangle (new Point (xPos + 2, 0), new Size (16, 16)));
2095 e.Graphics.DrawImage (bmp, e.Bounds.X, e.Bounds.Y);
2100 protected override void OnSelectedIndexChanged (EventArgs e)
2102 if (Items.Count > 0) {
2103 DirComboBoxItem dcbi = Items [SelectedIndex] as DirComboBoxItem;
2105 currentPath = dcbi.Path;
2109 protected override void OnSelectionChangeCommitted (EventArgs e)
2111 EventHandler eh = (EventHandler)(Events [CDirectoryChangedEvent]);
2113 eh (this, EventArgs.Empty);
2116 static object CDirectoryChangedEvent = new object ();
2118 public event EventHandler DirectoryChanged {
2119 add { Events.AddHandler (CDirectoryChangedEvent, value); }
2120 remove { Events.RemoveHandler (CDirectoryChangedEvent, value); }
2125 #region FilterStruct
2126 internal struct FilterStruct
2128 public string filterName;
2129 public StringCollection filters;
2131 public FilterStruct (string filterName, string filter)
2133 this.filterName = filterName;
2135 filters = new StringCollection ();
2137 SplitFilters (filter);
2140 private void SplitFilters (string filter)
2142 string[] split = filter.Split (new char [] {';'});
2143 foreach (string s in split) {
2144 filters.Add (s.Trim ());
2151 internal class FileFilter
2153 private ArrayList filterArrayList = new ArrayList();
2155 private string filter;
2157 public FileFilter ()
2160 public FileFilter (string filter) : base ()
2162 this.filter = filter;
2167 public static bool CheckFilter (string val)
2169 if (val.Length == 0)
2172 string[] filters = val.Split (new char [] {'|'});
2174 if ((filters.Length % 2) != 0)
2180 public ArrayList FilterArrayList {
2182 filterArrayList = value;
2186 return filterArrayList;
2190 public string Filter {
2202 private void SplitFilter ()
2204 filterArrayList.Clear ();
2206 if (filter.Length == 0)
2209 string[] filters = filter.Split (new char [] {'|'});
2211 for (int i = 0; i < filters.Length; i += 2) {
2212 FilterStruct filterStruct = new FilterStruct (filters [i], filters [i + 1]);
2214 filterArrayList.Add (filterStruct);
2222 internal class MWFFileView : ListView
2224 public delegate void ThumbnailDelegate(FileViewListViewItem fi);
2225 private ThumbnailCreator thumbCreator;
2227 private ArrayList filterArrayList;
2229 private bool showHiddenFiles = false;
2231 private string selectedFilesString;
2233 private int filterIndex = 1;
2235 private ToolTip toolTip;
2236 private int oldItemIndexForToolTip = -1;
2238 private ContextMenu contextMenu;
2240 private MenuItem menuItemView;
2241 private MenuItem menuItemNew;
2243 private MenuItem smallIconMenutItem;
2244 private MenuItem tilesMenutItem;
2245 private MenuItem largeIconMenutItem;
2246 private MenuItem listMenutItem;
2247 private MenuItem detailsMenutItem;
2249 private MenuItem newFolderMenuItem;
2250 private MenuItem showHiddenFilesMenuItem;
2252 private int previousCheckedMenuItemIndex;
2254 private ArrayList viewMenuItemClones = new ArrayList ();
2256 private FSEntry currentFSEntry = null;
2258 private string currentFolder;
2259 private string currentRealFolder;
2260 private FSEntry currentFolderFSEntry;
2262 // store DirectoryInfo for a back button for example
2263 private Stack directoryStack = new Stack();
2265 // list of controls(components to enable or disable depending on current directoryStack.Count
2266 private ArrayList dirStackControlsOrComponents = new ArrayList ();
2268 private ToolBarButton folderUpToolBarButton;
2270 private ArrayList registered_senders = new ArrayList ();
2272 private bool should_push = true;
2276 private View old_view;
2278 private int old_menuitem_index;
2279 private bool do_update_view = false;
2281 private ColumnHeader [] columns;
2283 public MWFFileView (MWFVFS vfs)
2286 this.vfs.RegisterUpdateDelegate (new MWFVFS.UpdateDelegate (RealFileViewUpdate), this);
2290 contextMenu = new ContextMenu ();
2292 toolTip = new ToolTip ();
2293 toolTip.InitialDelay = 300;
2294 toolTip.ReshowDelay = 0;
2299 menuItemView = new MenuItem ("View");
2301 smallIconMenutItem = new MenuItem ("Small Icon", new EventHandler (OnClickViewMenuSubItem));
2302 smallIconMenutItem.RadioCheck = true;
2303 menuItemView.MenuItems.Add (smallIconMenutItem);
2305 tilesMenutItem = new MenuItem ("Tiles", new EventHandler (OnClickViewMenuSubItem));
2306 tilesMenutItem.RadioCheck = true;
2307 menuItemView.MenuItems.Add (tilesMenutItem);
2309 largeIconMenutItem = new MenuItem ("Large Icon", new EventHandler (OnClickViewMenuSubItem));
2310 largeIconMenutItem.RadioCheck = true;
2311 menuItemView.MenuItems.Add (largeIconMenutItem);
2313 listMenutItem = new MenuItem ("List", new EventHandler (OnClickViewMenuSubItem));
2314 listMenutItem.RadioCheck = true;
2315 listMenutItem.Checked = true;
2316 menuItemView.MenuItems.Add (listMenutItem);
2317 previousCheckedMenuItemIndex = listMenutItem.Index;
2319 detailsMenutItem = new MenuItem ("Details", new EventHandler (OnClickViewMenuSubItem));
2320 detailsMenutItem.RadioCheck = true;
2321 menuItemView.MenuItems.Add (detailsMenutItem);
2323 contextMenu.MenuItems.Add (menuItemView);
2325 contextMenu.MenuItems.Add (new MenuItem ("-"));
2328 menuItemNew = new MenuItem ("New");
2330 newFolderMenuItem = new MenuItem ("New Folder", new EventHandler (OnClickNewFolderMenuItem));
2331 menuItemNew.MenuItems.Add (newFolderMenuItem);
2333 contextMenu.MenuItems.Add (menuItemNew);
2335 contextMenu.MenuItems.Add (new MenuItem ("-"));
2337 // Show hidden files menu item
2338 showHiddenFilesMenuItem = new MenuItem ("Show hidden files", new EventHandler (OnClickContextMenu));
2339 showHiddenFilesMenuItem.Checked = showHiddenFiles;
2340 contextMenu.MenuItems.Add (showHiddenFilesMenuItem);
2344 SmallImageList = MimeIconEngine.SmallIcons;
2345 LargeImageList = MimeIconEngine.LargeIcons;
2347 View = old_view = View.List;
2350 ContextMenu = contextMenu;
2352 // Create columns, but only add them when view changes to Details
2353 columns = new ColumnHeader [4];
2354 columns [0] = CreateColumnHeader (" Name", 170, HorizontalAlignment.Left);
2355 columns [1] = CreateColumnHeader ("Size ", 80, HorizontalAlignment.Right);
2356 columns [2] = CreateColumnHeader (" Type", 100, HorizontalAlignment.Left);
2357 columns [3] = CreateColumnHeader (" Last Access", 150, HorizontalAlignment.Left);
2359 AllowColumnReorder = true;
2361 ResumeLayout (false);
2363 KeyDown += new KeyEventHandler (MWF_KeyDown);
2366 ColumnHeader CreateColumnHeader (string text, int width, HorizontalAlignment alignment)
2368 ColumnHeader col = new ColumnHeader ();
2371 col.TextAlign = alignment;
2376 public string CurrentFolder {
2378 return currentFolder;
2381 currentFolder = value;
2385 public string CurrentRealFolder {
2387 return currentRealFolder;
2391 public FSEntry CurrentFSEntry {
2393 return currentFSEntry;
2397 public MenuItem[] ViewMenuItems {
2399 MenuItem[] menuItemClones = new MenuItem [] {
2400 smallIconMenutItem.CloneMenu (),
2401 tilesMenutItem.CloneMenu (),
2402 largeIconMenutItem.CloneMenu (),
2403 listMenutItem.CloneMenu (),
2404 detailsMenutItem.CloneMenu ()
2407 viewMenuItemClones.Add (menuItemClones);
2409 return menuItemClones;
2413 public ArrayList FilterArrayList {
2415 filterArrayList = value;
2419 return filterArrayList;
2423 public bool ShowHiddenFiles {
2425 showHiddenFiles = value;
2429 return showHiddenFiles;
2433 public int FilterIndex {
2435 filterIndex = value;
2445 public string SelectedFilesString {
2447 selectedFilesString = value;
2451 return selectedFilesString;
2455 public void PushDir ()
2457 if (currentFolder != null)
2458 directoryStack.Push (currentFolder);
2460 EnableOrDisableDirstackObjects ();
2463 public void PopDir ()
2468 public void PopDir (string filter)
2470 if (directoryStack.Count == 0)
2473 string new_folder = directoryStack.Pop () as string;
2475 EnableOrDisableDirstackObjects ();
2477 should_push = false;
2479 ChangeDirectory (null, new_folder, filter);
2482 public void RegisterSender (IUpdateFolder iud)
2484 registered_senders.Add (iud);
2487 public void CreateNewFolder ()
2489 if (currentFolder == MWFVFS.MyComputerPrefix ||
2490 currentFolder == MWFVFS.RecentlyUsedPrefix)
2493 FSEntry fsEntry = new FSEntry ();
2494 fsEntry.Attributes = FileAttributes.Directory;
2495 fsEntry.FileType = FSEntry.FSEntryType.Directory;
2496 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("inode/directory");
2497 fsEntry.LastAccessTime = DateTime.Now;
2499 // FIXME: when ListView.LabelEdit is available use it
2500 // listViewItem.BeginEdit();
2502 TextEntryDialog ted = new TextEntryDialog ();
2503 ted.IconPictureBoxImage = MimeIconEngine.LargeIcons.Images.GetImage (fsEntry.IconIndex);
2505 string folder = String.Empty;
2507 if (currentFolderFSEntry.RealName != null)
2508 folder = currentFolderFSEntry.RealName;
2510 folder = currentFolder;
2512 string tmp_filename = "New Folder";
2514 if (Directory.Exists (Path.Combine (folder, tmp_filename))) {
2517 if (XplatUI.RunningOnUnix) {
2518 tmp_filename = tmp_filename + "-" + i;
2520 tmp_filename = tmp_filename + " (" + i + ")";
2523 while (Directory.Exists (Path.Combine (folder, tmp_filename))) {
2525 if (XplatUI.RunningOnUnix) {
2526 tmp_filename = "New Folder" + "-" + i;
2528 tmp_filename = "New Folder" + " (" + i + ")";
2533 ted.FileName = tmp_filename;
2535 if (ted.ShowDialog () == DialogResult.OK) {
2536 string new_folder = Path.Combine (folder, ted.FileName);
2538 if (vfs.CreateFolder (new_folder)) {
2539 fsEntry.FullName = new_folder;
2540 fsEntry.Name = ted.FileName;
2542 FileViewListViewItem listViewItem = new FileViewListViewItem (fsEntry);
2545 Items.Add (listViewItem);
2548 listViewItem.EnsureVisible ();
2553 public void SetSelectedIndexTo (string fname)
2555 foreach (FileViewListViewItem item in Items) {
2556 if (item.Text == fname) {
2558 SelectedItems.Clear ();
2559 item.Selected = true;
2566 public void OneDirUp ()
2571 public void OneDirUp (string filter)
2573 string parent_folder = vfs.GetParent ();
2574 if (parent_folder != null)
2575 ChangeDirectory (null, parent_folder, filter);
2578 public void ChangeDirectory (object sender, string folder)
2580 ChangeDirectory (sender, folder, null);
2583 public void ChangeDirectory (object sender, string folder, string filter)
2585 if (folder == MWFVFS.DesktopPrefix || folder == MWFVFS.RecentlyUsedPrefix)
2586 folderUpToolBarButton.Enabled = false;
2588 folderUpToolBarButton.Enabled = true;
2590 foreach (IUpdateFolder iuf in registered_senders) {
2591 iuf.CurrentFolder = folder;
2599 currentFolderFSEntry = vfs.ChangeDirectory (folder);
2601 currentFolder = folder;
2603 if (currentFolder.IndexOf ("://") != -1)
2604 currentRealFolder = currentFolderFSEntry.RealName;
2606 currentRealFolder = currentFolder;
2611 SelectedItems.Clear ();
2613 if (folder == MWFVFS.RecentlyUsedPrefix) {
2615 View = View.Details;
2616 old_menuitem_index = previousCheckedMenuItemIndex;
2617 UpdateMenuItems (detailsMenutItem);
2618 do_update_view = true;
2620 if (View != old_view && do_update_view) {
2621 UpdateMenuItems (menuItemView.MenuItems [old_menuitem_index]);
2623 do_update_view = false;
2628 UpdateFileView (filter);
2629 } catch (Exception e) {
2632 MessageBox.Show (e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
2636 public void UpdateFileView ()
2638 UpdateFileView (null);
2641 internal void StopThumbnailCreation()
2643 if (thumbCreator != null) {
2644 thumbCreator.Stop();
2645 thumbCreator = null;
2649 public void UpdateFileView (string custom_filter)
2651 StopThumbnailCreation();
2653 if (custom_filter != null) {
2654 StringCollection custom_filters = new StringCollection ();
2655 custom_filters.Add (custom_filter);
2657 vfs.GetFolderContent (custom_filters);
2658 } else if (filterArrayList != null && filterArrayList.Count != 0) {
2659 FilterStruct fs = (FilterStruct)filterArrayList [filterIndex - 1];
2661 vfs.GetFolderContent (fs.filters);
2663 vfs.GetFolderContent ();
2666 public void RealFileViewUpdate (ArrayList directoriesArrayList, ArrayList fileArrayList)
2671 SelectedItems.Clear ();
2673 foreach (FSEntry directoryFSEntry in directoriesArrayList) {
2674 if (!ShowHiddenFiles)
2675 if (directoryFSEntry.Name.StartsWith (".") || (directoryFSEntry.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
2678 FileViewListViewItem listViewItem = new FileViewListViewItem (directoryFSEntry);
2680 Items.Add (listViewItem);
2683 StringCollection collection = new StringCollection ();
2685 foreach (FSEntry fsEntry in fileArrayList) {
2687 // remove duplicates. that can happen when you read recently used files for example
2688 if (collection.Contains (fsEntry.Name)) {
2690 string fileName = fsEntry.Name;
2692 if (collection.Contains (fileName)) {
2695 while (collection.Contains (fileName + "(" + i + ")")) {
2699 fileName = fileName + "(" + i + ")";
2702 fsEntry.Name = fileName;
2705 collection.Add (fsEntry.Name);
2707 DoOneFSEntry (fsEntry);
2712 collection.Clear ();
2715 directoriesArrayList.Clear ();
2716 fileArrayList.Clear ();
2718 // Create thumbnail images for Image type files. This greatly facilitates
2719 // choosing pictures whose names mean nothing.
2720 // See https://bugzilla.xamarin.com/show_bug.cgi?id=28025 for details.
2721 thumbCreator = new ThumbnailCreator(new ThumbnailDelegate(RedrawTheItem), this);
2722 var makeThumbnails = new Thread(new ThreadStart(thumbCreator.MakeThumbnails));
2723 makeThumbnails.IsBackground = true;
2724 makeThumbnails.Start();
2727 private void RedrawTheItem(FileViewListViewItem fi)
2729 this.RedrawItems(fi.Index, fi.Index, false);
2732 public void AddControlToEnableDisableByDirStack (object control)
2734 dirStackControlsOrComponents.Add (control);
2737 public void SetFolderUpToolBarButton (ToolBarButton tb)
2739 folderUpToolBarButton = tb;
2742 public void WriteRecentlyUsed (string fullfilename)
2744 vfs.WriteRecentlyUsedFiles (fullfilename);
2747 private void EnableOrDisableDirstackObjects ()
2749 foreach (object o in dirStackControlsOrComponents) {
2751 Control c = o as Control;
2752 c.Enabled = (directoryStack.Count > 1);
2754 if (o is ToolBarButton) {
2755 ToolBarButton t = o as ToolBarButton;
2756 t.Enabled = (directoryStack.Count > 0);
2761 private void DoOneFSEntry (FSEntry fsEntry)
2763 if (!ShowHiddenFiles)
2764 if (fsEntry.Name.StartsWith (".") || (fsEntry.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
2767 FileViewListViewItem listViewItem = new FileViewListViewItem (fsEntry);
2769 Items.Add (listViewItem);
2772 private void MWF_KeyDown (object sender, KeyEventArgs e)
2774 if (e.KeyCode == Keys.Back) {
2776 } else if (e.Control && e.KeyCode == Keys.A && MultiSelect) {
2777 foreach (ListViewItem lvi in Items)
2778 lvi.Selected = true;
2782 #region UIA Framework Members
2783 internal void PerformClick ()
2785 OnClick (EventArgs.Empty);
2788 internal void PerformDoubleClick ()
2790 OnDoubleClick (EventArgs.Empty);
2794 protected override void OnClick (EventArgs e)
2797 if (SelectedItems.Count > 0) {
2798 FileViewListViewItem listViewItem = SelectedItems [0] as FileViewListViewItem;
2800 FSEntry fsEntry = listViewItem.FSEntry;
2802 if (fsEntry.FileType == FSEntry.FSEntryType.File) {
2803 currentFSEntry = fsEntry;
2805 EventHandler eh = (EventHandler)(Events [MSelectedFileChangedEvent]);
2807 eh (this, EventArgs.Empty);
2815 protected override void OnDoubleClick (EventArgs e)
2817 if (SelectedItems.Count > 0) {
2818 FileViewListViewItem listViewItem = SelectedItems [0] as FileViewListViewItem;
2820 FSEntry fsEntry = listViewItem.FSEntry;
2822 if ((fsEntry.Attributes & FileAttributes.Directory) == FileAttributes.Directory) {
2824 ChangeDirectory (null, fsEntry.FullName);
2826 EventHandler eh = (EventHandler)(Events [MDirectoryChangedEvent]);
2828 eh (this, EventArgs.Empty);
2830 currentFSEntry = fsEntry;
2832 EventHandler eh = (EventHandler)(Events [MSelectedFileChangedEvent]);
2834 eh (this, EventArgs.Empty);
2836 eh = (EventHandler)(Events [MForceDialogEndEvent]);
2838 eh (this, EventArgs.Empty);
2844 base.OnDoubleClick (e);
2847 protected override void OnSelectedIndexChanged (EventArgs e)
2849 if (SelectedItems.Count > 0) {
2850 selectedFilesString = String.Empty;
2852 if (SelectedItems.Count == 1) {
2853 FileViewListViewItem listViewItem = SelectedItems [0] as FileViewListViewItem;
2855 FSEntry fsEntry = listViewItem.FSEntry;
2857 if ((fsEntry.Attributes & FileAttributes.Directory) != FileAttributes.Directory)
2858 selectedFilesString = SelectedItems [0].Text;
2860 foreach (FileViewListViewItem lvi in SelectedItems) {
2861 FSEntry fsEntry = lvi.FSEntry;
2863 if ((fsEntry.Attributes & FileAttributes.Directory) != FileAttributes.Directory)
2864 selectedFilesString = selectedFilesString + "\"" + lvi.Text + "\" ";
2868 EventHandler eh = (EventHandler)(Events [MSelectedFilesChangedEvent]);
2870 eh (this, EventArgs.Empty);
2873 base.OnSelectedIndexChanged (e);
2876 protected override void OnMouseMove (MouseEventArgs e)
2878 FileViewListViewItem item = GetItemAt (e.X, e.Y) as FileViewListViewItem;
2881 int currentItemIndex = item.Index;
2883 if (currentItemIndex != oldItemIndexForToolTip) {
2884 oldItemIndexForToolTip = currentItemIndex;
2886 if (toolTip != null && toolTip.Active)
2887 toolTip.Active = false;
2889 FSEntry fsEntry = item.FSEntry;
2891 string output = String.Empty;
2893 if (fsEntry.FileType == FSEntry.FSEntryType.Directory)
2894 output = "Directory: " + fsEntry.FullName;
2895 else if (fsEntry.FileType == FSEntry.FSEntryType.Device)
2896 output = "Device: "+ fsEntry.FullName;
2897 else if (fsEntry.FileType == FSEntry.FSEntryType.Network)
2898 output = "Network: " + fsEntry.FullName;
2900 output = "File: " + fsEntry.FullName;
2902 toolTip.SetToolTip (this, output);
2904 toolTip.Active = true;
2907 toolTip.Active = false;
2909 base.OnMouseMove (e);
2912 void OnClickContextMenu (object sender, EventArgs e)
2914 MenuItem senderMenuItem = sender as MenuItem;
2916 if (senderMenuItem == showHiddenFilesMenuItem) {
2917 senderMenuItem.Checked = !senderMenuItem.Checked;
2918 showHiddenFiles = senderMenuItem.Checked;
2923 void OnClickViewMenuSubItem (object sender, EventArgs e)
2925 MenuItem senderMenuItem = (MenuItem)sender;
2927 UpdateMenuItems (senderMenuItem);
2929 // update me - call BeginUpdate/EndUpdate to avoid flicker when columns change
2932 switch (senderMenuItem.Index) {
2934 View = View.SmallIcon;
2940 View = View.LargeIcon;
2946 View = View.Details;
2952 if (View == View.Details)
2953 Columns.AddRange (columns);
2955 ListViewItemSorter = null;
2962 protected override void OnBeforeLabelEdit (LabelEditEventArgs e)
2964 FileViewListViewItem listViewItem = SelectedItems [0] as FileViewListViewItem;
2965 FSEntry fsEntry = listViewItem.FSEntry;
2967 // only allow editing of files or directories
2968 if (fsEntry.FileType != FSEntry.FSEntryType.Directory &&
2969 fsEntry.FileType != FSEntry.FSEntryType.File)
2970 e.CancelEdit = true;
2972 base.OnBeforeLabelEdit (e);
2975 protected override void OnAfterLabelEdit (LabelEditEventArgs e)
2977 base.OnAfterLabelEdit (e);
2979 // no changes were made
2980 if (e.Label == null || Items [e.Item].Text == e.Label)
2983 FileViewListViewItem listViewItem = SelectedItems [0] as FileViewListViewItem;
2984 FSEntry fsEntry = listViewItem.FSEntry;
2986 string folder = (currentFolderFSEntry.RealName != null) ?
2987 currentFolderFSEntry.RealName : currentFolder;
2989 switch (fsEntry.FileType) {
2990 case FSEntry.FSEntryType.Directory:
2991 string sourceDir = (fsEntry.RealName != null) ? fsEntry.RealName : fsEntry.FullName;
2992 string destDir = Path.Combine (folder, e.Label);
2993 if (!vfs.MoveFolder (sourceDir, destDir)) {
2994 e.CancelEdit = true;
2996 if (fsEntry.RealName != null)
2997 fsEntry.RealName = destDir;
2999 fsEntry.FullName = destDir;
3002 case FSEntry.FSEntryType.File:
3003 string sourceFile = (fsEntry.RealName != null) ? fsEntry.RealName : fsEntry.FullName;
3004 string destFile = Path.Combine (folder, e.Label);
3005 if (!vfs.MoveFile (sourceFile, destFile)) {
3006 e.CancelEdit = true;
3008 if (fsEntry.RealName != null)
3009 fsEntry.RealName = destFile;
3011 fsEntry.FullName = destFile;
3017 private void UpdateMenuItems (MenuItem senderMenuItem)
3019 menuItemView.MenuItems [previousCheckedMenuItemIndex].Checked = false;
3020 menuItemView.MenuItems [senderMenuItem.Index].Checked = true;
3022 foreach (MenuItem[] items in viewMenuItemClones) {
3023 items [previousCheckedMenuItemIndex].Checked = false;
3024 items [senderMenuItem.Index].Checked = true;
3027 previousCheckedMenuItemIndex = senderMenuItem.Index;
3030 void OnClickNewFolderMenuItem (object sender, EventArgs e)
3035 static object MSelectedFileChangedEvent = new object ();
3036 static object MSelectedFilesChangedEvent = new object ();
3037 static object MDirectoryChangedEvent = new object ();
3038 static object MForceDialogEndEvent = new object ();
3040 public event EventHandler SelectedFileChanged {
3041 add { Events.AddHandler (MSelectedFileChangedEvent, value); }
3042 remove { Events.RemoveHandler (MSelectedFileChangedEvent, value); }
3045 public event EventHandler SelectedFilesChanged {
3046 add { Events.AddHandler (MSelectedFilesChangedEvent, value); }
3047 remove { Events.RemoveHandler (MSelectedFilesChangedEvent, value); }
3050 public event EventHandler DirectoryChanged {
3051 add { Events.AddHandler (MDirectoryChangedEvent, value); }
3052 remove { Events.RemoveHandler (MDirectoryChangedEvent, value); }
3055 public event EventHandler ForceDialogEnd {
3056 add { Events.AddHandler (MForceDialogEndEvent, value); }
3057 remove { Events.RemoveHandler (MForceDialogEndEvent, value); }
3060 internal class ThumbnailCreator
3062 private ThumbnailDelegate thumbnailDelegate;
3063 private ListView control;
3064 private readonly object lockobject = new object();
3065 private bool stopped = false;
3067 public ThumbnailCreator(ThumbnailDelegate thumbnailDelegate, ListView listView)
3069 this.thumbnailDelegate = thumbnailDelegate;
3070 this.control = listView;
3073 public void MakeThumbnails()
3075 foreach (var item in control.Items) {
3076 var fi = item as FileViewListViewItem;
3077 if (fi == null || fi.FSEntry == null || !fi.FSEntry.IsImageFile())
3079 fi.FSEntry.SetImage();
3082 if (thumbnailDelegate != null) {
3084 object[] objectArray = new object[1];
3085 objectArray[0] = fi;
3086 control.Invoke(thumbnailDelegate, objectArray);
3102 #region FileListViewItem
3103 internal class FileViewListViewItem : ListViewItem
3105 private FSEntry fsEntry;
3107 public FileViewListViewItem (FSEntry fsEntry)
3109 this.fsEntry = fsEntry;
3111 ImageIndex = fsEntry.IconIndex;
3113 Text = fsEntry.Name;
3115 switch (fsEntry.FileType) {
3116 case FSEntry.FSEntryType.Directory:
3117 SubItems.Add (String.Empty);
3118 SubItems.Add ("Directory");
3119 SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ());
3121 case FSEntry.FSEntryType.File:
3124 if (fsEntry.FileSize > 1024)
3125 fileLen = fsEntry.FileSize / 1024;
3126 } catch (Exception) {
3130 SubItems.Add (fileLen.ToString () + " KB");
3131 SubItems.Add ("File");
3132 SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ());
3134 case FSEntry.FSEntryType.Device:
3135 SubItems.Add (String.Empty);
3136 SubItems.Add ("Device");
3137 SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ());
3139 case FSEntry.FSEntryType.RemovableDevice:
3140 SubItems.Add (String.Empty);
3141 SubItems.Add ("RemovableDevice");
3142 SubItems.Add (fsEntry.LastAccessTime.ToShortDateString () + " " + fsEntry.LastAccessTime.ToShortTimeString ());
3149 public FSEntry FSEntry {
3162 #region MwfFileViewItemComparer
3163 class MwfFileViewItemComparer : IComparer
3168 public MwfFileViewItemComparer (bool asc)
3173 public int ColumnIndex {
3175 return column_index;
3178 column_index = value;
3182 public bool Ascendent {
3191 public int Compare (object a, object b)
3193 ListViewItem item_a = (ListViewItem)a;
3194 ListViewItem item_b = (ListViewItem)b;
3198 retval = String.Compare (item_a.SubItems [column_index].Text,
3199 item_b.SubItems [column_index].Text);
3201 retval = String.Compare (item_b.SubItems [column_index].Text,
3202 item_a.SubItems [column_index].Text);
3209 #region IUpdateFolder
3210 internal interface IUpdateFolder
3212 string CurrentFolder {get; set;}
3216 #region TextEntryDialog
3217 // FIXME: When ListView.LabelEdit is implemented remove me
3218 internal class TextEntryDialog : Form
3220 private Label label1;
3221 private Button okButton;
3222 private TextBox newNameTextBox;
3223 private PictureBox iconPictureBox;
3224 private Button cancelButton;
3225 private GroupBox groupBox1;
3227 public TextEntryDialog ()
3229 groupBox1 = new GroupBox ();
3230 cancelButton = new Button ();
3231 iconPictureBox = new PictureBox ();
3232 newNameTextBox = new TextBox ();
3233 okButton = new Button ();
3234 label1 = new Label ();
3235 groupBox1.SuspendLayout ();
3239 groupBox1.Controls.Add (newNameTextBox);
3240 groupBox1.Controls.Add (label1);
3241 groupBox1.Controls.Add (iconPictureBox);
3242 groupBox1.Location = new Point (8, 8);
3243 groupBox1.Size = new Size (232, 160);
3244 groupBox1.TabIndex = 5;
3245 groupBox1.TabStop = false;
3246 groupBox1.Text = "New Name";
3249 cancelButton.DialogResult = DialogResult.Cancel;
3250 cancelButton.Location = new Point (168, 176);
3251 cancelButton.TabIndex = 4;
3252 cancelButton.Text = "Cancel";
3255 iconPictureBox.BorderStyle = BorderStyle.Fixed3D;
3256 iconPictureBox.Location = new Point (86, 24);
3257 iconPictureBox.Size = new Size (60, 60);
3258 iconPictureBox.TabIndex = 3;
3259 iconPictureBox.TabStop = false;
3260 iconPictureBox.SizeMode = PictureBoxSizeMode.CenterImage;
3263 newNameTextBox.Location = new Point (16, 128);
3264 newNameTextBox.Size = new Size (200, 20);
3265 newNameTextBox.TabIndex = 5;
3266 newNameTextBox.Text = String.Empty;
3269 okButton.DialogResult = DialogResult.OK;
3270 okButton.Location = new Point (80, 176);
3271 okButton.TabIndex = 3;
3272 okButton.Text = "OK";
3275 label1.Location = new Point (16, 96);
3276 label1.Size = new Size (200, 23);
3277 label1.TabIndex = 4;
3278 label1.Text = "Enter Name:";
3279 label1.TextAlign = ContentAlignment.MiddleCenter;
3282 AcceptButton = okButton;
3283 AutoScaleBaseSize = new Size (5, 13);
3284 CancelButton = cancelButton;
3285 ClientSize = new Size (248, 205);
3286 Controls.Add (groupBox1);
3287 Controls.Add (cancelButton);
3288 Controls.Add (okButton);
3289 FormBorderStyle = FormBorderStyle.FixedDialog;
3290 Text = "New Folder or File";
3291 groupBox1.ResumeLayout (false);
3292 ResumeLayout (false);
3294 newNameTextBox.Select ();
3297 public Image IconPictureBoxImage {
3299 iconPictureBox.Image = value;
3303 public string FileName {
3305 return newNameTextBox.Text;
3308 newNameTextBox.Text = value;
3315 internal class MWFVFS
3317 private FileSystem fileSystem;
3319 public static readonly string DesktopPrefix = "Desktop://";
3320 public static readonly string PersonalPrefix = "Personal://";
3321 public static readonly string MyComputerPrefix = "MyComputer://";
3322 public static readonly string RecentlyUsedPrefix = "RecentlyUsed://";
3323 public static readonly string MyNetworkPrefix = "MyNetwork://";
3324 public static readonly string MyComputerPersonalPrefix = "MyComputerPersonal://";
3326 public static Hashtable MyComputerDevicesPrefix = new Hashtable ();
3328 public delegate void UpdateDelegate (ArrayList folders, ArrayList files);
3329 private UpdateDelegate updateDelegate;
3330 private Control calling_control;
3332 private ThreadStart get_folder_content_thread_start;
3333 private Thread worker;
3334 private WorkerThread workerThread = null;
3335 private StringCollection the_filters;
3339 if (XplatUI.RunningOnUnix) {
3340 fileSystem = new UnixFileSystem ();
3342 fileSystem = new WinFileSystem ();
3346 public FSEntry ChangeDirectory (string folder)
3348 return fileSystem.ChangeDirectory (folder);
3351 public void GetFolderContent ()
3353 GetFolderContent (null);
3356 public void GetFolderContent (StringCollection filters)
3358 the_filters = filters;
3360 if (workerThread != null) {
3361 workerThread.Stop ();
3362 workerThread = null;
3365 // Added next line to ensure the control is created before BeginInvoke is called on it
3366 calling_control.CreateControl();
3367 workerThread = new WorkerThread (fileSystem, the_filters, updateDelegate, calling_control);
3369 get_folder_content_thread_start = new ThreadStart (workerThread.GetFolderContentThread);
3370 worker = new Thread (get_folder_content_thread_start);
3371 worker.IsBackground = true;
3375 internal class WorkerThread
3377 private FileSystem fileSystem;
3378 private StringCollection the_filters;
3379 private UpdateDelegate updateDelegate;
3380 private Control calling_control;
3381 private readonly object lockobject = new object ();
3382 private bool stopped = false;
3384 public WorkerThread (FileSystem fileSystem, StringCollection the_filters, UpdateDelegate updateDelegate, Control calling_control)
3386 this.fileSystem = fileSystem;
3387 this.the_filters = the_filters;
3388 this.updateDelegate = updateDelegate;
3389 this.calling_control = calling_control;
3392 public void GetFolderContentThread()
3397 fileSystem.GetFolderContent (the_filters, out folders, out files);
3402 if (updateDelegate != null) {
3404 object[] objectArray = new object[2];
3406 objectArray[0] = folders;
3407 objectArray[1] = files;
3409 calling_control.BeginInvoke (updateDelegate, objectArray);
3422 public ArrayList GetFoldersOnly ()
3424 return fileSystem.GetFoldersOnly ();
3427 public void WriteRecentlyUsedFiles (string filename)
3429 fileSystem.WriteRecentlyUsedFiles (filename);
3432 public ArrayList GetRecentlyUsedFiles ()
3434 return fileSystem.GetRecentlyUsedFiles ();
3437 public ArrayList GetMyComputerContent ()
3439 return fileSystem.GetMyComputerContent ();
3442 public ArrayList GetMyNetworkContent ()
3444 return fileSystem.GetMyNetworkContent ();
3447 public bool CreateFolder (string new_folder)
3450 if (Directory.Exists (new_folder)) {
3451 string message = "Folder \"" + new_folder + "\" already exists.";
3452 MessageBox.Show (message, new_folder, MessageBoxButtons.OK,
3453 MessageBoxIcon.Error);
3456 Directory.CreateDirectory (new_folder);
3457 } catch (Exception e) {
3458 MessageBox.Show (e.Message, new_folder, MessageBoxButtons.OK, MessageBoxIcon.Error);
3465 public bool MoveFolder (string sourceDirName, string destDirName)
3468 if (Directory.Exists (destDirName)) {
3469 string message = "Cannot rename " + Path.GetFileName (sourceDirName)
3470 + ": A folder with the name you specified already exists."
3471 + " Specify a different folder name.";
3472 MessageBox.Show (message, "Error Renaming Folder", MessageBoxButtons.OK,
3473 MessageBoxIcon.Error);
3476 Directory.Move (sourceDirName, destDirName);
3477 } catch (Exception e) {
3478 MessageBox.Show (e.Message, "Error Renaming Folder",
3479 MessageBoxButtons.OK, MessageBoxIcon.Error);
3486 public bool MoveFile (string sourceFileName, string destFileName)
3489 if (File.Exists (destFileName)) {
3490 string message = "Cannot rename " + Path.GetFileName (sourceFileName)
3491 + ": A file with the name you specified already exists."
3492 + " Specify a different file name.";
3493 MessageBox.Show (message, "Error Renaming File",
3494 MessageBoxButtons.OK, MessageBoxIcon.Error);
3497 File.Move (sourceFileName, destFileName);
3498 } catch (Exception e) {
3499 MessageBox.Show (e.Message, "Error Renaming File",
3500 MessageBoxButtons.OK, MessageBoxIcon.Error);
3507 public string GetParent ()
3509 return fileSystem.GetParent ();
3512 public void RegisterUpdateDelegate(UpdateDelegate updateDelegate, Control control)
3514 this.updateDelegate = updateDelegate;
3515 calling_control = control;
3521 internal abstract class FileSystem
3523 protected string currentTopFolder = String.Empty;
3524 protected FSEntry currentFolderFSEntry = null;
3525 protected FSEntry currentTopFolderFSEntry = null;
3526 private FileInfoComparer fileInfoComparer = new FileInfoComparer ();
3527 private FSEntryComparer fsEntryComparer = new FSEntryComparer ();
3529 public FSEntry ChangeDirectory (string folder)
3531 if (folder == MWFVFS.DesktopPrefix) {
3532 currentTopFolder = MWFVFS.DesktopPrefix;
3533 currentTopFolderFSEntry = currentFolderFSEntry = GetDesktopFSEntry ();
3535 if (folder == MWFVFS.PersonalPrefix) {
3536 currentTopFolder = MWFVFS.PersonalPrefix;
3537 currentTopFolderFSEntry = currentFolderFSEntry = GetPersonalFSEntry ();
3539 if (folder == MWFVFS.MyComputerPersonalPrefix) {
3540 currentTopFolder = MWFVFS.MyComputerPersonalPrefix;
3541 currentTopFolderFSEntry = currentFolderFSEntry = GetMyComputerPersonalFSEntry ();
3543 if (folder == MWFVFS.RecentlyUsedPrefix) {
3544 currentTopFolder = MWFVFS.RecentlyUsedPrefix;
3545 currentTopFolderFSEntry = currentFolderFSEntry = GetRecentlyUsedFSEntry ();
3547 if (folder == MWFVFS.MyComputerPrefix) {
3548 currentTopFolder = MWFVFS.MyComputerPrefix;
3549 currentTopFolderFSEntry = currentFolderFSEntry = GetMyComputerFSEntry ();
3551 if (folder == MWFVFS.MyNetworkPrefix) {
3552 currentTopFolder = MWFVFS.MyNetworkPrefix;
3553 currentTopFolderFSEntry = currentFolderFSEntry = GetMyNetworkFSEntry ();
3557 foreach (DictionaryEntry entry in MWFVFS.MyComputerDevicesPrefix) {
3558 FSEntry fsEntry = entry.Value as FSEntry;
3559 if (folder == fsEntry.FullName) {
3560 currentTopFolder = entry.Key as string;
3561 currentTopFolderFSEntry = currentFolderFSEntry = fsEntry;
3568 currentFolderFSEntry = GetDirectoryFSEntry (new DirectoryInfo (folder), currentTopFolderFSEntry);
3572 return currentFolderFSEntry;
3575 public string GetParent ()
3577 return currentFolderFSEntry.Parent;
3580 // directories_out and files_out contain FSEntry objects
3581 public void GetFolderContent (StringCollection filters, out ArrayList directories_out, out ArrayList files_out)
3583 directories_out = new ArrayList ();
3584 files_out = new ArrayList ();
3586 if (currentFolderFSEntry.FullName == MWFVFS.DesktopPrefix) {
3587 FSEntry personalFSEntry = GetPersonalFSEntry ();
3589 directories_out.Add (personalFSEntry);
3591 FSEntry myComputerFSEntry = GetMyComputerFSEntry ();
3593 directories_out.Add (myComputerFSEntry);
3595 FSEntry myNetworkFSEntry = GetMyNetworkFSEntry ();
3597 directories_out.Add (myNetworkFSEntry);
3599 ArrayList d_out = null;
3600 ArrayList f_out = null;
3601 GetNormalFolderContent (ThemeEngine.Current.Places (UIIcon.PlacesDesktop), filters, out d_out, out f_out);
3602 directories_out.AddRange (d_out);
3603 files_out.AddRange (f_out);
3606 if (currentFolderFSEntry.FullName == MWFVFS.RecentlyUsedPrefix) {
3607 files_out = GetRecentlyUsedFiles ();
3609 if (currentFolderFSEntry.FullName == MWFVFS.MyComputerPrefix) {
3610 directories_out.AddRange (GetMyComputerContent ());
3612 if (currentFolderFSEntry.FullName == MWFVFS.PersonalPrefix || currentFolderFSEntry.FullName == MWFVFS.MyComputerPersonalPrefix) {
3613 ArrayList d_out = null;
3614 ArrayList f_out = null;
3615 GetNormalFolderContent (ThemeEngine.Current.Places (UIIcon.PlacesPersonal), filters, out d_out, out f_out);
3616 directories_out.AddRange (d_out);
3617 files_out.AddRange (f_out);
3619 if (currentFolderFSEntry.FullName == MWFVFS.MyNetworkPrefix) {
3620 directories_out.AddRange (GetMyNetworkContent ());
3622 GetNormalFolderContent (currentFolderFSEntry.FullName, filters, out directories_out, out files_out);
3626 public ArrayList GetFoldersOnly ()
3628 ArrayList directories_out = new ArrayList ();
3630 if (currentFolderFSEntry.FullName == MWFVFS.DesktopPrefix) {
3631 FSEntry personalFSEntry = GetPersonalFSEntry ();
3633 directories_out.Add (personalFSEntry);
3635 FSEntry myComputerFSEntry = GetMyComputerFSEntry ();
3637 directories_out.Add (myComputerFSEntry);
3639 FSEntry myNetworkFSEntry = GetMyNetworkFSEntry ();
3641 directories_out.Add (myNetworkFSEntry);
3643 ArrayList d_out = GetNormalFolders (ThemeEngine.Current.Places (UIIcon.PlacesDesktop));
3644 directories_out.AddRange (d_out);
3647 if (currentFolderFSEntry.FullName == MWFVFS.RecentlyUsedPrefix) {
3648 //files_out = GetRecentlyUsedFiles ();
3650 if (currentFolderFSEntry.FullName == MWFVFS.MyComputerPrefix) {
3651 directories_out.AddRange (GetMyComputerContent ());
3653 if (currentFolderFSEntry.FullName == MWFVFS.PersonalPrefix || currentFolderFSEntry.FullName == MWFVFS.MyComputerPersonalPrefix) {
3654 ArrayList d_out = GetNormalFolders (ThemeEngine.Current.Places (UIIcon.PlacesPersonal));
3655 directories_out.AddRange (d_out);
3657 if (currentFolderFSEntry.FullName == MWFVFS.MyNetworkPrefix) {
3658 directories_out.AddRange (GetMyNetworkContent ());
3660 directories_out = GetNormalFolders (currentFolderFSEntry.FullName);
3662 return directories_out;
3665 protected void GetNormalFolderContent (string from_folder, StringCollection filters, out ArrayList directories_out, out ArrayList files_out)
3667 DirectoryInfo dirinfo = new DirectoryInfo (from_folder);
3669 directories_out = new ArrayList ();
3671 DirectoryInfo[] dirs = null;
3674 dirs = dirinfo.GetDirectories ();
3675 } catch (Exception) {}
3678 for (int i = 0; i < dirs.Length; i++) {
3679 directories_out.Add (GetDirectoryFSEntry (dirs [i], currentTopFolderFSEntry));
3682 directories_out.Sort (fsEntryComparer);
3684 files_out = new ArrayList ();
3686 ArrayList files = new ArrayList ();
3689 if (filters == null) {
3690 files.AddRange (dirinfo.GetFiles ());
3692 foreach (string s in filters)
3693 files.AddRange (dirinfo.GetFiles (s));
3695 files.Sort (fileInfoComparer);
3697 } catch (Exception) {}
3699 for (int i = 0; i < files.Count; i++) {
3700 FSEntry fs = GetFileFSEntry (files [i] as FileInfo);
3706 protected ArrayList GetNormalFolders (string from_folder)
3708 DirectoryInfo dirinfo = new DirectoryInfo (from_folder);
3710 ArrayList directories_out = new ArrayList ();
3712 DirectoryInfo[] dirs = null;
3715 dirs = dirinfo.GetDirectories ();
3716 } catch (Exception) {}
3719 for (int i = 0; i < dirs.Length; i++) {
3720 directories_out.Add (GetDirectoryFSEntry (dirs [i], currentTopFolderFSEntry));
3723 return directories_out;
3726 protected virtual FSEntry GetDirectoryFSEntry (DirectoryInfo dirinfo, FSEntry topFolderFSEntry)
3728 FSEntry fs = new FSEntry ();
3730 fs.Attributes = dirinfo.Attributes;
3731 fs.FullName = dirinfo.FullName;
3732 fs.Name = dirinfo.Name;
3733 fs.MainTopNode = topFolderFSEntry;
3734 fs.FileType = FSEntry.FSEntryType.Directory;
3735 fs.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("inode/directory");
3736 fs.LastAccessTime = dirinfo.LastAccessTime;
3741 protected virtual FSEntry GetFileFSEntry (FileInfo fileinfo)
3743 // *sigh* FileInfo gives us no usable information for links to directories
3745 if ((fileinfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
3748 FSEntry fs = new FSEntry ();
3750 fs.Attributes = fileinfo.Attributes;
3751 fs.FullName = fileinfo.FullName;
3752 fs.Name = fileinfo.Name;
3753 fs.FileType = FSEntry.FSEntryType.File;
3754 fs.IconIndex = MimeIconEngine.GetIconIndexForFile (fileinfo.FullName);
3755 fs.FileSize = fileinfo.Length;
3756 fs.LastAccessTime = fileinfo.LastAccessTime;
3761 internal class FileInfoComparer : IComparer
3763 public int Compare (object fileInfo1, object fileInfo2)
3765 return String.Compare (((FileInfo)fileInfo1).Name, ((FileInfo)fileInfo2).Name);
3769 internal class FSEntryComparer : IComparer
3771 public int Compare (object fileInfo1, object fileInfo2)
3773 return String.Compare (((FSEntry)fileInfo1).Name, ((FSEntry)fileInfo2).Name);
3777 protected abstract FSEntry GetDesktopFSEntry ();
3779 protected abstract FSEntry GetRecentlyUsedFSEntry ();
3781 protected abstract FSEntry GetPersonalFSEntry ();
3783 protected abstract FSEntry GetMyComputerPersonalFSEntry ();
3785 protected abstract FSEntry GetMyComputerFSEntry ();
3787 protected abstract FSEntry GetMyNetworkFSEntry ();
3789 public abstract void WriteRecentlyUsedFiles (string fileToAdd);
3791 public abstract ArrayList GetRecentlyUsedFiles ();
3793 public abstract ArrayList GetMyComputerContent ();
3795 public abstract ArrayList GetMyNetworkContent ();
3799 #region UnixFileSystem
3800 internal class UnixFileSystem : FileSystem
3802 private MasterMount masterMount = new MasterMount ();
3803 private FSEntry desktopFSEntry = null;
3804 private FSEntry recentlyusedFSEntry = null;
3805 private FSEntry personalFSEntry = null;
3806 private FSEntry mycomputerpersonalFSEntry = null;
3807 private FSEntry mycomputerFSEntry = null;
3808 private FSEntry mynetworkFSEntry = null;
3810 private string personal_folder;
3811 private string recently_used_path;
3812 private string full_kde_recent_document_dir;
3814 public UnixFileSystem ()
3816 personal_folder = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
3817 recently_used_path = Path.Combine (personal_folder, ".recently-used");
3819 full_kde_recent_document_dir = personal_folder + "/.kde/share/apps/RecentDocuments";
3821 desktopFSEntry = new FSEntry ();
3823 desktopFSEntry.Attributes = FileAttributes.Directory;
3824 desktopFSEntry.FullName = MWFVFS.DesktopPrefix;
3825 desktopFSEntry.Name = "Desktop";
3826 desktopFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesDesktop);
3827 desktopFSEntry.FileType = FSEntry.FSEntryType.Directory;
3828 desktopFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("desktop/desktop");
3829 desktopFSEntry.LastAccessTime = DateTime.Now;
3831 recentlyusedFSEntry = new FSEntry ();
3833 recentlyusedFSEntry.Attributes = FileAttributes.Directory;
3834 recentlyusedFSEntry.FullName = MWFVFS.RecentlyUsedPrefix;
3835 recentlyusedFSEntry.Name = "Recently Used";
3836 recentlyusedFSEntry.FileType = FSEntry.FSEntryType.Directory;
3837 recentlyusedFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("recently/recently");
3838 recentlyusedFSEntry.LastAccessTime = DateTime.Now;
3840 personalFSEntry = new FSEntry ();
3842 personalFSEntry.Attributes = FileAttributes.Directory;
3843 personalFSEntry.FullName = MWFVFS.PersonalPrefix;
3844 personalFSEntry.Name = "Personal";
3845 personalFSEntry.MainTopNode = GetDesktopFSEntry ();
3846 personalFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
3847 personalFSEntry.FileType = FSEntry.FSEntryType.Directory;
3848 personalFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("directory/home");
3849 personalFSEntry.LastAccessTime = DateTime.Now;
3851 mycomputerpersonalFSEntry = new FSEntry ();
3853 mycomputerpersonalFSEntry.Attributes = FileAttributes.Directory;
3854 mycomputerpersonalFSEntry.FullName = MWFVFS.MyComputerPersonalPrefix;
3855 mycomputerpersonalFSEntry.Name = "Personal";
3856 mycomputerpersonalFSEntry.MainTopNode = GetMyComputerFSEntry ();
3857 mycomputerpersonalFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
3858 mycomputerpersonalFSEntry.FileType = FSEntry.FSEntryType.Directory;
3859 mycomputerpersonalFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("directory/home");
3860 mycomputerpersonalFSEntry.LastAccessTime = DateTime.Now;
3862 mycomputerFSEntry = new FSEntry ();
3864 mycomputerFSEntry.Attributes = FileAttributes.Directory;
3865 mycomputerFSEntry.FullName = MWFVFS.MyComputerPrefix;
3866 mycomputerFSEntry.Name = "My Computer";
3867 mycomputerFSEntry.MainTopNode = GetDesktopFSEntry ();
3868 mycomputerFSEntry.FileType = FSEntry.FSEntryType.Directory;
3869 mycomputerFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("workplace/workplace");
3870 mycomputerFSEntry.LastAccessTime = DateTime.Now;
3872 mynetworkFSEntry = new FSEntry ();
3874 mynetworkFSEntry.Attributes = FileAttributes.Directory;
3875 mynetworkFSEntry.FullName = MWFVFS.MyNetworkPrefix;
3876 mynetworkFSEntry.Name = "My Network";
3877 mynetworkFSEntry.MainTopNode = GetDesktopFSEntry ();
3878 mynetworkFSEntry.FileType = FSEntry.FSEntryType.Directory;
3879 mynetworkFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("network/network");
3880 mynetworkFSEntry.LastAccessTime = DateTime.Now;
3883 public override void WriteRecentlyUsedFiles (string fileToAdd)
3885 if (File.Exists (recently_used_path) && new FileInfo (recently_used_path).Length > 0) {
3886 XmlDocument xml_doc = new XmlDocument ();
3887 xml_doc.Load (recently_used_path);
3889 XmlNode grand_parent_node = xml_doc.SelectSingleNode ("RecentFiles");
3891 if (grand_parent_node != null) {
3892 // create a new element
3893 XmlElement new_recent_item_node = xml_doc.CreateElement ("RecentItem");
3895 XmlElement new_child = xml_doc.CreateElement ("URI");
3896 UriBuilder ub = new UriBuilder ();
3897 ub.Path = fileToAdd;
3900 XmlText new_text_child = xml_doc.CreateTextNode (ub.ToString ());
3901 new_child.AppendChild (new_text_child);
3903 new_recent_item_node.AppendChild (new_child);
3905 new_child = xml_doc.CreateElement ("Mime-Type");
3906 new_text_child = xml_doc.CreateTextNode (Mime.GetMimeTypeForFile (fileToAdd));
3907 new_child.AppendChild (new_text_child);
3909 new_recent_item_node.AppendChild (new_child);
3911 new_child = xml_doc.CreateElement ("Timestamp");
3912 long seconds = (long)(DateTime.UtcNow - new DateTime (1970, 1, 1)).TotalSeconds;
3913 new_text_child = xml_doc.CreateTextNode (seconds.ToString ());
3914 new_child.AppendChild (new_text_child);
3916 new_recent_item_node.AppendChild (new_child);
3918 new_child = xml_doc.CreateElement ("Groups");
3920 new_recent_item_node.AppendChild (new_child);
3922 // now search the nodes in grand_parent_node for another instance of the new uri and if found remove it
3923 // so that the new node is the first one
3924 foreach (XmlNode n in grand_parent_node.ChildNodes) {
3925 XmlNode uri_node = n.SelectSingleNode ("URI");
3926 if (uri_node != null) {
3927 XmlNode uri_node_child = uri_node.FirstChild;
3928 if (uri_node_child is XmlText)
3929 if (ub.ToString () == ((XmlText)uri_node_child).Data) {
3930 grand_parent_node.RemoveChild (n);
3936 // prepend the new recent item to the grand parent node list
3937 grand_parent_node.PrependChild (new_recent_item_node);
3939 // limit the # of RecentItems to 10
3940 if (grand_parent_node.ChildNodes.Count > 10) {
3941 while (grand_parent_node.ChildNodes.Count > 10)
3942 grand_parent_node.RemoveChild (grand_parent_node.LastChild);
3946 xml_doc.Save (recently_used_path);
3947 } catch (Exception) {
3951 XmlDocument xml_doc = new XmlDocument ();
3952 xml_doc.AppendChild (xml_doc.CreateXmlDeclaration ("1.0", String.Empty, String.Empty));
3954 XmlElement recentFiles_element = xml_doc.CreateElement ("RecentFiles");
3956 XmlElement new_recent_item_node = xml_doc.CreateElement ("RecentItem");
3958 XmlElement new_child = xml_doc.CreateElement ("URI");
3959 UriBuilder ub = new UriBuilder ();
3960 ub.Path = fileToAdd;
3963 XmlText new_text_child = xml_doc.CreateTextNode (ub.ToString ());
3964 new_child.AppendChild (new_text_child);
3966 new_recent_item_node.AppendChild (new_child);
3968 new_child = xml_doc.CreateElement ("Mime-Type");
3969 new_text_child = xml_doc.CreateTextNode (Mime.GetMimeTypeForFile (fileToAdd));
3970 new_child.AppendChild (new_text_child);
3972 new_recent_item_node.AppendChild (new_child);
3974 new_child = xml_doc.CreateElement ("Timestamp");
3975 long seconds = (long)(DateTime.UtcNow - new DateTime (1970, 1, 1)).TotalSeconds;
3976 new_text_child = xml_doc.CreateTextNode (seconds.ToString ());
3977 new_child.AppendChild (new_text_child);
3979 new_recent_item_node.AppendChild (new_child);
3981 new_child = xml_doc.CreateElement ("Groups");
3983 new_recent_item_node.AppendChild (new_child);
3985 recentFiles_element.AppendChild (new_recent_item_node);
3987 xml_doc.AppendChild (recentFiles_element);
3990 xml_doc.Save (recently_used_path);
3991 } catch (Exception) {
3996 // return an ArrayList with FSEntry objects
3997 public override ArrayList GetRecentlyUsedFiles ()
3999 // check for GNOME and KDE
4001 ArrayList files_al = new ArrayList ();
4004 if (File.Exists (recently_used_path)) {
4006 XmlTextReader xtr = new XmlTextReader (recently_used_path);
4007 while (xtr.Read ()) {
4008 if (xtr.NodeType == XmlNodeType.Element && xtr.Name.ToUpper () == "URI") {
4010 Uri uri = new Uri (xtr.Value);
4011 if (!files_al.Contains (uri.LocalPath))
4012 if (File.Exists (uri.LocalPath)) {
4013 FSEntry fs = GetFileFSEntry (new FileInfo (uri.LocalPath));
4020 } catch (Exception) {
4026 if (Directory.Exists (full_kde_recent_document_dir)) {
4027 string[] files = Directory.GetFiles (full_kde_recent_document_dir, "*.desktop");
4029 foreach (string file_name in files) {
4030 StreamReader sr = new StreamReader (file_name);
4032 string line = sr.ReadLine ();
4034 while (line != null) {
4035 line = line.Trim ();
4037 if (line.StartsWith ("URL=")) {
4038 line = line.Replace ("URL=", String.Empty);
4039 line = line.Replace ("$HOME", personal_folder);
4041 Uri uri = new Uri (line);
4042 if (!files_al.Contains (uri.LocalPath))
4043 if (File.Exists (uri.LocalPath)) {
4044 FSEntry fs = GetFileFSEntry (new FileInfo (uri.LocalPath));
4051 line = sr.ReadLine ();
4062 // return an ArrayList with FSEntry objects
4063 public override ArrayList GetMyComputerContent ()
4065 ArrayList my_computer_content_arraylist = new ArrayList ();
4067 if (masterMount.ProcMountAvailable) {
4068 masterMount.GetMounts ();
4070 foreach (MasterMount.Mount mount in masterMount.Block_devices) {
4071 FSEntry fsEntry = new FSEntry ();
4072 fsEntry.FileType = FSEntry.FSEntryType.Device;
4074 fsEntry.FullName = mount.mount_point;
4076 fsEntry.Name = "HDD (" + mount.fsType + ", " + mount.device_short + ")";
4078 fsEntry.FsType = mount.fsType;
4079 fsEntry.DeviceShort = mount.device_short;
4081 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("harddisk/harddisk");
4083 fsEntry.Attributes = FileAttributes.Directory;
4085 fsEntry.MainTopNode = GetMyComputerFSEntry ();
4087 my_computer_content_arraylist.Add (fsEntry);
4089 if (!MWFVFS.MyComputerDevicesPrefix.Contains (fsEntry.FullName + "://"))
4090 MWFVFS.MyComputerDevicesPrefix.Add (fsEntry.FullName + "://", fsEntry);
4093 foreach (MasterMount.Mount mount in masterMount.Removable_devices) {
4094 FSEntry fsEntry = new FSEntry ();
4095 fsEntry.FileType = FSEntry.FSEntryType.RemovableDevice;
4097 fsEntry.FullName = mount.mount_point;
4099 bool is_dvd_cdrom = mount.fsType == MasterMount.FsTypes.usbfs ? false : true;
4100 string type_name = is_dvd_cdrom ? "DVD/CD-Rom" : "USB";
4101 string mime_type = is_dvd_cdrom ? "cdrom/cdrom" : "removable/removable";
4103 fsEntry.Name = type_name +" (" + mount.device_short + ")";
4105 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType (mime_type);
4107 fsEntry.FsType = mount.fsType;
4108 fsEntry.DeviceShort = mount.device_short;
4110 fsEntry.Attributes = FileAttributes.Directory;
4112 fsEntry.MainTopNode = GetMyComputerFSEntry ();
4114 my_computer_content_arraylist.Add (fsEntry);
4116 string contain_string = fsEntry.FullName + "://";
4117 if (!MWFVFS.MyComputerDevicesPrefix.Contains (contain_string))
4118 MWFVFS.MyComputerDevicesPrefix.Add (contain_string, fsEntry);
4122 my_computer_content_arraylist.Add (GetMyComputerPersonalFSEntry ());
4124 return my_computer_content_arraylist;
4127 public override ArrayList GetMyNetworkContent ()
4129 ArrayList fsEntries = new ArrayList ();
4131 foreach (MasterMount.Mount mount in masterMount.Network_devices) {
4132 FSEntry fsEntry = new FSEntry ();
4133 fsEntry.FileType = FSEntry.FSEntryType.Network;
4135 fsEntry.FullName = mount.mount_point;
4137 fsEntry.FsType = mount.fsType;
4138 fsEntry.DeviceShort = mount.device_short;
4140 fsEntry.Name = "Network (" + mount.fsType + ", " + mount.device_short + ")";
4142 switch (mount.fsType) {
4143 case MasterMount.FsTypes.nfs:
4144 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("nfs/nfs");
4146 case MasterMount.FsTypes.smbfs:
4147 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("smb/smb");
4149 case MasterMount.FsTypes.ncpfs:
4150 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("network/network");
4152 case MasterMount.FsTypes.cifs:
4153 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("network/network");
4159 fsEntry.Attributes = FileAttributes.Directory;
4161 fsEntry.MainTopNode = GetMyNetworkFSEntry ();
4163 fsEntries.Add (fsEntry);
4168 protected override FSEntry GetDesktopFSEntry ()
4170 return desktopFSEntry;
4173 protected override FSEntry GetRecentlyUsedFSEntry ()
4175 return recentlyusedFSEntry;
4178 protected override FSEntry GetPersonalFSEntry ()
4180 return personalFSEntry;
4183 protected override FSEntry GetMyComputerPersonalFSEntry ()
4185 return mycomputerpersonalFSEntry;
4188 protected override FSEntry GetMyComputerFSEntry ()
4190 return mycomputerFSEntry;
4193 protected override FSEntry GetMyNetworkFSEntry ()
4195 return mynetworkFSEntry;
4200 #region WinFileSystem
4201 internal class WinFileSystem : FileSystem
4203 private FSEntry desktopFSEntry = null;
4204 private FSEntry recentlyusedFSEntry = null;
4205 private FSEntry personalFSEntry = null;
4206 private FSEntry mycomputerpersonalFSEntry = null;
4207 private FSEntry mycomputerFSEntry = null;
4208 private FSEntry mynetworkFSEntry = null;
4210 public WinFileSystem ()
4212 desktopFSEntry = new FSEntry ();
4214 desktopFSEntry.Attributes = FileAttributes.Directory;
4215 desktopFSEntry.FullName = MWFVFS.DesktopPrefix;
4216 desktopFSEntry.Name = "Desktop";
4217 desktopFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesDesktop);
4218 desktopFSEntry.FileType = FSEntry.FSEntryType.Directory;
4219 desktopFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("desktop/desktop");
4220 desktopFSEntry.LastAccessTime = DateTime.Now;
4222 recentlyusedFSEntry = new FSEntry ();
4224 recentlyusedFSEntry.Attributes = FileAttributes.Directory;
4225 recentlyusedFSEntry.FullName = MWFVFS.RecentlyUsedPrefix;
4226 recentlyusedFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesRecentDocuments);
4227 recentlyusedFSEntry.Name = "Recently Used";
4228 recentlyusedFSEntry.FileType = FSEntry.FSEntryType.Directory;
4229 recentlyusedFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("recently/recently");
4230 recentlyusedFSEntry.LastAccessTime = DateTime.Now;
4232 personalFSEntry = new FSEntry ();
4234 personalFSEntry.Attributes = FileAttributes.Directory;
4235 personalFSEntry.FullName = MWFVFS.PersonalPrefix;
4236 personalFSEntry.Name = "Personal";
4237 personalFSEntry.MainTopNode = GetDesktopFSEntry ();
4238 personalFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
4239 personalFSEntry.FileType = FSEntry.FSEntryType.Directory;
4240 personalFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("directory/home");
4241 personalFSEntry.LastAccessTime = DateTime.Now;
4243 mycomputerpersonalFSEntry = new FSEntry ();
4245 mycomputerpersonalFSEntry.Attributes = FileAttributes.Directory;
4246 mycomputerpersonalFSEntry.FullName = MWFVFS.MyComputerPersonalPrefix;
4247 mycomputerpersonalFSEntry.Name = "Personal";
4248 mycomputerpersonalFSEntry.MainTopNode = GetMyComputerFSEntry ();
4249 mycomputerpersonalFSEntry.RealName = ThemeEngine.Current.Places (UIIcon.PlacesPersonal);
4250 mycomputerpersonalFSEntry.FileType = FSEntry.FSEntryType.Directory;
4251 mycomputerpersonalFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("directory/home");
4252 mycomputerpersonalFSEntry.LastAccessTime = DateTime.Now;
4254 mycomputerFSEntry = new FSEntry ();
4256 mycomputerFSEntry.Attributes = FileAttributes.Directory;
4257 mycomputerFSEntry.FullName = MWFVFS.MyComputerPrefix;
4258 mycomputerFSEntry.Name = "My Computer";
4259 mycomputerFSEntry.MainTopNode = GetDesktopFSEntry ();
4260 mycomputerFSEntry.FileType = FSEntry.FSEntryType.Directory;
4261 mycomputerFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("workplace/workplace");
4262 mycomputerFSEntry.LastAccessTime = DateTime.Now;
4264 mynetworkFSEntry = new FSEntry ();
4266 mynetworkFSEntry.Attributes = FileAttributes.Directory;
4267 mynetworkFSEntry.FullName = MWFVFS.MyNetworkPrefix;
4268 mynetworkFSEntry.Name = "My Network";
4269 mynetworkFSEntry.MainTopNode = GetDesktopFSEntry ();
4270 mynetworkFSEntry.FileType = FSEntry.FSEntryType.Directory;
4271 mynetworkFSEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("network/network");
4272 mynetworkFSEntry.LastAccessTime = DateTime.Now;
4275 public override void WriteRecentlyUsedFiles (string fileToAdd)
4277 // TODO: Implement this method
4278 // use SHAddToRecentDocs ?
4281 public override ArrayList GetRecentlyUsedFiles ()
4283 ArrayList al = new ArrayList ();
4285 DirectoryInfo di = new DirectoryInfo (recentlyusedFSEntry.RealName);
4287 FileInfo[] fileinfos = di.GetFiles ();
4289 foreach (FileInfo fi in fileinfos) {
4290 FSEntry fs = GetFileFSEntry (fi);
4298 public override ArrayList GetMyComputerContent ()
4300 string[] logical_drives = Directory.GetLogicalDrives ();
4302 ArrayList my_computer_content_arraylist = new ArrayList ();
4304 foreach (string drive in logical_drives) {
4305 FSEntry fsEntry = new FSEntry ();
4306 fsEntry.FileType = FSEntry.FSEntryType.Device;
4308 fsEntry.FullName = drive;
4310 fsEntry.Name = drive;
4312 fsEntry.IconIndex = MimeIconEngine.GetIconIndexForMimeType ("harddisk/harddisk");
4314 fsEntry.Attributes = FileAttributes.Directory;
4316 fsEntry.MainTopNode = GetMyComputerFSEntry ();
4318 my_computer_content_arraylist.Add (fsEntry);
4320 string contain_string = fsEntry.FullName + "://";
4321 if (!MWFVFS.MyComputerDevicesPrefix.Contains (contain_string))
4322 MWFVFS.MyComputerDevicesPrefix.Add (contain_string, fsEntry);
4325 my_computer_content_arraylist.Add (GetMyComputerPersonalFSEntry ());
4327 return my_computer_content_arraylist;
4330 public override ArrayList GetMyNetworkContent ()
4332 // TODO: Implement this method
4333 return new ArrayList ();
4335 protected override FSEntry GetDesktopFSEntry ()
4337 return desktopFSEntry;
4340 protected override FSEntry GetRecentlyUsedFSEntry ()
4342 return recentlyusedFSEntry;
4345 protected override FSEntry GetPersonalFSEntry ()
4347 return personalFSEntry;
4350 protected override FSEntry GetMyComputerPersonalFSEntry ()
4352 return mycomputerpersonalFSEntry;
4355 protected override FSEntry GetMyComputerFSEntry ()
4357 return mycomputerFSEntry;
4360 protected override FSEntry GetMyNetworkFSEntry ()
4362 return mynetworkFSEntry;
4368 internal class FSEntry
4370 public enum FSEntryType
4382 private MasterMount.FsTypes fsType;
4383 private string device_short;
4384 private string fullName;
4385 private string name;
4386 private string realName = null;
4387 private FileAttributes attributes = FileAttributes.Normal;
4388 private long fileSize;
4389 private FSEntryType fileType;
4390 private DateTime lastAccessTime;
4391 private FSEntry mainTopNode = null;
4393 private int iconIndex;
4395 private string parent;
4397 public MasterMount.FsTypes FsType {
4407 public string DeviceShort {
4409 device_short = value;
4413 return device_short;
4417 public string FullName {
4427 public string Name {
4437 public string RealName {
4447 public FileAttributes Attributes {
4457 public long FileSize {
4467 public FSEntryType FileType {
4477 public DateTime LastAccessTime {
4479 lastAccessTime = value;
4483 return lastAccessTime;
4487 public int IconIndex {
4497 public FSEntry MainTopNode {
4499 mainTopNode = value;
4507 public string Parent {
4513 parent = GetParent ();
4519 private string GetParent ()
4521 if (fullName == MWFVFS.PersonalPrefix) {
4522 return MWFVFS.DesktopPrefix;
4524 if (fullName == MWFVFS.MyComputerPersonalPrefix) {
4525 return MWFVFS.MyComputerPrefix;
4527 if (fullName == MWFVFS.MyComputerPrefix) {
4528 return MWFVFS.DesktopPrefix;
4530 if (fullName == MWFVFS.MyNetworkPrefix) {
4531 return MWFVFS.DesktopPrefix;
4533 if (fullName == MWFVFS.DesktopPrefix) {
4536 if (fullName == MWFVFS.RecentlyUsedPrefix) {
4539 foreach (DictionaryEntry entry in MWFVFS.MyComputerDevicesPrefix) {
4540 FSEntry fsEntry = entry.Value as FSEntry;
4541 if (fullName == fsEntry.FullName) {
4542 return fsEntry.MainTopNode.FullName;
4546 DirectoryInfo dirInfo = new DirectoryInfo (fullName);
4548 DirectoryInfo dirInfoParent = dirInfo.Parent;
4550 if (dirInfoParent != null) {
4551 FSEntry fsEntry = MWFVFS.MyComputerDevicesPrefix [dirInfoParent.FullName + "://"] as FSEntry;
4553 if (fsEntry != null) {
4554 return fsEntry.FullName;
4557 if (mainTopNode != null) {
4558 if (dirInfoParent.FullName == ThemeEngine.Current.Places (UIIcon.PlacesDesktop) &&
4559 mainTopNode.FullName == MWFVFS.DesktopPrefix) {
4560 return mainTopNode.FullName;
4562 if (dirInfoParent.FullName == ThemeEngine.Current.Places (UIIcon.PlacesPersonal) &&
4563 mainTopNode.FullName == MWFVFS.PersonalPrefix) {
4564 return mainTopNode.FullName;
4566 if (dirInfoParent.FullName == ThemeEngine.Current.Places (UIIcon.PlacesPersonal) &&
4567 mainTopNode.FullName == MWFVFS.MyComputerPersonalPrefix) {
4568 return mainTopNode.FullName;
4572 return dirInfoParent.FullName;
4579 internal bool IsImageFile()
4581 var fileExtension = Path.GetExtension(FullName);
4582 if (String.IsNullOrEmpty(fileExtension))
4584 var extension = fileExtension.ToLowerInvariant();
4585 return extension == ".bmp" ||
4586 extension == ".gif" ||
4587 extension == ".jpg" || extension == ".jpeg" ||
4588 extension == ".png" ||
4589 extension == ".tif" || extension == ".tiff";
4592 internal Image Image { get; set; }
4594 internal void SetImage()
4597 Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
4598 Bitmap myBitmap = new Bitmap(FullName);
4599 this.Image = myBitmap.GetThumbnailImage(48, 48, myCallback, IntPtr.Zero);
4600 } catch (Exception) {
4601 // cannot handle this image format? not an image file?
4606 private bool ThumbnailCallback()
4614 // Alexsantas little *nix helper
4615 internal class MasterMount
4618 internal enum FsTypes
4642 internal struct Mount
4644 public string device_or_filesystem;
4645 public string device_short;
4646 public string mount_point;
4647 public FsTypes fsType;
4650 bool proc_mount_available = false;
4652 ArrayList block_devices = new ArrayList ();
4653 ArrayList network_devices = new ArrayList ();
4654 ArrayList removable_devices = new ArrayList ();
4656 private MountComparer mountComparer = new MountComparer ();
4658 public MasterMount ()
4660 // maybe check if the current user can access /proc/mounts
4661 if (XplatUI.RunningOnUnix)
4662 if (File.Exists ("/proc/mounts"))
4663 proc_mount_available = true;
4666 public ArrayList Block_devices {
4668 return block_devices;
4672 public ArrayList Network_devices {
4674 return network_devices;
4678 public ArrayList Removable_devices {
4680 return removable_devices;
4684 public bool ProcMountAvailable {
4686 return proc_mount_available;
4690 public void GetMounts ()
4692 if (!proc_mount_available)
4695 block_devices.Clear ();
4696 network_devices.Clear ();
4697 removable_devices.Clear ();
4700 StreamReader sr = new StreamReader ("/proc/mounts");
4702 string line = sr.ReadLine ();
4704 ArrayList lines = new ArrayList ();
4705 while (line != null) {
4706 if (lines.IndexOf (line) == -1) { // Avoid duplicates
4707 ProcessProcMountLine (line);
4710 line = sr.ReadLine ();
4715 block_devices.Sort (mountComparer);
4716 network_devices.Sort (mountComparer);
4717 removable_devices.Sort (mountComparer);
4723 private void ProcessProcMountLine (string line)
4725 string[] split = line.Split (new char [] {' '});
4727 if (split != null && split.Length > 0) {
4728 Mount mount = new Mount ();
4730 if (split [0].StartsWith ("/dev/"))
4731 mount.device_short = split [0].Replace ("/dev/", String.Empty);
4733 mount.device_short = split [0];
4735 mount.device_or_filesystem = split [0];
4736 mount.mount_point = split [1];
4738 // TODO: other removable devices, floppy
4742 if (split [2] == "nfs") {
4743 mount.fsType = FsTypes.nfs;
4744 network_devices.Add (mount);
4745 } else if (split [2] == "smbfs") {
4746 mount.fsType = FsTypes.smbfs;
4747 network_devices.Add (mount);
4748 } else if (split [2] == "cifs") {
4749 mount.fsType = FsTypes.cifs;
4750 network_devices.Add (mount);
4751 } else if (split [2] == "ncpfs") {
4752 mount.fsType = FsTypes.ncpfs;
4753 network_devices.Add (mount);
4755 } else if (split [2] == "iso9660") { //cdrom
4756 mount.fsType = FsTypes.iso9660;
4757 removable_devices.Add (mount);
4758 } else if (split [2] == "usbfs") { //usb ? not tested
4759 mount.fsType = FsTypes.usbfs;
4760 removable_devices.Add (mount);
4762 } else if (split [0].StartsWith ("/")) { //block devices
4763 if (split [1].StartsWith ("/dev/")) // root static, do not add
4766 if (split [2] == "ext2")
4767 mount.fsType = FsTypes.ext2;
4768 else if (split [2] == "ext3")
4769 mount.fsType = FsTypes.ext3;
4770 else if (split [2] == "reiserfs")
4771 mount.fsType = FsTypes.reiserfs;
4772 else if (split [2] == "xfs")
4773 mount.fsType = FsTypes.xfs;
4774 else if (split [2] == "vfat")
4775 mount.fsType = FsTypes.vfat;
4776 else if (split [2] == "ntfs")
4777 mount.fsType = FsTypes.ntfs;
4778 else if (split [2] == "msdos")
4779 mount.fsType = FsTypes.msdos;
4780 else if (split [2] == "umsdos")
4781 mount.fsType = FsTypes.umsdos;
4782 else if (split [2] == "hpfs")
4783 mount.fsType = FsTypes.hpfs;
4784 else if (split [2] == "minix")
4785 mount.fsType = FsTypes.minix;
4786 else if (split [2] == "jfs")
4787 mount.fsType = FsTypes.jfs;
4789 block_devices.Add (mount);
4794 public class MountComparer : IComparer
4796 public int Compare (object mount1, object mount2)
4798 return String.Compare (((Mount)mount1).device_short, ((Mount)mount2).device_short);
4805 // easy to use class to store and read internal MWF config settings.
4806 // the config values are stored in the users home dir as a hidden xml file called "mwf_config".
4807 // currently supports int, string, byte, color and arrays (including byte arrays)
4808 // don't forget, when you read a value you still have to cast this value.
4811 // MWFConfig.SetValue ("SomeClass", "What", value);
4812 // object o = MWFConfig.GetValue ("SomeClass", "What");
4816 // string[] configFileNames = (string[])MWFConfig.GetValue ("FileDialog", "FileNames");
4817 // MWFConfig.SetValue ("FileDialog", "LastFolder", "/home/user");
4819 internal class MWFConfig
4821 private static MWFConfigInstance Instance = new MWFConfigInstance ();
4823 private static object lock_object = new object();
4825 public static object GetValue (string class_name, string value_name)
4827 lock (lock_object) {
4828 return Instance.GetValue (class_name, value_name);
4832 public static void SetValue (string class_name, string value_name, object value)
4834 lock (lock_object) {
4835 Instance.SetValue (class_name, value_name, value);
4839 public static void Flush ()
4841 lock (lock_object) {
4846 public static void RemoveClass (string class_name)
4848 lock (lock_object) {
4849 Instance.RemoveClass (class_name);
4853 public static void RemoveClassValue (string class_name, string value_name)
4855 lock (lock_object) {
4856 Instance.RemoveClassValue (class_name, value_name);
4860 public static void RemoveAllClassValues (string class_name)
4862 lock (lock_object) {
4863 Instance.RemoveAllClassValues (class_name);
4867 internal class MWFConfigInstance
4869 Hashtable classes_hashtable = new Hashtable ();
4870 static string full_file_name;
4871 static string default_file_name;
4872 readonly string configName = "MWFConfig";
4874 static MWFConfigInstance ()
4876 string b = "mwf_config";
4877 string dir = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
4879 if (XplatUI.RunningOnUnix) {
4880 dir = Path.Combine (dir, ".mono");
4882 Directory.CreateDirectory (dir);
4886 default_file_name = Path.Combine (dir, b);
4887 full_file_name = default_file_name;
4890 public MWFConfigInstance ()
4892 Open (default_file_name);
4896 public MWFConfigInstance (string filename)
4898 string path = Path.GetDirectoryName (filename);
4899 if (path == null || path == String.Empty) {
4900 path = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
4902 full_file_name = Path.Combine (path, filename);
4904 full_file_name = filename;
4906 Open (full_file_name);
4909 ~MWFConfigInstance ()
4914 public object GetValue (string class_name, string value_name)
4916 ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
4918 if (class_entry != null)
4919 return class_entry.GetValue (value_name);
4924 public void SetValue (string class_name, string value_name, object value)
4926 ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
4928 if (class_entry == null) {
4929 class_entry = new ClassEntry ();
4930 class_entry.ClassName = class_name;
4931 classes_hashtable [class_name] = class_entry;
4934 class_entry.SetValue (value_name, value);
4937 private void Open (string filename)
4940 XmlTextReader xtr = new XmlTextReader (filename);
4945 } catch (Exception) {
4949 public void Flush ()
4952 XmlTextWriter xtw = new XmlTextWriter (full_file_name, null);
4953 xtw.Formatting = Formatting.Indented;
4959 if (!XplatUI.RunningOnUnix)
4960 File.SetAttributes (full_file_name, FileAttributes.Hidden);
4961 } catch (Exception){
4965 public void RemoveClass (string class_name)
4967 ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
4969 if (class_entry != null) {
4970 class_entry.RemoveAllClassValues ();
4972 classes_hashtable.Remove (class_name);
4976 public void RemoveClassValue (string class_name, string value_name)
4978 ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
4980 if (class_entry != null) {
4981 class_entry.RemoveClassValue (value_name);
4985 public void RemoveAllClassValues (string class_name)
4987 ClassEntry class_entry = classes_hashtable [class_name] as ClassEntry;
4989 if (class_entry != null) {
4990 class_entry.RemoveAllClassValues ();
4994 private void ReadConfig (XmlTextReader xtr)
4996 if (!CheckForMWFConfig (xtr))
4999 while (xtr.Read ()) {
5000 switch (xtr.NodeType) {
5001 case XmlNodeType.Element:
5002 ClassEntry class_entry = classes_hashtable [xtr.Name] as ClassEntry;
5004 if (class_entry == null) {
5005 class_entry = new ClassEntry ();
5006 class_entry.ClassName = xtr.Name;
5007 classes_hashtable [xtr.Name] = class_entry;
5010 class_entry.ReadXml (xtr);
5016 private bool CheckForMWFConfig (XmlTextReader xtr)
5019 if (xtr.NodeType == XmlNodeType.Element) {
5020 if (xtr.Name == configName)
5028 private void WriteConfig (XmlTextWriter xtw)
5030 if (classes_hashtable.Count == 0)
5033 xtw.WriteStartElement (configName);
5034 foreach (DictionaryEntry entry in classes_hashtable) {
5035 ClassEntry class_entry = entry.Value as ClassEntry;
5037 class_entry.WriteXml (xtw);
5039 xtw.WriteEndElement ();
5042 internal class ClassEntry
5044 private Hashtable classvalues_hashtable = new Hashtable ();
5045 private string className;
5047 public string ClassName {
5057 public void SetValue (string value_name, object value)
5059 ClassValue class_value = classvalues_hashtable [value_name] as ClassValue;
5061 if (class_value == null) {
5062 class_value = new ClassValue ();
5063 class_value.Name = value_name;
5064 classvalues_hashtable [value_name] = class_value;
5067 class_value.SetValue (value);
5070 public object GetValue (string value_name)
5072 ClassValue class_value = classvalues_hashtable [value_name] as ClassValue;
5074 if (class_value == null) {
5078 return class_value.GetValue ();
5081 public void RemoveAllClassValues ()
5083 classvalues_hashtable.Clear ();
5086 public void RemoveClassValue (string value_name)
5088 ClassValue class_value = classvalues_hashtable [value_name] as ClassValue;
5090 if (class_value != null) {
5091 classvalues_hashtable.Remove (value_name);
5095 public void ReadXml (XmlTextReader xtr)
5097 while (xtr.Read ()) {
5098 switch (xtr.NodeType) {
5099 case XmlNodeType.Element:
5100 string name = xtr.GetAttribute ("name");
5102 ClassValue class_value = classvalues_hashtable [name] as ClassValue;
5104 if (class_value == null) {
5105 class_value = new ClassValue ();
5106 class_value.Name = name;
5107 classvalues_hashtable [name] = class_value;
5110 class_value.ReadXml (xtr);
5113 case XmlNodeType.EndElement:
5119 public void WriteXml (XmlTextWriter xtw)
5121 if (classvalues_hashtable.Count == 0)
5124 xtw.WriteStartElement (className);
5126 foreach (DictionaryEntry entry in classvalues_hashtable) {
5127 ClassValue class_value = entry.Value as ClassValue;
5129 class_value.WriteXml (xtw);
5132 xtw.WriteEndElement ();
5136 internal class ClassValue
5138 private object value;
5139 private string name;
5141 public string Name {
5151 public void SetValue (object value)
5155 public object GetValue ()
5160 public void ReadXml (XmlTextReader xtr)
5163 string single_value;
5165 type = xtr.GetAttribute ("type");
5167 if (type == "byte_array" || type.IndexOf ("-array") == -1) {
5168 single_value = xtr.ReadString ();
5170 if (type == "string") {
5171 value = single_value;
5173 if (type == "int") {
5174 value = Int32.Parse (single_value);
5176 if (type == "byte") {
5177 value = Byte.Parse (single_value);
5179 if (type == "color") {
5180 int color = Int32.Parse (single_value);
5181 value = Color.FromArgb (color);
5183 if (type == "byte-array") {
5184 byte[] b_array = Convert.FromBase64String (single_value);
5188 ReadXmlArrayValues (xtr, type);
5192 private void ReadXmlArrayValues (XmlTextReader xtr, string type)
5194 ArrayList al = new ArrayList ();
5196 while (xtr.Read ()) {
5197 switch (xtr.NodeType) {
5198 case XmlNodeType.Element:
5199 string single_value = xtr.ReadString ();
5201 if (type == "int-array") {
5202 int int_val = Int32.Parse (single_value);
5205 if (type == "string-array") {
5206 string str_val = single_value;
5211 case XmlNodeType.EndElement:
5212 if (xtr.Name == "value") {
5213 if (type == "int-array") {
5214 value = al.ToArray (typeof(int));
5216 if (type == "string-array") {
5217 value = al.ToArray (typeof(string));
5226 public void WriteXml (XmlTextWriter xtw)
5228 xtw.WriteStartElement ("value");
5229 xtw.WriteAttributeString ("name", name);
5230 if (value is Array) {
5231 WriteArrayContent (xtw);
5233 WriteSingleContent (xtw);
5235 xtw.WriteEndElement ();
5238 private void WriteSingleContent (XmlTextWriter xtw)
5240 string type_string = String.Empty;
5242 if (value is string)
5243 type_string = "string";
5246 type_string = "int";
5249 type_string = "byte";
5252 type_string = "color";
5254 xtw.WriteAttributeString ("type", type_string);
5257 xtw.WriteString (((Color)value).ToArgb ().ToString ());
5259 xtw.WriteString (value.ToString ());
5262 private void WriteArrayContent (XmlTextWriter xtw)
5264 string type_string = String.Empty;
5265 string type_name = String.Empty;
5267 if (value is string[]) {
5268 type_string = "string-array";
5269 type_name = "string";
5271 if (value is int[]) {
5272 type_string = "int-array";
5275 if (value is byte[]) {
5276 type_string = "byte-array";
5280 xtw.WriteAttributeString ("type", type_string);
5282 if (type_string != "byte-array") {
5283 Array array = value as Array;
5285 foreach (object o in array) {
5286 xtw.WriteStartElement (type_name);
5287 xtw.WriteString (o.ToString ());
5288 xtw.WriteEndElement ();
5291 byte[] b_array = value as byte [];
5293 xtw.WriteString (Convert.ToBase64String (b_array, 0, b_array.Length));