]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/strconv.cpp
Added color setting tool to the RichTextCtrl sample
[wxWidgets.git] / src / common / strconv.cpp
index 33a2e7a1619296186f10aa73811a87071015c4b3..a603d02f18eeaee87abec04920ce164955230fe9 100644 (file)
@@ -1806,8 +1806,8 @@ size_t wxMBConv_iconv::MB2WC(wchar_t *buf, const char *psz, size_t n) const
     }
 
 #if wxUSE_THREADS
-    // NB: iconv() is MT-safe, but each thread must use it's own iconv_t handle.
-    //     Unfortunately there is a couple of global wxCSConv objects such as
+    // NB: iconv() is MT-safe, but each thread must use its own iconv_t handle.
+    //     Unfortunately there are a couple of global wxCSConv objects such as
     //     wxConvLocal that are used all over wx code, so we have to make sure
     //     the handle is used by at most one thread at the time. Otherwise
     //     only a few wx classes would be safe to use from non-main threads
@@ -2909,11 +2909,15 @@ public :
     {
         Init( kTextEncodingUnicodeDefault , kUnicodeNoSubset , kUnicodeUTF8Format ) ;
         m_uni = NULL;
+        m_uniBack = NULL ;
     }
      
     ~wxMBConv_macUTF8D()
     {
-        DisposeUnicodeToTextInfo(&m_uni);
+        if (m_uni!=NULL)
+            DisposeUnicodeToTextInfo(&m_uni);
+        if (m_uniBack!=NULL)
+            DisposeUnicodeToTextInfo(&m_uniBack);
     }
     
     size_t WC2MB(char *buf, const wchar_t *psz, size_t n) const
@@ -2979,6 +2983,68 @@ public :
         return res ;
     }
     
+    size_t MB2WC(wchar_t *buf, const char *psz, size_t n) const
+    {
+        CreateIfNeeded() ;
+        OSStatus status = noErr ;
+        ByteCount byteOutLen ;
+        ByteCount byteInLen = strlen(psz) + 1;
+        wchar_t *tbuf = NULL ;
+        UniChar* ubuf = NULL ;
+        size_t res = 0 ;
+        
+        if (buf == NULL)
+        {
+            // Apple specs say at least 32
+            n = wxMax( 32, byteInLen ) ;
+            tbuf = (wchar_t*) malloc( n * SIZEOF_WCHAR_T ) ;
+        }
+        
+        ByteCount byteBufferLen = n * sizeof( UniChar ) ;
+        
+#if SIZEOF_WCHAR_T == 4
+        ubuf = (UniChar*) malloc( byteBufferLen + 2 ) ;
+#else
+        ubuf = (UniChar*) (buf ? buf : tbuf) ;
+#endif
+        
+        ByteCount dcubuflen = byteBufferLen * 2 + 2 ;
+        ByteCount dcubufread , dcubufwritten ;
+        UniChar *dcubuf = (UniChar*) malloc( dcubuflen ) ; 
+
+        status = TECConvertText(
+                                m_MB2WC_converter, (ConstTextPtr) psz, byteInLen, &byteInLen,
+                                (TextPtr) dcubuf, dcubuflen, &byteOutLen);
+        // we have to terminate here, because n might be larger for the trailing zero, and if UniChar
+        // is not properly terminated we get random characters at the end
+        dcubuf[byteOutLen / sizeof( UniChar ) ] = 0 ;
+        
+        // now from the decomposed UniChar to properly composed uniChar
+        ConvertFromUnicodeToText( m_uniBack , byteOutLen , dcubuf , 
+                                  kUnicodeDefaultDirectionMask, 0, NULL, NULL, NULL, dcubuflen  , &dcubufread , &dcubufwritten , ubuf ) ;
+
+        free( dcubuf );
+        byteOutLen = dcubufwritten ;
+        ubuf[byteOutLen / sizeof( UniChar ) ] = 0 ;
+        
+        
+#if SIZEOF_WCHAR_T == 4
+        wxMBConvUTF16 converter ;
+        res = converter.MB2WC( (buf ? buf : tbuf), (const char*)ubuf, n ) ;
+        free( ubuf ) ;
+#else
+        res = byteOutLen / sizeof( UniChar ) ;
+#endif
+        
+        if ( buf == NULL )
+            free(tbuf) ;
+        
+        if ( buf  && res < n)
+            buf[res] = 0;
+        
+        return res ;
+    }
+
     virtual void CreateIfNeeded() const
     {
         wxMBConv_mac::CreateIfNeeded() ;
@@ -2992,10 +3058,19 @@ public :
             
             OSStatus err = CreateUnicodeToTextInfo(&m_map, &m_uni); 
             wxASSERT_MSG( err == noErr , _(" Couldn't create the UnicodeConverter")) ;
+            
+            m_map.unicodeEncoding = CreateTextEncoding(kTextEncodingUnicodeDefault,
+                                                       kUnicodeNoSubset, kTextEncodingDefaultFormat);
+            m_map.otherEncoding = CreateTextEncoding(kTextEncodingUnicodeDefault,
+                                                     kUnicodeCanonicalCompVariant, kTextEncodingDefaultFormat);
+            m_map.mappingVersion = kUnicodeUseLatestMapping;
+            err = CreateUnicodeToTextInfo(&m_map, &m_uniBack); 
+            wxASSERT_MSG( err == noErr , _(" Couldn't create the UnicodeConverter")) ;
         }
     }
 protected :
     mutable UnicodeToTextInfo   m_uni;
