details.c 13.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * OleView (details.c)
 *
 * Copyright 2006 Piotr Caban
 *
 * 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 22 23
 */

#include "main.h"

DETAILS details;
24 25 26 27 28 29 30
static const WCHAR wszAppID[] = { 'A','p','p','I','D','\0' };
static const WCHAR wszCLSID[] = { 'C','L','S','I','D','\0' };
static const WCHAR wszProgID[] = { 'P','r','o','g','I','D','\0' };
static const WCHAR wszProxyStubClsid32[] = 
    { 'P','r','o','x','y','S','t','u','b','C','l','s','i','d','3','2','\0' };
static const WCHAR wszTypeLib[] = { 'T','y','p','e','L','i','b','\0' };

31
static void CreateRegRec(HKEY hKey, HTREEITEM parent, WCHAR *wszKeyName, BOOL addings)
32 33 34 35 36 37 38 39 40 41 42
{
    int i=0, j, retEnum;
    HKEY hCurKey;
    DWORD lenName, lenData, valType;
    WCHAR wszName[MAX_LOAD_STRING];
    WCHAR wszData[MAX_LOAD_STRING];
    WCHAR wszTree[MAX_LOAD_STRING];
    const WCHAR wszBinary[] = { '%','0','2','X',' ','\0' };
    const WCHAR wszDots[] = { '.','.','.','\0' };
    const WCHAR wszFormat1[] = { '%','s',' ','[','%','s',']',' ','=',' ','%','s','\0' };
    const WCHAR wszFormat2[] = { '%','s',' ','=',' ','%','s','\0' };
43
    TVINSERTSTRUCTW tvis;
44 45
    HTREEITEM addPlace = parent;

46 47 48
    U(tvis).item.mask = TVIF_TEXT;
    U(tvis).item.cchTextMax = MAX_LOAD_STRING;
    U(tvis).item.pszText = wszTree;
49
    tvis.hInsertAfter = TVI_LAST;
50 51 52 53
    tvis.hParent = parent;

    while(TRUE)
    {
54
        lenName = ARRAY_SIZE(wszName);
55
        lenData = sizeof(wszData);
56

57
        retEnum = RegEnumValueW(hKey, i, wszName, &lenName,
58 59 60 61
                NULL, &valType, (LPBYTE)wszData, &lenData);

        if(retEnum != ERROR_SUCCESS)
        {
62
            if(!i && lstrlenW(wszKeyName) > 1)
63
            {
64
                U(tvis).item.pszText = wszKeyName;
65
                addPlace = TreeView_InsertItemW(details.hReg, &tvis);
66
                U(tvis).item.pszText = wszTree;
67 68 69 70 71 72 73 74 75 76 77
            }
            break;
        }

        if(valType == REG_BINARY)
        {
            WCHAR wszBuf[MAX_LOAD_STRING];

            for(j=0; j<MAX_LOAD_STRING/3-1; j++)
                wsprintfW(&wszBuf[3*j], wszBinary, (int)((unsigned char)wszData[j]));
            wszBuf[(lenData*3>=MAX_LOAD_STRING ? MAX_LOAD_STRING-1 : lenData*3)] = '\0';
78 79
            lstrcpyW(wszData, wszBuf);
            lstrcpyW(&wszData[MAX_LOAD_STRING-5], wszDots);
80 81 82 83 84
        }

        if(lenName) wsprintfW(wszTree, wszFormat1, wszKeyName, wszName, wszData);
        else wsprintfW(wszTree, wszFormat2, wszKeyName, wszData);

85
        addPlace = TreeView_InsertItemW(details.hReg, &tvis);
86 87 88

        if(addings && !memcmp(wszName, wszAppID, sizeof(WCHAR[6])))
        {
89
            lstrcpyW(wszTree, wszName);
90
            memmove(&wszData[6], wszData, sizeof(WCHAR[MAX_LOAD_STRING-6]));
91
            lstrcpyW(wszData, wszCLSID);
92 93
            wszData[5] = '\\';

94
            if(RegOpenKeyW(HKEY_CLASSES_ROOT, wszData, &hCurKey) != ERROR_SUCCESS)
95 96 97 98 99 100
            {
                i++;
                continue;
            }

            tvis.hParent = TVI_ROOT;
101
            tvis.hParent = TreeView_InsertItemW(details.hReg, &tvis);
102

103
            lenName = sizeof(wszName);
104
            RegQueryValueW(hCurKey, NULL, wszName, (LONG *)&lenName);
105 106 107 108
            RegCloseKey(hCurKey);

            wsprintfW(wszTree, wszFormat2, &wszData[6], wszName);

109 110
            SendMessageW(details.hReg, TVM_INSERTITEMW, 0, (LPARAM)&tvis);
            SendMessageW(details.hReg, TVM_EXPAND, TVE_EXPAND, (LPARAM)tvis.hParent);
111 112 113 114 115 116 117 118 119 120 121 122

            tvis.hParent = parent;
        }
        i++;
    }

    i=-1;

    while(TRUE)
    {
        i++;

123
        if(RegEnumKeyW(hKey, i, wszName, ARRAY_SIZE(wszName)) != ERROR_SUCCESS) break;
124

125
        if(RegOpenKeyW(hKey, wszName, &hCurKey) != ERROR_SUCCESS) continue;
126 127

        CreateRegRec(hCurKey, addPlace, wszName, addings);
128
        SendMessageW(details.hReg, TVM_EXPAND, TVE_EXPAND, (LPARAM)addPlace);
129 130 131

        if(addings && !memcmp(wszName, wszProgID, sizeof(WCHAR[7])))
        {
132
            lenData = sizeof(wszData);
133
            RegQueryValueW(hCurKey, NULL, wszData, (LONG *)&lenData);
134 135
            RegCloseKey(hCurKey);

136
            if(RegOpenKeyW(HKEY_CLASSES_ROOT, wszData, &hCurKey) != ERROR_SUCCESS)
137 138 139 140 141
                continue;
            CreateRegRec(hCurKey, TVI_ROOT, wszData, FALSE);
        }
        else if(addings && !memcmp(wszName, wszProxyStubClsid32, sizeof(WCHAR[17])))
        {
142
            lenData = sizeof(wszData);
143

144
            RegQueryValueW(hCurKey, NULL, wszData, (LONG *)&lenData);
145 146
            RegCloseKey(hCurKey);

147
            RegOpenKeyW(HKEY_CLASSES_ROOT, wszCLSID, &hCurKey);
148

149
            lenName = sizeof(wszName);
150
            RegQueryValueW(hCurKey, NULL, wszName, (LONG *)&lenName);
151 152 153

            tvis.hParent = TVI_ROOT;
            wsprintfW(wszTree, wszFormat2, wszCLSID, wszName);
154
            tvis.hParent = TreeView_InsertItemW(details.hReg, &tvis);
155 156 157

            RegCloseKey(hCurKey);

158
            memmove(&wszData[6], wszData, lenData);
159 160 161
            memcpy(wszData, wszCLSID, sizeof(WCHAR[6]));
            wszData[5] = '\\';

162
            RegOpenKeyW(HKEY_CLASSES_ROOT, wszData, &hCurKey);
163 164 165

            CreateRegRec(hCurKey, tvis.hParent, &wszData[6], FALSE);

166
            SendMessageW(details.hReg, TVM_EXPAND, TVE_EXPAND, (LPARAM)tvis.hParent);
167 168 169 170
            tvis.hParent = parent;
        }
        else if(addings && !memcmp(wszName, wszTypeLib, sizeof(WCHAR[8])))
        {
171
            lenData = sizeof(wszData);
172
            RegQueryValueW(hCurKey, NULL, wszData, (LONG *)&lenData);
173 174
            RegCloseKey(hCurKey);

175
            RegOpenKeyW(HKEY_CLASSES_ROOT, wszTypeLib, &hCurKey);
176

177
            lenName = sizeof(wszName);
178
            RegQueryValueW(hCurKey, NULL, wszName, (LONG *)&lenName);
179 180 181

            tvis.hParent = TVI_ROOT;
            wsprintfW(wszTree, wszFormat2, wszTypeLib, wszName);
182
            tvis.hParent = TreeView_InsertItemW(details.hReg, &tvis);
183 184 185

            RegCloseKey(hCurKey);

186
            memmove(&wszData[8], wszData, lenData);
187 188
            memcpy(wszData, wszTypeLib, sizeof(WCHAR[8]));
            wszData[7] = '\\';
189
            RegOpenKeyW(HKEY_CLASSES_ROOT, wszData, &hCurKey);
190 191 192

            CreateRegRec(hCurKey, tvis.hParent, &wszData[8], FALSE);

193
            SendMessageW(details.hReg, TVM_EXPAND, TVE_EXPAND, (LPARAM)tvis.hParent);
194 195 196 197 198 199
            tvis.hParent = parent;
        }
        RegCloseKey(hCurKey);
    }
}

