winemenubuilder: Escape freedesktop exec keys properly.
[wine] / programs / progman / main.c
1 /*
2  * Program Manager
3  *
4  * Copyright 1996 Ulrich Schmid
5  * Copyright 2002 Sylvain Petreolle
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <stdio.h>
23 #include <string.h>
24
25 #define OEMRESOURCE
26
27 #include "windows.h"
28 #include "progman.h"
29
30 GLOBALS Globals;
31
32 static VOID MAIN_CreateGroups(void);
33 static VOID MAIN_MenuCommand(HWND hWnd, WPARAM wParam, LPARAM lParam);
34 static ATOM MAIN_RegisterMainWinClass(void);
35 static VOID MAIN_CreateMainWindow(void);
36 static VOID MAIN_CreateMDIWindow(void);
37 static VOID MAIN_AutoStart(void);
38 static VOID WineLicense(HWND Wnd);
39 static VOID WineWarranty(HWND Wnd);
40
41 #define BUFFER_SIZE 1000
42
43 /***********************************************************************
44  *
45  *           WinMain
46  */
47
48 int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
49 {
50   MSG      msg;
51
52   Globals.lpszIniFile         = "progman.ini";
53   Globals.lpszIcoFile         = "progman.ico";
54
55   Globals.hInstance           = hInstance;
56   Globals.hGroups             = 0;
57   Globals.hActiveGroup        = 0;
58
59   /* Read Options from `progman.ini' */
60   Globals.bAutoArrange =
61     GetPrivateProfileInt("Settings", "AutoArrange", 0, Globals.lpszIniFile);
62   Globals.bMinOnRun =
63     GetPrivateProfileInt("Settings", "MinOnRun", 0, Globals.lpszIniFile);
64   Globals.bSaveSettings =
65     GetPrivateProfileInt("Settings", "SaveSettings", 0, Globals.lpszIniFile);
66
67   /* Load default icons */
68   Globals.hMainIcon    = ExtractIcon(Globals.hInstance, Globals.lpszIcoFile, 0);
69   Globals.hGroupIcon   = ExtractIcon(Globals.hInstance, Globals.lpszIcoFile, 0);
70   Globals.hDefaultIcon = ExtractIcon(Globals.hInstance, Globals.lpszIcoFile, 0);
71   if (!Globals.hMainIcon)    Globals.hMainIcon = LoadIcon(0, MAKEINTRESOURCE(DEFAULTICON));
72   if (!Globals.hGroupIcon)   Globals.hGroupIcon = LoadIcon(0, MAKEINTRESOURCE(DEFAULTICON));
73   if (!Globals.hDefaultIcon) Globals.hDefaultIcon = LoadIcon(0, MAKEINTRESOURCE(DEFAULTICON));
74
75   /* Register classes */
76   if (!prev)
77     {
78       if (!MAIN_RegisterMainWinClass()) return(FALSE);
79       if (!GROUP_RegisterGroupWinClass()) return(FALSE);
80       if (!PROGRAM_RegisterProgramWinClass()) return(FALSE);
81     }
82
83   /* Create main window */
84   MAIN_CreateMainWindow();
85   Globals.hAccel = LoadAccelerators(Globals.hInstance, STRING_ACCEL);
86
87   /* Setup menu, stringtable and resourcenames */
88   STRING_LoadMenus();
89
90   MAIN_CreateMDIWindow();
91
92   /* Initialize groups */
93   MAIN_CreateGroups();
94
95   /* Start initial applications */
96   MAIN_AutoStart();
97
98   /* Message loop */
99   while (GetMessage (&msg, 0, 0, 0))
100     if (!TranslateAccelerator(Globals.hMainWnd, Globals.hAccel, &msg))
101       {
102         TranslateMessage (&msg);
103         DispatchMessage (&msg);
104       }
105   return 0;
106 }
107
108 /***********************************************************************
109  *
110  *           MAIN_CreateGroups
111  */
112
113 static VOID MAIN_CreateGroups(void)
114 {
115   CHAR buffer[BUFFER_SIZE];
116   CHAR szPath[MAX_PATHNAME_LEN];
117   CHAR key[20], *ptr;
118
119   /* Initialize groups according the `Order' entry of `progman.ini' */
120   GetPrivateProfileString("Settings", "Order", "", buffer, sizeof(buffer), Globals.lpszIniFile);
121   ptr = buffer;
122   while (ptr < buffer + sizeof(buffer))
123     {
124       int num, skip, ret;
125       ret = sscanf(ptr, "%d%n", &num, &skip);
126       if (ret == 0)
127         MAIN_MessageBoxIDS_s(IDS_FILE_READ_ERROR_s, Globals.lpszIniFile, IDS_ERROR, MB_OK);
128       if (ret != 1) break;
129
130       sprintf(key, "Group%d", num);
131       GetPrivateProfileString("Groups", key, "", szPath,
132                               sizeof(szPath), Globals.lpszIniFile);
133       if (!szPath[0]) continue;
134
135       GRPFILE_ReadGroupFile(szPath);
136
137       ptr += skip;
138     }
139   /* FIXME initialize other groups, not enumerated by `Order' */
140 }
141
142 /***********************************************************************
143  *
144  *           MAIN_AutoStart
145  */
146
147 VOID MAIN_AutoStart(void)
148 {
149   CHAR buffer[BUFFER_SIZE];
150   HLOCAL hGroup, hProgram;
151
152   GetPrivateProfileString("Settings", "AutoStart", "Autostart", buffer,
153                           sizeof(buffer), Globals.lpszIniFile);
154
155   for (hGroup = GROUP_FirstGroup(); hGroup; hGroup = GROUP_NextGroup(hGroup))
156     if (!lstrcmp(buffer, GROUP_GroupName(hGroup)))
157       for (hProgram = PROGRAM_FirstProgram(hGroup); hProgram;
158            hProgram = PROGRAM_NextProgram(hProgram))
159         PROGRAM_ExecuteProgram(hProgram);
160 }
161
162 /***********************************************************************
163  *
164  *           MAIN_MainWndProc
165  */
166
167 static LRESULT CALLBACK MAIN_MainWndProc(HWND hWnd, UINT msg,
168                                  WPARAM wParam, LPARAM lParam)
169 {
170 #if 0
171   printf("M %4.4x %4.4x\n", msg, wParam);
172 #endif
173   switch (msg)
174     {
175     case WM_INITMENU:
176       CheckMenuItem(Globals.hOptionMenu, PM_AUTO_ARRANGE,
177                     MF_BYCOMMAND | (Globals.bAutoArrange ? MF_CHECKED : MF_UNCHECKED));
178       CheckMenuItem(Globals.hOptionMenu, PM_MIN_ON_RUN,
179                     MF_BYCOMMAND | (Globals.bMinOnRun ? MF_CHECKED : MF_UNCHECKED));
180       CheckMenuItem(Globals.hOptionMenu, PM_SAVE_SETTINGS,
181                     MF_BYCOMMAND | (Globals.bSaveSettings ? MF_CHECKED : MF_UNCHECKED));
182       break;
183
184     case WM_COMMAND:
185       if (wParam < PM_FIRST_CHILD){
186         MAIN_MenuCommand(hWnd, wParam, lParam);
187       }
188       break;
189
190     case WM_DESTROY:
191       PostQuitMessage (0);
192       break;
193     }
194   return(DefFrameProc(hWnd, Globals.hMDIWnd, msg, wParam, lParam));
195 }
196
197 /***********************************************************************
198  *
199  *           MAIN_MenuCommand
200  */
201
202 static VOID MAIN_MenuCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
203 {
204   HLOCAL hActiveGroup    = GROUP_ActiveGroup();
205   HLOCAL hActiveProgram  = PROGRAM_ActiveProgram(hActiveGroup);
206   HWND   hActiveGroupWnd = GROUP_GroupWnd(hActiveGroup);
207
208   switch(wParam)
209     {
210       /* Menu File */
211     case PM_NEW:
212       switch (DIALOG_New((hActiveGroupWnd && !IsIconic(hActiveGroupWnd)) ?
213                          PM_NEW_PROGRAM : PM_NEW_GROUP))
214       {
215       case PM_NEW_PROGRAM:
216         if (hActiveGroup) PROGRAM_NewProgram(hActiveGroup);
217         break;
218
219       case PM_NEW_GROUP:
220         GROUP_NewGroup();
221         break;
222       }
223       break;
224
225     case PM_OPEN:
226       if (hActiveProgram)
227         PROGRAM_ExecuteProgram(hActiveProgram);
228       else if (hActiveGroupWnd)
229         OpenIcon(hActiveGroupWnd);
230       break;
231
232     case PM_MOVE:
233     case PM_COPY:
234       if (hActiveProgram)
235         PROGRAM_CopyMoveProgram(hActiveProgram, wParam == PM_MOVE);
236       break;
237
238     case PM_DELETE:
239       if (hActiveProgram)
240         {
241         if (DIALOG_Delete(IDS_DELETE_PROGRAM_s, PROGRAM_ProgramName(hActiveProgram)))
242           PROGRAM_DeleteProgram(hActiveProgram, TRUE);
243         }
244       else if (hActiveGroup)
245         {
246         if (DIALOG_Delete(IDS_DELETE_GROUP_s, GROUP_GroupName(hActiveGroup)))
247           GROUP_DeleteGroup(hActiveGroup);
248         }
249       break;
250
251     case PM_ATTRIBUTES:
252       if (hActiveProgram)
253         PROGRAM_ModifyProgram(hActiveProgram);
254       else if (hActiveGroup)
255         GROUP_ModifyGroup(hActiveGroup);
256       break;
257
258     case PM_EXECUTE:
259       DIALOG_Execute();
260       break;
261
262     case PM_EXIT:
263       PostQuitMessage(0);
264       break;
265
266       /* Menu Options */
267     case PM_AUTO_ARRANGE:
268       Globals.bAutoArrange = !Globals.bAutoArrange;
269       CheckMenuItem(Globals.hOptionMenu, PM_AUTO_ARRANGE,
270                     MF_BYCOMMAND | (Globals.bAutoArrange ?
271                                     MF_CHECKED : MF_UNCHECKED));
272       WritePrivateProfileString("Settings", "AutoArrange",
273                                 Globals.bAutoArrange ? "1" : "0",
274                                 Globals.lpszIniFile);
275       WritePrivateProfileString(NULL,NULL,NULL,Globals.lpszIniFile); /* flush it */
276       break;
277
278     case PM_MIN_ON_RUN:
279       Globals.bMinOnRun = !Globals.bMinOnRun;
280       CheckMenuItem(Globals.hOptionMenu, PM_MIN_ON_RUN,
281                     MF_BYCOMMAND | (Globals.bMinOnRun ?
282                                     MF_CHECKED : MF_UNCHECKED));
283       WritePrivateProfileString("Settings", "MinOnRun",
284                                 Globals.bMinOnRun ? "1" : "0",
285                                 Globals.lpszIniFile);
286       WritePrivateProfileString(NULL,NULL,NULL,Globals.lpszIniFile); /* flush it */
287       break;
288
289     case PM_SAVE_SETTINGS:
290       Globals.bSaveSettings = !Globals.bSaveSettings;
291       CheckMenuItem(Globals.hOptionMenu, PM_SAVE_SETTINGS,
292                     MF_BYCOMMAND | (Globals.bSaveSettings ?
293                                     MF_CHECKED : MF_UNCHECKED));
294       WritePrivateProfileString("Settings", "SaveSettings",
295                                 Globals.bSaveSettings ? "1" : "0",
296                                 Globals.lpszIniFile);
297       WritePrivateProfileString(NULL,NULL,NULL,Globals.lpszIniFile); /* flush it */
298       break;
299
300       /* Menu Windows */
301     case PM_ARRANGE:
302
303       if (hActiveGroupWnd && !IsIconic(hActiveGroupWnd))
304         ArrangeIconicWindows(hActiveGroupWnd);
305       else
306         SendMessage(Globals.hMDIWnd, WM_MDIICONARRANGE, 0, 0);
307       break;
308
309       /* Menu Help */
310     case PM_CONTENTS:
311       if (!WinHelp(Globals.hMainWnd, "progman.hlp", HELP_CONTENTS, 0))
312         MAIN_MessageBoxIDS(IDS_WINHELP_ERROR, IDS_ERROR, MB_OK);
313       break;
314
315     case PM_HELPONHELP:
316       if (!WinHelp(Globals.hMainWnd, "progman.hlp", HELP_HELPONHELP, 0))
317         MAIN_MessageBoxIDS(IDS_WINHELP_ERROR, IDS_ERROR, MB_OK);
318       break;
319
320     case PM_TUTORIAL:
321       WinExec("wintutor.exe", SW_SHOWNORMAL);
322       break;
323
324     case PM_LICENSE:
325       WineLicense(Globals.hMainWnd);
326       break;
327
328     case PM_NO_WARRANTY:
329       WineWarranty(Globals.hMainWnd);
330       break;
331
332     case PM_ABOUT_WINE:
333       ShellAbout(hWnd, "WINE", "Program Manager", 0);
334       break;
335
336     default:
337         MAIN_MessageBoxIDS(IDS_NOT_IMPLEMENTED, IDS_ERROR, MB_OK);
338       break;
339     }
340 }
341
342 /***********************************************************************
343  *
344  *           MAIN_RegisterMainWinClass
345  */
346
347 static ATOM MAIN_RegisterMainWinClass(void)
348 {
349   WNDCLASS class;
350
351   class.style         = CS_HREDRAW | CS_VREDRAW;
352   class.lpfnWndProc   = MAIN_MainWndProc;
353   class.cbClsExtra    = 0;
354   class.cbWndExtra    = 0;
355   class.hInstance     = Globals.hInstance;
356   class.hIcon         = Globals.hMainIcon;
357   class.hCursor       = LoadCursor (0, IDC_ARROW);
358   class.hbrBackground = GetStockObject (NULL_BRUSH);
359   class.lpszMenuName  = 0;
360   class.lpszClassName = STRING_MAIN_WIN_CLASS_NAME;
361
362   return RegisterClass(&class);
363 }
364
365 /***********************************************************************
366  *
367  *           MAIN_CreateMainWindow
368  */
369
370 static VOID MAIN_CreateMainWindow(void)
371 {
372   INT  left , top, right, bottom, width, height, show;
373   CHAR buffer[100];
374
375   Globals.hMDIWnd   = 0;
376   Globals.hMainMenu = 0;
377
378   /* Get the geometry of the main window */
379   GetPrivateProfileString("Settings", "Window", "",
380                           buffer, sizeof(buffer), Globals.lpszIniFile);
381   if (5 == sscanf(buffer, "%d %d %d %d %d", &left, &top, &right, &bottom, &show))
382   {
383     width  = right - left;
384     height = bottom - top;
385   }
386   else
387   {
388     left = top = width = height = CW_USEDEFAULT;
389     show = SW_SHOWNORMAL;
390   }
391
392   /* Create main Window */
393   Globals.hMainWnd =
394     CreateWindow (STRING_MAIN_WIN_CLASS_NAME, "",
395                   WS_OVERLAPPEDWINDOW, left, top, width, height,
396                   0, 0, Globals.hInstance, 0);
397
398   ShowWindow (Globals.hMainWnd, show);
399   UpdateWindow (Globals.hMainWnd);
400 }
401
402 /***********************************************************************
403  *
404  *           MAIN_CreateMDIWindow
405  */
406
407 static VOID MAIN_CreateMDIWindow(void)
408 {
409   CLIENTCREATESTRUCT ccs;
410   RECT rect;
411
412   /* Get the geometry of the MDI window */
413   GetClientRect(Globals.hMainWnd, &rect);
414
415   ccs.hWindowMenu  = Globals.hWindowsMenu;
416   ccs.idFirstChild = PM_FIRST_CHILD;
417
418   /* Create MDI Window */
419   Globals.hMDIWnd =
420     CreateWindow (STRING_MDI_WIN_CLASS_NAME, "",
421                   WS_CHILD, rect.left, rect.top,
422                   rect.right - rect.left, rect.bottom - rect.top,
423                   Globals.hMainWnd, 0,
424                   Globals.hInstance, &ccs);
425
426   ShowWindow (Globals.hMDIWnd, SW_SHOW);
427   UpdateWindow (Globals.hMDIWnd);
428 }
429
430 /**********************************************************************/
431 /***********************************************************************
432  *
433  *           MAIN_MessageBoxIDS
434  */
435 INT MAIN_MessageBoxIDS(UINT ids_text, UINT ids_title, WORD type)
436 {
437   CHAR text[MAX_STRING_LEN];
438   CHAR title[MAX_STRING_LEN];
439
440   LoadString(Globals.hInstance, ids_text, text, sizeof(text));
441   LoadString(Globals.hInstance, ids_title, title, sizeof(title));
442
443   return(MessageBox(Globals.hMainWnd, text, title, type));
444 }
445
446 /***********************************************************************
447  *
448  *           MAIN_MessageBoxIDS_s
449  */
450 INT MAIN_MessageBoxIDS_s(UINT ids_text, LPCSTR str, UINT ids_title, WORD type)
451 {
452   CHAR text[MAX_STRING_LEN];
453   CHAR title[MAX_STRING_LEN];
454   CHAR newtext[MAX_STRING_LEN + MAX_PATHNAME_LEN];
455
456   LoadString(Globals.hInstance, ids_text, text, sizeof(text));
457   LoadString(Globals.hInstance, ids_title, title, sizeof(title));
458   wsprintf(newtext, text, str);
459
460   return(MessageBox(Globals.hMainWnd, newtext, title, type));
461 }
462
463 /***********************************************************************
464  *
465  *           MAIN_ReplaceString
466  */
467
468 VOID MAIN_ReplaceString(HLOCAL *handle, LPSTR replace)
469 {
470   HLOCAL newhandle = LocalAlloc(LMEM_FIXED, strlen(replace) + 1);
471   if (newhandle)
472     {
473       LPSTR  newstring = LocalLock(newhandle);
474       lstrcpy(newstring, replace);
475       LocalFree(*handle);
476       *handle = newhandle;
477     }
478   else MAIN_MessageBoxIDS(IDS_OUT_OF_MEMORY, IDS_ERROR, MB_OK);
479 }
480
481 VOID WineLicense(HWND Wnd)
482 {
483   char cap[20], text[1024];
484   LoadString(Globals.hInstance, IDS_LICENSE, text, sizeof text);
485   LoadString(Globals.hInstance, IDS_LICENSE_CAPTION, cap, sizeof cap);
486   MessageBox(Wnd, text, cap, MB_ICONINFORMATION | MB_OK);
487 }
488
489 VOID WineWarranty(HWND Wnd)
490 {
491   char cap[20], text[1024];
492   LoadString(Globals.hInstance, IDS_WARRANTY, text, sizeof text);
493   LoadString(Globals.hInstance, IDS_WARRANTY_CAPTION, cap, sizeof cap);
494   MessageBox(Wnd, text, cap, MB_ICONEXCLAMATION | MB_OK);
495 }
496
497 /* Local Variables:    */
498 /* c-file-style: "GNU" */
499 /* End:                */