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
))
167 HeapFree(GetProcessHeap(), 0, pTemp
);
172 * TRUE - User agreed to close (both save/don't save)
173 * FALSE - User cancelled close by selecting "Cancel"
175 BOOL
DoCloseFile(void)
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
);
185 case IDYES
: DIALOG_FileSave();
190 case IDCANCEL
: return(FALSE
);
193 default: return(FALSE
);
198 SetFileName(empty_strW
);
200 UpdateWindowCaption();
205 void DoOpenFile(LPCWSTR szFileName
)
212 /* Close any files and prompt to save changes */
216 hFile
= CreateFile(szFileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
217 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
218 if(hFile
== INVALID_HANDLE_VALUE
)
224 size
= GetFileSize(hFile
, NULL
);
225 if (size
== INVALID_FILE_SIZE
)
233 pTemp
= HeapAlloc(GetProcessHeap(), 0, size
);
241 if (!ReadFile(hFile
, pTemp
, size
, &dwNumRead
, NULL
))
244 HeapFree(GetProcessHeap(), 0, pTemp
);
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
);
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 SetFileName(szFileName
);
269 UpdateWindowCaption();
272 VOID
DIALOG_FileNew(VOID
)
274 static const WCHAR empty_strW
[] = { 0 };
276 /* Close any files and promt to save changes */
278 SetWindowText(Globals
.hEdit
, empty_strW
);
279 SendMessage(Globals
.hEdit
, EM_EMPTYUNDOBUFFER
, 0, 0);
280 SetFocus(Globals
.hEdit
);
284 VOID
DIALOG_FileOpen(VOID
)
286 OPENFILENAME openfilename
;
287 WCHAR szPath
[MAX_PATH
];
288 WCHAR szDir
[MAX_PATH
];
289 static const WCHAR szDefaultExt
[] = { 't','x','t',0 };
290 static const WCHAR txt_files
[] = { '*','.','t','x','t',0 };
292 ZeroMemory(&openfilename
, sizeof(openfilename
));
294 GetCurrentDirectory(SIZEOF(szDir
), szDir
);
295 lstrcpy(szPath
, txt_files
);
297 openfilename
.lStructSize
= sizeof(openfilename
);
298 openfilename
.hwndOwner
= Globals
.hMainWnd
;
299 openfilename
.hInstance
= Globals
.hInstance
;
300 openfilename
.lpstrFilter
= Globals
.szFilter
;
301 openfilename
.lpstrFile
= szPath
;
302 openfilename
.nMaxFile
= SIZEOF(szPath
);
303 openfilename
.lpstrInitialDir
= szDir
;
304 openfilename
.Flags
= OFN_FILEMUSTEXIST
| OFN_PATHMUSTEXIST
|
306 openfilename
.lpstrDefExt
= szDefaultExt
;
309 if (GetOpenFileName(&openfilename
)) {
310 if (FileExists(openfilename
.lpstrFile
))
311 DoOpenFile(openfilename
.lpstrFile
);
313 AlertFileNotFound(openfilename
.lpstrFile
);
318 VOID
DIALOG_FileSave(VOID
)
320 if (Globals
.szFileName
[0] == '\0')
326 VOID
DIALOG_FileSaveAs(VOID
)
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(OPENFILENAME
);
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
|
348 saveas
.lpstrDefExt
= szDefaultExt
;
350 if (GetSaveFileName(&saveas
)) {
352 UpdateWindowCaption();
357 VOID
DIALOG_FilePrint(VOID
)
362 int cWidthPels
, cHeightPels
, border
;
363 int xLeft
, yTop
, i
, pagecount
, dopage
, copycount
;
365 HFONT font
, old_font
=0;
368 static const WCHAR times_new_romanW
[] = { 'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n',0 };
370 /* Get a small font and print some header info on each page */
371 hdrFont
.lfHeight
= 100;
373 hdrFont
.lfEscapement
= 0;
374 hdrFont
.lfOrientation
= 0;
375 hdrFont
.lfWeight
= FW_BOLD
;
376 hdrFont
.lfItalic
= 0;
377 hdrFont
.lfUnderline
= 0;
378 hdrFont
.lfStrikeOut
= 0;
379 hdrFont
.lfCharSet
= ANSI_CHARSET
;
380 hdrFont
.lfOutPrecision
= OUT_DEFAULT_PRECIS
;
381 hdrFont
.lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
382 hdrFont
.lfQuality
= PROOF_QUALITY
;
383 hdrFont
.lfPitchAndFamily
= VARIABLE_PITCH
| FF_ROMAN
;
384 lstrcpy(hdrFont
.lfFaceName
, times_new_romanW
);
386 font
= CreateFontIndirect(&hdrFont
);
388 /* Get Current Settings */
389 ZeroMemory(&printer
, sizeof(printer
));
390 printer
.lStructSize
= sizeof(printer
);
391 printer
.hwndOwner
= Globals
.hMainWnd
;
392 printer
.hInstance
= Globals
.hInstance
;
394 /* Set some default flags */
395 printer
.Flags
= PD_RETURNDC
;
396 printer
.nFromPage
= 0;
397 printer
.nMinPage
= 1;
398 /* we really need to calculate number of pages to set nMaxPage and nToPage */
400 printer
.nMaxPage
= -1;
402 /* Let commdlg manage copy settings */
403 printer
.nCopies
= (WORD
)PD_USEDEVMODECOPIES
;
405 if (!PrintDlg(&printer
)) return;
407 assert(printer
.hDC
!= 0);
409 /* initialize DOCINFO */
410 di
.cbSize
= sizeof(DOCINFO
);
411 di
.lpszDocName
= Globals
.szFileTitle
;
412 di
.lpszOutput
= NULL
;
413 di
.lpszDatatype
= NULL
;
416 if (StartDoc(printer
.hDC
, &di
) <= 0) return;
418 /* Get the page dimensions in pixels. */
419 cWidthPels
= GetDeviceCaps(printer
.hDC
, HORZRES
);
420 cHeightPels
= GetDeviceCaps(printer
.hDC
, VERTRES
);
422 /* Get the file text */
423 size
= GetWindowTextLength(Globals
.hEdit
) + 1;
424 pTemp
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
));
430 size
= GetWindowText(Globals
.hEdit
, pTemp
, size
);
433 for (copycount
=1; copycount
<= printer
.nCopies
; copycount
++) {
437 static const WCHAR letterM
[] = { 'M',0 };
439 if (pagecount
>= printer
.nFromPage
&&
440 /* ((printer.Flags & PD_PAGENUMS) == 0 || pagecount <= printer.nToPage))*/
441 pagecount
<= printer
.nToPage
)
446 old_font
= SelectObject(printer
.hDC
, font
);
447 GetTextExtentPoint32(printer
.hDC
, letterM
, 1, &szMetric
);
450 if (StartPage(printer
.hDC
) <= 0) {
451 static const WCHAR failedW
[] = { 'S','t','a','r','t','P','a','g','e',' ','f','a','i','l','e','d',0 };
452 static const WCHAR errorW
[] = { 'P','r','i','n','t',' ','E','r','r','o','r',0 };
453 MessageBox(Globals
.hMainWnd
, failedW
, errorW
, MB_ICONEXCLAMATION
);
456 /* Write a rectangle and header at the top of each page */
457 Rectangle(printer
.hDC
, border
, border
, cWidthPels
-border
, border
+szMetric
.cy
*2);
458 /* I don't know what's up with this TextOut command. This comes out
461 TextOut(printer
.hDC
, border
*2, border
+szMetric
.cy
/2, Globals
.szFileTitle
, lstrlen(Globals
.szFileTitle
));
464 /* The starting point for the main text */
466 yTop
= border
+szMetric
.cy
*4;
468 SelectObject(printer
.hDC
, old_font
);
469 GetTextExtentPoint32(printer
.hDC
, letterM
, 1, &szMetric
);
471 /* Since outputting strings is giving me problems, output the main
472 text one character at a time.
475 if (pTemp
[i
] == '\n') {
479 else if (pTemp
[i
] != '\r') {
481 TextOut(printer
.hDC
, xLeft
, yTop
, &pTemp
[i
], 1);
482 xLeft
+= szMetric
.cx
;
484 } while (i
++<size
&& yTop
<(cHeightPels
-border
*2));
487 EndPage(printer
.hDC
);
493 DeleteDC(printer
.hDC
);
494 HeapFree(GetProcessHeap(), 0, pTemp
);
497 VOID
DIALOG_FilePrinterSetup(VOID
)
501 ZeroMemory(&printer
, sizeof(printer
));
502 printer
.lStructSize
= sizeof(printer
);
503 printer
.hwndOwner
= Globals
.hMainWnd
;
504 printer
.hInstance
= Globals
.hInstance
;
505 printer
.Flags
= PD_PRINTSETUP
;
511 VOID
DIALOG_FileExit(VOID
)
513 PostMessage(Globals
.hMainWnd
, WM_CLOSE
, 0, 0l);
516 VOID
DIALOG_EditUndo(VOID
)
518 SendMessage(Globals
.hEdit
, EM_UNDO
, 0, 0);
521 VOID
DIALOG_EditCut(VOID
)
523 SendMessage(Globals
.hEdit
, WM_CUT
, 0, 0);
526 VOID
DIALOG_EditCopy(VOID
)
528 SendMessage(Globals
.hEdit
, WM_COPY
, 0, 0);
531 VOID
DIALOG_EditPaste(VOID
)
533 SendMessage(Globals
.hEdit
, WM_PASTE
, 0, 0);
536 VOID
DIALOG_EditDelete(VOID
)
538 SendMessage(Globals
.hEdit
, WM_CLEAR
, 0, 0);
541 VOID
DIALOG_EditSelectAll(VOID
)
543 SendMessage(Globals
.hEdit
, EM_SETSEL
, 0, (LPARAM
)-1);
546 VOID
DIALOG_EditTimeDate(VOID
)
549 WCHAR szDate
[MAX_STRING_LEN
];
550 static const WCHAR spaceW
[] = { ' ',0 };
554 GetTimeFormat(LOCALE_USER_DEFAULT
, 0, &st
, NULL
, szDate
, MAX_STRING_LEN
);
555 SendMessage(Globals
.hEdit
, EM_REPLACESEL
, TRUE
, (LPARAM
)szDate
);
557 SendMessage(Globals
.hEdit
, EM_REPLACESEL
, TRUE
, (LPARAM
)spaceW
);
559 GetDateFormat(LOCALE_USER_DEFAULT
, DATE_LONGDATE
, &st
, NULL
, szDate
, MAX_STRING_LEN
);
560 SendMessage(Globals
.hEdit
, EM_REPLACESEL
, TRUE
, (LPARAM
)szDate
);
563 VOID
DIALOG_EditWrap(VOID
)
565 Globals
.bWrapLongLines
= !Globals
.bWrapLongLines
;
566 CheckMenuItem(GetMenu(Globals
.hMainWnd
), CMD_WRAP
,
567 MF_BYCOMMAND
| (Globals
.bWrapLongLines
? MF_CHECKED
: MF_UNCHECKED
));
570 VOID
DIALOG_SelectFont(VOID
)
573 LOGFONT lf
=Globals
.lfFont
;
575 ZeroMemory( &cf
, sizeof(cf
) );
576 cf
.lStructSize
=sizeof(cf
);
577 cf
.hwndOwner
=Globals
.hMainWnd
;
579 cf
.Flags
=CF_SCREENFONTS
;
581 if( ChooseFont(&cf
) )
583 HFONT currfont
=Globals
.hFont
;
585 Globals
.hFont
=CreateFontIndirect( &lf
);
587 SendMessage( Globals
.hEdit
, WM_SETFONT
, (WPARAM
)Globals
.hFont
, (LPARAM
)TRUE
);
589 DeleteObject( currfont
);
593 VOID
DIALOG_Search(VOID
)
595 ZeroMemory(&Globals
.find
, sizeof(Globals
.find
));
596 Globals
.find
.lStructSize
= sizeof(Globals
.find
);
597 Globals
.find
.hwndOwner
= Globals
.hMainWnd
;
598 Globals
.find
.hInstance
= Globals
.hInstance
;
599 Globals
.find
.lpstrFindWhat
= Globals
.szFindText
;
600 Globals
.find
.wFindWhatLen
= SIZEOF(Globals
.szFindText
);
601 Globals
.find
.Flags
= FR_DOWN
;
603 /* We only need to create the modal FindReplace dialog which will */
604 /* notify us of incoming events using hMainWnd Window Messages */
606 Globals
.hFindReplaceDlg
= FindText(&Globals
.find
);
607 assert(Globals
.hFindReplaceDlg
!=0);
610 VOID
DIALOG_SearchNext(VOID
)
612 /* FIXME: Search Next */
616 VOID
DIALOG_HelpContents(VOID
)
618 WinHelp(Globals
.hMainWnd
, helpfileW
, HELP_INDEX
, 0);
621 VOID
DIALOG_HelpSearch(VOID
)
626 VOID
DIALOG_HelpHelp(VOID
)
628 WinHelp(Globals
.hMainWnd
, helpfileW
, HELP_HELPONHELP
, 0);
631 VOID
DIALOG_HelpLicense(VOID
)
633 WineLicense(Globals
.hMainWnd
);
636 VOID
DIALOG_HelpNoWarranty(VOID
)
638 WineWarranty(Globals
.hMainWnd
);
641 VOID
DIALOG_HelpAboutWine(VOID
)
643 static const WCHAR notepadW
[] = { 'N','o','t','e','p','a','d','\n',0 };
644 WCHAR szNotepad
[MAX_STRING_LEN
];
646 LoadString(Globals
.hInstance
, STRING_NOTEPAD
, szNotepad
, SIZEOF(szNotepad
));
647 ShellAbout(Globals
.hMainWnd
, szNotepad
, notepadW
, 0);
651 /***********************************************************************
653 * DIALOG_FilePageSetup
655 VOID
DIALOG_FilePageSetup(void)
657 DialogBox(Globals
.hInstance
, MAKEINTRESOURCE(DIALOG_PAGESETUP
),
658 Globals
.hMainWnd
, DIALOG_PAGESETUP_DlgProc
);
662 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
664 * DIALOG_PAGESETUP_DlgProc
667 static INT_PTR WINAPI
DIALOG_PAGESETUP_DlgProc(HWND hDlg
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
676 /* save user input and close dialog */
677 GetDlgItemText(hDlg
, 0x141, Globals
.szHeader
, SIZEOF(Globals
.szHeader
));
678 GetDlgItemText(hDlg
, 0x143, Globals
.szFooter
, SIZEOF(Globals
.szFooter
));
679 GetDlgItemText(hDlg
, 0x14A, Globals
.szMarginTop
, SIZEOF(Globals
.szMarginTop
));
680 GetDlgItemText(hDlg
, 0x150, Globals
.szMarginBottom
, SIZEOF(Globals
.szMarginBottom
));
681 GetDlgItemText(hDlg
, 0x147, Globals
.szMarginLeft
, SIZEOF(Globals
.szMarginLeft
));
682 GetDlgItemText(hDlg
, 0x14D, Globals
.szMarginRight
, SIZEOF(Globals
.szMarginRight
));
683 EndDialog(hDlg
, IDOK
);
687 /* discard user input and close dialog */
688 EndDialog(hDlg
, IDCANCEL
);
693 /* FIXME: Bring this to work */
694 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 };
695 static const WCHAR helpW
[] = { 'H','e','l','p',0 };
696 MessageBox(Globals
.hMainWnd
, sorryW
, helpW
, MB_ICONEXCLAMATION
);
706 /* fetch last user input prior to display dialog */
707 SetDlgItemText(hDlg
, 0x141, Globals
.szHeader
);
708 SetDlgItemText(hDlg
, 0x143, Globals
.szFooter
);
709 SetDlgItemText(hDlg
, 0x14A, Globals
.szMarginTop
);
710 SetDlgItemText(hDlg
, 0x150, Globals
.szMarginBottom
);
711 SetDlgItemText(hDlg
, 0x147, Globals
.szMarginLeft
);
712 SetDlgItemText(hDlg
, 0x14D, Globals
.szMarginRight
);