200
static void CreateReg(WCHAR *buffer)
201 202 203 204 205
{
    HKEY hKey;
    DWORD lenBuffer=-1, lastLenBuffer, lenTree;
    WCHAR *path;
    WCHAR wszTree[MAX_LOAD_STRING];
206
    TVINSERTSTRUCTW tvis;
207 208
    HTREEITEM addPlace = TVI_ROOT;

209 210 211
    U(tvis).item.mask = TVIF_TEXT;
    U(tvis).item.cchTextMax = MAX_LOAD_STRING;
    U(tvis).item.pszText = wszTree;
212
    tvis.hInsertAfter = TVI_LAST;
213 214 215 216 217 218 219 220 221 222 223
    tvis.hParent = TVI_ROOT;

    path = buffer;
    while(TRUE)
    {
        while(*path != '\\' && *path != '\0') path += 1;

        if(*path == '\\')
        {
            *path = '\0';

224
            if(RegOpenKeyW(HKEY_CLASSES_ROOT, buffer, &hKey) != ERROR_SUCCESS)
225 226 227
                return;

            lastLenBuffer = lenBuffer+1;
228
            lenBuffer = lstrlenW(buffer);
229 230 231
            *path = '\\';
            path += 1;

232
            lenTree = sizeof(wszTree);
233

234
            if(RegQueryValueW(hKey, NULL, wszTree, (LONG *)&lenTree) == ERROR_SUCCESS)
235
            {
236
                memmove(&wszTree[lenBuffer-lastLenBuffer+3], wszTree, lenTree);
237
                memcpy(wszTree, &buffer[lastLenBuffer],
238
                        (lenBuffer - lastLenBuffer) * sizeof(WCHAR));
239 240 241 242 243 244 245 246 247

                if(lenTree == 1) wszTree[lenBuffer-lastLenBuffer] = '\0';
                else
                {
                    wszTree[lenBuffer-lastLenBuffer] = ' ';
                    wszTree[lenBuffer-lastLenBuffer+1] = '=';
                    wszTree[lenBuffer-lastLenBuffer+2] = ' ';
                }

248
                addPlace = TreeView_InsertItemW(details.hReg, &tvis);
249 250 251 252 253 254 255 256
            }

            tvis.hParent = addPlace;
            RegCloseKey(hKey);
        }
        else break;
    }

257
    if(RegOpenKeyW(HKEY_CLASSES_ROOT, buffer, &hKey) != ERROR_SUCCESS) return;
258

259
    CreateRegRec(hKey, addPlace, &buffer[lenBuffer+1], TRUE);
260 261 262

    RegCloseKey(hKey);

263 264
    SendMessageW(details.hReg, TVM_EXPAND, TVE_EXPAND, (LPARAM)addPlace);
    SendMessageW(details.hReg, TVM_ENSUREVISIBLE, 0, (LPARAM)addPlace);
265
}
266 267 268

