added samples
[windows-sources.git] / sdk / samples / CrossTechnologySamples / VistaBridge / vistabridgelibrary / dialogs / commonsavefiledialog.cs
blob7c5e3b5cec80ac17173e38376b042bcb0a4a074a
1 using System;
2 using System.Collections.Generic;
3 using System.Diagnostics;
4 using System.Runtime.InteropServices;
5 using System.Text;
6 using Microsoft.SDK.Samples.VistaBridge.Library;
7 using Microsoft.SDK.Samples.VistaBridge.Interop;
8 using System.Security.Permissions;
10 namespace Microsoft.SDK.Samples.VistaBridge.Library
12 /// <summary>
13 /// Provides access to a Vista Common File Dialog, which allows the user
14 /// to select the filename and location for a saved file.
15 /// </summary>
16 /// <permission cref="System.Security.Permissions.FileDialogPermission">
17 /// to save a file. Associated enumeration: <see cref="System.Security.Permissions.SecurityAction.LinkDemand"/>.
18 /// </permission>
19 [FileDialogPermissionAttribute(SecurityAction.LinkDemand, Save = true)]
20 public sealed class CommonSaveFileDialog : CommonFileDialog
22 private NativeFileSaveDialog saveDialogCoClass;
24 /// <summary>
25 /// Creates a new instance of this class.
26 /// </summary>
27 public CommonSaveFileDialog() : base() { }
28 /// <summary>
29 /// Creates a new instance of this class with the specified name.
30 /// </summary>
31 /// <param name="name">The name for this dialog.</param>
32 public CommonSaveFileDialog(string name) : base(name) { }
34 #region Public API specific to Save
36 private bool overwritePrompt;
38 /// <summary>
39 /// Gets or sets a value that controls whether to prompt before
40 /// overwriting an existing file of the same name.
41 /// </summary>
42 /// <permission cref="System.InvalidOperationException">
43 /// This property cannot be changed when the dialog is showing.
44 /// </permission>
45 public bool OverwritePrompt
47 get { return overwritePrompt; }
48 set
50 ThrowIfDialogShowing("OverwritePrompt" + IllegalPropertyChangeString);
51 overwritePrompt = value;
55 private bool createPrompt;
56 /// <summary>
57 /// Gets or sets a value that controls whether to prompt for creation
58 /// if the item returned in the save dialog does not exist.
59 /// </summary>
60 /// <remarks>Note that this does not actually create the item.</remarks>
61 /// <permission cref="System.InvalidOperationException">
62 /// This property cannot be changed when the dialog is showing.
63 /// </permission>
64 public bool CreatePrompt
66 get { return createPrompt; }
67 set
69 ThrowIfDialogShowing("CreatePrompt" + IllegalPropertyChangeString);
70 createPrompt = value;
74 private bool enableMiniMode;
75 /// <summary>
76 /// Gets or sets a value that controls whether to the save dialog
77 /// displays in expanded mode.
78 /// </summary>
79 /// <remarks>Expanded mode controls whether the dialog
80 /// shows folders for browsing or hides them.</remarks>
81 /// <permission cref="System.InvalidOperationException">
82 /// This property cannot be changed when the dialog is showing.
83 /// </permission>
84 public bool EnableMiniMode
86 get { return enableMiniMode; }
87 set
89 ThrowIfDialogShowing("EnableMiniMode" + IllegalPropertyChangeString);
90 enableMiniMode = value;
94 private bool strictExtensions;
95 /// <summary>
96 /// Gets or sets a value that controls whether to ensure that the
97 /// returned file name has a file extension that matches the
98 /// currently selected file type. The dialog appends the correct
99 /// file extension if necessary.
100 /// </summary>
101 /// <permission cref="System.InvalidOperationException">
102 /// This property cannot be changed when the dialog is showing.
103 /// </permission>
104 public bool StrictExtensions
106 get { return strictExtensions; }
109 ThrowIfDialogShowing("StrictExtensions" + IllegalPropertyChangeString);
110 strictExtensions = value;
114 #endregion
116 internal override void InitializeNativeFileDialog()
118 saveDialogCoClass = new NativeFileSaveDialog();
121 internal override IFileDialog GetNativeFileDialog()
123 Debug.Assert(saveDialogCoClass != null,
124 "Must call Initialize() before fetching dialog interface");
125 return (IFileDialog)saveDialogCoClass;
128 internal override void PopulateWithFileNames(
129 System.Collections.ObjectModel.Collection<string> names)
131 IShellItem item;
132 saveDialogCoClass.GetResult(out item);
134 if (item == null)
135 throw new InvalidOperationException(
136 "Retrieved a null shell item from dialog");
137 names.Add(GetFileNameFromShellItem(item));
140 internal override void CleanUpNativeFileDialog()
142 if (saveDialogCoClass != null)
143 Marshal.ReleaseComObject(saveDialogCoClass);
146 internal override SafeNativeMethods.FOS GetDerivedOptionFlags(SafeNativeMethods.FOS flags)
148 if (overwritePrompt)
149 flags |= SafeNativeMethods.FOS.FOS_OVERWRITEPROMPT;
150 if (createPrompt)
151 flags |= SafeNativeMethods.FOS.FOS_CREATEPROMPT;
152 if (!enableMiniMode)
153 flags |= SafeNativeMethods.FOS.FOS_DEFAULTNOMINIMODE;
154 if (strictExtensions)
155 flags |= SafeNativeMethods.FOS.FOS_STRICTFILETYPES;
156 return flags;