| 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 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Francesco Montorsi |
| 9 | // Licence: wxWindows licence |
| 10 | /////////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================ |
| 13 | // declarations |
| 14 | // ============================================================================ |
| 15 | |
| 16 | // ---------------------------------------------------------------------------- |
| 17 | // headers |
| 18 | // ---------------------------------------------------------------------------- |
| 19 | |
| 20 | // For compilers that support precompilation, includes "wx.h". |
| 21 | #include "wx/wxprec.h" |
| 22 | |
| 23 | #ifdef __BORLANDC__ |
| 24 | #pragma hdrstop |
| 25 | #endif |
| 26 | |
| 27 | #if wxUSE_FONTPICKERCTRL |
| 28 | |
| 29 | #include "wx/fontpicker.h" |
| 30 | |
| 31 | #include "wx/fontdlg.h" |
| 32 | |
| 33 | |
| 34 | // ============================================================================ |
| 35 | // implementation |
| 36 | // ============================================================================ |
| 37 | |
| 38 | wxFontData wxGenericFontButton::ms_data; |
| 39 | IMPLEMENT_DYNAMIC_CLASS(wxGenericFontButton, wxButton) |
| 40 | |
| 41 | // ---------------------------------------------------------------------------- |
| 42 | // wxGenericFontButton |
| 43 | // ---------------------------------------------------------------------------- |
| 44 | |
| 45 | bool wxGenericFontButton::Create( wxWindow *parent, wxWindowID id, |
| 46 | const wxFont &initial, const wxPoint &pos, |
| 47 | const wxSize &size, long style, |
| 48 | const wxValidator& validator, const wxString &name) |
| 49 | { |
| 50 | wxString label = (style & wxFNTP_FONTDESC_AS_LABEL) ? |
| 51 | wxEmptyString : // label will be updated by UpdateFont |
| 52 | wxT("Choose font"); |
| 53 | |
| 54 | // create this button |
| 55 | if (!wxButton::Create( parent, id, label, pos, |
| 56 | size, style, validator, name )) |
| 57 | { |
| 58 | wxFAIL_MSG( wxT("wxGenericFontButton creation failed") ); |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | // and handle user clicks on it |
| 63 | Connect(wxEVT_COMMAND_BUTTON_CLICKED, |
| 64 | wxCommandEventHandler(wxGenericFontButton::OnButtonClick), |
| 65 | NULL, this); |
| 66 | |
| 67 | m_selectedFont = initial; |
| 68 | UpdateFont(); |
| 69 | InitFontData(); |
| 70 | |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | void wxGenericFontButton::InitFontData() |
| 75 | { |
| 76 | ms_data.SetAllowSymbols(true); |
| 77 | ms_data.SetColour(*wxBLACK); |
| 78 | ms_data.EnableEffects(true); |
| 79 | } |
| 80 | |
| 81 | void wxGenericFontButton::OnButtonClick(wxCommandEvent& WXUNUSED(ev)) |
| 82 | { |
| 83 | // update the wxFontData to be shown in the the dialog |
| 84 | ms_data.SetInitialFont(m_selectedFont); |
| 85 | |
| 86 | // create the font dialog and display it |
| 87 | wxFontDialog dlg(this, ms_data); |
| 88 | if (dlg.ShowModal() == wxID_OK) |
| 89 | { |
| 90 | ms_data = dlg.GetFontData(); |
| 91 | SetSelectedFont(ms_data.GetChosenFont()); |
| 92 | |
| 93 | // fire an event |
| 94 | wxFontPickerEvent event(this, GetId(), m_selectedFont); |
| 95 | GetEventHandler()->ProcessEvent(event); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void wxGenericFontButton::UpdateFont() |
| 100 | { |
| 101 | if ( !m_selectedFont.Ok() ) |
| 102 | return; |
| 103 | |
| 104 | SetForegroundColour(ms_data.GetColour()); |
| 105 | |
| 106 | if (HasFlag(wxFNTP_USEFONT_FOR_LABEL)) |
| 107 | { |
| 108 | // use currently selected font for the label... |
| 109 | wxButton::SetFont(m_selectedFont); |
| 110 | } |
| 111 | |
| 112 | if (HasFlag(wxFNTP_FONTDESC_AS_LABEL)) |
| 113 | { |
| 114 | SetLabel(wxString::Format(wxT("%s, %d"), |
| 115 | m_selectedFont.GetFaceName().c_str(), |
| 116 | m_selectedFont.GetPointSize())); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | #endif // wxUSE_FONTPICKERCTRL |