reg: Stop parsing REG_SZ values containing NUL character sequences.
[wine/zf.git] / dlls / explorerframe / nstc.c
blob0698e6fa065578fcf2fbc4e61abf837090e5152f
1 /*
2 * NamespaceTreeControl implementation.
4 * Copyright 2010 David Hedberg
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.
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.
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
21 #include <stdarg.h>
23 #define COBJMACROS
24 #define NONAMELESSUNION
26 #include "winerror.h"
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "shellapi.h"
31 #include "commctrl.h"
32 #include "commoncontrols.h"
34 #include "wine/list.h"
35 #include "wine/debug.h"
36 #include "wine/heap.h"
38 #include "explorerframe_main.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(nstc);
42 typedef struct nstc_root {
43 IShellItem *psi;
44 HTREEITEM htreeitem;
45 SHCONTF enum_flags;
46 NSTCROOTSTYLE root_style;
47 IShellItemFilter *pif;
48 struct list entry;
49 } nstc_root;
51 typedef struct {
52 INameSpaceTreeControl2 INameSpaceTreeControl2_iface;
53 IOleWindow IOleWindow_iface;
54 LONG ref;
56 HWND hwnd_main;
57 HWND hwnd_tv;
59 WNDPROC tv_oldwndproc;
61 NSTCSTYLE style;
62 NSTCSTYLE2 style2;
63 struct list roots;
65 INameSpaceTreeControlCustomDraw *customdraw;
66 INameSpaceTreeControlDropHandler *dragdrop;
67 INameSpaceTreeControlEvents *events;
68 } NSTC2Impl;
70 static const DWORD unsupported_styles =
71 NSTCS_NOREPLACEOPEN | NSTCS_NOORDERSTREAM | NSTCS_FAVORITESMODE |
72 NSTCS_EMPTYTEXT | NSTCS_ALLOWJUNCTIONS | NSTCS_SHOWTABSBUTTON | NSTCS_SHOWDELETEBUTTON |
73 NSTCS_SHOWREFRESHBUTTON | NSTCS_SPRINGEXPAND | NSTCS_RICHTOOLTIP | NSTCS_NOINDENTCHECKS;
74 static const DWORD unsupported_styles2 =
75 NSTCS2_INTERRUPTNOTIFICATIONS | NSTCS2_SHOWNULLSPACEMENU | NSTCS2_DISPLAYPADDING |
76 NSTCS2_DISPLAYPINNEDONLY | NTSCS2_NOSINGLETONAUTOEXPAND | NTSCS2_NEVERINSERTNONENUMERATED;
78 static inline NSTC2Impl *impl_from_INameSpaceTreeControl2(INameSpaceTreeControl2 *iface)
80 return CONTAINING_RECORD(iface, NSTC2Impl, INameSpaceTreeControl2_iface);
83 static inline NSTC2Impl *impl_from_IOleWindow(IOleWindow *iface)
85 return CONTAINING_RECORD(iface, NSTC2Impl, IOleWindow_iface);
88 /* Forward declarations */
89 static LRESULT CALLBACK tv_wndproc(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam);
91 /*************************************************************************
92 * NamespaceTree event wrappers
94 static HRESULT events_OnGetDefaultIconIndex(NSTC2Impl *This, IShellItem *psi,
95 int *piDefaultIcon, int *piOpenIcon)
97 HRESULT ret;
98 LONG refcount;
99 if(!This->events) return E_NOTIMPL;
101 refcount = IShellItem_AddRef(psi);
102 ret = INameSpaceTreeControlEvents_OnGetDefaultIconIndex(This->events, psi, piDefaultIcon, piOpenIcon);
103 if(IShellItem_Release(psi) < refcount - 1)
104 ERR("ShellItem was released by client - please file a bug.\n");
105 return ret;
108 static HRESULT events_OnItemAdded(NSTC2Impl *This, IShellItem *psi, BOOL fIsRoot)
110 HRESULT ret;
111 LONG refcount;
112 if(!This->events) return S_OK;
114 refcount = IShellItem_AddRef(psi);
115 ret = INameSpaceTreeControlEvents_OnItemAdded(This->events, psi, fIsRoot);
116 if(IShellItem_Release(psi) < refcount - 1)
117 ERR("ShellItem was released by client - please file a bug.\n");
118 return ret;
121 static HRESULT events_OnItemDeleted(NSTC2Impl *This, IShellItem *psi, BOOL fIsRoot)
123 HRESULT ret;
124 LONG refcount;
125 if(!This->events) return S_OK;
127 refcount = IShellItem_AddRef(psi);
128 ret = INameSpaceTreeControlEvents_OnItemDeleted(This->events, psi, fIsRoot);
129 if(IShellItem_Release(psi) < refcount - 1)
130 ERR("ShellItem was released by client - please file a bug.\n");
131 return ret;
134 static HRESULT events_OnBeforeExpand(NSTC2Impl *This, IShellItem *psi)
136 HRESULT ret;
137 LONG refcount;
138 if(!This->events) return S_OK;
140 refcount = IShellItem_AddRef(psi);
141 ret = INameSpaceTreeControlEvents_OnBeforeExpand(This->events, psi);
142 if(IShellItem_Release(psi) < refcount - 1)
143 ERR("ShellItem was released by client - please file a bug.\n");
144 return ret;
147 static HRESULT events_OnAfterExpand(NSTC2Impl *This, IShellItem *psi)
149 HRESULT ret;
150 LONG refcount;
151 if(!This->events) return S_OK;
153 refcount = IShellItem_AddRef(psi);
154 ret = INameSpaceTreeControlEvents_OnAfterExpand(This->events, psi);
155 if(IShellItem_Release(psi) < refcount - 1)
156 ERR("ShellItem was released by client - please file a bug.\n");
157 return ret;
160 static HRESULT events_OnItemClick(NSTC2Impl *This, IShellItem *psi,
161 NSTCEHITTEST nstceHitTest, NSTCECLICKTYPE nstceClickType)
163 HRESULT ret;
164 LONG refcount;
165 if(!This->events) return S_OK;
167 refcount = IShellItem_AddRef(psi);
168 ret = INameSpaceTreeControlEvents_OnItemClick(This->events, psi, nstceHitTest, nstceClickType);
169 if(IShellItem_Release(psi) < refcount - 1)
170 ERR("ShellItem was released by client - please file a bug.\n");
171 return ret;
174 static HRESULT events_OnSelectionChanged(NSTC2Impl *This, IShellItemArray *psia)
176 if(!This->events) return S_OK;
178 return INameSpaceTreeControlEvents_OnSelectionChanged(This->events, psia);
181 static HRESULT events_OnKeyboardInput(NSTC2Impl *This, UINT uMsg, WPARAM wParam, LPARAM lParam)
183 if(!This->events) return S_OK;
185 return INameSpaceTreeControlEvents_OnKeyboardInput(This->events, uMsg, wParam, lParam);
188 /*************************************************************************
189 * NamespaceTree helper functions
191 static DWORD treeview_style_from_nstcs(NSTC2Impl *This, NSTCSTYLE nstcs,
192 NSTCSTYLE nstcs_mask, DWORD *new_style)
194 DWORD old_style, tv_mask = 0;
195 TRACE("%p, %x, %x, %p\n", This, nstcs, nstcs_mask, new_style);
197 if(This->hwnd_tv)
198 old_style = GetWindowLongPtrW(This->hwnd_tv, GWL_STYLE);
199 else
200 old_style = /* The default */
201 WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN |
202 WS_TABSTOP | TVS_NOHSCROLL | TVS_NONEVENHEIGHT | TVS_INFOTIP |
203 TVS_EDITLABELS | TVS_TRACKSELECT;
205 if(nstcs_mask & NSTCS_HASEXPANDOS) tv_mask |= TVS_HASBUTTONS;
206 if(nstcs_mask & NSTCS_HASLINES) tv_mask |= TVS_HASLINES;
207 if(nstcs_mask & NSTCS_FULLROWSELECT) tv_mask |= TVS_FULLROWSELECT;
208 if(nstcs_mask & NSTCS_HORIZONTALSCROLL) tv_mask |= TVS_NOHSCROLL;
209 if(nstcs_mask & NSTCS_ROOTHASEXPANDO) tv_mask |= TVS_LINESATROOT;
210 if(nstcs_mask & NSTCS_SHOWSELECTIONALWAYS) tv_mask |= TVS_SHOWSELALWAYS;
211 if(nstcs_mask & NSTCS_NOINFOTIP) tv_mask |= TVS_INFOTIP;
212 if(nstcs_mask & NSTCS_EVENHEIGHT) tv_mask |= TVS_NONEVENHEIGHT;
213 if(nstcs_mask & NSTCS_DISABLEDRAGDROP) tv_mask |= TVS_DISABLEDRAGDROP;
214 if(nstcs_mask & NSTCS_NOEDITLABELS) tv_mask |= TVS_EDITLABELS;
215 if(nstcs_mask & NSTCS_CHECKBOXES) tv_mask |= TVS_CHECKBOXES;
216 if(nstcs_mask & NSTCS_SINGLECLICKEXPAND) tv_mask |= TVS_SINGLEEXPAND;
218 *new_style = 0;
220 if(nstcs & NSTCS_HASEXPANDOS) *new_style |= TVS_HASBUTTONS;
221 if(nstcs & NSTCS_HASLINES) *new_style |= TVS_HASLINES;
222 if(nstcs & NSTCS_FULLROWSELECT) *new_style |= TVS_FULLROWSELECT;
223 if(!(nstcs & NSTCS_HORIZONTALSCROLL)) *new_style |= TVS_NOHSCROLL;
224 if(nstcs & NSTCS_ROOTHASEXPANDO) *new_style |= TVS_LINESATROOT;
225 if(nstcs & NSTCS_SHOWSELECTIONALWAYS) *new_style |= TVS_SHOWSELALWAYS;
226 if(!(nstcs & NSTCS_NOINFOTIP)) *new_style |= TVS_INFOTIP;
227 if(!(nstcs & NSTCS_EVENHEIGHT)) *new_style |= TVS_NONEVENHEIGHT;
228 if(nstcs & NSTCS_DISABLEDRAGDROP) *new_style |= TVS_DISABLEDRAGDROP;
229 if(!(nstcs & NSTCS_NOEDITLABELS)) *new_style |= TVS_EDITLABELS;
230 if(nstcs & NSTCS_CHECKBOXES) *new_style |= TVS_CHECKBOXES;
231 if(nstcs & NSTCS_SINGLECLICKEXPAND) *new_style |= TVS_SINGLEEXPAND;
233 *new_style = (old_style & ~tv_mask) | (*new_style & tv_mask);
235 TRACE("old: %08x, new: %08x\n", old_style, *new_style);
237 return old_style^*new_style;
240 static IShellItem *shellitem_from_treeitem(NSTC2Impl *This, HTREEITEM hitem)
242 TVITEMEXW tvi;
244 tvi.mask = TVIF_PARAM;
245 tvi.lParam = 0;
246 tvi.hItem = hitem;
248 SendMessageW(This->hwnd_tv, TVM_GETITEMW, 0, (LPARAM)&tvi);
250 TRACE("ShellItem: %p\n", (void*)tvi.lParam);
251 return (IShellItem*)tvi.lParam;
254 /* Returns the root that the given treeitem belongs to. */
255 static nstc_root *root_for_treeitem(NSTC2Impl *This, HTREEITEM hitem)
257 HTREEITEM tmp, hroot = hitem;
258 nstc_root *root;
260 /* Work our way up the hierarchy */
261 for(tmp = hitem; tmp != NULL; hroot = tmp?tmp:hroot)
262 tmp = (HTREEITEM)SendMessageW(This->hwnd_tv, TVM_GETNEXTITEM, TVGN_PARENT, (LPARAM)hroot);
264 /* Search through the list of roots for a match */
265 LIST_FOR_EACH_ENTRY(root, &This->roots, nstc_root, entry)
266 if(root->htreeitem == hroot)
267 break;
269 TRACE("root is %p\n", root);
270 return root;
273 /* Find a shellitem in the tree, starting from the given node. */
274 static HTREEITEM search_for_shellitem(NSTC2Impl *This, HTREEITEM node,
275 IShellItem *psi)
277 IShellItem *psi_node;
278 HTREEITEM next, result = NULL;
279 HRESULT hr;
280 int cmpo;
281 TRACE("%p, %p, %p\n", This, node, psi);
283 /* Check this node */
284 psi_node = shellitem_from_treeitem(This, node);
285 hr = IShellItem_Compare(psi, psi_node, SICHINT_DISPLAY, &cmpo);
286 if(hr == S_OK)
287 return node;
289 /* Any children? */
290 next = (HTREEITEM)SendMessageW(This->hwnd_tv, TVM_GETNEXTITEM,
291 TVGN_CHILD, (LPARAM)node);
292 if(next)
294 result = search_for_shellitem(This, next, psi);
295 if(result) return result;
298 /* Try our next sibling. */
299 next = (HTREEITEM)SendMessageW(This->hwnd_tv, TVM_GETNEXTITEM,
300 TVGN_NEXT, (LPARAM)node);
301 if(next)
302 result = search_for_shellitem(This, next, psi);
304 return result;
307 static HTREEITEM treeitem_from_shellitem(NSTC2Impl *This, IShellItem *psi)
309 HTREEITEM root;
310 TRACE("%p, %p\n", This, psi);
312 root = (HTREEITEM)SendMessageW(This->hwnd_tv, TVM_GETNEXTITEM,
313 TVGN_ROOT, 0);
314 if(!root)
315 return NULL;
317 return search_for_shellitem(This, root, psi);
320 static int get_icon(LPCITEMIDLIST lpi, UINT extra_flags)
322 SHFILEINFOW sfi;
323 UINT flags = SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_SMALLICON;
324 IImageList *list;
326 list = (IImageList *)SHGetFileInfoW((LPCWSTR)lpi, 0 ,&sfi, sizeof(SHFILEINFOW), flags | extra_flags);
327 if (list) IImageList_Release(list);
328 return sfi.iIcon;
331 /* Insert a shellitem into the given place in the tree and return the
332 resulting treeitem. */
333 static HTREEITEM insert_shellitem(NSTC2Impl *This, IShellItem *psi,
334 HTREEITEM hParent, HTREEITEM hInsertAfter)
336 TVINSERTSTRUCTW tvins;
337 TVITEMEXW *tvi = &tvins.u.itemex;
338 HTREEITEM hinserted;
339 TRACE("%p (%p, %p)\n", psi, hParent, hInsertAfter);
341 tvi->mask = TVIF_PARAM | TVIF_CHILDREN | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT;
342 tvi->cChildren = I_CHILDRENCALLBACK;
343 tvi->iImage = tvi->iSelectedImage = I_IMAGECALLBACK;
344 tvi->pszText = LPSTR_TEXTCALLBACKW;
346 /* Every treeitem contains a pointer to the corresponding ShellItem. */
347 tvi->lParam = (LPARAM)psi;
348 tvins.hParent = hParent;
349 tvins.hInsertAfter = hInsertAfter;
351 hinserted = (HTREEITEM)SendMessageW(This->hwnd_tv, TVM_INSERTITEMW, 0,
352 (LPARAM)(LPTVINSERTSTRUCTW)&tvins);
353 if(hinserted)
354 IShellItem_AddRef(psi);
356 return hinserted;
359 /* Enumerates the children of the folder represented by hitem
360 * according to the settings for the root, and adds them to the
361 * treeview. Returns the number of children added. */
362 static UINT fill_sublevel(NSTC2Impl *This, HTREEITEM hitem)
364 IShellItem *psiParent = shellitem_from_treeitem(This, hitem);
365 nstc_root *root = root_for_treeitem(This, hitem);
366 LPITEMIDLIST pidl_parent;
367 IShellFolder *psf;
368 IEnumIDList *peidl;
369 UINT added = 0;
370 HRESULT hr;
372 hr = SHGetIDListFromObject((IUnknown*)psiParent, &pidl_parent);
373 if(SUCCEEDED(hr))
375 hr = IShellItem_BindToHandler(psiParent, NULL, &BHID_SFObject, &IID_IShellFolder, (void**)&psf);
376 if(SUCCEEDED(hr))
378 hr = IShellFolder_EnumObjects(psf, NULL, root->enum_flags, &peidl);
379 if(SUCCEEDED(hr))
381 LPITEMIDLIST pidl;
382 IShellItem *psi;
383 ULONG fetched;
385 while( S_OK == IEnumIDList_Next(peidl, 1, &pidl, &fetched) )
387 hr = SHCreateShellItem(NULL, psf , pidl, &psi);
388 ILFree(pidl);
389 if(SUCCEEDED(hr))
391 if(insert_shellitem(This, psi, hitem, NULL))
393 events_OnItemAdded(This, psi, FALSE);
394 added++;
397 IShellItem_Release(psi);
399 else
400 ERR("SHCreateShellItem failed with 0x%08x\n", hr);
402 IEnumIDList_Release(peidl);
404 else
405 ERR("EnumObjects failed with 0x%08x\n", hr);
407 IShellFolder_Release(psf);
409 else
410 ERR("BindToHandler failed with 0x%08x\n", hr);
412 ILFree(pidl_parent);
414 else
415 ERR("SHGetIDListFromObject failed with 0x%08x\n", hr);
417 return added;
420 static HTREEITEM get_selected_treeitem(NSTC2Impl *This)
422 return (HTREEITEM)SendMessageW(This->hwnd_tv, TVM_GETNEXTITEM, TVGN_CARET, 0);
425 static IShellItem *get_selected_shellitem(NSTC2Impl *This)
427 return shellitem_from_treeitem(This, get_selected_treeitem(This));
430 static void collapse_all(NSTC2Impl *This, HTREEITEM node)
432 HTREEITEM next;
434 /* Collapse this node first, and then first child/next sibling. */
435 SendMessageW(This->hwnd_tv, TVM_EXPAND, TVE_COLLAPSE, (LPARAM)node);
437 next = (HTREEITEM)SendMessageW(This->hwnd_tv, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)node);
438 if(next) collapse_all(This, next);
440 next = (HTREEITEM)SendMessageW(This->hwnd_tv, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)node);
441 if(next) collapse_all(This, next);
444 static HTREEITEM treeitem_from_point(NSTC2Impl *This, const POINT *pt, UINT *hitflag)
446 TVHITTESTINFO tviht;
447 tviht.pt.x = pt->x;
448 tviht.pt.y = pt->y;
449 tviht.hItem = NULL;
451 SendMessageW(This->hwnd_tv, TVM_HITTEST, 0, (LPARAM)&tviht);
452 if(hitflag) *hitflag = tviht.flags;
453 return tviht.hItem;
456 /*************************************************************************
457 * NamespaceTree window functions
459 static LRESULT create_namespacetree(HWND hWnd, CREATESTRUCTW *crs)
461 NSTC2Impl *This = crs->lpCreateParams;
462 HIMAGELIST ShellSmallIconList;
463 DWORD treeview_style, treeview_ex_style;
465 TRACE("%p (%p)\n", This, crs);
466 SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LPARAM)This);
467 This->hwnd_main = hWnd;
469 treeview_style_from_nstcs(This, This->style, 0xFFFFFFFF, &treeview_style);
471 This->hwnd_tv = CreateWindowExW(0, WC_TREEVIEWW, NULL, treeview_style,
472 0, 0, crs->cx, crs->cy,
473 hWnd, NULL, explorerframe_hinstance, NULL);
475 if(!This->hwnd_tv)
477 ERR("Failed to create treeview!\n");
478 return HRESULT_FROM_WIN32(GetLastError());
481 treeview_ex_style = TVS_EX_DRAWIMAGEASYNC | TVS_EX_RICHTOOLTIP |
482 TVS_EX_DOUBLEBUFFER | TVS_EX_NOSINGLECOLLAPSE;
484 if(This->style & NSTCS_AUTOHSCROLL)
485 treeview_ex_style |= TVS_EX_AUTOHSCROLL;
486 if(This->style & NSTCS_FADEINOUTEXPANDOS)
487 treeview_ex_style |= TVS_EX_FADEINOUTEXPANDOS;
488 if(This->style & NSTCS_PARTIALCHECKBOXES)
489 treeview_ex_style |= TVS_EX_PARTIALCHECKBOXES;
490 if(This->style & NSTCS_EXCLUSIONCHECKBOXES)
491 treeview_ex_style |= TVS_EX_EXCLUSIONCHECKBOXES;
492 if(This->style & NSTCS_DIMMEDCHECKBOXES)
493 treeview_ex_style |= TVS_EX_DIMMEDCHECKBOXES;
495 SendMessageW(This->hwnd_tv, TVM_SETEXTENDEDSTYLE, treeview_ex_style, 0xffff);
497 if(Shell_GetImageLists(NULL, &ShellSmallIconList))
499 SendMessageW(This->hwnd_tv, TVM_SETIMAGELIST,
500 (WPARAM)TVSIL_NORMAL, (LPARAM)ShellSmallIconList);
502 else
504 ERR("Failed to get the System Image List.\n");
507 INameSpaceTreeControl2_AddRef(&This->INameSpaceTreeControl2_iface);
509 /* Subclass the treeview to get the keyboard events. */
510 This->tv_oldwndproc = (WNDPROC)SetWindowLongPtrW(This->hwnd_tv, GWLP_WNDPROC,
511 (ULONG_PTR)tv_wndproc);
512 if(This->tv_oldwndproc)
513 SetPropW(This->hwnd_tv, L"PROP_THIS", This);
515 return TRUE;
518 static LRESULT resize_namespacetree(NSTC2Impl *This)
520 RECT rc;
521 TRACE("%p\n", This);
523 GetClientRect(This->hwnd_main, &rc);
524 MoveWindow(This->hwnd_tv, 0, 0, rc.right-rc.left, rc.bottom-rc.top, TRUE);
526 return TRUE;
529 static LRESULT destroy_namespacetree(NSTC2Impl *This)
531 TRACE("%p\n", This);
533 /* Undo the subclassing */
534 if(This->tv_oldwndproc)
536 SetWindowLongPtrW(This->hwnd_tv, GWLP_WNDPROC, (ULONG_PTR)This->tv_oldwndproc);
537 RemovePropW(This->hwnd_tv, L"PROP_THIS");
540 INameSpaceTreeControl2_RemoveAllRoots(&This->INameSpaceTreeControl2_iface);
542 /* This reference was added in create_namespacetree */
543 INameSpaceTreeControl2_Release(&This->INameSpaceTreeControl2_iface);
544 return TRUE;
547 static LRESULT on_tvn_deleteitemw(NSTC2Impl *This, LPARAM lParam)
549 NMTREEVIEWW *nmtv = (NMTREEVIEWW*)lParam;
550 TRACE("%p\n", This);
552 IShellItem_Release((IShellItem*)nmtv->itemOld.lParam);
553 return TRUE;
556 static LRESULT on_tvn_getdispinfow(NSTC2Impl *This, LPARAM lParam)
558 NMTVDISPINFOW *dispinfo = (NMTVDISPINFOW*)lParam;
559 TVITEMEXW *item = (TVITEMEXW*)&dispinfo->item;
560 IShellItem *psi = shellitem_from_treeitem(This, item->hItem);
561 HRESULT hr;
563 TRACE("%p, %p (mask: %x)\n", This, dispinfo, item->mask);
565 if(item->mask & TVIF_CHILDREN)
567 SFGAOF sfgao;
569 hr = IShellItem_GetAttributes(psi, SFGAO_HASSUBFOLDER, &sfgao);
570 if(SUCCEEDED(hr))
571 item->cChildren = (sfgao & SFGAO_HASSUBFOLDER)?1:0;
572 else
573 item->cChildren = 1;
575 item->mask |= TVIF_DI_SETITEM;
578 if(item->mask & (TVIF_IMAGE|TVIF_SELECTEDIMAGE))
580 LPITEMIDLIST pidl;
582 hr = events_OnGetDefaultIconIndex(This, psi, &item->iImage, &item->iSelectedImage);
583 if(FAILED(hr))
585 hr = SHGetIDListFromObject((IUnknown*)psi, &pidl);
586 if(SUCCEEDED(hr))
588 item->iImage = item->iSelectedImage = get_icon(pidl, 0);
589 item->mask |= TVIF_DI_SETITEM;
590 ILFree(pidl);
592 else
593 ERR("Failed to get IDList (%08x).\n", hr);
597 if(item->mask & TVIF_TEXT)
599 LPWSTR display_name;
601 hr = IShellItem_GetDisplayName(psi, SIGDN_NORMALDISPLAY, &display_name);
602 if(SUCCEEDED(hr))
604 lstrcpynW(item->pszText, display_name, MAX_PATH);
605 item->mask |= TVIF_DI_SETITEM;
606 CoTaskMemFree(display_name);
608 else
609 ERR("Failed to get display name (%08x).\n", hr);
612 return TRUE;
615 static BOOL treenode_has_subfolders(NSTC2Impl *This, HTREEITEM node)
617 return SendMessageW(This->hwnd_tv, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)node);
620 static LRESULT on_tvn_itemexpandingw(NSTC2Impl *This, LPARAM lParam)
622 NMTREEVIEWW *nmtv = (NMTREEVIEWW*)lParam;
623 IShellItem *psi;
624 TRACE("%p\n", This);
626 psi = shellitem_from_treeitem(This, nmtv->itemNew.hItem);
627 events_OnBeforeExpand(This, psi);
629 if(!treenode_has_subfolders(This, nmtv->itemNew.hItem))
631 /* The node has no children, try to find some */
632 if(!fill_sublevel(This, nmtv->itemNew.hItem))
634 TVITEMEXW tvi;
635 /* Failed to enumerate any children, remove the expando
636 * (if any). */
637 tvi.hItem = nmtv->itemNew.hItem;
638 tvi.mask = TVIF_CHILDREN;
639 tvi.cChildren = 0;
640 SendMessageW(This->hwnd_tv, TVM_SETITEMW, 0, (LPARAM)&tvi);
642 return TRUE;
645 return FALSE;
648 static LRESULT on_tvn_itemexpandedw(NSTC2Impl *This, LPARAM lParam)
650 NMTREEVIEWW *nmtv = (NMTREEVIEWW*)lParam;
651 IShellItem *psi;
652 TRACE("%p\n", This);
654 psi = shellitem_from_treeitem(This, nmtv->itemNew.hItem);
655 events_OnAfterExpand(This, psi);
656 return TRUE;
659 static LRESULT on_tvn_selchangedw(NSTC2Impl *This, LPARAM lParam)
661 NMTREEVIEWW *nmtv = (NMTREEVIEWW*)lParam;
662 IShellItemArray *psia;
663 IShellItem *psi;
664 HRESULT hr;
665 TRACE("%p\n", This);
667 /* Note: Only supports one selected item. */
668 psi = shellitem_from_treeitem(This, nmtv->itemNew.hItem);
669 hr = SHCreateShellItemArrayFromShellItem(psi, &IID_IShellItemArray, (void**)&psia);
670 if(SUCCEEDED(hr))
672 events_OnSelectionChanged(This, psia);
673 IShellItemArray_Release(psia);
676 return TRUE;
679 static LRESULT on_nm_click(NSTC2Impl *This, NMHDR *nmhdr)
681 TVHITTESTINFO tvhit;
682 IShellItem *psi;
684 TRACE("%p (%p)\n", This, nmhdr);
686 GetCursorPos(&tvhit.pt);
687 ScreenToClient(This->hwnd_tv, &tvhit.pt);
688 SendMessageW(This->hwnd_tv, TVM_HITTEST, 0, (LPARAM)&tvhit);
690 if(tvhit.flags & (TVHT_NOWHERE|TVHT_ABOVE|TVHT_BELOW))
691 return TRUE;
693 /* TVHT_ maps onto the corresponding NSTCEHT_ */
694 psi = shellitem_from_treeitem(This, tvhit.hItem);
695 return FAILED(events_OnItemClick(This, psi, tvhit.flags, NSTCECT_LBUTTON));
698 static LRESULT on_wm_mbuttonup(NSTC2Impl *This, WPARAM wParam, LPARAM lParam)
700 TVHITTESTINFO tvhit;
701 IShellItem *psi;
702 HRESULT hr;
703 TRACE("%p (%lx, %lx)\n", This, wParam, lParam);
705 tvhit.pt.x = (int)(short)LOWORD(lParam);
706 tvhit.pt.y = (int)(short)HIWORD(lParam);
708 SendMessageW(This->hwnd_tv, TVM_HITTEST, 0, (LPARAM)&tvhit);
710 /* Seems to generate only ONITEM and ONITEMICON */
711 if( !(tvhit.flags & (TVHT_ONITEM|TVHT_ONITEMICON)) )
712 return FALSE;
714 psi = shellitem_from_treeitem(This, tvhit.hItem);
715 hr = events_OnItemClick(This, psi, tvhit.flags, NSTCECT_MBUTTON);
717 if(SUCCEEDED(hr))
718 return FALSE;
719 else
720 return TRUE;
723 static LRESULT on_kbd_event(NSTC2Impl *This, UINT uMsg, WPARAM wParam, LPARAM lParam)
725 IShellItem *psi;
726 HTREEITEM hitem;
727 TRACE("%p : %d, %lx, %lx\n", This, uMsg, wParam, lParam);
729 /* Handled by the client? */
730 if(FAILED(events_OnKeyboardInput(This, uMsg, wParam, lParam)))
731 return TRUE;
733 if(uMsg == WM_KEYDOWN)
735 switch(wParam)
737 case VK_DELETE:
738 psi = get_selected_shellitem(This);
739 FIXME("Deletion of file requested (shellitem: %p).\n", psi);
740 return TRUE;
742 case VK_F2:
743 hitem = get_selected_treeitem(This);
744 SendMessageW(This->hwnd_tv, TVM_EDITLABELW, 0, (LPARAM)hitem);
745 return TRUE;
749 /* Let the TreeView handle the key */
750 return FALSE;
753 static LRESULT CALLBACK tv_wndproc(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam)
755 NSTC2Impl *This = (NSTC2Impl*)GetPropW(hWnd, L"PROP_THIS");
757 switch(uMessage) {
758 case WM_KEYDOWN:
759 case WM_KEYUP:
760 case WM_CHAR:
761 case WM_SYSKEYDOWN:
762 case WM_SYSKEYUP:
763 case WM_SYSCHAR:
764 if(on_kbd_event(This, uMessage, wParam, lParam))
765 return TRUE;
766 break;
768 case WM_MBUTTONUP: return on_wm_mbuttonup(This, wParam, lParam);
771 /* Pass the message on to the treeview */
772 return CallWindowProcW(This->tv_oldwndproc, hWnd, uMessage, wParam, lParam);
775 static LRESULT CALLBACK NSTC2_WndProc(HWND hWnd, UINT uMessage,
776 WPARAM wParam, LPARAM lParam)
778 NSTC2Impl *This = (NSTC2Impl*)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
779 NMHDR *nmhdr;
781 switch(uMessage)
783 case WM_NCCREATE: return create_namespacetree(hWnd, (CREATESTRUCTW*)lParam);
784 case WM_SIZE: return resize_namespacetree(This);
785 case WM_DESTROY: return destroy_namespacetree(This);
786 case WM_NOTIFY:
787 nmhdr = (NMHDR*)lParam;
788 switch(nmhdr->code)
790 case NM_CLICK: return on_nm_click(This, nmhdr);
791 case TVN_DELETEITEMW: return on_tvn_deleteitemw(This, lParam);
792 case TVN_GETDISPINFOW: return on_tvn_getdispinfow(This, lParam);
793 case TVN_ITEMEXPANDINGW: return on_tvn_itemexpandingw(This, lParam);
794 case TVN_ITEMEXPANDEDW: return on_tvn_itemexpandedw(This, lParam);
795 case TVN_SELCHANGEDW: return on_tvn_selchangedw(This, lParam);
796 default: break;
798 break;
799 default: return DefWindowProcW(hWnd, uMessage, wParam, lParam);
801 return 0;
804 /**************************************************************************
805 * INameSpaceTreeControl2 Implementation
807 static HRESULT WINAPI NSTC2_fnQueryInterface(INameSpaceTreeControl2* iface,
808 REFIID riid,
809 void **ppvObject)
811 NSTC2Impl *This = impl_from_INameSpaceTreeControl2(iface);
812 TRACE("%p (%s, %p)\n", This, debugstr_guid(riid), ppvObject);
814 *ppvObject = NULL;
815 if(IsEqualIID(riid, &IID_INameSpaceTreeControl2) ||
816 IsEqualIID(riid, &IID_INameSpaceTreeControl) ||
817 IsEqualIID(riid, &IID_IUnknown))
819 *ppvObject = &This->INameSpaceTreeControl2_iface;
821 else if(IsEqualIID(riid, &IID_IOleWindow))
823 *ppvObject = &This->IOleWindow_iface;
826 if(*ppvObject)
828 IUnknown_AddRef((IUnknown*)*ppvObject);
829 return S_OK;
832 return E_NOINTERFACE;
835 static ULONG WINAPI NSTC2_fnAddRef(INameSpaceTreeControl2* iface)
837 NSTC2Impl *This = impl_from_INameSpaceTreeControl2(iface);
838 LONG ref = InterlockedIncrement(&This->ref);
840 TRACE("%p - ref %d\n", This, ref);
842 return ref;
845 static ULONG WINAPI NSTC2_fnRelease(INameSpaceTreeControl2* iface)
847 NSTC2Impl *This = impl_from_INameSpaceTreeControl2(iface);
848 LONG ref = InterlockedDecrement(&This->ref);
850 TRACE("%p - ref: %d\n", This, ref);
852 if(!ref)
854 TRACE("Freeing.\n");
855 heap_free(This);
856 EFRAME_UnlockModule();
859 return ref;
862 static HRESULT WINAPI NSTC2_fnInitialize(INameSpaceTreeControl2* iface,
863 HWND hwndParent,
864 RECT *prc,
865 NSTCSTYLE nstcsFlags)
867 NSTC2Impl *This = impl_from_INameSpaceTreeControl2(iface);
868 WNDCLASSW wc;
869 DWORD window_style, window_ex_style;
870 INITCOMMONCONTROLSEX icex;
871 RECT rc;
872 static const WCHAR NSTC2_CLASS_NAME[] = L"NamespaceTreeControl";
874 TRACE("%p (%p, %p, %x)\n", This, hwndParent, prc, nstcsFlags);
876 if(nstcsFlags & unsupported_styles)
877 FIXME("0x%08x contains the unsupported style(s) 0x%08x\n",
878 nstcsFlags, nstcsFlags & unsupported_styles);
880 This->style = nstcsFlags;
882 icex.dwSize = sizeof( icex );
883 icex.dwICC = ICC_TREEVIEW_CLASSES;
884 InitCommonControlsEx( &icex );
886 if(!GetClassInfoW(explorerframe_hinstance, NSTC2_CLASS_NAME, &wc))
888 wc.style = CS_HREDRAW | CS_VREDRAW;
889 wc.lpfnWndProc = NSTC2_WndProc;
890 wc.cbClsExtra = 0;
891 wc.cbWndExtra = 0;
892 wc.hInstance = explorerframe_hinstance;
893 wc.hIcon = 0;
894 wc.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
895 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
896 wc.lpszMenuName = NULL;
897 wc.lpszClassName = NSTC2_CLASS_NAME;
899 if (!RegisterClassW(&wc)) return E_FAIL;
902 /* NSTCS_TABSTOP and NSTCS_BORDER affects the host window */
903 window_style = WS_VISIBLE | WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS |
904 (nstcsFlags & NSTCS_BORDER ? WS_BORDER : 0);
905 window_ex_style = nstcsFlags & NSTCS_TABSTOP ? WS_EX_CONTROLPARENT : 0;
907 if(prc)
908 rc = *prc;
909 else
910 SetRectEmpty(&rc);
912 This->hwnd_main = CreateWindowExW(window_ex_style, NSTC2_CLASS_NAME, NULL, window_style,
913 rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
914 hwndParent, 0, explorerframe_hinstance, This);
916 if(!This->hwnd_main)
918 ERR("Failed to create the window.\n");
919 return HRESULT_FROM_WIN32(GetLastError());
922 return S_OK;
925 static HRESULT WINAPI NSTC2_fnTreeAdvise(INameSpaceTreeControl2* iface, IUnknown *handler, DWORD *cookie)
927 NSTC2Impl *This = impl_from_INameSpaceTreeControl2(iface);
929 TRACE("%p (%p, %p)\n", This, handler, cookie);
931 *cookie = 0;
933 /* Only one client supported */
934 if (This->events || This->customdraw || This->dragdrop)
935 return E_FAIL;
937 /* FIXME: request INameSpaceTreeAccessible too */
938 IUnknown_QueryInterface(handler, &IID_INameSpaceTreeControlEvents, (void**)&This->events);
939 IUnknown_QueryInterface(handler, &IID_INameSpaceTreeControlCustomDraw, (void**)&This->customdraw);
940 IUnknown_QueryInterface(handler, &IID_INameSpaceTreeControlDropHandler, (void**)&This->dragdrop);
942 if (This->events || This->customdraw || This->dragdrop)
943 *cookie = 1;
945 return *cookie ? S_OK : E_FAIL;
948 static HRESULT WINAPI NSTC2_fnTreeUnadvise(INameSpaceTreeControl2* iface, DWORD cookie)
950 NSTC2Impl *This = impl_from_INameSpaceTreeControl2(iface);
952 TRACE("%p (%x)\n", This, cookie);
954 /* The cookie is ignored. */
956 if (This->events)
958 INameSpaceTreeControlEvents_Release(This->events);
959 This->events = NULL;
962 if (This->customdraw)
964 INameSpaceTreeControlCustomDraw_Release(This->customdraw);
965 This->customdraw = NULL;
968 if (This->dragdrop)
970 INameSpaceTreeControlDropHandler_Release(This->dragdrop);
971 This->dragdrop = NULL;
974 return S_OK;
977 static HRESULT WINAPI NSTC2_fnInsertRoot(INameSpaceTreeControl2* iface,
978 int iIndex,
979 IShellItem *psiRoot,
980 SHCONTF grfEnumFlags,
981 NSTCROOTSTYLE grfRootStyle,
982 IShellItemFilter *pif)
984 NSTC2Impl *This = impl_from_INameSpaceTreeControl2(iface);
985 nstc_root *new_root;
986 struct list *add_after_entry;
987 HTREEITEM add_after_hitem;
988 int i;
990 TRACE("%p, %d, %p, %x, %x, %p\n", This, iIndex, psiRoot, grfEnumFlags, grfRootStyle, pif);
992 new_root = heap_alloc(sizeof(*new_root));
993 if(!new_root)
994 return E_OUTOFMEMORY;
996 new_root->psi = psiRoot;
997 new_root->enum_flags = grfEnumFlags;
998 new_root->root_style = grfRootStyle;
999 new_root->pif = pif;
1001 /* We want to keep the roots in the internal list and in the
1002 * treeview in the same order. */
1003 add_after_entry = &This->roots;
1004 for(i = 0; i < max(0, iIndex) && list_next(&This->roots, add_after_entry); i++)
1005 add_after_entry = list_next(&This->roots, add_after_entry);
1007 if(add_after_entry == &This->roots)
1008 add_after_hitem = TVI_FIRST;
1009 else
1010 add_after_hitem = LIST_ENTRY(add_after_entry, nstc_root, entry)->htreeitem;
1012 new_root->htreeitem = insert_shellitem(This, psiRoot, TVI_ROOT, add_after_hitem);
1013 if(!new_root->htreeitem)
1015 WARN("Failed to add the root.\n");
1016 heap_free(new_root);
1017 return E_FAIL;
1020 list_add_after(add_after_entry, &new_root->entry);
1021 events_OnItemAdded(This, psiRoot, TRUE);
1023 if(grfRootStyle & NSTCRS_HIDDEN)
1025 TVITEMEXW tvi;
1026 tvi.mask = TVIF_STATEEX;
1027 tvi.uStateEx = TVIS_EX_FLAT;
1028 tvi.hItem = new_root->htreeitem;
1030 SendMessageW(This->hwnd_tv, TVM_SETITEMW, 0, (LPARAM)&tvi);
1033 if(grfRootStyle & NSTCRS_EXPANDED)
1034 SendMessageW(This->hwnd_tv, TVM_EXPAND, TVE_EXPAND,
1035 (LPARAM)new_root->htreeitem);
1037 return S_OK;
1040 static HRESULT WINAPI NSTC2_fnAppendRoot(INameSpaceTreeControl2* iface,
1041 IShellItem *psiRoot,
1042 SHCONTF grfEnumFlags,
1043 NSTCROOTSTYLE grfRootStyle,
1044 IShellItemFilter *pif)
1046 NSTC2Impl *This = impl_from_INameSpaceTreeControl2(iface);
1047 UINT root_count;
1048 TRACE("%p, %p, %x, %x, %p\n",
1049 This, psiRoot, grfEnumFlags, grfRootStyle, pif);
1051 root_count = list_count(&This->roots);
1053 return INameSpaceTreeControl2_InsertRoot(iface, root_count, psiRoot, grfEnumFlags, grfRootStyle, pif);
1056 static HRESULT WINAPI NSTC2_fnRemoveRoot(INameSpaceTreeControl2* iface,
1057 IShellItem *psiRoot)
1059 NSTC2Impl *This = impl_from_INameSpaceTreeControl2(iface);
1060 nstc_root *cursor, *root = NULL;
1061 TRACE("%p (%p)\n", This, psiRoot);
1063 if(!psiRoot)
1064 return E_NOINTERFACE;
1066 LIST_FOR_EACH_ENTRY(cursor, &This->roots, nstc_root, entry)
1068 HRESULT hr;
1069 int order;
1070 hr = IShellItem_Compare(psiRoot, cursor->psi, SICHINT_DISPLAY, &order);
1071 if(hr == S_OK)
1073 root = cursor;
1074 break;
1078 TRACE("root %p\n", root);
1079 if(root)
1081 events_OnItemDeleted(This, root->psi, TRUE);
1082 SendMessageW(This->hwnd_tv, TVM_DELETEITEM, 0, (LPARAM)root->htreeitem);
1083 list_remove(&root->entry);
1084 heap_free(root);
1085 return S_OK;
1087 else
1089 WARN("No matching root found.\n");
1090 return E_FAIL;
1094 static HRESULT WINAPI NSTC2_fnRemoveAllRoots(INameSpaceTreeControl2* iface)
1096 NSTC2Impl *This = impl_from_INameSpaceTreeControl2(iface);
1097 nstc_root *cur1, *cur2;
1099 TRACE("%p\n", This);
1101 if (list_empty(&This->roots))
1102 return E_INVALIDARG;
1104 LIST_FOR_EACH_ENTRY_SAFE(cur1, cur2, &This->roots, nstc_root, entry)
1105 INameSpaceTreeControl2_RemoveRoot(iface, cur1->psi);
1107 return S_OK;
1110 static HRESULT WINAPI NSTC2_fnGetRootItems(INameSpaceTreeControl2* iface,
1111 IShellItemArray **ppsiaRootItems)
1113 NSTC2Impl *This = impl_from_INameSpaceTreeControl2(iface);
1114 IShellFolder *psf;
1115 LPITEMIDLIST *array;
1116 nstc_root *root;
1117 UINT count, i;
1118 HRESULT hr;
1119 TRACE("%p (%p)\n", This, ppsiaRootItems);
1121 count = list_count(&This->roots);
1123 if(!count)
1124 return E_INVALIDARG;
1126 array = heap_alloc(sizeof(LPITEMIDLIST)*count);
1128 i = 0;
1129 LIST_FOR_EACH_ENTRY(root, &This->roots, nstc_root, entry)
1130 SHGetIDListFromObject((IUnknown*)root->psi, &array[i++]);
1132 SHGetDesktopFolder(&psf);
1133 hr = SHCreateShellItemArray(NULL, psf, count, (PCUITEMID_CHILD_ARRAY)array,
1134 ppsiaRootItems);
1135 IShellFolder_Release(psf);
1137 for(i = 0; i < count; i++)
1138 ILFree(array[i]);
1140 heap_free(array);
1142 return hr;
1145 static HRESULT WINAPI NSTC2_fnSetItemState(INameSpaceTreeControl2* iface,
1146 IShellItem *psi,
1147 NSTCITEMSTATE nstcisMask,
1148 NSTCITEMSTATE nstcisFlags)
1150 NSTC2Impl *This = impl_from_INameSpaceTreeControl2(iface);
1151 TVITEMEXW tvi;
1152 HTREEITEM hitem;
1154 TRACE("%p (%p, %x, %x)\n", This, psi, nstcisMask, nstcisFlags);
1156 hitem = treeitem_from_shellitem(This, psi);
1157 if(!hitem) return E_INVALIDARG;
1159 /* Passing both NSTCIS_SELECTED and NSTCIS_SELECTEDNOEXPAND results
1160 in two TVM_SETITEMW's */
1161 if((nstcisMask&nstcisFlags) & NSTCIS_SELECTED)
1163 SendMessageW(This->hwnd_tv, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hitem);
1164 SendMessageW(This->hwnd_tv, TVM_ENSUREVISIBLE, 0, (LPARAM)hitem);
1166 if((nstcisMask&nstcisFlags) & NSTCIS_SELECTEDNOEXPAND)
1168 SendMessageW(This->hwnd_tv, TVM_SELECTITEM, TVGN_CARET|TVSI_NOSINGLEEXPAND, (LPARAM)hitem);
1171 /* If NSTCIS_EXPANDED is among the flags, the mask is ignored. */
1172 if((nstcisMask|nstcisFlags) & NSTCIS_EXPANDED)
1174 WPARAM arg = nstcisFlags&NSTCIS_EXPANDED ? TVE_EXPAND:TVE_COLLAPSE;
1175 SendMessageW(This->hwnd_tv, TVM_EXPAND, arg, (LPARAM)hitem);
1178 if(nstcisMask & NSTCIS_DISABLED)
1179 tvi.mask = TVIF_STATE | TVIF_STATEEX;
1180 else if( ((nstcisMask^nstcisFlags) & (NSTCIS_SELECTED|NSTCIS_EXPANDED|NSTCIS_SELECTEDNOEXPAND)) ||
1181 ((nstcisMask|nstcisFlags) & NSTCIS_BOLD) ||
1182 (nstcisFlags & NSTCIS_DISABLED) )
1183 tvi.mask = TVIF_STATE;
1184 else
1185 tvi.mask = 0;
1187 if(tvi.mask)
1189 tvi.stateMask = tvi.state = 0;
1190 tvi.stateMask |= ((nstcisFlags^nstcisMask)&NSTCIS_SELECTED) ? TVIS_SELECTED : 0;
1191 tvi.stateMask |= (nstcisMask|nstcisFlags)&NSTCIS_BOLD ? TVIS_BOLD:0;
1192 tvi.state |= (nstcisMask&nstcisFlags)&NSTCIS_BOLD ? TVIS_BOLD:0;
1194 if((nstcisMask&NSTCIS_EXPANDED)^(nstcisFlags&NSTCIS_EXPANDED))
1196 tvi.stateMask = 0;
1199 tvi.uStateEx = (nstcisFlags&nstcisMask)&NSTCIS_DISABLED?TVIS_EX_DISABLED:0;
1200 tvi.hItem = hitem;
1202 SendMessageW(This->hwnd_tv, TVM_SETITEMW, 0, (LPARAM)&tvi);
1205 return S_OK;
1208 static HRESULT WINAPI NSTC2_fnGetItemState(INameSpaceTreeControl2* iface,
1209 IShellItem *psi,
1210 NSTCITEMSTATE nstcisMask,
1211 NSTCITEMSTATE *pnstcisFlags)
1213 NSTC2Impl *This = impl_from_INameSpaceTreeControl2(iface);
1214 HTREEITEM hitem;
1215 TVITEMEXW tvi;
1216 TRACE("%p (%p, %x, %p)\n", This, psi, nstcisMask, pnstcisFlags);
1218 hitem = treeitem_from_shellitem(This, psi);
1219 if(!hitem)
1220 return E_INVALIDARG;
1222 *pnstcisFlags = 0;
1224 tvi.hItem = hitem;
1225 tvi.mask = TVIF_STATE;
1226 tvi.stateMask = TVIS_SELECTED|TVIS_EXPANDED|TVIS_BOLD;
1228 if(nstcisMask & NSTCIS_DISABLED)
1229 tvi.mask |= TVIF_STATEEX;
1231 SendMessageW(This->hwnd_tv, TVM_GETITEMW, 0, (LPARAM)&tvi);
1232 *pnstcisFlags |= (tvi.state & TVIS_SELECTED)?NSTCIS_SELECTED:0;
1233 *pnstcisFlags |= (tvi.state & TVIS_EXPANDED)?NSTCIS_EXPANDED:0;
1234 *pnstcisFlags |= (tvi.state & TVIS_BOLD)?NSTCIS_BOLD:0;
1235 *pnstcisFlags |= (tvi.uStateEx & TVIS_EX_DISABLED)?NSTCIS_DISABLED:0;
1237 *pnstcisFlags &= nstcisMask;
1239 return S_OK;
1242 static HRESULT WINAPI NSTC2_fnGetSelectedItems(INameSpaceTreeControl2* iface,
1243 IShellItemArray **psiaItems)
1245 NSTC2Impl *This = impl_from_INameSpaceTreeControl2(iface);
1246 IShellItem *psiselected;
1248 TRACE("%p (%p)\n", This, psiaItems);
1250 psiselected = get_selected_shellitem(This);
1251 if(!psiselected)
1253 *psiaItems = NULL;
1254 return E_FAIL;
1257 return SHCreateShellItemArrayFromShellItem(psiselected, &IID_IShellItemArray,
1258 (void**)psiaItems);
1261 static HRESULT WINAPI NSTC2_fnGetItemCustomState(INameSpaceTreeControl2* iface,
1262 IShellItem *psi,
1263 int *piStateNumber)
1265 NSTC2Impl *This = impl_from_INameSpaceTreeControl2(iface);
1266 FIXME("stub, %p (%p, %p)\n", This, psi, piStateNumber);
1267 return E_NOTIMPL;
1270 static HRESULT WINAPI NSTC2_fnSetItemCustomState(INameSpaceTreeControl2* iface,
1271 IShellItem *psi,
1272 int iStateNumber)
1274 NSTC2Impl *This = impl_from_INameSpaceTreeControl2(iface);
1275 FIXME("stub, %p (%p, %d)\n", This, psi, iStateNumber);
1276 return E_NOTIMPL;
1279 static HRESULT WINAPI NSTC2_fnEnsureItemVisible(INameSpaceTreeControl2* iface,
1280 IShellItem *psi)
1282 NSTC2Impl *This = impl_from_INameSpaceTreeControl2(iface);
1283 HTREEITEM hitem;
1285 TRACE("%p (%p)\n", This, psi);
1287 hitem = treeitem_from_shellitem(This, psi);
1288 if(hitem)
1290 SendMessageW(This->hwnd_tv, TVM_ENSUREVISIBLE, 0, (WPARAM)hitem);
1291 return S_OK;
1294 return E_INVALIDARG;
1297 static HRESULT WINAPI NSTC2_fnSetTheme(INameSpaceTreeControl2* iface,
1298 LPCWSTR pszTheme)
1300 NSTC2Impl *This = impl_from_INameSpaceTreeControl2(iface);
1301 FIXME("stub, %p (%p)\n", This, pszTheme);
1302 return E_NOTIMPL;
1305 static HRESULT WINAPI NSTC2_fnGetNextItem(INameSpaceTreeControl2* iface,
1306 IShellItem *psi,
1307 NSTCGNI nstcgi,
1308 IShellItem **ppsiNext)
1310 NSTC2Impl *This = impl_from_INameSpaceTreeControl2(iface);
1311 HTREEITEM hitem, hnext;
1312 UINT tvgn;
1313 TRACE("%p (%p, %x, %p)\n", This, psi, nstcgi, ppsiNext);
1315 if(!ppsiNext) return E_POINTER;
1316 if(!psi) return E_FAIL;
1318 *ppsiNext = NULL;
1320 hitem = treeitem_from_shellitem(This, psi);
1321 if(!hitem)
1322 return E_INVALIDARG;
1324 switch(nstcgi)
1326 case NSTCGNI_NEXT: tvgn = TVGN_NEXT; break;
1327 case NSTCGNI_NEXTVISIBLE: tvgn = TVGN_NEXTVISIBLE; break;
1328 case NSTCGNI_PREV: tvgn = TVGN_PREVIOUS; break;
1329 case NSTCGNI_PREVVISIBLE: tvgn = TVGN_PREVIOUSVISIBLE; break;
1330 case NSTCGNI_PARENT: tvgn = TVGN_PARENT; break;
1331 case NSTCGNI_CHILD: tvgn = TVGN_CHILD; break;
1332 case NSTCGNI_FIRSTVISIBLE: tvgn = TVGN_FIRSTVISIBLE; break;
1333 case NSTCGNI_LASTVISIBLE: tvgn = TVGN_LASTVISIBLE; break;
1334 default:
1335 FIXME("Unknown nstcgi value %d\n", nstcgi);
1336 return E_FAIL;
1339 hnext = (HTREEITEM)SendMessageW(This->hwnd_tv, TVM_GETNEXTITEM, tvgn, (WPARAM)hitem);
1340 if(hnext)
1342 *ppsiNext = shellitem_from_treeitem(This, hnext);
1343 IShellItem_AddRef(*ppsiNext);
1344 return S_OK;
1347 return E_FAIL;
1350 static HRESULT WINAPI NSTC2_fnHitTest(INameSpaceTreeControl2* iface,
1351 POINT *ppt,
1352 IShellItem **ppsiOut)
1354 NSTC2Impl *This = impl_from_INameSpaceTreeControl2(iface);
1355 HTREEITEM hitem;
1356 TRACE("%p (%p, %p)\n", This, ppsiOut, ppt);
1358 if(!ppt || !ppsiOut)
1359 return E_POINTER;
1361 *ppsiOut = NULL;
1363 hitem = treeitem_from_point(This, ppt, NULL);
1364 if(hitem)
1365 *ppsiOut = shellitem_from_treeitem(This, hitem);
1367 if(*ppsiOut)
1369 IShellItem_AddRef(*ppsiOut);
1370 return S_OK;
1373 return S_FALSE;
1376 static HRESULT WINAPI NSTC2_fnGetItemRect(INameSpaceTreeControl2* iface,
1377 IShellItem *psi,
1378 RECT *prect)
1380 NSTC2Impl *This = impl_from_INameSpaceTreeControl2(iface);
1381 HTREEITEM hitem;
1382 TRACE("%p (%p, %p)\n", This, psi, prect);
1384 if(!psi || !prect)
1385 return E_POINTER;
1387 hitem = treeitem_from_shellitem(This, psi);
1388 if(hitem)
1390 *(HTREEITEM*)prect = hitem;
1391 if(SendMessageW(This->hwnd_tv, TVM_GETITEMRECT, FALSE, (LPARAM)prect))
1393 MapWindowPoints(This->hwnd_tv, HWND_DESKTOP, (POINT*)prect, 2);
1394 return S_OK;
1398 return E_INVALIDARG;
1401 static HRESULT WINAPI NSTC2_fnCollapseAll(INameSpaceTreeControl2* iface)
1403 NSTC2Impl *This = impl_from_INameSpaceTreeControl2(iface);
1404 nstc_root *root;
1405 TRACE("%p\n", This);
1407 LIST_FOR_EACH_ENTRY(root, &This->roots, nstc_root, entry)
1408 collapse_all(This, root->htreeitem);
1410 return S_OK;
1413 static HRESULT WINAPI NSTC2_fnSetControlStyle(INameSpaceTreeControl2* iface,
1414 NSTCSTYLE nstcsMask,
1415 NSTCSTYLE nstcsStyle)
1417 NSTC2Impl *This = impl_from_INameSpaceTreeControl2(iface);
1418 static const DWORD tv_style_flags =
1419 NSTCS_HASEXPANDOS | NSTCS_HASLINES | NSTCS_FULLROWSELECT |
1420 NSTCS_HORIZONTALSCROLL | NSTCS_ROOTHASEXPANDO |
1421 NSTCS_SHOWSELECTIONALWAYS | NSTCS_NOINFOTIP | NSTCS_EVENHEIGHT |
1422 NSTCS_DISABLEDRAGDROP | NSTCS_NOEDITLABELS | NSTCS_CHECKBOXES;
1423 static const DWORD host_style_flags = NSTCS_TABSTOP | NSTCS_BORDER;
1424 static const DWORD nstc_flags =
1425 NSTCS_SINGLECLICKEXPAND | NSTCS_NOREPLACEOPEN | NSTCS_NOORDERSTREAM |
1426 NSTCS_FAVORITESMODE | NSTCS_EMPTYTEXT | NSTCS_ALLOWJUNCTIONS |
1427 NSTCS_SHOWTABSBUTTON | NSTCS_SHOWDELETEBUTTON | NSTCS_SHOWREFRESHBUTTON;
1428 TRACE("%p (%x, %x)\n", This, nstcsMask, nstcsStyle);
1430 /* Fail if there is an attempt to set an unknown style. */
1431 if(nstcsMask & ~(tv_style_flags | host_style_flags | nstc_flags))
1432 return E_FAIL;
1434 if(nstcsMask & tv_style_flags)
1436 DWORD new_style;
1437 treeview_style_from_nstcs(This, nstcsStyle, nstcsMask, &new_style);
1438 SetWindowLongPtrW(This->hwnd_tv, GWL_STYLE, new_style);
1441 /* Flags affecting the host window */
1442 if(nstcsMask & NSTCS_BORDER)
1444 DWORD new_style = GetWindowLongPtrW(This->hwnd_main, GWL_STYLE);
1445 new_style &= ~WS_BORDER;
1446 new_style |= nstcsStyle & NSTCS_BORDER ? WS_BORDER : 0;
1447 SetWindowLongPtrW(This->hwnd_main, GWL_STYLE, new_style);
1449 if(nstcsMask & NSTCS_TABSTOP)
1451 DWORD new_style = GetWindowLongPtrW(This->hwnd_main, GWL_EXSTYLE);
1452 new_style &= ~WS_EX_CONTROLPARENT;
1453 new_style |= nstcsStyle & NSTCS_TABSTOP ? WS_EX_CONTROLPARENT : 0;
1454 SetWindowLongPtrW(This->hwnd_main, GWL_EXSTYLE, new_style);
1457 if((nstcsStyle & nstcsMask) & unsupported_styles)
1458 FIXME("mask & style (0x%08x) contains unsupported style(s): 0x%08x\n",
1459 (nstcsStyle & nstcsMask),
1460 (nstcsStyle & nstcsMask) & unsupported_styles);
1462 This->style &= ~nstcsMask;
1463 This->style |= (nstcsStyle & nstcsMask);
1465 return S_OK;
1468 static HRESULT WINAPI NSTC2_fnGetControlStyle(INameSpaceTreeControl2* iface,
1469 NSTCSTYLE nstcsMask,
1470 NSTCSTYLE *pnstcsStyle)
1472 NSTC2Impl *This = impl_from_INameSpaceTreeControl2(iface);
1473 TRACE("%p (%x, %p)\n", This, nstcsMask, pnstcsStyle);
1475 *pnstcsStyle = (This->style & nstcsMask);
1477 return S_OK;
1480 static HRESULT WINAPI NSTC2_fnSetControlStyle2(INameSpaceTreeControl2* iface,
1481 NSTCSTYLE2 nstcsMask,
1482 NSTCSTYLE2 nstcsStyle)
1484 NSTC2Impl *This = impl_from_INameSpaceTreeControl2(iface);
1485 TRACE("%p (%x, %x)\n", This, nstcsMask, nstcsStyle);
1487 if((nstcsStyle & nstcsMask) & unsupported_styles2)
1488 FIXME("mask & style (0x%08x) contains unsupported style(s): 0x%08x\n",
1489 (nstcsStyle & nstcsMask),
1490 (nstcsStyle & nstcsMask) & unsupported_styles2);
1492 This->style2 &= ~nstcsMask;
1493 This->style2 |= (nstcsStyle & nstcsMask);
1495 return S_OK;
1498 static HRESULT WINAPI NSTC2_fnGetControlStyle2(INameSpaceTreeControl2* iface,
1499 NSTCSTYLE2 nstcsMask,
1500 NSTCSTYLE2 *pnstcsStyle)
1502 NSTC2Impl *This = impl_from_INameSpaceTreeControl2(iface);
1503 TRACE("%p (%x, %p)\n", This, nstcsMask, pnstcsStyle);
1505 *pnstcsStyle = (This->style2 & nstcsMask);
1507 return S_OK;
1510 static const INameSpaceTreeControl2Vtbl vt_INameSpaceTreeControl2 = {
1511 NSTC2_fnQueryInterface,
1512 NSTC2_fnAddRef,
1513 NSTC2_fnRelease,
1514 NSTC2_fnInitialize,
1515 NSTC2_fnTreeAdvise,
1516 NSTC2_fnTreeUnadvise,
1517 NSTC2_fnAppendRoot,
1518 NSTC2_fnInsertRoot,
1519 NSTC2_fnRemoveRoot,
1520 NSTC2_fnRemoveAllRoots,
1521 NSTC2_fnGetRootItems,
1522 NSTC2_fnSetItemState,
1523 NSTC2_fnGetItemState,
1524 NSTC2_fnGetSelectedItems,
1525 NSTC2_fnGetItemCustomState,
1526 NSTC2_fnSetItemCustomState,
1527 NSTC2_fnEnsureItemVisible,
1528 NSTC2_fnSetTheme,
1529 NSTC2_fnGetNextItem,
1530 NSTC2_fnHitTest,
1531 NSTC2_fnGetItemRect,
1532 NSTC2_fnCollapseAll,
1533 NSTC2_fnSetControlStyle,
1534 NSTC2_fnGetControlStyle,
1535 NSTC2_fnSetControlStyle2,
1536 NSTC2_fnGetControlStyle2
1539 /**************************************************************************
1540 * IOleWindow Implementation
1543 static HRESULT WINAPI IOW_fnQueryInterface(IOleWindow *iface, REFIID riid, void **ppvObject)
1545 NSTC2Impl *This = impl_from_IOleWindow(iface);
1546 return INameSpaceTreeControl2_QueryInterface(&This->INameSpaceTreeControl2_iface, riid, ppvObject);
1549 static ULONG WINAPI IOW_fnAddRef(IOleWindow *iface)
1551 NSTC2Impl *This = impl_from_IOleWindow(iface);
1552 return INameSpaceTreeControl2_AddRef(&This->INameSpaceTreeControl2_iface);
1555 static ULONG WINAPI IOW_fnRelease(IOleWindow *iface)
1557 NSTC2Impl *This = impl_from_IOleWindow(iface);
1558 return INameSpaceTreeControl2_Release(&This->INameSpaceTreeControl2_iface);
1561 static HRESULT WINAPI IOW_fnGetWindow(IOleWindow *iface, HWND *phwnd)
1563 NSTC2Impl *This = impl_from_IOleWindow(iface);
1564 TRACE("%p (%p)\n", This, phwnd);
1566 *phwnd = This->hwnd_main;
1567 return S_OK;
1570 static HRESULT WINAPI IOW_fnContextSensitiveHelp(IOleWindow *iface, BOOL fEnterMode)
1572 NSTC2Impl *This = impl_from_IOleWindow(iface);
1573 TRACE("%p (%d)\n", This, fEnterMode);
1575 /* Not implemented */
1576 return E_NOTIMPL;
1579 static const IOleWindowVtbl vt_IOleWindow = {
1580 IOW_fnQueryInterface,
1581 IOW_fnAddRef,
1582 IOW_fnRelease,
1583 IOW_fnGetWindow,
1584 IOW_fnContextSensitiveHelp
1587 HRESULT NamespaceTreeControl_Constructor(IUnknown *pUnkOuter, REFIID riid, void **ppv)
1589 NSTC2Impl *nstc;
1590 HRESULT ret;
1592 TRACE ("%p %s %p\n", pUnkOuter, debugstr_guid(riid), ppv);
1594 if(!ppv)
1595 return E_POINTER;
1596 if(pUnkOuter)
1597 return CLASS_E_NOAGGREGATION;
1599 EFRAME_LockModule();
1601 nstc = heap_alloc_zero(sizeof(*nstc));
1602 if (!nstc)
1603 return E_OUTOFMEMORY;
1605 nstc->ref = 1;
1606 nstc->INameSpaceTreeControl2_iface.lpVtbl = &vt_INameSpaceTreeControl2;
1607 nstc->IOleWindow_iface.lpVtbl = &vt_IOleWindow;
1609 list_init(&nstc->roots);
1611 ret = INameSpaceTreeControl2_QueryInterface(&nstc->INameSpaceTreeControl2_iface, riid, ppv);
1612 INameSpaceTreeControl2_Release(&nstc->INameSpaceTreeControl2_iface);
1614 TRACE("--(%p)\n", ppv);
1615 return ret;