2000-10-02 22:10:26 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*
|
|
|
|
*
|
2004-04-18 22:01:16 +00:00
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla 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
|
1999-06-28 23:39:35 +00:00
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
2004-04-18 22:01:16 +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-06-28 23:39:35 +00:00
|
|
|
*
|
|
|
|
* The Original Code is the Mozilla OS/2 libraries.
|
|
|
|
*
|
2004-04-18 22:01:16 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* John Fairhurst, <john_fairhurst@iname.com>.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1999
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-06-28 23:39:35 +00:00
|
|
|
*
|
2004-04-18 22:01:16 +00:00
|
|
|
* Contributor(s):
|
2000-10-02 22:10:26 +00:00
|
|
|
* 2000/08/04 Henry Sobotka <sobotka@axess.com> Update from M7
|
|
|
|
* 2000/10/02 IBM Corp. Sync-up to M18 level
|
1999-06-28 23:39:35 +00:00
|
|
|
*
|
2004-04-18 22:01:16 +00:00
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-06-28 23:39:35 +00:00
|
|
|
|
|
|
|
#include "nsClipboard.h"
|
2002-09-03 23:36:13 +00:00
|
|
|
#include "nsXPCOM.h"
|
2000-10-02 22:10:26 +00:00
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsPrimitiveHelpers.h"
|
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "prmem.h"
|
2004-06-10 22:03:27 +00:00
|
|
|
#include "nsIObserverService.h"
|
2004-06-10 22:25:52 +00:00
|
|
|
#include "nsIServiceManager.h"
|
1999-06-28 23:39:35 +00:00
|
|
|
|
2002-11-21 15:50:26 +00:00
|
|
|
#include "nsOS2Uni.h"
|
|
|
|
|
1999-06-28 23:39:35 +00:00
|
|
|
#include <unidef.h> // for UniStrlen
|
|
|
|
|
2002-08-27 03:07:03 +00:00
|
|
|
inline ULONG RegisterClipboardFormat(PCSZ pcszFormat)
|
1999-06-28 23:39:35 +00:00
|
|
|
{
|
2002-08-27 03:07:03 +00:00
|
|
|
ATOM atom = WinFindAtom(WinQuerySystemAtomTable(), pcszFormat);
|
|
|
|
if (!atom) {
|
|
|
|
atom = WinAddAtom(WinQuerySystemAtomTable(), pcszFormat);
|
|
|
|
}
|
|
|
|
return atom;
|
|
|
|
}
|
2000-12-01 00:08:36 +00:00
|
|
|
|
2000-10-02 22:10:26 +00:00
|
|
|
nsClipboard::nsClipboard() : nsBaseClipboard()
|
2000-12-01 00:08:36 +00:00
|
|
|
{
|
2002-08-27 03:07:03 +00:00
|
|
|
RegisterClipboardFormat(kTextMime);
|
|
|
|
RegisterClipboardFormat(kUnicodeMime);
|
|
|
|
RegisterClipboardFormat(kHTMLMime);
|
|
|
|
RegisterClipboardFormat(kAOLMailMime);
|
|
|
|
RegisterClipboardFormat(kPNGImageMime);
|
|
|
|
RegisterClipboardFormat(kJPEGImageMime);
|
|
|
|
RegisterClipboardFormat(kGIFImageMime);
|
|
|
|
RegisterClipboardFormat(kFileMime);
|
|
|
|
RegisterClipboardFormat(kURLMime);
|
|
|
|
RegisterClipboardFormat(kNativeImageMime);
|
|
|
|
RegisterClipboardFormat(kNativeHTMLMime);
|
2004-06-10 20:39:11 +00:00
|
|
|
|
|
|
|
// Register for a shutdown notification so that we can flush data
|
|
|
|
// to the OS clipboard.
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
do_GetService("@mozilla.org/observer-service;1");
|
|
|
|
if (observerService)
|
|
|
|
observerService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, PR_FALSE);
|
2000-12-01 00:08:36 +00:00
|
|
|
}
|
1999-06-28 23:39:35 +00:00
|
|
|
|
|
|
|
nsClipboard::~nsClipboard()
|
|
|
|
{}
|
|
|
|
|
2004-06-10 20:39:11 +00:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED1(nsClipboard, nsBaseClipboard, nsIObserver)
|
|
|
|
|
2000-10-02 22:10:26 +00:00
|
|
|
nsresult nsClipboard::SetNativeClipboardData(PRInt32 aWhichClipboard)
|
1999-06-28 23:39:35 +00:00
|
|
|
{
|
2000-10-02 22:10:26 +00:00
|
|
|
if (aWhichClipboard != kGlobalClipboard)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return DoClipboardAction(Write);
|
1999-06-28 23:39:35 +00:00
|
|
|
}
|
|
|
|
|
2000-10-02 22:10:26 +00:00
|
|
|
nsresult nsClipboard::GetNativeClipboardData(nsITransferable *aTransferable, PRInt32 aWhichClipboard)
|
1999-06-28 23:39:35 +00:00
|
|
|
{
|
2000-10-02 22:10:26 +00:00
|
|
|
// make sure we have a good transferable
|
|
|
|
if (!aTransferable || aWhichClipboard != kGlobalClipboard)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsITransferable *tmp = mTransferable;
|
|
|
|
mTransferable = aTransferable;
|
|
|
|
nsresult rc = DoClipboardAction(Read);
|
|
|
|
mTransferable = tmp;
|
|
|
|
return rc;
|
1999-06-28 23:39:35 +00:00
|
|
|
}
|
|
|
|
|
2000-10-02 22:10:26 +00:00
|
|
|
// Get some data from the clipboard
|
2000-12-01 00:08:36 +00:00
|
|
|
PRBool nsClipboard::GetClipboardData(const char *aFlavor)
|
2000-10-02 22:10:26 +00:00
|
|
|
{
|
2000-12-01 00:08:36 +00:00
|
|
|
ULONG ulFormatID = GetFormatID( aFlavor );
|
|
|
|
|
|
|
|
PRBool found = GetClipboardDataByID( ulFormatID, aFlavor );
|
1999-06-28 23:39:35 +00:00
|
|
|
|
2000-12-01 00:08:36 +00:00
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
if (!strcmp( aFlavor, kUnicodeMime ))
|
|
|
|
{
|
|
|
|
found = GetClipboardDataByID( CF_TEXT, aFlavor );
|
|
|
|
}
|
|
|
|
else if (strstr( aFlavor, "image/" ))
|
|
|
|
{
|
|
|
|
found = GetClipboardDataByID( CF_BITMAP, aFlavor );
|
|
|
|
}
|
|
|
|
}
|
1999-06-28 23:39:35 +00:00
|
|
|
|
2000-12-01 00:08:36 +00:00
|
|
|
return found;
|
|
|
|
}
|
2000-10-02 22:10:26 +00:00
|
|
|
|
2000-12-01 00:08:36 +00:00
|
|
|
PRBool nsClipboard::GetClipboardDataByID(ULONG ulFormatID, const char *aFlavor)
|
|
|
|
{
|
|
|
|
PVOID pDataMem;
|
|
|
|
PRUint32 NumOfBytes;
|
|
|
|
PRBool TempBufAllocated = PR_FALSE;
|
2000-10-02 22:10:26 +00:00
|
|
|
|
2007-07-08 07:08:04 +00:00
|
|
|
PVOID pClipboardData = reinterpret_cast<PVOID>(WinQueryClipbrdData( 0, ulFormatID ));
|
2000-10-02 22:10:26 +00:00
|
|
|
|
2000-12-01 00:08:36 +00:00
|
|
|
if (!pClipboardData)
|
|
|
|
return PR_FALSE;
|
2000-11-09 21:34:43 +00:00
|
|
|
|
2000-12-01 00:08:36 +00:00
|
|
|
if (strstr( aFlavor, "text/" )) // All text/.. flavors are null-terminated
|
|
|
|
{
|
|
|
|
pDataMem = pClipboardData;
|
2000-11-09 21:34:43 +00:00
|
|
|
|
2000-12-01 00:08:36 +00:00
|
|
|
if (ulFormatID == CF_TEXT) // CF_TEXT is one byte character set
|
|
|
|
{
|
2007-07-08 07:08:04 +00:00
|
|
|
PRUint32 NumOfChars = strlen( static_cast<char*>(pDataMem) );
|
2000-12-01 00:08:36 +00:00
|
|
|
NumOfBytes = NumOfChars;
|
2000-10-02 22:10:26 +00:00
|
|
|
|
2000-12-01 00:08:36 +00:00
|
|
|
if (!strcmp( aFlavor, kUnicodeMime )) // Asked for unicode, but only plain text available. Convert it!
|
|
|
|
{
|
2004-04-19 14:46:07 +00:00
|
|
|
nsAutoChar16Buffer buffer;
|
|
|
|
PRInt32 bufLength;
|
2007-07-08 07:08:04 +00:00
|
|
|
MultiByteToWideChar(0, static_cast<char*>(pDataMem), NumOfChars,
|
2004-04-19 14:46:07 +00:00
|
|
|
buffer, bufLength);
|
|
|
|
pDataMem = ToNewUnicode(nsDependentString(buffer.get()));
|
2000-12-01 00:08:36 +00:00
|
|
|
TempBufAllocated = PR_TRUE;
|
2004-04-19 14:46:07 +00:00
|
|
|
NumOfBytes = bufLength * sizeof(UniChar);
|
2000-10-02 22:10:26 +00:00
|
|
|
}
|
2000-12-01 00:08:36 +00:00
|
|
|
|
2000-10-02 22:10:26 +00:00
|
|
|
}
|
2000-12-01 00:08:36 +00:00
|
|
|
else // All other text/.. flavors are in unicode
|
2000-11-09 21:34:43 +00:00
|
|
|
{
|
2007-07-08 07:08:04 +00:00
|
|
|
PRUint32 NumOfChars = UniStrlen( static_cast<UniChar*>(pDataMem) );
|
2000-12-01 00:08:36 +00:00
|
|
|
NumOfBytes = NumOfChars * sizeof(UniChar);
|
2004-04-19 14:46:07 +00:00
|
|
|
PVOID pTempBuf = nsMemory::Alloc(NumOfBytes);
|
2001-07-19 22:05:29 +00:00
|
|
|
memcpy(pTempBuf, pDataMem, NumOfBytes);
|
|
|
|
pDataMem = pTempBuf;
|
2004-04-19 14:46:07 +00:00
|
|
|
TempBufAllocated = PR_TRUE;
|
2000-11-09 21:34:43 +00:00
|
|
|
}
|
2001-07-19 22:05:29 +00:00
|
|
|
|
|
|
|
// DOM wants LF only, so convert from CRLF
|
|
|
|
nsLinebreakHelpers::ConvertPlatformToDOMLinebreaks( aFlavor, &pDataMem, // pDataMem could be reallocated !!
|
2007-07-08 07:08:04 +00:00
|
|
|
reinterpret_cast<PRInt32*>(&NumOfBytes) ); // yuck
|
2001-07-19 22:05:29 +00:00
|
|
|
|
2000-12-01 00:08:36 +00:00
|
|
|
}
|
|
|
|
else // Assume rest of flavors are binary data
|
|
|
|
{
|
|
|
|
if (ulFormatID == CF_BITMAP)
|
|
|
|
{
|
|
|
|
if (!strcmp( aFlavor, kJPEGImageMime ))
|
|
|
|
{
|
|
|
|
// OS2TODO Convert bitmap to jpg
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf( "nsClipboard:: No JPG found on clipboard; need to convert BMP\n");
|
2000-10-02 22:10:26 +00:00
|
|
|
#endif
|
2000-12-01 00:08:36 +00:00
|
|
|
}
|
|
|
|
else if (!strcmp( aFlavor, kGIFImageMime ))
|
|
|
|
{
|
|
|
|
// OS2TODO Convert bitmap to gif
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf( "nsClipboard:: No GIF found on clipboard; need to convert BMP\n");
|
2000-10-02 22:10:26 +00:00
|
|
|
#endif
|
2000-12-01 00:08:36 +00:00
|
|
|
}
|
|
|
|
else if (!strcmp( aFlavor, kPNGImageMime ))
|
|
|
|
{
|
|
|
|
// OS2TODO Convert bitmap to png
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf( "nsClipboard:: No PNG found on clipboard; need to convert BMP\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-07-08 07:08:04 +00:00
|
|
|
pDataMem = static_cast<PBYTE>(pClipboardData) + sizeof(PRUint32);
|
|
|
|
NumOfBytes = *(static_cast<PRUint32*>(pClipboardData));
|
2000-10-02 22:10:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-12-01 00:08:36 +00:00
|
|
|
nsCOMPtr<nsISupports> genericDataWrapper;
|
|
|
|
nsPrimitiveHelpers::CreatePrimitiveForData( aFlavor, pDataMem, NumOfBytes, getter_AddRefs(genericDataWrapper) );
|
|
|
|
nsresult errCode = mTransferable->SetTransferData( aFlavor, genericDataWrapper, NumOfBytes );
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (errCode != NS_OK)
|
|
|
|
printf( "nsClipboard:: Error setting data into transferable\n" );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (TempBufAllocated)
|
|
|
|
nsMemory::Free(pDataMem);
|
2000-10-02 22:10:26 +00:00
|
|
|
|
2000-12-01 00:08:36 +00:00
|
|
|
return PR_TRUE;
|
1999-06-28 23:39:35 +00:00
|
|
|
}
|
|
|
|
|
2000-12-01 00:08:36 +00:00
|
|
|
|
1999-06-28 23:39:35 +00:00
|
|
|
// Set some data onto the clipboard
|
2000-12-01 00:08:36 +00:00
|
|
|
void nsClipboard::SetClipboardData(const char *aFlavor)
|
2000-10-02 22:10:26 +00:00
|
|
|
{
|
|
|
|
void *pMozData = nsnull;
|
2000-12-01 00:08:36 +00:00
|
|
|
PRUint32 NumOfBytes = 0;
|
2000-10-02 22:10:26 +00:00
|
|
|
|
|
|
|
// Get the data from the transferable
|
|
|
|
nsCOMPtr<nsISupports> genericDataWrapper;
|
2000-12-01 00:08:36 +00:00
|
|
|
nsresult errCode = mTransferable->GetTransferData( aFlavor, getter_AddRefs(genericDataWrapper), &NumOfBytes );
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (NS_FAILED(errCode)) printf( "nsClipboard:: Error getting data from transferable\n" );
|
1999-06-28 23:39:35 +00:00
|
|
|
#endif
|
2000-12-01 00:08:36 +00:00
|
|
|
if (NumOfBytes == 0) return;
|
|
|
|
nsPrimitiveHelpers::CreateDataFromPrimitive( aFlavor, genericDataWrapper, &pMozData, NumOfBytes );
|
2000-10-02 22:10:26 +00:00
|
|
|
|
2004-06-28 17:28:47 +00:00
|
|
|
/* If creating the data failed, just return */
|
|
|
|
if (!pMozData) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-12-01 00:08:36 +00:00
|
|
|
ULONG ulFormatID = GetFormatID( aFlavor );
|
2000-10-02 22:10:26 +00:00
|
|
|
|
2000-12-01 00:08:36 +00:00
|
|
|
if (strstr( aFlavor, "text/" )) // All text/.. flavors are null-terminated
|
|
|
|
{
|
|
|
|
if (ulFormatID == CF_TEXT) // CF_TEXT is one byte character set
|
2000-11-09 21:34:43 +00:00
|
|
|
{
|
2000-12-01 00:08:36 +00:00
|
|
|
char* pByteMem = nsnull;
|
2000-11-09 21:34:43 +00:00
|
|
|
|
2007-07-08 07:08:04 +00:00
|
|
|
if (DosAllocSharedMem( reinterpret_cast<PPVOID>(&pByteMem), nsnull, NumOfBytes + sizeof(char),
|
2000-12-01 00:08:36 +00:00
|
|
|
PAG_WRITE | PAG_COMMIT | OBJ_GIVEABLE ) == NO_ERROR)
|
|
|
|
{
|
|
|
|
memcpy( pByteMem, pMozData, NumOfBytes ); // Copy text string
|
|
|
|
pByteMem[NumOfBytes] = '\0'; // Append terminator
|
2000-11-09 21:34:43 +00:00
|
|
|
|
2004-03-30 16:14:13 +00:00
|
|
|
// Don't copy text larger than 64K to the clipboard
|
|
|
|
if (strlen(pByteMem) <= 0xFFFF) {
|
2007-07-08 07:08:04 +00:00
|
|
|
WinSetClipbrdData( 0, reinterpret_cast<ULONG>(pByteMem), ulFormatID, CFI_POINTER );
|
2004-03-30 16:14:13 +00:00
|
|
|
} else {
|
|
|
|
WinAlarm(HWND_DESKTOP, WA_ERROR);
|
|
|
|
}
|
2000-12-01 00:08:36 +00:00
|
|
|
}
|
2000-11-09 21:34:43 +00:00
|
|
|
}
|
2000-12-01 00:08:36 +00:00
|
|
|
else // All other text/.. flavors are in unicode
|
|
|
|
{
|
|
|
|
UniChar* pUnicodeMem = nsnull;
|
|
|
|
PRUint32 NumOfChars = NumOfBytes / sizeof(UniChar);
|
|
|
|
|
2007-07-08 07:08:04 +00:00
|
|
|
if (DosAllocSharedMem( reinterpret_cast<PPVOID>(&pUnicodeMem), nsnull, NumOfBytes + sizeof(UniChar),
|
2000-12-01 00:08:36 +00:00
|
|
|
PAG_WRITE | PAG_COMMIT | OBJ_GIVEABLE ) == NO_ERROR)
|
|
|
|
{
|
|
|
|
memcpy( pUnicodeMem, pMozData, NumOfBytes ); // Copy text string
|
|
|
|
pUnicodeMem[NumOfChars] = L'\0'; // Append terminator
|
|
|
|
|
2007-07-08 07:08:04 +00:00
|
|
|
WinSetClipbrdData( 0, reinterpret_cast<ULONG>(pUnicodeMem), ulFormatID, CFI_POINTER );
|
2000-12-01 00:08:36 +00:00
|
|
|
}
|
2000-10-02 22:10:26 +00:00
|
|
|
|
2000-12-01 00:08:36 +00:00
|
|
|
// If the flavor is unicode, we also put it on the clipboard as CF_TEXT
|
|
|
|
// after conversion to locale charset.
|
2000-10-02 22:10:26 +00:00
|
|
|
|
2000-12-01 00:08:36 +00:00
|
|
|
if (!strcmp( aFlavor, kUnicodeMime ))
|
|
|
|
{
|
|
|
|
char* pByteMem = nsnull;
|
2000-10-02 22:10:26 +00:00
|
|
|
|
2007-07-08 07:08:04 +00:00
|
|
|
if (DosAllocSharedMem(reinterpret_cast<PPVOID>(&pByteMem), nsnull,
|
2004-04-19 14:46:07 +00:00
|
|
|
NumOfBytes + 1,
|
|
|
|
PAG_WRITE | PAG_COMMIT | OBJ_GIVEABLE ) == NO_ERROR)
|
2000-12-01 00:08:36 +00:00
|
|
|
{
|
2003-08-25 22:31:13 +00:00
|
|
|
PRUnichar* uchtemp = (PRUnichar*)pMozData;
|
2004-04-23 19:08:41 +00:00
|
|
|
for (PRUint32 i=0;i<NumOfChars;i++) {
|
2003-08-25 22:31:13 +00:00
|
|
|
switch (uchtemp[i]) {
|
|
|
|
case 0x2018:
|
|
|
|
case 0x2019:
|
|
|
|
uchtemp[i] = 0x0027;
|
|
|
|
break;
|
|
|
|
case 0x201C:
|
|
|
|
case 0x201D:
|
|
|
|
uchtemp[i] = 0x0022;
|
|
|
|
break;
|
|
|
|
case 0x2014:
|
|
|
|
uchtemp[i] = 0x002D;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2000-10-02 22:10:26 +00:00
|
|
|
|
2004-04-19 14:46:07 +00:00
|
|
|
nsAutoCharBuffer buffer;
|
|
|
|
PRInt32 bufLength;
|
2007-07-08 07:08:04 +00:00
|
|
|
WideCharToMultiByte(0, static_cast<PRUnichar*>(pMozData),
|
2004-04-19 14:46:07 +00:00
|
|
|
NumOfBytes, buffer, bufLength);
|
|
|
|
memcpy(pByteMem, buffer.get(), NumOfBytes);
|
2004-03-30 16:14:13 +00:00
|
|
|
// Don't copy text larger than 64K to the clipboard
|
|
|
|
if (strlen(pByteMem) <= 0xFFFF) {
|
2007-07-08 07:08:04 +00:00
|
|
|
WinSetClipbrdData(0, reinterpret_cast<ULONG>(pByteMem), CF_TEXT,
|
2004-04-19 14:46:07 +00:00
|
|
|
CFI_POINTER);
|
2004-03-30 16:14:13 +00:00
|
|
|
} else {
|
|
|
|
WinAlarm(HWND_DESKTOP, WA_ERROR);
|
|
|
|
}
|
2000-12-01 00:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else // Assume rest of flavors are binary data
|
|
|
|
{
|
|
|
|
PBYTE pBinaryMem = nsnull;
|
2000-10-02 22:10:26 +00:00
|
|
|
|
2007-07-08 07:08:04 +00:00
|
|
|
if (DosAllocSharedMem( reinterpret_cast<PPVOID>(&pBinaryMem), nsnull, NumOfBytes + sizeof(PRUint32),
|
2000-12-01 00:08:36 +00:00
|
|
|
PAG_WRITE | PAG_COMMIT | OBJ_GIVEABLE ) == NO_ERROR)
|
|
|
|
{
|
2007-07-08 07:08:04 +00:00
|
|
|
*(reinterpret_cast<PRUint32*>(pBinaryMem)) = NumOfBytes; // First DWORD contains data length
|
2000-12-01 00:08:36 +00:00
|
|
|
memcpy( pBinaryMem + sizeof(PRUint32), pMozData, NumOfBytes ); // Copy binary data
|
2000-10-02 22:10:26 +00:00
|
|
|
|
2007-07-08 07:08:04 +00:00
|
|
|
WinSetClipbrdData( 0, reinterpret_cast<ULONG>(pBinaryMem), ulFormatID, CFI_POINTER );
|
2000-12-01 00:08:36 +00:00
|
|
|
}
|
2000-10-02 22:10:26 +00:00
|
|
|
|
2000-12-01 00:08:36 +00:00
|
|
|
// If the flavor is image, we also put it on clipboard as CF_BITMAP
|
|
|
|
// after conversion to OS2 bitmap
|
2000-10-02 22:10:26 +00:00
|
|
|
|
2000-12-01 00:08:36 +00:00
|
|
|
if (strstr (aFlavor, "image/"))
|
|
|
|
{
|
|
|
|
// XXX OS2TODO Convert jpg, gif, png to bitmap
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf( "nsClipboard:: Putting image on clipboard; should also convert to BMP\n" );
|
|
|
|
#endif
|
2000-10-02 22:10:26 +00:00
|
|
|
}
|
|
|
|
}
|
2003-08-25 22:31:13 +00:00
|
|
|
nsMemory::Free(pMozData);
|
1999-06-28 23:39:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Go through the flavors in the transferable and either get or set them
|
2000-10-02 22:10:26 +00:00
|
|
|
nsresult nsClipboard::DoClipboardAction(ClipboardAction aAction)
|
|
|
|
{
|
|
|
|
nsresult rc = NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
if (WinOpenClipbrd(0/*hab*/)) {
|
|
|
|
|
|
|
|
if (aAction == Write)
|
|
|
|
WinEmptyClipbrd(0/*hab*/);
|
|
|
|
|
|
|
|
// Get the list of formats the transferable can handle
|
|
|
|
nsCOMPtr<nsISupportsArray> pFormats;
|
|
|
|
if(aAction == Read)
|
|
|
|
rc = mTransferable->FlavorsTransferableCanImport(getter_AddRefs(pFormats));
|
|
|
|
else
|
|
|
|
rc = mTransferable->FlavorsTransferableCanExport(getter_AddRefs(pFormats));
|
1999-06-28 23:39:35 +00:00
|
|
|
|
2000-10-02 22:10:26 +00:00
|
|
|
if (NS_FAILED(rc))
|
|
|
|
return NS_ERROR_FAILURE;
|
1999-06-28 23:39:35 +00:00
|
|
|
|
2000-10-02 22:10:26 +00:00
|
|
|
PRUint32 cFormats = 0;
|
|
|
|
pFormats->Count(&cFormats);
|
|
|
|
|
|
|
|
for (PRUint32 i = 0; i < cFormats; i++) {
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> genericFlavor;
|
|
|
|
pFormats->GetElementAt(i, getter_AddRefs(genericFlavor));
|
2002-08-06 00:53:19 +00:00
|
|
|
nsCOMPtr<nsISupportsCString> currentFlavor(do_QueryInterface(genericFlavor));
|
2000-10-02 22:10:26 +00:00
|
|
|
|
|
|
|
if (currentFlavor) {
|
|
|
|
nsXPIDLCString flavorStr;
|
|
|
|
currentFlavor->ToString(getter_Copies(flavorStr));
|
|
|
|
|
|
|
|
if (aAction == Read) {
|
|
|
|
if (GetClipboardData(flavorStr))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
SetClipboardData(flavorStr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
WinCloseClipbrd(0/*hab*/);
|
|
|
|
rc = NS_OK;
|
|
|
|
}
|
|
|
|
return rc;
|
1999-06-28 23:39:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// get the format ID for a given mimetype
|
2000-12-01 00:08:36 +00:00
|
|
|
ULONG nsClipboard::GetFormatID(const char *aMimeStr)
|
1999-06-28 23:39:35 +00:00
|
|
|
{
|
2002-08-27 03:07:03 +00:00
|
|
|
if (strcmp(aMimeStr, kTextMime) == 0)
|
|
|
|
return CF_TEXT;
|
|
|
|
|
|
|
|
return RegisterClipboardFormat(aMimeStr);
|
2000-10-02 22:10:26 +00:00
|
|
|
}
|
1999-06-28 23:39:35 +00:00
|
|
|
|
2004-06-10 20:39:11 +00:00
|
|
|
// nsIObserver
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsClipboard::Observe(nsISupports *aSubject, const char *aTopic,
|
2004-06-10 20:41:09 +00:00
|
|
|
const PRUnichar *aData)
|
2000-10-02 22:10:26 +00:00
|
|
|
{
|
2004-06-10 20:39:11 +00:00
|
|
|
// This will be called on shutdown.
|
|
|
|
|
2000-10-02 22:10:26 +00:00
|
|
|
// make sure we have a good transferable
|
2004-06-10 22:50:58 +00:00
|
|
|
if (!mTransferable)
|
2000-10-02 22:10:26 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
if (WinOpenClipbrd(0/*hab*/)) {
|
|
|
|
WinEmptyClipbrd(0/*hab*/);
|
|
|
|
|
|
|
|
// get flavor list that includes all flavors that can be written (including ones
|
|
|
|
// obtained through conversion)
|
|
|
|
nsCOMPtr<nsISupportsArray> flavorList;
|
|
|
|
nsresult errCode = mTransferable->FlavorsTransferableCanExport(getter_AddRefs(flavorList));
|
|
|
|
if (NS_FAILED(errCode))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// Walk through flavors and put data on to clipboard
|
|
|
|
PRUint32 i;
|
|
|
|
PRUint32 cnt;
|
|
|
|
flavorList->Count(&cnt);
|
|
|
|
for (i = 0; i < cnt; i++) {
|
|
|
|
nsCOMPtr<nsISupports> genericFlavor;
|
|
|
|
flavorList->GetElementAt(i, getter_AddRefs(genericFlavor));
|
2002-08-06 00:53:19 +00:00
|
|
|
nsCOMPtr<nsISupportsCString> currentFlavor(do_QueryInterface(genericFlavor));
|
2000-10-02 22:10:26 +00:00
|
|
|
if (currentFlavor) {
|
|
|
|
nsXPIDLCString flavorStr;
|
|
|
|
currentFlavor->ToString(getter_Copies(flavorStr));
|
|
|
|
SetClipboardData(flavorStr);
|
1999-06-28 23:39:35 +00:00
|
|
|
}
|
2000-10-02 22:10:26 +00:00
|
|
|
}
|
|
|
|
WinCloseClipbrd(0/*hab*/);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-06-28 23:39:35 +00:00
|
|
|
|
2000-10-02 22:10:26 +00:00
|
|
|
NS_IMETHODIMP nsClipboard::HasDataMatchingFlavors(nsISupportsArray *aFlavorList, PRInt32 aWhichClipboard,
|
|
|
|
PRBool *_retval)
|
|
|
|
{
|
|
|
|
*_retval = PR_FALSE;
|
|
|
|
if (aWhichClipboard != kGlobalClipboard)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
PRUint32 cnt;
|
|
|
|
aFlavorList->Count(&cnt);
|
|
|
|
for (PRUint32 i = 0; i < cnt; ++i) {
|
|
|
|
nsCOMPtr<nsISupports> genericFlavor;
|
|
|
|
aFlavorList->GetElementAt(i, getter_AddRefs(genericFlavor));
|
2002-08-06 00:53:19 +00:00
|
|
|
nsCOMPtr<nsISupportsCString> currentFlavor(do_QueryInterface(genericFlavor));
|
2000-10-02 22:10:26 +00:00
|
|
|
if (currentFlavor) {
|
|
|
|
nsXPIDLCString flavorStr;
|
|
|
|
currentFlavor->ToString(getter_Copies(flavorStr));
|
|
|
|
ULONG fmtInfo = 0;
|
2000-12-01 00:08:36 +00:00
|
|
|
ULONG format = GetFormatID(flavorStr);
|
2000-10-02 22:10:26 +00:00
|
|
|
|
|
|
|
if (WinQueryClipbrdFmtInfo(0/*hab*/, format, &fmtInfo)) {
|
|
|
|
*_retval = PR_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2000-12-01 00:08:36 +00:00
|
|
|
|
|
|
|
// if the client asked for unicode and it wasn't present, check if we have CF_TEXT.
|
|
|
|
if (!strcmp( flavorStr, kUnicodeMime )) {
|
|
|
|
if (WinQueryClipbrdFmtInfo( 0/*hab*/, CF_TEXT, &fmtInfo )) {
|
|
|
|
*_retval = PR_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OS2TODO - Support for Images
|
|
|
|
// if the client asked for image/.. and it wasn't present, check if we have CF_BITMAP.
|
|
|
|
if (strstr (flavorStr, "image/")) {
|
|
|
|
if (WinQueryClipbrdFmtInfo (0, CF_BITMAP, &fmtInfo)) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf( "nsClipboard:: Image present on clipboard; need to add BMP conversion!\n" );
|
|
|
|
#endif
|
|
|
|
// *_retval = PR_TRUE;
|
|
|
|
// break;
|
2000-10-02 22:10:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
1999-06-28 23:39:35 +00:00
|
|
|
}
|