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