richedit: The wrong bits were cleared from wBorders in PARAFORMAT2.
[wine/wine64.git] / programs / notepad / dialog.c
blobf326906498f68e8b87d3623abf04579fb202cc94
1 /*
2 * Notepad (dialog.c)
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
24 #define UNICODE
26 #include <assert.h>
27 #include <stdio.h>
28 #include <windows.h>
29 #include <commdlg.h>
30 #include <shlwapi.h>
32 #include "main.h"
33 #include "dialog.h"
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 VOID ShowLastError(void)
44 DWORD error = GetLastError();
45 if (error != NO_ERROR)
47 LPWSTR lpMsgBuf;
48 WCHAR szTitle[MAX_STRING_LEN];
50 LoadString(Globals.hInstance, STRING_ERROR, szTitle, SIZEOF(szTitle));
51 FormatMessage(
52 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
53 NULL, error, 0,
54 (LPTSTR) &lpMsgBuf, 0, NULL);
55 MessageBox(NULL, lpMsgBuf, szTitle, MB_OK | MB_ICONERROR);
56 LocalFree(lpMsgBuf);
60 /**
61 * Sets the caption of the main window according to Globals.szFileTitle:
62 * Untitled - Notepad if no file is open
63 * filename - Notepad if a file is given
65 static void UpdateWindowCaption(void)
67 WCHAR szCaption[MAX_STRING_LEN];
68 WCHAR szNotepad[MAX_STRING_LEN];
69 static const WCHAR hyphenW[] = { ' ','-',' ',0 };
71 if (Globals.szFileTitle[0] != '\0')
72 lstrcpy(szCaption, Globals.szFileTitle);
73 else
74 LoadString(Globals.hInstance, STRING_UNTITLED, szCaption, SIZEOF(szCaption));
76 LoadString(Globals.hInstance, STRING_NOTEPAD, szNotepad, SIZEOF(szNotepad));
77 lstrcat(szCaption, hyphenW);
78 lstrcat(szCaption, szNotepad);
80 SetWindowText(Globals.hMainWnd, szCaption);
83 int DIALOG_StringMsgBox(HWND hParent, int formatId, LPCWSTR szString, DWORD dwFlags)
85 WCHAR szMessage[MAX_STRING_LEN];
86 WCHAR szResource[MAX_STRING_LEN];
88 /* Load and format szMessage */
89 LoadString(Globals.hInstance, formatId, szResource, SIZEOF(szResource));
90 wnsprintf(szMessage, SIZEOF(szMessage), szResource, szString);
92 /* Load szCaption */
93 if ((dwFlags & MB_ICONMASK) == MB_ICONEXCLAMATION)
94 LoadString(Globals.hInstance, STRING_ERROR, szResource, SIZEOF(szResource));
95 else
96 LoadString(Globals.hInstance, STRING_NOTEPAD, szResource, SIZEOF(szResource));
98 /* Display Modal Dialog */
99 if (hParent == NULL)
100 hParent = Globals.hMainWnd;
101 return MessageBox(hParent, szMessage, szResource, dwFlags);
104 static void AlertFileNotFound(LPCWSTR szFileName)
106 DIALOG_StringMsgBox(NULL, STRING_NOTFOUND, szFileName, MB_ICONEXCLAMATION|MB_OK);
109 static int AlertFileNotSaved(LPCWSTR szFileName)
111 WCHAR szUntitled[MAX_STRING_LEN];
113 LoadString(Globals.hInstance, STRING_UNTITLED, szUntitled, SIZEOF(szUntitled));
114 return DIALOG_StringMsgBox(NULL, STRING_NOTSAVED, szFileName[0] ? szFileName : szUntitled,
115 MB_ICONQUESTION|MB_YESNOCANCEL);
119 * Returns:
120 * TRUE - if file exists
121 * FALSE - if file does not exist
123 BOOL FileExists(LPCWSTR szFilename)
125 WIN32_FIND_DATA entry;
126 HANDLE hFile;
128 hFile = FindFirstFile(szFilename, &entry);
129 FindClose(hFile);
131 return (hFile != INVALID_HANDLE_VALUE);
135 static VOID DoSaveFile(VOID)
137 HANDLE hFile;
138 DWORD dwNumWrite;
139 LPSTR pTemp;
140 DWORD size;
142 hFile = CreateFile(Globals.szFileName, GENERIC_WRITE, FILE_SHARE_WRITE,
143 NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
144 if(hFile == INVALID_HANDLE_VALUE)
146 ShowLastError();
147 return;
150 size = GetWindowTextLengthA(Globals.hEdit) + 1;
151 pTemp = HeapAlloc(GetProcessHeap(), 0, size);
152 if (!pTemp)
154 CloseHandle(hFile);
155 ShowLastError();
156 return;
158 size = GetWindowTextA(Globals.hEdit, pTemp, size);
160 if (!WriteFile(hFile, pTemp, size, &dwNumWrite, NULL))
161 ShowLastError();
162 else
163 SendMessage(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
165 SetEndOfFile(hFile);
166 CloseHandle(hFile);
167 HeapFree(GetProcessHeap(), 0, pTemp);
171 * Returns:
172 * TRUE - User agreed to close (both save/don't save)
173 * FALSE - User cancelled close by selecting "Cancel"
175 BOOL DoCloseFile(void)
177 int nResult;
178 static const WCHAR empty_strW[] = { 0 };
180 if (SendMessage(Globals.hEdit, EM_GETMODIFY, 0, 0))
182 /* prompt user to save changes */
183 nResult = AlertFileNotSaved(Globals.szFileName);
184 switch (nResult) {
185 case IDYES: DIALOG_FileSave();
186 break;
188 case IDNO: break;
190 case IDCANCEL: return(FALSE);
192 default: return(FALSE);
193 } /* switch */
194 } /* if */
196 SetFileName(empty_strW);
198 UpdateWindowCaption();
199 return(TRUE);
203 void DoOpenFile(LPCWSTR szFileName)
205 static const WCHAR dotlog[] = { '.','L','O','G',0 };
206 HANDLE hFile;
207 LPSTR pTemp;
208 DWORD size;
209 DWORD dwNumRead;
210 WCHAR log[5];
212 /* Close any files and prompt to save changes */
213 if (!DoCloseFile())
214 return;
216 hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
217 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
218 if(hFile == INVALID_HANDLE_VALUE)
220 AlertFileNotFound(szFileName);
221 return;
224 size = GetFileSize(hFile, NULL);
225 if (size == INVALID_FILE_SIZE)
227 CloseHandle(hFile);
228 ShowLastError();
229 return;
231 size++;
233 pTemp = HeapAlloc(GetProcessHeap(), 0, size);
234 if (!pTemp)
236 CloseHandle(hFile);
237 ShowLastError();
238 return;
241 if (!ReadFile(hFile, pTemp, size, &dwNumRead, NULL))
243 CloseHandle(hFile);
244 HeapFree(GetProcessHeap(), 0, pTemp);
245 ShowLastError();
246 return;
249 CloseHandle(hFile);
250 pTemp[dwNumRead] = 0;
252 if (IsTextUnicode(pTemp, dwNumRead, NULL))
254 LPWSTR p = (LPWSTR)pTemp;
255 /* We need to strip BOM Unicode character, SetWindowTextW won't do it for us. */
256 if (*p == 0xFEFF || *p == 0xFFFE) p++;
257 SetWindowTextW(Globals.hEdit, p);
259 else
260 SetWindowTextA(Globals.hEdit, pTemp);
262 HeapFree(GetProcessHeap(), 0, pTemp);
264 SendMessage(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
265 SendMessage(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
266 SetFocus(Globals.hEdit);
268 /* If the file starts with .LOG, add a time/date at the end and set cursor after */
269 if (GetWindowTextW(Globals.hEdit, log, sizeof(log)/sizeof(log[0])) && !lstrcmp(log, dotlog))
271 static const WCHAR lfW[] = { '\r','\n',0 };
272 SendMessage(Globals.hEdit, EM_SETSEL, GetWindowTextLength(Globals.hEdit), -1);
273 SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW);
274 DIALOG_EditTimeDate();
275 SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW);
278 SetFileName(szFileName);
279 UpdateWindowCaption();
282 VOID DIALOG_FileNew(VOID)
284 static const WCHAR empty_strW[] = { 0 };
286 /* Close any files and prompt to save changes */
287 if (DoCloseFile()) {
288 SetWindowText(Globals.hEdit, empty_strW);
289 SendMessage(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
290 SetFocus(Globals.hEdit);
294 VOID DIALOG_FileOpen(VOID)
296 OPENFILENAME openfilename;
297 WCHAR szPath[MAX_PATH];
298 WCHAR szDir[MAX_PATH];
299 static const WCHAR szDefaultExt[] = { 't','x','t',0 };
300 static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
302 ZeroMemory(&openfilename, sizeof(openfilename));
304 GetCurrentDirectory(SIZEOF(szDir), szDir);
305 lstrcpy(szPath, txt_files);
307 openfilename.lStructSize = sizeof(openfilename);
308 openfilename.hwndOwner = Globals.hMainWnd;
309 openfilename.hInstance = Globals.hInstance;
310 openfilename.lpstrFilter = Globals.szFilter;
311 openfilename.lpstrFile = szPath;
312 openfilename.nMaxFile = SIZEOF(szPath);
313 openfilename.lpstrInitialDir = szDir;
314 openfilename.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST |
315 OFN_HIDEREADONLY;
316 openfilename.lpstrDefExt = szDefaultExt;
319 if (GetOpenFileName(&openfilename))
320 DoOpenFile(openfilename.lpstrFile);
324 VOID DIALOG_FileSave(VOID)
326 if (Globals.szFileName[0] == '\0')
327 DIALOG_FileSaveAs();
328 else
329 DoSaveFile();
332 VOID DIALOG_FileSaveAs(VOID)
334 OPENFILENAME saveas;
335 WCHAR szPath[MAX_PATH];
336 WCHAR szDir[MAX_PATH];
337 static const WCHAR szDefaultExt[] = { 't','x','t',0 };
338 static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
340 ZeroMemory(&saveas, sizeof(saveas));
342 GetCurrentDirectory(SIZEOF(szDir), szDir);
343 lstrcpy(szPath, txt_files);
345 saveas.lStructSize = sizeof(OPENFILENAME);
346 saveas.hwndOwner = Globals.hMainWnd;
347 saveas.hInstance = Globals.hInstance;
348 saveas.lpstrFilter = Globals.szFilter;
349 saveas.lpstrFile = szPath;
350 saveas.nMaxFile = SIZEOF(szPath);
351 saveas.lpstrInitialDir = szDir;
352 saveas.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT |
353 OFN_HIDEREADONLY;
354 saveas.lpstrDefExt = szDefaultExt;
356 if (GetSaveFileName(&saveas)) {
357 SetFileName(szPath);
358 UpdateWindowCaption();
359 DoSaveFile();
363 typedef struct {
364 LPWSTR mptr;
365 LPWSTR mend;
366 LPWSTR lptr;
367 DWORD len;
368 } TEXTINFO, *LPTEXTINFO;
370 static int notepad_print_header(HDC hdc, RECT *rc, BOOL dopage, BOOL header, int page, LPWSTR text)
372 SIZE szMetric;
374 if (*text)
376 /* Write the header or footer */
377 GetTextExtentPoint32(hdc, text, lstrlen(text), &szMetric);
378 if (dopage)
379 ExtTextOut(hdc, (rc->left + rc->right - szMetric.cx) / 2,
380 header ? rc->top : rc->bottom - szMetric.cy,
381 ETO_CLIPPED, rc, text, lstrlen(text), NULL);
382 return 1;
384 return 0;
387 static BOOL notepad_print_page(HDC hdc, RECT *rc, BOOL dopage, int page, LPTEXTINFO tInfo)
389 int b, y;
390 TEXTMETRIC tm;
391 SIZE szMetrics;
393 if (dopage)
395 if (StartPage(hdc) <= 0)
397 static const WCHAR failedW[] = { 'S','t','a','r','t','P','a','g','e',' ','f','a','i','l','e','d',0 };
398 static const WCHAR errorW[] = { 'P','r','i','n','t',' ','E','r','r','o','r',0 };
399 MessageBox(Globals.hMainWnd, failedW, errorW, MB_ICONEXCLAMATION);
400 return FALSE;
404 GetTextMetrics(hdc, &tm);
405 y = rc->top + notepad_print_header(hdc, rc, dopage, TRUE, page, Globals.szFileName) * tm.tmHeight;
406 b = rc->bottom - 2 * notepad_print_header(hdc, rc, FALSE, FALSE, page, Globals.szFooter) * tm.tmHeight;
408 do {
409 INT m, n;
411 if (!tInfo->len)
413 /* find the end of the line */
414 while (tInfo->mptr < tInfo->mend && *tInfo->mptr != '\n' && *tInfo->mptr != '\r')
416 if (*tInfo->mptr == '\t')
418 /* replace tabs with spaces */
419 for (m = 0; m < SPACES_IN_TAB; m++)
421 if (tInfo->len < PRINT_LEN_MAX)
422 tInfo->lptr[tInfo->len++] = ' ';
423 else if (Globals.bWrapLongLines)
424 break;
427 else if (tInfo->len < PRINT_LEN_MAX)
428 tInfo->lptr[tInfo->len++] = *tInfo->mptr;
430 if (tInfo->len >= PRINT_LEN_MAX && Globals.bWrapLongLines)
431 break;
433 tInfo->mptr++;
437 /* Find out how much we should print if line wrapping is enabled */
438 if (Globals.bWrapLongLines)
440 GetTextExtentExPoint(hdc, tInfo->lptr, tInfo->len, rc->right - rc->left, &n, NULL, &szMetrics);
441 if (n < tInfo->len && tInfo->lptr[n] != ' ')
443 m = n;
444 /* Don't wrap words unless it's a single word over the entire line */
445 while (m && tInfo->lptr[m] != ' ') m--;
446 if (m > 0) n = m + 1;
449 else
450 n = tInfo->len;
452 if (dopage)
453 ExtTextOut(hdc, rc->left, y, ETO_CLIPPED, rc, tInfo->lptr, n, NULL);
455 tInfo->len -= n;
457 if (tInfo->len)
459 memcpy(tInfo->lptr, tInfo->lptr + n, tInfo->len * sizeof(WCHAR));
460 y += tm.tmHeight + tm.tmExternalLeading;
462 else
464 /* find the next line */
465 while (tInfo->mptr < tInfo->mend && y < b && (*tInfo->mptr == '\n' || *tInfo->mptr == '\r'))
467 if (*tInfo->mptr == '\n')
468 y += tm.tmHeight + tm.tmExternalLeading;
469 tInfo->mptr++;
472 } while (tInfo->mptr < tInfo->mend && y < b);
474 notepad_print_header(hdc, rc, dopage, FALSE, page, Globals.szFooter);
475 if (dopage)
477 EndPage(hdc);
479 return TRUE;
482 VOID DIALOG_FilePrint(VOID)
484 DOCINFO di;
485 PRINTDLG printer;
486 int page, dopage, copy;
487 LOGFONT lfFont;
488 HFONT hTextFont, old_font = 0;
489 DWORD size;
490 BOOL ret = FALSE;
491 RECT rc;
492 LPWSTR pTemp;
493 TEXTINFO tInfo;
494 WCHAR cTemp[PRINT_LEN_MAX];
496 /* Get Current Settings */
497 ZeroMemory(&printer, sizeof(printer));
498 printer.lStructSize = sizeof(printer);
499 printer.hwndOwner = Globals.hMainWnd;
500 printer.hDevMode = Globals.hDevMode;
501 printer.hDevNames = Globals.hDevNames;
502 printer.hInstance = Globals.hInstance;
504 /* Set some default flags */
505 printer.Flags = PD_RETURNDC | PD_NOSELECTION;
506 printer.nFromPage = 0;
507 printer.nMinPage = 1;
508 /* we really need to calculate number of pages to set nMaxPage and nToPage */
509 printer.nToPage = 0;
510 printer.nMaxPage = -1;
511 /* Let commdlg manage copy settings */
512 printer.nCopies = (WORD)PD_USEDEVMODECOPIES;
514 if (!PrintDlg(&printer)) return;
516 Globals.hDevMode = printer.hDevMode;
517 Globals.hDevNames = printer.hDevNames;
519 SetMapMode(printer.hDC, MM_TEXT);
521 /* initialize DOCINFO */
522 di.cbSize = sizeof(DOCINFO);
523 di.lpszDocName = Globals.szFileTitle;
524 di.lpszOutput = NULL;
525 di.lpszDatatype = NULL;
526 di.fwType = 0;
528 /* Get the file text */
529 size = GetWindowTextLength(Globals.hEdit) + 1;
530 pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
531 if (!pTemp)
533 DeleteDC(printer.hDC);
534 ShowLastError();
535 return;
537 size = GetWindowText(Globals.hEdit, pTemp, size);
539 if (StartDoc(printer.hDC, &di) > 0)
541 /* Get the page margins in pixels. */
542 rc.top = MulDiv(Globals.iMarginTop, GetDeviceCaps(printer.hDC, LOGPIXELSY), 2540) -
543 GetDeviceCaps(printer.hDC, PHYSICALOFFSETY);
544 rc.bottom = GetDeviceCaps(printer.hDC, PHYSICALHEIGHT) -
545 MulDiv(Globals.iMarginBottom, GetDeviceCaps(printer.hDC, LOGPIXELSY), 2540);
546 rc.left = MulDiv(Globals.iMarginLeft, GetDeviceCaps(printer.hDC, LOGPIXELSX), 2540) -
547 GetDeviceCaps(printer.hDC, PHYSICALOFFSETX);
548 rc.right = GetDeviceCaps(printer.hDC, PHYSICALWIDTH) -
549 MulDiv(Globals.iMarginRight, GetDeviceCaps(printer.hDC, LOGPIXELSX), 2540);
551 /* Create a font for the printer resolution */
552 lfFont = Globals.lfFont;
553 lfFont.lfHeight = MulDiv(lfFont.lfHeight, GetDeviceCaps(printer.hDC, LOGPIXELSY), get_dpi());
554 /* Make the font a bit lighter */
555 lfFont.lfWeight -= 100;
556 hTextFont = CreateFontIndirect(&lfFont);
557 old_font = SelectObject(printer.hDC, hTextFont);
559 for (copy = 1; copy <= printer.nCopies; copy++)
561 page = 1;
563 tInfo.mptr = pTemp;
564 tInfo.mend = pTemp + size;
565 tInfo.lptr = cTemp;
566 tInfo.len = 0;
568 do {
569 if (printer.Flags & PD_PAGENUMS)
571 /* a specific range of pages is selected, so
572 * skip pages that are not to be printed
574 if (page > printer.nToPage)
575 break;
576 else if (page >= printer.nFromPage)
577 dopage = 1;
578 else
579 dopage = 0;
581 else
582 dopage = 1;
584 ret = notepad_print_page(printer.hDC, &rc, dopage, page, &tInfo);
585 page++;
586 } while (ret && tInfo.mptr < tInfo.mend);
588 if (!ret) break;
590 EndDoc(printer.hDC);
591 SelectObject(printer.hDC, old_font);
592 DeleteObject(hTextFont);
594 DeleteDC(printer.hDC);
595 HeapFree(GetProcessHeap(), 0, pTemp);
598 VOID DIALOG_FilePrinterSetup(VOID)
600 PRINTDLG printer;
602 ZeroMemory(&printer, sizeof(printer));
603 printer.lStructSize = sizeof(printer);
604 printer.hwndOwner = Globals.hMainWnd;
605 printer.hDevMode = Globals.hDevMode;
606 printer.hDevNames = Globals.hDevNames;
607 printer.hInstance = Globals.hInstance;
608 printer.Flags = PD_PRINTSETUP;
609 printer.nCopies = 1;
611 PrintDlg(&printer);
613 Globals.hDevMode = printer.hDevMode;
614 Globals.hDevNames = printer.hDevNames;
617 VOID DIALOG_FileExit(VOID)
619 PostMessage(Globals.hMainWnd, WM_CLOSE, 0, 0l);
622 VOID DIALOG_EditUndo(VOID)
624 SendMessage(Globals.hEdit, EM_UNDO, 0, 0);
627 VOID DIALOG_EditCut(VOID)
629 SendMessage(Globals.hEdit, WM_CUT, 0, 0);
632 VOID DIALOG_EditCopy(VOID)
634 SendMessage(Globals.hEdit, WM_COPY, 0, 0);
637 VOID DIALOG_EditPaste(VOID)
639 SendMessage(Globals.hEdit, WM_PASTE, 0, 0);
642 VOID DIALOG_EditDelete(VOID)
644 SendMessage(Globals.hEdit, WM_CLEAR, 0, 0);
647 VOID DIALOG_EditSelectAll(VOID)
649 SendMessage(Globals.hEdit, EM_SETSEL, 0, (LPARAM)-1);
652 VOID DIALOG_EditTimeDate(VOID)
654 SYSTEMTIME st;
655 WCHAR szDate[MAX_STRING_LEN];
656 static const WCHAR spaceW[] = { ' ',0 };
658 GetLocalTime(&st);
660 GetTimeFormat(LOCALE_USER_DEFAULT, 0, &st, NULL, szDate, MAX_STRING_LEN);
661 SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szDate);
663 SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)spaceW);
665 GetDateFormat(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, NULL, szDate, MAX_STRING_LEN);
666 SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szDate);
669 VOID DIALOG_EditWrap(VOID)
671 BOOL modify = FALSE;
672 static const WCHAR editW[] = { 'e','d','i','t',0 };
673 DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL |
674 ES_AUTOVSCROLL | ES_MULTILINE;
675 RECT rc;
676 DWORD size;
677 LPWSTR pTemp;
679 size = GetWindowTextLength(Globals.hEdit) + 1;
680 pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
681 if (!pTemp)
683 ShowLastError();
684 return;
686 GetWindowText(Globals.hEdit, pTemp, size);
687 modify = SendMessage(Globals.hEdit, EM_GETMODIFY, 0, 0);
688 DestroyWindow(Globals.hEdit);
689 GetClientRect(Globals.hMainWnd, &rc);
690 if( Globals.bWrapLongLines ) dwStyle |= WS_HSCROLL | ES_AUTOHSCROLL;
691 Globals.hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, editW, NULL, dwStyle,
692 0, 0, rc.right, rc.bottom, Globals.hMainWnd,
693 NULL, Globals.hInstance, NULL);
694 SendMessage(Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, (LPARAM)FALSE);
695 SetWindowTextW(Globals.hEdit, pTemp);
696 SendMessage(Globals.hEdit, EM_SETMODIFY, (WPARAM)modify, 0);
697 SetFocus(Globals.hEdit);
698 HeapFree(GetProcessHeap(), 0, pTemp);
700 Globals.bWrapLongLines = !Globals.bWrapLongLines;
701 CheckMenuItem(GetMenu(Globals.hMainWnd), CMD_WRAP,
702 MF_BYCOMMAND | (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED));
705 VOID DIALOG_SelectFont(VOID)
707 CHOOSEFONT cf;
708 LOGFONT lf=Globals.lfFont;
710 ZeroMemory( &cf, sizeof(cf) );
711 cf.lStructSize=sizeof(cf);
712 cf.hwndOwner=Globals.hMainWnd;
713 cf.lpLogFont=&lf;
714 cf.Flags=CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT;
716 if( ChooseFont(&cf) )
718 HFONT currfont=Globals.hFont;
720 Globals.hFont=CreateFontIndirect( &lf );
721 Globals.lfFont=lf;
722 SendMessage( Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, (LPARAM)TRUE );
723 if( currfont!=NULL )
724 DeleteObject( currfont );
728 VOID DIALOG_Search(VOID)
730 ZeroMemory(&Globals.find, sizeof(Globals.find));
731 Globals.find.lStructSize = sizeof(Globals.find);
732 Globals.find.hwndOwner = Globals.hMainWnd;
733 Globals.find.hInstance = Globals.hInstance;
734 Globals.find.lpstrFindWhat = Globals.szFindText;
735 Globals.find.wFindWhatLen = SIZEOF(Globals.szFindText);
736 Globals.find.Flags = FR_DOWN|FR_HIDEWHOLEWORD;
738 /* We only need to create the modal FindReplace dialog which will */
739 /* notify us of incoming events using hMainWnd Window Messages */
741 Globals.hFindReplaceDlg = FindText(&Globals.find);
742 assert(Globals.hFindReplaceDlg !=0);
745 VOID DIALOG_SearchNext(VOID)
747 if (Globals.lastFind.lpstrFindWhat == NULL)
748 DIALOG_Search();
749 else /* use the last find data */
750 NOTEPAD_DoFind(&Globals.lastFind);
753 VOID DIALOG_HelpContents(VOID)
755 WinHelp(Globals.hMainWnd, helpfileW, HELP_INDEX, 0);
758 VOID DIALOG_HelpSearch(VOID)
760 /* Search Help */
763 VOID DIALOG_HelpHelp(VOID)
765 WinHelp(Globals.hMainWnd, helpfileW, HELP_HELPONHELP, 0);
768 VOID DIALOG_HelpAboutNotepad(VOID)
770 static const WCHAR notepadW[] = { 'W','i','n','e',' ','N','o','t','e','p','a','d',0 };
771 WCHAR szNotepad[MAX_STRING_LEN];
772 HICON icon = LoadImageW( Globals.hInstance, MAKEINTRESOURCE(IDI_NOTEPAD),
773 IMAGE_ICON, 48, 48, LR_SHARED );
775 LoadString(Globals.hInstance, STRING_NOTEPAD, szNotepad, SIZEOF(szNotepad));
776 ShellAbout(Globals.hMainWnd, szNotepad, notepadW, icon);
780 /***********************************************************************
782 * DIALOG_FilePageSetup
784 VOID DIALOG_FilePageSetup(void)
786 DialogBox(Globals.hInstance, MAKEINTRESOURCE(DIALOG_PAGESETUP),
787 Globals.hMainWnd, DIALOG_PAGESETUP_DlgProc);
791 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
793 * DIALOG_PAGESETUP_DlgProc
796 static INT_PTR WINAPI DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
799 switch (msg)
801 case WM_COMMAND:
802 switch (wParam)
804 case IDOK:
805 /* save user input and close dialog */
806 GetDlgItemText(hDlg, IDC_PAGESETUP_HEADERVALUE, Globals.szHeader, SIZEOF(Globals.szHeader));
807 GetDlgItemText(hDlg, IDC_PAGESETUP_FOOTERVALUE, Globals.szFooter, SIZEOF(Globals.szFooter));
809 Globals.iMarginTop = GetDlgItemInt(hDlg, IDC_PAGESETUP_TOPVALUE, NULL, FALSE) * 100;
810 Globals.iMarginBottom = GetDlgItemInt(hDlg, IDC_PAGESETUP_BOTTOMVALUE, NULL, FALSE) * 100;
811 Globals.iMarginLeft = GetDlgItemInt(hDlg, IDC_PAGESETUP_LEFTVALUE, NULL, FALSE) * 100;
812 Globals.iMarginRight = GetDlgItemInt(hDlg, IDC_PAGESETUP_RIGHTVALUE, NULL, FALSE) * 100;
813 EndDialog(hDlg, IDOK);
814 return TRUE;
816 case IDCANCEL:
817 /* discard user input and close dialog */
818 EndDialog(hDlg, IDCANCEL);
819 return TRUE;
821 case IDHELP:
823 /* FIXME: Bring this to work */
824 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 };
825 static const WCHAR helpW[] = { 'H','e','l','p',0 };
826 MessageBox(Globals.hMainWnd, sorryW, helpW, MB_ICONEXCLAMATION);
827 return TRUE;
830 default:
831 break;
833 break;
835 case WM_INITDIALOG:
836 /* fetch last user input prior to display dialog */
837 SetDlgItemText(hDlg, IDC_PAGESETUP_HEADERVALUE, Globals.szHeader);
838 SetDlgItemText(hDlg, IDC_PAGESETUP_FOOTERVALUE, Globals.szFooter);
839 SetDlgItemInt(hDlg, IDC_PAGESETUP_TOPVALUE, Globals.iMarginTop / 100, FALSE);
840 SetDlgItemInt(hDlg, IDC_PAGESETUP_BOTTOMVALUE, Globals.iMarginBottom / 100, FALSE);
841 SetDlgItemInt(hDlg, IDC_PAGESETUP_LEFTVALUE, Globals.iMarginLeft / 100, FALSE);
842 SetDlgItemInt(hDlg, IDC_PAGESETUP_RIGHTVALUE, Globals.iMarginRight / 100, FALSE);
843 break;
846 return FALSE;