OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
opensave.h
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectWindows
3// Copyright (c) 1992, 1996 by Borland International, All Rights Reserved
4//
5/// \file
6/// Definition of TOpenSave abstract, TFileOpen, TFileSave common Dialog classes
7//----------------------------------------------------------------------------
8
9#if !defined(OWL_OPENSAVE_H)
10#define OWL_OPENSAVE_H
11
12#include <owl/private/defs.h>
13#if defined(BI_HAS_PRAGMA_ONCE)
14# pragma once
15#endif
16
17#include <owl/commdial.h>
18#include <vector>
19
20namespace owl {
21
22#include <owl/preclass.h>
23
24
25/// \addtogroup commdlg
26/// @{
27/// \class TOpenSaveDialog
28// ~~~~~ ~~~~~~~~~~~~~~~
29/// TOpenSaveDialog is the base class for modal dialogs that let you open and save a
30/// file under a specified name. TOpenSaveDialog constructs a TData structure and
31/// passes it the TOpenSaveDialog constructor. Then the dialog is executed (modal)
32/// or created (modeless). Upon return, the necessary fields are updated, including
33/// an error field that contains 0, or a common dialog extended error.
35 public:
36
37/// TOpenSaveDialog structure contains information about the user's file open or
38/// save selection. Specifically, this structure stores a user-specified file name
39/// filter, file extension, file name, the initial directory to use when displaying
40/// file names, any error codes, and various file attributes that determine, for
41/// example, if the file is a read-only file. The classes TFileOpenDialog and
42/// TFileSaveDialog use the information stored in this structure when a file is
43/// opened or saved.
45 public:
46
47 /// Flag contains one or more of the following constants:
48 /// - \c \b OFN_HIDEREADONLY Hides the read-only check box.
49 /// - \c \b OFN_FILEMUSTEXIST Lets the user enter only names of existing files in the File
50 /// Name entry field. If an invalid file name is entered, a warning message is
51 /// displayed.
52 /// - \c \b OFN_PATHMUSTEXIST Lets the user enter only valid path names. If an invalid path
53 /// name is entered, a warning message is displayed.
54 /// - \c \b OFN_NOVALIDATE Performs no check of the file name and requires the owner of a
55 /// derived class to perform validation.
56 /// - \c \b OFN_NOCHANGEDIR Sets the current directory back to what it was when the dialog
57 /// was initiated.
58 /// - \c \b OFN_ALLOWMULTISELECT Allows multiple selections in the File Name list box.
59 /// - \c \b OFN_CREATEPROMPT Asks if the user wants to create a file that does not
60 /// currently exist.
61 /// - \c \b OFN_EXTENSIONDIFFERENT Idicates the user entered a file name different from the
62 /// specified in DefExt. This message is returned to the caller.
63 /// - \c \b OFN_NOREADONLYRETURN The returned file does not have the Read Only attribute
64 /// set and is not in a write-protected directory. This message is returned to the
65 /// caller.
66 /// - \c \b OFN_NOTESTFILECREATE The file is created after the dialog box is closed. If the
67 /// application sets this flag, there is no check against write protection, a full
68 /// disk, an open drive door, or network protection. For certain network
69 /// environments, this flag should be set.
70 /// - \c \b OFN_OVERWRITEPROMPT The Save As dialog box displays a message asking the user if
71 /// it's OK to overwrite an existing file.
72 /// - \c \b OFN_SHAREAWARE If this flag is set and a call to open a file fails because of a
73 /// sharing violation, the error is ignored and the dialog box returns the given
74 /// file name. If this flag is not set, the virtual function ShareViolation is
75 /// called, which returns OFN_SHAREWARN (by default) or one of the following values:
76 /// -- OFN_SHAREFALLTHROUGH - File name is returned from the dialog box.
77 /// -- OFN_SHARENOWARN - No further action is taken.
78 /// -- OFN_SHAREWARN - User receives the standard warning message for this type of error.
79 /// - \c \b OFN_SHOWHELP Shows the Help button in the dialog box.
81
82 /// This is the return value from calling CommDlgExtendedError after executing the dialog
83 /// in the implementation of TOpenSaveDialog::Execute.
84 ///
85 /// \note An error may have occured, even if Execute returns IDOK. You should therefore
86 /// always test this error code, regardless of the return value. Also note that, in
87 /// earlier versions of OWLNext, the error code was always incorrectly set to 0 when IDOK
88 /// was returned. Make sure that you test the error code before the return value.
89 ///
90 /// Usage:
91 ///
92 /// \code
93 /// TOpenSaveDialog::TData data{/*...*/};
94 /// TFileOpenDialog dlg{this, data};
95 /// const auto r = dlg.Execute();
96 /// if (data.Error == FNERR_BUFFERTOOSMALL)
97 /// {
98 /// MessageBox("Buffer too small!", "File Open Error", MB_OK | MB_ICONEXCLAMATION);
99 /// return;
100 /// }
101 /// if (r == IDOK)
102 /// {
103 /// //...
104 /// }
105 /// \endcode
106 ///
107 /// \sa http://docs.microsoft.com/en-gb/windows/desktop/api/commdlg/nf-commdlg-commdlgextendederror
109
110 /// Holds the name of the file to be saved or opened.
111 /// Note: This member points to an internal buffer. Do not modify!
113
114 /// Filter holds the filter to use initially when displaying file names.
115 /// Note: This member points to an internal buffer. Do not modify!
117
118 /// FilterIndex indicates which filter to use initially when displaying file names.
120
121 /// Directory to use initially when displaying file names.
122 /// Note: This is an external reference; the string pointed to must outlive this object.
124
125 /// Default extension to use when saving file names.
126 /// Note: This is an external reference; the string pointed to must outlive this object.
128
131
132 TData(
133 uint32 flags = 0,
134 LPCTSTR filter = nullptr, ///< Note: Copied; the string is stored in an internal buffer.
135 LPTSTR customFilter = nullptr, ///< Note: Not used; dummy for backward compatibility.
136 LPCTSTR initialDir = nullptr, ///< Note: Not copied; the string pointed to must outlive this object.
137 LPCTSTR defExt = nullptr, ///< Note: Not copied; the string pointed to must outlive this object.
138 int maxPath = 0,
139 int filterIndex = 0,
140 uint32 flagsEx = 0);
141
142 TData(
143 uint32 flags,
144 const tstring& filter,
145 LPTSTR customFilter = nullptr, ///< Note: Not used; dummy for backward compatibility.
146 LPCTSTR initialDir = nullptr, ///< Note: Not copied; the string pointed to must outlive this object.
147 LPCTSTR defExt = nullptr, ///< Note: Not copied; the string pointed to must outlive this object.
148 int maxPath = 0,
149 int filterIndex = 0,
150 uint32 flagsEx = 0);
151
152 TData(const TData& src);
153 ~TData();
154
155 TData& operator =(const TData& src);
156
157 void SetFilter(LPCTSTR filter = nullptr);
158 void SetFilter(const tstring& filter) {SetFilter(filter.c_str());}
159
160 void Write(opstream& os);
161 void Read(ipstream& is);
162
163 auto GetFileNames() const -> std::vector<tstring>;
164 };
165
166 TOpenSaveDialog(TWindow* parent,
167 TData& data,
168 TResId templateId = 0,
169 LPCTSTR title = nullptr,
170 TModule* module = nullptr);
171
173 TWindow* parent,
174 TData& data,
176 const tstring& title,
177 TModule* module = nullptr);
178
179 static int GetFileTitleLen(LPCTSTR fileName);
180 static int GetFileTitle(LPCTSTR fileName, LPTSTR fileTitle,
181 int bufLen);
182
183 protected:
184 TOpenSaveDialog(TWindow* parent, TData& data, TModule* module = nullptr);
185 void Init(TResId templateId);
186
187 auto DialogFunction(TMsgId, TParam1, TParam2) -> INT_PTR override;
188
189 // Override TWindow & TDialog virtuals
190 //
191 auto DoExecute() -> int override = 0;
192
193 // Virtual function called when a share violation occurs in dlg
194 //
195 virtual int ShareViolation();
196
197 // Messages registered by the common dialog DLL
198 //
199 static uint GetShareViMsgId();
200
201 // Default behavior inline for message response functions
202 //
203 void CmOk(); //EV_COMMAND(IDOK,
204 void CmLbSelChanged(); //EV_COMMAND(lst1 or lst2
205
206 OPENFILENAME& GetOFN();
207 void SetOFN(const OPENFILENAME& _ofn);
208
209 TData& GetData();
210 void SetData(TData& data);
211
213/// Contains the attributes of the file name such as length, extension, and
214/// directory. ofn is initialized using the fields in the TOpenSaveDialog::TData
215/// class.
216 OPENFILENAME ofn;
217
218/// Stores the file name, its length, extension, filter, initial directory, default
219/// file name, extension, and any error messages.
220 TData& Data;
221
222 tstring Title; ///< Stores the title of the dialog, if given.
223
224/// Contains the message ID of the registered ShareViolation message.
225 static const uint ShareViMsgId;
226
227 private:
230
232};
233
234//
235/// \class TFileOpenDialog
236// ~~~~~ ~~~~~~~~~~~~~~~
237/// TFileOpenDialog is a modal dialog box that lets you specify the name of a file
238/// to open. Use this dialog box to respond to a CM_FILEOPEN command that's
239/// generated when a user selects File|Open from a menu. TFileOpenDialog uses the
240/// TOpenSave::TData structure to initialize the file open dialog box.
241///
242/// \note Simply regard the return value from Execute as an indication of confirmation (IDOK) or
243/// cancellation (IDCANCEL), and test TOpenSaveDialog::TData::Error for failure in both cases. The
244/// error code may indicate issues, such as buffer overflow.
245///
246/// Usage:
247///
248/// \code
249/// TOpenSaveDialog::TData data{...};
250/// TFileOpenDialog dlg{this, data};
251/// const auto r = dlg.Execute();
252/// if (data.Error != 0)
253/// {
254/// // ...Handle error...
255/// }
256/// else if (r == IDOK)
257/// {
258/// // ...Open file...
259/// }
260/// \endcode
261///
262/// \sa http://docs.microsoft.com/en-gb/windows/desktop/api/commdlg/nf-commdlg-commdlgextendederror
263//
265 public:
266 TFileOpenDialog(TWindow* parent,
267 TData& data,
268 TResId templateId = 0,
269 LPCTSTR title = nullptr,
270 TModule* module = nullptr);
271
272 // Override TDialog virtual functions
273 //
274 int DoExecute();
275
276 private:
278 TFileOpenDialog& operator=(const TOpenSaveDialog&);
279};
280
281//
282/// \class TFileSaveDialog
283// ~~~~~ ~~~~~~~~~~~~~~~
284/// TFileSaveDialog is a modal dialog box that lets you enter the name of a file to
285/// save. Use TFileSaveDialog to respond to a CM_FILESAVEAS command generated when a
286/// user selects File|Save from a menu. TFileSaveDialog uses the TOpenSave::TData
287/// structure to initialize the file save dialog box.
288///
289/// \note Simply regard the return value from Execute as an indication of confirmation (IDOK) or
290/// cancellation (IDCANCEL), and test TOpenSaveDialog::TData::Error for failure in both cases. The
291/// error code may indicate issues, such as buffer overflow.
292///
293/// Usage:
294///
295/// \code
296/// TOpenSaveDialog::TData data{...};
297/// TFileSaveDialog dlg{this, data};
298/// const auto r = dlg.Execute();
299/// if (data.Error != 0)
300/// {
301/// // ...Handle error...
302/// }
303/// else if (r == IDOK)
304/// {
305/// // ...Save file...
306/// }
307/// \endcode
308///
309/// \sa http://docs.microsoft.com/en-gb/windows/desktop/api/commdlg/nf-commdlg-commdlgextendederror
310//
312 public:
313 TFileSaveDialog(TWindow* parent,
314 TData& data,
315 TResId templateId = 0,
316 LPCTSTR title = nullptr,
317 TModule* module = nullptr);
318
319 // Override TDialog virtual functions
320 //
321 int DoExecute();
322
323 private:
325 TFileSaveDialog& operator=(const TFileSaveDialog&);
326};
327/// @}
328
329
330#include <owl/posclass.h>
331
332
333//----------------------------------------------------------------------------
334// Inline implementations
335//
336
337//
338/// Returns the sharing violation message ID.
339//
340inline uint TOpenSaveDialog::GetShareViMsgId()
341{
342 return ShareViMsgId;
343}
344
345//
346/// Returns the title length of the dialog.
347//
348inline int TOpenSaveDialog::GetFileTitleLen(LPCTSTR fileName)
349{
350 return ::GetFileTitle(fileName, nullptr, 0);
351}
352
353//
354/// Returns the title of the dialog.
355//
356inline int TOpenSaveDialog::GetFileTitle(LPCTSTR fileName,
358 int fileTitleLen)
359{
360 return ::GetFileTitle(fileName, fileTitle, static_cast<WORD>(fileTitleLen));
361}
362
363//
364/// Responds to a click on the dialog box's OK button (with the identifier IDOK).
365/// Calls CloseWindow (passing IDOK).
366inline void TOpenSaveDialog::CmOk()
367{
368 DefaultProcessing();
369}
370
371//
372/// Indicates that the selection state of the file name list box in the
373/// GetOpenFileName or GetSaveFileName dialog boxes has changed. CmLbSelChanged is a
374/// default handler for command messages sent by lst1 or lst2 (the file and
375/// directory list boxes, respectively).
376inline void TOpenSaveDialog::CmLbSelChanged()
377{
378 DefaultProcessing();
379}
380
381//
382/// Returns the OPENFILENAME structure.
383//
384inline OPENFILENAME& TOpenSaveDialog::GetOFN()
385{
386 return ofn;
387}
388
389//
390/// Sets the OPENFILENAME structure for the dialog.
391//
392inline void TOpenSaveDialog::SetOFN(const OPENFILENAME& _ofn)
393{
394 ofn = _ofn;
395}
396
397//
398/// Retrieves the transfer buffer for the dialog.
399//
400inline TOpenSaveDialog::TData& TOpenSaveDialog::GetData()
401{
402 return Data;
403}
404
405//
406/// Sets the transfer buffer for the dialog.
407//
408inline void TOpenSaveDialog::SetData(TOpenSaveDialog::TData& data)
409{
410 Data = data;
411}
412
413} // OWL namespace
414
415
416#endif // OWL_OPENSAVE_H
Derived from TDialog, TCommonDialog is the abstract base class for TCommonDialog objects.
Definition commdial.h:62
TFileOpenDialog is a modal dialog box that lets you specify the name of a file to open.
Definition opensave.h:264
TFileSaveDialog is a modal dialog box that lets you enter the name of a file to save.
Definition opensave.h:311
ObjectWindows dynamic-link libraries (DLLs) construct an instance of TModule, which acts as an object...
Definition module.h:75
TOpenSaveDialog structure contains information about the user's file open or save selection.
Definition opensave.h:44
TCHAR * FileName
Holds the name of the file to be saved or opened.
Definition opensave.h:112
int FilterIndex
FilterIndex indicates which filter to use initially when displaying file names.
Definition opensave.h:119
void SetFilter(const tstring &filter)
Definition opensave.h:158
LPCTSTR InitialDir
Directory to use initially when displaying file names.
Definition opensave.h:123
uint32 Flags
Flag contains one or more of the following constants:
Definition opensave.h:80
TCHAR * Filter
Filter holds the filter to use initially when displaying file names.
Definition opensave.h:116
uint32 Error
This is the return value from calling CommDlgExtendedError after executing the dialog in the implemen...
Definition opensave.h:108
LPCTSTR DefExt
Default extension to use when saving file names.
Definition opensave.h:127
TOpenSaveDialog is the base class for modal dialogs that let you open and save a file under a specifi...
Definition opensave.h:34
TWindow, derived from TEventHandler and TStreamableBase, provides window-specific behavior and encaps...
Definition window.h:414
ipstream, a specialized input stream derivative of pstream, is the base class for reading (extracting...
Definition objstrm.h:391
Base class for writing streamable objects.
Definition objstrm.h:480
Definition of Common Dialog abstract base class.
#define DECLARE_RESPONSE_TABLE(cls)
Definition eventhan.h:436
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
UINT TMsgId
Message ID type.
Definition dispatch.h:53
unsigned long uint32
Definition number.h:34
LPARAM TParam2
Second parameter type.
Definition dispatch.h:55
WPARAM TParam1
First parameter type.
Definition dispatch.h:54
std::string tstring
Definition defs.h:79
unsigned int uint
Definition number.h:25
#define protected_data
Definition defs.h:208
#define _OWLCLASS
Definition defs.h:338