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 };
352 /* Close any files and prompt to save changes */
356 hFile = CreateFileW(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
357 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
358 if(hFile == INVALID_HANDLE_VALUE)
360 AlertFileNotFound(szFileName);
364 size = GetFileSize(hFile, NULL);
365 if (size == INVALID_FILE_SIZE)
372 /* Extra memory for (WCHAR)'\0'-termination. */
373 pTemp = HeapAlloc(GetProcessHeap(), 0, size+2);
381 if (!ReadFile(hFile, pTemp, size, &dwNumRead, NULL))
384 HeapFree(GetProcessHeap(), 0, pTemp);
393 if (enc == ENCODING_AUTO)
394 enc = detect_encoding_of_buffer(pTemp, size);
395 else if (size >= 2 && (enc==ENCODING_UTF16LE || enc==ENCODING_UTF16BE))
397 /* If UTF-16 (BE or LE) is selected, and there is a UTF-16 BOM,
398 * override the selection (like native Notepad).
400 if ((BYTE)pTemp[0] == 0xff && (BYTE)pTemp[1] == 0xfe)
401 enc = ENCODING_UTF16LE;
402 else if ((BYTE)pTemp[0] == 0xfe && (BYTE)pTemp[1] == 0xff)
403 enc = ENCODING_UTF16BE;
408 case ENCODING_UTF16BE:
409 byteswap_wide_string((WCHAR*) pTemp, size/sizeof(WCHAR));
410 /* Forget whether the file is BE or LE, like native Notepad. */
411 enc = ENCODING_UTF16LE;
415 case ENCODING_UTF16LE:
416 textW = (LPWSTR)pTemp;
417 lenW = size/sizeof(WCHAR);
422 int cp = (enc==ENCODING_UTF8) ? CP_UTF8 : CP_ACP;
423 lenW = MultiByteToWideChar(cp, 0, pTemp, size, NULL, 0);
424 textW = HeapAlloc(GetProcessHeap(), 0, (lenW+1) * sizeof(WCHAR));
428 HeapFree(GetProcessHeap(), 0, pTemp);
431 MultiByteToWideChar(cp, 0, pTemp, size, textW, lenW);
432 HeapFree(GetProcessHeap(), 0, pTemp);
437 /* Replace '\0's with spaces. Other than creating a custom control that
438 * can deal with '\0' characters, it's the best that can be done.
440 for (i = 0; i < lenW; i++)
441 if (textW[i] == '\0')
445 if (lenW >= 1 && textW[0] == 0xfeff)
446 SetWindowTextW(Globals.hEdit, textW+1);
448 SetWindowTextW(Globals.hEdit, textW);
450 HeapFree(GetProcessHeap(), 0, textW);
452 SendMessageW(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
453 SendMessageW(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
454 SetFocus(Globals.hEdit);
456 /* If the file starts with .LOG, add a time/date at the end and set cursor after */
457 if (GetWindowTextW(Globals.hEdit, log, ARRAY_SIZE(log)) && !lstrcmpW(log, dotlog))
459 static const WCHAR lfW[] = { '\r','\n',0 };
460 SendMessageW(Globals.hEdit, EM_SETSEL, GetWindowTextLengthW(Globals.hEdit), -1);
461 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW);
462 DIALOG_EditTimeDate();
463 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW);
466 SetFileNameAndEncoding(szFileName, enc);
467 UpdateWindowCaption();
470 VOID DIALOG_FileNew(VOID)
472 static const WCHAR empty_strW[] = { 0 };
474 /* Close any files and prompt to save changes */
476 SetWindowTextW(Globals.hEdit, empty_strW);
477 SendMessageW(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
478 SetFocus(Globals.hEdit);
482 /* Used to detect encoding of files selected in Open dialog.
483 * Returns ENCODING_AUTO if file can't be read, etc.
485 static ENCODING detect_encoding_of_file(LPCWSTR szFileName)
488 HANDLE hFile = CreateFileW(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
489 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
490 if (hFile == INVALID_HANDLE_VALUE)
491 return ENCODING_AUTO;
492 size = GetFileSize(hFile, NULL);
493 if (size == INVALID_FILE_SIZE)
496 return ENCODING_AUTO;
501 BYTE buffer[MAX_STRING_LEN];
502 if (!ReadFile(hFile, buffer, min(size, sizeof(buffer)), &dwNumRead, NULL))
505 return ENCODING_AUTO;
508 return detect_encoding_of_buffer(buffer, dwNumRead);
512 static UINT_PTR CALLBACK OfnHookProc(HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
514 static HWND hEncCombo;
521 hEncCombo = GetDlgItem(hdlg, IDC_OFN_ENCCOMBO);
522 for (enc = MIN_ENCODING; enc <= MAX_ENCODING; enc++)
524 WCHAR szEnc[MAX_STRING_LEN];
525 load_encoding_name(enc, szEnc, ARRAY_SIZE(szEnc));
526 SendMessageW(hEncCombo, CB_ADDSTRING, 0, (LPARAM)szEnc);
528 SendMessageW(hEncCombo, CB_SETCURSEL, (WPARAM)Globals.encOfnCombo, 0);
533 if (LOWORD(wParam) == IDC_OFN_ENCCOMBO &&
534 HIWORD(wParam) == CBN_SELCHANGE)
536 int index = SendMessageW(hEncCombo, CB_GETCURSEL, 0, 0);
537 Globals.encOfnCombo = index==CB_ERR ? ENCODING_ANSI : (ENCODING)index;
543 switch (((OFNOTIFYW*)lParam)->hdr.code)
546 if (Globals.bOfnIsOpenDialog)
548 /* Check the start of the selected file for a BOM. */
550 WCHAR szFileName[MAX_PATH];
551 SendMessageW(GetParent(hdlg), CDM_GETFILEPATH,
552 ARRAY_SIZE(szFileName), (LPARAM)szFileName);
553 enc = detect_encoding_of_file(szFileName);
554 if (enc != ENCODING_AUTO)
556 Globals.encOfnCombo = enc;
557 SendMessageW(hEncCombo, CB_SETCURSEL, (WPARAM)enc, 0);
573 VOID DIALOG_FileOpen(VOID)
575 OPENFILENAMEW openfilename;
576 WCHAR szPath[MAX_PATH];
577 WCHAR szDir[MAX_PATH];
578 static const WCHAR szDefaultExt[] = { 't','x','t',0 };
579 static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
581 ZeroMemory(&openfilename, sizeof(openfilename));
583 GetCurrentDirectoryW(ARRAY_SIZE(szDir), szDir);
584 lstrcpyW(szPath, txt_files);
586 openfilename.lStructSize = sizeof(openfilename);
587 openfilename.hwndOwner = Globals.hMainWnd;
588 openfilename.hInstance = Globals.hInstance;
589 openfilename.lpstrFilter = Globals.szFilter;
590 openfilename.lpstrFile = szPath;
591 openfilename.nMaxFile = ARRAY_SIZE(szPath);
592 openfilename.lpstrInitialDir = szDir;
593 openfilename.Flags = OFN_ENABLETEMPLATE | OFN_ENABLEHOOK | OFN_EXPLORER |
594 OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST |
595 OFN_HIDEREADONLY | OFN_ENABLESIZING;
596 openfilename.lpfnHook = OfnHookProc;
597 openfilename.lpTemplateName = MAKEINTRESOURCEW(IDD_OFN_TEMPLATE);
598 openfilename.lpstrDefExt = szDefaultExt;
600 Globals.encOfnCombo = ENCODING_ANSI;
601 Globals.bOfnIsOpenDialog = TRUE;
603 if (GetOpenFileNameW(&openfilename))
604 DoOpenFile(openfilename.lpstrFile, Globals.encOfnCombo);
607 /* Return FALSE to cancel close */
608 BOOL DIALOG_FileSave(VOID)
610 if (Globals.szFileName[0] == '\0')
611 return DIALOG_FileSaveAs();
614 switch (DoSaveFile(Globals.szFileName, Globals.encFile))
616 case SAVED_OK: return TRUE;
617 case SHOW_SAVEAS_DIALOG: return DIALOG_FileSaveAs();
618 default: return FALSE;
623 BOOL DIALOG_FileSaveAs(VOID)
625 OPENFILENAMEW saveas;
626 WCHAR szPath[MAX_PATH];
627 WCHAR szDir[MAX_PATH];
628 static const WCHAR szDefaultExt[] = { 't','x','t',0 };
629 static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
631 ZeroMemory(&saveas, sizeof(saveas));
633 GetCurrentDirectoryW(ARRAY_SIZE(szDir), szDir);
634 lstrcpyW(szPath, txt_files);
636 saveas.lStructSize = sizeof(OPENFILENAMEW);
637 saveas.hwndOwner = Globals.hMainWnd;
638 saveas.hInstance = Globals.hInstance;
639 saveas.lpstrFilter = Globals.szFilter;
640 saveas.lpstrFile = szPath;
641 saveas.nMaxFile = ARRAY_SIZE(szPath);
642 saveas.lpstrInitialDir = szDir;
643 saveas.Flags = OFN_ENABLETEMPLATE | OFN_ENABLEHOOK | OFN_EXPLORER |
644 OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT |
645 OFN_HIDEREADONLY | OFN_ENABLESIZING;
646 saveas.lpfnHook = OfnHookProc;
647 saveas.lpTemplateName = MAKEINTRESOURCEW(IDD_OFN_TEMPLATE);
648 saveas.lpstrDefExt = szDefaultExt;
650 /* Preset encoding to what file was opened/saved last with. */
651 Globals.encOfnCombo = Globals.encFile;
652 Globals.bOfnIsOpenDialog = FALSE;
655 if (!GetSaveFileNameW(&saveas))
658 switch (DoSaveFile(szPath, Globals.encOfnCombo))
661 SetFileNameAndEncoding(szPath, Globals.encOfnCombo);
662 UpdateWindowCaption();
665 case SHOW_SAVEAS_DIALOG:
678 } TEXTINFO, *LPTEXTINFO;
680 static int notepad_print_header(HDC hdc, RECT *rc, BOOL dopage, BOOL header, int page, LPWSTR text)
686 /* Write the header or footer */
687 GetTextExtentPoint32W(hdc, text, lstrlenW(text), &szMetric);
689 ExtTextOutW(hdc, (rc->left + rc->right - szMetric.cx) / 2,
690 header ? rc->top : rc->bottom - szMetric.cy,
691 ETO_CLIPPED, rc, text, lstrlenW(text), NULL);
697 static BOOL notepad_print_page(HDC hdc, RECT *rc, BOOL dopage, int page, LPTEXTINFO tInfo)
705 if (StartPage(hdc) <= 0)
707 static const WCHAR failedW[] = { 'S','t','a','r','t','P','a','g','e',' ','f','a','i','l','e','d',0 };
708 static const WCHAR errorW[] = { 'P','r','i','n','t',' ','E','r','r','o','r',0 };
709 MessageBoxW(Globals.hMainWnd, failedW, errorW, MB_ICONEXCLAMATION);
714 GetTextMetricsW(hdc, &tm);
715 y = rc->top + notepad_print_header(hdc, rc, dopage, TRUE, page, Globals.szFileName) * tm.tmHeight;
716 b = rc->bottom - 2 * notepad_print_header(hdc, rc, FALSE, FALSE, page, Globals.szFooter) * tm.tmHeight;
723 /* find the end of the line */
724 while (tInfo->mptr < tInfo->mend && *tInfo->mptr != '\n' && *tInfo->mptr != '\r')
726 if (*tInfo->mptr == '\t')
728 /* replace tabs with spaces */
729 for (m = 0; m < SPACES_IN_TAB; m++)
731 if (tInfo->len < PRINT_LEN_MAX)
732 tInfo->lptr[tInfo->len++] = ' ';
733 else if (Globals.bWrapLongLines)
737 else if (tInfo->len < PRINT_LEN_MAX)
738 tInfo->lptr[tInfo->len++] = *tInfo->mptr;
740 if (tInfo->len >= PRINT_LEN_MAX && Globals.bWrapLongLines)
747 /* Find out how much we should print if line wrapping is enabled */
748 if (Globals.bWrapLongLines)
750 GetTextExtentExPointW(hdc, tInfo->lptr, tInfo->len, rc->right - rc->left, &n, NULL, &szMetrics);
751 if (n < tInfo->len && tInfo->lptr[n] != ' ')
754 /* Don't wrap words unless it's a single word over the entire line */
755 while (m && tInfo->lptr[m] != ' ') m--;
756 if (m > 0) n = m + 1;
763 ExtTextOutW(hdc, rc->left, y, ETO_CLIPPED, rc, tInfo->lptr, n, NULL);
769 memcpy(tInfo->lptr, tInfo->lptr + n, tInfo->len * sizeof(WCHAR));
770 y += tm.tmHeight + tm.tmExternalLeading;
774 /* find the next line */
775 while (tInfo->mptr < tInfo->mend && y < b && (*tInfo->mptr == '\n' || *tInfo->mptr == '\r'))
777 if (*tInfo->mptr == '\n')
778 y += tm.tmHeight + tm.tmExternalLeading;
782 } while (tInfo->mptr < tInfo->mend && y < b);
784 notepad_print_header(hdc, rc, dopage, FALSE, page, Globals.szFooter);
792 VOID DIALOG_FilePrint(VOID)
796 int page, dopage, copy;
798 HFONT hTextFont, old_font = 0;
804 WCHAR cTemp[PRINT_LEN_MAX];
806 /* Get Current Settings */
807 ZeroMemory(&printer, sizeof(printer));
808 printer.lStructSize = sizeof(printer);
809 printer.hwndOwner = Globals.hMainWnd;
810 printer.hDevMode = Globals.hDevMode;
811 printer.hDevNames = Globals.hDevNames;
812 printer.hInstance = Globals.hInstance;
814 /* Set some default flags */
815 printer.Flags = PD_RETURNDC | PD_NOSELECTION;
816 printer.nFromPage = 0;
817 printer.nMinPage = 1;
818 /* we really need to calculate number of pages to set nMaxPage and nToPage */
820 printer.nMaxPage = -1;
821 /* Let commdlg manage copy settings */
822 printer.nCopies = (WORD)PD_USEDEVMODECOPIES;
824 if (!PrintDlgW(&printer)) return;
826 Globals.hDevMode = printer.hDevMode;
827 Globals.hDevNames = printer.hDevNames;
829 SetMapMode(printer.hDC, MM_TEXT);
831 /* initialize DOCINFO */
832 di.cbSize = sizeof(DOCINFOW);
833 di.lpszDocName = Globals.szFileTitle;
834 di.lpszOutput = NULL;
835 di.lpszDatatype = NULL;
838 /* Get the file text */
839 size = GetWindowTextLengthW(Globals.hEdit) + 1;
840 pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
843 DeleteDC(printer.hDC);
847 size = GetWindowTextW(Globals.hEdit, pTemp, size);
849 if (StartDocW(printer.hDC, &di) > 0)
851 /* Get the page margins in pixels. */
852 rc.top = MulDiv(Globals.iMarginTop, GetDeviceCaps(printer.hDC, LOGPIXELSY), 2540) -
853 GetDeviceCaps(printer.hDC, PHYSICALOFFSETY);
854 rc.bottom = GetDeviceCaps(printer.hDC, PHYSICALHEIGHT) -
855 MulDiv(Globals.iMarginBottom, GetDeviceCaps(printer.hDC, LOGPIXELSY), 2540);
856 rc.left = MulDiv(Globals.iMarginLeft, GetDeviceCaps(printer.hDC, LOGPIXELSX), 2540) -
857 GetDeviceCaps(printer.hDC, PHYSICALOFFSETX);
858 rc.right = GetDeviceCaps(printer.hDC, PHYSICALWIDTH) -
859 MulDiv(Globals.iMarginRight, GetDeviceCaps(printer.hDC, LOGPIXELSX), 2540);
861 /* Create a font for the printer resolution */
862 lfFont = Globals.lfFont;
863 lfFont.lfHeight = MulDiv(lfFont.lfHeight, GetDeviceCaps(printer.hDC, LOGPIXELSY), get_dpi());
864 /* Make the font a bit lighter */
865 lfFont.lfWeight -= 100;
866 hTextFont = CreateFontIndirectW(&lfFont);
867 old_font = SelectObject(printer.hDC, hTextFont);
869 for (copy = 1; copy <= printer.nCopies; copy++)
874 tInfo.mend = pTemp + size;
879 if (printer.Flags & PD_PAGENUMS)
881 /* a specific range of pages is selected, so
882 * skip pages that are not to be printed
884 if (page > printer.nToPage)
886 else if (page >= printer.nFromPage)
894 ret = notepad_print_page(printer.hDC, &rc, dopage, page, &tInfo);
896 } while (ret && tInfo.mptr < tInfo.mend);
901 SelectObject(printer.hDC, old_font);
902 DeleteObject(hTextFont);
904 DeleteDC(printer.hDC);
905 HeapFree(GetProcessHeap(), 0, pTemp);
908 VOID DIALOG_FilePrinterSetup(VOID)
912 ZeroMemory(&printer, sizeof(printer));
913 printer.lStructSize = sizeof(printer);
914 printer.hwndOwner = Globals.hMainWnd;
915 printer.hDevMode = Globals.hDevMode;
916 printer.hDevNames = Globals.hDevNames;
917 printer.hInstance = Globals.hInstance;
918 printer.Flags = PD_PRINTSETUP;
923 Globals.hDevMode = printer.hDevMode;
924 Globals.hDevNames = printer.hDevNames;
927 VOID DIALOG_FileExit(VOID)
929 PostMessageW(Globals.hMainWnd, WM_CLOSE, 0, 0l);
932 VOID DIALOG_EditUndo(VOID)
934 SendMessageW(Globals.hEdit, EM_UNDO, 0, 0);
937 VOID DIALOG_EditCut(VOID)
939 SendMessageW(Globals.hEdit, WM_CUT, 0, 0);
942 VOID DIALOG_EditCopy(VOID)
944 SendMessageW(Globals.hEdit, WM_COPY, 0, 0);
947 VOID DIALOG_EditPaste(VOID)
949 SendMessageW(Globals.hEdit, WM_PASTE, 0, 0);
952 VOID DIALOG_EditDelete(VOID)
954 SendMessageW(Globals.hEdit, WM_CLEAR, 0, 0);
957 VOID DIALOG_EditSelectAll(VOID)
959 SendMessageW(Globals.hEdit, EM_SETSEL, 0, -1);
962 VOID DIALOG_EditTimeDate(VOID)
965 WCHAR szDate[MAX_STRING_LEN];
966 static const WCHAR spaceW[] = { ' ',0 };
970 GetTimeFormatW(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &st, NULL, szDate, MAX_STRING_LEN);
971 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szDate);
973 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)spaceW);
975 GetDateFormatW(LOCALE_USER_DEFAULT, 0, &st, NULL, szDate, MAX_STRING_LEN);
976 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szDate);
979 VOID DIALOG_EditWrap(VOID)
982 static const WCHAR editW[] = { 'e','d','i','t',0 };
983 DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL |
984 ES_AUTOVSCROLL | ES_MULTILINE;
989 size = GetWindowTextLengthW(Globals.hEdit) + 1;
990 pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
996 GetWindowTextW(Globals.hEdit, pTemp, size);
997 modify = SendMessageW(Globals.hEdit, EM_GETMODIFY, 0, 0);
998 DestroyWindow(Globals.hEdit);
999 GetClientRect(Globals.hMainWnd, &rc);
1000 if( Globals.bWrapLongLines ) dwStyle |= WS_HSCROLL | ES_AUTOHSCROLL;
1001 Globals.hEdit = CreateWindowExW(WS_EX_CLIENTEDGE, editW, NULL, dwStyle,
1002 0, 0, rc.right, rc.bottom, Globals.hMainWnd,
1003 NULL, Globals.hInstance, NULL);
1004 SendMessageW(Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, FALSE);
1005 SetWindowTextW(Globals.hEdit, pTemp);
1006 SendMessageW(Globals.hEdit, EM_SETMODIFY, modify, 0);
1007 SetFocus(Globals.hEdit);
1008 HeapFree(GetProcessHeap(), 0, pTemp);
1010 Globals.bWrapLongLines = !Globals.bWrapLongLines;
1011 CheckMenuItem(GetMenu(Globals.hMainWnd), CMD_WRAP,
1012 MF_BYCOMMAND | (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED));
1015 VOID DIALOG_SelectFont(VOID)
1018 LOGFONTW lf=Globals.lfFont;
1020 ZeroMemory( &cf, sizeof(cf) );
1021 cf.lStructSize=sizeof(cf);
1022 cf.hwndOwner=Globals.hMainWnd;
1024 cf.Flags=CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT;
1026 if( ChooseFontW(&cf) )
1028 HFONT currfont=Globals.hFont;
1030 Globals.hFont=CreateFontIndirectW( &lf );
1032 SendMessageW( Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, TRUE );
1033 if( currfont!=NULL )
1034 DeleteObject( currfont );
1038 VOID DIALOG_Search(VOID)
1040 /* Allow only one search/replace dialog to open */
1041 if(Globals.hFindReplaceDlg != NULL)
1043 SetActiveWindow(Globals.hFindReplaceDlg);
1047 ZeroMemory(&Globals.find, sizeof(Globals.find));
1048 Globals.find.lStructSize = sizeof(Globals.find);
1049 Globals.find.hwndOwner = Globals.hMainWnd;
1050 Globals.find.hInstance = Globals.hInstance;
1051 Globals.find.lpstrFindWhat = Globals.szFindText;
1052 Globals.find.wFindWhatLen = ARRAY_SIZE(Globals.szFindText);
1053 Globals.find.Flags = FR_DOWN|FR_HIDEWHOLEWORD;
1055 /* We only need to create the modal FindReplace dialog which will */
1056 /* notify us of incoming events using hMainWnd Window Messages */
1058 Globals.hFindReplaceDlg = FindTextW(&Globals.find);
1059 assert(Globals.hFindReplaceDlg !=0);
1062 VOID DIALOG_SearchNext(VOID)
1064 if (Globals.lastFind.lpstrFindWhat == NULL)
1066 else /* use the last find data */
1067 NOTEPAD_DoFind(&Globals.lastFind);
1070 VOID DIALOG_Replace(VOID)
1072 /* Allow only one search/replace dialog to open */
1073 if(Globals.hFindReplaceDlg != NULL)
1075 SetActiveWindow(Globals.hFindReplaceDlg);
1079 ZeroMemory(&Globals.find, sizeof(Globals.find));
1080 Globals.find.lStructSize = sizeof(Globals.find);
1081 Globals.find.hwndOwner = Globals.hMainWnd;
1082 Globals.find.hInstance = Globals.hInstance;
1083 Globals.find.lpstrFindWhat = Globals.szFindText;
1084 Globals.find.wFindWhatLen = ARRAY_SIZE(Globals.szFindText);
1085 Globals.find.lpstrReplaceWith = Globals.szReplaceText;
1086 Globals.find.wReplaceWithLen = ARRAY_SIZE(Globals.szReplaceText);
1087 Globals.find.Flags = FR_DOWN|FR_HIDEWHOLEWORD;
1089 /* We only need to create the modal FindReplace dialog which will */
1090 /* notify us of incoming events using hMainWnd Window Messages */
1092 Globals.hFindReplaceDlg = ReplaceTextW(&Globals.find);
1093 assert(Globals.hFindReplaceDlg !=0);
1096 VOID DIALOG_HelpContents(VOID)
1098 WinHelpW(Globals.hMainWnd, helpfileW, HELP_INDEX, 0);
1101 VOID DIALOG_HelpSearch(VOID)
1106 VOID DIALOG_HelpHelp(VOID)
1108 WinHelpW(Globals.hMainWnd, helpfileW, HELP_HELPONHELP, 0);
1111 VOID DIALOG_HelpAboutNotepad(VOID)
1113 static const WCHAR notepadW[] = { 'W','i','n','e',' ','N','o','t','e','p','a','d',0 };
1114 WCHAR szNotepad[MAX_STRING_LEN];
1115 HICON icon = LoadImageW(Globals.hInstance, MAKEINTRESOURCEW(IDI_NOTEPAD),
1116 IMAGE_ICON, 48, 48, LR_SHARED);
1118 LoadStringW(Globals.hInstance, STRING_NOTEPAD, szNotepad, ARRAY_SIZE(szNotepad));
1119 ShellAboutW(Globals.hMainWnd, szNotepad, notepadW, icon);
1123 /***********************************************************************
1125 * DIALOG_FilePageSetup
1127 VOID DIALOG_FilePageSetup(void)
1129 DialogBoxW(Globals.hInstance, MAKEINTRESOURCEW(DIALOG_PAGESETUP),
1130 Globals.hMainWnd, DIALOG_PAGESETUP_DlgProc);
1134 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1136 * DIALOG_PAGESETUP_DlgProc
1139 static INT_PTR WINAPI DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
1148 /* save user input and close dialog */
1149 GetDlgItemTextW(hDlg, IDC_PAGESETUP_HEADERVALUE, Globals.szHeader, ARRAY_SIZE(Globals.szHeader));
1150 GetDlgItemTextW(hDlg, IDC_PAGESETUP_FOOTERVALUE, Globals.szFooter, ARRAY_SIZE(Globals.szFooter));
1152 Globals.iMarginTop = GetDlgItemInt(hDlg, IDC_PAGESETUP_TOPVALUE, NULL, FALSE) * 100;
1153 Globals.iMarginBottom = GetDlgItemInt(hDlg, IDC_PAGESETUP_BOTTOMVALUE, NULL, FALSE) * 100;
1154 Globals.iMarginLeft = GetDlgItemInt(hDlg, IDC_PAGESETUP_LEFTVALUE, NULL, FALSE) * 100;
1155 Globals.iMarginRight = GetDlgItemInt(hDlg, IDC_PAGESETUP_RIGHTVALUE, NULL, FALSE) * 100;
1156 EndDialog(hDlg, IDOK);
1160 /* discard user input and close dialog */
1161 EndDialog(hDlg, IDCANCEL);
1166 /* FIXME: Bring this to work */
1167 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 };
1168 static const WCHAR helpW[] = { 'H','e','l','p',0 };
1169 MessageBoxW(Globals.hMainWnd, sorryW, helpW, MB_ICONEXCLAMATION);
1179 /* fetch last user input prior to display dialog */
1180 SetDlgItemTextW(hDlg, IDC_PAGESETUP_HEADERVALUE, Globals.szHeader);
1181 SetDlgItemTextW(hDlg, IDC_PAGESETUP_FOOTERVALUE, Globals.szFooter);
1182 SetDlgItemInt(hDlg, IDC_PAGESETUP_TOPVALUE, Globals.iMarginTop / 100, FALSE);
1183 SetDlgItemInt(hDlg, IDC_PAGESETUP_BOTTOMVALUE, Globals.iMarginBottom / 100, FALSE);
1184 SetDlgItemInt(hDlg, IDC_PAGESETUP_LEFTVALUE, Globals.iMarginLeft / 100, FALSE);
1185 SetDlgItemInt(hDlg, IDC_PAGESETUP_RIGHTVALUE, Globals.iMarginRight / 100, FALSE);