110ce8018SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
310ce8018SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
410ce8018SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
510ce8018SAndrew Rist  * distributed with this work for additional information
610ce8018SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
710ce8018SAndrew Rist  * to you under the Apache License, Version 2.0 (the
810ce8018SAndrew Rist  * "License"); you may not use this file except in compliance
910ce8018SAndrew Rist  * with the License.  You may obtain a copy of the License at
1010ce8018SAndrew Rist  *
1110ce8018SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1210ce8018SAndrew Rist  *
1310ce8018SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1410ce8018SAndrew Rist  * software distributed under the License is distributed on an
1510ce8018SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1610ce8018SAndrew Rist  * KIND, either express or implied.  See the License for the
1710ce8018SAndrew Rist  * specific language governing permissions and limitations
1810ce8018SAndrew Rist  * under the License.
1910ce8018SAndrew Rist  *
2010ce8018SAndrew Rist  *************************************************************/
2110ce8018SAndrew Rist 
2210ce8018SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef _FILEOPENDLG_HXX_
25cdf0e10cSrcweir #define _FILEOPENDLG_HXX_
26cdf0e10cSrcweir 
27cdf0e10cSrcweir //------------------------------------------------------------------------
28cdf0e10cSrcweir // includes
29cdf0e10cSrcweir //------------------------------------------------------------------------
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <sal/types.h>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #ifndef _RTL_USTRING_HXX_
34cdf0e10cSrcweir #include <rtl/ustring>
35cdf0e10cSrcweir #endif
36cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #include "platform_xp.h"
39cdf0e10cSrcweir #include "getfilenamewrapper.hxx"
40cdf0e10cSrcweir 
41cdf0e10cSrcweir // because we don't want to import the new W2k platform skd
42cdf0e10cSrcweir // into our build environment if have stolen the definition
43cdf0e10cSrcweir // for the new OPENFILENAME structure from the new headers
44cdf0e10cSrcweir 
45cdf0e10cSrcweir #ifndef _CDSIZEOF_STRUCT
46cdf0e10cSrcweir #define _CDSIZEOF_STRUCT(structname, member)  (((int)((LPBYTE)(&((structname*)0)->member) - ((LPBYTE)((structname*)0)))) + sizeof(((structname*)0)->member))
47cdf0e10cSrcweir #endif
48cdf0e10cSrcweir 
49cdf0e10cSrcweir typedef struct _tagOFNA {
50cdf0e10cSrcweir    DWORD        lStructSize;
51cdf0e10cSrcweir    HWND         hwndOwner;
52cdf0e10cSrcweir    HINSTANCE    hInstance;
53cdf0e10cSrcweir    LPCSTR       lpstrFilter;
54cdf0e10cSrcweir    LPSTR        lpstrCustomFilter;
55cdf0e10cSrcweir    DWORD        nMaxCustFilter;
56cdf0e10cSrcweir    DWORD        nFilterIndex;
57cdf0e10cSrcweir    LPSTR        lpstrFile;
58cdf0e10cSrcweir    DWORD        nMaxFile;
59cdf0e10cSrcweir    LPSTR        lpstrFileTitle;
60cdf0e10cSrcweir    DWORD        nMaxFileTitle;
61cdf0e10cSrcweir    LPCSTR       lpstrInitialDir;
62cdf0e10cSrcweir    LPCSTR       lpstrTitle;
63cdf0e10cSrcweir    DWORD        Flags;
64cdf0e10cSrcweir    WORD         nFileOffset;
65cdf0e10cSrcweir    WORD         nFileExtension;
66cdf0e10cSrcweir    LPCSTR       lpstrDefExt;
67cdf0e10cSrcweir    LPARAM       lCustData;
68cdf0e10cSrcweir    LPOFNHOOKPROC lpfnHook;
69cdf0e10cSrcweir    LPCSTR       lpTemplateName;
70cdf0e10cSrcweir #ifdef _MAC
71cdf0e10cSrcweir    LPEDITMENU   lpEditInfo;
72cdf0e10cSrcweir    LPCSTR       lpstrPrompt;
73cdf0e10cSrcweir #endif
74cdf0e10cSrcweir #if (_WIN32_WINNT >= 0x0500)
75cdf0e10cSrcweir    void *		pvReserved;
76cdf0e10cSrcweir    DWORD        dwReserved;
77cdf0e10cSrcweir    DWORD        FlagsEx;
78cdf0e10cSrcweir #endif // (_WIN32_WINNT >= 0x0500)
79cdf0e10cSrcweir } _OPENFILENAMEA, *_LPOPENFILENAMEA;
80cdf0e10cSrcweir 
81cdf0e10cSrcweir typedef struct _tagOFNW {
82cdf0e10cSrcweir    DWORD        lStructSize;
83cdf0e10cSrcweir    HWND         hwndOwner;
84cdf0e10cSrcweir    HINSTANCE    hInstance;
85cdf0e10cSrcweir    LPCWSTR      lpstrFilter;
86cdf0e10cSrcweir    LPWSTR       lpstrCustomFilter;
87cdf0e10cSrcweir    DWORD        nMaxCustFilter;
88cdf0e10cSrcweir    DWORD        nFilterIndex;
89cdf0e10cSrcweir    LPWSTR       lpstrFile;
90cdf0e10cSrcweir    DWORD        nMaxFile;
91cdf0e10cSrcweir    LPWSTR       lpstrFileTitle;
92cdf0e10cSrcweir    DWORD        nMaxFileTitle;
93cdf0e10cSrcweir    LPCWSTR      lpstrInitialDir;
94cdf0e10cSrcweir    LPCWSTR      lpstrTitle;
95cdf0e10cSrcweir    DWORD        Flags;
96cdf0e10cSrcweir    WORD         nFileOffset;
97cdf0e10cSrcweir    WORD         nFileExtension;
98cdf0e10cSrcweir    LPCWSTR      lpstrDefExt;
99cdf0e10cSrcweir    LPARAM       lCustData;
100cdf0e10cSrcweir    LPOFNHOOKPROC lpfnHook;
101cdf0e10cSrcweir    LPCWSTR      lpTemplateName;
102cdf0e10cSrcweir #if (_WIN32_WINNT >= 0x0500)
103cdf0e10cSrcweir    void *       pvReserved;
104cdf0e10cSrcweir    DWORD        dwReserved;
105cdf0e10cSrcweir    DWORD        FlagsEx;
106cdf0e10cSrcweir #endif // (_WIN32_WINNT >= 0x0500)
107cdf0e10cSrcweir } _OPENFILENAMEW, *_LPOPENFILENAMEW;
108cdf0e10cSrcweir 
109cdf0e10cSrcweir #ifdef UNICODE
110cdf0e10cSrcweir typedef _OPENFILENAMEW _OPENFILENAME;
111cdf0e10cSrcweir typedef _LPOPENFILENAMEW _LPOPENFILENAME;
112cdf0e10cSrcweir #else
113cdf0e10cSrcweir typedef _OPENFILENAMEA _OPENFILENAME;
114cdf0e10cSrcweir typedef _LPOPENFILENAMEA _LPOPENFILENAME;
115cdf0e10cSrcweir #endif // UNICODE
116cdf0e10cSrcweir 
117cdf0e10cSrcweir #if (_WIN32_WINNT >= 0x0500)
118cdf0e10cSrcweir     #define _OPENFILENAME_SIZE_VERSION_400A  _CDSIZEOF_STRUCT(_OPENFILENAMEA,lpTemplateName)
119cdf0e10cSrcweir     #define _OPENFILENAME_SIZE_VERSION_400W  _CDSIZEOF_STRUCT(_OPENFILENAMEW,lpTemplateName)
120cdf0e10cSrcweir     #ifdef UNICODE
121cdf0e10cSrcweir         #define _OPENFILENAME_SIZE_VERSION_400  _OPENFILENAME_SIZE_VERSION_400W
122cdf0e10cSrcweir     #else
123cdf0e10cSrcweir         #define _OPENFILENAME_SIZE_VERSION_400  _OPENFILENAME_SIZE_VERSION_400A
124cdf0e10cSrcweir     #endif // !UNICODE
125cdf0e10cSrcweir #else
126*796b7e2aSmseidel     #error _WIN32_WINNT seems not to be valid.
127cdf0e10cSrcweir #endif // (_WIN32_WINNT >= 0x0500)
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 
130cdf0e10cSrcweir //-------------------------------------------------------------
131cdf0e10cSrcweir // A simple wrapper class around the Win32 GetOpenFileName API.
132cdf0e10cSrcweir // This class is not thread-safe and only one instance at a
133cdf0e10cSrcweir // time is allowed
134cdf0e10cSrcweir //-------------------------------------------------------------
135cdf0e10cSrcweir 
136cdf0e10cSrcweir class CFileOpenDialog
137cdf0e10cSrcweir {
138cdf0e10cSrcweir public:
139cdf0e10cSrcweir 	// ctor
140cdf0e10cSrcweir 	// bFileOpenDialog idicates if we want a FileOpen or FileSave
141cdf0e10cSrcweir 	// dialog
142cdf0e10cSrcweir 	// dwFlags see OPENFILENAME
143cdf0e10cSrcweir 	// dwTemplateId - an ID for custom templates
144cdf0e10cSrcweir 	// hInstance    - an instance handle for the module
145cdf0e10cSrcweir 	// which provides the custom template, unused if dwTemplateId
146cdf0e10cSrcweir 	// is 0
147cdf0e10cSrcweir 	CFileOpenDialog(
148cdf0e10cSrcweir 		bool bFileOpenDialog = sal_True,
149cdf0e10cSrcweir 		sal_uInt32 dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
150cdf0e10cSrcweir 		sal_uInt32 dwTemplateId = 0,
151cdf0e10cSrcweir 		HINSTANCE hInstance = 0);
152cdf0e10cSrcweir 
153cdf0e10cSrcweir 	virtual ~CFileOpenDialog();
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 	virtual void SAL_CALL setTitle(const rtl::OUString& aTitle);
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 	// to set a filter string using the M$ format
158cdf0e10cSrcweir 	// e.g. FltName\0*.txt;*.rtf\0...\0\0
159cdf0e10cSrcweir 	void SAL_CALL setFilter(const rtl::OUString& aFilter);
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 	// set the index of the current filter when the
162cdf0e10cSrcweir 	// dialog is about to shown, the index starts with 1
163cdf0e10cSrcweir 	// the function succeeded if the given filter index
164cdf0e10cSrcweir 	// is greater than zero and is a valid position
165cdf0e10cSrcweir 	// within filter string that was previously set
166cdf0e10cSrcweir 	bool SAL_CALL setFilterIndex(sal_uInt32 aIndex);
167cdf0e10cSrcweir 
168cdf0e10cSrcweir 	// get the index of the currently selected filter
169cdf0e10cSrcweir 	// the index of the returned filter starts with 1
170cdf0e10cSrcweir 	sal_uInt32 SAL_CALL getSelectedFilterIndex() const;
171cdf0e10cSrcweir 
172cdf0e10cSrcweir 	// set the name and optional the path of the
173cdf0e10cSrcweir 	// file that will be initially be shown when
174cdf0e10cSrcweir 	// the dialog will be displayed
175cdf0e10cSrcweir 	virtual void SAL_CALL setDefaultName(const rtl::OUString& aName);
176cdf0e10cSrcweir 
177cdf0e10cSrcweir 	// set the initial directory
178cdf0e10cSrcweir 	virtual void SAL_CALL setDisplayDirectory(const rtl::OUString& aDirectory);
179cdf0e10cSrcweir 
180cdf0e10cSrcweir 	// returns only the path of the selected file
181cdf0e10cSrcweir 	virtual rtl::OUString SAL_CALL getLastDisplayDirectory() const;
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 	// returns the full file name including drive letter, path
184cdf0e10cSrcweir 	// file name and file extension
185cdf0e10cSrcweir 	virtual rtl::OUString SAL_CALL getFullFileName() const;
186cdf0e10cSrcweir 
187cdf0e10cSrcweir 	// returns the file name and the file extension without
188cdf0e10cSrcweir 	// drive letter and path
189cdf0e10cSrcweir 	rtl::OUString SAL_CALL getFileName() const;
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 	// returns the file extension of the selected file
192cdf0e10cSrcweir 	rtl::OUString SAL_CALL getFileExtension();
193cdf0e10cSrcweir 
194cdf0e10cSrcweir 	// set a default extension, only the first three letters of
195cdf0e10cSrcweir 	// the given extension will be used; the given extension
196cdf0e10cSrcweir 	// should not contain a '.'
197cdf0e10cSrcweir 	void SAL_CALL setDefaultFileExtension(const rtl::OUString& aExtension);
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 	// enables or disables the multiselection mode for
200cdf0e10cSrcweir 	// the FileOpen/FileSave dialog
201cdf0e10cSrcweir 	void SAL_CALL setMultiSelectionMode(bool bMode);
202cdf0e10cSrcweir 
203cdf0e10cSrcweir 	// returns whether multi-selection mode is enabled or not
204cdf0e10cSrcweir 	bool SAL_CALL getMultiSelectionMode() const;
205cdf0e10cSrcweir 
206cdf0e10cSrcweir 	// shows the dialog, calls preModal before
207cdf0e10cSrcweir 	// showing the dialog and postModal after
208cdf0e10cSrcweir 	// showing the dialog
209cdf0e10cSrcweir 	// the method returns:
210cdf0e10cSrcweir 	//  0 - when the dialog was canceled by the user
211cdf0e10cSrcweir 	//  1 - when the dialog was closed with ok
21207a3d7f1SPedro Giffuni 	// -1 - when an error occurred
213cdf0e10cSrcweir 	sal_Int16 SAL_CALL doModal();
214cdf0e10cSrcweir 
21507a3d7f1SPedro Giffuni 	// returns the last dialog error that occurred
216cdf0e10cSrcweir 	sal_uInt32 SAL_CALL getLastDialogError() const;
217cdf0e10cSrcweir 
218cdf0e10cSrcweir 	// retrievs the currently selected file
219cdf0e10cSrcweir 	// including path and drive information
220cdf0e10cSrcweir 	// can be called only if the dialog is
221cdf0e10cSrcweir 	// already displayed
222cdf0e10cSrcweir 	rtl::OUString SAL_CALL getCurrentFilePath() const;
223cdf0e10cSrcweir 
224cdf0e10cSrcweir 	// retrievs the currently selected folder
225cdf0e10cSrcweir 	rtl::OUString SAL_CALL getCurrentFolderPath() const;
226cdf0e10cSrcweir 
227cdf0e10cSrcweir 	// retrievs the currently selected file name
228cdf0e10cSrcweir 	// without drive and path
229cdf0e10cSrcweir 	rtl::OUString SAL_CALL getCurrentFileName() const;
230cdf0e10cSrcweir 
231cdf0e10cSrcweir protected:
232cdf0e10cSrcweir 	// have to be overwritten when subclasses
233cdf0e10cSrcweir 	// want to do special pre- and post-modal
234cdf0e10cSrcweir 	// processing
235cdf0e10cSrcweir 
236cdf0e10cSrcweir 	// if preModal return true processing will
237cdf0e10cSrcweir 	// continue else doModal exit without showing
238cdf0e10cSrcweir 	// a dialog and returns -1
239cdf0e10cSrcweir 	virtual bool SAL_CALL preModal();
240cdf0e10cSrcweir 
241cdf0e10cSrcweir 	// post modal processing
242cdf0e10cSrcweir 	// the function should accept only values returned from
243cdf0e10cSrcweir 	// doModal and act appropriately
244cdf0e10cSrcweir 	virtual void SAL_CALL postModal(sal_Int16 nDialogResult);
245cdf0e10cSrcweir 
246cdf0e10cSrcweir 	// message handler, to be overwritten by subclasses
247cdf0e10cSrcweir 	virtual sal_uInt32 SAL_CALL onShareViolation(const rtl::OUString& aPathName);
248cdf0e10cSrcweir 	virtual sal_uInt32 SAL_CALL onFileOk();
249cdf0e10cSrcweir 	virtual void SAL_CALL onSelChanged(HWND hwndListBox);
250cdf0e10cSrcweir 	virtual void SAL_CALL onHelp();
251cdf0e10cSrcweir 
252cdf0e10cSrcweir 	// only called back if OFN_EXPLORER is set
253cdf0e10cSrcweir 	virtual void SAL_CALL onInitDone();
254cdf0e10cSrcweir 	virtual void SAL_CALL onFolderChanged();
255cdf0e10cSrcweir 	virtual void SAL_CALL onTypeChanged(sal_uInt32 nFilterIndex);
256cdf0e10cSrcweir 
257cdf0e10cSrcweir 	virtual void SAL_CALL onInitDialog(HWND hwndDlg) = 0;
258cdf0e10cSrcweir 
259cdf0e10cSrcweir 	virtual sal_uInt32 SAL_CALL onCtrlCommand(HWND hwndDlg, sal_uInt16 ctrlId, sal_uInt16 notifyCode);
260cdf0e10cSrcweir 
261cdf0e10cSrcweir 	sal_uInt32 SAL_CALL onWMNotify(HWND hwndChild, LPOFNOTIFYW lpOfNotify);
262cdf0e10cSrcweir 
263cdf0e10cSrcweir 	// we use non-virtual functions to do necessary work before
264cdf0e10cSrcweir 	// calling the virtual funtions (see Gamma: Template method)
265cdf0e10cSrcweir 	void SAL_CALL handleInitDialog(HWND hwndDlg, HWND hwndChild);
266cdf0e10cSrcweir 
267cdf0e10cSrcweir protected:
268cdf0e10cSrcweir 
269cdf0e10cSrcweir 	// handle to the window of the
270cdf0e10cSrcweir 	// FileOpen/FileSave dialog
271cdf0e10cSrcweir 	// will be set on message
272cdf0e10cSrcweir 	// WM_INITDIALOG, before this
273cdf0e10cSrcweir 	// value is undefined
274cdf0e10cSrcweir 	HWND	m_hwndFileOpenDlg;
275cdf0e10cSrcweir 	HWND	m_hwndFileOpenDlgChild;
276cdf0e10cSrcweir 
277cdf0e10cSrcweir 	_OPENFILENAME	m_ofn;
278cdf0e10cSrcweir 
279cdf0e10cSrcweir     // we connect the instance with the dialog window using
280cdf0e10cSrcweir     // SetProp, with this function we can reconnect from
281cdf0e10cSrcweir     // callback functions to this instance
282cdf0e10cSrcweir     static CFileOpenDialog* SAL_CALL getCurrentInstance(HWND hwnd);
283cdf0e10cSrcweir 
284cdf0e10cSrcweir     void SAL_CALL centerPositionToParent() const;
285cdf0e10cSrcweir 
286cdf0e10cSrcweir private:
287cdf0e10cSrcweir 	// FileOpen or FileSaveDialog
288cdf0e10cSrcweir 	bool			m_bFileOpenDialog;
289cdf0e10cSrcweir 	rtl::OUString	m_dialogTitle;
290cdf0e10cSrcweir 	rtl::OUString	m_displayDirectory;
291cdf0e10cSrcweir 	rtl::OUString	m_defaultExtension;
292cdf0e10cSrcweir 
293cdf0e10cSrcweir 	mutable	rtl::OUStringBuffer m_filterBuffer;
294cdf0e10cSrcweir 	mutable	rtl::OUStringBuffer m_fileTitleBuffer;
295cdf0e10cSrcweir 	mutable rtl::OUStringBuffer	m_helperBuffer;
296cdf0e10cSrcweir 	mutable rtl::OUStringBuffer m_fileNameBuffer;
297cdf0e10cSrcweir 
298cdf0e10cSrcweir     CGetFileNameWrapper m_GetFileNameWrapper;
299cdf0e10cSrcweir 
300cdf0e10cSrcweir     WNDPROC	    		m_pfnBaseDlgProc;
301cdf0e10cSrcweir 
302cdf0e10cSrcweir 	// callback function
303cdf0e10cSrcweir 	static unsigned int CALLBACK ofnHookProc(
304cdf0e10cSrcweir 		HWND hChildDlg, // handle to child dialog box
305cdf0e10cSrcweir 		unsigned int uiMsg,     // message identifier
306cdf0e10cSrcweir 		WPARAM wParam,  // message parameter
307cdf0e10cSrcweir 		LPARAM lParam   // message parameter
308cdf0e10cSrcweir 	);
309cdf0e10cSrcweir 
310cdf0e10cSrcweir     // we have to subclass the dialog in order
311cdf0e10cSrcweir     // to clean up the window property we are
312cdf0e10cSrcweir     // using to connect the window with a class
313cdf0e10cSrcweir     // instance in WM_NCDESTROY
314cdf0e10cSrcweir     static LRESULT CALLBACK BaseDlgProc(
315cdf0e10cSrcweir         HWND hWnd, UINT wMessage, WPARAM wParam, LPARAM lParam );
316cdf0e10cSrcweir 
317cdf0e10cSrcweir private:
318cdf0e10cSrcweir 	// avoid copy and assignment
319cdf0e10cSrcweir 	CFileOpenDialog(const CFileOpenDialog&);
320cdf0e10cSrcweir 	CFileOpenDialog& operator=(const CFileOpenDialog&);
321cdf0e10cSrcweir };
322cdf0e10cSrcweir 
323cdf0e10cSrcweir #endif
324