]> git.saurik.com Git - wxWidgets.git/blobdiff - include/wx/string.h
[ 1713933 ] docs for wxEditableListBox
[wxWidgets.git] / include / wx / string.h
index c81e68b396b1fc57cc1ebc2fbf9b0b4012c75e96..dac27573fa722cdd517a24d943101786b4fc0716 100644 (file)
@@ -201,7 +201,7 @@ public:
     const wchar_t* AsWChar() const;
     operator const wchar_t*() const { return AsWChar(); }
 
-#if !wxUSE_UNICODE
+#if !wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY
     inline
 #endif
     const char* AsChar() const;
@@ -297,8 +297,14 @@ class WXDLLIMPEXP_BASE wxStringPrintfMixinBase
 protected:
     wxStringPrintfMixinBase() {}
 
-    int DoPrintf(const wxString& format, ...);
-    static wxString DoFormat(const wxString& format, ...);
+#if !wxUSE_UTF8_LOCALE_ONLY
+    int DoPrintfWchar(const wxChar *format, ...);
+    static wxString DoFormatWchar(const wxChar *format, ...);
+#endif
+#if wxUSE_UNICODE_UTF8
+    int DoPrintfUtf8(const char *format, ...);
+    static wxString DoFormatUtf8(const char *format, ...);
+#endif
 };
 
 // this class contains template wrappers for wxString's vararg methods, it's
@@ -322,9 +328,9 @@ public:
     // if !wxNEEDS_WXSTRING_PRINTF_MIXIN:
 
     // static wxString Format(const wString& format, ...) ATTRIBUTE_PRINTF_1;
-    WX_DEFINE_VARARG_FUNC2_SANS_N0(static typename StringReturnType<T1>::type,
-                                   Format, 1, (const wxString&),
-                                   DoFormat, DoFormat)
+    WX_DEFINE_VARARG_FUNC_SANS_N0(static typename StringReturnType<T1>::type,
+                                  Format, 1, (const wxFormatString&),
+                                  DoFormatWchar, DoFormatUtf8)
     // We have to implement the version without template arguments manually
     // because of the StringReturnType<> hack, although WX_DEFINE_VARARG_FUNC
     // normally does it itself. It has to be a template so that we can use
@@ -333,15 +339,17 @@ public:
 
     template<typename T>
     inline static typename StringReturnType<T>::type
-    Format(const wxString& fmt, FormatDummyArg dummy = FormatDummyArg())
+    Format(const wxFormatString& fmt, FormatDummyArg dummy = FormatDummyArg())
     {
-        return DoFormat(fmt);
+        return DoFormatWchar(fmt);
     }
 
     // int Printf(const wxString& format, ...);
-    WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxString&), DoPrintf)
+    WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxFormatString&),
+                          DoPrintfWchar, DoPrintfUtf8)
     // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
-    WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wxString&), DoPrintf)
+    WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wxFormatString&),
+                          DoPrintfWchar, DoPrintfUtf8)
 
 protected:
     wxStringPrintfMixin() : wxStringPrintfMixinBase() {}
@@ -469,9 +477,6 @@ private:
 
 #else // wxUSE_UNICODE_UTF8
 
-  // FIXME-UTF8: return as-is without copying under UTF8 locale, return
-  //             converted string under other locales - needs wxCharBuffer
-  //             changes
   static wxCharBuffer ImplStr(const char* str,
                               const wxMBConv& conv = wxConvLibc)
     { return ConvertStr(str, npos, conv).data; }
@@ -923,8 +928,7 @@ public:
         { return wxStdWideString(wc_str()); }
   #endif
 
-  #if !wxUSE_UNICODE && wxUSE_STL_BASED_WXSTRING
-    // FIXME-UTF8: do this in UTF8 build #if wxUSE_UTF8_LOCALE_ONLY, too
+  #if (!wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY) && wxUSE_STL_BASED_WXSTRING
     // wxStringImpl is std::string in the encoding we want
     operator const std::string&() const { return m_impl; }
   #else
@@ -933,8 +937,7 @@ public:
         // FIXME-UTF8: broken for embedded NULs
         { return std::string(mb_str()); }
   #endif
-
-#endif // wxUSE_STD_STRING
+#endif // wxUSE_STL
 
   // first valid index position
   const_iterator begin() const { return const_iterator(m_impl.begin()); }
@@ -1126,6 +1129,40 @@ public:
     const char *ToAscii() const { return c_str(); }
 #endif // Unicode/!Unicode
 
+    // conversion to/from UTF-8:
+#if wxUSE_UNICODE_UTF8
+    static wxString FromUTF8(const char *utf8)
+    {
+      wxASSERT( wxStringOperations::IsValidUtf8String(utf8) );
+      return FromImpl(wxStringImpl(utf8));
+    }
+    static wxString FromUTF8(const char *utf8, size_t len)
+    {
+      wxASSERT( wxStringOperations::IsValidUtf8String(utf8, len) );
+      return FromImpl(wxStringImpl(utf8, len));
+    }
+    const char* utf8_str() const { return wx_str(); }
+    const char* ToUTF8() const { return wx_str(); }
+#elif wxUSE_UNICODE_WCHAR
+    static wxString FromUTF8(const char *utf8)
+      { return wxString(utf8, wxConvUTF8); }
+    static wxString FromUTF8(const char *utf8, size_t len)
+      { return wxString(utf8, wxConvUTF8, len); }
+    const wxCharBuffer utf8_str() const { return mb_str(wxConvUTF8); }
+    const wxCharBuffer ToUTF8() const { return utf8_str(); }
+#else // ANSI
+    static wxString FromUTF8(const char *utf8)
+      { return wxString(wxConvUTF8.cMB2WC(utf8)); }
+    static wxString FromUTF8(const char *utf8, size_t len)
+    {
+      size_t wlen;
+      wxWCharBuffer buf(wxConvUTF8.cMB2WC(utf8, len == npos ? wxNO_LEN : len, &wlen));
+      return wxString(buf.data(), wlen);
+    }
+    const wxCharBuffer utf8_str() const { return wxConvUTF8.cWC2MB(wc_str()); }
+    const wxCharBuffer ToUTF8() const { return utf8_str(); }
+#endif
+
     // functions for storing binary data in wxString:
 #if wxUSE_UNICODE
     static wxString From8BitData(const char *data, size_t len)
