5
// Michael Hutchinson <mhutchinson@novell.com>
7
// Copyright (c) 2010 Novell, Inc. (http://www.novell.com)
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
28
using System.Runtime.InteropServices;
29
using System.Collections.Generic;
30
using System.Diagnostics;
32
namespace OSXIntegration.Framework
35
class NavDialog : IDisposable
39
public static NavDialog CreateChooseFileDialog (NavDialogCreationOptions options)
41
NavDialog dialog = new NavDialog ();
42
CheckReturn (NavCreateChooseFileDialog (ref options.data, IntPtr.Zero, new NavEventUPP (), new NavPreviewUPP (),
43
new NavObjectFilterUPP (), IntPtr.Zero, out dialog.ptr));
47
public static NavDialog CreateChooseFolderDialog (NavDialogCreationOptions options)
49
NavDialog dialog = new NavDialog ();
50
CheckReturn (NavCreateChooseFolderDialog (ref options.data, new NavEventUPP (),
51
new NavObjectFilterUPP (), IntPtr.Zero, out dialog.ptr));
55
public static NavDialog CreatePutFileDialog (NavDialogCreationOptions options)
57
NavDialog dialog = new NavDialog ();
58
CheckReturn (NavCreatePutFileDialog (ref options.data, new OSType (), new OSType (),
59
new NavEventUPP (), IntPtr.Zero, out dialog.ptr));
63
public static NavDialog CreateNewFolderDialog (NavDialogCreationOptions options)
65
NavDialog dialog = new NavDialog ();
66
CheckReturn (NavCreateNewFolderDialog (ref options.data, new NavEventUPP (),
67
IntPtr.Zero, out dialog.ptr));
71
public NavUserAction Run ()
74
CheckReturn (NavDialogRun (ptr));
75
return NavDialogGetUserAction (ptr);
78
public NavReplyRecordRef GetReply ()
81
var record = new NavReplyRecordRef ();
82
CheckReturn (NavDialogGetReply (ptr, out record.value));
86
public void SetLocation (string location)
89
throw new NotImplementedException ();
90
// AEDesc desc = new AEDesc ();
91
// CheckReturn (NavCustomControl (ptr, NavCustomControlMessage.SetLocation, ref desc));
96
if (ptr == IntPtr.Zero)
97
throw new ObjectDisposedException ("NavDialog");
100
public void Dispose ()
102
if (ptr != IntPtr.Zero) {
103
NavDialogDispose (ptr);
105
GC.SuppressFinalize (this);
111
Console.WriteLine ("WARNING: NavDialog not disposed");
114
[DllImport (Carbon.CarbonLib)]
115
static extern NavStatus NavCreateChooseFileDialog (ref NavDialogCreationOptionsData options, IntPtr inTypeList,
116
NavEventUPP inEventProc, NavPreviewUPP inPreviewProc,
117
NavObjectFilterUPP inFilterProc, IntPtr inClientData, out IntPtr navDialogRef);
118
//intTypeList is a NavTypeListHandle, which apparently is a pointer to NavTypeListPtr, which is a pointer to a NavTypeList
120
[DllImport (Carbon.CarbonLib)]
121
static extern NavStatus NavCreateChooseFolderDialog (ref NavDialogCreationOptionsData options,
122
NavEventUPP inEventProc, NavObjectFilterUPP inFilterProc,
123
IntPtr inClientData, out IntPtr navDialogRef);
125
[DllImport (Carbon.CarbonLib)]
126
static extern NavStatus NavCreatePutFileDialog (ref NavDialogCreationOptionsData options, OSType inFileType,
127
OSType inFileCreator, NavEventUPP inEventProc,
128
IntPtr inClientData, out IntPtr navDialogRef);
130
[DllImport (Carbon.CarbonLib)]
131
static extern NavStatus NavCreateNewFolderDialog (ref NavDialogCreationOptionsData options,
132
NavEventUPP inEventProc, IntPtr inClientData,
133
out IntPtr navDialogRef);
135
[DllImport (Carbon.CarbonLib)]
136
public static extern NavStatus NavDialogRun (IntPtr navDialogRef);
138
[DllImport (Carbon.CarbonLib)]
139
static extern NavStatus NavDialogGetReply (IntPtr navDialogRef, out NavReplyRecord outReply);
141
[DllImport (Carbon.CarbonLib)]
142
static extern NavUserAction NavDialogGetUserAction (IntPtr navDialogRef);
144
[DllImport (Carbon.CarbonLib)]
145
static extern void NavDialogDispose (IntPtr navDialogRef);
147
[DllImport (Carbon.CarbonLib)]
148
static extern NavStatus NavCustomControl (IntPtr dialog, NavCustomControlMessage selector, IntPtr parms);
150
[DllImport (Carbon.CarbonLib)]
151
static extern NavStatus NavCustomControl (IntPtr dialog, NavCustomControlMessage selector, ref AEDesc parm);
153
public static void CheckReturn (NavStatus status)
155
CheckReturn (status);
159
struct NavEventUPP { IntPtr ptr; }
160
struct NavObjectFilterUPP { IntPtr ptr; }
161
struct NavPreviewUPP { IntPtr ptr; }
163
class NavDialogCreationOptions : IDisposable
165
internal NavDialogCreationOptionsData data;
167
public static NavDialogCreationOptions NewFromDefaults ()
169
var options = new NavDialogCreationOptions ();
170
NavDialog.CheckReturn (NavGetDefaultDialogCreationOptions (out options.data));
174
[DllImport (Carbon.CarbonLib)]
175
static extern NavStatus NavGetDefaultDialogCreationOptions (out NavDialogCreationOptionsData options);
177
public NavDialogOptionFlags OptionFlags {
178
get { return data.optionFlags; }
179
set { data.optionFlags = value; }
182
public Point Location {
183
get { return data.location; }
184
set { data.location = value; }
187
public string ClientName {
188
get { return CoreFoundation.FetchString (data.clientName); }
189
set { data.clientName = AddCFString (value); }
192
public string WindowTitle {
193
get { return CoreFoundation.FetchString (data.windowTitle); }
194
set { data.windowTitle = AddCFString (value); }
197
public string ActionButtonLabel {
198
get { return CoreFoundation.FetchString (data.actionButtonLabel); }
199
set { data.actionButtonLabel = AddCFString (value); }
202
public string CancelButtonLabel {
203
get { return CoreFoundation.FetchString (data.cancelButtonLabel); }
204
set { data.cancelButtonLabel = AddCFString (value); }
207
public string SaveFileName {
208
get { return CoreFoundation.FetchString (data.saveFileName); }
209
set { data.saveFileName = AddCFString (value); }
212
public string Message {
213
get { return CoreFoundation.FetchString (data.message); }
214
set { data.message = AddCFString (value); }
217
public uint PreferenceKey {
218
get { return data.preferenceKey; }
219
set { data.preferenceKey = value; }
222
public IntPtr PopupExtension {
223
get { return data.popupExtension; }
224
set { data.popupExtension = value; }
227
public WindowModality Modality {
228
get { return data.modality; }
229
set { data.modality = value; }
232
public IntPtr ParentWindow {
233
get { return data.parentWindow; }
234
set { data.parentWindow = value; }
237
List<IntPtr> toDispose;
238
IntPtr AddCFString (string value)
240
var ptr = CoreFoundation.CreateString (value);
241
if (toDispose == null)
242
toDispose = new List<IntPtr> ();
247
public void Dispose ()
249
if (toDispose != null) {
250
foreach (IntPtr ptr in toDispose)
251
CoreFoundation.Release (ptr);
253
GC.SuppressFinalize (this);
257
~NavDialogCreationOptions ()
259
Console.WriteLine ("WARNING: Did not dispose NavDialogCreationOptions");
263
[StructLayout(LayoutKind.Sequential, Pack = 2, Size = 66)]
264
struct NavDialogCreationOptionsData
266
public ushort version;
267
public NavDialogOptionFlags optionFlags;
268
public Point location;
269
public IntPtr clientName; //CFStringRef
270
public IntPtr windowTitle; //CFStringRef
271
public IntPtr actionButtonLabel; // CFStringRef
272
public IntPtr cancelButtonLabel; // CFStringRef
273
public IntPtr saveFileName; // CFStringRef
274
public IntPtr message; // CFStringRef
275
public uint preferenceKey;
276
public IntPtr popupExtension; //CFArrayRef
277
public WindowModality modality;
278
public IntPtr parentWindow; //WindowRef
279
public char reserved; //char[16]
283
enum NavDialogOptionFlags : uint
285
Default = DontAddTranslateItems & AllowStationery & AllowPreviews & AllowMultipleFiles,
287
DontAutoTranslate = 1 << 1,
288
DontAddTranslateItems = 1 << 2,
289
AllFilesInPopup = 1 << 4,
290
AllowStationery = 1 << 5,
291
AllowPreviews = 1 << 6,
292
AllowMultipleFiles = 1 << 7,
293
AllowInvisibleFiles = 1 << 8,
294
DontResolveAliases = 1 << 9,
295
SelectDefaultLocation = 1 << 10,
296
SelectAllReadableItem = 1 << 11,
297
SupportPackages = 1 << 12,
298
AllowOpenPackages = 1 << 13,
299
DontAddRecents = 1 << 14,
300
DontUseCustomFrame = 1 << 15,
301
DontConfirmReplacement = 1 << 16,
302
PreserveSaveFileExtension = 1 << 17
305
[StructLayout(LayoutKind.Sequential, Pack = 2)]
311
public Point (short v, short h)
317
public short V { get { return v; } }
318
public short H { get { return h; } }
321
[StructLayout(LayoutKind.Sequential, Pack = 2)]
329
public short Top { get { return top; } }
330
public short Left { get { return left; } }
331
public short Bottom { get { return bottom; } }
332
public short Right { get { return right; } }
335
enum WindowModality : uint
343
enum WindowPositionMethod : uint
345
CenterOnMainScreen = 1,
346
CenterOnParentWindow = 2,
347
CenterOnParentWindowScreen = 3,
348
CascadeOnMainScreen = 4,
349
CascadeOnParentWindow = 5,
350
CascadeOnParentWindowScreen = 6,
351
CascadeStartAtParentWindowScreen = 10,
352
AlertPositionOnMainScreen = 7,
353
AlertPositionOnParentWindow = 8,
354
AlertPositionOnParentWindowScreen = 9,
360
WrongDialogStateErr = -5694,
361
WrongDialogClassErr = -5695,
362
InvalidSystemConfigErr = -5696,
363
CustomControlMessageFailedErr = -5697,
364
InvalidCustomControlMessageErr = -5698,
365
MissingKindStringErr = -5699,
368
enum NavEventCallbackMessage : int
383
OpenSelection = -2147483648, // unchecked 0x80000000
386
[StructLayout(LayoutKind.Sequential, Pack = 2, Size=254)]
390
IntPtr context; // NavDialogRef
391
IntPtr window; // WindowRef
394
NavEventData eventData;
395
NavUserAction userAction;
396
char reserved; //[218];
399
[StructLayout(LayoutKind.Sequential, Pack = 2)]
402
IntPtr eventDataParms; // NavEventDataInfo union, usually a pointer to either a EventRecord or an AEDescList
406
enum NavUserAction : uint
418
DiscardDocuments = 10
421
enum NavFilterModes : short
430
[StructLayout(LayoutKind.Sequential, Pack = 2, Size=255)]
431
struct NavReplyRecord
434
[MarshalAs(UnmanagedType.U1)]
436
[MarshalAs(UnmanagedType.U1)]
438
[MarshalAs(UnmanagedType.U1)]
440
[MarshalAs(UnmanagedType.U1)]
441
bool translationNeeded;
442
AEDesc selection; //actually an AEDescList
444
//fileTranslation is a FileTranslationSpecArrayHandle, which apparently is a pointer to a FileTranslationSpecArrayPtr,
445
//which is a pointer to a FileTranslationSpec
446
IntPtr fileTranslation;
448
IntPtr saveFileName; //CFStringRef
449
[MarshalAs(UnmanagedType.U1)]
450
bool saveFileExtensionHidden;
452
char reserved; //size [225];
455
class NavReplyRecordRef : IDisposable
458
internal NavReplyRecord value;
460
public void Dispose ()
464
NavDisposeReply (ref value);
465
GC.SuppressFinalize (this);
469
~NavReplyRecordRef ()
471
Console.WriteLine ("WARNING: NavReplyRecordRef not disposed");
474
[DllImport (Carbon.CarbonLib)]
475
static extern NavStatus NavDisposeReply (ref NavReplyRecord record);
478
enum NavCustomControlMessage : int
497
IsPreviewShowing = 17,
500
GetFirstControlID = 20,
501
SelectCustomType = 21,
503
GetEditFileName = 23,
504
SetEditFileName = 24,
505
SelectEditFileName = 25,
506
BrowserSelectAll = 26,