comctl32: A couple fixes for tab icon offsets.
[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(void)
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     CheckMenuItem(GetMenu(Globals.hMainWnd), CMD_WRAP,
148             MF_BYCOMMAND | (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED));
149 }
150
151 /***********************************************************************
152  * Enable/disable items on the menu based on control state
153  */
154 static VOID NOTEPAD_InitMenuPopup(HMENU menu, int index)
155 {
156     int enable;
157
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);
167     
168     EnableMenuItem(menu, CMD_SELECT_ALL,
169         GetWindowTextLength(Globals.hEdit) ? MF_ENABLED : MF_GRAYED);
170 }
171
172 /***********************************************************************
173  *
174  *           NOTEPAD_WndProc
175  */
176 static LRESULT WINAPI NOTEPAD_WndProc(HWND hWnd, UINT msg, WPARAM wParam,
177                                LPARAM lParam)
178 {
179     switch (msg) {
180
181     case WM_CREATE:
182     {
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);
188
189         if (!Globals.bWrapLongLines) dwStyle |= WS_HSCROLL | ES_AUTOHSCROLL;
190
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;
197     }
198
199     case WM_COMMAND:
200         NOTEPAD_MenuCommand(LOWORD(wParam));
201         break;
202
203     case WM_DESTROYCLIPBOARD:
204         /*MessageBox(Globals.hMainWnd, "Empty clipboard", "Debug", MB_ICONEXCLAMATION);*/
205         break;
206
207     case WM_CLOSE:
208         if (DoCloseFile()) {
209             DestroyWindow(hWnd);
210         }
211         break;
212
213     case WM_QUERYENDSESSION:
214         if (DoCloseFile()) {
215             return 1;
216         }
217         break;
218
219     case WM_DESTROY:
220         PostQuitMessage(0);
221         break;
222
223     case WM_SIZE:
224         SetWindowPos(Globals.hEdit, NULL, 0, 0, LOWORD(lParam), HIWORD(lParam),
225                      SWP_NOOWNERZORDER | SWP_NOZORDER);
226         break;
227
228     case WM_SETFOCUS:
229         SetFocus(Globals.hEdit);
230         break;
231
232     case WM_DROPFILES:
233     {
234         WCHAR szFileName[MAX_PATH];
235         HANDLE hDrop = (HANDLE) wParam;
236
237         DragQueryFile(hDrop, 0, szFileName, SIZEOF(szFileName));
238         DragFinish(hDrop);
239         DoOpenFile(szFileName);
240         break;
241     }
242     
243     case WM_INITMENUPOPUP:
244         NOTEPAD_InitMenuPopup((HMENU)wParam, lParam);
245         break;
246
247     default:
248         return DefWindowProc(hWnd, msg, wParam, lParam);
249     }
250     return 0;
251 }
252
253 static int AlertFileDoesNotExist(LPCWSTR szFileName)
254 {
255    int nResult;
256    WCHAR szMessage[MAX_STRING_LEN];
257    WCHAR szResource[MAX_STRING_LEN];
258
259    LoadString(Globals.hInstance, STRING_DOESNOTEXIST, szResource, SIZEOF(szResource));
260    wsprintf(szMessage, szResource, szFileName);
261
262    LoadString(Globals.hInstance, STRING_ERROR, szResource, SIZEOF(szResource));
263
264    nResult = MessageBox(Globals.hMainWnd, szMessage, szResource,
265                         MB_ICONEXCLAMATION | MB_YESNO);
266
267    return(nResult);
268 }
269
270 static void HandleCommandLine(LPWSTR cmdline)
271 {
272     WCHAR delimiter;
273     int opt_print=0;
274     
275     /* skip white space */
276     while (*cmdline == ' ') cmdline++;
277
278     /* skip executable name */
279     delimiter = (*cmdline == '"' ? '"' : ' ');
280
281     if (*cmdline == delimiter) cmdline++;
282
283     while (*cmdline && *cmdline != delimiter) cmdline++;
284
285     if (*cmdline == delimiter) cmdline++;
286
287     while (*cmdline == ' ' || *cmdline == '-' || *cmdline == '/')
288     {
289         WCHAR option;
290
291         if (*cmdline++ == ' ') continue;
292
293         option = *cmdline;
294         if (option) cmdline++;
295         while (*cmdline == ' ') cmdline++;
296
297         switch(option)
298         {
299             case 'p':
300             case 'P':
301                 opt_print=1;
302                 break;
303         }
304     }
305
306     if (*cmdline)
307     {
308         /* file name is passed in the command line */
309         LPCWSTR file_name;
310         BOOL file_exists;
311         WCHAR buf[MAX_PATH];
312
313         if (cmdline[0] == '"')
314         {
315             cmdline++;
316             cmdline[lstrlen(cmdline) - 1] = 0;
317         }
318
319         if (FileExists(cmdline))
320         {
321             file_exists = TRUE;
322             file_name = cmdline;
323         }
324         else
325         {
326             static const WCHAR txtW[] = { '.','t','x','t',0 };
327
328             /* try to find file with ".txt" extension */
329             if (!lstrcmp(txtW, cmdline + lstrlen(cmdline) - lstrlen(txtW)))
330             {
331                 file_exists = FALSE;
332                 file_name = cmdline;
333             }
334             else
335             {
336                 lstrcpyn(buf, cmdline, MAX_PATH - lstrlen(txtW) - 1);
337                 lstrcat(buf, txtW);
338                 file_name = buf;
339                 file_exists = FileExists(buf);
340             }
341         }
342
343         if (file_exists)
344         {
345             DoOpenFile(file_name);
346             InvalidateRect(Globals.hMainWnd, NULL, FALSE);
347             if (opt_print)
348                 DIALOG_FilePrint();
349         }
350         else
351         {
352             switch (AlertFileDoesNotExist(file_name)) {
353             case IDYES:
354                 DoOpenFile(file_name);
355                 break;
356
357             case IDNO:
358                 break;
359             }
360         }
361      }
362 }
363
364 /***********************************************************************
365  *
366  *           WinMain
367  */
368 int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
369 {
370     MSG        msg;
371     HACCEL      hAccel;
372     WNDCLASSEX class;
373     static const WCHAR className[] = {'N','P','C','l','a','s','s',0};
374     static const WCHAR winName[]   = {'N','o','t','e','p','a','d',0};
375
376     aFINDMSGSTRING = RegisterWindowMessage(FINDMSGSTRING);
377
378     ZeroMemory(&Globals, sizeof(Globals));
379     Globals.hInstance       = hInstance;
380     Globals.bWrapLongLines  = TRUE;
381
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;
391
392     if (!RegisterClassEx(&class)) return FALSE;
393
394     /* Setup windows */
395
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)
401     {
402         ShowLastError();
403         ExitProcess(1);
404     }
405
406     NOTEPAD_InitData();
407     DIALOG_FileNew();
408
409     ShowWindow(Globals.hMainWnd, show);
410     UpdateWindow(Globals.hMainWnd);
411     DragAcceptFiles(Globals.hMainWnd, TRUE);
412
413     HandleCommandLine(GetCommandLine());
414
415     hAccel = LoadAccelerators( hInstance, MAKEINTRESOURCE(ID_ACCEL) );
416
417     while (GetMessage(&msg, 0, 0, 0))
418     {
419         if (!TranslateAccelerator(Globals.hMainWnd, hAccel, &msg) && !IsDialogMessage(Globals.hFindReplaceDlg, &msg))
420         {
421             TranslateMessage(&msg);
422             DispatchMessage(&msg);
423         }
424     }
425     return msg.wParam;
426 }