Lots more Unicode fixes.

wxClipboard fixes for GTK2 and UTF8.
  wxFileConfig now uses wxConvLocal to convert text
    and doesn't crash anymore..


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@16601 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robert Roebling
2002-08-19 17:02:10 +00:00
parent 127328cdd3
commit ca11abde11
9 changed files with 76 additions and 83 deletions

View File

@@ -99,7 +99,7 @@ public:
// returns the number of bytes written // returns the number of bytes written
size_t Write(const void *pBuf, size_t nCount); size_t Write(const void *pBuf, size_t nCount);
// returns true on success // returns true on success
bool Write(const wxString& s, wxMBConv& conv = wxConvLibc) bool Write(const wxString& s, wxMBConv& conv = wxConvLocal)
{ {
const wxWX2MBbuf buf = s.mb_str(conv); const wxWX2MBbuf buf = s.mb_str(conv);
size_t size = strlen(buf); size_t size = strlen(buf);

View File

@@ -502,9 +502,9 @@ bool wxTempFile::Open(const wxString& strName)
#ifdef __UNIX__ #ifdef __UNIX__
// the temp file should have the same permissions as the original one // the temp file should have the same permissions as the original one
mode_t mode; mode_t mode;
wxStructStat st; wxStructStat st;
if ( stat(m_strName.fn_str(), &st) == 0 ) if ( stat( (const char*) m_strName.fn_str(), &st) == 0 )
{ {
mode = st.st_mode; mode = st.st_mode;
} }
@@ -517,7 +517,7 @@ bool wxTempFile::Open(const wxString& strName)
umask(mask); umask(mask);
} }
if ( chmod(m_strTemp.mb_str(), mode) == -1 ) if ( chmod( (const char*) m_strTemp.fn_str(), mode) == -1 )
{ {
wxLogSysError(_("Failed to set temporary file permissions")); wxLogSysError(_("Failed to set temporary file permissions"));
} }

View File

