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 if (SendMessageW(Globals.hEdit, EM_GETMODIFY, 0, 0))
302 /* prompt user to save changes */
303 nResult = AlertFileNotSaved(Globals.szFileName);
305 case IDYES: return DIALOG_FileSave();
309 case IDCANCEL: return(FALSE);
311 default: return(FALSE);
315 SetFileNameAndEncoding(empty_strW, ENCODING_ANSI);
317 UpdateWindowCaption();
321 static inline ENCODING detect_encoding_of_buffer(const void* buffer, int size)
323 static const char bom_utf8[] = { 0xef, 0xbb, 0xbf };
324 if (size >= sizeof(bom_utf8) && !memcmp(buffer, bom_utf8, sizeof(bom_utf8)))
325 return ENCODING_UTF8;
328 int flags = IS_TEXT_UNICODE_SIGNATURE |
329 IS_TEXT_UNICODE_REVERSE_SIGNATURE |
330 IS_TEXT_UNICODE_ODD_LENGTH;
331 IsTextUnicode(buffer, size, &flags);
332 if (flags & IS_TEXT_UNICODE_SIGNATURE)
333 return ENCODING_UTF16LE;
334 else if (flags & IS_TEXT_UNICODE_REVERSE_SIGNATURE)
335 return ENCODING_UTF16BE;
337 return ENCODING_ANSI;
341 void DoOpenFile(LPCWSTR szFileName, ENCODING enc)
343 static const WCHAR dotlog[] = { '.','L','O','G',0 };
353 /* Close any files and prompt to save changes */
357 hFile = CreateFileW(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
358 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
359 if(hFile == INVALID_HANDLE_VALUE)
361 AlertFileNotFound(szFileName);
365 size = GetFileSize(hFile, NULL);
366 if (size == INVALID_FILE_SIZE)
373 /* Extra memory for (WCHAR)'\0'-termination. */
374 pTemp = HeapAlloc(GetProcessHeap(), 0, size+2);
382 if (!ReadFile(hFile, pTemp, size, &dwNumRead, NULL))
385 HeapFree(GetProcessHeap(), 0, pTemp);
394 if (enc == ENCODING_AUTO)
395 enc = detect_encoding_of_buffer(pTemp, size);
396 else if (size >= 2 && (enc==ENCODING_UTF16LE || enc==ENCODING_UTF16BE))
398 /* If UTF-16 (BE or LE) is selected, and there is a UTF-16 BOM,
399 * override the selection (like native Notepad).
401 if ((BYTE)pTemp[0] == 0xff && (BYTE)pTemp[1] == 0xfe)
402 enc = ENCODING_UTF16LE;
403 else if ((BYTE)pTemp[0] == 0xfe && (BYTE)pTemp[1] == 0xff)
404 enc = ENCODING_UTF16BE;
409 case ENCODING_UTF16BE:
410 byteswap_wide_string((WCHAR*) pTemp, size/sizeof(WCHAR));
411 /* Forget whether the file is BE or LE, like native Notepad. */
412 enc = ENCODING_UTF16LE;
416 case ENCODING_UTF16LE:
417 textW = (LPWSTR)pTemp;
418 lenW = size/sizeof(WCHAR);
423 int cp = (enc==ENCODING_UTF8) ? CP_UTF8 : CP_ACP;
424 lenW = MultiByteToWideChar(cp, 0, pTemp, size, NULL, 0);
425 textW = HeapAlloc(GetProcessHeap(), 0, (lenW+1) * sizeof(WCHAR));
429 HeapFree(GetProcessHeap(), 0, pTemp);
432 MultiByteToWideChar(cp, 0, pTemp, size, textW, lenW);
433 HeapFree(GetProcessHeap(), 0, pTemp);
438 /* Replace '\0's with spaces. Other than creating a custom control that
439 * can deal with '\0' characters, it's the best that can be done.
441 for (i = 0; i < lenW; i++)
442 if (textW[i] == '\0')
446 if (lenW >= 1 && textW[0] == 0xfeff)
447 SetWindowTextW(Globals.hEdit, textW+1);
449 SetWindowTextW(Globals.hEdit, textW);
451 HeapFree(GetProcessHeap(), 0, textW);
453 SendMessageW(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
454 SendMessageW(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
455 SetFocus(Globals.hEdit);
457 /* If the file starts with .LOG, add a time/date at the end and set cursor after */
458 if (GetWindowTextW(Globals.hEdit, log, ARRAY_SIZE(log)) && !lstrcmpW(log, dotlog))
460 static const WCHAR lfW[] = { '\r','\n',0 };
461 SendMessageW(Globals.hEdit, EM_SETSEL, GetWindowTextLengthW(Globals.hEdit), -1);
462 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW);
463 DIALOG_EditTimeDate();
464 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW);
467 SetFileNameAndEncoding(szFileName, enc);
468 UpdateWindowCaption();
471 VOID DIALOG_FileNew(VOID)
473 static const WCHAR empty_strW[] = { 0 };
475 /* Close any files and prompt to save changes */
477 SetWindowTextW(Globals.hEdit, empty_strW);
478 SendMessageW(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
479 SetFocus(Globals.hEdit);
483 /* Used to detect encoding of files selected in Open dialog.
484 * Returns ENCODING_AUTO if file can't be read, etc.
486 static ENCODING detect_encoding_of_file(LPCWSTR szFileName)
489 HANDLE hFile = CreateFileW(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
490 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
491 if (hFile == INVALID_HANDLE_VALUE)
492 return ENCODING_AUTO;
493 size = GetFileSize(hFile, NULL);
494 if (size == INVALID_FILE_SIZE)
497 return ENCODING_AUTO;
502 BYTE buffer[MAX_STRING_LEN];
503 if (!ReadFile(hFile, buffer, min(size, sizeof(buffer)), &dwNumRead, NULL))
506 return ENCODING_AUTO;
509 return detect_encoding_of_buffer(buffer, dwNumRead);
513 static LPWSTR dialog_print_to_file(HWND hMainWnd)
516 static WCHAR file[MAX_PATH] = {'o','u','t','p','u','t','.','p','r','n',0};
517 static const WCHAR defExt[] = {'p','r','n',0};
519 ZeroMemory(&ofn, sizeof(ofn));
521 ofn.lStructSize = sizeof(ofn);
522 ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
523 ofn.hwndOwner = hMainWnd;
524 ofn.lpstrFile = file;
525 ofn.nMaxFile = MAX_PATH;
526 ofn.lpstrDefExt = defExt;
528 if(GetSaveFileNameW(&ofn))
533 static UINT_PTR CALLBACK OfnHookProc(HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
535 static HWND hEncCombo;
542 hEncCombo = GetDlgItem(hdlg, IDC_OFN_ENCCOMBO);
543 for (enc = MIN_ENCODING; enc <= MAX_ENCODING; enc++)
545 WCHAR szEnc[MAX_STRING_LEN];
546 load_encoding_name(enc, szEnc, ARRAY_SIZE(szEnc));
547 SendMessageW(hEncCombo, CB_ADDSTRING, 0, (LPARAM)szEnc);
549 SendMessageW(hEncCombo, CB_SETCURSEL, (WPARAM)Globals.encOfnCombo, 0);
554 if (LOWORD(wParam) == IDC_OFN_ENCCOMBO &&
555 HIWORD(wParam) == CBN_SELCHANGE)
557 int index = SendMessageW(hEncCombo, CB_GETCURSEL, 0, 0);
558 Globals.encOfnCombo = index==CB_ERR ? ENCODING_ANSI : (ENCODING)index;
564 switch (((OFNOTIFYW*)lParam)->hdr.code)
567 if (Globals.bOfnIsOpenDialog)
569 /* Check the start of the selected file for a BOM. */
571 WCHAR szFileName[MAX_PATH];
572 SendMessageW(GetParent(hdlg), CDM_GETFILEPATH,
573 ARRAY_SIZE(szFileName), (LPARAM)szFileName);
574 enc = detect_encoding_of_file(szFileName);
575 if (enc != ENCODING_AUTO)
577 Globals.encOfnCombo = enc;
578 SendMessageW(hEncCombo, CB_SETCURSEL, (WPARAM)enc, 0);
594 VOID DIALOG_FileOpen(VOID)
596 OPENFILENAMEW openfilename;
597 WCHAR szPath[MAX_PATH];
598 WCHAR szDir[MAX_PATH];
599 static const WCHAR szDefaultExt[] = { 't','x','t',0 };
600 static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
602 ZeroMemory(&openfilename, sizeof(openfilename));
604 GetCurrentDirectoryW(ARRAY_SIZE(szDir), szDir);
605 lstrcpyW(szPath, txt_files);
607 openfilename.lStructSize = sizeof(openfilename);
608 openfilename.hwndOwner = Globals.hMainWnd;
609 openfilename.hInstance = Globals.hInstance;
610 openfilename.lpstrFilter = Globals.szFilter;
611 openfilename.lpstrFile = szPath;
612 openfilename.nMaxFile = ARRAY_SIZE(szPath);
613 openfilename.lpstrInitialDir = szDir;
614 openfilename.Flags = OFN_ENABLETEMPLATE | OFN_ENABLEHOOK | OFN_EXPLORER |
615 OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST |
616 OFN_HIDEREADONLY | OFN_ENABLESIZING;
617 openfilename.lpfnHook = OfnHookProc;
618 openfilename.lpTemplateName = MAKEINTRESOURCEW(IDD_OFN_TEMPLATE);
619 openfilename.lpstrDefExt = szDefaultExt;
621 Globals.encOfnCombo = ENCODING_ANSI;
622 Globals.bOfnIsOpenDialog = TRUE;
624 if (GetOpenFileNameW(&openfilename))
625 DoOpenFile(openfilename.lpstrFile, Globals.encOfnCombo);
628 /* Return FALSE to cancel close */
629 BOOL DIALOG_FileSave(VOID)
631 if (Globals.szFileName[0] == '\0')
632 return DIALOG_FileSaveAs();
635 switch (DoSaveFile(Globals.szFileName, Globals.encFile))
637 case SAVED_OK: return TRUE;
638 case SHOW_SAVEAS_DIALOG: return DIALOG_FileSaveAs();
639 default: return FALSE;
644 BOOL DIALOG_FileSaveAs(VOID)
646 OPENFILENAMEW saveas;
647 WCHAR szPath[MAX_PATH];
648 WCHAR szDir[MAX_PATH];
649 static const WCHAR szDefaultExt[] = { 't','x','t',0 };
650 static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
652 ZeroMemory(&saveas, sizeof(saveas));
654 GetCurrentDirectoryW(ARRAY_SIZE(szDir), szDir);
655 lstrcpyW(szPath, txt_files);
657 saveas.lStructSize = sizeof(OPENFILENAMEW);
658 saveas.hwndOwner = Globals.hMainWnd;
659 saveas.hInstance = Globals.hInstance;
660 saveas.lpstrFilter = Globals.szFilter;
661 saveas.lpstrFile = szPath;
662 saveas.nMaxFile = ARRAY_SIZE(szPath);
663 saveas.lpstrInitialDir = szDir;
664 saveas.Flags = OFN_ENABLETEMPLATE | OFN_ENABLEHOOK | OFN_EXPLORER |
665 OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT |
666 OFN_HIDEREADONLY | OFN_ENABLESIZING;
667 saveas.lpfnHook = OfnHookProc;
668 saveas.lpTemplateName = MAKEINTRESOURCEW(IDD_OFN_TEMPLATE);
669 saveas.lpstrDefExt = szDefaultExt;
671 /* Preset encoding to what file was opened/saved last with. */
672 Globals.encOfnCombo = Globals.encFile;
673 Globals.bOfnIsOpenDialog = FALSE;
676 if (!GetSaveFileNameW(&saveas))
679 switch (DoSaveFile(szPath, Globals.encOfnCombo))
682 SetFileNameAndEncoding(szPath, Globals.encOfnCombo);
683 UpdateWindowCaption();
686 case SHOW_SAVEAS_DIALOG:
699 } TEXTINFO, *LPTEXTINFO;
701 static int notepad_print_header(HDC hdc, RECT *rc, BOOL dopage, BOOL header, int page, LPWSTR text)
707 /* Write the header or footer */
708 GetTextExtentPoint32W(hdc, text, lstrlenW(text), &szMetric);
710 ExtTextOutW(hdc, (rc->left + rc->right - szMetric.cx) / 2,
711 header ? rc->top : rc->bottom - szMetric.cy,
712 ETO_CLIPPED, rc, text, lstrlenW(text), NULL);
718 static BOOL notepad_print_page(HDC hdc, RECT *rc, BOOL dopage, int page, LPTEXTINFO tInfo)
726 if (StartPage(hdc) <= 0)
728 static const WCHAR failedW[] = { 'S','t','a','r','t','P','a','g','e',' ','f','a','i','l','e','d',0 };
729 static const WCHAR errorW[] = { 'P','r','i','n','t',' ','E','r','r','o','r',0 };
730 MessageBoxW(Globals.hMainWnd, failedW, errorW, MB_ICONEXCLAMATION);
735 GetTextMetricsW(hdc, &tm);
736 y = rc->top + notepad_print_header(hdc, rc, dopage, TRUE, page, Globals.szFileName) * tm.tmHeight;
737 b = rc->bottom - 2 * notepad_print_header(hdc, rc, FALSE, FALSE, page, Globals.szFooter) * tm.tmHeight;
744 /* find the end of the line */
745 while (tInfo->mptr < tInfo->mend && *tInfo->mptr != '\n' && *tInfo->mptr != '\r')
747 if (*tInfo->mptr == '\t')
749 /* replace tabs with spaces */
750 for (m = 0; m < SPACES_IN_TAB; m++)
752 if (tInfo->len < PRINT_LEN_MAX)
753 tInfo->lptr[tInfo->len++] = ' ';
754 else if (Globals.bWrapLongLines)
758 else if (tInfo->len < PRINT_LEN_MAX)
759 tInfo->lptr[tInfo->len++] = *tInfo->mptr;
761 if (tInfo->len >= PRINT_LEN_MAX && Globals.bWrapLongLines)
768 /* Find out how much we should print if line wrapping is enabled */
769 if (Globals.bWrapLongLines)
771 GetTextExtentExPointW(hdc, tInfo->lptr, tInfo->len, rc->right - rc->left, &n, NULL, &szMetrics);
772 if (n < tInfo->len && tInfo->lptr[n] != ' ')
775 /* Don't wrap words unless it's a single word over the entire line */
776 while (m && tInfo->lptr[m] != ' ') m--;
777 if (m > 0) n = m + 1;
784 ExtTextOutW(hdc, rc->left, y, ETO_CLIPPED, rc, tInfo->lptr, n, NULL);
790 memcpy(tInfo->lptr, tInfo->lptr + n, tInfo->len * sizeof(WCHAR));
791 y += tm.tmHeight + tm.tmExternalLeading;
795 /* find the next line */
796 while (tInfo->mptr < tInfo->mend && y < b && (*tInfo->mptr == '\n' || *tInfo->mptr == '\r'))
798 if (*tInfo->mptr == '\n')
799 y += tm.tmHeight + tm.tmExternalLeading;
803 } while (tInfo->mptr < tInfo->mend && y < b);
805 notepad_print_header(hdc, rc, dopage, FALSE, page, Globals.szFooter);
813 VOID DIALOG_FilePrint(VOID)
817 int page, dopage, copy;
819 HFONT hTextFont, old_font = 0;
825 WCHAR cTemp[PRINT_LEN_MAX];
827 /* Get Current Settings */
828 ZeroMemory(&printer, sizeof(printer));
829 printer.lStructSize = sizeof(printer);
830 printer.hwndOwner = Globals.hMainWnd;
831 printer.hDevMode = Globals.hDevMode;
832 printer.hDevNames = Globals.hDevNames;
833 printer.hInstance = Globals.hInstance;
835 /* Set some default flags */
836 printer.Flags = PD_RETURNDC | PD_NOSELECTION;
837 printer.nFromPage = 0;
838 printer.nMinPage = 1;
839 /* we really need to calculate number of pages to set nMaxPage and nToPage */
841 printer.nMaxPage = -1;
842 /* Let commdlg manage copy settings */
843 printer.nCopies = (WORD)PD_USEDEVMODECOPIES;
845 if (!PrintDlgW(&printer)) return;
847 Globals.hDevMode = printer.hDevMode;
848 Globals.hDevNames = printer.hDevNames;
850 SetMapMode(printer.hDC, MM_TEXT);
852 /* initialize DOCINFO */
853 di.cbSize = sizeof(DOCINFOW);
854 di.lpszDocName = Globals.szFileTitle;
855 di.lpszOutput = NULL;
856 di.lpszDatatype = NULL;
859 if(printer.Flags & PD_PRINTTOFILE)
861 di.lpszOutput = dialog_print_to_file(printer.hwndOwner);
866 /* Get the file text */
867 size = GetWindowTextLengthW(Globals.hEdit) + 1;
868 pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
871 DeleteDC(printer.hDC);
875 size = GetWindowTextW(Globals.hEdit, pTemp, size);
877 if (StartDocW(printer.hDC, &di) > 0)
879 /* Get the page margins in pixels. */
880 rc.top = MulDiv(Globals.iMarginTop, GetDeviceCaps(printer.hDC, LOGPIXELSY), 2540) -
881 GetDeviceCaps(printer.hDC, PHYSICALOFFSETY);
882 rc.bottom = GetDeviceCaps(printer.hDC, PHYSICALHEIGHT) -
883 MulDiv(Globals.iMarginBottom, GetDeviceCaps(printer.hDC, LOGPIXELSY), 2540);
884 rc.left = MulDiv(Globals.iMarginLeft, GetDeviceCaps(printer.hDC, LOGPIXELSX), 2540) -
885 GetDeviceCaps(printer.hDC, PHYSICALOFFSETX);
886 rc.right = GetDeviceCaps(printer.hDC, PHYSICALWIDTH) -
887 MulDiv(Globals.iMarginRight, GetDeviceCaps(printer.hDC, LOGPIXELSX), 2540);
889 /* Create a font for the printer resolution */
890 lfFont = Globals.lfFont;
891 lfFont.lfHeight = MulDiv(lfFont.lfHeight, GetDeviceCaps(printer.hDC, LOGPIXELSY), get_dpi());
892 /* Make the font a bit lighter */
893 lfFont.lfWeight -= 100;
894 hTextFont = CreateFontIndirectW(&lfFont);
895 old_font = SelectObject(printer.hDC, hTextFont);
897 for (copy = 1; copy <= printer.nCopies; copy++)
902 tInfo.mend = pTemp + size;
907 if (printer.Flags & PD_PAGENUMS)
909 /* a specific range of pages is selected, so
910 * skip pages that are not to be printed
912 if (page > printer.nToPage)
914 else if (page >= printer.nFromPage)
922 ret = notepad_print_page(printer.hDC, &rc, dopage, page, &tInfo);
924 } while (ret && tInfo.mptr < tInfo.mend);
929 SelectObject(printer.hDC, old_font);
930 DeleteObject(hTextFont);
932 DeleteDC(printer.hDC);
933 HeapFree(GetProcessHeap(), 0, pTemp);
936 VOID DIALOG_FilePrinterSetup(VOID)
940 ZeroMemory(&printer, sizeof(printer));
941 printer.lStructSize = sizeof(printer);
942 printer.hwndOwner = Globals.hMainWnd;
943 printer.hDevMode = Globals.hDevMode;
944 printer.hDevNames = Globals.hDevNames;
945 printer.hInstance = Globals.hInstance;
946 printer.Flags = PD_PRINTSETUP;
951 Globals.hDevMode = printer.hDevMode;
952 Globals.hDevNames = printer.hDevNames;
955 VOID DIALOG_FileExit(VOID)
957 PostMessageW(Globals.hMainWnd, WM_CLOSE, 0, 0l);
960 VOID DIALOG_EditUndo(VOID)
962 SendMessageW(Globals.hEdit, EM_UNDO, 0, 0);
965 VOID DIALOG_EditCut(VOID)
967 SendMessageW(Globals.hEdit, WM_CUT, 0, 0);
970 VOID DIALOG_EditCopy(VOID)
972 SendMessageW(Globals.hEdit, WM_COPY, 0, 0);
975 VOID DIALOG_EditPaste(VOID)
977 SendMessageW(Globals.hEdit, WM_PASTE, 0, 0);
980 VOID DIALOG_EditDelete(VOID)
982 SendMessageW(Globals.hEdit, WM_CLEAR, 0, 0);
985 VOID DIALOG_EditSelectAll(VOID)
987 SendMessageW(Globals.hEdit, EM_SETSEL, 0, -1);
990 VOID DIALOG_EditTimeDate(VOID)
993 WCHAR szDate[MAX_STRING_LEN];
994 static const WCHAR spaceW[] = { ' ',0 };
998 GetTimeFormatW(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &st, NULL, szDate, MAX_STRING_LEN);
999 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szDate);
1001 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)spaceW);
1003 GetDateFormatW(LOCALE_USER_DEFAULT, 0, &st, NULL, szDate, MAX_STRING_LEN);
1004 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szDate);
1007 VOID DIALOG_EditWrap(VOID)
1009 BOOL modify = FALSE;
1010 static const WCHAR editW[] = { 'e','d','i','t',0 };
1011 DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL |
1012 ES_AUTOVSCROLL | ES_MULTILINE;
1017 size = GetWindowTextLengthW(Globals.hEdit) + 1;
1018 pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
1024 GetWindowTextW(Globals.hEdit, pTemp, size);
1025 modify = SendMessageW(Globals.hEdit, EM_GETMODIFY, 0, 0);
1026 DestroyWindow(Globals.hEdit);
1027 GetClientRect(Globals.hMainWnd, &rc);
1028 if( Globals.bWrapLongLines ) dwStyle |= WS_HSCROLL | ES_AUTOHSCROLL;
1029 Globals.hEdit = CreateWindowExW(WS_EX_CLIENTEDGE, editW, NULL, dwStyle,
1030 0, 0, rc.right, rc.bottom, Globals.hMainWnd,
1031 NULL, Globals.hInstance, NULL);
1032 SendMessageW(Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, FALSE);
1033 SetWindowTextW(Globals.hEdit, pTemp);
1034 SendMessageW(Globals.hEdit, EM_SETMODIFY, modify, 0);
1035 SetFocus(Globals.hEdit);
1036 HeapFree(GetProcessHeap(), 0, pTemp);
1038 Globals.bWrapLongLines = !Globals.bWrapLongLines;
1039 CheckMenuItem(GetMenu(Globals.hMainWnd), CMD_WRAP,
1040 MF_BYCOMMAND | (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED));
1043 VOID DIALOG_SelectFont(VOID)
1046 LOGFONTW lf=Globals.lfFont;
1048 ZeroMemory( &cf, sizeof(cf) );
1049 cf.lStructSize=sizeof(cf);
1050 cf.hwndOwner=Globals.hMainWnd;
1052 cf.Flags=CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT;
1054 if( ChooseFontW(&cf) )
1056 HFONT currfont=Globals.hFont;
1058 Globals.hFont=CreateFontIndirectW( &lf );
1060 SendMessageW( Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, TRUE );
1061 if( currfont!=NULL )
1062 DeleteObject( currfont );
1066 VOID DIALOG_Search(VOID)
1068 /* Allow only one search/replace dialog to open */
1069 if(Globals.hFindReplaceDlg != NULL)
1071 SetActiveWindow(Globals.hFindReplaceDlg);
1075 ZeroMemory(&Globals.find, sizeof(Globals.find));
1076 Globals.find.lStructSize = sizeof(Globals.find);
1077 Globals.find.hwndOwner = Globals.hMainWnd;
1078 Globals.find.hInstance = Globals.hInstance;
1079 Globals.find.lpstrFindWhat = Globals.szFindText;
1080 Globals.find.wFindWhatLen = ARRAY_SIZE(Globals.szFindText);
1081 Globals.find.Flags = FR_DOWN|FR_HIDEWHOLEWORD;
1083 /* We only need to create the modal FindReplace dialog which will */
1084 /* notify us of incoming events using hMainWnd Window Messages */
1086 Globals.hFindReplaceDlg = FindTextW(&Globals.find);
1087 assert(Globals.hFindReplaceDlg !=0);
1090 VOID DIALOG_SearchNext(VOID)
1092 if (Globals.lastFind.lpstrFindWhat == NULL)
1094 else /* use the last find data */
1095 NOTEPAD_DoFind(&Globals.lastFind);
1098 VOID DIALOG_Replace(VOID)
1100 /* Allow only one search/replace dialog to open */
1101 if(Globals.hFindReplaceDlg != NULL)
1103 SetActiveWindow(Globals.hFindReplaceDlg);
1107 ZeroMemory(&Globals.find, sizeof(Globals.find));
1108 Globals.find.lStructSize = sizeof(Globals.find);
1109 Globals.find.hwndOwner = Globals.hMainWnd;
1110 Globals.find.hInstance = Globals.hInstance;
1111 Globals.find.lpstrFindWhat = Globals.szFindText;
1112 Globals.find.wFindWhatLen = ARRAY_SIZE(Globals.szFindText);
1113 Globals.find.lpstrReplaceWith = Globals.szReplaceText;
1114 Globals.find.wReplaceWithLen = ARRAY_SIZE(Globals.szReplaceText);
1115 Globals.find.Flags = FR_DOWN|FR_HIDEWHOLEWORD;
1117 /* We only need to create the modal FindReplace dialog which will */
1118 /* notify us of incoming events using hMainWnd Window Messages */
1120 Globals.hFindReplaceDlg = ReplaceTextW(&Globals.find);
1121 assert(Globals.hFindReplaceDlg !=0);
1124 VOID DIALOG_HelpContents(VOID)
1126 WinHelpW(Globals.hMainWnd, helpfileW, HELP_INDEX, 0);
1129 VOID DIALOG_HelpSearch(VOID)
1134 VOID DIALOG_HelpHelp(VOID)
1136 WinHelpW(Globals.hMainWnd, helpfileW, HELP_HELPONHELP, 0);
1139 VOID DIALOG_HelpAboutNotepad(VOID)
1141 static const WCHAR notepadW[] = { 'W','i','n','e',' ','N','o','t','e','p','a','d',0 };
1142 WCHAR szNotepad[MAX_STRING_LEN];
1143 HICON icon = LoadImageW(Globals.hInstance, MAKEINTRESOURCEW(IDI_NOTEPAD),
1144 IMAGE_ICON, 48, 48, LR_SHARED);
1146 LoadStringW(Globals.hInstance, STRING_NOTEPAD, szNotepad, ARRAY_SIZE(szNotepad));
1147 ShellAboutW(Globals.hMainWnd, szNotepad, notepadW, icon);
1151 /***********************************************************************
1153 * DIALOG_FilePageSetup
1155 VOID DIALOG_FilePageSetup(void)
1157 DialogBoxW(Globals.hInstance, MAKEINTRESOURCEW(DIALOG_PAGESETUP),
1158 Globals.hMainWnd, DIALOG_PAGESETUP_DlgProc);
1162 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1164 * DIALOG_PAGESETUP_DlgProc
1167 static INT_PTR WINAPI DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
1176 /* save user input and close dialog */
1177 GetDlgItemTextW(hDlg, IDC_PAGESETUP_HEADERVALUE, Globals.szHeader, ARRAY_SIZE(Globals.szHeader));
1178 GetDlgItemTextW(hDlg, IDC_PAGESETUP_FOOTERVALUE, Globals.szFooter, ARRAY_SIZE(Globals.szFooter));
1180 Globals.iMarginTop = GetDlgItemInt(hDlg, IDC_PAGESETUP_TOPVALUE, NULL, FALSE) * 100;
1181 Globals.iMarginBottom = GetDlgItemInt(hDlg, IDC_PAGESETUP_BOTTOMVALUE, NULL, FALSE) * 100;
1182 Globals.iMarginLeft = GetDlgItemInt(hDlg, IDC_PAGESETUP_LEFTVALUE, NULL, FALSE) * 100;
1183 Globals.iMarginRight = GetDlgItemInt(hDlg, IDC_PAGESETUP_RIGHTVALUE, NULL, FALSE) * 100;
1184 EndDialog(hDlg, IDOK);
1188 /* discard user input and close dialog */
1189 EndDialog(hDlg, IDCANCEL);
1194 /* FIXME: Bring this to work */
1195 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 };
1196 static const WCHAR helpW[] = { 'H','e','l','p',0 };
1197 MessageBoxW(Globals.hMainWnd, sorryW, helpW, MB_ICONEXCLAMATION);
1207 /* fetch last user input prior to display dialog */
1208 SetDlgItemTextW(hDlg, IDC_PAGESETUP_HEADERVALUE, Globals.szHeader);
1209 SetDlgItemTextW(hDlg, IDC_PAGESETUP_FOOTERVALUE, Globals.szFooter);
1210 SetDlgItemInt(hDlg, IDC_PAGESETUP_TOPVALUE, Globals.iMarginTop / 100, FALSE);
1211 SetDlgItemInt(hDlg, IDC_PAGESETUP_BOTTOMVALUE, Globals.iMarginBottom / 100, FALSE);
1212 SetDlgItemInt(hDlg, IDC_PAGESETUP_LEFTVALUE, Globals.iMarginLeft / 100, FALSE);
1213 SetDlgItemInt(hDlg, IDC_PAGESETUP_RIGHTVALUE, Globals.iMarginRight / 100, FALSE);