From: Robert Roebling Date: Mon, 19 Aug 2002 17:02:10 +0000 (+0000) Subject: Lots more Unicode fixes. X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/ca11abde117b55a0054588fcf138234f8172b98d 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 --- diff --git a/include/wx/file.h b/include/wx/file.h index 0c644bfe03..850bb99ded 100644 --- a/include/wx/file.h +++ b/include/wx/file.h @@ -99,7 +99,7 @@ public: // returns the number of bytes written size_t Write(const void *pBuf, size_t nCount); // 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); size_t size = strlen(buf); diff --git a/src/common/file.cpp b/src/common/file.cpp index 99582fae28..d5a136e704 100644 --- a/src/common/file.cpp +++ b/src/common/file.cpp @@ -502,9 +502,9 @@ bool wxTempFile::Open(const wxString& strName) #ifdef __UNIX__ // the temp file should have the same permissions as the original one mode_t mode; - + wxStructStat st; - if ( stat(m_strName.fn_str(), &st) == 0 ) + if ( stat( (const char*) m_strName.fn_str(), &st) == 0 ) { mode = st.st_mode; } @@ -517,7 +517,7 @@ bool wxTempFile::Open(const wxString& strName) 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")); } diff --git a/src/common/fileconf.cpp b/src/common/fileconf.cpp index 45ea8251d0..e635c6aaf7 100644 --- a/src/common/fileconf.cpp +++ b/src/common/fileconf.cpp @@ -901,14 +901,24 @@ bool wxFileConfig::Flush(bool /* bCurrentOnly */) wxTempFile file(m_strLocalFile); - if ( !file.IsOpened() ) { + if ( !file.IsOpened() ) + { wxLogError(_("can't open user configuration file.")); return FALSE; } // write all strings to file - for ( wxFileConfigLineList *p = m_linesHead; p != NULL; p = p->Next() ) { - if ( !file.Write(p->Text() + wxTextFile::GetEOL()) ) { + for ( wxFileConfigLineList *p = m_linesHead; p != NULL; p = p->Next() ) + { + 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.")); return FALSE; } diff --git a/src/common/filename.cpp b/src/common/filename.cpp index c711152e64..ae6440cc5a 100644 --- a/src/common/filename.cpp +++ b/src/common/filename.cpp @@ -639,10 +639,10 @@ wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp) path += _T("XXXXXX"); // 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 - int fdTemp = mkstemp( (char *)buf.data() ); + int fdTemp = mkstemp( (char*)(const char*) buf ); if ( fdTemp == -1 ) { // 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 { - path = wxConvFile.cMB2WX(buf); - + path = wxConvFile.cMB2WX( (const char*) buf ); + // avoid leaking the fd if ( fileTemp ) { @@ -669,14 +669,14 @@ wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp) // same as above path += _T("XXXXXX"); - wxCharBuffer buf(path.fn_str()); - if ( !mktemp( buf ) ) + wxCharBuffer buf = wxConvFile.cWX2MB( path ); + if ( !mktemp( (const char*) buf ) ) { path.clear(); } else { - path = wxConvFile.cMB2WX(buf); + path = wxConvFile.cMB2WX( (const char*) buf ); } #else // !HAVE_MKTEMP (includes __DOS__) // generate the unique file name ourselves diff --git a/src/common/strconv.cpp b/src/common/strconv.cpp index 745aef9699..516e0a6a7c 100644 --- a/src/common/strconv.cpp +++ b/src/common/strconv.cpp @@ -67,7 +67,7 @@ public: virtual void OnExit() { #if wxUSE_WCHAR_T - wxConvLocal.Clear(); + wxConvLocal.Clear(); #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++) buf[i] = (wchar_t) psz[i]; - // printf( "libc %s\n", buf ); return strlen( psz ); } 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++) buf[i] = (char) psz[i]; - // printf( "libc %s\n", buf ); return wxStrlen( psz ); } else @@ -250,7 +248,6 @@ const wxCharBuffer wxMBConv::cWC2MB(const wchar_t *psz) const return wxCharBuffer((char *) NULL); wxCharBuffer buf(nLen); // this allocates nLen+1 WC2MB((char *)(const char *) buf, psz, nLen+1); - // printf( "str %s\n", (const char*) buf ); return buf; } else @@ -911,23 +908,23 @@ static wxCharacterSet *wxGetCharacterSet(const wxChar *name) cset = NULL; } -#if defined(__WIN32__) && !defined(__WXMICROWIN__) - cset = new CP_CharSet(name); +#if wxUSE_FONTMAP + cset = new EC_CharSet(name); if ( cset->usable() ) return cset; delete cset; cset = NULL; -#endif // __WIN32__ +#endif // wxUSE_FONTMAP -#if wxUSE_FONTMAP - cset = new EC_CharSet(name); +#if defined(__WIN32__) && !defined(__WXMICROWIN__) + cset = new CP_CharSet(name); if ( cset->usable() ) return cset; delete cset; cset = NULL; -#endif // wxUSE_FONTMAP +#endif // __WIN32__ wxLogError(_("Cannot convert from encoding '%s'!"), name); diff --git a/src/gtk/clipbrd.cpp b/src/gtk/clipbrd.cpp index 9f9c3f827f..251709b69e 100644 --- a/src/gtk/clipbrd.cpp +++ b/src/gtk/clipbrd.cpp @@ -74,14 +74,12 @@ struct _GtkSelectionData static void targets_selection_received( GtkWidget *WXUNUSED(widget), GtkSelectionData *selection_data, -#if (GTK_MINOR_VERSION > 0) guint32 WXUNUSED(time), -#endif wxClipboard *clipboard ) { 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; if ( type != GDK_SELECTION_TYPE_ATOM ) { @@ -113,6 +111,10 @@ targets_selection_received( GtkWidget *WXUNUSED(widget), wxT("selection received for targets, format %s"), 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) { clipboard->m_waiting = FALSE; @@ -132,9 +134,7 @@ targets_selection_received( GtkWidget *WXUNUSED(widget), static void selection_received( GtkWidget *WXUNUSED(widget), GtkSelectionData *selection_data, -#if (GTK_MINOR_VERSION > 0) guint32 WXUNUSED(time), -#endif wxClipboard *clipboard ) { if (!wxTheClipboard) @@ -159,7 +159,7 @@ selection_received( GtkWidget *WXUNUSED(widget), 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 ) ) { clipboard->m_waiting = FALSE; @@ -248,23 +248,9 @@ selection_handler( GtkWidget *WXUNUSED(widget), void *d = malloc(size); + // Text data will be in UTF8 in Unicode mode. 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( selection_data, GDK_SELECTION_TYPE_STRING, @@ -341,8 +327,8 @@ void wxClipboard::Clear() /* disable GUI threads */ #endif - /* 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 */ + // 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 if (gdk_selection_owner_get( g_clipboardAtom ) == m_clipboardWidget->window) { m_waiting = TRUE; @@ -404,16 +390,16 @@ bool wxClipboard::AddData( wxDataObject *data ) wxCHECK_MSG( data, FALSE, wxT("data is invalid") ); - /* we can only store one wxDataObject */ + // we can only store one wxDataObject Clear(); m_data = data; - /* get formats from wxDataObjects */ + // get formats from wxDataObjects wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ]; m_data->GetAllFormats( array ); - /* primary selection or clipboard */ + // primary selection or clipboard GdkAtom clipboard = m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY : g_clipboardAtom; @@ -424,6 +410,9 @@ bool wxClipboard::AddData( wxDataObject *data ) wxT("wxClipboard now supports atom %s"), array[i].GetId().c_str() ); +// printf( "added %s\n", +// gdk_atom_name( array[i].GetFormatId() ) ); + gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget), clipboard, array[i], diff --git a/src/gtk/dataobj.cpp b/src/gtk/dataobj.cpp index 712616a074..e0d6d37609 100644 --- a/src/gtk/dataobj.cpp +++ b/src/gtk/dataobj.cpp @@ -72,8 +72,12 @@ wxDataFormat::wxDataFormat( NativeFormat format ) void wxDataFormat::SetType( wxDataFormatId type ) { PrepareFormats(); - m_type = type; + + if (type == wxDF_UNICODETEXT) + type = wxDF_TEXT; + m_type = type; + if (m_type == wxDF_TEXT) m_format = g_textAtom; else @@ -135,7 +139,7 @@ void wxDataFormat::PrepareFormats() // here (with whom?) if (!g_textAtom) #if wxUSE_UNICODE - g_textAtom = gdk_atom_intern( "text/utf8", FALSE ); + g_textAtom = gdk_atom_intern( "UTF8_STRING", FALSE ); #else g_textAtom = gdk_atom_intern( "STRING" /* "text/plain" */, FALSE ); #endif diff --git a/src/gtk1/clipbrd.cpp b/src/gtk1/clipbrd.cpp index 9f9c3f827f..251709b69e 100644 --- a/src/gtk1/clipbrd.cpp +++ b/src/gtk1/clipbrd.cpp @@ -74,14 +74,12 @@ struct _GtkSelectionData static void targets_selection_received( GtkWidget *WXUNUSED(widget), GtkSelectionData *selection_data, -#if (GTK_MINOR_VERSION > 0) guint32 WXUNUSED(time), -#endif wxClipboard *clipboard ) { 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; if ( type != GDK_SELECTION_TYPE_ATOM ) { @@ -113,6 +111,10 @@ targets_selection_received( GtkWidget *WXUNUSED(widget), wxT("selection received for targets, format %s"), 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) { clipboard->m_waiting = FALSE; @@ -132,9 +134,7 @@ targets_selection_received( GtkWidget *WXUNUSED(widget), static void selection_received( GtkWidget *WXUNUSED(widget), GtkSelectionData *selection_data, -#if (GTK_MINOR_VERSION > 0) guint32 WXUNUSED(time), -#endif wxClipboard *clipboard ) { if (!wxTheClipboard) @@ -159,7 +159,7 @@ selection_received( GtkWidget *WXUNUSED(widget), 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 ) ) { clipboard->m_waiting = FALSE; @@ -248,23 +248,9 @@ selection_handler( GtkWidget *WXUNUSED(widget), void *d = malloc(size); + // Text data will be in UTF8 in Unicode mode. 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( selection_data, GDK_SELECTION_TYPE_STRING, @@ -341,8 +327,8 @@ void wxClipboard::Clear() /* disable GUI threads */ #endif - /* 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 */ + // 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 if (gdk_selection_owner_get( g_clipboardAtom ) == m_clipboardWidget->window) { m_waiting = TRUE; @@ -404,16 +390,16 @@ bool wxClipboard::AddData( wxDataObject *data ) wxCHECK_MSG( data, FALSE, wxT("data is invalid") ); - /* we can only store one wxDataObject */ + // we can only store one wxDataObject Clear(); m_data = data; - /* get formats from wxDataObjects */ + // get formats from wxDataObjects wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ]; m_data->GetAllFormats( array ); - /* primary selection or clipboard */ + // primary selection or clipboard GdkAtom clipboard = m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY : g_clipboardAtom; @@ -424,6 +410,9 @@ bool wxClipboard::AddData( wxDataObject *data ) wxT("wxClipboard now supports atom %s"), array[i].GetId().c_str() ); +// printf( "added %s\n", +// gdk_atom_name( array[i].GetFormatId() ) ); + gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget), clipboard, array[i], diff --git a/src/gtk1/dataobj.cpp b/src/gtk1/dataobj.cpp index 712616a074..e0d6d37609 100644 --- a/src/gtk1/dataobj.cpp +++ b/src/gtk1/dataobj.cpp @@ -72,8 +72,12 @@ wxDataFormat::wxDataFormat( NativeFormat format ) void wxDataFormat::SetType( wxDataFormatId type ) { PrepareFormats(); - m_type = type; + + if (type == wxDF_UNICODETEXT) + type = wxDF_TEXT; + m_type = type; + if (m_type == wxDF_TEXT) m_format = g_textAtom; else @@ -135,7 +139,7 @@ void wxDataFormat::PrepareFormats() // here (with whom?) if (!g_textAtom) #if wxUSE_UNICODE - g_textAtom = gdk_atom_intern( "text/utf8", FALSE ); + g_textAtom = gdk_atom_intern( "UTF8_STRING", FALSE ); #else g_textAtom = gdk_atom_intern( "STRING" /* "text/plain" */, FALSE ); #endif