1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFontMapper class
4 // Author: Vadim Zeitlin
7 // Copyright: (c) Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_FONTMAPPER_H_
12 #define _WX_FONTMAPPER_H_
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
20 #include "wx/fontenc.h" // for wxFontEncoding
23 #include "wx/fontutil.h" // for wxNativeEncodingInfo
26 #if wxUSE_CONFIG && wxUSE_FILECONFIG
27 class WXDLLIMPEXP_FWD_BASE wxConfigBase
;
28 #endif // wxUSE_CONFIG
30 class WXDLLIMPEXP_FWD_CORE wxFontMapper
;
33 class WXDLLIMPEXP_FWD_CORE wxWindow
;
36 // ============================================================================
37 // wxFontMapper manages user-definable correspondence between wxWidgets font
38 // encodings and the fonts present on the machine.
40 // This is a singleton class, font mapper objects can only be accessed using
41 // wxFontMapper::Get().
42 // ============================================================================
44 // ----------------------------------------------------------------------------
45 // wxFontMapperBase: this is a non-interactive class which just uses its built
46 // in knowledge of the encodings equivalence
47 // ----------------------------------------------------------------------------
49 class WXDLLIMPEXP_BASE wxFontMapperBase
52 // constructor and such
53 // ---------------------
58 // virtual dtor for any base class
59 virtual ~wxFontMapperBase();
61 // return instance of the wxFontMapper singleton
62 // wxBase code only cares that it's a wxFontMapperBase
63 // In wxBase, wxFontMapper is only forward declared
64 // so one cannot implicitly cast from it to wxFontMapperBase.
65 static wxFontMapperBase
*Get();
67 // set the singleton to 'mapper' instance and return previous one
68 static wxFontMapper
*Set(wxFontMapper
*mapper
);
70 // delete the existing font mapper if any
74 // translates charset strings to encoding
75 // --------------------------------------
77 // returns the encoding for the given charset (in the form of RFC 2046) or
78 // wxFONTENCODING_SYSTEM if couldn't decode it
80 // interactive parameter is ignored in the base class, we behave as if it
82 virtual wxFontEncoding
CharsetToEncoding(const wxString
& charset
,
83 bool interactive
= true);
85 // information about supported encodings
86 // -------------------------------------
88 // get the number of font encodings we know about
89 static size_t GetSupportedEncodingsCount();
91 // get the n-th supported encoding
92 static wxFontEncoding
GetEncoding(size_t n
);
94 // return canonical name of this encoding (this is a short string,
95 // GetEncodingDescription() returns a longer one)
96 static wxString
GetEncodingName(wxFontEncoding encoding
);
98 // return a list of all names of this encoding (see GetEncodingName)
99 static const wxChar
** GetAllEncodingNames(wxFontEncoding encoding
);
101 // return user-readable string describing the given encoding
103 // NB: hard-coded now, but might change later (read it from config?)
104 static wxString
GetEncodingDescription(wxFontEncoding encoding
);
106 // find the encoding corresponding to the given name, inverse of
107 // GetEncodingName() and less general than CharsetToEncoding()
109 // returns wxFONTENCODING_MAX if the name is not a supported encoding
110 static wxFontEncoding
GetEncodingFromName(const wxString
& name
);
113 // functions which allow to configure the config object used: by default,
114 // the global one (from wxConfigBase::Get() will be used) and the default
115 // root path for the config settings is the string returned by
116 // GetDefaultConfigPath()
117 // ----------------------------------------------------------------------
119 #if wxUSE_CONFIG && wxUSE_FILECONFIG
120 // set the root config path to use (should be an absolute path)
121 void SetConfigPath(const wxString
& prefix
);
123 // return default config path
124 static const wxString
& GetDefaultConfigPath();
125 #endif // wxUSE_CONFIG
128 // returns true for the base class and false for a "real" font mapper object
129 // (implementation-only)
130 virtual bool IsDummy() { return true; }
133 #if wxUSE_CONFIG && wxUSE_FILECONFIG
134 // get the config object we're using -- either the global config object
135 // or a wxMemoryConfig object created by this class otherwise
136 wxConfigBase
*GetConfig();
138 // gets the root path for our settings -- if it wasn't set explicitly, use
139 // GetDefaultConfigPath()
140 const wxString
& GetConfigPath();
142 // change to the given (relative) path in the config, return true if ok
143 // (then GetConfig() will return something !NULL), false if no config
146 // caller should provide a pointer to the string variable which should be
147 // later passed to RestorePath()
148 bool ChangePath(const wxString
& pathNew
, wxString
*pathOld
);
150 // restore the config path after use
151 void RestorePath(const wxString
& pathOld
);
153 // config object and path (in it) to use
154 wxConfigBase
*m_configDummy
;
156 wxString m_configRootPath
;
157 #endif // wxUSE_CONFIG
159 // the real implementation of the base class version of CharsetToEncoding()
161 // returns wxFONTENCODING_UNKNOWN if encoding is unknown and we shouldn't
162 // ask the user about it, wxFONTENCODING_SYSTEM if it is unknown but we
163 // should/could ask the user
164 int NonInteractiveCharsetToEncoding(const wxString
& charset
);
167 // the global fontmapper object or NULL
168 static wxFontMapper
*sm_instance
;
170 friend class wxFontMapperPathChanger
;
172 wxDECLARE_NO_COPY_CLASS(wxFontMapperBase
);
175 // ----------------------------------------------------------------------------
176 // wxFontMapper: interactive extension of wxFontMapperBase
178 // The default implementations of all functions will ask the user if they are
179 // not capable of finding the answer themselves and store the answer in a
180 // config file (configurable via SetConfigXXX functions). This behaviour may
181 // be disabled by giving the value of false to "interactive" parameter.
182 // However, the functions will always consult the config file to allow the
183 // user-defined values override the default logic and there is no way to
184 // disable this -- which shouldn't be ever needed because if "interactive" was
185 // never true, the config file is never created anyhow.
186 // ----------------------------------------------------------------------------
190 class WXDLLIMPEXP_CORE wxFontMapper
: public wxFontMapperBase
196 // virtual dtor for a base class
197 virtual ~wxFontMapper();
199 // working with the encodings
200 // --------------------------
202 // returns the encoding for the given charset (in the form of RFC 2046) or
203 // wxFONTENCODING_SYSTEM if couldn't decode it
204 virtual wxFontEncoding
CharsetToEncoding(const wxString
& charset
,
205 bool interactive
= true);
207 // find an alternative for the given encoding (which is supposed to not be
208 // available on this system). If successful, return true and fill info
209 // structure with the parameters required to create the font, otherwise
211 virtual bool GetAltForEncoding(wxFontEncoding encoding
,
212 wxNativeEncodingInfo
*info
,
213 const wxString
& facename
= wxEmptyString
,
214 bool interactive
= true);
216 // version better suitable for 'public' use. Returns wxFontEcoding
217 // that can be used it wxFont ctor
218 bool GetAltForEncoding(wxFontEncoding encoding
,
219 wxFontEncoding
*alt_encoding
,
220 const wxString
& facename
= wxEmptyString
,
221 bool interactive
= true);
223 // checks whether given encoding is available in given face or not.
225 // if no facename is given (default), return true if it's available in any
227 virtual bool IsEncodingAvailable(wxFontEncoding encoding
,
228 const wxString
& facename
= wxEmptyString
);
231 // configure the appearance of the dialogs we may popup
232 // ----------------------------------------------------
234 // the parent window for modal dialogs
235 void SetDialogParent(wxWindow
*parent
) { m_windowParent
= parent
; }
237 // the title for the dialogs (note that default is quite reasonable)
238 void SetDialogTitle(const wxString
& title
) { m_titleDialog
= title
; }
240 // GUI code needs to know it's a wxFontMapper because there
241 // are additional methods in the subclass.
242 static wxFontMapper
*Get();
244 // pseudo-RTTI since we aren't a wxObject.
245 virtual bool IsDummy() { return false; }
248 // GetAltForEncoding() helper: tests for the existence of the given
249 // encoding and saves the result in config if ok - this results in the
250 // following (desired) behaviour: when an unknown/unavailable encoding is
251 // requested for the first time, the user is asked about a replacement,
252 // but if he doesn't choose any and the default logic finds one, it will
253 // be saved in the config so that the user won't be asked about it any
255 bool TestAltEncoding(const wxString
& configEntry
,
256 wxFontEncoding encReplacement
,
257 wxNativeEncodingInfo
*info
);
259 // the title for our dialogs
260 wxString m_titleDialog
;
262 // the parent window for our dialogs
263 wxWindow
*m_windowParent
;
266 wxDECLARE_NO_COPY_CLASS(wxFontMapper
);
271 // ----------------------------------------------------------------------------
273 // ----------------------------------------------------------------------------
275 // the default font mapper for wxWidgets programs do NOT use! This is for
276 // backward compatibility, use wxFontMapper::Get() instead
277 #define wxTheFontMapper (wxFontMapper::Get())
279 #else // !wxUSE_FONTMAP
282 // wxEncodingToCodepage (utils.cpp) needs wxGetNativeFontEncoding
283 #include "wx/fontutil.h"
286 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
288 #endif // _WX_FONTMAPPER_H_