fontmap.h 11 KB

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