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
32 #include "notepad_res.h"
34 NOTEPAD_GLOBALS Globals;
35 static ATOM aFINDMSGSTRING;
37 /***********************************************************************
41 * Sets Global File Name.
43 VOID SetFileName(LPCWSTR szFileName)
45 lstrcpy(Globals.szFileName, szFileName);
46 Globals.szFileTitle[0] = 0;
47 GetFileTitle(szFileName, Globals.szFileTitle, sizeof(Globals.szFileTitle));
50 /***********************************************************************
54 * All handling of main menu events
56 static int NOTEPAD_MenuCommand(WPARAM wParam)
60 case CMD_NEW: DIALOG_FileNew(); break;
61 case CMD_OPEN: DIALOG_FileOpen(); break;
62 case CMD_SAVE: DIALOG_FileSave(); break;
63 case CMD_SAVE_AS: DIALOG_FileSaveAs(); break;
64 case CMD_PRINT: DIALOG_FilePrint(); break;
65 case CMD_PAGE_SETUP: DIALOG_FilePageSetup(); break;
66 case CMD_PRINTER_SETUP: DIALOG_FilePrinterSetup();break;
67 case CMD_EXIT: DIALOG_FileExit(); break;
69 case CMD_UNDO: DIALOG_EditUndo(); break;
70 case CMD_CUT: DIALOG_EditCut(); break;
71 case CMD_COPY: DIALOG_EditCopy(); break;
72 case CMD_PASTE: DIALOG_EditPaste(); break;
73 case CMD_DELETE: DIALOG_EditDelete(); break;
74 case CMD_SELECT_ALL: DIALOG_EditSelectAll(); break;
75 case CMD_TIME_DATE: DIALOG_EditTimeDate();break;
77 case CMD_SEARCH: DIALOG_Search(); break;
78 case CMD_SEARCH_NEXT: DIALOG_SearchNext(); break;
80 case CMD_WRAP: DIALOG_EditWrap(); break;
81 case CMD_FONT: DIALOG_SelectFont(); break;
83 case CMD_HELP_CONTENTS: DIALOG_HelpContents(); break;
84 case CMD_HELP_SEARCH: DIALOG_HelpSearch(); break;
85 case CMD_HELP_ON_HELP: DIALOG_HelpHelp(); break;
86 case CMD_LICENSE: DIALOG_HelpLicense(); break;
87 case CMD_NO_WARRANTY: DIALOG_HelpNoWarranty(); break;
88 case CMD_ABOUT_WINE: DIALOG_HelpAboutWine(); break;
96 /***********************************************************************
99 static VOID NOTEPAD_InitData(VOID)
101 LPWSTR p = Globals.szFilter;
102 static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
103 static const WCHAR all_files[] = { '*','.','*',0 };
105 LoadString(Globals.hInstance, STRING_TEXT_FILES_TXT, p, MAX_STRING_LEN);
107 lstrcpy(p, txt_files);
109 LoadString(Globals.hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
111 lstrcpy(p, all_files);
116 /***********************************************************************
117 * Enable/disable items on the menu based on control state
119 static VOID NOTEPAD_InitMenuPopup(HMENU menu, int index)
123 EnableMenuItem(menu, CMD_UNDO,
124 SendMessage(Globals.hEdit, EM_CANUNDO, 0, 0) ? MF_ENABLED : MF_GRAYED);
125 EnableMenuItem(menu, CMD_PASTE,
126 IsClipboardFormatAvailable(CF_TEXT) ? MF_ENABLED : MF_GRAYED);
127 enable = SendMessage(Globals.hEdit, EM_GETSEL, 0, 0);
128 enable = (HIWORD(enable) == LOWORD(enable)) ? MF_GRAYED : MF_ENABLED;
129 EnableMenuItem(menu, CMD_CUT, enable);
130 EnableMenuItem(menu, CMD_COPY, enable);
131 EnableMenuItem(menu, CMD_DELETE, enable);
133 EnableMenuItem(menu, CMD_SELECT_ALL,
134 GetWindowTextLength(Globals.hEdit) ? MF_ENABLED : MF_GRAYED);
137 /***********************************************************************
141 static LRESULT WINAPI NOTEPAD_WndProc(HWND hWnd, UINT msg, WPARAM wParam,
148 static const WCHAR editW[] = { 'e','d','i','t',0 };
150 GetClientRect(hWnd, &rc);
151 Globals.hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, editW, NULL,
152 WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | WS_HSCROLL |
153 ES_AUTOVSCROLL | ES_MULTILINE,
154 0, 0, rc.right, rc.bottom, hWnd,
155 NULL, Globals.hInstance, NULL);
160 NOTEPAD_MenuCommand(LOWORD(wParam));
163 case WM_DESTROYCLIPBOARD:
164 /*MessageBox(Globals.hMainWnd, "Empty clipboard", "Debug", MB_ICONEXCLAMATION);*/
173 case WM_QUERYENDSESSION:
184 SetWindowPos(Globals.hEdit, NULL, 0, 0, LOWORD(lParam), HIWORD(lParam),
185 SWP_NOOWNERZORDER | SWP_NOZORDER);
189 SetFocus(Globals.hEdit);
194 WCHAR szFileName[MAX_PATH];
195 HANDLE hDrop = (HANDLE) wParam;
197 DragQueryFile(hDrop, 0, szFileName, SIZEOF(szFileName));
199 DoOpenFile(szFileName);
203 case WM_INITMENUPOPUP:
204 NOTEPAD_InitMenuPopup((HMENU)wParam, lParam);
208 return DefWindowProc(hWnd, msg, wParam, lParam);
213 static int AlertFileDoesNotExist(LPCWSTR szFileName)
216 WCHAR szMessage[MAX_STRING_LEN];
217 WCHAR szResource[MAX_STRING_LEN];
219 LoadString(Globals.hInstance, STRING_DOESNOTEXIST, szResource, SIZEOF(szResource));
220 wsprintf(szMessage, szResource, szFileName);
222 LoadString(Globals.hInstance, STRING_ERROR, szResource, SIZEOF(szResource));
224 nResult = MessageBox(Globals.hMainWnd, szMessage, szResource,
225 MB_ICONEXCLAMATION | MB_YESNO);
230 static void HandleCommandLine(LPWSTR cmdline)
235 /* skip white space */
236 while (*cmdline == ' ') cmdline++;
238 /* skip executable name */
239 delimiter = (*cmdline == '"' ? '"' : ' ');
241 if (*cmdline == delimiter) cmdline++;
243 while (*cmdline && *cmdline != delimiter) cmdline++;
245 if (*cmdline == delimiter) cmdline++;
247 while (*cmdline == ' ' || *cmdline == '-' || *cmdline == '/')
251 if (*cmdline++ == ' ') continue;
254 if (option) cmdline++;
255 while (*cmdline == ' ') cmdline++;
268 /* file name is passed in the command line */
273 if (cmdline[0] == '"')
276 cmdline[lstrlen(cmdline) - 1] = 0;
279 if (FileExists(cmdline))
286 static const WCHAR txtW[] = { '.','t','x','t',0 };
288 /* try to find file with ".txt" extension */
289 if (!lstrcmp(txtW, cmdline + lstrlen(cmdline) - lstrlen(txtW)))
296 lstrcpyn(buf, cmdline, MAX_PATH - lstrlen(txtW) - 1);
299 file_exists = FileExists(buf);
305 DoOpenFile(file_name);
306 InvalidateRect(Globals.hMainWnd, NULL, FALSE);
312 switch (AlertFileDoesNotExist(file_name)) {
314 DoOpenFile(file_name);
324 /***********************************************************************
328 int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
333 static const WCHAR className[] = {'N','P','C','l','a','s','s',0};
334 static const WCHAR winName[] = {'N','o','t','e','p','a','d',0};
336 aFINDMSGSTRING = RegisterWindowMessage(FINDMSGSTRING);
338 ZeroMemory(&Globals, sizeof(Globals));
339 Globals.hInstance = hInstance;
341 ZeroMemory(&class, sizeof(class));
342 class.cbSize = sizeof(class);
343 class.lpfnWndProc = NOTEPAD_WndProc;
344 class.hInstance = Globals.hInstance;
345 class.hIcon = LoadIcon(0, IDI_APPLICATION);
346 class.hCursor = LoadCursor(0, IDC_ARROW);
347 class.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
348 class.lpszMenuName = MAKEINTRESOURCE(MAIN_MENU);
349 class.lpszClassName = className;
351 if (!RegisterClassEx(&class)) return FALSE;
356 CreateWindow(className, winName, WS_OVERLAPPEDWINDOW,
357 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
358 NULL, NULL, Globals.hInstance, NULL);
359 if (!Globals.hMainWnd)
368 ShowWindow(Globals.hMainWnd, show);
369 UpdateWindow(Globals.hMainWnd);
370 DragAcceptFiles(Globals.hMainWnd, TRUE);
372 HandleCommandLine(GetCommandLine());
374 hAccel = LoadAccelerators( hInstance, MAKEINTRESOURCE(ID_ACCEL) );
376 while (GetMessage(&msg, 0, 0, 0))
378 if (!TranslateAccelerator(Globals.hMainWnd, hAccel, &msg) && !IsDialogMessage(Globals.hFindReplaceDlg, &msg))
380 TranslateMessage(&msg);
381 DispatchMessage(&msg);