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
34 #include "notepad_res.h"
36 NOTEPAD_GLOBALS Globals
;
37 static ATOM aFINDMSGSTRING
;
39 /***********************************************************************
43 * Sets Global File Name.
45 VOID
SetFileName(LPCWSTR szFileName
)
47 lstrcpy(Globals
.szFileName
, szFileName
);
48 Globals
.szFileTitle
[0] = 0;
49 GetFileTitle(szFileName
, Globals
.szFileTitle
, sizeof(Globals
.szFileTitle
));
52 /***********************************************************************
56 * All handling of main menu events
58 static int NOTEPAD_MenuCommand(WPARAM wParam
)
62 case CMD_NEW
: DIALOG_FileNew(); break;
63 case CMD_OPEN
: DIALOG_FileOpen(); break;
64 case CMD_SAVE
: DIALOG_FileSave(); break;
65 case CMD_SAVE_AS
: DIALOG_FileSaveAs(); break;
66 case CMD_PRINT
: DIALOG_FilePrint(); break;
67 case CMD_PAGE_SETUP
: DIALOG_FilePageSetup(); break;
68 case CMD_PRINTER_SETUP
: DIALOG_FilePrinterSetup();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;
82 case CMD_WRAP
: DIALOG_EditWrap(); break;
83 case CMD_FONT
: DIALOG_SelectFont(); break;
85 case CMD_HELP_CONTENTS
: DIALOG_HelpContents(); break;
86 case CMD_HELP_SEARCH
: DIALOG_HelpSearch(); break;
87 case CMD_HELP_ON_HELP
: DIALOG_HelpHelp(); break;
88 case CMD_LICENSE
: DIALOG_HelpLicense(); break;
89 case CMD_NO_WARRANTY
: DIALOG_HelpNoWarranty(); break;
90 case CMD_ABOUT_WINE
: DIALOG_HelpAboutWine(); break;
98 /***********************************************************************
103 static VOID
NOTEPAD_FindNext(FINDREPLACE
*pFindReplace
)
105 int iTextLength
, iTargetLength
;
106 LPTSTR pszText
= NULL
;
107 DWORD dwPosition
, dwDummy
;
108 BOOL bMatches
= FALSE
;
110 iTargetLength
= _tcslen(pFindReplace
->lpstrFindWhat
);
112 iTextLength
= GetWindowTextLength(Globals
.hEdit
);
116 pszText
= (LPTSTR
) HeapAlloc(GetProcessHeap(), 0, (iTextLength
+ 1) * sizeof(TCHAR
));
120 GetWindowText(Globals
.hEdit
, pszText
, iTextLength
+ 1);
123 SendMessage(Globals
.hEdit
, EM_GETSEL
, (WPARAM
) &dwDummy
, (LPARAM
) &dwPosition
);
125 while(dwPosition
< iTextLength
)
127 /* Make proper comparison */
128 if (pFindReplace
->Flags
& FR_MATCHCASE
)
129 bMatches
= !_tcsncmp(&pszText
[dwPosition
], pFindReplace
->lpstrFindWhat
, iTargetLength
);
131 bMatches
= !_tcsnicmp(&pszText
[dwPosition
], pFindReplace
->lpstrFindWhat
, iTargetLength
);
133 if (bMatches
&& pFindReplace
->Flags
& FR_WHOLEWORD
)
135 if ((dwPosition
> 0) && !_istspace(pszText
[dwPosition
-1]))
137 if ((dwPosition
< iTextLength
- 1) && !_istspace(pszText
[dwPosition
+1]))
144 if (pFindReplace
->Flags
& FR_DOWN
)
152 SendMessage(Globals
.hEdit
, EM_SETSEL
, dwPosition
, dwPosition
+ iTargetLength
);
153 SendMessage(Globals
.hEdit
, EM_SCROLLCARET
, 0, 0);
157 HeapFree(GetProcessHeap(), 0, pszText
);
160 /***********************************************************************
165 static VOID
NOTEPAD_FindTerm(VOID
)
167 Globals
.hFindReplaceDlg
= NULL
;
170 /***********************************************************************
171 * Data Initialization
173 static VOID
NOTEPAD_InitData(VOID
)
175 LPWSTR p
= Globals
.szFilter
;
176 static const WCHAR txt_files
[] = { '*','.','t','x','t',0 };
177 static const WCHAR all_files
[] = { '*','.','*',0 };
179 LoadString(Globals
.hInstance
, STRING_TEXT_FILES_TXT
, p
, MAX_STRING_LEN
);
181 lstrcpy(p
, txt_files
);
183 LoadString(Globals
.hInstance
, STRING_ALL_FILES
, p
, MAX_STRING_LEN
);
185 lstrcpy(p
, all_files
);
190 /***********************************************************************
191 * Enable/disable items on the menu based on control state
193 static VOID
NOTEPAD_InitMenuPopup(HMENU menu
, int index
)
197 EnableMenuItem(menu
, CMD_UNDO
,
198 SendMessage(Globals
.hEdit
, EM_CANUNDO
, 0, 0) ? MF_ENABLED
: MF_GRAYED
);
199 EnableMenuItem(menu
, CMD_PASTE
,
200 IsClipboardFormatAvailable(CF_TEXT
) ? MF_ENABLED
: MF_GRAYED
);
201 enable
= SendMessage(Globals
.hEdit
, EM_GETSEL
, 0, 0);
202 enable
= (HIWORD(enable
) == LOWORD(enable
)) ? MF_GRAYED
: MF_ENABLED
;
203 EnableMenuItem(menu
, CMD_CUT
, enable
);
204 EnableMenuItem(menu
, CMD_COPY
, enable
);
205 EnableMenuItem(menu
, CMD_DELETE
, enable
);
207 EnableMenuItem(menu
, CMD_SELECT_ALL
,
208 GetWindowTextLength(Globals
.hEdit
) ? MF_ENABLED
: MF_GRAYED
);
211 /***********************************************************************
215 static LRESULT WINAPI
NOTEPAD_WndProc(HWND hWnd
, UINT msg
, WPARAM wParam
,
222 static const WCHAR editW
[] = { 'e','d','i','t',0 };
224 GetClientRect(hWnd
, &rc
);
225 Globals
.hEdit
= CreateWindowEx(EDIT_EXSTYLE
, editW
, NULL
, EDIT_STYLE
,
226 0, 0, rc
.right
, rc
.bottom
, hWnd
,
227 NULL
, Globals
.hInstance
, NULL
);
232 NOTEPAD_MenuCommand(LOWORD(wParam
));
235 case WM_DESTROYCLIPBOARD
:
236 /*MessageBox(Globals.hMainWnd, "Empty clipboard", "Debug", MB_ICONEXCLAMATION);*/
245 case WM_QUERYENDSESSION
:
256 SetWindowPos(Globals
.hEdit
, NULL
, 0, 0, LOWORD(lParam
), HIWORD(lParam
),
257 SWP_NOOWNERZORDER
| SWP_NOZORDER
);
261 SetFocus(Globals
.hEdit
);
266 WCHAR szFileName
[MAX_PATH
];
267 HANDLE hDrop
= (HANDLE
) wParam
;
269 DragQueryFile(hDrop
, 0, szFileName
, SIZEOF(szFileName
));
271 DoOpenFile(szFileName
);
275 case WM_INITMENUPOPUP
:
276 NOTEPAD_InitMenuPopup((HMENU
)wParam
, lParam
);
280 if (msg
== aFINDMSGSTRING
)
282 FINDREPLACE
*pFindReplace
= (FINDREPLACE
*) lParam
;
284 if (pFindReplace
->Flags
& FR_FINDNEXT
)
285 NOTEPAD_FindNext(pFindReplace
);
286 else if (pFindReplace
->Flags
& FR_DIALOGTERM
)
291 return DefWindowProc(hWnd
, msg
, wParam
, lParam
);
296 static int AlertFileDoesNotExist(LPCWSTR szFileName
)
299 WCHAR szMessage
[MAX_STRING_LEN
];
300 WCHAR szResource
[MAX_STRING_LEN
];
302 LoadString(Globals
.hInstance
, STRING_DOESNOTEXIST
, szResource
, SIZEOF(szResource
));
303 wsprintf(szMessage
, szResource
, szFileName
);
305 LoadString(Globals
.hInstance
, STRING_NOTEPAD
, szResource
, SIZEOF(szResource
));
307 nResult
= MessageBox(Globals
.hMainWnd
, szMessage
, szResource
,
308 MB_ICONEXCLAMATION
| MB_YESNO
);
313 static void HandleCommandLine(LPWSTR cmdline
)
318 /* skip white space */
319 while (*cmdline
== ' ') cmdline
++;
321 /* skip executable name */
322 delimiter
= (*cmdline
== '"' ? '"' : ' ');
328 while (*cmdline
&& *cmdline
!= delimiter
);
329 if (*cmdline
== delimiter
) cmdline
++;
331 while (*cmdline
== ' ' || *cmdline
== '-' || *cmdline
== '/')
335 if (*cmdline
++ == ' ') continue;
338 if (option
) cmdline
++;
339 while (*cmdline
== ' ') cmdline
++;
352 /* file name is passed in the command line */
357 if (cmdline
[0] == '"')
360 cmdline
[lstrlen(cmdline
) - 1] = 0;
363 if (FileExists(cmdline
))
370 static const WCHAR txtW
[] = { '.','t','x','t',0 };
372 /* try to find file with ".txt" extension */
373 if (!lstrcmp(txtW
, cmdline
+ lstrlen(cmdline
) - lstrlen(txtW
)))
380 lstrcpyn(buf
, cmdline
, MAX_PATH
- lstrlen(txtW
) - 1);
383 file_exists
= FileExists(buf
);
389 DoOpenFile(file_name
);
390 InvalidateRect(Globals
.hMainWnd
, NULL
, FALSE
);
396 switch (AlertFileDoesNotExist(file_name
)) {
398 DoOpenFile(file_name
);
408 /***********************************************************************
412 int PASCAL
WinMain(HINSTANCE hInstance
, HINSTANCE prev
, LPSTR cmdline
, int show
)
417 static const WCHAR className
[] = {'N','P','C','l','a','s','s',0};
418 static const WCHAR winName
[] = {'N','o','t','e','p','a','d',0};
420 aFINDMSGSTRING
= RegisterWindowMessage(FINDMSGSTRING
);
422 ZeroMemory(&Globals
, sizeof(Globals
));
423 Globals
.hInstance
= hInstance
;
425 ZeroMemory(&class, sizeof(class));
426 class.cbSize
= sizeof(class);
427 class.lpfnWndProc
= NOTEPAD_WndProc
;
428 class.hInstance
= Globals
.hInstance
;
429 class.hIcon
= LoadIcon(0, IDI_APPLICATION
);
430 class.hCursor
= LoadCursor(0, IDC_ARROW
);
431 class.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
432 class.lpszMenuName
= MAKEINTRESOURCE(MAIN_MENU
);
433 class.lpszClassName
= className
;
435 if (!RegisterClassEx(&class)) return FALSE
;
440 CreateWindow(className
, winName
, WS_OVERLAPPEDWINDOW
,
441 CW_USEDEFAULT
, 0, CW_USEDEFAULT
, 0,
442 NULL
, NULL
, Globals
.hInstance
, NULL
);
443 if (!Globals
.hMainWnd
)
452 ShowWindow(Globals
.hMainWnd
, show
);
453 UpdateWindow(Globals
.hMainWnd
);
454 DragAcceptFiles(Globals
.hMainWnd
, TRUE
);
456 HandleCommandLine(GetCommandLine());
458 hAccel
= LoadAccelerators( hInstance
, MAKEINTRESOURCE(ID_ACCEL
) );
460 while (GetMessage(&msg
, 0, 0, 0))
462 if (!IsDialogMessage(Globals
.hFindReplaceDlg
, &msg
) &&
463 !TranslateAccelerator(Globals
.hMainWnd
, hAccel
, &msg
))
465 TranslateMessage(&msg
);
466 DispatchMessage(&msg
);