]> git.saurik.com Git - wxWidgets.git/blob - include/wx/fontmap.h
2dee4a244b4a00675725db764009d349f6477a6a
[wxWidgets.git] / include / wx / fontmap.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/fontmap.h
3 // Purpose: wxFontMapper class
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 04.11.99
7 // RCS-ID: $Id$
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_FONTMAPPER_H_
13 #define _WX_FONTMAPPER_H_
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 #if wxUSE_FONTMAP
20
21 #include "wx/fontenc.h" // for wxFontEncoding
22
23 #if wxUSE_GUI
24 #include "wx/fontutil.h" // for wxNativeEncodingInfo
25 #endif // wxUSE_GUI
26
27 #if wxUSE_CONFIG && wxUSE_FILECONFIG
28 class WXDLLIMPEXP_BASE wxConfigBase;
29 #endif // wxUSE_CONFIG
30
31 class WXDLLIMPEXP_CORE wxFontMapper;
32
33 #if wxUSE_GUI
34 class WXDLLIMPEXP_CORE wxWindow;
35 #endif // wxUSE_GUI
36
37 // ============================================================================
38 // wxFontMapper manages user-definable correspondence between wxWidgets font
39 // encodings and the fonts present on the machine.
40 //
41 // This is a singleton class, font mapper objects can only be accessed using
42 // wxFontMapper::Get().
43 // ============================================================================
44
45 // ----------------------------------------------------------------------------
46 // wxFontMapperBase: this is a non-interactive class which just uses its built
47 // in knowledge of the encodings equivalence
48 // ----------------------------------------------------------------------------
49
50 class WXDLLIMPEXP_BASE wxFontMapperBase
51 {
52 public:
53 // constructtor and such
54 // ---------------------
55
56 // default ctor
57 wxFontMapperBase();
58
59 // virtual dtor for any base class
60 virtual ~wxFontMapperBase();
61
62 // return instance of the wxFontMapper singleton
63 #if wxUSE_GUI
64 // GUI code needs to know it's a wxFontMapper because there
65 // are additional methods in the subclass.
66 static wxFontMapper *Get();
67 #else
68 // wxBase code only cares that it's a wxFontMapperBase
69 static wxFontMapperBase *Get();
70 #endif
71
72 // set the singleton to 'mapper' instance and return previous one
73 static wxFontMapper *Set(wxFontMapper *mapper);
74
75 // translates charset strings to encoding
76 // --------------------------------------
77
78 // returns the encoding for the given charset (in the form of RFC 2046) or
79 // wxFONTENCODING_SYSTEM if couldn't decode it
80 //
81 // interactive parameter is ignored in the base class, we behave as if it
82 // were always false
83 virtual wxFontEncoding CharsetToEncoding(const wxString& charset,
84 bool interactive = true);
85
86 // information about supported encodings
87 // -------------------------------------
88
89 // get the number of font encodings we know about
90 static size_t GetSupportedEncodingsCount();
91
92 // get the n-th supported encoding
93 static wxFontEncoding GetEncoding(size_t n);
94
95 // return internal string identifier for the encoding (see also
96 // GetEncodingDescription())
97 static wxString GetEncodingName(wxFontEncoding encoding);
98
99 // return user-readable string describing the given encoding
100 //
101 // NB: hard-coded now, but might change later (read it from config?)
102 static wxString GetEncodingDescription(wxFontEncoding encoding);
103
104 // find the encoding corresponding to the given name, inverse of
105 // GetEncodingName() and less general than CharsetToEncoding()
106 //
107 // returns wxFONTENCODING_MAX if the name is not a supported encoding
108 static wxFontEncoding GetEncodingFromName(const wxString& name);
109
110
111 // functions which allow to configure the config object used: by default,
112 // the global one (from wxConfigBase::Get() will be used) and the default
113 // root path for the config settings is the string returned by
114 // GetDefaultConfigPath()
115 // ----------------------------------------------------------------------
116
117 #if wxUSE_CONFIG && wxUSE_FILECONFIG
118 // set the config object to use (may be NULL to use default)
119 void SetConfig(wxConfigBase *config) { m_config = config; }
120
121 // set the root config path to use (should be an absolute path)
122 void SetConfigPath(const wxString& prefix);
123
124 // return default config path
125 static const wxChar *GetDefaultConfigPath();
126 #endif // wxUSE_CONFIG
127
128
129 protected:
130 #if wxUSE_CONFIG && wxUSE_FILECONFIG
131 // get the config object we're using -- if it wasn't set explicitly, this
132 // function will use wxConfig::Get() to get the global one
133 wxConfigBase *GetConfig();
134
135 // gets the root path for our settings -- if it wasn't set explicitly, use
136 // GetDefaultConfigPath()
137 const wxString& GetConfigPath();
138
139 // change to the given (relative) path in the config, return true if ok
140 // (then GetConfig() will return something !NULL), false if no config
141 // object
142 //
143 // caller should provide a pointer to the string variable which should be
144 // later passed to RestorePath()
145 bool ChangePath(const wxString& pathNew, wxString *pathOld);
146
147 // restore the config path after use
148 void RestorePath(const wxString& pathOld);
149
150 // config object and path (in it) to use
151 wxConfigBase *m_config;
152 bool m_configIsDummy;
153
154 wxString m_configRootPath;
155 #endif // wxUSE_CONFIG
156
157 // the real implementation of the base class version of CharsetToEncoding()
158 //
159 // returns wxFONTENCODING_UNKNOWN if encoding is unknown and we shouldn't
160 // ask the user about it, wxFONTENCODING_SYSTEM if it is unknown but we
161 // should/could ask the user
162 int NonInteractiveCharsetToEncoding(const wxString& charset);
163
164 private:
165 // the global fontmapper object or NULL
166 static wxFontMapper *sm_instance;
167
168 friend class wxFontMapperPathChanger;
169
170 DECLARE_NO_COPY_CLASS(wxFontMapperBase)
171 };
172
173 // ----------------------------------------------------------------------------
174 // wxFontMapper: interactive extension of wxFontMapperBase
175 //
176 // The default implementations of all functions will ask the user if they are
177 // not capable of finding the answer themselves and store the answer in a
178 // config file (configurable via SetConfigXXX functions). This behaviour may
179 // be disabled by giving the value of false to "interactive" parameter.
180 // However, the functions will always consult the config file to allow the
181 // user-defined values override the default logic and there is no way to
182 // disable this -- which shouldn't be ever needed because if "interactive" was
183 // never true, the config file is never created anyhow.
184 // ----------------------------------------------------------------------------
185
186 #if wxUSE_GUI
187
188 class WXDLLIMPEXP_CORE wxFontMapper : public wxFontMapperBase
189 {
190 public:
191 // default ctor
192 wxFontMapper();
193
194 // virtual dtor for a base class
195 virtual ~wxFontMapper();
196
197 // working with the encodings
198 // --------------------------
199
200 // returns the encoding for the given charset (in the form of RFC 2046) or
201 // wxFONTENCODING_SYSTEM if couldn't decode it
202 virtual wxFontEncoding CharsetToEncoding(const wxString& charset,
203 bool interactive = true);
204
205 // find an alternative for the given encoding (which is supposed to not be
206 // available on this system). If successful, return true and fill info
207 // structure with the parameters required to create the font, otherwise
208 // return false
209 virtual bool GetAltForEncoding(wxFontEncoding encoding,
210 wxNativeEncodingInfo *info,
211 const wxString& facename = wxEmptyString,
212 bool interactive = true);
213
214 // version better suitable for 'public' use. Returns wxFontEcoding
215 // that can be used it wxFont ctor
216 bool GetAltForEncoding(wxFontEncoding encoding,
217 wxFontEncoding *alt_encoding,
218 const wxString& facename = wxEmptyString,
219 bool interactive = true);
220
221 // checks whether given encoding is available in given face or not.
222 //
223 // if no facename is given (default), return true if it's available in any
224 // facename at alll.
225 virtual bool IsEncodingAvailable(wxFontEncoding encoding,
226 const wxString& facename = wxEmptyString);
227
228
229 // configure the appearance of the dialogs we may popup
230 // ----------------------------------------------------
231
232 // the parent window for modal dialogs
233 void SetDialogParent(wxWindow *parent) { m_windowParent = parent; }
234
235 // the title for the dialogs (note that default is quite reasonable)
236 void SetDialogTitle(const wxString& title) { m_titleDialog = title; }
237
238
239 protected:
240 // GetAltForEncoding() helper: tests for the existence of the given
241 // encoding and saves the result in config if ok - this results in the
242 // following (desired) behaviour: when an unknown/unavailable encoding is
243 // requested for the first time, the user is asked about a replacement,
244 // but if he doesn't choose any and the default logic finds one, it will
245 // be saved in the config so that the user won't be asked about it any
246 // more
247 bool TestAltEncoding(const wxString& configEntry,
248 wxFontEncoding encReplacement,
249 wxNativeEncodingInfo *info);
250
251 // the title for our dialogs
252 wxString m_titleDialog;
253
254 // the parent window for our dialogs
255 wxWindow *m_windowParent;
256
257 private:
258 DECLARE_NO_COPY_CLASS(wxFontMapper)
259 };
260
261 #endif // wxUSE_GUI
262
263 // ----------------------------------------------------------------------------
264 // global variables
265 // ----------------------------------------------------------------------------
266
267 // the default font mapper for wxWidgets programs do NOT use! This is for
268 // backward compatibility, use wxFontMapper::Get() instead
269 #define wxTheFontMapper (wxFontMapper::Get())
270
271 #else // !wxUSE_FONTMAP
272
273 #if wxUSE_GUI
274 // wxEncodingToCodepage (utils.cpp) needs wxGetNativeFontEncoding
275 #include "wx/fontutil.h"
276 #endif
277
278 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
279
280 #endif // _WX_FONTMAPPER_H_
281