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),
44 EV_COMMAND_ENABLE(CM_FORMATFONT, CeFormatFont),
45 EV_COMMAND(CM_FORMATCOLOR, CmFormatColor),
46 EV_COMMAND_ENABLE(CM_FORMATCOLOR, CeFormatColor),
47 EV_COMMAND(CM_FORMATBKCOLOR, CmFormatBkColor),
48 EV_COMMAND_ENABLE(CM_FORMATBKCOLOR, CeFormatBkColor),
49 EV_COMMAND(CM_PARAGRAPHLEFT, CmParagraphLeft),
50 EV_COMMAND_ENABLE(CM_PARAGRAPHLEFT, CeParagraphLeft),
51 EV_COMMAND(CM_PARAGRAPHCENTER, CmParagraphCenter),
52 EV_COMMAND_ENABLE(CM_PARAGRAPHCENTER, CeParagraphCenter),
53 EV_COMMAND(CM_PARAGRAPHRIGHT, CmParagraphRight),
54 EV_COMMAND_ENABLE(CM_PARAGRAPHRIGHT, CeParagraphRight),
55 EV_COMMAND(CM_PARAGRAPHBULLET, CmParagraphBullet),
56 EV_COMMAND_ENABLE(CM_PARAGRAPHBULLET, CeParagraphBullet),
57 EV_COMMAND(CM_FORMATBOLD, CmFormatBold),
58 EV_COMMAND_ENABLE(CM_FORMATBOLD, CeFormatBold),
59 EV_COMMAND(CM_FORMATITALIC, CmFormatItalic),
60 EV_COMMAND_ENABLE(CM_FORMATITALIC, CeFormatItalic),
61 EV_COMMAND(CM_FORMATUNDERLINE, CmFormatUnderline),
62 EV_COMMAND_ENABLE(CM_FORMATUNDERLINE, CeFormatUnderline),
63 EV_COMMAND(CM_FORMATSTRIKEOUT, CmFormatStrikeout),
64 EV_COMMAND_ENABLE(CM_FORMATSTRIKEOUT, CeFormatStrikeout),
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))
83 SetViewMenu( new TMenuDescr(IDM_RICHEDITVIEW,0,2,0,2,0,0, GetModule()));
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
97{
98 return hWnd == TWindow::GetHandle() || IsChild(hWnd);
99}
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
240 SetCharFormat(defaultFmt);
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);
288 fontData.Flags = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT | CF_EFFECTS;
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);
301 SetCharFormat(chFormat);
302 }
303}
304
305//
306//
307//
308void
310{
311 ToggleCharAttribute(CFM_BOLD, CFE_BOLD);
312}
313
314//
315//
316//
317void
319{
320 ToggleCharAttribute(CFM_ITALIC, CFE_ITALIC);
321}
322
323//
324//
325//
326void
328{
329 ToggleCharAttribute(CFM_UNDERLINE, CFE_UNDERLINE);
330}
331
332//
333//
334//
335void
337{
338 ToggleCharAttribute(CFM_STRIKEOUT, CFE_STRIKEOUT);
339}
340
341
342//
343//
344//
345void
349
350//
351//
352//
353void
355{
356 uint result = HasCharAttribute(CFM_BOLD, CFE_BOLD);
357 if( result == Yes )
359 else if( result == No )
361 else
363}
364
365//
366//
367//
368void
370{
371 uint fmt = HasCharAttribute(CFM_ITALIC, CFE_ITALIC);
372 if( fmt == Yes )
374 else
375 if( fmt == No )
377 else
379}
380
381//
382//
383//
384void
386{
387 uint result = HasCharAttribute(CFM_UNDERLINE, CFE_UNDERLINE);
388 if( result == Yes )
390 else
391 if( result == No )
393 else
395}
396
397//
398//
399//
400void
402{
403 uint result = HasCharAttribute(CFM_STRIKEOUT, CFE_STRIKEOUT);
404 if( result == Yes )
406 else
407 if( result == No )
409 else
411}
412
413
414//
415//
416//
417void
419{
420 TParaFormat pFormat;
421
422 pFormat.SetAlignment(PFA_LEFT);
423 SetParaFormat(pFormat);
424}
425
426//
427//
428//
429void
431{
432 TParaFormat pFormat;
433
434 pFormat.SetAlignment(PFA_CENTER);
435 SetParaFormat(pFormat);
436}
437
438//
439//
440//
441void
443{
444 TParaFormat pFormat;
445
446 pFormat.SetAlignment(PFA_RIGHT);
447 SetParaFormat(pFormat);
448}
449
450//
451//
452//
453void
455{
456 TParaFormat pFormat;
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
462 SetParaFormat(pFormat);
463}
464
465//
466//
467//
468void
470{
471 TParaFormat pFormat(*this);
472
473 tce.SetCheck( (pFormat.wAlignment == PFA_LEFT) ?
475}
476
477//
478//
479//
480void
482{
483 TParaFormat pFormat(*this);
484
485 tce.SetCheck( (pFormat.wAlignment == PFA_CENTER) ?
487}
488
489//
490//
491//
492void
494{
495 TParaFormat pFormat(*this);
496
497 tce.SetCheck((pFormat.wAlignment == PFA_RIGHT) ?
499}
500
501//
502//
503//
504void
506{
507 TParaFormat pFormat(*this);
508
509 tce.SetCheck( (pFormat.wNumbering == PFN_BULLET) ?
511}
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//==============================================================================
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
void GetFontInfo(const LOGFONT &lf)
Initializes the underlying CHARFORMAT structure using the information stored in a LOGFONT structure.
Definition richedit.cpp:259
TColor GetBkColor() const
Retrieves the character background color stored in the CHARFORMAT structure.
Definition richedit.cpp:305
void EnableBold(bool=true)
Toggles the bold character attribute according to the boolean parameter specified.
Definition richedit.cpp:71
void SetTextColor(const TColor &=TColor::None)
Updates the CHARFORMAT structure with the specified color.
Definition richedit.cpp:151
void SetFontInfo(LOGFONT &lf) const
Transfers the information currently in the underlying CHARFORMAT structure to a LOGFONT structure.
Definition richedit.cpp:228
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
void SetFaceName(LPCTSTR)
Sets the face name of the font.
Definition richedit.cpp:168
Defines information necessary to initialize the dialog box with the user's color selection.
Definition chooseco.h:43
uint32 Flags
Flags can be a combination of the following values that control the appearance and functionality of t...
Definition chooseco.h:60
TColor * CustColors
Points to an array of 16 colors.
Definition chooseco.h:76
TColor Color
Specifies the color that is initially selected when the dialog box is created.
Definition chooseco.h:73
TChooseColorDialog(TWindow *parent, TData &data, TResId templateId=0, LPCTSTR title=0, TModule *module=0)
Initialize the choose color common dialog data members.
Definition chooseco.cpp:28
Defines information necessary to initialize the dialog box with the user's font selection.
Definition choosefo.h:47
HDC DC
Handle to the device context from which fonts are obtained.
Definition choosefo.h:91
uint32 Flags
Flags can be a combination of the following constants that control the appearance and functionality o...
Definition choosefo.h:76
uint16 FontType
Font type or name.
Definition choosefo.h:107
LOGFONT LogFont
Attributes of the font.
Definition choosefo.h:94
TColor Color
Indicates the font color that is initially selected when the dialog box is created; contains the user...
Definition choosefo.h:101
TChooseFontDialog(TWindow *parent, TData &data, TResId templateId=0, LPCTSTR title=nullptr, TModule *module=nullptr)
Initialize the ChooseFont data members.
Definition choosefo.cpp:30
TClientDC(HWND wnd)
Creates a TClientDC object with the given owned window.
Definition windowdc.cpp:71
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
virtual void Enable(bool enable=true)
Enables or disables the command sender.
Definition window.cpp:301
virtual void SetCheck(int check)=0
Changes the check state of the command sender to either unchecked, checked, or indeterminate.
@ 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
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
TMenuDescr()
Default constructor for a TMenuDescr object.
Definition menudesc.cpp:165
TParaFormat encapsulates the PARAFORMAT structure, which contains information about paragraph formatt...
Definition richedit.h:119
void SetOffset(long)
Sets the indentation of the second line and subsequent lines, relative to the starting indentation.
Definition richedit.cpp:550
void SetNumbering(uint16)
Sets the numbering options. The only valid parameter is '0' or PFN_BULLET.
Definition richedit.cpp:508
void SetAlignment(uint16)
Sets the alignment option.
Definition richedit.cpp:563
Smart pointer to a single object. Provides member access operator ->.
Definition pointer.h:73
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
TRichEdit(TWindow *parent, int id, LPCTSTR text, int x, int y, int w, int h, LPCTSTR fileName=nullptr, TModule *module=nullptr)
Constructor for a TRichEdit object.
Definition richedit.cpp:707
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
void CeFormatColor(TCommandEnabler &)
Definition richedv.cpp:538
void CeFormatFont(TCommandEnabler &)
Definition richedv.cpp:346
void SetupWindow() override
Definition richedv.cpp:222
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
friend class TDocument
needs access to NextView
Definition docview.h:477
TView(TDocument &doc)
Constructs a TView object of the document associated with the view.
Definition view.cpp:34
void SetViewMenu(TMenuDescr *menu)
Definition view.cpp:95
static uint GetNextViewId()
Next global ID to assign.
Definition docview.h:1037
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
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
unsigned long uint32
Definition number.h:34
EV_VN_COMMIT
Definition editview.cpp:29
OWL_DIAGINFO
Definition animctrl.cpp:14
IMPLEMENT_STREAMABLE2(TEditView, TEditSearch, TView)
END_RESPONSE_TABLE
Definition button.cpp:26
EV_COMMAND_ENABLE(CM_FILENEW, CeFileNew)
unsigned int uint
Definition number.h:25
EV_COMMAND(IDOK, CmOkCancel)
EV_VN_ISDIRTY
Definition editview.cpp:28
DIAG_DECLARE_GROUP(OwlThread)
#define TOHIENGLISH(x)
Definition richedv.cpp:34
Definition of class TRichEditView Based on Russell Morris class.
#define EV_COMMAND(id, method)
Response table entry for a menu/accelerator/push button message.
Definition windowev.h:171