403fae3fece9c1badcb70581f62eca0934e3c569
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
)
313 DIALOG_StatusBarUpdateCaretPos();
319 DIALOG_StatusBarUpdateCaretPos();
323 return CallWindowProc( (WNDPROC
)Globals
.EditProc
, hWnd
, msg
, wParam
, lParam
);
326 /***********************************************************************
330 static LRESULT WINAPI
NOTEPAD_WndProc(HWND hWnd
, UINT msg
, WPARAM wParam
,
337 static const TCHAR edit
[] = _T("edit");
339 GetClientRect(hWnd
, &rc
);
340 Globals
.hEdit
= CreateWindowEx(EDIT_EXSTYLE
, edit
, NULL
, Globals
.bWrapLongLines
? EDIT_STYLE_WRAP
: EDIT_STYLE
,
341 0, 0, rc
.right
, rc
.bottom
, hWnd
,
342 NULL
, Globals
.hInstance
, NULL
);
345 SendMessage(Globals
.hEdit
, EM_LIMITTEXT
, 0, 0);
347 SendMessage(Globals
.hEdit
, WM_SETFONT
, (WPARAM
)Globals
.hFont
, (LPARAM
)TRUE
);
349 Globals
.EditProc
= (WNDPROC
) SetWindowLongPtr(Globals
.hEdit
, GWLP_WNDPROC
, (LONG_PTR
)EDIT_WndProc
);
355 if (HIWORD(wParam
) == EN_CHANGE
|| HIWORD(wParam
) == EN_HSCROLL
|| HIWORD(wParam
) == EN_VSCROLL
)
356 DIALOG_StatusBarUpdateCaretPos();
357 if ((HIWORD(wParam
) == EN_CHANGE
))
358 NOTEPAD_EnableSearchMenu();
359 NOTEPAD_MenuCommand(LOWORD(wParam
));
362 case WM_DESTROYCLIPBOARD
:
363 /*MessageBox(Globals.hMainWnd, "Empty clipboard", "Debug", MB_ICONEXCLAMATION);*/
369 DeleteObject(Globals
.hFont
);
374 case WM_QUERYENDSESSION
:
381 SetWindowLongPtr(Globals
.hEdit
, GWLP_WNDPROC
, (LONG_PTR
)Globals
.EditProc
);
388 if (Globals
.bShowStatusBar
)
393 if (!GetWindowRect(Globals
.hStatusBar
, &rcStatusBar
))
396 hdwp
= BeginDeferWindowPos(2);
400 hdwp
= DeferWindowPos(hdwp
, Globals
.hEdit
, NULL
, 0, 0, LOWORD(lParam
), HIWORD(lParam
) - (rcStatusBar
.bottom
- rcStatusBar
.top
), SWP_NOZORDER
| SWP_NOMOVE
);
404 hdwp
= DeferWindowPos(hdwp
, Globals
.hStatusBar
, NULL
, 0, 0, LOWORD(lParam
), LOWORD(wParam
), SWP_NOZORDER
);
407 EndDeferWindowPos(hdwp
);
410 SetWindowPos(Globals
.hEdit
, NULL
, 0, 0, LOWORD(lParam
), HIWORD(lParam
), SWP_NOZORDER
| SWP_NOMOVE
);
416 SetFocus(Globals
.hEdit
);
421 TCHAR szFileName
[MAX_PATH
];
422 HDROP hDrop
= (HDROP
) wParam
;
424 DragQueryFile(hDrop
, 0, szFileName
, SIZEOF(szFileName
));
426 DoOpenFile(szFileName
);
430 case WM_INITMENUPOPUP
:
431 NOTEPAD_InitMenuPopup((HMENU
)wParam
, lParam
);
434 if (msg
== aFINDMSGSTRING
)
436 FINDREPLACE
*pFindReplace
= (FINDREPLACE
*) lParam
;
437 Globals
.find
= *(FINDREPLACE
*) lParam
;
439 if (pFindReplace
->Flags
& FR_FINDNEXT
)
440 NOTEPAD_FindNext(pFindReplace
, FALSE
, TRUE
);
441 else if (pFindReplace
->Flags
& FR_REPLACE
)
442 NOTEPAD_FindNext(pFindReplace
, TRUE
, TRUE
);
443 else if (pFindReplace
->Flags
& FR_REPLACEALL
)
444 NOTEPAD_ReplaceAll(pFindReplace
);
445 else if (pFindReplace
->Flags
& FR_DIALOGTERM
)
450 return DefWindowProc(hWnd
, msg
, wParam
, lParam
);
455 static int AlertFileDoesNotExist(LPCTSTR szFileName
)
458 TCHAR szMessage
[MAX_STRING_LEN
];
459 TCHAR szResource
[MAX_STRING_LEN
];
461 LoadString(Globals
.hInstance
, STRING_DOESNOTEXIST
, szResource
, SIZEOF(szResource
));
462 wsprintf(szMessage
, szResource
, szFileName
);
464 LoadString(Globals
.hInstance
, STRING_NOTEPAD
, szResource
, SIZEOF(szResource
));
466 nResult
= MessageBox(Globals
.hMainWnd
, szMessage
, szResource
,
467 MB_ICONEXCLAMATION
| MB_YESNO
);
472 static void HandleCommandLine(LPTSTR cmdline
)
476 while (*cmdline
== _T(' ') || *cmdline
== _T('-') || *cmdline
== _T('/'))
480 if (*cmdline
++ == _T(' ')) continue;
483 if (option
) cmdline
++;
484 while (*cmdline
== _T(' ')) cmdline
++;
497 /* file name is passed in the command line */
498 LPCTSTR file_name
= NULL
;
499 BOOL file_exists
= FALSE
;
502 if (cmdline
[0] == _T('"'))
505 cmdline
[lstrlen(cmdline
) - 1] = 0;
509 if (FileExists(file_name
))
513 else if (!HasFileExtension(cmdline
))
515 static const TCHAR txt
[] = _T(".txt");
517 /* try to find file with ".txt" extension */
518 if (!_tcscmp(txt
, cmdline
+ _tcslen(cmdline
) - _tcslen(txt
)))
524 _tcsncpy(buf
, cmdline
, MAX_PATH
- _tcslen(txt
) - 1);
527 file_exists
= FileExists(file_name
);
533 DoOpenFile(file_name
);
534 InvalidateRect(Globals
.hMainWnd
, NULL
, FALSE
);
540 switch (AlertFileDoesNotExist(file_name
)) {
542 DoOpenFile(file_name
);
552 /***********************************************************************
556 int WINAPI
_tWinMain(HINSTANCE hInstance
, HINSTANCE prev
, LPTSTR cmdline
, int show
)
565 static const TCHAR className
[] = _T("NPClass");
566 static const TCHAR winName
[] = _T("Notepad");
568 UNREFERENCED_PARAMETER(prev
);
570 aFINDMSGSTRING
= (ATOM
) RegisterWindowMessage(FINDMSGSTRING
);
572 ZeroMemory(&Globals
, sizeof(Globals
));
573 Globals
.hInstance
= hInstance
;
576 ZeroMemory(&wndclass
, sizeof(wndclass
));
577 wndclass
.cbSize
= sizeof(wndclass
);
578 wndclass
.lpfnWndProc
= NOTEPAD_WndProc
;
579 wndclass
.hInstance
= Globals
.hInstance
;
580 wndclass
.hIcon
= LoadIcon(hInstance
, MAKEINTRESOURCE(IDI_NPICON
));
581 wndclass
.hCursor
= LoadCursor(0, IDC_ARROW
);
582 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
583 wndclass
.lpszMenuName
= MAKEINTRESOURCE(MAIN_MENU
);
584 wndclass
.lpszClassName
= className
;
585 wndclass
.hIconSm
= (HICON
)LoadImage(hInstance
, MAKEINTRESOURCE(IDI_NPICON
),
586 IMAGE_ICON
, 16, 16, 0);
588 if (!RegisterClassEx(&wndclass
)) return FALSE
;
592 monitor
= MonitorFromRect( &Globals
.main_rect
, MONITOR_DEFAULTTOPRIMARY
);
593 info
.cbSize
= sizeof(info
);
594 GetMonitorInfoW( monitor
, &info
);
596 x
= Globals
.main_rect
.left
;
597 y
= Globals
.main_rect
.top
;
598 if (Globals
.main_rect
.left
>= info
.rcWork
.right
||
599 Globals
.main_rect
.top
>= info
.rcWork
.bottom
||
600 Globals
.main_rect
.right
< info
.rcWork
.left
||
601 Globals
.main_rect
.bottom
< info
.rcWork
.top
)
602 x
= y
= CW_USEDEFAULT
;
605 CreateWindow(className
, winName
, WS_OVERLAPPEDWINDOW
,
606 x
, y
, Globals
.main_rect
.right
- Globals
.main_rect
.left
,
607 Globals
.main_rect
.bottom
- Globals
.main_rect
.top
,
608 NULL
, NULL
, Globals
.hInstance
, NULL
);
609 if (!Globals
.hMainWnd
)
618 ShowWindow(Globals
.hMainWnd
, show
);
619 UpdateWindow(Globals
.hMainWnd
);
620 DragAcceptFiles(Globals
.hMainWnd
, TRUE
);
622 DIALOG_ViewStatusBar();
624 HandleCommandLine(cmdline
);
626 hAccel
= LoadAccelerators( hInstance
, MAKEINTRESOURCE(ID_ACCEL
) );
628 while (GetMessage(&msg
, 0, 0, 0))
630 if (!IsDialogMessage(Globals
.hFindReplaceDlg
, &msg
) &&
631 !TranslateAccelerator(Globals
.hMainWnd
, hAccel
, &msg
))
633 TranslateMessage(&msg
);
634 DispatchMessage(&msg
);
637 return (int) msg
.wParam
;