Include sys/filio.h to get the FIONREAD definition on Solaris.
[wine.git] / programs / progman / main.c
blob83a346c5f592a2b57caffb18fe327c69fd7ae5bf
1 /*
2 * Program Manager
4 * Copyright 1996 Ulrich Schmid
5 * Copyright 2002 Sylvain Petreolle
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.
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.
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
22 #include <stdio.h>
23 #include <string.h>
25 #define OEMRESOURCE
27 #include "windows.h"
28 #include "progman.h"
30 GLOBALS Globals;
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);
41 #define BUFFER_SIZE 1000
43 /***********************************************************************
45 * WinMain
48 int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
50 MSG msg;
52 Globals.lpszIniFile = "progman.ini";
53 Globals.lpszIcoFile = "progman.ico";
55 Globals.hInstance = hInstance;
56 Globals.hGroups = 0;
57 Globals.hActiveGroup = 0;
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);
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));
75 /* Register classes */
76 if (!prev)
78 if (!MAIN_RegisterMainWinClass()) return(FALSE);
79 if (!GROUP_RegisterGroupWinClass()) return(FALSE);
80 if (!PROGRAM_RegisterProgramWinClass()) return(FALSE);
83 /* Create main window */
84 MAIN_CreateMainWindow();
85 Globals.hAccel = LoadAccelerators(Globals.hInstance, STRING_ACCEL);
87 /* Setup menu, stringtable and resourcenames */
88 STRING_LoadMenus();
90 MAIN_CreateMDIWindow();
92 /* Initialize groups */
93 MAIN_CreateGroups();
95 /* Start initial applications */
96 MAIN_AutoStart();
98 /* Message loop */
99 while (GetMessage (&msg, 0, 0, 0))
100 if (!TranslateAccelerator(Globals.hMainWnd, Globals.hAccel, &msg))
102 TranslateMessage (&msg);
103 DispatchMessage (&msg);
105 return 0;
108 /***********************************************************************
110 * MAIN_CreateGroups
113 static VOID MAIN_CreateGroups(void)
115 CHAR buffer[BUFFER_SIZE];
116 CHAR szPath[MAX_PATHNAME_LEN];
117 CHAR key[20], *ptr;
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))
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;
130 sprintf(key, "Group%d", num);
131 GetPrivateProfileString("Groups", key, "", szPath,
132 sizeof(szPath), Globals.lpszIniFile);
133 if (!szPath[0]) continue;
135 GRPFILE_ReadGroupFile(szPath);
137 ptr += skip;
139 /* FIXME initialize other groups, not enumerated by `Order' */
142 /***********************************************************************
144 * MAIN_AutoStart
147 VOID MAIN_AutoStart(void)
149 CHAR buffer[BUFFER_SIZE];
150 HLOCAL hGroup, hProgram;
152 GetPrivateProfileString("Settings", "AutoStart", "Autostart", buffer,
153 sizeof(buffer), Globals.lpszIniFile);
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);
162 /***********************************************************************
164 * MAIN_MainWndProc
167 static LRESULT CALLBACK MAIN_MainWndProc(HWND hWnd, UINT msg,
168 WPARAM wParam, LPARAM lParam)
170 #if 0
171 printf("M %4.4x %4.4x\n", msg, wParam);
172 #endif
173 switch (msg)
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;
184 case WM_COMMAND:
185 if (wParam < PM_FIRST_CHILD){
186 MAIN_MenuCommand(hWnd, wParam, lParam);
188 break;
190 case WM_DESTROY:
191 PostQuitMessage (0);
192 break;
194 return(DefFrameProc(hWnd, Globals.hMDIWnd, msg, wParam, lParam));
197 /***********************************************************************
199 * MAIN_MenuCommand
202 static VOID MAIN_MenuCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
204 HLOCAL hActiveGroup = GROUP_ActiveGroup();
205 HLOCAL hActiveProgram = PROGRAM_ActiveProgram(hActiveGroup);
206 HWND hActiveGroupWnd = GROUP_GroupWnd(hActiveGroup);
208 switch(wParam)
210 /* Menu File */
211 case PM_NEW:
212 switch (DIALOG_New((hActiveGroupWnd && !IsIconic(hActiveGroupWnd)) ?
213 PM_NEW_PROGRAM : PM_NEW_GROUP))
215 case PM_NEW_PROGRAM:
216 if (hActiveGroup) PROGRAM_NewProgram(hActiveGroup);
217 break;
219 case PM_NEW_GROUP:
220 GROUP_NewGroup();
221 break;
223 break;
225 case PM_OPEN:
226 if (hActiveProgram)
227 PROGRAM_ExecuteProgram(hActiveProgram);
228 else if (hActiveGroupWnd)
229 OpenIcon(hActiveGroupWnd);
230 break;
232 case PM_MOVE:
233 case PM_COPY:
234 if (hActiveProgram)
235 PROGRAM_CopyMoveProgram(hActiveProgram, wParam == PM_MOVE);
236 break;
238 case PM_DELETE:
239 if (hActiveProgram)
241 if (DIALOG_Delete(IDS_DELETE_PROGRAM_s, PROGRAM_ProgramName(hActiveProgram)))
242 PROGRAM_DeleteProgram(hActiveProgram, TRUE);
244 else if (hActiveGroup)
246 if (DIALOG_Delete(IDS_DELETE_GROUP_s, GROUP_GroupName(hActiveGroup)))
247 GROUP_DeleteGroup(hActiveGroup);
249 break;
251 case PM_ATTRIBUTES:
252 if (hActiveProgram)
253 PROGRAM_ModifyProgram(hActiveProgram);
254 else if (hActiveGroup)
255 GROUP_ModifyGroup(hActiveGroup);
256 break;
258 case PM_EXECUTE:
259 DIALOG_Execute();
260 break;
262 case PM_EXIT:
263 PostQuitMessage(0);
264 break;
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;
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;
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;
300 /* Menu Windows */
301 case PM_ARRANGE:
303 if (hActiveGroupWnd && !IsIconic(hActiveGroupWnd))
304 ArrangeIconicWindows(hActiveGroupWnd);
305 else
306 SendMessage(Globals.hMDIWnd, WM_MDIICONARRANGE, 0, 0);
307 break;
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;
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;
320 case PM_TUTORIAL:
321 WinExec("wintutor.exe", SW_SHOWNORMAL);
322 break;
324 case PM_LICENSE:
325 WineLicense(Globals.hMainWnd);
326 break;
328 case PM_NO_WARRANTY:
329 WineWarranty(Globals.hMainWnd);
330 break;
332 case PM_ABOUT_WINE:
333 ShellAbout(hWnd, "WINE", "Program Manager", 0);
334 break;
336 default:
337 MAIN_MessageBoxIDS(IDS_NOT_IMPLEMENTED, IDS_ERROR, MB_OK);
338 break;
342 /***********************************************************************
344 * MAIN_RegisterMainWinClass
347 static ATOM MAIN_RegisterMainWinClass(void)
349 WNDCLASS class;
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;
362 return RegisterClass(&class);
365 /***********************************************************************
367 * MAIN_CreateMainWindow
370 static VOID MAIN_CreateMainWindow(void)
372 INT left , top, right, bottom, width, height, show;
373 CHAR buffer[100];
375 Globals.hMDIWnd = 0;
376 Globals.hMainMenu = 0;
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))
383 width = right - left;
384 height = bottom - top;
386 else
388 left = top = width = height = CW_USEDEFAULT;
389 show = SW_SHOWNORMAL;
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);
398 ShowWindow (Globals.hMainWnd, show);
399 UpdateWindow (Globals.hMainWnd);
402 /***********************************************************************
404 * MAIN_CreateMDIWindow
407 static VOID MAIN_CreateMDIWindow(void)
409 CLIENTCREATESTRUCT ccs;
410 RECT rect;
412 /* Get the geometry of the MDI window */
413 GetClientRect(Globals.hMainWnd, &rect);
415 ccs.hWindowMenu = Globals.hWindowsMenu;
416 ccs.idFirstChild = PM_FIRST_CHILD;
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);
426 ShowWindow (Globals.hMDIWnd, SW_SHOW);
427 UpdateWindow (Globals.hMDIWnd);
430 /**********************************************************************/
431 /***********************************************************************
433 * MAIN_MessageBoxIDS
435 INT MAIN_MessageBoxIDS(UINT ids_text, UINT ids_title, WORD type)
437 CHAR text[MAX_STRING_LEN];
438 CHAR title[MAX_STRING_LEN];
440 LoadString(Globals.hInstance, ids_text, text, sizeof(text));
441 LoadString(Globals.hInstance, ids_title, title, sizeof(title));
443 return(MessageBox(Globals.hMainWnd, text, title, type));
446 /***********************************************************************
448 * MAIN_MessageBoxIDS_s
450 INT MAIN_MessageBoxIDS_s(UINT ids_text, LPCSTR str, UINT ids_title, WORD type)
452 CHAR text[MAX_STRING_LEN];
453 CHAR title[MAX_STRING_LEN];
454 CHAR newtext[MAX_STRING_LEN + MAX_PATHNAME_LEN];
456 LoadString(Globals.hInstance, ids_text, text, sizeof(text));
457 LoadString(Globals.hInstance, ids_title, title, sizeof(title));
458 wsprintf(newtext, text, str);
460 return(MessageBox(Globals.hMainWnd, newtext, title, type));
463 /***********************************************************************
465 * MAIN_ReplaceString
468 VOID MAIN_ReplaceString(HLOCAL *handle, LPSTR replace)
470 HLOCAL newhandle = LocalAlloc(LMEM_FIXED, strlen(replace) + 1);
471 if (newhandle)
473 LPSTR newstring = LocalLock(newhandle);
474 lstrcpy(newstring, replace);
475 LocalFree(*handle);
476 *handle = newhandle;
478 else MAIN_MessageBoxIDS(IDS_OUT_OF_MEMORY, IDS_ERROR, MB_OK);
481 VOID WineLicense(HWND Wnd)
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);
489 VOID WineWarranty(HWND Wnd)
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);
497 /* Local Variables: */
498 /* c-file-style: "GNU" */
499 /* End: */