4 * Copyright 2000 Mike McCormack <Mike_McCormack@looksmart.com.au>
5 * Copyright 1997,98 Marcel Baur <mbaur@g26.ethz.ch>
6 * Copyright 2002 Sylvain Petreolle <spetreolle@yahoo.fr>
7 * Copyright 2002 Andriy Palamarchuk
8 * Copyright 2020 Katayama Hirofumi MZ
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 NOTEPAD_GLOBALS Globals
;
32 static ATOM aFINDMSGSTRING
;
34 VOID
NOTEPAD_EnableSearchMenu()
36 EnableMenuItem(Globals
.hMenu
, CMD_SEARCH
,
37 MF_BYCOMMAND
| ((GetWindowTextLength(Globals
.hEdit
) == 0) ? MF_DISABLED
| MF_GRAYED
: MF_ENABLED
));
38 EnableMenuItem(Globals
.hMenu
, CMD_SEARCH_NEXT
,
39 MF_BYCOMMAND
| ((GetWindowTextLength(Globals
.hEdit
) == 0) ? MF_DISABLED
| MF_GRAYED
: MF_ENABLED
));
42 /***********************************************************************
46 * Sets Global File Name.
48 VOID
SetFileName(LPCTSTR szFileName
)
50 StringCchCopy(Globals
.szFileName
, ARRAY_SIZE(Globals
.szFileName
), szFileName
);
51 Globals
.szFileTitle
[0] = 0;
52 GetFileTitle(szFileName
, Globals
.szFileTitle
, ARRAY_SIZE(Globals
.szFileTitle
));
54 if (szFileName
&& szFileName
[0])
55 SHAddToRecentDocs(SHARD_PATHW
, szFileName
);
58 /***********************************************************************
62 * All handling of main menu events
64 static int NOTEPAD_MenuCommand(WPARAM wParam
)
68 case CMD_NEW
: DIALOG_FileNew(); break;
69 case CMD_NEW_WINDOW
: DIALOG_FileNewWindow(); break;
70 case CMD_OPEN
: DIALOG_FileOpen(); break;
71 case CMD_SAVE
: DIALOG_FileSave(); break;
72 case CMD_SAVE_AS
: DIALOG_FileSaveAs(); break;
73 case CMD_PRINT
: DIALOG_FilePrint(); break;
74 case CMD_PAGE_SETUP
: DIALOG_FilePageSetup(); break;
75 case CMD_EXIT
: DIALOG_FileExit(); break;
77 case CMD_UNDO
: DIALOG_EditUndo(); break;
78 case CMD_CUT
: DIALOG_EditCut(); break;
79 case CMD_COPY
: DIALOG_EditCopy(); break;
80 case CMD_PASTE
: DIALOG_EditPaste(); break;
81 case CMD_DELETE
: DIALOG_EditDelete(); break;
82 case CMD_SELECT_ALL
: DIALOG_EditSelectAll(); break;
83 case CMD_TIME_DATE
: DIALOG_EditTimeDate(); break;
85 case CMD_SEARCH
: DIALOG_Search(); break;
86 case CMD_SEARCH_NEXT
: DIALOG_SearchNext(); break;
87 case CMD_REPLACE
: DIALOG_Replace(); break;
88 case CMD_GOTO
: DIALOG_GoTo(); break;
90 case CMD_WRAP
: DIALOG_EditWrap(); break;
91 case CMD_FONT
: DIALOG_SelectFont(); break;
93 case CMD_STATUSBAR
: DIALOG_ViewStatusBar(); break;
95 case CMD_HELP_CONTENTS
: DIALOG_HelpContents(); break;
96 case CMD_HELP_ABOUT_NOTEPAD
: DIALOG_HelpAboutNotepad(); break;
104 /***********************************************************************
110 NOTEPAD_FindTextAt(FINDREPLACE
*pFindReplace
, LPCTSTR pszText
, int iTextLength
, DWORD dwPosition
)
113 size_t iTargetLength
;
115 if ((!pFindReplace
) || (!pszText
))
120 iTargetLength
= _tcslen(pFindReplace
->lpstrFindWhat
);
122 /* Make proper comparison */
123 if (pFindReplace
->Flags
& FR_MATCHCASE
)
124 bMatches
= !_tcsncmp(&pszText
[dwPosition
], pFindReplace
->lpstrFindWhat
, iTargetLength
);
126 bMatches
= !_tcsnicmp(&pszText
[dwPosition
], pFindReplace
->lpstrFindWhat
, iTargetLength
);
128 if (bMatches
&& pFindReplace
->Flags
& FR_WHOLEWORD
)
130 if ((dwPosition
> 0) && !_istspace(pszText
[dwPosition
-1]))
132 if ((dwPosition
< (DWORD
) iTextLength
- 1) && !_istspace(pszText
[dwPosition
+1]))
139 /***********************************************************************
144 BOOL
NOTEPAD_FindNext(FINDREPLACE
*pFindReplace
, BOOL bReplace
, BOOL bShowAlert
)
146 int iTextLength
, iTargetLength
;
147 size_t iAdjustment
= 0;
148 LPTSTR pszText
= NULL
;
149 DWORD dwPosition
, dwBegin
, dwEnd
;
150 BOOL bMatches
= FALSE
;
151 TCHAR szResource
[128], szText
[128];
154 iTargetLength
= (int) _tcslen(pFindReplace
->lpstrFindWhat
);
156 /* Retrieve the window text */
157 iTextLength
= GetWindowTextLength(Globals
.hEdit
);
160 pszText
= (LPTSTR
) HeapAlloc(GetProcessHeap(), 0, (iTextLength
+ 1) * sizeof(TCHAR
));
164 GetWindowText(Globals
.hEdit
, pszText
, iTextLength
+ 1);
167 SendMessage(Globals
.hEdit
, EM_GETSEL
, (WPARAM
) &dwBegin
, (LPARAM
) &dwEnd
);
168 if (bReplace
&& ((dwEnd
- dwBegin
) == (DWORD
) iTargetLength
))
170 if (NOTEPAD_FindTextAt(pFindReplace
, pszText
, iTextLength
, dwBegin
))
172 SendMessage(Globals
.hEdit
, EM_REPLACESEL
, TRUE
, (LPARAM
) pFindReplace
->lpstrReplaceWith
);
173 iAdjustment
= _tcslen(pFindReplace
->lpstrReplaceWith
) - (dwEnd
- dwBegin
);
177 if (pFindReplace
->Flags
& FR_DOWN
)
181 while(dwPosition
< (DWORD
) iTextLength
)
183 bMatches
= NOTEPAD_FindTextAt(pFindReplace
, pszText
, iTextLength
, dwPosition
);
192 dwPosition
= dwBegin
;
193 while(dwPosition
> 0)
196 bMatches
= NOTEPAD_FindTextAt(pFindReplace
, pszText
, iTextLength
, dwPosition
);
205 if (dwPosition
> dwBegin
)
206 dwPosition
+= (DWORD
) iAdjustment
;
207 SendMessage(Globals
.hEdit
, EM_SETSEL
, dwPosition
, dwPosition
+ iTargetLength
);
208 SendMessage(Globals
.hEdit
, EM_SCROLLCARET
, 0, 0);
213 /* Can't find target */
216 LoadString(Globals
.hInstance
, STRING_CANNOTFIND
, szResource
, ARRAY_SIZE(szResource
));
217 _sntprintf(szText
, ARRAY_SIZE(szText
), szResource
, pFindReplace
->lpstrFindWhat
);
218 LoadString(Globals
.hInstance
, STRING_NOTEPAD
, szResource
, ARRAY_SIZE(szResource
));
219 MessageBox(Globals
.hFindReplaceDlg
, szText
, szResource
, MB_OK
);
225 HeapFree(GetProcessHeap(), 0, pszText
);
229 /***********************************************************************
234 static VOID
NOTEPAD_ReplaceAll(FINDREPLACE
*pFindReplace
)
236 BOOL bShowAlert
= TRUE
;
238 SendMessage(Globals
.hEdit
, EM_SETSEL
, 0, 0);
240 while (NOTEPAD_FindNext(pFindReplace
, TRUE
, bShowAlert
))
246 /***********************************************************************
251 static VOID
NOTEPAD_FindTerm(VOID
)
253 Globals
.hFindReplaceDlg
= NULL
;
256 /***********************************************************************
257 * Data Initialization
259 static VOID
NOTEPAD_InitData(VOID
)
261 LPTSTR p
= Globals
.szFilter
;
262 static const TCHAR txt_files
[] = _T("*.txt");
263 static const TCHAR all_files
[] = _T("*.*");
265 p
+= LoadString(Globals
.hInstance
, STRING_TEXT_FILES_TXT
, p
, MAX_STRING_LEN
) + 1;
266 _tcscpy(p
, txt_files
);
267 p
+= ARRAY_SIZE(txt_files
);
269 p
+= LoadString(Globals
.hInstance
, STRING_ALL_FILES
, p
, MAX_STRING_LEN
) + 1;
270 _tcscpy(p
, all_files
);
271 p
+= ARRAY_SIZE(all_files
);
273 Globals
.find
.lpstrFindWhat
= NULL
;
275 Globals
.hDevMode
= NULL
;
276 Globals
.hDevNames
= NULL
;
279 /***********************************************************************
280 * Enable/disable items on the menu based on control state
282 static VOID
NOTEPAD_InitMenuPopup(HMENU menu
, LPARAM index
)
286 UNREFERENCED_PARAMETER(index
);
288 CheckMenuItem(GetMenu(Globals
.hMainWnd
), CMD_WRAP
,
289 MF_BYCOMMAND
| (Globals
.bWrapLongLines
? MF_CHECKED
: MF_UNCHECKED
));
290 if (!Globals
.bWrapLongLines
)
292 CheckMenuItem(GetMenu(Globals
.hMainWnd
), CMD_STATUSBAR
,
293 MF_BYCOMMAND
| (Globals
.bShowStatusBar
? MF_CHECKED
: MF_UNCHECKED
));
295 EnableMenuItem(menu
, CMD_UNDO
,
296 SendMessage(Globals
.hEdit
, EM_CANUNDO
, 0, 0) ? MF_ENABLED
: MF_GRAYED
);
297 EnableMenuItem(menu
, CMD_PASTE
,
298 IsClipboardFormatAvailable(CF_TEXT
) ? MF_ENABLED
: MF_GRAYED
);
299 enable
= (int) SendMessage(Globals
.hEdit
, EM_GETSEL
, 0, 0);
300 enable
= (HIWORD(enable
) == LOWORD(enable
)) ? MF_GRAYED
: MF_ENABLED
;
301 EnableMenuItem(menu
, CMD_CUT
, enable
);
302 EnableMenuItem(menu
, CMD_COPY
, enable
);
303 EnableMenuItem(menu
, CMD_DELETE
, enable
);
305 EnableMenuItem(menu
, CMD_SELECT_ALL
,
306 GetWindowTextLength(Globals
.hEdit
) ? MF_ENABLED
: MF_GRAYED
);
307 DrawMenuBar(Globals
.hMainWnd
);
310 LRESULT CALLBACK
EDIT_WndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
323 DIALOG_StatusBarUpdateCaretPos();
327 UpdateWindowCaption(FALSE
);
334 DIALOG_StatusBarUpdateCaretPos();
338 return CallWindowProc( Globals
.EditProc
, hWnd
, msg
, wParam
, lParam
);
341 /***********************************************************************
347 NOTEPAD_WndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
353 Globals
.hMenu
= GetMenu(hWnd
);
355 // For now, the "Help" dialog is disabled due to the lack of HTML Help support
356 EnableMenuItem(Globals
.hMenu
, CMD_HELP_CONTENTS
, MF_BYCOMMAND
| MF_GRAYED
);
360 if (HIWORD(wParam
) == EN_CHANGE
|| HIWORD(wParam
) == EN_HSCROLL
|| HIWORD(wParam
) == EN_VSCROLL
)
361 DIALOG_StatusBarUpdateCaretPos();
362 if ((HIWORD(wParam
) == EN_CHANGE
))
363 NOTEPAD_EnableSearchMenu();
364 NOTEPAD_MenuCommand(LOWORD(wParam
));
367 case WM_DESTROYCLIPBOARD
:
368 /*MessageBox(Globals.hMainWnd, "Empty clipboard", "Debug", MB_ICONEXCLAMATION);*/
374 DeleteObject(Globals
.hFont
);
375 if (Globals
.hDevMode
)
376 GlobalFree(Globals
.hDevMode
);
377 if (Globals
.hDevNames
)
378 GlobalFree(Globals
.hDevNames
);
383 case WM_QUERYENDSESSION
:
390 SetWindowLongPtr(Globals
.hEdit
, GWLP_WNDPROC
, (LONG_PTR
)Globals
.EditProc
);
391 NOTEPAD_SaveSettingsToRegistry();
397 if ((Globals
.bShowStatusBar
!= FALSE
) && (Globals
.bWrapLongLines
== FALSE
))
402 if (!GetWindowRect(Globals
.hStatusBar
, &rcStatusBar
))
405 hdwp
= BeginDeferWindowPos(2);
409 hdwp
= DeferWindowPos(hdwp
,
415 HIWORD(lParam
) - (rcStatusBar
.bottom
- rcStatusBar
.top
),
416 SWP_NOZORDER
| SWP_NOMOVE
);
421 hdwp
= DeferWindowPos(hdwp
,
431 EndDeferWindowPos(hdwp
);
434 SetWindowPos(Globals
.hEdit
,
440 SWP_NOZORDER
| SWP_NOMOVE
);
445 /* The entire client area is covered by edit control and by
446 * the status bar. So there is no need to erase main background.
447 * This resolves the horrible flicker effect during windows resizes. */
452 SetFocus(Globals
.hEdit
);
457 TCHAR szFileName
[MAX_PATH
];
458 HDROP hDrop
= (HDROP
) wParam
;
460 DragQueryFile(hDrop
, 0, szFileName
, ARRAY_SIZE(szFileName
));
462 DoOpenFile(szFileName
);
466 case WM_INITMENUPOPUP
:
467 NOTEPAD_InitMenuPopup((HMENU
)wParam
, lParam
);
470 if (msg
== aFINDMSGSTRING
)
472 FINDREPLACE
*pFindReplace
= (FINDREPLACE
*) lParam
;
473 Globals
.find
= *(FINDREPLACE
*) lParam
;
475 if (pFindReplace
->Flags
& FR_FINDNEXT
)
476 NOTEPAD_FindNext(pFindReplace
, FALSE
, TRUE
);
477 else if (pFindReplace
->Flags
& FR_REPLACE
)
478 NOTEPAD_FindNext(pFindReplace
, TRUE
, TRUE
);
479 else if (pFindReplace
->Flags
& FR_REPLACEALL
)
480 NOTEPAD_ReplaceAll(pFindReplace
);
481 else if (pFindReplace
->Flags
& FR_DIALOGTERM
)
486 return DefWindowProc(hWnd
, msg
, wParam
, lParam
);
491 static int AlertFileDoesNotExist(LPCTSTR szFileName
)
493 return DIALOG_StringMsgBox(Globals
.hMainWnd
, STRING_DOESNOTEXIST
,
495 MB_ICONEXCLAMATION
| MB_YESNO
);
498 static BOOL
HandleCommandLine(LPTSTR cmdline
)
500 BOOL opt_print
= FALSE
;
502 while (*cmdline
== _T(' ') || *cmdline
== _T('-') || *cmdline
== _T('/'))
506 if (*cmdline
++ == _T(' ')) continue;
509 if (option
) cmdline
++;
510 while (*cmdline
== _T(' ')) cmdline
++;
523 /* file name is passed in the command line */
524 LPCTSTR file_name
= NULL
;
525 BOOL file_exists
= FALSE
;
528 if (cmdline
[0] == _T('"'))
531 cmdline
[lstrlen(cmdline
) - 1] = 0;
535 if (FileExists(file_name
))
539 else if (!HasFileExtension(cmdline
))
541 static const TCHAR txt
[] = _T(".txt");
543 /* try to find file with ".txt" extension */
544 if (!_tcscmp(txt
, cmdline
+ _tcslen(cmdline
) - _tcslen(txt
)))
550 _tcsncpy(buf
, cmdline
, MAX_PATH
- _tcslen(txt
) - 1);
553 file_exists
= FileExists(file_name
);
559 DoOpenFile(file_name
);
560 InvalidateRect(Globals
.hMainWnd
, NULL
, FALSE
);
569 switch (AlertFileDoesNotExist(file_name
)) {
571 DoOpenFile(file_name
);
583 /***********************************************************************
587 int WINAPI
_tWinMain(HINSTANCE hInstance
, HINSTANCE prev
, LPTSTR cmdline
, int show
)
596 static const TCHAR className
[] = _T("Notepad");
597 static const TCHAR winName
[] = _T("Notepad");
599 switch (GetUserDefaultUILanguage())
601 case MAKELANGID(LANG_HEBREW
, SUBLANG_DEFAULT
):
602 SetProcessDefaultLayout(LAYOUT_RTL
);
609 UNREFERENCED_PARAMETER(prev
);
611 aFINDMSGSTRING
= (ATOM
)RegisterWindowMessage(FINDMSGSTRING
);
613 ZeroMemory(&Globals
, sizeof(Globals
));
614 Globals
.hInstance
= hInstance
;
615 NOTEPAD_LoadSettingsFromRegistry();
617 ZeroMemory(&wndclass
, sizeof(wndclass
));
618 wndclass
.cbSize
= sizeof(wndclass
);
619 wndclass
.lpfnWndProc
= NOTEPAD_WndProc
;
620 wndclass
.hInstance
= Globals
.hInstance
;
621 wndclass
.hIcon
= LoadIcon(hInstance
, MAKEINTRESOURCE(IDI_NPICON
));
622 wndclass
.hCursor
= LoadCursor(0, IDC_ARROW
);
623 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
624 wndclass
.lpszMenuName
= MAKEINTRESOURCE(MAIN_MENU
);
625 wndclass
.lpszClassName
= className
;
626 wndclass
.hIconSm
= (HICON
)LoadImage(hInstance
,
627 MAKEINTRESOURCE(IDI_NPICON
),
633 if (!RegisterClassEx(&wndclass
)) return FALSE
;
637 monitor
= MonitorFromRect(&Globals
.main_rect
, MONITOR_DEFAULTTOPRIMARY
);
638 info
.cbSize
= sizeof(info
);
639 GetMonitorInfoW(monitor
, &info
);
641 x
= Globals
.main_rect
.left
;
642 y
= Globals
.main_rect
.top
;
643 if (Globals
.main_rect
.left
>= info
.rcWork
.right
||
644 Globals
.main_rect
.top
>= info
.rcWork
.bottom
||
645 Globals
.main_rect
.right
< info
.rcWork
.left
||
646 Globals
.main_rect
.bottom
< info
.rcWork
.top
)
647 x
= y
= CW_USEDEFAULT
;
649 Globals
.hMainWnd
= CreateWindow(className
,
654 Globals
.main_rect
.right
- Globals
.main_rect
.left
,
655 Globals
.main_rect
.bottom
- Globals
.main_rect
.top
,
660 if (!Globals
.hMainWnd
)
666 DoCreateEditWindow();
671 ShowWindow(Globals
.hMainWnd
, show
);
672 UpdateWindow(Globals
.hMainWnd
);
673 DragAcceptFiles(Globals
.hMainWnd
, TRUE
);
675 DIALOG_ViewStatusBar();
677 if (!HandleCommandLine(cmdline
))
682 hAccel
= LoadAccelerators(hInstance
, MAKEINTRESOURCE(ID_ACCEL
));
684 while (GetMessage(&msg
, 0, 0, 0))
686 if (!IsDialogMessage(Globals
.hFindReplaceDlg
, &msg
) &&
687 !TranslateAccelerator(Globals
.hMainWnd
, hAccel
, &msg
))
689 TranslateMessage(&msg
);
690 DispatchMessage(&msg
);
693 return (int) msg
.wParam
;