listview.c 19.2 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
 */

#include <windows.h>
23
#include <winternl.h>
24 25 26 27 28 29
#include <commctrl.h>
#include <stdlib.h>
#include <stdio.h>

#include "main.h"

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

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

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

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

#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 };

58
LPWSTR GetItemText(HWND hwndLV, UINT item)
59 60 61
{
    LPWSTR newStr, curStr;
    unsigned int maxLen = 128;
62
    if (item == 0) return NULL; /* first item is ALWAYS a default */
63 64 65 66

    curStr = HeapAlloc(GetProcessHeap(), 0, maxLen * sizeof(WCHAR));
    if (!curStr) return NULL;
    do {
67
        ListView_GetItemTextW(hwndLV, item, 0, curStr, maxLen);
68
        if (lstrlenW(curStr) < maxLen - 1) return curStr;
69 70
        maxLen *= 2;
        newStr = HeapReAlloc(GetProcessHeap(), 0, curStr, maxLen * sizeof(WCHAR));
71 72 73 74 75 76 77 78
        if (!newStr) break;
        curStr = newStr;
    } while (TRUE);
    HeapFree(GetProcessHeap(), 0, curStr);
    return NULL;
}

LPCWSTR GetValueName(HWND hwndLV)
79
{
80
    INT item;
81

82
    if (g_valueName != LPSTR_TEXTCALLBACKW)
83
        HeapFree(GetProcessHeap(), 0,  g_valueName);
84
    g_valueName = NULL;
85

86
    item = SendMessageW(hwndLV, LVM_GETNEXTITEM, -1, MAKELPARAM(LVNI_FOCUSED, 0));
87
    if (item == -1) return NULL;
88

89
    g_valueName = GetItemText(hwndLV, item);
90 91 92

    return g_valueName;
}
93

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

109 110 111
/*******************************************************************************
 * Local module support methods
 */
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
void format_value_data(HWND hwndLV, int index, DWORD type, void *data, DWORD size)
{
    switch (type)
    {
        case REG_SZ:
        case REG_EXPAND_SZ:
            ListView_SetItemTextW(hwndLV, index, 2, data ? data : g_szValueNotSet);
            break;
        case REG_DWORD:
        case REG_DWORD_BIG_ENDIAN:
        {
            DWORD value = *(DWORD *)data;
            WCHAR buf[64];
            WCHAR format[] = {'0','x','%','0','8','x',' ','(','%','u',')',0};
            if (type == REG_DWORD_BIG_ENDIAN)
                value = RtlUlongByteSwap(value);
            wsprintfW(buf, format, value, value);
            ListView_SetItemTextW(hwndLV, index, 2, buf);
            break;
        }
132 133 134 135
        case REG_MULTI_SZ:
            MakeMULTISZDisplayable(data);
            ListView_SetItemTextW(hwndLV, index, 2, data);
            break;
136 137
        case REG_BINARY:
        case REG_NONE:
138
        default:
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
        {
            unsigned int i;
            BYTE *pData = data;
            WCHAR *strBinary = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR) * 3 + sizeof(WCHAR));
            WCHAR format[] = {'%','0','2','X',' ',0};
            for (i = 0; i < size; i++)
                wsprintfW( strBinary + i*3, format, pData[i] );
            strBinary[size * 3] = 0;
            ListView_SetItemTextW(hwndLV, index, 2, strBinary);
            HeapFree(GetProcessHeap(), 0, strBinary);
            break;
        }
    }
}

154
int AddEntryToList(HWND hwndLV, WCHAR *Name, DWORD dwValType, void *ValBuf, DWORD dwCount, int pos)
155
{
156
    LINE_INFO* linfo;
157
    LVITEMW item;
158 159
    int index;

160 161 162
    linfo = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LINE_INFO) + dwCount);
    linfo->dwValType = dwValType;
    linfo->val_len = dwCount;
163
    CopyMemory(&linfo[1], ValBuf, dwCount);
164 165
    
    if (Name)
166 167 168 169 170
    {
        linfo->name = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(Name) + 1) * sizeof(WCHAR));
        lstrcpyW(linfo->name, Name);
    } else
    {
171
        linfo->name = NULL;
172
    }
173

174
    item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE | LVIF_IMAGE;
175
    item.iItem = (pos == -1) ? SendMessageW(hwndLV, LVM_GETITEMCOUNT, 0, 0) : pos;
176 177
    item.iSubItem = 0;
    item.state = 0;
178
    item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
179 180
    item.pszText = Name ? Name : LPSTR_TEXTCALLBACKW;
    item.cchTextMax = Name ? lstrlenW(item.pszText) : 0;
