From f5796b5f7166b7c76a608a6912bd23ea7a023e99 Mon Sep 17 00:00:00 2001 From: "nhotta%netscape.com" Date: Tue, 16 Jul 2002 19:38:08 +0000 Subject: [PATCH] Changed charset conversions for clipboard, D&D, * copy from Mozilla, added fallback to try non system default scripts * copy to Mozilla, check 'styl' and if available use that script instead of using the system's default bug 108029, r=pinkerton, sr=sfraser, a=asa. git-svn-id: svn://10.0.0.236/trunk@125312 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/widget/src/mac/nsClipboard.cpp | 84 +++++++++++++++++++----- mozilla/widget/src/mac/nsDragService.cpp | 81 ++++++++++++++++++----- 2 files changed, 133 insertions(+), 32 deletions(-) diff --git a/mozilla/widget/src/mac/nsClipboard.cpp b/mozilla/widget/src/mac/nsClipboard.cpp index 433e53f42d9..c5e3da250eb 100644 --- a/mozilla/widget/src/mac/nsClipboard.cpp +++ b/mozilla/widget/src/mac/nsClipboard.cpp @@ -60,9 +60,13 @@ #include "nsPrimitiveHelpers.h" #include "nsIImageMac.h" #include "nsMemory.h" +#include "nsMacNativeUnicodeConverter.h" +#include "nsICharsetConverterManager.h" #include "nsCRT.h" #include +#include +#include @@ -152,8 +156,22 @@ nsClipboard :: SetNativeClipboardData ( PRInt32 aWhichClipboard ) char* plainTextData = nsnull; PRUnichar* castedUnicode = NS_REINTERPRET_CAST(PRUnichar*, data); PRInt32 plainTextLen = 0; - nsPrimitiveHelpers::ConvertUnicodeToPlatformPlainText ( castedUnicode, dataSize / 2, &plainTextData, &plainTextLen ); - if ( plainTextData ) { + errCode = nsPrimitiveHelpers::ConvertUnicodeToPlatformPlainText ( castedUnicode, dataSize / 2, &plainTextData, &plainTextLen ); + + // if characters are not mapped from Unicode then try native API to convert to + // available script + if (errCode == NS_ERROR_UENC_NOMAPPING) { + if (plainTextData) { + nsMemory::Free(plainTextData); + plainTextData = nsnull; + } + errCode = nsMacNativeUnicodeConverter::ConvertUnicodetoScript(castedUnicode, + dataSize / 2, + &plainTextData, + &plainTextLen); + } + + if ( NS_SUCCEEDED(errCode) && plainTextData ) { errCode = PutOnClipboard ( 'TEXT', plainTextData, plainTextLen ); nsMemory::Free ( plainTextData ); } @@ -289,21 +307,55 @@ nsClipboard :: GetNativeClipboardData ( nsITransferable * aTransferable, PRInt32 // if we are looking for text/unicode and we fail to find it on the clipboard first, // try again with text/plain. If that is present, convert it to unicode. if ( strcmp(flavorStr, kUnicodeMime) == 0 ) { - loadResult = GetDataOffClipboard ( 'TEXT', &clipboardData, &dataSize ); - if ( NS_SUCCEEDED(loadResult) && clipboardData ) { - const char* castedText = NS_REINTERPRET_CAST(char*, clipboardData); - PRUnichar* convertedText = nsnull; - PRInt32 convertedTextLen = 0; - nsPrimitiveHelpers::ConvertPlatformPlainTextToUnicode ( castedText, dataSize, - &convertedText, &convertedTextLen ); - if ( convertedText ) { - // out with the old, in with the new - nsMemory::Free(clipboardData); - clipboardData = convertedText; - dataSize = convertedTextLen * 2; - dataFound = PR_TRUE; + + // if 'styl' is available, we can get a script of the first run + // and use it for converting 'TEXT' + loadResult = GetDataOffClipboard ( 'styl', &clipboardData, &dataSize ); + if (NS_SUCCEEDED(loadResult) && + clipboardData && + (dataSize >= (sizeof(ScrpSTElement) + 2))) { + StScrpRec *scrpRecP = (StScrpRec *) clipboardData; + ScrpSTElement *styl = scrpRecP->scrpStyleTab; + ScriptCode script = styl ? ::FontToScript(styl->scrpFont) : smCurrentScript; + + // free 'styl' and get 'TEXT' + nsMemory::Free(clipboardData); + loadResult = GetDataOffClipboard ( 'TEXT', &clipboardData, &dataSize ); + if ( NS_SUCCEEDED(loadResult) && clipboardData ) { + PRUnichar* convertedText = nsnull; + PRInt32 convertedTextLen = 0; + errCode = nsMacNativeUnicodeConverter::ConvertScripttoUnicode( + script, + (const char *) clipboardData, + dataSize, + &convertedText, + &convertedTextLen); + if (NS_SUCCEEDED(errCode) && convertedText) { + nsMemory::Free(clipboardData); + clipboardData = convertedText; + dataSize = convertedTextLen * sizeof(PRUnichar); + dataFound = PR_TRUE; + } } - } // if plain text data on clipboard + } + + if (!dataFound) { + loadResult = GetDataOffClipboard ( 'TEXT', &clipboardData, &dataSize ); + if ( NS_SUCCEEDED(loadResult) && clipboardData ) { + const char* castedText = NS_REINTERPRET_CAST(char*, clipboardData); + PRUnichar* convertedText = nsnull; + PRInt32 convertedTextLen = 0; + nsPrimitiveHelpers::ConvertPlatformPlainTextToUnicode ( castedText, dataSize, + &convertedText, &convertedTextLen ); + if ( convertedText ) { + // out with the old, in with the new + nsMemory::Free(clipboardData); + clipboardData = convertedText; + dataSize = convertedTextLen * 2; + dataFound = PR_TRUE; + } + } // if plain text data on clipboard + } } // if looking for text/unicode } // else we try one last ditch effort to find our data diff --git a/mozilla/widget/src/mac/nsDragService.cpp b/mozilla/widget/src/mac/nsDragService.cpp index 5e7f6916b7e..9ff8e0464f4 100644 --- a/mozilla/widget/src/mac/nsDragService.cpp +++ b/mozilla/widget/src/mac/nsDragService.cpp @@ -80,6 +80,8 @@ #include "nsIDOMElement.h" #include "nsIImageMac.h" #include "nsIImage.h" +#include "nsMacNativeUnicodeConverter.h" +#include "nsICharsetConverterManager.h" // we need our own stuff for MacOS because of nsIDragSessionMac. @@ -495,22 +497,55 @@ printf("looking for data in type %s, mac flavor %ld\n", NS_STATIC_CAST(const cha // if we are looking for text/unicode and we fail to find it on the clipboard first, // try again with text/plain. If that is present, convert it to unicode. if ( strcmp(flavorStr, kUnicodeMime) == 0 ) { - if ( ::GetFlavorFlags(mDragRef, itemRef, 'TEXT', &unused) == noErr ) { - nsresult loadResult = ExtractDataFromOS(mDragRef, itemRef, 'TEXT', &dataBuff, &dataSize); - if ( NS_SUCCEEDED(loadResult) && dataBuff ) { - const char* castedText = NS_REINTERPRET_CAST(char*, dataBuff); - PRUnichar* convertedText = nsnull; - PRInt32 convertedTextLen = 0; - nsPrimitiveHelpers::ConvertPlatformPlainTextToUnicode ( castedText, dataSize, - &convertedText, &convertedTextLen ); - if ( convertedText ) { - // out with the old, in with the new - nsMemory::Free(dataBuff); - dataBuff = convertedText; - dataSize = convertedTextLen * 2; - dataFound = PR_TRUE; + if ( ::GetFlavorFlags(mDragRef, itemRef, 'TEXT', &unused) == noErr ) { + + // if 'styl' is available, we can get a script of the first run + // and use it for converting 'TEXT' + nsresult loadResult = ExtractDataFromOS(mDragRef, itemRef, 'styl', &dataBuff, &dataSize); + if (NS_SUCCEEDED(loadResult) && + dataBuff && + (dataSize >= (sizeof(ScrpSTElement) + 2))) { + StScrpRec *scrpRecP = (StScrpRec *) dataBuff; + ScrpSTElement *styl = scrpRecP->scrpStyleTab; + ScriptCode script = styl ? ::FontToScript(styl->scrpFont) : smCurrentScript; + + // free 'styl' and get 'TEXT' + nsMemory::Free(dataBuff); + loadResult = ExtractDataFromOS(mDragRef, itemRef, 'TEXT', &dataBuff, &dataSize); + if ( NS_SUCCEEDED(loadResult) && dataBuff ) { + PRUnichar* convertedText = nsnull; + PRInt32 convertedTextLen = 0; + errCode = nsMacNativeUnicodeConverter::ConvertScripttoUnicode(script, + (const char *) dataBuff, + dataSize, + &convertedText, + &convertedTextLen); + if (NS_SUCCEEDED(errCode) && convertedText) { + nsMemory::Free(dataBuff); + dataBuff = convertedText; + dataSize = convertedTextLen * sizeof(PRUnichar); + dataFound = PR_TRUE; + } } - } // if plain text data on clipboard + } + + if (!dataFound) { + loadResult = ExtractDataFromOS(mDragRef, itemRef, 'TEXT', &dataBuff, &dataSize); + if ( NS_SUCCEEDED(loadResult) && dataBuff ) { + const char* castedText = NS_REINTERPRET_CAST(char*, dataBuff); + PRUnichar* convertedText = nsnull; + PRInt32 convertedTextLen = 0; + nsPrimitiveHelpers::ConvertPlatformPlainTextToUnicode ( castedText, dataSize, + &convertedText, &convertedTextLen ); + if ( convertedText ) { + // out with the old, in with the new + nsMemory::Free(dataBuff); + dataBuff = convertedText; + dataSize = convertedTextLen * 2; + dataFound = PR_TRUE; + } + } // if plain text data on clipboard + } } // if plain text flavor present } // if looking for text/unicode } // else we try one last ditch effort to find our data @@ -765,8 +800,22 @@ nsDragService :: GetDataForFlavor ( nsISupportsArray* inDragItems, DragReference char* plainTextData = nsnull; PRUnichar* castedUnicode = NS_REINTERPRET_CAST(PRUnichar*, *outData); PRInt32 plainTextLen = 0; + nsresult rv = nsPrimitiveHelpers::ConvertUnicodeToPlatformPlainText ( castedUnicode, *outDataSize / 2, &plainTextData, &plainTextLen ); - if ( *outData ) { + // if characters are not mapped from Unicode then try native API to convert to + // available script + if (rv == NS_ERROR_UENC_NOMAPPING) { + if (plainTextData) { + nsMemory::Free(plainTextData); + plainTextData = nsnull; + } + rv = nsMacNativeUnicodeConverter::ConvertUnicodetoScript(castedUnicode, + *outDataSize / sizeof(PRUnichar), + &plainTextData, + &plainTextLen); + } + + if ( plainTextData && *outData ) { nsMemory::Free(*outData); *outData = plainTextData; *outDataSize = plainTextLen;