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
27 NOTEPAD_GLOBALS Globals
;
28 static ATOM aFINDMSGSTRING
;
30 VOID
NOTEPAD_EnableSearchMenu()
32 EnableMenuItem(Globals
.hMenu
, CMD_SEARCH
,
33 MF_BYCOMMAND
| ((GetWindowTextLength(Globals
.hEdit
) == 0) ? MF_DISABLED
| MF_GRAYED
: MF_ENABLED
));
34 EnableMenuItem(Globals
.hMenu
, CMD_SEARCH_NEXT
,
35 MF_BYCOMMAND
| ((GetWindowTextLength(Globals
.hEdit
) == 0) ? MF_DISABLED
| MF_GRAYED
: MF_ENABLED
));
38 /***********************************************************************
42 * Sets Global File Name.
44 VOID
SetFileName(LPCTSTR szFileName
)
46 _tcscpy(Globals
.szFileName
, szFileName
);
47 Globals
.szFileTitle
[0] = 0;
48 GetFileTitle(szFileName
, Globals
.szFileTitle
, SIZEOF(Globals
.szFileTitle
));
51 /***********************************************************************
55 * All handling of main menu events
57 static int NOTEPAD_MenuCommand(WPARAM wParam
)
61 case CMD_NEW
: DIALOG_FileNew(); break;
62 case CMD_OPEN
: DIALOG_FileOpen(); break;
63 case CMD_SAVE
: DIALOG_FileSave(); break;
64 case CMD_SAVE_AS
: DIALOG_FileSaveAs(); break;
65 case CMD_PRINT
: DIALOG_FilePrint(); break;
66 case CMD_PAGE_SETUP
: DIALOG_FilePageSetup(); break;
67 case CMD_PRINTER_SETUP
: DIALOG_FilePrinterSetup();break;
68 case CMD_EXIT
: DIALOG_FileExit(); break;
70 case CMD_UNDO
: DIALOG_EditUndo(); break;
71 case CMD_CUT
: DIALOG_EditCut(); break;
72 case CMD_COPY
: DIALOG_EditCopy(); break;
73 case CMD_PASTE
: DIALOG_EditPaste(); break;
74 case CMD_DELETE
: DIALOG_EditDelete(); break;
75 case CMD_SELECT_ALL
: DIALOG_EditSelectAll(); break;
76 case CMD_TIME_DATE
: DIALOG_EditTimeDate();break;
78 case CMD_SEARCH
: DIALOG_Search(); break;
79 case CMD_SEARCH_NEXT
: DIALOG_SearchNext(); break;
80 case CMD_REPLACE
: DIALOG_Replace(); break;
81 case CMD_GOTO
: DIALOG_GoTo(); break;
83 case CMD_WRAP
: DIALOG_EditWrap(); break;
84 case CMD_FONT
: DIALOG_SelectFont(); break;
86 case CMD_STATUSBAR
: DIALOG_ViewStatusBar(); break;
88 case CMD_HELP_CONTENTS
: DIALOG_HelpContents(); break;
89 case CMD_HELP_SEARCH
: DIALOG_HelpSearch(); break;
90 case CMD_HELP_ON_HELP
: DIALOG_HelpHelp(); break;
91 case CMD_ABOUT
: DialogBox(GetModuleHandle(NULL
),
92 MAKEINTRESOURCE(IDD_ABOUTBOX
),
96 case CMD_ABOUT_WINE
: DIALOG_HelpAboutWine(); break;
104 /***********************************************************************
109 static BOOL
NOTEPAD_FindTextAt(FINDREPLACE
*pFindReplace
, LPCTSTR pszText
, int iTextLength
, DWORD dwPosition
)
112 size_t iTargetLength
;
114 iTargetLength
= _tcslen(pFindReplace
->lpstrFindWhat
);
116 /* Make proper comparison */
117 if (pFindReplace
->Flags
& FR_MATCHCASE
)
118 bMatches
= !_tcsncmp(&pszText
[dwPosition
], pFindReplace
->lpstrFindWhat
, iTargetLength
);
120 bMatches
= !_tcsnicmp(&pszText
[dwPosition
], pFindReplace
->lpstrFindWhat
, iTargetLength
);
122 if (bMatches
&& pFindReplace
->Flags
& FR_WHOLEWORD
)
124 if ((dwPosition
> 0) && !_istspace(pszText
[dwPosition
-1]))
126 if ((dwPosition
< (DWORD
) iTextLength
- 1) && !_istspace(pszText
[dwPosition
+1]))
133 /***********************************************************************
138 BOOL
NOTEPAD_FindNext(FINDREPLACE
*pFindReplace
, BOOL bReplace
, BOOL bShowAlert
)
140 int iTextLength
, iTargetLength
;
141 size_t iAdjustment
= 0;
142 LPTSTR pszText
= NULL
;
143 DWORD dwPosition
, dwBegin
, dwEnd
;
144 BOOL bMatches
= FALSE
;
145 TCHAR szResource
[128], szText
[128];
148 iTargetLength
= (int) _tcslen(pFindReplace
->lpstrFindWhat
);
150 /* Retrieve the window text */
151 iTextLength
= GetWindowTextLength(Globals
.hEdit
);
154 pszText
= (LPTSTR
) HeapAlloc(GetProcessHeap(), 0, (iTextLength
+ 1) * sizeof(TCHAR
));
158 GetWindowText(Globals
.hEdit
, pszText
, iTextLength
+ 1);
161 SendMessage(Globals
.hEdit
, EM_GETSEL
, (WPARAM
) &dwBegin
, (LPARAM
) &dwEnd
);
162 if (bReplace
&& ((dwEnd
- dwBegin
) == (DWORD
) iTargetLength
))
164 if (NOTEPAD_FindTextAt(pFindReplace
, pszText
, iTextLength
, dwBegin
))
166 SendMessage(Globals
.hEdit
, EM_REPLACESEL
, TRUE
, (LPARAM
) pFindReplace
->lpstrReplaceWith
);
167 iAdjustment
= _tcslen(pFindReplace
->lpstrReplaceWith
) - (dwEnd
- dwBegin
);
171 if (pFindReplace
->Flags
& FR_DOWN
)
175 while(dwPosition
< (DWORD
) iTextLength
)
177 bMatches
= NOTEPAD_FindTextAt(pFindReplace
, pszText
, iTextLength
, dwPosition
);
186 dwPosition
= dwBegin
;
187 while(dwPosition
> 0)
190 bMatches
= NOTEPAD_FindTextAt(pFindReplace
, pszText
, iTextLength
, dwPosition
);
199 if (dwPosition
> dwBegin
)
200 dwPosition
+= (DWORD
) iAdjustment
;
201 SendMessage(Globals
.hEdit
, EM_SETSEL
, dwPosition
, dwPosition
+ iTargetLength
);
202 SendMessage(Globals
.hEdit
, EM_SCROLLCARET
, 0, 0);
207 /* Can't find target */
210 LoadString(Globals
.hInstance
, STRING_CANNOTFIND
, szResource
, SIZEOF(szResource
));
211 _sntprintf(szText
, SIZEOF(szText
), szResource
, pFindReplace
->lpstrFindWhat
);
212 LoadString(Globals
.hInstance
, STRING_NOTEPAD
, szResource
, SIZEOF(szResource
));
213 MessageBox(Globals
.hFindReplaceDlg
, szText
, szResource
, MB_OK
);
219 HeapFree(GetProcessHeap(), 0, pszText
);
223 /***********************************************************************
228 static VOID
NOTEPAD_ReplaceAll(FINDREPLACE
*pFindReplace
)
230 BOOL bShowAlert
= TRUE
;
232 SendMessage(Globals
.hEdit
, EM_SETSEL
, 0, 0);
234 while (NOTEPAD_FindNext(pFindReplace
, TRUE
, bShowAlert
))
240 /***********************************************************************
245 static VOID
NOTEPAD_FindTerm(VOID
)
247 Globals
.hFindReplaceDlg
= NULL
;
250 /***********************************************************************
251 * Data Initialization
253 static VOID
NOTEPAD_InitData(VOID
)
255 LPTSTR p
= Globals
.szFilter
;
256 static const TCHAR txt_files
[] = _T("*.txt");
257 static const TCHAR all_files
[] = _T("*.*");
259 p
+= LoadString(Globals
.hInstance
, STRING_TEXT_FILES_TXT
, p
, MAX_STRING_LEN
)+1;
260 _tcscpy(p
, txt_files
);
261 p
+= SIZEOF(txt_files
);
263 p
+= LoadString(Globals
.hInstance
, STRING_ALL_FILES
, p
, MAX_STRING_LEN
)+1;
264 _tcscpy(p
, all_files
);
265 p
+= SIZEOF(all_files
);
267 Globals
.find
.lpstrFindWhat
= NULL
;
270 /***********************************************************************
271 * Enable/disable items on the menu based on control state
273 static VOID
NOTEPAD_InitMenuPopup(HMENU menu
, LPARAM index
)
277 UNREFERENCED_PARAMETER(index
);
279 CheckMenuItem(GetMenu(Globals
.hMainWnd
), CMD_WRAP
,
280 MF_BYCOMMAND
| (Globals
.bWrapLongLines
? MF_CHECKED
: MF_UNCHECKED
));
281 if ( !Globals
.bWrapLongLines
)
283 CheckMenuItem(GetMenu(Globals
.hMainWnd
), CMD_STATUSBAR
,
284 MF_BYCOMMAND
| (Globals
.bShowStatusBar
? MF_CHECKED
: MF_UNCHECKED
));
286 EnableMenuItem(menu
, CMD_UNDO
,
287 SendMessage(Globals
.hEdit
, EM_CANUNDO
, 0, 0) ? MF_ENABLED
: MF_GRAYED
);
288 EnableMenuItem(menu
, CMD_PASTE
,
289 IsClipboardFormatAvailable(CF_TEXT
) ? MF_ENABLED
: MF_GRAYED
);
290 enable
= (int) SendMessage(Globals
.hEdit
, EM_GETSEL
, 0, 0);
291 enable
= (HIWORD(enable
) == LOWORD(enable
)) ? MF_GRAYED
: MF_ENABLED
;
292 EnableMenuItem(menu
, CMD_CUT
, enable
);
293 EnableMenuItem(menu
, CMD_COPY
, enable
);
294 EnableMenuItem(menu
, CMD_DELETE
, enable
);
296 EnableMenuItem(menu
, CMD_SELECT_ALL
,
297 GetWindowTextLength(Globals
.hEdit
) ? MF_ENABLED
: MF_GRAYED
);
298 DrawMenuBar(Globals
.hMainWnd
);
301 LRESULT CALLBACK
EDIT_WndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
314 DIALOG_StatusBarUpdateCaretPos();
320 DIALOG_StatusBarUpdateCaretPos();
324 return CallWindowProc( (WNDPROC
)Globals
.EditProc
, hWnd
, msg
, wParam
, lParam
);
327 /***********************************************************************
331 static LRESULT WINAPI
NOTEPAD_WndProc(HWND hWnd
, UINT msg
, WPARAM wParam
,
337 Globals
.hMenu
= GetMenu(hWnd
);
341 if (HIWORD(wParam
) == EN_CHANGE
|| HIWORD(wParam
) == EN_HSCROLL
|| HIWORD(wParam
) == EN_VSCROLL
)
342 DIALOG_StatusBarUpdateCaretPos();
343 if ((HIWORD(wParam
) == EN_CHANGE
))
344 NOTEPAD_EnableSearchMenu();
345 NOTEPAD_MenuCommand(LOWORD(wParam
));
348 case WM_DESTROYCLIPBOARD
:
349 /*MessageBox(Globals.hMainWnd, "Empty clipboard", "Debug", MB_ICONEXCLAMATION);*/
355 DeleteObject(Globals
.hFont
);
360 case WM_QUERYENDSESSION
:
367 SetWindowLongPtr(Globals
.hEdit
, GWLP_WNDPROC
, (LONG_PTR
)Globals
.EditProc
);
374 if (Globals
.bShowStatusBar
== TRUE
&&
375 Globals
.bWrapLongLines
== FALSE
)
380 if (!GetWindowRect(Globals
.hStatusBar
, &rcStatusBar
))
383 hdwp
= BeginDeferWindowPos(2);
387 hdwp
= DeferWindowPos(hdwp
, Globals
.hEdit
, NULL
, 0, 0, LOWORD(lParam
), HIWORD(lParam
) - (rcStatusBar
.bottom
- rcStatusBar
.top
), SWP_NOZORDER
| SWP_NOMOVE
);
391 hdwp
= DeferWindowPos(hdwp
, Globals
.hStatusBar
, NULL
, 0, 0, LOWORD(lParam
), LOWORD(wParam
), SWP_NOZORDER
);
394 EndDeferWindowPos(hdwp
);
397 SetWindowPos(Globals
.hEdit
, NULL
, 0, 0, LOWORD(lParam
), HIWORD(lParam
), SWP_NOZORDER
| SWP_NOMOVE
);
402 // The entire client area is covered by edit control and by
403 // the status bar. So there is no need to erase main background.
404 // This resolves the horrible fliker effect during windows resizes.
409 SetFocus(Globals
.hEdit
);
414 TCHAR szFileName
[MAX_PATH
];
415 HDROP hDrop
= (HDROP
) wParam
;
417 DragQueryFile(hDrop
, 0, szFileName
, SIZEOF(szFileName
));
419 DoOpenFile(szFileName
);
423 case WM_INITMENUPOPUP
:
424 NOTEPAD_InitMenuPopup((HMENU
)wParam
, lParam
);
427 if (msg
== aFINDMSGSTRING
)
429 FINDREPLACE
*pFindReplace
= (FINDREPLACE
*) lParam
;
430 Globals
.find
= *(FINDREPLACE
*) lParam
;
432 if (pFindReplace
->Flags
& FR_FINDNEXT
)
433 NOTEPAD_FindNext(pFindReplace
, FALSE
, TRUE
);
434 else if (pFindReplace
->Flags
& FR_REPLACE
)
435 NOTEPAD_FindNext(pFindReplace
, TRUE
, TRUE
);
436 else if (pFindReplace
->Flags
& FR_REPLACEALL
)
437 NOTEPAD_ReplaceAll(pFindReplace
);
438 else if (pFindReplace
->Flags
& FR_DIALOGTERM
)
443 return DefWindowProc(hWnd
, msg
, wParam
, lParam
);
448 static int AlertFileDoesNotExist(LPCTSTR szFileName
)
451 TCHAR szMessage
[MAX_STRING_LEN
];
452 TCHAR szResource
[MAX_STRING_LEN
];
454 LoadString(Globals
.hInstance
, STRING_DOESNOTEXIST
, szResource
, SIZEOF(szResource
));
455 wsprintf(szMessage
, szResource
, szFileName
);
457 LoadString(Globals
.hInstance
, STRING_NOTEPAD
, szResource
, SIZEOF(szResource
));
459 nResult
= MessageBox(Globals
.hMainWnd
, szMessage
, szResource
,
460 MB_ICONEXCLAMATION
| MB_YESNO
);
465 static void HandleCommandLine(LPTSTR cmdline
)
469 while (*cmdline
== _T(' ') || *cmdline
== _T('-') || *cmdline
== _T('/'))
473 if (*cmdline
++ == _T(' ')) continue;
476 if (option
) cmdline
++;
477 while (*cmdline
== _T(' ')) cmdline
++;
490 /* file name is passed in the command line */
491 LPCTSTR file_name
= NULL
;
492 BOOL file_exists
= FALSE
;
495 if (cmdline
[0] == _T('"'))
498 cmdline
[lstrlen(cmdline
) - 1] = 0;
502 if (FileExists(file_name
))
506 else if (!HasFileExtension(cmdline
))
508 static const TCHAR txt
[] = _T(".txt");
510 /* try to find file with ".txt" extension */
511 if (!_tcscmp(txt
, cmdline
+ _tcslen(cmdline
) - _tcslen(txt
)))
517 _tcsncpy(buf
, cmdline
, MAX_PATH
- _tcslen(txt
) - 1);
520 file_exists
= FileExists(file_name
);
526 DoOpenFile(file_name
);
527 InvalidateRect(Globals
.hMainWnd
, NULL
, FALSE
);
533 switch (AlertFileDoesNotExist(file_name
)) {
535 DoOpenFile(file_name
);
545 /***********************************************************************
549 int WINAPI
_tWinMain(HINSTANCE hInstance
, HINSTANCE prev
, LPTSTR cmdline
, int show
)
558 static const TCHAR className
[] = _T("NPClass");
559 static const TCHAR winName
[] = _T("Notepad");
561 UNREFERENCED_PARAMETER(prev
);
563 aFINDMSGSTRING
= (ATOM
) RegisterWindowMessage(FINDMSGSTRING
);
565 ZeroMemory(&Globals
, sizeof(Globals
));
566 Globals
.hInstance
= hInstance
;
569 ZeroMemory(&wndclass
, sizeof(wndclass
));
570 wndclass
.cbSize
= sizeof(wndclass
);
571 wndclass
.lpfnWndProc
= NOTEPAD_WndProc
;
572 wndclass
.hInstance
= Globals
.hInstance
;
573 wndclass
.hIcon
= LoadIcon(hInstance
, MAKEINTRESOURCE(IDI_NPICON
));
574 wndclass
.hCursor
= LoadCursor(0, IDC_ARROW
);
575 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
576 wndclass
.lpszMenuName
= MAKEINTRESOURCE(MAIN_MENU
);
577 wndclass
.lpszClassName
= className
;
578 wndclass
.hIconSm
= (HICON
)LoadImage(hInstance
, MAKEINTRESOURCE(IDI_NPICON
),
579 IMAGE_ICON
, 16, 16, 0);
581 if (!RegisterClassEx(&wndclass
)) return FALSE
;
585 monitor
= MonitorFromRect( &Globals
.main_rect
, MONITOR_DEFAULTTOPRIMARY
);
586 info
.cbSize
= sizeof(info
);
587 GetMonitorInfoW( monitor
, &info
);
589 x
= Globals
.main_rect
.left
;
590 y
= Globals
.main_rect
.top
;
591 if (Globals
.main_rect
.left
>= info
.rcWork
.right
||
592 Globals
.main_rect
.top
>= info
.rcWork
.bottom
||
593 Globals
.main_rect
.right
< info
.rcWork
.left
||
594 Globals
.main_rect
.bottom
< info
.rcWork
.top
)
595 x
= y
= CW_USEDEFAULT
;
598 CreateWindow(className
, winName
, WS_OVERLAPPEDWINDOW
,
599 x
, y
, Globals
.main_rect
.right
- Globals
.main_rect
.left
,
600 Globals
.main_rect
.bottom
- Globals
.main_rect
.top
,
601 NULL
, NULL
, Globals
.hInstance
, NULL
);
602 if (!Globals
.hMainWnd
)
608 DoCreateEditWindow();
613 ShowWindow(Globals
.hMainWnd
, show
);
614 UpdateWindow(Globals
.hMainWnd
);
615 DragAcceptFiles(Globals
.hMainWnd
, TRUE
);
617 DIALOG_ViewStatusBar();
619 HandleCommandLine(cmdline
);
621 hAccel
= LoadAccelerators( hInstance
, MAKEINTRESOURCE(ID_ACCEL
) );
623 while (GetMessage(&msg
, 0, 0, 0))
625 if (!IsDialogMessage(Globals
.hFindReplaceDlg
, &msg
) &&
626 !TranslateAccelerator(Globals
.hMainWnd
, hAccel
, &msg
))
628 TranslateMessage(&msg
);
629 DispatchMessage(&msg
);
632 return (int) msg
.wParam
;