oleview: Add safearray type.
[wine] / programs / oleview / tree.c
1 /*
2  * OleView (tree.c)
3  *
4  * Copyright 2006 Piotr Caban
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * 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 #include "main.h"
22
23 TREE tree;
24 static const WCHAR wszCLSID[] = { 'C','L','S','I','D','\\','\0' };
25 static const WCHAR wszInProcServer32[] = 
26     { 'I','n','P','r','o','c','S','e','r','v','e','r','3','2','\0' };
27 static const WCHAR wszOle32dll[] = { 'o','l','e','3','2','.','d','l','l','\0' };
28 static const WCHAR wszOleAut32dll[] =
29     { 'o','l','e','a','u','t','3','2','.','d','l','l','\0' };
30 static const WCHAR wszImplementedCategories[] = 
31     { 'I','m','p','l','e','m','e','n','t','e','d',' ',
32         'C','a','t','e','g','o','r','i','e','s','\0' };
33 static const WCHAR wszAppID[] = { 'A','p','p','I','D','\\','\0' };
34 static const WCHAR wszTypeLib[] = { 'T','y','p','e','L','i','b','\\','\0' };
35 static const WCHAR wszInterface[] = { 'I','n','t','e','r','f','a','c','e','\\','\0' };
36 static const WCHAR wszComponentCategories[] = { 'C','o','m','p','o','n','e','n','t',
37     ' ','C','a','t','e','g','o','r','i','e','s','\\','\0' };
38 static const WCHAR wszGetPath[] = { '0','\\','w','i','n','3','2','\0' };
39
40 LPARAM CreateITEM_INFO(INT flag, const WCHAR *info, const WCHAR *clsid, const WCHAR *path)
41 {
42     ITEM_INFO *reg;
43
44     reg = HeapAlloc(GetProcessHeap(), 0, sizeof(ITEM_INFO));
45     memset(reg, 0, sizeof(ITEM_INFO));
46
47     reg->cFlag = flag;
48     lstrcpyW(reg->info, info);
49     if(clsid) lstrcpyW(reg->clsid, clsid);
50     if(path) lstrcpyW(reg->path, path);
51
52     return (LPARAM)reg;
53 }
54
55 void CreateInst(HTREEITEM item, WCHAR *wszMachineName)
56 {
57     TVITEM tvi;
58     HTREEITEM hCur;
59     TVINSERTSTRUCT tvis;
60     WCHAR wszTitle[MAX_LOAD_STRING];
61     WCHAR wszMessage[MAX_LOAD_STRING];
62     WCHAR wszFlagName[MAX_LOAD_STRING];
63     WCHAR wszTreeName[MAX_LOAD_STRING];
64     WCHAR wszRegPath[MAX_LOAD_STRING];
65     const WCHAR wszFormat[] = { '\n','%','s',' ','(','$','%','x',')','\n','\0' };
66     CLSID clsid;
67     COSERVERINFO remoteInfo;
68     MULTI_QI qi;
69     IUnknown *obj, *unk;
70     HRESULT hRes;
71
72     memset(&tvi, 0, sizeof(TVITEM));
73     tvi.mask = TVIF_TEXT;
74     tvi.hItem = item;
75     tvi.cchTextMax = MAX_LOAD_STRING;
76     tvi.pszText = wszTreeName;
77
78     memset(&tvis, 0, sizeof(TVINSERTSTRUCT));
79     U(tvis).item.mask = TVIF_TEXT|TVIF_PARAM;
80     U(tvis).item.cchTextMax = MAX_LOAD_STRING;
81     tvis.hInsertAfter = (HTREEITEM)TVI_FIRST;
82     U(tvis).item.pszText = tvi.pszText;
83     tvis.hParent = item;
84     tvis.hInsertAfter = TVI_LAST;
85
86     SendMessage(globals.hTree, TVM_GETITEM, 0, (LPARAM)&tvi);
87
88     if(!tvi.lParam || ((ITEM_INFO *)tvi.lParam)->loaded
89                 || !(((ITEM_INFO *)tvi.lParam)->cFlag&SHOWALL)) return;
90
91     if(FAILED(CLSIDFromString(((ITEM_INFO *)tvi.lParam)->clsid, &clsid))) return;
92
93     if(wszMachineName)
94     {
95         remoteInfo.dwReserved1 = 0;
96         remoteInfo.dwReserved2 = 0;
97         remoteInfo.pAuthInfo = NULL;
98         remoteInfo.pwszName = wszMachineName;
99
100         qi.pIID = &IID_IUnknown;
101
102         CoCreateInstanceEx(&clsid, NULL, globals.dwClsCtx|CLSCTX_REMOTE_SERVER,
103                 &remoteInfo, 1, &qi);
104         hRes = qi.hr;
105         obj = qi.pItf;
106     }
107     else hRes = CoCreateInstance(&clsid, NULL, globals.dwClsCtx,
108             &IID_IUnknown, (void **)&obj);
109
110     if(FAILED(hRes))
111     {
112         LoadString(globals.hMainInst, IDS_CGCOFAIL, wszMessage,
113                 sizeof(WCHAR[MAX_LOAD_STRING]));
114         LoadString(globals.hMainInst, IDS_ABOUT, wszTitle,
115                 sizeof(WCHAR[MAX_LOAD_STRING]));
116
117 #define CASE_ERR(i) case i: \
118     MultiByteToWideChar(CP_ACP, 0, #i, -1, wszFlagName, MAX_LOAD_STRING); \
119     break
120
121         switch(hRes)
122         {
123             CASE_ERR(REGDB_E_CLASSNOTREG);
124             CASE_ERR(E_NOINTERFACE);
125             CASE_ERR(REGDB_E_READREGDB);
126             CASE_ERR(REGDB_E_KEYMISSING);
127             CASE_ERR(CO_E_DLLNOTFOUND);
128             CASE_ERR(CO_E_APPNOTFOUND);
129             CASE_ERR(E_ACCESSDENIED);
130             CASE_ERR(CO_E_ERRORINDLL);
131             CASE_ERR(CO_E_APPDIDNTREG);
132             CASE_ERR(CLASS_E_CLASSNOTAVAILABLE);
133             default:
134                 LoadString(globals.hMainInst, IDS_ERROR_UNKN, wszFlagName, MAX_LOAD_STRING);
135         }
136
137         wsprintfW(&wszMessage[lstrlenW(wszMessage)], wszFormat,
138                 wszFlagName, (unsigned)hRes);
139         MessageBox(globals.hMainWnd, wszMessage, wszTitle, MB_OK|MB_ICONEXCLAMATION);
140         return;
141     }
142
143     ((ITEM_INFO *)tvi.lParam)->loaded = 1;
144     ((ITEM_INFO *)tvi.lParam)->pU = obj;
145
146     tvi.mask = TVIF_STATE;
147     tvi.state = TVIS_BOLD;
148     tvi.stateMask = TVIS_BOLD;
149     SendMessage(globals.hTree, TVM_SETITEM, 0, (LPARAM)&tvi);
150
151     tvi.mask = TVIF_TEXT;
152     hCur = TreeView_GetChild(globals.hTree, tree.hI);
153
154     while(hCur)
155     {
156         tvi.hItem = hCur;
157         SendMessage(globals.hTree, TVM_GETITEM, 0, (LPARAM)&tvi);
158
159         if(!tvi.lParam)
160         {
161             hCur = TreeView_GetNextSibling(globals.hTree, hCur);
162             continue;
163         }
164
165         CLSIDFromString(((ITEM_INFO *)tvi.lParam)->clsid, &clsid);
166         hRes = IUnknown_QueryInterface(obj, &clsid, (void *)&unk);
167
168         if(SUCCEEDED(hRes))
169         {
170             IUnknown_Release(unk);
171
172             lstrcpyW(wszRegPath, wszInterface);
173             lstrcpyW(&wszRegPath[lstrlenW(wszRegPath)], ((ITEM_INFO *)tvi.lParam)->clsid);
174             U(tvis).item.lParam = CreateITEM_INFO(REGTOP|INTERFACE|REGPATH,
175                     wszRegPath, ((ITEM_INFO *)tvi.lParam)->clsid, NULL);
176             SendMessage(globals.hTree, TVM_INSERTITEM, 0, (LPARAM)&tvis);
177         }
178         hCur = TreeView_GetNextSibling(globals.hTree, hCur);
179     }
180
181     RefreshMenu(item);
182     RefreshDetails(item);
183 }
184
185 void ReleaseInst(HTREEITEM item)
186 {
187     TVITEM tvi;
188     HTREEITEM cur;
189     IUnknown *pU;
190
191     memset(&tvi, 0, sizeof(TVITEM));
192     tvi.hItem = item;
193     SendMessage(globals.hTree, TVM_GETITEM, 0, (LPARAM)&tvi);
194
195     if(!tvi.lParam) return;
196
197     pU = ((ITEM_INFO *)tvi.lParam)->pU;
198
199     if(pU) IUnknown_Release(pU);
200     ((ITEM_INFO *)tvi.lParam)->loaded = 0;
201
202     SendMessage(globals.hTree, TVM_EXPAND, TVE_COLLAPSE, (LPARAM)item);
203
204     cur = TreeView_GetChild(globals.hTree, item);
205     while(cur)
206     {
207         SendMessage(globals.hTree, TVM_DELETEITEM, 0, (LPARAM)cur);
208         cur = TreeView_GetChild(globals.hTree, item);
209     }
210
211     tvi.mask = TVIF_CHILDREN|TVIF_STATE;
212     tvi.state = 0;
213     tvi.stateMask = TVIS_BOLD;
214     tvi.cChildren = 1;
215     SendMessage(globals.hTree, TVM_SETITEM, 0, (LPARAM)&tvi);
216 }
217
218 BOOL CreateRegPath(HTREEITEM item, WCHAR *buffer, int bufSize)
219 {
220     TVITEM tvi;
221     int bufLen;
222     BOOL ret;
223
224     memset(buffer, 0, sizeof(WCHAR[bufSize]));
225     memset(&tvi, 0, sizeof(TVITEM));
226     tvi.hItem = item;
227
228     SendMessage(globals.hTree, TVM_GETITEM, 0, (LPARAM)&tvi);
229     ret = (tvi.lParam && ((ITEM_INFO *)tvi.lParam)->cFlag & REGPATH);
230
231     while(TRUE)
232     {
233         SendMessage(globals.hTree, TVM_GETITEM, 0, (LPARAM)&tvi);
234
235         if(tvi.lParam && (((ITEM_INFO *)tvi.lParam)->cFlag & (REGPATH|REGTOP)))
236         {
237             bufLen = lstrlenW(((ITEM_INFO *)tvi.lParam)->info);
238             memmove(&buffer[bufLen], buffer, sizeof(WCHAR[bufSize-bufLen]));
239             memcpy(buffer, ((ITEM_INFO *)tvi.lParam)->info, sizeof(WCHAR[bufLen]));
240         }
241
242         if(tvi.lParam && ((ITEM_INFO *)tvi.lParam)->cFlag & REGTOP) break;
243
244         if(!tvi.lParam) return FALSE;
245
246         tvi.hItem = TreeView_GetParent(globals.hTree, tvi.hItem);
247     }
248     return ret;
249 }
250
251 void AddCOMandAll(void)
252 {
253     TVINSERTSTRUCT tvis;
254     TVITEM tvi;
255     HTREEITEM curSearch;
256     HKEY hKey, hCurKey, hInfo;
257     WCHAR valName[MAX_LOAD_STRING];
258     WCHAR buffer[MAX_LOAD_STRING];
259     WCHAR wszComp[MAX_LOAD_STRING];
260     LONG lenBuffer;
261     int i=-1;
262
263     memset(&tvi, 0, sizeof(TVITEM));
264     U(tvis).item.mask = TVIF_TEXT|TVIF_PARAM|TVIF_CHILDREN;
265     U(tvis).item.cchTextMax = MAX_LOAD_STRING;
266     U(tvis).item.cChildren = 1;
267     tvis.hInsertAfter = (HTREEITEM)TVI_FIRST;
268
269     if(RegOpenKey(HKEY_CLASSES_ROOT, wszCLSID, &hKey) != ERROR_SUCCESS) return;
270
271     while(TRUE)
272     {
273         i++;
274
275         if(RegEnumKey(hKey, i, valName, -1) != ERROR_SUCCESS) break;
276
277         if(RegOpenKey(hKey, valName, &hCurKey) != ERROR_SUCCESS) continue;
278
279         lenBuffer = sizeof(WCHAR[MAX_LOAD_STRING]);
280         tvis.hParent = tree.hAO;
281
282         if(RegOpenKey(hCurKey, wszInProcServer32, &hInfo) == ERROR_SUCCESS)
283         {
284             if(RegQueryValue(hInfo, NULL, buffer, &lenBuffer) == ERROR_SUCCESS
285                     && *buffer)
286                 if(!memcmp(buffer, wszOle32dll, sizeof(WCHAR[9]))
287                         ||!memcmp(buffer, wszOleAut32dll, sizeof(WCHAR[12])))
288                     tvis.hParent = tree.hCLO;
289
290             RegCloseKey(hInfo);
291         }
292
293         lenBuffer = sizeof(WCHAR[MAX_LOAD_STRING]);
294
295         if(RegQueryValue(hCurKey, NULL, buffer, &lenBuffer) == ERROR_SUCCESS && *buffer)
296             U(tvis).item.pszText = buffer;
297         else U(tvis).item.pszText = valName;
298     
299         U(tvis).item.lParam = CreateITEM_INFO(REGPATH|SHOWALL, valName, valName, NULL);
300         if(tvis.hParent) SendMessage(globals.hTree, TVM_INSERTITEM, 0, (LPARAM)&tvis);
301
302         if(RegOpenKey(hCurKey, wszImplementedCategories, &hInfo) == ERROR_SUCCESS)
303         {
304             if(RegEnumKey(hInfo, 0, wszComp, -1) != ERROR_SUCCESS) break;
305
306             RegCloseKey(hInfo);
307
308             if(tree.hGBCC) curSearch = TreeView_GetChild(globals.hTree, tree.hGBCC);
309             else curSearch = TreeView_GetChild(globals.hTree, TVI_ROOT);
310
311             while(curSearch)
312             {
313                 tvi.hItem = curSearch;
314                 SendMessage(globals.hTree, TVM_GETITEM, 0, (LPARAM)&tvi);
315
316                 if(tvi.lParam && !lstrcmpW(((ITEM_INFO *)tvi.lParam)->info, wszComp))
317                 {
318                     tvis.hParent = curSearch;
319
320                     memmove(&valName[6], valName, sizeof(WCHAR[MAX_LOAD_STRING-6]));
321                     memmove(valName, wszCLSID, sizeof(WCHAR[6]));
322                     U(tvis).item.lParam = CreateITEM_INFO(REGTOP|REGPATH|SHOWALL,
323                             valName, &valName[6], NULL);
324
325                     SendMessage(globals.hTree, TVM_INSERTITEM, 0, (LPARAM)&tvis);
326                     break;
327                 }
328                 curSearch = TreeView_GetNextSibling(globals.hTree, curSearch);
329             }
330         }
331         RegCloseKey(hCurKey);
332     }
333     RegCloseKey(hKey);
334
335     SendMessage(globals.hTree, TVM_SORTCHILDREN, FALSE, (LPARAM)tree.hCLO);
336     SendMessage(globals.hTree, TVM_SORTCHILDREN, FALSE, (LPARAM)tree.hAO);
337 }
338
339 void AddApplicationID(void)
340 {
341     TVINSERTSTRUCT tvis;
342     HKEY hKey, hCurKey;
343     WCHAR valName[MAX_LOAD_STRING];
344     WCHAR buffer[MAX_LOAD_STRING];
345     LONG lenBuffer;
346     int i=-1;
347
348     U(tvis).item.mask = TVIF_TEXT|TVIF_PARAM;
349     U(tvis).item.cchTextMax = MAX_LOAD_STRING;
350     tvis.hInsertAfter = (HTREEITEM)TVI_FIRST;
351     tvis.hParent = tree.hAID;
352
353     if(RegOpenKey(HKEY_CLASSES_ROOT, wszAppID, &hKey) != ERROR_SUCCESS) return;
354
355     while(TRUE)
356     {
357         i++;
358
359         if(RegEnumKey(hKey, i, valName, -1) != ERROR_SUCCESS) break;
360
361         if(RegOpenKey(hKey, valName, &hCurKey) != ERROR_SUCCESS) continue;
362
363         lenBuffer = sizeof(WCHAR[MAX_LOAD_STRING]);
364
365         if(RegQueryValue(hCurKey, NULL, buffer, &lenBuffer) == ERROR_SUCCESS && *buffer)
366             U(tvis).item.pszText = buffer;
367         else U(tvis).item.pszText = valName;
368
369         RegCloseKey(hCurKey);
370
371         U(tvis).item.lParam = CreateITEM_INFO(REGPATH, valName, valName, NULL);
372         SendMessage(globals.hTree, TVM_INSERTITEM, 0, (LPARAM)&tvis);
373     }
374     RegCloseKey(hKey);
375
376     SendMessage(globals.hTree, TVM_SORTCHILDREN, FALSE, (LPARAM)tree.hAID);
377 }
378
379 void AddTypeLib(void)
380 {
381     TVINSERTSTRUCT tvis;
382     HKEY hKey, hCurKey, hInfoKey, hPath;
383     WCHAR valName[MAX_LOAD_STRING];
384     WCHAR valParent[MAX_LOAD_STRING];
385     WCHAR buffer[MAX_LOAD_STRING];
386     WCHAR wszVer[MAX_LOAD_STRING];
387     WCHAR wszPath[MAX_LOAD_STRING];
388     const WCHAR wszFormat[] = { ' ','(','%','s',' ','%','s',')','\0' };
389     const WCHAR wszFormat2[] = { '%','s','\\','%','s','\0' };
390     LONG lenBuffer;
391     int i=-1, j;
392
393     U(tvis).item.mask = TVIF_TEXT|TVIF_PARAM;
394     U(tvis).item.cchTextMax = MAX_LOAD_STRING;
395     tvis.hInsertAfter = (HTREEITEM)TVI_FIRST;
396     tvis.hParent = tree.hTL;
397
398     if(RegOpenKey(HKEY_CLASSES_ROOT, wszTypeLib, &hKey) != ERROR_SUCCESS) return;
399
400     while(TRUE)
401     {
402         i++;
403
404         if(RegEnumKey(hKey, i, valParent, -1) != ERROR_SUCCESS) break;
405
406         if(RegOpenKey(hKey, valParent, &hCurKey) != ERROR_SUCCESS) continue;
407
408         j = -1;
409         while(TRUE)
410         {
411             j++;
412
413             if(RegEnumKey(hCurKey, j, valName, -1) != ERROR_SUCCESS) break;
414
415             if(RegOpenKey(hCurKey, valName, &hInfoKey) != ERROR_SUCCESS) continue;
416
417             lenBuffer = sizeof(WCHAR[MAX_LOAD_STRING]);
418
419             if(RegQueryValue(hInfoKey, NULL, buffer, &lenBuffer) == ERROR_SUCCESS 
420                     && *buffer)
421             {
422                 LoadString(globals.hMainInst, IDS_TL_VER, wszVer,
423                         sizeof(WCHAR[MAX_LOAD_STRING]));
424
425                 wsprintfW(&buffer[lstrlenW(buffer)], wszFormat, wszVer, valName);
426                 U(tvis).item.pszText = buffer;
427
428                 lenBuffer = MAX_LOAD_STRING;
429                 RegOpenKey(hInfoKey, wszGetPath, &hPath);
430                 RegQueryValue(hPath, NULL, wszPath, &lenBuffer);
431                 RegCloseKey(hPath);
432             }
433             else U(tvis).item.pszText = valName;
434
435             RegCloseKey(hInfoKey);
436
437             wsprintfW(wszVer, wszFormat2, valParent, valName);
438             U(tvis).item.lParam = CreateITEM_INFO(REGPATH, wszVer, valParent, wszPath);
439
440             SendMessage(globals.hTree, TVM_INSERTITEM, 0, (LPARAM)&tvis);
441         }
442         RegCloseKey(hCurKey);
443     }
444
445     RegCloseKey(hKey);
446
447     SendMessage(globals.hTree, TVM_SORTCHILDREN, FALSE, (LPARAM)tree.hTL);
448 }
449
450 void AddInterfaces(void)
451 {
452     TVINSERTSTRUCT tvis;
453     HKEY hKey, hCurKey;
454     WCHAR valName[MAX_LOAD_STRING];
455     WCHAR buffer[MAX_LOAD_STRING];
456     LONG lenBuffer;
457     int i=-1;
458
459     U(tvis).item.mask = TVIF_TEXT|TVIF_PARAM;
460     U(tvis).item.cchTextMax = MAX_LOAD_STRING;
461     tvis.hInsertAfter = (HTREEITEM)TVI_FIRST;
462     tvis.hParent = tree.hI;
463
464     if(RegOpenKey(HKEY_CLASSES_ROOT, wszInterface, &hKey) != ERROR_SUCCESS) return;
465
466     while(TRUE)
467     {
468         i++;
469
470         if(RegEnumKey(hKey, i, valName, -1) != ERROR_SUCCESS) break;
471
472         if(RegOpenKey(hKey, valName, &hCurKey) != ERROR_SUCCESS) continue;
473
474         lenBuffer = sizeof(WCHAR[MAX_LOAD_STRING]);
475
476         if(RegQueryValue(hCurKey, NULL, buffer, &lenBuffer) == ERROR_SUCCESS && *buffer)
477             U(tvis).item.pszText = buffer;
478         else U(tvis).item.pszText = valName;
479
480         RegCloseKey(hCurKey);
481
482         U(tvis).item.lParam = CreateITEM_INFO(REGPATH|INTERFACE, valName, valName, NULL);
483         SendMessage(globals.hTree, TVM_INSERTITEM, 0, (LPARAM)&tvis);
484     }
485
486     RegCloseKey(hKey);
487
488     SendMessage(globals.hTree, TVM_SORTCHILDREN, FALSE, (LPARAM)tree.hI);
489 }
490
491 void AddComponentCategories(void)
492 {
493     TVINSERTSTRUCT tvis;
494     HKEY hKey, hCurKey;
495     WCHAR valName[MAX_LOAD_STRING];
496     WCHAR buffer[MAX_LOAD_STRING];
497     LONG lenBuffer;
498     DWORD lenBufferHlp;
499     int i=-1;
500
501     U(tvis).item.mask = TVIF_TEXT|TVIF_PARAM|TVIF_CHILDREN;
502     U(tvis).item.cchTextMax = MAX_LOAD_STRING;
503     tvis.hInsertAfter = (HTREEITEM)TVI_FIRST;
504     if(tree.hGBCC) tvis.hParent = tree.hGBCC;
505     else tvis.hParent = TVI_ROOT;
506     U(tvis).item.cChildren = 1;
507
508     if(RegOpenKey(HKEY_CLASSES_ROOT, wszComponentCategories, &hKey) != ERROR_SUCCESS)
509         return;
510
511     while(TRUE)
512     {
513         i++;
514
515         if(RegEnumKey(hKey, i, valName, -1) != ERROR_SUCCESS) break;
516
517         if(RegOpenKey(hKey, valName, &hCurKey) != ERROR_SUCCESS) continue;
518
519         lenBuffer = sizeof(WCHAR[MAX_LOAD_STRING]);
520         lenBufferHlp = sizeof(WCHAR[MAX_LOAD_STRING]);
521
522         if(RegQueryValue(hCurKey, NULL, buffer, &lenBuffer) == ERROR_SUCCESS && *buffer)
523             U(tvis).item.pszText = buffer;
524         else if(RegEnumValue(hCurKey, 0, NULL, NULL, NULL, NULL,
525                     (LPBYTE)buffer, &lenBufferHlp) == ERROR_SUCCESS && *buffer)
526             U(tvis).item.pszText = buffer;
527         else continue;
528
529         RegCloseKey(hCurKey);
530
531         U(tvis).item.lParam = CreateITEM_INFO(REGTOP, valName, valName, NULL);
532         SendMessage(globals.hTree, TVM_INSERTITEM, 0, (LPARAM)&tvis);
533     }
534
535     RegCloseKey(hKey);
536
537     SendMessage(globals.hTree, TVM_SORTCHILDREN, FALSE, (LPARAM)tree.hGBCC);
538 }
539
540 void AddBaseEntries(void)
541 {
542     TVINSERTSTRUCT tvis;
543     WCHAR name[MAX_LOAD_STRING];
544
545     U(tvis).item.mask = TVIF_TEXT|TVIF_CHILDREN|TVIF_PARAM;
546     /* FIXME add TVIF_IMAGE */
547     U(tvis).item.pszText = name;
548     U(tvis).item.cchTextMax = MAX_LOAD_STRING;
549     U(tvis).item.cChildren = 1;
550     tvis.hInsertAfter = (HTREEITEM)TVI_FIRST;
551     tvis.hParent = TVI_ROOT;
552
553     LoadString(globals.hMainInst, IDS_TREE_I, U(tvis).item.pszText,
554             sizeof(WCHAR[MAX_LOAD_STRING]));
555     U(tvis).item.lParam = CreateITEM_INFO(REGTOP, wszInterface, NULL, NULL);
556     tree.hI = TreeView_InsertItem(globals.hTree, &tvis);
557
558     LoadString(globals.hMainInst, IDS_TREE_TL, U(tvis).item.pszText,
559             sizeof(WCHAR[MAX_LOAD_STRING]));
560     U(tvis).item.lParam = CreateITEM_INFO(REGTOP, wszTypeLib, NULL, NULL);
561     tree.hTL = TreeView_InsertItem(globals.hTree, &tvis);
562
563     LoadString(globals.hMainInst, IDS_TREE_AID, U(tvis).item.pszText,
564             sizeof(WCHAR[MAX_LOAD_STRING]));
565     U(tvis).item.lParam = CreateITEM_INFO(REGTOP|REGPATH, wszAppID, NULL, NULL);
566     tree.hAID = TreeView_InsertItem(globals.hTree, &tvis);
567
568     LoadString(globals.hMainInst, IDS_TREE_OC, U(tvis).item.pszText,
569             sizeof(WCHAR[MAX_LOAD_STRING]));
570     U(tvis).item.lParam = (LPARAM)NULL;
571     tree.hOC = TreeView_InsertItem(globals.hTree, &tvis);
572
573
574     tvis.hParent = tree.hOC;
575     LoadString(globals.hMainInst, IDS_TREE_AO, U(tvis).item.pszText,
576             sizeof(WCHAR[MAX_LOAD_STRING]));
577     U(tvis).item.lParam = CreateITEM_INFO(REGTOP, wszCLSID, NULL, NULL);
578     tree.hAO = TreeView_InsertItem(globals.hTree, &tvis);
579
580     LoadString(globals.hMainInst, IDS_TREE_CLO, U(tvis).item.pszText,
581             sizeof(WCHAR[MAX_LOAD_STRING]));
582     tree.hCLO = TreeView_InsertItem(globals.hTree, &tvis);
583
584     LoadString(globals.hMainInst, IDS_TREE_O1O, U(tvis).item.pszText,
585             sizeof(WCHAR[MAX_LOAD_STRING]));
586     U(tvis).item.lParam = (LPARAM)NULL;
587     tree.hO1O = TreeView_InsertItem(globals.hTree, &tvis);
588
589     LoadString(globals.hMainInst, IDS_TREE_GBCC, U(tvis).item.pszText,
590             sizeof(WCHAR[MAX_LOAD_STRING]));
591     U(tvis).item.lParam = CreateITEM_INFO(REGTOP|REGPATH,
592             wszComponentCategories, NULL, NULL);
593     tree.hGBCC = TreeView_InsertItem(globals.hTree, &tvis);
594
595     SendMessage(globals.hTree, TVM_EXPAND, TVE_EXPAND, (LPARAM)tree.hOC);
596 }
597
598 void EmptyTree(void)
599 {
600     HTREEITEM cur, del;
601     TVITEM tvi;
602
603     tvi.mask = TVIF_PARAM;
604     cur = TreeView_GetChild(globals.hTree, TVI_ROOT);
605
606     while(TRUE)
607     {
608         del = cur;
609         cur = TreeView_GetChild(globals.hTree, del);
610
611         if(!cur) cur = TreeView_GetNextSibling(globals.hTree, del);
612         if(!cur)
613         {
614             cur = TreeView_GetPrevSibling(globals.hTree, del);
615             if(!cur) cur = TreeView_GetParent(globals.hTree, del);
616
617             tvi.hItem = del;
618             SendMessage(globals.hTree, TVM_GETITEM, 0, (LPARAM)&tvi);
619
620             if(tvi.lParam)
621             {
622                 if(((ITEM_INFO *)tvi.lParam)->loaded) ReleaseInst(del);
623                 HeapFree(GetProcessHeap(), 0, (ITEM_INFO *)tvi.lParam);
624             }
625
626             SendMessage(globals.hTree, TVM_DELETEITEM, 0, (LPARAM)del);
627
628             if(!cur) break;
629         }
630     }
631 }
632
633 void AddTreeEx(void)
634 {
635     AddBaseEntries();
636     AddComponentCategories();
637     AddCOMandAll();
638     AddApplicationID();
639     AddTypeLib();
640     AddInterfaces();
641 }
642
643 void AddTree(void)
644 {
645     memset(&tree, 0, sizeof(TREE));
646     AddComponentCategories();
647     AddCOMandAll();
648 }
649
650 LRESULT CALLBACK TreeProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
651 {
652     switch(uMsg)
653     {
654         case WM_CREATE:
655             globals.hTree = CreateWindow(WC_TREEVIEW, NULL,
656                     WS_CHILD|WS_VISIBLE|TVS_HASLINES|TVS_HASBUTTONS|TVS_LINESATROOT,
657                     0, 0, 0, 0, hWnd, (HMENU)TREE_WINDOW, globals.hMainInst, NULL);
658             AddTreeEx();
659             break;
660         case WM_NOTIFY:
661             if((int)wParam != TREE_WINDOW) break;
662             switch(((LPNMHDR)lParam)->code)
663             {
664                 case TVN_ITEMEXPANDING:
665                     CreateInst(((NMTREEVIEW *)lParam)->itemNew.hItem, NULL);
666                     break;
667                 case TVN_SELCHANGED:
668                     RefreshMenu(((NMTREEVIEW *)lParam)->itemNew.hItem);
669                     RefreshDetails(((NMTREEVIEW *)lParam)->itemNew.hItem);
670                     break;
671             }
672             break;
673         case WM_SIZE:
674             MoveWindow(globals.hTree, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
675             break;
676         default:
677             return DefWindowProc(hWnd, uMsg, wParam, lParam);
678     }
679     return 0;
680 }
681
682 HWND CreateTreeWindow(HINSTANCE hInst)
683 {
684     WNDCLASS wct;
685     const WCHAR wszTreeClass[] = { 'T','R','E','E','\0' };
686
687     memset(&wct, 0, sizeof(WNDCLASS));
688     wct.lpfnWndProc = TreeProc;
689     wct.lpszClassName = wszTreeClass;
690
691     if(!RegisterClass(&wct)) return NULL;
692
693     return CreateWindowEx(WS_EX_CLIENTEDGE, wszTreeClass, NULL, WS_CHILD|WS_VISIBLE,
694             0, 0, 0, 0, globals.hPaneWnd, NULL, hInst, NULL);
695 }