font.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: font.cpp
  3. // Purpose: wxFont demo
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 30.09.99
  7. // Copyright: (c) 1999 Vadim Zeitlin
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // For compilers that support precompilation, includes "wx/wx.h".
  11. #include "wx/wxprec.h"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. // for all others, include the necessary headers (this file is usually all you
  16. // need because it includes almost all standard wxWidgets headers
  17. #ifndef WX_PRECOMP
  18. #include "wx/wx.h"
  19. #include "wx/log.h"
  20. #endif
  21. #include "wx/choicdlg.h"
  22. #include "wx/fontdlg.h"
  23. #include "wx/fontenum.h"
  24. #include "wx/fontmap.h"
  25. #include "wx/encconv.h"
  26. #include "wx/splitter.h"
  27. #include "wx/textfile.h"
  28. #include "wx/settings.h"
  29. #include "../sample.xpm"
  30. #ifdef __WXMAC__
  31. #undef wxFontDialog
  32. #include "wx/osx/fontdlg.h"
  33. #endif
  34. // used as title for several dialog boxes
  35. static const wxChar SAMPLE_TITLE[] = wxT("wxWidgets Font Sample");
  36. // ----------------------------------------------------------------------------
  37. // private classes
  38. // ----------------------------------------------------------------------------
  39. // Define a new application type, each program should derive a class from wxApp
  40. class MyApp : public wxApp
  41. {
  42. public:
  43. // override base class virtuals
  44. // ----------------------------
  45. // this one is called on application startup and is a good place for the app
  46. // initialization (doing it here and not in the ctor allows to have an error
  47. // return: if OnInit() returns false, the application terminates)
  48. virtual bool OnInit();
  49. };
  50. // MyCanvas is a canvas on which we show the font sample
  51. class MyCanvas: public wxWindow
  52. {
  53. public:
  54. MyCanvas( wxWindow *parent );
  55. virtual ~MyCanvas(){};
  56. // accessors for the frame
  57. const wxFont& GetTextFont() const { return m_font; }
  58. const wxColour& GetColour() const { return m_colour; }
  59. void SetTextFont(const wxFont& font) { m_font = font; }
  60. void SetColour(const wxColour& colour) { m_colour = colour; }
  61. // event handlers
  62. void OnPaint( wxPaintEvent &event );
  63. private:
  64. wxColour m_colour;
  65. wxFont m_font;
  66. wxDECLARE_EVENT_TABLE();
  67. };
  68. // Define a new frame type: this is going to be our main frame
  69. class MyFrame : public wxFrame
  70. {
  71. public:
  72. // ctor(s)
  73. MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
  74. // accessors
  75. MyCanvas *GetCanvas() const { return m_canvas; }
  76. // event handlers (these functions should _not_ be virtual)
  77. void OnQuit(wxCommandEvent& event);
  78. void OnAbout(wxCommandEvent& event);
  79. void OnIncFont(wxCommandEvent& WXUNUSED(event)) { DoResizeFont(+2); }
  80. void OnDecFont(wxCommandEvent& WXUNUSED(event)) { DoResizeFont(-2); }
  81. void OnBold(wxCommandEvent& event);
  82. void OnLight(wxCommandEvent& event);
  83. void OnItalic(wxCommandEvent& event);
  84. void OnSlant(wxCommandEvent& event);
  85. void OnUnderline(wxCommandEvent& event);
  86. void OnStrikethrough(wxCommandEvent& event);
  87. void OnwxPointerFont(wxCommandEvent& event);
  88. void OnFontDefault(wxCommandEvent& event);
  89. void OnwxSystemSettingsFont(wxCommandEvent& event);
  90. void OnTestTextValue(wxCommandEvent& event);
  91. void OnViewMsg(wxCommandEvent& event);
  92. void OnSelectFont(wxCommandEvent& event);
  93. void OnEnumerateFamiliesForEncoding(wxCommandEvent& event);
  94. void OnEnumerateFamilies(wxCommandEvent& WXUNUSED(event))
  95. { DoEnumerateFamilies(false); }
  96. void OnEnumerateFixedFamilies(wxCommandEvent& WXUNUSED(event))
  97. { DoEnumerateFamilies(true); }
  98. void OnEnumerateEncodings(wxCommandEvent& event);
  99. void OnSetNativeDesc(wxCommandEvent& event);
  100. void OnSetNativeUserDesc(wxCommandEvent& event);
  101. void OnSetFamily(wxCommandEvent& event);
  102. void OnSetFaceName(wxCommandEvent& event);
  103. void OnSetEncoding(wxCommandEvent& event);
  104. protected:
  105. bool DoEnumerateFamilies(bool fixedWidthOnly,
  106. wxFontEncoding encoding = wxFONTENCODING_SYSTEM,
  107. bool silent = false);
  108. void DoResizeFont(int diff);
  109. void DoChangeFont(const wxFont& font, const wxColour& col = wxNullColour);
  110. // ask the user to choose an encoding and return it or
  111. // wxFONTENCODING_SYSTEM if the dialog was cancelled
  112. wxFontEncoding GetEncodingFromUser();
  113. // ask the user to choose a font family and return it or
  114. // wxFONTFAMILY_DEFAULT if the dialog was cancelled
  115. wxFontFamily GetFamilyFromUser();
  116. size_t m_fontSize; // in points
  117. wxTextCtrl *m_textctrl;
  118. MyCanvas *m_canvas;
  119. private:
  120. // any class wishing to process wxWidgets events must use this macro
  121. wxDECLARE_EVENT_TABLE();
  122. };
  123. // ----------------------------------------------------------------------------
  124. // constants
  125. // ----------------------------------------------------------------------------
  126. // IDs for the controls and the menu commands
  127. enum
  128. {
  129. // menu items
  130. Font_Quit = wxID_EXIT,
  131. Font_About = wxID_ABOUT,
  132. Font_ViewMsg = wxID_HIGHEST+1,
  133. Font_TestTextValue,
  134. Font_IncSize,
  135. Font_DecSize,
  136. Font_Bold,
  137. Font_Light,
  138. Font_Italic,
  139. Font_Slant,
  140. Font_Underlined,
  141. Font_Strikethrough,
  142. // standard global wxFont objects:
  143. Font_wxNORMAL_FONT,
  144. Font_wxSMALL_FONT,
  145. Font_wxITALIC_FONT,
  146. Font_wxSWISS_FONT,
  147. Font_wxFont_Default,
  148. Font_Standard,
  149. // wxSystemSettings::GetFont possible objects:
  150. Font_wxSYS_OEM_FIXED_FONT,
  151. Font_wxSYS_ANSI_FIXED_FONT,
  152. Font_wxSYS_ANSI_VAR_FONT,
  153. Font_wxSYS_SYSTEM_FONT,
  154. Font_wxSYS_DEVICE_DEFAULT_FONT,
  155. Font_wxSYS_DEFAULT_GUI_FONT,
  156. Font_SystemSettings,
  157. Font_Choose = 100,
  158. Font_EnumFamiliesForEncoding,
  159. Font_EnumFamilies,
  160. Font_EnumFixedFamilies,
  161. Font_EnumEncodings,
  162. Font_SetNativeDesc,
  163. Font_SetNativeUserDesc,
  164. Font_SetFamily,
  165. Font_SetFaceName,
  166. Font_SetEncoding,
  167. Font_Max
  168. };
  169. // ----------------------------------------------------------------------------
  170. // event tables and other macros for wxWidgets
  171. // ----------------------------------------------------------------------------
  172. // the event tables connect the wxWidgets events with the functions (event
  173. // handlers) which process them. It can be also done at run-time, but for the
  174. // simple menu events like this the static method is much simpler.
  175. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  176. EVT_MENU(Font_Quit, MyFrame::OnQuit)
  177. EVT_MENU(Font_TestTextValue, MyFrame::OnTestTextValue)
  178. EVT_MENU(Font_ViewMsg, MyFrame::OnViewMsg)
  179. EVT_MENU(Font_About, MyFrame::OnAbout)
  180. EVT_MENU(Font_IncSize, MyFrame::OnIncFont)
  181. EVT_MENU(Font_DecSize, MyFrame::OnDecFont)
  182. EVT_MENU(Font_Bold, MyFrame::OnBold)
  183. EVT_MENU(Font_Light, MyFrame::OnLight)
  184. EVT_MENU(Font_Italic, MyFrame::OnItalic)
  185. EVT_MENU(Font_Slant, MyFrame::OnSlant)
  186. EVT_MENU(Font_Underlined, MyFrame::OnUnderline)
  187. EVT_MENU(Font_Strikethrough, MyFrame::OnStrikethrough)
  188. EVT_MENU(Font_wxNORMAL_FONT, MyFrame::OnwxPointerFont)
  189. EVT_MENU(Font_wxSMALL_FONT, MyFrame::OnwxPointerFont)
  190. EVT_MENU(Font_wxITALIC_FONT, MyFrame::OnwxPointerFont)
  191. EVT_MENU(Font_wxSWISS_FONT, MyFrame::OnwxPointerFont)
  192. EVT_MENU(Font_wxFont_Default, MyFrame::OnFontDefault)
  193. EVT_MENU(Font_wxSYS_OEM_FIXED_FONT, MyFrame::OnwxSystemSettingsFont)
  194. EVT_MENU(Font_wxSYS_ANSI_FIXED_FONT, MyFrame::OnwxSystemSettingsFont)
  195. EVT_MENU(Font_wxSYS_ANSI_VAR_FONT, MyFrame::OnwxSystemSettingsFont)
  196. EVT_MENU(Font_wxSYS_SYSTEM_FONT, MyFrame::OnwxSystemSettingsFont)
  197. EVT_MENU(Font_wxSYS_DEVICE_DEFAULT_FONT, MyFrame::OnwxSystemSettingsFont)
  198. EVT_MENU(Font_wxSYS_DEFAULT_GUI_FONT, MyFrame::OnwxSystemSettingsFont)
  199. EVT_MENU(Font_SetNativeDesc, MyFrame::OnSetNativeDesc)
  200. EVT_MENU(Font_SetNativeUserDesc, MyFrame::OnSetNativeUserDesc)
  201. EVT_MENU(Font_SetFamily, MyFrame::OnSetFamily)
  202. EVT_MENU(Font_SetFaceName, MyFrame::OnSetFaceName)
  203. EVT_MENU(Font_SetEncoding, MyFrame::OnSetEncoding)
  204. EVT_MENU(Font_Choose, MyFrame::OnSelectFont)
  205. EVT_MENU(Font_EnumFamiliesForEncoding, MyFrame::OnEnumerateFamiliesForEncoding)
  206. EVT_MENU(Font_EnumFamilies, MyFrame::OnEnumerateFamilies)
  207. EVT_MENU(Font_EnumFixedFamilies, MyFrame::OnEnumerateFixedFamilies)
  208. EVT_MENU(Font_EnumEncodings, MyFrame::OnEnumerateEncodings)
  209. wxEND_EVENT_TABLE()
  210. // Create a new application object: this macro will allow wxWidgets to create
  211. // the application object during program execution (it's better than using a
  212. // static object for many reasons) and also declares the accessor function
  213. // wxGetApp() which will return the reference of the right type (i.e. MyApp and
  214. // not wxApp)
  215. IMPLEMENT_APP(MyApp)
  216. // ============================================================================
  217. // implementation
  218. // ============================================================================
  219. // ----------------------------------------------------------------------------
  220. // the application class
  221. // ----------------------------------------------------------------------------
  222. // `Main program' equivalent: the program execution "starts" here
  223. bool MyApp::OnInit()
  224. {
  225. if ( !wxApp::OnInit() )
  226. return false;
  227. // Create the main application window
  228. MyFrame *frame = new MyFrame(wxT("Font wxWidgets demo"),
  229. wxPoint(50, 50), wxSize(600, 400));
  230. // Show it
  231. frame->Show(true);
  232. // success: wxApp::OnRun() will be called which will enter the main message
  233. // loop and the application will run. If we returned 'false' here, the
  234. // application would exit immediately.
  235. return true;
  236. }
  237. // ----------------------------------------------------------------------------
  238. // main frame
  239. // ----------------------------------------------------------------------------
  240. // frame constructor
  241. MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
  242. : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size), m_textctrl(NULL)
  243. {
  244. m_fontSize = wxNORMAL_FONT->GetPointSize();
  245. SetIcon(wxICON(sample));
  246. // create a menu bar
  247. wxMenu *menuFile = new wxMenu;
  248. menuFile->Append(Font_TestTextValue, wxT("&Test text value"),
  249. wxT("Verify that getting and setting text value doesn't change it"));
  250. menuFile->Append(Font_ViewMsg, wxT("&View...\tCtrl-V"),
  251. wxT("View an email message file"));
  252. menuFile->AppendSeparator();
  253. menuFile->Append(Font_About, wxT("&About\tCtrl-A"), wxT("Show about dialog"));
  254. menuFile->AppendSeparator();
  255. menuFile->Append(Font_Quit, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
  256. wxMenu *menuFont = new wxMenu;
  257. menuFont->Append(Font_IncSize, wxT("&Increase font size by 2 points\tCtrl-I"));
  258. menuFont->Append(Font_DecSize, wxT("&Decrease font size by 2 points\tCtrl-D"));
  259. menuFont->AppendSeparator();
  260. menuFont->AppendCheckItem(Font_Bold, wxT("&Bold\tCtrl-B"), wxT("Toggle bold state"));
  261. menuFont->AppendCheckItem(Font_Light, wxT("&Light\tCtrl-L"), wxT("Toggle light state"));
  262. menuFont->AppendSeparator();
  263. menuFont->AppendCheckItem(Font_Italic, wxT("&Oblique\tCtrl-O"), wxT("Toggle italic state"));
  264. #ifndef __WXMSW__
  265. // under wxMSW slant == italic so there's no reason to provide another menu item for the same thing
  266. menuFont->AppendCheckItem(Font_Slant, wxT("&Slant\tCtrl-S"), wxT("Toggle slant state"));
  267. #endif
  268. menuFont->AppendSeparator();
  269. menuFont->AppendCheckItem(Font_Underlined, wxT("&Underlined\tCtrl-U"),
  270. wxT("Toggle underlined state"));
  271. menuFont->AppendCheckItem(Font_Strikethrough, wxT("&Strikethrough"),
  272. wxT("Toggle strikethrough state"));
  273. menuFont->AppendSeparator();
  274. menuFont->Append(Font_SetNativeDesc,
  275. wxT("Set native font &description\tShift-Ctrl-D"));
  276. menuFont->Append(Font_SetNativeUserDesc,
  277. wxT("Set &user font description\tShift-Ctrl-U"));
  278. menuFont->AppendSeparator();
  279. menuFont->Append(Font_SetFamily, wxT("Set font family"));
  280. menuFont->Append(Font_SetFaceName, wxT("Set font face name"));
  281. menuFont->Append(Font_SetEncoding, wxT("Set font &encoding\tShift-Ctrl-E"));
  282. wxMenu *menuSelect = new wxMenu;
  283. menuSelect->Append(Font_Choose, wxT("&Select font...\tCtrl-S"),
  284. wxT("Select a standard font"));
  285. wxMenu *menuStdFonts = new wxMenu;
  286. menuStdFonts->Append(Font_wxNORMAL_FONT, wxT("wxNORMAL_FONT"), wxT("Normal font used by wxWidgets"));
  287. menuStdFonts->Append(Font_wxSMALL_FONT, wxT("wxSMALL_FONT"), wxT("Small font used by wxWidgets"));
  288. menuStdFonts->Append(Font_wxITALIC_FONT, wxT("wxITALIC_FONT"), wxT("Italic font used by wxWidgets"));
  289. menuStdFonts->Append(Font_wxSWISS_FONT, wxT("wxSWISS_FONT"), wxT("Swiss font used by wxWidgets"));
  290. menuStdFonts->Append(Font_wxFont_Default, wxT("wxFont()"), wxT("wxFont constructed from default wxFontInfo"));
  291. menuSelect->Append(Font_Standard, wxT("Standar&d fonts"), menuStdFonts);
  292. wxMenu *menuSettingFonts = new wxMenu;
  293. menuSettingFonts->Append(Font_wxSYS_OEM_FIXED_FONT, wxT("wxSYS_OEM_FIXED_FONT"),
  294. wxT("Original equipment manufacturer dependent fixed-pitch font."));
  295. menuSettingFonts->Append(Font_wxSYS_ANSI_FIXED_FONT, wxT("wxSYS_ANSI_FIXED_FONT"),
  296. wxT("Windows fixed-pitch (monospaced) font. "));
  297. menuSettingFonts->Append(Font_wxSYS_ANSI_VAR_FONT, wxT("wxSYS_ANSI_VAR_FONT"),
  298. wxT("Windows variable-pitch (proportional) font."));
  299. menuSettingFonts->Append(Font_wxSYS_SYSTEM_FONT, wxT("wxSYS_SYSTEM_FONT"),
  300. wxT("System font."));
  301. menuSettingFonts->Append(Font_wxSYS_DEVICE_DEFAULT_FONT, wxT("wxSYS_DEVICE_DEFAULT_FONT"),
  302. wxT("Device-dependent font."));
  303. menuSettingFonts->Append(Font_wxSYS_DEFAULT_GUI_FONT, wxT("wxSYS_DEFAULT_GUI_FONT"),
  304. wxT("Default font for user interface objects such as menus and dialog boxes. "));
  305. menuSelect->Append(Font_SystemSettings, wxT("System fonts"), menuSettingFonts);
  306. menuSelect->AppendSeparator();
  307. menuSelect->Append(Font_EnumFamilies, wxT("Enumerate font &families\tCtrl-F"));
  308. menuSelect->Append(Font_EnumFixedFamilies,
  309. wxT("Enumerate fi&xed font families\tCtrl-X"));
  310. menuSelect->Append(Font_EnumEncodings,
  311. wxT("Enumerate &encodings\tCtrl-E"));
  312. menuSelect->Append(Font_EnumFamiliesForEncoding,
  313. wxT("Find font for en&coding...\tCtrl-C"),
  314. wxT("Find font families for given encoding"));
  315. // now append the freshly created menu to the menu bar...
  316. wxMenuBar *menuBar = new wxMenuBar;
  317. menuBar->Append(menuFile, wxT("&File"));
  318. menuBar->Append(menuFont, wxT("F&ont"));
  319. menuBar->Append(menuSelect, wxT("&Select"));
  320. // ... and attach this menu bar to the frame
  321. SetMenuBar(menuBar);
  322. wxSplitterWindow *splitter = new wxSplitterWindow(this);
  323. m_textctrl = new wxTextCtrl(splitter, wxID_ANY,
  324. wxT("Paste text here to see how it looks\nlike in the given font"),
  325. wxDefaultPosition, wxDefaultSize,
  326. wxTE_MULTILINE);
  327. m_canvas = new MyCanvas(splitter);
  328. splitter->SplitHorizontally(m_textctrl, m_canvas, 100);
  329. #if wxUSE_STATUSBAR
  330. // create a status bar just for fun (by default with 1 pane only)
  331. CreateStatusBar();
  332. SetStatusText(wxT("Welcome to wxWidgets font demo!"));
  333. #endif // wxUSE_STATUSBAR
  334. }
  335. // --------------------------------------------------------
  336. class MyEncodingEnumerator : public wxFontEnumerator
  337. {
  338. public:
  339. MyEncodingEnumerator()
  340. { m_n = 0; }
  341. const wxString& GetText() const
  342. { return m_text; }
  343. protected:
  344. virtual bool OnFontEncoding(const wxString& facename,
  345. const wxString& encoding)
  346. {
  347. wxString text;
  348. text.Printf(wxT("Encoding %u: %s (available in facename '%s')\n"),
  349. (unsigned int) ++m_n, encoding.c_str(), facename.c_str());
  350. m_text += text;
  351. return true;
  352. }
  353. private:
  354. size_t m_n;
  355. wxString m_text;
  356. };
  357. void MyFrame::OnEnumerateEncodings(wxCommandEvent& WXUNUSED(event))
  358. {
  359. MyEncodingEnumerator fontEnumerator;
  360. fontEnumerator.EnumerateEncodings();
  361. wxLogMessage(wxT("Enumerating all available encodings:\n%s"),
  362. fontEnumerator.GetText().c_str());
  363. }
  364. // -------------------------------------------------------------
  365. class MyFontEnumerator : public wxFontEnumerator
  366. {
  367. public:
  368. bool GotAny() const
  369. { return !m_facenames.IsEmpty(); }
  370. const wxArrayString& GetFacenames() const
  371. { return m_facenames; }
  372. protected:
  373. virtual bool OnFacename(const wxString& facename)
  374. {
  375. m_facenames.Add(facename);
  376. return true;
  377. }
  378. private:
  379. wxArrayString m_facenames;
  380. } fontEnumerator;
  381. bool MyFrame::DoEnumerateFamilies(bool fixedWidthOnly,
  382. wxFontEncoding encoding,
  383. bool silent)
  384. {
  385. MyFontEnumerator fontEnumerator;
  386. fontEnumerator.EnumerateFacenames(encoding, fixedWidthOnly);
  387. if ( fontEnumerator.GotAny() )
  388. {
  389. int nFacenames = fontEnumerator.GetFacenames().GetCount();
  390. if ( !silent )
  391. {
  392. wxLogStatus(this, wxT("Found %d %sfonts"),
  393. nFacenames, fixedWidthOnly ? wxT("fixed width ") : wxT(""));
  394. }
  395. wxString facename;
  396. if ( silent )
  397. {
  398. // choose the first
  399. facename = fontEnumerator.GetFacenames().Item(0);
  400. }
  401. else
  402. {
  403. // let the user choose
  404. wxString *facenames = new wxString[nFacenames];
  405. int n;
  406. for ( n = 0; n < nFacenames; n++ )
  407. facenames[n] = fontEnumerator.GetFacenames().Item(n);
  408. n = wxGetSingleChoiceIndex
  409. (
  410. wxT("Choose a facename"),
  411. SAMPLE_TITLE,
  412. nFacenames,
  413. facenames,
  414. this
  415. );
  416. if ( n != -1 )
  417. facename = facenames[n];
  418. delete [] facenames;
  419. }
  420. if ( !facename.empty() )
  421. {
  422. wxFont font(wxFontInfo().FaceName(facename).Encoding(encoding));
  423. DoChangeFont(font);
  424. }
  425. return true;
  426. }
  427. else if ( !silent )
  428. {
  429. wxLogWarning(wxT("No such fonts found."));
  430. }
  431. return false;
  432. }
  433. void MyFrame::OnEnumerateFamiliesForEncoding(wxCommandEvent& WXUNUSED(event))
  434. {
  435. wxFontEncoding enc = GetEncodingFromUser();
  436. if ( enc != wxFONTENCODING_SYSTEM )
  437. {
  438. DoEnumerateFamilies(false, enc);
  439. }
  440. }
  441. void MyFrame::OnSetNativeDesc(wxCommandEvent& WXUNUSED(event))
  442. {
  443. wxString fontInfo = wxGetTextFromUser
  444. (
  445. wxT("Enter native font string"),
  446. wxT("Input font description"),
  447. m_canvas->GetTextFont().GetNativeFontInfoDesc(),
  448. this
  449. );
  450. if ( fontInfo.empty() )
  451. return; // user clicked "Cancel" - do nothing
  452. wxFont font;
  453. font.SetNativeFontInfo(fontInfo);
  454. if ( !font.IsOk() )
  455. {
  456. wxLogError(wxT("Font info string \"%s\" is invalid."),
  457. fontInfo.c_str());
  458. return;
  459. }
  460. DoChangeFont(font);
  461. }
  462. void MyFrame::OnSetNativeUserDesc(wxCommandEvent& WXUNUSED(event))
  463. {
  464. wxString fontdesc = GetCanvas()->GetTextFont().GetNativeFontInfoUserDesc();
  465. wxString fontUserInfo = wxGetTextFromUser(
  466. wxT("Here you can edit current font description"),
  467. wxT("Input font description"), fontdesc,
  468. this);
  469. if (fontUserInfo.IsEmpty())
  470. return; // user clicked "Cancel" - do nothing
  471. wxFont font;
  472. if (font.SetNativeFontInfoUserDesc(fontUserInfo))
  473. {
  474. wxASSERT_MSG(font.IsOk(), wxT("The font should now be valid"));
  475. DoChangeFont(font);
  476. }
  477. else
  478. {
  479. wxASSERT_MSG(!font.IsOk(), wxT("The font should now be invalid"));
  480. wxMessageBox(wxT("Error trying to create a font with such description..."));
  481. }
  482. }
  483. void MyFrame::OnSetFamily(wxCommandEvent& WXUNUSED(event))
  484. {
  485. wxFontFamily f = GetFamilyFromUser();
  486. wxFont font = m_canvas->GetTextFont();
  487. font.SetFamily(f);
  488. DoChangeFont(font);
  489. }
  490. void MyFrame::OnSetFaceName(wxCommandEvent& WXUNUSED(event))
  491. {
  492. wxString facename = GetCanvas()->GetTextFont().GetFaceName();
  493. wxString newFaceName = wxGetTextFromUser(
  494. wxT("Here you can edit current font face name."),
  495. wxT("Input font facename"), facename,
  496. this);
  497. if (newFaceName.IsEmpty())
  498. return; // user clicked "Cancel" - do nothing
  499. wxFont font(GetCanvas()->GetTextFont());
  500. if (font.SetFaceName(newFaceName)) // change facename only
  501. {
  502. wxASSERT_MSG(font.IsOk(), wxT("The font should now be valid"));
  503. DoChangeFont(font);
  504. }
  505. else
  506. {
  507. wxASSERT_MSG(!font.IsOk(), wxT("The font should now be invalid"));
  508. wxMessageBox(wxT("There is no font with such face name..."),
  509. wxT("Invalid face name"), wxOK|wxICON_ERROR, this);
  510. }
  511. }
  512. void MyFrame::OnSetEncoding(wxCommandEvent& WXUNUSED(event))
  513. {
  514. wxFontEncoding enc = GetEncodingFromUser();
  515. if ( enc == wxFONTENCODING_SYSTEM )
  516. return;
  517. wxFont font = m_canvas->GetTextFont();
  518. font.SetEncoding(enc);
  519. DoChangeFont(font);
  520. }
  521. wxFontEncoding MyFrame::GetEncodingFromUser()
  522. {
  523. wxArrayString names;
  524. wxArrayInt encodings;
  525. const size_t count = wxFontMapper::GetSupportedEncodingsCount();
  526. names.reserve(count);
  527. encodings.reserve(count);
  528. for ( size_t n = 0; n < count; n++ )
  529. {
  530. wxFontEncoding enc = wxFontMapper::GetEncoding(n);
  531. encodings.push_back(enc);
  532. names.push_back(wxFontMapper::GetEncodingName(enc));
  533. }
  534. int i = wxGetSingleChoiceIndex
  535. (
  536. wxT("Choose the encoding"),
  537. SAMPLE_TITLE,
  538. names,
  539. this
  540. );
  541. return i == -1 ? wxFONTENCODING_SYSTEM : (wxFontEncoding)encodings[i];
  542. }
  543. wxFontFamily MyFrame::GetFamilyFromUser()
  544. {
  545. wxArrayString names;
  546. wxArrayInt families;
  547. families.push_back(wxFONTFAMILY_DECORATIVE);
  548. families.push_back(wxFONTFAMILY_ROMAN);
  549. families.push_back(wxFONTFAMILY_SCRIPT);
  550. families.push_back(wxFONTFAMILY_SWISS);
  551. families.push_back(wxFONTFAMILY_MODERN);
  552. families.push_back(wxFONTFAMILY_TELETYPE);
  553. names.push_back("DECORATIVE");
  554. names.push_back("ROMAN");
  555. names.push_back("SCRIPT");
  556. names.push_back("SWISS");
  557. names.push_back("MODERN");
  558. names.push_back("TELETYPE");
  559. int i = wxGetSingleChoiceIndex
  560. (
  561. wxT("Choose the family"),
  562. SAMPLE_TITLE,
  563. names,
  564. this
  565. );
  566. return i == -1 ? wxFONTFAMILY_DEFAULT : (wxFontFamily)families[i];
  567. }
  568. void MyFrame::DoResizeFont(int diff)
  569. {
  570. wxFont font = m_canvas->GetTextFont();
  571. font.SetPointSize(font.GetPointSize() + diff);
  572. DoChangeFont(font);
  573. }
  574. void MyFrame::OnBold(wxCommandEvent& event)
  575. {
  576. wxFont font = m_canvas->GetTextFont();
  577. font.SetWeight(event.IsChecked() ? wxFONTWEIGHT_BOLD : wxFONTWEIGHT_NORMAL);
  578. DoChangeFont(font);
  579. }
  580. void MyFrame::OnLight(wxCommandEvent& event)
  581. {
  582. wxFont font = m_canvas->GetTextFont();
  583. font.SetWeight(event.IsChecked() ? wxFONTWEIGHT_LIGHT : wxFONTWEIGHT_NORMAL);
  584. DoChangeFont(font);
  585. }
  586. void MyFrame::OnItalic(wxCommandEvent& event)
  587. {
  588. wxFont font = m_canvas->GetTextFont();
  589. font.SetStyle(event.IsChecked() ? wxFONTSTYLE_ITALIC : wxFONTSTYLE_NORMAL);
  590. DoChangeFont(font);
  591. }
  592. void MyFrame::OnSlant(wxCommandEvent& event)
  593. {
  594. wxFont font = m_canvas->GetTextFont();
  595. font.SetStyle(event.IsChecked() ? wxFONTSTYLE_SLANT : wxFONTSTYLE_NORMAL);
  596. DoChangeFont(font);
  597. }
  598. void MyFrame::OnUnderline(wxCommandEvent& event)
  599. {
  600. wxFont font = m_canvas->GetTextFont();
  601. font.SetUnderlined(event.IsChecked());
  602. DoChangeFont(font);
  603. }
  604. void MyFrame::OnStrikethrough(wxCommandEvent& event)
  605. {
  606. wxFont font = m_canvas->GetTextFont();
  607. font.SetStrikethrough(event.IsChecked());
  608. DoChangeFont(font);
  609. }
  610. void MyFrame::OnwxPointerFont(wxCommandEvent& event)
  611. {
  612. wxFont font;
  613. switch ( event.GetId() )
  614. {
  615. case Font_wxNORMAL_FONT:
  616. font = *wxNORMAL_FONT;
  617. break;
  618. case Font_wxSMALL_FONT:
  619. font = *wxSMALL_FONT;
  620. break;
  621. case Font_wxITALIC_FONT:
  622. font = *wxITALIC_FONT;
  623. break;
  624. case Font_wxSWISS_FONT:
  625. font = *wxSWISS_FONT;
  626. break;
  627. default:
  628. wxFAIL_MSG( wxT("unknown standard font") );
  629. return;
  630. }
  631. DoChangeFont(font);
  632. }
  633. void MyFrame::OnFontDefault(wxCommandEvent& WXUNUSED(event))
  634. {
  635. DoChangeFont(wxFont(wxFontInfo()));
  636. }
  637. void MyFrame::OnwxSystemSettingsFont(wxCommandEvent& event)
  638. {
  639. wxFont font;
  640. switch ( event.GetId() )
  641. {
  642. case Font_wxSYS_OEM_FIXED_FONT:
  643. font = wxSystemSettings::GetFont(wxSYS_OEM_FIXED_FONT);
  644. break;
  645. case Font_wxSYS_ANSI_FIXED_FONT:
  646. font = wxSystemSettings::GetFont(wxSYS_ANSI_FIXED_FONT);
  647. break;
  648. case Font_wxSYS_ANSI_VAR_FONT:
  649. font = wxSystemSettings::GetFont(wxSYS_ANSI_VAR_FONT);
  650. break;
  651. case Font_wxSYS_SYSTEM_FONT:
  652. font = wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT);
  653. break;
  654. case Font_wxSYS_DEVICE_DEFAULT_FONT:
  655. font = wxSystemSettings::GetFont(wxSYS_DEVICE_DEFAULT_FONT);
  656. break;
  657. case Font_wxSYS_DEFAULT_GUI_FONT:
  658. font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
  659. break;
  660. default:
  661. wxFAIL_MSG( wxT("unknown standard font") );
  662. return;
  663. }
  664. DoChangeFont(font);
  665. }
  666. void MyFrame::DoChangeFont(const wxFont& font, const wxColour& col)
  667. {
  668. m_canvas->SetTextFont(font);
  669. if ( col.IsOk() )
  670. m_canvas->SetColour(col);
  671. m_canvas->Refresh();
  672. m_textctrl->SetFont(font);
  673. if ( col.IsOk() )
  674. m_textctrl->SetForegroundColour(col);
  675. m_textctrl->Refresh();
  676. // update the state of the bold/italic/underlined menu items
  677. wxMenuBar *mbar = GetMenuBar();
  678. if ( mbar )
  679. {
  680. mbar->Check(Font_Light, font.GetWeight() == wxFONTWEIGHT_LIGHT);
  681. mbar->Check(Font_Bold, font.GetWeight() == wxFONTWEIGHT_BOLD);
  682. mbar->Check(Font_Italic, font.GetStyle() == wxFONTSTYLE_ITALIC);
  683. #ifndef __WXMSW__
  684. mbar->Check(Font_Slant, font.GetStyle() == wxFONTSTYLE_SLANT);
  685. #endif
  686. mbar->Check(Font_Underlined, font.GetUnderlined());
  687. mbar->Check(Font_Strikethrough, font.GetStrikethrough());
  688. }
  689. }
  690. void MyFrame::OnSelectFont(wxCommandEvent& WXUNUSED(event))
  691. {
  692. wxFontData data;
  693. data.SetInitialFont(m_canvas->GetTextFont());
  694. data.SetColour(m_canvas->GetColour());
  695. wxFontDialog dialog(this, data);
  696. if ( dialog.ShowModal() == wxID_OK )
  697. {
  698. wxFontData retData = dialog.GetFontData();
  699. wxFont font = retData.GetChosenFont();
  700. wxColour colour = retData.GetColour();
  701. DoChangeFont(font, colour);
  702. }
  703. }
  704. void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
  705. {
  706. // true is to force the frame to close
  707. Close(true);
  708. }
  709. void MyFrame::OnTestTextValue(wxCommandEvent& WXUNUSED(event))
  710. {
  711. wxString value = m_textctrl->GetValue();
  712. m_textctrl->SetValue(value);
  713. if ( m_textctrl->GetValue() != value )
  714. {
  715. wxLogError(wxT("Text value changed after getting and setting it"));
  716. }
  717. }
  718. void MyFrame::OnViewMsg(wxCommandEvent& WXUNUSED(event))
  719. {
  720. #if wxUSE_FILEDLG
  721. // first, choose the file
  722. static wxString s_dir, s_file;
  723. wxFileDialog dialog(this, wxT("Open an email message file"),
  724. s_dir, s_file);
  725. if ( dialog.ShowModal() != wxID_OK )
  726. return;
  727. // save for the next time
  728. s_dir = dialog.GetDirectory();
  729. s_file = dialog.GetFilename();
  730. wxString filename = dialog.GetPath();
  731. // load it and search for Content-Type header
  732. wxTextFile file(filename);
  733. if ( !file.Open() )
  734. return;
  735. wxString charset;
  736. static const wxChar *prefix = wxT("Content-Type: text/plain; charset=");
  737. const size_t len = wxStrlen(prefix);
  738. size_t n, count = file.GetLineCount();
  739. for ( n = 0; n < count; n++ )
  740. {
  741. wxString line = file[n];
  742. if ( !line )
  743. {
  744. // if it is an email message, headers are over, no need to parse
  745. // all the file
  746. break;
  747. }
  748. if ( line.Left(len) == prefix )
  749. {
  750. // found!
  751. const wxChar *pc = line.c_str() + len;
  752. if ( *pc == wxT('"') )
  753. pc++;
  754. while ( *pc && *pc != wxT('"') )
  755. {
  756. charset += *pc++;
  757. }
  758. break;
  759. }
  760. }
  761. if ( !charset )
  762. {
  763. wxLogError(wxT("The file '%s' doesn't contain charset information."),
  764. filename.c_str());
  765. return;
  766. }
  767. // ok, now get the corresponding encoding
  768. wxFontEncoding fontenc = wxFontMapper::Get()->CharsetToEncoding(charset);
  769. if ( fontenc == wxFONTENCODING_SYSTEM )
  770. {
  771. wxLogError(wxT("Charset '%s' is unsupported."), charset.c_str());
  772. return;
  773. }
  774. m_textctrl->LoadFile(filename);
  775. if ( fontenc == wxFONTENCODING_UTF8 ||
  776. !wxFontMapper::Get()->IsEncodingAvailable(fontenc) )
  777. {
  778. // try to find some similar encoding:
  779. wxFontEncoding encAlt;
  780. if ( wxFontMapper::Get()->GetAltForEncoding(fontenc, &encAlt) )
  781. {
  782. wxEncodingConverter conv;
  783. if (conv.Init(fontenc, encAlt))
  784. {
  785. fontenc = encAlt;
  786. m_textctrl -> SetValue(conv.Convert(m_textctrl -> GetValue()));
  787. }
  788. else
  789. {
  790. wxLogWarning(wxT("Cannot convert from '%s' to '%s'."),
  791. wxFontMapper::GetEncodingDescription(fontenc).c_str(),
  792. wxFontMapper::GetEncodingDescription(encAlt).c_str());
  793. }
  794. }
  795. else
  796. wxLogWarning(wxT("No fonts for encoding '%s' on this system."),
  797. wxFontMapper::GetEncodingDescription(fontenc).c_str());
  798. }
  799. // and now create the correct font
  800. if ( !DoEnumerateFamilies(false, fontenc, true /* silent */) )
  801. {
  802. wxFont font(wxFontInfo(wxNORMAL_FONT->GetPointSize()).Encoding(fontenc));
  803. if ( font.IsOk() )
  804. {
  805. DoChangeFont(font);
  806. }
  807. else
  808. {
  809. wxLogWarning(wxT("No fonts for encoding '%s' on this system."),
  810. wxFontMapper::GetEncodingDescription(fontenc).c_str());
  811. }
  812. }
  813. #endif // wxUSE_FILEDLG
  814. }
  815. void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
  816. {
  817. wxMessageBox(wxT("wxWidgets font sample\n")
  818. wxT("(c) 1999-2006 Vadim Zeitlin"),
  819. wxString(wxT("About ")) + SAMPLE_TITLE,
  820. wxOK | wxICON_INFORMATION, this);
  821. }
  822. // ----------------------------------------------------------------------------
  823. // MyCanvas
  824. // ----------------------------------------------------------------------------
  825. wxBEGIN_EVENT_TABLE(MyCanvas, wxWindow)
  826. EVT_PAINT(MyCanvas::OnPaint)
  827. wxEND_EVENT_TABLE()
  828. MyCanvas::MyCanvas( wxWindow *parent )
  829. : wxWindow( parent, wxID_ANY ),
  830. m_colour(*wxRED), m_font(*wxNORMAL_FONT)
  831. {
  832. }
  833. void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
  834. {
  835. wxPaintDC dc(this);
  836. PrepareDC(dc);
  837. // set background
  838. dc.SetBackground(*wxWHITE_BRUSH);
  839. dc.Clear();
  840. dc.SetFont(m_font);
  841. // one text line height
  842. wxCoord hLine = dc.GetCharHeight();
  843. // the current text origin
  844. wxCoord x = 5,
  845. y = 5;
  846. // output the font name/info
  847. wxString fontInfo;
  848. fontInfo.Printf(wxT("Face name: %s, family: %s"),
  849. m_font.GetFaceName().c_str(),
  850. m_font.GetFamilyString().c_str());
  851. dc.DrawText(fontInfo, x, y);
  852. y += hLine;
  853. fontInfo.Printf(wxT("Size: %d points or %d pixels; %d*%d average char size"),
  854. m_font.GetPointSize(),
  855. m_font.GetPixelSize().y,
  856. dc.GetCharWidth(), dc.GetCharHeight());
  857. dc.DrawText(fontInfo, x, y);
  858. y += hLine;
  859. fontInfo.Printf(wxT("Style: %s, weight: %s, fixed width: %s, encoding: %s"),
  860. m_font.GetStyleString().c_str(),
  861. m_font.GetWeightString().c_str(),
  862. m_font.IsFixedWidth() ? wxT("yes") : wxT("no"),
  863. wxFontMapper::GetEncodingDescription(m_font.GetEncoding()));
  864. dc.DrawText(fontInfo, x, y);
  865. y += hLine;
  866. if ( m_font.IsOk() )
  867. {
  868. const wxNativeFontInfo *info = m_font.GetNativeFontInfo();
  869. if ( info )
  870. {
  871. wxString fontDesc = m_font.GetNativeFontInfoUserDesc();
  872. fontInfo.Printf(wxT("Native font info: %s"), fontDesc.c_str());
  873. dc.DrawText(fontInfo, x, y);
  874. y += hLine;
  875. }
  876. }
  877. y += hLine;
  878. // prepare to draw the font
  879. dc.SetTextForeground(m_colour);
  880. // the size of one cell (Normally biggest char + small margin)
  881. wxCoord maxCharWidth, maxCharHeight;
  882. dc.GetTextExtent(wxT("W"), &maxCharWidth, &maxCharHeight);
  883. int w = maxCharWidth + 5,
  884. h = maxCharHeight + 4;
  885. // print all font symbols from 32 to 256 in 7 rows of 32 chars each
  886. for ( int i = 0; i < 7; i++ )
  887. {
  888. for ( int j = 0; j < 32; j++ )
  889. {
  890. wxChar c = (wxChar)(32 * (i + 1) + j);
  891. wxCoord charWidth, charHeight;
  892. dc.GetTextExtent(c, &charWidth, &charHeight);
  893. dc.DrawText
  894. (
  895. c,
  896. x + w*j + (maxCharWidth - charWidth) / 2 + 1,
  897. y + h*i + (maxCharHeight - charHeight) / 2
  898. );
  899. }
  900. }
  901. // draw the lines between them
  902. dc.SetPen(*wxBLUE_PEN);
  903. int l;
  904. // horizontal
  905. for ( l = 0; l < 8; l++ )
  906. {
  907. int yl = y + h*l - 2;
  908. dc.DrawLine(x - 2, yl, x + 32*w - 1, yl);
  909. }
  910. // and vertical
  911. for ( l = 0; l < 33; l++ )
  912. {
  913. int xl = x + w*l - 2;
  914. dc.DrawLine(xl, y - 2, xl, y + 7*h - 1);
  915. }
  916. }