Back to index

lightning-sunbird  0.9+nobinonly
nsNativeDragTarget.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include <stdio.h>
00039 #include "nsIDragService.h"
00040 #include "nsWidgetsCID.h"
00041 #include "nsNativeDragTarget.h"
00042 #include "nsDragService.h"
00043 #include "nsIServiceManager.h"
00044 #include "nsIDOMNode.h"
00045 #include "nsCOMPtr.h"
00046 
00047 #include "nsIWidget.h"
00048 #include "nsWindow.h"
00049 
00050 #if (_MSC_VER == 1100)
00051 #define INITGUID
00052 #include "objbase.h"
00053 DEFINE_OLEGUID(IID_IDropTarget, 0x00000122L, 0, 0);
00054 DEFINE_OLEGUID(IID_IUnknown, 0x00000000L, 0, 0);
00055 #endif
00056 
00057 #define DRAG_DEBUG 0
00058 
00059 /* Define Class IDs */
00060 static NS_DEFINE_IID(kCDragServiceCID,  NS_DRAGSERVICE_CID);
00061 
00062 /* Define Interface IDs */
00063 static NS_DEFINE_IID(kIDragServiceIID, NS_IDRAGSERVICE_IID);
00064 
00065 // This is cached for Leave notification
00066 static POINTL gDragLastPoint;
00067 
00068 
00069 
00070 
00071 /*
00072  * class nsNativeDragTarget
00073  */
00074 //-----------------------------------------------------
00075 // construction
00076 //-----------------------------------------------------
00077 nsNativeDragTarget::nsNativeDragTarget(nsIWidget * aWnd)
00078   : m_cRef(0), mWindow(aWnd), mCanMove(PR_TRUE)
00079 {
00080   mHWnd = (HWND)mWindow->GetNativeData(NS_NATIVE_WINDOW);
00081 
00082   /*
00083    * Create/Get the DragService that we have implemented
00084    */
00085   CallGetService(kCDragServiceCID, &mDragService);
00086 }
00087 
00088 
00089 //-----------------------------------------------------
00090 // destruction
00091 //-----------------------------------------------------
00092 nsNativeDragTarget::~nsNativeDragTarget()
00093 {
00094   NS_RELEASE(mDragService);
00095 }
00096 
00097 //-----------------------------------------------------
00098 // IUnknown methods - see iunknown.h for documentation
00099 //-----------------------------------------------------
00100 STDMETHODIMP
00101 nsNativeDragTarget::QueryInterface(REFIID riid, void** ppv)
00102 {
00103   *ppv=NULL;
00104 
00105   if (IID_IUnknown == riid || IID_IDropTarget == riid)
00106     *ppv=this;
00107 
00108   if (NULL!=*ppv) {
00109     ((LPUNKNOWN)*ppv)->AddRef();
00110     return NOERROR;
00111   }
00112 
00113   return ResultFromScode(E_NOINTERFACE);
00114 }
00115 
00116 
00117 //-----------------------------------------------------
00118 STDMETHODIMP_(ULONG)
00119 nsNativeDragTarget::AddRef(void)
00120 {
00121   ++m_cRef;
00122   NS_LOG_ADDREF(this, m_cRef, "nsNativeDragTarget", sizeof(*this));
00123   return m_cRef;
00124 }
00125 
00126 //-----------------------------------------------------
00127 STDMETHODIMP_(ULONG) nsNativeDragTarget::Release(void)
00128 {
00129   --m_cRef;
00130   NS_LOG_RELEASE(this, m_cRef, "nsNativeDragTarget");
00131   if (0 != m_cRef)
00132     return m_cRef;
00133 
00134   delete this;
00135   return 0;
00136 }
00137 
00138 
00139 //-----------------------------------------------------
00140 void
00141 nsNativeDragTarget::GetGeckoDragAction(LPDATAOBJECT pData, DWORD grfKeyState,
00142                                        LPDWORD pdwEffect,
00143                                        PRUint32 * aGeckoAction)
00144 {
00145   // Check if we can link from this data object as well.
00146   PRBool canLink = PR_FALSE;
00147   if (pData)
00148     canLink = (S_OK == ::OleQueryLinkFromData(pData) ? PR_TRUE : PR_FALSE);
00149 
00150   // Default is move if we can, in fact drop here,
00151   // and if the drop source supports a move operation.
00152   if (mCanMove) {
00153     *pdwEffect    = DROPEFFECT_MOVE;
00154     *aGeckoAction = nsIDragService::DRAGDROP_ACTION_MOVE;
00155   } else {
00156     *aGeckoAction = nsIDragService::DRAGDROP_ACTION_COPY;
00157     *pdwEffect    = DROPEFFECT_COPY;
00158   }
00159 
00160   // Given the key modifiers figure out what state we are in for both
00161   // the native system and Gecko
00162   if (grfKeyState & MK_CONTROL) {
00163     if (canLink && (grfKeyState & MK_SHIFT)) {
00164       *aGeckoAction = nsIDragService::DRAGDROP_ACTION_LINK;
00165       *pdwEffect    = DROPEFFECT_LINK;
00166     } else {
00167       *aGeckoAction = nsIDragService::DRAGDROP_ACTION_COPY;
00168       *pdwEffect    = DROPEFFECT_COPY;
00169     }
00170   }
00171 }
00172 
00173 
00174 inline
00175 PRBool
00176 IsKeyDown(char key)
00177 {
00178   return GetKeyState(key) < 0;
00179 }
00180 
00181 
00182 //-----------------------------------------------------
00183 void
00184 nsNativeDragTarget::DispatchDragDropEvent(PRUint32 aEventType, POINTL aPT)
00185 {
00186   nsEventStatus status;
00187   nsMouseEvent event(PR_TRUE, aEventType, mWindow, nsMouseEvent::eReal);
00188 
00189   nsWindow * win = NS_STATIC_CAST(nsWindow *, mWindow);
00190   win->InitEvent(event);
00191   POINT cpos;
00192 
00193   cpos.x = aPT.x;
00194   cpos.y = aPT.y;
00195 
00196   if (mHWnd != NULL) {
00197     ::ScreenToClient(mHWnd, &cpos);
00198     event.point.x = cpos.x;
00199     event.point.y = cpos.y;
00200   } else {
00201     event.point.x = 0;
00202     event.point.y = 0;
00203   }
00204 
00205   event.isShift   = IsKeyDown(NS_VK_SHIFT);
00206   event.isControl = IsKeyDown(NS_VK_CONTROL);
00207   event.isMeta    = PR_FALSE;
00208   event.isAlt     = IsKeyDown(NS_VK_ALT);
00209 
00210   mWindow->DispatchEvent(&event, status);
00211 }
00212 
00213 //-----------------------------------------------------
00214 void
00215 nsNativeDragTarget::ProcessDrag(LPDATAOBJECT pData,
00216                                 PRUint32     aEventType,
00217                                 DWORD        grfKeyState,
00218                                 POINTL       pt,
00219                                 DWORD*       pdwEffect)
00220 {
00221   // Before dispatching the event make sure we have the correct drop action set
00222   PRUint32 geckoAction;
00223   GetGeckoDragAction(pData, grfKeyState, pdwEffect, &geckoAction);
00224 
00225   // Set the current action into the Gecko specific type
00226   nsCOMPtr<nsIDragSession> currSession;
00227   mDragService->GetCurrentSession(getter_AddRefs(currSession));
00228   if (!currSession) {
00229     return;
00230   }
00231 
00232   currSession->SetDragAction(geckoAction);
00233 
00234   // Dispatch the event into Gecko
00235   DispatchDragDropEvent(aEventType, pt);
00236 
00237   // Now get the cached Drag effect from the drag service
00238   // the data memeber should have been set by who ever handled the
00239   // nsGUIEvent or nsIDOMEvent
00240   PRBool canDrop;
00241   currSession->GetCanDrop(&canDrop);
00242   if (!canDrop)
00243     *pdwEffect = DROPEFFECT_NONE;
00244 
00245   // Clear the cached value
00246   currSession->SetCanDrop(PR_FALSE);
00247 }
00248 
00249 
00250 //-----------------------------------------------------
00251 // IDropTarget methods
00252 //-----------------------------------------------------
00253 
00254 
00255 STDMETHODIMP
00256 nsNativeDragTarget::DragEnter(LPDATAOBJECT pIDataSource,
00257                               DWORD        grfKeyState,
00258                               POINTL       pt,
00259                               DWORD*       pdwEffect)
00260 {
00261   if (DRAG_DEBUG) printf("DragEnter\n");
00262 
00263        if (!mDragService) {
00264               return ResultFromScode(E_FAIL);
00265   }
00266 
00267   // tell the drag service about this drag (it may have come from an
00268   // outside app).
00269   mDragService->StartDragSession();
00270 
00271   // Remember if this operation allows a move.
00272   mCanMove = (*pdwEffect) & DROPEFFECT_MOVE;
00273 
00274   // Set the native data object into drag service
00275   //
00276   // This cast is ok because in the constructor we created a
00277   // the actual implementation we wanted, so we know this is
00278   // a nsDragService. It should be a private interface, though.
00279   nsDragService * winDragService =
00280     NS_STATIC_CAST(nsDragService *, mDragService);
00281   winDragService->SetIDataObject(pIDataSource);
00282 
00283   // Now process the native drag state and then dispatch the event
00284   ProcessDrag(pIDataSource, NS_DRAGDROP_ENTER, grfKeyState, pt, pdwEffect);
00285 
00286   return S_OK;
00287 }
00288 
00289 
00290 //-----------------------------------------------------
00291 STDMETHODIMP
00292 nsNativeDragTarget::DragOver(DWORD   grfKeyState,
00293                              POINTL  pt,
00294                              LPDWORD pdwEffect)
00295 {
00296   if (DRAG_DEBUG) printf("DragOver\n");
00297        if (!mDragService) {
00298               return ResultFromScode(E_FAIL);
00299   }
00300 
00301   // Now process the native drag state and then dispatch the event
00302   ProcessDrag(nsnull, NS_DRAGDROP_OVER, grfKeyState, pt, pdwEffect);
00303   return S_OK;
00304 }
00305 
00306 
00307 //-----------------------------------------------------
00308 STDMETHODIMP
00309 nsNativeDragTarget::DragLeave()
00310 {
00311   if (DRAG_DEBUG) printf("DragLeave\n");
00312 
00313        if (!mDragService) {
00314               return ResultFromScode(E_FAIL);
00315   }
00316 
00317   // dispatch the event into Gecko
00318   DispatchDragDropEvent(NS_DRAGDROP_EXIT, gDragLastPoint);
00319 
00320   nsCOMPtr<nsIDragSession> currentDragSession;
00321   mDragService->GetCurrentSession(getter_AddRefs(currentDragSession));
00322 
00323   if (currentDragSession) {
00324     nsCOMPtr<nsIDOMNode> sourceNode;
00325     currentDragSession->GetSourceNode(getter_AddRefs(sourceNode));
00326 
00327     if (!sourceNode) {
00328       // We're leaving a window while doing a drag that was
00329       // initiated in a different app. End the drag session, since
00330       // we're done with it for now (until the user drags back into
00331       // mozilla).
00332       mDragService->EndDragSession();
00333     }
00334   }
00335 
00336   return S_OK;
00337 }
00338 
00339 
00340 //-----------------------------------------------------
00341 STDMETHODIMP
00342 nsNativeDragTarget::Drop(LPDATAOBJECT pData,
00343                          DWORD        grfKeyState,
00344                          POINTL       aPT,
00345                          LPDWORD      pdwEffect)
00346 {
00347        if (!mDragService) {
00348               return ResultFromScode(E_FAIL);
00349   }
00350 
00351   // Set the native data object into the drag service
00352   //
00353   // This cast is ok because in the constructor we created a
00354   // the actual implementation we wanted, so we know this is
00355   // a nsDragService (but it should still be a private interface)
00356   nsDragService * winDragService =
00357     NS_STATIC_CAST(nsDragService *, mDragService);
00358   winDragService->SetIDataObject(pData);
00359 
00360   // Note: Calling ProcessDrag can destroy us; don't touch members after that.
00361   nsCOMPtr<nsIDragService> serv = mDragService;
00362 
00363   // Now process the native drag state and then dispatch the event
00364   ProcessDrag(pData, NS_DRAGDROP_DROP, grfKeyState, aPT, pdwEffect);
00365 
00366   // tell the drag service we're done with the session
00367   serv->EndDragSession();
00368   return S_OK;
00369 }