OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
editfile.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 class TEditFile, a text edit which can find/replace and
7/// read/write from/to a file.
8//----------------------------------------------------------------------------
9#include <owl/pch.h>
10#include <owl/window.h>
11#include <owl/control.h>
12#include <owl/edit.h>
13#include <owl/editsear.h>
14#include <owl/editfile.h>
15#include <owl/file.h>
16#include <owl/applicat.h>
17#include <owl/system.h>
18#include <string.h>
19#include <stdio.h>
20
21namespace owl {
22
24
25
26DEFINE_RESPONSE_TABLE1(TEditFile, TEditSearch)
27// !!! BUG EV_COMMAND(CM_FILENEW, CmFileNew), // BUG !!!!!!!
28// !!! BUG EV_COMMAND(CM_FILEOPEN, CmFileOpen), // BUG !!!!!!!
29 EV_COMMAND(CM_FILESAVE, CmFileSave),
30 EV_COMMAND(CM_FILESAVEAS, CmFileSaveAs),
33
34//
35/// Constructs a TEditFile window given the parent window, resource ID (Id),
36/// text, file name, and module ID. Sets Filename to fileName.
37//
39 int id,
41 int x, int y, int w, int h,
43 TModule* module)
44:
45 TEditSearch(parent, id, text, x, y, w, h, module)
46{
47 FileName = fileName ? strnewdup(fileName) : nullptr;
48}
49
50//
51/// String-aware overload
52//
54 TWindow* parent,
55 int id,
56 const tstring& text,
57 int x, int y, int w, int h,
58 const tstring& fileName,
59 TModule* module
60 )
61 : TEditSearch(parent, id, text, x, y, w, h, module),
62 FileName (fileName.empty() ? nullptr : strnewdup(fileName.c_str()))
63{}
64
65//
66/// Frees memory allocated to hold the name of the TEditFile.
67//
70 delete[] FileName;
71}
72
73//
74/// Creates the edit window's Editor edit control by calling TEditFile::SetupWindow().
75/// Sets the window's caption to FileName, if available; otherwise sets the name to
76/// "Untitled."
77//
78void
80{
83
84 FileData.Flags |= OFN_LONGNAMES;
85
86 FileData.Flags |= OFN_EXPLORER;
87
89
90 SetFileName(FileName);
91 if (FileName && !Read())
92 {
94 SetFileName(nullptr);
95 }
96}
97
98//
99/// Sets FileName and updates the caption of the window,
100/// replacing an empty name with 'Untitled' in its caption
101//
102void
104{
105 if (fileName != FileName) {
106 delete[] FileName;
107 FileName = fileName ? strnewdup(fileName) : nullptr;
108 }
110 SetDocTitle(FileName ? FileName : untitled.c_str(), 0);
111}
112
113//
114/// Begins the edit of a new file after calling CanClear to determine that it is
115/// safe to clear the text of the editor.
116//
117void
119{
120 if (CanClear()) {
121 Clear();
122 Invalidate();
123 ClearModify();
124 SetFileName(nullptr);
125 }
126}
127
128//
129/// Calls SetFileName and Read to replace the file currently being edited with a
130/// file whose name is supplied.
131//
132void
145
146//
147/// Opens a new file after determining that it is OK to clear the text of the
148/// Editor. Calls CanClear, and if true is returned, brings up a file dialog box to
149/// retrieve the name of a new file from the user. Calls ReplaceWith to pass the
150/// name of the new file.
151//
152/// Same as selecting File|Open from the menus
153//
154void
156{
157 if (CanClear()) {
158 *FileData.FileName = 0;
159 if (TFileOpenDialog(this, FileData).Execute() == IDOK)
160 ReplaceWith(FileData.FileName);
161 }
162}
163
164//
165/// Reads the contents of a previously specified file into the Editor. Returns true
166/// if read operation is successful.
167/// The caller is responsible for any error UI
168//
169bool
171{
172 if (!fileName)
173 {
174 if (FileName)
175 fileName = FileName;
176 else
177 return false;
178 }
179
180 bool success = false;
182
183 if (file.IsOpen()) {
184 long charsToRead = file.Length();
185 if (charsToRead >= 0) {
186 Clear();
187
188 // Lock and resize Editor's buffer to the size of the file
189 // Then if OK, read the file into editBuffer
190 //
192 if (editBuffer) {
194
195 // 0 terminate Editor's buffer
196 //
198 success = true;
199 ClearModify();
200 }
202 }
203 }
204 }
205 return success;
206}
207
208//
209/// Saves changes to the contents of the Editor to a file. If Editor->IsModified
210/// returns false, Save returns true, indicating there have been no changes since
211/// the last open or save.
212//
213bool
215{
216 if (IsModified())
217 {
218 if (!FileName)
219 return SaveAs();
220
221 if (!Write())
222 {
224 return false;
225 }
226 }
227 return true; // editor's contents haven't been changed
228}
229
230//
231/// Saves the contents of the Editor to a file whose name is retrieved from the
232/// user, through execution of a File Save dialog box. If the user selects OK,
233/// SaveAs calls SetFileName and Write. Returns true if the file was saved.
234//
235bool
237{
238 if (FileName)
239 ::_tcscpy(FileData.FileName, FileName);
240
241 else
242 *FileData.FileName = 0;
243
244 if (TFileSaveDialog(this, FileData).Execute() == IDOK)
245 {
246 if (Write(FileData.FileName))
247 {
248 SetFileName(FileData.FileName);
249 return true;
250 }
252 }
253 return false;
254}
255
256//
257/// Enables save command (only if text is modified).
258//
259void
264
265//
266/// Saves the contents of the Editor to a file whose name is specified by FileName.
267/// Returns true if the write operation is successful.
268/// The caller is responsible for any error UI
269//
270bool
272{
273 if (!fileName)
274 {
275 if (FileName)
276 fileName = FileName;
277 else
278 return false;
279 }
280
282 if (!file.IsOpen())
283 return false;
284
285 bool success = false;
287 if (editBuffer) {
288 success = file.Write(editBuffer, static_cast<int>(::_tcslen(editBuffer)));
290 if (success)
291 ClearModify();
292 }
293 return success;
294}
295
296//
297/// Returns true if the text of the associated edit control can be cleared
298/// which is if the text has not been changed, or if the user Oks the
299/// clearing of the text
300//
301bool
303{
304 if (IsModified())
305 {
307
309 FileName ? FileName : untitled.c_str());
310
311 return result==IDYES ? Save() : result != IDCANCEL;
312 }
313 return true;
314}
315
316//
317/// Returns true if the edit window can be closed.
318//
319bool
321{
322 return CanClear();
323}
324
325
327
328#if OWL_PERSISTENT_STREAMS
329
330//
331// reads an instance of TEditFile from the passed ipstream
332//
333void*
334TEditFile::Streamer::Read(ipstream& is, uint32 /*version*/) const
335{
336 TEditFile* o = GetObject();
338
339#if defined(UNICODE)
341 char * fileName = is.freadString();
342
343 o->FileName = _A2W(fileName);
344
345 delete[] fileName;
346#else
347 o->FileName = is.freadString();
348#endif
349 if (!*o->FileName) {
350 delete o->FileName;
351 o->FileName = 0;
352 }
353 return o;
354}
355
356//
357// writes the TEditFile to the passed opstream
358//
359void
360TEditFile::Streamer::Write(opstream& os) const
361{
362 TEditFile* o = GetObject();
363 WriteBaseObject((TEditSearch*)o, os);
365 os.fwriteString(o->FileName ? _W2A(o->FileName) : "");
366}
367
368#endif
369
370
371} // OWL namespace
372
Definition of class TApplication.
Base class for an extensible interface for auto enabling/disabling of commands (menu items,...
Definition window.h:209
TEditFile is a file-editing window.
Definition editfile.h:35
virtual void ReplaceWith(LPCTSTR fileName)
Calls SetFileName and Read to replace the file currently being edited with a file whose name is suppl...
Definition editfile.cpp:133
void SetFileName(LPCTSTR fileName)
Sets FileName and updates the caption of the window, replacing an empty name with 'Untitled' in its c...
Definition editfile.cpp:103
virtual void NewFile()
Begins the edit of a new file after calling CanClear to determine that it is safe to clear the text o...
Definition editfile.cpp:118
virtual bool SaveAs()
Saves the contents of the Editor to a file whose name is retrieved from the user, through execution o...
Definition editfile.cpp:236
void CmSaveEnable(TCommandEnabler &commandHandler)
Enables save command (only if text is modified).
Definition editfile.cpp:260
~TEditFile() override
Frees memory allocated to hold the name of the TEditFile.
Definition editfile.cpp:68
virtual bool Write(LPCTSTR fileName=nullptr)
Saves the contents of the Editor to a file whose name is specified by FileName.
Definition editfile.cpp:271
auto CanClose() -> bool override
Returns true if the edit window can be closed.
Definition editfile.cpp:320
virtual bool Read(LPCTSTR fileName=nullptr)
Reads the contents of a previously specified file into the Editor.
Definition editfile.cpp:170
virtual bool Save()
Saves changes to the contents of the Editor to a file.
Definition editfile.cpp:214
virtual bool CanClear()
Returns true if the text of the associated edit control can be cleared which is if the text has not b...
Definition editfile.cpp:302
void SetupWindow() override
Creates the edit window's Editor edit control by calling TEditFile::SetupWindow().
Definition editfile.cpp:79
virtual void Open()
Opens a new file after determining that it is OK to clear the text of the Editor.
Definition editfile.cpp:155
TEditFile(TWindow *parent=nullptr, int id=0, LPCTSTR text=nullptr, int x=0, int y=0, int w=0, int h=0, LPCTSTR fileName=nullptr, TModule *module=nullptr)
Constructs a TEditFile window given the parent window, resource ID (Id), text, file name,...
Definition editfile.cpp:38
TEditSearch is an edit control that responds to Find, Replace, and FindNext menu commands.
Definition editsear.h:35
The TFile class encapsulates standard file characteristics and operations.
Definition file.h:120
@ ReadOnly
Definition file.h:130
@ WriteOnly
Definition file.h:131
@ OpenExisting
Opens the file. The function fails if the file does not exist.
Definition file.h:151
@ PermExclusive
Definition file.h:141
@ CreateAlways
Creates a new file. The function overwrites the file if it exists.
Definition file.h:148
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
TCHAR * FileName
Holds the name of the file to be saved or opened.
Definition opensave.h:112
uint32 Flags
Flag contains one or more of the following constants:
Definition opensave.h:80
void SetFilter(LPCTSTR filter=nullptr)
Makes a copy of the filter list used to display the file names.
Definition opensave.cpp:141
TWindow, derived from TEventHandler and TStreamableBase, provides window-specific behavior and encaps...
Definition window.h:414
tstring LoadString(uint id) const
Definition window.h:608
virtual int Execute()
Creates the underlying HWND and makes it modal with the help of TApplication's BeginModal support.
Definition window.cpp:2498
auto FormatMessageBox(const tstring &formatStr, const tstring &caption, uint flags,...) const -> int
Definition window.cpp:4361
virtual bool SetDocTitle(LPCTSTR docname, int index)
Default behavior for updating document title is to pass it to parent frame.
Definition window.cpp:778
virtual void Invalidate(bool erase=true)
Invalidates (mark for painting) the entire client area of a window.
Definition window.h:2822
ipstream, a specialized input stream derivative of pstream, is the base class for reading (extracting...
Definition objstrm.h:391
Definition of class TControl.
#define _tcscpy
Definition cygwin.h:79
#define _tcslen
Definition cygwin.h:74
#define _T(x)
Definition cygwin.h:51
Definition of class TEdit.
Definition of class TEditFile, a text edit which can find/replace and read/write from/to a file.
Definition of class TEditSearch, an edit control that responds to Find, Replace and FindNext menu com...
#define DEFINE_RESPONSE_TABLE1(cls, base)
Macro to define a response table for a class with one base.
Definition eventhan.h:492
void ReadBaseObject(Base *base, ipstream &in)
Definition objstrm.h:1159
#define IMPLEMENT_STREAMABLE1(cls, base1)
Definition objstrm.h:1725
void WriteBaseObject(Base *base, opstream &out)
Definition objstrm.h:1150
void SetupWindow() override
Definition editsear.cpp:80
void ClearModify()
Resets the change flag of the edit control causing IsModified to return false.
Definition edit.h:276
void UnlockBuffer(LPCTSTR buffer, bool updateHandle=false)
Unlock the edit control's buffer locked by LockBuffer.
Definition edit.cpp:604
bool IsModified() const
Returns true if the user has changed the text in the edit control.
Definition edit.h:418
TCHAR * LockBuffer(uint newSize=0)
Lock and unlock this edit control's buffer.
Definition edit.cpp:571
void Clear() override
Override TStatic virtual member functions.
Definition edit.cpp:421
#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
unsigned long uint32
Definition number.h:34
OWL_DIAGINFO
Definition animctrl.cpp:14
END_RESPONSE_TABLE
Definition button.cpp:26
std::string tstring
Definition defs.h:79
unsigned int uint
Definition number.h:25
Definition of class TString, a flexible universal string envelope class.
Definition of TSystem, a system information provider class.
Base window class TWindow definition, including HWND encapsulation.
#define EV_COMMAND_ENABLE(id, method)
Response table entry for enabling a command.
Definition windowev.h:193
#define EV_COMMAND(id, method)
Response table entry for a menu/accelerator/push button message.
Definition windowev.h:171