blob: c5ba648abbdd16bb976c21e54824db643c180dd6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
using System;
using System.Collections.Generic;
using System.Text;
using MonoDevelop.Components.Extensions;
using System.Windows.Forms;
using MonoDevelop.Core;
namespace MonoDevelop.Platform
{
class SelectFileDialogHandler : ISelectFileDialogHandler
{
public bool Run(SelectFileDialogData data)
{
CommonDialog dlg = null;
if (data.Action == Gtk.FileChooserAction.Open)
dlg = new OpenFileDialog();
else if (data.Action == Gtk.FileChooserAction.Save)
dlg = new SaveFileDialog();
else if (data.Action == Gtk.FileChooserAction.SelectFolder)
dlg = new FolderBrowserDialog ();
if (dlg is FileDialog)
SetCommonFormProperties (data, dlg as FileDialog);
else
SetFolderBrowserProperties (data, dlg as FolderBrowserDialog);
using (dlg) {
WinFormsRoot root = new WinFormsRoot();
if (dlg.ShowDialog(root) == DialogResult.Cancel)
return false;
if (dlg is FileDialog) {
var fileDlg = dlg as OpenFileDialog;
FilePath[] paths = new FilePath [fileDlg.FileNames.Length];
for (int n=0; n < fileDlg.FileNames.Length; n++)
paths [n] = fileDlg.FileNames [n];
data.SelectedFiles = paths;
} else {
var folderDlg = dlg as FolderBrowserDialog;
data.SelectedFiles = new [] { new FilePath (folderDlg.SelectedPath) };
}
return true;
}
}
internal static void SetCommonFormProperties (SelectFileDialogData data, FileDialog dialog)
{
if (!string.IsNullOrEmpty (data.Title))
dialog.Title = data.Title;
dialog.AddExtension = true;
dialog.Filter = GetFilterFromData (data.Filters);
dialog.FilterIndex = data.DefaultFilter == null ? 0 : data.Filters.IndexOf (data.DefaultFilter);
dialog.InitialDirectory = data.CurrentFolder;
if (!string.IsNullOrEmpty (data.InitialFileName))
dialog.FileName = data.InitialFileName;
OpenFileDialog openDialog = dialog as OpenFileDialog;
if (openDialog != null)
openDialog.Multiselect = data.SelectMultiple;
}
static void SetFolderBrowserProperties (SelectFileDialogData data, FolderBrowserDialog dialog)
{
if (!string.IsNullOrEmpty (data.Title))
dialog.Description = data.Title;
dialog.SelectedPath = data.CurrentFolder;
}
static string GetFilterFromData (IList<SelectFileDialogFilter> filters)
{
if (filters == null || filters.Count == 0)
return null;
var sb = new StringBuilder ();
foreach (var f in filters) {
if (sb.Length > 0)
sb.Append ('|');
sb.Append (f.Name);
sb.Append ('|');
for (int i = 0; i < f.Patterns.Count; i++) {
if (i > 0)
sb.Append (';');
sb.Append (f.Patterns [i]);
}
}
return sb.ToString ();
}
}
}
|