listview.c 18.9 KB
Newer Older
1 2 3 4
/*
 * Regedit listviews
 *
 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
5
 * Copyright (C) 2008 Alexander N. Sørnes <alex@thehandofagony.com>
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
20 21 22 23 24 25 26 27 28
 */

#include <windows.h>
#include <commctrl.h>
#include <stdlib.h>
#include <stdio.h>

#include "main.h"

29
#include "wine/unicode.h"
30 31 32
static INT Image_String;
static INT Image_Binary;

33 34 35
typedef struct tagLINE_INFO
{
    DWORD dwValType;
36
    LPWSTR name;
37 38 39
    void* val;
    size_t val_len;
} LINE_INFO;
40 41 42 43 44 45

/*******************************************************************************
 * Global and Local Variables:
 */

static WNDPROC g_orgListWndProc;
46
static DWORD g_columnToSort = ~0U;
47
static BOOL  g_invertSort = FALSE;
48 49
static LPWSTR g_valueName;
static LPWSTR g_currentPath;
50
static HKEY g_currentRootKey;
51
static WCHAR g_szValueNotSet[64];
52 53 54 55 56

#define MAX_LIST_COLUMNS (IDS_LIST_COLUMN_LAST - IDS_LIST_COLUMN_FIRST + 1)
static int default_column_widths[MAX_LIST_COLUMNS] = { 200, 175, 400 };
static int column_alignment[MAX_LIST_COLUMNS] = { LVCFMT_LEFT, LVCFMT_LEFT, LVCFMT_LEFT };

57
LPWSTR GetItemText(HWND hwndLV, UINT item)
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
{
    LPWSTR newStr, curStr;
    unsigned int maxLen = 128;

    curStr = HeapAlloc(GetProcessHeap(), 0, maxLen * sizeof(WCHAR));
    if (!curStr) return NULL;
    if (item == 0) { /* first item is ALWAYS a default */
        HeapFree(GetProcessHeap(), 0, curStr);
        return NULL;
    }
    do {
        ListView_GetItemTextW(hwndLV, item, 0, curStr, maxLen * sizeof(WCHAR));
        if (lstrlenW(curStr) < maxLen - 1) return curStr;
        newStr = HeapReAlloc(GetProcessHeap(), 0, curStr, maxLen * 2 * sizeof(WCHAR));
        if (!newStr) break;
        curStr = newStr;
        maxLen *= 2;
    } while (TRUE);
    HeapFree(GetProcessHeap(), 0, curStr);
    return NULL;
}

LPCWSTR GetValueName(HWND hwndLV)
81
{
82
    INT item;
83

84
    if (g_valueName != LPSTR_TEXTCALLBACKW)
85
        HeapFree(GetProcessHeap(), 0,  g_valueName);
86
    g_valueName = NULL;
87 88 89

    item = ListView_GetNextItem(hwndLV, -1, LVNI_FOCUSED);
    if (item == -1) return NULL;
90

91
    g_valueName = GetItemText(hwndLV, item);
92 93 94

    return g_valueName;
}
95

96
/* convert '\0' separated string list into ',' separated string list */
97
static void MakeMULTISZDisplayable(LPWSTR multi)
98 99 100 101 102 103 104 105 106 107 108 109 110
{
    do
    {
        for (; *multi; multi++)
            ;
        if (*(multi+1))
        {
            *multi = ',';
            multi++;
        }
    } while (*multi);
}

111 112 113
/*******************************************************************************
 * Local module support methods
 */
114
static void AddEntryToList(HWND hwndLV, LPWSTR Name, DWORD dwValType,
115
    void* ValBuf, DWORD dwCount, BOOL bHighlight)
116
{
117
    LINE_INFO* linfo;
118
    LVITEMW item;
119 120
    int index;

121 122 123
    linfo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINE_INFO) + dwCount);
    linfo->dwValType = dwValType;
    linfo->val_len = dwCount;
124
    CopyMemory(&linfo[1], ValBuf, dwCount);
125 126
    
    if (Name)
