OWLNext    7.0
Borland's Object Windows Library for the modern age
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
editview.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectWindows
3// Copyright (c) 1993, 1996 by Borland International, All Rights Reserved
4//
5/// \file
6/// Implementation of class TEditView
7//----------------------------------------------------------------------------
8#include <owl/pch.h>
9#include <owl/editview.h>
10#include <owl/docview.rh>
11#include <owl/editview.rh>
12#include <memory>
13
14using namespace std;
15
16namespace owl {
17
19DIAG_DECLARE_GROUP(OwlDocView); // General Doc/View diagnostic group
20
21
22//
23// TEditView response table
24//
25DEFINE_RESPONSE_TABLE1(TEditView, TEditSearch)
33
34//
35/// Creates a TEditView object associated with the specified document and parent
36/// window. Sets AttrAccelTable to IDA_EDITVIEW to identify the edit view. Sets the
37/// TView private data member ViewMenu to the new TMenuDescr for this view.
38//
40:
41 TEditSearch(parent, GetNextViewId(), nullptr),
42 TView(doc),
43 Origin(0)
44{
45 Attr.AccelTable = IDA_EDITVIEW;
46 if (::FindResource(*GetModule(), TResId(IDM_EDITVIEW), RT_MENU))
47 SetViewMenu(new TMenuDescr(IDM_EDITVIEW, 0,2,0,0,0,1, GetModule()));
48}
49
50//
51/// Used internally by TEditView to manage memory.
52/// This member is not available under Presentation Manager.
53//
54void
56{
57 TEditSearch::EvNCDestroy();// call TWindow::EvNCDestroy, this may be deleted
58}
59
60//
61/// Destroys a TEditView object.
62//
66
67//
68/// Returns a nonzero value if the window's handle passed in hWnd is the same as
69/// that of the view's display window or a child of it.
70//
71bool
76
77//
78/// Indicates that the document has been closed. mode is one of the ofxxxx document
79/// open enum constants.
80//
81bool
83{
84 if (VnIsDirty() || !(omode & ofWrite)) // make sure someone else's write
85 return false;
86
87 int top = GetFirstVisibleLine();
88 int selbeg;
89 int selend;
92 LoadData();
93 Scroll(0, top);
95
96 return true;
97}
98
99//
100/// Reads the view from the stream and closes the file. It returns a nonzero value
101/// if the view was successfully loaded. If the file cannot be read, LoadData posts
102/// an error and returns 0.
103//
104bool
106{
107 unique_ptr<tistream> inStream (Doc->InStream(ios::in | ios::binary));
108 if (!inStream.get())
109 {
110 Doc->PostError(IDS_UNABLEOPEN, MB_OK);
111 return false;
112 }
113 inStream->seekg(0, ios::end);
114 streamsize count = static_cast<streamsize>(inStream->tellg() - streampos(0));
115 inStream->seekg(0, ios::beg);
116
117 // TODO: Widen the parameter type to LockBuffer to avoid this check and narrowing cast.
118 //
119 if (!IsRepresentable<uint>(count + 1)) {
120 Doc->PostError(IDS_NOMEMORYFORVIEW, MB_OK);
121 return false;
122 }
123 LPTSTR buf = LockBuffer(static_cast<uint>(count + 1));
124 if (!buf) {
125 Doc->PostError(IDS_NOMEMORYFORVIEW, MB_OK);
126 return false;
127 }
128
129 inStream->read(buf, count);
130 bool status = (inStream->gcount() == count);
131 buf[count] = 0; // 0 terminate buffer
132 UnlockBuffer(buf, true);
133 if (!status)
134 Doc->PostError(IDS_READERROR, MB_OK);
135 return status;
136}
137
138//
139/// Creates the view's window. Calls GetDocPath to determine if the file is new or
140/// if it already has data. If there is data, calls LoadData to add the data to the
141/// view. If the view's window cannot be created, Create indicates that the view is
142/// invalid.
143//
144bool
146{
147 try {
148 TEditSearch::Create(); // throws exception TWindow::TXWindow
149 }
150 catch(TXOwl&) {
151 Doc->PostError(IDS_NOMEMORYFORVIEW, MB_OK);
152 NotOK();
153 return true; // cannot return false - throws another exception
154 }
155 if (Doc->GetDocPath() == nullptr) {
156 return true; // new file, no data to display
157 }
158 if (!LoadData())
159 NotOK();
160 return true;
161}
162
163//
164/// Commits changes made in the view to the document. If force is nonzero, all data,
165/// even if unchanged, is saved to the document.
166//
167bool
169{
170 if (!force && !(VnIsDirty()))
171 return true;
172
173 unique_ptr<tostream> outStream(Doc->OutStream(ios::out | ios::binary));
174 if (!outStream.get())
175 {
176 Doc->PostError(IDS_UNABLEOPEN, MB_OK);
177 return false;
178 }
179 outStream->seekp(Origin);
180
181 bool status = false;
182 LPTSTR buf = LockBuffer();
183 if (buf) {
184 streamsize count = ::_tcslen(buf);
185 outStream->write(buf, count);
186 status = outStream->good();
187 UnlockBuffer(buf);
188 ClearModify(); // reset edit control
189 }
190 if (!status)
191 Doc->PostError(IDS_WRITEERROR, MB_OK);
192
193 return status;
194}
195
196//
197/// Returns a nonzero value if changes made to the view should be erased and the
198/// data from the document should be restored to the view. If clear is nonzero, the
199/// data is cleared instead of restored to the view.
200//
201bool
203{
204 TEdit::Clear();
205 ClearModify(); // reset edit control
206 return clear ? true : LoadData();
207}
208
209
211
212#if OWL_PERSISTENT_STREAMS
213
214//
215//
216//
217void*
218TEditView::Streamer::Read(ipstream& is, uint32 /*version*/) const
219{
220 ReadBaseObject((TEditSearch*)GetObject(), is);
221 ReadBaseObject((TView*)GetObject(), is);
222 is >> GetObject()->Origin;
223 return GetObject();
224}
225
226//
227//
228//
229void
230TEditView::Streamer::Write(opstream& os) const
231{
232 WriteBaseObject((TEditSearch*)GetObject(), os);
233 WriteBaseObject((TView*)GetObject(), os);
234 os << GetObject()->Origin;
235}
236
237#endif
238
239} // OWL namespace
240
#define DIAG_DECLARE_GROUP(group)
Definition checks.h:404
An abstract base class, TDocument is the base class for all document objects and serves as an interfa...
Definition docview.h:187
TEditSearch is an edit control that responds to Find, Replace, and FindNext menu commands.
Definition editsear.h:35
Derived from TView and TEditSearch, TEditView provides a view wrapper for the ObjectWindows text edit...
Definition editview.h:34
bool VnCommit(bool force)
Commits changes made in the view to the document.
Definition editview.cpp:168
void EvNCDestroy()
Used internally by TEditView to manage memory.
Definition editview.cpp:55
bool VnDocClosed(int omode)
Indicates that the document has been closed.
Definition editview.cpp:82
bool VnRevert(bool clear)
Returns a nonzero value if changes made to the view should be erased and the data from the document s...
Definition editview.cpp:202
auto Create() -> bool override
< String-aware overload
Definition editview.cpp:145
~TEditView() override
Destroys a TEditView object.
Definition editview.cpp:63
bool VnIsDirty()
Returns a nonzero value if changes made to the data in the view have not been saved to the document; ...
Definition editview.h:144
bool LoadData()
Reads the view from the stream and closes the file.
Definition editview.cpp:105
bool VnIsWindow(HWND hWnd)
Returns a nonzero value if the window's handle passed in hWnd is the same as that of the view's displ...
Definition editview.cpp:72
Menu with information used to allow merging.
Definition menu.h:206
Abstract base class for view access from document.
Definition docview.h:397
void NotOK()
To flag errors in creation.
Definition docview.h:1093
TWindow, derived from TEventHandler and TStreamableBase, provides window-specific behavior and encaps...
Definition window.h:414
virtual bool Create()
Creates the window interface element to be associated with this ObjectWindows interface element.
Definition window.cpp:2399
bool IsChild(HWND hWnd) const
Returns true if the window is a child window or a descendant window of this window.
Definition window.h:3176
void EvNCDestroy()
Responds to an incoming WM_NCDESTROY message, the last message sent to an MS-Windows interface elemen...
Definition window.cpp:2871
HWND GetHandle() const
Returns the handle of the window.
Definition window.h:2020
TXOwl is root class of the ObjectWindows exception hierarchy.
Definition except.h:38
ipstream, a specialized input stream derivative of pstream, is the base class for reading (extracting...
Definition objstrm.h:391
#define _tcslen
Definition cygwin.h:74
#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_STREAMABLE2(cls, base1, base2)
Definition objstrm.h:1726
void WriteBaseObject(Base *base, opstream &out)
Definition objstrm.h:1150
void Scroll(int horizontalUnit, int verticalUnit)
Scroll the text by the specified horizontal and vertical amounts.
Definition edit.h:497
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
int GetFirstVisibleLine() const
Indicates the topmost visible line in an edit control.
Definition edit.h:640
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
virtual bool SetSelection(int startPos, int endPos)
Select the characters in the range "startPos .. endPos".
Definition edit.h:430
TRange GetSelection() const
Functional style overload.
Definition edit.cpp:979
@ ofWrite
ios::out, open for writing
Definition docview.h:64
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
EV_VN_REVERT
Definition editview.cpp:30
EV_VN_DOCCLOSED
Definition editview.cpp:26
EV_WM_NCDESTROY
Definition editview.cpp:31
EV_VN_ISWINDOW
Definition commview.cpp:26
EV_VN_COMMIT
Definition editview.cpp:29
OWL_DIAGINFO
Definition animctrl.cpp:14
END_RESPONSE_TABLE
Definition button.cpp:26
EV_VN_ISDIRTY
Definition editview.cpp:28