Release 20030408.
[wine/gsoc-2012-control.git] / programs / notepad / main.c
blob1da23ba2305366dd604ec7273427a4258784b1f5
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 #include <windows.h>
26 #include <richedit.h>
27 #include <stdio.h>
29 #include "main.h"
30 #include "dialog.h"
31 #include "notepad_res.h"
33 NOTEPAD_GLOBALS Globals;
35 /***********************************************************************
37 * SetFileName
39 * Sets Global File Name.
41 VOID SetFileName(LPSTR szFileName)
43 lstrcpy(Globals.szFileName, szFileName);
44 Globals.szFileTitle[0] = 0;
45 GetFileTitle(szFileName, Globals.szFileTitle, sizeof(Globals.szFileTitle));
48 /***********************************************************************
50 * NOTEPAD_MenuCommand
52 * All handling of main menu events
55 int NOTEPAD_MenuCommand(WPARAM wParam)
57 switch (wParam)
59 case CMD_NEW: DIALOG_FileNew(); break;
60 case CMD_OPEN: DIALOG_FileOpen(); break;
61 case CMD_SAVE: DIALOG_FileSave(); break;
62 case CMD_SAVE_AS: DIALOG_FileSaveAs(); break;
63 case CMD_PRINT: DIALOG_FilePrint(); break;
64 case CMD_PAGE_SETUP: DIALOG_FilePageSetup(); break;
65 case CMD_PRINTER_SETUP: DIALOG_FilePrinterSetup();break;
66 case CMD_EXIT: DIALOG_FileExit(); break;
68 case CMD_UNDO: DIALOG_EditUndo(); break;
69 case CMD_CUT: DIALOG_EditCut(); break;
70 case CMD_COPY: DIALOG_EditCopy(); break;
71 case CMD_PASTE: DIALOG_EditPaste(); break;
72 case CMD_DELETE: DIALOG_EditDelete(); break;
73 case CMD_SELECT_ALL: DIALOG_EditSelectAll(); break;
74 case CMD_TIME_DATE: DIALOG_EditTimeDate();break;
76 case CMD_SEARCH: DIALOG_Search(); break;
77 case CMD_SEARCH_NEXT: DIALOG_SearchNext(); break;
79 case CMD_WRAP: DIALOG_EditWrap(); break;
80 case CMD_FONT: DIALOG_SelectFont(); break;
82 case CMD_HELP_CONTENTS: DIALOG_HelpContents(); break;
83 case CMD_HELP_SEARCH: DIALOG_HelpSearch(); break;
84 case CMD_HELP_ON_HELP: DIALOG_HelpHelp(); break;
85 case CMD_LICENSE: DIALOG_HelpLicense(); break;
86 case CMD_NO_WARRANTY: DIALOG_HelpNoWarranty(); break;
87 case CMD_ABOUT_WINE: DIALOG_HelpAboutWine(); break;
89 return 0;
92 /***********************************************************************
93 * Data Initialization
95 VOID NOTEPAD_InitData(VOID)
97 LPSTR p = Globals.szFilter;
99 LoadString(Globals.hInstance, STRING_TEXT_FILES_TXT, p, MAX_STRING_LEN);
100 p += strlen(p) + 1;
101 lstrcpy(p, "*.txt");
102 p += strlen(p) + 1;
103 LoadString(Globals.hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
104 p += strlen(p) + 1;
105 lstrcpy(p, "*.*");
106 p += strlen(p) + 1;
107 *p = '\0';
110 /***********************************************************************
112 * NOTEPAD_WndProc
114 LRESULT WINAPI NOTEPAD_WndProc(HWND hWnd, UINT msg, WPARAM wParam,
115 LPARAM lParam)
117 switch (msg) {
119 case WM_CREATE:
121 RECT rc;
122 GetClientRect(hWnd, &rc);
123 Globals.hEdit = CreateWindow("EDIT", "",
124 WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL |
125 ES_AUTOVSCROLL | ES_MULTILINE,
126 0, 0, rc.right, rc.bottom, hWnd,
127 NULL, Globals.hInstance, NULL);
128 break;
131 case WM_COMMAND:
132 NOTEPAD_MenuCommand(LOWORD(wParam));
133 break;
135 case WM_DESTROYCLIPBOARD:
136 MessageBox(Globals.hMainWnd, "Empty clipboard", "Debug", MB_ICONEXCLAMATION);
137 break;
139 case WM_CLOSE:
140 if (DoCloseFile()) {
141 DestroyWindow(hWnd);
143 break;
145 case WM_DESTROY:
146 PostQuitMessage(0);
147 break;
149 case WM_SIZE:
150 SetWindowPos(Globals.hEdit, NULL, 0, 0, LOWORD(lParam), HIWORD(lParam),
151 SWP_NOOWNERZORDER | SWP_NOZORDER);
152 break;
154 case WM_DROPFILES:
156 CHAR szFileName[MAX_PATH];
157 HANDLE hDrop = (HANDLE) wParam;
159 DragQueryFile(hDrop, 0, (CHAR *) &szFileName, sizeof(szFileName));
160 DragFinish(hDrop);
161 DoOpenFile(szFileName);
162 break;
165 default:
166 return DefWindowProc(hWnd, msg, wParam, lParam);
168 return 0;
171 int AlertFileDoesNotExist(LPSTR szFileName) {
173 int nResult;
174 CHAR szMessage[MAX_STRING_LEN];
175 CHAR szRessource[MAX_STRING_LEN];
177 LoadString(Globals.hInstance, STRING_DOESNOTEXIST, szRessource,
178 sizeof(szRessource));
179 wsprintf(szMessage, szRessource, szFileName);
181 LoadString(Globals.hInstance, STRING_ERROR, szRessource, sizeof(szRessource));
183 nResult = MessageBox(Globals.hMainWnd, szMessage, szRessource,
184 MB_ICONEXCLAMATION | MB_YESNO);
186 return(nResult);
189 void HandleCommandLine(LPSTR cmdline)
192 while (*cmdline && (*cmdline == ' ' || *cmdline == '-'))
194 CHAR option;
196 if (*cmdline++ == ' ') continue;
198 option = *cmdline;
199 if (option) cmdline++;
200 while (*cmdline && *cmdline == ' ') cmdline++;
202 switch(option)
204 case 'p':
205 case 'P': printf("Print file: ");
206 /* TODO - not yet able to print a file */
207 break;
211 if (*cmdline)
213 /* file name is passed in the command line */
214 char *file_name;
215 BOOL file_exists;
216 char buf[MAX_PATH];
218 if (cmdline[0] == '"')
220 cmdline++;
221 cmdline[strlen(cmdline) - 1] = 0;
223 if (FileExists(cmdline))
225 file_exists = TRUE;
226 file_name = cmdline;
228 else
230 /* try to find file with ".txt" extention */
231 if (!strcmp(".txt", cmdline + strlen(cmdline) - strlen(".txt")))
233 file_exists = FALSE;
234 file_name = cmdline;
236 else
238 strncpy(buf, cmdline, MAX_PATH - strlen(".txt") - 1);
239 strcat(buf, ".txt");
240 file_name = buf;
241 file_exists = FileExists(buf);
245 if (file_exists)
247 DoOpenFile(file_name);
248 InvalidateRect(Globals.hMainWnd, NULL, FALSE);
250 else
252 switch (AlertFileDoesNotExist(file_name)) {
253 case IDYES:
254 DoOpenFile(file_name);
255 break;
257 case IDNO:
258 break;
264 /***********************************************************************
266 * WinMain
268 int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
270 MSG msg;
271 HACCEL hAccel;
272 WNDCLASSEX class;
273 char className[] = "NPClass";
274 char winName[] = "Notepad";
276 ZeroMemory(&Globals, sizeof(Globals));
277 Globals.hInstance = hInstance;
279 ZeroMemory(&class, sizeof(class));
280 class.cbSize = sizeof(class);
281 class.lpfnWndProc = NOTEPAD_WndProc;
282 class.hInstance = Globals.hInstance;
283 class.hIcon = LoadIcon(0, IDI_APPLICATION);
284 class.hCursor = LoadCursor(0, IDC_ARROW);
285 class.hbrBackground = (HBRUSH)(COLOR_WINDOW);
286 class.lpszMenuName = MAKEINTRESOURCE(MAIN_MENU);
287 class.lpszClassName = className;
289 if (!RegisterClassEx(&class)) return FALSE;
291 /* Setup windows */
293 Globals.hMainWnd =
294 CreateWindow(className, winName, WS_OVERLAPPEDWINDOW,
295 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
296 NULL, NULL, Globals.hInstance, NULL);
297 if (!Globals.hMainWnd)
299 ShowLastError();
300 ExitProcess(1);
303 NOTEPAD_InitData();
304 DIALOG_FileNew();
306 ShowWindow(Globals.hMainWnd, show);
307 UpdateWindow(Globals.hMainWnd);
308 DragAcceptFiles(Globals.hMainWnd, TRUE);
310 HandleCommandLine(cmdline);
312 hAccel=LoadAccelerators( hInstance, MAKEINTRESOURCE(ID_ACCEL) );
314 if( hAccel!=NULL )
316 while( GetMessage(&msg, 0, 0, 0)) {
317 if( !TranslateAccelerator( Globals.hMainWnd, hAccel, &msg ) ) {
318 TranslateMessage( &msg );
319 DispatchMessage( &msg );
322 } else
324 while (GetMessage(&msg, 0, 0, 0)) {
325 TranslateMessage(&msg);
326 DispatchMessage(&msg);
329 return msg.wParam;