Repository navigation
Expand file tree
/
Copy pathEditor.cpp
More file actions
948 lines (764 loc) · 26.1 KB
/
Copy pathEditor.cpp
File metadata and controls
948 lines (764 loc) · 26.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
/*
* $Header: /Book/Editor.cpp 28 16.07.04 10:42 Oslph312 $
*/
#include "precomp.h"
#include "Editor.h"
#include "AutoArray.h"
#include "DisableDebugFill.h"
#include "Registry.h"
#include "Exception.h"
#include "WaitCursor.h"
#include "FontDlg.h"
#include "MRU.h"
#include "formatMessage.h"
#include "mainwnd.h"
#include "fileUtils.h"
#include "menuUtils.h"
#include "winUtils.h"
#include "activateOldInstance.h"
#include "createNewFile.h"
#include "saveFile.h"
#include "utils.h"
#include "resource.h"
#include "persistence.h"
#include "timers.h"
#include <richedit.h>
LPTSTR Editor::getContents( void ) {
assert( isGoodPtr( this ) );
LPTSTR pszOrgText = m_pDocument->getContents();
assert( isGoodStringPtr( pszOrgText ) );
m_pDocument->addCRs( &pszOrgText );
return pszOrgText;
}
void Editor::setDocument( Document *pNewDocument ) {
assert( isGoodPtr( this ) );
KillTimer( m_hwndMain, timer_save_document );
SetFocus( m_hwndMain ); // In case we're on the tool bar.
assert( isGoodPtr( this ) );
assert( isGoodPtr( m_pDocument ) );
Document *pOldDocument = m_pDocument;
try {
assert( isGoodPtr( pNewDocument ) );
m_pDocument = pNewDocument;
MRU().addFile( m_pDocument->getPath() );
const AutoString pszOrgText( getContents() );
assert( isGoodPtr( m_pEditWnd ) );
m_pEditWnd->resetPosition();
m_pEditWnd->setText( pszOrgText );
}
catch ( ... ) {
m_pDocument = pOldDocument;
throw;
}
delete pOldDocument;
m_isClean = true;
m_isWhistleClean = true;
}
void Editor::setReadOnly( bool bReadOnly ) {
assert( isGoodPtr( this ) );
if ( getEditWnd()->isReadOnly() != bReadOnly ) {
m_isWhistleClean = false;
}
if ( getEditWnd()->isReadOnly() ) {
assert( isClean() );
} else if ( !isClean() ) {
if ( !save() ) {
throwException();
}
}
if ( !m_pDocument->modifyAttribs(
bReadOnly ? FILE_ATTRIBUTE_READONLY : 0,
bReadOnly ? 0 : FILE_ATTRIBUTE_READONLY ) )
{
throwException();
}
m_pEditWnd->setReadOnly( bReadOnly );
assert( !m_pDocument->isAccessDenied() );
m_pToolbar->setReadOnly( bReadOnly );
updateToolbar();
}
bool Editor::getReadOnly( void ) const {
return m_pToolbar->getReadOnly();
}
bool Editor::save( void ) throw() {
static bool s_bBusy = false;
if ( s_bBusy ) {
debugBreak();
return false;
}
s_bBusy = true;
assert( isGoodPtr( this ) );
KillTimer( m_hwndMain, timer_save_document );
TemporaryStatusIcon statusIcon( m_pStatusbar, STD_FILESAVE );
bool bSaved = false;
try {
assert( isGoodPtr( m_pEditWnd ) );
// NOTE: nLength may be larger than the actual text!
const int nLength = m_pEditWnd->getTextLength();
AutoString pszContents( new TCHAR[ nLength + 1 ] );
m_pEditWnd->getText( pszContents, nLength + 1 );
pszContents[ nLength ] = 0;
m_pDocument->update(
GetLastActivePopup( m_hwndMain ), pszContents, nLength );
setTitle();
m_pStatusbar->setFileType( m_pDocument->isUnicode() );
bSaved = true;
}
catch ( ... ) {
messageBox( GetLastActivePopup( m_hwndMain ),
MB_ICONERROR | MB_OK, IDS_SAVE_EXCEPTION );
}
if ( bSaved ) {
trace( _T( "Saved %s successfully\n" ),
m_pDocument->getPath().c_str() );
m_pDocument->clean();
m_pEditWnd->clean( /* bEmptyUndo = */ false );
m_isClean = true;
m_isWhistleClean = false; // Time stamp will have changed
} else {
trace( _T( "Unable to save %s\n" ),
m_pDocument->getPath().c_str() );
}
s_bBusy = false;
return bSaved;
}
bool Editor::saveIfNecessary( void ) {
assert( isGoodPtr( this ) );
if ( !isClean() ) {
save();
}
return isClean();
}
void CALLBACK Editor::autoSaveTimerProc(
HWND hwnd, UINT msg, UINT id, DWORD dwTime )
{
assert( timer_save_document == id );
KillTimer( hwnd, id );
Editor *pEditor = getEditor( hwnd );
assert( isGoodPtr( pEditor ) );
assert( hwnd == pEditor->m_hwndMain );
pEditor->saveIfNecessary();
}
void Editor::restoreOriginal( void ) {
assert( isGoodPtr( this ) );
KillTimer( m_hwndMain, timer_save_document );
TemporaryStatusIcon statusIcon( m_pStatusbar, STD_UNDO );
int nBytes = 0;
AutoString pszOrgText( m_pDocument->getOrgContents( &nBytes ) );
assert( isGoodStringPtr( pszOrgText ) );
if ( (LPTSTR) 0 == pszOrgText ) {
throwException( _T( "m_pDocument->getOrgCopy failed!" ) );
}
// TODO: doc::restore, specially if I move whistleclean.
m_pDocument->save(
GetLastActivePopup( m_hwndMain ), pszOrgText, nBytes );
m_pDocument->addCRs( &pszOrgText );
assert( isGoodPtr( m_pEditWnd ) );
m_pEditWnd->resetPosition();
#if 0 // If we do this, we loose the ability to undo!
m_pEditWnd->setText( pszOrgText );
#else // ...so we do this instead:
m_pEditWnd->setSel( 0, -1 );
m_pEditWnd->replaceSel( pszOrgText );
#endif
m_pEditWnd->setSel( 0, 0 ); // LATER: Try to retain selection?
m_pEditWnd->sendMessage( EM_SCROLLCARET );
m_isClean = true;
m_isWhistleClean = true;
}
bool Editor::searchAndSelect(
const String& strSearchPattern, bool *pbWrapped )
{
assert( isGoodPtr( this ) );
WaitCursor waitCursor( _T( "wrdcount.ani" ) );
const HIMAGELIST hImageList = reinterpret_cast< HIMAGELIST >(
m_pToolbar->sendMessage( TB_GETIMAGELIST ) );
TemporaryStatusIcon statusIcon(
m_pStatusbar, hImageList, find_icon );
assert( isGoodPtr( m_pEditWnd ) );
int nOldStart = 0;
int nOldEnd = 0;
m_pEditWnd->getSel( &nOldStart, &nOldEnd );
const bool bFound = m_pEditWnd->searchAndSelect( strSearchPattern,
getMatchWholeWord(), getMatchCase(), getBackwards() ? -1 : 1 );
if ( bFound ) {
int nNewStart = 0;
int nNewEnd = 0;
m_pEditWnd->getSel( &nNewStart, &nNewEnd );
bool bWrapAround = false;
if ( getBackwards() ) {
bWrapAround =
nOldStart < nNewStart ? true :
nNewStart < nOldStart ? false :
nOldEnd == nNewEnd; // In case nNewStart == nOldStart.
} else {
bWrapAround =
nNewStart < nOldStart ? true :
nOldStart < nNewStart ? false :
nOldEnd == nNewEnd; // In case nNewStart == nOldStart.
}
if ( bWrapAround ) {
m_pStatusbar->setHighlightMessage(
getBackwards()
? IDS_PASSED_START_OF_FILE : IDS_PASSED_END_OF_FILE,
strSearchPattern.c_str() );
} else {
m_pStatusbar->setMessage(
IDS_STRING_FOUND, strSearchPattern.c_str() );
}
if ( 0 != pbWrapped ) {
assert( isGoodPtr( pbWrapped ) );
*pbWrapped = bWrapAround;
}
}
return bFound;
}
int Editor::getAutoSaveTime( void ) const {
assertValid();
return m_pDocument->isFloppy() ? 600000 : 60000;
}
void Editor::saveState( ) {
assert( isGoodPtr( this ) );
assert( IsWindow( m_hwndMain ) );
// Get window position and min/max state:
WINDOWPLACEMENT wpl = { sizeof wpl };
assert( 0 == offsetof( WINDOWPLACEMENT, length ) );
if ( !GetWindowPlacement( m_hwndMain, &wpl ) ) {
GetWindowRect( m_hwndMain, &wpl.rcNormalPosition );
wpl.showCmd = IsZoomed( m_hwndMain ) ? SW_SHOWMAXIMIZED : SW_NORMAL;
}
// TODO: Window placement is screen relative, not workspace relative.
// The window sizing functions are workspace relative...
// Save window position and state for document:
assert( isGoodPtr( m_pDocument ) );
m_pDocument->setLeft ( wpl.rcNormalPosition.left );
m_pDocument->setTop ( wpl.rcNormalPosition.top );
m_pDocument->setWidth (
wpl.rcNormalPosition.right - wpl.rcNormalPosition.left );
m_pDocument->setHeight(
wpl.rcNormalPosition.bottom - wpl.rcNormalPosition.top );
m_pDocument->setWindowState( wpl.showCmd );
// Save edit window state
assert( isGoodPtr( m_pEditWnd ) );
m_pDocument->setFirstLine( m_pEditWnd->getFirstVisibleLine() );
int nSelStart = 0;
int nSelEnd = 0;
m_pEditWnd->getSel( &nSelStart, &nSelEnd );
m_pDocument->setSelStart ( nSelStart );
m_pDocument->setSelEnd ( nSelEnd );
}
// Note: activateOldInstance will activate us,
// too, if we're reloading the same file!
void Editor::openFile( const String& strPath ) {
assert( isGoodPtr( this ) );
TemporaryStatusIcon statusIcon( m_pStatusbar, STD_FILEOPEN );
if ( !activateOldInstance( strPath.c_str() ) ) {
WaitCursor waitCursor( _T( "load.ani" ) );
m_pStatusbar->setMessage( IDS_LOADING, strPath.c_str() );
Document *pNewDcument = new Document( m_hwndMain, strPath.c_str() );
setDocument( pNewDcument );
setSettings();
updateToolbar();
m_pStatusbar->update( m_pEditWnd->getCurPos() );
}
}
// TODO -- too similar to openFile; extract "load" part
void Editor::reload()
{
assert(isGoodPtr(this));
TemporaryStatusIcon statusIcon(m_pStatusbar, STD_FILEOPEN);
WaitCursor waitCursor(_T("load.ani"));
const String strPath = m_pDocument->getPath();
m_pStatusbar->setMessage(IDS_LOADING, strPath.c_str());
Document *pNewDcument = new Document(m_hwndMain, strPath.c_str());
setDocument(pNewDcument);
setSettings();
updateToolbar();
m_pStatusbar->update(m_pEditWnd->getCurPos());
}
void Editor::copyFile(void) {
assert(isGoodPtr(this));
String strPath = m_pDocument->getPath();
const int nEnd = strPath.find_last_of(_T("\\"));
assert(0 < nEnd);
strPath.erase(nEnd);
String strNewPath = createNewFile(m_hwndMain, file_for_copy, 0, strPath, m_pDocument->getTitle());
DisableDebugFill disableDebugFill;
while (saveFile(GetLastActivePopup(m_hwndMain), &strNewPath, IDS_COPY_FILE, IDD_COPY_CHILD))
{
if (strNewPath.empty())
{
continue;
}
// The pFrom member of SHFILEOPSTRUCT is actually a list
// of null-terminated file names; the list itself must be
// doubly null-terminated. This is the reason for using the
// szFileName buffer rather than getPath().c_str.
TCHAR szOldPath[MAX_PATH + 2] = { 0 };
LPCTSTR oldString = m_pDocument->getPath().c_str();
_tcscpy_s(szOldPath, std::min(dim(szOldPath), _tcslen(oldString) + 1), oldString);
TCHAR szNewPath[MAX_PATH + 2] = { 0 };
LPCTSTR newString = strNewPath.c_str();
_tcscpy_s(szNewPath, std::min(dim(szNewPath), _tcslen(newString) + 1), newString);
SHFILEOPSTRUCT shFileOpStruct =
{
m_hwndMain, FO_COPY, szOldPath, szNewPath,
FOF_NOCONFIRMATION | FOF_SIMPLEPROGRESS | FOF_MULTIDESTFILES,
};
WaitCursor waitCursor(_T("save.ani"));
const int nErr = SHFileOperation(&shFileOpStruct);
if (0 == nErr)
{
openFile(strNewPath);
break; //*** SUCCESS LOOP EXIT
}
}
}
void Editor::refreshToolbar( void ) {
assert( isGoodPtr( this ) );
assert( isGoodPtr( m_pEditWnd ) );
m_pToolbar.reset( new Toolbar( this, IDC_TOOLBAR,
m_pEditWnd->hasRedo(), m_pEditWnd->canSetTabs() ) );
}
PRIVATE void initFont( LOGFONT *pLogFont, HFONT *phfont,
const String& strFace, long lHeight, long lWeight,
unsigned char cItalic, unsigned char cCharSet )
{
memset( pLogFont, 0, sizeof *pLogFont );
_tcscpy_s( pLogFont->lfFaceName, strFace.c_str() );
pLogFont->lfHeight = devPointsFromPrinterPoints( lHeight );
pLogFont->lfWeight = lWeight;
pLogFont->lfItalic = cItalic;
pLogFont->lfCharSet = cCharSet;
*phfont = CreateFontIndirect( pLogFont );
verify( GetObject( *phfont, sizeof( *pLogFont ), pLogFont ) );
}
/**
* Constructs the one and only Editor object, our link
* to the edit window. This constructor constructs
* the actual edit window.
*/
Editor::Editor( HWND hwndParent, AutoDocument *ppDocument )
: m_hwndMain( hwndParent )
, m_haccel( 0 )
, m_pToolbar( 0 )
, m_pStatusbar( 0 )
, m_hasFixedFont( true )
, m_hfontProportional( 0 )
, m_hfontFixed( 0 )
, m_isClean( true )
, m_isWhistleClean( true )
{
assert( isGoodPtr( this ) );
assert( isGoodPtr( ppDocument ) );
// Do this carefully, as we're transferring ownership of a pointer
// that's already protected by autoprotection (in init.cpp).
m_pDocument = *ppDocument;
*ppDocument = 0;
const AutoString pszOrgText( getContents() );
MRU().addFile( m_pDocument->getPath() );
LoadLibrary(_T("RICHED20.DLL"));
LoadLibrary(_T("Msftedit.DLL"));
m_pEditWnd = AbstractEditWnd::create(
hwndParent, pszOrgText, this, 0 != m_pDocument->getWordWrap() );
assert( isGoodPtr( m_pEditWnd ) );
refreshToolbar();
assert( isGoodPtr( m_pToolbar ) );
assert( IsWindow( *m_pToolbar ) );
if ( !getToolbarVisible() ) {
ShowWindow( *m_pToolbar, SW_HIDE );
}
m_pStatusbar = new Statusbar( hwndParent, IDC_STATUSBAR );
assert( isGoodPtr( m_pStatusbar ) );
assert( IsWindow( *m_pStatusbar ) );
if ( !getStatusbarVisible() ) {
ShowWindow( *m_pStatusbar, SW_HIDE );
}
initFont( &m_logFontFixed, &m_hfontFixed,
getFixedFace (),
getFixedHeight (),
getFixedWeight (),
getFixedItalic (),
getFixedCharSet() );
initFont( &m_logFontProportional, &m_hfontProportional,
getProportionalFace (),
getProportionalHeight (),
getProportionalWeight (),
getProportionalItalic (),
getProportionalCharSet() );
}
static bool is_alive = true;
Editor::~Editor() {
if ( 0 != m_hfontProportional ) {
verify( DeleteFont( m_hfontProportional ) );
m_hfontProportional = 0;
}
if ( 0 != m_hfontFixed ) {
verify( DeleteFont( m_hfontFixed ) );
m_hfontFixed = 0;
}
assert( 0 != m_haccel );
verify( DestroyAcceleratorTable( m_haccel ) );
is_alive = false;
FreeLibrary(GetModuleHandle(_T("Msftedit.DLL")));
FreeLibrary(GetModuleHandle(_T("RICHED20.DLL")));
}
HFONT Editor::getFont( bool bFixedFont ) const {
assert( isGoodConstPtr( this ) );
HFONT hfont = bFixedFont ? m_hfontFixed : m_hfontProportional;
if ( 0 == hfont ) {
trace( _T( "Editor::getFont: hfont is null\n" ) );
hfont = GetStockFont(
hasFixedFont() ? ANSI_FIXED_FONT : DEFAULT_GUI_FONT );
}
return hfont;
}
HFONT Editor::getFont( void ) const {
assert( isGoodConstPtr( this ) );
return getFont( hasFixedFont() );
}
void Editor::setTitle( void ) {
assert( isGoodPtr( this ) );
const String strTitle = m_pDocument->getTitle();
String strWindowTitle =
formatMessage( IDS_APP_TITLE, strTitle.c_str() );
#ifdef _DEBUG
if ( AbstractEditWnd::bForceEdit ) {
strWindowTitle += _T( " [Debug - EDIT]" );
} else {
strWindowTitle += _T( " [Debug - " ) EDIT_CLASS_NAME _T( "]" );
}
#endif
assert( IsWindow( m_hwndMain ) );
SetWindowText( m_hwndMain, strWindowTitle.c_str() );
// Change the large and small icons to match the file type:
SHFILEINFO fileInfo = { 0 };
SHGetFileInfo( m_pDocument->getPath().c_str(), 0,
&fileInfo, sizeof fileInfo, SHGFI_ICON | SHGFI_SMALLICON );
if ( 0 != fileInfo.hIcon ) {
SNDMSG( m_hwndMain, WM_SETICON, ICON_SMALL,
reinterpret_cast< LPARAM >( fileInfo.hIcon ) );
}
SHGetFileInfo( m_pDocument->getPath().c_str(), 0,
&fileInfo, sizeof fileInfo, SHGFI_ICON );
if ( 0 != fileInfo.hIcon ) {
SNDMSG( m_hwndMain, WM_SETICON, ICON_BIG,
reinterpret_cast< LPARAM >( fileInfo.hIcon ) );
}
}
void Editor::setWordWrap( bool bOn ) {
assert( isGoodPtr( m_pEditWnd ) );
m_pEditWnd->setWordWrap( bOn );
InvalidateRect( *m_pEditWnd, 0, FALSE );
m_pDocument->setWordWrap( bOn );
HMENU hmenu = GetMenu( m_hwndMain );
assert( IsMenu( hmenu ) );
checkMenuItem( hmenu, ID_VIEW_WORDWRAP, bOn );
m_pToolbar->check( ID_VIEW_WORDWRAP, bOn );
}
void Editor::setSettings( void ) {
setTitle();
const int nFixedFont = m_pDocument->getFixedFont();
setFont( 0 != nFixedFont );
setWordWrap( 0 != m_pDocument->getWordWrap() );
const bool bReadOnly = m_pDocument->isReadOnly();
assert( isGoodPtr( m_pEditWnd ) );
m_pEditWnd->setReadOnly(
bReadOnly || m_pDocument->isAccessDenied() );
m_pToolbar->setReadOnly(
bReadOnly, m_pDocument->isAccessDenied() );
if ( m_pEditWnd->canSetTabs() ) {
const int nTabs = m_pDocument->getTabs();
m_pEditWnd->setSpacesPerTab( nTabs );
m_pToolbar->setSpacesPerTab( nTabs );
}
m_pStatusbar->setFileType( m_pDocument->isUnicode() );
saveState();
}
void Editor::setFont( bool isFixedFont ) {
m_pDocument->setFixedFont( isFixedFont ? 1 : 0 );
m_hasFixedFont = isFixedFont;
SetWindowFont( *m_pEditWnd, getFont(), true );
UpdateWindow( *m_pEditWnd );
m_pEditWnd->sendMessage( EM_REQUESTRESIZE );
HMENU hmenu = GetMenu( m_hwndMain );
assert( IsMenu( hmenu ) );
checkMenuRadioItem( hmenu,
ID_VIEW_FIXEDFONT, ID_VIEW_PROPORTIONALFONT,
0 == m_pDocument->getFixedFont()
? ID_VIEW_PROPORTIONALFONT : ID_VIEW_FIXEDFONT );
m_pToolbar->check( ID_VIEW_FIXEDFONT , isFixedFont );
m_pToolbar->check( ID_VIEW_PROPORTIONALFONT, !isFixedFont );
}
void Editor::setLogFont( const LOGFONT *pLogFont, bool bFixed ) {
assert( isGoodPtr( this ) );
assert( isGoodConstPtr( pLogFont ) );
if ( bFixed ) {
if ( 0 != m_hfontFixed ) {
DeleteFont( m_hfontFixed );
m_hfontFixed = 0;
}
m_logFontFixed = *pLogFont;
m_hfontFixed = CreateFontIndirect( &m_logFontFixed );
setFixedFace ( m_logFontFixed.lfFaceName );
setFixedHeight (
printerPointsFromDevPoints( m_logFontFixed.lfHeight ) );
setFixedWeight ( m_logFontFixed.lfWeight );
setFixedItalic ( m_logFontFixed.lfItalic );
setFixedCharSet( m_logFontFixed.lfCharSet );
} else {
if ( 0 != m_hfontProportional ) {
DeleteFont( m_hfontProportional );
m_hfontProportional = 0;
}
m_logFontProportional = *pLogFont;
m_hfontProportional =
CreateFontIndirect( &m_logFontProportional );
setProportionalFace ( m_logFontProportional.lfFaceName );
setProportionalHeight ( printerPointsFromDevPoints(
m_logFontProportional.lfHeight ) );
setProportionalWeight ( m_logFontProportional.lfWeight );
setProportionalItalic ( m_logFontProportional.lfItalic );
setProportionalCharSet( m_logFontProportional.lfCharSet );
}
if ( m_hasFixedFont == bFixed ) {
setFont( bFixed );
}
}
/**
* If you use cascading submenus, start with the innermost menus.
*/
String Editor::getMenuDescription( HMENU hmnuPopup ) const {
assert( IsMenu( hmnuPopup ) );
int nStringID = 0;
if ( containsItem( hmnuPopup, ID_FILE_NEW ) ) {
nStringID = IDS_FILE_MENU;
} else if ( containsItem( hmnuPopup, ID_EDIT_UNDO ) ) {
nStringID = IDS_EDIT_MENU;
} else if ( containsItem( hmnuPopup, ID_EDIT_FIND ) ) {
nStringID = IDS_FIND_MENU;
} else if ( containsItem( hmnuPopup, ID_VIEW_TOOLBAR ) ) {
nStringID = IDS_VIEW_MENU;
} else if ( containsItem( hmnuPopup, ID_HELP_CONTENTS ) ) {
nStringID = IDS_HELP_MENU;
} else if ( containsItem( hmnuPopup, ID_FILE_SENDTO_MAILRECIPIENT ) ) {
nStringID = IDS_FILE_SENDTO_MENU;
} else if ( containsItem( hmnuPopup, SC_RESTORE ) ) {
nStringID = IDS_SYSTEM_MENU;
}
return loadString( nStringID );
}
String Editor::getMenuItemDescription(
int nItem, const String& strLast ) const
{
String strArg;
switch ( nItem ) {
case SC_CLOSE:
nItem = ID_FILE_CLOSE;
//*** FALL THROUGH
case ID_FILE_DELETE:
case ID_FILE_PAGESETUP:
case ID_FILE_PRINT:
case ID_FILE_PROPERTIES:
case ID_FILE_ABANDONCHANGES:
case ID_FILE_CLOSE:
case ID_FILE_COPY:
case ID_FILE_RENAME:
strArg = m_pDocument->getPath();
break;
#if 0
case ID_VIEW_WORDWRAP:
if ( 0 != m_pDocument->getWordWrap() ) {
nItem = ID_VIEW_WORDWRAPOFF;
} else {
nItem = ID_VIEW_WORDWRAPON;
}
break;
#endif
case ID_EDIT_UNDO:
{
String strAction = m_pEditWnd->getUndoName();
if ( !strAction.empty() ) {
strArg = strAction;
nItem = ID_EDIT_UNDO_ARG;
}
}
break;
case ID_EDIT_REDO:
{
String strAction = m_pEditWnd->getRedoName();
if ( !strAction.empty() ) {
strArg = strAction;
nItem = ID_EDIT_REDO_ARG;
}
}
break;
case ID_EDIT_COPY:
case ID_EDIT_CUT:
{
String strSel;
if ( m_pEditWnd->getSel( &strSel ) &&
(int) strSel.find( _T( '\n' ) ) < 0 )
{
assert( !strSel.empty() );
nItem = ID_EDIT_COPY == nItem
? ID_EDIT_COPY_ARG : ID_EDIT_CUT_ARG;
strArg = compactPath( strSel.c_str(), 100 );
}
}
break;
case ID_EDIT_FINDNEXT:
if ( !strLast.empty() ) {
nItem = ID_EDIT_FINDNEXT_ARG;
strArg = strLast;
}
break;
case ID_EDIT_FINDPREVIOUS:
if ( !strLast.empty() ) {
nItem = ID_EDIT_FINDPREVIOUS_ARG;
strArg = strLast;
}
break;
case ID_EDIT_FINDSELECTION:
if ( m_pEditWnd->getSel( &strArg ) ) {
nItem = ID_EDIT_FINDSELECTION_ARG;
} else if ( m_pEditWnd->getWord( &strArg ) ) {
nItem = ID_EDIT_FINDWORD;
}
break;
case ID_MRU_1: case ID_MRU_2: case ID_MRU_3:
case ID_MRU_4: case ID_MRU_5: case ID_MRU_6:
case ID_MRU_7: case ID_MRU_8: case ID_MRU_9:
strArg = MRU().getFileTitle( nItem );
nItem = ID_MRU_1;
break;
}
return 0 == nItem
? _T( "" ) : loadMenuDescription( nItem, strArg.c_str() );
}
void Editor::updateToolbar( void ) {
assert( isGoodPtr( this ) );
if ( (Toolbar *) 0 == m_pToolbar ) {
MessageBox( HWND_DESKTOP, _T( "0 == m_pToolbar" ), 0, MB_OK );
debugBreak();
}
if ( !isGoodPtr( m_pToolbar ) ) {
MessageBox( HWND_DESKTOP, _T( "!isGoodPtr( m_pToolbar )" ), 0, MB_OK );
debugBreak();
}
assert( isGoodPtr( m_pToolbar ) );
const bool bCanUndo = m_pEditWnd->canUndo();
m_pToolbar->setEnabled( ID_EDIT_UNDO, bCanUndo );
if ( m_pEditWnd->hasRedo() ) {
m_pToolbar->setEnabled( ID_EDIT_REDO, m_pEditWnd->canRedo() );
}
int nStart = 0;
int nEnd = 0;
const bool bHasSelection = m_pEditWnd->getSel( &nStart, &nEnd );
const int nTextLength = m_pEditWnd->getTextLength( true );
// The book says that the delete command should be enabled
// if and only if there is text and the file is writable.
// This is not quite right; if the caret is at the bottom
// of the file, no delete is possible.
const bool bDeleteOK = nStart < nTextLength;
const bool bHasText = 0 < m_pEditWnd->getTextLength();
const bool bReadOnly = m_pEditWnd->isReadOnly();
m_pToolbar->setEnabled( ID_EDIT_CUT , bHasSelection && !bReadOnly );
m_pToolbar->setEnabled( ID_EDIT_COPY , bHasSelection );
m_pToolbar->setEnabled( ID_EDIT_DELETE, !bReadOnly && bDeleteOK );
m_pToolbar->setEnabled( ID_EDIT_FIND , bHasText );
}
// implementation of EditListener:
void Editor::onChange( void ) {
assert( isGoodPtr( this ) );
updateToolbar();
m_isClean = false;
m_isWhistleClean = false;
SetTimer( m_hwndMain, timer_save_document,
getAutoSaveTime(), autoSaveTimerProc );
}
void Editor::onMaxText( void ) {
assert( isGoodPtr( this ) );
m_pStatusbar->setHighlightMessage( IDS_ERR_SPACE );
}
void Editor::onErrSpace( void ) {
assert( isGoodPtr( this ) );
m_pStatusbar->setHighlightMessage( IDS_ERR_SPACE );
}
void Editor::onPosChange( const Point& position ) {
assert( isGoodPtr( this ) );
// May get called before constructor is done, so be careful:
if ( (Toolbar *) 0 != m_pToolbar ) {
assert( isGoodPtr( m_pToolbar ) );
updateToolbar();
}
if ( (Statusbar *) 0 != m_pStatusbar ) {
assert( isGoodPtr( m_pStatusbar ) );
m_pStatusbar->update( position );
}
}
void Editor::onZoomChange( const int zoomPercentage ) {
if ( (Statusbar *) 0 != m_pStatusbar ) {
assert( isGoodPtr( m_pStatusbar ) );
m_pStatusbar->update( zoomPercentage );
}
}
void Editor::loadAcceleratorTable( HINSTANCE hinst ) {
assert( is_alive );
if ( 0 != m_haccel ) {
verify( DestroyAcceleratorTable( m_haccel ) );
}
m_haccel =
LoadAccelerators( hinst, MAKEINTRESOURCE( IDR_ACCELERATOR ) );
assert( 0 != m_haccel );
}
// assignment within conditional expression
#pragma warning( disable: 4706 )
int Editor::run( void ) {
assert( isGoodPtr( this ) );
loadAcceleratorTable( getModuleHandle() );
MSG msg = { 0, 0, static_cast< WPARAM >( -1 ) };
// TEST: Force m_hwndMain to null and see what happens:
// The thing is surprisingly well-behaved.
// Accelerators don't work, of course, and it triggers a lot
// of assertions, but it seems to work quite reliably.
// (Menu updates are also a problem in some cases.)
BOOL bRun = true;
while ( bRun ) {
try {
assertValid();
while ( bRun = GetMessage( &msg, 0, 0, 0 ) ) {
assertValid();
if ( !TranslateAccelerator( m_hwndMain, m_haccel, &msg )
&& !isToolbarDialogMessage( &msg ) )
{
TranslateMessage( &msg );
DispatchMessage ( &msg );
}
}
}
catch ( const NullPointerException& ) {
trace( _T( "NullPointerException in Editor::run; this is OK.\n" ) );
}
catch ( const CommonDialogException& x ) {
// *All* common dialog failures go through here.
trace( _T( "CommonDialogException in Editor::run; %s\n" ), x.what() );
m_pStatusbar->setHighlightMessage( IDS_STRING, x.what() );
}
catch ( const Exception& x ) {
trace( _T( "Exception in Editor::run: \"%s\"\n" ), x.what() );
if ( (Statusbar *) 0 != m_pStatusbar &&
IsWindowVisible( *m_pStatusbar ) )
{
m_pStatusbar->setHighlightMessage( IDS_STRING, x.what() );
} else {
messageBox( GetLastActivePopup( m_hwndMain ),
MB_OK, IDS_STRING, x.what() );
}
}
}
return msg.wParam;
}
// assignment within conditional expression
#pragma warning( default: 4706 )
// end of file