appdefaults.c 16.9 KB
Newer Older
1
/*
2
 * WineCfg app settings tabsheet
3
 *
4
 * Copyright 2004 Robert van Herk
5 6
 * Copyright 2004 Chris Morgan
 * Copyright 2004 Mike Hearn
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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
20
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 22 23
 *
 */

24
#define WIN32_LEAN_AND_MEAN
25 26 27 28
#define NONAMELESSUNION
#include <windows.h>
#include <commdlg.h>
#include <wine/debug.h>
29
#include <stdio.h>
30
#include <stdlib.h>
31
#include <assert.h>
32 33 34 35 36
#include "winecfg.h"
#include "resource.h"

WINE_DEFAULT_DEBUG_CHANNEL(winecfg);

37 38 39 40 41 42 43 44 45 46 47 48 49 50
static const struct
{
    const char *szVersion;
    const char *szDescription;
    DWORD       dwMajorVersion;
    DWORD       dwMinorVersion;
    DWORD       dwBuildNumber;
    DWORD       dwPlatformId;
    const char *szCSDVersion;
    WORD        wServicePackMajor;
    WORD        wServicePackMinor;
    const char *szProductType;
} win_versions[] =
{
51
    { "win2008", "Windows 2008",   6,  0, 0x1771,VER_PLATFORM_WIN32_NT, "Service Pack 1", 0, 0, "ServerNT"},
52
    { "vista",   "Windows Vista",  6,  0, 0x1770,VER_PLATFORM_WIN32_NT, " ", 0, 0, "WinNT"},
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    { "win2003", "Windows 2003",   5,  2, 0xECE, VER_PLATFORM_WIN32_NT, "Service Pack 1", 1, 0, "ServerNT"},
    { "winxp",   "Windows XP",     5,  1, 0xA28, VER_PLATFORM_WIN32_NT, "Service Pack 2", 2, 0, "WinNT"},
    { "win2k",   "Windows 2000",   5,  0, 0x893, VER_PLATFORM_WIN32_NT, "Service Pack 4", 4, 0, "WinNT"},
    { "winme",   "Windows ME",     4, 90, 0xBB8, VER_PLATFORM_WIN32_WINDOWS, " ", 0, 0, ""},
    { "win98",   "Windows 98",     4, 10, 0x8AE, VER_PLATFORM_WIN32_WINDOWS, " A ", 0, 0, ""},
    { "win95",   "Windows 95",     4,  0, 0x3B6, VER_PLATFORM_WIN32_WINDOWS, "", 0, 0, ""},
    { "nt40",    "Windows NT 4.0", 4,  0, 0x565, VER_PLATFORM_WIN32_NT, "Service Pack 6a", 6, 0, "WinNT"},
    { "nt351",   "Windows NT 3.5", 3, 51, 0x421, VER_PLATFORM_WIN32_NT, "Service Pack 2", 0, 0, "WinNT"},
    { "win31",   "Windows 3.1",    2, 10,     0, VER_PLATFORM_WIN32s, "Win32s 1.3", 0, 0, ""},
    { "win30",   "Windows 3.0",    3,  0,     0, VER_PLATFORM_WIN32s, "Win32s 1.3", 0, 0, ""},
    { "win20",   "Windows 2.0",    2,  0,     0, VER_PLATFORM_WIN32s, "Win32s 1.3", 0, 0, ""}
};

#define NB_VERSIONS (sizeof(win_versions)/sizeof(win_versions[0]))

68 69 70 71
static const char szKey9x[] = "Software\\Microsoft\\Windows\\CurrentVersion";
static const char szKeyNT[] = "Software\\Microsoft\\Windows NT\\CurrentVersion";

