winetest: Generate the list of test resources in make_makefiles.
[wine/wine-gecko.git] / programs / notepad / main.c
blob139d61774b79ae3702dc2cedabe6db53c1357e16
1 /*
2 * Notepad
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #define UNICODE
27 #include <windows.h>
28 #include <stdio.h>
30 #include "main.h"
31 #include "dialog.h"
32 #include "notepad_res.h"
34 NOTEPAD_GLOBALS Globals;
35 static ATOM aFINDMSGSTRING;
37 /***********************************************************************
39 * SetFileName
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 /***********************************************************************
52 * NOTEPAD_InitFont
54 * Initialize font for the edit window
56 static VOID NOTEPAD_InitFont(void)
58 LOGFONT *lf = &Globals.lfFont;
59 static const WCHAR systemW[] = { 'S','y','s','t','e','m',0 };
61 lf->lfHeight = -40;
62 lf->lfWidth = 0;
63 lf->lfEscapement = 0;
64 lf->lfOrientation = 0;
65 lf->lfWeight = FW_REGULAR;
66 lf->lfItalic = FALSE;
67 lf->lfUnderline = FALSE;
68 lf->lfStrikeOut = FALSE;
69 lf->lfCharSet = DEFAULT_CHARSET;
70 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
71 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
72 lf->lfQuality = DEFAULT_QUALITY;
73 lf->lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
74 lstrcpy(lf->lfFaceName, systemW);
76 Globals.hFont = CreateFontIndirect(lf);
77 SendMessage(Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, (LPARAM)FALSE);
80 /***********************************************************************
82 * NOTEPAD_MenuCommand
84 * All handling of main menu events
86 static int NOTEPAD_MenuCommand(WPARAM wParam)
88 switch (wParam)
90 case CMD_NEW: DIALOG_FileNew(); break;
91 case CMD_OPEN: DIALOG_FileOpen(); break;
92 case CMD_SAVE: DIALOG_FileSave(); break;
93 case CMD_SAVE_AS: DIALOG_FileSaveAs(); break;
94 case CMD_PRINT: DIALOG_FilePrint(); break;
95 case CMD_PAGE_SETUP: DIALOG_FilePageSetup(); break;
96 case CMD_PRINTER_SETUP: DIALOG_FilePrinterSetup();break;
97 case CMD_EXIT: DIALOG_FileExit(); break;
99 case CMD_UNDO: DIALOG_EditUndo(); break;
100 case CMD_CUT: DIALOG_EditCut(); break;
101 case CMD_COPY: DIALOG_EditCopy(); break;
102 case CMD_PASTE: DIALOG_EditPaste(); break;
103 case CMD_DELETE: DIALOG_EditDelete(); break;
104 case CMD_SELECT_ALL: DIALOG_EditSelectAll(); break;
105 case CMD_TIME_DATE: DIALOG_EditTimeDate();break;
107 case CMD_SEARCH: DIALOG_Search(); break;
108 case CMD_SEARCH_NEXT: DIALOG_SearchNext(); break;
110 case CMD_WRAP: DIALOG_EditWrap(); break;
111 case CMD_FONT: DIALOG_SelectFont(); break;
113 case CMD_HELP_CONTENTS: DIALOG_HelpContents(); break;
114 case CMD_HELP_SEARCH: DIALOG_HelpSearch(); break;
115 case CMD_HELP_ON_HELP: DIALOG_HelpHelp(); break;
116 case CMD_LICENSE: DIALOG_HelpLicense(); break;
117 case CMD_NO_WARRANTY: DIALOG_HelpNoWarranty(); break;
118 case CMD_ABOUT_WINE: DIALOG_HelpAboutWine(); break;
120 default:
121 break;
123 return 0;
126 /***********************************************************************
127 * Data Initialization
129 static VOID NOTEPAD_InitData(VOID)
131 LPWSTR p = Globals.szFilter;
132 static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
133 static const WCHAR all_files[] = { '*','.','*',0 };
135 LoadString(Globals.hInstance, STRING_TEXT_FILES_TXT, p, MAX_STRING_LEN);
136 p += lstrlen(p) + 1;
137 lstrcpy(p, txt_files);
138 p += lstrlen(p) + 1;
139 LoadString(Globals.hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
140 p += lstrlen(p) + 1;
141 lstrcpy(p, all_files);
142 p += lstrlen(p) + 1;
143 *p = '\0';
144 Globals.hDevMode = NULL;
145 Globals.hDevNames = NULL;
147 CheckMenuItem(GetMenu(Globals.hMainWnd), CMD_WRAP,
148 MF_BYCOMMAND | (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED));
151 /***********************************************************************
152 * Enable/disable items on the menu based on control state
154 static VOID NOTEPAD_InitMenuPopup(HMENU menu, int index)
156 int enable;
158 EnableMenuItem(menu, CMD_UNDO,
159 SendMessage(Globals.hEdit, EM_CANUNDO, 0, 0) ? MF_ENABLED : MF_GRAYED);
160 EnableMenuItem(menu, CMD_PASTE,
161 IsClipboardFormatAvailable(CF_TEXT) ? MF_ENABLED : MF_GRAYED);
162 enable = SendMessage(Globals.hEdit, EM_GETSEL, 0, 0);
163 enable = (HIWORD(enable) == LOWORD(enable)) ? MF_GRAYED : MF_ENABLED;
164 EnableMenuItem(menu, CMD_CUT, enable);
165 EnableMenuItem(menu, CMD_COPY, enable);
166 EnableMenuItem(menu, CMD_DELETE, enable);
168 EnableMenuItem(menu, CMD_SELECT_ALL,
169 GetWindowTextLength(Globals.hEdit) ? MF_ENABLED : MF_GRAYED);
172 /***********************************************************************
174 * NOTEPAD_WndProc
176 static LRESULT WINAPI NOTEPAD_WndProc(HWND hWnd, UINT msg, WPARAM wParam,
177 LPARAM lParam)
179 switch (msg) {
181 case WM_CREATE:
183 static const WCHAR editW[] = { 'e','d','i','t',0 };
184 DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL |
185 ES_AUTOVSCROLL | ES_MULTILINE;
186 RECT rc;
187 GetClientRect(hWnd, &rc);
189 if (!Globals.bWrapLongLines) dwStyle |= WS_HSCROLL | ES_AUTOHSCROLL;
191 Globals.hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, editW, NULL,
192 dwStyle,
193 0, 0, rc.right, rc.bottom, hWnd,
194 NULL, Globals.hInstance, NULL);
195 NOTEPAD_InitFont();
196 break;
199 case WM_COMMAND:
200 NOTEPAD_MenuCommand(LOWORD(wParam));
201 break;
203 case WM_DESTROYCLIPBOARD:
204 /*MessageBox(Globals.hMainWnd, "Empty clipboard", "Debug", MB_ICONEXCLAMATION);*/
205 break;
207 case WM_CLOSE:
208 if (DoCloseFile()) {
209 DestroyWindow(hWnd);
211 break;
213 case WM_QUERYENDSESSION:
214 if (DoCloseFile()) {
215 return 1;
217 break;
219 case WM_DESTROY:
220 PostQuitMessage(0);
221 break;
223 case WM_SIZE:
224 SetWindowPos(Globals.hEdit, NULL, 0, 0, LOWORD(lParam), HIWORD(lParam),
225 SWP_NOOWNERZORDER | SWP_NOZORDER);
226 break;
228 case WM_SETFOCUS:
229 SetFocus(Globals.hEdit);
230 break;
232 case WM_DROPFILES:
234 WCHAR szFileName[MAX_PATH];
235 HANDLE hDrop = (HANDLE) wParam;
237 DragQueryFile(hDrop, 0, szFileName, SIZEOF(szFileName));
238 DragFinish(hDrop);
239 DoOpenFile(szFileName);
240 break;
243 case WM_INITMENUPOPUP:
244 NOTEPAD_InitMenuPopup((HMENU)wParam, lParam);
245 break;
247 default:
248 return DefWindowProc(hWnd, msg, wParam, lParam);
250 return 0;
253 static int AlertFileDoesNotExist(LPCWSTR szFileName)
255 int nResult;
256 WCHAR szMessage[MAX_STRING_LEN];
257 WCHAR szResource[MAX_STRING_LEN];
259 LoadString(Globals.hInstance, STRING_DOESNOTEXIST, szResource, SIZEOF(szResource));
260 wsprintf(szMessage, szResource, szFileName);
262 LoadString(Globals.hInstance, STRING_ERROR, szResource, SIZEOF(szResource));
264 nResult = MessageBox(Globals.hMainWnd, szMessage, szResource,
265 MB_ICONEXCLAMATION | MB_YESNO);
267 return(nResult);
270 static void HandleCommandLine(LPWSTR cmdline)
272 WCHAR delimiter;
273 int opt_print=0;
275 /* skip white space */
276 while (*cmdline == ' ') cmdline++;
278 /* skip executable name */
279 delimiter = (*cmdline == '"' ? '"' : ' ');
281 if (*cmdline == delimiter) cmdline++;
283 while (*cmdline && *cmdline != delimiter) cmdline++;
285 if (*cmdline == delimiter) cmdline++;
287 while (*cmdline == ' ' || *cmdline == '-' || *cmdline == '/')
289 WCHAR option;
291 if (*cmdline++ == ' ') continue;
293 option = *cmdline;
294 if (option) cmdline++;
295 while (*cmdline == ' ') cmdline++;
297 switch(option)
299 case 'p':
300 case 'P':
301 opt_print=1;
302 break;
306 if (*cmdline)
308 /* file name is passed in the command line */
309 LPCWSTR file_name;
310 BOOL file_exists;
311 WCHAR buf[MAX_PATH];
313 if (cmdline[0] == '"')
315 cmdline++;
316 cmdline[lstrlen(cmdline) - 1] = 0;
319 if (FileExists(cmdline))
321 file_exists = TRUE;
322 file_name = cmdline;
324 else
326 static const WCHAR txtW[] = { '.','t','x','t',0 };
328 /* try to find file with ".txt" extension */
329 if (!lstrcmp(txtW, cmdline + lstrlen(cmdline) - lstrlen(txtW)))
331 file_exists = FALSE;
332 file_name = cmdline;
334 else
336 lstrcpyn(buf, cmdline, MAX_PATH - lstrlen(txtW) - 1);
337 lstrcat(buf, txtW);
338 file_name = buf;
339 file_exists = FileExists(buf);
343 if (file_exists)
345 DoOpenFile(file_name);
346 InvalidateRect(Globals.hMainWnd, NULL, FALSE);
347 if (opt_print)
348 DIALOG_FilePrint();
350 else
352 switch (AlertFileDoesNotExist(file_name)) {
353 case IDYES:
354 DoOpenFile(file_name);
355 break;
357 case IDNO:
358 break;
364 /***********************************************************************
366 * WinMain
368 int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
370 MSG msg;
371 HACCEL hAccel;
372 WNDCLASSEX class;
373 static const WCHAR className[] = {'N','o','t','e','p','a','d',0};
374 static const WCHAR winName[] = {'N','o','t','e','p','a','d',0};
376 aFINDMSGSTRING = RegisterWindowMessage(FINDMSGSTRING);
378 ZeroMemory(&Globals, sizeof(Globals));
379 Globals.hInstance = hInstance;
380 Globals.bWrapLongLines = TRUE;
382 ZeroMemory(&class, sizeof(class));
383 class.cbSize = sizeof(class);
384 class.lpfnWndProc = NOTEPAD_WndProc;
385 class.hInstance = Globals.hInstance;
386 class.hIcon = LoadIcon(0, IDI_APPLICATION);
387 class.hCursor = LoadCursor(0, IDC_ARROW);
388 class.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
389 class.lpszMenuName = MAKEINTRESOURCE(MAIN_MENU);
390 class.lpszClassName = className;
392 if (!RegisterClassEx(&class)) return FALSE;
394 /* Setup windows */
396 Globals.hMainWnd =
397 CreateWindow(className, winName, WS_OVERLAPPEDWINDOW,
398 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
399 NULL, NULL, Globals.hInstance, NULL);
400 if (!Globals.hMainWnd)
402 ShowLastError();
403 ExitProcess(1);
406 NOTEPAD_InitData();
407 DIALOG_FileNew();
409 ShowWindow(Globals.hMainWnd, show);
410 UpdateWindow(Globals.hMainWnd);
411 DragAcceptFiles(Globals.hMainWnd, TRUE);
413 HandleCommandLine(GetCommandLine());
415 hAccel = LoadAccelerators( hInstance, MAKEINTRESOURCE(ID_ACCEL) );
417 while (GetMessage(&msg, 0, 0, 0))
419 if (!TranslateAccelerator(Globals.hMainWnd, hAccel, &msg) && !IsDialogMessage(Globals.hFindReplaceDlg, &msg))
421 TranslateMessage(&msg);
422 DispatchMessage(&msg);
425 return msg.wParam;