control.c 27.7 KB
Newer Older
1 2 3
/* Control Panel management
 *
 * Copyright 2001 Eric Pouech
4
 * Copyright 2008 Owen Rudge
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
19
 */
20 21

#include <assert.h>
22
#include <stdarg.h>
23 24 25 26
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

27
#include "windef.h"
28 29 30
#include "winbase.h"
#include "wingdi.h"
#include "winuser.h"
31
#include "winreg.h"
32 33
#include "wine/winbase16.h"
#include "wownt32.h"
34
#include "wine/debug.h"
35
#include "cpl.h"
36
#include "wine/unicode.h"
37
#include "commctrl.h"
38 39 40

#define NO_SHLWAPI_REG
#include "shlwapi.h"
41

42
#include "cpanel.h"
43 44 45 46
#include "shresdef.h"
#include "shell32_main.h"

#define MAX_STRING_LEN      1024
47

48
WINE_DEFAULT_DEBUG_CHANNEL(shlctrl);
49

50
CPlApplet*	Control_UnloadApplet(CPlApplet* applet)
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
{
    unsigned	i;
    CPlApplet*	next;

    for (i = 0; i < applet->count; i++) {
        if (!applet->info[i].dwSize) continue;
        applet->proc(applet->hWnd, CPL_STOP, i, applet->info[i].lData);
    }
    if (applet->proc) applet->proc(applet->hWnd, CPL_EXIT, 0L, 0L);
    FreeLibrary(applet->hModule);
    next = applet->next;
    HeapFree(GetProcessHeap(), 0, applet);
    return next;
}

66
CPlApplet*	Control_LoadApplet(HWND hWnd, LPCWSTR cmd, CPanel* panel)
67 68 69 70
{
    CPlApplet*	applet;
    unsigned 	i;
    CPLINFO	info;
71
    NEWCPLINFOW newinfo;
72 73 74 75 76 77

    if (!(applet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*applet))))
       return applet;

    applet->hWnd = hWnd;

78 79
    if (!(applet->hModule = LoadLibraryW(cmd))) {
        WARN("Cannot load control panel applet %s\n", debugstr_w(cmd));
80 81 82
	goto theError;
    }
    if (!(applet->proc = (APPLET_PROC)GetProcAddress(applet->hModule, "CPlApplet"))) {
83
        WARN("Not a valid control panel applet %s\n", debugstr_w(cmd));
84 85 86 87 88 89 90 91 92 93 94
	goto theError;
    }
    if (!applet->proc(hWnd, CPL_INIT, 0L, 0L)) {
        WARN("Init of applet has failed\n");
	goto theError;
    }
    if ((applet->count = applet->proc(hWnd, CPL_GETCOUNT, 0L, 0L)) == 0) {
        WARN("No subprogram in applet\n");
	goto theError;
    }

95
    applet = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, applet,
96
			 sizeof(*applet) + (applet->count - 1) * sizeof(NEWCPLINFOW));
97

