]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/fontdlg.cpp
corrected conditional compilation for Universal Interfaces (3.4 or later)
[wxWidgets.git] / src / gtk1 / fontdlg.cpp
CommitLineData
6dfaa4e5
RR
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
7826e2dd 14#include "wx/fontutil.h"
6dfaa4e5
RR
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
fb5dc8a2 21#include <gtk/gtk.h>
6dfaa4e5
RR
22
23//-----------------------------------------------------------------------------
24// idle system
25//-----------------------------------------------------------------------------
26
27extern void wxapp_install_idle_handler();
28extern bool g_isIdle;
29
30//-----------------------------------------------------------------------------
31// "delete_event"
32//-----------------------------------------------------------------------------
33
34static
35bool gtk_fontdialog_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxDialog *win )
36{
0b862e20 37 if (g_isIdle)
6dfaa4e5
RR
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
0b862e20 56#ifdef __WXGTK12__
6dfaa4e5
RR
57static
58void gtk_fontdialog_ok_callback( GtkWidget *WXUNUSED(widget), wxFontDialog *dialog )
59{
0b862e20 60 if (g_isIdle)
6dfaa4e5
RR
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);
0b862e20 65
6dfaa4e5
RR
66 if (!gfont)
67 {
7826e2dd
VZ
68 wxMessageBox(_("Please choose a valid font."), _("Error"),
69 wxOK | wxICON_ERROR);
6dfaa4e5
RR
70 return;
71 }
0b862e20 72
6dfaa4e5 73 gchar *fontname = gtk_font_selection_dialog_get_font_name(fontdlg);
7beba2fc
VZ
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 {
58c837a4 89 wxFAIL_MSG( wxT("no registry in X font spec?") );
7beba2fc
VZ
90 }
91
92 // restore the dash we changed to NUL above
93 *(fontname + strlen(fontname)) = '-';
94 }
95 else
96 {
58c837a4 97 wxFAIL_MSG( wxT("no encoding in X font spec?") );
7beba2fc
VZ
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
7beba2fc
VZ
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)
7826e2dd 111 dialog->m_fontData.SetChosenFont(wxFont(fontname, fontdata.GetEncoding()));
7beba2fc 112
6dfaa4e5 113 g_free( fontname );
0b862e20 114
6dfaa4e5
RR
115 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
116 event.SetEventObject( dialog );
117 dialog->GetEventHandler()->ProcessEvent( event );
118}
7beba2fc 119#endif // GTK+ 1.2 and later only
6dfaa4e5
RR
120
121//-----------------------------------------------------------------------------
122// "clicked" for Cancel-button
123//-----------------------------------------------------------------------------
124
125static
126void gtk_fontdialog_cancel_callback( GtkWidget *WXUNUSED(w), wxFontDialog *dialog )
127{
0b862e20 128 if (g_isIdle)
6dfaa4e5
RR
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
140IMPLEMENT_DYNAMIC_CLASS(wxFontDialog,wxDialog)
141
7beba2fc
VZ
142wxFontDialog::wxFontDialog( wxWindow *parent, wxFontData *fontdata )
143 : m_fontData(*fontdata)
6dfaa4e5
RR
144{
145 m_needParent = FALSE;
146
147 if (!PreCreation( parent, wxDefaultPosition, wxDefaultSize ) ||
0b862e20 148 !CreateBase( parent, -1, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE,
223d09f6 149 wxDefaultValidator, wxT("fontdialog") ))
6dfaa4e5 150 {
223d09f6 151 wxFAIL_MSG( wxT("wxXX creation failed") );
0b862e20 152 return;
6dfaa4e5 153 }
58c837a4 154
6dfaa4e5
RR
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);
0b862e20 163
6dfaa4e5
RR
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 );
0b862e20 172
6dfaa4e5
RR
173 // strange way to internationalize
174 gtk_label_set( GTK_LABEL( GTK_BUTTON(sel->cancel_button)->child ), wxConvCurrent->cWX2MB(_("Cancel")) );
0b862e20 175
6dfaa4e5
RR
176 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
177 GTK_SIGNAL_FUNC(gtk_fontdialog_delete_callback), (gpointer)this );
30764ab5
VZ
178
179 wxFont font = m_fontData.GetInitialFont();
180 if( font.Ok() )
181 {
7826e2dd 182 wxNativeFontInfo *info = font.GetNativeFontInfo();
30764ab5 183
7826e2dd
VZ
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 }
30764ab5 197 }
6dfaa4e5
RR
198}
199
200wxFontDialog::~wxFontDialog()
201{
202}
203