@@ -901,14 +901,24 @@ bool wxFileConfig::Flush(bool /* bCurrentOnly */)
wxTempFile file(m_strLocalFile); wxTempFile file(m_strLocalFile);
if ( !file.IsOpened() ) { if ( !file.IsOpened() )
{
wxLogError(_("can't open user configuration file.")); wxLogError(_("can't open user configuration file."));
return FALSE; return FALSE;
} }
// write all strings to file // write all strings to file
for ( wxFileConfigLineList *p = m_linesHead; p != NULL; p = p->Next() ) { for ( wxFileConfigLineList *p = m_linesHead; p != NULL; p = p->Next() )
if ( !file.Write(p->Text() + wxTextFile::GetEOL()) ) { {
wxString line = p->Text();
line += wxTextFile::GetEOL();
#if wxUSE_UNICODE
wxCharBuffer buf = wxConvLocal.cWX2MB( line );
if ( !file.Write( (const char*)buf, strlen( (const char*) buf ) ) )
#else
if ( !file.Write( line.c_str(), line.Len() ) )
#endif
{
wxLogError(_("can't write user configuration file.")); wxLogError(_("can't write user configuration file."));
return FALSE; return FALSE;
} }

View File

@@ -639,10 +639,10 @@ wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp)
path += _T("XXXXXX"); path += _T("XXXXXX");
// we need to copy the path to the buffer in which mkstemp() can modify it // we need to copy the path to the buffer in which mkstemp() can modify it
wxCharBuffer buf(path.fn_str()); wxCharBuffer buf = wxConvFile.cWX2MB( path );
// cast is safe because the string length doesn't change // cast is safe because the string length doesn't change
int fdTemp = mkstemp( (char *)buf.data() ); int fdTemp = mkstemp( (char*)(const char*) buf );
if ( fdTemp == -1 ) if ( fdTemp == -1 )
{ {
// this might be not necessary as mkstemp() on most systems should have // this might be not necessary as mkstemp() on most systems should have
@@ -651,8 +651,8 @@ wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp)
} }
else // mkstemp() succeeded else // mkstemp() succeeded
{ {
path = wxConvFile.cMB2WX(buf); path = wxConvFile.cMB2WX( (const char*) buf );
// avoid leaking the fd // avoid leaking the fd
if ( fileTemp ) if ( fileTemp )
{ {
@@ -669,14 +669,14 @@ wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp)
// same as above // same as above
path += _T("XXXXXX"); path += _T("XXXXXX");
wxCharBuffer buf(path.fn_str()); wxCharBuffer buf = wxConvFile.cWX2MB( path );
if ( !mktemp( buf ) ) if ( !mktemp( (const char*) buf ) )
{ {
path.clear(); path.clear();
} }
else else
{ {
path = wxConvFile.cMB2WX(buf); path = wxConvFile.cMB2WX( (const char*) buf );
} }
#else // !HAVE_MKTEMP (includes __DOS__) #else // !HAVE_MKTEMP (includes __DOS__)
// generate the unique file name ourselves // generate the unique file name ourselves

View File

@@ -67,7 +67,7 @@ public:
virtual void OnExit() virtual void OnExit()
{ {
#if wxUSE_WCHAR_T #if wxUSE_WCHAR_T
wxConvLocal.Clear(); wxConvLocal.Clear();
#endif #endif
} }
@@ -195,7 +195,6 @@ size_t wxMBConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const
{ {
for (size_t i = 0; i < strlen( psz )+1; i++) for (size_t i = 0; i < strlen( psz )+1; i++)
buf[i] = (wchar_t) psz[i]; buf[i] = (wchar_t) psz[i];
// printf( "libc %s\n", buf );
return strlen( psz ); return strlen( psz );
} }
else else
@@ -214,7 +213,6 @@ size_t wxMBConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const
{ {
for (size_t i = 0; i < wxStrlen( psz )+1; i++) for (size_t i = 0; i < wxStrlen( psz )+1; i++)
buf[i] = (char) psz[i]; buf[i] = (char) psz[i];
// printf( "libc %s\n", buf );
return wxStrlen( psz ); return wxStrlen( psz );
} }
else else
@@ -250,7 +248,6 @@ const wxCharBuffer wxMBConv::cWC2MB(const wchar_t *psz) const
return wxCharBuffer((char *) NULL); return wxCharBuffer((char *) NULL);
wxCharBuffer buf(nLen); // this allocates nLen+1 wxCharBuffer buf(nLen); // this allocates nLen+1
WC2MB((char *)(const char *) buf, psz, nLen+1); WC2MB((char *)(const char *) buf, psz, nLen+1);
// printf( "str %s\n", (const char*) buf );
return buf; return buf;
} }
else else
@@ -911,15 +908,6 @@ static wxCharacterSet *wxGetCharacterSet(const wxChar *name)
cset = NULL; cset = NULL;
} }
#if defined(__WIN32__) && !defined(__WXMICROWIN__)
cset = new CP_CharSet(name);
if ( cset->usable() )
return cset;
delete cset;
cset = NULL;
#endif // __WIN32__
#if wxUSE_FONTMAP #if wxUSE_FONTMAP
cset = new EC_CharSet(name); cset = new EC_CharSet(name);
if ( cset->usable() ) if ( cset->usable() )
@@ -929,6 +917,15 @@ static wxCharacterSet *wxGetCharacterSet(const wxChar *name)
cset = NULL; cset = NULL;
#endif // wxUSE_FONTMAP #endif // wxUSE_FONTMAP
#if defined(__WIN32__) && !defined(__WXMICROWIN__)
cset = new CP_CharSet(name);
if ( cset->usable() )
return cset;
delete cset;
cset = NULL;
#endif // __WIN32__
wxLogError(_("Cannot convert from encoding '%s'!"), name); wxLogError(_("Cannot convert from encoding '%s'!"), name);
return NULL; return NULL;

View File

