2
// FileDialogBackend.cs
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
27
using System.Collections.Generic;
31
namespace Xwt.GtkBackend
33
public class FileDialogBackend: IFileDialogBackend
35
Gtk.FileChooserDialog dialog;
36
Gtk.FileChooserAction action;
37
List<Gtk.FileFilter> gtkFilters;
38
List<FileDialogFilter> filters;
40
public FileDialogBackend (Gtk.FileChooserAction action)
45
public void InitializeBackend (object frontend)
49
public void EnableEvent (object eventId)
53
public void DisableEvent (object eventId)
57
#region IFileDialogBackend implementation
59
public void Initialize (bool multiselect)
61
dialog = new Gtk.FileChooserDialog ("", null, action);
62
dialog.SelectMultiple = multiselect;
65
case Gtk.FileChooserAction.Open:
66
dialog.AddButton (Gtk.Stock.Cancel, Gtk.ResponseType.Cancel);
67
dialog.AddButton (Gtk.Stock.Open, Gtk.ResponseType.Ok);
69
case Gtk.FileChooserAction.SelectFolder:
70
dialog.AddButton (Gtk.Stock.Cancel, Gtk.ResponseType.Cancel);
71
dialog.AddButton ("Select Folder", Gtk.ResponseType.Ok);
73
case Gtk.FileChooserAction.Save:
74
dialog.AddButton (Gtk.Stock.Cancel, Gtk.ResponseType.Cancel);
75
dialog.AddButton (Gtk.Stock.Save, Gtk.ResponseType.Ok);
80
dialog.DefaultResponse = Gtk.ResponseType.Ok;
83
public void Initialize (IEnumerable<FileDialogFilter> filters, bool multiselect, string initialFileName)
85
Initialize (multiselect);
87
if (!string.IsNullOrEmpty (initialFileName))
88
dialog.CurrentName = initialFileName;
90
this.filters = filters.ToList ();
91
gtkFilters = new List<Gtk.FileFilter> ();
93
foreach (var filter in filters) {
94
var gf = new Gtk.FileFilter ();
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);
103
foreach (var filter in gtkFilters)
104
dialog.AddFilter (filter);
107
public string Title {
108
get { return dialog.Title; }
109
set { dialog.Title = value; }
112
public FileDialogFilter ActiveFilter {
114
int i = gtkFilters.IndexOf (dialog.Filter);
115
if (i != -1 && i < filters.Count)
121
int i = filters.IndexOf (value);
122
dialog.Filter = gtkFilters [i];
126
public bool Run (IWindowFrameBackend parent)
128
var p = (WindowFrameBackend) parent;
129
int result = MessageService.RunCustomDialog (dialog, p != null ? p.Window : null);
130
return result == (int) Gtk.ResponseType.Ok;
133
public void Cleanup ()
138
public string FileName {
140
return dialog.Filename;
144
public string[] FileNames {
146
return dialog.Filenames;
150
public string Folder {
151
get { return dialog.Filename; }
154
public string[] Folders {
155
get { return dialog.Filenames; }
158
public string CurrentFolder {
160
return dialog.CurrentFolder;
163
dialog.SetCurrentFolder (value);
170
public class OpenFileDialogBackend: FileDialogBackend, IOpenFileDialogBackend
172
public OpenFileDialogBackend (): base (Gtk.FileChooserAction.Open)
177
public class SaveFileDialogBackend: FileDialogBackend, ISaveFileDialogBackend
179
public SaveFileDialogBackend (): base (Gtk.FileChooserAction.Save)
184
public class SelectFolderDialogBackend: FileDialogBackend, ISelectFolderDialogBackend
186
public SelectFolderDialogBackend (): base (Gtk.FileChooserAction.SelectFolder)