typelib.c 58.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * OleView (typelib.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
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "main.h"

23
#include "wine/debug.h"
24
#include "wine/unicode.h"
25 26 27

WINE_DEFAULT_DEBUG_CHANNEL(oleview);

28 29 30
TYPELIB typelib;
static const WCHAR wszTypeLib[] = { 'T','Y','P','E','L','I','B','\0' };

31 32 33
static const WCHAR wszFailed[] = { '<','f','a','i','l','e','d','>','\0' };
static const WCHAR wszSpace[] = { ' ','\0' };
static const WCHAR wszAsterix[] = { '*','\0' };
34
static const WCHAR wszComa[] = { ',','\0' };
35
static const WCHAR wszEquals[] = { '=','\0' };
36 37 38 39 40 41
static const WCHAR wszSemicolon[] = { ';','\0' };
static const WCHAR wszNewLine[] = { '\n','\0' };
static const WCHAR wszOpenBrackets1[] = { '[','\0' };
static const WCHAR wszCloseBrackets1[] = { ']','\0' };
static const WCHAR wszOpenBrackets2[] = { '(','\0' };
static const WCHAR wszCloseBrackets2[] = { ')','\0' };
42 43
static const WCHAR wszOpenBrackets3[] = { '{','\0' };
static const WCHAR wszCloseBrackets3[] = { '}','\0' };
44 45
static const WCHAR wszInvertedComa[] = { '"','\0' };
static const WCHAR wszColon[] = { ':','\0' };
46 47

static const WCHAR wszUUID[] = { 'u','u','i','d','\0' };
48
static const WCHAR wszOdl[] = { 'o','d','l','\0' };
49 50 51 52 53

static const WCHAR wszVT_BOOL[]
    = { 'V','A','R','I','A','N','T','_','B','O','O','L','\0' };
static const WCHAR wszVT_UI1[]
    = { 'u','n','s','i','g','n','e','d',' ','c','h','a','r','\0' };
54 55
static const WCHAR wszVT_UI2[]
    = { 'u','n','s','i','g','n','e','d',' ','s','h','o','r','t','\0' };
56 57
static const WCHAR wszVT_UI4[]
    = { 'u','n','s','i','g','n','e','d',' ','l','o','n','g','\0' };
58 59 60 61 62
static const WCHAR wszVT_UI8[] = { 'u','i','n','t','6','4','\0' };
static const WCHAR wszVT_UINT[]
    = { 'u','n','s','i','g','n','e','d',' ','i','n','t','\0' };
static const WCHAR wszVT_I1[] = { 'c','h','a','r','\0' };
static const WCHAR wszVT_I2[] = { 's','h','o','r','t','\0' };
63
static const WCHAR wszVT_I4[] = { 'l','o','n','g','\0' };
64
static const WCHAR wszVT_I8[] = { 'i','n','t','6','4','\0' };
65 66 67 68
static const WCHAR wszVT_R4[] = { 's','i','n','g','l','e','\0' };
static const WCHAR wszVT_INT[] = { 'i','n','t','\0' };
static const WCHAR wszVT_BSTR[] = { 'B','S','T','R','\0' };
static const WCHAR wszVT_CY[] = { 'C','U','R','R','E','N','C','Y','\0' };
69 70 71 72 73
static const WCHAR wszVT_VARIANT[] = { 'V','A','R','I','A','N','T','\0' };
static const WCHAR wszVT_VOID[] = { 'v','o','i','d','\0' };
static const WCHAR wszVT_ERROR[] = { 'S','C','O','D','E','\0' };
static const WCHAR wszVT_LPSTR[] = { 'L','P','S','T','R','\0' };
static const WCHAR wszVT_LPWSTR[] = { 'L','P','W','S','T','R','\0' };
74 75 76 77 78
static const WCHAR wszVT_HRESULT[] = { 'H','R','E','S','U','L','T','\0' };
static const WCHAR wszVT_UNKNOWN[] = { 'I','U','n','k','n','o','w','n','\0' };
static const WCHAR wszVT_DISPATCH[] = { 'I','D','i','s','p','a','t','c','h','\0' };
static const WCHAR wszVT_DATE[] = { 'D','A','T','E','\0' };
static const WCHAR wszVT_R8[] = { 'd','o','u','b','l','e','\0' };
79
static const WCHAR wszVT_SAFEARRAY[] = { 'S','A','F','E','A','R','R','A','Y','\0' };
80

81
static const WCHAR wszFormat[] = { '0','x','%','.','8','l','x','\0' };
82
static const WCHAR wszStdCall[] = { '_','s','t','d','c','a','l','l','\0' };
83
static const WCHAR wszId[] = { 'i','d','\0' };
84
static const WCHAR wszHelpstring[] = { 'h','e','l','p','s','t','r','i','n','g','\0' };
85 86 87 88 89 90 91
static const WCHAR wszPropPut[] = { 'p','r','o','p','p','u','t','\0' };
static const WCHAR wszPropGet[] = { 'p','r','o','p','g','e','t','\0' };
static const WCHAR wszPropPutRef[] = { 'p','r','o','p','p','u','t','r','e','f','\0' };
static const WCHAR wszPARAMFLAG_FIN[] = { 'i','n','\0' };
static const WCHAR wszPARAMFLAG_FOUT[] = { 'o','u','t','\0' };
static const WCHAR wszPARAMFLAG_FLCID[] = { 'c','i','d','\0' };
static const WCHAR wszPARAMFLAG_FRETVAL[] = { 'r','e','t','v','a','l','\0' };
92
static const WCHAR wszPARAMFLAG_FOPT[] = { 'o','p','t','i','o','n','a','l','\0' };
93 94
static const WCHAR wszPARAMFLAG_FHASCUSTDATA[]
    = { 'h','a','s','c','u','s','t','d','a','t','a','\0' };
95 96
static const WCHAR wszDefaultValue[]
    = { 'd','e','f','a','u','l','t','v','a','l','u','e','\0' };
97

98
static const WCHAR wszReadOnly[] = { 'r','e','a','d','o','n','l','y','\0' };
99
static const WCHAR wszConst[] = { 'c','o','n','s','t','\0' };
100

101
static void ShowLastError(void)
102 103 104 105 106
{
    DWORD error = GetLastError();
    LPWSTR lpMsgBuf;
    WCHAR wszTitle[MAX_LOAD_STRING];

107
    LoadStringW(globals.hMainInst, IDS_TYPELIBTITLE, wszTitle,
108
            sizeof(wszTitle)/sizeof(wszTitle[0]));
109 110 111
    FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
            NULL, error, 0, (LPWSTR)&lpMsgBuf, 0, NULL);
    MessageBoxW(NULL, lpMsgBuf, wszTitle, MB_OK | MB_ICONERROR);
112 113 114 115
    LocalFree(lpMsgBuf);
    return;
}

116
static void SaveIdl(WCHAR *wszFileName)
117 118
{
    HTREEITEM hIDL;
119
    TVITEMW tvi;
120
    HANDLE hFile;
121
    DWORD len, dwNumWrite;
122
    char *wszIdl;
123
    TYPELIB_DATA *data;
124

125 126
    hIDL = (HTREEITEM)SendMessageW(typelib.hTree, TVM_GETNEXTITEM,
            TVGN_CHILD, (LPARAM)TVI_ROOT);
127

128
    memset(&tvi, 0, sizeof(TVITEMW));
129 130
    tvi.hItem = hIDL;

131
    SendMessageW(typelib.hTree, TVM_GETITEMW, 0, (LPARAM)&tvi);
132
    data = (TYPELIB_DATA *)tvi.lParam;
133

134
    hFile = CreateFileW(wszFileName, GENERIC_WRITE, FILE_SHARE_WRITE,
135
                       NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
136 137 138 139 140 141
    if(hFile == INVALID_HANDLE_VALUE)
    {
        ShowLastError();
        return;
    }

142 143 144
    len = WideCharToMultiByte( CP_UTF8, 0, data->idl, data->idlLen, NULL, 0, NULL, NULL );
    wszIdl = HeapAlloc(GetProcessHeap(), 0, len);
    WideCharToMultiByte( CP_UTF8, 0, data->idl, data->idlLen, wszIdl, len, NULL, NULL );
145

146 147
    if(!WriteFile(hFile, wszIdl, len, &dwNumWrite, NULL))
        ShowLastError();
148 149 150 151 152

    HeapFree(GetProcessHeap(), 0, wszIdl);
    CloseHandle(hFile);
}

153
static void GetSaveIdlAsPath(void)
154
{
155
    OPENFILENAMEW saveidl;
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    WCHAR *pFileName;
    WCHAR wszPath[MAX_LOAD_STRING];
    WCHAR wszDir[MAX_LOAD_STRING];
    static const WCHAR wszDefaultExt[] = { 'i','d','l',0 };
    static const WCHAR wszIdlFiles[] = { '*','.','i','d','l','\0','\0' };

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

    lstrcpyW(wszDir, typelib.wszFileName);
    pFileName = wszDir + lstrlenW(wszDir);
    while(*pFileName != '.' && *pFileName != '\\' && *pFileName != '/'
            && pFileName > wszDir) pFileName -= 1;
    if(*pFileName == '.')
    {
        *pFileName = '\0';
        while(*pFileName != '\\' && *pFileName != '/' && pFileName > wszDir)
            pFileName -= 1;
    }
    if(*pFileName == '\\' || *pFileName == '/') pFileName += 1;
    lstrcpyW(wszPath, pFileName);

177
    GetCurrentDirectoryW(MAX_LOAD_STRING, wszDir);
178

179
    saveidl.lStructSize = sizeof(OPENFILENAMEW);
180 181 182 183 184 185 186 187 188
    saveidl.hwndOwner = globals.hTypeLibWnd;
    saveidl.hInstance = globals.hMainInst;
    saveidl.lpstrFilter = wszIdlFiles;
    saveidl.lpstrFile = wszPath;
    saveidl.nMaxFile = MAX_LOAD_STRING;
    saveidl.lpstrInitialDir = wszDir;
    saveidl.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY;
    saveidl.lpstrDefExt = wszDefaultExt;

189
    if (GetSaveFileNameW(&saveidl))
190 191 192
        SaveIdl(wszPath);
}

193
static void AddToStrW(WCHAR *wszDest, const WCHAR *wszSource)
194 195 196 197
{
    lstrcpyW(&wszDest[lstrlenW(wszDest)], wszSource);
}

198
static void AddToTLDataStrW(TYPELIB_DATA *pTLData, const WCHAR *wszSource)
199 200 201 202 203
{
    int SourceLen = lstrlenW(wszSource);

    pTLData->idl = HeapReAlloc(GetProcessHeap(), 0, pTLData->idl,
            sizeof(WCHAR)*(pTLData->idlLen+SourceLen+1));
204

205 206 207 208
    memcpy(&pTLData->idl[pTLData->idlLen], wszSource, sizeof(WCHAR)*(SourceLen+1));
    pTLData->idlLen += SourceLen;
}

209
static void AddToTLDataStrWithTabsW(TYPELIB_DATA *pTLData, WCHAR *wszSource)
210 211 212
{
    int lineLen = lstrlenW(wszSource);
    int newLinesNo = 0;
213
    WCHAR *pSourcePos = wszSource;
214 215
    WCHAR *pSourceBeg;

216
    if(!lineLen) return;
217 218 219 220 221
    while(*pSourcePos)
    {
        if(*pSourcePos == *wszNewLine) newLinesNo++;
        pSourcePos += 1;
    }
222
    if(*(pSourcePos - 1) != *wszNewLine) newLinesNo++;
223 224 225 226

    pTLData->idl = HeapReAlloc(GetProcessHeap(), 0, pTLData->idl,
            sizeof(WCHAR)*(pTLData->idlLen+lineLen+4*newLinesNo+1));

227 228
    pSourcePos = wszSource;
    pSourceBeg = wszSource;
229
    while(newLinesNo)
230
    {
231
        if(*pSourcePos != *wszNewLine && *pSourcePos)
232 233 234 235
        {
            pSourcePos += 1;
            continue;
        }
236 237
        newLinesNo--;

238
        if(*pSourcePos)
239 240 241 242 243 244
        {
            *pSourcePos = '\0';
            lineLen = lstrlenW(pSourceBeg)+1;
            *pSourcePos = '\n';
            pSourcePos += 1;
        }
245
        else lineLen = lstrlenW(pSourceBeg);
246 247 248 249 250 251 252

        pTLData->idl[pTLData->idlLen] = *wszSpace;
        pTLData->idl[pTLData->idlLen+1] = *wszSpace;
        pTLData->idl[pTLData->idlLen+2] = *wszSpace;
        pTLData->idl[pTLData->idlLen+3] = *wszSpace;
        memcpy(&pTLData->idl[pTLData->idlLen+4], pSourceBeg, sizeof(WCHAR)*lineLen);
        pTLData->idlLen += lineLen + 4;
253
        pTLData->idl[pTLData->idlLen] = '\0';
254 255 256 257 258

        pSourceBeg = pSourcePos;
    }
}

259
static TYPELIB_DATA *InitializeTLData(void)
260 261 262
{
    TYPELIB_DATA *pTLData;

263
    pTLData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(TYPELIB_DATA));
264 265

    pTLData->idl = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR));