127 128 129 130 131
    {
        linfo->name = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(Name) + 1) * sizeof(WCHAR));
        lstrcpyW(linfo->name, Name);
    } else
    {
132
        linfo->name = NULL;
133
    }
134

135
    item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE | LVIF_IMAGE;
136
    item.iItem = ListView_GetItemCount(hwndLV);/*idx;  */
137 138
    item.iSubItem = 0;
    item.state = 0;
139
    item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
140 141
    item.pszText = Name ? Name : LPSTR_TEXTCALLBACKW;
    item.cchTextMax = Name ? lstrlenW(item.pszText) : 0;
142 143 144
    if (bHighlight) {
        item.stateMask = item.state = LVIS_FOCUSED | LVIS_SELECTED;
    }
145 146 147 148 149 150 151 152 153 154 155
    switch (dwValType)
    {
    case REG_SZ:
    case REG_EXPAND_SZ:
    case REG_MULTI_SZ:
        item.iImage = Image_String;
        break;
    default:
        item.iImage = Image_Binary;
        break;
    }
156 157
    item.lParam = (LPARAM)linfo;

158 159 160 161
#if (_WIN32_IE >= 0x0300)
    item.iIndent = 0;
#endif

162
    index = ListView_InsertItemW(hwndLV, &item);
163 164 165 166
    if (index != -1) {
        switch (dwValType) {
        case REG_SZ:
        case REG_EXPAND_SZ:
167
            if (ValBuf) {
168
                ListView_SetItemTextW(hwndLV, index, 2, ValBuf);
169
            } else {
170
                ListView_SetItemTextW(hwndLV, index, 2, g_szValueNotSet);
171
            }
172
            break;
173
        case REG_DWORD: {
174 175 176 177
                WCHAR buf[64];
                WCHAR format[] = {'0','x','%','0','8','x',' ','(','%','u',')',0};
                wsprintfW(buf, format, *(DWORD*)ValBuf, *(DWORD*)ValBuf);
                ListView_SetItemTextW(hwndLV, index, 2, buf);
178 179
            }
            break;
180
        case REG_BINARY: {
181
                unsigned int i;
182
                LPBYTE pData = ValBuf;
183 184
                LPWSTR strBinary = HeapAlloc(GetProcessHeap(), 0, dwCount * sizeof(WCHAR) * 3 + sizeof(WCHAR));
                WCHAR format[] = {'%','0','2','X',' ',0};
185
                for (i = 0; i < dwCount; i++)
186
                    wsprintfW( strBinary + i*3, format, pData[i] );
187
                strBinary[dwCount * 3] = 0;
188
                ListView_SetItemTextW(hwndLV, index, 2, strBinary);
189 190 191
                HeapFree(GetProcessHeap(), 0, strBinary);
            }
            break;
192 193
        case REG_MULTI_SZ:
            MakeMULTISZDisplayable(ValBuf);
194
            ListView_SetItemTextW(hwndLV, index, 2, ValBuf);
195
            break;
196
        default:
197
          {
198 199 200
            WCHAR szText[128];
            LoadStringW(hInst, IDS_REGISTRY_VALUE_CANT_DISPLAY, szText, COUNT_OF(szText));
            ListView_SetItemTextW(hwndLV, index, 2, szText);
201
            break;
202
          }
203 204 205 206
        }
    }
}

