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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.Gtk/Xwt.GtkBackend/FileDialogBackend.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
// FileDialogBackend.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 System.Collections.Generic;
 
28
using Xwt.Backends;
 
29
using System.Linq;
 
30
 
 
31
namespace Xwt.GtkBackend
 
32
{
 
33
        public class FileDialogBackend: IFileDialogBackend
 
34
        {
 
35
                Gtk.FileChooserDialog dialog;
 
36
                Gtk.FileChooserAction action;
 
37
                List<Gtk.FileFilter> gtkFilters;
 
38
                List<FileDialogFilter> filters;
 
39
 
 
40
                public FileDialogBackend (Gtk.FileChooserAction action)
 
41
                {
 
42
                        this.action = action;
 
43
                }
 
44
 
 
45
                public void InitializeBackend (object frontend)
 
46
                {
 
47
                }
 
48
                
 
49
                public void EnableEvent (object eventId)
 
50
                {
 
51
                }
 
52
                
 
53
                public void DisableEvent (object eventId)
 
54
                {
 
55
                }
 
56
                
 
57
                #region IFileDialogBackend implementation
 
58
                
 
59
                public void Initialize (bool multiselect)
 
60
                {
 
61
                        dialog = new Gtk.FileChooserDialog ("", null, action);
 
62
                        dialog.SelectMultiple = multiselect;
 
63
                        
 
64
                        switch (action) {
 
65
                                case Gtk.FileChooserAction.Open:
 
66
                                        dialog.AddButton (Gtk.Stock.Cancel, Gtk.ResponseType.Cancel);
 
67
                                        dialog.AddButton (Gtk.Stock.Open, Gtk.ResponseType.Ok);
 
68
                                        break;
 
69
                                case Gtk.FileChooserAction.SelectFolder:
 
70
                                        dialog.AddButton (Gtk.Stock.Cancel, Gtk.ResponseType.Cancel);
 
71
                                        dialog.AddButton ("Select Folder", Gtk.ResponseType.Ok);
 
72
                                        break;
 
73
                                case Gtk.FileChooserAction.Save:
 
74
                                        dialog.AddButton (Gtk.Stock.Cancel, Gtk.ResponseType.Cancel);
 
75
                                        dialog.AddButton (Gtk.Stock.Save, Gtk.ResponseType.Ok);
 
76
                                        break;
 
77
                                default:
 
78
                                        break;
 
79
                        }
 
80
                        dialog.DefaultResponse = Gtk.ResponseType.Ok;
 
81
                }
 
82
                
 
83
                public void Initialize (IEnumerable<FileDialogFilter> filters, bool multiselect, string initialFileName)
 
84
                {
 
85
                        Initialize (multiselect);
 
86
                        
 
87
                        if (!string.IsNullOrEmpty (initialFileName))
 
88
                                dialog.CurrentName = initialFileName;
 
89
                        
 
90
                        this.filters = filters.ToList ();
 
91
                        gtkFilters = new List<Gtk.FileFilter> ();
 
92
                        
 
93
                        foreach (var filter in filters) {
 
94
                                var gf = new Gtk.FileFilter ();
 
95
                                gtkFilters.Add (gf);
 
96
                                if (!string.IsNullOrEmpty (filter.Name))
 
97
                                        gf.Name = filter.Name;
 
98
                                if (filter.Patterns != null)
 
99
                                        foreach (var pattern in filter.Patterns)
 
100
                                                gf.AddPattern (pattern);
 
101
                        }
 
102
                        
 
103
                        foreach (var filter in gtkFilters)
 
104
                                dialog.AddFilter (filter);
 
105
                }
 
106
                
 
107
                public string Title {
 
108
                        get { return dialog.Title; }
 
109
                        set { dialog.Title = value; }
 
110
                }
 
111
                
 
112
                public FileDialogFilter ActiveFilter {
 
113
                        get {
 
114
                                int i = gtkFilters.IndexOf (dialog.Filter);
 
115
                                if (i != -1 && i < filters.Count)
 
116
                                        return filters [i];
 
117
                                else
 
118
                                        return null;
 
119
                        }
 
120
                        set {
 
121
                                int i = filters.IndexOf (value);
 
122
                                dialog.Filter = gtkFilters [i];
 
123
                        }
 
124
                }
 
125
                
 
126
                public bool Run (IWindowFrameBackend parent)
 
127
                {
 
128
                        var p = (WindowFrameBackend) parent;
 
129
                        int result = MessageService.RunCustomDialog (dialog, p != null ? p.Window : null);
 
130
                        return result == (int) Gtk.ResponseType.Ok;
 
131
                }
 
132
                
 
133
                public void Cleanup ()
 
134
                {
 
135
                        dialog.Destroy ();
 
136
                }
 
137
 
 
138
                public string FileName {
 
139
                        get {
 
140
                                return dialog.Filename;
 
141
                        }
 
142
                }
 
143
                
 
144
                public string[] FileNames {
 
145
                        get {
 
146
                                return dialog.Filenames;
 
147
                        }
 
148
                }
 
149
 
 
150
                public string Folder {
 
151
                        get { return dialog.Filename; }
 
152
                }
 
153
 
 
154
                public string[] Folders {
 
155
                        get { return dialog.Filenames; }
 
156
                }
 
157
 
 
158
                public string CurrentFolder {
 
159
                        get {
 
160
                                return dialog.CurrentFolder;
 
161
                        }
 
162
                        set {
 
163
                                dialog.SetCurrentFolder (value);
 
164
                        }
 
165
                }
 
166
                
 
167
                #endregion
 
168
        }
 
169
        
 
170
        public class OpenFileDialogBackend: FileDialogBackend, IOpenFileDialogBackend
 
171
        {
 
172
                public OpenFileDialogBackend (): base (Gtk.FileChooserAction.Open)
 
173
                {
 
174
                }
 
175
        }
 
176
        
 
177
        public class SaveFileDialogBackend: FileDialogBackend, ISaveFileDialogBackend
 
178
        {
 
179
                public SaveFileDialogBackend (): base (Gtk.FileChooserAction.Save)
 
180
                {
 
181
                }
 
182
        }
 
183
        
 
184
        public class SelectFolderDialogBackend: FileDialogBackend, ISelectFolderDialogBackend
 
185
        {
 
186
                public SelectFolderDialogBackend (): base (Gtk.FileChooserAction.SelectFolder)
 
187
                {
 
188
                }
 
189
        }
 
190
}