266
    pTLData->idl[0] = '\0';
267

268
    return pTLData;
269 270
}

271
static void AddSpaces(TYPELIB_DATA *pTLData, int tabSize)
272 273 274 275 276
{
    for(; tabSize>0; tabSize--)
        AddToTLDataStrW(pTLData, wszSpace);
}

277
static void AddChildrenData(HTREEITEM hParent, TYPELIB_DATA *pData)
278 279
{
    HTREEITEM hCur;
280
    TVITEMW tvi;
281

282
    memset(&tvi, 0, sizeof(tvi));
283

284 285
    hCur = (HTREEITEM)SendMessageW(typelib.hTree, TVM_GETNEXTITEM,
            TVGN_CHILD, (LPARAM)hParent);
286 287
    if(!hCur) return;

288 289 290
    do
    {
        tvi.hItem = hCur;
291
        SendMessageW(typelib.hTree, TVM_GETITEMW, 0, (LPARAM)&tvi);
292
        if(tvi.lParam && ((TYPELIB_DATA *)(tvi.lParam))->idlLen)
293
            AddToTLDataStrWithTabsW(pData, ((TYPELIB_DATA *)(tvi.lParam))->idl);
294 295
    } while((hCur = (HTREEITEM)SendMessageW(typelib.hTree, TVM_GETNEXTITEM,
            TVGN_NEXT, (LPARAM)hCur)));
296 297
}

