[EXPLORER-NEW]
[reactos.git] / dll / win32 / browseui / shellbrowser.cpp
index e79f8b8..c12143e 100644 (file)
@@ -26,8 +26,7 @@
 extern "C"
 BOOL WINAPI Shell_GetImageLists(
     _Out_  HIMAGELIST *phiml,
-    _Out_  HIMAGELIST *phimlSmall
-    );
+    _Out_  HIMAGELIST *phimlSmall);
 
 extern HRESULT IUnknown_ShowDW(IUnknown * punk, BOOL fShow);
 
@@ -898,28 +897,32 @@ HRESULT IEGetNameAndFlagsEx(LPITEMIDLIST pidl, SHGDNF uFlags, long param10,
     LPWSTR pszBuf, UINT cchBuf, SFGAOF *rgfInOut)
 {
     CComPtr<IShellFolder>                   parentFolder;
-    LPITEMIDLIST                            childPIDL;
+    LPITEMIDLIST                            childPIDL = NULL;
     STRRET                                  L108;
     HRESULT                                 hResult;
 
     hResult = SHBindToFolderIDListParent(NULL, pidl, &IID_PPV_ARG(IShellFolder, &parentFolder), &childPIDL);
     if (FAILED(hResult))
-        return hResult;
+        goto cleanup;
 
     hResult = parentFolder->GetDisplayNameOf(childPIDL, uFlags, &L108);
     if (FAILED(hResult))
-        return hResult;
+        goto cleanup;
 
     StrRetToBufW(&L108, childPIDL, pszBuf, cchBuf);
     if (rgfInOut)
     {
         hResult = parentFolder->GetAttributesOf(1, const_cast<LPCITEMIDLIST *>(&childPIDL), rgfInOut);
         if (FAILED(hResult))
-            return hResult;
+            goto cleanup;
     }
 
-    ILFree(childPIDL);
-    return S_OK;
+    hResult = S_OK;
+
+cleanup:
+    if (childPIDL)
+        ILFree(childPIDL);
+    return hResult;
 }
 
 long IEGetNameAndFlags(LPITEMIDLIST pidl, SHGDNF uFlags, LPWSTR pszBuf, UINT cchBuf, SFGAOF *rgfInOut)
@@ -1075,11 +1078,17 @@ HRESULT CShellBrowser::BrowseToPath(IShellFolder *newShellFolder,
         HICON icSmall = ImageList_GetIcon(himlSmall, indexOpen, 0);
         HICON icLarge = ImageList_GetIcon(himlLarge, indexOpen, 0);
 
+        /* Hack to make it possible to release the old icons */
+        /* Something seems to go wrong with WM_SETICON */
+        HICON oldSmall = (HICON)SendMessage(WM_GETICON, ICON_SMALL, 0);
+        HICON oldLarge = (HICON)SendMessage(WM_GETICON, ICON_BIG,   0);
+
         SendMessage(WM_SETICON, ICON_SMALL, reinterpret_cast<LPARAM>(icSmall));
         SendMessage(WM_SETICON, ICON_BIG,   reinterpret_cast<LPARAM>(icLarge));
-    }
 
-    // TODO: Update the window icon
+        DestroyIcon(oldSmall);
+        DestroyIcon(oldLarge);
+    }
 
     FireCommandStateChangeAll();
     hResult = UpdateForwardBackState();
@@ -1851,6 +1860,7 @@ HRESULT STDMETHODCALLTYPE CShellBrowser::ContextSensitiveHelp(BOOL fEnterMode)
 HRESULT STDMETHODCALLTYPE CShellBrowser::InsertMenusSB(HMENU hmenuShared, LPOLEMENUGROUPWIDTHS lpMenuWidths)
 {
     HMENU mainMenu = LoadMenu(_AtlBaseModule.GetResourceInstance(), MAKEINTRESOURCE(IDM_CABINET_MAINMENU));
+
     Shell_MergeMenus(hmenuShared, mainMenu, 0, 0, FCIDM_BROWSERLAST, MM_SUBMENUSHAVEIDS);
 
     int GCCU(itemCount3) = GetMenuItemCount(hmenuShared);
@@ -2563,13 +2573,10 @@ HRESULT STDMETHODCALLTYPE CShellBrowser::v_MayTranslateAccelerator(MSG *pmsg)
             return S_OK;
     }
 
-    if (fCurrentShellView->TranslateAcceleratorW(pmsg) != S_OK)
-    {
-        if (TranslateAcceleratorSB(pmsg, 0) != S_OK)
-            return S_FALSE;
-        return S_OK;
-    }
-    return S_OK;
+    if (!fCurrentShellView)
+        return S_FALSE;
+
+    return fCurrentShellView->TranslateAcceleratorW(pmsg);
 }
 
 HRESULT STDMETHODCALLTYPE CShellBrowser::_GetBorderDWHelper(IUnknown *punkSrc, LPRECT lprectBorder, BOOL bUseHmonitor)