207 208 209 210 211 212 213 214 215 216 217
static BOOL InitListViewImageList(HWND hWndListView)
{
    HIMAGELIST himl;
    HICON hicon;
    INT cx = GetSystemMetrics(SM_CXSMICON);
    INT cy = GetSystemMetrics(SM_CYSMICON);

    himl = ImageList_Create(cx, cy, ILC_MASK, 0, 2);
    if (!himl)
        return FALSE;

218
    hicon = LoadImageW(hInst, MAKEINTRESOURCEW(IDI_STRING),
219 220 221
        IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
    Image_String = ImageList_AddIcon(himl, hicon);

222
    hicon = LoadImageW(hInst, MAKEINTRESOURCEW(IDI_BIN),
223 224 225
        IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
    Image_Binary = ImageList_AddIcon(himl, hicon);

226
    SendMessageW( hWndListView, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM) himl );
227 228 229 230 231 232 233 234

    /* fail if some of the icons failed to load */
    if (ImageList_GetImageCount(himl) < 2)
        return FALSE;

    return TRUE;
}

235
static BOOL CreateListColumns(HWND hWndListView)
236
{
237
    WCHAR szText[50];
238
    int index;
239
    LVCOLUMNW lvC;
240 241 242 243 244 245 246 247 248 249

    /* Create columns. */
    lvC.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
    lvC.pszText = szText;

    /* Load the column labels from the resource file. */
    for (index = 0; index < MAX_LIST_COLUMNS; index++) {
        lvC.iSubItem = index;
        lvC.cx = default_column_widths[index];
        lvC.fmt = column_alignment[index];
250 251
        LoadStringW(hInst, IDS_LIST_COLUMN_FIRST + index, szText, sizeof(szText)/sizeof(WCHAR));
        if (ListView_InsertColumnW(hWndListView, index, &lvC) == -1) return FALSE;
252
    }
253
    return TRUE;
254 255 256 257
}

/* OnGetDispInfo - processes the LVN_GETDISPINFO notification message.  */

258
static void OnGetDispInfo(NMLVDISPINFOW* plvdi)
259
{
260 261
    static WCHAR buffer[200];
    static WCHAR reg_szT[]               = {'R','E','G','_','S','Z',0},
262 263 264 265 266 267 268 269 270 271
                 reg_expand_szT[]        = {'R','E','G','_','E','X','P','A','N','D','_','S','Z',0},
                 reg_binaryT[]           = {'R','E','G','_','B','I','N','A','R','Y',0},
                 reg_dwordT[]            = {'R','E','G','_','D','W','O','R','D',0},
                 reg_dword_big_endianT[] = {'R','E','G','_','D','W','O','R','D','_',
                                            'B','I','G','_','E','N','D','I','A','N',0},
                 reg_multi_szT[]         = {'R','E','G','_','M','U','L','T','I','_','S','Z',0},
                 reg_linkT[]             = {'R','E','G','_','L','I','N','K',0},
                 reg_resource_listT[]    = {'R','E','G','_','R','E','S','O','U','R','C','E','_','L','I','S','T',0},
                 reg_noneT[]             = {'R','E','G','_','N','O','N','E',0},
                 emptyT[]                = {0};
272 273 274 275 276 277

    plvdi->item.pszText = NULL;
    plvdi->item.cchTextMax = 0;

    switch (plvdi->item.iSubItem) {
    case 0:
278
        plvdi->item.pszText = g_pszDefaultValueName;
279 280
        break;
    case 1:
281
        switch (((LINE_INFO*)plvdi->item.lParam)->dwValType) {
282
        case REG_SZ:
283
            plvdi->item.pszText = reg_szT;
284 285
            break;
        case REG_EXPAND_SZ:
286
            plvdi->item.pszText = reg_expand_szT;
287 288
            break;
        case REG_BINARY:
289
            plvdi->item.pszText = reg_binaryT;
290 291
            break;
        case REG_DWORD:
292
            plvdi->item.pszText = reg_dwordT;
293 294
            break;
        case REG_DWORD_BIG_ENDIAN:
295
            plvdi->item.pszText = reg_dword_big_endianT;
296 297
            break;
        case REG_MULTI_SZ:
298
            plvdi->item.pszText = reg_multi_szT;
299 300
            break;
        case REG_LINK:
301
            plvdi->item.pszText = reg_linkT;
302 303
            break;
        case REG_RESOURCE_LIST:
304
            plvdi->item.pszText = reg_resource_listT;
305 306
            break;
        case REG_NONE:
307
            plvdi->item.pszText = reg_noneT;
308 309
            break;
        default:
310
          {
311 312 313
            WCHAR szUnknownFmt[64];
            LoadStringW(hInst, IDS_REGISTRY_UNKNOWN_TYPE, szUnknownFmt, COUNT_OF(szUnknownFmt));
            wsprintfW(buffer, szUnknownFmt, plvdi->item.lParam);
314 315
            plvdi->item.pszText = buffer;
            break;
316
          }
317 318 319
        }
        break;
    case 2:
320
        plvdi->item.pszText = g_szValueNotSet;
321 322
        break;
    case 3:
323
        plvdi->item.pszText = emptyT;
324 325 326 327 328 329
        break;
    }
}

static int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
330 331 332
    LINE_INFO*l, *r;
    l = (LINE_INFO*)lParam1;
    r = (LINE_INFO*)lParam2;
333 334
    if (!l->name) return -1;
    if (!r->name) return +1;
335
        
336
    if (g_columnToSort == ~0U)
337 338 339 340
        g_columnToSort = 0;
    
    if (g_columnToSort == 1 && l->dwValType != r->dwValType)
        return g_invertSort ? (int)r->dwValType - (int)l->dwValType : (int)l->dwValType - (int)r->dwValType;
341
    if (g_columnToSort == 2) {
342 343
        /* FIXME: Sort on value */
    }
344
    return g_invertSort ? lstrcmpiW(r->name, l->name) : lstrcmpiW(l->name, r->name);
345 346
}