@@ -74,14 +74,12 @@ struct _GtkSelectionData
static void static void
targets_selection_received( GtkWidget *WXUNUSED(widget), targets_selection_received( GtkWidget *WXUNUSED(widget),
GtkSelectionData *selection_data, GtkSelectionData *selection_data,
#if (GTK_MINOR_VERSION > 0)
guint32 WXUNUSED(time), guint32 WXUNUSED(time),
#endif
wxClipboard *clipboard ) wxClipboard *clipboard )
{ {
if ( wxTheClipboard && selection_data->length > 0 ) if ( wxTheClipboard && selection_data->length > 0 )
{ {
/* make sure we got the data in the correct form */ // make sure we got the data in the correct form
GdkAtom type = selection_data->type; GdkAtom type = selection_data->type;
if ( type != GDK_SELECTION_TYPE_ATOM ) if ( type != GDK_SELECTION_TYPE_ATOM )
{ {
@@ -113,6 +111,10 @@ targets_selection_received( GtkWidget *WXUNUSED(widget),
wxT("selection received for targets, format %s"), wxT("selection received for targets, format %s"),
format.GetId().c_str() ); format.GetId().c_str() );
// printf( "format %s requested %s\n",
// gdk_atom_name( atoms[i] ),
// gdk_atom_name( clipboard->m_targetRequested ) );
if (format == clipboard->m_targetRequested) if (format == clipboard->m_targetRequested)
{ {
clipboard->m_waiting = FALSE; clipboard->m_waiting = FALSE;
@@ -132,9 +134,7 @@ targets_selection_received( GtkWidget *WXUNUSED(widget),
static void static void
selection_received( GtkWidget *WXUNUSED(widget), selection_received( GtkWidget *WXUNUSED(widget),
GtkSelectionData *selection_data, GtkSelectionData *selection_data,
#if (GTK_MINOR_VERSION > 0)
guint32 WXUNUSED(time), guint32 WXUNUSED(time),
#endif
wxClipboard *clipboard ) wxClipboard *clipboard )
{ {
if (!wxTheClipboard) if (!wxTheClipboard)
@@ -159,7 +159,7 @@ selection_received( GtkWidget *WXUNUSED(widget),
wxDataFormat format( selection_data->target ); wxDataFormat format( selection_data->target );
/* make sure we got the data in the correct format */ // make sure we got the data in the correct format
if (!data_object->IsSupportedFormat( format ) ) if (!data_object->IsSupportedFormat( format ) )
{ {
clipboard->m_waiting = FALSE; clipboard->m_waiting = FALSE;
@@ -248,23 +248,9 @@ selection_handler( GtkWidget *WXUNUSED(widget),
void *d = malloc(size); void *d = malloc(size);
// Text data will be in UTF8 in Unicode mode.
data->GetDataHere( selection_data->target, d ); data->GetDataHere( selection_data->target, d );
// transform Unicode text into multibyte before putting it on clipboard
#if wxUSE_UNICODE
if ( format.GetType() == wxDF_TEXT )
{
const wchar_t *wstr = (const wchar_t *)d;
size_t len = wxConvCurrent->WC2MB(NULL, wstr, 0);
char *str = (char*) malloc(len + 1);
wxConvCurrent->WC2MB(str, wstr, len);
str[len] = '\0';
free(d);
d = str;
}
#endif // wxUSE_UNICODE
gtk_selection_data_set( gtk_selection_data_set(
selection_data, selection_data,
GDK_SELECTION_TYPE_STRING, GDK_SELECTION_TYPE_STRING,
@@ -341,8 +327,8 @@ void wxClipboard::Clear()
/* disable GUI threads */ /* disable GUI threads */
#endif #endif
/* As we have data we also own the clipboard. Once we no longer own // As we have data we also own the clipboard. Once we no longer own
it, clear_selection is called which will set m_data to zero */ // it, clear_selection is called which will set m_data to zero
if (gdk_selection_owner_get( g_clipboardAtom ) == m_clipboardWidget->window) if (gdk_selection_owner_get( g_clipboardAtom ) == m_clipboardWidget->window)
{ {
m_waiting = TRUE; m_waiting = TRUE;
@@ -404,16 +390,16 @@ bool wxClipboard::AddData( wxDataObject *data )
wxCHECK_MSG( data, FALSE, wxT("data is invalid") ); wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
/* we can only store one wxDataObject */ // we can only store one wxDataObject
Clear(); Clear();
m_data = data; m_data = data;
/* get formats from wxDataObjects */ // get formats from wxDataObjects
wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ]; wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
m_data->GetAllFormats( array ); m_data->GetAllFormats( array );
/* primary selection or clipboard */ // primary selection or clipboard
GdkAtom clipboard = m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY GdkAtom clipboard = m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
: g_clipboardAtom; : g_clipboardAtom;
@@ -424,6 +410,9 @@ bool wxClipboard::AddData( wxDataObject *data )
wxT("wxClipboard now supports atom %s"), wxT("wxClipboard now supports atom %s"),
array[i].GetId().c_str() ); array[i].GetId().c_str() );
// printf( "added %s\n",
// gdk_atom_name( array[i].GetFormatId() ) );
gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget), gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget),
clipboard, clipboard,
array[i], array[i],

View File

@@ -72,8 +72,12 @@ wxDataFormat::wxDataFormat( NativeFormat format )
void wxDataFormat::SetType( wxDataFormatId type ) void wxDataFormat::SetType( wxDataFormatId type )
{ {
PrepareFormats(); PrepareFormats();
m_type = type;
if (type == wxDF_UNICODETEXT)
type = wxDF_TEXT;
m_type = type;
if (m_type == wxDF_TEXT) if (m_type == wxDF_TEXT)
m_format = g_textAtom; m_format = g_textAtom;
else else
@@ -135,7 +139,7 @@ void wxDataFormat::PrepareFormats()
// here (with whom?) // here (with whom?)
if (!g_textAtom) if (!g_textAtom)
#if wxUSE_UNICODE #if wxUSE_UNICODE
g_textAtom = gdk_atom_intern( "text/utf8", FALSE ); g_textAtom = gdk_atom_intern( "UTF8_STRING", FALSE );
#else #else
g_textAtom = gdk_atom_intern( "STRING" /* "text/plain" */, FALSE ); g_textAtom = gdk_atom_intern( "STRING" /* "text/plain" */, FALSE );
#endif #endif

View File

@@ -74,14 +74,12 @@ struct _GtkSelectionData
static void static void
targets_selection_received( GtkWidget *WXUNUSED(widget), targets_selection_received( GtkWidget *WXUNUSED(widget),
GtkSelectionData *selection_data, GtkSelectionData *selection_data,
#if (GTK_MINOR_VERSION > 0)
guint32 WXUNUSED(time), guint32 WXUNUSED(time),
#endif
wxClipboard *clipboard ) wxClipboard *clipboard )
{ {
if ( wxTheClipboard && selection_data->length > 0 ) if ( wxTheClipboard && selection_data->length > 0 )
{ {
/* make sure we got the data in the correct form */ // make sure we got the data in the correct form
GdkAtom type = selection_data->type; GdkAtom type = selection_data->type;
if ( type != GDK_SELECTION_TYPE_ATOM ) if ( type != GDK_SELECTION_TYPE_ATOM )
{ {
@@ -113,6 +111,10 @@ targets_selection_received( GtkWidget *WXUNUSED(widget),
wxT("selection received for targets, format %s"), wxT("selection received for targets, format %s"),
format.GetId().c_str() ); format.GetId().c_str() );
// printf( "format %s requested %s\n",
// gdk_atom_name( atoms[i] ),
// gdk_atom_name( clipboard->m_targetRequested ) );
if (format == clipboard->m_targetRequested) if (format == clipboard->m_targetRequested)
{ {
clipboard->m_waiting = FALSE; clipboard->m_waiting = FALSE;
@@ -132,9 +134,7 @@ targets_selection_received( GtkWidget *WXUNUSED(widget),
static void static void
selection_received( GtkWidget *WXUNUSED(widget), selection_received( GtkWidget *WXUNUSED(widget),
GtkSelectionData *selection_data, GtkSelectionData *selection_data,
#if (GTK_MINOR_VERSION > 0)
guint32 WXUNUSED(time), guint32 WXUNUSED(time),
#endif
wxClipboard *clipboard ) wxClipboard *clipboard )
{ {
if (!wxTheClipboard) if (!wxTheClipboard)
@@ -159,7 +159,7 @@ selection_received( GtkWidget *WXUNUSED(widget),
wxDataFormat format( selection_data->target ); wxDataFormat format( selection_data->target );
/* make sure we got the data in the correct format */ // make sure we got the data in the correct format
if (!data_object->IsSupportedFormat( format ) ) if (!data_object->IsSupportedFormat( format ) )
{ {
clipboard->m_waiting = FALSE; clipboard->m_waiting = FALSE;
@@ -248,23 +248,9 @@ selection_handler( GtkWidget *WXUNUSED(widget),
void *d = malloc(size); void *d = malloc(size);
// Text data will be in UTF8 in Unicode mode.
data->GetDataHere( selection_data->target, d ); data->GetDataHere( selection_data->target, d );
// transform Unicode text into multibyte before putting it on clipboard
#if wxUSE_UNICODE
if ( format.GetType() == wxDF_TEXT )
{
const wchar_t *wstr = (const wchar_t *)d;
size_t len = wxConvCurrent->WC2MB(NULL, wstr, 0);
char *str = (char*) malloc(len + 1);
wxConvCurrent->WC2MB(str, wstr, len);
str[len] = '\0';
free(d);
d = str;
}
#endif // wxUSE_UNICODE
gtk_selection_data_set( gtk_selection_data_set(
selection_data, selection_data,
GDK_SELECTION_TYPE_STRING, GDK_SELECTION_TYPE_STRING,
@@ -341,8 +327,8 @@ void wxClipboard::Clear()
/* disable GUI threads */ /* disable GUI threads */
#endif #endif
/* As we have data we also own the clipboard. Once we no longer own // As we have data we also own the clipboard. Once we no longer own
it, clear_selection is called which will set m_data to zero */ // it, clear_selection is called which will set m_data to zero
if (gdk_selection_owner_get( g_clipboardAtom ) == m_clipboardWidget->window) if (gdk_selection_owner_get( g_clipboardAtom ) == m_clipboardWidget->window)
{ {
m_waiting = TRUE; m_waiting = TRUE;
@@ -404,16 +390,16 @@ bool wxClipboard::AddData( wxDataObject *data )
wxCHECK_MSG( data, FALSE, wxT("data is invalid") ); wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
/* we can only store one wxDataObject */ // we can only store one wxDataObject
Clear(); Clear();
m_data = data; m_data = data;
/* get formats from wxDataObjects */ // get formats from wxDataObjects
wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ]; wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
m_data->GetAllFormats( array ); m_data->GetAllFormats( array );
/* primary selection or clipboard */ // primary selection or clipboard
GdkAtom clipboard = m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY GdkAtom clipboard = m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
: g_clipboardAtom; : g_clipboardAtom;
@@ -424,6 +410,9 @@ bool wxClipboard::AddData( wxDataObject *data )
wxT("wxClipboard now supports atom %s"), wxT("wxClipboard now supports atom %s"),
array[i].GetId().c_str() ); array[i].GetId().c_str() );
// printf( "added %s\n",
// gdk_atom_name( array[i].GetFormatId() ) );
gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget), gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget),
clipboard, clipboard,
array[i], array[i],

View File

@@ -72,8 +72,12 @@ wxDataFormat::wxDataFormat( NativeFormat format )
void wxDataFormat::SetType( wxDataFormatId type ) void wxDataFormat::SetType( wxDataFormatId type )
{ {
PrepareFormats(); PrepareFormats();
m_type = type;
if (type == wxDF_UNICODETEXT)
type = wxDF_TEXT;
m_type = type;
if (m_type == wxDF_TEXT) if (m_type == wxDF_TEXT)
m_format = g_textAtom; m_format = g_textAtom;
else else
@@ -135,7 +139,7 @@ void wxDataFormat::PrepareFormats()
// here (with whom?) // here (with whom?)
if (!g_textAtom) if (!g_textAtom)
#if wxUSE_UNICODE #if wxUSE_UNICODE
g_textAtom = gdk_atom_intern( "text/utf8", FALSE ); g_textAtom = gdk_atom_intern( "UTF8_STRING", FALSE );
#else #else
g_textAtom = gdk_atom_intern( "STRING" /* "text/plain" */, FALSE ); g_textAtom = gdk_atom_intern( "STRING" /* "text/plain" */, FALSE );
#endif #endif