OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
richedv.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectWindows
3// Copyright (c) 1998 by Yura Bidus, All Rights Reserved
4//
5/// \file
6/// Implementation of class TRichEditView
7//----------------------------------------------------------------------------
8#include <owl/pch.h>
9
10#include <owl/richedv.h>
11#include <owl/choosefo.h>
12#include <owl/chooseco.h>
13#include <owl/docview.rh>
14#include <owl/richedv.rh>
15
16#include <stdio.h>
17
18#if defined(__BORLANDC__)
19# pragma option -w-ccc // Disable "Condition is always true/false"
20#endif
21
22using namespace std;
23
24namespace owl {
25
27DIAG_DECLARE_GROUP(OwlDocView); // General Doc/View diagnostic group
28
29
30// Makes it easier to read when specifying offsets. Converts x to
31// thousandths of inches. Make sure x is decimal, not a fraction ( .25
32// instead of 1/4).
33//
34#define TOHIENGLISH(x) static_cast<long> ((1000)*(x))
35
36
37DEFINE_RESPONSE_TABLE1(TRichEditView, TRichEdit)
38 EV_COMMAND(CM_FILESAVE, CmFileSave),
39 EV_COMMAND(CM_FILESAVEAS, CmFileSaveAs),
40// EV_COMMAND(CM_FILEOPEN, CmFileOpen),
41// EV_COMMAND(CM_FILENEW, CmFileNew),
42 EV_COMMAND(CM_FILECLOSE, CmFileClose),
43 EV_COMMAND(CM_FORMATFONT, CmFormatFont),
45 EV_COMMAND(CM_FORMATCOLOR, CmFormatColor),
47 EV_COMMAND(CM_FORMATBKCOLOR, CmFormatBkColor),
49 EV_COMMAND(CM_PARAGRAPHLEFT, CmParagraphLeft),
51 EV_COMMAND(CM_PARAGRAPHCENTER, CmParagraphCenter),
53 EV_COMMAND(CM_PARAGRAPHRIGHT, CmParagraphRight),
55 EV_COMMAND(CM_PARAGRAPHBULLET, CmParagraphBullet),
57 EV_COMMAND(CM_FORMATBOLD, CmFormatBold),
59 EV_COMMAND(CM_FORMATITALIC, CmFormatItalic),
61 EV_COMMAND(CM_FORMATUNDERLINE, CmFormatUnderline),
63 EV_COMMAND(CM_FORMATSTRIKEOUT, CmFormatStrikeout),
70// EV_WM_RBUTTONDOWN,
72
73//
74// Create the rich edit control object
75//
77:
78 TRichEdit(parent, GetNextViewId(), (LPCTSTR)nullptr,0,0,0,0),
79 TView(doc)
80{
81 Attr.AccelTable = IDA_RICHEDITVIEW;
82 if (::FindResource(*GetModule(), TResId(IDM_RICHEDITVIEW), RT_MENU))
84}
85
86
90
91
92//
93// Does a given HWND belong to this view? Yes if it is us, or a child of us
94//
95bool
100
101
102//
103// Our document object was closed
104//
105bool
107{
108 if (VnIsDirty() || !(omode & ofWrite)) // make sure someone else's write
109 return false;
110
111 LoadData();
112 return true;
113}
114
115
116//
117//
118//
119bool
121{
122 TPointer<tistream> inStream(Doc->InStream(ios::in | ios::binary));
123 if (!inStream) {
124 Doc->PostError(IDS_UNABLEOPEN, MB_OK);
125 return false;
126 }
127 // Could check for a valid file (eg. FileSize != 0)
128 // before proceeding with a call to Clear() here.
129 //
130 Clear();
131
132 // Stream in data from file
133 //
134 bool status = ReadFromStream(*inStream, GetFormat());
135 if (!status){
136 Doc->PostError(IDS_READERROR, MB_OK);
137 }
138 else
139 ClearModify();
140
141 return status;
142}
143
144
145//
146// Create the control with scroll bars for easy text editing
147//
148bool
150{
151 //Attr.Style |= ( WS_HSCROLL | WS_VSCROLL /*| ES_AUTOHSCROLL*/);
152
153 try {
154 TRichEdit::Create(); // throws exception TWindow::TXWindow
155 }
156 catch (TXOwl&) {
157 Doc->PostError(IDS_NOMEMORYFORVIEW, MB_OK);
158 NotOK();
159 return true; // cannot return false - throws another exception
160 }
161 if (Doc->GetDocPath() == nullptr) {
162 return true; // new file, no data to display
163 }
164 if (!LoadData())
165 NotOK();
166 return true;
167}
168
169//
170//
171//
172bool
174{
175 if (!force && !VnIsDirty())
176 return true;
177
178 TPointer<tostream> outStream(Doc->OutStream(ios::out | ios::binary));
179 if(!outStream){
180 Doc->PostError(IDS_UNABLEOPEN, MB_OK);
181 return false;
182 }
183
184 // in case user change template ?????
185 //
186// if(!_tcscmp(GetDocument().GetTemplate()->GetDefaultExt(),_T("rtf"))){
187// SetFormat(SF_RTF);
188// SetViewMenu( new TMenuDescr(IDM_RICHEDITVIEW,0,2,0,2,0,0) );
189// }
190// else{
191// SetFormat(SF_TEXT);
192// SetViewMenu( new TMenuDescr(IDM_EDITVIEW,0,2,0,0,0,0) );
193// }
194
195 bool status = WriteToStream(*outStream, GetFormat());
196
197 if (!status)
198 Doc->PostError(IDS_WRITEERROR, MB_OK);
199 else
200 ClearModify();
201
202 return status;
203}
204
205
206//
207// Erase all changes since last save
208//
209bool
211{
213 ClearModify(); // reset edit control
214 return clear ? true : LoadData();
215}
216
217
218//
219// Set the font
220//
221void
223{
224 // Select all the text in the new richedit. Actually, there shouldn't
225 // be any, but there seems to be some character format junk already
226 // there. If we don't select it too, it won't be changed by the
227 // call to SetCharFormat() below, which means if the user cursors
228 // to the last position in the richedit and types, he will be
229 // typing in the default font (not our Courier New).
230 //
231 SetSelection(0,2);
232
233 // Set the default format to an easy-reading font so everything lines
234 // up
235 //
236 TCharFormat defaultFmt( *this, false);
237 defaultFmt.SetFaceName(_T("Courier New"));
238 defaultFmt.EnableBold(false);
239
241 SetSelection(0,0);
242
243 // Oodles and oodles of text
244 //
245 SendMessage( EM_EXLIMITTEXT, 0, (DWORD)0xFFFFFFF );
246 ClearModify();
247}
248
249//
250// We need to make CanClose() a dummy function and let the DocManager
251// determine if doc needs to be saved. If we don't, the user will get
252// two "you gonna save this before you close it, you moron?" message
253// boxes.
254//
255bool
257{
258 // Return true to let it fall through (pardon the inadvertent rhyme)
259 //
260 return true;
261}
262
263// menu response functions...
264
265//
266//
267//
268void
270{
271 //
272 // Retrieve current character format
273 //
274 TCharFormat chFormat(*this);
275
276 //
277 // Fill logfont structure using info. from character format
278 //
279 LOGFONT lf;
280 chFormat.SetFontInfo(lf);
281
282 //
283 // Initialize structure for Font common dialog
284 //
286 fontData.LogFont = lf;
287 fontData.DC = TClientDC(*this);
289 fontData.Color = chFormat.GetTextColor();
290 fontData.FontType = SCREEN_FONTTYPE;
291
292 //
293 // Display dialog
294 //
295 if (TChooseFontDialog(this, fontData).Execute() == IDOK) {
296 //
297 // Update character format from chosen font info
298 //
299 chFormat.GetFontInfo(fontData.LogFont);
300 chFormat.SetTextColor(fontData.Color);
302 }
303}
304
305//
306//
307//
308void
313
314//
315//
316//
317void
322
323//
324//
325//
326void
331
332//
333//
334//
335void
340
341
342//
343//
344//
345void
349
350//
351//
352//
353void
355{
357 if( result == Yes )
358 ce.SetCheck( TCommandEnabler::Checked );
359 else if( result == No )
360 ce.SetCheck( TCommandEnabler::Unchecked );
361 else
363}
364
365//
366//
367//
368void
370{
372 if( fmt == Yes )
373 ce.SetCheck( TCommandEnabler::Checked );
374 else
375 if( fmt == No )
376 ce.SetCheck( TCommandEnabler::Unchecked );
377 else
379}
380
381//
382//
383//
384void
386{
388 if( result == Yes )
389 ce.SetCheck( TCommandEnabler::Checked );
390 else
391 if( result == No )
392 ce.SetCheck( TCommandEnabler::Unchecked );
393 else
395}
396
397//
398//
399//
400void
402{
404 if( result == Yes )
405 ce.SetCheck( TCommandEnabler::Checked );
406 else
407 if( result == No )
408 ce.SetCheck( TCommandEnabler::Unchecked );
409 else
411}
412
413
414//
415//
416//
417void
425
426//
427//
428//
429void
437
438//
439//
440//
441void
449
450//
451//
452//
453void
455{
457
458 pFormat.SetNumbering((pFormat.wNumbering == 0) ? (WORD)PFN_BULLET : (WORD)0);
459 pFormat.SetOffset(TOHIENGLISH( .25 )); // 1/4 inch offset. The
460 // richedit control operates
461 // in MM_HIENGLISH
463}
464
465//
466//
467//
468void
476
477//
478//
479//
480void
488
489//
490//
491//
492void
500
501//
502//
503//
504void
512
513
514//
515//
516//
517void
519{
521 TCharFormat cf(*this);
522 static TColor custColors[16];
523
524 data.Flags = CC_RGBINIT;
525 data.Color = cf.GetTextColor();
526 data.CustColors = custColors;
527
528 if( TChooseColorDialog(this, data).Execute() == IDOK ) {
529 cf.SetTextColor( data.Color );
530 SetCharFormat( cf );
531 }
532}
533
534//
535//
536//
537void
542//
543//
544//
545void
547{
549 TCharFormat cf(*this);
550 static TColor custColors[16];
551
552 data.Flags = CC_RGBINIT;
553 data.Color = cf.GetBkColor();
554 data.CustColors = custColors;
555
556 if( TChooseColorDialog(this, data).Execute() == IDOK ) {
557 cf.SetBkColor( data.Color );
558 SetCharFormat( cf );
559 }
560}
561
562//
563//
564//
565void
570
572
573#if OWL_PERSISTENT_STREAMS
574//
575//
576void*
577TRichEditView::Streamer::Read(ipstream& is, uint32 /*version*/) const
578{
579 ReadBaseObject((TRichEdit*)GetObject(), is);
580 ReadBaseObject((TView*)GetObject(), is);
581 return GetObject();
582}
583
584//
585//
586//
587void
588TRichEditView::Streamer::Write(opstream& os) const
589{
590 WriteBaseObject((TRichEdit*)GetObject(), os);
591 WriteBaseObject((TView*)GetObject(), os);
592}
593
594#endif
595
596} // OWL namespace
597
598//==============================================================================
#define DIAG_DECLARE_GROUP(group)
Definition checks.h:404
Definition of Choose Color Common Dialog class.
Definition of Choose Font Common Dialog class.
TCharFormat encapsulates the CHARFORMAT2 structure which contains information about character formatt...
Definition richedit.h:56
TColor GetBkColor() const
Retrieves the character background color stored in the CHARFORMAT structure.
Definition richedit.cpp:305
void SetTextColor(const TColor &=TColor::None)
Updates the CHARFORMAT structure with the specified color.
Definition richedit.cpp:151
TColor GetTextColor() const
Retrieves the character color stored in the CHARFORMAT structure.
Definition richedit.cpp:138
void SetBkColor(const TColor &clr)
Background color.
Definition richedit.cpp:316
Defines information necessary to initialize the dialog box with the user's color selection.
Definition chooseco.h:43
Wrapper for the Choose-Color common dialog.
Definition chooseco.h:38
Defines information necessary to initialize the dialog box with the user's font selection.
Definition choosefo.h:47
Encapsulates the Choose-Font common dialog.
Definition choosefo.h:41
A device context class derived from TWindowDC, TClientDC provides access to the client area owned by ...
Definition dc.h:640
Class wrapper for management of color values.
Definition color.h:245
Base class for an extensible interface for auto enabling/disabling of commands (menu items,...
Definition window.h:209
@ Unchecked
Command is not enabled.
Definition window.h:226
@ Checked
Command is enabled.
Definition window.h:227
@ Indeterminate
Command is unavaiable.
Definition window.h:228
An abstract base class, TDocument is the base class for all document objects and serves as an interfa...
Definition docview.h:187
Menu with information used to allow merging.
Definition menu.h:206
TParaFormat encapsulates the PARAFORMAT structure, which contains information about paragraph formatt...
Definition richedit.h:119
TRichEdit encapsulates a rich edit control, a window in which a user can enter, edit and format text.
Definition richedit.h:261
bool ToggleCharAttribute(ulong mask, uint32 effects)
Toggles a set of character attributes.
Definition richedit.cpp:875
bool ReadFromStream(tistream &, uint format=SF_RTF)
< String-aware overload
bool SetCharFormat(const TCharFormat &, uint flags=SCF_SELECTION)
Sets the character formatting of a rich edit control.
Definition richedit.cpp:821
bool WriteToStream(tostream &, uint format=SF_RTF)
auto SetSelection(int startPos, int endPos) -> bool override
Selects a range of characters in the rich edit control.
Definition richedit.cpp:939
@ Yes
The attribute is absent from the selection.
Definition richedit.h:295
@ No
The whole selection has the attribute.
Definition richedit.h:294
bool SetParaFormat(const TParaFormat &)
Sets the paragraph formatting of the current selection of the rich edit control.
Definition richedit.cpp:830
uint GetFormat() const
Definition richedit.h:699
uint HasCharAttribute(ulong mask, uint32 effects)
Function returns whether or not the current selection has a particular attribute.
Definition richedit.cpp:857
void CeParagraphBullet(TCommandEnabler &)
Definition richedv.cpp:505
~TRichEditView() override
Definition richedv.cpp:87
void CeFormatItalic(TCommandEnabler &)
Definition richedv.cpp:369
TRichEditView(TDocument &doc, TWindow *parent=0)
Definition richedv.cpp:76
void CeParagraphLeft(TCommandEnabler &)
Definition richedv.cpp:469
bool VnDocClosed(int omode)
Definition richedv.cpp:106
void CmFormatUnderline()
Definition richedv.cpp:327
void CmFormatStrikeout()
Definition richedv.cpp:336
auto Create() -> bool override
< String-aware overload
Definition richedv.cpp:149
void CeFormatColor(TCommandEnabler &)
Definition richedv.cpp:538
void CeFormatFont(TCommandEnabler &)
Definition richedv.cpp:346
void SetupWindow() override
Definition richedv.cpp:222
auto CanClear() -> bool override
Definition richedv.cpp:256
bool VnCommit(bool force)
Definition richedv.cpp:173
void CeFormatBold(TCommandEnabler &)
Definition richedv.cpp:354
void CeFormatUnderline(TCommandEnabler &)
Definition richedv.cpp:385
void CmParagraphRight()
Definition richedv.cpp:442
virtual bool LoadData()
Definition richedv.cpp:120
void CeFormatStrikeout(TCommandEnabler &)
Definition richedv.cpp:401
void CeParagraphCenter(TCommandEnabler &)
Definition richedv.cpp:481
bool VnRevert(bool clear)
Definition richedv.cpp:210
void CmParagraphBullet()
Definition richedv.cpp:454
void CeParagraphRight(TCommandEnabler &)
Definition richedv.cpp:493
void CeFormatBkColor(TCommandEnabler &)
Definition richedv.cpp:566
void CmParagraphCenter()
Definition richedv.cpp:430
bool VnIsWindow(HWND hWnd)
Definition richedv.cpp:96
Abstract base class for view access from document.
Definition docview.h:397
void SetViewMenu(TMenuDescr *menu)
Definition view.cpp:95
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 int Execute()
Creates the underlying HWND and makes it modal with the help of TApplication's BeginModal support.
Definition window.cpp:2498
virtual bool Create()
Creates the window interface element to be associated with this ObjectWindows interface element.
Definition window.cpp:2399
TModule * GetModule() const
Returns a pointer to the module object.
Definition window.h:1841
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
TResult SendMessage(TMsgId, TParam1=0, TParam2=0) const
Sends a message (msg) to a specified window or windows.
Definition window.cpp:3288
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 _T(x)
Definition cygwin.h:51
#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 ClearModify()
Resets the change flag of the edit control causing IsModified to return false.
Definition edit.h:276
void Clear() override
Override TStatic virtual member functions.
Definition edit.cpp:421
@ 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_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
#define TOHIENGLISH(x)
Definition richedv.cpp:34
Definition of class TRichEditView Based on Russell Morris class.
#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