1999-03-23 15:37:34 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
1999-11-06 03:40:37 +00:00
|
|
|
* The contents of this file are subject to the Netscape Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/NPL/
|
1999-03-23 15:37:34 +00:00
|
|
|
*
|
1999-11-06 03:40:37 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1999-03-23 15:37:34 +00:00
|
|
|
*
|
1999-11-06 03:40:37 +00:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1999-03-23 15:37:34 +00:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 03:40:37 +00:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1999-03-23 15:37:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsDragService.h"
|
|
|
|
#include "nsITransferable.h"
|
|
|
|
#include "nsDataObj.h"
|
1999-04-06 20:25:09 +00:00
|
|
|
|
|
|
|
#include "nsWidgetsCID.h"
|
1999-04-17 13:49:39 +00:00
|
|
|
#include "nsNativeDragTarget.h"
|
|
|
|
#include "nsNativeDragSource.h"
|
1999-04-06 20:25:09 +00:00
|
|
|
#include "nsClipboard.h"
|
1999-05-07 19:55:00 +00:00
|
|
|
#include "nsISupportsArray.h"
|
1999-05-26 21:08:21 +00:00
|
|
|
#include "nsDataObjCollection.h"
|
1999-03-23 15:37:34 +00:00
|
|
|
|
1999-04-17 13:49:39 +00:00
|
|
|
#include <OLE2.h>
|
|
|
|
#include "OLEIDL.H"
|
|
|
|
|
1999-03-23 15:37:34 +00:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// DragService constructor
|
|
|
|
//
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
nsDragService::nsDragService()
|
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
1999-04-17 13:49:39 +00:00
|
|
|
mNativeDragTarget = nsnull;
|
|
|
|
mNativeDragSrc = nsnull;
|
|
|
|
mDataObject = nsnull;
|
1999-03-23 15:37:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// DragService destructor
|
|
|
|
//
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
nsDragService::~nsDragService()
|
|
|
|
{
|
1999-04-17 13:49:39 +00:00
|
|
|
NS_IF_RELEASE(mNativeDragSrc);
|
|
|
|
NS_IF_RELEASE(mNativeDragTarget);
|
|
|
|
NS_IF_RELEASE(mDataObject);
|
1999-03-23 15:37:34 +00:00
|
|
|
}
|
|
|
|
|
1999-08-25 08:35:06 +00:00
|
|
|
|
1999-05-07 19:55:00 +00:00
|
|
|
//-------------------------------------------------------------------------
|
2000-05-08 23:43:01 +00:00
|
|
|
NS_IMETHODIMP nsDragService::InvokeDragSession (nsIDOMNode *aDOMNode, nsISupportsArray * anArrayTransferables, nsIScriptableRegion * aRegion, PRUint32 aActionType)
|
1999-05-07 19:55:00 +00:00
|
|
|
{
|
2000-07-31 20:51:42 +00:00
|
|
|
nsBaseDragService::InvokeDragSession ( aDOMNode, anArrayTransferables, aRegion, aActionType );
|
|
|
|
|
1999-05-13 04:56:04 +00:00
|
|
|
nsresult rv;
|
2000-06-05 00:34:11 +00:00
|
|
|
PRUint32 numItemsToDrag = 0;
|
|
|
|
rv = anArrayTransferables->Count(&numItemsToDrag);
|
|
|
|
if ( !numItemsToDrag )
|
1999-05-07 19:55:00 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// The clipboard class contains some static utility methods
|
|
|
|
// that we can use to create an IDataObject from the transferable
|
|
|
|
|
2000-06-05 00:34:11 +00:00
|
|
|
// if we're dragging more than one item, we need to create a "collection" object to fake out
|
|
|
|
// the OS. This collection contains one |IDataObject| for each transerable. If there is just
|
|
|
|
// the one (most cases), only pass around the native |IDataObject|.
|
|
|
|
IDataObject* itemToDrag = nsnull;
|
|
|
|
if ( numItemsToDrag > 1 ) {
|
|
|
|
nsDataObjCollection * dataObjCollection = new nsDataObjCollection();
|
|
|
|
IDataObject* dataObj = nsnull;
|
|
|
|
for ( PRUint32 i=0; i<numItemsToDrag; ++i ) {
|
|
|
|
nsCOMPtr<nsISupports> supports;
|
|
|
|
anArrayTransferables->GetElementAt(i, getter_AddRefs(supports));
|
|
|
|
nsCOMPtr<nsITransferable> trans(do_QueryInterface(supports));
|
|
|
|
if ( trans ) {
|
|
|
|
if ( NS_SUCCEEDED(nsClipboard::CreateNativeDataObject(trans, &dataObj)) ) {
|
|
|
|
dataObjCollection->AddDataObject(dataObj);
|
|
|
|
NS_IF_RELEASE(dataObj);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
itemToDrag = NS_STATIC_CAST ( IDataObject*, dataObjCollection );
|
|
|
|
} // if dragging multiple items
|
|
|
|
else {
|
1999-08-25 08:35:06 +00:00
|
|
|
nsCOMPtr<nsISupports> supports;
|
2000-06-05 00:34:11 +00:00
|
|
|
anArrayTransferables->GetElementAt(0, getter_AddRefs(supports));
|
1999-05-07 19:55:00 +00:00
|
|
|
nsCOMPtr<nsITransferable> trans(do_QueryInterface(supports));
|
1999-08-25 08:35:06 +00:00
|
|
|
if ( trans ) {
|
2000-06-05 00:34:11 +00:00
|
|
|
if ( NS_FAILED(nsClipboard::CreateNativeDataObject(trans, &itemToDrag)) )
|
|
|
|
return NS_ERROR_FAILURE;
|
1999-08-25 08:35:06 +00:00
|
|
|
}
|
2000-06-05 00:34:11 +00:00
|
|
|
} // else dragging a single object
|
|
|
|
|
|
|
|
return StartInvokingDragSession ( itemToDrag, aActionType );
|
1999-05-07 19:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
NS_IMETHODIMP nsDragService::StartInvokingDragSession(IDataObject * aDataObj, PRUint32 aActionType)
|
|
|
|
{
|
1999-04-17 13:49:39 +00:00
|
|
|
// To do the drag we need to create an object that
|
1999-04-23 14:35:26 +00:00
|
|
|
// implements the IDataObject interface (for OLE)
|
1999-04-17 13:49:39 +00:00
|
|
|
NS_IF_RELEASE(mNativeDragSrc);
|
|
|
|
mNativeDragSrc = (IDropSource *)new nsNativeDragSource();
|
2000-06-05 00:34:11 +00:00
|
|
|
if ( !mNativeDragSrc )
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
mNativeDragSrc->AddRef();
|
1999-04-17 13:49:39 +00:00
|
|
|
|
1999-04-23 14:35:26 +00:00
|
|
|
// Now figure out what the native drag effect should be
|
1999-04-17 13:49:39 +00:00
|
|
|
DWORD dropRes;
|
|
|
|
DWORD effects = DROPEFFECT_SCROLL;
|
|
|
|
if (aActionType & DRAGDROP_ACTION_COPY) {
|
|
|
|
effects |= DROPEFFECT_COPY;
|
|
|
|
}
|
|
|
|
if (aActionType & DRAGDROP_ACTION_MOVE) {
|
|
|
|
effects |= DROPEFFECT_MOVE;
|
|
|
|
}
|
|
|
|
if (aActionType & DRAGDROP_ACTION_LINK) {
|
|
|
|
effects |= DROPEFFECT_LINK;
|
|
|
|
}
|
|
|
|
|
2000-06-05 00:34:11 +00:00
|
|
|
mDragAction = aActionType; //XXX not sure why we bother to cache this, it can change during the drag
|
1999-05-07 19:55:00 +00:00
|
|
|
mDoingDrag = PR_TRUE;
|
1999-04-23 14:35:26 +00:00
|
|
|
|
|
|
|
// Call the native D&D method
|
1999-05-07 19:55:00 +00:00
|
|
|
HRESULT res = ::DoDragDrop(aDataObj, mNativeDragSrc, effects, &dropRes);
|
|
|
|
|
|
|
|
mDoingDrag = PR_FALSE;
|
1999-04-17 13:49:39 +00:00
|
|
|
|
|
|
|
return (DRAGDROP_S_DROP == res?NS_OK:NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
1999-05-26 21:08:21 +00:00
|
|
|
NS_IMETHODIMP nsDragService::GetNumDropItems (PRUint32 * aNumItems)
|
|
|
|
{
|
2000-06-05 00:34:11 +00:00
|
|
|
if ( IsCollectionObject(mDataObject) ) {
|
2000-04-26 01:04:13 +00:00
|
|
|
nsDataObjCollection * dataObjCol = NS_STATIC_CAST(nsDataObjCollection*, mDataObject);
|
|
|
|
if ( dataObjCol )
|
|
|
|
*aNumItems = dataObjCol->GetNumDataObjects();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Next check if we have a file drop. Return the number of files in
|
|
|
|
// the file drop as the number of items we have, pretending like we
|
|
|
|
// actually have > 1 drag item.
|
|
|
|
FORMATETC fe2;
|
|
|
|
SET_FORMATETC(fe2, CF_HDROP, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL);
|
2000-05-02 22:36:21 +00:00
|
|
|
if ( mDataObject->QueryGetData(&fe2) == S_OK ) {
|
2000-04-26 01:04:13 +00:00
|
|
|
STGMEDIUM stm;
|
2000-05-02 22:50:28 +00:00
|
|
|
if ( mDataObject->GetData(&fe2, &stm) == S_OK ) {
|
2000-04-26 01:04:13 +00:00
|
|
|
HDROP hdrop = (HDROP) GlobalLock(stm.hGlobal);
|
|
|
|
*aNumItems = ::DragQueryFile(hdrop, 0xFFFFFFFF, NULL, 0);
|
|
|
|
::GlobalUnlock(stm.hGlobal);
|
|
|
|
::ReleaseStgMedium(&stm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*aNumItems = 1;
|
1999-05-26 21:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
NS_IMETHODIMP nsDragService::GetData (nsITransferable * aTransferable, PRUint32 anItem)
|
1999-03-23 15:37:34 +00:00
|
|
|
{
|
1999-04-23 14:35:26 +00:00
|
|
|
// This typcially happens on a drop, the target would be asking
|
|
|
|
// for it's transferable to be filled in
|
1999-05-26 21:08:21 +00:00
|
|
|
// Use a static clipboard utility method for this
|
2000-06-05 00:34:11 +00:00
|
|
|
if ( !mDataObject )
|
1999-05-26 21:08:21 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2000-06-05 00:34:11 +00:00
|
|
|
if ( IsCollectionObject(mDataObject) ) {
|
|
|
|
// multiple items, use |anItem| as an index into our collection
|
|
|
|
nsDataObjCollection * dataObjCol = NS_STATIC_CAST(nsDataObjCollection*, mDataObject);
|
2000-02-01 22:26:21 +00:00
|
|
|
PRUint32 cnt = dataObjCol->GetNumDataObjects();
|
|
|
|
if (anItem >= 0 && anItem < cnt) {
|
|
|
|
IDataObject * dataObj = dataObjCol->GetDataObjectAt(anItem);
|
2000-04-26 01:04:13 +00:00
|
|
|
return nsClipboard::GetDataFromDataObject(dataObj, 0, nsnull, aTransferable);
|
2000-02-01 22:26:21 +00:00
|
|
|
}
|
1999-05-26 21:08:21 +00:00
|
|
|
}
|
2000-06-05 00:34:11 +00:00
|
|
|
else {
|
|
|
|
// Since there is only one object, they better be asking for item "0"
|
|
|
|
if (anItem == 0)
|
|
|
|
return nsClipboard::GetDataFromDataObject(mDataObject, anItem, nsnull, aTransferable);
|
|
|
|
else
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
1999-04-17 13:49:39 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
1999-03-23 15:37:34 +00:00
|
|
|
}
|
|
|
|
|
1999-04-17 13:49:39 +00:00
|
|
|
//---------------------------------------------------------
|
|
|
|
NS_IMETHODIMP nsDragService::SetIDataObject (IDataObject * aDataObj)
|
|
|
|
{
|
1999-05-26 21:08:21 +00:00
|
|
|
// When the native drag starts the DragService gets
|
|
|
|
// the IDataObject that is being dragged
|
1999-04-17 13:49:39 +00:00
|
|
|
NS_IF_RELEASE(mDataObject);
|
|
|
|
mDataObject = aDataObj;
|
2000-10-07 00:11:28 +00:00
|
|
|
NS_IF_ADDREF(mDataObject);
|
1999-04-23 14:35:26 +00:00
|
|
|
|
1999-04-17 13:49:39 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-05-07 19:55:00 +00:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
1999-08-25 08:35:06 +00:00
|
|
|
NS_IMETHODIMP nsDragService::IsDataFlavorSupported(const char *aDataFlavor, PRBool *_retval)
|
1999-05-07 19:55:00 +00:00
|
|
|
{
|
1999-08-25 08:35:06 +00:00
|
|
|
if ( !aDataFlavor || !mDataObject || !_retval )
|
1999-05-07 19:55:00 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2000-02-01 22:26:21 +00:00
|
|
|
#ifdef NS_DEBUG
|
|
|
|
if ( strcmp(aDataFlavor, kTextMime) == 0 )
|
|
|
|
NS_WARNING ( "DO NOT USE THE text/plain DATA FLAVOR ANY MORE. USE text/unicode INSTEAD" );
|
|
|
|
#endif
|
|
|
|
|
1999-08-25 22:04:02 +00:00
|
|
|
*_retval = PR_FALSE;
|
2000-02-01 22:26:21 +00:00
|
|
|
|
1999-05-26 21:08:21 +00:00
|
|
|
FORMATETC fe;
|
2000-06-05 00:34:11 +00:00
|
|
|
UINT format = 0;
|
|
|
|
|
|
|
|
if ( IsCollectionObject(mDataObject) ) {
|
2000-02-01 22:26:21 +00:00
|
|
|
// We know we have one of our special collection objects.
|
1999-08-25 08:35:06 +00:00
|
|
|
format = nsClipboard::GetFormat(aDataFlavor);
|
1999-05-26 21:08:21 +00:00
|
|
|
SET_FORMATETC(fe, format, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL | TYMED_FILE | TYMED_GDI);
|
1999-05-07 19:55:00 +00:00
|
|
|
|
2000-02-01 22:26:21 +00:00
|
|
|
// See if any one of the IDataObjects in the collection supports this data type
|
|
|
|
nsDataObjCollection* dataObjCol = NS_STATIC_CAST(nsDataObjCollection*, mDataObject);
|
|
|
|
if ( dataObjCol ) {
|
|
|
|
PRUint32 cnt = dataObjCol->GetNumDataObjects();
|
|
|
|
for (PRUint32 i=0;i<cnt;++i) {
|
|
|
|
IDataObject * dataObj = dataObjCol->GetDataObjectAt(i);
|
|
|
|
if (S_OK == dataObj->QueryGetData(&fe))
|
|
|
|
*_retval = PR_TRUE; // found it!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // if special collection object
|
|
|
|
else {
|
|
|
|
// Ok, so we have a single object. Check to see if has the correct data type. Since
|
|
|
|
// this can come from an outside app, we also need to see if we need to perform
|
|
|
|
// text->unicode conversion if the client asked for unicode and it wasn't available.
|
|
|
|
format = nsClipboard::GetFormat(aDataFlavor);
|
|
|
|
SET_FORMATETC(fe, format, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL | TYMED_FILE | TYMED_GDI);
|
|
|
|
if ( mDataObject->QueryGetData(&fe) == S_OK )
|
|
|
|
*_retval = PR_TRUE; // found it!
|
|
|
|
else {
|
2000-08-03 00:47:36 +00:00
|
|
|
// We haven't found the exact flavor the client asked for, but maybe we can
|
|
|
|
// still find it from something else that's on the clipboard
|
|
|
|
if ( strcmp(aDataFlavor, kUnicodeMime) == 0 ) {
|
|
|
|
// client asked for unicode and it wasn't present, check if we have CF_TEXT.
|
|
|
|
// We'll handle the actual data substitution in the data object.
|
|
|
|
format = nsClipboard::GetFormat(kTextMime);
|
|
|
|
SET_FORMATETC(fe, format, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL | TYMED_FILE | TYMED_GDI);
|
|
|
|
if ( mDataObject->QueryGetData(&fe) == S_OK )
|
|
|
|
*_retval = PR_TRUE; // found it!
|
|
|
|
}
|
|
|
|
else if ( strcmp(aDataFlavor, kURLMime) == 0 ) {
|
|
|
|
// client asked for a url and it wasn't present, but if we have a file, then
|
|
|
|
// we have a URL to give them (the path, or the internal URL if an InternetShortcut).
|
|
|
|
format = nsClipboard::GetFormat(kFileMime);
|
|
|
|
SET_FORMATETC(fe, format, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL | TYMED_FILE | TYMED_GDI);
|
|
|
|
if ( mDataObject->QueryGetData(&fe) == S_OK )
|
|
|
|
*_retval = PR_TRUE; // found it!
|
|
|
|
}
|
2000-02-01 22:26:21 +00:00
|
|
|
} // else try again
|
1999-05-07 19:55:00 +00:00
|
|
|
}
|
|
|
|
|
1999-08-25 22:04:02 +00:00
|
|
|
return NS_OK;
|
1999-05-07 19:55:00 +00:00
|
|
|
}
|
|
|
|
|
2000-06-05 00:34:11 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// IsCollectionObject
|
|
|
|
//
|
|
|
|
// Determine if this is a single |IDataObject| or one of our private collection
|
|
|
|
// objects. We know the difference because our collection object will respond to supporting
|
|
|
|
// the private |MULTI_MIME| format.
|
|
|
|
//
|
|
|
|
PRBool
|
|
|
|
nsDragService :: IsCollectionObject ( IDataObject* inDataObj )
|
|
|
|
{
|
|
|
|
PRBool isCollection = PR_FALSE;
|
|
|
|
|
|
|
|
// setup the format object to ask for the MULTI_MIME format. We only need to do this once
|
|
|
|
static UINT sFormat = 0;
|
|
|
|
static FORMATETC sFE;
|
|
|
|
if ( !sFormat ) {
|
|
|
|
sFormat = nsClipboard::GetFormat(MULTI_MIME);
|
|
|
|
SET_FORMATETC(sFE, sFormat, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ask the object if it supports it. If yes, we have a collection object
|
|
|
|
if ( inDataObj->QueryGetData(&sFE) == S_OK )
|
|
|
|
isCollection = PR_TRUE;
|
|
|
|
|
|
|
|
return isCollection;
|
|
|
|
|
2000-06-16 00:32:17 +00:00
|
|
|
} // IsCollectionObject
|
2000-10-07 00:11:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// EndDragSession
|
|
|
|
//
|
|
|
|
// Override the default to make sure that we release the data object when the drag ends. It
|
|
|
|
// seems that OLE doesn't like to let apps quit w/out crashing when we're still holding onto
|
|
|
|
// their data
|
|
|
|
//
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDragService::EndDragSession ()
|
|
|
|
{
|
|
|
|
nsBaseDragService::EndDragSession();
|
|
|
|
NS_IF_RELEASE(mDataObject);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|