- Remove redunant braces.
[wine/multimedia.git] / programs / notepad / main.c
blob063ccac80611df14ccbde91ab3ccb78dd667f123
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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_MenuCommand
54 * All handling of main menu events
56 static int NOTEPAD_MenuCommand(WPARAM wParam)
58 switch (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;
90 default:
91 break;
93 return 0;
96 /***********************************************************************
97 * Data Initialization
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);
106 p += lstrlen(p) + 1;
107 lstrcpy(p, txt_files);
108 p += lstrlen(p) + 1;
109 LoadString(Globals.hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
110 p += lstrlen(p) + 1;
111 lstrcpy(p, all_files);
112 p += lstrlen(p) + 1;
113 *p = '\0';
116 /***********************************************************************
117 * Enable/disable items on the menu based on control state
119 static VOID NOTEPAD_InitMenuPopup(HMENU menu, int index)
121 int enable;
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 /***********************************************************************
139 * NOTEPAD_WndProc
141 static LRESULT WINAPI NOTEPAD_WndProc(HWND hWnd, UINT msg, WPARAM wParam,
142 LPARAM lParam)
144 switch (msg) {
146 case WM_CREATE:
148 static const WCHAR editW[] = { 'e','d','i','t',0 };
149 RECT rc;
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);
156 break;
159 case WM_COMMAND:
160 NOTEPAD_MenuCommand(LOWORD(wParam));
161 break;
163 case WM_DESTROYCLIPBOARD:
164 /*MessageBox(Globals.hMainWnd, "Empty clipboard", "Debug", MB_ICONEXCLAMATION);*/
165 break;
167 case WM_CLOSE:
168 if (DoCloseFile()) {
169 DestroyWindow(hWnd);
171 break;
173 case WM_QUERYENDSESSION:
174 if (DoCloseFile()) {
175 return 1;
177 break;
179 case WM_DESTROY:
180 PostQuitMessage(0);
181 break;
183 case WM_SIZE:
184 SetWindowPos(Globals.hEdit, NULL, 0, 0, LOWORD(lParam), HIWORD(lParam),
185 SWP_NOOWNERZORDER | SWP_NOZORDER);
186 break;
188 case WM_SETFOCUS:
189 SetFocus(Globals.hEdit);
190 break;
192 case WM_DROPFILES:
194 WCHAR szFileName[MAX_PATH];
195 HANDLE hDrop = (HANDLE) wParam;
197 DragQueryFile(hDrop, 0, szFileName, SIZEOF(szFileName));
198 DragFinish(hDrop);
199 DoOpenFile(szFileName);
200 break;
203 case WM_INITMENUPOPUP:
204 NOTEPAD_InitMenuPopup((HMENU)wParam, lParam);
205 break;
207 default:
208 return DefWindowProc(hWnd, msg, wParam, lParam);
210 return 0;
213 static int AlertFileDoesNotExist(LPCWSTR szFileName)
215 int nResult;
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);
227 return(nResult);
230 static void HandleCommandLine(LPWSTR cmdline)
232 WCHAR delimiter;
233 int opt_print=0;
235 /* skip white space */
236 while (*cmdline == ' ') cmdline++;
238 /* skip executable name */
239 delimiter = (*cmdline == '"' ? '"' : ' ');
243 cmdline++;
245 while (*cmdline && *cmdline != delimiter);
246 if (*cmdline == delimiter) cmdline++;
248 while (*cmdline == ' ' || *cmdline == '-' || *cmdline == '/')
250 WCHAR option;
252 if (*cmdline++ == ' ') continue;
254 option = *cmdline;
255 if (option) cmdline++;
256 while (*cmdline == ' ') cmdline++;
258 switch(option)
260 case 'p':
261 case 'P':
262 opt_print=1;
263 break;
267 if (*cmdline)
269 /* file name is passed in the command line */
270 LPCWSTR file_name;
271 BOOL file_exists;
272 WCHAR buf[MAX_PATH];
274 if (cmdline[0] == '"')
276 cmdline++;
277 cmdline[lstrlen(cmdline) - 1] = 0;
280 if (FileExists(cmdline))
282 file_exists = TRUE;
283 file_name = cmdline;
285 else
287 static const WCHAR txtW[] = { '.','t','x','t',0 };
289 /* try to find file with ".txt" extension */
290 if (!lstrcmp(txtW, cmdline + lstrlen(cmdline) - lstrlen(txtW)))
292 file_exists = FALSE;
293 file_name = cmdline;
295 else
297 lstrcpyn(buf, cmdline, MAX_PATH - lstrlen(txtW) - 1);
298 lstrcat(buf, txtW);
299 file_name = buf;
300 file_exists = FileExists(buf);
304 if (file_exists)
306 DoOpenFile(file_name);
307 InvalidateRect(Globals.hMainWnd, NULL, FALSE);
308 if (opt_print)
309 DIALOG_FilePrint();
311 else
313 switch (AlertFileDoesNotExist(file_name)) {
314 case IDYES:
315 DoOpenFile(file_name);
316 break;
318 case IDNO:
319 break;
325 /***********************************************************************
327 * WinMain
329 int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
331 MSG msg;
332 HACCEL hAccel;
333 WNDCLASSEX class;
334 static const WCHAR className[] = {'N','P','C','l','a','s','s',0};
335 static const WCHAR winName[] = {'N','o','t','e','p','a','d',0};
337 aFINDMSGSTRING = RegisterWindowMessage(FINDMSGSTRING);
339 ZeroMemory(&Globals, sizeof(Globals));
340 Globals.hInstance = hInstance;
342 ZeroMemory(&class, sizeof(class));
343 class.cbSize = sizeof(class);
344 class.lpfnWndProc = NOTEPAD_WndProc;
345 class.hInstance = Globals.hInstance;
346 class.hIcon = LoadIcon(0, IDI_APPLICATION);
347 class.hCursor = LoadCursor(0, IDC_ARROW);
348 class.hbrBackground = (HBRUSH)(COLOR_WINDOW);
349 class.lpszMenuName = MAKEINTRESOURCE(MAIN_MENU);
350 class.lpszClassName = className;
352 if (!RegisterClassEx(&class)) return FALSE;
354 /* Setup windows */
356 Globals.hMainWnd =
357 CreateWindow(className, winName, WS_OVERLAPPEDWINDOW,
358 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
359 NULL, NULL, Globals.hInstance, NULL);
360 if (!Globals.hMainWnd)
362 ShowLastError();
363 ExitProcess(1);
366 NOTEPAD_InitData();
367 DIALOG_FileNew();
369 ShowWindow(Globals.hMainWnd, show);
370 UpdateWindow(Globals.hMainWnd);
371 DragAcceptFiles(Globals.hMainWnd, TRUE);
373 HandleCommandLine(GetCommandLine());
375 hAccel = LoadAccelerators( hInstance, MAKEINTRESOURCE(ID_ACCEL) );
377 while (GetMessage(&msg, 0, 0, 0))
379 if (!TranslateAccelerator(Globals.hMainWnd, hAccel, &msg) && !IsDialogMessage(Globals.hFindReplaceDlg, &msg))
381 TranslateMessage(&msg);
382 DispatchMessage(&msg);
385 return msg.wParam;