programs: Use WIN32_LEAN_AND_MEAN in some more places.
[wine] / programs / progman / program.c
1 /*
2  * Program Manager
3  *
4  * Copyright 1996 Ulrich Schmid
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #define WIN32_LEAN_AND_MEAN
22
23 #include <string.h>
24 #include "windows.h"
25 #include "windowsx.h"
26 #include "progman.h"
27
28 /***********************************************************************
29  *
30  *           PROGRAM_ProgramWndProc
31  */
32
33 static LRESULT CALLBACK PROGRAM_ProgramWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
34 {
35   switch (msg)
36     {
37     case WM_NCLBUTTONDOWN:
38       {
39         HLOCAL  hProgram = (HLOCAL) GetWindowLong(hWnd, 0);
40         PROGRAM *program = LocalLock(hProgram);
41         PROGGROUP   *group   = LocalLock(program->hGroup);
42         group->hActiveProgram = hProgram;
43         EnableMenuItem(Globals.hFileMenu, PM_MOVE , MF_ENABLED);
44         EnableMenuItem(Globals.hFileMenu, PM_COPY , MF_ENABLED);
45         break;
46       }
47     case WM_NCLBUTTONDBLCLK:
48       {
49         PROGRAM_ExecuteProgram((HLOCAL) GetWindowLong(hWnd, 0));
50         return(0);
51       }
52
53     case WM_PAINTICON:
54     case WM_NCPAINT:
55       {
56         PROGRAM *program;
57         PAINTSTRUCT      ps;
58         HDC              hdc;
59         hdc     = BeginPaint(hWnd,&ps);
60         program = LocalLock((HLOCAL) GetWindowLong(hWnd, 0));
61         if (program->hIcon)
62           DrawIcon(hdc, 0, 0, program->hIcon);
63         EndPaint(hWnd,&ps);
64         break;
65       }
66     }
67   return(DefWindowProc(hWnd, msg, wParam, lParam));
68 }
69
70 /***********************************************************************
71  *
72  *           PROGRAM_RegisterProgramWinClass
73  */
74
75 ATOM PROGRAM_RegisterProgramWinClass()
76 {
77   WNDCLASS class;
78
79   class.style         = CS_HREDRAW | CS_VREDRAW;
80   class.lpfnWndProc   = PROGRAM_ProgramWndProc;
81   class.cbClsExtra    = 0;
82   class.cbWndExtra    = sizeof(LONG);
83   class.hInstance     = Globals.hInstance;
84   class.hIcon         = 0;
85   class.hCursor       = LoadCursor (0, IDC_ARROW);
86   class.hbrBackground = GetStockObject (WHITE_BRUSH);
87   class.lpszMenuName  = 0;
88   class.lpszClassName = STRING_PROGRAM_WIN_CLASS_NAME;
89
90   return RegisterClass(&class);
91 }
92
93 /***********************************************************************
94  *
95  *           PROGRAM_NewProgram
96  */
97
98 VOID PROGRAM_NewProgram(HLOCAL hGroup)
99 {
100   INT  nCmdShow = SW_SHOWNORMAL;
101   INT  nHotKey = 0;
102   INT  nIconIndex = 0;
103   CHAR szName[MAX_PATHNAME_LEN] = "";
104   CHAR szCmdLine[MAX_PATHNAME_LEN] = "";
105   CHAR szIconFile[MAX_PATHNAME_LEN] = "";
106   CHAR szWorkDir[MAX_PATHNAME_LEN] = "";
107   HICON hIcon = 0;
108
109   if (!DIALOG_ProgramAttributes(szName, szCmdLine, szWorkDir, szIconFile,
110                                 &hIcon, &nIconIndex, &nHotKey,
111                                 &nCmdShow, MAX_PATHNAME_LEN))
112     return;
113
114   if (!hIcon) hIcon = LoadIcon(0, IDI_WINLOGO);
115
116
117   if (!PROGRAM_AddProgram(hGroup, hIcon, szName, 0, 0, szCmdLine, szIconFile,
118                           nIconIndex, szWorkDir, nHotKey, nCmdShow))
119     return;
120
121   GRPFILE_WriteGroupFile(hGroup);
122 }
123
124 /***********************************************************************
125  *
126  *           PROGRAM_ModifyProgram
127  */
128
129 VOID PROGRAM_ModifyProgram(HLOCAL hProgram)
130 {
131   PROGRAM *program = LocalLock(hProgram);
132   CHAR szName[MAX_PATHNAME_LEN];
133   CHAR szCmdLine[MAX_PATHNAME_LEN];
134   CHAR szIconFile[MAX_PATHNAME_LEN];
135   CHAR szWorkDir[MAX_PATHNAME_LEN];
136
137   lstrcpyn(szName, LocalLock(program->hName), MAX_PATHNAME_LEN);
138   lstrcpyn(szCmdLine, LocalLock(program->hCmdLine), MAX_PATHNAME_LEN);
139   lstrcpyn(szIconFile, LocalLock(program->hIconFile), MAX_PATHNAME_LEN);
140   lstrcpyn(szWorkDir, LocalLock(program->hWorkDir), MAX_PATHNAME_LEN);
141
142   if (!DIALOG_ProgramAttributes(szName, szCmdLine, szWorkDir, szIconFile,
143                                 &program->hIcon, &program->nIconIndex,
144                                 &program->nHotKey, &program->nCmdShow,
145                                 MAX_PATHNAME_LEN))
146     return;
147
148   MAIN_ReplaceString(&program->hName, szName);
149   MAIN_ReplaceString(&program->hCmdLine, szCmdLine);
150   MAIN_ReplaceString(&program->hIconFile, szIconFile);
151   MAIN_ReplaceString(&program->hWorkDir, szWorkDir);
152
153   SetWindowText(program->hWnd, szName);
154   UpdateWindow(program->hWnd);
155
156   GRPFILE_WriteGroupFile(program->hGroup);
157
158   return;
159 }
160
161 /***********************************************************************
162  *
163  *           PROGRAM_AddProgram
164  */
165
166 HLOCAL PROGRAM_AddProgram(HLOCAL hGroup, HICON hIcon, LPCSTR lpszName,
167                           INT x, INT y, LPCSTR lpszCmdLine,
168                           LPCSTR lpszIconFile, INT nIconIndex,
169                           LPCSTR lpszWorkDir, INT nHotKey, INT nCmdShow)
170 {
171   PROGGROUP *group = LocalLock(hGroup);
172   PROGRAM *program;
173   HLOCAL hPrior, *p;
174   HLOCAL hProgram  = LocalAlloc(LMEM_FIXED, sizeof(PROGRAM));
175   HLOCAL hName     = LocalAlloc(LMEM_FIXED, 1 + lstrlen(lpszName));
176   HLOCAL hCmdLine  = LocalAlloc(LMEM_FIXED, 1 + lstrlen(lpszCmdLine));
177   HLOCAL hIconFile = LocalAlloc(LMEM_FIXED, 1 + lstrlen(lpszIconFile));
178   HLOCAL hWorkDir  = LocalAlloc(LMEM_FIXED, 1 + lstrlen(lpszWorkDir));
179   if (!hProgram || !hName || !hCmdLine || !hIconFile || !hWorkDir)
180     {
181       MAIN_MessageBoxIDS(IDS_OUT_OF_MEMORY, IDS_ERROR, MB_OK);
182       if (hProgram)  LocalFree(hProgram);
183       if (hName)     LocalFree(hName);
184       if (hCmdLine)  LocalFree(hCmdLine);
185       if (hIconFile) LocalFree(hIconFile);
186       if (hWorkDir)  LocalFree(hWorkDir);
187       return(0);
188     }
189   memcpy(LocalLock(hName),     lpszName,     1 + lstrlen(lpszName));
190   memcpy(LocalLock(hCmdLine),  lpszCmdLine,  1 + lstrlen(lpszCmdLine));
191   memcpy(LocalLock(hIconFile), lpszIconFile, 1 + lstrlen(lpszIconFile));
192   memcpy(LocalLock(hWorkDir),  lpszWorkDir,  1 + lstrlen(lpszWorkDir));
193
194   group->hActiveProgram  = hProgram;
195
196   hPrior = 0;
197   p = &group->hPrograms;
198   while (*p)
199     {
200       hPrior = *p;
201       p = &((PROGRAM*)LocalLock(hPrior))->hNext;
202     }
203   *p = hProgram;
204
205   program = LocalLock(hProgram);
206   program->hGroup     = hGroup;
207   program->hPrior     = hPrior;
208   program->hNext      = 0;
209   program->hName      = hName;
210   program->hCmdLine   = hCmdLine;
211   program->hIconFile  = hIconFile;
212   program->nIconIndex = nIconIndex;
213   program->hWorkDir   = hWorkDir;
214   program->hIcon      = hIcon;
215   program->nCmdShow   = nCmdShow;
216   program->nHotKey    = nHotKey;
217
218   program->hWnd =
219     CreateWindow (STRING_PROGRAM_WIN_CLASS_NAME, lpszName,
220                   WS_CHILD | WS_CAPTION,
221                   x, y, CW_USEDEFAULT, CW_USEDEFAULT,
222                   group->hWnd, 0, Globals.hInstance, 0);
223
224   SetWindowLong(program->hWnd, 0, (LONG) hProgram);
225
226   ShowWindow (program->hWnd, SW_SHOWMINIMIZED);
227   SetWindowPos (program->hWnd, 0, x, y, 0, 0, SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOSIZE);
228   UpdateWindow (program->hWnd);
229
230   return hProgram;
231 }
232
233 /***********************************************************************
234  *
235  *           PROGRAM_CopyMoveProgram
236  */
237
238 VOID PROGRAM_CopyMoveProgram(HLOCAL hProgram, BOOL bMove)
239 {
240   PROGRAM *program = LocalLock(hProgram);
241   PROGGROUP   *fromgroup = LocalLock(program->hGroup);
242   HLOCAL hGroup = DIALOG_CopyMove(LocalLock(program->hName),
243                                   LocalLock(fromgroup->hName), bMove);
244   if (!hGroup) return;
245
246   /* FIXME shouldn't be necessary */
247   OpenIcon(((PROGGROUP*)LocalLock(hGroup))->hWnd);
248
249   if (!PROGRAM_AddProgram(hGroup,
250 #if 0
251                           CopyIcon(program->hIcon),
252 #else
253                           program->hIcon,
254 #endif
255                           LocalLock(program->hName),
256                           program->x, program->y,
257                           LocalLock(program->hCmdLine),
258                           LocalLock(program->hIconFile),
259                           program->nIconIndex,
260                           LocalLock(program->hWorkDir),
261                           program->nHotKey, program->nCmdShow)) return;
262   GRPFILE_WriteGroupFile(hGroup);
263
264   if (bMove) PROGRAM_DeleteProgram(hProgram, TRUE);
265 }
266
267 /***********************************************************************
268  *
269  *           PROGRAM_ExecuteProgram
270  */
271
272 VOID PROGRAM_ExecuteProgram(HLOCAL hProgram)
273 {
274   PROGRAM *program = LocalLock(hProgram);
275   LPSTR lpszCmdLine = LocalLock(program->hCmdLine);
276   LPSTR lpszWorkDir = LocalLock(program->hWorkDir);
277
278   /* FIXME set working directory */
279   lpszWorkDir = lpszWorkDir;
280
281   WinExec(lpszCmdLine, program->nCmdShow);
282   if (Globals.bMinOnRun) CloseWindow(Globals.hMainWnd);
283 }
284
285 /***********************************************************************
286  *
287  *           PROGRAM_DeleteProgram
288  */
289
290 VOID PROGRAM_DeleteProgram(HLOCAL hProgram, BOOL bUpdateGrpFile)
291 {
292   PROGRAM *program = LocalLock(hProgram);
293   PROGGROUP   *group   = LocalLock(program->hGroup);
294
295   group->hActiveProgram = 0;
296
297   if (program->hPrior)
298     ((PROGRAM*)LocalLock(program->hPrior))->hNext = program->hNext;
299   else
300     ((PROGGROUP*)LocalLock(program->hGroup))->hPrograms = program->hNext;
301
302   if (program->hNext)
303     ((PROGRAM*)LocalLock(program->hNext))->hPrior = program->hPrior;
304
305   if (bUpdateGrpFile)
306     GRPFILE_WriteGroupFile(program->hGroup);
307
308   DestroyWindow(program->hWnd);
309 #if 0
310   if (program->hIcon)
311     DestroyIcon(program->hIcon);
312 #endif
313   LocalFree(program->hName);
314   LocalFree(program->hCmdLine);
315   LocalFree(program->hIconFile);
316   LocalFree(program->hWorkDir);
317   LocalFree(hProgram);
318 }
319
320 /***********************************************************************
321  *
322  *           PROGRAM_FirstProgram
323  */
324
325 HLOCAL PROGRAM_FirstProgram(HLOCAL hGroup)
326 {
327   PROGGROUP *group;
328   if (!hGroup) return(0);
329   group = LocalLock(hGroup);
330   return(group->hPrograms);
331 }
332
333 /***********************************************************************
334  *
335  *           PROGRAM_NextProgram
336  */
337
338 HLOCAL PROGRAM_NextProgram(HLOCAL hProgram)
339 {
340   PROGRAM *program;
341   if (!hProgram) return(0);
342   program = LocalLock(hProgram);
343   return(program->hNext);
344 }
345
346 /***********************************************************************
347  *
348  *           PROGRAM_ActiveProgram
349  */
350
351 HLOCAL PROGRAM_ActiveProgram(HLOCAL hGroup)
352 {
353   PROGGROUP *group;
354   if (!hGroup) return(0);
355   group = LocalLock(hGroup);
356   if (IsIconic(group->hWnd)) return(0);
357
358   return(group->hActiveProgram);
359 }
360
361 /***********************************************************************
362  *
363  *           PROGRAM_ProgramName
364  */
365
366 LPCSTR PROGRAM_ProgramName(HLOCAL hProgram)
367 {
368   PROGRAM *program;
369   if (!hProgram) return(0);
370   program = LocalLock(hProgram);
371   return(LocalLock(program->hName));
372 }
373
374 /* Local Variables:    */
375 /* c-file-style: "GNU" */
376 /* End:                */