[RSHELL]
[reactos.git] / base / shell / rshell / CMenuDeskBar.cpp
index 8007995..673e793 100644 (file)
@@ -28,27 +28,19 @@ WINE_DEFAULT_DEBUG_CHANNEL(CMenuDeskBar);
 const static GUID CGID_MenuDeskBar = { 0x5C9F0A12, 0x959E, 0x11D0, { 0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x08, 0x26, 0x36 } };
 
 extern "C"
-HRESULT CMenuDeskBar_Constructor(REFIID riid, LPVOID *ppv)
+HRESULT WINAPI CMenuDeskBar_Constructor(REFIID riid, LPVOID *ppv)
 {
-    *ppv = NULL;
-
-    CMenuDeskBar * deskbar = new CComObject<CMenuDeskBar>();
-
-    if (!deskbar)
-        return E_OUTOFMEMORY;
-
-    HRESULT hr = deskbar->QueryInterface(riid, ppv);
-
-    if (FAILED(hr))
-        deskbar->Release();
-
-    return hr;
+    return ShellObjectCreator<CMenuDeskBar>(riid, ppv);
 }
 
 CMenuDeskBar::CMenuDeskBar() :
     m_Client(NULL),
+    m_ClientWindow(NULL),
+    m_IconSize(0),
     m_Banner(NULL),
-    m_Shown(FALSE)
+    m_Shown(FALSE),
+    m_ShowFlags(0),
+    m_didAddRef(FALSE)
 {
 }
 
@@ -56,6 +48,27 @@ CMenuDeskBar::~CMenuDeskBar()
 {
 }
 
