fontpicker.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Program: wxWidgets Widgets Sample
  3. // Name: fontpicker.cpp
  4. // Purpose: Shows wxFontPickerCtrl
  5. // Author: Francesco Montorsi
  6. // Created: 20/6/2006
  7. // Copyright: (c) 2006 Francesco Montorsi
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // ============================================================================
  11. // declarations
  12. // ============================================================================
  13. // ----------------------------------------------------------------------------
  14. // headers
  15. // ----------------------------------------------------------------------------
  16. // for compilers that support precompilation, includes "wx/wx.h".
  17. #include "wx/wxprec.h"
  18. #ifdef __BORLANDC__
  19. #pragma hdrstop
  20. #endif
  21. #if wxUSE_FONTPICKERCTRL
  22. // for all others, include the necessary headers
  23. #ifndef WX_PRECOMP
  24. #include "wx/app.h"
  25. #include "wx/log.h"
  26. #include "wx/radiobox.h"
  27. #endif
  28. #include "wx/artprov.h"
  29. #include "wx/sizer.h"
  30. #include "wx/stattext.h"
  31. #include "wx/checkbox.h"
  32. #include "wx/imaglist.h"
  33. #include "wx/fontpicker.h"
  34. #include "widgets.h"
  35. #include "icons/fontpicker.xpm"
  36. // ----------------------------------------------------------------------------
  37. // constants
  38. // ----------------------------------------------------------------------------
  39. // control ids
  40. enum
  41. {
  42. PickerPage_Reset = wxID_HIGHEST,
  43. PickerPage_Font
  44. };
  45. // ----------------------------------------------------------------------------
  46. // FontPickerWidgetsPage
  47. // ----------------------------------------------------------------------------
  48. class FontPickerWidgetsPage : public WidgetsPage
  49. {
  50. public:
  51. FontPickerWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
  52. virtual ~FontPickerWidgetsPage(){};
  53. virtual wxControl *GetWidget() const { return m_fontPicker; }
  54. virtual void RecreateWidget() { RecreatePicker(); }
  55. // lazy creation of the content
  56. virtual void CreateContent();
  57. protected:
  58. // called only once at first construction
  59. void CreatePicker();
  60. // called to recreate an existing control
  61. void RecreatePicker();
  62. // restore the checkboxes state to the initial values
  63. void Reset();
  64. // get the initial style for the picker of the given kind
  65. long GetPickerStyle();
  66. void OnFontChange(wxFontPickerEvent &ev);
  67. void OnCheckBox(wxCommandEvent &ev);
  68. void OnButtonReset(wxCommandEvent &ev);
  69. // the picker
  70. wxFontPickerCtrl *m_fontPicker;
  71. // other controls
  72. // --------------
  73. wxCheckBox *m_chkFontTextCtrl,
  74. *m_chkFontDescAsLabel,
  75. *m_chkFontUseFontForLabel;
  76. wxBoxSizer *m_sizer;
  77. private:
  78. wxDECLARE_EVENT_TABLE();
  79. DECLARE_WIDGETS_PAGE(FontPickerWidgetsPage)
  80. };
  81. // ----------------------------------------------------------------------------
  82. // event tables
  83. // ----------------------------------------------------------------------------
  84. wxBEGIN_EVENT_TABLE(FontPickerWidgetsPage, WidgetsPage)
  85. EVT_BUTTON(PickerPage_Reset, FontPickerWidgetsPage::OnButtonReset)
  86. EVT_FONTPICKER_CHANGED(PickerPage_Font, FontPickerWidgetsPage::OnFontChange)
  87. EVT_CHECKBOX(wxID_ANY, FontPickerWidgetsPage::OnCheckBox)
  88. wxEND_EVENT_TABLE()
  89. // ============================================================================
  90. // implementation
  91. // ============================================================================
  92. #if defined(__WXGTK24__)
  93. #define FAMILY_CTRLS NATIVE_CTRLS
  94. #else
  95. #define FAMILY_CTRLS GENERIC_CTRLS
  96. #endif
  97. IMPLEMENT_WIDGETS_PAGE(FontPickerWidgetsPage, wxT("FontPicker"),
  98. PICKER_CTRLS | FAMILY_CTRLS);
  99. FontPickerWidgetsPage::FontPickerWidgetsPage(WidgetsBookCtrl *book,
  100. wxImageList *imaglist)
  101. : WidgetsPage(book, imaglist, fontpicker_xpm)
  102. {
  103. }
  104. void FontPickerWidgetsPage::CreateContent()
  105. {
  106. // left pane
  107. wxSizer *boxleft = new wxBoxSizer(wxVERTICAL);
  108. wxStaticBoxSizer *fontbox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("&FontPicker style"));
  109. m_chkFontTextCtrl = CreateCheckBoxAndAddToSizer(fontbox, wxT("With textctrl"));
  110. m_chkFontDescAsLabel = CreateCheckBoxAndAddToSizer(fontbox, wxT("Font desc as btn label"));
  111. m_chkFontUseFontForLabel = CreateCheckBoxAndAddToSizer(fontbox, wxT("Use font for label"));
  112. boxleft->Add(fontbox, 0, wxALL|wxGROW, 5);
  113. boxleft->Add(new wxButton(this, PickerPage_Reset, wxT("&Reset")),
  114. 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
  115. Reset(); // set checkboxes state
  116. // create pickers
  117. m_fontPicker = NULL;
  118. CreatePicker();
  119. // right pane
  120. m_sizer = new wxBoxSizer(wxVERTICAL);
  121. m_sizer->Add(1, 1, 1, wxGROW | wxALL, 5); // spacer
  122. m_sizer->Add(m_fontPicker, 0, wxALIGN_CENTER|wxALL, 5);
  123. m_sizer->Add(1, 1, 1, wxGROW | wxALL, 5); // spacer
  124. // global pane
  125. wxSizer *sz = new wxBoxSizer(wxHORIZONTAL);
  126. sz->Add(boxleft, 0, wxGROW|wxALL, 5);
  127. sz->Add(m_sizer, 1, wxGROW|wxALL, 5);
  128. SetSizer(sz);
  129. }
  130. void FontPickerWidgetsPage::CreatePicker()
  131. {
  132. delete m_fontPicker;
  133. m_fontPicker = new wxFontPickerCtrl(this, PickerPage_Font,
  134. *wxSWISS_FONT,
  135. wxDefaultPosition, wxDefaultSize,
  136. GetPickerStyle());
  137. }
  138. long FontPickerWidgetsPage::GetPickerStyle()
  139. {
  140. long style = 0;
  141. if ( m_chkFontTextCtrl->GetValue() )
  142. style |= wxFNTP_USE_TEXTCTRL;
  143. if ( m_chkFontUseFontForLabel->GetValue() )
  144. style |= wxFNTP_USEFONT_FOR_LABEL;
  145. if ( m_chkFontDescAsLabel->GetValue() )
  146. style |= wxFNTP_FONTDESC_AS_LABEL;
  147. return style;
  148. }
  149. void FontPickerWidgetsPage::RecreatePicker()
  150. {
  151. m_sizer->Remove(1);
  152. CreatePicker();
  153. m_sizer->Insert(1, m_fontPicker, 0, wxALIGN_CENTER|wxALL, 5);
  154. m_sizer->Layout();
  155. }
  156. void FontPickerWidgetsPage::Reset()
  157. {
  158. m_chkFontTextCtrl->SetValue((wxFNTP_DEFAULT_STYLE & wxFNTP_USE_TEXTCTRL) != 0);
  159. m_chkFontUseFontForLabel->SetValue((wxFNTP_DEFAULT_STYLE & wxFNTP_USEFONT_FOR_LABEL) != 0);
  160. m_chkFontDescAsLabel->SetValue((wxFNTP_DEFAULT_STYLE & wxFNTP_FONTDESC_AS_LABEL) != 0);
  161. }
  162. // ----------------------------------------------------------------------------
  163. // event handlers
  164. // ----------------------------------------------------------------------------
  165. void FontPickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
  166. {
  167. Reset();
  168. RecreatePicker();
  169. }
  170. void FontPickerWidgetsPage::OnFontChange(wxFontPickerEvent& event)
  171. {
  172. wxLogMessage(wxT("The font changed to '%s' with size %d !"),
  173. event.GetFont().GetFaceName().c_str(), event.GetFont().GetPointSize());
  174. }
  175. void FontPickerWidgetsPage::OnCheckBox(wxCommandEvent &event)
  176. {
  177. if (event.GetEventObject() == m_chkFontTextCtrl ||
  178. event.GetEventObject() == m_chkFontDescAsLabel ||
  179. event.GetEventObject() == m_chkFontUseFontForLabel)
  180. RecreatePicker();
  181. }
  182. #endif // wxUSE_FONTPICKERCTRL