OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
inputdia.cpp
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/// Implementation of TInputDialog. User string input dialog box
7//----------------------------------------------------------------------------
8#include <owl/pch.h>
9
10#include <owl/inputdia.h>
11#include <owl/edit.h>
12#include <owl/validate.h>
13#include <algorithm>
14
15namespace owl {
16
18
19/** Let the compiler know that the following template instances will be defined elsewhere. **/
20//#pragma option -Jgx
21
22
23//
24/// Invokes TDialog's constructor, passing it parent, the resource identifier, and
25/// module. Sets the caption of the dialog box to title and the prompt static
26/// control to prompt. Sets the Buffer and BufferSize data members to buffer and
27/// bufferSize.
28//
33 int bufferSize,
34 TModule* module,
35 TValidator* validator)
36: TDialog(parent, IDD_INPUTDIALOG, module),
37 ShouldDelete(false)
38{
40 SetCaption(title); // Override title in resource.
41 Prompt = strnewdup(prompt);
42 Buffer = buffer;
43 BufferSize = bufferSize;
44 if (validator)
45 (new TEdit(this,ID_INPUT))->SetValidator(validator);
46}
47
48//
49/// String-aware overload
50//
52 TWindow* parent,
53 const tstring& title,
54 const tstring& prompt,
56 int bufferSize,
57 TModule* module,
58 TValidator* validator
59 )
60 : TDialog(parent, IDD_INPUTDIALOG, module),
61 ShouldDelete(false)
62{
64 SetCaption(title); // Override title in resource.
65 Prompt = strnewdup(prompt.c_str());
66 Buffer = buffer;
67 BufferSize = bufferSize;
68 if (validator)
69 (new TEdit(this,ID_INPUT))->SetValidator(validator);
70}
71
72namespace
73{
74
75 const size_t DefaultBufferSize_ = 1024;
76
77 int CalculateBufferSize_(const tstring& s)
78 {
79 PRECONDITION(s.size() <= INT_MAX);
80 return static_cast<int>(std::max(DefaultBufferSize_, s.size() + 1));
81 }
82
84 {
86 std::copy(s.begin(), s.end(), b);
87 b[s.size()] = _T('\0');
88 return b;
89 }
90
91} // namespace
92
93//
94/// String-aware overload
95/// This overload requires no buffer to be passed. A buffer is allocated internally.
96/// Optionally you can pass the initial string.
97//
99 TWindow* parent,
100 const tstring& title,
101 const tstring& prompt,
102 const tstring& initValue,
103 TModule* module,
104 TValidator* validator
105 )
106 : TDialog(parent, IDD_INPUTDIALOG, module),
107 Prompt(strnewdup(prompt.c_str())),
109 BufferSize(CalculateBufferSize_(initValue)),
110 ShouldDelete(true)
111{
112 try
113 {
114 SetCaption(title); // Override title in resource.
115 if (validator)
116 (new TEdit(this, ID_INPUT))->SetValidator(validator);
117 }
118 catch (...)
119 {
120 delete[] Prompt;
121 delete[] Buffer;
122 throw;
123 }
124}
125
126//
127/// Destructor for this class.
128//
130{
131 delete[] Prompt;
132 if (ShouldDelete)
133 delete[] Buffer;
134}
135
136//
137/// Transfers the data of the input dialog box. If direction is tdSetData, sets the
138/// text of the static and edit controls of the dialog box to the text in prompt and
139/// buffer.
140//
141void
143{
144 if (direction == tdSetData) {
145 SetDlgItemText(ID_PROMPT, Prompt);
146 SetDlgItemText(ID_INPUT, Buffer);
147 }
148 else if (direction == tdGetData) {
149 GetDlgItemText(ID_INPUT, Buffer, BufferSize);
150 }
151}
152
153//
154/// Calls TDialog::SetupWindow to set up the window, then limits the number of
155/// characters the user can enter to bufferSize - 1.
156//
157void
163
164
166
167#if OWL_PERSISTENT_STREAMS
168
169//
170/// Reads an instance of TInputDialog from the passed ipstream
171//
172void*
173TInputDialog::Streamer::Read(ipstream& is, uint32 /*version*/) const
174{
175 ReadBaseObject((TDialog*)GetObject(), is);
176#if defined(UNICODE)
178 char * prompt = is.freadString();
179
180 GetObject()->Prompt = strnewdup(_A2W(prompt));
181
182 delete[] prompt;
183#else
184 GetObject()->Prompt = is.freadString();
185#endif
186 return GetObject();
187}
188
189//
190/// Writes the TInputDialog to the passed opstream
191//
192void
193TInputDialog::Streamer::Write(opstream& os) const
194{
195 WriteBaseObject((TDialog*)GetObject(), os);
197 os.fwriteString(_W2A(GetObject()->Prompt));
198}
199
200
201#endif
202
203} // OWL namespace
204/* ========================================================================== */
205
#define PRECONDITION(condition)
Definition checks.h:227
Typically used to obtain information from a user, a dialog box is a window inside of which other cont...
Definition dialog.h:85
void SetupWindow() override
Overrides the virtual function defined in TWindow.
Definition dialog.cpp:825
A TEdit is an interface object that represents an edit control interface element.
Definition edit.h:34
Provides a generic dialog box to retrieve text input by a user.
Definition inputdia.h:35
void TransferData(TTransferDirection)
Transfers the data of the input dialog box.
Definition inputdia.cpp:142
TInputDialog(TWindow *parent, LPCTSTR title, LPCTSTR prompt, TCHAR *buffer, int bufferSize, TModule *module=0, TValidator *valid=0)
Let the compiler know that the following template instances will be defined elsewhere.
Definition inputdia.cpp:29
~TInputDialog()
Destructor for this class.
Definition inputdia.cpp:129
void SetupWindow()
Calls TDialog::SetupWindow to set up the window, then limits the number of characters the user can en...
Definition inputdia.cpp:158
ObjectWindows dynamic-link libraries (DLLs) construct an instance of TModule, which acts as an object...
Definition module.h:75
A streamable class, TValidator defines an abstract data validation object.
Definition validate.h:71
TWindow, derived from TEventHandler and TStreamableBase, provides window-specific behavior and encaps...
Definition window.h:414
void SetCaption(LPCTSTR title)
Copies title to an allocated string pointed to by title.
Definition window.cpp:3410
TResult SendDlgItemMessage(int childId, TMsgId, TParam1=0, TParam2=0)
Sends a message (msg) to the control specified in childId.
Definition window.h:2092
ipstream, a specialized input stream derivative of pstream, is the base class for reading (extracting...
Definition objstrm.h:391
#define _T(x)
Definition cygwin.h:51
Definition of class TEdit.
void ReadBaseObject(Base *base, ipstream &in)
Definition objstrm.h:1159
#define IMPLEMENT_STREAMABLE2(cls, base1, base2)
Definition objstrm.h:1726
void WriteBaseObject(Base *base, opstream &out)
Definition objstrm.h:1150
TTransferDirection
The TTransferDirection enum describes the constants that the transfer function uses to determine how ...
Definition window.h:92
@ tdSetData
Set data from the buffer into the window.
Definition window.h:94
@ tdGetData
Get data from the window into the buffer.
Definition window.h:93
Definition of TInputDialog class.
#define _W2A(lpw)
Definition memory.h:219
char * strnewdup(const char *s, size_t minAllocSize=0)
Definition memory.cpp:25
#define _A2W(lpw)
Definition memory.h:220
#define _USES_CONVERSION
Definition memory.h:217
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
void SetDlgItemText(HWND ctrl, const tstring &text)
String overload.
Definition transfer.h:63
unsigned long uint32
Definition number.h:34
char tchar
Definition defs.h:77
OWL_DIAGINFO
Definition animctrl.cpp:14
std::string tstring
Definition defs.h:79
tstring GetDlgItemText(HWND ctrl)
String overload.
Definition transfer.h:57