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
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
34 #define SPACES_IN_TAB 8
35 #define PRINT_LEN_MAX 500
37 static const WCHAR helpfileW[] = { 'n','o','t','e','p','a','d','.','h','l','p',0 };
39 static INT_PTR WINAPI DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam);
41 /* Swap bytes of WCHAR buffer (big-endian <-> little-endian). */
42 static inline void byteswap_wide_string(LPWSTR str, UINT num)
45 for (i = 0; i < num; i++) str[i] = RtlUshortByteSwap(str[i]);
48 static void load_encoding_name(ENCODING enc, WCHAR* buffer, int length)
52 case ENCODING_UTF16LE:
53 LoadStringW(Globals.hInstance, STRING_UNICODE_LE, buffer, length);
56 case ENCODING_UTF16BE:
57 LoadStringW(Globals.hInstance, STRING_UNICODE_BE, buffer, length);
63 GetCPInfoExW((enc==ENCODING_UTF8) ? CP_UTF8 : CP_ACP, 0, &cpi);
64 lstrcpynW(buffer, cpi.CodePageName, length);
70 VOID ShowLastError(void)
72 DWORD error = GetLastError();
73 if (error != NO_ERROR)
76 WCHAR szTitle[MAX_STRING_LEN];
78 LoadStringW(Globals.hInstance, STRING_ERROR, szTitle, ARRAY_SIZE(szTitle));
80 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
81 NULL, error, 0, (LPWSTR)&lpMsgBuf, 0, NULL);
82 MessageBoxW(NULL, lpMsgBuf, szTitle, MB_OK | MB_ICONERROR);
88 * Sets the caption of the main window according to Globals.szFileTitle:
89 * Untitled - Notepad if no file is open
90 * filename - Notepad if a file is given
92 void UpdateWindowCaption(void)
94 WCHAR szCaption[MAX_STRING_LEN];
95 WCHAR szNotepad[MAX_STRING_LEN];
96 static const WCHAR hyphenW[] = { ' ','-',' ',0 };
98 if (Globals.szFileTitle[0] != '\0')
99 lstrcpyW(szCaption, Globals.szFileTitle);
101 LoadStringW(Globals.hInstance, STRING_UNTITLED, szCaption, ARRAY_SIZE(szCaption));
103 LoadStringW(Globals.hInstance, STRING_NOTEPAD, szNotepad, ARRAY_SIZE(szNotepad));
104 lstrcatW(szCaption, hyphenW);
105 lstrcatW(szCaption, szNotepad);
107 SetWindowTextW(Globals.hMainWnd, szCaption);
110 int DIALOG_StringMsgBox(HWND hParent, int formatId, LPCWSTR szString, DWORD dwFlags)
112 WCHAR szMessage[MAX_STRING_LEN];
113 WCHAR szResource[MAX_STRING_LEN];
115 /* Load and format szMessage */
116 LoadStringW(Globals.hInstance, formatId, szResource, ARRAY_SIZE(szResource));
117 wnsprintfW(szMessage, ARRAY_SIZE(szMessage), szResource, szString);
120 if ((dwFlags & MB_ICONMASK) == MB_ICONEXCLAMATION)
121 LoadStringW(Globals.hInstance, STRING_ERROR, szResource, ARRAY_SIZE(szResource));
123 LoadStringW(Globals.hInstance, STRING_NOTEPAD, szResource, ARRAY_SIZE(szResource));
125 /* Display Modal Dialog */
127 hParent = Globals.hMainWnd;
128 return MessageBoxW(hParent, szMessage, szResource, dwFlags);
131 static void AlertFileNotFound(LPCWSTR szFileName)
133 DIALOG_StringMsgBox(NULL, STRING_NOTFOUND, szFileName, MB_ICONEXCLAMATION|MB_OK);
136 static int AlertFileNotSaved(LPCWSTR szFileName)
138 WCHAR szUntitled[MAX_STRING_LEN];
140 LoadStringW(Globals.hInstance, STRING_UNTITLED, szUntitled, ARRAY_SIZE(szUntitled));
141 return DIALOG_StringMsgBox(NULL, STRING_NOTSAVED, szFileName[0] ? szFileName : szUntitled,
142 MB_ICONQUESTION|MB_YESNOCANCEL);
145 static int AlertUnicodeCharactersLost(LPCWSTR szFileName)
147 WCHAR szMsgFormat[MAX_STRING_LEN];
148 WCHAR szEnc[MAX_STRING_LEN];
149 WCHAR szMsg[ARRAY_SIZE(szMsgFormat) + MAX_PATH + ARRAY_SIZE(szEnc)];
150 WCHAR szCaption[MAX_STRING_LEN];
152 LoadStringW(Globals.hInstance, STRING_LOSS_OF_UNICODE_CHARACTERS,
153 szMsgFormat, ARRAY_SIZE(szMsgFormat));
154 load_encoding_name(ENCODING_ANSI, szEnc, ARRAY_SIZE(szEnc));
155 wnsprintfW(szMsg, ARRAY_SIZE(szMsg), szMsgFormat, szFileName, szEnc);
156 LoadStringW(Globals.hInstance, STRING_NOTEPAD, szCaption,
157 ARRAY_SIZE(szCaption));
158 return MessageBoxW(Globals.hMainWnd, szMsg, szCaption,
159 MB_OKCANCEL|MB_ICONEXCLAMATION);
164 * TRUE - if file exists
165 * FALSE - if file does not exist
167 BOOL FileExists(LPCWSTR szFilename)
169 WIN32_FIND_DATAW entry;
172 hFile = FindFirstFileW(szFilename, &entry);
175 return (hFile != INVALID_HANDLE_VALUE);
178 static inline BOOL is_conversion_to_ansi_lossy(LPCWSTR textW, int lenW)
181 WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, textW, lenW, NULL, 0,
193 /* szFileName is the filename to save under; enc is the encoding to use.
195 * If the function succeeds, it returns SAVED_OK.
196 * If the function fails, it returns SAVE_FAILED.
197 * If Unicode data could be lost due to conversion to a non-Unicode character
198 * set, a warning is displayed. The user can continue (and the function carries
199 * on), or cancel (and the function returns SHOW_SAVEAS_DIALOG).
201 static SAVE_STATUS DoSaveFile(LPCWSTR szFileName, ENCODING enc)
210 /* lenW includes the byte-order mark, but not the \0. */
211 lenW = GetWindowTextLengthW(Globals.hEdit) + 1;
212 textW = HeapAlloc(GetProcessHeap(), 0, (lenW+1) * sizeof(WCHAR));
218 textW[0] = (WCHAR) 0xfeff;
219 lenW = GetWindowTextW(Globals.hEdit, textW+1, lenW) + 1;
223 case ENCODING_UTF16BE:
224 byteswap_wide_string(textW, lenW);
227 case ENCODING_UTF16LE:
228 size = lenW * sizeof(WCHAR);
233 size = WideCharToMultiByte(CP_UTF8, 0, textW, lenW, NULL, 0, NULL, NULL);
234 pBytes = HeapAlloc(GetProcessHeap(), 0, size);
238 HeapFree(GetProcessHeap(), 0, textW);
241 WideCharToMultiByte(CP_UTF8, 0, textW, lenW, pBytes, size, NULL, NULL);
242 HeapFree(GetProcessHeap(), 0, textW);
246 if (is_conversion_to_ansi_lossy(textW+1, lenW-1)
247 && AlertUnicodeCharactersLost(szFileName) == IDCANCEL)
249 HeapFree(GetProcessHeap(), 0, textW);
250 return SHOW_SAVEAS_DIALOG;
253 size = WideCharToMultiByte(CP_ACP, 0, textW+1, lenW-1, NULL, 0, NULL, NULL);
254 pBytes = HeapAlloc(GetProcessHeap(), 0, size);
258 HeapFree(GetProcessHeap(), 0, textW);
261 WideCharToMultiByte(CP_ACP, 0, textW+1, lenW-1, pBytes, size, NULL, NULL);
262 HeapFree(GetProcessHeap(), 0, textW);
266 hFile = CreateFileW(szFileName, GENERIC_WRITE, FILE_SHARE_WRITE,
267 NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
268 if(hFile == INVALID_HANDLE_VALUE)
271 HeapFree(GetProcessHeap(), 0, pBytes);
274 if (!WriteFile(hFile, pBytes, size, &dwNumWrite, NULL))
278 HeapFree(GetProcessHeap(), 0, pBytes);
283 HeapFree(GetProcessHeap(), 0, pBytes);
285 SendMessageW(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
291 * TRUE - User agreed to close (both save/don't save)
292 * FALSE - User cancelled close by selecting "Cancel"
294 BOOL DoCloseFile(void)
297 static const WCHAR empty_strW[] = { 0 };
299 if (SendMessageW(Globals.hEdit, EM_GETMODIFY, 0, 0))
301 /* prompt user to save changes */
302 nResult = AlertFileNotSaved(Globals.szFileName);
304 case IDYES: return DIALOG_FileSave();
308 case IDCANCEL: return(FALSE);
310 default: return(FALSE);
314 SetFileNameAndEncoding(empty_strW, ENCODING_ANSI);
316 UpdateWindowCaption();
320 static inline ENCODING detect_encoding_of_buffer(const void* buffer, int size)
322 static const char bom_utf8[] = { 0xef, 0xbb, 0xbf };
323 if (size >= sizeof(bom_utf8) && !memcmp(buffer, bom_utf8, sizeof(bom_utf8)))
324 return ENCODING_UTF8;
327 int flags = IS_TEXT_UNICODE_SIGNATURE |
328 IS_TEXT_UNICODE_REVERSE_SIGNATURE |
329 IS_TEXT_UNICODE_ODD_LENGTH;
330 IsTextUnicode(buffer, size, &flags);
331 if (flags & IS_TEXT_UNICODE_SIGNATURE)
332 return ENCODING_UTF16LE;
333 else if (flags & IS_TEXT_UNICODE_REVERSE_SIGNATURE)
334 return ENCODING_UTF16BE;
336 return ENCODING_ANSI;
340 void DoOpenFile(LPCWSTR szFileName, ENCODING enc)
342 static const WCHAR dotlog[] = { '.','L','O','G',0 };
351 /* Close any files and prompt to save changes */
355 hFile = CreateFileW(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
356 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
357 if(hFile == INVALID_HANDLE_VALUE)
359 AlertFileNotFound(szFileName);
363 size = GetFileSize(hFile, NULL);
364 if (size == INVALID_FILE_SIZE)
371 /* Extra memory for (WCHAR)'\0'-termination. */
372 pTemp = HeapAlloc(GetProcessHeap(), 0, size+2);
380 if (!ReadFile(hFile, pTemp, size, &dwNumRead, NULL))
383 HeapFree(GetProcessHeap(), 0, pTemp);
392 if (enc == ENCODING_AUTO)
393 enc = detect_encoding_of_buffer(pTemp, size);
394 else if (size >= 2 && (enc==ENCODING_UTF16LE || enc==ENCODING_UTF16BE))
396 /* If UTF-16 (BE or LE) is selected, and there is a UTF-16 BOM,
397 * override the selection (like native Notepad).
399 if ((BYTE)pTemp[0] == 0xff && (BYTE)pTemp[1] == 0xfe)
400 enc = ENCODING_UTF16LE;
401 else if ((BYTE)pTemp[0] == 0xfe && (BYTE)pTemp[1] == 0xff)
402 enc = ENCODING_UTF16BE;
407 case ENCODING_UTF16BE:
408 byteswap_wide_string((WCHAR*) pTemp, size/sizeof(WCHAR));
409 /* Forget whether the file is BE or LE, like native Notepad. */
410 enc = ENCODING_UTF16LE;
414 case ENCODING_UTF16LE:
415 textW = (LPWSTR)pTemp;
416 lenW = size/sizeof(WCHAR);
421 int cp = (enc==ENCODING_UTF8) ? CP_UTF8 : CP_ACP;
422 lenW = MultiByteToWideChar(cp, 0, pTemp, size, NULL, 0);
423 textW = HeapAlloc(GetProcessHeap(), 0, (lenW+1) * sizeof(WCHAR));
427 HeapFree(GetProcessHeap(), 0, pTemp);
430 MultiByteToWideChar(cp, 0, pTemp, size, textW, lenW);
431 HeapFree(GetProcessHeap(), 0, pTemp);
438 if (lenW >= 1 && textW[0] == 0xfeff)
439 SetWindowTextW(Globals.hEdit, textW+1);
441 SetWindowTextW(Globals.hEdit, textW);
443 HeapFree(GetProcessHeap(), 0, textW);
445 SendMessageW(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
446 SendMessageW(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
447 SetFocus(Globals.hEdit);
449 /* If the file starts with .LOG, add a time/date at the end and set cursor after */
450 if (GetWindowTextW(Globals.hEdit, log, ARRAY_SIZE(log)) && !lstrcmpW(log, dotlog))
452 static const WCHAR lfW[] = { '\r','\n',0 };
453 SendMessageW(Globals.hEdit, EM_SETSEL, GetWindowTextLengthW(Globals.hEdit), -1);
454 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW);
455 DIALOG_EditTimeDate();
456 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW);
459 SetFileNameAndEncoding(szFileName, enc);
460 UpdateWindowCaption();
463 VOID DIALOG_FileNew(VOID)
465 static const WCHAR empty_strW[] = { 0 };
467 /* Close any files and prompt to save changes */
469 SetWindowTextW(Globals.hEdit, empty_strW);
470 SendMessageW(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
471 SetFocus(Globals.hEdit);
475 /* Used to detect encoding of files selected in Open dialog.
476 * Returns ENCODING_AUTO if file can't be read, etc.
478 static ENCODING detect_encoding_of_file(LPCWSTR szFileName)
481 HANDLE hFile = CreateFileW(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
482 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
483 if (hFile == INVALID_HANDLE_VALUE)
484 return ENCODING_AUTO;
485 size = GetFileSize(hFile, NULL);
486 if (size == INVALID_FILE_SIZE)
489 return ENCODING_AUTO;
494 BYTE buffer[MAX_STRING_LEN];
495 if (!ReadFile(hFile, buffer, min(size, sizeof(buffer)), &dwNumRead, NULL))
498 return ENCODING_AUTO;
501 return detect_encoding_of_buffer(buffer, dwNumRead);
505 static UINT_PTR CALLBACK OfnHookProc(HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
507 static HWND hEncCombo;
514 hEncCombo = GetDlgItem(hdlg, IDC_OFN_ENCCOMBO);
515 for (enc = MIN_ENCODING; enc <= MAX_ENCODING; enc++)
517 WCHAR szEnc[MAX_STRING_LEN];
518 load_encoding_name(enc, szEnc, ARRAY_SIZE(szEnc));
519 SendMessageW(hEncCombo, CB_ADDSTRING, 0, (LPARAM)szEnc);
521 SendMessageW(hEncCombo, CB_SETCURSEL, (WPARAM)Globals.encOfnCombo, 0);
526 if (LOWORD(wParam) == IDC_OFN_ENCCOMBO &&
527 HIWORD(wParam) == CBN_SELCHANGE)
529 int index = SendMessageW(hEncCombo, CB_GETCURSEL, 0, 0);
530 Globals.encOfnCombo = index==CB_ERR ? ENCODING_ANSI : (ENCODING)index;
536 switch (((OFNOTIFYW*)lParam)->hdr.code)
539 if (Globals.bOfnIsOpenDialog)
541 /* Check the start of the selected file for a BOM. */
543 WCHAR szFileName[MAX_PATH];
544 SendMessageW(GetParent(hdlg), CDM_GETFILEPATH,
545 ARRAY_SIZE(szFileName), (LPARAM)szFileName);
546 enc = detect_encoding_of_file(szFileName);
547 if (enc != ENCODING_AUTO)
549 Globals.encOfnCombo = enc;
550 SendMessageW(hEncCombo, CB_SETCURSEL, (WPARAM)enc, 0);
566 VOID DIALOG_FileOpen(VOID)
568 OPENFILENAMEW openfilename;
569 WCHAR szPath[MAX_PATH];
570 WCHAR szDir[MAX_PATH];
571 static const WCHAR szDefaultExt[] = { 't','x','t',0 };
572 static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
574 ZeroMemory(&openfilename, sizeof(openfilename));
576 GetCurrentDirectoryW(ARRAY_SIZE(szDir), szDir);
577 lstrcpyW(szPath, txt_files);
579 openfilename.lStructSize = sizeof(openfilename);
580 openfilename.hwndOwner = Globals.hMainWnd;
581 openfilename.hInstance = Globals.hInstance;
582 openfilename.lpstrFilter = Globals.szFilter;
583 openfilename.lpstrFile = szPath;
584 openfilename.nMaxFile = ARRAY_SIZE(szPath);
585 openfilename.lpstrInitialDir = szDir;
586 openfilename.Flags = OFN_ENABLETEMPLATE | OFN_ENABLEHOOK | OFN_EXPLORER |
587 OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST |
588 OFN_HIDEREADONLY | OFN_ENABLESIZING;
589 openfilename.lpfnHook = OfnHookProc;
590 openfilename.lpTemplateName = MAKEINTRESOURCEW(IDD_OFN_TEMPLATE);
591 openfilename.lpstrDefExt = szDefaultExt;
593 Globals.encOfnCombo = ENCODING_ANSI;
594 Globals.bOfnIsOpenDialog = TRUE;
596 if (GetOpenFileNameW(&openfilename))
597 DoOpenFile(openfilename.lpstrFile, Globals.encOfnCombo);
600 /* Return FALSE to cancel close */
601 BOOL DIALOG_FileSave(VOID)
603 if (Globals.szFileName[0] == '\0')
604 return DIALOG_FileSaveAs();
607 switch (DoSaveFile(Globals.szFileName, Globals.encFile))
609 case SAVED_OK: return TRUE;
610 case SHOW_SAVEAS_DIALOG: return DIALOG_FileSaveAs();
611 default: return FALSE;
616 BOOL DIALOG_FileSaveAs(VOID)
618 OPENFILENAMEW saveas;
619 WCHAR szPath[MAX_PATH];
620 WCHAR szDir[MAX_PATH];
621 static const WCHAR szDefaultExt[] = { 't','x','t',0 };
622 static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
624 ZeroMemory(&saveas, sizeof(saveas));
626 GetCurrentDirectoryW(ARRAY_SIZE(szDir), szDir);
627 lstrcpyW(szPath, txt_files);
629 saveas.lStructSize = sizeof(OPENFILENAMEW);
630 saveas.hwndOwner = Globals.hMainWnd;
631 saveas.hInstance = Globals.hInstance;
632 saveas.lpstrFilter = Globals.szFilter;
633 saveas.lpstrFile = szPath;
634 saveas.nMaxFile = ARRAY_SIZE(szPath);
635 saveas.lpstrInitialDir = szDir;
636 saveas.Flags = OFN_ENABLETEMPLATE | OFN_ENABLEHOOK | OFN_EXPLORER |
637 OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT |
638 OFN_HIDEREADONLY | OFN_ENABLESIZING;
639 saveas.lpfnHook = OfnHookProc;
640 saveas.lpTemplateName = MAKEINTRESOURCEW(IDD_OFN_TEMPLATE);
641 saveas.lpstrDefExt = szDefaultExt;
643 /* Preset encoding to what file was opened/saved last with. */
644 Globals.encOfnCombo = Globals.encFile;
645 Globals.bOfnIsOpenDialog = FALSE;
648 if (!GetSaveFileNameW(&saveas))
651 switch (DoSaveFile(szPath, Globals.encOfnCombo))
654 SetFileNameAndEncoding(szPath, Globals.encOfnCombo);
655 UpdateWindowCaption();
658 case SHOW_SAVEAS_DIALOG:
671 } TEXTINFO, *LPTEXTINFO;
673 static int notepad_print_header(HDC hdc, RECT *rc, BOOL dopage, BOOL header, int page, LPWSTR text)
679 /* Write the header or footer */
680 GetTextExtentPoint32W(hdc, text, lstrlenW(text), &szMetric);
682 ExtTextOutW(hdc, (rc->left + rc->right - szMetric.cx) / 2,
683 header ? rc->top : rc->bottom - szMetric.cy,
684 ETO_CLIPPED, rc, text, lstrlenW(text), NULL);
690 static BOOL notepad_print_page(HDC hdc, RECT *rc, BOOL dopage, int page, LPTEXTINFO tInfo)
698 if (StartPage(hdc) <= 0)
700 static const WCHAR failedW[] = { 'S','t','a','r','t','P','a','g','e',' ','f','a','i','l','e','d',0 };
701 static const WCHAR errorW[] = { 'P','r','i','n','t',' ','E','r','r','o','r',0 };
702 MessageBoxW(Globals.hMainWnd, failedW, errorW, MB_ICONEXCLAMATION);
707 GetTextMetricsW(hdc, &tm);
708 y = rc->top + notepad_print_header(hdc, rc, dopage, TRUE, page, Globals.szFileName) * tm.tmHeight;
709 b = rc->bottom - 2 * notepad_print_header(hdc, rc, FALSE, FALSE, page, Globals.szFooter) * tm.tmHeight;
716 /* find the end of the line */
717 while (tInfo->mptr < tInfo->mend && *tInfo->mptr != '\n' && *tInfo->mptr != '\r')
719 if (*tInfo->mptr == '\t')
721 /* replace tabs with spaces */
722 for (m = 0; m < SPACES_IN_TAB; m++)
724 if (tInfo->len < PRINT_LEN_MAX)
725 tInfo->lptr[tInfo->len++] = ' ';
726 else if (Globals.bWrapLongLines)
730 else if (tInfo->len < PRINT_LEN_MAX)
731 tInfo->lptr[tInfo->len++] = *tInfo->mptr;
733 if (tInfo->len >= PRINT_LEN_MAX && Globals.bWrapLongLines)
740 /* Find out how much we should print if line wrapping is enabled */
741 if (Globals.bWrapLongLines)
743 GetTextExtentExPointW(hdc, tInfo->lptr, tInfo->len, rc->right - rc->left, &n, NULL, &szMetrics);
744 if (n < tInfo->len && tInfo->lptr[n] != ' ')
747 /* Don't wrap words unless it's a single word over the entire line */
748 while (m && tInfo->lptr[m] != ' ') m--;
749 if (m > 0) n = m + 1;
756 ExtTextOutW(hdc, rc->left, y, ETO_CLIPPED, rc, tInfo->lptr, n, NULL);
762 memcpy(tInfo->lptr, tInfo->lptr + n, tInfo->len * sizeof(WCHAR));
763 y += tm.tmHeight + tm.tmExternalLeading;
767 /* find the next line */
768 while (tInfo->mptr < tInfo->mend && y < b && (*tInfo->mptr == '\n' || *tInfo->mptr == '\r'))
770 if (*tInfo->mptr == '\n')
771 y += tm.tmHeight + tm.tmExternalLeading;
775 } while (tInfo->mptr < tInfo->mend && y < b);
777 notepad_print_header(hdc, rc, dopage, FALSE, page, Globals.szFooter);
785 VOID DIALOG_FilePrint(VOID)
789 int page, dopage, copy;
791 HFONT hTextFont, old_font = 0;
797 WCHAR cTemp[PRINT_LEN_MAX];
799 /* Get Current Settings */
800 ZeroMemory(&printer, sizeof(printer));
801 printer.lStructSize = sizeof(printer);
802 printer.hwndOwner = Globals.hMainWnd;
803 printer.hDevMode = Globals.hDevMode;
804 printer.hDevNames = Globals.hDevNames;
805 printer.hInstance = Globals.hInstance;
807 /* Set some default flags */
808 printer.Flags = PD_RETURNDC | PD_NOSELECTION;
809 printer.nFromPage = 0;
810 printer.nMinPage = 1;
811 /* we really need to calculate number of pages to set nMaxPage and nToPage */
813 printer.nMaxPage = -1;
814 /* Let commdlg manage copy settings */
815 printer.nCopies = (WORD)PD_USEDEVMODECOPIES;
817 if (!PrintDlgW(&printer)) return;
819 Globals.hDevMode = printer.hDevMode;
820 Globals.hDevNames = printer.hDevNames;
822 SetMapMode(printer.hDC, MM_TEXT);
824 /* initialize DOCINFO */
825 di.cbSize = sizeof(DOCINFOW);
826 di.lpszDocName = Globals.szFileTitle;
827 di.lpszOutput = NULL;
828 di.lpszDatatype = NULL;
831 /* Get the file text */
832 size = GetWindowTextLengthW(Globals.hEdit) + 1;
833 pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
836 DeleteDC(printer.hDC);
840 size = GetWindowTextW(Globals.hEdit, pTemp, size);
842 if (StartDocW(printer.hDC, &di) > 0)
844 /* Get the page margins in pixels. */
845 rc.top = MulDiv(Globals.iMarginTop, GetDeviceCaps(printer.hDC, LOGPIXELSY), 2540) -
846 GetDeviceCaps(printer.hDC, PHYSICALOFFSETY);
847 rc.bottom = GetDeviceCaps(printer.hDC, PHYSICALHEIGHT) -
848 MulDiv(Globals.iMarginBottom, GetDeviceCaps(printer.hDC, LOGPIXELSY), 2540);
849 rc.left = MulDiv(Globals.iMarginLeft, GetDeviceCaps(printer.hDC, LOGPIXELSX), 2540) -
850 GetDeviceCaps(printer.hDC, PHYSICALOFFSETX);
851 rc.right = GetDeviceCaps(printer.hDC, PHYSICALWIDTH) -
852 MulDiv(Globals.iMarginRight, GetDeviceCaps(printer.hDC, LOGPIXELSX), 2540);
854 /* Create a font for the printer resolution */
855 lfFont = Globals.lfFont;
856 lfFont.lfHeight = MulDiv(lfFont.lfHeight, GetDeviceCaps(printer.hDC, LOGPIXELSY), get_dpi());
857 /* Make the font a bit lighter */
858 lfFont.lfWeight -= 100;
859 hTextFont = CreateFontIndirectW(&lfFont);
860 old_font = SelectObject(printer.hDC, hTextFont);
862 for (copy = 1; copy <= printer.nCopies; copy++)
867 tInfo.mend = pTemp + size;
872 if (printer.Flags & PD_PAGENUMS)
874 /* a specific range of pages is selected, so
875 * skip pages that are not to be printed
877 if (page > printer.nToPage)
879 else if (page >= printer.nFromPage)
887 ret = notepad_print_page(printer.hDC, &rc, dopage, page, &tInfo);
889 } while (ret && tInfo.mptr < tInfo.mend);
894 SelectObject(printer.hDC, old_font);
895 DeleteObject(hTextFont);
897 DeleteDC(printer.hDC);
898 HeapFree(GetProcessHeap(), 0, pTemp);
901 VOID DIALOG_FilePrinterSetup(VOID)
905 ZeroMemory(&printer, sizeof(printer));
906 printer.lStructSize = sizeof(printer);
907 printer.hwndOwner = Globals.hMainWnd;
908 printer.hDevMode = Globals.hDevMode;
909 printer.hDevNames = Globals.hDevNames;
910 printer.hInstance = Globals.hInstance;
911 printer.Flags = PD_PRINTSETUP;
916 Globals.hDevMode = printer.hDevMode;
917 Globals.hDevNames = printer.hDevNames;
920 VOID DIALOG_FileExit(VOID)
922 PostMessageW(Globals.hMainWnd, WM_CLOSE, 0, 0l);
925 VOID DIALOG_EditUndo(VOID)
927 SendMessageW(Globals.hEdit, EM_UNDO, 0, 0);
930 VOID DIALOG_EditCut(VOID)
932 SendMessageW(Globals.hEdit, WM_CUT, 0, 0);
935 VOID DIALOG_EditCopy(VOID)
937 SendMessageW(Globals.hEdit, WM_COPY, 0, 0);
940 VOID DIALOG_EditPaste(VOID)
942 SendMessageW(Globals.hEdit, WM_PASTE, 0, 0);
945 VOID DIALOG_EditDelete(VOID)
947 SendMessageW(Globals.hEdit, WM_CLEAR, 0, 0);
950 VOID DIALOG_EditSelectAll(VOID)
952 SendMessageW(Globals.hEdit, EM_SETSEL, 0, -1);
955 VOID DIALOG_EditTimeDate(VOID)
958 WCHAR szDate[MAX_STRING_LEN];
959 static const WCHAR spaceW[] = { ' ',0 };
963 GetTimeFormatW(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &st, NULL, szDate, MAX_STRING_LEN);
964 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szDate);
966 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)spaceW);
968 GetDateFormatW(LOCALE_USER_DEFAULT, 0, &st, NULL, szDate, MAX_STRING_LEN);
969 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szDate);
972 VOID DIALOG_EditWrap(VOID)
975 static const WCHAR editW[] = { 'e','d','i','t',0 };
976 DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL |
977 ES_AUTOVSCROLL | ES_MULTILINE;
982 size = GetWindowTextLengthW(Globals.hEdit) + 1;
983 pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
989 GetWindowTextW(Globals.hEdit, pTemp, size);
990 modify = SendMessageW(Globals.hEdit, EM_GETMODIFY, 0, 0);
991 DestroyWindow(Globals.hEdit);
992 GetClientRect(Globals.hMainWnd, &rc);
993 if( Globals.bWrapLongLines ) dwStyle |= WS_HSCROLL | ES_AUTOHSCROLL;
994 Globals.hEdit = CreateWindowExW(WS_EX_CLIENTEDGE, editW, NULL, dwStyle,
995 0, 0, rc.right, rc.bottom, Globals.hMainWnd,
996 NULL, Globals.hInstance, NULL);
997 SendMessageW(Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, FALSE);
998 SetWindowTextW(Globals.hEdit, pTemp);
999 SendMessageW(Globals.hEdit, EM_SETMODIFY, modify, 0);
1000 SetFocus(Globals.hEdit);
1001 HeapFree(GetProcessHeap(), 0, pTemp);
1003 Globals.bWrapLongLines = !Globals.bWrapLongLines;
1004 CheckMenuItem(GetMenu(Globals.hMainWnd), CMD_WRAP,
1005 MF_BYCOMMAND | (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED));
1008 VOID DIALOG_SelectFont(VOID)
1011 LOGFONTW lf=Globals.lfFont;
1013 ZeroMemory( &cf, sizeof(cf) );
1014 cf.lStructSize=sizeof(cf);
1015 cf.hwndOwner=Globals.hMainWnd;
1017 cf.Flags=CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT;
1019 if( ChooseFontW(&cf) )
1021 HFONT currfont=Globals.hFont;
1023 Globals.hFont=CreateFontIndirectW( &lf );
1025 SendMessageW( Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, TRUE );
1026 if( currfont!=NULL )
1027 DeleteObject( currfont );
1031 VOID DIALOG_Search(VOID)
1033 /* Allow only one search/replace dialog to open */
1034 if(Globals.hFindReplaceDlg != NULL)
1036 SetActiveWindow(Globals.hFindReplaceDlg);
1040 ZeroMemory(&Globals.find, sizeof(Globals.find));
1041 Globals.find.lStructSize = sizeof(Globals.find);
1042 Globals.find.hwndOwner = Globals.hMainWnd;
1043 Globals.find.hInstance = Globals.hInstance;
1044 Globals.find.lpstrFindWhat = Globals.szFindText;
1045 Globals.find.wFindWhatLen = ARRAY_SIZE(Globals.szFindText);
1046 Globals.find.Flags = FR_DOWN|FR_HIDEWHOLEWORD;
1048 /* We only need to create the modal FindReplace dialog which will */
1049 /* notify us of incoming events using hMainWnd Window Messages */
1051 Globals.hFindReplaceDlg = FindTextW(&Globals.find);
1052 assert(Globals.hFindReplaceDlg !=0);
1055 VOID DIALOG_SearchNext(VOID)
1057 if (Globals.lastFind.lpstrFindWhat == NULL)
1059 else /* use the last find data */
1060 NOTEPAD_DoFind(&Globals.lastFind);
1063 VOID DIALOG_Replace(VOID)
1065 /* Allow only one search/replace dialog to open */
1066 if(Globals.hFindReplaceDlg != NULL)
1068 SetActiveWindow(Globals.hFindReplaceDlg);
1072 ZeroMemory(&Globals.find, sizeof(Globals.find));
1073 Globals.find.lStructSize = sizeof(Globals.find);
1074 Globals.find.hwndOwner = Globals.hMainWnd;
1075 Globals.find.hInstance = Globals.hInstance;
1076 Globals.find.lpstrFindWhat = Globals.szFindText;
1077 Globals.find.wFindWhatLen = ARRAY_SIZE(Globals.szFindText);
1078 Globals.find.lpstrReplaceWith = Globals.szReplaceText;
1079 Globals.find.wReplaceWithLen = ARRAY_SIZE(Globals.szReplaceText);
1080 Globals.find.Flags = FR_DOWN|FR_HIDEWHOLEWORD;
1082 /* We only need to create the modal FindReplace dialog which will */
1083 /* notify us of incoming events using hMainWnd Window Messages */
1085 Globals.hFindReplaceDlg = ReplaceTextW(&Globals.find);
1086 assert(Globals.hFindReplaceDlg !=0);
1089 VOID DIALOG_HelpContents(VOID)
1091 WinHelpW(Globals.hMainWnd, helpfileW, HELP_INDEX, 0);
1094 VOID DIALOG_HelpSearch(VOID)
1099 VOID DIALOG_HelpHelp(VOID)
1101 WinHelpW(Globals.hMainWnd, helpfileW, HELP_HELPONHELP, 0);
1104 VOID DIALOG_HelpAboutNotepad(VOID)
1106 static const WCHAR notepadW[] = { 'W','i','n','e',' ','N','o','t','e','p','a','d',0 };
1107 WCHAR szNotepad[MAX_STRING_LEN];
1108 HICON icon = LoadImageW(Globals.hInstance, MAKEINTRESOURCEW(IDI_NOTEPAD),
1109 IMAGE_ICON, 48, 48, LR_SHARED);
1111 LoadStringW(Globals.hInstance, STRING_NOTEPAD, szNotepad, ARRAY_SIZE(szNotepad));
1112 ShellAboutW(Globals.hMainWnd, szNotepad, notepadW, icon);
1116 /***********************************************************************
1118 * DIALOG_FilePageSetup
1120 VOID DIALOG_FilePageSetup(void)
1122 DialogBoxW(Globals.hInstance, MAKEINTRESOURCEW(DIALOG_PAGESETUP),
1123 Globals.hMainWnd, DIALOG_PAGESETUP_DlgProc);
1127 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1129 * DIALOG_PAGESETUP_DlgProc
1132 static INT_PTR WINAPI DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
1141 /* save user input and close dialog */
1142 GetDlgItemTextW(hDlg, IDC_PAGESETUP_HEADERVALUE, Globals.szHeader, ARRAY_SIZE(Globals.szHeader));
1143 GetDlgItemTextW(hDlg, IDC_PAGESETUP_FOOTERVALUE, Globals.szFooter, ARRAY_SIZE(Globals.szFooter));
1145 Globals.iMarginTop = GetDlgItemInt(hDlg, IDC_PAGESETUP_TOPVALUE, NULL, FALSE) * 100;
1146 Globals.iMarginBottom = GetDlgItemInt(hDlg, IDC_PAGESETUP_BOTTOMVALUE, NULL, FALSE) * 100;
1147 Globals.iMarginLeft = GetDlgItemInt(hDlg, IDC_PAGESETUP_LEFTVALUE, NULL, FALSE) * 100;
1148 Globals.iMarginRight = GetDlgItemInt(hDlg, IDC_PAGESETUP_RIGHTVALUE, NULL, FALSE) * 100;
1149 EndDialog(hDlg, IDOK);
1153 /* discard user input and close dialog */
1154 EndDialog(hDlg, IDCANCEL);
1159 /* FIXME: Bring this to work */
1160 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 };
1161 static const WCHAR helpW[] = { 'H','e','l','p',0 };
1162 MessageBoxW(Globals.hMainWnd, sorryW, helpW, MB_ICONEXCLAMATION);
1172 /* fetch last user input prior to display dialog */
1173 SetDlgItemTextW(hDlg, IDC_PAGESETUP_HEADERVALUE, Globals.szHeader);
1174 SetDlgItemTextW(hDlg, IDC_PAGESETUP_FOOTERVALUE, Globals.szFooter);
1175 SetDlgItemInt(hDlg, IDC_PAGESETUP_TOPVALUE, Globals.iMarginTop / 100, FALSE);
1176 SetDlgItemInt(hDlg, IDC_PAGESETUP_BOTTOMVALUE, Globals.iMarginBottom / 100, FALSE);
1177 SetDlgItemInt(hDlg, IDC_PAGESETUP_LEFTVALUE, Globals.iMarginLeft / 100, FALSE);
1178 SetDlgItemInt(hDlg, IDC_PAGESETUP_RIGHTVALUE, Globals.iMarginRight / 100, FALSE);