summaryrefslogtreecommitdiff
path: root/fpicker/source/win32/filepicker/FileOpenDlg.hxx
blob: 054f7466749d04bd544123adebdd72aad2c9afa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#ifndef INCLUDED_FPICKER_SOURCE_WIN32_FILEPICKER_FILEOPENDLG_HXX
#define INCLUDED_FPICKER_SOURCE_WIN32_FILEPICKER_FILEOPENDLG_HXX

#include <sal/types.h>

#include <rtl/ustring.hxx>
#include <rtl/ustrbuf.hxx>

#include "platform_xp.h"
#include "getfilenamewrapper.hxx"

// because we don't want to import the new W2k platform skd
// into our build environment if have stolen the definition
// for the new OPENFILENAME structure from the new headers

#ifndef _CDSIZEOF_STRUCT
#define _CDSIZEOF_STRUCT(structname, member)  (((int)((LPBYTE)(&((structname*)0)->member) - ((LPBYTE)((structname*)0)))) + sizeof(((structname*)0)->member))
#endif

typedef struct _tagOFNA {
   DWORD        lStructSize;
   HWND         hwndOwner;
   HINSTANCE    hInstance;
   LPCSTR       lpstrFilter;
   LPSTR        lpstrCustomFilter;
   DWORD        nMaxCustFilter;
   DWORD        nFilterIndex;
   LPSTR        lpstrFile;
   DWORD        nMaxFile;
   LPSTR        lpstrFileTitle;
   DWORD        nMaxFileTitle;
   LPCSTR       lpstrInitialDir;
   LPCSTR       lpstrTitle;
   DWORD        Flags;
   WORD         nFileOffset;
   WORD         nFileExtension;
   LPCSTR       lpstrDefExt;
   LPARAM       lCustData;
   LPOFNHOOKPROC lpfnHook;
   LPCSTR       lpTemplateName;
#ifdef _MAC
   LPEDITMENU   lpEditInfo;
   LPCSTR       lpstrPrompt;
#endif
#if (_WIN32_WINNT >= 0x0500)
   void *       pvReserved;
   DWORD        dwReserved;
   DWORD        FlagsEx;
#endif // (_WIN32_WINNT >= 0x0500)
} _OPENFILENAMEA, *_LPOPENFILENAMEA;

typedef struct _tagOFNW {
   DWORD        lStructSize;
   HWND         hwndOwner;
   HINSTANCE    hInstance;
   LPCWSTR      lpstrFilter;
   LPWSTR       lpstrCustomFilter;
   DWORD        nMaxCustFilter;
   DWORD        nFilterIndex;
   LPWSTR       lpstrFile;
   DWORD        nMaxFile;
   LPWSTR       lpstrFileTitle;
   DWORD        nMaxFileTitle;
   LPCWSTR      lpstrInitialDir;
   LPCWSTR      lpstrTitle;
   DWORD        Flags;
   WORD         nFileOffset;
   WORD         nFileExtension;
   LPCWSTR      lpstrDefExt;
   LPARAM       lCustData;
   LPOFNHOOKPROC lpfnHook;
   LPCWSTR      lpTemplateName;
#if (_WIN32_WINNT >= 0x0500)
   void *       pvReserved;
   DWORD        dwReserved;
   DWORD        FlagsEx;
#endif // (_WIN32_WINNT >= 0x0500)
} _OPENFILENAMEW, *_LPOPENFILENAMEW;

#ifdef UNICODE
typedef _OPENFILENAMEW _OPENFILENAME;
typedef _LPOPENFILENAMEW _LPOPENFILENAME;
#else
typedef _OPENFILENAMEA _OPENFILENAME;
typedef _LPOPENFILENAMEA _LPOPENFILENAME;
#endif // UNICODE

#if (_WIN32_WINNT >= 0x0500)
    #define _OPENFILENAME_SIZE_VERSION_400A  _CDSIZEOF_STRUCT(_OPENFILENAMEA,lpTemplateName)
    #define _OPENFILENAME_SIZE_VERSION_400W  _CDSIZEOF_STRUCT(_OPENFILENAMEW,lpTemplateName)
    #ifdef UNICODE
        #define _OPENFILENAME_SIZE_VERSION_400  _OPENFILENAME_SIZE_VERSION_400W
    #else
        #define _OPENFILENAME_SIZE_VERSION_400  _OPENFILENAME_SIZE_VERSION_400A
    #endif // !UNICODE
#else
    #error _WIN32_WINNT seems not to be valid.
#endif // (_WIN32_WINNT >= 0x0500)



// A simple wrapper class around the Win32 GetOpenFileName API.
// This class is not thread-safe and only one instance at a
// time is allowed


class CFileOpenDialog
{
public:
    // ctor
    // bFileOpenDialog idicates if we want a FileOpen or FileSave
    // dialog
    // dwFlags see OPENFILENAME
    // dwTemplateId - an ID for custom templates
    // hInstance    - an instance handle for the module
    // which provides the custom template, unused if dwTemplateId
    // is 0
    CFileOpenDialog(
        bool bFileOpenDialog = sal_True,
        sal_uInt32 dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
        sal_uInt32 dwTemplateId = 0,
        HINSTANCE hInstance = 0);

    virtual ~CFileOpenDialog();

    virtual void SAL_CALL setTitle(const OUString& aTitle);

    // to set a filter string using the M$ format
    // e.g. FltName\0*.txt;*.rtf\0...\0\0
    void SAL_CALL setFilter(const OUString& aFilter);

