1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/fontpicker.cpp
3 // Purpose: implementation of wxFontButton
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 // ----------------------------------------------------------------------------
13 // headers
14 // ----------------------------------------------------------------------------
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #if wxUSE_FONTPICKERCTRL
20
21 #include "wx/fontpicker.h"
22
23 #include "wx/fontutil.h" // for wxNativeFontInfo
24 #include "wx/gtk/private.h"
25
26 // ============================================================================
27 // implementation
28 // ============================================================================
29
30 //-----------------------------------------------------------------------------
31 // "font-set"
32 //-----------------------------------------------------------------------------
33
34 extern "C" {
gtk_fontbutton_setfont_callback(GtkFontButton * widget,wxFontButton * p)35 static void gtk_fontbutton_setfont_callback(GtkFontButton *widget,
36 wxFontButton *p)
37 {
38 // update the m_selectedFont member of the wxFontButton
39 wxASSERT(p);
40 p->SetNativeFontInfo(gtk_font_button_get_font_name(widget));
41
42 // fire the colour-changed event
43 wxFontPickerEvent event(p, p->GetId(), p->GetSelectedFont());
44 p->HandleWindowEvent(event);
45 }
46 }
47
48 //-----------------------------------------------------------------------------
49 // wxFontButton
50 //-----------------------------------------------------------------------------
51
IMPLEMENT_DYNAMIC_CLASS(wxFontButton,wxButton)52 IMPLEMENT_DYNAMIC_CLASS(wxFontButton, wxButton)
53
54 bool wxFontButton::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 {
60 if (!PreCreation( parent, pos, size ) ||
61 !wxControl::CreateBase(parent, id, pos, size, style, validator, name))
62 {
63 wxFAIL_MSG( wxT("wxFontButton creation failed") );
64 return false;
65 }
66
67 m_widget = gtk_font_button_new();
68 g_object_ref(m_widget);
69
70 // set initial font
71 m_selectedFont = initial.IsOk() ? initial : *wxNORMAL_FONT;
72 UpdateFont();
73
74 // honour the fontbutton styles
75 bool showall = (style & wxFNTP_FONTDESC_AS_LABEL) != 0,
76 usefont = (style & wxFNTP_USEFONT_FOR_LABEL) != 0;
77 gtk_font_button_set_show_style(GTK_FONT_BUTTON(m_widget), showall);
78 gtk_font_button_set_show_size(GTK_FONT_BUTTON(m_widget), showall);
79
80 gtk_font_button_set_use_size(GTK_FONT_BUTTON(m_widget), usefont);
81 gtk_font_button_set_use_font(GTK_FONT_BUTTON(m_widget), usefont);
82
83 // GtkFontButton signals
84 g_signal_connect(m_widget, "font-set",
85 G_CALLBACK(gtk_fontbutton_setfont_callback), this);
86
87
88 m_parent->DoAddChild( this );
89
90 PostCreation(size);
91 SetInitialSize(size);
92
93 return true;
94 }
95
~wxFontButton()96 wxFontButton::~wxFontButton()
97 {
98 }
99
UpdateFont()100 void wxFontButton::UpdateFont()
101 {
102 const wxNativeFontInfo *info = m_selectedFont.GetNativeFontInfo();
103 wxASSERT_MSG( info, wxT("The fontbutton's internal font is not valid ?") );
104
105 const wxString& fontname = info->ToString();
106 gtk_font_button_set_font_name(GTK_FONT_BUTTON(m_widget), wxGTK_CONV(fontname));
107 }
108
109 #endif // wxUSE_FONTPICKERCTRL
110