98
    for (i = 0; i < applet->count; i++) {
99 100 101
       ZeroMemory(&newinfo, sizeof(newinfo));
       newinfo.dwSize = sizeof(NEWCPLINFOA);
       applet->info[i].dwSize = sizeof(NEWCPLINFOW);
102 103 104
       applet->info[i].dwFlags = 0;
       applet->info[i].dwHelpContext = 0;
       applet->info[i].szHelpFile[0] = '\0';
105 106 107 108 109
       /* proc is supposed to return a null value upon success for
	* CPL_INQUIRE and CPL_NEWINQUIRE
	* However, real drivers don't seem to behave like this
	* So, use introspection rather than return value
	*/
110 111 112 113 114 115 116 117 118 119 120 121
       applet->proc(hWnd, CPL_INQUIRE, i, (LPARAM)&info);
       applet->info[i].lData = info.lData;
       if (info.idIcon != CPL_DYNAMIC_RES)
	   applet->info[i].hIcon = LoadIconW(applet->hModule,
					     MAKEINTRESOURCEW(info.idIcon));
       if (info.idName != CPL_DYNAMIC_RES)
	   LoadStringW(applet->hModule, info.idName,
		       applet->info[i].szName, sizeof(applet->info[i].szName) / sizeof(WCHAR));
       if (info.idInfo != CPL_DYNAMIC_RES)
	   LoadStringW(applet->hModule, info.idInfo,
		       applet->info[i].szInfo, sizeof(applet->info[i].szInfo) / sizeof(WCHAR));

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
       /* some broken control panels seem to return incorrect values in CPL_INQUIRE,
          but proper data in CPL_NEWINQUIRE. if we get an empty string or a null
          icon, see what we can get from CPL_NEWINQUIRE */

       if ((applet->info[i].szName == 0) || (lstrlenW(applet->info[i].szName) == 0))
           info.idName = CPL_DYNAMIC_RES;

       /* zero-length szInfo may not be a buggy applet, but it doesn't hurt for us
          to check anyway */

       if ((applet->info[i].szInfo == 0) || (lstrlenW(applet->info[i].szInfo) == 0))
           info.idInfo = CPL_DYNAMIC_RES;

       if (applet->info[i].hIcon == NULL)
           info.idIcon = CPL_DYNAMIC_RES;

138 139 140 141 142 143 144 145 146 147
       if ((info.idIcon == CPL_DYNAMIC_RES) || (info.idName == CPL_DYNAMIC_RES) ||
           (info.idInfo == CPL_DYNAMIC_RES)) {
	   applet->proc(hWnd, CPL_NEWINQUIRE, i, (LPARAM)&newinfo);

	   applet->info[i].dwFlags = newinfo.dwFlags;
	   applet->info[i].dwHelpContext = newinfo.dwHelpContext;
	   applet->info[i].lData = newinfo.lData;
	   if (info.idIcon == CPL_DYNAMIC_RES) {
	       if (!newinfo.hIcon) WARN("couldn't get icon for applet %u\n", i);
	       applet->info[i].hIcon = newinfo.hIcon;
148
	   }
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
	   if (newinfo.dwSize == sizeof(NEWCPLINFOW)) {
	       if (info.idName == CPL_DYNAMIC_RES)
	           memcpy(applet->info[i].szName, newinfo.szName, sizeof(newinfo.szName));
	       if (info.idInfo == CPL_DYNAMIC_RES)
	           memcpy(applet->info[i].szInfo, newinfo.szInfo, sizeof(newinfo.szInfo));
	       memcpy(applet->info[i].szHelpFile, newinfo.szHelpFile, sizeof(newinfo.szHelpFile));
	   } else {
	       if (info.idName == CPL_DYNAMIC_RES)
                   MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szName,
	                               sizeof(((LPNEWCPLINFOA)&newinfo)->szName) / sizeof(CHAR),
			               applet->info[i].szName,
			               sizeof(applet->info[i].szName) / sizeof(WCHAR));
	       if (info.idInfo == CPL_DYNAMIC_RES)
                   MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szInfo,
	                               sizeof(((LPNEWCPLINFOA)&newinfo)->szInfo) / sizeof(CHAR),
			               applet->info[i].szInfo,
			               sizeof(applet->info[i].szInfo) / sizeof(WCHAR));
166 167 168 169 170 171
               MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szHelpFile,
	                           sizeof(((LPNEWCPLINFOA)&newinfo)->szHelpFile) / sizeof(CHAR),
			           applet->info[i].szHelpFile,
			           sizeof(applet->info[i].szHelpFile) / sizeof(WCHAR));
           }
       }
172 173 174 175 176 177 178 179 180 181 182 183
    }

    applet->next = panel->first;
    panel->first = applet;

    return applet;

 theError:
    Control_UnloadApplet(applet);
    return NULL;
}

184
#define IDC_LISTVIEW        1000
185
#define IDC_STATUSBAR       1001
186 187 188 189 190 191 192

#define NUM_COLUMNS            2
#define LISTVIEW_DEFSTYLE   (WS_CHILD | WS_VISIBLE | WS_TABSTOP |\
                             LVS_SORTASCENDING | LVS_AUTOARRANGE | LVS_SINGLESEL)

static BOOL Control_CreateListView (CPanel *panel)
{
193
    RECT ws, sb;
194 195 196 197 198
    WCHAR empty_string[] = {0};
    WCHAR buf[MAX_STRING_LEN];
    LVCOLUMNW lvc;

    /* Create list view */
199
    GetClientRect(panel->hWndStatusBar, &sb);
200 201 202 203
    GetClientRect(panel->hWnd, &ws);

    panel->hWndListView = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW,
                          empty_string, LISTVIEW_DEFSTYLE | LVS_ICON,
204 205 206
                          0, 0, ws.right - ws.left, ws.bottom - ws.top -
                          (sb.bottom - sb.top), panel->hWnd,
                          (HMENU) IDC_LISTVIEW, panel->hInst, NULL);
