main.c 13 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3
/*
 * Program Manager
 *
4 5
 * Copyright 1996 Ulrich Schmid
 * Copyright 2002 Sylvain Petreolle
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard's avatar
Alexandre Julliard committed
20 21 22
 */

#include <stdio.h>
23
#include <string.h>
24 25 26

#define OEMRESOURCE

Alexandre Julliard's avatar
Alexandre Julliard committed
27
#include "windows.h"
28
#include "shellapi.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
29
#include "progman.h"
30

Alexandre Julliard's avatar
Alexandre Julliard committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
GLOBALS Globals;

static VOID MAIN_CreateGroups(void);
static VOID MAIN_MenuCommand(HWND hWnd, WPARAM wParam, LPARAM lParam);
static ATOM MAIN_RegisterMainWinClass(void);
static VOID MAIN_CreateMainWindow(void);
static VOID MAIN_CreateMDIWindow(void);
static VOID MAIN_AutoStart(void);

#define BUFFER_SIZE 1000

/***********************************************************************
 *
 *           WinMain
 */

47
int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
Alexandre Julliard's avatar
Alexandre Julliard committed
48 49 50 51 52 53 54 55
{
  MSG      msg;

  Globals.lpszIniFile         = "progman.ini";
  Globals.lpszIcoFile         = "progman.ico";

  Globals.hInstance           = hInstance;
  Globals.hGroups             = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
56
  Globals.hActiveGroup        = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
57 58 59

  /* Read Options from `progman.ini' */
  Globals.bAutoArrange =
60
    GetPrivateProfileIntA("Settings", "AutoArrange", 0, Globals.lpszIniFile);
Alexandre Julliard's avatar
Alexandre Julliard committed
61
  Globals.bMinOnRun =
62
    GetPrivateProfileIntA("Settings", "MinOnRun", 0, Globals.lpszIniFile);
Alexandre Julliard's avatar
Alexandre Julliard committed
63
  Globals.bSaveSettings =
64
    GetPrivateProfileIntA("Settings", "SaveSettings", 0, Globals.lpszIniFile);
Alexandre Julliard's avatar
Alexandre Julliard committed
65 66

  /* Load default icons */
67 68 69 70 71 72
  Globals.hMainIcon    = ExtractIconA(Globals.hInstance, Globals.lpszIcoFile, 0);
  Globals.hGroupIcon   = ExtractIconA(Globals.hInstance, Globals.lpszIcoFile, 0);
  Globals.hDefaultIcon = ExtractIconA(Globals.hInstance, Globals.lpszIcoFile, 0);
  if (!Globals.hMainIcon)    Globals.hMainIcon = LoadIconW(0, (LPWSTR)DEFAULTICON);
  if (!Globals.hGroupIcon)   Globals.hGroupIcon = LoadIconW(0, (LPWSTR)DEFAULTICON);
  if (!Globals.hDefaultIcon) Globals.hDefaultIcon = LoadIconW(0, (LPWSTR)DEFAULTICON);
Alexandre Julliard's avatar
Alexandre Julliard committed
73 74 75 76 77 78 79 80 81 82 83

  /* Register classes */
  if (!prev)
    {
      if (!MAIN_RegisterMainWinClass()) return(FALSE);
      if (!GROUP_RegisterGroupWinClass()) return(FALSE);
      if (!PROGRAM_RegisterProgramWinClass()) return(FALSE);
    }

  /* Create main window */
  MAIN_CreateMainWindow();
84
  Globals.hAccel = LoadAcceleratorsW(Globals.hInstance, MAKEINTRESOURCEW(IDA_ACCEL));
Alexandre Julliard's avatar
Alexandre Julliard committed
85 86

  /* Setup menu, stringtable and resourcenames */
87
  STRING_LoadMenus();
Alexandre Julliard's avatar
Alexandre Julliard committed
88 89 90 91 92 93 94 95 96 97

  MAIN_CreateMDIWindow();

  /* Initialize groups */
  MAIN_CreateGroups();

  /* Start initial applications */
  MAIN_AutoStart();

  /* Message loop */
98 99
  while (GetMessageW (&msg, 0, 0, 0))
    if (!TranslateAcceleratorW(Globals.hMainWnd, Globals.hAccel, &msg))
Alexandre Julliard's avatar
Alexandre Julliard committed
100 101
      {
	TranslateMessage (&msg);
102
	DispatchMessageW (&msg);
Alexandre Julliard's avatar
Alexandre Julliard committed
103 104 105 106 107 108 109 110 111
      }
  return 0;
}