@@ -3076,27 +3083,6 @@ LRESULT CShellBrowser::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &b
     return 0;
 }
 
-template<class T>
-void ReleaseCComPtrExpectZero(CComPtr<T>& cptr, BOOL forceRelease = FALSE)
-{
-    if (cptr.p != NULL)
-    {
-        int nrc = cptr->Release();
-        if (nrc > 0)
-        {
-            DbgPrint("WARNING: Unexpected RefCount > 0!\n");
-            if (forceRelease)
-            {
-                while (nrc > 0)
-                {
-                    nrc = cptr->Release();
-                }
-            }
-        }
-        cptr.Detach();
-    }
-}
-
 LRESULT CShellBrowser::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
 {
     HRESULT hr;
@@ -3161,12 +3147,40 @@ LRESULT CShellBrowser::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHa
 
 LRESULT CShellBrowser::OnInitMenuPopup(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
 {
-    HMENU                                   theMenu;
+    HMENU  theMenu;
+    LPARAM menuIndex = lParam;
 
     theMenu = reinterpret_cast<HMENU>(wParam);
-    if (theMenu == SHGetMenuFromID(fCurrentMenuBar, FCIDM_MENU_VIEW))
+
+    if (theMenu == SHGetMenuFromID(fCurrentMenuBar, FCIDM_MENU_FILE))
+    {
+        menuIndex = 0;
+    }
+    else if (theMenu == SHGetMenuFromID(fCurrentMenuBar, FCIDM_MENU_EDIT))
+    {
+        menuIndex = 1;
+    }
+    else if (theMenu == SHGetMenuFromID(fCurrentMenuBar, FCIDM_MENU_VIEW))
+    {
         UpdateViewMenu(theMenu);
-    return RelayMsgToShellView(uMsg, wParam, lParam, bHandled);
+        menuIndex = 2;
+    }
+    else if (theMenu == SHGetMenuFromID(fCurrentMenuBar, FCIDM_MENU_FAVORITES))
+    {
+        menuIndex = 3;
+    }
+    else if (theMenu == SHGetMenuFromID(fCurrentMenuBar, FCIDM_MENU_TOOLS))
+    {
+        menuIndex = 4;
+    }
+    else if (theMenu == SHGetMenuFromID(fCurrentMenuBar, FCIDM_MENU_HELP))
+    {
+        menuIndex = 5;
+    }
+
+    LRESULT ret = RelayMsgToShellView(uMsg, wParam, menuIndex, bHandled);
+
+    return ret;
 }
 
 LRESULT CShellBrowser::RelayMsgToShellView(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
@@ -3377,6 +3391,10 @@ static HRESULT ExplorerMessageLoop(IEThreadParamBlock * parameters)
     HRESULT                   hResult;
     MSG Msg;
     BOOL Ret;
+
+    // Tell the thread ref we are using it.
+    if (parameters && parameters->offsetF8)
+        parameters->offsetF8->AddRef();
     
     ATLTRY(theCabinet = new CComObject<CShellBrowser>);
     if (theCabinet == NULL)
@@ -3405,19 +3423,19 @@ static HRESULT ExplorerMessageLoop(IEThreadParamBlock * parameters)
             DispatchMessage(&Msg);
         }
     }
-    
-    //TerminateProcess(GetCurrentProcess(), hResult);
 
     int nrc = theCabinet->Release();
     if (nrc > 0)
     {
-        DbgPrint("WARNING: There are %d references to the CShellBrowser active or leaked, process will never terminate. Terminating forcefully.\n", nrc);
-
-        //TerminateProcess(GetCurrentProcess(), 1);
+        DbgPrint("WARNING: There are %d references to the CShellBrowser active or leaked.\n", nrc);
     }
 
     theCabinet.Detach();
 
+    // Tell the thread ref we are not using it anymore.
+    if (parameters && parameters->offsetF8)
+        parameters->offsetF8->Release();
+
     return hResult;
 }
 
@@ -3425,18 +3443,14 @@ DWORD WINAPI BrowserThreadProc(LPVOID lpThreadParameter)
 {
     HRESULT hr;
     IEThreadParamBlock * parameters = (IEThreadParamBlock *) lpThreadParameter;
-    INITCOMMONCONTROLSEX iccex =
-    {
-        sizeof(iccex),
-        0xFFFF /* everything! */
-    };
 
     OleInitialize(NULL);
-    InitCommonControlsEx(&iccex);
 
     ATLTRY(hr = ExplorerMessageLoop(parameters));
 
     OleUninitialize();
 
+    SHDestroyIETHREADPARAM(parameters);
+
     return hr;
 }