347
HWND StartValueRename(HWND hwndLV)
348 349 350
{
    int item;

351
    item = ListView_GetNextItem(hwndLV, -1, LVNI_FOCUSED | LVNI_SELECTED);
352 353 354 355
    if (item < 1) { /* cannot rename default key */
        MessageBeep(MB_ICONHAND);
        return 0;
    }
356
    return ListView_EditLabel(hwndLV, item);
357
}
358 359 360

static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
361 362 363 364
    switch (LOWORD(wParam)) {
        /*    case ID_FILE_OPEN: */
        /*        break; */
    default:
365
        return FALSE;
366 367
    }
    return TRUE;
368 369 370 371
}

static LRESULT CALLBACK ListWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
372 373
    switch (message) {
    case WM_COMMAND:
374 375 376
        if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
            return CallWindowProc(g_orgListWndProc, hWnd, message, wParam, lParam);
        }
377
        break;
378
    case WM_NOTIFY_REFLECT:
379
        switch (((LPNMHDR)lParam)->code) {
380
	
381 382
        case LVN_BEGINLABELEDITW:
            if (!((NMLVDISPINFOW *)lParam)->item.iItem)
383 384
                return 1;
            return 0;
385 386
        case LVN_GETDISPINFOW:
            OnGetDispInfo((NMLVDISPINFOW*)lParam);
387
            break;
388 389 390
        case LVN_COLUMNCLICK:
            if (g_columnToSort == ((LPNMLISTVIEW)lParam)->iSubItem)
                g_invertSort = !g_invertSort;
391
            else {
392 393 394 395
                g_columnToSort = ((LPNMLISTVIEW)lParam)->iSubItem;
                g_invertSort = FALSE;
            }
                    
396
            SendMessageW(hWnd, LVM_SORTITEMS, (WPARAM)hWnd, (LPARAM)CompareFunc);
397
            break;
398 399
	case LVN_ENDLABELEDITW: {
	        LPNMLVDISPINFOW dispInfo = (LPNMLVDISPINFOW)lParam;
400
		LPWSTR oldName = GetItemText(hWnd, dispInfo->item.iItem);
401
                LONG ret;
402
                if (!oldName) return -1; /* cannot rename a default value */
403
	        ret = RenameValue(hWnd, g_currentRootKey, g_currentPath, oldName, dispInfo->item.pszText);
404
		if (ret)
405
                {
406
                    RefreshListView(hWnd, g_currentRootKey, g_currentPath, dispInfo->item.pszText);
407
                }
408
		HeapFree(GetProcessHeap(), 0, oldName);
409 410 411 412 413
		return 0;
	    }
        case NM_RETURN: {
                int cnt = ListView_GetNextItem(hWnd, -1, LVNI_FOCUSED | LVNI_SELECTED);
	        if (cnt != -1)
414
		    SendMessageW(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
415
	    }
416
	    break;
417 418 419 420
        case NM_DBLCLK: {
                NMITEMACTIVATE* nmitem = (LPNMITEMACTIVATE)lParam;
                LVHITTESTINFO info;

421
                /* if (nmitem->hdr.hwndFrom != hWnd) break; unnecessary because of WM_NOTIFY_REFLECT */
422 423
                /*            if (nmitem->hdr.idFrom != IDW_LISTVIEW) break;  */
                /*            if (nmitem->hdr.code != ???) break;  */
424
#ifdef _MSC_VER
425 426 427 428 429 430 431 432 433 434
                switch (nmitem->uKeyFlags) {
                case LVKF_ALT:     /*  The ALT key is pressed.   */
                    /* properties dialog box ? */
                    break;
                case LVKF_CONTROL: /*  The CTRL key is pressed. */
                    /* run dialog box for providing parameters... */
                    break;
                case LVKF_SHIFT:   /*  The SHIFT key is pressed.    */
                    break;
                }
435
#endif
436 437 438
                info.pt.x = nmitem->ptAction.x;
                info.pt.y = nmitem->ptAction.y;
                if (ListView_HitTest(hWnd, &info) != -1) {
439 440 441
                    ListView_SetItemState(hWnd, -1, 0, LVIS_FOCUSED|LVIS_SELECTED);
                    ListView_SetItemState(hWnd, info.iItem, LVIS_FOCUSED|LVIS_SELECTED,
                        LVIS_FOCUSED|LVIS_SELECTED);
442
		    SendMessageW(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
443
                }
444 445 446 447
            }
            break;

        default:
448
            return 0; /* shouldn't call default ! */
449
        }
450
        break;
451
    case WM_CONTEXTMENU: {
452
        int cnt = ListView_GetNextItem(hWnd, -1, LVNI_SELECTED);
453 454 455
        TrackPopupMenu(GetSubMenu(hPopupMenus, cnt == -1 ? PM_NEW : PM_MODIFYVALUE),
                       TPM_RIGHTBUTTON, (short)LOWORD(lParam), (short)HIWORD(lParam),
                       0, hFrameWnd, NULL);
456 457
        break;
    }
458 459
    default:
        return CallWindowProc(g_orgListWndProc, hWnd, message, wParam, lParam);
460 461
    }
    return 0;
462 463 464
}


