4 * Copyright 1998,99 Marcel Baur <mbaur@g26.ethz.ch>
5 * Copyright 2002 Sylvain Petreolle <spetreolle@yahoo.fr>
6 * Copyright 2002 Andriy Palamarchuk
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 LRESULT CALLBACK
EDIT_WndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
);
31 static const TCHAR helpfile
[] = _T("notepad.hlp");
32 static const TCHAR empty_str
[] = _T("");
33 static const TCHAR szDefaultExt
[] = _T("txt");
34 static const TCHAR txt_files
[] = _T("*.txt");
36 static UINT_PTR CALLBACK
DIALOG_PAGESETUP_Hook(HWND hDlg
, UINT msg
, WPARAM wParam
, LPARAM lParam
);
38 VOID
ShowLastError(VOID
)
40 DWORD error
= GetLastError();
41 if (error
!= NO_ERROR
)
43 LPTSTR lpMsgBuf
= NULL
;
44 TCHAR szTitle
[MAX_STRING_LEN
];
46 LoadString(Globals
.hInstance
, STRING_ERROR
, szTitle
, ARRAY_SIZE(szTitle
));
48 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
56 MessageBox(NULL
, lpMsgBuf
, szTitle
, MB_OK
| MB_ICONERROR
);
62 * Sets the caption of the main window according to Globals.szFileTitle:
63 * (untitled) - Notepad if no file is open
64 * [filename] - Notepad if a file is given
66 static void UpdateWindowCaption(void)
68 TCHAR szCaption
[MAX_STRING_LEN
];
69 TCHAR szNotepad
[MAX_STRING_LEN
];
71 LoadString(Globals
.hInstance
, STRING_NOTEPAD
, szNotepad
, ARRAY_SIZE(szNotepad
));
73 if (Globals
.szFileTitle
[0] != 0)
75 StringCchCopy(szCaption
, ARRAY_SIZE(szCaption
), Globals
.szFileTitle
);
79 LoadString(Globals
.hInstance
, STRING_UNTITLED
, szCaption
, ARRAY_SIZE(szCaption
));
82 StringCchCat(szCaption
, ARRAY_SIZE(szCaption
), _T(" - "));
83 StringCchCat(szCaption
, ARRAY_SIZE(szCaption
), szNotepad
);
84 SetWindowText(Globals
.hMainWnd
, szCaption
);
87 int DIALOG_StringMsgBox(HWND hParent
, int formatId
, LPCTSTR szString
, DWORD dwFlags
)
89 TCHAR szMessage
[MAX_STRING_LEN
];
90 TCHAR szResource
[MAX_STRING_LEN
];
92 /* Load and format szMessage */
93 LoadString(Globals
.hInstance
, formatId
, szResource
, ARRAY_SIZE(szResource
));
94 _sntprintf(szMessage
, ARRAY_SIZE(szMessage
), szResource
, szString
);
97 if ((dwFlags
& MB_ICONMASK
) == MB_ICONEXCLAMATION
)
98 LoadString(Globals
.hInstance
, STRING_ERROR
, szResource
, ARRAY_SIZE(szResource
));
100 LoadString(Globals
.hInstance
, STRING_NOTEPAD
, szResource
, ARRAY_SIZE(szResource
));
102 /* Display Modal Dialog */
103 // if (hParent == NULL)
104 // hParent = Globals.hMainWnd;
105 return MessageBox(hParent
, szMessage
, szResource
, dwFlags
);
108 static void AlertFileNotFound(LPCTSTR szFileName
)
110 DIALOG_StringMsgBox(Globals
.hMainWnd
, STRING_NOTFOUND
, szFileName
, MB_ICONEXCLAMATION
| MB_OK
);
113 static int AlertFileNotSaved(LPCTSTR szFileName
)
115 TCHAR szUntitled
[MAX_STRING_LEN
];
117 LoadString(Globals
.hInstance
, STRING_UNTITLED
, szUntitled
, ARRAY_SIZE(szUntitled
));
119 return DIALOG_StringMsgBox(Globals
.hMainWnd
, STRING_NOTSAVED
,
120 szFileName
[0] ? szFileName
: szUntitled
,
121 MB_ICONQUESTION
| MB_YESNOCANCEL
);
124 static void AlertPrintError(void)
126 TCHAR szUntitled
[MAX_STRING_LEN
];
128 LoadString(Globals
.hInstance
, STRING_UNTITLED
, szUntitled
, ARRAY_SIZE(szUntitled
));
130 DIALOG_StringMsgBox(Globals
.hMainWnd
, STRING_PRINTERROR
,
131 Globals
.szFileName
[0] ? Globals
.szFileName
: szUntitled
,
132 MB_ICONEXCLAMATION
| MB_OK
);
137 * TRUE - if file exists
138 * FALSE - if file does not exist
140 BOOL
FileExists(LPCTSTR szFilename
)
142 WIN32_FIND_DATA entry
;
145 hFile
= FindFirstFile(szFilename
, &entry
);
148 return (hFile
!= INVALID_HANDLE_VALUE
);
151 BOOL
HasFileExtension(LPCTSTR szFilename
)
155 s
= _tcsrchr(szFilename
, _T('\\'));
158 return _tcsrchr(szFilename
, _T('.')) != NULL
;
161 int GetSelectionTextLength(HWND hWnd
)
166 SendMessage(hWnd
, EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
);
168 return dwEnd
- dwStart
;
171 int GetSelectionText(HWND hWnd
, LPTSTR lpString
, int nMaxCount
)
184 SendMessage(hWnd
, EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwEnd
);
186 if (dwStart
== dwEnd
)
191 dwSize
= GetWindowTextLength(hWnd
) + 1;
192 lpTemp
= HeapAlloc(GetProcessHeap(), 0, dwSize
* sizeof(TCHAR
));
198 dwSize
= GetWindowText(hWnd
, lpTemp
, dwSize
);
202 HeapFree(GetProcessHeap(), 0, lpTemp
);
206 hResult
= StringCchCopyN(lpString
, nMaxCount
, lpTemp
+ dwStart
, dwEnd
- dwStart
);
207 HeapFree(GetProcessHeap(), 0, lpTemp
);
213 return dwEnd
- dwStart
;
216 case STRSAFE_E_INSUFFICIENT_BUFFER
:
218 return nMaxCount
- 1;
229 GetPrintingRect(HDC hdc
, RECT margins
)
231 int iLogPixelsX
, iLogPixelsY
;
232 int iHorzRes
, iVertRes
;
233 int iPhysPageX
, iPhysPageY
, iPhysPageW
, iPhysPageH
;
236 iPhysPageX
= GetDeviceCaps(hdc
, PHYSICALOFFSETX
);
237 iPhysPageY
= GetDeviceCaps(hdc
, PHYSICALOFFSETY
);
238 iPhysPageW
= GetDeviceCaps(hdc
, PHYSICALWIDTH
);
239 iPhysPageH
= GetDeviceCaps(hdc
, PHYSICALHEIGHT
);
240 iLogPixelsX
= GetDeviceCaps(hdc
, LOGPIXELSX
);
241 iLogPixelsY
= GetDeviceCaps(hdc
, LOGPIXELSY
);
242 iHorzRes
= GetDeviceCaps(hdc
, HORZRES
);
243 iVertRes
= GetDeviceCaps(hdc
, VERTRES
);
245 rcPrintRect
.left
= (margins
.left
* iLogPixelsX
/ 2540) - iPhysPageX
;
246 rcPrintRect
.top
= (margins
.top
* iLogPixelsY
/ 2540) - iPhysPageY
;
247 rcPrintRect
.right
= iHorzRes
- (((margins
.left
* iLogPixelsX
/ 2540) - iPhysPageX
) + ((margins
.right
* iLogPixelsX
/ 2540) - (iPhysPageW
- iPhysPageX
- iHorzRes
)));
248 rcPrintRect
.bottom
= iVertRes
- (((margins
.top
* iLogPixelsY
/ 2540) - iPhysPageY
) + ((margins
.bottom
* iLogPixelsY
/ 2540) - (iPhysPageH
- iPhysPageY
- iVertRes
)));
253 static BOOL
DoSaveFile(VOID
)
260 hFile
= CreateFile(Globals
.szFileName
, GENERIC_WRITE
, FILE_SHARE_WRITE
,
261 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
262 if(hFile
== INVALID_HANDLE_VALUE
)
268 size
= GetWindowTextLength(Globals
.hEdit
) + 1;
269 pTemp
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(*pTemp
));
276 size
= GetWindowText(Globals
.hEdit
, pTemp
, size
);
280 if (!WriteText(hFile
, (LPWSTR
)pTemp
, size
, Globals
.encFile
, Globals
.iEoln
))
287 SendMessage(Globals
.hEdit
, EM_SETMODIFY
, FALSE
, 0);
293 HeapFree(GetProcessHeap(), 0, pTemp
);
299 * TRUE - User agreed to close (both save/don't save)
300 * FALSE - User cancelled close by selecting "Cancel"
302 BOOL
DoCloseFile(VOID
)
306 if (SendMessage(Globals
.hEdit
, EM_GETMODIFY
, 0, 0))
308 /* prompt user to save changes */
309 nResult
= AlertFileNotSaved(Globals
.szFileName
);
313 if(!DIALOG_FileSave())
328 SetFileName(empty_str
);
329 UpdateWindowCaption();
334 VOID
DoOpenFile(LPCTSTR szFileName
)
336 static const TCHAR dotlog
[] = _T(".LOG");
338 LPTSTR pszText
= NULL
;
342 /* Close any files and prompt to save changes */
346 hFile
= CreateFile(szFileName
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
347 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
348 if (hFile
== INVALID_HANDLE_VALUE
)
354 if (!ReadText(hFile
, (LPWSTR
*)&pszText
, &dwTextLen
, &Globals
.encFile
, &Globals
.iEoln
))
359 SetWindowText(Globals
.hEdit
, pszText
);
361 SendMessage(Globals
.hEdit
, EM_SETMODIFY
, FALSE
, 0);
362 SendMessage(Globals
.hEdit
, EM_EMPTYUNDOBUFFER
, 0, 0);
363 SetFocus(Globals
.hEdit
);
365 /* If the file starts with .LOG, add a time/date at the end and set cursor after
366 * See http://support.microsoft.com/?kbid=260563
368 if (GetWindowText(Globals
.hEdit
, log
, ARRAY_SIZE(log
)) && !_tcscmp(log
, dotlog
))
370 static const TCHAR lf
[] = _T("\r\n");
371 SendMessage(Globals
.hEdit
, EM_SETSEL
, GetWindowTextLength(Globals
.hEdit
), -1);
372 SendMessage(Globals
.hEdit
, EM_REPLACESEL
, TRUE
, (LPARAM
)lf
);
373 DIALOG_EditTimeDate();
374 SendMessage(Globals
.hEdit
, EM_REPLACESEL
, TRUE
, (LPARAM
)lf
);
377 SetFileName(szFileName
);
378 UpdateWindowCaption();
379 NOTEPAD_EnableSearchMenu();
381 if (hFile
!= INVALID_HANDLE_VALUE
)
384 HeapFree(GetProcessHeap(), 0, pszText
);
387 VOID
DIALOG_FileNew(VOID
)
389 /* Close any files and prompt to save changes */
391 SetWindowText(Globals
.hEdit
, empty_str
);
392 SendMessage(Globals
.hEdit
, EM_EMPTYUNDOBUFFER
, 0, 0);
393 SetFocus(Globals
.hEdit
);
394 NOTEPAD_EnableSearchMenu();
398 VOID
DIALOG_FileOpen(VOID
)
400 OPENFILENAME openfilename
;
401 TCHAR szPath
[MAX_PATH
];
403 ZeroMemory(&openfilename
, sizeof(openfilename
));
405 if (Globals
.szFileName
[0] == 0)
406 _tcscpy(szPath
, txt_files
);
408 _tcscpy(szPath
, Globals
.szFileName
);
410 openfilename
.lStructSize
= sizeof(openfilename
);
411 openfilename
.hwndOwner
= Globals
.hMainWnd
;
412 openfilename
.hInstance
= Globals
.hInstance
;
413 openfilename
.lpstrFilter
= Globals
.szFilter
;
414 openfilename
.lpstrFile
= szPath
;
415 openfilename
.nMaxFile
= ARRAY_SIZE(szPath
);
416 openfilename
.Flags
= OFN_FILEMUSTEXIST
| OFN_PATHMUSTEXIST
| OFN_HIDEREADONLY
;
417 openfilename
.lpstrDefExt
= szDefaultExt
;
419 if (GetOpenFileName(&openfilename
)) {
420 if (FileExists(openfilename
.lpstrFile
))
421 DoOpenFile(openfilename
.lpstrFile
);
423 AlertFileNotFound(openfilename
.lpstrFile
);
427 BOOL
DIALOG_FileSave(VOID
)
429 if (Globals
.szFileName
[0] == 0)
430 return DIALOG_FileSaveAs();
437 DIALOG_FileSaveAs_Hook(HWND hDlg
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
442 UNREFERENCED_PARAMETER(wParam
);
447 hCombo
= GetDlgItem(hDlg
, ID_ENCODING
);
449 LoadString(Globals
.hInstance
, STRING_ANSI
, szText
, ARRAY_SIZE(szText
));
450 SendMessage(hCombo
, CB_ADDSTRING
, 0, (LPARAM
) szText
);
452 LoadString(Globals
.hInstance
, STRING_UNICODE
, szText
, ARRAY_SIZE(szText
));
453 SendMessage(hCombo
, CB_ADDSTRING
, 0, (LPARAM
) szText
);
455 LoadString(Globals
.hInstance
, STRING_UNICODE_BE
, szText
, ARRAY_SIZE(szText
));
456 SendMessage(hCombo
, CB_ADDSTRING
, 0, (LPARAM
) szText
);
458 LoadString(Globals
.hInstance
, STRING_UTF8
, szText
, ARRAY_SIZE(szText
));
459 SendMessage(hCombo
, CB_ADDSTRING
, 0, (LPARAM
) szText
);
461 SendMessage(hCombo
, CB_SETCURSEL
, Globals
.encFile
, 0);
463 hCombo
= GetDlgItem(hDlg
, ID_EOLN
);
465 LoadString(Globals
.hInstance
, STRING_CRLF
, szText
, ARRAY_SIZE(szText
));
466 SendMessage(hCombo
, CB_ADDSTRING
, 0, (LPARAM
) szText
);
468 LoadString(Globals
.hInstance
, STRING_LF
, szText
, ARRAY_SIZE(szText
));
469 SendMessage(hCombo
, CB_ADDSTRING
, 0, (LPARAM
) szText
);
471 LoadString(Globals
.hInstance
, STRING_CR
, szText
, ARRAY_SIZE(szText
));
472 SendMessage(hCombo
, CB_ADDSTRING
, 0, (LPARAM
) szText
);
474 SendMessage(hCombo
, CB_SETCURSEL
, Globals
.iEoln
, 0);
478 if (((NMHDR
*) lParam
)->code
== CDN_FILEOK
)
480 hCombo
= GetDlgItem(hDlg
, ID_ENCODING
);
482 Globals
.encFile
= (int) SendMessage(hCombo
, CB_GETCURSEL
, 0, 0);
484 hCombo
= GetDlgItem(hDlg
, ID_EOLN
);
486 Globals
.iEoln
= (int) SendMessage(hCombo
, CB_GETCURSEL
, 0, 0);
493 BOOL
DIALOG_FileSaveAs(VOID
)
496 TCHAR szPath
[MAX_PATH
];
498 ZeroMemory(&saveas
, sizeof(saveas
));
500 if (Globals
.szFileName
[0] == 0)
501 _tcscpy(szPath
, txt_files
);
503 _tcscpy(szPath
, Globals
.szFileName
);
505 saveas
.lStructSize
= sizeof(OPENFILENAME
);
506 saveas
.hwndOwner
= Globals
.hMainWnd
;
507 saveas
.hInstance
= Globals
.hInstance
;
508 saveas
.lpstrFilter
= Globals
.szFilter
;
509 saveas
.lpstrFile
= szPath
;
510 saveas
.nMaxFile
= ARRAY_SIZE(szPath
);
511 saveas
.Flags
= OFN_PATHMUSTEXIST
| OFN_OVERWRITEPROMPT
| OFN_HIDEREADONLY
|
512 OFN_EXPLORER
| OFN_ENABLETEMPLATE
| OFN_ENABLEHOOK
;
513 saveas
.lpstrDefExt
= szDefaultExt
;
514 saveas
.lpTemplateName
= MAKEINTRESOURCE(DIALOG_ENCODING
);
515 saveas
.lpfnHook
= DIALOG_FileSaveAs_Hook
;
517 if (GetSaveFileName(&saveas
))
519 /* HACK: Because in ROS, Save-As boxes don't check the validity
520 * of file names and thus, here, szPath can be invalid !! We only
521 * see its validity when we call DoSaveFile()... */
525 UpdateWindowCaption();
540 VOID
DIALOG_FilePrint(VOID
)
547 int xLeft
, yTop
, pagecount
, dopage
, copycount
;
550 HFONT font
, old_font
=0;
553 static const TCHAR times_new_roman
[] = _T("Times New Roman");
556 /* Get a small font and print some header info on each page */
557 ZeroMemory(&hdrFont
, sizeof(hdrFont
));
558 hdrFont
.lfHeight
= 100;
559 hdrFont
.lfWeight
= FW_BOLD
;
560 hdrFont
.lfCharSet
= ANSI_CHARSET
;
561 hdrFont
.lfOutPrecision
= OUT_DEFAULT_PRECIS
;
562 hdrFont
.lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
563 hdrFont
.lfQuality
= PROOF_QUALITY
;
564 hdrFont
.lfPitchAndFamily
= VARIABLE_PITCH
| FF_ROMAN
;
565 _tcscpy(hdrFont
.lfFaceName
, times_new_roman
);
567 font
= CreateFontIndirect(&hdrFont
);
569 /* Get Current Settings */
570 ZeroMemory(&printer
, sizeof(printer
));
571 printer
.lStructSize
= sizeof(printer
);
572 printer
.hwndOwner
= Globals
.hMainWnd
;
573 printer
.hInstance
= Globals
.hInstance
;
575 /* Set some default flags */
576 printer
.Flags
= PD_RETURNDC
| PD_SELECTION
;
578 /* Disable the selection radio button if there is no text selected */
579 if (!GetSelectionTextLength(Globals
.hEdit
))
581 printer
.Flags
= printer
.Flags
| PD_NOSELECTION
;
584 printer
.nFromPage
= 0;
585 printer
.nMinPage
= 1;
586 /* we really need to calculate number of pages to set nMaxPage and nToPage */
587 printer
.nToPage
= (WORD
)-1;
588 printer
.nMaxPage
= (WORD
)-1;
590 /* Let commdlg manage copy settings */
591 printer
.nCopies
= (WORD
)PD_USEDEVMODECOPIES
;
593 printer
.hDevMode
= Globals
.hDevMode
;
594 printer
.hDevNames
= Globals
.hDevNames
;
596 if (!PrintDlg(&printer
))
602 Globals
.hDevMode
= printer
.hDevMode
;
603 Globals
.hDevNames
= printer
.hDevNames
;
605 assert(printer
.hDC
!= 0);
607 /* initialize DOCINFO */
608 di
.cbSize
= sizeof(DOCINFO
);
609 di
.lpszDocName
= Globals
.szFileTitle
;
610 di
.lpszOutput
= NULL
;
611 di
.lpszDatatype
= NULL
;
614 if (StartDoc(printer
.hDC
, &di
) <= 0)
621 /* Get the file text */
622 if (printer
.Flags
& PD_SELECTION
)
624 size
= GetSelectionTextLength(Globals
.hEdit
) + 1;
628 size
= GetWindowTextLength(Globals
.hEdit
) + 1;
631 pTemp
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(TCHAR
));
640 if (printer
.Flags
& PD_SELECTION
)
642 size
= GetSelectionText(Globals
.hEdit
, pTemp
, size
);
646 size
= GetWindowText(Globals
.hEdit
, pTemp
, size
);
649 /* Get the current printing area */
650 rcPrintRect
= GetPrintingRect(printer
.hDC
, Globals
.lMargins
);
652 /* Ensure that each logical unit maps to one pixel */
653 SetMapMode(printer
.hDC
, MM_TEXT
);
655 /* Needed to get the correct height of a text line */
656 GetTextMetrics(printer
.hDC
, &tm
);
659 for (copycount
=1; copycount
<= printer
.nCopies
; copycount
++) {
663 /* Don't start a page if none of the conditions below are true */
666 /* The user wants to print the current selection */
667 if (printer
.Flags
& PD_SELECTION
)
672 /* The user wants to print the entire document */
673 if (!(printer
.Flags
& PD_PAGENUMS
) && !(printer
.Flags
& PD_SELECTION
))
678 /* The user wants to print a specified range of pages */
679 if ((pagecount
>= printer
.nFromPage
&& pagecount
<= printer
.nToPage
))
684 old_font
= SelectObject(printer
.hDC
, font
);
687 if (StartPage(printer
.hDC
) <= 0) {
688 SelectObject(printer
.hDC
, old_font
);
690 DeleteDC(printer
.hDC
);
691 HeapFree(GetProcessHeap(), 0, pTemp
);
697 SetViewportOrgEx(printer
.hDC
, rcPrintRect
.left
, rcPrintRect
.top
, NULL
);
699 /* Write a rectangle and header at the top of each page */
700 Rectangle(printer
.hDC
, border
, border
, rcPrintRect
.right
- border
, border
+ tm
.tmHeight
* 2);
701 /* I don't know what's up with this TextOut command. This comes out
706 border
+ tm
.tmHeight
/ 2,
708 lstrlen(Globals
.szFileTitle
));
711 /* The starting point for the main text */
713 yTop
= border
+ tm
.tmHeight
* 4;
715 SelectObject(printer
.hDC
, old_font
);
717 /* Since outputting strings is giving me problems, output the main
718 * text one character at a time. */
720 if (pTemp
[i
] == '\n') {
724 else if (pTemp
[i
] != '\r') {
726 TextOut(printer
.hDC
, xLeft
, yTop
, &pTemp
[i
], 1);
728 /* We need to get the width for each individual char, since a proportional font may be used */
729 GetTextExtentPoint32(printer
.hDC
, &pTemp
[i
], 1, &szMetric
);
730 xLeft
+= szMetric
.cx
;
732 /* Insert a line break if the current line does not fit into the printing area */
733 if (xLeft
> rcPrintRect
.right
)
736 yTop
= yTop
+ tm
.tmHeight
;
739 } while (i
++ < size
&& yTop
< rcPrintRect
.bottom
);
742 EndPage(printer
.hDC
);
748 SelectObject(printer
.hDC
, old_font
);
750 DeleteDC(printer
.hDC
);
751 HeapFree(GetProcessHeap(), 0, pTemp
);
755 VOID
DIALOG_FileExit(VOID
)
757 PostMessage(Globals
.hMainWnd
, WM_CLOSE
, 0, 0l);
760 VOID
DIALOG_EditUndo(VOID
)
762 SendMessage(Globals
.hEdit
, EM_UNDO
, 0, 0);
765 VOID
DIALOG_EditCut(VOID
)
767 SendMessage(Globals
.hEdit
, WM_CUT
, 0, 0);
770 VOID
DIALOG_EditCopy(VOID
)
772 SendMessage(Globals
.hEdit
, WM_COPY
, 0, 0);
775 VOID
DIALOG_EditPaste(VOID
)
777 SendMessage(Globals
.hEdit
, WM_PASTE
, 0, 0);
780 VOID
DIALOG_EditDelete(VOID
)
782 SendMessage(Globals
.hEdit
, WM_CLEAR
, 0, 0);
785 VOID
DIALOG_EditSelectAll(VOID
)
787 SendMessage(Globals
.hEdit
, EM_SETSEL
, 0, (LPARAM
)-1);
790 VOID
DIALOG_EditTimeDate(VOID
)
793 TCHAR szDate
[MAX_STRING_LEN
];
794 TCHAR szText
[MAX_STRING_LEN
* 2 + 2];
798 GetTimeFormat(LOCALE_USER_DEFAULT
, 0, &st
, NULL
, szDate
, MAX_STRING_LEN
);
799 _tcscpy(szText
, szDate
);
800 _tcscat(szText
, _T(" "));
801 GetDateFormat(LOCALE_USER_DEFAULT
, DATE_LONGDATE
, &st
, NULL
, szDate
, MAX_STRING_LEN
);
802 _tcscat(szText
, szDate
);
803 SendMessage(Globals
.hEdit
, EM_REPLACESEL
, TRUE
, (LPARAM
)szText
);
806 VOID
DoCreateStatusBar(VOID
)
810 BOOL bStatusBarVisible
;
812 /* Check if status bar object already exists. */
813 if (Globals
.hStatusBar
== NULL
)
815 /* Try to create the status bar */
816 Globals
.hStatusBar
= CreateStatusWindow(WS_CHILD
| WS_VISIBLE
| WS_EX_STATICEDGE
,
819 CMD_STATUSBAR_WND_ID
);
821 if (Globals
.hStatusBar
== NULL
)
827 /* Load the string for formatting column/row text output */
828 LoadString(Globals
.hInstance
, STRING_LINE_COLUMN
, Globals
.szStatusBarLineCol
, MAX_PATH
- 1);
830 /* Set the status bar for single-text output */
831 SendMessage(Globals
.hStatusBar
, SB_SIMPLE
, (WPARAM
)TRUE
, (LPARAM
)0);
834 /* Set status bar visiblity according to the settings. */
835 if (Globals
.bWrapLongLines
== TRUE
|| Globals
.bShowStatusBar
== FALSE
)
837 bStatusBarVisible
= FALSE
;
838 ShowWindow(Globals
.hStatusBar
, SW_HIDE
);
842 bStatusBarVisible
= TRUE
;
843 ShowWindow(Globals
.hStatusBar
, SW_SHOW
);
844 SendMessage(Globals
.hStatusBar
, WM_SIZE
, 0, 0);
847 /* Set check state in show status bar item. */
848 if (bStatusBarVisible
)
850 CheckMenuItem(Globals
.hMenu
, CMD_STATUSBAR
, MF_BYCOMMAND
| MF_CHECKED
);
854 CheckMenuItem(Globals
.hMenu
, CMD_STATUSBAR
, MF_BYCOMMAND
| MF_UNCHECKED
);
857 /* Update menu mar with the previous changes */
858 DrawMenuBar(Globals
.hMainWnd
);
860 /* Sefety test is edit control exists */
861 if (Globals
.hEdit
!= NULL
)
863 /* Retrieve the sizes of the controls */
864 GetClientRect(Globals
.hMainWnd
, &rc
);
865 GetClientRect(Globals
.hStatusBar
, &rcstatus
);
867 /* If status bar is currently visible, update dimensions of edit control */
868 if (bStatusBarVisible
)
869 rc
.bottom
-= (rcstatus
.bottom
- rcstatus
.top
);
871 /* Resize edit control to right size. */
872 MoveWindow(Globals
.hEdit
,
880 /* Update content with current row/column text */
881 DIALOG_StatusBarUpdateCaretPos();
884 VOID
DoCreateEditWindow(VOID
)
889 BOOL bModified
= FALSE
;
893 /* If the edit control already exists, try to save its content */
894 if (Globals
.hEdit
!= NULL
)
896 /* number of chars currently written into the editor. */
897 iSize
= GetWindowTextLength(Globals
.hEdit
);
900 /* Allocates temporary buffer. */
901 pTemp
= HeapAlloc(GetProcessHeap(), 0, (iSize
+ 1) * sizeof(TCHAR
));
908 /* Recover the text into the control. */
909 GetWindowText(Globals
.hEdit
, pTemp
, iSize
+ 1);
911 if (SendMessage(Globals
.hEdit
, EM_GETMODIFY
, 0, 0))
915 /* Restore original window procedure */
916 SetWindowLongPtr(Globals
.hEdit
, GWLP_WNDPROC
, (LONG_PTR
)Globals
.EditProc
);
918 /* Destroy the edit control */
919 DestroyWindow(Globals
.hEdit
);
922 /* Update wrap status into the main menu and recover style flags */
923 if (Globals
.bWrapLongLines
)
925 dwStyle
= EDIT_STYLE_WRAP
;
926 EnableMenuItem(Globals
.hMenu
, CMD_STATUSBAR
, MF_BYCOMMAND
| MF_DISABLED
| MF_GRAYED
);
928 dwStyle
= EDIT_STYLE
;
929 EnableMenuItem(Globals
.hMenu
, CMD_STATUSBAR
, MF_BYCOMMAND
| MF_ENABLED
);
932 /* Update previous changes */
933 DrawMenuBar(Globals
.hMainWnd
);
935 /* Create the new edit control */
936 Globals
.hEdit
= CreateWindowEx(WS_EX_CLIENTEDGE
,
949 if (Globals
.hEdit
== NULL
)
953 HeapFree(GetProcessHeap(), 0, pTemp
);
960 SendMessage(Globals
.hEdit
, WM_SETFONT
, (WPARAM
)Globals
.hFont
, FALSE
);
961 SendMessage(Globals
.hEdit
, EM_LIMITTEXT
, 0, 0);
963 /* If some text was previously saved, restore it. */
966 SetWindowText(Globals
.hEdit
, pTemp
);
967 HeapFree(GetProcessHeap(), 0, pTemp
);
970 SendMessage(Globals
.hEdit
, EM_SETMODIFY
, TRUE
, 0);
973 /* Sub-class a new window callback for row/column detection. */
974 Globals
.EditProc
= (WNDPROC
)SetWindowLongPtr(Globals
.hEdit
,
976 (LONG_PTR
)EDIT_WndProc
);
978 /* Create/update status bar */
981 /* Finally shows new edit control and set focus into it. */
982 ShowWindow(Globals
.hEdit
, SW_SHOW
);
983 SetFocus(Globals
.hEdit
);
986 VOID
DIALOG_EditWrap(VOID
)
988 Globals
.bWrapLongLines
= !Globals
.bWrapLongLines
;
990 if (Globals
.bWrapLongLines
)
992 EnableMenuItem(Globals
.hMenu
, CMD_GOTO
, MF_BYCOMMAND
| MF_DISABLED
| MF_GRAYED
);
996 EnableMenuItem(Globals
.hMenu
, CMD_GOTO
, MF_BYCOMMAND
| MF_ENABLED
);
999 DoCreateEditWindow();
1002 VOID
DIALOG_SelectFont(VOID
)
1005 LOGFONT lf
= Globals
.lfFont
;
1007 ZeroMemory( &cf
, sizeof(cf
) );
1008 cf
.lStructSize
= sizeof(cf
);
1009 cf
.hwndOwner
= Globals
.hMainWnd
;
1011 cf
.Flags
= CF_SCREENFONTS
| CF_INITTOLOGFONTSTRUCT
| CF_NOVERTFONTS
;
1013 if (ChooseFont(&cf
))
1015 HFONT currfont
= Globals
.hFont
;
1017 Globals
.hFont
= CreateFontIndirect(&lf
);
1018 Globals
.lfFont
= lf
;
1019 SendMessage(Globals
.hEdit
, WM_SETFONT
, (WPARAM
)Globals
.hFont
, (LPARAM
)TRUE
);
1020 if (currfont
!= NULL
)
1021 DeleteObject(currfont
);
1025 typedef HWND (WINAPI
*FINDPROC
)(LPFINDREPLACE lpfr
);
1027 static VOID
DIALOG_SearchDialog(FINDPROC pfnProc
)
1029 ZeroMemory(&Globals
.find
, sizeof(Globals
.find
));
1030 Globals
.find
.lStructSize
= sizeof(Globals
.find
);
1031 Globals
.find
.hwndOwner
= Globals
.hMainWnd
;
1032 Globals
.find
.hInstance
= Globals
.hInstance
;
1033 Globals
.find
.lpstrFindWhat
= Globals
.szFindText
;
1034 Globals
.find
.wFindWhatLen
= ARRAY_SIZE(Globals
.szFindText
);
1035 Globals
.find
.lpstrReplaceWith
= Globals
.szReplaceText
;
1036 Globals
.find
.wReplaceWithLen
= ARRAY_SIZE(Globals
.szReplaceText
);
1037 Globals
.find
.Flags
= FR_DOWN
;
1039 /* We only need to create the modal FindReplace dialog which will */
1040 /* notify us of incoming events using hMainWnd Window Messages */
1042 Globals
.hFindReplaceDlg
= pfnProc(&Globals
.find
);
1043 assert(Globals
.hFindReplaceDlg
!= 0);
1046 VOID
DIALOG_Search(VOID
)
1048 DIALOG_SearchDialog(FindText
);
1051 VOID
DIALOG_SearchNext(VOID
)
1053 if (Globals
.find
.lpstrFindWhat
!= NULL
)
1054 NOTEPAD_FindNext(&Globals
.find
, FALSE
, TRUE
);
1059 VOID
DIALOG_Replace(VOID
)
1061 DIALOG_SearchDialog(ReplaceText
);
1066 DIALOG_GoTo_DialogProc(HWND hwndDialog
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
1068 BOOL bResult
= FALSE
;
1074 hTextBox
= GetDlgItem(hwndDialog
, ID_LINENUMBER
);
1075 _sntprintf(szText
, ARRAY_SIZE(szText
), _T("%ld"), lParam
);
1076 SetWindowText(hTextBox
, szText
);
1079 if (HIWORD(wParam
) == BN_CLICKED
)
1081 if (LOWORD(wParam
) == IDOK
)
1083 hTextBox
= GetDlgItem(hwndDialog
, ID_LINENUMBER
);
1084 GetWindowText(hTextBox
, szText
, ARRAY_SIZE(szText
));
1085 EndDialog(hwndDialog
, _ttoi(szText
));
1088 else if (LOWORD(wParam
) == IDCANCEL
)
1090 EndDialog(hwndDialog
, 0);
1100 VOID
DIALOG_GoTo(VOID
)
1105 DWORD dwStart
, dwEnd
;
1107 nLength
= GetWindowTextLength(Globals
.hEdit
);
1108 pszText
= (LPTSTR
) HeapAlloc(GetProcessHeap(), 0, (nLength
+ 1) * sizeof(*pszText
));
1112 /* Retrieve current text */
1113 GetWindowText(Globals
.hEdit
, pszText
, nLength
+ 1);
1114 SendMessage(Globals
.hEdit
, EM_GETSEL
, (WPARAM
) &dwStart
, (LPARAM
) &dwEnd
);
1117 for (i
= 0; (i
< (int) dwStart
) && pszText
[i
]; i
++)
1119 if (pszText
[i
] == '\n')
1123 nLine
= DialogBoxParam(Globals
.hInstance
,
1124 MAKEINTRESOURCE(DIALOG_GOTO
),
1126 DIALOG_GoTo_DialogProc
,
1131 for (i
= 0; pszText
[i
] && (nLine
> 1) && (i
< nLength
- 1); i
++)
1133 if (pszText
[i
] == '\n')
1136 SendMessage(Globals
.hEdit
, EM_SETSEL
, i
, i
);
1137 SendMessage(Globals
.hEdit
, EM_SCROLLCARET
, 0, 0);
1139 HeapFree(GetProcessHeap(), 0, pszText
);
1142 VOID
DIALOG_StatusBarUpdateCaretPos(VOID
)
1145 TCHAR buff
[MAX_PATH
];
1146 DWORD dwStart
, dwSize
;
1148 SendMessage(Globals
.hEdit
, EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwSize
);
1149 line
= SendMessage(Globals
.hEdit
, EM_LINEFROMCHAR
, (WPARAM
)dwStart
, 0);
1150 col
= dwStart
- SendMessage(Globals
.hEdit
, EM_LINEINDEX
, (WPARAM
)line
, 0);
1152 _stprintf(buff
, Globals
.szStatusBarLineCol
, line
+ 1, col
+ 1);
1153 SendMessage(Globals
.hStatusBar
, SB_SETTEXT
, SB_SIMPLEID
, (LPARAM
)buff
);
1156 VOID
DIALOG_ViewStatusBar(VOID
)
1158 Globals
.bShowStatusBar
= !Globals
.bShowStatusBar
;
1160 DoCreateStatusBar();
1163 VOID
DIALOG_HelpContents(VOID
)
1165 WinHelp(Globals
.hMainWnd
, helpfile
, HELP_INDEX
, 0);
1168 VOID
DIALOG_HelpAboutNotepad(VOID
)
1170 TCHAR szNotepad
[MAX_STRING_LEN
];
1171 HICON notepadIcon
= LoadIcon(Globals
.hInstance
, MAKEINTRESOURCE(IDI_NPICON
));
1173 LoadString(Globals
.hInstance
, STRING_NOTEPAD
, szNotepad
, ARRAY_SIZE(szNotepad
));
1174 ShellAbout(Globals
.hMainWnd
, szNotepad
, 0, notepadIcon
);
1175 DeleteObject(notepadIcon
);
1180 AboutDialogProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
1182 HWND hLicenseEditWnd
;
1189 hLicenseEditWnd
= GetDlgItem(hDlg
, IDC_LICENSE
);
1191 /* 0x1000 should be enough */
1192 strLicense
= (TCHAR
*)_alloca(0x1000);
1193 LoadString(GetModuleHandle(NULL
), STRING_LICENSE
, strLicense
, 0x1000);
1195 SetWindowText(hLicenseEditWnd
, strLicense
);
1201 if ((LOWORD(wParam
) == IDOK
) || (LOWORD(wParam
) == IDCANCEL
))
1203 EndDialog(hDlg
, LOWORD(wParam
));
1213 /***********************************************************************
1215 * DIALOG_FilePageSetup
1217 VOID
DIALOG_FilePageSetup(void)
1221 ZeroMemory(&page
, sizeof(page
));
1222 page
.lStructSize
= sizeof(page
);
1223 page
.hwndOwner
= Globals
.hMainWnd
;
1224 page
.Flags
= PSD_ENABLEPAGESETUPTEMPLATE
| PSD_ENABLEPAGESETUPHOOK
| PSD_MARGINS
;
1225 page
.hInstance
= Globals
.hInstance
;
1226 page
.rtMargin
= Globals
.lMargins
;
1227 page
.hDevMode
= Globals
.hDevMode
;
1228 page
.hDevNames
= Globals
.hDevNames
;
1229 page
.lpPageSetupTemplateName
= MAKEINTRESOURCE(DIALOG_PAGESETUP
);
1230 page
.lpfnPageSetupHook
= DIALOG_PAGESETUP_Hook
;
1232 PageSetupDlg(&page
);
1234 Globals
.hDevMode
= page
.hDevMode
;
1235 Globals
.hDevNames
= page
.hDevNames
;
1236 Globals
.lMargins
= page
.rtMargin
;
1239 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1241 * DIALOG_PAGESETUP_Hook
1244 static UINT_PTR CALLBACK
DIALOG_PAGESETUP_Hook(HWND hDlg
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
1249 if (HIWORD(wParam
) == BN_CLICKED
)
1251 switch (LOWORD(wParam
))
1254 /* save user input and close dialog */
1255 GetDlgItemText(hDlg
, 0x141, Globals
.szHeader
, ARRAY_SIZE(Globals
.szHeader
));
1256 GetDlgItemText(hDlg
, 0x143, Globals
.szFooter
, ARRAY_SIZE(Globals
.szFooter
));
1260 /* discard user input and close dialog */
1265 /* FIXME: Bring this to work */
1266 static const TCHAR sorry
[] = _T("Sorry, no help available");
1267 static const TCHAR help
[] = _T("Help");
1268 MessageBox(Globals
.hMainWnd
, sorry
, help
, MB_ICONEXCLAMATION
);
1279 /* fetch last user input prior to display dialog */
1280 SetDlgItemText(hDlg
, 0x141, Globals
.szHeader
);
1281 SetDlgItemText(hDlg
, 0x143, Globals
.szFooter
);