fontpicker.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/fontpicker.h
  3. // Purpose: wxFontPickerCtrl base header
  4. // Author: Francesco Montorsi
  5. // Modified by:
  6. // Created: 14/4/2006
  7. // Copyright: (c) Francesco Montorsi
  8. // Licence: wxWindows Licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_FONTPICKER_H_BASE_
  11. #define _WX_FONTPICKER_H_BASE_
  12. #include "wx/defs.h"
  13. #if wxUSE_FONTPICKERCTRL
  14. #include "wx/pickerbase.h"
  15. class WXDLLIMPEXP_FWD_CORE wxFontPickerEvent;
  16. extern WXDLLIMPEXP_DATA_CORE(const char) wxFontPickerWidgetNameStr[];
  17. extern WXDLLIMPEXP_DATA_CORE(const char) wxFontPickerCtrlNameStr[];
  18. // ----------------------------------------------------------------------------
  19. // wxFontPickerWidgetBase: a generic abstract interface which must be
  20. // implemented by controls used by wxFontPickerCtrl
  21. // ----------------------------------------------------------------------------
  22. class WXDLLIMPEXP_CORE wxFontPickerWidgetBase
  23. {
  24. public:
  25. wxFontPickerWidgetBase() { m_selectedFont = *wxNORMAL_FONT; }
  26. virtual ~wxFontPickerWidgetBase() {}
  27. wxFont GetSelectedFont() const
  28. { return m_selectedFont; }
  29. virtual void SetSelectedFont(const wxFont &f)
  30. { m_selectedFont = f; UpdateFont(); }
  31. protected:
  32. virtual void UpdateFont() = 0;
  33. // the current font (may be invalid if none)
  34. // NOTE: don't call this m_font as wxWindow::m_font already exists
  35. wxFont m_selectedFont;
  36. };
  37. // Styles which must be supported by all controls implementing wxFontPickerWidgetBase
  38. // NB: these styles must be defined to carefully-chosen values to
  39. // avoid conflicts with wxButton's styles
  40. // keeps the label of the button updated with the fontface name + font size
  41. // E.g. choosing "Times New Roman bold, italic with size 10" from the fontdialog,
  42. // updates the wxFontButtonGeneric's label (overwriting any previous label)
  43. // with the "Times New Roman, 10" text (only fontface + fontsize is displayed
  44. // to avoid extralong labels).
  45. #define wxFNTP_FONTDESC_AS_LABEL 0x0008
  46. // uses the currently selected font to draw the label of the button
  47. #define wxFNTP_USEFONT_FOR_LABEL 0x0010
  48. #define wxFONTBTN_DEFAULT_STYLE \
  49. (wxFNTP_FONTDESC_AS_LABEL | wxFNTP_USEFONT_FOR_LABEL)
  50. // native version currently only exists in wxGTK2
  51. #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
  52. #include "wx/gtk/fontpicker.h"
  53. #define wxFontPickerWidget wxFontButton
  54. #else
  55. #include "wx/generic/fontpickerg.h"
  56. #define wxFontPickerWidget wxGenericFontButton
  57. #endif
  58. // ----------------------------------------------------------------------------
  59. // wxFontPickerCtrl specific flags
  60. // ----------------------------------------------------------------------------
  61. #define wxFNTP_USE_TEXTCTRL (wxPB_USE_TEXTCTRL)
  62. #define wxFNTP_DEFAULT_STYLE (wxFNTP_FONTDESC_AS_LABEL|wxFNTP_USEFONT_FOR_LABEL)
  63. // not a style but rather the default value of the maximum pointsize allowed
  64. #define wxFNTP_MAXPOINT_SIZE 100
  65. // ----------------------------------------------------------------------------
  66. // wxFontPickerCtrl: platform-independent class which embeds the
  67. // platform-dependent wxFontPickerWidget andm if wxFNTP_USE_TEXTCTRL style is
  68. // used, a textctrl next to it.
  69. // ----------------------------------------------------------------------------
  70. class WXDLLIMPEXP_CORE wxFontPickerCtrl : public wxPickerBase
  71. {
  72. public:
  73. wxFontPickerCtrl()
  74. : m_nMaxPointSize(wxFNTP_MAXPOINT_SIZE)
  75. {
  76. }
  77. virtual ~wxFontPickerCtrl() {}
  78. wxFontPickerCtrl(wxWindow *parent,
  79. wxWindowID id,
  80. const wxFont& initial = wxNullFont,
  81. const wxPoint& pos = wxDefaultPosition,
  82. const wxSize& size = wxDefaultSize,
  83. long style = wxFNTP_DEFAULT_STYLE,
  84. const wxValidator& validator = wxDefaultValidator,
  85. const wxString& name = wxFontPickerCtrlNameStr)
  86. : m_nMaxPointSize(wxFNTP_MAXPOINT_SIZE)
  87. {
  88. Create(parent, id, initial, pos, size, style, validator, name);
  89. }
  90. bool Create(wxWindow *parent,
  91. wxWindowID id,
  92. const wxFont& initial = wxNullFont,
  93. const wxPoint& pos = wxDefaultPosition,
  94. const wxSize& size = wxDefaultSize,
  95. long style = wxFNTP_DEFAULT_STYLE,
  96. const wxValidator& validator = wxDefaultValidator,
  97. const wxString& name = wxFontPickerCtrlNameStr);
  98. public: // public API
  99. // get the font chosen
  100. wxFont GetSelectedFont() const
  101. { return ((wxFontPickerWidget *)m_picker)->GetSelectedFont(); }
  102. // sets currently displayed font
  103. void SetSelectedFont(const wxFont& f);
  104. // set/get the max pointsize
  105. void SetMaxPointSize(unsigned int max)
  106. { m_nMaxPointSize=max; }
  107. unsigned int GetMaxPointSize() const
  108. { return m_nMaxPointSize; }
  109. public: // internal functions
  110. void UpdatePickerFromTextCtrl();
  111. void UpdateTextCtrlFromPicker();
  112. // event handler for our picker
  113. void OnFontChange(wxFontPickerEvent &);
  114. // used to convert wxString <-> wxFont
  115. virtual wxString Font2String(const wxFont &font);
  116. virtual wxFont String2Font(const wxString &font);
  117. protected:
  118. // extracts the style for our picker from wxFontPickerCtrl's style
  119. long GetPickerStyle(long style) const
  120. { return (style & (wxFNTP_FONTDESC_AS_LABEL|wxFNTP_USEFONT_FOR_LABEL)); }
  121. // the maximum pointsize allowed to the user
  122. unsigned int m_nMaxPointSize;
  123. private:
  124. DECLARE_DYNAMIC_CLASS(wxFontPickerCtrl)
  125. };
  126. // ----------------------------------------------------------------------------
  127. // wxFontPickerEvent: used by wxFontPickerCtrl only
  128. // ----------------------------------------------------------------------------
  129. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE, wxEVT_FONTPICKER_CHANGED, wxFontPickerEvent );
  130. class WXDLLIMPEXP_CORE wxFontPickerEvent : public wxCommandEvent
  131. {
  132. public:
  133. wxFontPickerEvent() {}
  134. wxFontPickerEvent(wxObject *generator, int id, const wxFont &f)
  135. : wxCommandEvent(wxEVT_FONTPICKER_CHANGED, id),
  136. m_font(f)
  137. {
  138. SetEventObject(generator);
  139. }
  140. wxFont GetFont() const { return m_font; }
  141. void SetFont(const wxFont &c) { m_font = c; }
  142. // default copy ctor, assignment operator and dtor are ok
  143. virtual wxEvent *Clone() const { return new wxFontPickerEvent(*this); }
  144. private:
  145. wxFont m_font;
  146. DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxFontPickerEvent)
  147. };
  148. // ----------------------------------------------------------------------------
  149. // event types and macros
  150. // ----------------------------------------------------------------------------
  151. typedef void (wxEvtHandler::*wxFontPickerEventFunction)(wxFontPickerEvent&);
  152. #define wxFontPickerEventHandler(func) \
  153. wxEVENT_HANDLER_CAST(wxFontPickerEventFunction, func)
  154. #define EVT_FONTPICKER_CHANGED(id, fn) \
  155. wx__DECLARE_EVT1(wxEVT_FONTPICKER_CHANGED, id, wxFontPickerEventHandler(fn))
  156. // old wxEVT_COMMAND_* constants
  157. #define wxEVT_COMMAND_FONTPICKER_CHANGED wxEVT_FONTPICKER_CHANGED
  158. #endif // wxUSE_FONTPICKERCTRL
  159. #endif
  160. // _WX_FONTPICKER_H_BASE_