+LRESULT CMenuDeskBar::_OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
+{
+    if (!m_didAddRef)
+    {
+        this->AddRef();
+        m_didAddRef = TRUE;
+    }
+
+    bHandled = FALSE;
+    return 0;
+}
+
+void CMenuDeskBar::OnFinalMessage(HWND /* hWnd */)
+{
+    if (m_didAddRef)
+    {
+        this->Release();
+        m_didAddRef = FALSE;
+    }
+}
+
 HRESULT STDMETHODCALLTYPE CMenuDeskBar::Initialize(THIS)
 {
     return S_OK;
@@ -79,7 +92,7 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::OnFocusChangeIS(IUnknown *punkObj, BOOL
     CComPtr<IInputObjectSite> ios;
 
     HRESULT hr = m_Client->QueryInterface(IID_PPV_ARG(IInputObjectSite, &ios));
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
     return ios->OnFocusChangeIS(punkObj, fSetFocus);
@@ -127,13 +140,30 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::Exec(const GUID *pguidCmdGroup, DWORD nC
 
 HRESULT STDMETHODCALLTYPE CMenuDeskBar::QueryService(REFGUID guidService, REFIID riid, void **ppvObject)
 {
+    HRESULT hr;
+
     if (IsEqualGUID(guidService, SID_SMenuPopup) ||
         IsEqualGUID(guidService, SID_SMenuBandParent) ||
         IsEqualGUID(guidService, SID_STopLevelBrowser))
     {
-        return this->QueryInterface(riid, ppvObject);
+        hr = this->QueryInterface(riid, ppvObject);
+        if (SUCCEEDED(hr))
+            return hr;
+    }
+
+    if (IsEqualGUID(guidService, SID_SMenuBandBottom) ||
+        IsEqualGUID(guidService, SID_SMenuBandBottomSelected) ||
+        IsEqualGUID(guidService, SID_SMenuBandChild))
+    {
+        if (m_Client == NULL)
+            return E_NOINTERFACE;
+
+        hr = IUnknown_QueryService(m_Client, guidService, riid, ppvObject);
+        if (SUCCEEDED(hr))
+            return hr;
     }
 
+
     if (m_Site == NULL)
         return E_NOINTERFACE;
 
@@ -150,7 +180,7 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::HasFocusIO()
     CComPtr<IInputObject> io;
 
     HRESULT hr = m_Client->QueryInterface(IID_PPV_ARG(IInputObject, &io));
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
     return io->HasFocusIO();
@@ -161,7 +191,7 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::TranslateAcceleratorIO(LPMSG lpMsg)
     CComPtr<IInputObject> io;
 
     HRESULT hr = m_Client->QueryInterface(IID_PPV_ARG(IInputObject, &io));
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
     return io->TranslateAcceleratorIO(lpMsg);
@@ -172,7 +202,17 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::SetClient(IUnknown *punkClient)
     CComPtr<IDeskBarClient> pDeskBandClient;
     HRESULT hr;
 
-    m_Client.Release();
+    if (m_Client)
+    {
+        hr = m_Client->QueryInterface(IID_PPV_ARG(IDeskBarClient, &pDeskBandClient));
+        if (FAILED_UNEXPECTEDLY(hr))
+            return hr;
+
+        pDeskBandClient->SetDeskBarSite(NULL);
+        
+        pDeskBandClient = NULL;
+        m_Client = NULL;
+    }
 
     if (punkClient == NULL)
         return S_OK;
@@ -183,15 +223,15 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::SetClient(IUnknown *punkClient)
     }
 
     hr = punkClient->QueryInterface(IID_PPV_ARG(IUnknown, &m_Client));
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
     hr = m_Client->QueryInterface(IID_PPV_ARG(IDeskBarClient, &pDeskBandClient));
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
     hr = pDeskBandClient->SetDeskBarSite(static_cast<IDeskBar*>(this));
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
     return IUnknown_GetWindow(m_Client, &m_ClientWindow);
@@ -223,9 +263,19 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::SetSite(IUnknown *pUnkSite)
     if (m_Shown)
         _CloseBar();
 
+    m_SubMenuParent = NULL;
+
     m_Site = pUnkSite;
 
-    IUnknown_QueryService(m_Site, SID_SMenuPopup, IID_PPV_ARG(IMenuPopup, &m_SubMenuParent));
+    if (m_Site)
+    {
+        IUnknown_QueryService(m_Site, SID_SMenuPopup, IID_PPV_ARG(IMenuPopup, &m_SubMenuParent));
+    }
+    else
+    {
+        SetClient(NULL);
+        DestroyWindow();
+    }
 
     return S_OK;
 }
@@ -250,11 +300,11 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::Popup(POINTL *ppt, RECTL *prcExclude, MP
         return E_FAIL;
 
     hr = IUnknown_QueryService(m_Client, SID_SMenuBandChild, IID_PPV_ARG(IOleCommandTarget, &oct));
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
     hr = m_Client->QueryInterface(IID_PPV_ARG(IDeskBarClient, &dbc));
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
     // Windows calls this, but it appears to be unimplemented?
@@ -263,12 +313,12 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::Popup(POINTL *ppt, RECTL *prcExclude, MP
 
     // No clue about the arg, using anything != 0
     hr = dbc->UIActivateDBC(TRUE);
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
     RECT rc = { 0 };
     hr = dbc->GetSize(0, &rc);
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
     // Unknown meaning
@@ -276,7 +326,7 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::Popup(POINTL *ppt, RECTL *prcExclude, MP
     const int CMD_EXEC_OPT = 0;
 
     hr = IUnknown_QueryServiceExec(m_Client, SID_SMenuBandChild, &CLSID_MenuBand, CMD, CMD_EXEC_OPT, NULL, NULL);
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
     ::AdjustWindowRect(&rc, ::GetWindowLong(m_hWnd, GWL_STYLE), FALSE);
@@ -288,38 +338,33 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::Popup(POINTL *ppt, RECTL *prcExclude, MP
         ::GetObject(m_Banner, sizeof(bm), &bm);
         rc.right += bm.bmWidth;
     }
-    
-    int x, y, cx, cy;
 
     RECT rcWorkArea;
     GetWindowRect(GetDesktopWindow(), &rcWorkArea);
     int waHeight = rcWorkArea.bottom - rcWorkArea.top;
 
+    int x = ppt->x;
+    int y = ppt->y;
+    int cx = rc.right - rc.left;
+    int cy = rc.bottom - rc.top;
+
     switch (dwFlags & 0xFF000000)
     {
     case MPPF_BOTTOM:
         x = ppt->x;
-        cx = rc.right - rc.left;
         y = ppt->y - rc.bottom;
-        cy = rc.bottom - rc.top;
         break;
     case MPPF_RIGHT:
         x = ppt->x + rc.left;
-        cx = rc.right - rc.left;
         y = ppt->y + rc.top;
-        cy = rc.bottom - rc.top;
         break;
     case MPPF_TOP | MPPF_ALIGN_LEFT:
         x = ppt->x - rc.right;
-        cx = rc.right - rc.left;
         y = ppt->y + rc.top;
-        cy = rc.bottom - rc.top;
         break;
     case MPPF_TOP | MPPF_ALIGN_RIGHT:
         x = ppt->x;
-        cx = rc.right - rc.left;
         y = ppt->y + rc.top;
-        cy = rc.bottom - rc.top;
         break;
     }
 
@@ -339,14 +384,12 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::Popup(POINTL *ppt, RECTL *prcExclude, MP
     {
         cy = waHeight;
     }
-    
+
     if (y + cy > rcWorkArea.bottom)
     {
         y = rcWorkArea.bottom - cy;
     }
 
-
-
     this->SetWindowPos(HWND_TOPMOST, x, y, cx, cy, SWP_SHOWWINDOW);
 
     m_ShowFlags = dwFlags;
@@ -359,6 +402,13 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::Popup(POINTL *ppt, RECTL *prcExclude, MP
 
     UIActivateIO(TRUE, NULL);
 
+    if (dwFlags & (MPPF_INITIALSELECT | MPPF_FINALSELECT))
+    {
+        const int CMD_SELECT = 5;
+        int CMD_SELECT_OPTS = dwFlags & MPPF_INITIALSELECT ? 0 : -2;
+        IUnknown_QueryServiceExec(m_Client, SID_SMenuBandChild, &CLSID_MenuBand, CMD_SELECT, CMD_SELECT_OPTS, NULL, NULL);
+    }
+
     return S_OK;
 }
 
@@ -372,7 +422,7 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::SetIconSize(THIS_ DWORD iIcon)
     const int CMD_EXEC_OPT = iIcon ? 0 : 2; // seems to work
 
     hr = IUnknown_QueryServiceExec(m_Client, SID_SMenuBandChild, &CLSID_MenuBand, CMD, CMD_EXEC_OPT, NULL, NULL);
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
     BOOL bHandled;
@@ -417,7 +467,7 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::SetSubMenu(IMenuPopup *pmp, BOOL fSet)
     {
         if (m_SubMenuChild)
         {
-            if (SHIsSameObject(pmp, m_SubMenuChild))
+            if (pmp == m_SubMenuChild)
             {
                 m_SubMenuChild = NULL;
             }
@@ -428,24 +478,28 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::SetSubMenu(IMenuPopup *pmp, BOOL fSet)
 
 HRESULT STDMETHODCALLTYPE CMenuDeskBar::OnSelect(DWORD dwSelectType)
 {
-    /* As far as I can tell, the submenu hierarchy looks like this:
-
-    The DeskBar's Child is the Band it contains.
-    The DeskBar's Parent is the SID_SMenuPopup of the Site.
+    CComPtr<IDeskBar> safeThis = this;
 
-    The Band's Child is the IMenuPopup of the child submenu.
-    The Band's Parent is the SID_SMenuPopup of the Site (the DeskBar).
-
-    When the DeskBar receives a selection event:
-        If it requires closing the window, it will notify the Child (Band) using CancelLevel.
-        If it has to spread upwards (everything but CancelLevel), it will notify the Parent.
-
-    When the Band receives a selection event, this is where it gets fuzzy:
-        In which cases does it call the Parent? Probably not CancelLevel.
-        In which cases does it call the Child?
-        How does it react to calls?
-
-    */
+    /* As far as I can tell, the submenu hierarchy looks like this:
+     *
+     * The DeskBar's Child is the Band it contains.
+     * The DeskBar's Parent is the SID_SMenuPopup of the Site.
+     *
+     * The Band's Child is the IMenuPopup of the child submenu.
+     * The Band's Parent is the SID_SMenuPopup of the Site (the DeskBar).
+     *
+     * When the DeskBar receives a selection event:
+     * If it requires closing the window, it will notify the Child (Band) using CancelLevel.
+     * If it has to spread upwards (everything but CancelLevel), it will notify the Parent.
+     *
+     * When the Band receives a selection event, this is where it gets fuzzy:
+     * In which cases does it call the Parent? Probably not CancelLevel.
+     * In which cases does it call the Child?
+     * How does it react to calls?
+     *
+     */
+
+    CComPtr<IMenuPopup> oldParent = m_SubMenuParent;
 
     switch (dwSelectType)
     {
@@ -461,8 +515,8 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::OnSelect(DWORD dwSelectType)
     case MPOS_SELECTLEFT:
     case MPOS_SELECTRIGHT:
     case MPOS_CHILDTRACKING:
-        if (m_SubMenuParent)
-            return m_SubMenuParent->OnSelect(dwSelectType);
+        if (oldParent)
+            return oldParent->OnSelect(dwSelectType);
         break;
     }
 
@@ -471,27 +525,33 @@ HRESULT STDMETHODCALLTYPE CMenuDeskBar::OnSelect(DWORD dwSelectType)
 
 HRESULT CMenuDeskBar::_CloseBar()
 {
+    CComPtr<IDeskBar> safeThis = this;
     CComPtr<IDeskBarClient> dbc;
     HRESULT hr;
 
     m_Shown = false;
 
+    if (m_SubMenuParent)
+    {
+        m_SubMenuParent->SetSubMenu(this, FALSE);
+    }
+
     if (m_SubMenuChild)
     {
         hr = m_SubMenuChild->OnSelect(MPOS_CANCELLEVEL);
-        if (FAILED(hr))
+        if (FAILED_UNEXPECTEDLY(hr))
             return hr;
     }
 
     hr = m_Client->QueryInterface(IID_PPV_ARG(IDeskBarClient, &dbc));
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
     hr = dbc->UIActivateDBC(FALSE);
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return hr;
 
-    SetWindowPos(m_hWnd, 0, 0, 0, 0, SWP_HIDEWINDOW | SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
+    SetWindowPos(NULL, 0, 0, 0, 0, SWP_HIDEWINDOW | SWP_NOACTIVATE | SWP_NOMOVE);
 
     return UIActivateIO(FALSE, NULL);
 }
@@ -506,7 +566,7 @@ BOOL CMenuDeskBar::_IsSubMenuParent(HWND hwnd)
         CComPtr<IOleWindow> window;
 
         hr = popup->QueryInterface(IID_PPV_ARG(IOleWindow, &window));
-        if (FAILED(hr))
+        if (FAILED_UNEXPECTEDLY(hr))
             return FALSE;
 
         HWND parent;
@@ -552,14 +612,14 @@ LRESULT CMenuDeskBar::_OnNotify(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &b
 
     CComPtr<IWinEventHandler> winEventHandler;
     HRESULT hr = m_Client->QueryInterface(IID_PPV_ARG(IWinEventHandler, &winEventHandler));
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return 0;
 
     if (winEventHandler)
     {
         LRESULT result;
         hr = winEventHandler->OnWinEvent(NULL, uMsg, wParam, lParam, &result);
-        if (FAILED(hr))
+        if (FAILED_UNEXPECTEDLY(hr))
             return 0;
         return result;
     }
@@ -609,19 +669,22 @@ LRESULT CMenuDeskBar::_OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bH
 
 LRESULT CMenuDeskBar::_OnActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
 {
-    if (wParam != 0)
+    // BUG in ReactOS: WM_ACTIVATE/WA_INACTIVE makes no sense with lParam==hWnd
+    if (LOWORD(wParam) != 0 || reinterpret_cast<HWND>(lParam) == m_hWnd)
+    {
         return 0;
+    }
 
     // HACK! I just want it to work !!!
     CComPtr<IDeskBar> db;
     HRESULT hr = IUnknown_QueryService(m_Client, SID_SMenuBandChild, IID_PPV_ARG(IDeskBar, &db));
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return 0;
 
     CComPtr<IUnknown> punk;
 
     hr = db->GetClient(&punk);
-    if (FAILED(hr))
+    if (FAILED_UNEXPECTEDLY(hr))
         return 0;
 
     if (!punk && m_Shown)
@@ -635,9 +698,14 @@ LRESULT CMenuDeskBar::_OnActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL
     return 0;
 }
 
+LRESULT CMenuDeskBar::_OnMouseActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
+{
+    return MA_NOACTIVATE;
+}
+
 LRESULT CMenuDeskBar::_OnAppActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
 {
-    if (wParam == 0)
+    if (wParam == 0 && m_Shown)
     {
         OnSelect(MPOS_FULLCANCEL);
     }