~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/xwt/Xwt/Xwt/FileDialog.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// FileDialog.cs
 
3
//  
 
4
// Author:
 
5
//       Lluis Sanchez <lluis@xamarin.com>
 
6
// 
 
7
// Copyright (c) 2012 Xamarin Inc
 
8
// 
 
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:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
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
 
25
// THE SOFTWARE.
 
26
using System;
 
27
using Xwt.Backends;
 
28
using System.ComponentModel;
 
29
using Xwt.Engine;
 
30
 
 
31
namespace Xwt
 
32
{
 
33
        public class FileDialog: Component
 
34
        {
 
35
                FileDialogFilterCollection filters;
 
36
                bool running;
 
37
                bool multiselect;
 
38
                string initialFileName;
 
39
                FileDialogFilter activeFilter;
 
40
                string currentFolder;
 
41
                string title = "";
 
42
                string fileName;
 
43
                string[] fileNames = new string[0];
 
44
                
 
45
                BackendHost<FileDialog,IFileDialogBackend> backendHost;
 
46
                
 
47
                internal FileDialog ()
 
48
                {
 
49
                        filters = new FileDialogFilterCollection (AddRemoveItem);
 
50
                        backendHost = new BackendHost<FileDialog,IFileDialogBackend> ();
 
51
                        backendHost.Parent = this;
 
52
                }
 
53
 
 
54
                internal FileDialog (string title): this ()
 
55
                {
 
56
                        this.title = title;
 
57
                }
 
58
 
 
59
                IFileDialogBackend Backend {
 
60
                        get { return backendHost.Backend; }
 
61
                }
 
62
                
 
63
                void AddRemoveItem (FileDialogFilter filter, bool added)
 
64
                {
 
65
                        CheckNotRunning ();
 
66
                }
 
67
                
 
68
                public string Title {
 
69
                        get {
 
70
                                return title ?? "";
 
71
                        }
 
72
                        set {
 
73
                                title = value ?? "";
 
74
                                if (running)
 
75
                                        Backend.Title = title;
 
76
                        }
 
77
                }
 
78
                
 
79
                /// <summary>
 
80
                /// Gets the name of the file that the user has selected in the dialog
 
81
                /// </summary>
 
82
                /// <value>
 
83
                /// The name of the file, or null if no selection was made
 
84
                /// </value>
 
85
                public string FileName {
 
86
                        get { return running ? Backend.FileName : fileName; }
 
87
                }
 
88
                
 
89
                /// <summary>
 
90
                /// Gets the files the the user has selected in the dialog
 
91
                /// </summary>
 
92
                /// <value>
 
93
                /// The names of the files
 
94
                /// </value>
 
95
                public string[] FileNames {
 
96
                        get {
 
97
                                return running ? Backend.FileNames : fileNames;
 
98
                        }
 
99
                }
 
100
                
 
101
                /// <summary>
 
102
                /// Gets or sets the current folder.
 
103
                /// </summary>
 
104
                /// <value>
 
105
                /// The current folder.
 
106
                /// </value>
 
107
                public string CurrentFolder {
 
108
                        get {
 
109
                                return running ? Backend.CurrentFolder : currentFolder;
 
110
                        }
 
111
                        set {
 
112
                                if (running)
 
113
                                        Backend.CurrentFolder = value;
 
114
                                else
 
115
                                        currentFolder = value;
 
116
                        }
 
117
                }
 
118
                
 
119
                /// <summary>
 
120
                /// Gets or sets a value indicating whether the user can select multiple files
 
121
                /// </summary>
 
122
                /// <value>
 
123
                /// <c>true</c> if multiselection is allowed; otherwise, <c>false</c>.
 
124
                /// </value>
 
125
                public bool Multiselect {
 
126
                        get { return multiselect; }
 
127
                        set { CheckNotRunning (); multiselect = value; }
 
128
                }
 
129
 
 
130
                /// <summary>
 
131
                /// File name to show by default.
 
132
                /// </summary>
 
133
                public string InitialFileName {
 
134
                        get { return initialFileName; }
 
135
                        set { CheckNotRunning (); initialFileName = value; }
 
136
                }
 
137
 
 
138
                /// <summary>
 
139
                /// Filters that allow the user to chose the kinds of files the dialog displays.
 
140
                /// </summary>
 
141
                public FileDialogFilterCollection Filters {
 
142
                        get { return filters; }
 
143
                }
 
144
 
 
145
                /// <summary>
 
146
                /// The filter currently selected in the file dialog
 
147
                /// </summary>
 
148
                public FileDialogFilter ActiveFilter {
 
149
                        get { return running ? Backend.ActiveFilter : activeFilter; }
 
150
                        set {
 
151
                                if (!filters.Contains (value))
 
152
                                        throw new ArgumentException ("The active filter must be one of the filters included in the Filters collection");
 
153
                                if (running)
 
154
                                        Backend.ActiveFilter = value;
 
155
                                else
 
156
                                        activeFilter = value;
 
157
                        }
 
158
                }
 
159
                
 
160
                void CheckNotRunning ()
 
161
                {
 
162
                        if (running)
 
163
                                throw new InvalidOperationException ("Options can't be modified when the dialog is running");
 
164
                }
 
165
 
 
166
 
 
167
                /// <summary>
 
168
                /// Shows the dialog.
 
169
                /// </summary>
 
170
                public bool Run ()
 
171
                {
 
172
                        return Run (null);
 
173
                }
 
174
 
 
175
                /// <summary>
 
176
                /// Shows the dialog.
 
177
                /// </summary>
 
178
                public bool Run (WindowFrame parentWindow)
 
179
                {
 
180
                        try {
 
181
                                running = true;
 
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));
 
190
                        } finally {
 
191
                                currentFolder = Backend.CurrentFolder;
 
192
                                activeFilter = Backend.ActiveFilter;
 
193
                                fileName = Backend.FileName;
 
194
                                fileNames = Backend.FileNames; 
 
195
                                currentFolder = Backend.CurrentFolder;
 
196
                                running = false;
 
197
                                Backend.Cleanup ();
 
198
                        }
 
199
                }
 
200
        }
 
201
}
 
202