Back to index

lightning-sunbird  0.9+nobinonly
nsBaseDragService.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.org 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 "nsBaseDragService.h"
00039 #include "nsITransferable.h"
00040 
00041 #include "nsIServiceManager.h"
00042 #include "nsITransferable.h"
00043 #include "nsISupportsArray.h"
00044 #include "nsSize.h"
00045 #include "nsIRegion.h"
00046 #include "nsXPCOM.h"
00047 #include "nsISupportsPrimitives.h"
00048 #include "nsCOMPtr.h"
00049 #include "nsIFrame.h"
00050 #include "nsIDocument.h"
00051 #include "nsIContent.h"
00052 #include "nsIPresShell.h"
00053 #include "nsIViewManager.h"
00054 #include "nsIDOMNode.h"
00055 #include "nsPresContext.h"
00056 
00057 
00058 NS_IMPL_ADDREF(nsBaseDragService)
00059 NS_IMPL_RELEASE(nsBaseDragService)
00060 NS_IMPL_QUERY_INTERFACE3(nsBaseDragService, nsIDragService,
00061                          nsIDragService_1_8_BRANCH, nsIDragSession)
00062 
00063 
00064 //-------------------------------------------------------------------------
00065 //
00066 // DragService constructor
00067 //
00068 //-------------------------------------------------------------------------
00069 nsBaseDragService::nsBaseDragService()
00070   : mCanDrop(PR_FALSE), mDoingDrag(PR_FALSE),
00071     mDragAction(DRAGDROP_ACTION_NONE), mTargetSize(0,0), mSuppressLevel(0)
00072 {
00073   nsresult result = NS_NewISupportsArray(getter_AddRefs(mTransArray));
00074   if (NS_FAILED(result)) {
00075     //what do we do? we can't throw!
00076     ;
00077   }
00078 }
00079 
00080 //-------------------------------------------------------------------------
00081 //
00082 // DragService destructor
00083 //
00084 //-------------------------------------------------------------------------
00085 nsBaseDragService::~nsBaseDragService()
00086 {
00087 }
00088 
00089 
00090 //---------------------------------------------------------
00091 NS_IMETHODIMP
00092 nsBaseDragService::SetCanDrop(PRBool aCanDrop)
00093 {
00094   mCanDrop = aCanDrop;
00095   return NS_OK;
00096 }
00097 
00098 //---------------------------------------------------------
00099 NS_IMETHODIMP
00100 nsBaseDragService::GetCanDrop(PRBool * aCanDrop)
00101 {
00102   *aCanDrop = mCanDrop;
00103   return NS_OK;
00104 }
00105 
00106 //---------------------------------------------------------
00107 NS_IMETHODIMP
00108 nsBaseDragService::SetDragAction(PRUint32 anAction)
00109 {
00110   mDragAction = anAction;
00111   return NS_OK;
00112 }
00113 
00114 //---------------------------------------------------------
00115 NS_IMETHODIMP
00116 nsBaseDragService::GetDragAction(PRUint32 * anAction)
00117 {
00118   *anAction = mDragAction;
00119   return NS_OK;
00120 }
00121 
00122 //---------------------------------------------------------
00123 NS_IMETHODIMP
00124 nsBaseDragService::SetTargetSize(nsSize aDragTargetSize)
00125 {
00126   mTargetSize = aDragTargetSize;
00127   return NS_OK;
00128 }
00129 
00130 //---------------------------------------------------------
00131 NS_IMETHODIMP
00132 nsBaseDragService::GetTargetSize(nsSize * aDragTargetSize)
00133 {
00134   *aDragTargetSize = mTargetSize;
00135   return NS_OK;
00136 }
00137 
00138 //-------------------------------------------------------------------------
00139 
00140 NS_IMETHODIMP
00141 nsBaseDragService::GetNumDropItems(PRUint32 * aNumItems)
00142 {
00143   *aNumItems = 0;
00144   return NS_ERROR_FAILURE;
00145 }
00146 
00147 
00148 //
00149 // GetSourceDocument
00150 //
00151 // Returns the DOM document where the drag was initiated. This will be
00152 // nsnull if the drag began outside of our application.
00153 //
00154 NS_IMETHODIMP
00155 nsBaseDragService::GetSourceDocument(nsIDOMDocument** aSourceDocument)
00156 {
00157   *aSourceDocument = mSourceDocument.get();
00158   NS_IF_ADDREF(*aSourceDocument);
00159 
00160   return NS_OK;
00161 }
00162 
00163 //
00164 // GetSourceNode
00165 //
00166 // Returns the DOM node where the drag was initiated. This will be
00167 // nsnull if the drag began outside of our application.
00168 //
00169 NS_IMETHODIMP
00170 nsBaseDragService::GetSourceNode(nsIDOMNode** aSourceNode)
00171 {
00172   *aSourceNode = mSourceNode.get();
00173   NS_IF_ADDREF(*aSourceNode);
00174 
00175   return NS_OK;
00176 }
00177 
00178 
00179 //-------------------------------------------------------------------------
00180 
00181 NS_IMETHODIMP
00182 nsBaseDragService::GetData(nsITransferable * aTransferable,
00183                            PRUint32 aItemIndex)
00184 {
00185   return NS_ERROR_FAILURE;
00186 }
00187 
00188 //-------------------------------------------------------------------------
00189 NS_IMETHODIMP
00190 nsBaseDragService::IsDataFlavorSupported(const char *aDataFlavor,
00191                                          PRBool *_retval)
00192 {
00193   return NS_ERROR_FAILURE;
00194 }
00195 
00196 
00197 //-------------------------------------------------------------------------
00198 NS_IMETHODIMP
00199 nsBaseDragService::InvokeDragSession(nsIDOMNode *aDOMNode,
00200                                      nsISupportsArray * anArrayTransferables,
00201                                      nsIScriptableRegion * aRegion,
00202                                      PRUint32 aActionType)
00203 {
00204   NS_ENSURE_TRUE(aDOMNode, NS_ERROR_INVALID_ARG);
00205   NS_ENSURE_TRUE(mSuppressLevel == 0, NS_ERROR_FAILURE);
00206 
00207   // stash the document of the dom node
00208   aDOMNode->GetOwnerDocument(getter_AddRefs(mSourceDocument));
00209   mSourceNode = aDOMNode;
00210 
00211   // When the mouse goes down, the selection code starts a mouse
00212   // capture. However, this gets in the way of determining drag
00213   // feedback for things like trees because the event coordinates
00214   // are in the wrong coord system. Turn off mouse capture in
00215   // the associated view manager.
00216   nsCOMPtr<nsIContent> contentNode = do_QueryInterface(aDOMNode);
00217   if (contentNode) {
00218     nsIDocument* doc = contentNode->GetCurrentDoc();
00219     if (doc) {
00220       nsIPresShell* presShell = doc->GetShellAt(0);
00221       if (presShell) {
00222         nsIViewManager* vm = presShell->GetViewManager();
00223         if (vm) {
00224           PRBool notUsed;
00225           vm->GrabMouseEvents(nsnull, notUsed);
00226         }
00227       }
00228     }
00229   }
00230 
00231   return NS_OK;
00232 }
00233 
00234 
00235 //-------------------------------------------------------------------------
00236 NS_IMETHODIMP
00237 nsBaseDragService::GetCurrentSession(nsIDragSession ** aSession)
00238 {
00239   if (!aSession)
00240     return NS_ERROR_INVALID_ARG;
00241 
00242   // "this" also implements a drag session, so say we are one but only
00243   // if there is currently a drag going on.
00244   if (!mSuppressLevel && mDoingDrag) {
00245     *aSession = this;
00246     NS_ADDREF(*aSession);      // addRef because we're a "getter"
00247   }
00248   else
00249     *aSession = nsnull;
00250 
00251   return NS_OK;
00252 }
00253 
00254 //-------------------------------------------------------------------------
00255 NS_IMETHODIMP
00256 nsBaseDragService::StartDragSession()
00257 {
00258   if (mDoingDrag) {
00259     return NS_ERROR_FAILURE;
00260   }
00261   mDoingDrag = PR_TRUE;
00262   return NS_OK;
00263 }
00264 
00265 //-------------------------------------------------------------------------
00266 NS_IMETHODIMP
00267 nsBaseDragService::EndDragSession()
00268 {
00269   if (!mDoingDrag) {
00270     return NS_ERROR_FAILURE;
00271   }
00272 
00273   mDoingDrag = PR_FALSE;
00274 
00275   // release the source we've been holding on to.
00276   mSourceDocument = nsnull;
00277   mSourceNode = nsnull;
00278 
00279   return NS_OK;
00280 }
00281 
00282 
00283 //
00284 // GetFrameFromNode
00285 //
00286 // Get the frame for this content node (note: frames are not refcounted).
00287 //
00288 void
00289 nsBaseDragService::GetFrameFromNode(nsIDOMNode* inNode, nsIFrame** outFrame,
00290                                     nsPresContext** outContext)
00291 {
00292   *outFrame = nsnull;
00293   *outContext = nsnull;
00294   if (!inNode || !outContext)
00295     return;
00296 
00297   nsCOMPtr<nsIContent> contentNode = do_QueryInterface(inNode);
00298   if (contentNode) {
00299     nsIDocument* doc = contentNode->GetDocument();
00300     if (doc) {
00301       nsIPresShell *presShell = doc->GetShellAt(0);
00302       if (presShell) {
00303         NS_IF_ADDREF(*outContext = presShell->GetPresContext());
00304         presShell->GetPrimaryFrameFor(contentNode, outFrame);
00305         NS_ASSERTION(*outFrame, "Can't get frame for this dom node");
00306       }
00307     }
00308   }
00309 
00310 } // GetFrameFromNode
00311 
00312 NS_IMETHODIMP
00313 nsBaseDragService::Suppress()
00314 {
00315   EndDragSession();
00316   ++mSuppressLevel;
00317   return NS_OK;
00318 }
00319 
00320 NS_IMETHODIMP
00321 nsBaseDragService::Unsuppress()
00322 {
00323   --mSuppressLevel;
00324   return NS_OK;
00325 }