user32: Support the MOUSEEVENTF_VIRTUALDESK flag in SendInput().
[wine.git] / programs / explorer / explorer.c
blob955678d23ba4b6f48e355b28cf3e73be5f60b5a9
1 /*
2 * explorer.exe
4 * Copyright 2004 CodeWeavers, Mike Hearn
5 * Copyright 2005,2006 CodeWeavers, Aric Stewart
6 * Copyright 2011 Jay Yang
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #define COBJMACROS
25 #include "wine/unicode.h"
26 #include "wine/debug.h"
27 #include "wine/heap.h"
28 #include "explorer_private.h"
29 #include "resource.h"
31 #include <initguid.h>
32 #include <windows.h>
33 #include <shellapi.h>
34 #include <shobjidl.h>
35 #include <shlobj.h>
36 #include <shlwapi.h>
37 #include <commoncontrols.h>
38 #include <commctrl.h>
40 WINE_DEFAULT_DEBUG_CHANNEL(explorer);
42 #define EXPLORER_INFO_INDEX 0
44 #define NAV_TOOLBAR_HEIGHT 30
45 #define PATHBOX_HEIGHT 24
46 static int nav_toolbar_height;
47 static int pathbox_height;
49 #define DEFAULT_WIDTH 640
50 #define DEFAULT_HEIGHT 480
51 static int default_width;
52 static int default_height;
55 static const WCHAR EXPLORER_CLASS[] = {'E','x','p','l','o','r','e','r','W','C','l','a','s','s',0};
56 static const WCHAR PATH_BOX_NAME[] = {'\0'};
58 HINSTANCE explorer_hInstance;
60 typedef struct parametersTAG {
61 BOOL explorer_mode;
62 WCHAR root[MAX_PATH];
63 WCHAR selection[MAX_PATH];
64 } parameters_struct;
66 typedef struct
68 IExplorerBrowser *browser;
69 HWND main_window,path_box;
70 INT rebar_height;
71 LPITEMIDLIST pidl;
72 IImageList *icon_list;
73 DWORD advise_cookie;
74 } explorer_info;
76 enum
78 BACK_BUTTON,FORWARD_BUTTON,UP_BUTTON
81 typedef struct
83 IExplorerBrowserEvents IExplorerBrowserEvents_iface;
84 explorer_info* info;
85 LONG ref;
86 } IExplorerBrowserEventsImpl;
88 static IExplorerBrowserEventsImpl *impl_from_IExplorerBrowserEvents(IExplorerBrowserEvents *iface)
90 return CONTAINING_RECORD(iface, IExplorerBrowserEventsImpl, IExplorerBrowserEvents_iface);
93 static HRESULT WINAPI IExplorerBrowserEventsImpl_fnQueryInterface(IExplorerBrowserEvents *iface, REFIID riid, void **ppvObject)
95 return E_NOINTERFACE;
98 static ULONG WINAPI IExplorerBrowserEventsImpl_fnAddRef(IExplorerBrowserEvents *iface)
100 IExplorerBrowserEventsImpl *This = impl_from_IExplorerBrowserEvents(iface);
101 return InterlockedIncrement(&This->ref);
104 static ULONG WINAPI IExplorerBrowserEventsImpl_fnRelease(IExplorerBrowserEvents *iface)
106 IExplorerBrowserEventsImpl *This = impl_from_IExplorerBrowserEvents(iface);
107 ULONG ref = InterlockedDecrement(&This->ref);
108 if(!ref)
109 HeapFree(GetProcessHeap(),0,This);
110 return ref;
113 static BOOL create_combobox_item(IShellFolder *folder, LPCITEMIDLIST child_pidl, IImageList *icon_list, COMBOBOXEXITEMW *item)
115 STRRET strret;
116 HRESULT hres;
117 PIDLIST_ABSOLUTE parent_pidl, pidl;
118 SHFILEINFOW info;
119 IImageList *list;
121 strret.uType=STRRET_WSTR;
122 hres = IShellFolder_GetDisplayNameOf( folder, child_pidl, SHGDN_FORADDRESSBAR, &strret );
123 if(SUCCEEDED(hres))
124 hres = StrRetToStrW(&strret, child_pidl, &item->pszText);
125 if(FAILED(hres))
127 WINE_WARN("Could not get name for pidl\n");
128 return FALSE;
131 item->mask &= ~CBEIF_IMAGE;
132 hres = SHGetIDListFromObject( (IUnknown *)folder, &parent_pidl );
133 if (FAILED(hres)) return FALSE;
135 pidl = ILCombine( parent_pidl, child_pidl );
136 if (pidl)
138 list = (IImageList *)SHGetFileInfoW( (WCHAR *)pidl, 0, &info, sizeof(info),
139 SHGFI_PIDL | SHGFI_SMALLICON | SHGFI_SYSICONINDEX );
140 if (list)
142 IImageList_Release( list );
143 item->iImage = info.iIcon;
144 item->mask |= CBEIF_IMAGE;
146 ILFree( pidl );
148 ILFree( parent_pidl );
150 return TRUE;
153 static void update_path_box(explorer_info *info)
155 COMBOBOXEXITEMW item;
156 COMBOBOXEXITEMW main_item;
157 IShellFolder *desktop;
158 IPersistFolder2 *persist;
159 LPITEMIDLIST desktop_pidl;
160 IEnumIDList *ids;
162 SendMessageW(info->path_box,CB_RESETCONTENT,0,0);
163 SHGetDesktopFolder(&desktop);
164 IShellFolder_QueryInterface(desktop,&IID_IPersistFolder2,(void**)&persist);
165 IPersistFolder2_GetCurFolder(persist,&desktop_pidl);
166 IPersistFolder2_Release(persist);
167 persist = NULL;
168 /*Add Desktop*/
169 item.iItem = -1;
170 item.mask = CBEIF_TEXT | CBEIF_INDENT | CBEIF_LPARAM;
171 item.iIndent = 0;
172 create_combobox_item(desktop,desktop_pidl,info->icon_list,&item);
173 item.lParam = (LPARAM)desktop_pidl;
174 SendMessageW(info->path_box,CBEM_INSERTITEMW,0,(LPARAM)&item);
175 if(ILIsEqual(info->pidl,desktop_pidl))
176 main_item = item;
177 else
178 CoTaskMemFree(item.pszText);
179 /*Add all direct subfolders of Desktop*/
180 if(SUCCEEDED(IShellFolder_EnumObjects(desktop,NULL,SHCONTF_FOLDERS,&ids))
181 && ids!=NULL)
183 LPITEMIDLIST curr_pidl=NULL;
184 HRESULT hres;
186 item.iIndent = 1;
187 while(1)
189 ILFree(curr_pidl);
190 curr_pidl=NULL;
191 hres = IEnumIDList_Next(ids,1,&curr_pidl,NULL);
192 if(FAILED(hres) || hres == S_FALSE)
193 break;
194 if(!create_combobox_item(desktop,curr_pidl,info->icon_list,&item))
195 WINE_WARN("Could not create a combobox item\n");
196 else
198 LPITEMIDLIST full_pidl = ILCombine(desktop_pidl,curr_pidl);
199 item.lParam = (LPARAM)full_pidl;
200 SendMessageW(info->path_box,CBEM_INSERTITEMW,0,(LPARAM)&item);
201 if(ILIsEqual(full_pidl,info->pidl))
202 main_item = item;
203 else if(ILIsParent(full_pidl,info->pidl,FALSE))
205 /*add all parents of the pidl passed in*/
206 LPITEMIDLIST next_pidl = ILFindChild(full_pidl,info->pidl);
207 IShellFolder *curr_folder = NULL, *temp;
208 hres = IShellFolder_BindToObject(desktop,curr_pidl,NULL,
209 &IID_IShellFolder,
210 (void**)&curr_folder);
211 if(FAILED(hres))
212 WINE_WARN("Could not get an IShellFolder\n");
213 while(!ILIsEmpty(next_pidl))
215 LPITEMIDLIST first = ILCloneFirst(next_pidl);
216 CoTaskMemFree(item.pszText);
217 if(!create_combobox_item(curr_folder,first,
218 info->icon_list,&item))
220 WINE_WARN("Could not create a combobox item\n");
221 break;
223 ++item.iIndent;
224 full_pidl = ILCombine(full_pidl,first);
225 item.lParam = (LPARAM)full_pidl;
226 SendMessageW(info->path_box,CBEM_INSERTITEMW,0,(LPARAM)&item);
227 temp=NULL;
228 hres = IShellFolder_BindToObject(curr_folder,first,NULL,
229 &IID_IShellFolder,
230 (void**)&temp);
231 if(FAILED(hres))
233 WINE_WARN("Could not get an IShellFolder\n");
234 break;
236 IShellFolder_Release(curr_folder);
237 curr_folder = temp;
239 ILFree(first);
240 next_pidl = ILGetNext(next_pidl);
242 memcpy(&main_item,&item,sizeof(item));
243 if(curr_folder)
244 IShellFolder_Release(curr_folder);
245 item.iIndent = 1;
247 else
248 CoTaskMemFree(item.pszText);
251 ILFree(curr_pidl);
252 IEnumIDList_Release(ids);
254 else
255 WINE_WARN("Could not enumerate the desktop\n");
256 SendMessageW(info->path_box,CBEM_SETITEMW,0,(LPARAM)&main_item);
257 CoTaskMemFree(main_item.pszText);
260 static HRESULT WINAPI IExplorerBrowserEventsImpl_fnOnNavigationComplete(IExplorerBrowserEvents *iface, PCIDLIST_ABSOLUTE pidl)
262 IExplorerBrowserEventsImpl *This = impl_from_IExplorerBrowserEvents(iface);
263 IShellFolder *parent;
264 PCUITEMID_CHILD child_pidl;
265 HRESULT hres;
266 STRRET strret;
267 WCHAR *name;
269 ILFree(This->info->pidl);
270 This->info->pidl = ILClone(pidl);
271 update_path_box(This->info);
273 hres = SHBindToParent(pidl, &IID_IShellFolder, (void **)&parent, &child_pidl);
274 if (SUCCEEDED(hres))
276 hres = IShellFolder_GetDisplayNameOf(parent, child_pidl, SHGDN_FORADDRESSBAR, &strret);
277 if (SUCCEEDED(hres))
278 hres = StrRetToStrW(&strret, child_pidl, &name);
279 if (SUCCEEDED(hres))
281 SetWindowTextW(This->info->main_window, name);
282 CoTaskMemFree(name);
285 IShellFolder_Release(parent);
288 return hres;
291 static HRESULT WINAPI IExplorerBrowserEventsImpl_fnOnNavigationFailed(IExplorerBrowserEvents *iface, PCIDLIST_ABSOLUTE pidl)
293 return S_OK;
296 static HRESULT WINAPI IExplorerBrowserEventsImpl_fnOnNavigationPending(IExplorerBrowserEvents *iface, PCIDLIST_ABSOLUTE pidl)
298 return S_OK;
301 static HRESULT WINAPI IExplorerBrowserEventsImpl_fnOnViewCreated(IExplorerBrowserEvents *iface, IShellView *psv)
303 return S_OK;
306 static IExplorerBrowserEventsVtbl vt_IExplorerBrowserEvents =
308 IExplorerBrowserEventsImpl_fnQueryInterface,
309 IExplorerBrowserEventsImpl_fnAddRef,
310 IExplorerBrowserEventsImpl_fnRelease,
311 IExplorerBrowserEventsImpl_fnOnNavigationPending,
312 IExplorerBrowserEventsImpl_fnOnViewCreated,
313 IExplorerBrowserEventsImpl_fnOnNavigationComplete,
314 IExplorerBrowserEventsImpl_fnOnNavigationFailed
317 static IExplorerBrowserEvents *make_explorer_events(explorer_info *info)
319 IExplorerBrowserEventsImpl *ret
320 = HeapAlloc(GetProcessHeap(), 0, sizeof(IExplorerBrowserEventsImpl));
321 ret->IExplorerBrowserEvents_iface.lpVtbl = &vt_IExplorerBrowserEvents;
322 ret->info = info;
323 ret->ref = 1;
324 SHGetImageList(SHIL_SMALL,&IID_IImageList,(void**)&(ret->info->icon_list));
325 SendMessageW(info->path_box,CBEM_SETIMAGELIST,0,(LPARAM)ret->info->icon_list);
326 return &ret->IExplorerBrowserEvents_iface;
329 static void make_explorer_window(IShellFolder* startFolder)
331 RECT rect;
332 HWND rebar,nav_toolbar;
333 FOLDERSETTINGS fs;
334 IExplorerBrowserEvents *events;
335 explorer_info *info;
336 HRESULT hres;
337 WCHAR explorer_title[100];
338 WCHAR pathbox_label[50];
339 TBADDBITMAP bitmap_info;
340 TBBUTTON nav_buttons[3];
341 int hist_offset,view_offset;
342 REBARBANDINFOW band_info;
343 UINT dpix, dpiy;
344 HDC hdc;
346 memset(nav_buttons,0,sizeof(nav_buttons));
348 LoadStringW(explorer_hInstance,IDS_EXPLORER_TITLE,explorer_title, ARRAY_SIZE( explorer_title ));
349 LoadStringW(explorer_hInstance,IDS_PATHBOX_LABEL,pathbox_label, ARRAY_SIZE( pathbox_label ));
351 hdc = GetDC(0);
352 dpix = GetDeviceCaps(hdc, LOGPIXELSX);
353 dpiy = GetDeviceCaps(hdc, LOGPIXELSY);
354 ReleaseDC(0, hdc);
355 nav_toolbar_height = MulDiv(NAV_TOOLBAR_HEIGHT, dpiy, USER_DEFAULT_SCREEN_DPI);
356 pathbox_height = MulDiv(PATHBOX_HEIGHT, dpiy, USER_DEFAULT_SCREEN_DPI);
357 default_width = MulDiv(DEFAULT_WIDTH, dpix, USER_DEFAULT_SCREEN_DPI);
358 default_height = MulDiv(DEFAULT_HEIGHT, dpiy, USER_DEFAULT_SCREEN_DPI);
360 info = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(explorer_info));
361 if(!info)
363 WINE_ERR("Could not allocate an explorer_info struct\n");
364 return;
366 hres = CoCreateInstance(&CLSID_ExplorerBrowser,NULL,CLSCTX_INPROC_SERVER,
367 &IID_IExplorerBrowser,(LPVOID*)&info->browser);
368 if(FAILED(hres))
370 WINE_ERR("Could not obtain an instance of IExplorerBrowser\n");
371 HeapFree(GetProcessHeap(),0,info);
372 return;
374 info->rebar_height=0;
375 info->main_window
376 = CreateWindowW(EXPLORER_CLASS,explorer_title,WS_OVERLAPPEDWINDOW,
377 CW_USEDEFAULT,CW_USEDEFAULT,default_width,
378 default_height,NULL,NULL,explorer_hInstance,NULL);
380 fs.ViewMode = FVM_DETAILS;
381 fs.fFlags = FWF_AUTOARRANGE;
383 SetRect(&rect, 0, 0, default_width, default_height);
384 IExplorerBrowser_Initialize(info->browser,info->main_window,&rect,&fs);
385 IExplorerBrowser_SetOptions(info->browser,EBO_SHOWFRAMES);
386 SetWindowLongPtrW(info->main_window,EXPLORER_INFO_INDEX,(LONG_PTR)info);
388 /*setup navbar*/
389 rebar = CreateWindowExW(WS_EX_TOOLWINDOW,REBARCLASSNAMEW,NULL,
390 WS_CHILD|WS_VISIBLE|RBS_VARHEIGHT|CCS_TOP|CCS_NODIVIDER,
391 0,0,0,0,info->main_window,NULL,explorer_hInstance,NULL);
392 nav_toolbar
393 = CreateWindowExW(TBSTYLE_EX_MIXEDBUTTONS,TOOLBARCLASSNAMEW,NULL,
394 WS_CHILD|WS_VISIBLE|TBSTYLE_FLAT,0,0,0,0,rebar,NULL,
395 explorer_hInstance,NULL);
397 bitmap_info.hInst = HINST_COMMCTRL;
398 bitmap_info.nID = IDB_HIST_LARGE_COLOR;
399 hist_offset= SendMessageW(nav_toolbar,TB_ADDBITMAP,0,(LPARAM)&bitmap_info);
400 bitmap_info.nID = IDB_VIEW_LARGE_COLOR;
401 view_offset= SendMessageW(nav_toolbar,TB_ADDBITMAP,0,(LPARAM)&bitmap_info);
403 nav_buttons[0].iBitmap=hist_offset+HIST_BACK;
404 nav_buttons[0].idCommand=BACK_BUTTON;
405 nav_buttons[0].fsState=TBSTATE_ENABLED;
406 nav_buttons[0].fsStyle=BTNS_BUTTON|BTNS_AUTOSIZE;
407 nav_buttons[1].iBitmap=hist_offset+HIST_FORWARD;
408 nav_buttons[1].idCommand=FORWARD_BUTTON;
409 nav_buttons[1].fsState=TBSTATE_ENABLED;
410 nav_buttons[1].fsStyle=BTNS_BUTTON|BTNS_AUTOSIZE;
411 nav_buttons[2].iBitmap=view_offset+VIEW_PARENTFOLDER;
412 nav_buttons[2].idCommand=UP_BUTTON;
413 nav_buttons[2].fsState=TBSTATE_ENABLED;
414 nav_buttons[2].fsStyle=BTNS_BUTTON|BTNS_AUTOSIZE;
415 SendMessageW(nav_toolbar,TB_BUTTONSTRUCTSIZE,sizeof(TBBUTTON),0);
416 SendMessageW(nav_toolbar,TB_ADDBUTTONSW,ARRAY_SIZE( nav_buttons ),(LPARAM)nav_buttons);
418 band_info.cbSize = sizeof(band_info);
419 band_info.fMask = RBBIM_STYLE|RBBIM_CHILD|RBBIM_CHILDSIZE|RBBIM_SIZE;
420 band_info.hwndChild = nav_toolbar;
421 band_info.fStyle=RBBS_GRIPPERALWAYS|RBBS_CHILDEDGE;
422 band_info.cyChild=nav_toolbar_height;
423 band_info.cx=0;
424 band_info.cyMinChild=nav_toolbar_height;
425 band_info.cxMinChild=0;
426 SendMessageW(rebar,RB_INSERTBANDW,-1,(LPARAM)&band_info);
427 info->path_box = CreateWindowW(WC_COMBOBOXEXW,PATH_BOX_NAME,
428 WS_CHILD | WS_VISIBLE | CBS_DROPDOWN,
429 0,0,default_width,pathbox_height,rebar,NULL,
430 explorer_hInstance,NULL);
431 GetWindowRect(info->path_box, &rect);
432 band_info.cyChild = rect.bottom - rect.top;
433 band_info.cx=0;
434 band_info.cyMinChild = rect.bottom - rect.top;
435 band_info.cxMinChild=0;
436 band_info.fMask|=RBBIM_TEXT;
437 band_info.lpText=pathbox_label;
438 band_info.fStyle|=RBBS_BREAK;
439 band_info.hwndChild=info->path_box;
440 SendMessageW(rebar,RB_INSERTBANDW,-1,(LPARAM)&band_info);
441 events = make_explorer_events(info);
442 IExplorerBrowser_Advise(info->browser,events,&info->advise_cookie);
443 IExplorerBrowser_BrowseToObject(info->browser,(IUnknown*)startFolder,
444 SBSP_ABSOLUTE);
445 ShowWindow(info->main_window,SW_SHOWDEFAULT);
446 UpdateWindow(info->main_window);
447 IExplorerBrowserEvents_Release(events);
450 static void update_window_size(explorer_info *info, int height, int width)
452 RECT new_rect;
453 new_rect.left = 0;
454 new_rect.top = info->rebar_height;
455 new_rect.right = width;
456 new_rect.bottom = height;
457 IExplorerBrowser_SetRect(info->browser,NULL,new_rect);
460 static void do_exit(int code)
462 OleUninitialize();
463 ExitProcess(code);
466 static LRESULT explorer_on_end_edit(explorer_info *info,NMCBEENDEDITW *edit_info)
468 LPITEMIDLIST pidl = NULL;
470 WINE_TRACE("iWhy=%x\n",edit_info->iWhy);
471 switch(edit_info->iWhy)
473 case CBENF_DROPDOWN:
474 if(edit_info->iNewSelection!=CB_ERR)
475 pidl = (LPITEMIDLIST)SendMessageW(edit_info->hdr.hwndFrom,
476 CB_GETITEMDATA,
477 edit_info->iNewSelection,0);
478 break;
479 case CBENF_RETURN:
481 WCHAR path[MAX_PATH];
482 HWND edit_ctrl = (HWND)SendMessageW(edit_info->hdr.hwndFrom,
483 CBEM_GETEDITCONTROL,0,0);
484 *((WORD*)path)=MAX_PATH;
485 SendMessageW(edit_ctrl,EM_GETLINE,0,(LPARAM)path);
486 pidl = ILCreateFromPathW(path);
487 break;
489 case CBENF_ESCAPE:
490 /*make sure the that the path box resets*/
491 update_path_box(info);
492 return 0;
493 default:
494 return 0;
496 if(pidl)
497 IExplorerBrowser_BrowseToIDList(info->browser,pidl,SBSP_ABSOLUTE);
498 if(edit_info->iWhy==CBENF_RETURN)
499 ILFree(pidl);
500 return 0;
503 static LRESULT update_rebar_size(explorer_info* info,NMRBAUTOSIZE *size_info)
505 RECT new_rect;
506 RECT window_rect;
507 info->rebar_height = size_info->rcTarget.bottom-size_info->rcTarget.top;
508 GetWindowRect(info->main_window,&window_rect);
509 new_rect.left = 0;
510 new_rect.top = info->rebar_height;
511 new_rect.right = window_rect.right-window_rect.left;
512 new_rect.bottom = window_rect.bottom-window_rect.top;
513 IExplorerBrowser_SetRect(info->browser,NULL,new_rect);
514 return 0;
517 static LRESULT explorer_on_notify(explorer_info* info,NMHDR* notification)
519 WINE_TRACE("code=%i\n",notification->code);
520 switch(notification->code)
522 case CBEN_BEGINEDIT:
524 WCHAR path[MAX_PATH];
525 HWND edit_ctrl = (HWND)SendMessageW(notification->hwndFrom,
526 CBEM_GETEDITCONTROL,0,0);
527 SHGetPathFromIDListW(info->pidl,path);
528 SetWindowTextW(edit_ctrl,path);
529 break;
531 case CBEN_ENDEDITA:
533 NMCBEENDEDITA *edit_info_a = (NMCBEENDEDITA*)notification;
534 NMCBEENDEDITW edit_info_w;
535 edit_info_w.hdr = edit_info_a->hdr;
536 edit_info_w.fChanged = edit_info_a->fChanged;
537 edit_info_w.iNewSelection = edit_info_a->iNewSelection;
538 MultiByteToWideChar(CP_ACP,0,edit_info_a->szText,-1,
539 edit_info_w.szText,CBEMAXSTRLEN);
540 edit_info_w.iWhy = edit_info_a->iWhy;
541 return explorer_on_end_edit(info,&edit_info_w);
543 case CBEN_ENDEDITW:
544 return explorer_on_end_edit(info,(NMCBEENDEDITW*)notification);
545 case CBEN_DELETEITEM:
547 NMCOMBOBOXEXW *entry = (NMCOMBOBOXEXW*)notification;
548 if(entry->ceItem.lParam)
549 ILFree((LPITEMIDLIST)entry->ceItem.lParam);
550 break;
552 case RBN_AUTOSIZE:
553 return update_rebar_size(info,(NMRBAUTOSIZE*)notification);
554 default:
555 break;
557 return 0;
560 static LRESULT CALLBACK explorer_wnd_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
562 explorer_info *info
563 = (explorer_info*)GetWindowLongPtrW(hwnd,EXPLORER_INFO_INDEX);
564 IExplorerBrowser *browser = NULL;
566 WINE_TRACE("(hwnd=%p,uMsg=%u,wParam=%lx,lParam=%lx)\n",hwnd,uMsg,wParam,lParam);
567 if(info)
568 browser = info->browser;
569 switch(uMsg)
571 case WM_DESTROY:
572 IExplorerBrowser_Unadvise(browser,info->advise_cookie);
573 IExplorerBrowser_Destroy(browser);
574 IExplorerBrowser_Release(browser);
575 ILFree(info->pidl);
576 IImageList_Release(info->icon_list);
577 HeapFree(GetProcessHeap(),0,info);
578 SetWindowLongPtrW(hwnd,EXPLORER_INFO_INDEX,0);
579 PostQuitMessage(0);
580 break;
581 case WM_QUIT:
582 do_exit(wParam);
583 case WM_NOTIFY:
584 return explorer_on_notify(info,(NMHDR*)lParam);
585 case WM_COMMAND:
586 if(HIWORD(wParam)==BN_CLICKED)
588 switch(LOWORD(wParam))
590 case BACK_BUTTON:
591 IExplorerBrowser_BrowseToObject(browser,NULL,SBSP_NAVIGATEBACK);
592 break;
593 case FORWARD_BUTTON:
594 IExplorerBrowser_BrowseToObject(browser,NULL,SBSP_NAVIGATEFORWARD);
595 break;
596 case UP_BUTTON:
597 IExplorerBrowser_BrowseToObject(browser,NULL,SBSP_PARENT);
598 break;
601 break;
602 case WM_SIZE:
603 update_window_size(info,HIWORD(lParam),LOWORD(lParam));
604 break;
605 default:
606 return DefWindowProcW(hwnd,uMsg,wParam,lParam);
608 return 0;
611 static void register_explorer_window_class(void)
613 WNDCLASSEXW window_class;
614 window_class.cbSize = sizeof(WNDCLASSEXW);
615 window_class.style = 0;
616 window_class.cbClsExtra = 0;
617 window_class.cbWndExtra = sizeof(LONG_PTR);
618 window_class.lpfnWndProc = explorer_wnd_proc;
619 window_class.hInstance = explorer_hInstance;
620 window_class.hIcon = NULL;
621 window_class.hCursor = NULL;
622 window_class.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
623 window_class.lpszMenuName = NULL;
624 window_class.lpszClassName = EXPLORER_CLASS;
625 window_class.hIconSm = NULL;
626 RegisterClassExW(&window_class);
629 static IShellFolder* get_starting_shell_folder(parameters_struct* params)
631 IShellFolder* desktop,*folder;
632 LPITEMIDLIST root_pidl;
633 WCHAR *fullpath = NULL;
634 HRESULT hres;
635 DWORD size;
637 SHGetDesktopFolder(&desktop);
638 if (!params->root[0])
640 return desktop;
643 size = GetFullPathNameW(params->root, 0, fullpath, NULL);
644 if (!size)
645 return desktop;
646 fullpath = heap_alloc(size * sizeof(WCHAR));
647 GetFullPathNameW(params->root, size, fullpath, NULL);
649 hres = IShellFolder_ParseDisplayName(desktop,NULL,NULL,
650 fullpath,NULL,
651 &root_pidl,NULL);
652 heap_free(fullpath);
654 if(FAILED(hres))
656 return desktop;
658 hres = IShellFolder_BindToObject(desktop,root_pidl,NULL,
659 &IID_IShellFolder,
660 (void**)&folder);
661 ILFree(root_pidl);
662 if(FAILED(hres))
664 return desktop;
666 IShellFolder_Release(desktop);
667 return folder;
670 static WCHAR *copy_path_string(WCHAR *target, WCHAR *source)
672 INT i = 0;
674 while (isspaceW(*source)) source++;
676 if (*source == '\"')
678 source ++;
679 while (*source && *source != '\"') target[i++] = *source++;
680 target[i] = 0;
681 if (*source) source++;
683 else
685 while (*source && *source != ',') target[i++] = *source++;
686 target[i] = 0;
688 PathRemoveBackslashW(target);
689 return source;
693 static void copy_path_root(LPWSTR root, LPWSTR path)
695 LPWSTR p,p2;
696 INT i = 0;
698 p = path;
699 while (*p!=0)
700 p++;
702 while (*p!='\\' && p > path)
703 p--;
705 if (p == path)
706 return;
708 p2 = path;
709 while (p2 != p)
711 root[i] = *p2;
712 i++;
713 p2++;
715 root[i] = 0;
719 * Command Line parameters are:
720 * [/n] Opens in single-paned view for each selected items. This is default
721 * [/e,] Uses Windows Explorer View
722 * [/root,object] Specifies the root level of the view
723 * [/select,object] parent folder is opened and specified object is selected
725 static void parse_command_line(LPWSTR commandline,parameters_struct *parameters)
727 static const WCHAR arg_n[] = {'/','n'};
728 static const WCHAR arg_e[] = {'/','e',','};
729 static const WCHAR arg_root[] = {'/','r','o','o','t',','};
730 static const WCHAR arg_select[] = {'/','s','e','l','e','c','t',','};
731 static const WCHAR arg_desktop[] = {'/','d','e','s','k','t','o','p'};
732 static const WCHAR arg_desktop_quotes[] = {'"','/','d','e','s','k','t','o','p'};
734 LPWSTR p = commandline;
736 while (*p)
738 while (isspaceW(*p)) p++;
739 if (strncmpW(p, arg_n, ARRAY_SIZE( arg_n ))==0)
741 parameters->explorer_mode = FALSE;
742 p += ARRAY_SIZE( arg_n );
744 else if (strncmpW(p, arg_e, ARRAY_SIZE( arg_e ))==0)
746 parameters->explorer_mode = TRUE;
747 p += ARRAY_SIZE( arg_e );
749 else if (strncmpW(p, arg_root, ARRAY_SIZE( arg_root ))==0)
751 p += ARRAY_SIZE( arg_root );
752 p = copy_path_string(parameters->root,p);
754 else if (strncmpW(p, arg_select, ARRAY_SIZE( arg_select ))==0)
756 p += ARRAY_SIZE( arg_select );
757 p = copy_path_string(parameters->selection,p);
758 if (!parameters->root[0])
759 copy_path_root(parameters->root,
760 parameters->selection);
762 else if (strncmpW(p, arg_desktop, ARRAY_SIZE( arg_desktop ))==0)
764 p += ARRAY_SIZE( arg_desktop );
765 manage_desktop( p ); /* the rest of the command line is handled by desktop mode */
767 /* workaround for Worms Armageddon that hardcodes a /desktop option with quotes */
768 else if (strncmpW(p, arg_desktop_quotes, ARRAY_SIZE( arg_desktop_quotes ))==0)
770 p += ARRAY_SIZE( arg_desktop_quotes );
771 manage_desktop( p ); /* the rest of the command line is handled by desktop mode */
773 else
775 /* left over command line is generally the path to be opened */
776 copy_path_string(parameters->root,p);
777 break;
782 int WINAPI wWinMain(HINSTANCE hinstance,
783 HINSTANCE previnstance,
784 LPWSTR cmdline,
785 int cmdshow)
788 parameters_struct parameters;
789 HRESULT hres;
790 MSG msg;
791 IShellFolder *folder;
792 INITCOMMONCONTROLSEX init_info;
794 memset(&parameters,0,sizeof(parameters));
795 explorer_hInstance = hinstance;
796 parse_command_line(cmdline,&parameters);
797 hres = OleInitialize(NULL);
798 if(FAILED(hres))
800 WINE_ERR("Could not initialize COM\n");
801 ExitProcess(EXIT_FAILURE);
803 if(parameters.root[0] && !PathIsDirectoryW(parameters.root))
804 if(ShellExecuteW(NULL,NULL,parameters.root,NULL,NULL,SW_SHOWDEFAULT) > (HINSTANCE)32)
805 ExitProcess(EXIT_SUCCESS);
806 init_info.dwSize = sizeof(INITCOMMONCONTROLSEX);
807 init_info.dwICC = ICC_USEREX_CLASSES | ICC_BAR_CLASSES | ICC_COOL_CLASSES;
808 if(!InitCommonControlsEx(&init_info))
810 WINE_ERR("Could not initialize Comctl\n");
811 ExitProcess(EXIT_FAILURE);
813 register_explorer_window_class();
814 folder = get_starting_shell_folder(&parameters);
815 make_explorer_window(folder);
816 IShellFolder_Release(folder);
817 while(GetMessageW( &msg, NULL, 0, 0 ) != 0)
819 TranslateMessage(&msg);
820 DispatchMessageW(&msg);
822 return 0;