be3ba17efd1b57a7962eaa7209bc937882f7adcf
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 /***********************************************************************
34 * Sets Global File Name.
36 VOID
SetFileName(LPCWSTR szFileName
)
38 lstrcpy(Globals
.szFileName
, szFileName
);
39 Globals
.szFileTitle
[0] = 0;
40 GetFileTitle(szFileName
, Globals
.szFileTitle
, wcslen(Globals
.szFileTitle
));
43 /***********************************************************************
47 * All handling of main menu events
49 static int NOTEPAD_MenuCommand(WPARAM wParam
)
53 case CMD_NEW
: DIALOG_FileNew(); break;
54 case CMD_OPEN
: DIALOG_FileOpen(); break;
55 case CMD_SAVE
: DIALOG_FileSave(); break;
56 case CMD_SAVE_AS
: DIALOG_FileSaveAs(); break;
57 case CMD_PRINT
: DIALOG_FilePrint(); break;
58 case CMD_PAGE_SETUP
: DIALOG_FilePageSetup(); break;
59 case CMD_PRINTER_SETUP
: DIALOG_FilePrinterSetup();break;
60 case CMD_EXIT
: DIALOG_FileExit(); break;
62 case CMD_UNDO
: DIALOG_EditUndo(); break;
63 case CMD_CUT
: DIALOG_EditCut(); break;
64 case CMD_COPY
: DIALOG_EditCopy(); break;
65 case CMD_PASTE
: DIALOG_EditPaste(); break;
66 case CMD_DELETE
: DIALOG_EditDelete(); break;
67 case CMD_SELECT_ALL
: DIALOG_EditSelectAll(); break;
68 case CMD_TIME_DATE
: DIALOG_EditTimeDate();break;
70 case CMD_SEARCH
: DIALOG_Search(); break;
71 case CMD_SEARCH_NEXT
: DIALOG_SearchNext(); break;
72 case CMD_REPLACE
: DIALOG_Replace(); break;
73 case CMD_GOTO
: DIALOG_GoTo(); break;
75 case CMD_WRAP
: DIALOG_EditWrap(); break;
76 case CMD_FONT
: DIALOG_SelectFont(); break;
78 case CMD_HELP_CONTENTS
: DIALOG_HelpContents(); break;
79 case CMD_HELP_SEARCH
: DIALOG_HelpSearch(); break;
80 case CMD_HELP_ON_HELP
: DIALOG_HelpHelp(); break;
81 case CMD_LICENSE
: DIALOG_HelpLicense(); break;
82 case CMD_NO_WARRANTY
: DIALOG_HelpNoWarranty(); break;
83 case CMD_ABOUT_WINE
: DIALOG_HelpAboutWine(); break;
91 /***********************************************************************
96 static BOOL
NOTEPAD_FindTextAt(FINDREPLACE
*pFindReplace
, LPCTSTR pszText
, int iTextLength
, DWORD dwPosition
)
101 iTargetLength
= _tcslen(pFindReplace
->lpstrFindWhat
);
103 /* Make proper comparison */
104 if (pFindReplace
->Flags
& FR_MATCHCASE
)
105 bMatches
= !_tcsncmp(&pszText
[dwPosition
], pFindReplace
->lpstrFindWhat
, iTargetLength
);
107 bMatches
= !_tcsnicmp(&pszText
[dwPosition
], pFindReplace
->lpstrFindWhat
, iTargetLength
);
109 if (bMatches
&& pFindReplace
->Flags
& FR_WHOLEWORD
)
111 if ((dwPosition
> 0) && !_istspace(pszText
[dwPosition
-1]))
113 if ((dwPosition
< iTextLength
- 1) && !_istspace(pszText
[dwPosition
+1]))
120 /***********************************************************************
125 static BOOL
NOTEPAD_FindNext(FINDREPLACE
*pFindReplace
, BOOL bReplace
, BOOL bShowAlert
)
127 int iTextLength
, iTargetLength
;
129 LPTSTR pszText
= NULL
;
130 DWORD dwPosition
, dwBegin
, dwEnd
;
131 BOOL bMatches
= FALSE
;
132 TCHAR szResource
[128], szText
[128];
135 iTargetLength
= _tcslen(pFindReplace
->lpstrFindWhat
);
137 /* Retrieve the window text */
138 iTextLength
= GetWindowTextLength(Globals
.hEdit
);
141 pszText
= (LPTSTR
) HeapAlloc(GetProcessHeap(), 0, (iTextLength
+ 1) * sizeof(TCHAR
));
145 GetWindowText(Globals
.hEdit
, pszText
, iTextLength
+ 1);
148 SendMessage(Globals
.hEdit
, EM_GETSEL
, (WPARAM
) &dwBegin
, (LPARAM
) &dwEnd
);
149 if (bReplace
&& ((dwEnd
- dwBegin
) == iTargetLength
))
151 if (NOTEPAD_FindTextAt(pFindReplace
, pszText
, iTextLength
, dwBegin
))
153 SendMessage(Globals
.hEdit
, EM_REPLACESEL
, TRUE
, (LPARAM
) pFindReplace
->lpstrReplaceWith
);
154 iAdjustment
= _tcslen(pFindReplace
->lpstrReplaceWith
) - (dwEnd
- dwBegin
);
158 if (pFindReplace
->Flags
& FR_DOWN
)
162 while(dwPosition
< iTextLength
)
164 bMatches
= NOTEPAD_FindTextAt(pFindReplace
, pszText
, iTextLength
, dwPosition
);
173 dwPosition
= dwBegin
;
174 while(dwPosition
> 0)
177 bMatches
= NOTEPAD_FindTextAt(pFindReplace
, pszText
, iTextLength
, dwPosition
);
186 if (dwPosition
> dwBegin
)
187 dwPosition
+= iAdjustment
;
188 SendMessage(Globals
.hEdit
, EM_SETSEL
, dwPosition
, dwPosition
+ iTargetLength
);
189 SendMessage(Globals
.hEdit
, EM_SCROLLCARET
, 0, 0);
194 /* Can't find target */
197 LoadString(Globals
.hInstance
, STRING_CANNOTFIND
, szResource
, SIZEOF(szResource
));
198 _sntprintf(szText
, SIZEOF(szText
), szResource
, pFindReplace
->lpstrFindWhat
);
199 LoadString(Globals
.hInstance
, STRING_NOTEPAD
, szResource
, SIZEOF(szResource
));
200 MessageBox(Globals
.hFindReplaceDlg
, szText
, szResource
, MB_OK
);
206 HeapFree(GetProcessHeap(), 0, pszText
);
210 /***********************************************************************
215 static VOID
NOTEPAD_ReplaceAll(FINDREPLACE
*pFindReplace
)
217 BOOL bShowAlert
= TRUE
;
219 SendMessage(Globals
.hEdit
, EM_SETSEL
, 0, 0);
221 while (NOTEPAD_FindNext(pFindReplace
, TRUE
, bShowAlert
))
227 /***********************************************************************
232 static VOID
NOTEPAD_FindTerm(VOID
)
234 Globals
.hFindReplaceDlg
= NULL
;
237 /***********************************************************************
238 * Data Initialization
240 static VOID
NOTEPAD_InitData(VOID
)
242 LPWSTR p
= Globals
.szFilter
;
243 static const WCHAR txt_files
[] = { '*','.','t','x','t',0 };
244 static const WCHAR all_files
[] = { '*','.','*',0 };
246 LoadString(Globals
.hInstance
, STRING_TEXT_FILES_TXT
, p
, MAX_STRING_LEN
);
248 lstrcpy(p
, txt_files
);
250 LoadString(Globals
.hInstance
, STRING_ALL_FILES
, p
, MAX_STRING_LEN
);
252 lstrcpy(p
, all_files
);
257 /***********************************************************************
258 * Enable/disable items on the menu based on control state
260 static VOID
NOTEPAD_InitMenuPopup(HMENU menu
, int index
)
264 CheckMenuItem(GetMenu(Globals
.hMainWnd
), CMD_WRAP
,
265 MF_BYCOMMAND
| (Globals
.bWrapLongLines
? MF_CHECKED
: MF_UNCHECKED
));
267 EnableMenuItem(menu
, CMD_UNDO
,
268 SendMessage(Globals
.hEdit
, EM_CANUNDO
, 0, 0) ? MF_ENABLED
: MF_GRAYED
);
269 EnableMenuItem(menu
, CMD_PASTE
,
270 IsClipboardFormatAvailable(CF_TEXT
) ? MF_ENABLED
: MF_GRAYED
);
271 enable
= SendMessage(Globals
.hEdit
, EM_GETSEL
, 0, 0);
272 enable
= (HIWORD(enable
) == LOWORD(enable
)) ? MF_GRAYED
: MF_ENABLED
;
273 EnableMenuItem(menu
, CMD_CUT
, enable
);
274 EnableMenuItem(menu
, CMD_COPY
, enable
);
275 EnableMenuItem(menu
, CMD_DELETE
, enable
);
277 EnableMenuItem(menu
, CMD_SELECT_ALL
,
278 GetWindowTextLength(Globals
.hEdit
) ? MF_ENABLED
: MF_GRAYED
);
281 /***********************************************************************
285 static LRESULT WINAPI
NOTEPAD_WndProc(HWND hWnd
, UINT msg
, WPARAM wParam
,
292 static const WCHAR editW
[] = { 'e','d','i','t',0 };
294 GetClientRect(hWnd
, &rc
);
295 Globals
.hEdit
= CreateWindowEx(EDIT_EXSTYLE
, editW
, NULL
, Globals
.bWrapLongLines
? EDIT_STYLE_WRAP
: EDIT_STYLE
,
296 0, 0, rc
.right
, rc
.bottom
, hWnd
,
297 NULL
, Globals
.hInstance
, NULL
);
300 SendMessage(Globals
.hEdit
, EM_LIMITTEXT
, 0, 0);
302 SendMessage(Globals
.hEdit
, WM_SETFONT
, (WPARAM
)Globals
.hFont
, (LPARAM
)TRUE
);
307 NOTEPAD_MenuCommand(LOWORD(wParam
));
310 case WM_DESTROYCLIPBOARD
:
311 /*MessageBox(Globals.hMainWnd, "Empty clipboard", "Debug", MB_ICONEXCLAMATION);*/
320 case WM_QUERYENDSESSION
:
331 SetWindowPos(Globals
.hEdit
, NULL
, 0, 0, LOWORD(lParam
), HIWORD(lParam
),
332 SWP_NOOWNERZORDER
| SWP_NOZORDER
);
336 SetFocus(Globals
.hEdit
);
341 WCHAR szFileName
[MAX_PATH
];
342 HANDLE hDrop
= (HANDLE
) wParam
;
344 DragQueryFile(hDrop
, 0, szFileName
, SIZEOF(szFileName
));
346 DoOpenFile(szFileName
);
350 case WM_INITMENUPOPUP
:
351 NOTEPAD_InitMenuPopup((HMENU
)wParam
, lParam
);
355 if (msg
== aFINDMSGSTRING
)
357 FINDREPLACE
*pFindReplace
= (FINDREPLACE
*) lParam
;
359 if (pFindReplace
->Flags
& FR_FINDNEXT
)
360 NOTEPAD_FindNext(pFindReplace
, FALSE
, TRUE
);
361 else if (pFindReplace
->Flags
& FR_REPLACE
)
362 NOTEPAD_FindNext(pFindReplace
, TRUE
, TRUE
);
363 else if (pFindReplace
->Flags
& FR_REPLACEALL
)
364 NOTEPAD_ReplaceAll(pFindReplace
);
365 else if (pFindReplace
->Flags
& FR_DIALOGTERM
)
370 return DefWindowProc(hWnd
, msg
, wParam
, lParam
);
375 static int AlertFileDoesNotExist(LPCWSTR szFileName
)
378 WCHAR szMessage
[MAX_STRING_LEN
];
379 WCHAR szResource
[MAX_STRING_LEN
];
381 LoadString(Globals
.hInstance
, STRING_DOESNOTEXIST
, szResource
, SIZEOF(szResource
));
382 wsprintf(szMessage
, szResource
, szFileName
);
384 LoadString(Globals
.hInstance
, STRING_NOTEPAD
, szResource
, SIZEOF(szResource
));
386 nResult
= MessageBox(Globals
.hMainWnd
, szMessage
, szResource
,
387 MB_ICONEXCLAMATION
| MB_YESNO
);
392 static void HandleCommandLine(LPWSTR cmdline
)
397 /* skip white space */
398 while (*cmdline
== ' ') cmdline
++;
400 /* skip executable name */
401 delimiter
= (*cmdline
== '"' ? '"' : ' ');
407 while (*cmdline
&& *cmdline
!= delimiter
);
408 if (*cmdline
== delimiter
) cmdline
++;
410 while (*cmdline
== ' ' || *cmdline
== '-' || *cmdline
== '/')
414 if (*cmdline
++ == ' ') continue;
417 if (option
) cmdline
++;
418 while (*cmdline
== ' ') cmdline
++;
431 /* file name is passed in the command line */
432 LPCWSTR file_name
= NULL
;
433 BOOL file_exists
= FALSE
;
436 if (cmdline
[0] == '"')
439 cmdline
[lstrlen(cmdline
) - 1] = 0;
443 if (FileExists(file_name
))
447 else if (!HasFileExtension(cmdline
))
449 static const WCHAR txtW
[] = { '.','t','x','t',0 };
451 /* try to find file with ".txt" extension */
452 if (!lstrcmp(txtW
, cmdline
+ lstrlen(cmdline
) - lstrlen(txtW
)))
458 lstrcpyn(buf
, cmdline
, MAX_PATH
- lstrlen(txtW
) - 1);
461 file_exists
= FileExists(file_name
);
467 DoOpenFile(file_name
);
468 InvalidateRect(Globals
.hMainWnd
, NULL
, FALSE
);
474 switch (AlertFileDoesNotExist(file_name
)) {
476 DoOpenFile(file_name
);
486 /***********************************************************************
490 int PASCAL
WinMain(HINSTANCE hInstance
, HINSTANCE prev
, LPSTR cmdline
, int show
)
495 static const WCHAR className
[] = {'N','P','C','l','a','s','s',0};
496 static const WCHAR winName
[] = {'N','o','t','e','p','a','d',0};
498 aFINDMSGSTRING
= RegisterWindowMessage(FINDMSGSTRING
);
500 ZeroMemory(&Globals
, sizeof(Globals
));
501 Globals
.hInstance
= hInstance
;
504 ZeroMemory(&class, sizeof(class));
505 class.cbSize
= sizeof(class);
506 class.lpfnWndProc
= NOTEPAD_WndProc
;
507 class.hInstance
= Globals
.hInstance
;
508 class.hIcon
= LoadIcon(0, IDI_APPLICATION
);
509 class.hCursor
= LoadCursor(0, IDC_ARROW
);
510 class.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
511 class.lpszMenuName
= MAKEINTRESOURCE(MAIN_MENU
);
512 class.lpszClassName
= className
;
514 if (!RegisterClassEx(&class)) return FALSE
;
519 CreateWindow(className
, winName
, WS_OVERLAPPEDWINDOW
,
520 CW_USEDEFAULT
, 0, CW_USEDEFAULT
, 0,
521 NULL
, NULL
, Globals
.hInstance
, NULL
);
522 if (!Globals
.hMainWnd
)
531 ShowWindow(Globals
.hMainWnd
, show
);
532 UpdateWindow(Globals
.hMainWnd
);
533 DragAcceptFiles(Globals
.hMainWnd
, TRUE
);
535 HandleCommandLine(GetCommandLine());
537 hAccel
= LoadAccelerators( hInstance
, MAKEINTRESOURCE(ID_ACCEL
) );
539 while (GetMessage(&msg
, 0, 0, 0))
541 if (!IsDialogMessage(Globals
.hFindReplaceDlg
, &msg
) &&
542 !TranslateAccelerator(Globals
.hMainWnd
, hAccel
, &msg
))
544 TranslateMessage(&msg
);
545 DispatchMessage(&msg
);