* Filters/FilterPackage.cs, Filters/FilterRPM.cs,
[beagle.git] / Util / CompatFileChooser.cs
blob014d668de523b470f81587b3eb5ab951973cdfaa
1 //
2 // CompatFileChooser.cs - file chooser that uses GtkFileSelection or GtkFileChooser if it is available
3 //
4 // Authors:
5 // Federico Mena-Quintero <federico@ximian.com>
6 //
7 // Copyright (C) 2004 Novell, Inc.
8 //
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
27 // SOFTWARE.
30 using System;
31 using Gtk;
32 using System.Runtime.InteropServices;
34 namespace Beagle.Util {
35 public class CompatFileChooserDialog {
36 /* Public interface */
38 public enum Action {
39 Open,
40 Save,
41 SelectFolder
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);
49 if (use_file_chooser)
50 create_with_file_chooser (title, parent, action);
51 else
52 create_with_file_selection (title, parent, action);
55 public bool SelectMultiple {
56 get {
57 if (use_file_chooser)
58 return gtk_file_chooser_get_select_multiple (chooser.Handle);
59 else
60 return filesel.SelectMultiple;
63 set {
64 if (use_file_chooser)
65 gtk_file_chooser_set_select_multiple (chooser.Handle, value);
66 else
67 filesel.SelectMultiple = value;
71 public string Filename {
72 get {
73 if (use_file_chooser)
74 return gtk_file_chooser_get_filename (chooser.Handle);
75 else
76 return filesel.Filename;
79 set {
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);
87 else
88 gtk_file_chooser_set_filename (chooser.Handle, value);
89 } else
90 filesel.Filename = value;
94 public string[] Selections {
95 get {
96 if (use_file_chooser) {
97 IntPtr ptr = gtk_file_chooser_get_filenames (chooser.Handle);
98 if (ptr == IntPtr.Zero)
99 return null;
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];
107 return paths;
108 } else
109 return filesel.Selections;
113 public void Destroy ()
115 if (use_file_chooser) {
116 chooser.Destroy ();
117 } else
118 filesel.Destroy ();
121 public int Run ()
123 int response;
125 if (use_file_chooser)
126 response = chooser.Run ();
127 else
128 response = filesel.Run ();
130 return response;
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) {
141 int a = 0;
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
146 * like this:
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);
153 * ... use intval ...
155 switch (action) {
156 case Action.Open:
157 a = 0;
158 stock = Gtk.Stock.Open;
159 break;
161 case Action.Save:
162 a = 1;
163 stock = Gtk.Stock.Save;
164 break;
166 case Action.SelectFolder:
167 a = 2;
168 stock = Gtk.Stock.Open;
169 break;
172 #if false
173 IntPtr ptr = gtk_file_chooser_dialog_new_with_backend (title,
174 parent != null ? parent.Handle : IntPtr.Zero,
175 a, "gtk+", IntPtr.Zero);
176 #else
177 IntPtr ptr = gtk_file_chooser_dialog_new (title,
178 parent != null ? parent.Handle : IntPtr.Zero,
179 a, IntPtr.Zero);
180 #endif
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
201 * each special case.
203 switch (action) {
204 case Action.Open:
205 filesel.ShowFileops = false;
206 break;
208 case Action.Save:
209 filesel.FileopDelFile.Hide ();
210 filesel.FileopRenFile.Hide ();
211 break;
213 case Action.SelectFolder:
214 filesel.FileList.Parent.Hide ();
215 filesel.SelectionEntry.Hide ();
216 filesel.FileopDelFile.Hide ();
217 filesel.FileopRenFile.Hide ();
218 break;
222 /* Imports */
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);