1 ///////////////////////////////////////////////////////////////////////////// 
   3 // Purpose:     wxFontMapper class 
   4 // Author:      Vadim Zeitlin 
   8 // Copyright:   (c) Vadim Zeitlin 
   9 // Licence:     wxWindows licence 
  10 ///////////////////////////////////////////////////////////////////////////// 
  12 #ifndef _WX_FONTMAPPER_H_ 
  13 #define _WX_FONTMAPPER_H_ 
  15 // ---------------------------------------------------------------------------- 
  17 // ---------------------------------------------------------------------------- 
  21 #include "wx/fontenc.h"         // for wxFontEncoding 
  24     #include "wx/fontutil.h"    // for wxNativeEncodingInfo 
  27 #if wxUSE_CONFIG && wxUSE_FILECONFIG 
  28     class WXDLLIMPEXP_BASE wxConfigBase
; 
  29 #endif // wxUSE_CONFIG 
  31 class WXDLLIMPEXP_CORE wxFontMapper
; 
  34     class WXDLLIMPEXP_CORE wxWindow
; 
  37 // ============================================================================ 
  38 // wxFontMapper manages user-definable correspondence between wxWidgets font 
  39 // encodings and the fonts present on the machine. 
  41 // This is a singleton class, font mapper objects can only be accessed using 
  42 // wxFontMapper::Get(). 
  43 // ============================================================================ 
  45 // ---------------------------------------------------------------------------- 
  46 // wxFontMapperBase: this is a non-interactive class which just uses its built 
  47 //                   in knowledge of the encodings equivalence 
  48 // ---------------------------------------------------------------------------- 
  50 class WXDLLIMPEXP_BASE wxFontMapperBase
 
  52     // For IsWxFontMapper() 
  53     friend class WXDLLIMPEXP_CORE wxFontMapper
; 
  55     // constructtor and such 
  56     // --------------------- 
  61     // virtual dtor for any base class 
  62     virtual ~wxFontMapperBase(); 
  64     // return instance of the wxFontMapper singleton 
  65     // wxBase code only cares that it's a wxFontMapperBase 
  66     // In wxBase, wxFontMapper is only forward declared 
  67     // so one cannot implicitly cast from it to wxFontMapperBase. 
  68     static wxFontMapperBase 
*Get(); 
  70     // set the singleton to 'mapper' instance and return previous one 
  71     static wxFontMapper 
*Set(wxFontMapper 
*mapper
); 
  73     // translates charset strings to encoding 
  74     // -------------------------------------- 
  76     // returns the encoding for the given charset (in the form of RFC 2046) or 
  77     // wxFONTENCODING_SYSTEM if couldn't decode it 
  79     // interactive parameter is ignored in the base class, we behave as if it 
  81     virtual wxFontEncoding 
CharsetToEncoding(const wxString
& charset
, 
  82                                              bool interactive 
= true); 
  84     // information about supported encodings 
  85     // ------------------------------------- 
  87     // get the number of font encodings we know about 
  88     static size_t GetSupportedEncodingsCount(); 
  90     // get the n-th supported encoding 
  91     static wxFontEncoding 
GetEncoding(size_t n
); 
  93     // return canonical name of this encoding (this is a short string, 
  94     // GetEncodingDescription() returns a longer one) 
  95     static wxString 
GetEncodingName(wxFontEncoding encoding
); 
  97     // return a list of all names of this encoding (see GetEncodingName) 
  98     static const wxChar
** GetAllEncodingNames(wxFontEncoding encoding
); 
 100     // return user-readable string describing the given encoding 
 102     // NB: hard-coded now, but might change later (read it from config?) 
 103     static wxString 
GetEncodingDescription(wxFontEncoding encoding
); 
 105     // find the encoding corresponding to the given name, inverse of 
 106     // GetEncodingName() and less general than CharsetToEncoding() 
 108     // returns wxFONTENCODING_MAX if the name is not a supported encoding 
 109     static wxFontEncoding 
GetEncodingFromName(const wxString
& name
); 
 112     // functions which allow to configure the config object used: by default, 
 113     // the global one (from wxConfigBase::Get() will be used) and the default 
 114     // root path for the config settings is the string returned by 
 115     // GetDefaultConfigPath() 
 116     // ---------------------------------------------------------------------- 
 118 #if wxUSE_CONFIG && wxUSE_FILECONFIG 
 119     // set the root config path to use (should be an absolute path) 
 120     void SetConfigPath(const wxString
& prefix
); 
 122     // return default config path 
 123     static const wxChar 
*GetDefaultConfigPath(); 
 124 #endif // wxUSE_CONFIG 
 128 #if wxUSE_CONFIG && wxUSE_FILECONFIG 
 129     // get the config object we're using -- either the global config object 
 130     // or a wxMemoryConfig object created by this class otherwise 
 131     wxConfigBase 
