notepad: Add a Font menu item for the Chinese locale.
[wine] / programs / notepad / main.c
1 /*
2  *  Notepad
3  *
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
8  *
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.
13  *
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.
18  *
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
22  *
23  */
24
25 #define UNICODE
26
27 #include <windows.h>
28 #include <stdio.h>
29
30 #include "main.h"
31 #include "dialog.h"
32 #include "notepad_res.h"
33
34 NOTEPAD_GLOBALS Globals;
35 static ATOM aFINDMSGSTRING;
36
37 /***********************************************************************
38  *
39  *           SetFileName
40  *
41  *  Sets Global File Name.
42  */
43 VOID SetFileName(LPCWSTR szFileName)
44 {
45     lstrcpy(Globals.szFileName, szFileName);
46     Globals.szFileTitle[0] = 0;
47     GetFileTitle(szFileName, Globals.szFileTitle, sizeof(Globals.szFileTitle));
48 }
49
50 /***********************************************************************
51  *
52  *           NOTEPAD_InitFont
53  *
54  *  Initialize font for the edit window
55  */
56 static VOID NOTEPAD_InitFont()
57 {
58     LOGFONT *lf = &Globals.lfFont;
59     static const WCHAR systemW[] = { 'S','y','s','t','e','m',0 };
60
61     lf->lfHeight        = -10;
62     lf->lfWidth         = 0;
63     lf->lfEscapement    = 0;
64     lf->lfOrientation   = 0;
65     lf->lfWeight        = FW_BOLD;
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);
75     
76     Globals.hFont = CreateFontIndirect(lf);
77     SendMessage(Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, (LPARAM)FALSE);
78 }
79
80 /***********************************************************************
81  *
82  *           NOTEPAD_MenuCommand
83  *
84  *  All handling of main menu events
85  */
86 static int NOTEPAD_MenuCommand(WPARAM wParam)
87 {
88     switch (wParam)
89     {
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;
98
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;
106
107     case CMD_SEARCH:           DIALOG_Search(); break;
108     case CMD_SEARCH_NEXT:      DIALOG_SearchNext(); break;
109                                
110     case CMD_WRAP:             DIALOG_EditWrap(); break;
111     case CMD_FONT:             DIALOG_SelectFont(); break;
112
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;
119
120     default:
121         break;
122     }
123    return 0;
124 }
125
126 /***********************************************************************
127  * Data Initialization
128  */
129 static VOID NOTEPAD_InitData(VOID)
130 {
131     LPWSTR p = Globals.szFilter;
132     static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
133     static const WCHAR all_files[] = { '*','.','*',0 };
134
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;
146 }
147
148 /***********************************************************************
149  * Enable/disable items on the menu based on control state
150  */
151 static VOID NOTEPAD_InitMenuPopup(HMENU menu, int index)
152 {
153     int enable;
154
155     EnableMenuItem(menu, CMD_UNDO,
156         SendMessage(Globals.hEdit, EM_CANUNDO, 0, 0) ? MF_ENABLED : MF_GRAYED);
157     EnableMenuItem(menu, CMD_PASTE,
158         IsClipboardFormatAvailable(CF_TEXT) ? MF_ENABLED : MF_GRAYED);
159     enable = SendMessage(Globals.hEdit, EM_GETSEL, 0, 0);
160     enable = (HIWORD(enable) == LOWORD(enable)) ? MF_GRAYED : MF_ENABLED;
161     EnableMenuItem(menu, CMD_CUT, enable);
162     EnableMenuItem(menu, CMD_COPY, enable);
163     EnableMenuItem(menu, CMD_DELETE, enable);
164     
165     EnableMenuItem(menu, CMD_SELECT_ALL,
166         GetWindowTextLength(Globals.hEdit) ? MF_ENABLED : MF_GRAYED);
167 }
168
169 /***********************************************************************
170  *
171  *           NOTEPAD_WndProc
172  */
173 static LRESULT WINAPI NOTEPAD_WndProc(HWND hWnd, UINT msg, WPARAM wParam,
174                                LPARAM lParam)
175 {
176     switch (msg) {
177
178     case WM_CREATE:
179     {
180         static const WCHAR editW[] = { 'e','d','i','t',0 };
181         RECT rc;
182         GetClientRect(hWnd, &rc);
183         Globals.hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, editW, NULL,
184                              WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | WS_HSCROLL |
185                              ES_AUTOVSCROLL | ES_MULTILINE,
186                              0, 0, rc.right, rc.bottom, hWnd,
187                              NULL, Globals.hInstance, NULL);
188         NOTEPAD_InitFont();
189         break;
190     }
191
192     case WM_COMMAND:
193         NOTEPAD_MenuCommand(LOWORD(wParam));
194         break;
195
196     case WM_DESTROYCLIPBOARD:
197         /*MessageBox(Globals.hMainWnd, "Empty clipboard", "Debug", MB_ICONEXCLAMATION);*/
198         break;
199
200     case WM_CLOSE:
201         if (DoCloseFile()) {
202             DestroyWindow(hWnd);
203         }
204         break;
205
206     case WM_QUERYENDSESSION:
207         if (DoCloseFile()) {
208             return 1;
209         }
210         break;
211
212     case WM_DESTROY:
213         PostQuitMessage(0);
214         break;
215
216     case WM_SIZE:
217         SetWindowPos(Globals.hEdit, NULL, 0, 0, LOWORD(lParam), HIWORD(lParam),
218                      SWP_NOOWNERZORDER | SWP_NOZORDER);
219         break;
220
221     case WM_SETFOCUS:
222         SetFocus(Globals.hEdit);
223         break;
224
225     case WM_DROPFILES:
226     {
227         WCHAR szFileName[MAX_PATH];
228         HANDLE hDrop = (HANDLE) wParam;
229
230         DragQueryFile(hDrop, 0, szFileName, SIZEOF(szFileName));
231         DragFinish(hDrop);
232         DoOpenFile(szFileName);
233         break;
234     }
235     
236     case WM_INITMENUPOPUP:
237         NOTEPAD_InitMenuPopup((HMENU)wParam, lParam);
238         break;
239
240     default:
241         return DefWindowProc(hWnd, msg, wParam, lParam);
242     }
243     return 0;
244 }
245
246 static int AlertFileDoesNotExist(LPCWSTR szFileName)
247 {
248    int nResult;
249    WCHAR szMessage[MAX_STRING_LEN];
250    WCHAR szResource[MAX_STRING_LEN];
251
252    LoadString(Globals.hInstance, STRING_DOESNOTEXIST, szResource, SIZEOF(szResource));
253    wsprintf(szMessage, szResource, szFileName);
254
255    LoadString(Globals.hInstance, STRING_ERROR, szResource, SIZEOF(szResource));
256
257    nResult = MessageBox(Globals.hMainWnd, szMessage, szResource,
258                         MB_ICONEXCLAMATION | MB_YESNO);
259
260    return(nResult);
261 }
262
263 static void HandleCommandLine(LPWSTR cmdline)
264 {
265     WCHAR delimiter;
266     int opt_print=0;
267     
268     /* skip white space */
269     while (*cmdline == ' ') cmdline++;
270
271     /* skip executable name */
272     delimiter = (*cmdline == '"' ? '"' : ' ');
273
274     if (*cmdline == delimiter) cmdline++;
275
276     while (*cmdline && *cmdline != delimiter) cmdline++;
277
278     if (*cmdline == delimiter) cmdline++;
279
280     while (*cmdline == ' ' || *cmdline == '-' || *cmdline == '/')
281     {
282         WCHAR option;
283
284         if (*cmdline++ == ' ') continue;
285
286         option = *cmdline;
287         if (option) cmdline++;
288         while (*cmdline == ' ') cmdline++;
289
290         switch(option)
291         {
292             case 'p':
293             case 'P':
294                 opt_print=1;
295                 break;
296         }
297     }
298
299     if (*cmdline)
300     {
301         /* file name is passed in the command line */
302         LPCWSTR file_name;
303         BOOL file_exists;
304         WCHAR buf[MAX_PATH];
305
306         if (cmdline[0] == '"')
307         {
308             cmdline++;
309             cmdline[lstrlen(cmdline) - 1] = 0;
310         }
311
312         if (FileExists(cmdline))
313         {
314             file_exists = TRUE;
315             file_name = cmdline;
316         }
317         else
318         {
319             static const WCHAR txtW[] = { '.','t','x','t',0 };
320
321             /* try to find file with ".txt" extension */
322             if (!lstrcmp(txtW, cmdline + lstrlen(cmdline) - lstrlen(txtW)))
323             {
324                 file_exists = FALSE;
325                 file_name = cmdline;
326             }
327             else
328             {
329                 lstrcpyn(buf, cmdline, MAX_PATH - lstrlen(txtW) - 1);
330                 lstrcat(buf, txtW);
331                 file_name = buf;
332                 file_exists = FileExists(buf);
333             }
334         }
335
336         if (file_exists)
337         {
338             DoOpenFile(file_name);
339             InvalidateRect(Globals.hMainWnd, NULL, FALSE);
340             if (opt_print)
341                 DIALOG_FilePrint();
342         }
343         else
344         {
345             switch (AlertFileDoesNotExist(file_name)) {
346             case IDYES:
347                 DoOpenFile(file_name);
348                 break;
349
350             case IDNO:
351                 break;
352             }
353         }
354      }
355 }
356
357 /***********************************************************************
358  *
359  *           WinMain
360  */
361 int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
362 {
363     MSG        msg;
364     HACCEL      hAccel;
365     WNDCLASSEX class;
366     static const WCHAR className[] = {'N','P','C','l','a','s','s',0};
367     static const WCHAR winName[]   = {'N','o','t','e','p','a','d',0};
368
369     aFINDMSGSTRING = RegisterWindowMessage(FINDMSGSTRING);
370
371     ZeroMemory(&Globals, sizeof(Globals));
372     Globals.hInstance       = hInstance;
373
374     ZeroMemory(&class, sizeof(class));
375     class.cbSize        = sizeof(class);
376     class.lpfnWndProc   = NOTEPAD_WndProc;
377     class.hInstance     = Globals.hInstance;
378     class.hIcon         = LoadIcon(0, IDI_APPLICATION);
379     class.hCursor       = LoadCursor(0, IDC_ARROW);
380     class.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
381     class.lpszMenuName  = MAKEINTRESOURCE(MAIN_MENU);
382     class.lpszClassName = className;
383
384     if (!RegisterClassEx(&class)) return FALSE;
385
386     /* Setup windows */
387
388     Globals.hMainWnd =
389         CreateWindow(className, winName, WS_OVERLAPPEDWINDOW,
390                      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
391                      NULL, NULL, Globals.hInstance, NULL);
392     if (!Globals.hMainWnd)
393     {
394         ShowLastError();
395         ExitProcess(1);
396     }
397
398     NOTEPAD_InitData();
399     DIALOG_FileNew();
400
401     ShowWindow(Globals.hMainWnd, show);
402     UpdateWindow(Globals.hMainWnd);
403     DragAcceptFiles(Globals.hMainWnd, TRUE);
404
405     HandleCommandLine(GetCommandLine());
406
407     hAccel = LoadAccelerators( hInstance, MAKEINTRESOURCE(ID_ACCEL) );
408
409     while (GetMessage(&msg, 0, 0, 0))
410     {
411         if (!TranslateAccelerator(Globals.hMainWnd, hAccel, &msg) && !IsDialogMessage(Globals.hFindReplaceDlg, &msg))
412         {
413             TranslateMessage(&msg);
414             DispatchMessage(&msg);
415         }
416     }
417     return msg.wParam;
418 }