181

182 183 184 185 186 187 188 189 190 191 192
    switch (dwValType)
    {
    case REG_SZ:
    case REG_EXPAND_SZ:
    case REG_MULTI_SZ:
        item.iImage = Image_String;
        break;
    default:
        item.iImage = Image_Binary;
        break;
    }
193 194
    item.lParam = (LPARAM)linfo;

195 196 197 198
#if (_WIN32_IE >= 0x0300)
    item.iIndent = 0;
#endif

199 200
    if ((index = ListView_InsertItemW(hwndLV, &item)) != -1)
        format_value_data(hwndLV, index, dwValType, ValBuf, dwCount);
201
    return index;
202 203
}

204 205 206 207 208 209 210 211 212 213 214
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;

215
    hicon = LoadImageW(hInst, MAKEINTRESOURCEW(IDI_STRING),
216 217 218
        IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
    Image_String = ImageList_AddIcon(himl, hicon);

219
    hicon = LoadImageW(hInst, MAKEINTRESOURCEW(IDI_BIN),
220 221 222
        IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
    Image_Binary = ImageList_AddIcon(himl, hicon);

223
    SendMessageW( hWndListView, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM) himl );
224 225 226 227 228 229 230 231

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

    return TRUE;
}

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

    /* 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];
247 248
        LoadStringW(hInst, IDS_LIST_COLUMN_FIRST + index, szText, sizeof(szText)/sizeof(WCHAR));
        if (ListView_InsertColumnW(hWndListView, index, &lvC) == -1) return FALSE;
249
    }
250
    return TRUE;
251 252 253 254
}

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

255
static void OnGetDispInfo(NMLVDISPINFOW* plvdi)
256
{
257 258
    static WCHAR buffer[200];
    static WCHAR reg_szT[]               = {'R','E','G','_','S','Z',0},
259 260 261 262 263 264 265 266 267 268
                 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};
269 270 271 272 273 274

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

    switch (plvdi->item.iSubItem) {
    case 0:
275
        plvdi->item.pszText = g_pszDefaultValueName;
276 277
        break;
    case 1:
278 279 280 281
    {
        DWORD data_type = ((LINE_INFO *)plvdi->item.lParam)->dwValType;

        switch (data_type) {
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
            WCHAR fmt[] = {'0','x','%','x',0};
            wsprintfW(buffer, fmt, data_type);
313 314
            plvdi->item.pszText = buffer;
            break;
315
          }
316 317
        }
        break;
318
    }
319
    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
        g_columnToSort = 0;
    
339
    if (g_columnToSort == 1)
340
        return g_invertSort ? (int)r->dwValType - (int)l->dwValType : (int)l->dwValType - (int)r->dwValType;
341
    if (g_columnToSort == 2) {
342
        /* FIXME: Sort on value */
343
        return 0;
344
    }
345
    return g_invertSort ? lstrcmpiW(r->name, l->name) : lstrcmpiW(l->name, r->name);
346 347
}

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

352
    item = SendMessageW(hwndLV, LVM_GETNEXTITEM, -1, MAKELPARAM(LVNI_FOCUSED | LVNI_SELECTED, 0));
353 354 355 356
    if (item < 1) { /* cannot rename default key */
        MessageBeep(MB_ICONHAND);
        return 0;
    }
357
    return (HWND)SendMessageW(hwndLV, LVM_EDITLABELW, item, 0);
358
}
359 360 361

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

static LRESULT CALLBACK ListWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
373 374
    switch (message) {
    case WM_COMMAND:
375
        if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
376
            return CallWindowProcW(g_orgListWndProc, hWnd, message, wParam, lParam);
377
        }
378
        break;
379
    case WM_NOTIFY_REFLECT:
