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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 NOTEPAD_GLOBALS Globals
;
28 static ATOM aFINDMSGSTRING
;
30 VOID
NOTEPAD_EnableSearchMenu()
32 EnableMenuItem(GetMenu(Globals
.hMainWnd
), CMD_SEARCH
,
33 MF_BYCOMMAND
| ((GetWindowTextLength(Globals
.hEdit
) == 0) ? MF_DISABLED
| MF_GRAYED
: MF_ENABLED
));
34 EnableMenuItem(GetMenu(Globals
.hMainWnd
), 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
),
94 (DLGPROC
) AboutDialogProc
);
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
,
338 static const TCHAR edit
[] = _T("edit");
340 GetClientRect(hWnd
, &rc
);
341 Globals
.hEdit
= CreateWindowEx(EDIT_EXSTYLE
, edit
, NULL
, Globals
.bWrapLongLines
? EDIT_STYLE_WRAP
: EDIT_STYLE
,
342 0, 0, rc
.right
, rc
.bottom
, hWnd
,
343 NULL
, Globals
.hInstance
, NULL
);
346 SendMessage(Globals
.hEdit
, EM_LIMITTEXT
, 0, 0);
348 SendMessage(Globals
.hEdit
, WM_SETFONT
, (WPARAM
)Globals
.hFont
, (LPARAM
)TRUE
);
350 Globals
.EditProc
= (WNDPROC
) SetWindowLongPtr(Globals
.hEdit
, GWLP_WNDPROC
, (LONG_PTR
)EDIT_WndProc
);
356 if (HIWORD(wParam
) == EN_CHANGE
|| HIWORD(wParam
) == EN_HSCROLL
|| HIWORD(wParam
) == EN_VSCROLL
)
357 DIALOG_StatusBarUpdateCaretPos();
358 if ((HIWORD(wParam
) == EN_CHANGE
))
359 NOTEPAD_EnableSearchMenu();
360 NOTEPAD_MenuCommand(LOWORD(wParam
));
363 case WM_DESTROYCLIPBOARD
:
364 /*MessageBox(Globals.hMainWnd, "Empty clipboard", "Debug", MB_ICONEXCLAMATION);*/
370 DeleteObject(Globals
.hFont
);
375 case WM_QUERYENDSESSION
:
382 SetWindowLongPtr(Globals
.hEdit
, GWLP_WNDPROC
, (LONG_PTR
)Globals
.EditProc
);
389 if (Globals
.bShowStatusBar
)
394 if (!GetWindowRect(Globals
.hStatusBar
, &rcStatusBar
))
397 hdwp
= BeginDeferWindowPos(2);
401 hdwp
= DeferWindowPos(hdwp
, Globals
.hEdit
, NULL
, 0, 0, LOWORD(lParam
), HIWORD(lParam
) - (rcStatusBar
.bottom
- rcStatusBar
.top
), SWP_NOZORDER
| SWP_NOMOVE
);
405 hdwp
= DeferWindowPos(hdwp
, Globals
.hStatusBar
, NULL
, 0, 0, LOWORD(lParam
), LOWORD(wParam
), SWP_NOZORDER
);
408 EndDeferWindowPos(hdwp
);
411 SetWindowPos(Globals
.hEdit
, NULL
, 0, 0, LOWORD(lParam
), HIWORD(lParam
), SWP_NOZORDER
| SWP_NOMOVE
);
417 SetFocus(Globals
.hEdit
);
422 TCHAR szFileName
[MAX_PATH
];
423 HDROP hDrop
= (HDROP
) wParam
;
425 DragQueryFile(hDrop
, 0, szFileName
, SIZEOF(szFileName
));
427 DoOpenFile(szFileName
);
431 case WM_INITMENUPOPUP
:
432 NOTEPAD_InitMenuPopup((HMENU
)wParam
, lParam
);
435 if (msg
== aFINDMSGSTRING
)
437 FINDREPLACE
*pFindReplace
= (FINDREPLACE
*) lParam
;
438 Globals
.find
= *(FINDREPLACE
*) lParam
;
440 if (pFindReplace
->Flags
& FR_FINDNEXT
)
441 NOTEPAD_FindNext(pFindReplace
, FALSE
, TRUE
);
442 else if (pFindReplace
->Flags
& FR_REPLACE
)
443 NOTEPAD_FindNext(pFindReplace
, TRUE
, TRUE
);
444 else if (pFindReplace
->Flags
& FR_REPLACEALL
)
445 NOTEPAD_ReplaceAll(pFindReplace
);
446 else if (pFindReplace
->Flags
& FR_DIALOGTERM
)
451 return DefWindowProc(hWnd
, msg
, wParam
, lParam
);
456 static int AlertFileDoesNotExist(LPCTSTR szFileName
)
459 TCHAR szMessage
[MAX_STRING_LEN
];
460 TCHAR szResource
[MAX_STRING_LEN
];
462 LoadString(Globals
.hInstance
, STRING_DOESNOTEXIST
, szResource
, SIZEOF(szResource
));
463 wsprintf(szMessage
, szResource
, szFileName
);
465 LoadString(Globals
.hInstance
, STRING_NOTEPAD
, szResource
, SIZEOF(szResource
));
467 nResult
= MessageBox(Globals
.hMainWnd
, szMessage
, szResource
,
468 MB_ICONEXCLAMATION
| MB_YESNO
);
473 static void HandleCommandLine(LPTSTR cmdline
)
477 while (*cmdline
== _T(' ') || *cmdline
== _T('-') || *cmdline
== _T('/'))
481 if (*cmdline
++ == _T(' ')) continue;
484 if (option
) cmdline
++;
485 while (*cmdline
== _T(' ')) cmdline
++;
498 /* file name is passed in the command line */
499 LPCTSTR file_name
= NULL
;
500 BOOL file_exists
= FALSE
;
503 if (cmdline
[0] == _T('"'))
506 cmdline
[lstrlen(cmdline
) - 1] = 0;
510 if (FileExists(file_name
))
514 else if (!HasFileExtension(cmdline
))
516 static const TCHAR txt
[] = _T(".txt");
518 /* try to find file with ".txt" extension */
519 if (!_tcscmp(txt
, cmdline
+ _tcslen(cmdline
) - _tcslen(txt
)))
525 _tcsncpy(buf
, cmdline
, MAX_PATH
- _tcslen(txt
) - 1);
528 file_exists
= FileExists(file_name
);
534 DoOpenFile(file_name
);
535 InvalidateRect(Globals
.hMainWnd
, NULL
, FALSE
);
541 switch (AlertFileDoesNotExist(file_name
)) {
543 DoOpenFile(file_name
);
553 /***********************************************************************
557 int WINAPI
_tWinMain(HINSTANCE hInstance
, HINSTANCE prev
, LPTSTR cmdline
, int show
)
566 static const TCHAR className
[] = _T("NPClass");
567 static const TCHAR winName
[] = _T("Notepad");
569 UNREFERENCED_PARAMETER(prev
);
571 aFINDMSGSTRING
= (ATOM
) RegisterWindowMessage(FINDMSGSTRING
);
573 ZeroMemory(&Globals
, sizeof(Globals
));
574 Globals
.hInstance
= hInstance
;
577 ZeroMemory(&wndclass
, sizeof(wndclass
));
578 wndclass
.cbSize
= sizeof(wndclass
);
579 wndclass
.lpfnWndProc
= NOTEPAD_WndProc
;
580 wndclass
.hInstance
= Globals
.hInstance
;
581 wndclass
.hIcon
= LoadIcon(hInstance
, MAKEINTRESOURCE(IDI_NPICON
));
582 wndclass
.hCursor
= LoadCursor(0, IDC_ARROW
);
583 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
584 wndclass
.lpszMenuName
= MAKEINTRESOURCE(MAIN_MENU
);
585 wndclass
.lpszClassName
= className
;
586 wndclass
.hIconSm
= (HICON
)LoadImage(hInstance
, MAKEINTRESOURCE(IDI_NPICON
),
587 IMAGE_ICON
, 16, 16, 0);
589 if (!RegisterClassEx(&wndclass
)) return FALSE
;
593 monitor
= MonitorFromRect( &Globals
.main_rect
, MONITOR_DEFAULTTOPRIMARY
);
594 info
.cbSize
= sizeof(info
);
595 GetMonitorInfoW( monitor
, &info
);
597 x
= Globals
.main_rect
.left
;
598 y
= Globals
.main_rect
.top
;
599 if (Globals
.main_rect
.left
>= info
.rcWork
.right
||
600 Globals
.main_rect
.top
>= info
.rcWork
.bottom
||
601 Globals
.main_rect
.right
< info
.rcWork
.left
||
602 Globals
.main_rect
.bottom
< info
.rcWork
.top
)
603 x
= y
= CW_USEDEFAULT
;
606 CreateWindow(className
, winName
, WS_OVERLAPPEDWINDOW
,
607 x
, y
, Globals
.main_rect
.right
- Globals
.main_rect
.left
,
608 Globals
.main_rect
.bottom
- Globals
.main_rect
.top
,
609 NULL
, NULL
, Globals
.hInstance
, NULL
);
610 if (!Globals
.hMainWnd
)
619 ShowWindow(Globals
.hMainWnd
, show
);
620 UpdateWindow(Globals
.hMainWnd
);
621 DragAcceptFiles(Globals
.hMainWnd
, TRUE
);
623 DIALOG_ViewStatusBar();
625 HandleCommandLine(cmdline
);
627 hAccel
= LoadAccelerators( hInstance
, MAKEINTRESOURCE(ID_ACCEL
) );
629 while (GetMessage(&msg
, 0, 0, 0))
631 if (!IsDialogMessage(Globals
.hFindReplaceDlg
, &msg
) &&
632 !TranslateAccelerator(Globals
.hMainWnd
, hAccel
, &msg
))
634 TranslateMessage(&msg
);
635 DispatchMessage(&msg
);
638 return (int) msg
.wParam
;