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