465
HWND CreateListView(HWND hwndParent, UINT id)
466 467 468
{
    RECT rcClient;
    HWND hwndLV;
469
    WCHAR ListView[] = {'L','i','s','t',' ','V','i','e','w',0};
470

471
    /* prepare strings */
472
    LoadStringW(hInst, IDS_REGISTRY_VALUE_NOT_SET, g_szValueNotSet, COUNT_OF(g_szValueNotSet));
473

474 475
    /* Get the dimensions of the parent window's client area, and create the list view control.  */
    GetClientRect(hwndParent, &rcClient);
476
    hwndLV = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW, ListView,
477
                            WS_VISIBLE | WS_CHILD | WS_TABSTOP | LVS_REPORT | LVS_EDITLABELS,
478
                            0, 0, rcClient.right, rcClient.bottom,
479
                            hwndParent, ULongToHandle(id), hInst, NULL);
480
    if (!hwndLV) return NULL;
481
    SendMessageW(hwndLV, LVM_SETUNICODEFORMAT, TRUE, 0);
482
    SendMessageW(hwndLV, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
483

484 485
    /* Initialize the image list */
    if (!InitListViewImageList(hwndLV)) goto fail;
486
    if (!CreateListColumns(hwndLV)) goto fail;
487
    g_orgListWndProc = (WNDPROC) SetWindowLongPtrW(hwndLV, GWLP_WNDPROC, (LPARAM)ListWndProc);
488
    return hwndLV;
489 490 491
fail:
    DestroyWindow(hwndLV);
    return NULL;
492 493
}