380
        switch (((LPNMHDR)lParam)->code) {
381
	
382 383
        case LVN_BEGINLABELEDITW:
            if (!((NMLVDISPINFOW *)lParam)->item.iItem)
384 385
                return 1;
            return 0;
386 387
        case LVN_GETDISPINFOW:
            OnGetDispInfo((NMLVDISPINFOW*)lParam);
388
            break;
389 390 391
        case LVN_COLUMNCLICK:
            if (g_columnToSort == ((LPNMLISTVIEW)lParam)->iSubItem)
                g_invertSort = !g_invertSort;
392
            else {
393 394 395 396
                g_columnToSort = ((LPNMLISTVIEW)lParam)->iSubItem;
                g_invertSort = FALSE;
            }
                    
397
            SendMessageW(hWnd, LVM_SORTITEMS, (WPARAM)hWnd, (LPARAM)CompareFunc);
398
            break;
399 400
	case LVN_ENDLABELEDITW: {
	        LPNMLVDISPINFOW dispInfo = (LPNMLVDISPINFOW)lParam;
401
		LPWSTR oldName = GetItemText(hWnd, dispInfo->item.iItem);
402
                LONG ret;
403

404
                if (!oldName) return -1; /* cannot rename a default value */
405
	        ret = RenameValue(hWnd, g_currentRootKey, g_currentPath, oldName, dispInfo->item.pszText);
406
		if (ret)
407
                {
408 409
                    dispInfo->item.iSubItem = 0;
                    SendMessageW(hWnd, LVM_SETITEMTEXTW, dispInfo->item.iItem, (LPARAM)&dispInfo->item);
410
                }
411
		HeapFree(GetProcessHeap(), 0, oldName);
412 413 414
		return 0;
	    }
        case NM_RETURN: {
415 416 417 418 419
            int cnt = SendMessageW(hWnd, LVM_GETNEXTITEM, -1, MAKELPARAM(LVNI_FOCUSED | LVNI_SELECTED, 0));
            if (cnt != -1)
                SendMessageW(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
            }
            break;
420 421 422
        case NM_SETFOCUS:
            g_pChildWnd->nFocusPanel = 1;
            break;
423 424 425 426
        case NM_DBLCLK: {
                NMITEMACTIVATE* nmitem = (LPNMITEMACTIVATE)lParam;
                LVHITTESTINFO info;

427
                /* if (nmitem->hdr.hwndFrom != hWnd) break; unnecessary because of WM_NOTIFY_REFLECT */
428 429
                /*            if (nmitem->hdr.idFrom != IDW_LISTVIEW) break;  */
                /*            if (nmitem->hdr.code != ???) break;  */
430
#ifdef _MSC_VER
431 432 433 434 435 436 437 438 439 440
                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;
                }
441
#endif
442 443
                info.pt.x = nmitem->ptAction.x;
                info.pt.y = nmitem->ptAction.y;
444 445 446 447 448 449 450 451 452 453 454 455
                if (SendMessageW(hWnd, LVM_HITTEST, 0, (LPARAM)&info) != -1) {
                    LVITEMW item;

                    item.state = 0;
                    item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
                    SendMessageW(hWnd, LVM_SETITEMSTATE, (UINT)-1, (LPARAM)&item);

                    item.state = LVIS_FOCUSED | LVIS_SELECTED;
                    item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
                    SendMessageW(hWnd, LVM_SETITEMSTATE, info.iItem, (LPARAM)&item);

                    SendMessageW(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
456
                }
457 458 459 460
            }
            break;

        default:
461
            return 0; /* shouldn't call default ! */
462
        }
463
        break;
464
    case WM_CONTEXTMENU: {
465
        int cnt = SendMessageW(hWnd, LVM_GETNEXTITEM, -1, MAKELPARAM(LVNI_SELECTED, 0));
466
        TrackPopupMenu(GetSubMenu(hPopupMenus, cnt == -1 ? PM_NEW_VALUE : PM_MODIFY_VALUE),
467 468
                       TPM_RIGHTBUTTON, (short)LOWORD(lParam), (short)HIWORD(lParam),
                       0, hFrameWnd, NULL);
469 470
        break;
    }
471
    default:
472
        return CallWindowProcW(g_orgListWndProc, hWnd, message, wParam, lParam);
473 474
    }
    return 0;
475 476 477
}


478
HWND CreateListView(HWND hwndParent, UINT id)
479 480 481
{
    RECT rcClient;
    HWND hwndLV;
482
    WCHAR ListView[] = {'L','i','s','t',' ','V','i','e','w',0};
483

484
    /* prepare strings */
485
    LoadStringW(hInst, IDS_REGISTRY_VALUE_NOT_SET, g_szValueNotSet, COUNT_OF(g_szValueNotSet));
486

487 488
    /* Get the dimensions of the parent window's client area, and create the list view control.  */
    GetClientRect(hwndParent, &rcClient);
489
    hwndLV = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW, ListView,
490
                            WS_VISIBLE | WS_CHILD | WS_TABSTOP | LVS_REPORT | LVS_EDITLABELS,
491
                            0, 0, rcClient.right, rcClient.bottom,
492
                            hwndParent, ULongToHandle(id), hInst, NULL);
493
    if (!hwndLV) return NULL;
494
    SendMessageW(hwndLV, LVM_SETUNICODEFORMAT, TRUE, 0);
