]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/generic/fontpickerg.cpp | |
3 | // Purpose: wxGenericFontButton class implementation | |
4 | // Author: Francesco Montorsi | |
5 | // Modified by: | |
6 | // Created: 15/04/2006 | |
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 | ||
26 | #if wxUSE_FONTPICKERCTRL | |
27 | ||
28 | #include "wx/fontpicker.h" | |
29 | ||
30 | #include "wx/fontdlg.h" | |
31 | ||
32 | ||
33 | // ============================================================================ | |
34 | // implementation | |
35 | // ============================================================================ | |
36 | ||
37 | IMPLEMENT_DYNAMIC_CLASS(wxGenericFontButton, wxButton) | |
38 | ||
39 | // ---------------------------------------------------------------------------- | |
40 | // wxGenericFontButton | |
41 | // ---------------------------------------------------------------------------- | |
42 | ||
43 | bool wxGenericFontButton::Create( wxWindow *parent, wxWindowID id, | |
44 | const wxFont &initial, const wxPoint &pos, | |
45 | const wxSize &size, long style, | |
46 | const wxValidator& validator, const wxString &name) | |
47 | { | |
48 | wxString label = (style & wxFNTP_FONTDESC_AS_LABEL) ? | |
49 | wxString() : // label will be updated by UpdateFont | |
50 | _("Choose font"); | |
51 | ||
52 | // create this button | |
53 | if (!wxButton::Create( parent, id, label, pos, | |
54 | size, style, validator, name )) | |
55 | { | |
56 | wxFAIL_MSG( wxT("wxGenericFontButton creation failed") ); | |
57 | return false; | |
58 | } | |
59 | ||
60 | // and handle user clicks on it | |
61 | Connect(GetId(), wxEVT_BUTTON, | |
62 | wxCommandEventHandler(wxGenericFontButton::OnButtonClick), | |
63 | NULL, this); | |
64 | ||
65 | m_selectedFont = initial.IsOk() ? initial : *wxNORMAL_FONT; | |
66 | UpdateFont(); | |
67 | InitFontData(); | |
68 | ||
69 | return true; | |
70 | } | |
71 | ||
72 | void wxGenericFontButton::InitFontData() | |
73 | { | |
74 | m_data.SetAllowSymbols(true); | |
75 | m_data.SetColour(*wxBLACK); | |
76 | m_data.EnableEffects(true); | |
77 | } | |
78 | ||
79 | void wxGenericFontButton::OnButtonClick(wxCommandEvent& WXUNUSED(ev)) | |
80 | { | |
81 | // update the wxFontData to be shown in the dialog | |
82 | m_data.SetInitialFont(m_selectedFont); | |
83 | ||
84 | // create the font dialog and display it | |
85 | wxFontDialog dlg(this, m_data); | |
86 | if (dlg.ShowModal() == wxID_OK) | |
87 | { | |
88 | m_data = dlg.GetFontData(); | |
89 | SetSelectedFont(m_data.GetChosenFont()); | |
90 | ||
91 | // fire an event | |
92 | wxFontPickerEvent event(this, GetId(), m_selectedFont); | |
93 | GetEventHandler()->ProcessEvent(event); | |
94 | } | |
95 | } | |
96 | ||
97 | void wxGenericFontButton::UpdateFont() | |
98 | { | |
99 | if ( !m_selectedFont.IsOk() ) | |
100 | return; | |
101 | ||
102 | SetForegroundColour(m_data.GetColour()); | |
103 | ||
104 | if (HasFlag(wxFNTP_USEFONT_FOR_LABEL)) | |
105 | { | |
106 | // use currently selected font for the label... | |
107 | wxButton::SetFont(m_selectedFont); | |
108 | } | |
109 | ||
110 | if (HasFlag(wxFNTP_FONTDESC_AS_LABEL)) | |
111 | { | |
112 | SetLabel(wxString::Format(wxT("%s, %d"), | |
113 | m_selectedFont.GetFaceName().c_str(), | |
114 | m_selectedFont.GetPointSize())); | |
115 | } | |
116 | } | |
117 | ||
118 | #endif // wxUSE_FONTPICKERCTRL |