4 * Copyright 1998,99 Marcel Baur <mbaur@g26.ethz.ch>
5 * Copyright 2002 Sylvain Petreolle <spetreolle@yahoo.fr>
6 * Copyright 2002 Andriy Palamarchuk
7 * Copyright 2007 Rolf Kalbermatter
8 * Copyright 2010 Vitaly Perov
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
35 #define SPACES_IN_TAB 8
36 #define PRINT_LEN_MAX 500
38 static const WCHAR helpfileW
[] = { 'n','o','t','e','p','a','d','.','h','l','p',0 };
40 static INT_PTR WINAPI
DIALOG_PAGESETUP_DlgProc(HWND hDlg
, UINT msg
, WPARAM wParam
, LPARAM lParam
);
42 /* Swap bytes of WCHAR buffer (big-endian <-> little-endian). */
43 static inline void byteswap_wide_string(LPWSTR str
, UINT num
)
46 for (i
= 0; i
< num
; i
++) str
[i
] = RtlUshortByteSwap(str
[i
]);
49 static void load_encoding_name(ENCODING enc
, WCHAR
* buffer
, int length
)
53 case ENCODING_UTF16LE
:
54 LoadStringW(Globals
.hInstance
, STRING_UNICODE_LE
, buffer
, length
);
57 case ENCODING_UTF16BE
:
58 LoadStringW(Globals
.hInstance
, STRING_UNICODE_BE
, buffer
, length
);
64 GetCPInfoExW((enc
==ENCODING_UTF8
) ? CP_UTF8
: CP_ACP
, 0, &cpi
);
65 lstrcpynW(buffer
, cpi
.CodePageName
, length
);
71 VOID
ShowLastError(void)
73 DWORD error
= GetLastError();
74 if (error
!= NO_ERROR
)
77 WCHAR szTitle
[MAX_STRING_LEN
];
79 LoadStringW(Globals
.hInstance
, STRING_ERROR
, szTitle
, ARRAY_SIZE(szTitle
));
81 FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
82 NULL
, error
, 0, (LPWSTR
)&lpMsgBuf
, 0, NULL
);
83 MessageBoxW(NULL
, lpMsgBuf
, szTitle
, MB_OK
| MB_ICONERROR
);
89 * Sets the caption of the main window according to Globals.szFileTitle:
90 * Untitled - Notepad if no file is open
91 * filename - Notepad if a file is given
93 void UpdateWindowCaption(void)
95 WCHAR szCaption
[MAX_STRING_LEN
];
96 WCHAR szNotepad
[MAX_STRING_LEN
];
97 static const WCHAR hyphenW
[] = { ' ','-',' ',0 };
99 if (Globals
.szFileTitle
[0] != '\0')
100 lstrcpyW(szCaption
, Globals
.szFileTitle
);
102 LoadStringW(Globals
.hInstance
, STRING_UNTITLED
, szCaption
, ARRAY_SIZE(szCaption
));
104 LoadStringW(Globals
.hInstance
, STRING_NOTEPAD
, szNotepad
, ARRAY_SIZE(szNotepad
));
105 lstrcatW(szCaption
, hyphenW
);
106 lstrcatW(szCaption
, szNotepad
);
108 SetWindowTextW(Globals
.hMainWnd
, szCaption
);
111 int DIALOG_StringMsgBox(HWND hParent
, int formatId
, LPCWSTR szString
, DWORD dwFlags
)
113 WCHAR szMessage
[MAX_STRING_LEN
];
114 WCHAR szResource
[MAX_STRING_LEN
];
116 /* Load and format szMessage */
117 LoadStringW(Globals
.hInstance
, formatId
, szResource
, ARRAY_SIZE(szResource
));
118 wnsprintfW(szMessage
, ARRAY_SIZE(szMessage
), szResource
, szString
);
121 if ((dwFlags
& MB_ICONMASK
) == MB_ICONEXCLAMATION
)
122 LoadStringW(Globals
.hInstance
, STRING_ERROR
, szResource
, ARRAY_SIZE(szResource
));
124 LoadStringW(Globals
.hInstance
, STRING_NOTEPAD
, szResource
, ARRAY_SIZE(szResource
));
126 /* Display Modal Dialog */
128 hParent
= Globals
.hMainWnd
;
129 return MessageBoxW(hParent
, szMessage
, szResource
, dwFlags
);
132 static void AlertFileNotFound(LPCWSTR szFileName
)
134 DIALOG_StringMsgBox(NULL
, STRING_NOTFOUND
, szFileName
, MB_ICONEXCLAMATION
|MB_OK
);
137 static int AlertFileNotSaved(LPCWSTR szFileName
)
139 WCHAR szUntitled
[MAX_STRING_LEN
];
141 LoadStringW(Globals
.hInstance
, STRING_UNTITLED
, szUntitled
, ARRAY_SIZE(szUntitled
));
142 return DIALOG_StringMsgBox(NULL
, STRING_NOTSAVED
, szFileName
[0] ? szFileName
: szUntitled
,
143 MB_ICONQUESTION
|MB_YESNOCANCEL
);
146 static int AlertUnicodeCharactersLost(LPCWSTR szFileName
)
148 WCHAR szMsgFormat
[MAX_STRING_LEN
];
149 WCHAR szEnc
[MAX_STRING_LEN
];
150 WCHAR szMsg
[ARRAY_SIZE(szMsgFormat
) + MAX_PATH
+ ARRAY_SIZE(szEnc
)];
151 WCHAR szCaption
[MAX_STRING_LEN
];
153 LoadStringW(Globals
.hInstance
, STRING_LOSS_OF_UNICODE_CHARACTERS
,
154 szMsgFormat
, ARRAY_SIZE(szMsgFormat
));
155 load_encoding_name(ENCODING_ANSI
, szEnc
, ARRAY_SIZE(szEnc
));
156 wnsprintfW(szMsg
, ARRAY_SIZE(szMsg
), szMsgFormat
, szFileName
, szEnc
);
157 LoadStringW(Globals
.hInstance
, STRING_NOTEPAD
, szCaption
,
158 ARRAY_SIZE(szCaption
));
159 return MessageBoxW(Globals
.hMainWnd
, szMsg
, szCaption
,
160 MB_OKCANCEL
|MB_ICONEXCLAMATION
);
165 * TRUE - if file exists
166 * FALSE - if file does not exist
168 BOOL
FileExists(LPCWSTR szFilename
)
170 WIN32_FIND_DATAW entry
;
173 hFile
= FindFirstFileW(szFilename
, &entry
);
176 return (hFile
!= INVALID_HANDLE_VALUE
);
179 static inline BOOL
is_conversion_to_ansi_lossy(LPCWSTR textW
, int lenW
)
182 WideCharToMultiByte(CP_ACP
, WC_NO_BEST_FIT_CHARS
, textW
, lenW
, NULL
, 0,
194 /* szFileName is the filename to save under; enc is the encoding to use.
196 * If the function succeeds, it returns SAVED_OK.
197 * If the function fails, it returns SAVE_FAILED.
198 * If Unicode data could be lost due to conversion to a non-Unicode character
199 * set, a warning is displayed. The user can continue (and the function carries
200 * on), or cancel (and the function returns SHOW_SAVEAS_DIALOG).
202 static SAVE_STATUS
DoSaveFile(LPCWSTR szFileName
, ENCODING enc
)
211 /* lenW includes the byte-order mark, but not the \0. */
212 lenW
= GetWindowTextLengthW(Globals
.hEdit
) + 1;
213 textW
= HeapAlloc(GetProcessHeap(), 0, (lenW
+1) * sizeof(WCHAR
));
219 textW
[0] = (WCHAR
) 0xfeff;
220 lenW
= GetWindowTextW(Globals
.hEdit
, textW
+1, lenW
) + 1;
224 case ENCODING_UTF16BE
:
225 byteswap_wide_string(textW
, lenW
);
228 case ENCODING_UTF16LE
:
229 size
= lenW
* sizeof(WCHAR
);
234 size
= WideCharToMultiByte(CP_UTF8
, 0, textW
, lenW
, NULL
, 0, NULL
, NULL
);
235 pBytes
= HeapAlloc(GetProcessHeap(), 0, size
);
239 HeapFree(GetProcessHeap(), 0, textW
);
242 WideCharToMultiByte(CP_UTF8
, 0, textW
, lenW
, pBytes
, size
, NULL
, NULL
);
243 HeapFree(GetProcessHeap(), 0, textW
);
247 if (is_conversion_to_ansi_lossy(textW
+1, lenW
-1)
248 && AlertUnicodeCharactersLost(szFileName
) == IDCANCEL
)
250 HeapFree(GetProcessHeap(), 0, textW
);
251 return SHOW_SAVEAS_DIALOG
;
254 size
= WideCharToMultiByte(CP_ACP
, 0, textW
+1, lenW
-1, NULL
, 0, NULL
, NULL
);
255 pBytes
= HeapAlloc(GetProcessHeap(), 0, size
);
259 HeapFree(GetProcessHeap(), 0, textW
);
262 WideCharToMultiByte(CP_ACP
, 0, textW
+1, lenW
-1, pBytes
, size
, NULL
, NULL
);
263 HeapFree(GetProcessHeap(), 0, textW
);
267 hFile
= CreateFileW(szFileName
, GENERIC_WRITE
, FILE_SHARE_WRITE
,
268 NULL
, OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
269 if(hFile
== INVALID_HANDLE_VALUE
)
272 HeapFree(GetProcessHeap(), 0, pBytes
);
275 if (!WriteFile(hFile
, pBytes
, size
, &dwNumWrite
, NULL
))
279 HeapFree(GetProcessHeap(), 0, pBytes
);
284 HeapFree(GetProcessHeap(), 0, pBytes
);
286 SendMessageW(Globals
.hEdit
, EM_SETMODIFY
, FALSE
, 0);
292 * TRUE - User agreed to close (both save/don't save)
293 * FALSE - User cancelled close by selecting "Cancel"
295 BOOL
DoCloseFile(void)
298 static const WCHAR empty_strW
[] = { 0 };
300 nResult
=GetWindowTextLengthW(Globals
.hEdit
);
301 if (SendMessageW(Globals
.hEdit
, EM_GETMODIFY
, 0, 0) &&
302 (nResult
|| Globals
.szFileName
[0]))
304 /* prompt user to save changes */
305 nResult
= AlertFileNotSaved(Globals
.szFileName
);
307 case IDYES
: return DIALOG_FileSave();
311 case IDCANCEL
: return(FALSE
);
313 default: return(FALSE
);
317 SetFileNameAndEncoding(empty_strW
, ENCODING_ANSI
);
319 UpdateWindowCaption();
323 static inline ENCODING
detect_encoding_of_buffer(const void* buffer
, int size
)
325 static const char bom_utf8
[] = { 0xef, 0xbb, 0xbf };
326 if (size
>= sizeof(bom_utf8
) && !memcmp(buffer
, bom_utf8
, sizeof(bom_utf8
)))
327 return ENCODING_UTF8
;
330 int flags
= IS_TEXT_UNICODE_SIGNATURE
|
331 IS_TEXT_UNICODE_REVERSE_SIGNATURE
|
332 IS_TEXT_UNICODE_ODD_LENGTH
;
333 IsTextUnicode(buffer
, size
, &flags
);
334 if (flags
& IS_TEXT_UNICODE_SIGNATURE
)
335 return ENCODING_UTF16LE
;
336 else if (flags
& IS_TEXT_UNICODE_REVERSE_SIGNATURE
)
337 return ENCODING_UTF16BE
;
339 return ENCODING_ANSI
;
343 void DoOpenFile(LPCWSTR szFileName
, ENCODING enc
)
345 static const WCHAR dotlog
[] = { '.','L','O','G',0 };
355 /* Close any files and prompt to save changes */
359 hFile
= CreateFileW(szFileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
360 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
361 if(hFile
== INVALID_HANDLE_VALUE
)
363 AlertFileNotFound(szFileName
);
367 size
= GetFileSize(hFile
, NULL
);
368 if (size
== INVALID_FILE_SIZE
)
375 /* Extra memory for (WCHAR)'\0'-termination. */
376 pTemp
= HeapAlloc(GetProcessHeap(), 0, size
+2);
384 if (!ReadFile(hFile
, pTemp
, size
, &dwNumRead
, NULL
))
387 HeapFree(GetProcessHeap(), 0, pTemp
);
396 if (enc
== ENCODING_AUTO
)
397 enc
= detect_encoding_of_buffer(pTemp
, size
);
398 else if (size
>= 2 && (enc
==ENCODING_UTF16LE
|| enc
==ENCODING_UTF16BE
))
400 /* If UTF-16 (BE or LE) is selected, and there is a UTF-16 BOM,
401 * override the selection (like native Notepad).
403 if ((BYTE
)pTemp
[0] == 0xff && (BYTE
)pTemp
[1] == 0xfe)
404 enc
= ENCODING_UTF16LE
;
405 else if ((BYTE
)pTemp
[0] == 0xfe && (BYTE
)pTemp
[1] == 0xff)
406 enc
= ENCODING_UTF16BE
;
411 case ENCODING_UTF16BE
:
412 byteswap_wide_string((WCHAR
*) pTemp
, size
/sizeof(WCHAR
));
413 /* Forget whether the file is BE or LE, like native Notepad. */
414 enc
= ENCODING_UTF16LE
;
418 case ENCODING_UTF16LE
:
419 textW
= (LPWSTR
)pTemp
;
420 lenW
= size
/sizeof(WCHAR
);
425 int cp
= (enc
==ENCODING_UTF8
) ? CP_UTF8
: CP_ACP
;
426 lenW
= MultiByteToWideChar(cp
, 0, pTemp
, size
, NULL
, 0);
427 textW
= HeapAlloc(GetProcessHeap(), 0, (lenW
+1) * sizeof(WCHAR
));
431 HeapFree(GetProcessHeap(), 0, pTemp
);
434 MultiByteToWideChar(cp
, 0, pTemp
, size
, textW
, lenW
);
435 HeapFree(GetProcessHeap(), 0, pTemp
);
440 /* Replace '\0's with spaces. Other than creating a custom control that
441 * can deal with '\0' characters, it's the best that can be done.
443 for (i
= 0; i
< lenW
; i
++)
444 if (textW
[i
] == '\0')
448 if (lenW
>= 1 && textW
[0] == 0xfeff)
449 SetWindowTextW(Globals
.hEdit
, textW
+1);
451 SetWindowTextW(Globals
.hEdit
, textW
);
453 HeapFree(GetProcessHeap(), 0, textW
);
455 SendMessageW(Globals
.hEdit
, EM_SETMODIFY
, FALSE
, 0);
456 SendMessageW(Globals
.hEdit
, EM_EMPTYUNDOBUFFER
, 0, 0);
457 SetFocus(Globals
.hEdit
);
459 /* If the file starts with .LOG, add a time/date at the end and set cursor after */
460 if (GetWindowTextW(Globals
.hEdit
, log
, ARRAY_SIZE(log
)) && !lstrcmpW(log
, dotlog
))
462 static const WCHAR lfW
[] = { '\r','\n',0 };
463 SendMessageW(Globals
.hEdit
, EM_SETSEL
, GetWindowTextLengthW(Globals
.hEdit
), -1);
464 SendMessageW(Globals
.hEdit
, EM_REPLACESEL
, TRUE
, (LPARAM
)lfW
);
465 DIALOG_EditTimeDate();
466 SendMessageW(Globals
.hEdit
, EM_REPLACESEL
, TRUE
, (LPARAM
)lfW
);
469 SetFileNameAndEncoding(szFileName
, enc
);
470 UpdateWindowCaption();
473 VOID
DIALOG_FileNew(VOID
)
475 static const WCHAR empty_strW
[] = { 0 };
477 /* Close any files and prompt to save changes */
479 SetWindowTextW(Globals
.hEdit
, empty_strW
);
480 SendMessageW(Globals
.hEdit
, EM_EMPTYUNDOBUFFER
, 0, 0);
481 SetFocus(Globals
.hEdit
);
485 /* Used to detect encoding of files selected in Open dialog.
486 * Returns ENCODING_AUTO if file can't be read, etc.
488 static ENCODING
detect_encoding_of_file(LPCWSTR szFileName
)
491 HANDLE hFile
= CreateFileW(szFileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
492 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
493 if (hFile
== INVALID_HANDLE_VALUE
)
494 return ENCODING_AUTO
;
495 size
= GetFileSize(hFile
, NULL
);
496 if (size
== INVALID_FILE_SIZE
)
499 return ENCODING_AUTO
;
504 BYTE buffer
[MAX_STRING_LEN
];
505 if (!ReadFile(hFile
, buffer
, min(size
, sizeof(buffer
)), &dwNumRead
, NULL
))
508 return ENCODING_AUTO
;
511 return detect_encoding_of_buffer(buffer
, dwNumRead
);
515 static LPWSTR
dialog_print_to_file(HWND hMainWnd
)
518 static WCHAR file
[MAX_PATH
] = {'o','u','t','p','u','t','.','p','r','n',0};
519 static const WCHAR defExt
[] = {'p','r','n',0};
521 ZeroMemory(&ofn
, sizeof(ofn
));
523 ofn
.lStructSize
= sizeof(ofn
);
524 ofn
.Flags
= OFN_PATHMUSTEXIST
| OFN_HIDEREADONLY
| OFN_OVERWRITEPROMPT
;
525 ofn
.hwndOwner
= hMainWnd
;
526 ofn
.lpstrFile
= file
;
527 ofn
.nMaxFile
= MAX_PATH
;
528 ofn
.lpstrDefExt
= defExt
;
530 if(GetSaveFileNameW(&ofn
))
535 static UINT_PTR CALLBACK
OfnHookProc(HWND hdlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
537 static HWND hEncCombo
;
544 hEncCombo
= GetDlgItem(hdlg
, IDC_OFN_ENCCOMBO
);
545 for (enc
= MIN_ENCODING
; enc
<= MAX_ENCODING
; enc
++)
547 WCHAR szEnc
[MAX_STRING_LEN
];
548 load_encoding_name(enc
, szEnc
, ARRAY_SIZE(szEnc
));
549 SendMessageW(hEncCombo
, CB_ADDSTRING
, 0, (LPARAM
)szEnc
);
551 SendMessageW(hEncCombo
, CB_SETCURSEL
, (WPARAM
)Globals
.encOfnCombo
, 0);
556 if (LOWORD(wParam
) == IDC_OFN_ENCCOMBO
&&
557 HIWORD(wParam
) == CBN_SELCHANGE
)
559 int index
= SendMessageW(hEncCombo
, CB_GETCURSEL
, 0, 0);
560 Globals
.encOfnCombo
= index
==CB_ERR
? ENCODING_ANSI
: (ENCODING
)index
;
566 switch (((OFNOTIFYW
*)lParam
)->hdr
.code
)
569 if (Globals
.bOfnIsOpenDialog
)
571 /* Check the start of the selected file for a BOM. */
573 WCHAR szFileName
[MAX_PATH
];
574 SendMessageW(GetParent(hdlg
), CDM_GETFILEPATH
,
575 ARRAY_SIZE(szFileName
), (LPARAM
)szFileName
);
576 enc
= detect_encoding_of_file(szFileName
);
577 if (enc
!= ENCODING_AUTO
)
579 Globals
.encOfnCombo
= enc
;
580 SendMessageW(hEncCombo
, CB_SETCURSEL
, (WPARAM
)enc
, 0);
596 VOID
DIALOG_FileOpen(VOID
)
598 OPENFILENAMEW openfilename
;
599 WCHAR szPath
[MAX_PATH
];
600 WCHAR szDir
[MAX_PATH
];
601 static const WCHAR szDefaultExt
[] = { 't','x','t',0 };
602 static const WCHAR txt_files
[] = { '*','.','t','x','t',0 };
604 ZeroMemory(&openfilename
, sizeof(openfilename
));
606 GetCurrentDirectoryW(ARRAY_SIZE(szDir
), szDir
);
607 lstrcpyW(szPath
, txt_files
);
609 openfilename
.lStructSize
= sizeof(openfilename
);
610 openfilename
.hwndOwner
= Globals
.hMainWnd
;
611 openfilename
.hInstance
= Globals
.hInstance
;
612 openfilename
.lpstrFilter
= Globals
.szFilter
;
613 openfilename
.lpstrFile
= szPath
;
614 openfilename
.nMaxFile
= ARRAY_SIZE(szPath
);
615 openfilename
.lpstrInitialDir
= szDir
;
616 openfilename
.Flags
= OFN_ENABLETEMPLATE
| OFN_ENABLEHOOK
| OFN_EXPLORER
|
617 OFN_FILEMUSTEXIST
| OFN_PATHMUSTEXIST
|
618 OFN_HIDEREADONLY
| OFN_ENABLESIZING
;
619 openfilename
.lpfnHook
= OfnHookProc
;
620 openfilename
.lpTemplateName
= MAKEINTRESOURCEW(IDD_OFN_TEMPLATE
);
621 openfilename
.lpstrDefExt
= szDefaultExt
;
623 Globals
.encOfnCombo
= ENCODING_ANSI
;
624 Globals
.bOfnIsOpenDialog
= TRUE
;
626 if (GetOpenFileNameW(&openfilename
))
627 DoOpenFile(openfilename
.lpstrFile
, Globals
.encOfnCombo
);
630 /* Return FALSE to cancel close */
631 BOOL
DIALOG_FileSave(VOID
)
633 if (Globals
.szFileName
[0] == '\0')
634 return DIALOG_FileSaveAs();
637 switch (DoSaveFile(Globals
.szFileName
, Globals
.encFile
))
639 case SAVED_OK
: return TRUE
;
640 case SHOW_SAVEAS_DIALOG
: return DIALOG_FileSaveAs();
641 default: return FALSE
;
646 BOOL
DIALOG_FileSaveAs(VOID
)
648 OPENFILENAMEW saveas
;
649 WCHAR szPath
[MAX_PATH
];
650 WCHAR szDir
[MAX_PATH
];
651 static const WCHAR szDefaultExt
[] = { 't','x','t',0 };
652 static const WCHAR txt_files
[] = { '*','.','t','x','t',0 };
654 ZeroMemory(&saveas
, sizeof(saveas
));
656 GetCurrentDirectoryW(ARRAY_SIZE(szDir
), szDir
);
657 lstrcpyW(szPath
, txt_files
);
659 saveas
.lStructSize
= sizeof(OPENFILENAMEW
);
660 saveas
.hwndOwner
= Globals
.hMainWnd
;
661 saveas
.hInstance
= Globals
.hInstance
;
662 saveas
.lpstrFilter
= Globals
.szFilter
;
663 saveas
.lpstrFile
= szPath
;
664 saveas
.nMaxFile
= ARRAY_SIZE(szPath
);
665 saveas
.lpstrInitialDir
= szDir
;
666 saveas
.Flags
= OFN_ENABLETEMPLATE
| OFN_ENABLEHOOK
| OFN_EXPLORER
|
667 OFN_PATHMUSTEXIST
| OFN_OVERWRITEPROMPT
|
668 OFN_HIDEREADONLY
| OFN_ENABLESIZING
;
669 saveas
.lpfnHook
= OfnHookProc
;
670 saveas
.lpTemplateName
= MAKEINTRESOURCEW(IDD_OFN_TEMPLATE
);
671 saveas
.lpstrDefExt
= szDefaultExt
;
673 /* Preset encoding to what file was opened/saved last with. */
674 Globals
.encOfnCombo
= Globals
.encFile
;
675 Globals
.bOfnIsOpenDialog
= FALSE
;
678 if (!GetSaveFileNameW(&saveas
))
681 switch (DoSaveFile(szPath
, Globals
.encOfnCombo
))
684 SetFileNameAndEncoding(szPath
, Globals
.encOfnCombo
);
685 UpdateWindowCaption();
688 case SHOW_SAVEAS_DIALOG
:
701 } TEXTINFO
, *LPTEXTINFO
;
703 static int notepad_print_header(HDC hdc
, RECT
*rc
, BOOL dopage
, BOOL header
, int page
, LPWSTR text
)
709 /* Write the header or footer */
710 GetTextExtentPoint32W(hdc
, text
, lstrlenW(text
), &szMetric
);
712 ExtTextOutW(hdc
, (rc
->left
+ rc
->right
- szMetric
.cx
) / 2,
713 header
? rc
->top
: rc
->bottom
- szMetric
.cy
,
714 ETO_CLIPPED
, rc
, text
, lstrlenW(text
), NULL
);
720 static BOOL
notepad_print_page(HDC hdc
, RECT
*rc
, BOOL dopage
, int page
, LPTEXTINFO tInfo
)
728 if (StartPage(hdc
) <= 0)
730 static const WCHAR failedW
[] = { 'S','t','a','r','t','P','a','g','e',' ','f','a','i','l','e','d',0 };
731 static const WCHAR errorW
[] = { 'P','r','i','n','t',' ','E','r','r','o','r',0 };
732 MessageBoxW(Globals
.hMainWnd
, failedW
, errorW
, MB_ICONEXCLAMATION
);
737 GetTextMetricsW(hdc
, &tm
);
738 y
= rc
->top
+ notepad_print_header(hdc
, rc
, dopage
, TRUE
, page
, Globals
.szFileName
) * tm
.tmHeight
;
739 b
= rc
->bottom
- 2 * notepad_print_header(hdc
, rc
, FALSE
, FALSE
, page
, Globals
.szFooter
) * tm
.tmHeight
;
746 /* find the end of the line */
747 while (tInfo
->mptr
< tInfo
->mend
&& *tInfo
->mptr
!= '\n' && *tInfo
->mptr
!= '\r')
749 if (*tInfo
->mptr
== '\t')
751 /* replace tabs with spaces */
752 for (m
= 0; m
< SPACES_IN_TAB
; m
++)
754 if (tInfo
->len
< PRINT_LEN_MAX
)
755 tInfo
->lptr
[tInfo
->len
++] = ' ';
756 else if (Globals
.bWrapLongLines
)
760 else if (tInfo
->len
< PRINT_LEN_MAX
)
761 tInfo
->lptr
[tInfo
->len
++] = *tInfo
->mptr
;
763 if (tInfo
->len
>= PRINT_LEN_MAX
&& Globals
.bWrapLongLines
)
770 /* Find out how much we should print if line wrapping is enabled */
771 if (Globals
.bWrapLongLines
)
773 GetTextExtentExPointW(hdc
, tInfo
->lptr
, tInfo
->len
, rc
->right
- rc
->left
, &n
, NULL
, &szMetrics
);
774 if (n
< tInfo
->len
&& tInfo
->lptr
[n
] != ' ')
777 /* Don't wrap words unless it's a single word over the entire line */
778 while (m
&& tInfo
->lptr
[m
] != ' ') m
--;
779 if (m
> 0) n
= m
+ 1;
786 ExtTextOutW(hdc
, rc
->left
, y
, ETO_CLIPPED
, rc
, tInfo
->lptr
, n
, NULL
);
792 memcpy(tInfo
->lptr
, tInfo
->lptr
+ n
, tInfo
->len
* sizeof(WCHAR
));
793 y
+= tm
.tmHeight
+ tm
.tmExternalLeading
;
797 /* find the next line */
798 while (tInfo
->mptr
< tInfo
->mend
&& y
< b
&& (*tInfo
->mptr
== '\n' || *tInfo
->mptr
== '\r'))
800 if (*tInfo
->mptr
== '\n')
801 y
+= tm
.tmHeight
+ tm
.tmExternalLeading
;
805 } while (tInfo
->mptr
< tInfo
->mend
&& y
< b
);
807 notepad_print_header(hdc
, rc
, dopage
, FALSE
, page
, Globals
.szFooter
);
815 VOID
DIALOG_FilePrint(VOID
)
819 int page
, dopage
, copy
;
821 HFONT hTextFont
, old_font
= 0;
827 WCHAR cTemp
[PRINT_LEN_MAX
];
829 /* Get Current Settings */
830 ZeroMemory(&printer
, sizeof(printer
));
831 printer
.lStructSize
= sizeof(printer
);
832 printer
.hwndOwner
= Globals
.hMainWnd
;
833 printer
.hDevMode
= Globals
.hDevMode
;
834 printer
.hDevNames
= Globals
.hDevNames
;
835 printer
.hInstance
= Globals
.hInstance
;
837 /* Set some default flags */
838 printer
.Flags
= PD_RETURNDC
| PD_NOSELECTION
;
839 printer
.nFromPage
= 0;
840 printer
.nMinPage
= 1;
841 /* we really need to calculate number of pages to set nMaxPage and nToPage */
843 printer
.nMaxPage
= -1;
844 /* Let commdlg manage copy settings */
845 printer
.nCopies
= (WORD
)PD_USEDEVMODECOPIES
;
847 if (!PrintDlgW(&printer
)) return;
849 Globals
.hDevMode
= printer
.hDevMode
;
850 Globals
.hDevNames
= printer
.hDevNames
;
852 SetMapMode(printer
.hDC
, MM_TEXT
);
854 /* initialize DOCINFO */
855 di
.cbSize
= sizeof(DOCINFOW
);
856 di
.lpszDocName
= Globals
.szFileTitle
;
857 di
.lpszOutput
= NULL
;
858 di
.lpszDatatype
= NULL
;
861 if(printer
.Flags
& PD_PRINTTOFILE
)
863 di
.lpszOutput
= dialog_print_to_file(printer
.hwndOwner
);
868 /* Get the file text */
869 size
= GetWindowTextLengthW(Globals
.hEdit
) + 1;
870 pTemp
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
));
873 DeleteDC(printer
.hDC
);
877 size
= GetWindowTextW(Globals
.hEdit
, pTemp
, size
);
879 if (StartDocW(printer
.hDC
, &di
) > 0)
881 /* Get the page margins in pixels. */
882 rc
.top
= MulDiv(Globals
.iMarginTop
, GetDeviceCaps(printer
.hDC
, LOGPIXELSY
), 2540) -
883 GetDeviceCaps(printer
.hDC
, PHYSICALOFFSETY
);
884 rc
.bottom
= GetDeviceCaps(printer
.hDC
, PHYSICALHEIGHT
) -
885 MulDiv(Globals
.iMarginBottom
, GetDeviceCaps(printer
.hDC
, LOGPIXELSY
), 2540);
886 rc
.left
= MulDiv(Globals
.iMarginLeft
, GetDeviceCaps(printer
.hDC
, LOGPIXELSX
), 2540) -
887 GetDeviceCaps(printer
.hDC
, PHYSICALOFFSETX
);
888 rc
.right
= GetDeviceCaps(printer
.hDC
, PHYSICALWIDTH
) -
889 MulDiv(Globals
.iMarginRight
, GetDeviceCaps(printer
.hDC
, LOGPIXELSX
), 2540);
891 /* Create a font for the printer resolution */
892 lfFont
= Globals
.lfFont
;
893 lfFont
.lfHeight
= MulDiv(lfFont
.lfHeight
, GetDeviceCaps(printer
.hDC
, LOGPIXELSY
), get_dpi());
894 /* Make the font a bit lighter */
895 lfFont
.lfWeight
-= 100;
896 hTextFont
= CreateFontIndirectW(&lfFont
);
897 old_font
= SelectObject(printer
.hDC
, hTextFont
);
899 for (copy
= 1; copy
<= printer
.nCopies
; copy
++)
904 tInfo
.mend
= pTemp
+ size
;
909 if (printer
.Flags
& PD_PAGENUMS
)
911 /* a specific range of pages is selected, so
912 * skip pages that are not to be printed
914 if (page
> printer
.nToPage
)
916 else if (page
>= printer
.nFromPage
)
924 ret
= notepad_print_page(printer
.hDC
, &rc
, dopage
, page
, &tInfo
);
926 } while (ret
&& tInfo
.mptr
< tInfo
.mend
);
931 SelectObject(printer
.hDC
, old_font
);
932 DeleteObject(hTextFont
);
934 DeleteDC(printer
.hDC
);
935 HeapFree(GetProcessHeap(), 0, pTemp
);
938 VOID
DIALOG_FilePrinterSetup(VOID
)
942 ZeroMemory(&printer
, sizeof(printer
));
943 printer
.lStructSize
= sizeof(printer
);
944 printer
.hwndOwner
= Globals
.hMainWnd
;
945 printer
.hDevMode
= Globals
.hDevMode
;
946 printer
.hDevNames
= Globals
.hDevNames
;
947 printer
.hInstance
= Globals
.hInstance
;
948 printer
.Flags
= PD_PRINTSETUP
;
953 Globals
.hDevMode
= printer
.hDevMode
;
954 Globals
.hDevNames
= printer
.hDevNames
;
957 VOID
DIALOG_FileExit(VOID
)
959 PostMessageW(Globals
.hMainWnd
, WM_CLOSE
, 0, 0l);
962 VOID
DIALOG_EditUndo(VOID
)
964 SendMessageW(Globals
.hEdit
, EM_UNDO
, 0, 0);
967 VOID
DIALOG_EditCut(VOID
)
969 SendMessageW(Globals
.hEdit
, WM_CUT
, 0, 0);
972 VOID
DIALOG_EditCopy(VOID
)
974 SendMessageW(Globals
.hEdit
, WM_COPY
, 0, 0);
977 VOID
DIALOG_EditPaste(VOID
)
979 SendMessageW(Globals
.hEdit
, WM_PASTE
, 0, 0);
982 VOID
DIALOG_EditDelete(VOID
)
984 SendMessageW(Globals
.hEdit
, WM_CLEAR
, 0, 0);
987 VOID
DIALOG_EditSelectAll(VOID
)
989 SendMessageW(Globals
.hEdit
, EM_SETSEL
, 0, -1);
992 VOID
DIALOG_EditTimeDate(VOID
)
995 WCHAR szDate
[MAX_STRING_LEN
];
996 static const WCHAR spaceW
[] = { ' ',0 };
1000 GetTimeFormatW(LOCALE_USER_DEFAULT
, TIME_NOSECONDS
, &st
, NULL
, szDate
, MAX_STRING_LEN
);
1001 SendMessageW(Globals
.hEdit
, EM_REPLACESEL
, TRUE
, (LPARAM
)szDate
);
1003 SendMessageW(Globals
.hEdit
, EM_REPLACESEL
, TRUE
, (LPARAM
)spaceW
);
1005 GetDateFormatW(LOCALE_USER_DEFAULT
, 0, &st
, NULL
, szDate
, MAX_STRING_LEN
);
1006 SendMessageW(Globals
.hEdit
, EM_REPLACESEL
, TRUE
, (LPARAM
)szDate
);
1009 VOID
DIALOG_EditWrap(VOID
)
1011 BOOL modify
= FALSE
;
1012 static const WCHAR editW
[] = { 'e','d','i','t',0 };
1013 DWORD dwStyle
= WS_CHILD
| WS_VISIBLE
| WS_BORDER
| WS_VSCROLL
|
1014 ES_AUTOVSCROLL
| ES_MULTILINE
;
1019 size
= GetWindowTextLengthW(Globals
.hEdit
) + 1;
1020 pTemp
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
));
1026 GetWindowTextW(Globals
.hEdit
, pTemp
, size
);
1027 modify
= SendMessageW(Globals
.hEdit
, EM_GETMODIFY
, 0, 0);
1028 DestroyWindow(Globals
.hEdit
);
1029 GetClientRect(Globals
.hMainWnd
, &rc
);
1030 if( Globals
.bWrapLongLines
) dwStyle
|= WS_HSCROLL
| ES_AUTOHSCROLL
;
1031 Globals
.hEdit
= CreateWindowExW(WS_EX_CLIENTEDGE
, editW
, NULL
, dwStyle
,
1032 0, 0, rc
.right
, rc
.bottom
, Globals
.hMainWnd
,
1033 NULL
, Globals
.hInstance
, NULL
);
1034 SendMessageW(Globals
.hEdit
, WM_SETFONT
, (WPARAM
)Globals
.hFont
, FALSE
);
1035 SetWindowTextW(Globals
.hEdit
, pTemp
);
1036 SendMessageW(Globals
.hEdit
, EM_SETMODIFY
, modify
, 0);
1037 SetFocus(Globals
.hEdit
);
1038 HeapFree(GetProcessHeap(), 0, pTemp
);
1040 Globals
.bWrapLongLines
= !Globals
.bWrapLongLines
;
1041 CheckMenuItem(GetMenu(Globals
.hMainWnd
), CMD_WRAP
,
1042 MF_BYCOMMAND
| (Globals
.bWrapLongLines
? MF_CHECKED
: MF_UNCHECKED
));
1045 VOID
DIALOG_SelectFont(VOID
)
1048 LOGFONTW lf
=Globals
.lfFont
;
1050 ZeroMemory( &cf
, sizeof(cf
) );
1051 cf
.lStructSize
=sizeof(cf
);
1052 cf
.hwndOwner
=Globals
.hMainWnd
;
1054 cf
.Flags
=CF_SCREENFONTS
| CF_INITTOLOGFONTSTRUCT
;
1056 if( ChooseFontW(&cf
) )
1058 HFONT currfont
=Globals
.hFont
;
1060 Globals
.hFont
=CreateFontIndirectW( &lf
);
1062 SendMessageW( Globals
.hEdit
, WM_SETFONT
, (WPARAM
)Globals
.hFont
, TRUE
);
1063 if( currfont
!=NULL
)
1064 DeleteObject( currfont
);
1068 VOID
DIALOG_Search(VOID
)
1070 /* Allow only one search/replace dialog to open */
1071 if(Globals
.hFindReplaceDlg
!= NULL
)
1073 SetActiveWindow(Globals
.hFindReplaceDlg
);
1077 ZeroMemory(&Globals
.find
, sizeof(Globals
.find
));
1078 Globals
.find
.lStructSize
= sizeof(Globals
.find
);
1079 Globals
.find
.hwndOwner
= Globals
.hMainWnd
;
1080 Globals
.find
.hInstance
= Globals
.hInstance
;
1081 Globals
.find
.lpstrFindWhat
= Globals
.szFindText
;
1082 Globals
.find
.wFindWhatLen
= ARRAY_SIZE(Globals
.szFindText
);
1083 Globals
.find
.Flags
= FR_DOWN
|FR_HIDEWHOLEWORD
;
1085 /* We only need to create the modal FindReplace dialog which will */
1086 /* notify us of incoming events using hMainWnd Window Messages */
1088 Globals
.hFindReplaceDlg
= FindTextW(&Globals
.find
);
1089 assert(Globals
.hFindReplaceDlg
!=0);
1092 VOID
DIALOG_SearchNext(VOID
)
1094 if (Globals
.lastFind
.lpstrFindWhat
== NULL
)
1096 else /* use the last find data */
1097 NOTEPAD_DoFind(&Globals
.lastFind
);
1100 VOID
DIALOG_Replace(VOID
)
1102 /* Allow only one search/replace dialog to open */
1103 if(Globals
.hFindReplaceDlg
!= NULL
)
1105 SetActiveWindow(Globals
.hFindReplaceDlg
);
1109 ZeroMemory(&Globals
.find
, sizeof(Globals
.find
));
1110 Globals
.find
.lStructSize
= sizeof(Globals
.find
);
1111 Globals
.find
.hwndOwner
= Globals
.hMainWnd
;
1112 Globals
.find
.hInstance
= Globals
.hInstance
;
1113 Globals
.find
.lpstrFindWhat
= Globals
.szFindText
;
1114 Globals
.find
.wFindWhatLen
= ARRAY_SIZE(Globals
.szFindText
);
1115 Globals
.find
.lpstrReplaceWith
= Globals
.szReplaceText
;
1116 Globals
.find
.wReplaceWithLen
= ARRAY_SIZE(Globals
.szReplaceText
);
1117 Globals
.find
.Flags
= FR_DOWN
|FR_HIDEWHOLEWORD
;
1119 /* We only need to create the modal FindReplace dialog which will */
1120 /* notify us of incoming events using hMainWnd Window Messages */
1122 Globals
.hFindReplaceDlg
= ReplaceTextW(&Globals
.find
);
1123 assert(Globals
.hFindReplaceDlg
!=0);
1126 VOID
DIALOG_HelpContents(VOID
)
1128 WinHelpW(Globals
.hMainWnd
, helpfileW
, HELP_INDEX
, 0);
1131 VOID
DIALOG_HelpSearch(VOID
)
1136 VOID
DIALOG_HelpHelp(VOID
)
1138 WinHelpW(Globals
.hMainWnd
, helpfileW
, HELP_HELPONHELP
, 0);
1141 VOID
DIALOG_HelpAboutNotepad(VOID
)
1143 static const WCHAR notepadW
[] = { 'W','i','n','e',' ','N','o','t','e','p','a','d',0 };
1144 WCHAR szNotepad
[MAX_STRING_LEN
];
1145 HICON icon
= LoadImageW(Globals
.hInstance
, MAKEINTRESOURCEW(IDI_NOTEPAD
),
1146 IMAGE_ICON
, 48, 48, LR_SHARED
);
1148 LoadStringW(Globals
.hInstance
, STRING_NOTEPAD
, szNotepad
, ARRAY_SIZE(szNotepad
));
1149 ShellAboutW(Globals
.hMainWnd
, szNotepad
, notepadW
, icon
);
1153 /***********************************************************************
1155 * DIALOG_FilePageSetup
1157 VOID
DIALOG_FilePageSetup(void)
1159 DialogBoxW(Globals
.hInstance
, MAKEINTRESOURCEW(DIALOG_PAGESETUP
),
1160 Globals
.hMainWnd
, DIALOG_PAGESETUP_DlgProc
);
1164 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1166 * DIALOG_PAGESETUP_DlgProc
1169 static INT_PTR WINAPI
DIALOG_PAGESETUP_DlgProc(HWND hDlg
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
1178 /* save user input and close dialog */
1179 GetDlgItemTextW(hDlg
, IDC_PAGESETUP_HEADERVALUE
, Globals
.szHeader
, ARRAY_SIZE(Globals
.szHeader
));
1180 GetDlgItemTextW(hDlg
, IDC_PAGESETUP_FOOTERVALUE
, Globals
.szFooter
, ARRAY_SIZE(Globals
.szFooter
));
1182 Globals
.iMarginTop
= GetDlgItemInt(hDlg
, IDC_PAGESETUP_TOPVALUE
, NULL
, FALSE
) * 100;
1183 Globals
.iMarginBottom
= GetDlgItemInt(hDlg
, IDC_PAGESETUP_BOTTOMVALUE
, NULL
, FALSE
) * 100;
1184 Globals
.iMarginLeft
= GetDlgItemInt(hDlg
, IDC_PAGESETUP_LEFTVALUE
, NULL
, FALSE
) * 100;
1185 Globals
.iMarginRight
= GetDlgItemInt(hDlg
, IDC_PAGESETUP_RIGHTVALUE
, NULL
, FALSE
) * 100;
1186 EndDialog(hDlg
, IDOK
);
1190 /* discard user input and close dialog */
1191 EndDialog(hDlg
, IDCANCEL
);
1196 /* FIXME: Bring this to work */
1197 static const WCHAR sorryW
[] = { 'S','o','r','r','y',',',' ','n','o',' ','h','e','l','p',' ','a','v','a','i','l','a','b','l','e',0 };
1198 static const WCHAR helpW
[] = { 'H','e','l','p',0 };
1199 MessageBoxW(Globals
.hMainWnd
, sorryW
, helpW
, MB_ICONEXCLAMATION
);
1209 /* fetch last user input prior to display dialog */
1210 SetDlgItemTextW(hDlg
, IDC_PAGESETUP_HEADERVALUE
, Globals
.szHeader
);
1211 SetDlgItemTextW(hDlg
, IDC_PAGESETUP_FOOTERVALUE
, Globals
.szFooter
);
1212 SetDlgItemInt(hDlg
, IDC_PAGESETUP_TOPVALUE
, Globals
.iMarginTop
/ 100, FALSE
);
1213 SetDlgItemInt(hDlg
, IDC_PAGESETUP_BOTTOMVALUE
, Globals
.iMarginBottom
/ 100, FALSE
);
1214 SetDlgItemInt(hDlg
, IDC_PAGESETUP_LEFTVALUE
, Globals
.iMarginLeft
/ 100, FALSE
);
1215 SetDlgItemInt(hDlg
, IDC_PAGESETUP_RIGHTVALUE
, Globals
.iMarginRight
/ 100, FALSE
);