495
    SendMessageW(hwndLV, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
496

497 498
    /* Initialize the image list */
    if (!InitListViewImageList(hwndLV)) goto fail;
499
    if (!CreateListColumns(hwndLV)) goto fail;
500
    g_orgListWndProc = (WNDPROC) SetWindowLongPtrW(hwndLV, GWLP_WNDPROC, (LPARAM)ListWndProc);
501
    return hwndLV;
502 503 504
fail:
    DestroyWindow(hwndLV);
    return NULL;
505 506
}

507
BOOL RefreshListView(HWND hwndLV, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR highlightValue)
508
{
509
    BOOL result = FALSE;
510
    DWORD max_sub_key_len;
511 512 513
    DWORD max_val_name_len, valNameLen;
    DWORD max_val_size, valSize;
    DWORD val_count, index, valType;
514
    WCHAR* valName = 0;
515 516
    BYTE* valBuf = 0;
    HKEY hKey = 0;
517 518
    LONG errCode;
    INT count, i;
519
    LVITEMW item;
520

521 522
    if (!hwndLV) return FALSE;

523 524
    SendMessageW(hwndLV, WM_SETREDRAW, FALSE, 0);

525
    errCode = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ, &hKey);
526 527
    if (errCode != ERROR_SUCCESS) goto done;

528
    count = SendMessageW(hwndLV, LVM_GETITEMCOUNT, 0, 0);
529 530 531
    for (i = 0; i < count; i++) {
        item.mask = LVIF_PARAM;
        item.iItem = i;
532
        SendMessageW( hwndLV, LVM_GETITEMW, 0, (LPARAM)&item );
533
        HeapFree(GetProcessHeap(), 0, ((LINE_INFO*)item.lParam)->name);
534 535
        HeapFree(GetProcessHeap(), 0, (void*)item.lParam);
    }
536
    g_columnToSort = ~0U;
537
    SendMessageW( hwndLV, LVM_DELETEALLITEMS, 0, 0L );
538 539

    /* get size information and resize the buffers if necessary */
540
    errCode = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, &max_sub_key_len, NULL,
541
                              &val_count, &max_val_name_len, &max_val_size, NULL, NULL);
542 543 544 545 546 547
    if (errCode != ERROR_SUCCESS) goto done;

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

548
    valName = HeapAlloc(GetProcessHeap(), 0, max_val_name_len * sizeof(WCHAR));
549 550
    if (!valName)
        goto done;
551
    valBuf = HeapAlloc(GetProcessHeap(), 0, max_val_size);
552 553 554
    if (!valBuf)
        goto done;

555
    valSize = max_val_size;
556
    if (RegQueryValueExW(hKey, NULL, NULL, &valType, valBuf, &valSize) == ERROR_FILE_NOT_FOUND) {
557
        AddEntryToList(hwndLV, NULL, REG_SZ, NULL, 0, -1);
558
    }
559 560 561 562
    for(index = 0; index < val_count; index++) {
        valNameLen = max_val_name_len;
        valSize = max_val_size;
	valType = 0;
563
        errCode = RegEnumValueW(hKey, index, valName, &valNameLen, NULL, &valType, valBuf, &valSize);
564 565
	if (errCode != ERROR_SUCCESS) goto done;
        valBuf[valSize] = 0;
566
        AddEntryToList(hwndLV, valName[0] ? valName : NULL, valType, valBuf, valSize, -1);
567 568 569 570 571 572 573
    }

    memset(&item, 0, sizeof(item));
    if (!highlightValue)
    {
        item.state = item.stateMask = LVIS_FOCUSED;
        SendMessageW(hwndLV, LVM_SETITEMSTATE, 0, (LPARAM)&item);
574
    }
575

576
    SendMessageW(hwndLV, LVM_SORTITEMS, (WPARAM)hwndLV, (LPARAM)CompareFunc);
577 578 579 580

    g_currentRootKey = hKeyRoot;
    if (keyPath != g_currentPath) {
	HeapFree(GetProcessHeap(), 0, g_currentPath);
581
	g_currentPath = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(keyPath) + 1) * sizeof(WCHAR));
582
	if (!g_currentPath) goto done;
583
	lstrcpyW(g_currentPath, keyPath);
584 585 586 587 588 589 590
    }

    result = TRUE;

done:
    HeapFree(GetProcessHeap(), 0, valBuf);
    HeapFree(GetProcessHeap(), 0, valName);
591
    SendMessageW(hwndLV, WM_SETREDRAW, TRUE, 0);
592
    if (hKey) RegCloseKey(hKey);
593

594
    return result;
595
}