2 // CompatFileChooser.cs - file chooser that uses GtkFileSelection or GtkFileChooser if it is available
5 // Federico Mena-Quintero <federico@ximian.com>
7 // Copyright (C) 2004 Novell, Inc.
11 // Permission is hereby granted, free of charge, to any person obtaining a copy
12 // of this software and associated documentation files (the "Software"), to deal
13 // in the Software without restriction, including without limitation the rights
14 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 // copies of the Software, and to permit persons to whom the Software is
16 // furnished to do so, subject to the following conditions:
18 // The above copyright notice and this permission notice shall be included in all
19 // copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 using System
.Runtime
.InteropServices
;
34 namespace Beagle
.Util
{
35 public class CompatFileChooserDialog
{
36 /* Public interface */
44 public CompatFileChooserDialog (string title
, Gtk
.Window parent
, Action action
) {
45 string check
= Gtk
.Global
.CheckVersion (2, 4, 0);
47 use_file_chooser
= (check
== "" || check
== null);
50 create_with_file_chooser (title
, parent
, action
);
52 create_with_file_selection (title
, parent
, action
);
55 public bool SelectMultiple
{
58 return gtk_file_chooser_get_select_multiple (chooser
.Handle
);
60 return filesel
.SelectMultiple
;
65 gtk_file_chooser_set_select_multiple (chooser
.Handle
, value);
67 filesel
.SelectMultiple
= value;
71 public string Filename
{
74 return gtk_file_chooser_get_filename (chooser
.Handle
);
76 return filesel
.Filename
;
80 if (use_file_chooser
) {
81 // FIXME: This returns a boolean success code.
82 // We can't do much if it fails (e.g. when the
83 // file does not exist), so do we need to
84 // actually check the return value?
85 if (System
.IO
.Directory
.Exists (value))
86 gtk_file_chooser_set_current_folder (chooser
.Handle
, value);
88 gtk_file_chooser_set_filename (chooser
.Handle
, value);
90 filesel
.Filename
= value;
94 public string[] Selections
{
96 if (use_file_chooser
) {
97 IntPtr ptr
= gtk_file_chooser_get_filenames (chooser
.Handle
);
98 if (ptr
== IntPtr
.Zero
)
101 GLib
.SList slist
= new GLib
.SList (ptr
, typeof (string));
103 string [] paths
= new string [slist
.Count
];
104 for (int i
= 0; i
< slist
.Count
; i
++)
105 paths
[i
] = (string) slist
[i
];
109 return filesel
.Selections
;
113 public void Destroy ()
115 if (use_file_chooser
) {
125 if (use_file_chooser
)
126 response
= chooser
.Run ();
128 response
= filesel
.Run ();
133 /* Private implementation */
135 private bool use_file_chooser
;
137 private Gtk
.FileSelection filesel
;
138 private Gtk
.Dialog chooser
;
140 private void create_with_file_chooser (string title
, Gtk
.Window parent
, Action action
) {
142 string stock
= Gtk
.Stock
.Open
;
144 /* FIXME: here we use the raw enum values from
145 * GtkFileChooserAction, because I'm too lazy to do something
148 * GType t = gtk_file_chooser_action_get_type ();
149 * GEnumClass *c = g_type_class_ref (t);
150 * GEnumValue *v = g_enum_get_value_by_name (c, "GTK_FILE_CHOOSER_ACTION_FOO");
151 * int intval = v->value;
152 * g_type_class_unref (c);
158 stock
= Gtk
.Stock
.Open
;
163 stock
= Gtk
.Stock
.Save
;
166 case Action
.SelectFolder
:
168 stock
= Gtk
.Stock
.Open
;
173 IntPtr ptr
= gtk_file_chooser_dialog_new_with_backend (title
,
174 parent
!= null ? parent
.Handle
: IntPtr
.Zero
,
175 a
, "gtk+", IntPtr
.Zero
);
177 IntPtr ptr
= gtk_file_chooser_dialog_new (title
,
178 parent
!= null ? parent
.Handle
: IntPtr
.Zero
,
182 chooser
= GLib
.Object
.GetObject (ptr
, false) as Gtk
.Dialog
;
184 chooser
.AddButton (Gtk
.Stock
.Cancel
, Gtk
.ResponseType
.Cancel
);
186 /* Note that we use Ok rather than the preferred Accept because
187 * that's what GtkFileSelection uses. Rather than have two
188 * separate tests for each case (chooser/filesel), we'll rather
189 * just test for a single response code, which is Ok.
191 chooser
.AddButton (stock
, Gtk
.ResponseType
.Ok
);
192 chooser
.DefaultResponse
= Gtk
.ResponseType
.Ok
;
195 private void create_with_file_selection (string title
, Gtk
.Window parent
, Action action
) {
196 filesel
= new Gtk
.FileSelection (title
);
197 filesel
.TransientFor
= parent
;
199 /* We try to present as similar a UI as possible with both file
200 * selection widgets, so we frob the file operation buttons for
205 filesel
.ShowFileops
= false;
209 filesel
.FileopDelFile
.Hide ();
210 filesel
.FileopRenFile
.Hide ();
213 case Action
.SelectFolder
:
214 filesel
.FileList
.Parent
.Hide ();
215 filesel
.SelectionEntry
.Hide ();
216 filesel
.FileopDelFile
.Hide ();
217 filesel
.FileopRenFile
.Hide ();
224 [DllImport ("libgtk-x11-2.0.so.0")]
225 extern static IntPtr
gtk_file_chooser_dialog_new (string title
, IntPtr parent
, int action
, IntPtr varargs
);
227 //[DllImport ("libgtk-x11-2.0.so.0")]
228 //extern static IntPtr gtk_file_chooser_dialog_new_with_backend (string title, IntPtr parent, int action, string backend, IntPtr varargs);
230 [DllImport ("libgtk-x11-2.0.so.0")]
231 extern static string gtk_file_chooser_get_filename (IntPtr handle
);
233 [DllImport ("libgtk-x11-2.0.so.0")]
234 extern static int gtk_file_chooser_set_filename (IntPtr handle
, string filename
);
236 [DllImport ("libgtk-x11-2.0.so.0")]
237 extern static int gtk_file_chooser_set_current_folder (IntPtr handle
, string filename
);
239 [DllImport ("libgtk-x11-2.0.so.0")]
240 extern static IntPtr
gtk_file_chooser_get_filenames (IntPtr handle
);
242 [DllImport ("libgtk-x11-2.0.so.0")]
243 extern static bool gtk_file_chooser_get_select_multiple (IntPtr handle
);
245 [DllImport ("libgtk-x11-2.0.so.0")]
246 extern static void gtk_file_chooser_set_select_multiple (IntPtr handle
, bool multi
);