+    mutable UnicodeToTextInfo   m_uniBack;
     mutable UnicodeMapping      m_map;
 }; 
 #endif // defined(__WXMAC__) && defined(TARGET_CARBON)
@@ -3385,10 +3460,10 @@ wxMBConv *wxCSConv::DoCreate() const
     // NB: This is a hack to prevent deadlock. What could otherwise happen
     //     in Unicode build: wxConvLocal creation ends up being here
     //     because of some failure and logs the error. But wxLog will try to
-    //     attach timestamp, for which it will need wxConvLocal (to convert
-    //     time to char* and then wchar_t*), but that fails, tries to log
-    //     error, but wxLog has a (already locked) critical section that
-    //     guards static buffer.
+    //     attach timestamp, for which it will need wxConvLocal (to convert
+    //     time to char* and then wchar_t*), but that fails, tries to log the
+    //     error, but wxLog has an (already locked) critical section that
+    //     guards the static buffer.
     static bool alreadyLoggingError = false;
     if (!alreadyLoggingError)
     {
@@ -3507,7 +3582,7 @@ static wxCSConv wxConvLocalObj(wxFONTENCODING_SYSTEM);
 static wxCSConv wxConvISO8859_1Obj(wxFONTENCODING_ISO8859_1);
 static wxMBConvUTF7 wxConvUTF7Obj;
 static wxMBConvUTF8 wxConvUTF8Obj;
-#ifdef __WXOSX__
+#if defined(__WXMAC__) && defined(TARGET_CARBON)
 static wxMBConv_macUTF8D wxConvMacUTF8DObj;
 #endif
 WXDLLIMPEXP_DATA_BASE(wxMBConv&) wxConvLibc = wxConvLibcObj;
@@ -3519,7 +3594,11 @@ WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvCurrent = &wxConvLibcObj;
 WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvUI = &wxConvLocal;
 WXDLLIMPEXP_DATA_BASE(wxMBConv *) wxConvFileName = &
 #ifdef __WXOSX__
+#if defined(__WXMAC__) && defined(TARGET_CARBON)
                                     wxConvMacUTF8DObj;
+#else
+                                    wxConvUTF8Obj;
+#endif
 #else
                                     wxConvLibcObj;
 #endif