listview.c 13.4 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
#include <commctrl.h>

#include "main.h"
27

28 29 30
static INT Image_String;
static INT Image_Binary;

31 32 33 34
/*******************************************************************************
 * Global and Local Variables:
 */

35 36 37 38 39
DWORD   g_columnToSort = ~0U;
BOOL    g_invertSort = FALSE;
WCHAR  *g_currentPath;
HKEY    g_currentRootKey;
static  WCHAR g_szValueNotSet[64];
40 41 42 43 44

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

45
LPWSTR GetItemText(HWND hwndLV, UINT item)
46
{
47
    WCHAR *curStr;
48
    unsigned int maxLen = 128;
49
    if (item == 0) return NULL; /* first item is ALWAYS a default */
50

51
    curStr = malloc(maxLen * sizeof(WCHAR));
52
    do {
53
        ListView_GetItemTextW(hwndLV, item, 0, curStr, maxLen);
54
        if (lstrlenW(curStr) < maxLen - 1) return curStr;
55
        maxLen *= 2;
56
        curStr = realloc(curStr, maxLen * sizeof(WCHAR));
57
    } while (TRUE);
58
    free(curStr);
59 60 61
    return NULL;
}

62
WCHAR *GetValueName(HWND hwndLV)
63
{
64
    INT item;
65

66
    item = SendMessageW(hwndLV, LVM_GETNEXTITEM, -1, MAKELPARAM(LVNI_FOCUSED, 0));
67
    if (item == -1) return NULL;
68

69
    return GetItemText(hwndLV, item);
70
}
71

72 73
BOOL update_listview_path(const WCHAR *path)
{
74
    free(g_currentPath);
75

76
    g_currentPath = malloc((lstrlenW(path) + 1) * sizeof(WCHAR));
77 78 79 80 81
    lstrcpyW(g_currentPath, path);

    return TRUE;
}

82
/* convert '\0' separated string list into ',' separated string list */
83
static void MakeMULTISZDisplayable(LPWSTR multi)
84 85 86 87 88 89 90 91 92 93 94 95 96
{
    do
    {
        for (; *multi; multi++)
            ;
        if (*(multi+1))
        {
            *multi = ',';
            multi++;
        }
    } while (*multi);
}

97 98 99
/*******************************************************************************
 * Local module support methods
 */
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
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];
            if (type == REG_DWORD_BIG_ENDIAN)
                value = RtlUlongByteSwap(value);
115
            wsprintfW(buf, L"0x%08x (%u)", value, value);
116 117 118
            ListView_SetItemTextW(hwndLV, index, 2, buf);
            break;
        }
119 120 121 122
        case REG_QWORD:
        {
            UINT64 value = *(UINT64 *)data;
            WCHAR buf[64];
123
            swprintf(buf, ARRAY_SIZE(buf), L"0x%08I64x (%I64u)", value, value);
124 125 126
            ListView_SetItemTextW(hwndLV, index, 2, buf);
            break;
        }
127 128 129 130
        case REG_MULTI_SZ:
            MakeMULTISZDisplayable(data);
            ListView_SetItemTextW(hwndLV, index, 2, data);
            break;
131 132
        case REG_BINARY:
        case REG_NONE:
133
        default:
134 135 136
        {
            unsigned int i;
            BYTE *pData = data;
137
            WCHAR *strBinary = malloc(size * sizeof(WCHAR) * 3 + sizeof(WCHAR));
138
            for (i = 0; i < size; i++)
139
                wsprintfW( strBinary + i*3, L"%02X ", pData[i] );
140 141
            strBinary[size * 3] = 0;
            ListView_SetItemTextW(hwndLV, index, 2, strBinary);
142
            free(strBinary);
143 144 145 146 147
            break;
        }
    }
}

148
int AddEntryToList(HWND hwndLV, WCHAR *Name, DWORD dwValType, void *ValBuf, DWORD dwCount, int pos)
149
{
150
    LINE_INFO *linfo;
151
    LVITEMW item = { 0 };
152 153
    int index;

154
    linfo = malloc(sizeof(LINE_INFO));
155 156
    linfo->dwValType = dwValType;
    linfo->val_len = dwCount;
157

158
    if (Name)
159
    {
160
        linfo->name = malloc((lstrlenW(Name) + 1) * sizeof(WCHAR));
161
        lstrcpyW(linfo->name, Name);
162 163 164 165
    }
    else linfo->name = NULL;

    if (ValBuf && dwCount)
166
    {
167
        linfo->val = malloc(dwCount);
168
        memcpy(linfo->val, ValBuf, dwCount);
169
    }
170
    else linfo->val = NULL;
171

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

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

191 192
    if ((index = ListView_InsertItemW(hwndLV, &item)) != -1)
        format_value_data(hwndLV, index, dwValType, ValBuf, dwCount);
193
    return index;
194 195
}