207 208 209 210 211 212 213 214 215 216

    if (!panel->hWndListView)
        return FALSE;

    /* Create image lists for list view */
    panel->hImageListSmall = ImageList_Create(GetSystemMetrics(SM_CXSMICON),
        GetSystemMetrics(SM_CYSMICON), ILC_MASK, 1, 1);
    panel->hImageListLarge = ImageList_Create(GetSystemMetrics(SM_CXICON),
        GetSystemMetrics(SM_CYICON), ILC_MASK, 1, 1);

217 218
    SendMessageW(panel->hWndListView, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM)panel->hImageListSmall);
    SendMessageW(panel->hWndListView, LVM_SETIMAGELIST, LVSIL_NORMAL, (LPARAM)panel->hImageListLarge);
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244

    /* Create columns for list view */
    lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH;
    lvc.pszText = buf;
    lvc.fmt = LVCFMT_LEFT;

    /* Name column */
    lvc.iSubItem = 0;
    lvc.cx = (ws.right - ws.left) / 3;
    LoadStringW(shell32_hInstance, IDS_CPANEL_NAME, buf, sizeof(buf) / sizeof(buf[0]));

    if (ListView_InsertColumnW(panel->hWndListView, 0, &lvc) == -1)
        return FALSE;

    /* Description column */
    lvc.iSubItem = 1;
    lvc.cx = ((ws.right - ws.left) / 3) * 2;
    LoadStringW(shell32_hInstance, IDS_CPANEL_DESCRIPTION, buf, sizeof(buf) /
        sizeof(buf[0]));

    if (ListView_InsertColumnW(panel->hWndListView, 1, &lvc) == -1)
        return FALSE;

    return(TRUE);
}

245
static void 	 Control_WndProc_Create(HWND hWnd, const CREATESTRUCTW* cs)
246
{
247
   CPanel* panel = cs->lpCreateParams;
248
   HMENU hMenu, hSubMenu;
249
   CPlApplet* applet;
250
   MENUITEMINFOW mii;
251 252
   unsigned int i;
   int menucount, index;
253
   CPlItem *item;
254 255
   LVITEMW lvItem;
   INITCOMMONCONTROLSEX icex;
256
   INT sb_parts;
257
   int itemidx;
258

259
   SetWindowLongPtrW(hWnd, 0, (LONG_PTR)panel);
260
   panel->hWnd = hWnd;
261

262 263
   /* Initialise common control DLL */
   icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
264
   icex.dwICC = ICC_LISTVIEW_CLASSES | ICC_BAR_CLASSES;
265 266
   InitCommonControlsEx(&icex);

267 268 269 270 271 272 273
   /* create the status bar */
   if (!(panel->hWndStatusBar = CreateStatusWindowW(WS_CHILD | WS_VISIBLE | CCS_BOTTOM | SBARS_SIZEGRIP, NULL, hWnd, IDC_STATUSBAR)))
       return;

   sb_parts = -1;
   SendMessageW(panel->hWndStatusBar, SB_SETPARTS, 1, (LPARAM) &sb_parts);

274 275 276 277
   /* create the list view */
   if (!Control_CreateListView(panel))
       return;

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
   hMenu = LoadMenuW(shell32_hInstance, MAKEINTRESOURCEW(MENU_CPANEL));

   /* insert menu items for applets */
   hSubMenu = GetSubMenu(hMenu, 0);
   menucount = 0;

   for (applet = panel->first; applet; applet = applet->next) {
      for (i = 0; i < applet->count; i++) {
         if (!applet->info[i].dwSize)
            continue;

         /* set up a CPlItem for this particular subprogram */
         item = HeapAlloc(GetProcessHeap(), 0, sizeof(CPlItem));

         if (!item)
            continue;

295
         item->applet = applet;
296 297 298 299 300 301 302
         item->id = i;

         mii.cbSize = sizeof(MENUITEMINFOW);
         mii.fMask = MIIM_ID | MIIM_STRING | MIIM_DATA;
         mii.dwTypeData = applet->info[i].szName;
         mii.cch = sizeof(applet->info[i].szName) / sizeof(applet->info[i].szName[0]);
         mii.wID = IDM_CPANEL_APPLET_BASE + menucount;
303
         mii.dwItemData = (ULONG_PTR)item;
304 305

         if (InsertMenuItemW(hSubMenu, menucount, TRUE, &mii)) {
306 307 308 309 310 311 312 313 314 315 316
            /* add the list view item */
            index = ImageList_AddIcon(panel->hImageListLarge, applet->info[i].hIcon);
            ImageList_AddIcon(panel->hImageListSmall, applet->info[i].hIcon);

            lvItem.mask = LVIF_IMAGE | LVIF_TEXT | LVIF_PARAM;
            lvItem.iItem = menucount;
            lvItem.iSubItem = 0;
            lvItem.pszText = applet->info[i].szName;
            lvItem.iImage = index;
            lvItem.lParam = (LPARAM) item;

317
            itemidx = ListView_InsertItemW(panel->hWndListView, &lvItem);
318 319

            /* add the description */
320
            ListView_SetItemTextW(panel->hWndListView, itemidx, 1,
321 322 323 324 325
                applet->info[i].szInfo);

            /* update menu bar, increment count */
            DrawMenuBar(hWnd);
            menucount++;
326 327 328 329 330 331 332 333 334 335 336 337
         }
      }
   }

   panel->total_subprogs = menucount;

   /* check the "large items" icon in the View menu */
   hSubMenu = GetSubMenu(hMenu, 1);
   CheckMenuRadioItem(hSubMenu, FCIDM_SHVIEW_BIGICON, FCIDM_SHVIEW_REPORTVIEW,
      FCIDM_SHVIEW_BIGICON, MF_BYCOMMAND);

   SetMenu(hWnd, hMenu);
338 339
}