/***********************************************************************
 *
 *           MAIN_CreateGroups
 */

112
static VOID MAIN_CreateGroups(void)
Alexandre Julliard's avatar
Alexandre Julliard committed
113 114 115 116 117 118
{
  CHAR buffer[BUFFER_SIZE];
  CHAR szPath[MAX_PATHNAME_LEN];
  CHAR key[20], *ptr;

  /* Initialize groups according the `Order' entry of `progman.ini' */
119
  GetPrivateProfileStringA("Settings", "Order", "", buffer, sizeof(buffer), Globals.lpszIniFile);
Alexandre Julliard's avatar
Alexandre Julliard committed
120 121 122 123 124
  ptr = buffer;
  while (ptr < buffer + sizeof(buffer))
    {
      int num, skip, ret;
      ret = sscanf(ptr, "%d%n", &num, &skip);
Alexandre Julliard's avatar
Alexandre Julliard committed
125 126
      if (ret == 0)
	MAIN_MessageBoxIDS_s(IDS_FILE_READ_ERROR_s, Globals.lpszIniFile, IDS_ERROR, MB_OK);
Alexandre Julliard's avatar
Alexandre Julliard committed
127 128 129
      if (ret != 1) break;

      sprintf(key, "Group%d", num);
130
      GetPrivateProfileStringA("Groups", key, "", szPath,
Alexandre Julliard's avatar
Alexandre Julliard committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
			      sizeof(szPath), Globals.lpszIniFile);
      if (!szPath[0]) continue;

      GRPFILE_ReadGroupFile(szPath);

      ptr += skip;
    }
  /* FIXME initialize other groups, not enumerated by `Order' */
}

/***********************************************************************
 *
 *           MAIN_AutoStart
 */

146
VOID MAIN_AutoStart(void)
Alexandre Julliard's avatar
Alexandre Julliard committed
147 148 149 150
{
  CHAR buffer[BUFFER_SIZE];
  HLOCAL hGroup, hProgram;

151
  GetPrivateProfileStringA("Settings", "AutoStart", "Autostart", buffer,
Alexandre Julliard's avatar
Alexandre Julliard committed
152 153 154
			  sizeof(buffer), Globals.lpszIniFile);

  for (hGroup = GROUP_FirstGroup(); hGroup; hGroup = GROUP_NextGroup(hGroup))
155
    if (!lstrcmpA(buffer, GROUP_GroupName(hGroup)))
Alexandre Julliard's avatar
Alexandre Julliard committed
156 157 158 159 160 161 162 163 164 165
      for (hProgram = PROGRAM_FirstProgram(hGroup); hProgram;
	   hProgram = PROGRAM_NextProgram(hProgram))
	PROGRAM_ExecuteProgram(hProgram);
}

/***********************************************************************
 *
 *           MAIN_MainWndProc
 */

