d679d08c0f92e650779107b346af16a9e4e21038
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
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 Street, Fifth Floor, Boston, MA 02110-1301 USA
29 NOTEPAD_GLOBALS Globals
;
30 static ATOM aFINDMSGSTRING
;
32 VOID
NOTEPAD_EnableSearchMenu()
34 EnableMenuItem(Globals
.hMenu
, CMD_SEARCH
,
35 MF_BYCOMMAND
| ((GetWindowTextLength(Globals
.hEdit
) == 0) ? MF_DISABLED
| MF_GRAYED
: MF_ENABLED
));
36 EnableMenuItem(Globals
.hMenu
, CMD_SEARCH_NEXT
,
37 MF_BYCOMMAND
| ((GetWindowTextLength(Globals
.hEdit
) == 0) ? MF_DISABLED
| MF_GRAYED
: MF_ENABLED
));
40 /***********************************************************************
44 * Sets Global File Name.
46 VOID
SetFileName(LPCTSTR szFileName
)
48 StringCchCopy(Globals
.szFileName
, ARRAY_SIZE(Globals
.szFileName
), szFileName
);
49 Globals
.szFileTitle
[0] = 0;
50 GetFileTitle(szFileName
, Globals
.szFileTitle
, ARRAY_SIZE(Globals
.szFileTitle
));
53 /***********************************************************************
57 * All handling of main menu events
59 static int NOTEPAD_MenuCommand(WPARAM wParam
)
63 case CMD_NEW
: DIALOG_FileNew(); break;
64 case CMD_OPEN
: DIALOG_FileOpen(); break;
65 case CMD_SAVE
: DIALOG_FileSave(); break;
66 case CMD_SAVE_AS
: DIALOG_FileSaveAs(); break;
67 case CMD_PRINT
: DIALOG_FilePrint(); break;
68 case CMD_PAGE_SETUP
: DIALOG_FilePageSetup(); break;
69 case CMD_EXIT
: DIALOG_FileExit(); break;
71 case CMD_UNDO
: DIALOG_EditUndo(); break;
72 case CMD_CUT
: DIALOG_EditCut(); break;
73 case CMD_COPY
: DIALOG_EditCopy(); break;
74 case CMD_PASTE
: DIALOG_EditPaste(); break;
75 case CMD_DELETE
: DIALOG_EditDelete(); break;
76 case CMD_SELECT_ALL
: DIALOG_EditSelectAll(); break;
77 case CMD_TIME_DATE
: DIALOG_EditTimeDate(); break;
79 case CMD_SEARCH
: DIALOG_Search(); break;
80 case CMD_SEARCH_NEXT
: DIALOG_SearchNext(); break;
81 case CMD_REPLACE
: DIALOG_Replace(); break;
82 case CMD_GOTO
: DIALOG_GoTo(); break;
84 case CMD_WRAP
: DIALOG_EditWrap(); break;
85 case CMD_FONT
: DIALOG_SelectFont(); break;
87 case CMD_STATUSBAR
: DIALOG_ViewStatusBar(); break;
89 case CMD_HELP_CONTENTS
: DIALOG_HelpContents(); break;
92 DialogBox(GetModuleHandle(NULL
),
93 MAKEINTRESOURCE(IDD_ABOUTBOX
),
98 case CMD_HELP_ABOUT_NOTEPAD
: DIALOG_HelpAboutNotepad(); break;
106 /***********************************************************************
112 NOTEPAD_FindTextAt(FINDREPLACE
*pFindReplace
, LPCTSTR pszText
, int iTextLength
, DWORD dwPosition
)
115 size_t iTargetLength
;
117 if ((!pFindReplace
) || (!pszText
))
122 iTargetLength
= _tcslen(pFindReplace
->lpstrFindWhat
);
124 /* Make proper comparison */
125 if (pFindReplace
->Flags
& FR_MATCHCASE
)
126 bMatches
= !_tcsncmp(&pszText
[dwPosition
], pFindReplace
->lpstrFindWhat
, iTargetLength
);
128 bMatches
= !_tcsnicmp(&pszText
[dwPosition
], pFindReplace
->lpstrFindWhat
, iTargetLength
);
130 if (bMatches
&& pFindReplace
->Flags
& FR_WHOLEWORD
)
132 if ((dwPosition
> 0) && !_istspace(pszText
[dwPosition
-1]))
134 if ((dwPosition
< (DWORD
) iTextLength
- 1) && !_istspace(pszText
[dwPosition
+1]))
141 /***********************************************************************
146 BOOL
NOTEPAD_FindNext(FINDREPLACE
*pFindReplace
, BOOL bReplace
, BOOL bShowAlert
)
148 int iTextLength
, iTargetLength
;
149 size_t iAdjustment
= 0;
150 LPTSTR pszText
= NULL
;
151 DWORD dwPosition
, dwBegin
, dwEnd
;
152 BOOL bMatches
= FALSE
;
153 TCHAR szResource
[128], szText
[128];
156 iTargetLength
= (int) _tcslen(pFindReplace
->lpstrFindWhat
);
158 /* Retrieve the window text */
159 iTextLength
= GetWindowTextLength(Globals
.hEdit
);
162 pszText
= (LPTSTR
) HeapAlloc(GetProcessHeap(), 0, (iTextLength
+ 1) * sizeof(TCHAR
));
166 GetWindowText(Globals
.hEdit
, pszText
, iTextLength
+ 1);
169 SendMessage(Globals
.hEdit
, EM_GETSEL
, (WPARAM
) &dwBegin
, (LPARAM
) &dwEnd
);
170 if (bReplace
&& ((dwEnd
- dwBegin
) == (DWORD
) iTargetLength
))
172 if (NOTEPAD_FindTextAt(pFindReplace
, pszText
, iTextLength
, dwBegin
))
174 SendMessage(Globals
.hEdit
, EM_REPLACESEL
, TRUE
, (LPARAM
) pFindReplace
->lpstrReplaceWith
);
175 iAdjustment
= _tcslen(pFindReplace
->lpstrReplaceWith
) - (dwEnd
- dwBegin
);
179 if (pFindReplace
->Flags
& FR_DOWN
)
183 while(dwPosition
< (DWORD
) iTextLength
)
185 bMatches
= NOTEPAD_FindTextAt(pFindReplace
, pszText
, iTextLength
, dwPosition
);
194 dwPosition
= dwBegin
;
195 while(dwPosition
> 0)
198 bMatches
= NOTEPAD_FindTextAt(pFindReplace
, pszText
, iTextLength
, dwPosition
);
207 if (dwPosition
> dwBegin
)
208 dwPosition
+= (DWORD
) iAdjustment
;
209 SendMessage(Globals
.hEdit
, EM_SETSEL
, dwPosition
, dwPosition
+ iTargetLength
);
210 SendMessage(Globals
.hEdit
, EM_SCROLLCARET
, 0, 0);
215 /* Can't find target */
218 LoadString(Globals
.hInstance
, STRING_CANNOTFIND
, szResource
, ARRAY_SIZE(szResource
));
219 _sntprintf(szText
, ARRAY_SIZE(szText
), szResource
, pFindReplace
->lpstrFindWhat
);
220 LoadString(Globals
.hInstance
, STRING_NOTEPAD
, szResource
, ARRAY_SIZE(szResource
));
221 MessageBox(Globals
.hFindReplaceDlg
, szText
, szResource
, MB_OK
);
227 HeapFree(GetProcessHeap(), 0, pszText
);
231 /***********************************************************************
236 static VOID
NOTEPAD_ReplaceAll(FINDREPLACE
*pFindReplace
)
238 BOOL bShowAlert
= TRUE
;
240 SendMessage(Globals
.hEdit
, EM_SETSEL
, 0, 0);
242 while (NOTEPAD_FindNext(pFindReplace
, TRUE
, bShowAlert
))
248 /***********************************************************************
253 static VOID
NOTEPAD_FindTerm(VOID
)
255 Globals
.hFindReplaceDlg
= NULL
;
258 /***********************************************************************
259 * Data Initialization
261 static VOID
NOTEPAD_InitData(VOID
)
263 LPTSTR p
= Globals
.szFilter
;
264 static const TCHAR txt_files
[] = _T("*.txt");
265 static const TCHAR all_files
[] = _T("*.*");
267 p
+= LoadString(Globals
.hInstance
, STRING_TEXT_FILES_TXT
, p
, MAX_STRING_LEN
) + 1;
268 _tcscpy(p
, txt_files
);
269 p
+= ARRAY_SIZE(txt_files
);
271 p
+= LoadString(Globals
.hInstance
, STRING_ALL_FILES
, p
, MAX_STRING_LEN
) + 1;
272 _tcscpy(p
, all_files
);
273 p
+= ARRAY_SIZE(all_files
);
275 Globals
.find
.lpstrFindWhat
= NULL
;
277 Globals
.hDevMode
= NULL
;
278 Globals
.hDevNames
= NULL
;
281 /***********************************************************************
282 * Enable/disable items on the menu based on control state
284 static VOID
NOTEPAD_InitMenuPopup(HMENU menu
, LPARAM index
)
288 UNREFERENCED_PARAMETER(index
);
290 CheckMenuItem(GetMenu(Globals
.hMainWnd
), CMD_WRAP
,
291 MF_BYCOMMAND
| (Globals
.bWrapLongLines
? MF_CHECKED
: MF_UNCHECKED
));
292 if (!Globals
.bWrapLongLines
)
294 CheckMenuItem(GetMenu(Globals
.hMainWnd
), CMD_STATUSBAR
,
295 MF_BYCOMMAND
| (Globals
.bShowStatusBar
? MF_CHECKED
: MF_UNCHECKED
));
297 EnableMenuItem(menu
, CMD_UNDO
,
298 SendMessage(Globals
.hEdit
, EM_CANUNDO
, 0, 0) ? MF_ENABLED
: MF_GRAYED
);
299 EnableMenuItem(menu
, CMD_PASTE
,
300 IsClipboardFormatAvailable(CF_TEXT
) ? MF_ENABLED
: MF_GRAYED
);
301 enable
= (int) SendMessage(Globals
.hEdit
, EM_GETSEL
, 0, 0);
302 enable
= (HIWORD(enable
) == LOWORD(enable
)) ? MF_GRAYED
: MF_ENABLED
;
303 EnableMenuItem(menu
, CMD_CUT
, enable
);
304 EnableMenuItem(menu
, CMD_COPY
, enable
);
305 EnableMenuItem(menu
, CMD_DELETE
, enable
);
307 EnableMenuItem(menu
, CMD_SELECT_ALL
,
308 GetWindowTextLength(Globals
.hEdit
) ? MF_ENABLED
: MF_GRAYED
);
309 DrawMenuBar(Globals
.hMainWnd
);
312 LRESULT CALLBACK
EDIT_WndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
325 DIALOG_StatusBarUpdateCaretPos();
331 DIALOG_StatusBarUpdateCaretPos();
335 return CallWindowProc( Globals
.EditProc
, hWnd
, msg
, wParam
, lParam
);
338 /***********************************************************************
344 NOTEPAD_WndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
350 Globals
.hMenu
= GetMenu(hWnd
);
354 if (HIWORD(wParam
) == EN_CHANGE
|| HIWORD(wParam
) == EN_HSCROLL
|| HIWORD(wParam
) == EN_VSCROLL
)
355 DIALOG_StatusBarUpdateCaretPos();
356 if ((HIWORD(wParam
) == EN_CHANGE
))
357 NOTEPAD_EnableSearchMenu();
358 NOTEPAD_MenuCommand(LOWORD(wParam
));
361 case WM_DESTROYCLIPBOARD
:
362 /*MessageBox(Globals.hMainWnd, "Empty clipboard", "Debug", MB_ICONEXCLAMATION);*/
368 DeleteObject(Globals
.hFont
);
369 if (Globals
.hDevMode
)
370 GlobalFree(Globals
.hDevMode
);
371 if (Globals
.hDevNames
)
372 GlobalFree(Globals
.hDevNames
);
377 case WM_QUERYENDSESSION
:
384 SetWindowLongPtr(Globals
.hEdit
, GWLP_WNDPROC
, (LONG_PTR
)Globals
.EditProc
);
385 NOTEPAD_SaveSettingsToRegistry();
391 if (Globals
.bShowStatusBar
== TRUE
&& Globals
.bWrapLongLines
== FALSE
)
396 if (!GetWindowRect(Globals
.hStatusBar
, &rcStatusBar
))
399 hdwp
= BeginDeferWindowPos(2);
403 hdwp
= DeferWindowPos(hdwp
,
409 HIWORD(lParam
) - (rcStatusBar
.bottom
- rcStatusBar
.top
),
410 SWP_NOZORDER
| SWP_NOMOVE
);
415 hdwp
= DeferWindowPos(hdwp
,
425 EndDeferWindowPos(hdwp
);
428 SetWindowPos(Globals
.hEdit
,
434 SWP_NOZORDER
| SWP_NOMOVE
);
439 /* The entire client area is covered by edit control and by
440 * the status bar. So there is no need to erase main background.
441 * This resolves the horrible flicker effect during windows resizes. */
446 SetFocus(Globals
.hEdit
);
451 TCHAR szFileName
[MAX_PATH
];
452 HDROP hDrop
= (HDROP
) wParam
;
454 DragQueryFile(hDrop
, 0, szFileName
, ARRAY_SIZE(szFileName
));
456 DoOpenFile(szFileName
);
460 case WM_INITMENUPOPUP
:
461 NOTEPAD_InitMenuPopup((HMENU
)wParam
, lParam
);
464 if (msg
== aFINDMSGSTRING
)
466 FINDREPLACE
*pFindReplace
= (FINDREPLACE
*) lParam
;
467 Globals
.find
= *(FINDREPLACE
*) lParam
;
469 if (pFindReplace
->Flags
& FR_FINDNEXT
)
470 NOTEPAD_FindNext(pFindReplace
, FALSE
, TRUE
);
471 else if (pFindReplace
->Flags
& FR_REPLACE
)
472 NOTEPAD_FindNext(pFindReplace
, TRUE
, TRUE
);
473 else if (pFindReplace
->Flags
& FR_REPLACEALL
)
474 NOTEPAD_ReplaceAll(pFindReplace
);
475 else if (pFindReplace
->Flags
& FR_DIALOGTERM
)
480 return DefWindowProc(hWnd
, msg
, wParam
, lParam
);
485 static int AlertFileDoesNotExist(LPCTSTR szFileName
)
487 return DIALOG_StringMsgBox(Globals
.hMainWnd
, STRING_DOESNOTEXIST
,
489 MB_ICONEXCLAMATION
| MB_YESNO
);
492 static BOOL
HandleCommandLine(LPTSTR cmdline
)
494 BOOL opt_print
= FALSE
;
496 while (*cmdline
== _T(' ') || *cmdline
== _T('-') || *cmdline
== _T('/'))
500 if (*cmdline
++ == _T(' ')) continue;
503 if (option
) cmdline
++;
504 while (*cmdline
== _T(' ')) cmdline
++;
517 /* file name is passed in the command line */
518 LPCTSTR file_name
= NULL
;
519 BOOL file_exists
= FALSE
;
522 if (cmdline
[0] == _T('"'))
525 cmdline
[lstrlen(cmdline
) - 1] = 0;
529 if (FileExists(file_name
))
533 else if (!HasFileExtension(cmdline
))
535 static const TCHAR txt
[] = _T(".txt");
537 /* try to find file with ".txt" extension */
538 if (!_tcscmp(txt
, cmdline
+ _tcslen(cmdline
) - _tcslen(txt
)))
544 _tcsncpy(buf
, cmdline
, MAX_PATH
- _tcslen(txt
) - 1);
547 file_exists
= FileExists(file_name
);
553 DoOpenFile(file_name
);
554 InvalidateRect(Globals
.hMainWnd
, NULL
, FALSE
);
563 switch (AlertFileDoesNotExist(file_name
)) {
565 DoOpenFile(file_name
);
577 /***********************************************************************
581 int WINAPI
_tWinMain(HINSTANCE hInstance
, HINSTANCE prev
, LPTSTR cmdline
, int show
)
590 static const TCHAR className
[] = _T("NPClass");
591 static const TCHAR winName
[] = _T("Notepad");
593 switch (GetUserDefaultUILanguage())
595 case MAKELANGID(LANG_HEBREW
, SUBLANG_DEFAULT
):
596 SetProcessDefaultLayout(LAYOUT_RTL
);
603 UNREFERENCED_PARAMETER(prev
);
605 aFINDMSGSTRING
= (ATOM
)RegisterWindowMessage(FINDMSGSTRING
);
607 ZeroMemory(&Globals
, sizeof(Globals
));
608 Globals
.hInstance
= hInstance
;
609 NOTEPAD_LoadSettingsFromRegistry();
611 ZeroMemory(&wndclass
, sizeof(wndclass
));
612 wndclass
.cbSize
= sizeof(wndclass
);
613 wndclass
.lpfnWndProc
= NOTEPAD_WndProc
;
614 wndclass
.hInstance
= Globals
.hInstance
;
615 wndclass
.hIcon
= LoadIcon(hInstance
, MAKEINTRESOURCE(IDI_NPICON
));
616 wndclass
.hCursor
= LoadCursor(0, IDC_ARROW
);
617 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
618 wndclass
.lpszMenuName
= MAKEINTRESOURCE(MAIN_MENU
);
619 wndclass
.lpszClassName
= className
;
620 wndclass
.hIconSm
= (HICON
)LoadImage(hInstance
,
621 MAKEINTRESOURCE(IDI_NPICON
),
627 if (!RegisterClassEx(&wndclass
)) return FALSE
;
631 monitor
= MonitorFromRect(&Globals
.main_rect
, MONITOR_DEFAULTTOPRIMARY
);
632 info
.cbSize
= sizeof(info
);
633 GetMonitorInfoW(monitor
, &info
);
635 x
= Globals
.main_rect
.left
;
636 y
= Globals
.main_rect
.top
;
637 if (Globals
.main_rect
.left
>= info
.rcWork
.right
||
638 Globals
.main_rect
.top
>= info
.rcWork
.bottom
||
639 Globals
.main_rect
.right
< info
.rcWork
.left
||
640 Globals
.main_rect
.bottom
< info
.rcWork
.top
)
641 x
= y
= CW_USEDEFAULT
;
643 Globals
.hMainWnd
= CreateWindow(className
,
648 Globals
.main_rect
.right
- Globals
.main_rect
.left
,
649 Globals
.main_rect
.bottom
- Globals
.main_rect
.top
,
654 if (!Globals
.hMainWnd
)
660 DoCreateEditWindow();
665 ShowWindow(Globals
.hMainWnd
, show
);
666 UpdateWindow(Globals
.hMainWnd
);
667 DragAcceptFiles(Globals
.hMainWnd
, TRUE
);
669 DIALOG_ViewStatusBar();
671 if (!HandleCommandLine(cmdline
))
676 hAccel
= LoadAccelerators(hInstance
, MAKEINTRESOURCE(ID_ACCEL
));
678 while (GetMessage(&msg
, 0, 0, 0))
680 if (!IsDialogMessage(Globals
.hFindReplaceDlg
, &msg
) &&
681 !TranslateAccelerator(Globals
.hMainWnd
, hAccel
, &msg
))
683 TranslateMessage(&msg
);
684 DispatchMessage(&msg
);
687 return (int) msg
.wParam
;