]> git.saurik.com Git - wxWidgets.git/blame - src/common/fontpickercmn.cpp
skip apple options
[wxWidgets.git] / src / common / fontpickercmn.cpp
CommitLineData
ec376c8f
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/common/fontpickercmn.cpp
3// Purpose: wxFontPickerCtrl class implementation
4// Author: Francesco Montorsi
5// Modified by:
6// Created: 15/04/2006
ec376c8f
VZ
7// Copyright: (c) Francesco Montorsi
8// Licence: wxWindows licence
9///////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
4ce7b1e4
WS
26#if wxUSE_FONTPICKERCTRL
27
ec376c8f 28#include "wx/fontpicker.h"
c757b5fe 29
fec9cc08
WS
30#ifndef WX_PRECOMP
31 #include "wx/textctrl.h"
32#endif
33
ec376c8f
VZ
34#include "wx/fontenum.h"
35#include "wx/tokenzr.h"
36
ec376c8f
VZ
37// ============================================================================
38// implementation
39// ============================================================================
40
f36e602b
VZ
41const char wxFontPickerCtrlNameStr[] = "fontpicker";
42const char wxFontPickerWidgetNameStr[] = "fontpickerwidget";
43af39c8 43
ce7fe42e 44wxDEFINE_EVENT(wxEVT_FONTPICKER_CHANGED, wxFontPickerEvent);
ec376c8f
VZ
45IMPLEMENT_DYNAMIC_CLASS(wxFontPickerCtrl, wxPickerBase)
46IMPLEMENT_DYNAMIC_CLASS(wxFontPickerEvent, wxCommandEvent)
47
48// ----------------------------------------------------------------------------
49// wxFontPickerCtrl
50// ----------------------------------------------------------------------------
51
52#define M_PICKER ((wxFontPickerWidget*)m_picker)
53
54bool wxFontPickerCtrl::Create( wxWindow *parent, wxWindowID id,
55 const wxFont &initial,
56 const wxPoint &pos, const wxSize &size,
57 long style, const wxValidator& validator,
58 const wxString &name )
59{
305329c2
VZ
60 if (!wxPickerBase::CreateBase(parent, id,
61 Font2String(initial.IsOk() ? initial
62 : *wxNORMAL_FONT),
ec376c8f
VZ
63 pos, size, style, validator, name))
64 return false;
65
66 // the picker of a wxFontPickerCtrl is a wxFontPickerWidget
67 m_picker = new wxFontPickerWidget(this, wxID_ANY, initial,
68 wxDefaultPosition, wxDefaultSize,
69 GetPickerStyle(style));
a65ffcb2
VZ
70 // complete sizer creation
71 wxPickerBase::PostCreation();
72
ce7fe42e 73 m_picker->Connect(wxEVT_FONTPICKER_CHANGED,
ec376c8f
VZ
74 wxFontPickerEventHandler(wxFontPickerCtrl::OnFontChange),
75 NULL, this);
76
77 return true;
78}
79
80wxString wxFontPickerCtrl::Font2String(const wxFont &f)
81{
82 wxString ret = f.GetNativeFontInfoUserDesc();
83#ifdef __WXMSW__
84 // on wxMSW the encoding of the font is appended at the end of the string;
85 // since encoding is not very user-friendly we remove it.
86 wxFontEncoding enc = f.GetEncoding();
87 if ( enc != wxFONTENCODING_DEFAULT && enc != wxFONTENCODING_SYSTEM )
88 ret = ret.BeforeLast(wxT(' '));
89#endif
90 return ret;
91}
92
93wxFont wxFontPickerCtrl::String2Font(const wxString &s)
94{
95 wxString str(s);
96 wxFont ret;
97 double n;
98
99 // put a limit on the maximum point size which the user can enter
100 // NOTE: we suppose the last word of given string is the pointsize
101 wxString size = str.AfterLast(wxT(' '));
102 if (size.ToDouble(&n))
103 {
104 if (n < 1)
4ce7b1e4 105 str = str.Left(str.length() - size.length()) + wxT("1");
ec376c8f 106 else if (n >= m_nMaxPointSize)
4ce7b1e4 107 str = str.Left(str.length() - size.length()) +
ec376c8f
VZ
108 wxString::Format(wxT("%d"), m_nMaxPointSize);
109 }
110
111 if (!ret.SetNativeFontInfoUserDesc(str))
112 return wxNullFont;
113
114 return ret;
115}
116
117void wxFontPickerCtrl::SetSelectedFont(const wxFont &f)
118{
119 M_PICKER->SetSelectedFont(f);
120 UpdateTextCtrlFromPicker();
121}
122
123void wxFontPickerCtrl::UpdatePickerFromTextCtrl()
124{
125 wxASSERT(m_text);
126
ec376c8f
VZ
127 // NB: we don't use the wxFont::wxFont(const wxString &) constructor
128 // since that constructor expects the native font description
129 // string returned by wxFont::GetNativeFontInfoDesc() and not
130 // the user-friendly one returned by wxFont::GetNativeFontInfoUserDesc()
131 wxFont f = String2Font(m_text->GetValue());
a1b806b9 132 if (!f.IsOk())
ec376c8f
VZ
133 return; // invalid user input
134
135 if (M_PICKER->GetSelectedFont() != f)
136 {
137 M_PICKER->SetSelectedFont(f);
138
139 // fire an event
140 wxFontPickerEvent event(this, GetId(), f);
141 GetEventHandler()->ProcessEvent(event);
142 }
143}
144
145void wxFontPickerCtrl::UpdateTextCtrlFromPicker()
146{
147 if (!m_text)
148 return; // no textctrl to update
149
44b72116
VZ
150 // Take care to use ChangeValue() here and not SetValue() to avoid
151 // infinite recursion.
152 m_text->ChangeValue(Font2String(M_PICKER->GetSelectedFont()));
ec376c8f
VZ
153}
154
155
156
157// ----------------------------------------------------------------------------
158// wxFontPickerCtrl - event handlers
159// ----------------------------------------------------------------------------
160
161void wxFontPickerCtrl::OnFontChange(wxFontPickerEvent &ev)
162{
163 UpdateTextCtrlFromPicker();
164
165 // the wxFontPickerWidget sent us a colour-change notification.
166 // forward this event to our parent
167 wxFontPickerEvent event(this, GetId(), ev.GetFont());
168 GetEventHandler()->ProcessEvent(event);
169}
170
171#endif // wxUSE_FONTPICKERCTRL