298
static void CreateTypeInfo(WCHAR *wszAddTo, WCHAR *wszAddAfter, TYPEDESC tdesc, ITypeInfo *pTypeInfo)
299
{
300
    int i;
301 302 303
    BSTR bstrData;
    HRESULT hRes;
    ITypeInfo *pRefTypeInfo;
304 305
    WCHAR wszBuf[MAX_LOAD_STRING];
    WCHAR wszFormat[] = { '[','%','l','u',']','\0' };
306 307 308 309 310 311 312 313

    switch(tdesc.vt&VT_TYPEMASK)
    {
#define VTADDTOSTR(x) case x:\
        AddToStrW(wszAddTo, wsz##x);\
        break
        VTADDTOSTR(VT_BOOL);
        VTADDTOSTR(VT_UI1);
314
        VTADDTOSTR(VT_UI2);
315
        VTADDTOSTR(VT_UI4);
316 317 318 319
        VTADDTOSTR(VT_UI8);
        VTADDTOSTR(VT_UINT);
        VTADDTOSTR(VT_I1);
        VTADDTOSTR(VT_I2);
320
        VTADDTOSTR(VT_I4);
321
        VTADDTOSTR(VT_I8);
322 323 324 325
        VTADDTOSTR(VT_R4);
        VTADDTOSTR(VT_INT);
        VTADDTOSTR(VT_BSTR);
        VTADDTOSTR(VT_CY);
326 327 328 329 330
        VTADDTOSTR(VT_VARIANT);
        VTADDTOSTR(VT_VOID);
        VTADDTOSTR(VT_ERROR);
        VTADDTOSTR(VT_LPSTR);
        VTADDTOSTR(VT_LPWSTR);
331 332 333 334 335
        VTADDTOSTR(VT_HRESULT);
        VTADDTOSTR(VT_UNKNOWN);
        VTADDTOSTR(VT_DISPATCH);
        VTADDTOSTR(VT_DATE);
        VTADDTOSTR(VT_R8);
336 337 338 339 340 341 342 343
        case VT_CARRAY:
        for(i=0; i<U(tdesc).lpadesc->cDims; i++)
        {
            wsprintfW(wszBuf, wszFormat, U(tdesc).lpadesc->rgbounds[i].cElements);
            AddToStrW(wszAddAfter, wszBuf);
        }
        CreateTypeInfo(wszAddTo, wszAddAfter, U(tdesc).lpadesc->tdescElem, pTypeInfo);
        break;
344 345 346 347 348 349
	case VT_SAFEARRAY:
	AddToStrW(wszAddTo, wszVT_SAFEARRAY);
	AddToStrW(wszAddTo, wszOpenBrackets2);
        CreateTypeInfo(wszAddTo, wszAddAfter, *U(tdesc).lptdesc, pTypeInfo);
        AddToStrW(wszAddTo, wszCloseBrackets2);
	break;
350
        case VT_PTR:
351
        CreateTypeInfo(wszAddTo, wszAddAfter, *U(tdesc).lptdesc, pTypeInfo);
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
        AddToStrW(wszAddTo, wszAsterix);
        break;
        case VT_USERDEFINED:
        hRes = ITypeInfo_GetRefTypeInfo(pTypeInfo,
                U(tdesc).hreftype, &pRefTypeInfo);
        if(SUCCEEDED(hRes))
        {
            ITypeInfo_GetDocumentation(pRefTypeInfo, MEMBERID_NIL,
                    &bstrData, NULL, NULL, NULL);
            AddToStrW(wszAddTo, bstrData);
            SysFreeString(bstrData);
            ITypeInfo_Release(pRefTypeInfo);
        }
        else AddToStrW(wszAddTo, wszFailed);
        break;
        default:
        WINE_FIXME("tdesc.vt&VT_TYPEMASK == %d not supported\n",
                tdesc.vt&VT_TYPEMASK);
    }
}

373
static int EnumVars(ITypeInfo *pTypeInfo, int cVars, HTREEITEM hParent)
374 375
{
    int i;
376
    TVINSERTSTRUCTW tvis;
377 378 379 380 381
    VARDESC *pVarDesc;
    BSTR bstrName;
    WCHAR wszText[MAX_LOAD_STRING];
    WCHAR wszAfter[MAX_LOAD_STRING];

382
    U(tvis).item.mask = TVIF_TEXT|TVIF_PARAM;
383 384
    U(tvis).item.cchTextMax = MAX_LOAD_STRING;
    U(tvis).item.pszText = wszText;
385
    tvis.hInsertAfter = TVI_LAST;
386 387 388 389
    tvis.hParent = hParent;

    for(i=0; i<cVars; i++)
    {
390 391
        TYPELIB_DATA *tld;

392 393 394 395
        if(FAILED(ITypeInfo_GetVarDesc(pTypeInfo, i, &pVarDesc))) continue;
        if(FAILED(ITypeInfo_GetDocumentation(pTypeInfo, pVarDesc->memid, &bstrName,
                NULL, NULL, NULL))) continue;

396 397
        tld = InitializeTLData();
        U(tvis).item.lParam = (LPARAM) tld;
398 399
        if(pVarDesc->memid < MIN_VAR_ID)
        {
400 401 402 403

            AddToTLDataStrW(tld, wszOpenBrackets1);
            AddToTLDataStrW(tld, wszId);
            AddToTLDataStrW(tld, wszOpenBrackets2);
404
            wsprintfW(wszText, wszFormat, pVarDesc->memid);
405
            AddToTLDataStrW(tld, wszText);
406
            memset(wszText, 0, sizeof(wszText));
407
            AddToTLDataStrW(tld, wszCloseBrackets2);
408 409 410

            if(pVarDesc->wVarFlags & VARFLAG_FREADONLY)
            {
411 412 413
                AddToTLDataStrW(tld, wszComa);
                AddToTLDataStrW(tld, wszSpace);
                AddToTLDataStrW(tld, wszReadOnly);
414
            }
415 416
            AddToTLDataStrW(tld, wszCloseBrackets1);
            AddToTLDataStrW(tld, wszNewLine);
417 418
        }

419 420 421 422
        memset(wszText, 0, sizeof(wszText));
        memset(wszAfter, 0, sizeof(wszAfter));
        CreateTypeInfo(wszText, wszAfter, pVarDesc->elemdescVar.tdesc, pTypeInfo);
        AddToStrW(wszText, wszSpace);
423
        if (bstrName) AddToStrW(wszText, bstrName);
424
        AddToStrW(wszText, wszAfter);
425 426 427
        AddToTLDataStrW(tld, wszText);
        AddToTLDataStrW(tld, wszSemicolon);
        AddToTLDataStrW(tld, wszNewLine);
428

429
        SendMessageW(typelib.hTree, TVM_INSERTITEMW, 0, (LPARAM)&tvis);
430 431 432 433 434 435 436
        SysFreeString(bstrName);
        ITypeInfo_ReleaseVarDesc(pTypeInfo, pVarDesc);
    }

    return 0;
}

437
static int EnumEnums(ITypeInfo *pTypeInfo, int cVars, HTREEITEM hParent)
438 439
{
    int i;
440
    TVINSERTSTRUCTW tvis;
441 442 443
    VARDESC *pVarDesc;
    BSTR bstrName;
    WCHAR wszText[MAX_LOAD_STRING];
444
    WCHAR wszAfter[MAX_LOAD_STRING];
445 446 447 448

    U(tvis).item.mask = TVIF_TEXT|TVIF_PARAM;
    U(tvis).item.cchTextMax = MAX_LOAD_STRING;
    U(tvis).item.pszText = wszText;
449
    tvis.hInsertAfter = TVI_LAST;
450 451 452 453 454 455 456 457 458 459 460 461 462 463
    tvis.hParent = hParent;

    for(i=0; i<cVars; i++)
    {
        TYPELIB_DATA *tld;

        if(FAILED(ITypeInfo_GetVarDesc(pTypeInfo, i, &pVarDesc))) continue;
        if(FAILED(ITypeInfo_GetDocumentation(pTypeInfo, pVarDesc->memid, &bstrName,
                NULL, NULL, NULL))) continue;

        tld = InitializeTLData();
        U(tvis).item.lParam = (LPARAM) tld;

        memset(wszText, 0, sizeof(wszText));
464
        memset(wszAfter, 0, sizeof(wszAfter));
465 466 467 468 469

        if (pVarDesc->varkind == VAR_CONST)
        {
            VARIANT var;
            VariantInit(&var);
470
            if (VariantChangeType(&var, U(*pVarDesc).lpvarValue, 0, VT_BSTR) == S_OK)
471
            {
472
                AddToStrW(wszText, wszConst);
473
                AddToStrW(wszText, wszSpace);
474 475 476 477
                AddToStrW(wszAfter, wszSpace);
                AddToStrW(wszAfter, wszEquals);
                AddToStrW(wszAfter, wszSpace);
                AddToStrW(wszAfter, V_BSTR(&var));
478 479 480
            }
        }

481 482 483 484
        CreateTypeInfo(wszText, wszAfter, pVarDesc->elemdescVar.tdesc, pTypeInfo);
        AddToStrW(wszText, wszSpace);
        AddToStrW(wszText, bstrName);
        AddToStrW(wszText, wszAfter);
485
        AddToTLDataStrW(tld, bstrName);
486
        AddToTLDataStrW(tld, wszAfter);
487 488 489 490
	if (i<cVars-1)
            AddToTLDataStrW(tld, wszComa);
        AddToTLDataStrW(tld, wszNewLine);

491
        SendMessageW(typelib.hTree, TVM_INSERTITEMW, 0, (LPARAM)&tvis);
492 493 494 495 496 497 498
        SysFreeString(bstrName);
        ITypeInfo_ReleaseVarDesc(pTypeInfo, pVarDesc);
    }

    return 0;
}

499
static int EnumFuncs(ITypeInfo *pTypeInfo, TYPEATTR *pTypeAttr, HTREEITEM hParent)
500
{
501
    int i, j;
502
    int cFuncs;
503
    unsigned namesNo;
504
    TVINSERTSTRUCTW tvis;
505
    FUNCDESC *pFuncDesc;
506
    BSTR bstrName, bstrHelpString, *bstrParamNames;
507 508
    WCHAR wszText[MAX_LOAD_STRING];
    WCHAR wszAfter[MAX_LOAD_STRING];
509
    WCHAR szRhs[] = {'r','h','s',0};    /* Right-hand side of a propput */
510
    BOOL bFirst;
511

512
    U(tvis).item.mask = TVIF_TEXT|TVIF_PARAM;
513
    tvis.hInsertAfter = TVI_LAST;
514 515
    tvis.hParent = hParent;

516 517
    cFuncs = pTypeAttr->cFuncs;

518 519 520 521 522 523 524
    i = 0;
    if(pTypeAttr->wTypeFlags & TYPEFLAG_FDUAL) { /* skip 7 members of IDispatch */
        cFuncs += 7;
        i += 7;
    }

    for(; i<cFuncs; i++)
525
    {
526 527
        TYPELIB_DATA *tld;

528
        if(FAILED(ITypeInfo_GetFuncDesc(pTypeInfo, i, &pFuncDesc))) continue;
529

530
        if(FAILED(ITypeInfo_GetDocumentation(pTypeInfo, pFuncDesc->memid, &bstrName,
531
                &bstrHelpString, NULL, NULL))) continue;
532

533 534
        memset(wszText, 0, sizeof(wszText));
        memset(wszAfter, 0, sizeof(wszAfter));
535
        tld = InitializeTLData();
536 537
        U(tvis).item.cchTextMax = SysStringLen(bstrName);
        U(tvis).item.pszText = bstrName;
538
        U(tvis).item.lParam = (LPARAM) tld;
539
        bFirst = TRUE;
540
        if(pFuncDesc->memid < MIN_FUNC_ID || pTypeAttr->wTypeFlags & TYPEFLAG_FDUAL)
541
        {
542
            AddToTLDataStrW(tld, wszOpenBrackets1);
543
            bFirst = FALSE;
544 545
            AddToTLDataStrW(tld, wszId);
            AddToTLDataStrW(tld, wszOpenBrackets2);
546
            wsprintfW(wszText, wszFormat, pFuncDesc->memid);
547 548
            AddToTLDataStrW(tld, wszText);
            AddToTLDataStrW(tld, wszCloseBrackets2);
549 550
            memset(wszText, 0, sizeof(wszText));
        }
551

552 553 554 555
        CreateTypeInfo(wszText, wszAfter, pFuncDesc->elemdescFunc.tdesc, pTypeInfo);
        switch(pFuncDesc->invkind)
        {
            case INVOKE_PROPERTYGET:
556
                if(bFirst) AddToTLDataStrW(tld, wszOpenBrackets1);
557 558
                else
                {
559 560
                    AddToTLDataStrW(tld, wszComa);
                    AddToTLDataStrW(tld, wszSpace);
561 562
                }
                bFirst = FALSE;
563
                AddToTLDataStrW(tld, wszPropGet);
564 565
                break;
            case INVOKE_PROPERTYPUT:
566
                if(bFirst) AddToTLDataStrW(tld, wszOpenBrackets1);
567 568
                else
                {
569 570
                    AddToTLDataStrW(tld, wszComa);
                    AddToTLDataStrW(tld, wszSpace);
571 572
                }
                bFirst = FALSE;
573
                AddToTLDataStrW(tld, wszPropPut);
574 575
                break;
            case INVOKE_PROPERTYPUTREF:
576
                if(bFirst) AddToTLDataStrW(tld, wszOpenBrackets1);
577 578
                else
                {
579 580
                    AddToTLDataStrW(tld, wszComa);
                    AddToTLDataStrW(tld, wszSpace);
581 582
                }
                bFirst = FALSE;
583
                AddToTLDataStrW(tld, wszPropPutRef);
584 585 586
                break;
            default:;
        }
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
        if(SysStringLen(bstrHelpString))
        {
            if(bFirst) AddToTLDataStrW(tld, wszOpenBrackets1);
            else
            {
                AddToTLDataStrW(tld, wszComa);
                AddToTLDataStrW(tld, wszSpace);
            }
            bFirst = FALSE;
            AddToTLDataStrW(tld, wszHelpstring);
            AddToTLDataStrW(tld, wszOpenBrackets2);
            AddToTLDataStrW(tld, wszInvertedComa);
            AddToTLDataStrW(tld, bstrHelpString);
            AddToTLDataStrW(tld, wszInvertedComa);
            AddToTLDataStrW(tld, wszCloseBrackets2);
        }
603 604
        if(!bFirst)
        {
605 606
            AddToTLDataStrW(tld, wszCloseBrackets1);
            AddToTLDataStrW(tld, wszNewLine);
607
        }
608

609
        if(pTypeAttr->wTypeFlags & TYPEFLAG_FOLEAUTOMATION) {
610
            AddToTLDataStrW(tld, wszVT_HRESULT);
611 612
            if(strcmpW(wszText, wszVT_VOID)) pFuncDesc->cParams++;
        }
613 614 615 616
        else {
            AddToTLDataStrW(tld, wszText);
            AddToTLDataStrW(tld, wszAfter);
        }
617 618 619 620 621 622 623 624 625 626 627

        bstrParamNames = HeapAlloc(GetProcessHeap(), 0,
                sizeof(BSTR*)*(pFuncDesc->cParams+1));
        if(FAILED(ITypeInfo_GetNames(pTypeInfo, pFuncDesc->memid, bstrParamNames,
                pFuncDesc->cParams+1, &namesNo)))
        {
            HeapFree(GetProcessHeap(), 0, bstrParamNames);
            continue;
        }
        SysFreeString(bstrParamNames[0]);

628
        AddToTLDataStrW(tld, wszSpace);
629 630
        if(pFuncDesc->memid >= MIN_FUNC_ID)
        {
631 632
            AddToTLDataStrW(tld, wszStdCall);
            AddToTLDataStrW(tld, wszSpace);
633
        }
634
        if (bstrName) AddToTLDataStrW(tld, bstrName);
635
        AddToTLDataStrW(tld, wszOpenBrackets2);
636

637
        for(j=0; j<pFuncDesc->cParams; j++)
638
        {
639
            if(j != 0) AddToTLDataStrW(tld, wszComa);
640
            if(pFuncDesc->cParams != 1)
641
            {
642
                AddToTLDataStrW(tld, wszNewLine);
643
                AddSpaces(tld, TAB_SIZE);
644
            }
645 646
            bFirst = TRUE;
#define ENUM_PARAM_FLAG(x)\
647
            if(U(pFuncDesc->lprgelemdescParam[j]).paramdesc.wParamFlags & x) \
648
            {\
649
                if(bFirst) AddToTLDataStrW(tld,\
650 651 652
                        wszOpenBrackets1);\
                else\
                {\
653 654
                    AddToTLDataStrW(tld, wszComa);\
                    AddToTLDataStrW(tld, wszSpace);\
655 656
                }\
                bFirst = FALSE;\
657
                AddToTLDataStrW(tld, wsz##x);\
658 659 660 661 662 663 664 665
            }
            ENUM_PARAM_FLAG(PARAMFLAG_FIN);
            ENUM_PARAM_FLAG(PARAMFLAG_FOUT);
            ENUM_PARAM_FLAG(PARAMFLAG_FLCID);
            ENUM_PARAM_FLAG(PARAMFLAG_FRETVAL);
            ENUM_PARAM_FLAG(PARAMFLAG_FOPT);
            ENUM_PARAM_FLAG(PARAMFLAG_FHASCUSTDATA);

666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
            if(U(pFuncDesc->lprgelemdescParam[j]).paramdesc.wParamFlags & PARAMFLAG_FHASDEFAULT)
            {
		VARIANT var, *param=&U(pFuncDesc->lprgelemdescParam[j]).paramdesc.pparamdescex->varDefaultValue;
		VariantInit(&var);
                if(bFirst) AddToTLDataStrW(tld,
                        wszOpenBrackets1);
                else
                {
                    AddToTLDataStrW(tld, wszComa);
                    AddToTLDataStrW(tld, wszSpace);
                }
                bFirst = FALSE;
                AddToTLDataStrW(tld, wszDefaultValue);
                AddToTLDataStrW(tld, wszOpenBrackets2);
		if (V_VT(param) == VT_BSTR)
		{
		    AddToTLDataStrW(tld, wszInvertedComa);
683
		    AddToTLDataStrW(tld, V_BSTR(param));
684 685 686 687 688 689
		    AddToTLDataStrW(tld, wszInvertedComa);
		} else if (VariantChangeType(&var, param, 0, VT_BSTR) == S_OK)
		    AddToTLDataStrW(tld, V_BSTR(&var));
                AddToTLDataStrW(tld, wszCloseBrackets2);
            }

690 691
            if(!bFirst)
            {
692 693
                AddToTLDataStrW(tld, wszCloseBrackets1);
                AddToTLDataStrW(tld, wszSpace);
694 695 696 697 698 699
            }

            memset(wszText, 0, sizeof(wszText));
            memset(wszAfter, 0, sizeof(wszAfter));
            CreateTypeInfo(wszText, wszAfter, pFuncDesc->lprgelemdescParam[j].tdesc,
                    pTypeInfo);
700 701 702
            AddToTLDataStrW(tld, wszText);
            AddToTLDataStrW(tld, wszAfter);
            AddToTLDataStrW(tld, wszSpace);
703
            if (j+1 < namesNo) {
704 705 706 707 708
                if (bstrParamNames[j+1])
                {
                    AddToTLDataStrW(tld, bstrParamNames[j+1]);
                    SysFreeString(bstrParamNames[j+1]);
                }
709 710 711
            } else {
                AddToTLDataStrW(tld, szRhs);
            }
712
        }
713 714 715
        AddToTLDataStrW(tld, wszCloseBrackets2);
        AddToTLDataStrW(tld, wszSemicolon);
        AddToTLDataStrW(tld, wszNewLine);
716

717
        SendMessageW(typelib.hTree, TVM_INSERTITEMW, 0, (LPARAM)&tvis);
718
        HeapFree(GetProcessHeap(), 0, bstrParamNames);
719
        SysFreeString(bstrName);
720
        SysFreeString(bstrHelpString);
721 722 723 724 725 726
        ITypeInfo_ReleaseFuncDesc(pTypeInfo, pFuncDesc);
    }

    return 0;
}

727
static int EnumImplTypes(ITypeInfo *pTypeInfo, int cImplTypes, HTREEITEM hParent)
728 729
{
    int i;
730
    TVINSERTSTRUCTW tvis;
731 732 733 734 735 736 737 738
    ITypeInfo *pRefTypeInfo;
    HREFTYPE hRefType;
    TYPEATTR *pTypeAttr;
    BSTR bstrName;
    WCHAR wszInheritedInterfaces[MAX_LOAD_STRING];

    if(!cImplTypes) return 0;

739
    LoadStringW(globals.hMainInst, IDS_INHERITINTERFACES, wszInheritedInterfaces,
740
            sizeof(wszInheritedInterfaces)/sizeof(wszInheritedInterfaces[0]));
741 742 743 744

    U(tvis).item.mask = TVIF_TEXT;
    U(tvis).item.cchTextMax = MAX_LOAD_STRING;
    U(tvis).item.pszText = wszInheritedInterfaces;
745
    tvis.hInsertAfter = TVI_LAST;
746 747
    tvis.hParent = hParent;

748
    tvis.hParent = TreeView_InsertItemW(typelib.hTree, &tvis);
749 750 751 752 753 754 755

    for(i=0; i<cImplTypes; i++)
    {
        if(FAILED(ITypeInfo_GetRefTypeOfImplType(pTypeInfo, i, &hRefType))) continue;
        if(FAILED(ITypeInfo_GetRefTypeInfo(pTypeInfo, hRefType, &pRefTypeInfo)))
            continue;
        if(FAILED(ITypeInfo_GetDocumentation(pRefTypeInfo, MEMBERID_NIL, &bstrName,
756 757 758 759 760 761 762 763 764 765
                NULL, NULL, NULL)))
        {
            ITypeInfo_Release(pRefTypeInfo);
            continue;
        }
        if(FAILED(ITypeInfo_GetTypeAttr(pRefTypeInfo, &pTypeAttr)))
        {
            ITypeInfo_Release(pRefTypeInfo);
            continue;
        }
766 767 768 769

        U(tvis).item.cchTextMax = SysStringLen(bstrName);
        U(tvis).item.pszText = bstrName;

770
        hParent = TreeView_InsertItemW(typelib.hTree, &tvis);
771
        EnumVars(pRefTypeInfo, pTypeAttr->cVars, hParent);
772
        EnumFuncs(pRefTypeInfo, pTypeAttr, hParent);
773 774 775 776 777 778 779 780 781 782
        EnumImplTypes(pRefTypeInfo, pTypeAttr->cImplTypes, hParent);

        SysFreeString(bstrName);
        ITypeInfo_ReleaseTypeAttr(pRefTypeInfo, pTypeAttr);
        ITypeInfo_Release(pRefTypeInfo);
    }

    return 0;
}

783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
static void EnumCoclassImplTypes(ITypeInfo *pTypeInfo,
        int cImplTypes, TYPELIB_DATA *pTLData)
{
    int i;
    ITypeInfo *pRefTypeInfo;
    HREFTYPE hRefType;
    TYPEATTR *pTypeAttr;
    BSTR bstrName;
    BOOL bFirst;
    INT flags;
    const WCHAR wszTKIND_INTERFACE[] = { 'i','n','t','e','r','f','a','c','e',' ','\0' };
    const WCHAR wszTKIND_DISPATCH[]
        = { 'd','i','s','p','i','n','t','e','r','f','a','c','e',' ','\0' };
    const WCHAR wszIMPLTYPEFLAG_FDEFAULT[]
        = { 'd','e','f','a','u','l','t','\0' };
    const WCHAR wszIMPLTYPEFLAG_FSOURCE[]
        = { 's','o','u','r','c','e','\0' };
    const WCHAR wszIMPLTYPEFLAG_FRESTRICTED[]
        = { 'r','e','s','t','r','i','c','t','e','d','\0' };

    for(i=0; i<cImplTypes; i++)
    {
        if(FAILED(ITypeInfo_GetRefTypeOfImplType(pTypeInfo, i, &hRefType))) continue;
        if(FAILED(ITypeInfo_GetRefTypeInfo(pTypeInfo, hRefType, &pRefTypeInfo)))
            continue;
        if(FAILED(ITypeInfo_GetDocumentation(pRefTypeInfo, MEMBERID_NIL, &bstrName,
                        NULL, NULL, NULL)))
        {
            ITypeInfo_Release(pRefTypeInfo);
            continue;
        }
        if(FAILED(ITypeInfo_GetTypeAttr(pRefTypeInfo, &pTypeAttr)))
        {
            ITypeInfo_Release(pRefTypeInfo);
            continue;
        }

820
        AddSpaces(pTLData, TAB_SIZE);
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
        ITypeInfo_GetImplTypeFlags(pTypeInfo, i, &flags);
        bFirst = TRUE;
#define ENUM_IMPLTYPEFLAG(x)\
        if(flags & x) \
        {\
            if(bFirst) AddToTLDataStrW(pTLData,\
                    wszOpenBrackets1);\
            else\
            {\
                AddToTLDataStrW(pTLData, wszComa);\
                AddToTLDataStrW(pTLData, wszSpace);\
            }\
            bFirst = FALSE;\
            AddToTLDataStrW(pTLData, wsz##x);\
        }
        ENUM_IMPLTYPEFLAG(IMPLTYPEFLAG_FDEFAULT);
        ENUM_IMPLTYPEFLAG(IMPLTYPEFLAG_FSOURCE);
        ENUM_IMPLTYPEFLAG(IMPLTYPEFLAG_FRESTRICTED);
        if(!bFirst)
        {
            AddToTLDataStrW(pTLData, wszCloseBrackets1);
            AddToTLDataStrW(pTLData, wszSpace);
        }

845 846
        if(pTypeAttr->typekind == TKIND_INTERFACE ||
                (pTypeAttr->wTypeFlags & TYPEFLAG_FDUAL))
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
            AddToTLDataStrW(pTLData, wszTKIND_INTERFACE);
        else if(pTypeAttr->typekind == TKIND_DISPATCH)
            AddToTLDataStrW(pTLData, wszTKIND_DISPATCH);
        AddToTLDataStrW(pTLData, wszSpace);

        AddToTLDataStrW(pTLData, bstrName);
        AddToTLDataStrW(pTLData, wszSemicolon);
        AddToTLDataStrW(pTLData, wszNewLine);

        SysFreeString(bstrName);
        ITypeInfo_ReleaseTypeAttr(pRefTypeInfo, pTypeAttr);
        ITypeInfo_Release(pRefTypeInfo);
    }
}

862
static void AddIdlData(HTREEITEM hCur, TYPELIB_DATA *pTLData)
863
{
864
    TVITEMW tvi;
865

866 867
    hCur = (HTREEITEM)SendMessageW(typelib.hTree, TVM_GETNEXTITEM,
            TVGN_CHILD, (LPARAM)hCur);
868
    memset(&tvi, 0, sizeof(TVITEMW));
869 870 871 872 873
    tvi.mask = TVIF_PARAM;

    while(hCur)
    {
        tvi.hItem = hCur;
874
        SendMessageW(typelib.hTree, TVM_GETITEMW, 0, (LPARAM)&tvi);
875 876 877 878
        if(!((TYPELIB_DATA*)(tvi.lParam))->bHide) {
            AddToTLDataStrW(pTLData, wszNewLine);
            AddToTLDataStrWithTabsW(pTLData, ((TYPELIB_DATA*)(tvi.lParam))->idl);
        }
879 880
        hCur = (HTREEITEM)SendMessageW(typelib.hTree, TVM_GETNEXTITEM,
                TVGN_NEXT, (LPARAM)hCur);
881 882 883
    }
}

884
static void AddPredefinitions(HTREEITEM hFirst, TYPELIB_DATA *pTLData)
885 886
{
    HTREEITEM hCur;
887
    TVITEMW tvi;
888 889 890 891 892 893
    WCHAR wszText[MAX_LOAD_STRING];
    WCHAR wszPredefinition[] = { '/','/',' ','T','L','i','b',' ',':','\n',
        '/','/',' ','F','o','r','w','a','r','d',' ','d','e','c','l','a','r','e',' ',
        'a','l','l',' ','t','y','p','e','s',' ','d','e','f','i','n','e','d',' ',
        'i','n',' ','t','h','i','s',' ','t','y','p','e','l','i','b','\0' };

894 895
    hFirst = (HTREEITEM)SendMessageW(typelib.hTree, TVM_GETNEXTITEM,
            TVGN_CHILD, (LPARAM)hFirst);
896 897

    AddToTLDataStrWithTabsW(pTLData, wszPredefinition);
898
    AddToTLDataStrW(pTLData, wszNewLine);
899 900

    hCur = hFirst;
901
    memset(&tvi, 0, sizeof(TVITEMW));
902 903 904 905 906 907
    tvi.mask = TVIF_TEXT|TVIF_PARAM;
    tvi.cchTextMax = MAX_LOAD_STRING;
    tvi.pszText = wszText;
    while(hCur)
    {
        tvi.hItem = hCur;
908
        SendMessageW(typelib.hTree, TVM_GETITEMW, 0, (LPARAM)&tvi);
909 910
        if(((TYPELIB_DATA*)(tvi.lParam))->bPredefine &&
                !((TYPELIB_DATA*)(tvi.lParam))->bHide)
911 912 913
        {
            AddToStrW(wszText, wszSemicolon);
            AddToTLDataStrWithTabsW(pTLData, wszText);
914
            AddToTLDataStrW(pTLData, wszNewLine);
915
        }
916 917
        hCur = (HTREEITEM)SendMessageW(typelib.hTree, TVM_GETNEXTITEM,
                TVGN_NEXT, (LPARAM)hCur);
918 919 920
    }
}

921
static void CreateInterfaceInfo(ITypeInfo *pTypeInfo, int cImplTypes, WCHAR *wszName,
922
        WCHAR *wszHelpString, ULONG ulHelpContext, TYPEATTR *pTypeAttr,
923
        TYPELIB_DATA *pTLData)
924 925 926 927 928
{
    ITypeInfo *pRefTypeInfo;
    HREFTYPE hRefType;
    BSTR bstrName;
    WCHAR wszGuid[MAX_LOAD_STRING];
929
    WCHAR wszHelpContext[MAX_LOAD_STRING];
930

931
    const WCHAR wszInterface[] = { 'i','n','t','e','r','f','a','c','e',' ','\0' };
932 933
    const WCHAR wszDispinterface[]
        = { 'd','i','s','p','i','n','t','e','r','f','a','c','e',' ','\0' };
934
    const WCHAR wszHelpcontext[] = { 'h','e','l','p','c','o','n','t','e','x','t','\0' };
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
    const WCHAR wszTYPEFLAG_FAPPOBJECT[] = { 'a','p','p','o','b','j','e','c','t','\0' };
    const WCHAR wszTYPEFLAG_FCANCREATE[] = { 'c','a','n','c','r','e','a','t','e','\0' };
    const WCHAR wszTYPEFLAG_FLICENSED[] = { 'l','i','c','e','n','s','e','d','\0' };
    const WCHAR wszTYPEFLAG_FPREDECLID[] = { 'p','r','e','d','e','c','l','i','d','\0' };
    const WCHAR wszTYPEFLAG_FHIDDEN[] = { 'h','i','d','d','e','n','\0' };
    const WCHAR wszTYPEFLAG_FCONTROL[] = { 'c','o','n','t','r','o','l','\0' };
    const WCHAR wszTYPEFLAG_FDUAL[] = { 'd','u','a','l','\0' };
    const WCHAR wszTYPEFLAG_FNONEXTENSIBLE[]
        = { 'n','o','n','e','x','t','e','n','s','i','b','l','e','\0' };
    const WCHAR wszTYPEFLAG_FOLEAUTOMATION[]
        = { 'o','l','e','a','u','t','o','m','a','t','i','o','n','\0' };
    const WCHAR wszTYPEFLAG_FRESTRICTED[]
        = { 'r','e','s','t','r','i','c','t','e','d','\0' };
    const WCHAR wszTYPEFLAG_FAGGREGATABLE[]
        = { 'a','g','g','r','e','g','a','t','a','b','l','e','\0' };
    const WCHAR wszTYPEFLAG_FREPLACEABLE[]
        = { 'r','e','p','l','a','c','a','b','l','e','\0' };
    const WCHAR wszTYPEFLAG_FREVERSEBIND[]
        = { 'r','e','v','e','r','s','e','b','i','n','d','\0' };
    const WCHAR wszTYPEFLAG_FPROXY[] = { 'p','r','o','x','y','\0' };

    AddToTLDataStrW(pTLData, wszOpenBrackets1);
    AddToTLDataStrW(pTLData, wszNewLine);
958 959
    if(pTypeAttr->typekind != TKIND_DISPATCH)
    {
960
        AddSpaces(pTLData, TAB_SIZE);
961 962 963 964
        AddToTLDataStrW(pTLData, wszOdl);
        AddToTLDataStrW(pTLData, wszComa);
        AddToTLDataStrW(pTLData, wszNewLine);
    }
965
    AddSpaces(pTLData, TAB_SIZE);
966 967 968 969 970 971 972 973 974 975
    AddToTLDataStrW(pTLData, wszUUID);
    AddToTLDataStrW(pTLData, wszOpenBrackets2);
    StringFromGUID2(&(pTypeAttr->guid), wszGuid, MAX_LOAD_STRING);
    wszGuid[lstrlenW(wszGuid)-1] = '\0';
    AddToTLDataStrW(pTLData, &wszGuid[1]);
    AddToTLDataStrW(pTLData, wszCloseBrackets2);
    if(wszHelpString)
    {
        AddToTLDataStrW(pTLData, wszComa);
        AddToTLDataStrW(pTLData, wszNewLine);
976
        AddSpaces(pTLData, TAB_SIZE);
977 978 979 980 981 982 983
        AddToTLDataStrW(pTLData, wszHelpstring);
        AddToTLDataStrW(pTLData, wszOpenBrackets2);
        AddToTLDataStrW(pTLData, wszInvertedComa);
        AddToTLDataStrW(pTLData, wszHelpString);
        AddToTLDataStrW(pTLData, wszInvertedComa);
        AddToTLDataStrW(pTLData, wszCloseBrackets2);
    }
984 985 986 987
    if(ulHelpContext)
    {
        AddToTLDataStrW(pTLData, wszComa);
        AddToTLDataStrW(pTLData, wszNewLine);
988
        AddSpaces(pTLData, TAB_SIZE);
989 990 991 992 993 994
        AddToTLDataStrW(pTLData, wszHelpcontext);
        AddToTLDataStrW(pTLData, wszOpenBrackets2);
        wsprintfW(wszHelpContext, wszFormat, ulHelpContext);
        AddToTLDataStrW(pTLData, wszHelpContext);
        AddToTLDataStrW(pTLData, wszCloseBrackets2);
    }
995 996
    if(pTypeAttr->wTypeFlags)
    {
997
#define ENUM_FLAGS(x) if(pTypeAttr->wTypeFlags & x)\
998
        {\
999 1000
            AddToTLDataStrW(pTLData, wszComa);\
            AddToTLDataStrW(pTLData, wszNewLine);\
1001
            AddSpaces(pTLData, TAB_SIZE);\
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
            AddToTLDataStrW(pTLData, wsz##x);\
        }
        ENUM_FLAGS(TYPEFLAG_FAPPOBJECT);
        ENUM_FLAGS(TYPEFLAG_FCANCREATE);
        ENUM_FLAGS(TYPEFLAG_FLICENSED);
        ENUM_FLAGS(TYPEFLAG_FPREDECLID);
        ENUM_FLAGS(TYPEFLAG_FHIDDEN);
        ENUM_FLAGS(TYPEFLAG_FCONTROL);
        ENUM_FLAGS(TYPEFLAG_FDUAL);
        ENUM_FLAGS(TYPEFLAG_FNONEXTENSIBLE);
        ENUM_FLAGS(TYPEFLAG_FOLEAUTOMATION);
        ENUM_FLAGS(TYPEFLAG_FRESTRICTED);
        ENUM_FLAGS(TYPEFLAG_FAGGREGATABLE);
        ENUM_FLAGS(TYPEFLAG_FREPLACEABLE);
        ENUM_FLAGS(TYPEFLAG_FREVERSEBIND);
        ENUM_FLAGS(TYPEFLAG_FPROXY);
    }
    AddToTLDataStrW(pTLData, wszNewLine);
    AddToTLDataStrW(pTLData, wszCloseBrackets1);
    AddToTLDataStrW(pTLData, wszNewLine);
1022 1023
    if(pTypeAttr->typekind != TKIND_DISPATCH) AddToTLDataStrW(pTLData, wszInterface);
    else AddToTLDataStrW(pTLData, wszDispinterface);
1024 1025
    AddToTLDataStrW(pTLData, wszName);
    AddToTLDataStrW(pTLData, wszSpace);
1026
    if(cImplTypes && pTypeAttr->typekind != TKIND_DISPATCH)
1027 1028 1029 1030 1031
    {
        AddToTLDataStrW(pTLData, wszColon);
        AddToTLDataStrW(pTLData, wszSpace);

        ITypeInfo_GetRefTypeOfImplType(pTypeInfo, 0, &hRefType);
1032 1033 1034
        if (SUCCEEDED(ITypeInfo_GetRefTypeInfo(pTypeInfo, hRefType, &pRefTypeInfo)))
        {
            ITypeInfo_GetDocumentation(pRefTypeInfo, MEMBERID_NIL, &bstrName,
1035
                NULL, NULL, NULL);
1036 1037
            AddToTLDataStrW(pTLData, bstrName);
            AddToTLDataStrW(pTLData, wszSpace);
1038

1039 1040 1041 1042 1043
            SysFreeString(bstrName);
            ITypeInfo_Release(pRefTypeInfo);
        }
        else
            AddToTLDataStrW(pTLData, wszFailed);
1044 1045 1046 1047 1048 1049 1050 1051 1052
    }
    AddToTLDataStrW(pTLData, wszOpenBrackets3);
    AddToTLDataStrW(pTLData, wszNewLine);

    AddToStrW(pTLData->wszInsertAfter, wszCloseBrackets3);
    AddToStrW(pTLData->wszInsertAfter, wszSemicolon);
    AddToStrW(pTLData->wszInsertAfter, wszNewLine);
}

1053
static void CreateTypedefHeader(ITypeInfo *pTypeInfo,
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
        TYPEATTR *pTypeAttr, TYPELIB_DATA *pTLData)
{
    BOOL bFirst = TRUE;
    WCHAR wszGuid[MAX_LOAD_STRING];
    const WCHAR wszTypedef[] = { 't','y','p','e','d','e','f',' ','\0' };
    const WCHAR wszPublic[] = { 'p','u','b','l','i','c','\0' };

    AddToTLDataStrW(pTLData, wszTypedef);
    if(memcmp(&pTypeAttr->guid, &GUID_NULL, sizeof(GUID)))
    {
1064
        AddToTLDataStrW(pTLData, wszOpenBrackets1);
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
        bFirst = FALSE;
        AddToTLDataStrW(pTLData, wszUUID);
        AddToTLDataStrW(pTLData, wszOpenBrackets2);
        StringFromGUID2(&(pTypeAttr->guid), wszGuid, MAX_LOAD_STRING);
        wszGuid[lstrlenW(wszGuid)-1] = '\0';
        AddToTLDataStrW(pTLData, &wszGuid[1]);
        AddToTLDataStrW(pTLData, wszCloseBrackets2);
    }
    if(pTypeAttr->typekind == TKIND_ALIAS)
    {
        if(bFirst) AddToTLDataStrW(pTLData, wszOpenBrackets1);
        else
        {
            AddToTLDataStrW(pTLData, wszComa);
            AddToTLDataStrW(pTLData, wszSpace);
        }
        bFirst = FALSE;
        AddToTLDataStrW(pTLData, wszPublic);
    }
    if(!bFirst)
    {
        AddToTLDataStrW(pTLData, wszCloseBrackets1);
        AddToTLDataStrW(pTLData, wszNewLine);
    }
}

1091 1092 1093
static void CreateCoclassHeader(ITypeInfo *pTypeInfo,
        TYPEATTR *pTypeAttr, TYPELIB_DATA *pTLData)
{
1094 1095 1096 1097 1098
    WCHAR wszGuid[MAX_LOAD_STRING];
    BSTR bstrHelpString;
    const WCHAR wszNoncreatable[]
        = { 'n','o','n','c','r','e','a','t','a','b','l','e','\0' };

1099 1100 1101
    AddToTLDataStrW(pTLData, wszOpenBrackets1);
    AddToTLDataStrW(pTLData, wszNewLine);

1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
    AddSpaces(pTLData, TAB_SIZE);
    AddToTLDataStrW(pTLData, wszUUID);
    AddToTLDataStrW(pTLData, wszOpenBrackets2);
    StringFromGUID2(&(pTypeAttr->guid), wszGuid, MAX_LOAD_STRING);
    wszGuid[lstrlenW(wszGuid)-1] = '\0';
    AddToTLDataStrW(pTLData, &wszGuid[1]);
    AddToTLDataStrW(pTLData, wszCloseBrackets2);

    if(SUCCEEDED(ITypeInfo_GetDocumentation(pTypeInfo, MEMBERID_NIL, NULL,
            &bstrHelpString, NULL, NULL)))
    {
        if(SysStringLen(bstrHelpString))
        {
            AddToTLDataStrW(pTLData, wszComa);
            AddToTLDataStrW(pTLData, wszNewLine);
            AddSpaces(pTLData, TAB_SIZE);
            AddToTLDataStrW(pTLData, wszHelpstring);
1119
            AddToTLDataStrW(pTLData, wszOpenBrackets2);
1120 1121 1122
            AddToTLDataStrW(pTLData, wszInvertedComa);
            AddToTLDataStrW(pTLData, bstrHelpString);
            AddToTLDataStrW(pTLData, wszInvertedComa);
1123
            AddToTLDataStrW(pTLData, wszCloseBrackets2);
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
        }
        SysFreeString(bstrHelpString);
    }

    if(!(pTypeAttr->wTypeFlags & TYPEFLAG_FCANCREATE))
    {
        AddToTLDataStrW(pTLData, wszComa);
        AddToTLDataStrW(pTLData, wszNewLine);
        AddSpaces(pTLData, TAB_SIZE);
        AddToTLDataStrW(pTLData, wszNoncreatable);
    }

    AddToTLDataStrW(pTLData, wszNewLine);
1137 1138 1139 1140
    AddToTLDataStrW(pTLData, wszCloseBrackets1);
    AddToTLDataStrW(pTLData, wszNewLine);
}

1141
static int PopulateTree(void)
1142
{
1143 1144
    TVINSERTSTRUCTW tvis;
    TVITEMW tvi;
1145
    ITypeLib *pTypeLib;
1146
    TLIBATTR *pTLibAttr;
1147 1148
    ITypeInfo *pTypeInfo, *pRefTypeInfo;
    HREFTYPE hRefType;
1149
    TYPEATTR *pTypeAttr;
1150
    INT count, i;
1151
    ULONG ulHelpContext;
1152
    BSTR bstrName;
1153 1154
    BSTR bstrData;
    WCHAR wszText[MAX_LOAD_STRING];
1155
    WCHAR wszAfter[MAX_LOAD_STRING];
1156
    HRESULT hRes;
1157
    HTREEITEM hParent;
1158 1159 1160
    HTREEITEM hMain;
    BOOL bInsert;
    TYPELIB_DATA *tldDispatch;
1161
    TYPELIB_DATA *tld;
1162

1163 1164 1165 1166 1167 1168
    const WCHAR wszGeneratedInfo[] = { '/','/',' ','G','e','n','e','r','a','t','e','d',
        ' ','.','I','D','L',' ','f','i','l','e',' ','(','b','y',' ','t','h','e',' ',
        'O','L','E','/','C','O','M',' ','O','b','j','e','c','t',' ',
        'V','i','e','w','e','r',')','\n','/','/','\n','/','/',' ',
        't','y','p','e','l','i','b',' ','f','i','l','e','n','a','m','e',':',' ','\0'};

1169
    const WCHAR wszFormat[] = { '%','s',' ','(','%','s',')','\0' };
1170 1171
    const WCHAR wszFormat2[] = { 'v','e','r','s','i','o','n',
        '(','%','l','d','.','%','l','d',')','\0' };
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184

    const WCHAR wszTKIND_ENUM[] = { 't','y','p','e','d','e','f',' ','e','n','u','m',' ','\0' };
    const WCHAR wszTKIND_RECORD[]
        = { 't','y','p','e','d','e','f',' ','s','t','r','u','c','t',' ','\0' };
    const WCHAR wszTKIND_MODULE[] = { 'm','o','d','u','l','e',' ','\0' };
    const WCHAR wszTKIND_INTERFACE[] = { 'i','n','t','e','r','f','a','c','e',' ','\0' };
    const WCHAR wszTKIND_DISPATCH[]
        = { 'd','i','s','p','i','n','t','e','r','f','a','c','e',' ','\0' };
    const WCHAR wszTKIND_COCLASS[] = { 'c','o','c','l','a','s','s',' ','\0' };
    const WCHAR wszTKIND_ALIAS[] = { 't','y','p','e','d','e','f',' ','\0' };
    const WCHAR wszTKIND_UNION[]
        = { 't','y','p','e','d','e','f',' ','u','n','i','o','n',' ','\0' };

1185
    const WCHAR wszLibrary[] = { 'l','i','b','r','a','r','y',' ','\0' };
1186
    const WCHAR wszTag[] = { 't','a','g','\0' };
1187

1188 1189 1190
    WCHAR wszProperties[] = { 'p','r','o','p','e','r','t','i','e','s','\0' };
    WCHAR wszMethods[] = { 'm','e','t','h','o','d','s','\0' };

1191
    U(tvis).item.mask = TVIF_TEXT|TVIF_PARAM;
1192 1193
    U(tvis).item.cchTextMax = MAX_LOAD_STRING;
    U(tvis).item.pszText = wszText;
1194
    tvis.hInsertAfter = TVI_LAST;
1195 1196
    tvis.hParent = TVI_ROOT;

1197
    if(FAILED((hRes = LoadTypeLib(typelib.wszFileName, &pTypeLib))))
1198 1199 1200 1201
    {
        WCHAR wszMessage[MAX_LOAD_STRING];
        WCHAR wszError[MAX_LOAD_STRING];

1202
        LoadStringW(globals.hMainInst, IDS_ERROR_LOADTYPELIB,
1203
                wszError, sizeof(wszError)/sizeof(wszError[0]));
1204
        wsprintfW(wszMessage, wszError, typelib.wszFileName, hRes);
1205
        MessageBoxW(globals.hMainWnd, wszMessage, NULL, MB_OK|MB_ICONEXCLAMATION);
1206 1207
        return 1;
    }
1208 1209
    count = ITypeLib_GetTypeInfoCount(pTypeLib);

1210
    ITypeLib_GetDocumentation(pTypeLib, -1, &bstrName, &bstrData, NULL, NULL);
1211
    ITypeLib_GetLibAttr(pTypeLib, &pTLibAttr);
1212 1213 1214 1215 1216 1217 1218 1219 1220

    tld = InitializeTLData();
    U(tvis).item.lParam = (LPARAM) tld;
    AddToTLDataStrW(tld, wszGeneratedInfo);
    AddToTLDataStrW(tld, typelib.wszFileName);
    AddToTLDataStrW(tld, wszNewLine);
    AddToTLDataStrW(tld, wszNewLine);
    AddToTLDataStrW(tld, wszOpenBrackets1);
    AddToTLDataStrW(tld, wszNewLine);
1221
    AddSpaces(tld, TAB_SIZE);
1222 1223
    AddToTLDataStrW(tld, wszUUID);
    AddToTLDataStrW(tld, wszOpenBrackets2);
1224 1225
    StringFromGUID2(&(pTLibAttr->guid), wszText, MAX_LOAD_STRING);
    wszText[lstrlenW(wszText)-1] = '\0';
1226 1227 1228 1229
    AddToTLDataStrW(tld, &wszText[1]);
    AddToTLDataStrW(tld, wszCloseBrackets2);
    AddToTLDataStrW(tld, wszComa);
    AddToTLDataStrW(tld, wszNewLine);
1230
    AddSpaces(tld, TAB_SIZE);
1231
    wsprintfW(wszText, wszFormat2, pTLibAttr->wMajorVerNum, pTLibAttr->wMinorVerNum);
1232
    AddToTLDataStrW(tld, wszText);
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247

    if (bstrData)
    {
        /* helpstring is optional */
        AddToTLDataStrW(tld, wszComa);
        AddToTLDataStrW(tld, wszNewLine);
        AddSpaces(tld, TAB_SIZE);
        AddToTLDataStrW(tld, wszHelpstring);
        AddToTLDataStrW(tld, wszOpenBrackets2);
        AddToTLDataStrW(tld, wszInvertedComa);
        AddToTLDataStrW(tld, bstrData);
        AddToTLDataStrW(tld, wszInvertedComa);
        AddToTLDataStrW(tld, wszCloseBrackets2);
    }

1248 1249 1250 1251
    AddToTLDataStrW(tld, wszNewLine);
    AddToTLDataStrW(tld, wszCloseBrackets1);
    AddToTLDataStrW(tld, wszNewLine);
    AddToTLDataStrW(tld, wszLibrary);
1252
    if (bstrName) AddToTLDataStrW(tld, bstrName);
1253 1254 1255 1256 1257 1258
    AddToTLDataStrW(tld, wszNewLine);
    AddToTLDataStrW(tld, wszOpenBrackets3);
    AddToTLDataStrW(tld, wszNewLine);

    AddToStrW(tld->wszInsertAfter, wszCloseBrackets3);
    AddToStrW(tld->wszInsertAfter, wszSemicolon);
1259

1260 1261 1262
    wsprintfW(wszText, wszFormat, bstrName, bstrData);
    SysFreeString(bstrName);
    SysFreeString(bstrData);
1263 1264
    tvis.hParent = (HTREEITEM)SendMessageW(typelib.hTree,
            TVM_INSERTITEMW, 0, (LPARAM)&tvis);
1265

1266 1267
    for(i=0; i<count; i++)
    {
1268
        bInsert = TRUE;
1269
        ITypeLib_GetTypeInfo(pTypeLib, i, &pTypeInfo);
1270

1271
        ITypeInfo_GetDocumentation(pTypeInfo, MEMBERID_NIL, &bstrName, &bstrData,
1272
                &ulHelpContext, NULL);
1273
        ITypeInfo_GetTypeAttr(pTypeInfo, &pTypeAttr);
1274

1275
        memset(wszText, 0, sizeof(wszText));
1276
        memset(wszAfter, 0, sizeof(wszAfter));
1277 1278
        tld = InitializeTLData();
        U(tvis).item.lParam = (LPARAM)tld;
1279 1280
        switch(pTypeAttr->typekind)
        {
1281 1282 1283
            case TKIND_ENUM:
                AddToStrW(wszText, wszTKIND_ENUM);
                AddToStrW(wszText, bstrName);
1284

1285 1286 1287 1288 1289 1290 1291 1292 1293
                CreateTypedefHeader(pTypeInfo, pTypeAttr, tld);
                AddToTLDataStrW(tld, &wszTKIND_ENUM[lstrlenW(wszTKIND_ALIAS)]);
                AddToTLDataStrW(tld, wszOpenBrackets3);
                AddToTLDataStrW(tld,wszNewLine);
                AddToStrW(tld->wszInsertAfter, wszCloseBrackets3);
                AddToStrW(tld->wszInsertAfter, wszSpace);
                AddToStrW(tld->wszInsertAfter, bstrName);
                AddToStrW(tld->wszInsertAfter, wszSemicolon);
                AddToStrW(tld->wszInsertAfter, wszNewLine);
1294 1295

                bInsert = FALSE;
1296
                hParent = TreeView_InsertItemW(typelib.hTree, &tvis);
1297 1298 1299
                EnumEnums(pTypeInfo, pTypeAttr->cVars, hParent);
                AddChildrenData(hParent, tld);
                AddToTLDataStrW(tld, tld->wszInsertAfter);
1300 1301
                break;
            case TKIND_RECORD:
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
                AddToTLDataStrW(tld, wszTKIND_RECORD);
                AddToTLDataStrW(tld, wszTag);
                AddToTLDataStrW(tld, bstrName);
                AddToTLDataStrW(tld, wszSpace);
                AddToTLDataStrW(tld, wszOpenBrackets3);
                AddToTLDataStrW(tld, wszNewLine);

                AddToStrW(tld->wszInsertAfter, wszCloseBrackets3);
                AddToStrW(tld->wszInsertAfter, wszSpace);
                AddToStrW(tld->wszInsertAfter, bstrName);
                AddToStrW(tld->wszInsertAfter, wszSemicolon);
                AddToStrW(tld->wszInsertAfter, wszNewLine);
1314

1315 1316 1317 1318 1319 1320 1321 1322
                AddToStrW(wszText, wszTKIND_RECORD);
                AddToStrW(wszText, bstrName);
                break;
            case TKIND_MODULE:
                AddToStrW(wszText, wszTKIND_MODULE);
                AddToStrW(wszText, bstrName);
                break;
            case TKIND_INTERFACE:
1323
                CreateInterfaceInfo(pTypeInfo, pTypeAttr->cImplTypes, bstrName,
1324 1325
                        bstrData, ulHelpContext, pTypeAttr, tld);
                tld->bPredefine = TRUE;
1326

1327 1328 1329 1330 1331 1332
                AddToStrW(wszText, wszTKIND_INTERFACE);
                AddToStrW(wszText, bstrName);
                break;
            case TKIND_COCLASS:
                AddToStrW(wszText, wszTKIND_COCLASS);
                AddToStrW(wszText, bstrName);
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347

                CreateCoclassHeader(pTypeInfo, pTypeAttr, tld);
                AddToTLDataStrW(tld, wszTKIND_COCLASS);
                AddToTLDataStrW(tld, bstrName);
                AddToTLDataStrW(tld, wszSpace);
                AddToTLDataStrW(tld, wszOpenBrackets3);
                AddToTLDataStrW(tld, wszNewLine);

                EnumCoclassImplTypes(pTypeInfo, pTypeAttr->cImplTypes, tld);

                AddToStrW(tld->wszInsertAfter, wszCloseBrackets3);
                AddToStrW(tld->wszInsertAfter, wszSemicolon);
                AddToStrW(tld->wszInsertAfter, wszNewLine);

                bInsert = FALSE;
1348
                hParent = TreeView_InsertItemW(typelib.hTree, &tvis);
1349
                AddToTLDataStrW(tld, tld->wszInsertAfter);
1350 1351 1352 1353 1354
                break;
            case TKIND_UNION:
                AddToStrW(wszText, wszTKIND_UNION);
                AddToStrW(wszText, bstrName);
                break;
1355
            case TKIND_DISPATCH:
1356
                CreateInterfaceInfo(pTypeInfo, pTypeAttr->cImplTypes, bstrName,
1357 1358
                        bstrData, ulHelpContext, pTypeAttr, tld);
                tld->bPredefine = TRUE;
1359 1360
                if(pTypeAttr->wTypeFlags & TYPEFLAG_FDUAL)
                    tld->bHide = TRUE;
1361 1362
                AddToStrW(wszText, wszTKIND_DISPATCH);
                AddToStrW(wszText, bstrName);
1363

1364
                hParent = TreeView_InsertItemW(typelib.hTree, &tvis);
1365
                hMain = tvis.hParent;
1366
                tldDispatch = tld;
1367 1368 1369

                lstrcpyW(wszText, wszProperties);
                tvis.hParent = hParent;
1370 1371 1372 1373 1374
                tld = InitializeTLData();
                U(tvis).item.lParam = (LPARAM) tld;
                AddToTLDataStrW(tld, wszProperties);
                AddToTLDataStrW(tld, wszColon);
                AddToTLDataStrW(tld, wszNewLine);
1375
                tvis.hParent = TreeView_InsertItemW(typelib.hTree, &tvis);
1376
                EnumVars(pTypeInfo, pTypeAttr->cVars, tvis.hParent);
1377
                AddChildrenData(tvis.hParent, tld);
1378 1379 1380

                lstrcpyW(wszText, wszMethods);
                tvis.hParent = hParent;
1381 1382 1383 1384 1385
                tld = InitializeTLData();
                U(tvis).item.lParam = (LPARAM) tld;
                AddToTLDataStrW(tld, wszMethods);
                AddToTLDataStrW(tld, wszColon);
                AddToTLDataStrW(tld, wszNewLine);
1386
                tvis.hParent = TreeView_InsertItemW(typelib.hTree, &tvis);
1387
                EnumFuncs(pTypeInfo, pTypeAttr, tvis.hParent);
1388
                AddChildrenData(tvis.hParent, tld);
1389 1390 1391 1392 1393 1394 1395 1396

                EnumImplTypes(pTypeInfo, pTypeAttr->cImplTypes, hParent);
                AddChildrenData(hParent, tldDispatch);
                AddToTLDataStrW(tldDispatch, tldDispatch->wszInsertAfter);

                bInsert = FALSE;
                tvis.hParent = hMain;

1397 1398
                if(SUCCEEDED(ITypeInfo_GetRefTypeOfImplType(pTypeInfo, -1, &hRefType)))
                {
1399
                    bInsert = TRUE;
1400

1401
                    ITypeInfo_ReleaseTypeAttr(pTypeInfo, pTypeAttr);
1402 1403 1404
                    SysFreeString(bstrName);
                    SysFreeString(bstrData);

1405
                    memset(wszText, 0, sizeof(wszText));
1406 1407
                    tld = InitializeTLData();
                    U(tvis).item.lParam = (LPARAM) tld;
1408

1409 1410
                    ITypeInfo_GetRefTypeInfo(pTypeInfo, hRefType, &pRefTypeInfo);
                    ITypeInfo_GetDocumentation(pRefTypeInfo, MEMBERID_NIL, &bstrName,
1411
                                &bstrData, &ulHelpContext, NULL);
1412
                    ITypeInfo_GetTypeAttr(pRefTypeInfo, &pTypeAttr);
1413 1414

                    CreateInterfaceInfo(pTypeInfo, pTypeAttr->cImplTypes, bstrName,
1415 1416
                            bstrData, ulHelpContext, pTypeAttr, tld);
                    tld->bPredefine = TRUE;
1417

1418 1419 1420 1421 1422
                    AddToStrW(wszText, wszTKIND_INTERFACE);
                    AddToStrW(wszText, bstrName);
                    ITypeInfo_Release(pRefTypeInfo);
                }
                break;
1423 1424
            case TKIND_ALIAS:
                AddToStrW(wszText, wszTKIND_ALIAS);
1425
                CreateTypeInfo(wszText, wszAfter, pTypeAttr->tdescAlias, pTypeInfo);
1426
                AddToStrW(wszText, wszSpace);
1427
                AddToStrW(wszText, bstrName);
1428
                AddToStrW(wszText, wszAfter);
1429

1430 1431 1432 1433
                CreateTypedefHeader(pTypeInfo, pTypeAttr, tld);
                AddToTLDataStrW(tld, &wszText[lstrlenW(wszTKIND_ALIAS)]);
                AddToTLDataStrW(tld, wszSemicolon);
                AddToTLDataStrW(tld, wszNewLine);
1434 1435 1436
                break;
            default:
                lstrcpyW(wszText, bstrName);
1437
                WINE_FIXME("pTypeAttr->typekind == %d not supported\n",
1438
                        pTypeAttr->typekind);
1439
        }
1440

1441 1442
        if(bInsert)
        {
1443
            hParent = TreeView_InsertItemW(typelib.hTree, &tvis);
1444 1445

            EnumVars(pTypeInfo, pTypeAttr->cVars, hParent);
1446
            EnumFuncs(pTypeInfo, pTypeAttr, hParent);
1447
            EnumImplTypes(pTypeInfo, pTypeAttr->cImplTypes, hParent);
1448

1449
            if(memcmp(bstrName, wszVT_UNKNOWN, sizeof(wszVT_UNKNOWN)))
1450 1451
                AddChildrenData(hParent, tld);
            AddToTLDataStrW(tld, tld->wszInsertAfter);
1452
        }
1453

1454 1455
        ITypeInfo_ReleaseTypeAttr(pTypeInfo, pTypeAttr);
        ITypeInfo_Release(pTypeInfo);
1456
        SysFreeString(bstrName);
1457
        SysFreeString(bstrData);
1458
    }
1459
    SendMessageW(typelib.hTree, TVM_EXPAND, TVE_EXPAND, (LPARAM)tvis.hParent);
1460

1461
    memset(&tvi, 0, sizeof(TVITEMW));
1462 1463 1464
    tvi.mask = TVIF_PARAM;
    tvi.hItem = tvis.hParent;

1465
    SendMessageW(typelib.hTree, TVM_GETITEMW, 0, (LPARAM)&tvi);
1466 1467 1468 1469 1470
    AddPredefinitions(tvi.hItem, (TYPELIB_DATA*)(tvi.lParam));
    AddIdlData(tvi.hItem, (TYPELIB_DATA*)(tvi.lParam));
    AddToTLDataStrW((TYPELIB_DATA*)(tvi.lParam),
            ((TYPELIB_DATA*)(tvi.lParam))->wszInsertAfter);

1471
    ITypeLib_Release(pTypeLib);
1472
    return 0;
1473 1474
}

1475 1476
void UpdateData(HTREEITEM item)
{
1477
    TVITEMW tvi;
1478

1479
    memset(&tvi, 0, sizeof(TVITEMW));
1480 1481 1482
    tvi.mask = TVIF_PARAM;
    tvi.hItem = item;

1483
    SendMessageW(typelib.hTree, TVM_GETITEMW, 0, (LPARAM)&tvi);
1484 1485
    if(!tvi.lParam)
    {
1486
        SetWindowTextW(typelib.hEdit, wszSpace);
1487 1488
        return;
    }
1489

1490
    SetWindowTextW(typelib.hEdit, ((TYPELIB_DATA*)tvi.lParam)->idl);
1491 1492
}

1493
static void TypeLibResizeChild(void)
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
{
    RECT client, stat;

    MoveWindow(typelib.hStatusBar, 0, 0, 0, 0, TRUE);

    if(IsWindowVisible(typelib.hStatusBar))
        GetClientRect(typelib.hStatusBar, &stat);
    else stat.bottom = 0;

    GetClientRect(globals.hTypeLibWnd, &client);
    MoveWindow(typelib.hPaneWnd, 0, 0,
            client.right, client.bottom-stat.bottom, TRUE);
}

1508
static void TypeLibMenuCommand(WPARAM wParam, HWND hWnd)
1509 1510 1511 1512 1513
{
    BOOL vis;

    switch(wParam)
    {
1514 1515 1516
        case IDM_SAVEAS:
            GetSaveIdlAsPath();
            break;
1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
        case IDM_STATUSBAR:
            vis = IsWindowVisible(typelib.hStatusBar);
            ShowWindow(typelib.hStatusBar, vis ? SW_HIDE : SW_SHOW);
            CheckMenuItem(GetMenu(hWnd), LOWORD(wParam),
                    vis ? MF_UNCHECKED : MF_CHECKED);
            TypeLibResizeChild();
            break;
        case IDM_CLOSE:
            DestroyWindow(hWnd);
            break;
    }
}

1530
static void UpdateTypeLibStatusBar(int itemID)
1531 1532 1533
{
    WCHAR info[MAX_LOAD_STRING];

1534 1535
    if(!LoadStringW(globals.hMainInst, itemID, info, sizeof(info)/sizeof(info[0])))
        LoadStringW(globals.hMainInst, IDS_READY, info, sizeof(info)/sizeof(info[0]));
1536

1537
    SendMessageW(typelib.hStatusBar, SB_SETTEXTW, 0, (LPARAM)info);
1538 1539
}

1540
static void EmptyTLTree(void)
1541 1542
{
    HTREEITEM cur, del;
1543
    TVITEMW tvi;
1544 1545

    tvi.mask = TVIF_PARAM;
1546 1547
    cur = (HTREEITEM)SendMessageW(typelib.hTree, TVM_GETNEXTITEM,
            TVGN_CHILD, (LPARAM)TVI_ROOT);
1548 1549 1550 1551

    while(TRUE)
    {
        del = cur;
1552 1553
        cur = (HTREEITEM)SendMessageW(typelib.hTree, TVM_GETNEXTITEM,
                TVGN_CHILD, (LPARAM)del);
1554

1555 1556 1557 1558
        if(!cur) cur = (HTREEITEM)SendMessageW(typelib.hTree,
                TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)del);
        if(!cur) cur = (HTREEITEM)SendMessageW(typelib.hTree,
                TVM_GETNEXTITEM, TVGN_PARENT, (LPARAM)del);
1559 1560

        tvi.hItem = del;
1561
        SendMessageW(typelib.hTree, TVM_GETITEMW, 0, (LPARAM)&tvi);
1562 1563 1564 1565 1566 1567
        if(tvi.lParam)
        {
            HeapFree(GetProcessHeap(), 0, ((TYPELIB_DATA *)tvi.lParam)->idl);
            HeapFree(GetProcessHeap(), 0, (TYPELIB_DATA *)tvi.lParam);
        }

1568
        SendMessageW(typelib.hTree, TVM_DELETEITEM, 0, (LPARAM)del);
1569 1570 1571 1572 1573

        if(!cur) break;
    }
}

1574
static LRESULT CALLBACK TypeLibProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1575 1576 1577 1578 1579 1580 1581
{
    switch(uMsg)
    {
        case WM_CREATE:
        {
            if(!CreatePanedWindow(hWnd, &typelib.hPaneWnd, globals.hMainInst))
                DestroyWindow(hWnd);
1582
            typelib.hTree = CreateWindowExW(WS_EX_CLIENTEDGE, WC_TREEVIEWW, NULL,
1583
                    WS_CHILD|WS_VISIBLE|TVS_HASLINES|TVS_HASBUTTONS|TVS_LINESATROOT,
1584 1585
                    0, 0, 0, 0, typelib.hPaneWnd, (HMENU)TYPELIB_TREE,
                    globals.hMainInst, NULL);
1586
            typelib.hEdit = CreateWindowExW(WS_EX_CLIENTEDGE, WC_EDITW, NULL,
1587 1588 1589 1590 1591 1592
                    WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_READONLY|WS_HSCROLL|WS_VSCROLL,
                    0, 0, 0, 0, typelib.hPaneWnd, NULL, globals.hMainInst, NULL);

            SetLeft(typelib.hPaneWnd, typelib.hTree);
            SetRight(typelib.hPaneWnd, typelib.hEdit);

1593 1594
            if(PopulateTree()) DestroyWindow(hWnd);
            else SetFocus(typelib.hTree);
1595 1596
            break;
        }
1597 1598
        case WM_COMMAND:
            TypeLibMenuCommand(LOWORD(wParam), hWnd);
1599 1600 1601 1602 1603 1604
        case WM_MENUSELECT:
            UpdateTypeLibStatusBar(LOWORD(wParam));
            break;
        case WM_SETFOCUS:
            SetFocus(typelib.hTree);
            break;
1605
        case WM_SIZE:
1606 1607
            if(wParam == SIZE_MINIMIZED) break;
            TypeLibResizeChild();
1608 1609
            break;
        case WM_DESTROY:
1610
            EmptyTLTree();
1611 1612
            break;
        default:
1613
            return DefWindowProcW(hWnd, uMsg, wParam, lParam);
1614 1615 1616 1617
    }
    return 0;
}

1618
BOOL TypeLibRegisterClassW(void)
1619
{
1620
    WNDCLASSW wcc;
1621

1622
    memset(&wcc, 0, sizeof(WNDCLASSW));
1623 1624
    wcc.lpfnWndProc = TypeLibProc;
    wcc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
1625
    wcc.lpszMenuName = MAKEINTRESOURCEW(IDM_TYPELIB);
1626 1627
    wcc.lpszClassName = wszTypeLib;

1628
    if(!RegisterClassW(&wcc))
1629 1630 1631 1632 1633
        return FALSE;

    return TRUE;
}

1634
BOOL CreateTypeLibWindow(HINSTANCE hInst, WCHAR *wszFileName)
1635 1636
{
    WCHAR wszTitle[MAX_LOAD_STRING];
1637
    LoadStringW(hInst, IDS_TYPELIBTITLE, wszTitle, sizeof(wszTitle)/sizeof(wszTitle[0]));
1638

1639 1640 1641
    if(wszFileName) lstrcpyW(typelib.wszFileName, wszFileName);
    else
    {
1642
        TVITEMW tvi;
1643

1644
        memset(&tvi, 0, sizeof(TVITEMW));
1645 1646
        tvi.hItem = (HTREEITEM)SendMessageW(globals.hTree, TVM_GETNEXTITEM,
                TVGN_CARET, 0);
1647

1648
        SendMessageW(globals.hTree, TVM_GETITEMW, 0, (LPARAM)&tvi);
1649 1650 1651
        lstrcpyW(typelib.wszFileName, ((ITEM_INFO*)tvi.lParam)->path);
    }

1652
    globals.hTypeLibWnd = CreateWindowW(wszTypeLib, wszTitle,
1653 1654 1655
            WS_OVERLAPPEDWINDOW|WS_VISIBLE,
            CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInst, NULL);
    if(!globals.hTypeLibWnd) return FALSE;
1656

1657
    typelib.hStatusBar = CreateStatusWindowW(WS_VISIBLE|WS_CHILD,
1658
            wszTitle, globals.hTypeLibWnd, 0);
1659 1660

    TypeLibResizeChild();
1661 1662
    return TRUE;
}