Small fix.
[wine] / dlls / shell32 / brsfolder.c
1 #include <stdlib.h>
2 #include <string.h>
3
4 #include "winerror.h"
5 #include "heap.h"
6 #include "resource.h"
7 #include "dlgs.h"
8 #include "win.h"
9 #include "sysmetrics.h"
10 #include "debugtools.h"
11 #include "winreg.h"
12 #include "authors.h"
13 #include "winnls.h"
14 #include "commctrl.h"
15 #include "spy.h"
16
17 #include "wine/obj_base.h"
18 #include "wine/obj_enumidlist.h"
19 #include "wine/obj_shellfolder.h"
20 #include "wine/undocshell.h"
21
22 #include "pidl.h"
23 #include "shell32_main.h"
24 #include "shellapi.h"
25
26 DEFAULT_DEBUG_CHANNEL(shell)
27
28 #define         IDD_TREEVIEW 99
29
30 static HWND             hwndTreeView;
31 static LPBROWSEINFOA  lpBrowseInfo;
32 static LPITEMIDLIST     pidlRet;
33
34 static void FillTreeView(LPSHELLFOLDER lpsf, LPITEMIDLIST  lpifq, HTREEITEM hParent);
35
36 static void InitializeTreeView(HWND hwndParent)
37 {
38         HIMAGELIST      hImageList;
39         IShellFolder *  lpsf;
40         HRESULT hr;
41
42         hwndTreeView = GetDlgItem (hwndParent, IDD_TREEVIEW);
43         Shell_GetImageList(NULL, &hImageList);
44         
45         TRACE("dlg=%x tree=%x\n", hwndParent, hwndTreeView );
46
47         if (hImageList && hwndTreeView)
48         { TreeView_SetImageList(hwndTreeView, hImageList, 0);
49         }
50
51         hr = SHGetDesktopFolder(&lpsf);
52
53         if (SUCCEEDED(hr) && hwndTreeView)
54         { TreeView_DeleteAllItems(hwndTreeView);
55           FillTreeView(lpsf, NULL, TVI_ROOT);
56         }
57         
58         if (SUCCEEDED(hr))
59         { IShellFolder_Release(lpsf);
60         }
61 }
62
63 static int GetIcon(LPITEMIDLIST lpi, UINT uFlags)
64 {       SHFILEINFOA    sfi;
65         SHGetFileInfoA((LPCSTR)lpi,0,&sfi, sizeof(SHFILEINFOA), uFlags);
66         return sfi.iIcon;
67 }
68
69 static void GetNormalAndSelectedIcons(LPITEMIDLIST lpifq,LPTVITEMA lpTV_ITEM)
70 {       TRACE("%p %p\n",lpifq, lpTV_ITEM);
71
72         lpTV_ITEM->iImage = GetIcon(lpifq, SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
73         lpTV_ITEM->iSelectedImage = GetIcon(lpifq, SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_OPENICON);
74
75         return;
76 }
77
78 typedef struct tagID
79 {
80    LPSHELLFOLDER lpsfParent;
81    LPITEMIDLIST  lpi;
82    LPITEMIDLIST  lpifq;
83 } TV_ITEMDATA, *LPTV_ITEMDATA;
84
85 static BOOL GetName(LPSHELLFOLDER lpsf, LPITEMIDLIST lpi, DWORD dwFlags, LPSTR lpFriendlyName)
86 {
87         BOOL   bSuccess=TRUE;
88         STRRET str;
89
90         TRACE("%p %p %lx %p\n", lpsf, lpi, dwFlags, lpFriendlyName);
91         if (SUCCEEDED(IShellFolder_GetDisplayNameOf(lpsf, lpi, dwFlags, &str)))
92         {
93           if(FAILED(StrRetToStrNA (lpFriendlyName, MAX_PATH, &str, lpi)))
94           {
95               bSuccess = FALSE;
96           }
97         }
98         else
99           bSuccess = FALSE;
100
101         TRACE("-- %s\n",lpFriendlyName);
102         return bSuccess;
103 }
104
105 static void FillTreeView(IShellFolder * lpsf, LPITEMIDLIST  pidl, HTREEITEM hParent)
106 {
107         TVITEMA         tvi;
108         TVINSERTSTRUCTA tvins;
109         HTREEITEM       hPrev = 0;
110         LPENUMIDLIST    lpe=0;
111         LPITEMIDLIST    pidlTemp=0;
112         LPTV_ITEMDATA   lptvid=0;
113         ULONG           ulFetched;
114         HRESULT         hr;
115         char            szBuff[256];
116         HWND            hwnd=GetParent(hwndTreeView);
117
118         TRACE("%p %p %x\n",lpsf, pidl, (INT)hParent);
119         SetCapture(GetParent(hwndTreeView));
120         SetCursor(LoadCursorA(0, IDC_WAITA));
121
122         hr=IShellFolder_EnumObjects(lpsf,hwnd, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS,&lpe);
123
124         if (SUCCEEDED(hr))
125         { while (NOERROR == IEnumIDList_Next(lpe,1,&pidlTemp,&ulFetched))
126           { ULONG ulAttrs = SFGAO_HASSUBFOLDER | SFGAO_FOLDER;
127             IShellFolder_GetAttributesOf(lpsf, 1, &pidlTemp, &ulAttrs);
128             if (ulAttrs & (SFGAO_HASSUBFOLDER | SFGAO_FOLDER))
129             { if (ulAttrs & SFGAO_FOLDER)
130               { tvi.mask  = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
131
132                 if (ulAttrs & SFGAO_HASSUBFOLDER)
133                 {  tvi.cChildren=1;
134                    tvi.mask |= TVIF_CHILDREN;
135                 }
136
137                 if (!( lptvid = (LPTV_ITEMDATA)SHAlloc(sizeof(TV_ITEMDATA))))
138                     goto Done;
139
140                 if (!GetName(lpsf, pidlTemp, SHGDN_NORMAL, szBuff))
141                     goto Done;
142
143                 tvi.pszText    = szBuff;
144                 tvi.cchTextMax = MAX_PATH;
145                 tvi.lParam = (LPARAM)lptvid;
146
147                 IShellFolder_AddRef(lpsf);
148                 lptvid->lpsfParent = lpsf;
149                 lptvid->lpi     = ILClone(pidlTemp);
150                 lptvid->lpifq   = ILCombine(pidl, pidlTemp);
151                 GetNormalAndSelectedIcons(lptvid->lpifq, &tvi);
152
153                 tvins.DUMMYUNIONNAME.item         = tvi;
154                 tvins.hInsertAfter = hPrev;
155                 tvins.hParent      = hParent;
156
157                 hPrev = (HTREEITEM)TreeView_InsertItemA (hwndTreeView, &tvins);
158
159               }
160             }
161             SHFree(pidlTemp);  /* Finally, free the pidl that the shell gave us... */
162             pidlTemp=0;
163           }
164         }
165
166 Done:
167         ReleaseCapture();
168         SetCursor(LoadCursorA(0, IDC_ARROWA));
169
170         if (lpe)
171           IEnumIDList_Release(lpe);
172         if (pidlTemp )
173           SHFree(pidlTemp);
174 }
175
176 static LRESULT MsgNotify(HWND hWnd,  UINT CtlID, LPNMHDR lpnmh)
177 {       
178         NMTREEVIEWA     *pnmtv   = (NMTREEVIEWA *)lpnmh;
179         LPTV_ITEMDATA   lptvid;  /* Long pointer to TreeView item data */
180         IShellFolder *  lpsf2=0;
181         
182
183         TRACE("%x %x %p msg=%x\n", hWnd,  CtlID, lpnmh, pnmtv->hdr.code);
184
185         switch (pnmtv->hdr.idFrom)
186         { case IDD_TREEVIEW:
187             switch (pnmtv->hdr.code)   
188             { case TVN_DELETEITEM:
189                 { FIXME("TVN_DELETEITEM\n");
190                   lptvid=(LPTV_ITEMDATA)pnmtv->itemOld.lParam;
191                   IShellFolder_Release(lptvid->lpsfParent);
192                   SHFree(lptvid->lpi);  
193                   SHFree(lptvid->lpifq);  
194                   SHFree(lptvid);  
195                 }
196                 break;
197                         
198               case TVN_ITEMEXPANDING:
199                 { FIXME("TVN_ITEMEXPANDING\n");
200                   if ((pnmtv->itemNew.state & TVIS_EXPANDEDONCE))
201                     break;
202                 
203                   lptvid=(LPTV_ITEMDATA)pnmtv->itemNew.lParam;
204                   if (SUCCEEDED(IShellFolder_BindToObject(lptvid->lpsfParent, lptvid->lpi,0,(REFIID)&IID_IShellFolder,(LPVOID *)&lpsf2)))
205                   { FillTreeView( lpsf2, lptvid->lpifq, pnmtv->itemNew.hItem );
206                   }
207                   TreeView_SortChildren(hwndTreeView, pnmtv->itemNew.hItem, FALSE);
208                 }
209                 break;
210               case TVN_SELCHANGED:
211                 lptvid=(LPTV_ITEMDATA)pnmtv->itemNew.lParam;
212                 pidlRet = lptvid->lpifq;
213                 break;
214
215               default:
216                 FIXME("unhandled\n");
217                 break;
218             }
219             break;
220
221           default:
222             break;
223         }
224
225         return 0;
226 }
227
228
229 /*************************************************************************
230  *             BrsFolderDlgProc32  (not an exported API function)
231  */
232 static BOOL WINAPI BrsFolderDlgProc( HWND hWnd, UINT msg, WPARAM wParam,
233                                      LPARAM lParam )
234 {    TRACE("hwnd=%i msg=%i 0x%08x 0x%08lx\n", hWnd,  msg, wParam, lParam );
235
236         switch(msg)
237         { case WM_INITDIALOG:
238             pidlRet = NULL;
239             lpBrowseInfo = (LPBROWSEINFOA) lParam;
240             if (lpBrowseInfo->lpfn)
241               FIXME("Callbacks not implemented\n");
242             if (lpBrowseInfo->ulFlags)
243               FIXME("flag %x not implemented\n", lpBrowseInfo->ulFlags);
244             if (lpBrowseInfo->lpszTitle)
245               FIXME("title %s not displayed\n", lpBrowseInfo->lpszTitle);
246             if ( lpBrowseInfo->pidlRoot )
247               FIXME("root is desktop\n");
248
249             InitializeTreeView ( hWnd);
250             return 1;
251
252           case WM_NOTIFY:
253             MsgNotify( hWnd, (UINT)wParam, (LPNMHDR)lParam);
254             break;
255             
256           case WM_COMMAND:
257             switch (wParam)
258             { case IDOK:
259                 pdump ( pidlRet );
260                 SHGetPathFromIDListA(pidlRet, lpBrowseInfo->pszDisplayName);
261                 EndDialog(hWnd, (DWORD) ILClone(pidlRet));
262                 return TRUE;
263
264               case IDCANCEL:
265                 EndDialog(hWnd, 0);
266                 return TRUE;
267             }
268             break;
269         }
270         return 0;
271 }
272
273 /*************************************************************************
274  * SHBrowseForFolderA [SHELL32.209]
275  *
276  */
277 LPITEMIDLIST WINAPI SHBrowseForFolderA (LPBROWSEINFOA lpbi)
278 {
279         TRACE("(%lx,%s) empty stub!\n", (DWORD)lpbi, lpbi->lpszTitle);
280
281         return (LPITEMIDLIST) DialogBoxParamA( shell32_hInstance,
282                         "SHBRSFORFOLDER_MSGBOX", 0,
283                         BrsFolderDlgProc, (INT)lpbi );
284 }