494
BOOL RefreshListView(HWND hwndLV, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR highlightValue)
495
{
496
    BOOL result = FALSE;
497
    DWORD max_sub_key_len;
498 499 500
    DWORD max_val_name_len, valNameLen;
    DWORD max_val_size, valSize;
    DWORD val_count, index, valType;
501
    WCHAR* valName = 0;
502 503
    BYTE* valBuf = 0;
    HKEY hKey = 0;
504 505
    LONG errCode;
    INT count, i;
506
    LVITEMW item;
507

508 509
    if (!hwndLV) return FALSE;

510 511
    SendMessageW(hwndLV, WM_SETREDRAW, FALSE, 0);

512
    errCode = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ, &hKey);
513 514
    if (errCode != ERROR_SUCCESS) goto done;

515 516 517 518
    count = ListView_GetItemCount(hwndLV);
    for (i = 0; i < count; i++) {
        item.mask = LVIF_PARAM;
        item.iItem = i;
519 520
        SendMessageW( hwndLV, LVM_GETITEM, 0, (LPARAM)&item );
        HeapFree(GetProcessHeap(), 0, ((LINE_INFO*)item.lParam)->name);
521 522
        HeapFree(GetProcessHeap(), 0, (void*)item.lParam);
    }
523
    g_columnToSort = ~0U;
524
    SendMessageW( hwndLV, LVM_DELETEALLITEMS, 0, 0L );
525 526

    /* get size information and resize the buffers if necessary */
527
    errCode = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, &max_sub_key_len, NULL,
528
                              &val_count, &max_val_name_len, &max_val_size, NULL, NULL);
529 530 531 532 533 534
    if (errCode != ERROR_SUCCESS) goto done;

    /* account for the terminator char */
    max_val_name_len++;
    max_val_size++;

535
    valName = HeapAlloc(GetProcessHeap(), 0, max_val_name_len * sizeof(WCHAR));
536 537
    if (!valName)
        goto done;
538
    valBuf = HeapAlloc(GetProcessHeap(), 0, max_val_size);
539 540 541
    if (!valBuf)
        goto done;

542
    if (RegQueryValueExW(hKey, NULL, NULL, &valType, valBuf, &valSize) == ERROR_FILE_NOT_FOUND) {
543 544
        AddEntryToList(hwndLV, NULL, REG_SZ, NULL, 0, !highlightValue);
    }
545
    for(index = 0; index < val_count; index++) {
546
        BOOL bSelected = (valName == highlightValue); /* NOT a bug, we check for double NULL here */
547 548 549
        valNameLen = max_val_name_len;
        valSize = max_val_size;
	valType = 0;
550
        errCode = RegEnumValueW(hKey, index, valName, &valNameLen, NULL, &valType, valBuf, &valSize);
551 552
	if (errCode != ERROR_SUCCESS) goto done;
        valBuf[valSize] = 0;
553
        if (highlightValue && !lstrcmpW(valName, highlightValue))
554 555
            bSelected = TRUE;
        AddEntryToList(hwndLV, valName[0] ? valName : NULL, valType, valBuf, valSize, bSelected);
556
    }
557
    SendMessageW(hwndLV, LVM_SORTITEMS, (WPARAM)hwndLV, (LPARAM)CompareFunc);
558 559 560 561

    g_currentRootKey = hKeyRoot;
    if (keyPath != g_currentPath) {
	HeapFree(GetProcessHeap(), 0, g_currentPath);
562
	g_currentPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(keyPath) + 1) * sizeof(WCHAR));
563
	if (!g_currentPath) goto done;
564
	lstrcpyW(g_currentPath, keyPath);
565 566 567 568 569 570 571
    }

    result = TRUE;

done:
    HeapFree(GetProcessHeap(), 0, valBuf);
    HeapFree(GetProcessHeap(), 0, valName);
572
    SendMessageW(hwndLV, WM_SETREDRAW, TRUE, 0);
573
    if (hKey) RegCloseKey(hKey);
574

575
    return result;
576
}