4 * Copyright 1998,99 Marcel Baur <mbaur@g26.ethz.ch>
5 * Copyright 2002 Sylvain Petreolle <spetreolle@yahoo.fr>
6 * Copyright 2002 Andriy Palamarchuk
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 static const WCHAR helpfileW
[] = { 'n','o','t','e','p','a','d','.','h','l','p',0 };
36 static INT_PTR WINAPI
DIALOG_PAGESETUP_DlgProc(HWND hDlg
, UINT msg
, WPARAM wParam
, LPARAM lParam
);
38 VOID
ShowLastError(void)
40 DWORD error
= GetLastError();
41 if (error
!= NO_ERROR
)
44 WCHAR szTitle
[MAX_STRING_LEN
];
46 LoadString(Globals
.hInstance
, STRING_ERROR
, szTitle
, SIZEOF(szTitle
));
48 FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
50 (LPTSTR
) &lpMsgBuf
, 0, NULL
);
51 MessageBox(NULL
, lpMsgBuf
, szTitle
, MB_OK
| MB_ICONERROR
);
57 * Sets the caption of the main window according to Globals.szFileTitle:
58 * Notepad - (untitled) if no file is open
59 * Notepad - [filename] if a file is given
61 static void UpdateWindowCaption(void)
63 WCHAR szCaption
[MAX_STRING_LEN
];
64 WCHAR szUntitled
[MAX_STRING_LEN
];
66 LoadString(Globals
.hInstance
, STRING_NOTEPAD
, szCaption
, SIZEOF(szCaption
));
68 if (Globals
.szFileTitle
[0] != '\0') {
69 static const WCHAR bracket_lW
[] = { ' ','-',' ','[',0 };
70 static const WCHAR bracket_rW
[] = { ']',0 };
71 lstrcat(szCaption
, bracket_lW
);
72 lstrcat(szCaption
, Globals
.szFileTitle
);
73 lstrcat(szCaption
, bracket_rW
);
77 static const WCHAR hyphenW
[] = { ' ','-',' ',0 };
78 LoadString(Globals
.hInstance
, STRING_UNTITLED
, szUntitled
, SIZEOF(szUntitled
));
79 lstrcat(szCaption
, hyphenW
);
80 lstrcat(szCaption
, szUntitled
);
83 SetWindowText(Globals
.hMainWnd
, szCaption
);
86 static void AlertFileNotFound(LPCWSTR szFileName
)
88 WCHAR szMessage
[MAX_STRING_LEN
];
89 WCHAR szResource
[MAX_STRING_LEN
];
91 /* Load and format szMessage */
92 LoadString(Globals
.hInstance
, STRING_NOTFOUND
, szResource
, SIZEOF(szResource
));
93 wsprintf(szMessage
, szResource
, szFileName
);
96 LoadString(Globals
.hInstance
, STRING_ERROR
, szResource
, SIZEOF(szResource
));
98 /* Display Modal Dialog */
99 MessageBox(Globals
.hMainWnd
, szMessage
, szResource
, MB_ICONEXCLAMATION
);
102 static int AlertFileNotSaved(LPCWSTR szFileName
)
104 WCHAR szMessage
[MAX_STRING_LEN
];
105 WCHAR szResource
[MAX_STRING_LEN
];
106 WCHAR szUntitled
[MAX_STRING_LEN
];
108 LoadString(Globals
.hInstance
, STRING_UNTITLED
, szUntitled
, SIZEOF(szUntitled
));
110 /* Load and format Message */
111 LoadString(Globals
.hInstance
, STRING_NOTSAVED
, szResource
, SIZEOF(szResource
));
112 wsprintf(szMessage
, szResource
, szFileName
[0] ? szFileName
: szUntitled
);
115 LoadString(Globals
.hInstance
, STRING_ERROR
, szResource
, SIZEOF(szResource
));
118 return MessageBox(Globals
.hMainWnd
, szMessage
, szResource
, MB_ICONEXCLAMATION
|MB_YESNOCANCEL
);
123 * TRUE - if file exists
124 * FALSE - if file does not exist
126 BOOL
FileExists(LPCWSTR szFilename
)
128 WIN32_FIND_DATA entry
;
131 hFile
= FindFirstFile(szFilename
, &entry
);
134 return (hFile
!= INVALID_HANDLE_VALUE
);
138 static VOID
DoSaveFile(VOID
)
145 hFile
= CreateFile(Globals
.szFileName
, GENERIC_WRITE
, FILE_SHARE_WRITE
,
146 NULL
, OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
147 if(hFile
== INVALID_HANDLE_VALUE
)
153 size
= GetWindowTextLengthA(Globals
.hEdit
) + 1;
154 pTemp
= HeapAlloc(GetProcessHeap(), 0, size
);
161 size
= GetWindowTextA(Globals
.hEdit
, pTemp
, size
);
163 if (!WriteFile(hFile
, pTemp
, size
, &dwNumWrite
, NULL
))
166 SendMessage(Globals
.hEdit
, EM_SETMODIFY
, FALSE
, 0);
169 HeapFree(GetProcessHeap(), 0, pTemp
);
174 * TRUE - User agreed to close (both save/don't save)
175 * FALSE - User cancelled close by selecting "Cancel"
177 BOOL
DoCloseFile(void)
180 static const WCHAR empty_strW
[] = { 0 };
182 if (SendMessage(Globals
.hEdit
, EM_GETMODIFY
, 0, 0))
184 /* prompt user to save changes */
185 nResult
= AlertFileNotSaved(Globals
.szFileName
);
187 case IDYES
: DIALOG_FileSave();
192 case IDCANCEL
: return(FALSE
);
195 default: return(FALSE
);
200 SetFileName(empty_strW
);
202 UpdateWindowCaption();
207 void DoOpenFile(LPCWSTR szFileName
)
214 /* Close any files and prompt to save changes */
218 hFile
= CreateFile(szFileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
219 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
220 if(hFile
== INVALID_HANDLE_VALUE
)
226 size
= GetFileSize(hFile
, NULL
);
227 if (size
== INVALID_FILE_SIZE
)
235 pTemp
= HeapAlloc(GetProcessHeap(), 0, size
);
243 if (!ReadFile(hFile
, pTemp
, size
, &dwNumRead
, NULL
))
246 HeapFree(GetProcessHeap(), 0, pTemp
);
252 pTemp
[dwNumRead
] = 0;
254 if (IsTextUnicode(pTemp
, dwNumRead
, NULL
))
256 LPWSTR p
= (LPWSTR
)pTemp
;
257 /* We need to strip BOM Unicode character, SetWindowTextW won't do it for us. */
258 if (*p
== 0xFEFF || *p
== 0xFFFE) p
++;
259 SetWindowTextW(Globals
.hEdit
, p
);
262 SetWindowTextA(Globals
.hEdit
, pTemp
);
264 HeapFree(GetProcessHeap(), 0, pTemp
);
266 SendMessage(Globals
.hEdit
, EM_SETMODIFY
, FALSE
, 0);
267 SendMessage(Globals
.hEdit
, EM_EMPTYUNDOBUFFER
, 0, 0);
268 SetFocus(Globals
.hEdit
);
270 SetFileName(szFileName
);
271 UpdateWindowCaption();
274 VOID
DIALOG_FileNew(VOID
)
276 static const WCHAR empty_strW
[] = { 0 };
278 /* Close any files and promt to save changes */
280 SetWindowText(Globals
.hEdit
, empty_strW
);
281 SendMessage(Globals
.hEdit
, EM_EMPTYUNDOBUFFER
, 0, 0);
282 SetFocus(Globals
.hEdit
);
286 VOID
DIALOG_FileOpen(VOID
)
288 OPENFILENAME openfilename
;
289 WCHAR szPath
[MAX_PATH
];
290 WCHAR szDir
[MAX_PATH
];
291 static const WCHAR szDefaultExt
[] = { 't','x','t',0 };
292 static const WCHAR txt_files
[] = { '*','.','t','x','t',0 };
294 ZeroMemory(&openfilename
, sizeof(openfilename
));
296 GetCurrentDirectory(SIZEOF(szDir
), szDir
);
297 lstrcpy(szPath
, txt_files
);
299 openfilename
.lStructSize
= sizeof(openfilename
);
300 openfilename
.hwndOwner
= Globals
.hMainWnd
;
301 openfilename
.hInstance
= Globals
.hInstance
;
302 openfilename
.lpstrFilter
= Globals
.szFilter
;
303 openfilename
.lpstrFile
= szPath
;
304 openfilename
.nMaxFile
= SIZEOF(szPath
);
305 openfilename
.lpstrInitialDir
= szDir
;
306 openfilename
.Flags
= OFN_FILEMUSTEXIST
| OFN_PATHMUSTEXIST
|
308 openfilename
.lpstrDefExt
= szDefaultExt
;
311 if (GetOpenFileName(&openfilename
)) {
312 if (FileExists(openfilename
.lpstrFile
))
313 DoOpenFile(openfilename
.lpstrFile
);
315 AlertFileNotFound(openfilename
.lpstrFile
);
320 VOID
DIALOG_FileSave(VOID
)
322 if (Globals
.szFileName
[0] == '\0')
328 VOID
DIALOG_FileSaveAs(VOID
)
331 WCHAR szPath
[MAX_PATH
];
332 WCHAR szDir
[MAX_PATH
];
333 static const WCHAR szDefaultExt
[] = { 't','x','t',0 };
334 static const WCHAR txt_files
[] = { '*','.','t','x','t',0 };
336 ZeroMemory(&saveas
, sizeof(saveas
));
338 GetCurrentDirectory(SIZEOF(szDir
), szDir
);
339 lstrcpy(szPath
, txt_files
);
341 saveas
.lStructSize
= sizeof(OPENFILENAME
);
342 saveas
.hwndOwner
= Globals
.hMainWnd
;
343 saveas
.hInstance
= Globals
.hInstance
;
344 saveas
.lpstrFilter
= Globals
.szFilter
;
345 saveas
.lpstrFile
= szPath
;
346 saveas
.nMaxFile
= SIZEOF(szPath
);
347 saveas
.lpstrInitialDir
= szDir
;
348 saveas
.Flags
= OFN_PATHMUSTEXIST
| OFN_OVERWRITEPROMPT
|
350 saveas
.lpstrDefExt
= szDefaultExt
;
352 if (GetSaveFileName(&saveas
)) {
354 UpdateWindowCaption();
359 VOID
DIALOG_FilePrint(VOID
)
364 int cWidthPels
, cHeightPels
, border
;
365 int xLeft
, yTop
, pagecount
, dopage
, copycount
;
368 HFONT font
, old_font
=0;
371 static const WCHAR times_new_romanW
[] = { 'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n',0 };
373 /* Get a small font and print some header info on each page */
374 hdrFont
.lfHeight
= 100;
376 hdrFont
.lfEscapement
= 0;
377 hdrFont
.lfOrientation
= 0;
378 hdrFont
.lfWeight
= FW_BOLD
;
379 hdrFont
.lfItalic
= 0;
380 hdrFont
.lfUnderline
= 0;
381 hdrFont
.lfStrikeOut
= 0;
382 hdrFont
.lfCharSet
= ANSI_CHARSET
;
383 hdrFont
.lfOutPrecision
= OUT_DEFAULT_PRECIS
;
384 hdrFont
.lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
385 hdrFont
.lfQuality
= PROOF_QUALITY
;
386 hdrFont
.lfPitchAndFamily
= VARIABLE_PITCH
| FF_ROMAN
;
387 lstrcpy(hdrFont
.lfFaceName
, times_new_romanW
);
389 font
= CreateFontIndirect(&hdrFont
);
391 /* Get Current Settings */
392 ZeroMemory(&printer
, sizeof(printer
));
393 printer
.lStructSize
= sizeof(printer
);
394 printer
.hwndOwner
= Globals
.hMainWnd
;
395 printer
.hInstance
= Globals
.hInstance
;
397 /* Set some default flags */
398 printer
.Flags
= PD_RETURNDC
;
399 printer
.nFromPage
= 0;
400 printer
.nMinPage
= 1;
401 /* we really need to calculate number of pages to set nMaxPage and nToPage */
403 printer
.nMaxPage
= -1;
405 /* Let commdlg manage copy settings */
406 printer
.nCopies
= (WORD
)PD_USEDEVMODECOPIES
;
408 if (!PrintDlg(&printer
)) return;
410 assert(printer
.hDC
!= 0);
412 /* initialize DOCINFO */
413 di
.cbSize
= sizeof(DOCINFO
);
414 di
.lpszDocName
= Globals
.szFileTitle
;
415 di
.lpszOutput
= NULL
;
416 di
.lpszDatatype
= NULL
;
419 if (StartDoc(printer
.hDC
, &di
) <= 0) return;
421 /* Get the page dimensions in pixels. */
422 cWidthPels
= GetDeviceCaps(printer
.hDC
, HORZRES
);
423 cHeightPels
= GetDeviceCaps(printer
.hDC
, VERTRES
);
425 /* Get the file text */
426 size
= GetWindowTextLength(Globals
.hEdit
) + 1;
427 pTemp
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
));
433 size
= GetWindowText(Globals
.hEdit
, pTemp
, size
);
436 for (copycount
=1; copycount
<= printer
.nCopies
; copycount
++) {
440 static const WCHAR letterM
[] = { 'M',0 };
442 if (pagecount
>= printer
.nFromPage
&&
443 /* ((printer.Flags & PD_PAGENUMS) == 0 || pagecount <= printer.nToPage))*/
444 pagecount
<= printer
.nToPage
)
449 old_font
= SelectObject(printer
.hDC
, font
);
450 GetTextExtentPoint32(printer
.hDC
, letterM
, 1, &szMetric
);
453 if (StartPage(printer
.hDC
) <= 0) {
454 static const WCHAR failedW
[] = { 'S','t','a','r','t','P','a','g','e',' ','f','a','i','l','e','d',0 };
455 static const WCHAR errorW
[] = { 'P','r','i','n','t',' ','E','r','r','o','r',0 };
456 MessageBox(Globals
.hMainWnd
, failedW
, errorW
, MB_ICONEXCLAMATION
);
459 /* Write a rectangle and header at the top of each page */
460 Rectangle(printer
.hDC
, border
, border
, cWidthPels
-border
, border
+szMetric
.cy
*2);
461 /* I don't know what's up with this TextOut command. This comes out
464 TextOut(printer
.hDC
, border
*2, border
+szMetric
.cy
/2, Globals
.szFileTitle
, lstrlen(Globals
.szFileTitle
));
467 /* The starting point for the main text */
469 yTop
= border
+szMetric
.cy
*4;
471 SelectObject(printer
.hDC
, old_font
);
472 GetTextExtentPoint32(printer
.hDC
, letterM
, 1, &szMetric
);
474 /* Since outputting strings is giving me problems, output the main
475 text one character at a time.
478 if (pTemp
[i
] == '\n') {
482 else if (pTemp
[i
] != '\r') {
484 TextOut(printer
.hDC
, xLeft
, yTop
, &pTemp
[i
], 1);
485 xLeft
+= szMetric
.cx
;
487 } while (i
++<size
&& yTop
<(cHeightPels
-border
*2));
490 EndPage(printer
.hDC
);
496 DeleteDC(printer
.hDC
);
497 HeapFree(GetProcessHeap(), 0, pTemp
);
500 VOID
DIALOG_FilePrinterSetup(VOID
)
504 ZeroMemory(&printer
, sizeof(printer
));
505 printer
.lStructSize
= sizeof(printer
);
506 printer
.hwndOwner
= Globals
.hMainWnd
;
507 printer
.hInstance
= Globals
.hInstance
;
508 printer
.Flags
= PD_PRINTSETUP
;
514 VOID
DIALOG_FileExit(VOID
)
516 PostMessage(Globals
.hMainWnd
, WM_CLOSE
, 0, 0l);
519 VOID
DIALOG_EditUndo(VOID
)
521 SendMessage(Globals
.hEdit
, EM_UNDO
, 0, 0);
524 VOID
DIALOG_EditCut(VOID
)
526 SendMessage(Globals
.hEdit
, WM_CUT
, 0, 0);
529 VOID
DIALOG_EditCopy(VOID
)
531 SendMessage(Globals
.hEdit
, WM_COPY
, 0, 0);
534 VOID
DIALOG_EditPaste(VOID
)
536 SendMessage(Globals
.hEdit
, WM_PASTE
, 0, 0);
539 VOID
DIALOG_EditDelete(VOID
)
541 SendMessage(Globals
.hEdit
, WM_CLEAR
, 0, 0);
544 VOID
DIALOG_EditSelectAll(VOID
)
546 SendMessage(Globals
.hEdit
, EM_SETSEL
, 0, (LPARAM
)-1);
549 VOID
DIALOG_EditTimeDate(VOID
)
552 WCHAR szDate
[MAX_STRING_LEN
];
553 static const WCHAR spaceW
[] = { ' ',0 };
557 GetTimeFormat(LOCALE_USER_DEFAULT
, 0, &st
, NULL
, szDate
, MAX_STRING_LEN
);
558 SendMessage(Globals
.hEdit
, EM_REPLACESEL
, TRUE
, (LPARAM
)szDate
);
560 SendMessage(Globals
.hEdit
, EM_REPLACESEL
, TRUE
, (LPARAM
)spaceW
);
562 GetDateFormat(LOCALE_USER_DEFAULT
, DATE_LONGDATE
, &st
, NULL
, szDate
, MAX_STRING_LEN
);
563 SendMessage(Globals
.hEdit
, EM_REPLACESEL
, TRUE
, (LPARAM
)szDate
);
566 VOID
DIALOG_EditWrap(VOID
)
568 Globals
.bWrapLongLines
= !Globals
.bWrapLongLines
;
569 CheckMenuItem(GetMenu(Globals
.hMainWnd
), CMD_WRAP
,
570 MF_BYCOMMAND
| (Globals
.bWrapLongLines
? MF_CHECKED
: MF_UNCHECKED
));
573 VOID
DIALOG_SelectFont(VOID
)
576 LOGFONT lf
=Globals
.lfFont
;
578 ZeroMemory( &cf
, sizeof(cf
) );
579 cf
.lStructSize
=sizeof(cf
);
580 cf
.hwndOwner
=Globals
.hMainWnd
;
582 cf
.Flags
=CF_SCREENFONTS
;
584 if( ChooseFont(&cf
) )
586 HFONT currfont
=Globals
.hFont
;
588 Globals
.hFont
=CreateFontIndirect( &lf
);
590 SendMessage( Globals
.hEdit
, WM_SETFONT
, (WPARAM
)Globals
.hFont
, (LPARAM
)TRUE
);
592 DeleteObject( currfont
);
596 VOID
DIALOG_Search(VOID
)
598 ZeroMemory(&Globals
.find
, sizeof(Globals
.find
));
599 Globals
.find
.lStructSize
= sizeof(Globals
.find
);
600 Globals
.find
.hwndOwner
= Globals
.hMainWnd
;
601 Globals
.find
.hInstance
= Globals
.hInstance
;
602 Globals
.find
.lpstrFindWhat
= Globals
.szFindText
;
603 Globals
.find
.wFindWhatLen
= SIZEOF(Globals
.szFindText
);
604 Globals
.find
.Flags
= FR_DOWN
;
606 /* We only need to create the modal FindReplace dialog which will */
607 /* notify us of incoming events using hMainWnd Window Messages */
609 Globals
.hFindReplaceDlg
= FindText(&Globals
.find
);
610 assert(Globals
.hFindReplaceDlg
!=0);
613 VOID
DIALOG_SearchNext(VOID
)
615 /* FIXME: Search Next */
619 VOID
DIALOG_HelpContents(VOID
)
621 WinHelp(Globals
.hMainWnd
, helpfileW
, HELP_INDEX
, 0);
624 VOID
DIALOG_HelpSearch(VOID
)
629 VOID
DIALOG_HelpHelp(VOID
)
631 WinHelp(Globals
.hMainWnd
, helpfileW
, HELP_HELPONHELP
, 0);
634 VOID
DIALOG_HelpLicense(VOID
)
636 WineLicense(Globals
.hMainWnd
);
639 VOID
DIALOG_HelpNoWarranty(VOID
)
641 WineWarranty(Globals
.hMainWnd
);
644 VOID
DIALOG_HelpAboutWine(VOID
)
646 static const WCHAR notepadW
[] = { 'N','o','t','e','p','a','d','\n',0 };
647 WCHAR szNotepad
[MAX_STRING_LEN
];
649 LoadString(Globals
.hInstance
, STRING_NOTEPAD
, szNotepad
, SIZEOF(szNotepad
));
650 ShellAbout(Globals
.hMainWnd
, szNotepad
, notepadW
, 0);
654 /***********************************************************************
656 * DIALOG_FilePageSetup
658 VOID
DIALOG_FilePageSetup(void)
660 DialogBox(Globals
.hInstance
, MAKEINTRESOURCE(DIALOG_PAGESETUP
),
661 Globals
.hMainWnd
, DIALOG_PAGESETUP_DlgProc
);
665 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
667 * DIALOG_PAGESETUP_DlgProc
670 static INT_PTR WINAPI
DIALOG_PAGESETUP_DlgProc(HWND hDlg
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
679 /* save user input and close dialog */
680 GetDlgItemText(hDlg
, 0x141, Globals
.szHeader
, SIZEOF(Globals
.szHeader
));
681 GetDlgItemText(hDlg
, 0x143, Globals
.szFooter
, SIZEOF(Globals
.szFooter
));
682 GetDlgItemText(hDlg
, 0x14A, Globals
.szMarginTop
, SIZEOF(Globals
.szMarginTop
));
683 GetDlgItemText(hDlg
, 0x150, Globals
.szMarginBottom
, SIZEOF(Globals
.szMarginBottom
));
684 GetDlgItemText(hDlg
, 0x147, Globals
.szMarginLeft
, SIZEOF(Globals
.szMarginLeft
));
685 GetDlgItemText(hDlg
, 0x14D, Globals
.szMarginRight
, SIZEOF(Globals
.szMarginRight
));
686 EndDialog(hDlg
, IDOK
);
690 /* discard user input and close dialog */
691 EndDialog(hDlg
, IDCANCEL
);
696 /* FIXME: Bring this to work */
697 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 };
698 static const WCHAR helpW
[] = { 'H','e','l','p',0 };
699 MessageBox(Globals
.hMainWnd
, sorryW
, helpW
, MB_ICONEXCLAMATION
);
709 /* fetch last user input prior to display dialog */
710 SetDlgItemText(hDlg
, 0x141, Globals
.szHeader
);
711 SetDlgItemText(hDlg
, 0x143, Globals
.szFooter
);
712 SetDlgItemText(hDlg
, 0x14A, Globals
.szMarginTop
);
713 SetDlgItemText(hDlg
, 0x150, Globals
.szMarginBottom
);
714 SetDlgItemText(hDlg
, 0x147, Globals
.szMarginLeft
);
715 SetDlgItemText(hDlg
, 0x14D, Globals
.szMarginRight
);