340 341 342 343
static void Control_FreeCPlItems(HWND hWnd, CPanel *panel)
{
    HMENU hMenu, hSubMenu;
    MENUITEMINFOW mii;
344
    unsigned int i;
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369

    /* get the File menu */
    hMenu = GetMenu(hWnd);

    if (!hMenu)
        return;

    hSubMenu = GetSubMenu(hMenu, 0);

    if (!hSubMenu)
        return;

    /* loop and free the item data */
    for (i = IDM_CPANEL_APPLET_BASE; i <= IDM_CPANEL_APPLET_BASE + panel->total_subprogs; i++)
    {
        mii.cbSize = sizeof(MENUITEMINFOW);
        mii.fMask = MIIM_DATA;

        if (!GetMenuItemInfoW(hSubMenu, i, FALSE, &mii))
            continue;

        HeapFree(GetProcessHeap(), 0, (LPVOID) mii.dwItemData);
    }
}

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
static void Control_UpdateListViewStyle(CPanel *panel, UINT style, UINT id)
{
    HMENU hMenu, hSubMenu;

    SetWindowLongW(panel->hWndListView, GWL_STYLE, LISTVIEW_DEFSTYLE | style);

    /* update the menu */
    hMenu = GetMenu(panel->hWnd);
    hSubMenu = GetSubMenu(hMenu, 1);

    CheckMenuRadioItem(hSubMenu, FCIDM_SHVIEW_BIGICON, FCIDM_SHVIEW_REPORTVIEW,
        id, MF_BYCOMMAND);
}

static CPlItem* Control_GetCPlItem_From_MenuID(HWND hWnd, UINT id)
{
    HMENU hMenu, hSubMenu;
    MENUITEMINFOW mii;

    /* retrieve the CPlItem structure from the menu item data */
    hMenu = GetMenu(hWnd);

    if (!hMenu)
        return NULL;

    hSubMenu = GetSubMenu(hMenu, 0);

    if (!hSubMenu)
        return NULL;

    mii.cbSize = sizeof(MENUITEMINFOW);
    mii.fMask = MIIM_DATA;

    if (!GetMenuItemInfoW(hSubMenu, id, FALSE, &mii))
        return NULL;

    return (CPlItem *) mii.dwItemData;
}

static CPlItem* Control_GetCPlItem_From_ListView(CPanel *panel)
{
    LVITEMW lvItem;
    int selitem;

    selitem = SendMessageW(panel->hWndListView, LVM_GETNEXTITEM, -1, LVNI_FOCUSED
        | LVNI_SELECTED);

    if (selitem != -1)
    {
        lvItem.iItem = selitem;
        lvItem.mask = LVIF_PARAM;

        if (SendMessageW(panel->hWndListView, LVM_GETITEMW, 0, (LPARAM) &lvItem))
            return (CPlItem *) lvItem.lParam;
    }

    return NULL;
}