196 197 198 199 200 201 202 203 204 205 206
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;

207
    hicon = LoadImageW(hInst, MAKEINTRESOURCEW(IDI_STRING),
208 209 210
        IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
    Image_String = ImageList_AddIcon(himl, hicon);

211
    hicon = LoadImageW(hInst, MAKEINTRESOURCEW(IDI_BIN),
212 213 214
        IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
    Image_Binary = ImageList_AddIcon(himl, hicon);

215
    SendMessageW( hWndListView, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM) himl );
216 217 218 219 220 221 222 223

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

    return TRUE;
}

224
static BOOL CreateListColumns(HWND hWndListView)
225
{
226
    WCHAR szText[50];
227
    int index;
228
    LVCOLUMNW lvC;
229 230 231 232 233 234 235 236 237 238

    /* 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];
239
        LoadStringW(hInst, IDS_LIST_COLUMN_FIRST + index, szText, ARRAY_SIZE(szText));
240
        if (ListView_InsertColumnW(hWndListView, index, &lvC) == -1) return FALSE;
241
    }
242
    return TRUE;
243 244 245
}

/* OnGetDispInfo - processes the LVN_GETDISPINFO notification message.  */
246
void OnGetDispInfo(NMLVDISPINFOW *plvdi)
247
{
248
    static WCHAR buffer[200];
249 250 251 252 253
    static WCHAR reg_szT[]               = L"REG_SZ",
                 reg_expand_szT[]        = L"REG_EXPAND_SZ",
                 reg_binaryT[]           = L"REG_BINARY",
                 reg_dwordT[]            = L"REG_DWORD",
                 reg_dword_big_endianT[] = L"REG_DWORD_BIG_ENDIAN",
254
                 reg_qwordT[]            = L"REG_QWORD",
255 256 257 258 259
                 reg_multi_szT[]         = L"REG_MULTI_SZ",
                 reg_linkT[]             = L"REG_LINK",
                 reg_resource_listT[]    = L"REG_RESOURCE_LIST",
                 reg_noneT[]             = L"REG_NONE",
                 emptyT[]                = L"";
260 261 262 263 264 265

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

    switch (plvdi->item.iSubItem) {
    case 0:
266
        plvdi->item.pszText = g_pszDefaultValueName;
267 268
        break;
    case 1:
269 270 271 272
    {
        DWORD data_type = ((LINE_INFO *)plvdi->item.lParam)->dwValType;

        switch (data_type) {
273
        case REG_SZ:
274
            plvdi->item.pszText = reg_szT;
275 276
            break;
        case REG_EXPAND_SZ:
277
            plvdi->item.pszText = reg_expand_szT;
278 279
            break;
        case REG_BINARY:
280
            plvdi->item.pszText = reg_binaryT;
281 282
            break;
        case REG_DWORD:
283
            plvdi->item.pszText = reg_dwordT;
284
            break;
285 286 287
        case REG_QWORD:
            plvdi->item.pszText = reg_qwordT;
            break;
288
        case REG_DWORD_BIG_ENDIAN:
289
            plvdi->item.pszText = reg_dword_big_endianT;
290 291
            break;
        case REG_MULTI_SZ:
292
            plvdi->item.pszText = reg_multi_szT;
293 294
            break;
        case REG_LINK:
295
            plvdi->item.pszText = reg_linkT;
296 297
            break;
        case REG_RESOURCE_LIST:
298
            plvdi->item.pszText = reg_resource_listT;
299 300
            break;
        case REG_NONE:
301
            plvdi->item.pszText = reg_noneT;
302 303
            break;
        default:
304
          {
305
            wsprintfW(buffer, L"0x%x", data_type);
306 307
            plvdi->item.pszText = buffer;
            break;
308
          }
309 310
        }
        break;
311
    }
312
    case 2:
313
        plvdi->item.pszText = g_szValueNotSet;
314 315
        break;
    case 3:
316
        plvdi->item.pszText = emptyT;
317 318 319 320
        break;
    }
}

321
int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
322
{
323 324 325
    LINE_INFO*l, *r;
    l = (LINE_INFO*)lParam1;
    r = (LINE_INFO*)lParam2;
326 327
    if (!l->name) return -1;
    if (!r->name) return +1;
328
        
329
    if (g_columnToSort == ~0U)
330 331
        g_columnToSort = 0;
    
332
    if (g_columnToSort == 1)
333
        return g_invertSort ? (int)r->dwValType - (int)l->dwValType : (int)l->dwValType - (int)r->dwValType;
334
    if (g_columnToSort == 2) {
335
        /* FIXME: Sort on value */
336
        return 0;
337
    }
338
    return g_invertSort ? lstrcmpiW(r->name, l->name) : lstrcmpiW(l->name, r->name);
339 340
}

341
HWND StartValueRename(HWND hwndLV)
342 343 344
{
    int item;

345
    item = SendMessageW(hwndLV, LVM_GETNEXTITEM, -1, MAKELPARAM(LVNI_FOCUSED | LVNI_SELECTED, 0));
346 347 348 349
    if (item < 1) { /* cannot rename default key */
        MessageBeep(MB_ICONHAND);
        return 0;
    }
350
    return (HWND)SendMessageW(hwndLV, LVM_EDITLABELW, item, 0);
351
}
352

353
HWND CreateListView(HWND hwndParent, UINT id)
354 355 356 357
{
    RECT rcClient;
    HWND hwndLV;

358
    /* prepare strings */
359
    LoadStringW(hInst, IDS_REGISTRY_VALUE_NOT_SET, g_szValueNotSet, ARRAY_SIZE(g_szValueNotSet));
360

361 362
    /* Get the dimensions of the parent window's client area, and create the list view control.  */
    GetClientRect(hwndParent, &rcClient);
363
    hwndLV = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW, L"List View",
364
                            WS_VISIBLE | WS_CHILD | WS_TABSTOP | LVS_REPORT | LVS_EDITLABELS,
365
                            0, 0, rcClient.right, rcClient.bottom,
366
                            hwndParent, ULongToHandle(id), hInst, NULL);
367
    if (!hwndLV) return NULL;
368
    SendMessageW(hwndLV, LVM_SETUNICODEFORMAT, TRUE, 0);
369
    SendMessageW(hwndLV, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
370

371 372
    /* Initialize the image list */
    if (!InitListViewImageList(hwndLV)) goto fail;
373
    if (!CreateListColumns(hwndLV)) goto fail;
374
    return hwndLV;
375 376 377
fail:
    DestroyWindow(hwndLV);
    return NULL;
378 379
}

380
BOOL RefreshListView(HWND hwndLV, HKEY hKeyRoot, LPCWSTR keyPath, LPCWSTR highlightValue)
381
{
382
    BOOL result = FALSE;
383
    DWORD max_sub_key_len;
384 385 386
    DWORD max_val_name_len, valNameLen;
    DWORD max_val_size, valSize;
    DWORD val_count, index, valType;
387
    WCHAR* valName = 0;
388 389
    BYTE* valBuf = 0;
    HKEY hKey = 0;
390
    LONG errCode;
391
    LVITEMW item;
392

393 394
    if (!hwndLV) return FALSE;

395 396
    SendMessageW(hwndLV, WM_SETREDRAW, FALSE, 0);

397
    errCode = RegOpenKeyExW(hKeyRoot, keyPath, 0, KEY_READ, &hKey);
398 399
    if (errCode != ERROR_SUCCESS) goto done;

400
    g_columnToSort = ~0U;
401
    SendMessageW(hwndLV, LVM_DELETEALLITEMS, 0, 0);
402 403

    /* get size information and resize the buffers if necessary */
404
    errCode = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, &max_sub_key_len, NULL,
405
                              &val_count, &max_val_name_len, &max_val_size, NULL, NULL);
406 407 408 409 410 411
    if (errCode != ERROR_SUCCESS) goto done;

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

412 413
    valName = malloc(max_val_name_len * sizeof(WCHAR));
    valBuf = malloc(max_val_size);
414

415
    valSize = max_val_size;
416
    if (RegQueryValueExW(hKey, NULL, NULL, &valType, valBuf, &valSize) == ERROR_FILE_NOT_FOUND) {
417
        AddEntryToList(hwndLV, NULL, REG_SZ, NULL, 0, -1);
418
    }
419 420 421 422
    for(index = 0; index < val_count; index++) {
        valNameLen = max_val_name_len;
        valSize = max_val_size;
	valType = 0;
423
        errCode = RegEnumValueW(hKey, index, valName, &valNameLen, NULL, &valType, valBuf, &valSize);
424 425
	if (errCode != ERROR_SUCCESS) goto done;
        valBuf[valSize] = 0;
426
        AddEntryToList(hwndLV, valName[0] ? valName : NULL, valType, valBuf, valSize, -1);
427 428 429 430 431 432 433
    }

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

436
    SendMessageW(hwndLV, LVM_SORTITEMS, (WPARAM)hwndLV, (LPARAM)CompareFunc);
437 438

    g_currentRootKey = hKeyRoot;
439 440
    if (keyPath != g_currentPath && !update_listview_path(keyPath))
        goto done;
441 442 443 444

    result = TRUE;

done:
445 446
    free(valBuf);
    free(valName);
447
    SendMessageW(hwndLV, WM_SETREDRAW, TRUE, 0);
448
    if (hKey) RegCloseKey(hKey);
449

450
    return result;
451
}