166
static LRESULT CALLBACK MAIN_MainWndProc(HWND hWnd, UINT msg,
Alexandre Julliard's avatar
Alexandre Julliard committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180
				 WPARAM wParam, LPARAM lParam)
{
  switch (msg)
    {
    case WM_INITMENU:
      CheckMenuItem(Globals.hOptionMenu, PM_AUTO_ARRANGE,
		    MF_BYCOMMAND | (Globals.bAutoArrange ? MF_CHECKED : MF_UNCHECKED));
      CheckMenuItem(Globals.hOptionMenu, PM_MIN_ON_RUN,
		    MF_BYCOMMAND | (Globals.bMinOnRun ? MF_CHECKED : MF_UNCHECKED));
      CheckMenuItem(Globals.hOptionMenu, PM_SAVE_SETTINGS,
		    MF_BYCOMMAND | (Globals.bSaveSettings ? MF_CHECKED : MF_UNCHECKED));
      break;

    case WM_COMMAND:
181 182
      if (LOWORD(wParam) < PM_FIRST_CHILD){
	MAIN_MenuCommand(hWnd, LOWORD(wParam), lParam);
183
      }
Alexandre Julliard's avatar
Alexandre Julliard committed
184 185 186 187 188 189
      break;

    case WM_DESTROY:
      PostQuitMessage (0);
      break;
    }
190
  return DefFrameProcW(hWnd, Globals.hMDIWnd, msg, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
191 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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
}

/***********************************************************************
 *
 *           MAIN_MenuCommand
 */

static VOID MAIN_MenuCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
  HLOCAL hActiveGroup    = GROUP_ActiveGroup();
  HLOCAL hActiveProgram  = PROGRAM_ActiveProgram(hActiveGroup);
  HWND   hActiveGroupWnd = GROUP_GroupWnd(hActiveGroup);

  switch(wParam)
    {
      /* Menu File */
    case PM_NEW:
      switch (DIALOG_New((hActiveGroupWnd && !IsIconic(hActiveGroupWnd)) ?
			 PM_NEW_PROGRAM : PM_NEW_GROUP))
      {
      case PM_NEW_PROGRAM:
	if (hActiveGroup) PROGRAM_NewProgram(hActiveGroup);
	break;

      case PM_NEW_GROUP:
	GROUP_NewGroup();
	break;
      }
      break;

    case PM_OPEN:
      if (hActiveProgram)
	PROGRAM_ExecuteProgram(hActiveProgram);
      else if (hActiveGroupWnd)
	OpenIcon(hActiveGroupWnd);
      break;

    case PM_MOVE:
    case PM_COPY:
      if (hActiveProgram)
	PROGRAM_CopyMoveProgram(hActiveProgram, wParam == PM_MOVE);
      break;

    case PM_DELETE:
      if (hActiveProgram)
	{
Alexandre Julliard's avatar
Alexandre Julliard committed
237
	if (DIALOG_Delete(IDS_DELETE_PROGRAM_s, PROGRAM_ProgramName(hActiveProgram)))
Alexandre Julliard's avatar
Alexandre Julliard committed
238 239 240 241
	  PROGRAM_DeleteProgram(hActiveProgram, TRUE);
	}
      else if (hActiveGroup)
	{
Alexandre Julliard's avatar
Alexandre Julliard committed
242
	if (DIALOG_Delete(IDS_DELETE_GROUP_s, GROUP_GroupName(hActiveGroup)))
Alexandre Julliard's avatar
Alexandre Julliard committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
	  GROUP_DeleteGroup(hActiveGroup);
	}
      break;

    case PM_ATTRIBUTES:
      if (hActiveProgram)
	PROGRAM_ModifyProgram(hActiveProgram);
      else if (hActiveGroup)
	GROUP_ModifyGroup(hActiveGroup);
      break;

    case PM_EXECUTE:
      DIALOG_Execute();
      break;

    case PM_EXIT:
      PostQuitMessage(0);
      break;

      /* Menu Options */
    case PM_AUTO_ARRANGE:
      Globals.bAutoArrange = !Globals.bAutoArrange;
      CheckMenuItem(Globals.hOptionMenu, PM_AUTO_ARRANGE,
		    MF_BYCOMMAND | (Globals.bAutoArrange ?
				    MF_CHECKED : MF_UNCHECKED));
268
      WritePrivateProfileStringA("Settings", "AutoArrange",
Alexandre Julliard's avatar
Alexandre Julliard committed
269 270
				Globals.bAutoArrange ? "1" : "0",
				Globals.lpszIniFile);
271
      WritePrivateProfileStringA(NULL,NULL,NULL,Globals.lpszIniFile); /* flush it */
Alexandre Julliard's avatar
Alexandre Julliard committed
272 273 274 275 276 277 278
      break;

    case PM_MIN_ON_RUN:
      Globals.bMinOnRun = !Globals.bMinOnRun;
      CheckMenuItem(Globals.hOptionMenu, PM_MIN_ON_RUN,
		    MF_BYCOMMAND | (Globals.bMinOnRun ?
				    MF_CHECKED : MF_UNCHECKED));
279
      WritePrivateProfileStringA("Settings", "MinOnRun",
Alexandre Julliard's avatar
Alexandre Julliard committed
280 281
				Globals.bMinOnRun ? "1" : "0",
				Globals.lpszIniFile);
282
      WritePrivateProfileStringA(NULL,NULL,NULL,Globals.lpszIniFile); /* flush it */
Alexandre Julliard's avatar
Alexandre Julliard committed
283 284 285 286 287 288 289
      break;

    case PM_SAVE_SETTINGS:
      Globals.bSaveSettings = !Globals.bSaveSettings;
      CheckMenuItem(Globals.hOptionMenu, PM_SAVE_SETTINGS,
		    MF_BYCOMMAND | (Globals.bSaveSettings ?
				    MF_CHECKED : MF_UNCHECKED));
290
      WritePrivateProfileStringA("Settings", "SaveSettings",
Alexandre Julliard's avatar
Alexandre Julliard committed
291 292
				Globals.bSaveSettings ? "1" : "0",
				Globals.lpszIniFile);
293
      WritePrivateProfileStringA(NULL,NULL,NULL,Globals.lpszIniFile); /* flush it */
Alexandre Julliard's avatar
Alexandre Julliard committed
294 295 296
      break;

      /* Menu Windows */
297 298 299 300 301 302 303 304
    case PM_OVERLAP:
      SendMessageW(Globals.hMDIWnd, WM_MDICASCADE, 0, 0);
      break;

    case PM_SIDE_BY_SIDE:
      SendMessageW(Globals.hMDIWnd, WM_MDITILE, MDITILE_VERTICAL, 0);
      break;

Alexandre Julliard's avatar
Alexandre Julliard committed
305
    case PM_ARRANGE:
306

Alexandre Julliard's avatar
Alexandre Julliard committed
307 308 309
      if (hActiveGroupWnd && !IsIconic(hActiveGroupWnd))
	ArrangeIconicWindows(hActiveGroupWnd);
      else
310
	SendMessageW(Globals.hMDIWnd, WM_MDIICONARRANGE, 0, 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
311 312 313 314
      break;

      /* Menu Help */
    case PM_CONTENTS:
315
      if (!WinHelpA(Globals.hMainWnd, "progman.hlp", HELP_CONTENTS, 0))
Alexandre Julliard's avatar
Alexandre Julliard committed
316
	MAIN_MessageBoxIDS(IDS_WINHELP_ERROR, IDS_ERROR, MB_OK);
Alexandre Julliard's avatar
Alexandre Julliard committed
317 318 319
      break;

    case PM_ABOUT_WINE:
320
      ShellAboutA(hWnd, "WINE", "Program Manager", 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
321 322 323
      break;

    default:
Alexandre Julliard's avatar
Alexandre Julliard committed
324
	MAIN_MessageBoxIDS(IDS_NOT_IMPLEMENTED, IDS_ERROR, MB_OK);
Alexandre Julliard's avatar
Alexandre Julliard committed
325 326 327 328 329 330 331 332 333
      break;
    }
}

/***********************************************************************
 *
 *           MAIN_RegisterMainWinClass
 */

334
static ATOM MAIN_RegisterMainWinClass(void)
Alexandre Julliard's avatar
Alexandre Julliard committed
335
{
336
  WNDCLASSW class;
Alexandre Julliard's avatar
Alexandre Julliard committed
337 338 339 340 341 342 343

  class.style         = CS_HREDRAW | CS_VREDRAW;
  class.lpfnWndProc   = MAIN_MainWndProc;
  class.cbClsExtra    = 0;
  class.cbWndExtra    = 0;
  class.hInstance     = Globals.hInstance;
  class.hIcon         = Globals.hMainIcon;
344
  class.hCursor       = LoadCursorW (0, (LPWSTR)IDC_ARROW);
Alexandre Julliard's avatar
Alexandre Julliard committed
345 346 347 348
  class.hbrBackground = GetStockObject (NULL_BRUSH);
  class.lpszMenuName  = 0;
  class.lpszClassName = STRING_MAIN_WIN_CLASS_NAME;

349
  return RegisterClassW(&class);
Alexandre Julliard's avatar
Alexandre Julliard committed
350 351 352 353 354 355 356
}

/***********************************************************************
 *
 *           MAIN_CreateMainWindow
 */

357
static VOID MAIN_CreateMainWindow(void)
Alexandre Julliard's avatar
Alexandre Julliard committed
358 359 360 361 362 363 364 365
{
  INT  left , top, right, bottom, width, height, show;
  CHAR buffer[100];

  Globals.hMDIWnd   = 0;
  Globals.hMainMenu = 0;

  /* Get the geometry of the main window */
366
  GetPrivateProfileStringA("Settings", "Window", "", buffer, sizeof(buffer), Globals.lpszIniFile);
Alexandre Julliard's avatar
Alexandre Julliard committed
367 368 369 370 371 372 373 374 375 376 377 378 379
  if (5 == sscanf(buffer, "%d %d %d %d %d", &left, &top, &right, &bottom, &show))
  {
    width  = right - left;
    height = bottom - top;
  }
  else
  {
    left = top = width = height = CW_USEDEFAULT;
    show = SW_SHOWNORMAL;
  }

  /* Create main Window */
  Globals.hMainWnd =
380
    CreateWindowW(STRING_MAIN_WIN_CLASS_NAME, NULL,
Alexandre Julliard's avatar
Alexandre Julliard committed
381 382 383 384 385 386 387 388 389 390 391 392
		  WS_OVERLAPPEDWINDOW, left, top, width, height,
		  0, 0, Globals.hInstance, 0);

  ShowWindow (Globals.hMainWnd, show);
  UpdateWindow (Globals.hMainWnd);
}

/***********************************************************************
 *
 *           MAIN_CreateMDIWindow
 */

393
static VOID MAIN_CreateMDIWindow(void)
Alexandre Julliard's avatar
Alexandre Julliard committed
394 395 396 397 398 399 400 401 402 403 404 405
{
  CLIENTCREATESTRUCT ccs;
  RECT rect;

  /* Get the geometry of the MDI window */
  GetClientRect(Globals.hMainWnd, &rect);

  ccs.hWindowMenu  = Globals.hWindowsMenu;
  ccs.idFirstChild = PM_FIRST_CHILD;

  /* Create MDI Window */
  Globals.hMDIWnd =
406
    CreateWindowW(STRING_MDI_WIN_CLASS_NAME, NULL,
Alexandre Julliard's avatar
Alexandre Julliard committed
407 408 409 410 411 412 413 414 415 416 417 418
		  WS_CHILD, rect.left, rect.top,
		  rect.right - rect.left, rect.bottom - rect.top,
		  Globals.hMainWnd, 0,
		  Globals.hInstance, &ccs);

  ShowWindow (Globals.hMDIWnd, SW_SHOW);
  UpdateWindow (Globals.hMDIWnd);
}

/**********************************************************************/
/***********************************************************************
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
419
 *           MAIN_MessageBoxIDS
Alexandre Julliard's avatar
Alexandre Julliard committed
420
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
421
INT MAIN_MessageBoxIDS(UINT ids_text, UINT ids_title, WORD type)
Alexandre Julliard's avatar
Alexandre Julliard committed
422
{
Alexandre Julliard's avatar
Alexandre Julliard committed
423 424
  CHAR text[MAX_STRING_LEN];
  CHAR title[MAX_STRING_LEN];
Alexandre Julliard's avatar
Alexandre Julliard committed
425

426 427
  LoadStringA(Globals.hInstance, ids_text, text, sizeof(text));
  LoadStringA(Globals.hInstance, ids_title, title, sizeof(title));
Alexandre Julliard's avatar
Alexandre Julliard committed
428

429
  return(MessageBoxA(Globals.hMainWnd, text, title, type));
Alexandre Julliard's avatar
Alexandre Julliard committed
430 431 432 433
}

/***********************************************************************
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
434
 *           MAIN_MessageBoxIDS_s
Alexandre Julliard's avatar
Alexandre Julliard committed
435
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
436
INT MAIN_MessageBoxIDS_s(UINT ids_text, LPCSTR str, UINT ids_title, WORD type)
Alexandre Julliard's avatar
Alexandre Julliard committed
437
{
Alexandre Julliard's avatar
Alexandre Julliard committed
438 439 440
  CHAR text[MAX_STRING_LEN];
  CHAR title[MAX_STRING_LEN];
  CHAR newtext[MAX_STRING_LEN + MAX_PATHNAME_LEN];
Alexandre Julliard's avatar
Alexandre Julliard committed
441

442 443 444
  LoadStringA(Globals.hInstance, ids_text, text, sizeof(text));
  LoadStringA(Globals.hInstance, ids_title, title, sizeof(title));
  wsprintfA(newtext, text, str);
Alexandre Julliard's avatar
Alexandre Julliard committed
445

446
  return(MessageBoxA(Globals.hMainWnd, newtext, title, type));
Alexandre Julliard's avatar
Alexandre Julliard committed
447 448 449 450
}

/***********************************************************************
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
451
 *           MAIN_ReplaceString
Alexandre Julliard's avatar
Alexandre Julliard committed
452 453
 */

Alexandre Julliard's avatar
Alexandre Julliard committed
454
VOID MAIN_ReplaceString(HLOCAL *handle, LPSTR replace)
Alexandre Julliard's avatar
Alexandre Julliard committed
455
{
Alexandre Julliard's avatar
Alexandre Julliard committed
456 457 458 459
  HLOCAL newhandle = LocalAlloc(LMEM_FIXED, strlen(replace) + 1);
  if (newhandle)
    {
      LPSTR  newstring = LocalLock(newhandle);
460
      strcpy(newstring, replace);
Alexandre Julliard's avatar
Alexandre Julliard committed
461 462 463 464
      LocalFree(*handle);
      *handle = newhandle;
    }
  else MAIN_MessageBoxIDS(IDS_OUT_OF_MEMORY, IDS_ERROR, MB_OK);
Alexandre Julliard's avatar
Alexandre Julliard committed
465
}