429
static LRESULT WINAPI	Control_WndProc(HWND hWnd, UINT wMsg,
430 431
					WPARAM lParam1, LPARAM lParam2)
{
432
   CPanel*	panel = (CPanel*)GetWindowLongPtrW(hWnd, 0);
433 434 435 436

   if (panel || wMsg == WM_CREATE) {
      switch (wMsg) {
      case WM_CREATE:
437
	 Control_WndProc_Create(hWnd, (CREATESTRUCTW*)lParam2);
438 439
	 return 0;
      case WM_DESTROY:
440 441 442 443 444
         {
	    CPlApplet*	applet = panel->first;
	    while (applet)
	       applet = Control_UnloadApplet(applet);
         }
445
         Control_FreeCPlItems(hWnd, panel);
446
         PostQuitMessage(0);
447
	 break;
448 449 450 451 452 453 454
      case WM_COMMAND:
         switch (LOWORD(lParam1))
         {
             case IDM_CPANEL_EXIT:
                 SendMessageW(hWnd, WM_CLOSE, 0, 0);
                 return 0;

455 456 457 458 459 460 461 462 463 464 465
             case IDM_CPANEL_ABOUT:
                 {
                     WCHAR appName[MAX_STRING_LEN];

                     LoadStringW(shell32_hInstance, IDS_CPANEL_TITLE, appName,
                         sizeof(appName) / sizeof(appName[0]));
                     ShellAboutW(hWnd, appName, NULL, NULL);

                     return 0;
                 }

466
             case FCIDM_SHVIEW_BIGICON:
467 468 469
                 Control_UpdateListViewStyle(panel, LVS_ICON, FCIDM_SHVIEW_BIGICON);
                 return 0;

470
             case FCIDM_SHVIEW_SMALLICON:
471 472 473
                 Control_UpdateListViewStyle(panel, LVS_SMALLICON, FCIDM_SHVIEW_SMALLICON);
                 return 0;

474
             case FCIDM_SHVIEW_LISTVIEW:
475 476 477
                 Control_UpdateListViewStyle(panel, LVS_LIST, FCIDM_SHVIEW_LISTVIEW);
                 return 0;

478
             case FCIDM_SHVIEW_REPORTVIEW:
479
                 Control_UpdateListViewStyle(panel, LVS_REPORT, FCIDM_SHVIEW_REPORTVIEW);
480 481 482 483 484 485 486
                 return 0;

             default:
                 /* check if this is an applet */
                 if ((LOWORD(lParam1) >= IDM_CPANEL_APPLET_BASE) &&
                     (LOWORD(lParam1) <= IDM_CPANEL_APPLET_BASE + panel->total_subprogs))
                 {
487
                     CPlItem *item = Control_GetCPlItem_From_MenuID(hWnd, LOWORD(lParam1));
488 489 490 491 492 493 494 495 496 497 498 499 500

                     /* execute the applet if item is valid */
                     if (item)
                         item->applet->proc(item->applet->hWnd, CPL_DBLCLK, item->id,
                             item->applet->info[item->id].lData);

                     return 0;
                 }

                 break;
         }

         break;
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522

      case WM_NOTIFY:
      {
          LPNMHDR nmh = (LPNMHDR) lParam2;

          switch (nmh->idFrom)
          {
              case IDC_LISTVIEW:
                  switch (nmh->code)
                  {
                      case NM_RETURN:
                      case NM_DBLCLK:
                      {
                          CPlItem *item = Control_GetCPlItem_From_ListView(panel);

                          /* execute the applet if item is valid */
                          if (item)
                              item->applet->proc(item->applet->hWnd, CPL_DBLCLK,
                                  item->id, item->applet->info[item->id].lData);

                          return 0;
                      }
523 524 525 526 527 528 529 530
                      case LVN_ITEMCHANGED:
                      {
                          CPlItem *item = Control_GetCPlItem_From_ListView(panel);

                          /* update the status bar if item is valid */
                          if (item)
                              SetWindowTextW(panel->hWndStatusBar,
                                  item->applet->info[item->id].szInfo);
531 532
                          else
                              SetWindowTextW(panel->hWndStatusBar, NULL);
533 534 535

                          return 0;
                      }
536 537 538 539 540 541 542 543
                  }

                  break;
          }

          break;
      }

544 545 546 547 548 549 550 551 552 553 554
      case WM_MENUSELECT:
          /* check if this is an applet */
          if ((LOWORD(lParam1) >= IDM_CPANEL_APPLET_BASE) &&
              (LOWORD(lParam1) <= IDM_CPANEL_APPLET_BASE + panel->total_subprogs))
          {
              CPlItem *item = Control_GetCPlItem_From_MenuID(hWnd, LOWORD(lParam1));

              /* update the status bar if item is valid */
              if (item)
                  SetWindowTextW(panel->hWndStatusBar, item->applet->info[item->id].szInfo);
          }
555 556 557 558 559 560 561 562 563 564 565 566
          else if ((HIWORD(lParam1) == 0xFFFF) && (lParam2 == 0))
          {
              /* reset status bar description to that of the selected icon */
              CPlItem *item = Control_GetCPlItem_From_ListView(panel);

              if (item)
                  SetWindowTextW(panel->hWndStatusBar, item->applet->info[item->id].szInfo);
              else
                  SetWindowTextW(panel->hWndStatusBar, NULL);

              return 0;
          }
567 568 569 570 571
          else
              SetWindowTextW(panel->hWndStatusBar, NULL);

          return 0;

572 573
      case WM_SIZE:
      {
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
          HDWP hdwp;
          RECT sb;

          hdwp = BeginDeferWindowPos(2);

          if (hdwp == NULL)
              break;

          GetClientRect(panel->hWndStatusBar, &sb);

          hdwp = DeferWindowPos(hdwp, panel->hWndListView, NULL, 0, 0,
              LOWORD(lParam2), HIWORD(lParam2) - (sb.bottom - sb.top),
              SWP_NOZORDER | SWP_NOMOVE);

          if (hdwp == NULL)
              break;

          hdwp = DeferWindowPos(hdwp, panel->hWndStatusBar, NULL, 0, 0,
              LOWORD(lParam2), LOWORD(lParam1), SWP_NOZORDER | SWP_NOMOVE);
593

594 595
          if (hdwp != NULL)
              EndDeferWindowPos(hdwp);
596 597

          return 0;
598
      }
599
     }
600 601
   }

602
   return DefWindowProcW(hWnd, wMsg, lParam1, lParam2);
603 604 605 606
}

