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();
329 UpdateWindowCaption(FALSE
);
336 DIALOG_StatusBarUpdateCaretPos();
340 return CallWindowProc( Globals
.EditProc
, hWnd
, msg
, wParam
, lParam
);
343 /***********************************************************************
349 NOTEPAD_WndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
355 Globals
.hMenu
= GetMenu(hWnd
);
359 if (HIWORD(wParam
) == EN_CHANGE
|| HIWORD(wParam
) == EN_HSCROLL
|| HIWORD(wParam
) == EN_VSCROLL
)
360 DIALOG_StatusBarUpdateCaretPos();
361 if ((HIWORD(wParam
) == EN_CHANGE
))
362 NOTEPAD_EnableSearchMenu();
363 NOTEPAD_MenuCommand(LOWORD(wParam
));
366 case WM_DESTROYCLIPBOARD
:
367 /*MessageBox(Globals.hMainWnd, "Empty clipboard", "Debug", MB_ICONEXCLAMATION);*/
373 DeleteObject(Globals
.hFont
);
374 if (Globals
.hDevMode
)
375 GlobalFree(Globals
.hDevMode
);
376 if (Globals
.hDevNames
)
377 GlobalFree(Globals
.hDevNames
);
382 case WM_QUERYENDSESSION
:
389 SetWindowLongPtr(Globals
.hEdit
, GWLP_WNDPROC
, (LONG_PTR
)Globals
.EditProc
);
390 NOTEPAD_SaveSettingsToRegistry();
396 if (Globals
.bShowStatusBar
== TRUE
&& Globals
.bWrapLongLines
== FALSE
)
401 if (!GetWindowRect(Globals
.hStatusBar
, &rcStatusBar
))
404 hdwp
= BeginDeferWindowPos(2);
408 hdwp
= DeferWindowPos(hdwp
,
414 HIWORD(lParam
) - (rcStatusBar
.bottom
- rcStatusBar
.top
),
415 SWP_NOZORDER
| SWP_NOMOVE
);
420 hdwp
= DeferWindowPos(hdwp
,
430 EndDeferWindowPos(hdwp
);
433 SetWindowPos(Globals
.hEdit
,
439 SWP_NOZORDER
| SWP_NOMOVE
);
444 /* The entire client area is covered by edit control and by
445 * the status bar. So there is no need to erase main background.
446 * This resolves the horrible flicker effect during windows resizes. */
451 SetFocus(Globals
.hEdit
);
456 TCHAR szFileName
[MAX_PATH
];
457 HDROP hDrop
= (HDROP
) wParam
;
459 DragQueryFile(hDrop
, 0, szFileName
, ARRAY_SIZE(szFileName
));
461 DoOpenFile(szFileName
);
465 case WM_INITMENUPOPUP
:
466 NOTEPAD_InitMenuPopup((HMENU
)wParam
, lParam
);
469 if (msg
== aFINDMSGSTRING
)
471 FINDREPLACE
*pFindReplace
= (FINDREPLACE
*) lParam
;
472 Globals
.find
= *(FINDREPLACE
*) lParam
;
474 if (pFindReplace
->Flags
& FR_FINDNEXT
)
475 NOTEPAD_FindNext(pFindReplace
, FALSE
, TRUE
);
476 else if (pFindReplace
->Flags
& FR_REPLACE
)
477 NOTEPAD_FindNext(pFindReplace
, TRUE
, TRUE
);
478 else if (pFindReplace
->Flags
& FR_REPLACEALL
)
479 NOTEPAD_ReplaceAll(pFindReplace
);
480 else if (pFindReplace
->Flags
& FR_DIALOGTERM
)
485 return DefWindowProc(hWnd
, msg
, wParam
, lParam
);
490 static int AlertFileDoesNotExist(LPCTSTR szFileName
)
492 return DIALOG_StringMsgBox(Globals
.hMainWnd
, STRING_DOESNOTEXIST
,
494 MB_ICONEXCLAMATION
| MB_YESNO
);
497 static BOOL
HandleCommandLine(LPTSTR cmdline
)
499 BOOL opt_print
= FALSE
;
501 while (*cmdline
== _T(' ') || *cmdline
== _T('-') || *cmdline
== _T('/'))
505 if (*cmdline
++ == _T(' ')) continue;
508 if (option
) cmdline
++;
509 while (*cmdline
== _T(' ')) cmdline
++;
522 /* file name is passed in the command line */
523 LPCTSTR file_name
= NULL
;
524 BOOL file_exists
= FALSE
;
527 if (cmdline
[0] == _T('"'))
530 cmdline
[lstrlen(cmdline
) - 1] = 0;
534 if (FileExists(file_name
))
538 else if (!HasFileExtension(cmdline
))
540 static const TCHAR txt
[] = _T(".txt");
542 /* try to find file with ".txt" extension */
543 if (!_tcscmp(txt
, cmdline
+ _tcslen(cmdline
) - _tcslen(txt
)))
549 _tcsncpy(buf
, cmdline
, MAX_PATH
- _tcslen(txt
) - 1);
552 file_exists
= FileExists(file_name
);
558 DoOpenFile(file_name
);
559 InvalidateRect(Globals
.hMainWnd
, NULL
, FALSE
);
568 switch (AlertFileDoesNotExist(file_name
)) {
570 DoOpenFile(file_name
);
582 /***********************************************************************
586 int WINAPI
_tWinMain(HINSTANCE hInstance
, HINSTANCE prev
, LPTSTR cmdline
, int show
)
595 static const TCHAR className
[] = _T("Notepad");
596 static const TCHAR winName
[] = _T("Notepad");
598 switch (GetUserDefaultUILanguage())
600 case MAKELANGID(LANG_HEBREW
, SUBLANG_DEFAULT
):
601 SetProcessDefaultLayout(LAYOUT_RTL
);
608 UNREFERENCED_PARAMETER(prev
);
610 aFINDMSGSTRING
= (ATOM
)RegisterWindowMessage(FINDMSGSTRING
);
612 ZeroMemory(&Globals
, sizeof(Globals
));
613 Globals
.hInstance
= hInstance
;
614 NOTEPAD_LoadSettingsFromRegistry();
616 ZeroMemory(&wndclass
, sizeof(wndclass
));
617 wndclass
.cbSize
= sizeof(wndclass
);
618 wndclass
.lpfnWndProc
= NOTEPAD_WndProc
;
619 wndclass
.hInstance
= Globals
.hInstance
;
620 wndclass
.hIcon
= LoadIcon(hInstance
, MAKEINTRESOURCE(IDI_NPICON
));
621 wndclass
.hCursor
= LoadCursor(0, IDC_ARROW
);
622 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
623 wndclass
.lpszMenuName
= MAKEINTRESOURCE(MAIN_MENU
);
624 wndclass
.lpszClassName
= className
;
625 wndclass
.hIconSm
= (HICON
)LoadImage(hInstance
,
626 MAKEINTRESOURCE(IDI_NPICON
),
632 if (!RegisterClassEx(&wndclass
)) return FALSE
;
636 monitor
= MonitorFromRect(&Globals
.main_rect
, MONITOR_DEFAULTTOPRIMARY
);
637 info
.cbSize
= sizeof(info
);
638 GetMonitorInfoW(monitor
, &info
);
640 x
= Globals
.main_rect
.left
;
641 y
= Globals
.main_rect
.top
;
642 if (Globals
.main_rect
.left
>= info
.rcWork
.right
||
643 Globals
.main_rect
.top
>= info
.rcWork
.bottom
||
644 Globals
.main_rect
.right
< info
.rcWork
.left
||
645 Globals
.main_rect
.bottom
< info
.rcWork
.top
)
646 x
= y
= CW_USEDEFAULT
;
648 Globals
.hMainWnd
= CreateWindow(className
,
653 Globals
.main_rect
.right
- Globals
.main_rect
.left
,
654 Globals
.main_rect
.bottom
- Globals
.main_rect
.top
,
659 if (!Globals
.hMainWnd
)
665 DoCreateEditWindow();
670 ShowWindow(Globals
.hMainWnd
, show
);
671 UpdateWindow(Globals
.hMainWnd
);
672 DragAcceptFiles(Globals
.hMainWnd
, TRUE
);
674 DIALOG_ViewStatusBar();
676 if (!HandleCommandLine(cmdline
))
681 hAccel
= LoadAccelerators(hInstance
, MAKEINTRESOURCE(ID_ACCEL
));
683 while (GetMessage(&msg
, 0, 0, 0))
685 if (!IsDialogMessage(Globals
.hFindReplaceDlg
, &msg
) &&
686 !TranslateAccelerator(Globals
.hMainWnd
, hAccel
, &msg
))
688 TranslateMessage(&msg
);
689 DispatchMessage(&msg
);
692 return (int) msg
.wParam
;