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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 LRESULT CALLBACK
EDIT_WndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
);
31 static const TCHAR helpfile
[] = _T("notepad.hlp");
32 static const TCHAR empty_str
[] = _T("");
33 static const TCHAR szDefaultExt
[] = _T("txt");
34 static const TCHAR txt_files
[] = _T("*.txt");
36 static INT_PTR WINAPI
DIALOG_PAGESETUP_DlgProc(HWND hDlg
, UINT msg
, WPARAM wParam
, LPARAM lParam
);
39 static LPSTR
ConvertToASCII(LPSTR pszText
)
42 LPWSTR pszTextW
= (LPWSTR
)pszText
;
44 /* default return value */
47 /* query about requested size for conversion */
48 sz
= WideCharToMultiByte(CP_ACP
, 0, pszTextW
, -1, NULL
, 0, NULL
, NULL
);
52 /* get space for ASCII buffer */
53 pszText
= (LPSTR
)HeapAlloc(GetProcessHeap(), 0, sz
);
57 /* if previous diagnostic call worked fine,
58 * then this one will work too,
59 * so no need to test return value here
61 WideCharToMultiByte(CP_ACP
, 0, pszTextW
, -1, pszText
, sz
, NULL
, NULL
);
64 HeapFree(GetProcessHeap(), 0, pszTextW
);
68 static LPWSTR
ConvertToUNICODE(LPSTR pszText
, DWORD
*pdwSize
)
71 LPWSTR pszTextW
= NULL
;
74 /* query about requested size for conversion */
75 sz
= MultiByteToWideChar(CP_ACP
, 0, pszText
, -1, NULL
, 0);
79 /* get space for UNICODE buffer */
80 pszTextW
= HeapAlloc(GetProcessHeap(), 0, sz
*sizeof(WCHAR
));
84 /* if previous diagnostic call worked fine,
85 * then this one will work too,
86 * so no need to test return value here
88 MultiByteToWideChar(CP_ACP
, 0, pszText
, -1, pszTextW
, sz
);
90 /* report the new size of the text to the caller */
94 HeapFree(GetProcessHeap(), 0, pszText
);
99 VOID
ShowLastError(VOID
)
101 DWORD error
= GetLastError();
102 if (error
!= NO_ERROR
)
104 LPTSTR lpMsgBuf
= NULL
;
105 TCHAR szTitle
[MAX_STRING_LEN
];
107 LoadString(Globals
.hInstance
, STRING_ERROR
, szTitle
, SIZEOF(szTitle
));
109 FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
111 (LPTSTR
) &lpMsgBuf
, 0, NULL
);
112 MessageBox(NULL
, lpMsgBuf
, szTitle
, MB_OK
| MB_ICONERROR
);
118 * Sets the caption of the main window according to Globals.szFileTitle:
119 * (untitled) - Notepad if no file is open
120 * [filename] - Notepad if a file is given
122 static void UpdateWindowCaption(void)
124 TCHAR szCaption
[MAX_STRING_LEN
];
125 TCHAR szNotepad
[MAX_STRING_LEN
];
127 LoadString(Globals
.hInstance
, STRING_NOTEPAD
, szNotepad
, SIZEOF(szNotepad
));
129 if (Globals
.szFileTitle
[0] != 0)
131 StringCchCopy(szCaption
, SIZEOF(szCaption
), Globals
.szFileTitle
);
135 LoadString(Globals
.hInstance
, STRING_UNTITLED
, szCaption
, SIZEOF(szCaption
));
138 StringCchCat(szCaption
, SIZEOF(szCaption
), _T(" - "));
139 StringCchCat(szCaption
, SIZEOF(szCaption
), szNotepad
);
140 SetWindowText(Globals
.hMainWnd
, szCaption
);
143 static void AlertFileNotFound(LPCTSTR szFileName
)
145 TCHAR szMessage
[MAX_STRING_LEN
];
146 TCHAR szResource
[MAX_STRING_LEN
];
148 /* Load and format szMessage */
149 LoadString(Globals
.hInstance
, STRING_NOTFOUND
, szResource
, SIZEOF(szResource
));
150 wsprintf(szMessage
, szResource
, szFileName
);
153 LoadString(Globals
.hInstance
, STRING_NOTEPAD
, szResource
, SIZEOF(szResource
));
155 /* Display Modal Dialog */
156 MessageBox(Globals
.hMainWnd
, szMessage
, szResource
, MB_ICONEXCLAMATION
);
159 static int AlertFileNotSaved(LPCTSTR szFileName
)
161 TCHAR szMessage
[MAX_STRING_LEN
];
162 TCHAR szResource
[MAX_STRING_LEN
];
163 TCHAR szUntitled
[MAX_STRING_LEN
];
165 LoadString(Globals
.hInstance
, STRING_UNTITLED
, szUntitled
, SIZEOF(szUntitled
));
167 /* Load and format Message */
168 LoadString(Globals
.hInstance
, STRING_NOTSAVED
, szResource
, SIZEOF(szResource
));
169 wsprintf(szMessage
, szResource
, szFileName
[0] ? szFileName
: szUntitled
);
172 LoadString(Globals
.hInstance
, STRING_NOTEPAD
, szResource
, SIZEOF(szResource
));
175 return MessageBox(Globals
.hMainWnd
, szMessage
, szResource
, MB_ICONEXCLAMATION
|MB_YESNOCANCEL
);
180 * TRUE - if file exists
181 * FALSE - if file does not exist
183 BOOL
FileExists(LPCTSTR szFilename
)
185 WIN32_FIND_DATA entry
;
188 hFile
= FindFirstFile(szFilename
, &entry
);
191 return (hFile
!= INVALID_HANDLE_VALUE
);
194 BOOL
HasFileExtension(LPCTSTR szFilename
)
198 s
= _tcsrchr(szFilename
, _T('\\'));
201 return _tcsrchr(szFilename
, _T('.')) != NULL
;
204 static BOOL
DoSaveFile(VOID
)
211 hFile
= CreateFile(Globals
.szFileName
, GENERIC_WRITE
, FILE_SHARE_WRITE
,
212 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
213 if(hFile
== INVALID_HANDLE_VALUE
)
219 size
= GetWindowTextLength(Globals
.hEdit
) + 1;
220 pTemp
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(*pTemp
));
227 size
= GetWindowText(Globals
.hEdit
, pTemp
, size
);
230 pTemp
= (LPTSTR
)ConvertToUNICODE(pTemp
, &size
);
233 /* original "pTemp" already freed */
242 if (!WriteText(hFile
, (LPWSTR
)pTemp
, size
, Globals
.iEncoding
, Globals
.iEoln
))
249 SendMessage(Globals
.hEdit
, EM_SETMODIFY
, FALSE
, 0);
255 HeapFree(GetProcessHeap(), 0, pTemp
);
261 * TRUE - User agreed to close (both save/don't save)
262 * FALSE - User cancelled close by selecting "Cancel"
264 BOOL
DoCloseFile(VOID
)
268 if (SendMessage(Globals
.hEdit
, EM_GETMODIFY
, 0, 0))
270 /* prompt user to save changes */
271 nResult
= AlertFileNotSaved(Globals
.szFileName
);
275 if(!DIALOG_FileSave())
290 SetFileName(empty_str
);
291 UpdateWindowCaption();
296 VOID
DoOpenFile(LPCTSTR szFileName
)
298 static const TCHAR dotlog
[] = _T(".LOG");
300 LPTSTR pszText
= NULL
;
304 /* Close any files and prompt to save changes */
308 hFile
= CreateFile(szFileName
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
309 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
310 if (hFile
== INVALID_HANDLE_VALUE
)
316 if (!ReadText(hFile
, (LPWSTR
*)&pszText
, &dwTextLen
, &Globals
.iEncoding
, &Globals
.iEoln
))
322 pszText
= ConvertToASCII(pszText
);
323 if (pszText
== NULL
) {
328 SetWindowText(Globals
.hEdit
, pszText
);
330 SendMessage(Globals
.hEdit
, EM_SETMODIFY
, FALSE
, 0);
331 SendMessage(Globals
.hEdit
, EM_EMPTYUNDOBUFFER
, 0, 0);
332 SetFocus(Globals
.hEdit
);
334 /* If the file starts with .LOG, add a time/date at the end and set cursor after
335 * See http://support.microsoft.com/?kbid=260563
337 if (GetWindowText(Globals
.hEdit
, log
, SIZEOF(log
)) && !_tcscmp(log
, dotlog
))
339 static const TCHAR lf
[] = _T("\r\n");
340 SendMessage(Globals
.hEdit
, EM_SETSEL
, GetWindowTextLength(Globals
.hEdit
), -1);
341 SendMessage(Globals
.hEdit
, EM_REPLACESEL
, TRUE
, (LPARAM
)lf
);
342 DIALOG_EditTimeDate();
343 SendMessage(Globals
.hEdit
, EM_REPLACESEL
, TRUE
, (LPARAM
)lf
);
346 SetFileName(szFileName
);
347 UpdateWindowCaption();
348 NOTEPAD_EnableSearchMenu();
350 if (hFile
!= INVALID_HANDLE_VALUE
)
353 HeapFree(GetProcessHeap(), 0, pszText
);
356 VOID
DIALOG_FileNew(VOID
)
358 /* Close any files and prompt to save changes */
360 SetWindowText(Globals
.hEdit
, empty_str
);
361 SendMessage(Globals
.hEdit
, EM_EMPTYUNDOBUFFER
, 0, 0);
362 SetFocus(Globals
.hEdit
);
363 NOTEPAD_EnableSearchMenu();
367 VOID
DIALOG_FileOpen(VOID
)
369 OPENFILENAME openfilename
;
370 TCHAR szDir
[MAX_PATH
];
371 TCHAR szPath
[MAX_PATH
];
373 ZeroMemory(&openfilename
, sizeof(openfilename
));
375 GetCurrentDirectory(SIZEOF(szDir
), szDir
);
376 if (Globals
.szFileName
[0] == 0)
377 _tcscpy(szPath
, txt_files
);
379 _tcscpy(szPath
, Globals
.szFileName
);
381 openfilename
.lStructSize
= sizeof(openfilename
);
382 openfilename
.hwndOwner
= Globals
.hMainWnd
;
383 openfilename
.hInstance
= Globals
.hInstance
;
384 openfilename
.lpstrFilter
= Globals
.szFilter
;
385 openfilename
.lpstrFile
= szPath
;
386 openfilename
.nMaxFile
= SIZEOF(szPath
);
387 openfilename
.lpstrInitialDir
= szDir
;
388 openfilename
.Flags
= OFN_FILEMUSTEXIST
| OFN_PATHMUSTEXIST
|
390 openfilename
.lpstrDefExt
= szDefaultExt
;
393 if (GetOpenFileName(&openfilename
)) {
394 if (FileExists(openfilename
.lpstrFile
))
395 DoOpenFile(openfilename
.lpstrFile
);
397 AlertFileNotFound(openfilename
.lpstrFile
);
402 BOOL
DIALOG_FileSave(VOID
)
404 if (Globals
.szFileName
[0] == 0)
405 return DIALOG_FileSaveAs();
410 static UINT_PTR CALLBACK
DIALOG_FileSaveAs_Hook(HWND hDlg
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
415 UNREFERENCED_PARAMETER(wParam
);
420 hCombo
= GetDlgItem(hDlg
, ID_ENCODING
);
422 LoadString(Globals
.hInstance
, STRING_ANSI
, szText
, SIZEOF(szText
));
423 SendMessage(hCombo
, CB_ADDSTRING
, 0, (LPARAM
) szText
);
425 LoadString(Globals
.hInstance
, STRING_UNICODE
, szText
, SIZEOF(szText
));
426 SendMessage(hCombo
, CB_ADDSTRING
, 0, (LPARAM
) szText
);
428 LoadString(Globals
.hInstance
, STRING_UNICODE_BE
, szText
, SIZEOF(szText
));
429 SendMessage(hCombo
, CB_ADDSTRING
, 0, (LPARAM
) szText
);
431 LoadString(Globals
.hInstance
, STRING_UTF8
, szText
, SIZEOF(szText
));
432 SendMessage(hCombo
, CB_ADDSTRING
, 0, (LPARAM
) szText
);
434 SendMessage(hCombo
, CB_SETCURSEL
, Globals
.iEncoding
, 0);
436 hCombo
= GetDlgItem(hDlg
, ID_EOLN
);
438 LoadString(Globals
.hInstance
, STRING_CRLF
, szText
, SIZEOF(szText
));
439 SendMessage(hCombo
, CB_ADDSTRING
, 0, (LPARAM
) szText
);
441 LoadString(Globals
.hInstance
, STRING_LF
, szText
, SIZEOF(szText
));
442 SendMessage(hCombo
, CB_ADDSTRING
, 0, (LPARAM
) szText
);
444 LoadString(Globals
.hInstance
, STRING_CR
, szText
, SIZEOF(szText
));
445 SendMessage(hCombo
, CB_ADDSTRING
, 0, (LPARAM
) szText
);
447 SendMessage(hCombo
, CB_SETCURSEL
, Globals
.iEoln
, 0);
451 if (((NMHDR
*) lParam
)->code
== CDN_FILEOK
)
453 hCombo
= GetDlgItem(hDlg
, ID_ENCODING
);
455 Globals
.iEncoding
= (int) SendMessage(hCombo
, CB_GETCURSEL
, 0, 0);
457 hCombo
= GetDlgItem(hDlg
, ID_EOLN
);
459 Globals
.iEoln
= (int) SendMessage(hCombo
, CB_GETCURSEL
, 0, 0);
466 BOOL
DIALOG_FileSaveAs(VOID
)
469 TCHAR szDir
[MAX_PATH
];
470 TCHAR szPath
[MAX_PATH
];
472 ZeroMemory(&saveas
, sizeof(saveas
));
474 GetCurrentDirectory(SIZEOF(szDir
), szDir
);
475 if (Globals
.szFileName
[0] == 0)
476 _tcscpy(szPath
, txt_files
);
478 _tcscpy(szPath
, Globals
.szFileName
);
480 saveas
.lStructSize
= sizeof(OPENFILENAME
);
481 saveas
.hwndOwner
= Globals
.hMainWnd
;
482 saveas
.hInstance
= Globals
.hInstance
;
483 saveas
.lpstrFilter
= Globals
.szFilter
;
484 saveas
.lpstrFile
= szPath
;
485 saveas
.nMaxFile
= SIZEOF(szPath
);
486 saveas
.lpstrInitialDir
= szDir
;
487 saveas
.Flags
= OFN_PATHMUSTEXIST
| OFN_OVERWRITEPROMPT
|
488 OFN_HIDEREADONLY
| OFN_EXPLORER
| OFN_ENABLETEMPLATE
| OFN_ENABLEHOOK
;
489 saveas
.lpstrDefExt
= szDefaultExt
;
490 saveas
.lpTemplateName
= MAKEINTRESOURCE(DIALOG_ENCODING
);
491 saveas
.lpfnHook
= DIALOG_FileSaveAs_Hook
;
493 if (GetSaveFileName(&saveas
))
495 // HACK: Because in ROS, Save-As boxes don't check the validity
496 // of file names and thus, here, szPath can be invalid !! We only
497 // see its validity when we call DoSaveFile()...
501 UpdateWindowCaption();
516 VOID
DIALOG_FilePrint(VOID
)
521 int cWidthPels
, cHeightPels
, border
;
522 int xLeft
, yTop
, pagecount
, dopage
, copycount
;
525 HFONT font
, old_font
=0;
528 static const TCHAR times_new_roman
[] = _T("Times New Roman");
530 /* Get a small font and print some header info on each page */
531 ZeroMemory(&hdrFont
, sizeof(hdrFont
));
532 hdrFont
.lfHeight
= 100;
533 hdrFont
.lfWeight
= FW_BOLD
;
534 hdrFont
.lfCharSet
= ANSI_CHARSET
;
535 hdrFont
.lfOutPrecision
= OUT_DEFAULT_PRECIS
;
536 hdrFont
.lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
537 hdrFont
.lfQuality
= PROOF_QUALITY
;
538 hdrFont
.lfPitchAndFamily
= VARIABLE_PITCH
| FF_ROMAN
;
539 _tcscpy(hdrFont
.lfFaceName
, times_new_roman
);
541 font
= CreateFontIndirect(&hdrFont
);
543 /* Get Current Settings */
544 ZeroMemory(&printer
, sizeof(printer
));
545 printer
.lStructSize
= sizeof(printer
);
546 printer
.hwndOwner
= Globals
.hMainWnd
;
547 printer
.hInstance
= Globals
.hInstance
;
549 /* Set some default flags */
550 printer
.Flags
= PD_RETURNDC
;
551 printer
.nFromPage
= 0;
552 printer
.nMinPage
= 1;
553 /* we really need to calculate number of pages to set nMaxPage and nToPage */
555 printer
.nMaxPage
= (WORD
) -1;
557 /* Let commdlg manage copy settings */
558 printer
.nCopies
= (WORD
)PD_USEDEVMODECOPIES
;
560 if (!PrintDlg(&printer
)) return;
562 assert(printer
.hDC
!= 0);
564 /* initialize DOCINFO */
565 di
.cbSize
= sizeof(DOCINFO
);
566 di
.lpszDocName
= Globals
.szFileTitle
;
567 di
.lpszOutput
= NULL
;
568 di
.lpszDatatype
= NULL
;
571 if (StartDoc(printer
.hDC
, &di
) <= 0) return;
573 /* Get the page dimensions in pixels. */
574 cWidthPels
= GetDeviceCaps(printer
.hDC
, HORZRES
);
575 cHeightPels
= GetDeviceCaps(printer
.hDC
, VERTRES
);
577 /* Get the file text */
578 size
= GetWindowTextLength(Globals
.hEdit
) + 1;
579 pTemp
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(TCHAR
));
585 size
= GetWindowText(Globals
.hEdit
, pTemp
, size
);
588 for (copycount
=1; copycount
<= printer
.nCopies
; copycount
++) {
592 static const TCHAR letterM
[] = _T("M");
594 if (pagecount
>= printer
.nFromPage
&&
595 /* ((printer.Flags & PD_PAGENUMS) == 0 || pagecount <= printer.nToPage))*/
596 pagecount
<= printer
.nToPage
)
601 old_font
= SelectObject(printer
.hDC
, font
);
602 GetTextExtentPoint32(printer
.hDC
, letterM
, 1, &szMetric
);
605 if (StartPage(printer
.hDC
) <= 0) {
606 static const TCHAR failed
[] = _T("StartPage failed");
607 static const TCHAR error
[] = _T("Print Error");
608 MessageBox(Globals
.hMainWnd
, failed
, error
, MB_ICONEXCLAMATION
);
611 /* Write a rectangle and header at the top of each page */
612 Rectangle(printer
.hDC
, border
, border
, cWidthPels
-border
, border
+szMetric
.cy
*2);
613 /* I don't know what's up with this TextOut command. This comes out
616 TextOut(printer
.hDC
, border
*2, border
+szMetric
.cy
/2, Globals
.szFileTitle
, lstrlen(Globals
.szFileTitle
));
619 /* The starting point for the main text */
621 yTop
= border
+szMetric
.cy
*4;
623 SelectObject(printer
.hDC
, old_font
);
624 GetTextExtentPoint32(printer
.hDC
, letterM
, 1, &szMetric
);
626 /* Since outputting strings is giving me problems, output the main
627 text one character at a time.
630 if (pTemp
[i
] == '\n') {
634 else if (pTemp
[i
] != '\r') {
636 TextOut(printer
.hDC
, xLeft
, yTop
, &pTemp
[i
], 1);
637 xLeft
+= szMetric
.cx
;
639 } while (i
++<size
&& yTop
<(cHeightPels
-border
*2));
642 EndPage(printer
.hDC
);
648 DeleteDC(printer
.hDC
);
649 HeapFree(GetProcessHeap(), 0, pTemp
);
652 VOID
DIALOG_FilePrinterSetup(VOID
)
656 ZeroMemory(&printer
, sizeof(printer
));
657 printer
.lStructSize
= sizeof(printer
);
658 printer
.hwndOwner
= Globals
.hMainWnd
;
659 printer
.hInstance
= Globals
.hInstance
;
660 printer
.Flags
= PD_PRINTSETUP
;
666 VOID
DIALOG_FileExit(VOID
)
668 PostMessage(Globals
.hMainWnd
, WM_CLOSE
, 0, 0l);
671 VOID
DIALOG_EditUndo(VOID
)
673 SendMessage(Globals
.hEdit
, EM_UNDO
, 0, 0);
676 VOID
DIALOG_EditCut(VOID
)
678 SendMessage(Globals
.hEdit
, WM_CUT
, 0, 0);
681 VOID
DIALOG_EditCopy(VOID
)
683 SendMessage(Globals
.hEdit
, WM_COPY
, 0, 0);
686 VOID
DIALOG_EditPaste(VOID
)
688 SendMessage(Globals
.hEdit
, WM_PASTE
, 0, 0);
691 VOID
DIALOG_EditDelete(VOID
)
693 SendMessage(Globals
.hEdit
, WM_CLEAR
, 0, 0);
696 VOID
DIALOG_EditSelectAll(VOID
)
698 SendMessage(Globals
.hEdit
, EM_SETSEL
, 0, (LPARAM
)-1);
701 VOID
DIALOG_EditTimeDate(VOID
)
704 TCHAR szDate
[MAX_STRING_LEN
];
705 TCHAR szText
[MAX_STRING_LEN
* 2 + 2];
709 GetTimeFormat(LOCALE_USER_DEFAULT
, 0, &st
, NULL
, szDate
, MAX_STRING_LEN
);
710 _tcscpy(szText
, szDate
);
711 _tcscat(szText
, _T(" "));
712 GetDateFormat(LOCALE_USER_DEFAULT
, DATE_LONGDATE
, &st
, NULL
, szDate
, MAX_STRING_LEN
);
713 _tcscat(szText
, szDate
);
714 SendMessage(Globals
.hEdit
, EM_REPLACESEL
, TRUE
, (LPARAM
)szText
);
717 VOID
DoCreateStatusBar(VOID
)
721 BOOL bStatusBarVisible
;
723 // Check if status bar object already exists.
724 if (Globals
.hStatusBar
== NULL
)
726 // Try to create the status bar
727 Globals
.hStatusBar
= CreateStatusWindow(
728 WS_CHILD
| WS_VISIBLE
| WS_EX_STATICEDGE
,
731 CMD_STATUSBAR_WND_ID
);
733 if (Globals
.hStatusBar
== NULL
)
739 // Load the string for formatting column/row text output
740 LoadString(Globals
.hInstance
, STRING_LINE_COLUMN
, Globals
.szStatusBarLineCol
, MAX_PATH
-1);
742 // Set the status bar for single-text output
743 SendMessage(Globals
.hStatusBar
, SB_SIMPLE
, (WPARAM
)TRUE
, (LPARAM
)0);
746 // Set status bar visible or not accordind the the settings.
747 if (Globals
.bWrapLongLines
== TRUE
||
748 Globals
.bShowStatusBar
== FALSE
)
750 bStatusBarVisible
= FALSE
;
751 ShowWindow(Globals
.hStatusBar
, SW_HIDE
);
755 bStatusBarVisible
= TRUE
;
756 ShowWindow(Globals
.hStatusBar
, SW_SHOW
);
757 SendMessage(Globals
.hStatusBar
, WM_SIZE
, 0, 0);
760 // Set check state in show status bar item.
761 if (Globals
.bShowStatusBar
== TRUE
)
763 CheckMenuItem(Globals
.hMenu
, CMD_STATUSBAR
, MF_BYCOMMAND
| MF_CHECKED
);
767 CheckMenuItem(Globals
.hMenu
, CMD_STATUSBAR
, MF_BYCOMMAND
| MF_UNCHECKED
);
770 // Update menu mar with the previous changes
771 DrawMenuBar(Globals
.hMainWnd
);
773 // Sefety test is edit control exists
774 if (Globals
.hEdit
!= NULL
)
776 // Retrieve the sizes of the controls
777 GetClientRect(Globals
.hMainWnd
, &rc
);
778 GetClientRect(Globals
.hStatusBar
, &rcstatus
);
780 // If status bar is currently visible, update dimensions of edir control
781 if (bStatusBarVisible
)
782 rc
.bottom
-= (rcstatus
.bottom
- rcstatus
.top
);
784 // Resize edit control to right size.
785 MoveWindow(Globals
.hEdit
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
, TRUE
);
788 // Update content with current row/column text
789 DIALOG_StatusBarUpdateCaretPos();
792 VOID
DoCreateEditWindow(VOID
)
797 BOOL bModified
= FALSE
;
801 // If the edit control already exists, try to save its content
802 if (Globals
.hEdit
!= NULL
)
804 // number of chars currently written into the editor.
805 iSize
= GetWindowTextLength(Globals
.hEdit
);
809 // Allocates temporary buffer.
810 pTemp
= HeapAlloc(GetProcessHeap(), 0, (iSize
+ 1) * sizeof(TCHAR
));
818 // Recover the text into the control.
819 GetWindowText(Globals
.hEdit
, pTemp
, iSize
+ 1);
821 if (SendMessage(Globals
.hEdit
, EM_GETMODIFY
, 0, 0))
825 // Restore original window procedure
826 SetWindowLongPtr(Globals
.hEdit
, GWLP_WNDPROC
, (LONG_PTR
)Globals
.EditProc
);
828 // Destroy the edit control
829 DestroyWindow(Globals
.hEdit
);
832 // Update wrap status into the main menu and recover style flags
833 if (Globals
.bWrapLongLines
)
835 dwStyle
= EDIT_STYLE_WRAP
;
836 EnableMenuItem(Globals
.hMenu
, CMD_STATUSBAR
, MF_BYCOMMAND
| MF_DISABLED
| MF_GRAYED
);
838 dwStyle
= EDIT_STYLE
;
839 EnableMenuItem(Globals
.hMenu
, CMD_STATUSBAR
, MF_BYCOMMAND
| MF_ENABLED
);
842 // Update previous changes
843 DrawMenuBar(Globals
.hMainWnd
);
845 // Create the new edit control
846 Globals
.hEdit
= CreateWindowEx(
860 if (Globals
.hEdit
== NULL
)
866 SendMessage(Globals
.hEdit
, WM_SETFONT
, (WPARAM
)Globals
.hFont
, FALSE
);
867 SendMessage(Globals
.hEdit
, EM_LIMITTEXT
, 0, 0);
869 // If some text was previously saved, restore it.
872 SetWindowText(Globals
.hEdit
, pTemp
);
873 HeapFree(GetProcessHeap(), 0, pTemp
);
876 SendMessage(Globals
.hEdit
, EM_SETMODIFY
, TRUE
, 0);
879 // Sub-class a new window callback for row/column detection.
880 Globals
.EditProc
= (WNDPROC
) SetWindowLongPtr(Globals
.hEdit
, GWLP_WNDPROC
, (LONG_PTR
)EDIT_WndProc
);
882 // Create/update status bar
885 // Finally shows new edit control and set focus into it.
886 ShowWindow(Globals
.hEdit
, SW_SHOW
);
887 SetFocus(Globals
.hEdit
);
890 VOID
DIALOG_EditWrap(VOID
)
892 Globals
.bWrapLongLines
= !Globals
.bWrapLongLines
;
894 DoCreateEditWindow();
897 VOID
DIALOG_SelectFont(VOID
)
900 LOGFONT lf
=Globals
.lfFont
;
902 ZeroMemory( &cf
, sizeof(cf
) );
903 cf
.lStructSize
=sizeof(cf
);
904 cf
.hwndOwner
=Globals
.hMainWnd
;
906 cf
.Flags
=CF_SCREENFONTS
| CF_INITTOLOGFONTSTRUCT
;
908 if( ChooseFont(&cf
) )
910 HFONT currfont
=Globals
.hFont
;
912 Globals
.hFont
=CreateFontIndirect( &lf
);
914 SendMessage( Globals
.hEdit
, WM_SETFONT
, (WPARAM
)Globals
.hFont
, (LPARAM
)TRUE
);
916 DeleteObject( currfont
);
920 typedef HWND (WINAPI
*FINDPROC
)(LPFINDREPLACE lpfr
);
922 static VOID
DIALOG_SearchDialog(FINDPROC pfnProc
)
924 ZeroMemory(&Globals
.find
, sizeof(Globals
.find
));
925 Globals
.find
.lStructSize
= sizeof(Globals
.find
);
926 Globals
.find
.hwndOwner
= Globals
.hMainWnd
;
927 Globals
.find
.hInstance
= Globals
.hInstance
;
928 Globals
.find
.lpstrFindWhat
= Globals
.szFindText
;
929 Globals
.find
.wFindWhatLen
= SIZEOF(Globals
.szFindText
);
930 Globals
.find
.lpstrReplaceWith
= Globals
.szReplaceText
;
931 Globals
.find
.wReplaceWithLen
= SIZEOF(Globals
.szReplaceText
);
932 Globals
.find
.Flags
= FR_DOWN
;
934 /* We only need to create the modal FindReplace dialog which will */
935 /* notify us of incoming events using hMainWnd Window Messages */
937 Globals
.hFindReplaceDlg
= pfnProc(&Globals
.find
);
938 assert(Globals
.hFindReplaceDlg
!=0);
941 VOID
DIALOG_Search(VOID
)
943 DIALOG_SearchDialog(FindText
);
946 VOID
DIALOG_SearchNext(VOID
)
948 if (Globals
.find
.lpstrFindWhat
!= NULL
)
949 NOTEPAD_FindNext(&Globals
.find
, FALSE
, TRUE
);
954 VOID
DIALOG_Replace(VOID
)
956 DIALOG_SearchDialog(ReplaceText
);
959 static INT_PTR CALLBACK
DIALOG_GoTo_DialogProc(HWND hwndDialog
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
961 BOOL bResult
= FALSE
;
967 hTextBox
= GetDlgItem(hwndDialog
, ID_LINENUMBER
);
968 _sntprintf(szText
, SIZEOF(szText
), _T("%ld"), lParam
);
969 SetWindowText(hTextBox
, szText
);
972 if (HIWORD(wParam
) == BN_CLICKED
)
974 if (LOWORD(wParam
) == IDOK
)
976 hTextBox
= GetDlgItem(hwndDialog
, ID_LINENUMBER
);
977 GetWindowText(hTextBox
, szText
, SIZEOF(szText
));
978 EndDialog(hwndDialog
, _ttoi(szText
));
981 else if (LOWORD(wParam
) == IDCANCEL
)
983 EndDialog(hwndDialog
, 0);
993 VOID
DIALOG_GoTo(VOID
)
998 DWORD dwStart
, dwEnd
;
1000 nLength
= GetWindowTextLength(Globals
.hEdit
);
1001 pszText
= (LPTSTR
) HeapAlloc(GetProcessHeap(), 0, (nLength
+ 1) * sizeof(*pszText
));
1005 /* Retrieve current text */
1006 GetWindowText(Globals
.hEdit
, pszText
, nLength
+ 1);
1007 SendMessage(Globals
.hEdit
, EM_GETSEL
, (WPARAM
) &dwStart
, (LPARAM
) &dwEnd
);
1010 for (i
= 0; pszText
[i
] && (i
< (int) dwStart
); i
++)
1012 if (pszText
[i
] == '\n')
1016 nLine
= DialogBoxParam(Globals
.hInstance
, MAKEINTRESOURCE(DIALOG_GOTO
),
1017 Globals
.hMainWnd
, DIALOG_GoTo_DialogProc
, nLine
);
1021 for (i
= 0; pszText
[i
] && (nLine
> 1) && (i
< nLength
- 1); i
++)
1023 if (pszText
[i
] == '\n')
1026 SendMessage(Globals
.hEdit
, EM_SETSEL
, i
, i
);
1027 SendMessage(Globals
.hEdit
, EM_SCROLLCARET
, 0, 0);
1029 HeapFree(GetProcessHeap(), 0, pszText
);
1032 VOID
DIALOG_StatusBarUpdateCaretPos(VOID
)
1035 TCHAR buff
[MAX_PATH
];
1036 DWORD dwStart
, dwSize
;
1038 SendMessage(Globals
.hEdit
, EM_GETSEL
, (WPARAM
)&dwStart
, (LPARAM
)&dwSize
);
1039 line
= SendMessage(Globals
.hEdit
, EM_LINEFROMCHAR
, (WPARAM
)dwStart
, 0);
1040 col
= dwStart
- SendMessage(Globals
.hEdit
, EM_LINEINDEX
, (WPARAM
)line
, 0);
1042 _stprintf(buff
, Globals
.szStatusBarLineCol
, line
+1, col
+1);
1043 SendMessage(Globals
.hStatusBar
, SB_SETTEXT
, SB_SIMPLEID
, (LPARAM
)buff
);
1046 VOID
DIALOG_ViewStatusBar(VOID
)
1048 Globals
.bShowStatusBar
= !Globals
.bShowStatusBar
;
1050 DoCreateStatusBar();
1053 VOID
DIALOG_HelpContents(VOID
)
1055 WinHelp(Globals
.hMainWnd
, helpfile
, HELP_INDEX
, 0);
1058 VOID
DIALOG_HelpSearch(VOID
)
1063 VOID
DIALOG_HelpHelp(VOID
)
1065 WinHelp(Globals
.hMainWnd
, helpfile
, HELP_HELPONHELP
, 0);
1069 #pragma warning(disable : 4100)
1072 AboutDialogProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
1074 HWND hLicenseEditWnd
;
1081 hLicenseEditWnd
= GetDlgItem(hDlg
, IDC_LICENSE
);
1083 /* 0x1000 should be enought */
1084 strLicense
= (TCHAR
*)_alloca(0x1000);
1085 LoadString(GetModuleHandle(NULL
), STRING_LICENSE
, strLicense
, 0x1000);
1087 SetWindowText(hLicenseEditWnd
, strLicense
);
1093 if ((LOWORD(wParam
) == IDOK
) || (LOWORD(wParam
) == IDCANCEL
))
1095 EndDialog(hDlg
, LOWORD(wParam
));
1107 VOID
DIALOG_HelpAboutWine(VOID
)
1109 TCHAR szNotepad
[MAX_STRING_LEN
];
1110 HICON notepadIcon
= LoadIcon(Globals
.hInstance
, MAKEINTRESOURCE(IDI_NPICON
));
1112 LoadString(Globals
.hInstance
, STRING_NOTEPAD
, szNotepad
, SIZEOF(szNotepad
));
1113 ShellAbout(Globals
.hMainWnd
, szNotepad
, 0, notepadIcon
);
1114 DeleteObject(notepadIcon
);
1118 /***********************************************************************
1120 * DIALOG_FilePageSetup
1122 VOID
DIALOG_FilePageSetup(void)
1124 DialogBox(Globals
.hInstance
, MAKEINTRESOURCE(DIALOG_PAGESETUP
),
1125 Globals
.hMainWnd
, DIALOG_PAGESETUP_DlgProc
);
1129 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1131 * DIALOG_PAGESETUP_DlgProc
1134 static INT_PTR WINAPI
DIALOG_PAGESETUP_DlgProc(HWND hDlg
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
1140 if (HIWORD(wParam
) == BN_CLICKED
)
1142 switch (LOWORD(wParam
))
1145 /* save user input and close dialog */
1146 GetDlgItemText(hDlg
, 0x141, Globals
.szHeader
, SIZEOF(Globals
.szHeader
));
1147 GetDlgItemText(hDlg
, 0x143, Globals
.szFooter
, SIZEOF(Globals
.szFooter
));
1148 GetDlgItemText(hDlg
, 0x14A, Globals
.szMarginTop
, SIZEOF(Globals
.szMarginTop
));
1149 GetDlgItemText(hDlg
, 0x150, Globals
.szMarginBottom
, SIZEOF(Globals
.szMarginBottom
));
1150 GetDlgItemText(hDlg
, 0x147, Globals
.szMarginLeft
, SIZEOF(Globals
.szMarginLeft
));
1151 GetDlgItemText(hDlg
, 0x14D, Globals
.szMarginRight
, SIZEOF(Globals
.szMarginRight
));
1152 EndDialog(hDlg
, IDOK
);
1156 /* discard user input and close dialog */
1157 EndDialog(hDlg
, IDCANCEL
);
1162 /* FIXME: Bring this to work */
1163 static const TCHAR sorry
[] = _T("Sorry, no help available");
1164 static const TCHAR help
[] = _T("Help");
1165 MessageBox(Globals
.hMainWnd
, sorry
, help
, MB_ICONEXCLAMATION
);
1176 /* fetch last user input prior to display dialog */
1177 SetDlgItemText(hDlg
, 0x141, Globals
.szHeader
);
1178 SetDlgItemText(hDlg
, 0x143, Globals
.szFooter
);
1179 SetDlgItemText(hDlg
, 0x14A, Globals
.szMarginTop
);
1180 SetDlgItemText(hDlg
, 0x150, Globals
.szMarginBottom
);
1181 SetDlgItemText(hDlg
, 0x147, Globals
.szMarginLeft
);
1182 SetDlgItemText(hDlg
, 0x14D, Globals
.szMarginRight
);