static void    Control_DoInterface(CPanel* panel, HWND hWnd, HINSTANCE hInst)
{
607
    WNDCLASSW	wc;
608
    MSG		msg;
609
    WCHAR appName[MAX_STRING_LEN];
610 611 612
    const WCHAR className[] = {'S','h','e','l','l','_','C','o','n','t','r','o',
        'l','_','W','n','d','C','l','a','s','s',0};

613 614
    LoadStringW(shell32_hInstance, IDS_CPANEL_TITLE, appName, sizeof(appName) / sizeof(appName[0]));

615 616 617 618
    wc.style = CS_HREDRAW|CS_VREDRAW;
    wc.lpfnWndProc = Control_WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = sizeof(CPlApplet*);
619
    wc.hInstance = panel->hInst = hInst;
620
    wc.hIcon = 0;
621
    wc.hCursor = LoadCursorW( 0, (LPWSTR)IDC_ARROW );
622 623
    wc.hbrBackground = GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = NULL;
624
    wc.lpszClassName = className;
625

626
    if (!RegisterClassW(&wc)) return;
627

628
    CreateWindowExW(0, wc.lpszClassName, appName,
629 630 631
		    WS_OVERLAPPEDWINDOW | WS_VISIBLE,
		    CW_USEDEFAULT, CW_USEDEFAULT,
		    CW_USEDEFAULT, CW_USEDEFAULT,
632
		    hWnd, NULL, hInst, panel);
633
    if (!panel->hWnd) return;
634

635
    while (GetMessageW(&msg, panel->hWnd, 0, 0)) {
636
        TranslateMessage(&msg);
637
        DispatchMessageW(&msg);
638 639 640
    }
}

641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
static void Control_RegisterRegistryApplets(HWND hWnd, CPanel *panel, HKEY hkey_root, LPCWSTR szRepPath)
{
    WCHAR name[MAX_PATH];
    WCHAR value[MAX_PATH];
    HKEY hkey;

    if (RegOpenKeyW(hkey_root, szRepPath, &hkey) == ERROR_SUCCESS)
    {
        int idx = 0;

        for(;; ++idx)
        {
            DWORD nameLen = MAX_PATH;
            DWORD valueLen = MAX_PATH;

656
            if (RegEnumValueW(hkey, idx, name, &nameLen, NULL, NULL, (LPBYTE)value, &valueLen) != ERROR_SUCCESS)
657 658 659 660 661 662 663 664
                break;

            Control_LoadApplet(hWnd, value, panel);
        }
        RegCloseKey(hkey);
    }
}

