better native font support for wxGTK
[wxWidgets.git] / src / gtk1 / fontdlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/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/defs.h"
15
16 #if wxUSE_FONTDLG
17
18 #include "wx/fontutil.h"
19 #include "wx/fontdlg.h"
20 #include "wx/utils.h"
21 #include "wx/intl.h"
22 #include "wx/debug.h"
23 #include "wx/msgdlg.h"
24
25 #include <gtk/gtk.h>
26
27 //-----------------------------------------------------------------------------
28 // idle system
29 //-----------------------------------------------------------------------------
30
31 extern void wxapp_install_idle_handler();
32 extern bool g_isIdle;
33
34 //-----------------------------------------------------------------------------
35 // "delete_event"
36 //-----------------------------------------------------------------------------
37
38 static
39 bool gtk_fontdialog_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxDialog *win )
40 {
41 if (g_isIdle)
42 wxapp_install_idle_handler();
43
44 /*
45 printf( "OnDelete from " );
46 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
47 printf( win->GetClassInfo()->GetClassName() );
48 printf( ".\n" );
49 */
50
51 win->Close();
52
53 return TRUE;
54 }
55
56 //-----------------------------------------------------------------------------
57 // "clicked" for OK-button
58 //-----------------------------------------------------------------------------
59
60 #ifdef __WXGTK12__
61 static
62 void gtk_fontdialog_ok_callback( GtkWidget *WXUNUSED(widget), wxFontDialog *dialog )
63 {
64 if (g_isIdle)
65 wxapp_install_idle_handler();
66
67 GtkFontSelectionDialog *fontdlg = GTK_FONT_SELECTION_DIALOG(dialog->m_widget);
68 GdkFont *gfont = gtk_font_selection_dialog_get_font(fontdlg);
69
70 if (!gfont)
71 {
72 wxMessageBox(_("Please choose a valid font."), _("Error"),
73 wxOK | wxICON_ERROR);
74 return;
75 }
76
77 gchar *fontname = gtk_font_selection_dialog_get_font_name(fontdlg);
78
79 dialog->m_fontData.SetChosenFont(wxFont(fontname));
80
81 g_free( fontname );
82
83 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
84 event.SetEventObject( dialog );
85 dialog->GetEventHandler()->ProcessEvent( event );
86 }
87 #endif // GTK+ 1.2 and later only
88
89 //-----------------------------------------------------------------------------
90 // "clicked" for Cancel-button
91 //-----------------------------------------------------------------------------
92
93 static
94 void gtk_fontdialog_cancel_callback( GtkWidget *WXUNUSED(w), wxFontDialog *dialog )
95 {
96 if (g_isIdle)
97 wxapp_install_idle_handler();
98
99 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
100 event.SetEventObject( dialog );
101 dialog->GetEventHandler()->ProcessEvent( event );
102 }
103
104 //-----------------------------------------------------------------------------
105 // wxFontDialog
106 //-----------------------------------------------------------------------------
107
108 IMPLEMENT_DYNAMIC_CLASS(wxFontDialog,wxDialog)
109
110 wxFontDialog::wxFontDialog( wxWindow *parent, wxFontData *fontdata )
111 : m_fontData(*fontdata)
112 {
113 m_needParent = FALSE;
114
115 if (!PreCreation( parent, wxDefaultPosition, wxDefaultSize ) ||
116 !CreateBase( parent, -1, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE,
117 wxDefaultValidator, wxT("fontdialog") ))
118 {
119 wxFAIL_MSG( wxT("wxXX creation failed") );
120 return;
121 }
122
123 wxString m_message( _("Choose font") );
124 m_widget = gtk_font_selection_dialog_new( m_message.mbc_str() );
125
126 int x = (gdk_screen_width () - 400) / 2;
127 int y = (gdk_screen_height () - 400) / 2;
128 gtk_widget_set_uposition( m_widget, x, y );
129
130 GtkFontSelectionDialog *sel = GTK_FONT_SELECTION_DIALOG(m_widget);
131
132 gtk_signal_connect( GTK_OBJECT(sel->ok_button), "clicked",
133 GTK_SIGNAL_FUNC(gtk_fontdialog_ok_callback), (gpointer*)this );
134
135 // strange way to internationalize
136 gtk_label_set( GTK_LABEL( GTK_BUTTON(sel->ok_button)->child ), wxConvCurrent->cWX2MB(_("OK")) );
137
138 gtk_signal_connect( GTK_OBJECT(sel->cancel_button), "clicked",
139 GTK_SIGNAL_FUNC(gtk_fontdialog_cancel_callback), (gpointer*)this );
140
141 // strange way to internationalize
142 gtk_label_set( GTK_LABEL( GTK_BUTTON(sel->cancel_button)->child ), wxConvCurrent->cWX2MB(_("Cancel")) );
143
144 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
145 GTK_SIGNAL_FUNC(gtk_fontdialog_delete_callback), (gpointer)this );
146
147 wxFont font = m_fontData.GetInitialFont();
148 if( font.Ok() )
149 {
150 wxNativeFontInfo *info = font.GetNativeFontInfo();
151
152 if ( info )
153 {
154 const wxString& fontname = info->GetXFontName();
155 if ( !fontname )
156 font.GetInternalFont();
157 gtk_font_selection_dialog_set_font_name
158 (
159 sel,
160 wxConvCurrent->cWX2MB(fontname)
161 );
162 }
163 else
164 {
165 // this is not supposed to happen!
166 wxFAIL_MSG(_T("font is ok but no native font info?"));
167 }
168 }
169 }
170
171 wxFontDialog::~wxFontDialog()
172 {
173 }
174
175 #endif // wxUSE_FONTDLG
176