Fix some types and function prototypes according to Platform SDK
[wine.git] / dlls / shell32 / brsfolder.c
blob0f325adb1408041d6dbd39ade7985c76a18189d7
1 /*
2 * Copyright 1999 Juergen Schmied
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.
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.
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
18 * FIXME:
19 * - view with root unequal desktop
20 * - many memory leaks
21 * - show only filesystem objects
24 #include <stdlib.h>
25 #include <string.h>
27 #include "wine/debug.h"
28 #include "undocshell.h"
29 #include "shlguid.h"
30 #include "pidl.h"
31 #include "shell32_main.h"
32 #include "shellapi.h"
33 #include "shresdef.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(shell);
37 static HWND hwndTreeView;
38 static LPBROWSEINFOA lpBrowseInfo;
39 static LPITEMIDLIST pidlRet;
41 static void FillTreeView(LPSHELLFOLDER lpsf, LPITEMIDLIST lpifq, HTREEITEM hParent);
43 static void InitializeTreeView(HWND hwndParent, LPCITEMIDLIST root)
45 HIMAGELIST hImageList;
46 IShellFolder * lpsf;
47 HRESULT hr;
49 hwndTreeView = GetDlgItem (hwndParent, IDD_TREEVIEW);
50 Shell_GetImageList(NULL, &hImageList);
52 TRACE("dlg=%x tree=%x\n", hwndParent, hwndTreeView );
54 if (hImageList && hwndTreeView)
55 { TreeView_SetImageList(hwndTreeView, hImageList, 0);
58 /* so far, this method doesn't work (still missing the upper level), keep the old way */
59 #if 0
60 if (_ILIsDesktop (root)) {
61 hr = SHGetDesktopFolder(&lpsf);
62 } else {
63 IShellFolder * lpsfdesktop;
65 hr = SHGetDesktopFolder(&lpsfdesktop);
66 if (SUCCEEDED(hr)) {
67 hr = IShellFolder_BindToObject(lpsfdesktop, root, 0,(REFIID)&IID_IShellFolder,(LPVOID *)&lpsf);
68 IShellFolder_Release(lpsfdesktop);
71 #else
72 hr = SHGetDesktopFolder(&lpsf);
73 #endif
75 if (SUCCEEDED(hr) && hwndTreeView)
76 { TreeView_DeleteAllItems(hwndTreeView);
77 FillTreeView(lpsf, NULL, TVI_ROOT);
80 if (SUCCEEDED(hr))
81 { IShellFolder_Release(lpsf);
83 TRACE("done\n");
86 static int GetIcon(LPITEMIDLIST lpi, UINT uFlags)
87 { SHFILEINFOA sfi;
88 SHGetFileInfoA((LPCSTR)lpi,0,&sfi, sizeof(SHFILEINFOA), uFlags);
89 return sfi.iIcon;
92 static void GetNormalAndSelectedIcons(LPITEMIDLIST lpifq,LPTVITEMA lpTV_ITEM)
93 { TRACE("%p %p\n",lpifq, lpTV_ITEM);
95 lpTV_ITEM->iImage = GetIcon(lpifq, SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
96 lpTV_ITEM->iSelectedImage = GetIcon(lpifq, SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_OPENICON);
98 return;
101 typedef struct tagID
103 LPSHELLFOLDER lpsfParent;
104 LPITEMIDLIST lpi;
105 LPITEMIDLIST lpifq;
106 } TV_ITEMDATA, *LPTV_ITEMDATA;
108 static BOOL GetName(LPSHELLFOLDER lpsf, LPITEMIDLIST lpi, DWORD dwFlags, LPSTR lpFriendlyName)
110 BOOL bSuccess=TRUE;
111 STRRET str;
113 TRACE("%p %p %lx %p\n", lpsf, lpi, dwFlags, lpFriendlyName);
114 if (SUCCEEDED(IShellFolder_GetDisplayNameOf(lpsf, lpi, dwFlags, &str)))
116 if(FAILED(StrRetToStrNA (lpFriendlyName, MAX_PATH, &str, lpi)))
118 bSuccess = FALSE;
121 else
122 bSuccess = FALSE;
124 TRACE("-- %s\n",lpFriendlyName);
125 return bSuccess;
128 static void FillTreeView(IShellFolder * lpsf, LPITEMIDLIST pidl, HTREEITEM hParent)
130 TVITEMA tvi;
131 TVINSERTSTRUCTA tvins;
132 HTREEITEM hPrev = 0;
133 LPENUMIDLIST lpe=0;
134 LPITEMIDLIST pidlTemp=0;
135 LPTV_ITEMDATA lptvid=0;
136 ULONG ulFetched;
137 HRESULT hr;
138 char szBuff[256];
139 HWND hwnd=GetParent(hwndTreeView);
141 TRACE("%p %p %x\n",lpsf, pidl, (INT)hParent);
142 SetCapture(GetParent(hwndTreeView));
143 SetCursor(LoadCursorA(0, IDC_WAITA));
145 hr=IShellFolder_EnumObjects(lpsf,hwnd, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS,&lpe);
147 if (SUCCEEDED(hr))
148 { while (NOERROR == IEnumIDList_Next(lpe,1,&pidlTemp,&ulFetched))
149 { ULONG ulAttrs = SFGAO_HASSUBFOLDER | SFGAO_FOLDER;
150 IShellFolder_GetAttributesOf(lpsf, 1, &pidlTemp, &ulAttrs);
151 if (ulAttrs & (SFGAO_HASSUBFOLDER | SFGAO_FOLDER))
152 { if (ulAttrs & SFGAO_FOLDER)
153 { tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
155 if (ulAttrs & SFGAO_HASSUBFOLDER)
156 { tvi.cChildren=1;
157 tvi.mask |= TVIF_CHILDREN;
160 if (!( lptvid = (LPTV_ITEMDATA)SHAlloc(sizeof(TV_ITEMDATA))))
161 goto Done;
163 if (!GetName(lpsf, pidlTemp, SHGDN_NORMAL, szBuff))
164 goto Done;
166 tvi.pszText = szBuff;
167 tvi.cchTextMax = MAX_PATH;
168 tvi.lParam = (LPARAM)lptvid;
170 IShellFolder_AddRef(lpsf);
171 lptvid->lpsfParent = lpsf;
172 lptvid->lpi = ILClone(pidlTemp);
173 lptvid->lpifq = ILCombine(pidl, pidlTemp);
174 GetNormalAndSelectedIcons(lptvid->lpifq, &tvi);
176 tvins.DUMMYUNIONNAME.item = tvi;
177 tvins.hInsertAfter = hPrev;
178 tvins.hParent = hParent;
180 hPrev = (HTREEITEM)TreeView_InsertItemA (hwndTreeView, &tvins);
184 SHFree(pidlTemp); /* Finally, free the pidl that the shell gave us... */
185 pidlTemp=0;
189 Done:
190 ReleaseCapture();
191 SetCursor(LoadCursorA(0, IDC_ARROWA));
193 if (lpe)
194 IEnumIDList_Release(lpe);
195 if (pidlTemp )
196 SHFree(pidlTemp);
199 static LRESULT MsgNotify(HWND hWnd, UINT CtlID, LPNMHDR lpnmh)
201 NMTREEVIEWA *pnmtv = (NMTREEVIEWA *)lpnmh;
202 LPTV_ITEMDATA lptvid; /* Long pointer to TreeView item data */
203 IShellFolder * lpsf2=0;
206 TRACE("%x %x %p msg=%x\n", hWnd, CtlID, lpnmh, pnmtv->hdr.code);
208 switch (pnmtv->hdr.idFrom)
209 { case IDD_TREEVIEW:
210 switch (pnmtv->hdr.code)
211 { case TVN_DELETEITEMA: case TVN_DELETEITEMW:
212 { FIXME("TVN_DELETEITEMA/W\n");
213 lptvid=(LPTV_ITEMDATA)pnmtv->itemOld.lParam;
214 IShellFolder_Release(lptvid->lpsfParent);
215 SHFree(lptvid->lpi);
216 SHFree(lptvid->lpifq);
217 SHFree(lptvid);
219 break;
221 case TVN_ITEMEXPANDINGA: case TVN_ITEMEXPANDINGW:
222 { FIXME("TVN_ITEMEXPANDINGA/W\n");
223 if ((pnmtv->itemNew.state & TVIS_EXPANDEDONCE))
224 break;
226 lptvid=(LPTV_ITEMDATA)pnmtv->itemNew.lParam;
227 if (SUCCEEDED(IShellFolder_BindToObject(lptvid->lpsfParent, lptvid->lpi,0,(REFIID)&IID_IShellFolder,(LPVOID *)&lpsf2)))
228 { FillTreeView( lpsf2, lptvid->lpifq, pnmtv->itemNew.hItem );
230 TreeView_SortChildren(hwndTreeView, pnmtv->itemNew.hItem, FALSE);
232 break;
233 case TVN_SELCHANGEDA: case TVN_SELCHANGEDW:
234 lptvid=(LPTV_ITEMDATA)pnmtv->itemNew.lParam;
235 pidlRet = lptvid->lpifq;
236 if (lpBrowseInfo->lpfn)
237 (lpBrowseInfo->lpfn)(hWnd, BFFM_SELCHANGED, (LPARAM)pidlRet, lpBrowseInfo->lParam);
238 break;
240 default:
241 FIXME("unhandled (%d)\n", pnmtv->hdr.code);
242 break;
244 break;
246 default:
247 break;
250 return 0;
254 /*************************************************************************
255 * BrsFolderDlgProc32 (not an exported API function)
257 static INT_PTR CALLBACK BrsFolderDlgProc( HWND hWnd, UINT msg, WPARAM wParam,
258 LPARAM lParam )
260 TRACE("hwnd=%08x msg=%04x 0x%08x 0x%08lx\n", hWnd, msg, wParam, lParam );
262 switch(msg)
263 { case WM_INITDIALOG:
264 pidlRet = NULL;
265 lpBrowseInfo = (LPBROWSEINFOA) lParam;
266 if (lpBrowseInfo->ulFlags & ~(BIF_STATUSTEXT))
267 FIXME("flags %x not implemented\n", lpBrowseInfo->ulFlags & ~(BIF_STATUSTEXT));
268 if (lpBrowseInfo->lpszTitle) {
269 SetWindowTextA(GetDlgItem(hWnd, IDD_TITLE), lpBrowseInfo->lpszTitle);
270 } else {
271 ShowWindow(GetDlgItem(hWnd, IDD_TITLE), SW_HIDE);
273 if (!(lpBrowseInfo->ulFlags & BIF_STATUSTEXT))
274 ShowWindow(GetDlgItem(hWnd, IDD_STATUS), SW_HIDE);
276 if ( lpBrowseInfo->pidlRoot )
277 FIXME("root is desktop\n");
279 InitializeTreeView( hWnd, lpBrowseInfo->pidlRoot );
281 if (lpBrowseInfo->lpfn) {
282 (lpBrowseInfo->lpfn)(hWnd, BFFM_INITIALIZED, 0, lpBrowseInfo->lParam);
283 (lpBrowseInfo->lpfn)(hWnd, BFFM_SELCHANGED, 0/*FIXME*/, lpBrowseInfo->lParam);
286 return TRUE;
288 case WM_NOTIFY:
289 MsgNotify( hWnd, (UINT)wParam, (LPNMHDR)lParam);
290 break;
292 case WM_COMMAND:
293 switch (wParam)
294 { case IDOK:
295 pdump ( pidlRet );
296 SHGetPathFromIDListA(pidlRet, lpBrowseInfo->pszDisplayName);
297 EndDialog(hWnd, (DWORD) ILClone(pidlRet));
298 return TRUE;
300 case IDCANCEL:
301 EndDialog(hWnd, 0);
302 return TRUE;
304 break;
305 case BFFM_SETSTATUSTEXTA:
306 TRACE("Set status %s\n", debugstr_a((LPSTR)lParam));
307 SetWindowTextA(GetDlgItem(hWnd, IDD_STATUS), (LPSTR)lParam);
308 break;
309 case BFFM_SETSTATUSTEXTW:
310 TRACE("Set status %s\n", debugstr_w((LPWSTR)lParam));
311 SetWindowTextW(GetDlgItem(hWnd, IDD_STATUS), (LPWSTR)lParam);
312 break;
313 case BFFM_ENABLEOK:
314 TRACE("Enable %ld\n", lParam);
315 EnableWindow(GetDlgItem(hWnd, 1), (lParam)?TRUE:FALSE);
316 break;
317 case BFFM_SETSELECTIONA:
318 if (wParam)
319 TRACE("Set selection %s\n", debugstr_a((LPSTR)lParam));
320 else
321 TRACE("Set selection %p\n", (void*)lParam);
322 break;
323 case BFFM_SETSELECTIONW:
324 if (wParam)
325 TRACE("Set selection %s\n", debugstr_w((LPWSTR)lParam));
326 else
327 TRACE("Set selection %p\n", (void*)lParam);
328 break;
330 return FALSE;
333 /*************************************************************************
334 * SHBrowseForFolderA [SHELL32.@]
335 * SHBrowseForFolder [SHELL32.@]
338 LPITEMIDLIST WINAPI SHBrowseForFolderA (LPBROWSEINFOA lpbi)
340 TRACE("(%p{lpszTitle=%s,owner=%i})\n",
341 lpbi, debugstr_a(lpbi->lpszTitle), lpbi->hwndOwner);
343 return (LPITEMIDLIST) DialogBoxParamA( shell32_hInstance,
344 "SHBRSFORFOLDER_MSGBOX",
345 lpbi->hwndOwner,
346 BrsFolderDlgProc, (INT)lpbi );
349 /*************************************************************************
350 * SHBrowseForFolderW [SHELL32.@]
352 LPITEMIDLIST WINAPI SHBrowseForFolderW (LPBROWSEINFOW lpbi)
354 char szDisplayName[MAX_PATH], szTitle[MAX_PATH];
355 BROWSEINFOA bi;
357 TRACE("((%p->{lpszTitle=%s,owner=%i})\n", lpbi,
358 lpbi ? debugstr_w(lpbi->lpszTitle): NULL, lpbi ? lpbi->hwndOwner: 0);
360 if (!lpbi)
361 return NULL;
363 bi.hwndOwner = lpbi->hwndOwner;
364 bi.pidlRoot = lpbi->pidlRoot;
365 if (lpbi->pszDisplayName)
367 WideCharToMultiByte(CP_ACP, 0, lpbi->pszDisplayName, -1, szDisplayName, MAX_PATH, 0, NULL);
368 bi.pszDisplayName = szDisplayName;
370 else
371 bi.pszDisplayName = NULL;
373 if (lpbi->lpszTitle)
375 WideCharToMultiByte(CP_ACP, 0, lpbi->lpszTitle, -1, szTitle, MAX_PATH, 0, NULL);
376 bi.lpszTitle = szTitle;
378 else
379 bi.lpszTitle = NULL;
381 bi.ulFlags = lpbi->ulFlags;
382 bi.lpfn = lpbi->lpfn;
383 bi.lParam = lpbi->lParam;
384 bi.iImage = lpbi->iImage;
385 return (LPITEMIDLIST) DialogBoxParamA(shell32_hInstance,
386 "SHBRSFORFOLDER_MSGBOX", lpbi->hwndOwner,
387 BrsFolderDlgProc, (INT)lpbi);