font.cpp 34 KB

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