applied wxNativeFontInfo patch from Derry Bryson (with minor changes)
[wxWidgets.git] / src / gtk1 / fontdlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: fontdlg.cpp
3 // Purpose: wxFontDialog
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "fontdlg.h"
12 #endif
13
14 #include "wx/fontdlg.h"
15 #include "wx/utils.h"
16 #include "wx/intl.h"
17 #include "wx/debug.h"
18 #include "wx/msgdlg.h"
19
20 #include <gtk/gtk.h>
21
22 //-----------------------------------------------------------------------------
23 // idle system
24 //-----------------------------------------------------------------------------
25
26 extern void wxapp_install_idle_handler();
27 extern bool g_isIdle;
28
29 //-----------------------------------------------------------------------------
30 // "delete_event"
31 //-----------------------------------------------------------------------------
32
33 static
34 bool gtk_fontdialog_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxDialog *win )
35 {
36 if (g_isIdle)
37 wxapp_install_idle_handler();
38
39 /*
40 printf( "OnDelete from " );
41 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
42 printf( win->GetClassInfo()->GetClassName() );
43 printf( ".\n" );
44 */
45
46 win->Close();
47
48 return TRUE;
49 }
50
51 //-----------------------------------------------------------------------------
52 // "clicked" for OK-button
53 //-----------------------------------------------------------------------------
54
55 #ifdef __WXGTK12__
56 static
57 void gtk_fontdialog_ok_callback( GtkWidget *WXUNUSED(widget), wxFontDialog *dialog )
58 {
59 if (g_isIdle)
60 wxapp_install_idle_handler();
61
62 GtkFontSelectionDialog *fontdlg = GTK_FONT_SELECTION_DIALOG(dialog->m_widget);
63 GdkFont *gfont = gtk_font_selection_dialog_get_font(fontdlg);
64
65 if (!gfont)
66 {
67 wxMessageBox(_("Please choose a valid font."), _("Error"), wxOK);
68 return;
69 }
70
71 gchar *fontname = gtk_font_selection_dialog_get_font_name(fontdlg);
72
73 // extract the relevant bits from it
74 wxString xregistry, xencoding;
75 char *dash = strrchr(fontname, '-'); // find the last dash
76 if ( dash )
77 {
78 xencoding = dash + 1;
79 *dash = '\0';
80 dash = strrchr(fontname, '-'); // the last before one
81 if ( dash )
82 {
83 xregistry = dash + 1;
84 }
85 else
86 {
87 wxFAIL_MSG( wxT("no registry in X font spec?") );
88 }
89
90 // restore the dash we changed to NUL above
91 *(fontname + strlen(fontname)) = '-';
92 }
93 else
94 {
95 wxFAIL_MSG( wxT("no encoding in X font spec?") );
96 }
97
98 // transfer the X registry/encoding to wxFontData - they are used by
99 // wxFontMapper after wxFontDialog returns
100 wxFontData& fontdata = dialog->m_fontData;
101
102 // we ignore the facename here - should be enough to choose an arbitrary
103 // one if the registry/encoding are specified
104 // dialog->m_fontData.EncodingInfo().facename = xfamily;
105 fontdata.EncodingInfo().xregistry = xregistry;
106 fontdata.EncodingInfo().xencoding = xencoding;
107
108 // pass fontdata to wxFont ctor so that it can get the encoding from there
109 // if it is already known (otherwise it will try to deduce it itself)
110 dialog->m_fontData.SetChosenFont( wxFont(fontname, fontdata) );
111
112 g_free( fontname );
113
114 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
115 event.SetEventObject( dialog );
116 dialog->GetEventHandler()->ProcessEvent( event );
117 }
118 #endif // GTK+ 1.2 and later only
119
120 //-----------------------------------------------------------------------------
121 // "clicked" for Cancel-button
122 //-----------------------------------------------------------------------------
123
124 static
125 void gtk_fontdialog_cancel_callback( GtkWidget *WXUNUSED(w), wxFontDialog *dialog )
126 {
127 if (g_isIdle)
128 wxapp_install_idle_handler();
129
130 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
131 event.SetEventObject( dialog );
132 dialog->GetEventHandler()->ProcessEvent( event );
133 }
134
135 //-----------------------------------------------------------------------------
136 // wxFontDialog
137 //-----------------------------------------------------------------------------
138
139 IMPLEMENT_DYNAMIC_CLASS(wxFontDialog,wxDialog)
140
141 wxFontDialog::wxFontDialog( wxWindow *parent, wxFontData *fontdata )
142 : m_fontData(*fontdata)
143 {
144 m_needParent = FALSE;
145
146 if (!PreCreation( parent, wxDefaultPosition, wxDefaultSize ) ||
147 !CreateBase( parent, -1, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE,
148 wxDefaultValidator, wxT("fontdialog") ))
149 {
150 wxFAIL_MSG( wxT("wxXX creation failed") );
151 return;
152 }
153
154 wxString m_message( _("Choose font") );
155 m_widget = gtk_font_selection_dialog_new( m_message.mbc_str() );
156
157 int x = (gdk_screen_width () - 400) / 2;
158 int y = (gdk_screen_height () - 400) / 2;
159 gtk_widget_set_uposition( m_widget, x, y );
160
161 GtkFontSelectionDialog *sel = GTK_FONT_SELECTION_DIALOG(m_widget);
162
163 gtk_signal_connect( GTK_OBJECT(sel->ok_button), "clicked",
164 GTK_SIGNAL_FUNC(gtk_fontdialog_ok_callback), (gpointer*)this );
165
166 // strange way to internationalize
167 gtk_label_set( GTK_LABEL( GTK_BUTTON(sel->ok_button)->child ), wxConvCurrent->cWX2MB(_("OK")) );
168
169 gtk_signal_connect( GTK_OBJECT(sel->cancel_button), "clicked",
170 GTK_SIGNAL_FUNC(gtk_fontdialog_cancel_callback), (gpointer*)this );
171
172 // strange way to internationalize
173 gtk_label_set( GTK_LABEL( GTK_BUTTON(sel->cancel_button)->child ), wxConvCurrent->cWX2MB(_("Cancel")) );
174
175 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
176 GTK_SIGNAL_FUNC(gtk_fontdialog_delete_callback), (gpointer)this );
177
178 wxFont font = m_fontData.GetInitialFont();
179 if( font.Ok() )
180 {
181 wxNativeFontInfo info = font.GetNativeFontInfo();
182
183 if( info.xFontName.IsEmpty() )
184 font.GetInternalFont();
185 gtk_font_selection_dialog_set_font_name(sel, wxConvCurrent->cWX2MB(info.xFontName.GetData()));
186 }
187 }
188
189 wxFontDialog::~wxFontDialog()
190 {
191 }
192