void RefreshDetails(HTREEITEM item)
{
269
    TVITEMW tvi;
270 271 272 273 274
    WCHAR wszBuf[MAX_LOAD_STRING];
    WCHAR wszStaticText[MAX_LOAD_STRING];
    const WCHAR wszFormat[] = { '%','s','\n','%','s','\0' };
    BOOL show;

275
    memset(&tvi, 0, sizeof(TVITEMW));
276 277 278 279 280
    memset(&wszStaticText, 0, sizeof(WCHAR[MAX_LOAD_STRING]));
    tvi.mask = TVIF_TEXT;
    tvi.hItem = item;
    tvi.pszText = wszBuf;
    tvi.cchTextMax = MAX_LOAD_STRING;
281
    SendMessageW(globals.hTree, TVM_GETITEMW, 0, (LPARAM)&tvi);
282 283 284

    if(tvi.lParam)
        wsprintfW(wszStaticText, wszFormat, tvi.pszText, ((ITEM_INFO *)tvi.lParam)->clsid);
285
    else lstrcpyW(wszStaticText, tvi.pszText);
286

287
    SetWindowTextW(details.hStatic, wszStaticText);
288

289
    SendMessageW(details.hTab, TCM_SETCURSEL, 0, 0);
290 291 292

    if(tvi.lParam && ((ITEM_INFO *)tvi.lParam)->cFlag & SHOWALL)
    {
293
        if(SendMessageW(details.hTab, TCM_GETITEMCOUNT, 0, 0) == 1)
294
        {
295 296
            TCITEMW tci;
            memset(&tci, 0, sizeof(TCITEMW));
297 298
            tci.mask = TCIF_TEXT;
            tci.pszText = wszBuf;
299
            tci.cchTextMax = ARRAY_SIZE(wszBuf);
300

301
            LoadStringW(globals.hMainInst, IDS_TAB_IMPL, wszBuf, ARRAY_SIZE(wszBuf));
302
            SendMessageW(details.hTab, TCM_INSERTITEMW, 1, (LPARAM)&tci);
303

304
            LoadStringW(globals.hMainInst, IDS_TAB_ACTIV, wszBuf, ARRAY_SIZE(wszBuf));
305
            SendMessageW(details.hTab, TCM_INSERTITEMW, 2, (LPARAM)&tci);
306 307 308 309
        }
    }
    else
    {
310 311
        SendMessageW(details.hTab, TCM_DELETEITEM, 2, 0);
        SendMessageW(details.hTab, TCM_DELETEITEM, 1, 0);
312 313 314 315 316 317
    }

    show = CreateRegPath(item, wszBuf, MAX_LOAD_STRING);
    ShowWindow(details.hTab, show ? SW_SHOW : SW_HIDE);

    /* FIXME Next line deals with TreeView_EnsureVisible bug */
318 319 320
    SendMessageW(details.hReg, TVM_ENSUREVISIBLE, 0,
            SendMessageW(details.hReg, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)TVI_ROOT));
    SendMessageW(details.hReg, TVM_DELETEITEM, 0, (LPARAM)TVI_ROOT);