*GetConfig(); 
 133     // gets the root path for our settings -- if it wasn't set explicitly, use 
 134     // GetDefaultConfigPath() 
 135     const wxString
& GetConfigPath(); 
 137     // change to the given (relative) path in the config, return true if ok 
 138     // (then GetConfig() will return something !NULL), false if no config 
 141     // caller should provide a pointer to the string variable which should be 
 142     // later passed to RestorePath() 
 143     bool ChangePath(const wxString
& pathNew
, wxString 
*pathOld
); 
 145     // restore the config path after use 
 146     void RestorePath(const wxString
& pathOld
); 
 148     // config object and path (in it) to use 
 149     wxConfigBase 
*m_configDummy
; 
 151     wxString m_configRootPath
; 
 152 #endif // wxUSE_CONFIG 
 154     // the real implementation of the base class version of CharsetToEncoding() 
 156     // returns wxFONTENCODING_UNKNOWN if encoding is unknown and we shouldn't 
 157     // ask the user about it, wxFONTENCODING_SYSTEM if it is unknown but we 
 158     // should/could ask the user 
 159     int NonInteractiveCharsetToEncoding(const wxString
& charset
); 
 162     // pseudo-RTTI since we aren't a wxObject. 
 163     virtual bool IsWxFontMapper(); 
 165     // the global fontmapper object or NULL 
 166     static wxFontMapper 
*sm_instance
; 
 168     friend class wxFontMapperPathChanger
; 
 170     DECLARE_NO_COPY_CLASS(wxFontMapperBase
) 
 173 // ---------------------------------------------------------------------------- 
 174 // wxFontMapper: interactive extension of wxFontMapperBase 
 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 // ---------------------------------------------------------------------------- 
 188 class WXDLLIMPEXP_CORE wxFontMapper 
: public wxFontMapperBase
 
 194     // virtual dtor for a base class 
 195     virtual ~wxFontMapper(); 
 197     // working with the encodings 
 198     // -------------------------- 
 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); 
 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 
 209     virtual bool GetAltForEncoding(wxFontEncoding encoding
, 
 210                                    wxNativeEncodingInfo 
*info
, 
 211                                    const wxString
& facename 
= wxEmptyString
, 
 212                                    bool interactive 
= true); 
 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); 
 221     // checks whether given encoding is available in given face or not. 
 223     // if no facename is given (default), return true if it's available in any 
 225     virtual bool IsEncodingAvailable(wxFontEncoding encoding
, 
 226                                      const wxString
& facename 
= wxEmptyString
); 
 229     // configure the appearance of the dialogs we may popup 
 230     // ---------------------------------------------------- 
 232     // the parent window for modal dialogs 
 233     void SetDialogParent(wxWindow 
*parent
) { m_windowParent 
= parent
; } 
 235     // the title for the dialogs (note that default is quite reasonable) 
 236     void SetDialogTitle(const wxString
& title
) { m_titleDialog 
= title
; } 
 238     // GUI code needs to know it's a wxFontMapper because there 
 239     // are additional methods in the subclass. 
 240     static wxFontMapper 
*Get(); 
 243     // GetAltForEncoding() helper: tests for the existence of the given 
 244     // encoding and saves the result in config if ok - this results in the 
 245     // following (desired) behaviour: when an unknown/unavailable encoding is 
 246     // requested for the first time, the user is asked about a replacement, 
 247     // but if he doesn't choose any and the default logic finds one, it will 
 248     // be saved in the config so that the user won't be asked about it any 
 250     bool TestAltEncoding(const wxString
& configEntry
, 
 251                          wxFontEncoding encReplacement
, 
 252                          wxNativeEncodingInfo 
*info
); 
 254     // the title for our dialogs 
 255     wxString m_titleDialog
; 
 257     // the parent window for our dialogs 
 258     wxWindow 
*m_windowParent
; 
 261     // pseudo-RTTI since we aren't a wxObject. 
 262     virtual bool IsWxFontMapper(); 
 264     DECLARE_NO_COPY_CLASS(wxFontMapper
) 
 269 // ---------------------------------------------------------------------------- 
 271 // ---------------------------------------------------------------------------- 
 273 // the default font mapper for wxWidgets programs do NOT use! This is for 
 274 // backward compatibility, use wxFontMapper::Get() instead 
 275 #define wxTheFontMapper (wxFontMapper::Get()) 
 277 #else // !wxUSE_FONTMAP 
 280     // wxEncodingToCodepage (utils.cpp) needs wxGetNativeFontEncoding 
 281     #include "wx/fontutil.h" 
 284 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP 
 286 #endif // _WX_FONTMAPPER_H_