5
// Lluis Sanchez <lluis@xamarin.com>
7
// Copyright (c) 2012 Xamarin Inc
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28
using System.ComponentModel;
33
public class FileDialog: Component
35
FileDialogFilterCollection filters;
38
string initialFileName;
39
FileDialogFilter activeFilter;
43
string[] fileNames = new string[0];
45
BackendHost<FileDialog,IFileDialogBackend> backendHost;
47
internal FileDialog ()
49
filters = new FileDialogFilterCollection (AddRemoveItem);
50
backendHost = new BackendHost<FileDialog,IFileDialogBackend> ();
51
backendHost.Parent = this;
54
internal FileDialog (string title): this ()
59
IFileDialogBackend Backend {
60
get { return backendHost.Backend; }
63
void AddRemoveItem (FileDialogFilter filter, bool added)
75
Backend.Title = title;
80
/// Gets the name of the file that the user has selected in the dialog
83
/// The name of the file, or null if no selection was made
85
public string FileName {
86
get { return running ? Backend.FileName : fileName; }
90
/// Gets the files the the user has selected in the dialog
93
/// The names of the files
95
public string[] FileNames {
97
return running ? Backend.FileNames : fileNames;
102
/// Gets or sets the current folder.
105
/// The current folder.
107
public string CurrentFolder {
109
return running ? Backend.CurrentFolder : currentFolder;
113
Backend.CurrentFolder = value;
115
currentFolder = value;
120
/// Gets or sets a value indicating whether the user can select multiple files
123
/// <c>true</c> if multiselection is allowed; otherwise, <c>false</c>.
125
public bool Multiselect {
126
get { return multiselect; }
127
set { CheckNotRunning (); multiselect = value; }
131
/// File name to show by default.
133
public string InitialFileName {
134
get { return initialFileName; }
135
set { CheckNotRunning (); initialFileName = value; }
139
/// Filters that allow the user to chose the kinds of files the dialog displays.
141
public FileDialogFilterCollection Filters {
142
get { return filters; }
146
/// The filter currently selected in the file dialog
148
public FileDialogFilter ActiveFilter {
149
get { return running ? Backend.ActiveFilter : activeFilter; }
151
if (!filters.Contains (value))
152
throw new ArgumentException ("The active filter must be one of the filters included in the Filters collection");
154
Backend.ActiveFilter = value;
156
activeFilter = value;
160
void CheckNotRunning ()
163
throw new InvalidOperationException ("Options can't be modified when the dialog is running");
168
/// Shows the dialog.
176
/// Shows the dialog.
178
public bool Run (WindowFrame parentWindow)
182
Backend.Initialize (filters, multiselect, initialFileName);
183
if (!string.IsNullOrEmpty (currentFolder))
184
Backend.CurrentFolder = currentFolder;
185
if (activeFilter != null)
186
Backend.ActiveFilter = activeFilter;
187
if (!string.IsNullOrEmpty (title))
188
Backend.Title = title;
189
return Backend.Run ((IWindowFrameBackend)WidgetRegistry.GetBackend (parentWindow));
191
currentFolder = Backend.CurrentFolder;
192
activeFilter = Backend.ActiveFilter;
193
fileName = Backend.FileName;
194
fileNames = Backend.FileNames;
195
currentFolder = Backend.CurrentFolder;