Moved [efg]cvt functions to msvcrt and fixed them to use a per-thread
[wine] / dlls / shell32 / brsfolder.c
1 /*
2  * Copyright 1999 Juergen Schmied
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  * FIXME:
19  *  - view with root unequal desktop
20  *  - many memory leaks
21  *  - show only filesystem objects
22  */
23
24 #include <stdlib.h>
25 #include <string.h>
26
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
29 #include "wine/debug.h"
30 #include "undocshell.h"
31 #include "shlguid.h"
32 #include "pidl.h"
33 #include "shell32_main.h"
34 #include "shellapi.h"
35 #include "shresdef.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(shell);
38
39 static HWND             hwndTreeView;
40 static LPBROWSEINFOW    lpBrowseInfo;
41 static LPITEMIDLIST     pidlRet;
42
43 static void FillTreeView(LPSHELLFOLDER lpsf, LPITEMIDLIST  lpifq, HTREEITEM hParent);
44
45 static void InitializeTreeView(HWND hwndParent, LPCITEMIDLIST root)
46 {
47         HIMAGELIST      hImageList;
48         IShellFolder *  lpsf;
49         HRESULT hr;
50
51         hwndTreeView = GetDlgItem (hwndParent, IDD_TREEVIEW);
52         Shell_GetImageList(NULL, &hImageList);
53
54         TRACE("dlg=%p tree=%p\n", hwndParent, hwndTreeView );
55
56         if (hImageList && hwndTreeView)
57         { TreeView_SetImageList(hwndTreeView, hImageList, 0);
58         }
59
60         /* so far, this method doesn't work (still missing the upper level), keep the old way */
61 #if 0
62         if (_ILIsDesktop (root)) {
63            hr = SHGetDesktopFolder(&lpsf);
64         } else {
65            IShellFolder *       lpsfdesktop;
66
67            hr = SHGetDesktopFolder(&lpsfdesktop);
68            if (SUCCEEDED(hr)) {
69               hr = IShellFolder_BindToObject(lpsfdesktop, root, 0,(REFIID)&IID_IShellFolder,(LPVOID *)&lpsf);
70               IShellFolder_Release(lpsfdesktop);
71            }
72         }
73 #else
74         hr = SHGetDesktopFolder(&lpsf);
75 #endif
76
77         if (SUCCEEDED(hr) && hwndTreeView)
78         { TreeView_DeleteAllItems(hwndTreeView);
79           FillTreeView(lpsf, NULL, TVI_ROOT);
80         }
81
82         if (SUCCEEDED(hr))
83         { IShellFolder_Release(lpsf);
84         }
85         TRACE("done\n");
86 }
87
88 static int GetIcon(LPITEMIDLIST lpi, UINT uFlags)
89 {       SHFILEINFOW    sfi;
90         SHGetFileInfoW((LPCWSTR)lpi, 0 ,&sfi, sizeof(SHFILEINFOW), uFlags);
91         return sfi.iIcon;
92 }
93
94 static void GetNormalAndSelectedIcons(LPITEMIDLIST lpifq, LPTVITEMW lpTV_ITEM)
95 {       TRACE("%p %p\n",lpifq, lpTV_ITEM);
96
97         lpTV_ITEM->iImage = GetIcon(lpifq, SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
98         lpTV_ITEM->iSelectedImage = GetIcon(lpifq, SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_OPENICON);
99
100         return;
101 }
102
103 typedef struct tagID
104 {
105    LPSHELLFOLDER lpsfParent;
106    LPITEMIDLIST  lpi;
107    LPITEMIDLIST  lpifq;
108 } TV_ITEMDATA, *LPTV_ITEMDATA;
109
110 static BOOL GetName(LPSHELLFOLDER lpsf, LPITEMIDLIST lpi, DWORD dwFlags, LPWSTR lpFriendlyName)
111 {
112         BOOL   bSuccess=TRUE;
113         STRRET str;
114
115         TRACE("%p %p %lx %p\n", lpsf, lpi, dwFlags, lpFriendlyName);
116         if (SUCCEEDED(IShellFolder_GetDisplayNameOf(lpsf, lpi, dwFlags, &str)))
117         {
118           if (FAILED(StrRetToStrNW(lpFriendlyName, MAX_PATH, &str, lpi)))
119           {
120               bSuccess = FALSE;
121           }
122         }
123         else
124           bSuccess = FALSE;
125
126         TRACE("-- %s\n", debugstr_w(lpFriendlyName));
127         return bSuccess;
128 }
129
130 static void FillTreeView(IShellFolder * lpsf, LPITEMIDLIST  pidl, HTREEITEM hParent)
131 {
132         TVITEMW         tvi;
133         TVINSERTSTRUCTW tvins;
134         HTREEITEM       hPrev = 0;
135         LPENUMIDLIST    lpe=0;
136         LPITEMIDLIST    pidlTemp=0;
137         LPTV_ITEMDATA   lptvid=0;
138         ULONG           ulFetched;
139         HRESULT         hr;
140         WCHAR           szBuff[MAX_PATH];
141         HWND            hwnd=GetParent(hwndTreeView);
142
143         TRACE("%p %p %x\n",lpsf, pidl, (INT)hParent);
144         SetCapture(GetParent(hwndTreeView));
145         SetCursor(LoadCursorA(0, IDC_WAITA));
146
147         hr=IShellFolder_EnumObjects(lpsf, hwnd, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &lpe);
148
149         if (SUCCEEDED(hr))
150         { while (NOERROR == IEnumIDList_Next(lpe,1,&pidlTemp,&ulFetched))
151           { ULONG ulAttrs = SFGAO_HASSUBFOLDER | SFGAO_FOLDER;
152             IShellFolder_GetAttributesOf(lpsf, 1, &pidlTemp, &ulAttrs);
153             if (ulAttrs & (SFGAO_HASSUBFOLDER | SFGAO_FOLDER))
154             { if (ulAttrs & SFGAO_FOLDER)
155               { tvi.mask  = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
156
157                 if (ulAttrs & SFGAO_HASSUBFOLDER)
158                 {  tvi.cChildren=1;
159                    tvi.mask |= TVIF_CHILDREN;
160                 }
161
162                 if (!(lptvid = (LPTV_ITEMDATA)SHAlloc(sizeof(TV_ITEMDATA))))
163                     goto Done;
164
165                 if (!GetName(lpsf, pidlTemp, SHGDN_NORMAL, szBuff))
166                     goto Done;
167
168                 tvi.pszText    = szBuff;
169                 tvi.cchTextMax = MAX_PATH;
170                 tvi.lParam = (LPARAM)lptvid;
171
172                 IShellFolder_AddRef(lpsf);
173                 lptvid->lpsfParent = lpsf;
174                 lptvid->lpi     = ILClone(pidlTemp);
175                 lptvid->lpifq   = ILCombine(pidl, pidlTemp);
176                 GetNormalAndSelectedIcons(lptvid->lpifq, &tvi);
177
178                 tvins.DUMMYUNIONNAME.item         = tvi;
179                 tvins.hInsertAfter = hPrev;
180                 tvins.hParent      = hParent;
181
182                 hPrev = (HTREEITEM)TreeView_InsertItemW(hwndTreeView, &tvins);
183
184               }
185             }
186             SHFree(pidlTemp);  /* Finally, free the pidl that the shell gave us... */
187             pidlTemp=0;
188           }
189         }
190
191 Done:
192         ReleaseCapture();
193         SetCursor(LoadCursorW(0, IDC_ARROWW));
194
195         if (lpe)
196           IEnumIDList_Release(lpe);
197         if (pidlTemp )
198           SHFree(pidlTemp);
199 }
200
201 static LRESULT MsgNotify(HWND hWnd,  UINT CtlID, LPNMHDR lpnmh)
202 {
203         NMTREEVIEWW     *pnmtv   = (NMTREEVIEWW *)lpnmh;
204         LPTV_ITEMDATA   lptvid;  /* Long pointer to TreeView item data */
205         IShellFolder *  lpsf2=0;
206
207
208         TRACE("%p %x %p msg=%x\n", hWnd,  CtlID, lpnmh, pnmtv->hdr.code);
209
210         switch (pnmtv->hdr.idFrom)
211         { case IDD_TREEVIEW:
212             switch (pnmtv->hdr.code)
213             { case TVN_DELETEITEMA:
214               case TVN_DELETEITEMW:
215                 { FIXME("TVN_DELETEITEMA/W\n");
216                   lptvid=(LPTV_ITEMDATA)pnmtv->itemOld.lParam;
217                   IShellFolder_Release(lptvid->lpsfParent);
218                   SHFree(lptvid->lpi);
219                   SHFree(lptvid->lpifq);
220                   SHFree(lptvid);
221                 }
222                 break;
223
224               case TVN_ITEMEXPANDINGA:
225               case TVN_ITEMEXPANDINGW:
226                 { FIXME("TVN_ITEMEXPANDINGA/W\n");
227                   if ((pnmtv->itemNew.state & TVIS_EXPANDEDONCE))
228                     break;
229
230                   lptvid=(LPTV_ITEMDATA)pnmtv->itemNew.lParam;
231                   if (SUCCEEDED(IShellFolder_BindToObject(lptvid->lpsfParent, lptvid->lpi,0,(REFIID)&IID_IShellFolder,(LPVOID *)&lpsf2)))
232                   { FillTreeView( lpsf2, lptvid->lpifq, pnmtv->itemNew.hItem );
233                   }
234                   TreeView_SortChildren(hwndTreeView, pnmtv->itemNew.hItem, FALSE);
235                 }
236                 break;
237               case TVN_SELCHANGEDA:
238               case TVN_SELCHANGEDW:
239                 lptvid=(LPTV_ITEMDATA)pnmtv->itemNew.lParam;
240                 pidlRet = lptvid->lpifq;
241                 if (lpBrowseInfo->lpfn)
242                    (lpBrowseInfo->lpfn)(hWnd, BFFM_SELCHANGED, (LPARAM)pidlRet, lpBrowseInfo->lParam);
243                 break;
244
245               default:
246                 FIXME("unhandled (%d)\n", pnmtv->hdr.code);
247                 break;
248             }
249             break;
250
251           default:
252             break;
253         }
254
255         return 0;
256 }
257
258
259 /*************************************************************************
260  *             BrsFolderDlgProc32  (not an exported API function)
261  */
262 static INT_PTR CALLBACK BrsFolderDlgProc(HWND hWnd, UINT msg, WPARAM wParam,
263                                      LPARAM lParam )
264 {
265         TRACE("hwnd=%p msg=%04x 0x%08x 0x%08lx\n", hWnd,  msg, wParam, lParam );
266
267         switch(msg)
268         { case WM_INITDIALOG:
269             pidlRet = NULL;
270             lpBrowseInfo = (LPBROWSEINFOW) lParam;
271             if (lpBrowseInfo->ulFlags & ~(BIF_STATUSTEXT))
272               FIXME("flags %x not implemented\n", lpBrowseInfo->ulFlags & ~(BIF_STATUSTEXT));
273             if (lpBrowseInfo->lpszTitle) {
274                SetWindowTextW(GetDlgItem(hWnd, IDD_TITLE), lpBrowseInfo->lpszTitle);
275             } else {
276                ShowWindow(GetDlgItem(hWnd, IDD_TITLE), SW_HIDE);
277             }
278             if (!(lpBrowseInfo->ulFlags & BIF_STATUSTEXT))
279                ShowWindow(GetDlgItem(hWnd, IDD_STATUS), SW_HIDE);
280
281             if (lpBrowseInfo->pidlRoot )
282               FIXME("root is desktop\n");
283
284             InitializeTreeView(hWnd, lpBrowseInfo->pidlRoot);
285
286             if (lpBrowseInfo->lpfn) {
287                (lpBrowseInfo->lpfn)(hWnd, BFFM_INITIALIZED, 0, lpBrowseInfo->lParam);
288                (lpBrowseInfo->lpfn)(hWnd, BFFM_SELCHANGED, 0/*FIXME*/, lpBrowseInfo->lParam);
289             }
290
291             return TRUE;
292
293           case WM_NOTIFY:
294             MsgNotify( hWnd, (UINT)wParam, (LPNMHDR)lParam);
295             break;
296
297           case WM_COMMAND:
298             switch (wParam)
299             { case IDOK:
300                 pdump ( pidlRet );
301                 SHGetPathFromIDListW(pidlRet, lpBrowseInfo->pszDisplayName);
302                 EndDialog(hWnd, (DWORD) ILClone(pidlRet));
303                 return TRUE;
304
305               case IDCANCEL:
306                 EndDialog(hWnd, 0);
307                 return TRUE;
308             }
309             break;
310         case BFFM_SETSTATUSTEXTA:
311            TRACE("Set status %s\n", debugstr_a((LPSTR)lParam));
312            SetWindowTextA(GetDlgItem(hWnd, IDD_STATUS), (LPSTR)lParam);
313            break;
314         case BFFM_SETSTATUSTEXTW:
315            TRACE("Set status %s\n", debugstr_w((LPWSTR)lParam));
316            SetWindowTextW(GetDlgItem(hWnd, IDD_STATUS), (LPWSTR)lParam);
317            break;
318         case BFFM_ENABLEOK:
319            TRACE("Enable %ld\n", lParam);
320            EnableWindow(GetDlgItem(hWnd, 1), (lParam)?TRUE:FALSE);
321            break;
322         case BFFM_SETSELECTIONA:
323            if (wParam)
324               TRACE("Set selection %s\n", debugstr_a((LPSTR)lParam));
325            else
326               TRACE("Set selection %p\n", (void*)lParam);
327            break;
328         case BFFM_SETSELECTIONW:
329            if (wParam)
330               TRACE("Set selection %s\n", debugstr_w((LPWSTR)lParam));
331            else
332               TRACE("Set selection %p\n", (void*)lParam);
333            break;
334         }
335         return FALSE;
336 }
337
338 static WCHAR swBrowseTempName[] = {'S','H','B','R','S','F','O','R','F','O','L','D','E','R','_','M','S','G','B','O','X',0};
339
340 /*************************************************************************
341  * SHBrowseForFolderA [SHELL32.@]
342  * SHBrowseForFolder  [SHELL32.@]
343  */
344 LPITEMIDLIST WINAPI SHBrowseForFolderA (LPBROWSEINFOA lpbi)
345 {
346         BROWSEINFOW bi;
347         LPITEMIDLIST lpid;
348         INT len;
349         
350         TRACE("(%p{lpszTitle=%s,owner=%p})\n", lpbi,
351             lpbi ? debugstr_a(lpbi->lpszTitle) : NULL, lpbi ? lpbi->hwndOwner : NULL);
352
353         if (!lpbi)
354           return NULL;
355
356         bi.hwndOwner = lpbi->hwndOwner;
357         bi.pidlRoot = lpbi->pidlRoot;
358         if (lpbi->pszDisplayName)
359         {
360           len = MultiByteToWideChar(CP_ACP, 0, lpbi->pszDisplayName, -1, NULL, 0);
361           bi.pszDisplayName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
362           MultiByteToWideChar(CP_ACP, 0, lpbi->pszDisplayName, -1, bi.pszDisplayName, len);
363         }
364         else
365           bi.pszDisplayName = NULL;
366
367         if (lpbi->lpszTitle)
368         {
369           len = MultiByteToWideChar(CP_ACP, 0, lpbi->lpszTitle, -1, NULL, 0);
370           bi.lpszTitle = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
371           MultiByteToWideChar(CP_ACP, 0, lpbi->lpszTitle, -1, (LPWSTR)bi.lpszTitle, len);
372         }
373         else
374           bi.lpszTitle = NULL;
375
376         bi.ulFlags = lpbi->ulFlags;
377         bi.lpfn = lpbi->lpfn;
378         bi.lParam = lpbi->lParam;
379         bi.iImage = lpbi->iImage;
380         lpid = (LPITEMIDLIST) DialogBoxParamW(shell32_hInstance,
381                                               swBrowseTempName, lpbi->hwndOwner,
382                                               BrsFolderDlgProc, (INT)&bi);
383         if (bi.pszDisplayName)
384         {
385           WideCharToMultiByte(CP_ACP, 0, bi.pszDisplayName, -1, lpbi->pszDisplayName, MAX_PATH, 0, NULL);
386           HeapFree(GetProcessHeap(), 0, bi.pszDisplayName);
387         }
388         if (bi.lpszTitle)
389         {
390           HeapFree(GetProcessHeap(), 0, (LPVOID)bi.lpszTitle);
391         }
392         lpbi->iImage = bi.iImage;
393         return lpid;
394 }
395
396
397 /*************************************************************************
398  * SHBrowseForFolderW [SHELL32.@]
399  */
400 LPITEMIDLIST WINAPI SHBrowseForFolderW (LPBROWSEINFOW lpbi)
401 {
402         TRACE("((%p->{lpszTitle=%s,owner=%p})\n", lpbi,
403             lpbi ? debugstr_w(lpbi->lpszTitle) : NULL, lpbi ? lpbi->hwndOwner : 0);
404
405         if (!lpbi)
406           return NULL;
407
408         return (LPITEMIDLIST) DialogBoxParamW(shell32_hInstance,
409                                               swBrowseTempName, lpbi->hwndOwner,
410                                               BrsFolderDlgProc, (INT)lpbi);
411 }