665 666 667
static	void	Control_DoWindow(CPanel* panel, HWND hWnd, HINSTANCE hInst)
{
    HANDLE		h;
668 669 670
    WIN32_FIND_DATAW	fd;
    WCHAR		buffer[MAX_PATH];
    static const WCHAR wszAllCpl[] = {'*','.','c','p','l',0};
671 672 673
    static const WCHAR wszRegPath[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t',
            '\\','W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',
            '\\','C','o','n','t','r','o','l',' ','P','a','n','e','l','\\','C','p','l','s',0};
674 675
    WCHAR *p;

676
    /* first add .cpl files in the system directory */
677 678 679 680
    GetSystemDirectoryW( buffer, MAX_PATH );
    p = buffer + strlenW(buffer);
    *p++ = '\\';
    lstrcpyW(p, wszAllCpl);
681

682
    if ((h = FindFirstFileW(buffer, &fd)) != INVALID_HANDLE_VALUE) {
683
        do {
684
	   lstrcpyW(p, fd.cFileName);
685
	   Control_LoadApplet(hWnd, buffer, panel);
686
	} while (FindNextFileW(h, &fd));
687 688 689
	FindClose(h);
    }

690 691 692 693
    /* now check for cpls in the registry */
    Control_RegisterRegistryApplets(hWnd, panel, HKEY_LOCAL_MACHINE, wszRegPath);
    Control_RegisterRegistryApplets(hWnd, panel, HKEY_CURRENT_USER, wszRegPath);

694
    Control_DoInterface(panel, hWnd, hInst);
695 696
}

697
static	void	Control_DoLaunch(CPanel* panel, HWND hWnd, LPCWSTR wszCmd)
698 699 700 701 702 703
   /* forms to parse:
    *	foo.cpl,@sp,str
    *	foo.cpl,@sp
    *	foo.cpl,,str
    *	foo.cpl @sp
    *	foo.cpl str
704
    *   "a path\foo.cpl"
705 706
    */
{
707 708 709 710 711
    LPWSTR	buffer;
    LPWSTR	beg = NULL;
    LPWSTR	end;
    WCHAR	ch;
    LPWSTR       ptr;
712 713
    signed 	sp = -1;
    LPWSTR	extraPmtsBuf = NULL;
714
    LPWSTR	extraPmts = NULL;
715
    int        quoted = 0;
716

717
    buffer = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(wszCmd) + 1) * sizeof(*wszCmd));
718 719
    if (!buffer) return;

720
    end = lstrcpyW(buffer, wszCmd);
721 722

    for (;;) {
723
        ch = *end;
724
        if (ch == '"') quoted = !quoted;
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
        if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
            *end = '\0';
            if (beg) {
                if (*beg == '@') {
                    sp = atoiW(beg + 1);
                } else if (*beg == '\0') {
                    sp = -1;
                } else {
                    extraPmtsBuf = beg;
                }
            }
            if (ch == '\0') break;
            beg = end + 1;
            if (ch == ' ') while (end[1] == ' ') end++;
        }
        end++;
741
    }
742
    while ((ptr = StrChrW(buffer, '"')))
743
	memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
744

745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
    /* now check for any quotes in extraPmtsBuf and remove */
    if (extraPmtsBuf != NULL)
    {
        beg = end = extraPmtsBuf;
        quoted = 0;

        for (;;) {
            ch = *end;
            if (ch == '"') quoted = !quoted;
            if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
                *end = '\0';
                if (beg) {
                    if (*beg != '\0') {
                        extraPmts = beg;
                    }
                }
                if (ch == '\0') break;
		    beg = end + 1;
                if (ch == ' ') while (end[1] == ' ') end++;
            }
            end++;
        }

        while ((ptr = StrChrW(extraPmts, '"')))
            memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));

        if (extraPmts == NULL)
            extraPmts = extraPmtsBuf;
    }

775
    TRACE("cmd %s, extra %s, sp %d\n", debugstr_w(buffer), debugstr_w(extraPmts), sp);
776

777 778 779
    Control_LoadApplet(hWnd, buffer, panel);

    if (panel->first) {
780
        CPlApplet* applet = panel->first;
781

782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
        assert(applet && applet->next == NULL);

        /* we've been given a textual parameter (or none at all) */
        if (sp == -1) {
            while ((++sp) != applet->count) {
                if (applet->info[sp].dwSize) {
                    TRACE("sp %d, name %s\n", sp, debugstr_w(applet->info[sp].szName));

                    if (StrCmpIW(extraPmts, applet->info[sp].szName) == 0)
                        break;
                }
            }
        }

        if (sp >= applet->count) {
            WARN("Out of bounds (%u >= %u), setting to 0\n", sp, applet->count);
            sp = 0;
        }

        if (applet->info[sp].dwSize) {
            if (!applet->proc(applet->hWnd, CPL_STARTWPARMSA, sp, (LPARAM)extraPmts))
                applet->proc(applet->hWnd, CPL_DBLCLK, sp, applet->info[sp].lData);
        }

        Control_UnloadApplet(applet);
807
    }
