]> git.saurik.com Git - wxWidgets.git/blob - include/wx/encconv.h
fixed Cygwin compilation (there's no _get_osfhandle)
[wxWidgets.git] / include / wx / encconv.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/encconv.h
3 // Purpose: wxEncodingConverter class for converting between different
4 // font encodings
5 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifndef _WX_ENCCONV_H_
11 #define _WX_ENCCONV_H_
12
13 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
14 #pragma interface "encconv.h"
15 #endif
16
17 #include "wx/defs.h"
18
19 #include "wx/object.h"
20 #include "wx/fontenc.h"
21 #include "wx/dynarray.h"
22
23 // ----------------------------------------------------------------------------
24 // constants
25 // ----------------------------------------------------------------------------
26
27 enum
28 {
29 wxCONVERT_STRICT,
30 wxCONVERT_SUBSTITUTE
31 };
32
33
34 enum
35 {
36 wxPLATFORM_CURRENT = -1,
37
38 wxPLATFORM_UNIX = 0,
39 wxPLATFORM_WINDOWS,
40 wxPLATFORM_OS2,
41 wxPLATFORM_MAC
42 };
43
44 // ----------------------------------------------------------------------------
45 // types
46 // ----------------------------------------------------------------------------
47
48 WX_DEFINE_ARRAY_INT(wxFontEncoding, wxFontEncodingArray);
49
50 //--------------------------------------------------------------------------------
51 // wxEncodingConverter
52 // This class is capable of converting strings between any two
53 // 8bit encodings/charsets. It can also convert from/to Unicode
54 //--------------------------------------------------------------------------------
55
56 class WXDLLIMPEXP_BASE wxEncodingConverter : public wxObject
57 {
58 public:
59
60 wxEncodingConverter();
61 ~wxEncodingConverter() { if (m_Table) delete[] m_Table; }
62
63 // Initialize conversion. Both output or input encoding may
64 // be wxFONTENCODING_UNICODE, but only if wxUSE_WCHAR_T is set to 1.
65 //
66 // All subsequent calls to Convert() will interpret it's argument
67 // as a string in input_enc encoding and will output string in
68 // output_enc encoding.
69 //
70 // You must call this method before calling Convert. You may call
71 // it more than once in order to switch to another conversion
72 //
73 // Method affects behaviour of Convert() in case input character
74 // cannot be converted because it does not exist in output encoding:
75 // wxCONVERT_STRICT --
76 // follow behaviour of GNU Recode - just copy unconvertable
77 // characters to output and don't change them (it's integer
78 // value will stay the same)
79 // wxCONVERT_SUBSTITUTE --
80 // try some (lossy) substitutions - e.g. replace
81 // unconvertable latin capitals with acute by ordinary
82 // capitals, replace en-dash or em-dash by '-' etc.
83 // both modes gurantee that output string will have same length
84 // as input string
85 //
86 // Returns false if given conversion is impossible, true otherwise
87 // (conversion may be impossible either if you try to convert
88 // to Unicode with non-Unicode build of wxWidgets or if input
89 // or output encoding is not supported.)
90 bool Init(wxFontEncoding input_enc, wxFontEncoding output_enc, int method = wxCONVERT_STRICT);
91
92 // Convert input string according to settings passed to Init.
93 // Note that you must call Init before using Convert!
94 bool Convert(const char* input, char* output) const;
95 bool Convert(char* str) const { return Convert(str, str); }
96 wxString Convert(const wxString& input) const;
97
98 #if wxUSE_WCHAR_T
99 bool Convert(const char* input, wchar_t* output) const;
100 bool Convert(const wchar_t* input, char* output) const;
101 bool Convert(const wchar_t* input, wchar_t* output) const;
102 bool Convert(wchar_t* str) const { return Convert(str, str); }
103 #endif
104 // Return equivalent(s) for given font that are used
105 // under given platform. wxPLATFORM_CURRENT means the plaform
106 // this binary was compiled for
107 //
108 // Examples:
109 // current platform enc returned value
110 // -----------------------------------------------------
111 // unix CP1250 {ISO8859_2}
112 // unix ISO8859_2 {}
113 // windows ISO8859_2 {CP1250}
114 //
115 // Equivalence is defined in terms of convertibility:
116 // 2 encodings are equivalent if you can convert text between
117 // then without loosing information (it may - and will - happen
118 // that you loose special chars like quotation marks or em-dashes
119 // but you shouldn't loose any diacritics and language-specific
120 // characters when converting between equivalent encodings).
121 //
122 // Convert() method is not limited to converting between
123 // equivalent encodings, it can convert between arbitrary
124 // two encodings!
125 //
126 // Remember that this function does _NOT_ check for presence of
127 // fonts in system. It only tells you what are most suitable
128 // encodings. (It usually returns only one encoding)
129 //
130 // Note that argument enc itself may be present in returned array!
131 // (so that you can -- as a side effect -- detect whether the
132 // encoding is native for this platform or not)
133 static wxFontEncodingArray GetPlatformEquivalents(wxFontEncoding enc, int platform = wxPLATFORM_CURRENT);
134
135 // Similar to GetPlatformEquivalent, but this one will return ALL
136 // equivalent encodings, regardless the platform, including itself.
137 static wxFontEncodingArray GetAllEquivalents(wxFontEncoding enc);
138
139 // Return true if [any text in] one multibyte encoding can be
140 // converted to another one losslessly.
141 //
142 // Do not call this with wxFONTENCODING_UNICODE, it doesn't make
143 // sense (always works in one sense and always depends on the text
144 // to convert in the other)
145 static bool CanConvert(wxFontEncoding encIn, wxFontEncoding encOut)
146 {
147 return GetAllEquivalents(encIn).Index(encOut) != wxNOT_FOUND;
148 }
149
150 private:
151
152 #if wxUSE_WCHAR_T
153 wchar_t *m_Table;
154 #else
155 char *m_Table;
156 #endif
157 bool m_UnicodeInput, m_UnicodeOutput;
158 bool m_JustCopy;
159
160 DECLARE_NO_COPY_CLASS(wxEncodingConverter)
161 };
162
163 #endif // _WX_ENCCONV_H_