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