program.c 10.2 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4
/*
 * Program Manager
 *
 * Copyright 1996 Ulrich Schmid
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
Alexandre Julliard's avatar
Alexandre Julliard committed
19 20
 */

21 22
#define WIN32_LEAN_AND_MEAN

23
#include <string.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
24
#include "windows.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
25 26 27 28 29 30 31
#include "progman.h"

/***********************************************************************
 *
 *           PROGRAM_ProgramWndProc
 */

32
static LRESULT CALLBACK PROGRAM_ProgramWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
33 34 35 36 37
{
  switch (msg)
    {
    case WM_NCLBUTTONDOWN:
      {
38
	HLOCAL  hProgram = (HLOCAL) GetWindowLongPtrW(hWnd, 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
39
	PROGRAM *program = LocalLock(hProgram);
40
	PROGGROUP   *group   = LocalLock(program->hGroup);
Alexandre Julliard's avatar
Alexandre Julliard committed
41 42 43 44 45 46 47
	group->hActiveProgram = hProgram;
	EnableMenuItem(Globals.hFileMenu, PM_MOVE , MF_ENABLED);
	EnableMenuItem(Globals.hFileMenu, PM_COPY , MF_ENABLED);
	break;
      }
    case WM_NCLBUTTONDBLCLK:
      {
48
	PROGRAM_ExecuteProgram((HLOCAL) GetWindowLongPtrW(hWnd, 0));
Alexandre Julliard's avatar
Alexandre Julliard committed
49 50 51
	return(0);
      }

Alexandre Julliard's avatar
Alexandre Julliard committed
52 53
    case WM_PAINTICON:
    case WM_NCPAINT:
Alexandre Julliard's avatar
Alexandre Julliard committed
54 55 56 57 58
      {
	PROGRAM *program;
	PAINTSTRUCT      ps;
	HDC              hdc;
	hdc     = BeginPaint(hWnd,&ps);
59
	program = LocalLock((HLOCAL) GetWindowLongPtrW(hWnd, 0));
Alexandre Julliard's avatar
Alexandre Julliard committed
60 61 62 63 64 65
	if (program->hIcon)
	  DrawIcon(hdc, 0, 0, program->hIcon);
	EndPaint(hWnd,&ps);
	break;
      }
    }
66
  return DefWindowProcW(hWnd, msg, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
67 68 69 70 71 72 73
}

/***********************************************************************
 *
 *           PROGRAM_RegisterProgramWinClass
 */

74
ATOM PROGRAM_RegisterProgramWinClass(void)
Alexandre Julliard's avatar
Alexandre Julliard committed
75
{
76
  WNDCLASSW class;
Alexandre Julliard's avatar
Alexandre Julliard committed
77 78 79 80

  class.style         = CS_HREDRAW | CS_VREDRAW;
  class.lpfnWndProc   = PROGRAM_ProgramWndProc;
  class.cbClsExtra    = 0;
Mike McCormack's avatar
Mike McCormack committed
81
  class.cbWndExtra    = sizeof(LONG_PTR);
Alexandre Julliard's avatar
Alexandre Julliard committed
82 83
  class.hInstance     = Globals.hInstance;
  class.hIcon         = 0;
84
  class.hCursor       = LoadCursorW (0, (LPWSTR)IDC_ARROW);
Alexandre Julliard's avatar
Alexandre Julliard committed
85 86 87 88
  class.hbrBackground = GetStockObject (WHITE_BRUSH);
  class.lpszMenuName  = 0;
  class.lpszClassName = STRING_PROGRAM_WIN_CLASS_NAME;

89
  return RegisterClassW(&class);
Alexandre Julliard's avatar
Alexandre Julliard committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
}

/***********************************************************************
 *
 *           PROGRAM_NewProgram
 */

VOID PROGRAM_NewProgram(HLOCAL hGroup)
{
  INT  nCmdShow = SW_SHOWNORMAL;
  INT  nHotKey = 0;
  INT  nIconIndex = 0;
  CHAR szName[MAX_PATHNAME_LEN] = "";
  CHAR szCmdLine[MAX_PATHNAME_LEN] = "";
  CHAR szIconFile[MAX_PATHNAME_LEN] = "";
  CHAR szWorkDir[MAX_PATHNAME_LEN] = "";
  HICON hIcon = 0;

  if (!DIALOG_ProgramAttributes(szName, szCmdLine, szWorkDir, szIconFile,
				&hIcon, &nIconIndex, &nHotKey,
				&nCmdShow, MAX_PATHNAME_LEN))
    return;

113
  if (!hIcon) hIcon = LoadIconW(0, (LPWSTR)IDI_WINLOGO);
Alexandre Julliard's avatar
Alexandre Julliard committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135


  if (!PROGRAM_AddProgram(hGroup, hIcon, szName, 0, 0, szCmdLine, szIconFile,
			  nIconIndex, szWorkDir, nHotKey, nCmdShow))
    return;

  GRPFILE_WriteGroupFile(hGroup);
}

/***********************************************************************
 *
 *           PROGRAM_ModifyProgram
 */

VOID PROGRAM_ModifyProgram(HLOCAL hProgram)
{
  PROGRAM *program = LocalLock(hProgram);
  CHAR szName[MAX_PATHNAME_LEN];
  CHAR szCmdLine[MAX_PATHNAME_LEN];
  CHAR szIconFile[MAX_PATHNAME_LEN];
  CHAR szWorkDir[MAX_PATHNAME_LEN];

136 137 138 139
  lstrcpynA(szName, LocalLock(program->hName), MAX_PATHNAME_LEN);
  lstrcpynA(szCmdLine, LocalLock(program->hCmdLine), MAX_PATHNAME_LEN);
  lstrcpynA(szIconFile, LocalLock(program->hIconFile), MAX_PATHNAME_LEN);
  lstrcpynA(szWorkDir, LocalLock(program->hWorkDir), MAX_PATHNAME_LEN);
Alexandre Julliard's avatar
Alexandre Julliard committed
140 141 142 143 144 145 146 147 148 149 150 151

  if (!DIALOG_ProgramAttributes(szName, szCmdLine, szWorkDir, szIconFile,
				&program->hIcon, &program->nIconIndex,
				&program->nHotKey, &program->nCmdShow,
				MAX_PATHNAME_LEN))
    return;

  MAIN_ReplaceString(&program->hName, szName);
  MAIN_ReplaceString(&program->hCmdLine, szCmdLine);
  MAIN_ReplaceString(&program->hIconFile, szIconFile);
  MAIN_ReplaceString(&program->hWorkDir, szWorkDir);

152
  SetWindowTextA(program->hWnd, szName);
Alexandre Julliard's avatar
Alexandre Julliard committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
  UpdateWindow(program->hWnd);

  GRPFILE_WriteGroupFile(program->hGroup);

  return;
}

/***********************************************************************
 *
 *           PROGRAM_AddProgram
 */

HLOCAL PROGRAM_AddProgram(HLOCAL hGroup, HICON hIcon, LPCSTR lpszName,
			  INT x, INT y, LPCSTR lpszCmdLine,
			  LPCSTR lpszIconFile, INT nIconIndex,
			  LPCSTR lpszWorkDir, INT nHotKey, INT nCmdShow)
{
170
  PROGGROUP *group = LocalLock(hGroup);
Alexandre Julliard's avatar
Alexandre Julliard committed
171 172 173
  PROGRAM *program;
  HLOCAL hPrior, *p;
  HLOCAL hProgram  = LocalAlloc(LMEM_FIXED, sizeof(PROGRAM));
174 175 176 177
  HLOCAL hName     = LocalAlloc(LMEM_FIXED, 1 + strlen(lpszName));
  HLOCAL hCmdLine  = LocalAlloc(LMEM_FIXED, 1 + strlen(lpszCmdLine));
  HLOCAL hIconFile = LocalAlloc(LMEM_FIXED, 1 + strlen(lpszIconFile));
  HLOCAL hWorkDir  = LocalAlloc(LMEM_FIXED, 1 + strlen(lpszWorkDir));
Alexandre Julliard's avatar
Alexandre Julliard committed
178 179
  if (!hProgram || !hName || !hCmdLine || !hIconFile || !hWorkDir)
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
180
      MAIN_MessageBoxIDS(IDS_OUT_OF_MEMORY, IDS_ERROR, MB_OK);
Alexandre Julliard's avatar
Alexandre Julliard committed
181 182 183 184 185 186 187
      if (hProgram)  LocalFree(hProgram);
      if (hName)     LocalFree(hName);
      if (hCmdLine)  LocalFree(hCmdLine);
      if (hIconFile) LocalFree(hIconFile);
      if (hWorkDir)  LocalFree(hWorkDir);
      return(0);
    }
188 189 190 191
  memcpy(LocalLock(hName),     lpszName,     1 + strlen(lpszName));
  memcpy(LocalLock(hCmdLine),  lpszCmdLine,  1 + strlen(lpszCmdLine));
  memcpy(LocalLock(hIconFile), lpszIconFile, 1 + strlen(lpszIconFile));
  memcpy(LocalLock(hWorkDir),  lpszWorkDir,  1 + strlen(lpszWorkDir));
Alexandre Julliard's avatar
Alexandre Julliard committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

  group->hActiveProgram  = hProgram;

  hPrior = 0;
  p = &group->hPrograms;
  while (*p)
    {
      hPrior = *p;
      p = &((PROGRAM*)LocalLock(hPrior))->hNext;
    }
  *p = hProgram;

  program = LocalLock(hProgram);
  program->hGroup     = hGroup;
  program->hPrior     = hPrior;
  program->hNext      = 0;
  program->hName      = hName;
  program->hCmdLine   = hCmdLine;
  program->hIconFile  = hIconFile;
  program->nIconIndex = nIconIndex;
  program->hWorkDir   = hWorkDir;
  program->hIcon      = hIcon;
  program->nCmdShow   = nCmdShow;
  program->nHotKey    = nHotKey;

  program->hWnd =
218
    CreateWindowW(STRING_PROGRAM_WIN_CLASS_NAME, NULL,
Alexandre Julliard's avatar
Alexandre Julliard committed
219
		  WS_CHILD | WS_CAPTION,
Alexandre Julliard's avatar
Alexandre Julliard committed
220 221 222
		  x, y, CW_USEDEFAULT, CW_USEDEFAULT,
		  group->hWnd, 0, Globals.hInstance, 0);

223 224
  SetWindowTextA(program->hWnd, lpszName);
  SetWindowLongPtrW(program->hWnd, 0, (LONG_PTR) hProgram);
Alexandre Julliard's avatar
Alexandre Julliard committed
225 226

  ShowWindow (program->hWnd, SW_SHOWMINIMIZED);
Alexandre Julliard's avatar
Alexandre Julliard committed
227
  SetWindowPos (program->hWnd, 0, x, y, 0, 0, SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOSIZE);
Alexandre Julliard's avatar
Alexandre Julliard committed
228 229 230 231 232 233 234 235 236 237 238 239 240
  UpdateWindow (program->hWnd);

  return hProgram;
}

/***********************************************************************
 *
 *           PROGRAM_CopyMoveProgram
 */

VOID PROGRAM_CopyMoveProgram(HLOCAL hProgram, BOOL bMove)
{
  PROGRAM *program = LocalLock(hProgram);
241
  PROGGROUP   *fromgroup = LocalLock(program->hGroup);
Alexandre Julliard's avatar
Alexandre Julliard committed
242 243 244 245 246
  HLOCAL hGroup = DIALOG_CopyMove(LocalLock(program->hName),
				  LocalLock(fromgroup->hName), bMove);
  if (!hGroup) return;

  /* FIXME shouldn't be necessary */
247
  OpenIcon(((PROGGROUP*)LocalLock(hGroup))->hWnd);
Alexandre Julliard's avatar
Alexandre Julliard committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276

  if (!PROGRAM_AddProgram(hGroup,
#if 0
			  CopyIcon(program->hIcon),
#else
			  program->hIcon,
#endif
			  LocalLock(program->hName),
			  program->x, program->y,
			  LocalLock(program->hCmdLine),
			  LocalLock(program->hIconFile),
			  program->nIconIndex,
			  LocalLock(program->hWorkDir),
			  program->nHotKey, program->nCmdShow)) return;
  GRPFILE_WriteGroupFile(hGroup);

  if (bMove) PROGRAM_DeleteProgram(hProgram, TRUE);
}

/***********************************************************************
 *
 *           PROGRAM_ExecuteProgram
 */

VOID PROGRAM_ExecuteProgram(HLOCAL hProgram)
{
  PROGRAM *program = LocalLock(hProgram);
  LPSTR lpszCmdLine = LocalLock(program->hCmdLine);

277
  /* FIXME set working directory from program->hWorkDir */
Alexandre Julliard's avatar
Alexandre Julliard committed
278 279 280 281 282 283 284 285 286 287 288 289 290

  WinExec(lpszCmdLine, program->nCmdShow);
  if (Globals.bMinOnRun) CloseWindow(Globals.hMainWnd);
}

/***********************************************************************
 *
 *           PROGRAM_DeleteProgram
 */

VOID PROGRAM_DeleteProgram(HLOCAL hProgram, BOOL bUpdateGrpFile)
{
  PROGRAM *program = LocalLock(hProgram);
291
  PROGGROUP   *group   = LocalLock(program->hGroup);
Alexandre Julliard's avatar
Alexandre Julliard committed
292 293 294 295 296 297

  group->hActiveProgram = 0;

  if (program->hPrior)
    ((PROGRAM*)LocalLock(program->hPrior))->hNext = program->hNext;
  else
298
    ((PROGGROUP*)LocalLock(program->hGroup))->hPrograms = program->hNext;
299

Alexandre Julliard's avatar
Alexandre Julliard committed
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
  if (program->hNext)
    ((PROGRAM*)LocalLock(program->hNext))->hPrior = program->hPrior;

  if (bUpdateGrpFile)
    GRPFILE_WriteGroupFile(program->hGroup);

  DestroyWindow(program->hWnd);
#if 0
  if (program->hIcon)
    DestroyIcon(program->hIcon);
#endif
  LocalFree(program->hName);
  LocalFree(program->hCmdLine);
  LocalFree(program->hIconFile);
  LocalFree(program->hWorkDir);
  LocalFree(hProgram);
}

/***********************************************************************
 *
 *           PROGRAM_FirstProgram
 */

HLOCAL PROGRAM_FirstProgram(HLOCAL hGroup)
{
325
  PROGGROUP *group;
Alexandre Julliard's avatar
Alexandre Julliard committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
  if (!hGroup) return(0);
  group = LocalLock(hGroup);
  return(group->hPrograms);
}

/***********************************************************************
 *
 *           PROGRAM_NextProgram
 */

HLOCAL PROGRAM_NextProgram(HLOCAL hProgram)
{
  PROGRAM *program;
  if (!hProgram) return(0);
  program = LocalLock(hProgram);
  return(program->hNext);
}

/***********************************************************************
 *
 *           PROGRAM_ActiveProgram
 */

HLOCAL PROGRAM_ActiveProgram(HLOCAL hGroup)
{
351
  PROGGROUP *group;
Alexandre Julliard's avatar
Alexandre Julliard committed
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
  if (!hGroup) return(0);
  group = LocalLock(hGroup);
  if (IsIconic(group->hWnd)) return(0);

  return(group->hActiveProgram);
}

/***********************************************************************
 *
 *           PROGRAM_ProgramName
 */

LPCSTR PROGRAM_ProgramName(HLOCAL hProgram)
{
  PROGRAM *program;
  if (!hProgram) return(0);
  program = LocalLock(hProgram);
  return(LocalLock(program->hName));
}