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