gdiplus: Store the rect passed to GdipCreateLineBrushFromRect.
[wine/multimedia.git] / programs / notepad / dialog.c
blobb65126950a0c2e6fa0519db36e2d3632ad254cff
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, (LPWSTR)&lpMsgBuf, 0, NULL);
54 MessageBox(NULL, lpMsgBuf, szTitle, MB_OK | MB_ICONERROR);
55 LocalFree(lpMsgBuf);
59 /**
60 * Sets the caption of the main window according to Globals.szFileTitle:
61 * Untitled - Notepad if no file is open
62 * filename - Notepad if a file is given
64 static void UpdateWindowCaption(void)
66 WCHAR szCaption[MAX_STRING_LEN];
67 WCHAR szNotepad[MAX_STRING_LEN];
68 static const WCHAR hyphenW[] = { ' ','-',' ',0 };
70 if (Globals.szFileTitle[0] != '\0')
71 lstrcpy(szCaption, Globals.szFileTitle);
72 else
73 LoadString(Globals.hInstance, STRING_UNTITLED, szCaption, SIZEOF(szCaption));
75 LoadString(Globals.hInstance, STRING_NOTEPAD, szNotepad, SIZEOF(szNotepad));
76 lstrcat(szCaption, hyphenW);
77 lstrcat(szCaption, szNotepad);
79 SetWindowText(Globals.hMainWnd, szCaption);
82 int DIALOG_StringMsgBox(HWND hParent, int formatId, LPCWSTR szString, DWORD dwFlags)
84 WCHAR szMessage[MAX_STRING_LEN];
85 WCHAR szResource[MAX_STRING_LEN];
87 /* Load and format szMessage */
88 LoadString(Globals.hInstance, formatId, szResource, SIZEOF(szResource));
89 wnsprintf(szMessage, SIZEOF(szMessage), szResource, szString);
91 /* Load szCaption */
92 if ((dwFlags & MB_ICONMASK) == MB_ICONEXCLAMATION)
93 LoadString(Globals.hInstance, STRING_ERROR, szResource, SIZEOF(szResource));
94 else
95 LoadString(Globals.hInstance, STRING_NOTEPAD, szResource, SIZEOF(szResource));
97 /* Display Modal Dialog */
98 if (hParent == NULL)
99 hParent = Globals.hMainWnd;
100 return MessageBox(hParent, szMessage, szResource, dwFlags);
103 static void AlertFileNotFound(LPCWSTR szFileName)
105 DIALOG_StringMsgBox(NULL, STRING_NOTFOUND, szFileName, MB_ICONEXCLAMATION|MB_OK);
108 static int AlertFileNotSaved(LPCWSTR szFileName)
110 WCHAR szUntitled[MAX_STRING_LEN];
112 LoadString(Globals.hInstance, STRING_UNTITLED, szUntitled, SIZEOF(szUntitled));
113 return DIALOG_StringMsgBox(NULL, STRING_NOTSAVED, szFileName[0] ? szFileName : szUntitled,
114 MB_ICONQUESTION|MB_YESNOCANCEL);
118 * Returns:
119 * TRUE - if file exists
120 * FALSE - if file does not exist
122 BOOL FileExists(LPCWSTR szFilename)
124 WIN32_FIND_DATAW entry;
125 HANDLE hFile;
127 hFile = FindFirstFile(szFilename, &entry);
128 FindClose(hFile);
130 return (hFile != INVALID_HANDLE_VALUE);
134 static VOID DoSaveFile(VOID)
136 HANDLE hFile;
137 DWORD dwNumWrite;
138 LPSTR pTemp;
139 DWORD size;
141 hFile = CreateFile(Globals.szFileName, GENERIC_WRITE, FILE_SHARE_WRITE,
142 NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
143 if(hFile == INVALID_HANDLE_VALUE)
145 ShowLastError();
146 return;
149 size = GetWindowTextLengthA(Globals.hEdit) + 1;
150 pTemp = HeapAlloc(GetProcessHeap(), 0, size);
151 if (!pTemp)
153 CloseHandle(hFile);
154 ShowLastError();
155 return;
157 size = GetWindowTextA(Globals.hEdit, pTemp, size);
159 if (!WriteFile(hFile, pTemp, size, &dwNumWrite, NULL))
160 ShowLastError();
161 else
162 SendMessageW(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
164 SetEndOfFile(hFile);
165 CloseHandle(hFile);
166 HeapFree(GetProcessHeap(), 0, pTemp);
170 * Returns:
171 * TRUE - User agreed to close (both save/don't save)
172 * FALSE - User cancelled close by selecting "Cancel"
174 BOOL DoCloseFile(void)
176 int nResult;
177 static const WCHAR empty_strW[] = { 0 };
179 if (SendMessageW(Globals.hEdit, EM_GETMODIFY, 0, 0))
181 /* prompt user to save changes */
182 nResult = AlertFileNotSaved(Globals.szFileName);
183 switch (nResult) {
184 case IDYES: return DIALOG_FileSave();
186 case IDNO: break;
188 case IDCANCEL: return(FALSE);
190 default: return(FALSE);
191 } /* switch */
192 } /* if */
194 SetFileName(empty_strW);
196 UpdateWindowCaption();
197 return(TRUE);
201 void DoOpenFile(LPCWSTR szFileName)
203 static const WCHAR dotlog[] = { '.','L','O','G',0 };
204 HANDLE hFile;
205 LPSTR pTemp;
206 DWORD size;
207 DWORD dwNumRead;
208 WCHAR log[5];
210 /* Close any files and prompt to save changes */
211 if (!DoCloseFile())
212 return;
214 hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
215 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
216 if(hFile == INVALID_HANDLE_VALUE)
218 AlertFileNotFound(szFileName);
219 return;
222 size = GetFileSize(hFile, NULL);
223 if (size == INVALID_FILE_SIZE)
225 CloseHandle(hFile);
226 ShowLastError();
227 return;
229 size++;
231 pTemp = HeapAlloc(GetProcessHeap(), 0, size);
232 if (!pTemp)
234 CloseHandle(hFile);
235 ShowLastError();
236 return;
239 if (!ReadFile(hFile, pTemp, size, &dwNumRead, NULL))
241 CloseHandle(hFile);
242 HeapFree(GetProcessHeap(), 0, pTemp);
243 ShowLastError();
244 return;
247 CloseHandle(hFile);
248 pTemp[dwNumRead] = 0;
250 if((size -1) >= 2 && (BYTE)pTemp[0] == 0xff && (BYTE)pTemp[1] == 0xfe)
251 SetWindowTextW(Globals.hEdit, (LPWSTR)pTemp + 1);
252 else
253 SetWindowTextA(Globals.hEdit, pTemp);
255 HeapFree(GetProcessHeap(), 0, pTemp);
257 SendMessageW(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
258 SendMessageW(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
259 SetFocus(Globals.hEdit);
261 /* If the file starts with .LOG, add a time/date at the end and set cursor after */
262 if (GetWindowTextW(Globals.hEdit, log, sizeof(log)/sizeof(log[0])) && !lstrcmp(log, dotlog))
264 static const WCHAR lfW[] = { '\r','\n',0 };
265 SendMessageW(Globals.hEdit, EM_SETSEL, GetWindowTextLength(Globals.hEdit), -1);
266 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW);
267 DIALOG_EditTimeDate();
268 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW);
271 SetFileName(szFileName);
272 UpdateWindowCaption();
275 VOID DIALOG_FileNew(VOID)
277 static const WCHAR empty_strW[] = { 0 };
279 /* Close any files and prompt to save changes */
280 if (DoCloseFile()) {
281 SetWindowText(Globals.hEdit, empty_strW);
282 SendMessageW(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
283 SetFocus(Globals.hEdit);
287 VOID DIALOG_FileOpen(VOID)
289 OPENFILENAMEW openfilename;
290 WCHAR szPath[MAX_PATH];
291 WCHAR szDir[MAX_PATH];
292 static const WCHAR szDefaultExt[] = { 't','x','t',0 };
293 static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
295 ZeroMemory(&openfilename, sizeof(openfilename));
297 GetCurrentDirectory(SIZEOF(szDir), szDir);
298 lstrcpy(szPath, txt_files);
300 openfilename.lStructSize = sizeof(openfilename);
301 openfilename.hwndOwner = Globals.hMainWnd;
302 openfilename.hInstance = Globals.hInstance;
303 openfilename.lpstrFilter = Globals.szFilter;
304 openfilename.lpstrFile = szPath;
305 openfilename.nMaxFile = SIZEOF(szPath);
306 openfilename.lpstrInitialDir = szDir;
307 openfilename.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST |
308 OFN_HIDEREADONLY | OFN_ENABLESIZING;
309 openfilename.lpstrDefExt = szDefaultExt;
312 if (GetOpenFileName(&openfilename))
313 DoOpenFile(openfilename.lpstrFile);
317 BOOL DIALOG_FileSave(VOID)
319 if (Globals.szFileName[0] == '\0')
320 return DIALOG_FileSaveAs();
321 else
322 DoSaveFile();
323 return TRUE;
326 BOOL DIALOG_FileSaveAs(VOID)
328 OPENFILENAMEW saveas;
329 WCHAR szPath[MAX_PATH];
330 WCHAR szDir[MAX_PATH];
331 static const WCHAR szDefaultExt[] = { 't','x','t',0 };
332 static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
334 ZeroMemory(&saveas, sizeof(saveas));
336 GetCurrentDirectory(SIZEOF(szDir), szDir);
337 lstrcpy(szPath, txt_files);
339 saveas.lStructSize = sizeof(OPENFILENAMEW);
340 saveas.hwndOwner = Globals.hMainWnd;
341 saveas.hInstance = Globals.hInstance;
342 saveas.lpstrFilter = Globals.szFilter;
343 saveas.lpstrFile = szPath;
344 saveas.nMaxFile = SIZEOF(szPath);
345 saveas.lpstrInitialDir = szDir;
346 saveas.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT |
347 OFN_HIDEREADONLY | OFN_ENABLESIZING;
348 saveas.lpstrDefExt = szDefaultExt;
350 if (GetSaveFileName(&saveas)) {
351 SetFileName(szPath);
352 UpdateWindowCaption();
353 DoSaveFile();
354 return TRUE;
356 return FALSE;
359 typedef struct {
360 LPWSTR mptr;
361 LPWSTR mend;
362 LPWSTR lptr;
363 DWORD len;
364 } TEXTINFO, *LPTEXTINFO;
366 static int notepad_print_header(HDC hdc, RECT *rc, BOOL dopage, BOOL header, int page, LPWSTR text)
368 SIZE szMetric;
370 if (*text)
372 /* Write the header or footer */
373 GetTextExtentPoint32(hdc, text, lstrlen(text), &szMetric);
374 if (dopage)
375 ExtTextOut(hdc, (rc->left + rc->right - szMetric.cx) / 2,
376 header ? rc->top : rc->bottom - szMetric.cy,
377 ETO_CLIPPED, rc, text, lstrlen(text), NULL);
378 return 1;
380 return 0;
383 static BOOL notepad_print_page(HDC hdc, RECT *rc, BOOL dopage, int page, LPTEXTINFO tInfo)
385 int b, y;
386 TEXTMETRICW tm;
387 SIZE szMetrics;
389 if (dopage)
391 if (StartPage(hdc) <= 0)
393 static const WCHAR failedW[] = { 'S','t','a','r','t','P','a','g','e',' ','f','a','i','l','e','d',0 };
394 static const WCHAR errorW[] = { 'P','r','i','n','t',' ','E','r','r','o','r',0 };
395 MessageBox(Globals.hMainWnd, failedW, errorW, MB_ICONEXCLAMATION);
396 return FALSE;
400 GetTextMetrics(hdc, &tm);
401 y = rc->top + notepad_print_header(hdc, rc, dopage, TRUE, page, Globals.szFileName) * tm.tmHeight;
402 b = rc->bottom - 2 * notepad_print_header(hdc, rc, FALSE, FALSE, page, Globals.szFooter) * tm.tmHeight;
404 do {
405 INT m, n;
407 if (!tInfo->len)
409 /* find the end of the line */
410 while (tInfo->mptr < tInfo->mend && *tInfo->mptr != '\n' && *tInfo->mptr != '\r')
412 if (*tInfo->mptr == '\t')
414 /* replace tabs with spaces */
415 for (m = 0; m < SPACES_IN_TAB; m++)
417 if (tInfo->len < PRINT_LEN_MAX)
418 tInfo->lptr[tInfo->len++] = ' ';
419 else if (Globals.bWrapLongLines)
420 break;
423 else if (tInfo->len < PRINT_LEN_MAX)
424 tInfo->lptr[tInfo->len++] = *tInfo->mptr;
426 if (tInfo->len >= PRINT_LEN_MAX && Globals.bWrapLongLines)
427 break;
429 tInfo->mptr++;
433 /* Find out how much we should print if line wrapping is enabled */
434 if (Globals.bWrapLongLines)
436 GetTextExtentExPoint(hdc, tInfo->lptr, tInfo->len, rc->right - rc->left, &n, NULL, &szMetrics);
437 if (n < tInfo->len && tInfo->lptr[n] != ' ')
439 m = n;
440 /* Don't wrap words unless it's a single word over the entire line */
441 while (m && tInfo->lptr[m] != ' ') m--;
442 if (m > 0) n = m + 1;
445 else
446 n = tInfo->len;
448 if (dopage)
449 ExtTextOut(hdc, rc->left, y, ETO_CLIPPED, rc, tInfo->lptr, n, NULL);
451 tInfo->len -= n;
453 if (tInfo->len)
455 memcpy(tInfo->lptr, tInfo->lptr + n, tInfo->len * sizeof(WCHAR));
456 y += tm.tmHeight + tm.tmExternalLeading;
458 else
460 /* find the next line */
461 while (tInfo->mptr < tInfo->mend && y < b && (*tInfo->mptr == '\n' || *tInfo->mptr == '\r'))
463 if (*tInfo->mptr == '\n')
464 y += tm.tmHeight + tm.tmExternalLeading;
465 tInfo->mptr++;
468 } while (tInfo->mptr < tInfo->mend && y < b);
470 notepad_print_header(hdc, rc, dopage, FALSE, page, Globals.szFooter);
471 if (dopage)
473 EndPage(hdc);
475 return TRUE;
478 VOID DIALOG_FilePrint(VOID)
480 DOCINFOW di;
481 PRINTDLGW printer;
482 int page, dopage, copy;
483 LOGFONTW lfFont;
484 HFONT hTextFont, old_font = 0;
485 DWORD size;
486 BOOL ret = FALSE;
487 RECT rc;
488 LPWSTR pTemp;
489 TEXTINFO tInfo;
490 WCHAR cTemp[PRINT_LEN_MAX];
492 /* Get Current Settings */
493 ZeroMemory(&printer, sizeof(printer));
494 printer.lStructSize = sizeof(printer);
495 printer.hwndOwner = Globals.hMainWnd;
496 printer.hDevMode = Globals.hDevMode;
497 printer.hDevNames = Globals.hDevNames;
498 printer.hInstance = Globals.hInstance;
500 /* Set some default flags */
501 printer.Flags = PD_RETURNDC | PD_NOSELECTION;
502 printer.nFromPage = 0;
503 printer.nMinPage = 1;
504 /* we really need to calculate number of pages to set nMaxPage and nToPage */
505 printer.nToPage = 0;
506 printer.nMaxPage = -1;
507 /* Let commdlg manage copy settings */
508 printer.nCopies = (WORD)PD_USEDEVMODECOPIES;
510 if (!PrintDlg(&printer)) return;
512 Globals.hDevMode = printer.hDevMode;
513 Globals.hDevNames = printer.hDevNames;
515 SetMapMode(printer.hDC, MM_TEXT);
517 /* initialize DOCINFO */
518 di.cbSize = sizeof(DOCINFOW);
519 di.lpszDocName = Globals.szFileTitle;
520 di.lpszOutput = NULL;
521 di.lpszDatatype = NULL;
522 di.fwType = 0;
524 /* Get the file text */
525 size = GetWindowTextLength(Globals.hEdit) + 1;
526 pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
527 if (!pTemp)
529 DeleteDC(printer.hDC);
530 ShowLastError();
531 return;
533 size = GetWindowText(Globals.hEdit, pTemp, size);
535 if (StartDoc(printer.hDC, &di) > 0)
537 /* Get the page margins in pixels. */
538 rc.top = MulDiv(Globals.iMarginTop, GetDeviceCaps(printer.hDC, LOGPIXELSY), 2540) -
539 GetDeviceCaps(printer.hDC, PHYSICALOFFSETY);
540 rc.bottom = GetDeviceCaps(printer.hDC, PHYSICALHEIGHT) -
541 MulDiv(Globals.iMarginBottom, GetDeviceCaps(printer.hDC, LOGPIXELSY), 2540);
542 rc.left = MulDiv(Globals.iMarginLeft, GetDeviceCaps(printer.hDC, LOGPIXELSX), 2540) -
543 GetDeviceCaps(printer.hDC, PHYSICALOFFSETX);
544 rc.right = GetDeviceCaps(printer.hDC, PHYSICALWIDTH) -
545 MulDiv(Globals.iMarginRight, GetDeviceCaps(printer.hDC, LOGPIXELSX), 2540);
547 /* Create a font for the printer resolution */
548 lfFont = Globals.lfFont;
549 lfFont.lfHeight = MulDiv(lfFont.lfHeight, GetDeviceCaps(printer.hDC, LOGPIXELSY), get_dpi());
550 /* Make the font a bit lighter */
551 lfFont.lfWeight -= 100;
552 hTextFont = CreateFontIndirect(&lfFont);
553 old_font = SelectObject(printer.hDC, hTextFont);
555 for (copy = 1; copy <= printer.nCopies; copy++)
557 page = 1;
559 tInfo.mptr = pTemp;
560 tInfo.mend = pTemp + size;
561 tInfo.lptr = cTemp;
562 tInfo.len = 0;
564 do {
565 if (printer.Flags & PD_PAGENUMS)
567 /* a specific range of pages is selected, so
568 * skip pages that are not to be printed
570 if (page > printer.nToPage)
571 break;
572 else if (page >= printer.nFromPage)
573 dopage = 1;
574 else
575 dopage = 0;
577 else
578 dopage = 1;
580 ret = notepad_print_page(printer.hDC, &rc, dopage, page, &tInfo);
581 page++;
582 } while (ret && tInfo.mptr < tInfo.mend);
584 if (!ret) break;
586 EndDoc(printer.hDC);
587 SelectObject(printer.hDC, old_font);
588 DeleteObject(hTextFont);
590 DeleteDC(printer.hDC);
591 HeapFree(GetProcessHeap(), 0, pTemp);
594 VOID DIALOG_FilePrinterSetup(VOID)
596 PRINTDLGW printer;
598 ZeroMemory(&printer, sizeof(printer));
599 printer.lStructSize = sizeof(printer);
600 printer.hwndOwner = Globals.hMainWnd;
601 printer.hDevMode = Globals.hDevMode;
602 printer.hDevNames = Globals.hDevNames;
603 printer.hInstance = Globals.hInstance;
604 printer.Flags = PD_PRINTSETUP;
605 printer.nCopies = 1;
607 PrintDlg(&printer);
609 Globals.hDevMode = printer.hDevMode;
610 Globals.hDevNames = printer.hDevNames;
613 VOID DIALOG_FileExit(VOID)
615 PostMessage(Globals.hMainWnd, WM_CLOSE, 0, 0l);
618 VOID DIALOG_EditUndo(VOID)
620 SendMessageW(Globals.hEdit, EM_UNDO, 0, 0);
623 VOID DIALOG_EditCut(VOID)
625 SendMessageW(Globals.hEdit, WM_CUT, 0, 0);
628 VOID DIALOG_EditCopy(VOID)
630 SendMessageW(Globals.hEdit, WM_COPY, 0, 0);
633 VOID DIALOG_EditPaste(VOID)
635 SendMessageW(Globals.hEdit, WM_PASTE, 0, 0);
638 VOID DIALOG_EditDelete(VOID)
640 SendMessageW(Globals.hEdit, WM_CLEAR, 0, 0);
643 VOID DIALOG_EditSelectAll(VOID)
645 SendMessageW(Globals.hEdit, EM_SETSEL, 0, -1);
648 VOID DIALOG_EditTimeDate(VOID)
650 SYSTEMTIME st;
651 WCHAR szDate[MAX_STRING_LEN];
652 static const WCHAR spaceW[] = { ' ',0 };
654 GetLocalTime(&st);
656 GetTimeFormat(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &st, NULL, szDate, MAX_STRING_LEN);
657 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szDate);
659 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)spaceW);
661 GetDateFormat(LOCALE_USER_DEFAULT, 0, &st, NULL, szDate, MAX_STRING_LEN);
662 SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szDate);
665 VOID DIALOG_EditWrap(VOID)
667 BOOL modify = FALSE;
668 static const WCHAR editW[] = { 'e','d','i','t',0 };
669 DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL |
670 ES_AUTOVSCROLL | ES_MULTILINE;
671 RECT rc;
672 DWORD size;
673 LPWSTR pTemp;
675 size = GetWindowTextLength(Globals.hEdit) + 1;
676 pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
677 if (!pTemp)
679 ShowLastError();
680 return;
682 GetWindowText(Globals.hEdit, pTemp, size);
683 modify = SendMessageW(Globals.hEdit, EM_GETMODIFY, 0, 0);
684 DestroyWindow(Globals.hEdit);
685 GetClientRect(Globals.hMainWnd, &rc);
686 if( Globals.bWrapLongLines ) dwStyle |= WS_HSCROLL | ES_AUTOHSCROLL;
687 Globals.hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, editW, NULL, dwStyle,
688 0, 0, rc.right, rc.bottom, Globals.hMainWnd,
689 NULL, Globals.hInstance, NULL);
690 SendMessageW(Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, FALSE);
691 SetWindowTextW(Globals.hEdit, pTemp);
692 SendMessageW(Globals.hEdit, EM_SETMODIFY, modify, 0);
693 SetFocus(Globals.hEdit);
694 HeapFree(GetProcessHeap(), 0, pTemp);
696 Globals.bWrapLongLines = !Globals.bWrapLongLines;
697 CheckMenuItem(GetMenu(Globals.hMainWnd), CMD_WRAP,
698 MF_BYCOMMAND | (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED));
701 VOID DIALOG_SelectFont(VOID)
703 CHOOSEFONTW cf;
704 LOGFONTW lf=Globals.lfFont;
706 ZeroMemory( &cf, sizeof(cf) );
707 cf.lStructSize=sizeof(cf);
708 cf.hwndOwner=Globals.hMainWnd;
709 cf.lpLogFont=&lf;
710 cf.Flags=CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT;
712 if( ChooseFont(&cf) )
714 HFONT currfont=Globals.hFont;
716 Globals.hFont=CreateFontIndirect( &lf );
717 Globals.lfFont=lf;
718 SendMessageW( Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, TRUE );
719 if( currfont!=NULL )
720 DeleteObject( currfont );
724 VOID DIALOG_Search(VOID)
726 /* Allow only one search/replace dialog to open */
727 if(Globals.hFindReplaceDlg != NULL)
729 SetActiveWindow(Globals.hFindReplaceDlg);
730 return;
733 ZeroMemory(&Globals.find, sizeof(Globals.find));
734 Globals.find.lStructSize = sizeof(Globals.find);
735 Globals.find.hwndOwner = Globals.hMainWnd;
736 Globals.find.hInstance = Globals.hInstance;
737 Globals.find.lpstrFindWhat = Globals.szFindText;
738 Globals.find.wFindWhatLen = SIZEOF(Globals.szFindText);
739 Globals.find.Flags = FR_DOWN|FR_HIDEWHOLEWORD;
741 /* We only need to create the modal FindReplace dialog which will */
742 /* notify us of incoming events using hMainWnd Window Messages */
744 Globals.hFindReplaceDlg = FindText(&Globals.find);
745 assert(Globals.hFindReplaceDlg !=0);
748 VOID DIALOG_SearchNext(VOID)
750 if (Globals.lastFind.lpstrFindWhat == NULL)
751 DIALOG_Search();
752 else /* use the last find data */
753 NOTEPAD_DoFind(&Globals.lastFind);
756 VOID DIALOG_Replace(VOID)
758 /* Allow only one search/replace dialog to open */
759 if(Globals.hFindReplaceDlg != NULL)
761 SetActiveWindow(Globals.hFindReplaceDlg);
762 return;
765 ZeroMemory(&Globals.find, sizeof(Globals.find));
766 Globals.find.lStructSize = sizeof(Globals.find);
767 Globals.find.hwndOwner = Globals.hMainWnd;
768 Globals.find.hInstance = Globals.hInstance;
769 Globals.find.lpstrFindWhat = Globals.szFindText;
770 Globals.find.wFindWhatLen = SIZEOF(Globals.szFindText);
771 Globals.find.lpstrReplaceWith = Globals.szReplaceText;
772 Globals.find.wReplaceWithLen = SIZEOF(Globals.szReplaceText);
773 Globals.find.Flags = FR_DOWN|FR_HIDEWHOLEWORD;
775 /* We only need to create the modal FindReplace dialog which will */
776 /* notify us of incoming events using hMainWnd Window Messages */
778 Globals.hFindReplaceDlg = ReplaceText(&Globals.find);
779 assert(Globals.hFindReplaceDlg !=0);
782 VOID DIALOG_HelpContents(VOID)
784 WinHelp(Globals.hMainWnd, helpfileW, HELP_INDEX, 0);
787 VOID DIALOG_HelpSearch(VOID)
789 /* Search Help */
792 VOID DIALOG_HelpHelp(VOID)
794 WinHelp(Globals.hMainWnd, helpfileW, HELP_HELPONHELP, 0);
797 VOID DIALOG_HelpAboutNotepad(VOID)
799 static const WCHAR notepadW[] = { 'W','i','n','e',' ','N','o','t','e','p','a','d',0 };
800 WCHAR szNotepad[MAX_STRING_LEN];
801 HICON icon = LoadImageW( Globals.hInstance, MAKEINTRESOURCE(IDI_NOTEPAD),
802 IMAGE_ICON, 48, 48, LR_SHARED );
804 LoadString(Globals.hInstance, STRING_NOTEPAD, szNotepad, SIZEOF(szNotepad));
805 ShellAbout(Globals.hMainWnd, szNotepad, notepadW, icon);
809 /***********************************************************************
811 * DIALOG_FilePageSetup
813 VOID DIALOG_FilePageSetup(void)
815 DialogBox(Globals.hInstance, MAKEINTRESOURCE(DIALOG_PAGESETUP),
816 Globals.hMainWnd, DIALOG_PAGESETUP_DlgProc);
820 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
822 * DIALOG_PAGESETUP_DlgProc
825 static INT_PTR WINAPI DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
828 switch (msg)
830 case WM_COMMAND:
831 switch (wParam)
833 case IDOK:
834 /* save user input and close dialog */
835 GetDlgItemText(hDlg, IDC_PAGESETUP_HEADERVALUE, Globals.szHeader, SIZEOF(Globals.szHeader));
836 GetDlgItemText(hDlg, IDC_PAGESETUP_FOOTERVALUE, Globals.szFooter, SIZEOF(Globals.szFooter));
838 Globals.iMarginTop = GetDlgItemInt(hDlg, IDC_PAGESETUP_TOPVALUE, NULL, FALSE) * 100;
839 Globals.iMarginBottom = GetDlgItemInt(hDlg, IDC_PAGESETUP_BOTTOMVALUE, NULL, FALSE) * 100;
840 Globals.iMarginLeft = GetDlgItemInt(hDlg, IDC_PAGESETUP_LEFTVALUE, NULL, FALSE) * 100;
841 Globals.iMarginRight = GetDlgItemInt(hDlg, IDC_PAGESETUP_RIGHTVALUE, NULL, FALSE) * 100;
842 EndDialog(hDlg, IDOK);
843 return TRUE;
845 case IDCANCEL:
846 /* discard user input and close dialog */
847 EndDialog(hDlg, IDCANCEL);
848 return TRUE;
850 case IDHELP:
852 /* FIXME: Bring this to work */
853 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 };
854 static const WCHAR helpW[] = { 'H','e','l','p',0 };
855 MessageBox(Globals.hMainWnd, sorryW, helpW, MB_ICONEXCLAMATION);
856 return TRUE;
859 default:
860 break;
862 break;
864 case WM_INITDIALOG:
865 /* fetch last user input prior to display dialog */
866 SetDlgItemText(hDlg, IDC_PAGESETUP_HEADERVALUE, Globals.szHeader);
867 SetDlgItemText(hDlg, IDC_PAGESETUP_FOOTERVALUE, Globals.szFooter);
868 SetDlgItemInt(hDlg, IDC_PAGESETUP_TOPVALUE, Globals.iMarginTop / 100, FALSE);
869 SetDlgItemInt(hDlg, IDC_PAGESETUP_BOTTOMVALUE, Globals.iMarginBottom / 100, FALSE);
870 SetDlgItemInt(hDlg, IDC_PAGESETUP_LEFTVALUE, Globals.iMarginLeft / 100, FALSE);
871 SetDlgItemInt(hDlg, IDC_PAGESETUP_RIGHTVALUE, Globals.iMarginRight / 100, FALSE);
872 break;
875 return FALSE;