@@ -1153,7 +1190,13 @@ public:
     // type differs because a function may either return pointer to the buffer
     // directly or have to use intermediate buffer for translation.
 #if wxUSE_UNICODE
+
+#if wxUSE_UTF8_LOCALE_ONLY
+    const char* mb_str() const { return wx_str(); }
+    const wxCharBuffer mb_str(const wxMBConv& conv) const;
+#else
     const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const;
+#endif
 
     const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); }
 
@@ -1533,11 +1576,18 @@ public:
     // as sprintf(), returns the number of characters written or < 0 on error
     // (take 'this' into account in attribute parameter count)
   // int Printf(const wxString& format, ...);
-  WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxString&), DoPrintf)
+  WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxFormatString&),
+                        DoPrintfWchar, DoPrintfUtf8)
 #ifdef __WATCOMC__
-  WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const char*), DoPrintf)
-  WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wchar_t*), DoPrintf)
-  WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxCStrData&), DoPrintf)
+  // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
+  WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wxString&),
+                              (wxFormatString(f1)));
+  WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wxCStrData&),
+                              (wxFormatString(f1)));
+  WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const char*),
+                              (wxFormatString(f1)));
+  WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wchar_t*),
+                              (wxFormatString(f1)));
 #endif
 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
     // as vprintf(), returns the number of characters written or < 0 on error
@@ -1546,12 +1596,18 @@ public:
 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
     // returns the string containing the result of Printf() to it
   // static wxString Format(const wxString& format, ...) ATTRIBUTE_PRINTF_1;
-  WX_DEFINE_VARARG_FUNC(static wxString, Format, 1, (const wxString&), DoFormat)
+  WX_DEFINE_VARARG_FUNC(static wxString, Format, 1, (const wxFormatString&),
+                        DoFormatWchar, DoFormatUtf8)
 #ifdef __WATCOMC__
   // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
-  WX_DEFINE_VARARG_FUNC(static wxString, Format, 1, (const char*), DoFormat)
-  WX_DEFINE_VARARG_FUNC(static wxString, Format, 1, (const wchar_t*), DoFormat)
-  WX_DEFINE_VARARG_FUNC(static wxString, Format, 1, (const wxCStrData&), DoFormat)
+  WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wxString&),
+                              (wxFormatString(f1)));
+  WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wxCStrData&),
+                              (wxFormatString(f1)));
+  WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const char*),
+                              (wxFormatString(f1)));
+  WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wchar_t*),
+                              (wxFormatString(f1)));
 #endif
 #endif
     // the same as above, but takes a va_list
@@ -1589,12 +1645,18 @@ public:
   // use Printf()
   // (take 'this' into account in attribute parameter count)
   // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
-  WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wxString&), DoPrintf)
+  WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wxFormatString&),
+                        DoPrintfWchar, DoPrintfUtf8)
 #ifdef __WATCOMC__
   // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
-  WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const char*), DoPrintf)
-  WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wchar_t*), DoPrintf)
-  WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wxCStrData&), DoPrintf)
+  WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wxString&),
+                              (wxFormatString(f1)));
+  WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wxCStrData&),
+                              (wxFormatString(f1)));
+  WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const char*),
+                              (wxFormatString(f1)));
+  WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wchar_t*),
+                              (wxFormatString(f1)));
 #endif
 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
 
@@ -2369,8 +2431,14 @@ private:
 #endif // !wxUSE_STL_BASED_WXSTRING
 
 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
-  int DoPrintf(const wxString& format, ...);
-  static wxString DoFormat(const wxString& format, ...);
+  #if !wxUSE_UTF8_LOCALE_ONLY
+  int DoPrintfWchar(const wxChar *format, ...);
+  static wxString DoFormatWchar(const wxChar *format, ...);
+  #endif
+  #if wxUSE_UNICODE_UTF8
+  int DoPrintfUtf8(const char *format, ...);
+  static wxString DoFormatUtf8(const char *format, ...);
+  #endif
 #endif
 
 #if !wxUSE_STL_BASED_WXSTRING
@@ -2402,7 +2470,7 @@ private:
 
       T *m_buf;
   };
-#if wxUSE_UNICODE
+#if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
   ConvertedBuffer<char> m_convertedToChar;
 #endif
 #if !wxUSE_UNICODE_WCHAR
@@ -2795,10 +2863,10 @@ inline const wchar_t* wxCStrData::AsWChar() const
 }
 #endif // wxUSE_UNICODE_WCHAR
 
-#if !wxUSE_UNICODE
+#if !wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY
 inline const char* wxCStrData::AsChar() const
 {
-    return m_str->wx_str() + m_offset;
+    return wxStringOperations::AddToIter(m_str->wx_str(), m_offset);
 }
 #endif // !wxUSE_UNICODE