321
    if(show) CreateReg(wszBuf);
322 323
}

324
static void CreateTabCtrl(HWND hWnd)
325
{
326
    TCITEMW tci;
327 328
    WCHAR buffer[MAX_LOAD_STRING];

329
    memset(&tci, 0, sizeof(TCITEMW));
330 331
    tci.mask = TCIF_TEXT;
    tci.pszText = buffer;
332
    tci.cchTextMax = ARRAY_SIZE(buffer);
333

334
    details.hTab = CreateWindowW(WC_TABCONTROLW, NULL, WS_CHILD|WS_VISIBLE,
335 336 337
            0, 0, 0, 0, hWnd, (HMENU)TAB_WINDOW, globals.hMainInst, NULL);
    ShowWindow(details.hTab, SW_HIDE);

338
    LoadStringW(globals.hMainInst, IDS_TAB_REG, buffer, ARRAY_SIZE(buffer));
339
    SendMessageW(details.hTab, TCM_INSERTITEMW, 0, (LPARAM)&tci);
340

341
    details.hReg = CreateWindowExW(WS_EX_CLIENTEDGE, WC_TREEVIEWW, NULL,
342 343 344 345
            WS_CHILD|WS_VISIBLE|TVS_HASLINES,
            0, 0, 0, 0, details.hTab, NULL, globals.hMainInst, NULL);
}

346
static LRESULT CALLBACK DetailsProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
347 348 349 350 351 352 353 354 355
{
    int sel;

    switch(uMsg)
    {
        case WM_CREATE:
            {
                const WCHAR wszStatic[] = { 'S','t','a','t','i','c','\0' };

356
                details.hStatic = CreateWindowW(wszStatic, NULL, WS_CHILD|WS_VISIBLE,
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
                        0, 0, 0, 0, hWnd, NULL, globals.hMainInst, NULL);
                CreateTabCtrl(hWnd);
            }
            break;
        case WM_SIZE:
            MoveWindow(details.hStatic, 0, 0, LOWORD(lParam), 40, TRUE);
            MoveWindow(details.hTab, 3, 40, LOWORD(lParam)-6, HIWORD(lParam)-43, TRUE);
            MoveWindow(details.hReg, 10, 34, LOWORD(lParam)-26,
                    HIWORD(lParam)-87, TRUE);
            break;
        case WM_NOTIFY:
            if((int)wParam != TAB_WINDOW) break;
            switch(((LPNMHDR)lParam)->code)
            {
                case TCN_SELCHANGE:
                    ShowWindow(details.hReg, SW_HIDE);
373
                    sel = SendMessageW(details.hTab, TCM_GETCURSEL, 0, 0);
374 375 376 377 378 379

                    if(sel==0) ShowWindow(details.hReg, SW_SHOW);
                    break;
            }
            break;
        default:
380
            return DefWindowProcW(hWnd, uMsg, wParam, lParam);
381 382 383 384 385 386
    }
    return 0;
}

HWND CreateDetailsWindow(HINSTANCE hInst)
{
387
    WNDCLASSW wcd;
388
    const WCHAR wszDetailsClass[] = { 'D','E','T','A','I','L','S','\0' };
389 390

    memset(&wcd, 0, sizeof(WNDCLASSW));
391 392
    wcd.lpfnWndProc = DetailsProc;
    wcd.lpszClassName = wszDetailsClass;
393 394
    wcd.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wcd.hCursor       = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
395

396
    if(!RegisterClassW(&wcd)) return NULL;
397

398
    globals.hDetails = CreateWindowExW(WS_EX_CLIENTEDGE, wszDetailsClass, NULL,
399 400 401 402
            WS_CHILD|WS_VISIBLE, 0, 0, 0, 0, globals.hPaneWnd, NULL, hInst, NULL);

    return globals.hDetails;
}