    // set the index of the current filter when the
    // dialog is about to shown, the index starts with 1
    // the function succeeded if the given filter index
    // is greater than zero and is a valid position
    // within filter string that was previously set
    bool SAL_CALL setFilterIndex(sal_uInt32 aIndex);

    // get the index of the currently selected filter
    // the index of the returned filter starts with 1
    sal_uInt32 SAL_CALL getSelectedFilterIndex() const;

    // set the name and optional the path of the
    // file that will be initially be shown when
    // the dialog will be displayed
    virtual void SAL_CALL setDefaultName(const OUString& aName);

    // set the initial directory
    virtual void SAL_CALL setDisplayDirectory(const OUString& aDirectory);

    // returns only the path of the selected file
    virtual OUString SAL_CALL getLastDisplayDirectory() const;

    // returns the full file name including drive letter, path
    // file name and file extension
    virtual OUString SAL_CALL getFullFileName() const;

    // returns the file name and the file extension without
    // drive letter and path
    OUString SAL_CALL getFileName() const;

    // returns the file extension of the selected file
    OUString SAL_CALL getFileExtension();

    // set a default extension, only the first three letters of
    // the given extension will be used; the given extension
    // should not contain a '.'
    void SAL_CALL setDefaultFileExtension(const OUString& aExtension);

    // enables or disables the multiselection mode for
    // the FileOpen/FileSave dialog
    void SAL_CALL setMultiSelectionMode(bool bMode);

    // returns whether multi-selection mode is enabled or not
    bool SAL_CALL getMultiSelectionMode() const;

    // shows the dialog, calls preModal before
    // showing the dialog and postModal after
    // showing the dialog
    // the method returns:
    //  0 - when the dialog was canceled by the user
    //  1 - when the dialog was closed with ok
    // -1 - when an error occurred
    sal_Int16 SAL_CALL doModal();

    // returns the last dialog error that occurred
    sal_uInt32 SAL_CALL getLastDialogError() const;

    // retrievs the currently selected file
    // including path and drive information
    // can be called only if the dialog is
    // already displayed
    OUString SAL_CALL getCurrentFilePath() const;

    // retrievs the currently selected folder
    OUString SAL_CALL getCurrentFolderPath() const;

    // retrievs the currently selected file name
    // without drive and path
    OUString SAL_CALL getCurrentFileName() const;

protected:
    // have to be overwritten when subclasses
    // want to do special pre- and post-modal
    // processing

    // if preModal return true processing will
    // continue else doModal exit without showing
    // a dialog and returns -1
    virtual bool SAL_CALL preModal();

    // post modal processing
    // the function should accept only values returned from
    // doModal and act appropriately
    virtual void SAL_CALL postModal(sal_Int16 nDialogResult);

    // message handler, to be overwritten by subclasses
    virtual sal_uInt32 SAL_CALL onShareViolation(const OUString& aPathName);
    virtual sal_uInt32 SAL_CALL onFileOk();
    virtual void SAL_CALL onSelChanged(HWND hwndListBox);
    virtual void SAL_CALL onHelp();

    // only called back if OFN_EXPLORER is set
    virtual void SAL_CALL onInitDone();
    virtual void SAL_CALL onFolderChanged();
    virtual void SAL_CALL onTypeChanged(sal_uInt32 nFilterIndex);

    virtual void SAL_CALL onInitDialog(HWND hwndDlg) = 0;

    virtual sal_uInt32 SAL_CALL onCtrlCommand(HWND hwndDlg, sal_uInt16 ctrlId, sal_uInt16 notifyCode);

    sal_uInt32 SAL_CALL onWMNotify(HWND hwndChild, LPOFNOTIFYW lpOfNotify);

    // we use non-virtual functions to do necessary work before
    // calling the virtual funtions (see Gamma: Template method)
    void SAL_CALL handleInitDialog(HWND hwndDlg, HWND hwndChild);

protected:

    // handle to the window of the
    // FileOpen/FileSave dialog
    // will be set on message
    // WM_INITDIALOG, before this
    // value is undefined
    HWND    m_hwndFileOpenDlg;
    HWND    m_hwndFileOpenDlgChild;

    _OPENFILENAME   m_ofn;

    // we connect the instance with the dialog window using
    // SetProp, with this function we can reconnect from
    // callback functions to this instance
    static CFileOpenDialog* SAL_CALL getCurrentInstance(HWND hwnd);

    void SAL_CALL centerPositionToParent() const;

private:
    // FileOpen or FileSaveDialog
    bool            m_bFileOpenDialog;
    OUString   m_dialogTitle;
    OUString   m_displayDirectory;
    OUString   m_defaultExtension;

    mutable OUStringBuffer m_filterBuffer;
    mutable OUStringBuffer m_fileTitleBuffer;
    mutable OUStringBuffer m_helperBuffer;
    mutable OUStringBuffer m_fileNameBuffer;

    CGetFileNameWrapper m_GetFileNameWrapper;

    WNDPROC             m_pfnBaseDlgProc;

    // callback function
    static UINT_PTR CALLBACK ofnHookProc(
        HWND hChildDlg, // handle to child dialog box
        UINT uiMsg,     // message identifier
        WPARAM wParam,  // message parameter
        LPARAM lParam   // message parameter
    );

    // we have to subclass the dialog in order
    // to clean up the window property we are
    // using to connect the window with a class
    // instance in WM_NCDESTROY
    static LRESULT CALLBACK BaseDlgProc(
        HWND hWnd, UINT wMessage, WPARAM wParam, LPARAM lParam );

private:
    // avoid copy and assignment
    CFileOpenDialog(const CFileOpenDialog&);
    CFileOpenDialog& operator=(const CFileOpenDialog&);
};

#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */