]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/fontdlg.cpp
Avoid unexpected negative array index (CID 41).
[wxWidgets.git] / src / gtk / 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 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #include "wx/defs.h"
14
15 #if wxUSE_FONTDLG
16
17 #ifndef __WXGPE__
18
19 #include "wx/fontdlg.h"
20 #include "wx/fontutil.h"
21 #include "wx/utils.h"
22 #include "wx/intl.h"
23 #include "wx/debug.h"
24 #include "wx/msgdlg.h"
25
26 #include "wx/gtk/private.h"
27
28 //-----------------------------------------------------------------------------
29 // "delete_event"
30 //-----------------------------------------------------------------------------
31
32 extern "C" {
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 //-----------------------------------------------------------------------------
53 // "clicked" for OK-button
54 //-----------------------------------------------------------------------------
55
56 extern "C" {
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
65 gchar *fontname = gtk_font_selection_dialog_get_font_name(fontdlg);
66 dialog->SetChosenFont( fontname);
67
68 g_free( fontname );
69
70 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
71 event.SetEventObject( dialog );
72 dialog->GetEventHandler()->ProcessEvent( event );
73 }
74 }
75
76 //-----------------------------------------------------------------------------
77 // "clicked" for Cancel-button
78 //-----------------------------------------------------------------------------
79
80 extern "C" {
81 static
82 void gtk_fontdialog_cancel_callback( GtkWidget *WXUNUSED(w), wxFontDialog *dialog )
83 {
84 if (g_isIdle)
85 wxapp_install_idle_handler();
86
87 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
88 event.SetEventObject( dialog );
89 dialog->GetEventHandler()->ProcessEvent( event );
90 }
91 }
92
93 //-----------------------------------------------------------------------------
94 // wxFontDialog
95 //-----------------------------------------------------------------------------
96
97 IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog)
98
99 bool wxFontDialog::DoCreate(wxWindow *parent)
100 {
101 m_needParent = FALSE;
102
103 if (!PreCreation( parent, wxDefaultPosition, wxDefaultSize ) ||
104 !CreateBase( parent, -1, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE,
105 wxDefaultValidator, wxT("fontdialog") ))
106 {
107 wxFAIL_MSG( wxT("wxFontDialog creation failed") );
108 return FALSE;
109 }
110
111 wxString m_message( _("Choose font") );
112 m_widget = gtk_font_selection_dialog_new( wxGTK_CONV( m_message ) );
113
114 if (parent)
115 gtk_window_set_transient_for(GTK_WINDOW(m_widget),
116 GTK_WINDOW(parent->m_widget));
117
118 GtkFontSelectionDialog *sel = GTK_FONT_SELECTION_DIALOG(m_widget);
119
120 g_signal_connect (sel->ok_button, "clicked",
121 G_CALLBACK (gtk_fontdialog_ok_callback), this);
122
123 g_signal_connect (sel->cancel_button, "clicked",
124 G_CALLBACK (gtk_fontdialog_cancel_callback), this);
125
126 g_signal_connect (m_widget, "delete_event",
127 G_CALLBACK (gtk_fontdialog_delete_callback), this);
128
129 wxFont font = m_fontData.GetInitialFont();
130 if( font.Ok() )
131 {
132 const wxNativeFontInfo *info = font.GetNativeFontInfo();
133
134 if ( info )
135 {
136
137 const wxString& fontname = info->ToString();
138 gtk_font_selection_dialog_set_font_name(sel, wxGTK_CONV(fontname));
139 }
140 else
141 {
142 // this is not supposed to happen!
143 wxFAIL_MSG(_T("font is ok but no native font info?"));
144 }
145 }
146
147 return TRUE;
148 }
149
150 wxFontDialog::~wxFontDialog()
151 {
152 }
153
154 void wxFontDialog::SetChosenFont(const char *fontname)
155 {
156 m_fontData.SetChosenFont(wxFont( wxString::FromAscii(fontname) ));
157 }
158
159 #endif // wxUSE_FONTDLG
160
161 #endif // GPE
162