r=pedemont, sr=blizzard (platform specific code)
OS/2 only - convert old Unicode stuff to new APIs
This commit is contained in:
mkaply%us.ibm.com 2002-11-21 15:50:26 +00:00
parent 73076a23a3
commit f4177c62ad
4 changed files with 47 additions and 295 deletions

View File

@ -31,6 +31,8 @@
#include "nsXPIDLString.h"
#include "prmem.h"
#include "nsOS2Uni.h"
#include <unidef.h> // for UniStrlen
inline ULONG RegisterClipboardFormat(PCSZ pcszFormat)
@ -130,7 +132,7 @@ PRBool nsClipboard::GetClipboardDataByID(ULONG ulFormatID, const char *aFlavor)
pTempBuf = nsMemory::Alloc( NumOfBytes + sizeof(UniChar) );
TempBufAllocated = PR_TRUE;
NumOfChars = gWidgetModuleData->ConvertToUcs( NS_STATIC_CAST(char*, pDataMem), NS_STATIC_CAST(PRUnichar*, pTempBuf), NumOfChars + 1 );
NumOfChars = MultiByteToWideChar(0, NS_STATIC_CAST(char*, pDataMem), NumOfChars, NS_STATIC_CAST(PRUnichar*, pTempBuf), NumOfChars);
NumOfBytes = NumOfChars * sizeof(UniChar);
pDataMem = pTempBuf;
}
@ -254,7 +256,7 @@ void nsClipboard::SetClipboardData(const char *aFlavor)
if (DosAllocSharedMem( NS_REINTERPRET_CAST(PPVOID, &pByteMem), nsnull, NumOfBytes + 1,
PAG_WRITE | PAG_COMMIT | OBJ_GIVEABLE ) == NO_ERROR)
{
gWidgetModuleData->ConvertFromUcs( NS_STATIC_CAST(PRUnichar*, pMozData), pByteMem, NumOfBytes + 1 );
WideCharToMultiByte(0, NS_STATIC_CAST(PRUnichar*, pMozData), NumOfBytes, pByteMem, NumOfBytes);
pByteMem [NumOfBytes] = '\0';
WinSetClipbrdData( 0, NS_REINTERPRET_CAST(ULONG, pByteMem), CF_TEXT, CFI_POINTER );

View File

@ -44,7 +44,6 @@
// Module-level data & utility functions:
// * unicode keycode & string conversion
// * atom management
// * printing bits & pieces (though not really any more)
#include "nsDragService.h"
#include "nsAppShell.h"
@ -110,30 +109,17 @@ void nsWidgetModuleData::Init( nsIAppShell *aPrimaevalAppShell)
szScreen.cx = WinQuerySysValue( HWND_DESKTOP, SV_CXSCREEN);
szScreen.cy = WinQuerySysValue( HWND_DESKTOP, SV_CYSCREEN);
bMouseSwitched = WinQuerySysValue( HWND_DESKTOP, SV_SWAPBUTTON);
// Magic number -- height of normal text fields in pixels.
// Both combobox and (atm) nsBrowserWindow depend on this.
lHtEntryfield = 26;
// Work out if the system is DBCS
COUNTRYCODE cc = { 0 };
DosQueryDBCSEnv( CCHMAXPATH, &cc, buffer);
bIsDBCS = buffer[0] || buffer[1];
// XXXX KNOCKED OUT UNTIL nsDragService.cpp builds again
// dragService = new nsDragService;
// NS_ADDREF(dragService);
// keep a ref beyond the client app so we shut ourselves down properly.
// don't do this for embedding where the appshell pointer is nsnull
appshell = aPrimaevalAppShell;
if (appshell != nsnull)
NS_ADDREF(appshell);
converter = 0;
supplantConverter = FALSE;
#if 0
mWindows = nsnull;
#endif
@ -152,9 +138,6 @@ void nsWidgetModuleData::Init( nsIAppShell *aPrimaevalAppShell)
nsWidgetModuleData::~nsWidgetModuleData()
{
if( converter)
UniFreeUconvObject( converter);
NS_IF_RELEASE(dragService);
for (int i=0;i<=16;i++ ) {
@ -168,243 +151,6 @@ nsWidgetModuleData::~nsWidgetModuleData()
NS_IF_RELEASE(appshell);
}
int nsWidgetModuleData::CreateUcsConverter()
{
// Create a converter from unicode to a codepage which PM can display.
UniChar codepage[20];
int unirc = UniMapCpToUcsCp( 0, codepage, 20);
if( unirc == ULS_SUCCESS)
{
unirc = UniCreateUconvObject( codepage, &converter);
// XXX do we need to set substitution options here?
#ifdef DEBUG
if( unirc == ULS_SUCCESS)
{
printf( "Widget library created unicode converter for cp %s\n",
ConvertFromUcs( (PRUnichar *) codepage));
}
#endif
}
#ifdef DEBUG
if( unirc != ULS_SUCCESS)
{
printf( "Couldn't create widget unicode converter.\n");
}
#endif
return unirc;
}
// Conversion from appropriate codepage to unicode
ULONG nsWidgetModuleData::ConvertToUcs( const char *szText,
PRUnichar *pBuffer,
ULONG ulSize)
{
if( supplantConverter)
{
// We couldn't create a converter for some reason, so do this 'by hand'.
// Note this algorithm is fine for most of most western charsets, but
// fails dismally for various glyphs, baltic, points east...
ULONG ulCount = 0;
PRUnichar *pTmp = pBuffer;
while( *szText && ulCount < ulSize - 1) // (one for terminator)
{
*pTmp = (PRUnichar)((SHORT)*szText & 0x00FF);
pTmp++;
szText++;
ulCount++;
}
// terminate string
*pTmp = (PRUnichar)0;
return ulCount;
}
if( !converter)
{
if( CreateUcsConverter() != ULS_SUCCESS)
{
supplantConverter = TRUE;
return ConvertToUcs( szText, pBuffer, ulSize);
}
}
// Have converter, now get it to work...
char *szString = (char *)szText;
size_t szLen = strlen( szText) + 1;
size_t ucsLen = ulSize;
size_t cSubs = 0;
UniChar *tmp = NS_REINTERPRET_CAST(UniChar *,pBuffer); // function alters the out pointer
int unirc = UniUconvToUcs( converter, (void **)&szText, &szLen,
&tmp, &ucsLen, &cSubs);
if( unirc == UCONV_E2BIG) // k3w1
{
// terminate output string (truncating)
*(pBuffer + ulSize - 1) = (PRUnichar)0;
ucsLen = ulSize - 1;
}
else if( unirc != ULS_SUCCESS)
{
#ifdef DEBUG
printf( "UniUconvToUcs failed, rc %X\n", unirc);
#endif
supplantConverter = TRUE;
ucsLen = ConvertToUcs( szText, pBuffer, ulSize);
supplantConverter = FALSE;
}
else
{
// ucsLen is the number of unused Unichars in the output buffer,
// but we want the number of converted unicode characters
ucsLen = ulSize - ucsLen - 1;
*(pBuffer + ucsLen) = (PRUnichar)0;
}
return (ULONG)ucsLen;
}
// Conversion from unicode to appropriate codepage
char *nsWidgetModuleData::ConvertFromUcs( const PRUnichar *pText,
char *szBuffer, ULONG ulSize)
{
if( supplantConverter)
{
// We couldn't create a converter for some reason, so do this 'by hand'.
// Note this algorithm is fine for most of most western charsets, but
// fails dismally for various glyphs, baltic, points east...
ULONG ulCount = 0;
char *szSave = szBuffer;
while( *pText && ulCount < ulSize - 1) // (one for terminator)
{
*szBuffer = (char) *pText;
szBuffer++;
pText++;
ulCount++;
}
// terminate string
*szBuffer = '\0';
return szSave;
}
if( !converter)
{
if( CreateUcsConverter() != ULS_SUCCESS)
{
supplantConverter = TRUE;
return ConvertFromUcs( pText, szBuffer, ulSize);
}
}
// Have converter, now get it to work...
UniChar *ucsString = NS_REINTERPRET_CAST(UniChar *,pText);
size_t ucsLen = UniStrlen( ucsString) + 1;
size_t cplen = ulSize;
size_t cSubs = 0;
char *tmp = szBuffer; // function alters the out pointer
int unirc = UniUconvFromUcs( converter, &ucsString, &ucsLen,
(void**) &tmp, &cplen, &cSubs);
if( unirc == UCONV_E2BIG) // k3w1
{
// terminate output string (truncating)
*(szBuffer + ulSize - 1) = '\0';
}
else if( unirc != ULS_SUCCESS)
{
#ifdef DEBUG
printf( "UniUconvFromUcs failed, rc %X\n", unirc);
#endif
supplantConverter = TRUE;
szBuffer = ConvertFromUcs( pText, szBuffer, ulSize);
supplantConverter = FALSE;
}
return szBuffer;
}
char *nsWidgetModuleData::ConvertFromUcs( const nsString &aString,
char *szBuffer, ULONG ulSize)
{
char *szRet = 0;
const PRUnichar *pUnicode = aString.get();
if( pUnicode)
szRet = ConvertFromUcs( pUnicode, szBuffer, ulSize);
else
szRet = aString.ToCString( szBuffer, ulSize);
return szRet;
}
const char *nsWidgetModuleData::ConvertFromUcs( const PRUnichar *pText)
{
// This is probably okay; longer strings will be truncated but istr there's
// a PM limit on things like windowtext
// (which these routines are usually used for)
static char buffer[1024]; // XXX (multithread)
*buffer = '\0';
return ConvertFromUcs( pText, buffer, 1024);
}
const char *nsWidgetModuleData::ConvertFromUcs( const nsString &aString)
{
return ConvertFromUcs( aString.get());
}
// Printing stuff -- need to be able to generate a window of a given
// flavour and then do stuff to it in nsWindow::Paint()
#if 0
class nsWndClassKey : public nsHashKey
{
PCSZ pszClassname;
public:
nsWndClassKey( PCSZ pszName) : pszClassname( pszName)
{}
PRUint32 HashValue() const
{ return (PRUint32) pszClassname; }
PRBool Equals( const nsHashKey *aKey) const
{ return HashValue() == aKey->HashValue(); }
nsHashKey *Clone() const
{ return new nsWndClassKey( pszClassname); }
};
// Get a window to render its state
HWND nsWidgetModuleData::GetWindowForPrinting( PCSZ pszClass, ULONG ulStyle)
{
if( mWindows == nsnull)
mWindows = new nsHashtable;
nsWndClassKey aKey( pszClass);
if( mWindows->Get( &aKey) == nsnull)
{
HWND hwnd = WinCreateWindow( HWND_DESKTOP, pszClass, "", ulStyle,
0, -4096, 0, 0, HWND_DESKTOP,
HWND_TOP, 0, 0, 0);
NS_ASSERTION( hwnd, "WinCreateWindow for printing failed");
mWindows->Put( &aKey, (void*) hwnd);
}
return (HWND) mWindows->Get( &aKey);
}
#endif
const char *nsWidgetModuleData::DBCSstrchr( const char *string, int c )
{
const char* p = string;

View File

@ -53,8 +53,6 @@ class nsWidgetModuleData : public nsISupports
HMODULE hModResources; // resource module
SIZEL szScreen; // size of screen in pixels
BOOL bMouseSwitched; // true if MB1 is the RH mouse button
LONG lHtEntryfield; // ideal height of an entryfield
BOOL bIsDBCS; // true if system is dbcs
PRBool bIsTrackPoint; // true if system has a TrackPoint
HPOINTER hptrArray[17];
@ -67,16 +65,6 @@ class nsWidgetModuleData : public nsISupports
// better-suited elsewhere, but there shouldn't be very many of them.
HPOINTER GetPointer( nsCursor aCursor);
// local->Unicode cp. conversion
ULONG ConvertToUcs( const char *szText, PRUnichar *pBuffer, ULONG ulSize);
// Unicode->local cp. conversions
char *ConvertFromUcs( const PRUnichar *pText, char *szBuffer, ULONG ulSize);
char *ConvertFromUcs( const nsString &aStr, char *szBuffer, ULONG ulSize);
// these methods use a single static buffer
const char *ConvertFromUcs( const PRUnichar *pText);
const char *ConvertFromUcs( const nsString &aStr);
const char *DBCSstrchr( const char *string, int c );
const char *DBCSstrrchr( const char *string, int c );
@ -84,14 +72,6 @@ class nsWidgetModuleData : public nsISupports
~nsWidgetModuleData();
void Init( nsIAppShell *aPrimaevalAppShell);
private:
// Utility function for creating the Unicode conversion object
int CreateUcsConverter();
UconvObject converter;
BOOL supplantConverter;
};
#endif

View File

@ -60,6 +60,8 @@
#include "nsXPIDLString.h"
#include "nsIFile.h"
#include "nsOS2Uni.h"
#include <stdlib.h>
#include <ctype.h>
@ -1522,8 +1524,6 @@ NS_METHOD nsWindow::SetFont(const nsFont &aFont)
{
if( mToolkit) // called from print-routine (XXX check)
{
const char *fontname = gWidgetModuleData->ConvertFromUcs( aFont.name);
// jump through hoops to convert the size in the font (in app units)
// into points.
float dev2twip, app2twip;
@ -1532,18 +1532,27 @@ NS_METHOD nsWindow::SetFont(const nsFont &aFont)
app2twip *= dev2twip;
int points = NSTwipsToFloorIntPoints( nscoord( aFont.size * app2twip));
char *buffer = new char [ strlen( fontname) + 6];
sprintf( buffer, "%d.%s", points, fontname);
BOOL rc = WinSetPresParam( mWnd, PP_FONTNAMESIZE,
strlen( buffer) + 1, buffer);
#ifdef DEBUG
if( !rc)
printf( "WinSetPresParam PP_FONTNAMESIZE %s failed\n", buffer);
#endif
int length = aFont.name.Length() * 2 + 1;
char * fontname = new char[length];
if (fontname) {
int outlen = ::WideCharToMultiByte( 0,
aFont.name.get(), aFont.name.Length(),
fontname, length);
if ( outlen >= 0) {
fontname[outlen] = '\0';
}
delete [] buffer;
char *buffer = new char [ strlen( fontname) + 6];
if (buffer) {
sprintf( buffer, "%d.%s", points, fontname);
BOOL rc = WinSetPresParam( mWnd, PP_FONTNAMESIZE,
strlen( buffer) + 1, buffer);
delete [] buffer;
}
delete [] fontname;
}
}
if( !mFont)
@ -2119,7 +2128,7 @@ PRBool nsWindow::OnKey( MPARAM mp1, MPARAM mp2)
inbuf[1] = '\0';
outbuf[0] = (UniChar)0;
gWidgetModuleData->ConvertToUcs( (char *)inbuf, (PRUnichar *)outbuf, 4);
MultiByteToWideChar(0, (const char*)inbuf, 2, outbuf, 4);
event.charCode = outbuf[0];
@ -3162,6 +3171,9 @@ PRBool nsWindow::OnHScroll( MPARAM mp1, MPARAM mp2)
return PR_FALSE;
}
/* On OS/2, if you pass a titlebar > 512 characters, it doesn't display at all. */
/* We are going to limit our titlebars to 256 just to be on the safe side */
#define MAX_TITLEBAR_LENGTH 256
NS_METHOD nsWindow::SetTitle(const nsString& aTitle)
{
@ -3174,12 +3186,24 @@ NS_METHOD nsWindow::SetTitle(const nsString& aTitle)
}
else if( mWnd)
{
/* On OS/2, if you pass a titlebar > 512 characters, it doesn't display at all. */
/* We are going to limit our titlebars to 256 just to be on the safe side */
nsAutoString left;
aTitle.Left(left, 256);
WinSetWindowText( GetMainWindow(),
gWidgetModuleData->ConvertFromUcs(left));
int length = aTitle.Length() * 2 + 1;
char * title = new char[length];
if (title)
{
int outlen = ::WideCharToMultiByte( 0,
aTitle.get(), aTitle.Length(),
title, length);
if ( outlen >= 0) {
if (outlen > MAX_TITLEBAR_LENGTH) {
title[MAX_TITLEBAR_LENGTH] = '\0';
} else {
title[outlen] = '\0';
}
}
WinSetWindowText( GetMainWindow(),
title );
delete [] title;
}
}
return NS_OK;
}