static int get_registry_version(void)
72
{
73
    int i, best = -1, platform, major, minor = 0, build = 0;
74 75 76
    char *p, *ver;

    if ((ver = get_reg_key( HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", NULL )))
77 78 79
    {
        char *build_str;

80
        platform = VER_PLATFORM_WIN32_NT;
81 82 83 84

	build_str = get_reg_key( HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", NULL );
        build = atoi(build_str);
    }
85 86 87 88 89 90 91
    else if ((ver = get_reg_key( HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", NULL )))
        platform = VER_PLATFORM_WIN32_WINDOWS;
    else
        return -1;

    if ((p = strchr( ver, '.' )))
    {
92 93 94 95 96 97 98 99 100
        char *minor_str = p;
        *minor_str++ = 0;
        if ((p = strchr( minor_str, '.' )))
        {
            char *build_str = p;
            *build_str++ = 0;
            build = atoi(build_str);
        }
        minor = atoi(minor_str);
101 102 103 104 105 106 107 108
    }
    major = atoi(ver);

    for (i = 0; i < NB_VERSIONS; i++)
    {
        if (win_versions[i].dwPlatformId != platform) continue;
        if (win_versions[i].dwMajorVersion != major) continue;
        best = i;
109 110 111
        if ((win_versions[i].dwMinorVersion == minor) &&
            (win_versions[i].dwBuildNumber == build))
            return i;
112 113 114
    }
    return best;
}
115

116 117 118
static void update_comboboxes(HWND dialog)
{
    int i, ver;
119
    char *winver;
120

121 122
    /* retrieve the registry values */
    winver = get_reg_key(config_key, keypath(""), "Version", "");
123
    ver = get_registry_version();
124

125
    if (!winver || !winver[0])
126 127
    {
        HeapFree(GetProcessHeap(), 0, winver);
128

129 130 131 132 133 134
        if (current_app) /* no explicit setting */
        {
            WINE_TRACE("setting winver combobox to default\n");
            SendDlgItemMessage (dialog, IDC_WINVER, CB_SETCURSEL, 0, 0);
            return;
        }
135
        if (ver != -1) winver = strdupA( win_versions[ver].szVersion );
136
        else winver = strdupA("winxp");
137 138 139 140 141 142 143 144 145 146 147 148
    }
    WINE_TRACE("winver is %s\n", winver);

    /* normalize the version strings */
    for (i = 0; i < NB_VERSIONS; i++)
    {
        if (!strcasecmp (win_versions[i].szVersion, winver))
        {
            SendDlgItemMessage (dialog, IDC_WINVER, CB_SETCURSEL,
                                (WPARAM) i + (current_app?1:0), 0);
            WINE_TRACE("match with %s\n", win_versions[i].szVersion);
            break;
149
	}
150
    }
151

152
    HeapFree(GetProcessHeap(), 0, winver);
153 154
}

155
static void
156
init_comboboxes (HWND dialog)
157
{
158
    int i;
159

160
    SendDlgItemMessage(dialog, IDC_WINVER, CB_RESETCONTENT, 0, 0);
161

162 163
    /* add the default entries (automatic) which correspond to no setting  */
    if (current_app)
164 165 166 167 168 169
    {
        WCHAR str[256];
        LoadStringW (GetModuleHandle (NULL), IDS_USE_GLOBAL_SETTINGS, str,
            sizeof(str)/sizeof(str[0]));
        SendDlgItemMessageW (dialog, IDC_WINVER, CB_ADDSTRING, 0, (LPARAM)str);
    }
170

171
    for (i = 0; i < NB_VERSIONS; i++)
172
    {
173
      SendDlgItemMessage (dialog, IDC_WINVER, CB_ADDSTRING,
174
                          0, (LPARAM) win_versions[i].szDescription);
175
    }
176 177
}

178
static void add_listview_item(HWND listview, WCHAR *text, void *association)
179
{
180
  LVITEMW item;
181 182

  item.mask = LVIF_TEXT | LVIF_PARAM;
183
  item.pszText = text;
184
  item.cchTextMax = lstrlenW(text);
185 186
  item.lParam = (LPARAM) association;
  item.iItem = ListView_GetItemCount(listview);
187
  item.iSubItem = 0;
188

189
  SendMessage(listview, LVM_INSERTITEMW, 0, (LPARAM) &item);
190
}
191

192
/* Called when the application is initialized (cannot reinit!)  */
193
static void init_appsheet(HWND dialog)
194
{
195 196
  HWND listview;
  HKEY key;
197 198
  int i;
  DWORD size;
199
  WCHAR appname[1024];
200

201
  WINE_TRACE("()\n");
202

203 204 205 206
  listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);

  /* we use the lparam field of the item so we can alter the presentation later and not change code
   * for instance, to use the tile view or to display the EXEs embedded 'display name' */
207 208 209
  LoadStringW (GetModuleHandle (NULL), IDS_DEFAULT_SETTINGS, appname,
      sizeof(appname)/sizeof(appname[0]));
  add_listview_item(listview, appname, NULL);
210 211 212

  /* because this list is only populated once, it's safe to bypass the settings list here  */
  if (RegOpenKey(config_key, "AppDefaults", &key) == ERROR_SUCCESS)
213
  {
214
      i = 0;
215 216
      size = sizeof(appname)/sizeof(appname[0]);
      while (RegEnumKeyExW (key, i, appname, &size, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
217
      {
218
          add_listview_item(listview, appname, strdupW(appname));
219 220

          i++;
221
          size = sizeof(appname)/sizeof(appname[0]);
222 223 224 225 226 227 228 229 230 231 232 233
      }

      RegCloseKey(key);
  }

  init_comboboxes(dialog);
  
  /* Select the default settings listview item  */
  {
      LVITEM item;
      
      item.iItem = 0;
234 235
      item.iSubItem = 0;
      item.mask = LVIF_STATE;
236 237 238
      item.state = LVIS_SELECTED | LVIS_FOCUSED;
      item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;

239
      SendMessage(listview, LVM_SETITEM, 0, (LPARAM) &item);
240
  }
241
  
242 243
}

244 245
/* there has to be an easier way than this  */
static int get_listview_selection(HWND listview)
246
{
247 248 249 250
  int count = ListView_GetItemCount(listview);
  int i;
  
  for (i = 0; i < count; i++)
251
  {
252
    if (ListView_GetItemState(listview, i, LVIS_SELECTED)) return i;
253
  }
254 255

  return -1;
256 257
}

258

259 260
/* called when the user selects a different application */
static void on_selection_change(HWND dialog, HWND listview)
261
{
262
  LVITEM item;
263
  WCHAR* oldapp = current_app;
264

265
  WINE_TRACE("()\n");
266

267
  item.iItem = get_listview_selection(listview);
268
  item.iSubItem = 0;
269
  item.mask = LVIF_PARAM;
270 271

  WINE_TRACE("item.iItem=%d\n", item.iItem);
272 273 274
  
  if (item.iItem == -1) return;
  
275
  SendMessage(listview, LVM_GETITEM, 0, (LPARAM) &item);
276

277
  current_app = (WCHAR*) item.lParam;
278

Mike Hearn's avatar
Mike Hearn committed
279
  if (current_app)
280
  {
281
      WINE_TRACE("current_app is now %s\n", wine_dbgstr_w (current_app));
282 283 284 285
      enable(IDC_APP_REMOVEAPP);
  }
  else
  {
Mike Hearn's avatar
Mike Hearn committed
286
      WINE_TRACE("current_app=NULL, editing global settings\n");
287 288 289 290 291 292
      /* focus will never be on the button in this callback so it's safe  */
      disable(IDC_APP_REMOVEAPP);
  }

  /* reset the combo boxes if we changed from/to global/app-specific  */

Mike Hearn's avatar
Mike Hearn committed
293
  if ((oldapp && !current_app) || (!oldapp && current_app))
294 295 296
      init_comboboxes(dialog);
  
  update_comboboxes(dialog);
297 298

  set_window_title(dialog);
299 300
}

301
static BOOL list_contains_file(HWND listview, WCHAR *filename)
302
{
303
  LVFINDINFOW find_info = { LVFI_STRING, filename, 0, {0, 0}, 0 };
304 305
  int index;

306
  index = ListView_FindItemW(listview, -1, &find_info);
307 308 309 310

  return (index != -1);
}

311
static void on_add_app_click(HWND dialog)
312
{
313 314 315 316 317 318 319 320 321
  WCHAR filetitle[MAX_PATH];
  WCHAR file[MAX_PATH];
  WCHAR programsFilter[100];
  WCHAR selectExecutableStr[100];
  static const WCHAR pathC[] = { 'c',':','\\',0 };

  OPENFILENAMEW ofn = { sizeof(OPENFILENAMEW),
		       0, /*hInst*/0, 0, NULL, 0, 0, NULL,
		       0, NULL, 0, pathC, 0,
322 323
		       OFN_SHOWHELP | OFN_HIDEREADONLY | OFN_ENABLESIZING,
                       0, 0, NULL, 0, NULL };
324

325 326 327 328 329 330 331
  LoadStringW (GetModuleHandle (NULL), IDS_SELECT_EXECUTABLE, selectExecutableStr,
      sizeof(selectExecutableStr)/sizeof(selectExecutableStr[0]));
  LoadStringW (GetModuleHandle (NULL), IDS_EXECUTABLE_FILTER, programsFilter,
      sizeof(programsFilter)/sizeof(programsFilter[0]));

  ofn.lpstrTitle = selectExecutableStr;
  ofn.lpstrFilter = programsFilter;
332
  ofn.lpstrFileTitle = filetitle;
333
  ofn.lpstrFileTitle[0] = '\0';
334
  ofn.nMaxFileTitle = sizeof(filetitle)/sizeof(filetitle[0]);
335
  ofn.lpstrFile = file;
336
  ofn.lpstrFile[0] = '\0';
337
  ofn.nMaxFile = sizeof(file)/sizeof(file[0]);
338

339
  if (GetOpenFileNameW (&ofn))
340
  {
341 342
      HWND listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);
      int count = ListView_GetItemCount(listview);
343
      WCHAR* new_app;
344
      
345
      if (list_contains_file(listview, filetitle))
346 347
          return;
      
348 349
      new_app = strdupW(filetitle);

350 351
      WINE_TRACE("adding %s\n", wine_dbgstr_w (new_app));
      
352
      add_listview_item(listview, new_app, new_app);
353 354 355 356

      ListView_SetItemState(listview, count, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);

      SetFocus(listview);
357
  }
358
  else WINE_TRACE("user cancelled\n");
359 360
}

361
static void on_remove_app_click(HWND dialog)
362
{
363 364
    HWND listview = GetDlgItem(dialog, IDC_APP_LISTVIEW);
    int selection = get_listview_selection(listview);
365
    char *section = keypath(""); /* AppDefaults\\whatever.exe\\ */
366 367 368
    LVITEMW item;

    item.iItem = selection;
369
    item.iSubItem = 0;
370
    item.mask = LVIF_PARAM;
371

372 373 374
    WINE_TRACE("selection=%d, section=%s\n", selection, section);
    
    assert( selection != 0 ); /* user cannot click this button when "default settings" is selected  */
375

376
    section[strlen(section)] = '\0'; /* remove last backslash  */
377
    set_reg_key(config_key, section, NULL, NULL); /* delete the section  */
378 379
    SendMessage(listview, LVM_GETITEMW, 0, (LPARAM) &item);
    HeapFree (GetProcessHeap(), 0, (void*)item.lParam);
380
    SendMessage(listview, LVM_DELETEITEM, selection, 0);
381
    ListView_SetItemState(listview, selection - 1, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
382

383
    SetFocus(listview);
384 385
    
    SendMessage(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);        
386 387
}

388
static void on_winver_change(HWND dialog)
389
{
390
    int selection = SendDlgItemMessage(dialog, IDC_WINVER, CB_GETCURSEL, 0, 0);
391

392
    if (current_app)
393
    {
394 395 396 397 398 399 400 401 402 403
        if (!selection)
        {
            WINE_TRACE("default selected so removing current setting\n");
            set_reg_key(config_key, keypath(""), "Version", NULL);
        }
        else
        {
            WINE_TRACE("setting Version key to value '%s'\n", win_versions[selection-1].szVersion);
            set_reg_key(config_key, keypath(""), "Version", win_versions[selection-1].szVersion);
        }
404
    }
405
    else /* global version only */
406 407 408
    {
        static const char szKeyProdNT[] = "System\\CurrentControlSet\\Control\\ProductOptions";
        static const char szKeyWindNT[] = "System\\CurrentControlSet\\Control\\Windows";
409
        static const char szKeyEnvNT[]  = "System\\CurrentControlSet\\Control\\Session Manager\\Environment";
410 411
        char Buffer[40];

412
        switch (win_versions[selection].dwPlatformId)
413 414
        {
        case VER_PLATFORM_WIN32_WINDOWS:
415
            snprintf(Buffer, sizeof(Buffer), "%d.%d.%d", win_versions[selection].dwMajorVersion,
416
                     win_versions[selection].dwMinorVersion, win_versions[selection].dwBuildNumber);
417
            set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", Buffer);
418
            set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "SubVersionNumber", win_versions[selection].szCSDVersion);
419 420 421 422 423 424

            set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CSDVersion", NULL);
            set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", NULL);
            set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", NULL);
            set_reg_key(HKEY_LOCAL_MACHINE, szKeyProdNT, "ProductType", NULL);
            set_reg_key(HKEY_LOCAL_MACHINE, szKeyWindNT, "CSDVersion", NULL);
425
            set_reg_key(HKEY_LOCAL_MACHINE, szKeyEnvNT, "OS", NULL);
426
            set_reg_key(config_key, keypath(""), "Version", NULL);
427 428 429
            break;

        case VER_PLATFORM_WIN32_NT:
430
            snprintf(Buffer, sizeof(Buffer), "%d.%d", win_versions[selection].dwMajorVersion,
431
                     win_versions[selection].dwMinorVersion);
432
            set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", Buffer);
433
            set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CSDVersion", win_versions[selection].szCSDVersion);
434
            snprintf(Buffer, sizeof(Buffer), "%d", win_versions[selection].dwBuildNumber);
435
            set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", Buffer);
436
            set_reg_key(HKEY_LOCAL_MACHINE, szKeyProdNT, "ProductType", win_versions[selection].szProductType);
437
            set_reg_key_dword(HKEY_LOCAL_MACHINE, szKeyWindNT, "CSDVersion",
438 439
                              MAKEWORD( win_versions[selection].wServicePackMinor,
                                        win_versions[selection].wServicePackMajor ));
440
            set_reg_key(HKEY_LOCAL_MACHINE, szKeyEnvNT, "OS", "Windows_NT");
441 442 443

            set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", NULL);
            set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "SubVersionNumber", NULL);
444
            set_reg_key(config_key, keypath(""), "Version", NULL);
445 446 447 448 449 450 451 452
            break;

        case VER_PLATFORM_WIN32s:
            set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CSDVersion", NULL);
            set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentVersion", NULL);
            set_reg_key(HKEY_LOCAL_MACHINE, szKeyNT, "CurrentBuildNumber", NULL);
            set_reg_key(HKEY_LOCAL_MACHINE, szKeyProdNT, "ProductType", NULL);
            set_reg_key(HKEY_LOCAL_MACHINE, szKeyWindNT, "CSDVersion", NULL);
453
            set_reg_key(HKEY_LOCAL_MACHINE, szKeyEnvNT, "OS", NULL);
454 455
            set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "VersionNumber", NULL);
            set_reg_key(HKEY_LOCAL_MACHINE, szKey9x, "SubVersionNumber", NULL);
456
            set_reg_key(config_key, keypath(""), "Version", win_versions[selection].szVersion);
457 458
            break;
        }
459
    }
460 461 462

    /* enable the apply button  */
    SendMessage(GetParent(dialog), PSM_CHANGED, (WPARAM) dialog, 0);
463
}
464

465 466 467
INT_PTR CALLBACK
AppDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
468 469
  switch (uMsg)
  {
470
    case WM_INITDIALOG:
471 472 473 474 475 476
        init_appsheet(hDlg);
        break;

    case WM_SHOWWINDOW:
        set_window_title(hDlg);
        break;
477 478 479 480 481

    case WM_NOTIFY:
      switch (((LPNMHDR)lParam)->code)
      {
        case LVN_ITEMCHANGED:
482 483 484 485
            on_selection_change(hDlg, GetDlgItem(hDlg, IDC_APP_LISTVIEW));
            break;
        case PSN_APPLY:
            apply();
486
            SetWindowLongPtr(hDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
487
            break;
488
      }
489
      
490 491 492
      break;
    
    case WM_COMMAND:
Mike Hearn's avatar
Mike Hearn committed
493 494
      switch(HIWORD(wParam))
      {
495
        case CBN_SELCHANGE:
Mike Hearn's avatar
Mike Hearn committed
496 497
          switch(LOWORD(wParam))
          {
498 499 500 501 502
            case IDC_WINVER:
              on_winver_change(hDlg);
              break;
          }
        case BN_CLICKED:
Mike Hearn's avatar
Mike Hearn committed
503 504
          switch(LOWORD(wParam))
          {
505 506 507 508 509 510 511 512
            case IDC_APP_ADDAPP:
              on_add_app_click(hDlg);
              break;
            case IDC_APP_REMOVEAPP:
              on_remove_app_click(hDlg);
              break;
          }
          break;
513
      }
514

515 516
      break;
  }
517
  
518
  return 0;
519
}