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