added wxCSConv ctor taking wxFontEncoding
[wxWidgets.git] / include / wx / strconv.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: strconv.h
3 // Purpose: conversion routines for char sets any Unicode
4 // Author: Robert Roebling, Ove Kaaven
5 // Modified by:
6 // Created: 29/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Ove Kaaven, Robert Roebling, Vadim Zeitlin
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_WXSTRCONVH__
13 #define _WX_WXSTRCONVH__
14
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "strconv.h"
17 #endif
18
19 #include "wx/defs.h"
20 #include "wx/wxchar.h"
21 #include "wx/buffer.h"
22
23 #ifdef __DIGITALMARS__
24 #include "typeinfo.h"
25 #endif
26
27 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
28 # undef __BSEXCPT__
29 #endif
30
31 #include <stdlib.h>
32
33 #if wxUSE_WCHAR_T
34
35 // ----------------------------------------------------------------------------
36 // wxMBConv (base class for conversions, using libc conversion itself)
37 // ----------------------------------------------------------------------------
38
39 class WXDLLIMPEXP_BASE wxMBConv
40 {
41 public:
42 // the actual conversion takes place here
43 virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n) const;
44 virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n) const;
45
46 // No longer inline since BC++ complains.
47 const wxWCharBuffer cMB2WC(const char *psz) const;
48 const wxCharBuffer cWC2MB(const wchar_t *psz) const;
49
50 #if wxUSE_UNICODE
51 const wxWCharBuffer cMB2WX(const char *psz) const { return cMB2WC(psz); }
52 const wxCharBuffer cWX2MB(const wchar_t *psz) const { return cWC2MB(psz); }
53 const wchar_t* cWC2WX(const wchar_t *psz) const { return psz; }
54 const wchar_t* cWX2WC(const wchar_t *psz) const { return psz; }
55 #else // ANSI
56 const char* cMB2WX(const char *psz) const { return psz; }
57 const char* cWX2MB(const char *psz) const { return psz; }
58 const wxCharBuffer cWC2WX(const wchar_t *psz) const { return cWC2MB(psz); }
59 const wxWCharBuffer cWX2WC(const char *psz) const { return cMB2WC(psz); }
60 #endif // Unicode/ANSI
61
62 // virtual dtor for any base class
63 virtual ~wxMBConv();
64 };
65
66 WXDLLIMPEXP_DATA_BASE(extern wxMBConv) wxConvLibc;
67
68 // ----------------------------------------------------------------------------
69 // wxMBConvUTF7 (for conversion using UTF7 encoding)
70 // ----------------------------------------------------------------------------
71
72 class WXDLLIMPEXP_BASE wxMBConvUTF7 : public wxMBConv
73 {
74 public:
75 virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n) const;
76 virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n) const;
77 };
78
79 WXDLLIMPEXP_DATA_BASE(extern wxMBConvUTF7) wxConvUTF7;
80
81 // ----------------------------------------------------------------------------
82 // wxMBConvUTF8 (for conversion using UTF8 encoding)
83 // ----------------------------------------------------------------------------
84
85 class WXDLLIMPEXP_BASE wxMBConvUTF8 : public wxMBConv
86 {
87 public:
88 virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n) const;
89 virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n) const;
90 };
91
92 WXDLLIMPEXP_DATA_BASE(extern wxMBConvUTF8) wxConvUTF8;
93
94 // ----------------------------------------------------------------------------
95 // wxCSConv (for conversion based on loadable char sets)
96 // ----------------------------------------------------------------------------
97
98 #include "wx/fontenc.h"
99
100 class WXDLLIMPEXP_BASE wxCharacterSet;
101
102 class WXDLLIMPEXP_BASE wxCSConv : public wxMBConv
103 {
104 public:
105 wxCSConv(const wxChar *charset);
106 wxCSConv(wxFontEncoding encoding);
107 wxCSConv(const wxCSConv& conv);
108 virtual ~wxCSConv();
109
110 wxCSConv& operator=(const wxCSConv& conv);
111
112 void LoadNow();
113
114 virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n) const;
115 virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n) const;
116
117 void Clear() ;
118
119 private:
120 // common part of all ctors
121 void Init();
122
123 void SetName(const wxChar *charset);
124
125 // note that we can't use wxString here because of compilation
126 // dependencies: we're included from wx/string.h
127 wxChar *m_name;
128 wxCharacterSet *m_cset;
129 wxFontEncoding m_encoding;
130 bool m_deferred;
131 };
132
133 #define wxConvFile wxConvLocal
134 WXDLLIMPEXP_DATA_BASE(extern wxCSConv) wxConvLocal;
135 WXDLLIMPEXP_DATA_BASE(extern wxCSConv) wxConvISO8859_1;
136 WXDLLIMPEXP_DATA_BASE(extern wxMBConv *) wxConvCurrent;
137
138 // ----------------------------------------------------------------------------
139 // filename conversion macros
140 // ----------------------------------------------------------------------------
141
142 // filenames are multibyte on Unix and probably widechar on Windows?
143 #if defined(__UNIX__) || defined(__BORLANDC__) || defined(__WXMAC__ )
144 #define wxMBFILES 1
145 #else
146 #define wxMBFILES 0
147 #endif
148
149 #if wxMBFILES && wxUSE_UNICODE
150 #define wxFNCONV(name) wxConvFile.cWX2MB(name)
151 #define wxFNSTRINGCAST wxMBSTRINGCAST
152 #else
153 #define wxFNCONV(name) name
154 #define wxFNSTRINGCAST WXSTRINGCAST
155 #endif
156
157 #else
158 // !wxUSE_WCHAR_T
159
160 // ----------------------------------------------------------------------------
161 // stand-ins in absence of wchar_t
162 // ----------------------------------------------------------------------------
163
164 class WXDLLIMPEXP_BASE wxMBConv
165 {
166 public:
167 const char* cMB2WX(const char *psz) const { return psz; }
168 const char* cWX2MB(const char *psz) const { return psz; }
169 };
170
171 WXDLLIMPEXP_DATA_BASE(extern wxMBConv) wxConvLibc,
172 wxConvFile,
173 wxConvLocal,
174 wxConvISO8859_1,
175 wxConvUTF8;
176 WXDLLIMPEXP_DATA_BASE(extern wxMBConv *) wxConvCurrent;
177
178 #define wxFNCONV(name) name
179 #define wxFNSTRINGCAST WXSTRINGCAST
180
181 #endif
182 // wxUSE_WCHAR_T
183
184 // ----------------------------------------------------------------------------
185 // macros for the most common conversions
186 // ----------------------------------------------------------------------------
187
188 #if wxUSE_UNICODE
189 #define wxConvertWX2MB(s) wxConvCurrent->cWX2MB(s)
190 #define wxConvertMB2WX(s) wxConvCurrent->cMB2WX(s)
191 #else // ANSI
192 // no conversions to do
193 #define wxConvertWX2MB(s) (s)
194 #define wxConvertMB2WX(s) (s)
195 #endif // Unicode/ANSI
196
197 #endif
198 // _WX_WXSTRCONVH__
199