|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
|
2 * |
|
3 * This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #ifndef nsFilePicker_h__ |
|
8 #define nsFilePicker_h__ |
|
9 |
|
10 #include <windows.h> |
|
11 |
|
12 // For Vista IFileDialog interfaces which aren't exposed |
|
13 // unless _WIN32_WINNT >= _WIN32_WINNT_LONGHORN. |
|
14 #if _WIN32_WINNT < _WIN32_WINNT_LONGHORN |
|
15 #define _WIN32_WINNT_bak _WIN32_WINNT |
|
16 #undef _WIN32_WINNT |
|
17 #define _WIN32_WINNT _WIN32_WINNT_LONGHORN |
|
18 #define _WIN32_IE_bak _WIN32_IE |
|
19 #undef _WIN32_IE |
|
20 #define _WIN32_IE _WIN32_IE_IE70 |
|
21 #endif |
|
22 |
|
23 #include "nsIFile.h" |
|
24 #include "nsITimer.h" |
|
25 #include "nsISimpleEnumerator.h" |
|
26 #include "nsCOMArray.h" |
|
27 #include "nsAutoPtr.h" |
|
28 #include "nsBaseFilePicker.h" |
|
29 #include "nsString.h" |
|
30 #include "nsdefs.h" |
|
31 #include <commdlg.h> |
|
32 #include <shobjidl.h> |
|
33 #undef LogSeverity // SetupAPI.h #defines this as DWORD |
|
34 |
|
35 class nsILoadContext; |
|
36 |
|
37 class nsBaseWinFilePicker : |
|
38 public nsBaseFilePicker |
|
39 { |
|
40 public: |
|
41 NS_IMETHOD GetDefaultString(nsAString& aDefaultString); |
|
42 NS_IMETHOD SetDefaultString(const nsAString& aDefaultString); |
|
43 NS_IMETHOD GetDefaultExtension(nsAString& aDefaultExtension); |
|
44 NS_IMETHOD SetDefaultExtension(const nsAString& aDefaultExtension); |
|
45 |
|
46 protected: |
|
47 nsString mDefaultFilePath; |
|
48 nsString mDefaultFilename; |
|
49 nsString mDefaultExtension; |
|
50 }; |
|
51 |
|
52 /** |
|
53 * Native Windows FileSelector wrapper |
|
54 */ |
|
55 |
|
56 class nsFilePicker : |
|
57 public IFileDialogEvents, |
|
58 public nsBaseWinFilePicker |
|
59 { |
|
60 public: |
|
61 nsFilePicker(); |
|
62 virtual ~nsFilePicker(); |
|
63 |
|
64 NS_IMETHOD Init(nsIDOMWindow *aParent, const nsAString& aTitle, int16_t aMode); |
|
65 |
|
66 NS_DECL_ISUPPORTS |
|
67 |
|
68 // IUnknown's QueryInterface |
|
69 STDMETHODIMP QueryInterface(REFIID refiid, void** ppvResult); |
|
70 |
|
71 // nsIFilePicker (less what's in nsBaseFilePicker and nsBaseWinFilePicker) |
|
72 NS_IMETHOD GetFilterIndex(int32_t *aFilterIndex); |
|
73 NS_IMETHOD SetFilterIndex(int32_t aFilterIndex); |
|
74 NS_IMETHOD GetFile(nsIFile * *aFile); |
|
75 NS_IMETHOD GetFileURL(nsIURI * *aFileURL); |
|
76 NS_IMETHOD GetFiles(nsISimpleEnumerator **aFiles); |
|
77 NS_IMETHOD Show(int16_t *aReturnVal); |
|
78 NS_IMETHOD ShowW(int16_t *aReturnVal); |
|
79 NS_IMETHOD AppendFilter(const nsAString& aTitle, const nsAString& aFilter); |
|
80 |
|
81 // IFileDialogEvents |
|
82 HRESULT STDMETHODCALLTYPE OnFileOk(IFileDialog *pfd); |
|
83 HRESULT STDMETHODCALLTYPE OnFolderChanging(IFileDialog *pfd, IShellItem *psiFolder); |
|
84 HRESULT STDMETHODCALLTYPE OnFolderChange(IFileDialog *pfd); |
|
85 HRESULT STDMETHODCALLTYPE OnSelectionChange(IFileDialog *pfd); |
|
86 HRESULT STDMETHODCALLTYPE OnShareViolation(IFileDialog *pfd, IShellItem *psi, FDE_SHAREVIOLATION_RESPONSE *pResponse); |
|
87 HRESULT STDMETHODCALLTYPE OnTypeChange(IFileDialog *pfd); |
|
88 HRESULT STDMETHODCALLTYPE OnOverwrite(IFileDialog *pfd, IShellItem *psi, FDE_OVERWRITE_RESPONSE *pResponse); |
|
89 |
|
90 protected: |
|
91 enum PickerType { |
|
92 PICKER_TYPE_OPEN, |
|
93 PICKER_TYPE_SAVE, |
|
94 }; |
|
95 |
|
96 /* method from nsBaseFilePicker */ |
|
97 virtual void InitNative(nsIWidget *aParent, |
|
98 const nsAString& aTitle); |
|
99 static void GetQualifiedPath(const wchar_t *aInPath, nsString &aOutPath); |
|
100 void GetFilterListArray(nsString& aFilterList); |
|
101 bool FilePickerWrapper(OPENFILENAMEW* ofn, PickerType aType); |
|
102 bool ShowXPFolderPicker(const nsString& aInitialDir); |
|
103 bool ShowXPFilePicker(const nsString& aInitialDir); |
|
104 bool ShowFolderPicker(const nsString& aInitialDir, bool &aWasInitError); |
|
105 bool ShowFilePicker(const nsString& aInitialDir, bool &aWasInitError); |
|
106 void AppendXPFilter(const nsAString& aTitle, const nsAString& aFilter); |
|
107 void RememberLastUsedDirectory(); |
|
108 bool IsPrivacyModeEnabled(); |
|
109 bool IsDefaultPathLink(); |
|
110 bool IsDefaultPathHtml(); |
|
111 void SetDialogHandle(HWND aWnd); |
|
112 bool ClosePickerIfNeeded(bool aIsXPDialog); |
|
113 static void PickerCallbackTimerFunc(nsITimer *aTimer, void *aPicker); |
|
114 static UINT_PTR CALLBACK MultiFilePickerHook(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); |
|
115 static UINT_PTR CALLBACK FilePickerHook(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); |
|
116 |
|
117 nsCOMPtr<nsILoadContext> mLoadContext; |
|
118 nsCOMPtr<nsIWidget> mParentWidget; |
|
119 nsString mTitle; |
|
120 nsCString mFile; |
|
121 nsString mFilterList; |
|
122 int16_t mSelectedType; |
|
123 nsCOMArray<nsIFile> mFiles; |
|
124 static char mLastUsedDirectory[]; |
|
125 nsString mUnicodeFile; |
|
126 static char16_t *mLastUsedUnicodeDirectory; |
|
127 HWND mDlgWnd; |
|
128 |
|
129 class ComDlgFilterSpec |
|
130 { |
|
131 public: |
|
132 ComDlgFilterSpec() {} |
|
133 ~ComDlgFilterSpec() {} |
|
134 |
|
135 const uint32_t Length() { |
|
136 return mSpecList.Length(); |
|
137 } |
|
138 |
|
139 const bool IsEmpty() { |
|
140 return (mSpecList.Length() == 0); |
|
141 } |
|
142 |
|
143 const COMDLG_FILTERSPEC* get() { |
|
144 return mSpecList.Elements(); |
|
145 } |
|
146 |
|
147 void Append(const nsAString& aTitle, const nsAString& aFilter); |
|
148 private: |
|
149 nsAutoTArray<COMDLG_FILTERSPEC, 1> mSpecList; |
|
150 nsAutoTArray<nsString, 2> mStrings; |
|
151 }; |
|
152 |
|
153 ComDlgFilterSpec mComFilterList; |
|
154 DWORD mFDECookie; |
|
155 }; |
|
156 |
|
157 #if defined(_WIN32_WINNT_bak) |
|
158 #undef _WIN32_WINNT |
|
159 #define _WIN32_WINNT _WIN32_WINNT_bak |
|
160 #undef _WIN32_IE |
|
161 #define _WIN32_IE _WIN32_IE_bak |
|
162 #endif |
|
163 |
|
164 #endif // nsFilePicker_h__ |