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 11 #ifndef _WX_FONTPICKER_H_BASE_ 12 #define _WX_FONTPICKER_H_BASE_ 13 14 #include "wx/defs.h" 15 16 17 #if wxUSE_FONTPICKERCTRL 18 19 #include "wx/pickerbase.h" 20 21 22 class WXDLLIMPEXP_FWD_CORE wxFontPickerEvent; 23 24 extern WXDLLIMPEXP_DATA_CORE(const char) wxFontPickerWidgetNameStr[]; 25 extern WXDLLIMPEXP_DATA_CORE(const char) wxFontPickerCtrlNameStr[]; 26 27 28 // ---------------------------------------------------------------------------- 29 // wxFontPickerWidgetBase: a generic abstract interface which must be 30 // implemented by controls used by wxFontPickerCtrl 31 // ---------------------------------------------------------------------------- 32 33 class WXDLLIMPEXP_CORE wxFontPickerWidgetBase 34 { 35 public: wxFontPickerWidgetBase()36 wxFontPickerWidgetBase() : m_selectedFont(*wxNORMAL_FONT) { } ~wxFontPickerWidgetBase()37 virtual ~wxFontPickerWidgetBase() {} 38 GetSelectedFont()39 wxFont GetSelectedFont() const 40 { return m_selectedFont; } SetSelectedFont(const wxFont & f)41 virtual void SetSelectedFont(const wxFont &f) 42 { m_selectedFont = f; UpdateFont(); } 43 44 virtual wxColour GetSelectedColour() const = 0; 45 virtual void SetSelectedColour(const wxColour &colour) = 0; 46 47 protected: 48 49 virtual void UpdateFont() = 0; 50 51 // the current font (may be invalid if none) 52 // NOTE: don't call this m_font as wxWindow::m_font already exists 53 wxFont m_selectedFont; 54 }; 55 56 // Styles which must be supported by all controls implementing wxFontPickerWidgetBase 57 // NB: these styles must be defined to carefully-chosen values to 58 // avoid conflicts with wxButton's styles 59 60 61 // keeps the label of the button updated with the fontface name + font size 62 // E.g. choosing "Times New Roman bold, italic with size 10" from the fontdialog, 63 // updates the wxFontButtonGeneric's label (overwriting any previous label) 64 // with the "Times New Roman, 10" text (only fontface + fontsize is displayed 65 // to avoid extralong labels). 66 #define wxFNTP_FONTDESC_AS_LABEL 0x0008 67 68 // uses the currently selected font to draw the label of the button 69 #define wxFNTP_USEFONT_FOR_LABEL 0x0010 70 71 #define wxFONTBTN_DEFAULT_STYLE \ 72 (wxFNTP_FONTDESC_AS_LABEL | wxFNTP_USEFONT_FOR_LABEL) 73 74 // native version currently only exists in wxGTK2 75 #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__) 76 #include "wx/gtk/fontpicker.h" 77 #define wxFontPickerWidget wxFontButton 78 #else 79 #include "wx/generic/fontpickerg.h" 80 #define wxFontPickerWidget wxGenericFontButton 81 #endif 82 83 84 // ---------------------------------------------------------------------------- 85 // wxFontPickerCtrl specific flags 86 // ---------------------------------------------------------------------------- 87 88 #define wxFNTP_USE_TEXTCTRL (wxPB_USE_TEXTCTRL) 89 #define wxFNTP_DEFAULT_STYLE (wxFNTP_FONTDESC_AS_LABEL|wxFNTP_USEFONT_FOR_LABEL) 90 91 // not a style but rather the default value of the minimum/maximum pointsize allowed 92 #define wxFNTP_MINPOINT_SIZE 0 93 #define wxFNTP_MAXPOINT_SIZE 100 94 95 96 // ---------------------------------------------------------------------------- 97 // wxFontPickerCtrl: platform-independent class which embeds the 98 // platform-dependent wxFontPickerWidget andm if wxFNTP_USE_TEXTCTRL style is 99 // used, a textctrl next to it. 100 // ---------------------------------------------------------------------------- 101 102 class WXDLLIMPEXP_CORE wxFontPickerCtrl : public wxPickerBase 103 { 104 public: wxFontPickerCtrl()105 wxFontPickerCtrl() 106 : m_nMinPointSize(wxFNTP_MINPOINT_SIZE), m_nMaxPointSize(wxFNTP_MAXPOINT_SIZE) 107 { 108 } 109 ~wxFontPickerCtrl()110 virtual ~wxFontPickerCtrl() {} 111 112 113 wxFontPickerCtrl(wxWindow *parent, 114 wxWindowID id, 115 const wxFont& initial = wxNullFont, 116 const wxPoint& pos = wxDefaultPosition, 117 const wxSize& size = wxDefaultSize, 118 long style = wxFNTP_DEFAULT_STYLE, 119 const wxValidator& validator = wxDefaultValidator, 120 const wxString& name = wxASCII_STR(wxFontPickerCtrlNameStr)) m_nMinPointSize(wxFNTP_MINPOINT_SIZE)121 : m_nMinPointSize(wxFNTP_MINPOINT_SIZE), m_nMaxPointSize(wxFNTP_MAXPOINT_SIZE) 122 { 123 Create(parent, id, initial, pos, size, style, validator, name); 124 } 125 126 bool Create(wxWindow *parent, 127 wxWindowID id, 128 const wxFont& initial = wxNullFont, 129 const wxPoint& pos = wxDefaultPosition, 130 const wxSize& size = wxDefaultSize, 131 long style = wxFNTP_DEFAULT_STYLE, 132 const wxValidator& validator = wxDefaultValidator, 133 const wxString& name = wxASCII_STR(wxFontPickerCtrlNameStr)); 134 135 136 public: // public API 137 138 // get the font chosen GetSelectedFont()139 wxFont GetSelectedFont() const 140 { return GetPickerWidget()->GetSelectedFont(); } 141 142 // sets currently displayed font 143 void SetSelectedFont(const wxFont& f); 144 145 // returns the selected color GetSelectedColour()146 wxColour GetSelectedColour() const 147 { return GetPickerWidget()->GetSelectedColour(); } 148 149 // sets the currently selected color SetSelectedColour(const wxColour & colour)150 void SetSelectedColour(const wxColour& colour) 151 { GetPickerWidget()->SetSelectedColour(colour); } 152 153 // set/get the min point size 154 void SetMinPointSize(unsigned int min); GetMinPointSize()155 unsigned int GetMinPointSize() const 156 { return m_nMinPointSize; } 157 158 // set/get the max point size 159 void SetMaxPointSize(unsigned int max); GetMaxPointSize()160 unsigned int GetMaxPointSize() const 161 { return m_nMaxPointSize; } 162 163 public: // internal functions 164 165 void UpdatePickerFromTextCtrl() wxOVERRIDE; 166 void UpdateTextCtrlFromPicker() wxOVERRIDE; 167 168 // event handler for our picker 169 void OnFontChange(wxFontPickerEvent &); 170 171 // used to convert wxString <-> wxFont 172 virtual wxString Font2String(const wxFont &font); 173 virtual wxFont String2Font(const wxString &font); 174 175 protected: 176 177 // extracts the style for our picker from wxFontPickerCtrl's style GetPickerStyle(long style)178 long GetPickerStyle(long style) const wxOVERRIDE 179 { return (style & (wxFNTP_FONTDESC_AS_LABEL|wxFNTP_USEFONT_FOR_LABEL)); } 180 181 // the minimum pointsize allowed to the user 182 unsigned int m_nMinPointSize; 183 184 // the maximum pointsize allowed to the user 185 unsigned int m_nMaxPointSize; 186 187 private: GetPickerWidget()188 wxFontPickerWidget* GetPickerWidget() const 189 { return static_cast<wxFontPickerWidget*>(m_picker); } 190 191 wxDECLARE_DYNAMIC_CLASS(wxFontPickerCtrl); 192 }; 193 194 195 // ---------------------------------------------------------------------------- 196 // wxFontPickerEvent: used by wxFontPickerCtrl only 197 // ---------------------------------------------------------------------------- 198 199 wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE, wxEVT_FONTPICKER_CHANGED, wxFontPickerEvent ); 200 201 class WXDLLIMPEXP_CORE wxFontPickerEvent : public wxCommandEvent 202 { 203 public: wxFontPickerEvent()204 wxFontPickerEvent() {} wxFontPickerEvent(wxObject * generator,int id,const wxFont & f)205 wxFontPickerEvent(wxObject *generator, int id, const wxFont &f) 206 : wxCommandEvent(wxEVT_FONTPICKER_CHANGED, id), 207 m_font(f) 208 { 209 SetEventObject(generator); 210 } 211 GetFont()212 wxFont GetFont() const { return m_font; } SetFont(const wxFont & c)213 void SetFont(const wxFont &c) { m_font = c; } 214 215 // default copy ctor, assignment operator and dtor are ok Clone()216 virtual wxEvent *Clone() const wxOVERRIDE { return new wxFontPickerEvent(*this); } 217 218 private: 219 wxFont m_font; 220 221 wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxFontPickerEvent); 222 }; 223 224 // ---------------------------------------------------------------------------- 225 // event types and macros 226 // ---------------------------------------------------------------------------- 227 228 typedef void (wxEvtHandler::*wxFontPickerEventFunction)(wxFontPickerEvent&); 229 230 #define wxFontPickerEventHandler(func) \ 231 wxEVENT_HANDLER_CAST(wxFontPickerEventFunction, func) 232 233 #define EVT_FONTPICKER_CHANGED(id, fn) \ 234 wx__DECLARE_EVT1(wxEVT_FONTPICKER_CHANGED, id, wxFontPickerEventHandler(fn)) 235 236 // old wxEVT_COMMAND_* constants 237 #define wxEVT_COMMAND_FONTPICKER_CHANGED wxEVT_FONTPICKER_CHANGED 238 239 240 #endif // wxUSE_FONTPICKERCTRL 241 242 #endif 243 // _WX_FONTPICKER_H_BASE_ 244