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