added wx{Colour|File|Dir|Font}PickerCtrl (patch 1472329 by Francesco)
[wxWidgets.git] / src / gtk / fontpicker.cpp
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 // Id: $Id$
8 // Copyright: (c) Francesco Montorsi
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12
13 // ----------------------------------------------------------------------------
14 // headers
15 // ----------------------------------------------------------------------------
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20 #include "wx/gtk/private.h"
21 #include "wx/fontpicker.h"
22
23 #include "wx/fontutil.h" // for wxNativeFontInfo
24 #include "wx/gtk/private.h"
25
26 #include <gdk/gdk.h>
27 #include <gtk/gtk.h>
28
29
30
31 // ============================================================================
32 // implementation
33 // ============================================================================
34
35 #if wxUSE_FONTPICKERCTRL && defined(__WXGTK24__)
36
37 //-----------------------------------------------------------------------------
38 // "font-set"
39 //-----------------------------------------------------------------------------
40
41 extern "C" {
42 static void gtk_fontbutton_setfont_callback(GtkFontButton *widget,
43 wxFontButton *p)
44 {
45 // update the m_selectedFont member of the wxFontButton
46 wxASSERT(p);
47 p->SetNativeFontInfo(gtk_font_button_get_font_name(widget));
48
49 // fire the colour-changed event
50 wxFontPickerEvent event(p, p->GetId(), p->GetSelectedFont());
51 p->GetEventHandler()->ProcessEvent(event);
52 }
53 }
54
55 //-----------------------------------------------------------------------------
56 // wxFontButton
57 //-----------------------------------------------------------------------------
58
59 IMPLEMENT_DYNAMIC_CLASS(wxFontButton, wxGenericFontButton)
60
61 bool wxFontButton::Create( wxWindow *parent, wxWindowID id,
62 const wxFont &initial,
63 const wxPoint &pos, const wxSize &size,
64 long style, const wxValidator& validator,
65 const wxString &name )
66 {
67 if (!gtk_check_version(2,4,0))
68 {
69 m_needParent = true;
70
71 if (!PreCreation( parent, pos, size ) ||
72 !wxControl::CreateBase(parent, id, pos, size, style, validator, name))
73 {
74 wxFAIL_MSG( wxT("wxFontButton creation failed") );
75 return false;
76 }
77
78 m_widget = gtk_font_button_new();
79
80 // set initial font
81 m_selectedFont = initial;
82 UpdateFont();
83
84 // honour the fontbutton styles
85 bool showall = (style & wxFNTP_FONTDESC_AS_LABEL) != 0,
86 usefont = (style & wxFNTP_USEFONT_FOR_LABEL) != 0;
87 gtk_font_button_set_show_style(GTK_FONT_BUTTON(m_widget), showall);
88 gtk_font_button_set_show_size(GTK_FONT_BUTTON(m_widget), showall);
89
90 gtk_font_button_set_use_size(GTK_FONT_BUTTON(m_widget), usefont);
91 gtk_font_button_set_use_font(GTK_FONT_BUTTON(m_widget), usefont);
92
93 gtk_widget_show( GTK_WIDGET(m_widget) );
94
95 // GtkFontButton signals
96 g_signal_connect(m_widget, "font-set",
97 G_CALLBACK(gtk_fontbutton_setfont_callback), this);
98
99
100 m_parent->DoAddChild( this );
101
102 PostCreation(size);
103 SetBestSize(size);
104 }
105 else
106 return wxGenericFontButton::Create(parent, id, initial, pos, size,
107 style, validator, name);
108 return true;
109 }
110
111 wxFontButton::~wxFontButton()
112 {
113 }
114
115 void wxFontButton::UpdateFont()
116 {
117 if (!gtk_check_version(2,4,0))
118 {
119 const wxNativeFontInfo *info = m_selectedFont.GetNativeFontInfo();
120 wxASSERT_MSG( info, wxT("The fontbutton's internal font is not valid ?") );
121
122 const wxString& fontname = info->ToString();
123 gtk_font_button_set_font_name(GTK_FONT_BUTTON(m_widget), wxGTK_CONV(fontname));
124 }
125 else
126 wxGenericFontButton::UpdateFont();
127 }
128
129 #endif // wxUSE_FONTPICKERCTRL