808

809 810 811 812
    HeapFree(GetProcessHeap(), 0, buffer);
}

/*************************************************************************
813
 * Control_RunDLLW			[SHELL32.@]
814 815
 *
 */
816
void WINAPI Control_RunDLLW(HWND hWnd, HINSTANCE hInst, LPCWSTR cmd, DWORD nCmdShow)
817 818 819
{
    CPanel	panel;

820
    TRACE("(%p, %p, %s, 0x%08x)\n",
821
	  hWnd, hInst, debugstr_w(cmd), nCmdShow);
822 823 824 825 826 827 828 829 830 831

    memset(&panel, 0, sizeof(panel));

    if (!cmd || !*cmd) {
        Control_DoWindow(&panel, hWnd, hInst);
    } else {
        Control_DoLaunch(&panel, hWnd, cmd);
    }
}

832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
/*************************************************************************
 * Control_RunDLLA			[SHELL32.@]
 *
 */
void WINAPI Control_RunDLLA(HWND hWnd, HINSTANCE hInst, LPCSTR cmd, DWORD nCmdShow)
{
    DWORD len = MultiByteToWideChar(CP_ACP, 0, cmd, -1, NULL, 0 );
    LPWSTR wszCmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
    if (wszCmd && MultiByteToWideChar(CP_ACP, 0, cmd, -1, wszCmd, len ))
    {
        Control_RunDLLW(hWnd, hInst, wszCmd, nCmdShow);
    }
    HeapFree(GetProcessHeap(), 0, wszCmd);
}

847
/*************************************************************************
848
 * Control_FillCache_RunDLLW			[SHELL32.@]
849 850
 *
 */
851
HRESULT WINAPI Control_FillCache_RunDLLW(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
852
{
853
    FIXME("%p %p 0x%08x 0x%08x stub\n", hWnd, hModule, w, x);
854 855 856
    return 0;
}

857
/*************************************************************************
858
 * Control_FillCache_RunDLLA			[SHELL32.@]
859 860
 *
 */
861
HRESULT WINAPI Control_FillCache_RunDLLA(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
862
{
863
    return Control_FillCache_RunDLLW(hWnd, hModule, w, x);
864 865 866
}


867 868
/*************************************************************************
 * RunDLL_CallEntry16				[SHELL32.122]
869
 * the name is probably wrong
870
 */
871 872
void WINAPI RunDLL_CallEntry16( DWORD proc, HWND hwnd, HINSTANCE inst,
                                LPCSTR cmdline, INT cmdshow )
873
{
874 875 876
    WORD args[5];
    SEGPTR cmdline_seg;

877
    TRACE( "proc %x hwnd %p inst %p cmdline %s cmdshow %d\n",
878 879 880 881 882 883 884 885 886 887
           proc, hwnd, inst, debugstr_a(cmdline), cmdshow );

    cmdline_seg = MapLS( cmdline );
    args[4] = HWND_16(hwnd);
    args[3] = MapHModuleLS(inst);
    args[2] = SELECTOROF(cmdline_seg);
    args[1] = OFFSETOF(cmdline_seg);
    args[0] = cmdshow;
    WOWCallback16Ex( proc, WCB16_PASCAL, sizeof(args), args, NULL );
    UnMapLS( cmdline_seg );
888
}
889

890 891 892
/*************************************************************************
 * CallCPLEntry16				[SHELL32.166]
 *
893 894 895 896 897 898
 * called by desk.cpl on "Advanced" with:
 * hMod("DeskCp16.Dll"), pFunc("CplApplet"), 0, 1, 0xc, 0
 *
 */
DWORD WINAPI CallCPLEntry16(HMODULE hMod, FARPROC pFunc, DWORD dw3, DWORD dw4, DWORD dw5, DWORD dw6)
{
899
    FIXME("(%p, %p, %08x, %08x, %08x, %08x): stub.\n", hMod, pFunc, dw3, dw4, dw5, dw6);
900 901
    return 0x0deadbee;
}