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;
228 static BOOL
DoSaveFile(VOID
)
235 hFile
= CreateFile(Globals
.szFileName
, GENERIC_WRITE
, FILE_SHARE_WRITE
,
236 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
237 if(hFile
== INVALID_HANDLE_VALUE
)
243 size
= GetWindowTextLength(Globals
.hEdit
) + 1;
244 pTemp
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(*pTemp
));
251 size
= GetWindowText(Globals
.hEdit
, pTemp
, size
);
255 if (!WriteText(hFile
, (LPWSTR
)pTemp
, size
, Globals
.encFile
, Globals
.iEoln
))
262 SendMessage(Globals
.hEdit
, EM_SETMODIFY
, FALSE
, 0);
268 HeapFree(GetProcessHeap(), 0, pTemp
);
274 * TRUE - User agreed to close (both save/don't save)
275 * FALSE - User cancelled close by selecting "Cancel"
277 BOOL
DoCloseFile(VOID
)
281 if (SendMessage(Globals
.hEdit
, EM_GETMODIFY
, 0, 0))
283 /* prompt user to save changes */
284 nResult
= AlertFileNotSaved(Globals
.szFileName
);
288 if(!DIALOG_FileSave())
303 SetFileName(empty_str
);
304 UpdateWindowCaption();
309 VOID
DoOpenFile(LPCTSTR szFileName
)
311 static const TCHAR dotlog
[] = _T(".LOG");
313 LPTSTR pszText
= NULL
;
317 /* Close any files and prompt to save changes */
321 hFile
= CreateFile(szFileName
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
322 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
323 if (hFile
== INVALID_HANDLE_VALUE
)
329 if (!ReadText(hFile
, (LPWSTR
*)&pszText
, &dwTextLen
, &Globals
.encFile
, &Globals
.iEoln
))
334 SetWindowText(Globals
.hEdit
, pszText
);
336 SendMessage(Globals
.hEdit
, EM_SETMODIFY
, FALSE
, 0);
337 SendMessage(Globals
.hEdit
, EM_EMPTYUNDOBUFFER
, 0, 0);
338 SetFocus(Globals
.hEdit
);
340 /* If the file starts with .LOG, add a time/date at the end and set cursor after
341 * See http://support.microsoft.com/?kbid=260563
343 if (GetWindowText(Globals
.hEdit
, log
, ARRAY_SIZE(log
)) && !_tcscmp(log
, dotlog
))
345 static const TCHAR lf
[] = _T("\r\n");
346 SendMessage(Globals
.hEdit
, EM_SETSEL
, GetWindowTextLength(Globals
.hEdit
), -1);
347 SendMessage(Globals
.hEdit
, EM_REPLACESEL
, TRUE
, (LPARAM
)lf
);
348 DIALOG_EditTimeDate();
349 SendMessage(Globals
.hEdit
, EM_REPLACESEL
, TRUE
, (LPARAM
)lf
);
352 SetFileName(szFileName
);
353 UpdateWindowCaption();
354 NOTEPAD_EnableSearchMenu();
356 if (hFile
!= INVALID_HANDLE_VALUE
)
359 HeapFree(GetProcessHeap(), 0, pszText
);
362 VOID
DIALOG_FileNew(VOID
)
364 /* Close any files and prompt to save changes */
366 SetWindowText(Globals
.hEdit
, empty_str
);
367 SendMessage(Globals
.hEdit
, EM_EMPTYUNDOBUFFER
, 0, 0);
368 SetFocus(Globals
.hEdit
);
369 NOTEPAD_EnableSearchMenu();
373 VOID
DIALOG_FileOpen(VOID
)
375 OPENFILENAME openfilename
;
376 TCHAR szPath
[MAX_PATH
];
378 ZeroMemory(&openfilename
, sizeof(openfilename
));
380 if (Globals
.szFileName
[0] == 0)
381 _tcscpy(szPath
, txt_files
);
383 _tcscpy(szPath
, Globals
.szFileName
);
385 openfilename
.lStructSize
= sizeof(openfilename
);
386 openfilename
.hwndOwner
= Globals
.hMainWnd
;
387 openfilename
.hInstance
= Globals
.hInstance
;
388 openfilename
.lpstrFilter
= Globals
.szFilter
;
389 openfilename
.lpstrFile
= szPath
;
390 openfilename
.nMaxFile
= ARRAY_SIZE(szPath
);
391 openfilename
.Flags
= OFN_FILEMUSTEXIST
| OFN_PATHMUSTEXIST
| OFN_HIDEREADONLY
;
392 openfilename
.lpstrDefExt
= szDefaultExt
;
394 if (GetOpenFileName(&openfilename
)) {
395 if (FileExists(openfilename
.lpstrFile
))
396 DoOpenFile(openfilename
.lpstrFile
);
398 AlertFileNotFound(openfilename
.lpstrFile
);
402 BOOL
DIALOG_FileSave(VOID
)
404 if (Globals
.szFileName
[0] == 0)
405 return DIALOG_FileSaveAs();
412 DIALOG_FileSaveAs_Hook(HWND hDlg
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
417 UNREFERENCED_PARAMETER(wParam
);
422 hCombo
= GetDlgItem(hDlg
, ID_ENCODING
);
424 LoadString(Globals
.hInstance
, STRING_ANSI
, szText
, ARRAY_SIZE(szText
));
425 SendMessage(hCombo
, CB_ADDSTRING
, 0, (LPARAM
) szText
);
427 LoadString(Globals
.hInstance
, STRING_UNICODE
, szText
, ARRAY_SIZE(szText
));
428 SendMessage(hCombo
, CB_ADDSTRING
, 0, (LPARAM
) szText
);
430 LoadString(Globals
.hInstance
, STRING_UNICODE_BE
, szText
, ARRAY_SIZE(szText
));
431 SendMessage(hCombo
, CB_ADDSTRING
, 0, (LPARAM
) szText
);
433 LoadString(Globals
.hInstance
, STRING_UTF8
, szText
, ARRAY_SIZE(szText
));
434 SendMessage(hCombo
, CB_ADDSTRING
, 0, (LPARAM
) szText
);
436 SendMessage(hCombo
, CB_SETCURSEL
, Globals
.encFile
, 0);
438 hCombo
= GetDlgItem(hDlg
, ID_EOLN
);
440 LoadString(Globals
.hInstance
, STRING_CRLF
, szText
, ARRAY_SIZE(szText
));
441 SendMessage(hCombo
, CB_ADDSTRING
, 0, (LPARAM
) szText
);
443 LoadString(Globals
.hInstance
, STRING_LF
, szText
, ARRAY_SIZE(szText
));
444 SendMessage(hCombo
, CB_ADDSTRING
, 0, (LPARAM
) szText
);
446 LoadString(Globals
.hInstance
, STRING_CR
, szText
, ARRAY_SIZE(szText
));
447 SendMessage(hCombo
, CB_ADDSTRING
, 0, (LPARAM
) szText
);
449 SendMessage(hCombo
, CB_SETCURSEL
, Globals
.iEoln
, 0);
453 if (((NMHDR
*) lParam
)->code
== CDN_FILEOK
)
455 hCombo
= GetDlgItem(hDlg
, ID_ENCODING
);
457 Globals
.encFile
= (int) SendMessage(hCombo
, CB_GETCURSEL
, 0, 0);
459 hCombo
= GetDlgItem(hDlg
, ID_EOLN
);
461 Globals
.iEoln
= (int) SendMessage(hCombo
, CB_GETCURSEL
, 0, 0);
468 BOOL
DIALOG_FileSaveAs(VOID
)
471 TCHAR szPath
[MAX_PATH
];
473 ZeroMemory(&saveas
, sizeof(saveas
));
475 if (Globals
.szFileName
[0] == 0)
476 _tcscpy(szPath
, txt_files
);
478 _tcscpy(szPath
, Globals
.szFileName
);
480 saveas
.lStructSize
= sizeof(OPENFILENAME
);
481 saveas
.hwndOwner
= Globals
.hMainWnd
;
482 saveas
.hInstance
= Globals
.hInstance
;
483 saveas
.lpstrFilter
= Globals
.szFilter
;
484 saveas
.lpstrFile
= szPath
;
485 saveas
.nMaxFile
= ARRAY_SIZE(szPath
);
486 saveas
.Flags
= OFN_PATHMUSTEXIST
| OFN_OVERWRITEPROMPT
| OFN_HIDEREADONLY
|
487 OFN_EXPLORER
| OFN_ENABLETEMPLATE
| OFN_ENABLEHOOK
;
488 saveas
.lpstrDefExt
= szDefaultExt
;
489 saveas
.lpTemplateName
= MAKEINTRESOURCE(DIALOG_ENCODING
);
490 saveas
.lpfnHook
= DIALOG_FileSaveAs_Hook
;
492 if (GetSaveFileName(&saveas
))
494 /* HACK: Because in ROS, Save-As boxes don't check the validity
495 * of file names and thus, here, szPath can be invalid !! We only
496 * see its validity when we call DoSaveFile()... */
500 UpdateWindowCaption();
515 VOID
DIALOG_FilePrint(VOID
)
521 int cWidthPels
, cHeightPels
, border
;
522 int xLeft
, yTop
, pagecount
, dopage
, copycount
;
525 HFONT font
, old_font
=0;
528 static const TCHAR times_new_roman
[] = _T("Times New Roman");
530 /* Get a small font and print some header info on each page */
531 ZeroMemory(&hdrFont
, sizeof(hdrFont
));
532 hdrFont
.lfHeight
= 100;
533 hdrFont
.lfWeight
= FW_BOLD
;
534 hdrFont
.lfCharSet
= ANSI_CHARSET
;
535 hdrFont
.lfOutPrecision
= OUT_DEFAULT_PRECIS
;
536 hdrFont
.lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
537 hdrFont
.lfQuality
= PROOF_QUALITY
;
538 hdrFont
.lfPitchAndFamily
= VARIABLE_PITCH
| FF_ROMAN
;
539 _tcscpy(hdrFont
.lfFaceName
, times_new_roman
);
541 font
= CreateFontIndirect(&hdrFont
);
543 /* Get Current Settings */
544 ZeroMemory(&printer
, sizeof(printer
));
545 printer
.lStructSize
= sizeof(printer
);
546 printer
.hwndOwner
= Globals
.hMainWnd
;
547 printer
.hInstance
= Globals
.hInstance
;
549 /* Set some default flags */
550 printer
.Flags
= PD_RETURNDC
| PD_SELECTION
;
552 /* Disable the selection radio button if there is no text selected */
553 if (!GetSelectionTextLength(Globals
.hEdit
))
555 printer
.Flags
= printer
.Flags
| PD_NOSELECTION
;
558 printer
.nFromPage
= 0;
559 printer
.nMinPage
= 1;
560 /* we really need to calculate number of pages to set nMaxPage and nToPage */
561 printer
.nToPage
= (WORD
)-1;
562 printer
.nMaxPage
= (WORD
)-1;
564 /* Let commdlg manage copy settings */
565 printer
.nCopies
= (WORD
)PD_USEDEVMODECOPIES
;
567 printer
.hDevMode
= Globals
.hDevMode
;
568 printer
.hDevNames
= Globals
.hDevNames
;
570 if (!PrintDlg(&printer
))
576 Globals
.hDevMode
= printer
.hDevMode
;
577 Globals
.hDevNames
= printer
.hDevNames
;
579 assert(printer
.hDC
!= 0);
581 /* initialize DOCINFO */
582 di
.cbSize
= sizeof(DOCINFO
);
583 di
.lpszDocName
= Globals
.szFileTitle
;
584 di
.lpszOutput
= NULL
;
585 di
.lpszDatatype
= NULL
;
588 if (StartDoc(printer
.hDC
, &di
) <= 0)
594 /* Get the page dimensions in pixels. */
595 cWidthPels
= GetDeviceCaps(printer
.hDC
, HORZRES
);
596 cHeightPels
= GetDeviceCaps(printer
.hDC
, VERTRES
);
598 /* Get the file text */
599 if (printer
.Flags
& PD_SELECTION
)
601 size
= GetSelectionTextLength(Globals
.hEdit
) + 1;
605 size
= GetWindowTextLength(Globals
.hEdit
) + 1;
608 pTemp
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(TCHAR
));
617 if (printer
.Flags
& PD_SELECTION
)
619 size
= GetSelectionText(Globals
.hEdit
, pTemp
, size
);
623 size
= GetWindowText(Globals
.hEdit
, pTemp
, size
);
626 /* Ensure that each logical unit maps to one pixel */
627 SetMapMode(printer
.hDC
, MM_TEXT
);
629 /* Needed to get the correct height of a text line */
630 GetTextMetrics(printer
.hDC
, &tm
);
633 for (copycount
=1; copycount
<= printer
.nCopies
; copycount
++) {
637 /* Don't start a page if none of the conditions below are true */
640 /* The user wants to print the current selection */
641 if (printer
.Flags
& PD_SELECTION
)
646 /* The user wants to print the entire document */
647 if (!(printer
.Flags
& PD_PAGENUMS
) && !(printer
.Flags
& PD_SELECTION
))
652 /* The user wants to print a specified range of pages */
653 if ((pagecount
>= printer
.nFromPage
&& pagecount
<= printer
.nToPage
))
658 old_font
= SelectObject(printer
.hDC
, font
);
661 if (StartPage(printer
.hDC
) <= 0) {
662 SelectObject(printer
.hDC
, old_font
);
664 DeleteDC(printer
.hDC
);
665 HeapFree(GetProcessHeap(), 0, pTemp
);
670 /* Write a rectangle and header at the top of each page */
671 Rectangle(printer
.hDC
, border
, border
, cWidthPels
-border
, border
+ tm
.tmHeight
* 2);
672 /* I don't know what's up with this TextOut command. This comes out
677 border
+ tm
.tmHeight
/ 2,
679 lstrlen(Globals
.szFileTitle
));
682 /* The starting point for the main text */
684 yTop
= border
+ tm
.tmHeight
* 4;
686 SelectObject(printer
.hDC
, old_font
);
688 /* Since outputting strings is giving me problems, output the main
689 * text one character at a time. */
691 if (pTemp
[i
] == '\n') {
695 else if (pTemp
[i
] != '\r') {
697 TextOut(printer
.hDC
, xLeft
, yTop
, &pTemp
[i
], 1);
699 /* We need to get the width for each individual char, since a proportional font may be used */
700 GetTextExtentPoint32(printer
.hDC
, &pTemp
[i
], 1, &szMetric
);
701 xLeft
+= szMetric
.cx
;
703 /* Insert a line break if the current line does not fit into the printing area */
704 if (xLeft
> (cWidthPels
- border
* 2))
707 yTop
= yTop
+ tm
.tmHeight
;
710 } while (i
++ < size
&& yTop
< (cHeightPels
- border
* 2));
713 EndPage(printer
.hDC
);
719 SelectObject(printer
.hDC
, old_font
);
721 DeleteDC(printer
.hDC
);
722 HeapFree(GetProcessHeap(), 0, pTemp
);
726 VOID
DIALOG_FileExit(VOID
)
728 PostMessage(Globals
.hMainWnd
, WM_CLOSE
, 0, 0l);
731 VOID
DIALOG_EditUndo(VOID
)
733 SendMessage(Globals
.hEdit
, EM_UNDO
, 0, 0);
736 VOID
DIALOG_EditCut(VOID
)
738 SendMessage(Globals
.hEdit
, WM_CUT
, 0, 0);
741 VOID
DIALOG_EditCopy(VOID
)
743 SendMessage(Globals
.hEdit
, WM_COPY
, 0, 0);
746 VOID
DIALOG_EditPaste(VOID
)
748 SendMessage(Globals
.hEdit
, WM_PASTE
, 0, 0);
751 VOID
DIALOG_EditDelete(VOID
)
753 SendMessage(Globals
.hEdit
, WM_CLEAR
, 0, 0);
756 VOID
DIALOG_EditSelectAll(VOID
)
758 SendMessage(Globals
.hEdit
, EM_SETSEL
, 0, (LPARAM
)-1);
761 VOID
DIALOG_EditTimeDate(VOID
)
764 TCHAR szDate
[MAX_STRING_LEN
];
765 TCHAR szText
[MAX_STRING_LEN
* 2 + 2];
769 GetTimeFormat(LOCALE_USER_DEFAULT
, 0, &st
, NULL
, szDate
, MAX_STRING_LEN
);
770 _tcscpy(szText
, szDate
);
771 _tcscat(szText
, _T(" "));
772 GetDateFormat(LOCALE_USER_DEFAULT
, DATE_LONGDATE
, &st
, NULL
, szDate
, MAX_STRING_LEN
);
773 _tcscat(szText
, szDate
);
774 SendMessage(Globals
.hEdit
, EM_REPLACESEL
, TRUE
, (LPARAM
)szText
);
777 VOID
DoCreateStatusBar(VOID
)
781 BOOL bStatusBarVisible
;
783 /* Check if status bar object already exists. */
784 if (Globals
.hStatusBar
== NULL
)
786 /* Try to create the status bar */
787 Globals
.hStatusBar
= CreateStatusWindow(WS_CHILD
| WS_VISIBLE
| WS_EX_STATICEDGE
,
790 CMD_STATUSBAR_WND_ID
);
792 if (Globals
.hStatusBar
== NULL
)
798 /* Load the string for formatting column/row text output */
799 LoadString(Globals
.hInstance
, STRING_LINE_COLUMN
, Globals
.szStatusBarLineCol
, MAX_PATH
- 1);
801 /* Set the status bar for single-text output */
802 SendMessage(Globals
.hStatusBar
, SB_SIMPLE
, (WPARAM
)TRUE
, (LPARAM
)0);
805 /* Set status bar visiblity according to the settings. */
806 if (Globals
.bWrapLongLines
== TRUE
|| Globals
.bShowStatusBar
== FALSE
)
808 bStatusBarVisible
= FALSE
;
809 ShowWindow(Globals
.hStatusBar
, SW_HIDE
);
813 bStatusBarVisible
= TRUE
;
814 ShowWindow(Globals
.hStatusBar
, SW_SHOW
);
815 SendMessage(Globals
.hStatusBar
, WM_SIZE
, 0, 0);
818 /* Set check state in show status bar item. */
819 if (bStatusBarVisible
)
821 CheckMenuItem(Globals
.hMenu
, CMD_STATUSBAR
, MF_BYCOMMAND
| MF_CHECKED
);
825 CheckMenuItem(Globals
.hMenu
, CMD_STATUSBAR
, MF_BYCOMMAND
| MF_UNCHECKED
);
828 /* Update menu mar with the previous changes */
829 DrawMenuBar(Globals
.hMainWnd
);
831 /* Sefety test is edit control exists */
832 if (Globals
.hEdit
!= NULL
)
834 /* Retrieve the sizes of the controls */
835 GetClientRect(Globals
.hMainWnd
, &rc
);
836 GetClientRect(Globals
.hStatusBar
, &rcstatus
);
838 /* If status bar is currently visible, update dimensions of edit control */
839 if (bStatusBarVisible
)
840 rc
.bottom
-= (rcstatus
.bottom
- rcstatus
.top
);
842 /* Resize edit control to right size. */
843 MoveWindow(Globals
.hEdit
,
851 /* Update content with current row/column text */
852 DIALOG_StatusBarUpdateCaretPos();
855 VOID
DoCreateEditWindow(VOID
)
860 BOOL bModified
= FALSE
;
864 /* If the edit control already exists, try to save its content */
865 if (Globals
.hEdit
!= NULL
)
867 /* number of chars currently written into the editor. */
868 iSize
= GetWindowTextLength(Globals
.hEdit
);
871 /* Allocates temporary buffer. */
872 pTemp
= HeapAlloc(GetProcessHeap(), 0, (iSize
+ 1) * sizeof(TCHAR
));
879 /* Recover the text into the control. */
880 GetWindowText(Globals
.hEdit
, pTemp
, iSize
+ 1);
882 if (SendMessage(Globals
.hEdit
, EM_GETMODIFY
, 0, 0))
886 /* Restore original window procedure */
887 SetWindowLongPtr(Globals
.hEdit
, GWLP_WNDPROC
, (LONG_PTR
)Globals
.EditProc
);
889 /* Destroy the edit control */
890 DestroyWindow(Globals
.hEdit
);
893 /* Update wrap status into the main menu and recover style flags */
894 if (Globals
.bWrapLongLines
)
896 dwStyle
= EDIT_STYLE_WRAP
;
897 EnableMenuItem(Globals
.hMenu
, CMD_STATUSBAR
, MF_BYCOMMAND
| MF_DISABLED
| MF_GRAYED
);
899 dwStyle
= EDIT_STYLE
;
900 EnableMenuItem(Globals
.hMenu
, CMD_STATUSBAR
, MF_BYCOMMAND
| MF_ENABLED
);
903 /* Update previous changes */
904 DrawMenuBar(Globals
.hMainWnd
);
906 /* Create the new edit control */
907 Globals
.hEdit
= CreateWindowEx(WS_EX_CLIENTEDGE
,
920 if (Globals
.hEdit
== NULL
)
924 HeapFree(GetProcessHeap(), 0, pTemp
);
931 SendMessage(Globals
.hEdit
, WM_SETFONT
, (WPARAM
)Globals
.hFont
, FALSE
);
932 SendMessage(Globals
.hEdit
, EM_LIMITTEXT
, 0, 0);
934 /* If some text was previously saved, restore it. */
937 SetWindowText(Globals
.hEdit
, pTemp
);
938 HeapFree(GetProcessHeap(), 0, pTemp
);
941 SendMessage(Globals
.hEdit
, EM_SETMODIFY
, TRUE
, 0);
944 /* Sub-class a new window callback for row/column detection. */
945 Globals
.EditProc
= (WNDPROC
)SetWindowLongPtr(Globals
.hEdit
,
947 (LONG_PTR
)EDIT_WndProc
);
949 /* Create/update status bar */
952 /* Finally shows new edit control and set focus into it. */
953 ShowWindow(Globals
.hEdit
, SW_SHOW
);
954 SetFocus(Globals
.hEdit
);
957 VOID
DIALOG_EditWrap(VOID
)
959 Globals
.bWrapLongLines
= !Globals
.bWrapLongLines
;
960 DoCreateEditWindow();
963 VOID
DIALOG_SelectFont(VOID
)
966 LOGFONT lf
= Globals
.lfFont
;
968 ZeroMemory( &cf
, sizeof(cf
) );
969 cf
.lStructSize
= sizeof(cf
);
970 cf
.hwndOwner
= Globals
.hMainWnd
;
972 cf
.Flags
= CF_SCREENFONTS
| CF_INITTOLOGFONTSTRUCT
| CF_NOVERTFONTS
;
976 HFONT currfont
= Globals
.hFont
;
978 Globals
.hFont
= CreateFontIndirect(&lf
);
980 SendMessage(Globals
.hEdit
, WM_SETFONT
, (WPARAM
)Globals
.hFont
, (LPARAM
)TRUE
);
981 if (currfont
!= NULL
)
982 DeleteObject(currfont
);
986 typedef HWND (WINAPI
*FINDPROC
)(LPFINDREPLACE lpfr
);
988 static VOID
DIALOG_SearchDialog(FINDPROC pfnProc
)
990 ZeroMemory(&Globals
.find
, sizeof(Globals
.find
));
991 Globals
.find
.lStructSize
= sizeof(Globals
.find
);
992 Globals
.find
.hwndOwner
= Globals
.hMainWnd
;
993 Globals
.find
.hInstance
= Globals
.hInstance
;
994 Globals
.find
.lpstrFindWhat
= Globals
.szFindText
;
995 Globals
.find
.wFindWhatLen
= ARRAY_SIZE(Globals
.szFindText
);
996 Globals
.find
.lpstrReplaceWith
= Globals
.szReplaceText
;
997 Globals
.find
.wReplaceWithLen
= ARRAY_SIZE(Globals
.szReplaceText
);
998 Globals
.find
.Flags
= FR_DOWN
;
1000 /* We only need to create the modal FindReplace dialog which will */
1001 /* notify us of incoming events using hMainWnd Window Messages */
1003 Globals
.hFindReplaceDlg
= pfnProc(&Globals
.find
);
1004 assert(Globals
.hFindReplaceDlg
!= 0);
1007 VOID
DIALOG_Search(VOID
)
1009 DIALOG_SearchDialog(FindText
);
1012 VOID
DIALOG_SearchNext(VOID
)
1014 if (Globals
.find
.lpstrFindWhat
!= NULL
)
1015 NOTEPAD_FindNext(&Globals
.find
, FALSE
, TRUE
);
1020 VOID
DIALOG_Replace(VOID
)
1022 DIALOG_SearchDialog(ReplaceText
);
1027 DIALOG_GoTo_DialogProc(HWND hwndDialog
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
1029 BOOL bResult
= FALSE
;
1035 hTextBox
= GetDlgItem(hwndDialog
, ID_LINENUMBER
);
1036 _sntprintf(szText
, ARRAY_SIZE(szText
), _T("%ld"), lParam
);
1037 SetWindowText(hTextBox
, szText
);
1040 if (HIWORD(wParam
) == BN_CLICKED
)
1042 if (LOWORD(wParam
) == IDOK
)
1044 hTextBox
= GetDlgItem(hwndDialog
, ID_LINENUMBER
);
1045 GetWindowText(hTextBox
, szText
, ARRAY_SIZE(szText
));
1046 EndDialog(hwndDialog
, _ttoi(szText
));
1049 else if (LOWORD(wParam
) == IDCANCEL
)
1051 EndDialog(hwndDialog
, 0);
1061 VOID
DIALOG_GoTo(VOID
)
1066 DWORD dwStart
, dwEnd
;
1068 nLength
= GetWindowTextLength(Globals
.hEdit
);
1069 pszText
= (LPTSTR
) HeapAlloc(GetProcessHeap(), 0, (nLength
+ 1) * sizeof(*pszText
));
1073 /* Retrieve current text */
1074 GetWindowText(Globals
.hEdit
, pszText
, nLength
+ 1);
1075 SendMessage(Globals
.hEdit
, EM_GETSEL
, (WPARAM
) &dwStart
, (LPARAM
) &dwEnd
);
1078 for (i
= 0; pszText
[i
] && (i
< (int) dwStart
); i
++)
1080 if (pszText
[i
] == '\n')
1084 nLine
= DialogBoxParam(Globals
.hInstance
,
1085 MAKEINTRESOURCE(DIALOG_GOTO
),
1087 DIALOG_GoTo_DialogProc
,
1092 for (i
= 0; pszText
[i
] && (nLine
> 1) && (i
< nLength
- 1); i
++)
1094 if (pszText
[i
] == '\n')
1097 SendMessage(Globals
.hEdit
, EM_SETSEL
, i
, i
);
1098 SendMessage(Globals
.hEdit
, EM_SCROLLCARET
, 0, 0);
1100 HeapFree(GetProcessHeap(), 0, pszText
);
1103 VOID
DIALOG_StatusBarUpdateCaretPos(VOID
)
1106 TCHAR buff
[MAX_PATH
];
1107 DWORD dwStart
, dwSize
;
1109 SendMessage(Globals
.hEdit
, EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwSize
);
1110 line
= SendMessage(Globals
.hEdit
, EM_LINEFROMCHAR
, (WPARAM
)dwStart
, 0);
1111 col
= dwStart
- SendMessage(Globals
.hEdit
, EM_LINEINDEX
, (WPARAM
)line
, 0);
1113 _stprintf(buff
, Globals
.szStatusBarLineCol
, line
+ 1, col
+ 1);
1114 SendMessage(Globals
.hStatusBar
, SB_SETTEXT
, SB_SIMPLEID
, (LPARAM
)buff
);
1117 VOID
DIALOG_ViewStatusBar(VOID
)
1119 Globals
.bShowStatusBar
= !Globals
.bShowStatusBar
;
1121 DoCreateStatusBar();
1124 VOID
DIALOG_HelpContents(VOID
)
1126 WinHelp(Globals
.hMainWnd
, helpfile
, HELP_INDEX
, 0);
1129 VOID
DIALOG_HelpAboutNotepad(VOID
)
1131 TCHAR szNotepad
[MAX_STRING_LEN
];
1132 HICON notepadIcon
= LoadIcon(Globals
.hInstance
, MAKEINTRESOURCE(IDI_NPICON
));
1134 LoadString(Globals
.hInstance
, STRING_NOTEPAD
, szNotepad
, ARRAY_SIZE(szNotepad
));
1135 ShellAbout(Globals
.hMainWnd
, szNotepad
, 0, notepadIcon
);
1136 DeleteObject(notepadIcon
);
1141 AboutDialogProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
1143 HWND hLicenseEditWnd
;
1150 hLicenseEditWnd
= GetDlgItem(hDlg
, IDC_LICENSE
);
1152 /* 0x1000 should be enough */
1153 strLicense
= (TCHAR
*)_alloca(0x1000);
1154 LoadString(GetModuleHandle(NULL
), STRING_LICENSE
, strLicense
, 0x1000);
1156 SetWindowText(hLicenseEditWnd
, strLicense
);
1162 if ((LOWORD(wParam
) == IDOK
) || (LOWORD(wParam
) == IDCANCEL
))
1164 EndDialog(hDlg
, LOWORD(wParam
));
1174 /***********************************************************************
1176 * DIALOG_FilePageSetup
1178 VOID
DIALOG_FilePageSetup(void)
1182 ZeroMemory(&page
, sizeof(page
));
1183 page
.lStructSize
= sizeof(page
);
1184 page
.hwndOwner
= Globals
.hMainWnd
;
1185 page
.Flags
= PSD_ENABLEPAGESETUPTEMPLATE
| PSD_ENABLEPAGESETUPHOOK
| PSD_MARGINS
;
1186 page
.hInstance
= Globals
.hInstance
;
1187 page
.rtMargin
.left
= Globals
.lMarginLeft
;
1188 page
.rtMargin
.top
= Globals
.lMarginTop
;
1189 page
.rtMargin
.right
= Globals
.lMarginRight
;
1190 page
.rtMargin
.bottom
= Globals
.lMarginBottom
;
1191 page
.hDevMode
= Globals
.hDevMode
;
1192 page
.hDevNames
= Globals
.hDevNames
;
1193 page
.lpPageSetupTemplateName
= MAKEINTRESOURCE(DIALOG_PAGESETUP
);
1194 page
.lpfnPageSetupHook
= DIALOG_PAGESETUP_Hook
;
1196 PageSetupDlg(&page
);
1198 Globals
.hDevMode
= page
.hDevMode
;
1199 Globals
.hDevNames
= page
.hDevNames
;
1200 Globals
.lMarginLeft
= page
.rtMargin
.left
;
1201 Globals
.lMarginTop
= page
.rtMargin
.top
;
1202 Globals
.lMarginRight
= page
.rtMargin
.right
;
1203 Globals
.lMarginBottom
= page
.rtMargin
.bottom
;
1206 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1208 * DIALOG_PAGESETUP_Hook
1211 static UINT_PTR CALLBACK
DIALOG_PAGESETUP_Hook(HWND hDlg
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
1216 if (HIWORD(wParam
) == BN_CLICKED
)
1218 switch (LOWORD(wParam
))
1221 /* save user input and close dialog */
1222 GetDlgItemText(hDlg
, 0x141, Globals
.szHeader
, ARRAY_SIZE(Globals
.szHeader
));
1223 GetDlgItemText(hDlg
, 0x143, Globals
.szFooter
, ARRAY_SIZE(Globals
.szFooter
));
1227 /* discard user input and close dialog */
1232 /* FIXME: Bring this to work */
1233 static const TCHAR sorry
[] = _T("Sorry, no help available");
1234 static const TCHAR help
[] = _T("Help");
1235 MessageBox(Globals
.hMainWnd
, sorry
, help
, MB_ICONEXCLAMATION
);
1246 /* fetch last user input prior to display dialog */
1247 SetDlgItemText(hDlg
, 0x141, Globals
.szHeader
);
1248 SetDlgItemText(hDlg
, 0x143, Globals
.szFooter
);