]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/msgdlg.cpp
Use real id for wxPGCanvas
[wxWidgets.git] / src / gtk / msgdlg.cpp
CommitLineData
13a7abf9 1/////////////////////////////////////////////////////////////////////////////
e5b50758 2// Name: src/gtk/msgdlg.cpp
13a7abf9
VS
3// Purpose: wxMessageDialog for GTK+2
4// Author: Vaclav Slavik
5// Modified by:
6// Created: 2003/02/28
7// RCS-ID: $Id$
8// Copyright: (c) Vaclav Slavik, 2003
e5b50758 9// Licence: wxWindows licence
13a7abf9
VS
10/////////////////////////////////////////////////////////////////////////////
11
13a7abf9
VS
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
88a7a4e1 16 #pragma hdrstop
13a7abf9
VS
17#endif
18
ff654490 19#if wxUSE_MSGDLG && !defined(__WXGPE__)
13a7abf9 20
e1bf3ad3 21#include "wx/msgdlg.h"
88a7a4e1
WS
22
23#ifndef WX_PRECOMP
24 #include "wx/intl.h"
25#endif
26
13a7abf9 27#include "wx/gtk/private.h"
92763588 28#include "wx/gtk/private/mnemonics.h"
13a7abf9
VS
29#include <gtk/gtk.h>
30
c96d7bec
VZ
31#if wxUSE_LIBHILDON
32 #include <hildon-widgets/hildon-note.h>
33#endif // wxUSE_LIBHILDON
34
13a7abf9
VS
35IMPLEMENT_CLASS(wxMessageDialog, wxDialog)
36
37wxMessageDialog::wxMessageDialog(wxWindow *parent,
38 const wxString& message,
39 const wxString& caption,
40 long style,
41 const wxPoint& WXUNUSED(pos))
92763588
VZ
42 : wxMessageDialogWithCustomLabels(GetParentForModalDialog(parent),
43 message,
44 caption,
45 style)
13a7abf9 46{
2afb9e16 47}
13a7abf9 48
92763588
VZ
49wxString wxMessageDialog::GetDefaultYesLabel() const
50{
51 return GTK_STOCK_YES;
52}
53
54wxString wxMessageDialog::GetDefaultNoLabel() const
55{
56 return GTK_STOCK_NO;
57}
58
59wxString wxMessageDialog::GetDefaultOKLabel() const
60{
61 return GTK_STOCK_OK;
62}
63
64wxString wxMessageDialog::GetDefaultCancelLabel() const
65{
66 return GTK_STOCK_CANCEL;
67}
68
e08931c0 69void wxMessageDialog::DoSetCustomLabel(wxString& var, const ButtonLabel& label)
92763588 70{
e08931c0
VZ
71 int stockId = label.GetStockId();
72 if ( stockId == wxID_NONE )
73 {
74 wxMessageDialogWithCustomLabels::DoSetCustomLabel(var, label);
75 var = wxConvertMnemonicsToGTK(var);
76 }
77 else // stock label
78 {
79 var = wxGetStockGtkID(stockId);
80 }
92763588
VZ
81}
82
2afb9e16
VZ
83void wxMessageDialog::GTKCreateMsgDialog()
84{
c96d7bec
VZ
85 GtkWindow * const parent = m_parent ? GTK_WINDOW(m_parent->m_widget) : NULL;
86
87#if wxUSE_LIBHILDON
88 const char *stockIcon;
7e3204b4
VZ
89 if ( m_dialogStyle & wxICON_NONE )
90 stockIcon = "";
91 else if ( m_dialogStyle & wxICON_ERROR )
c96d7bec
VZ
92 stockIcon = "qgn_note_gene_syserror";
93 else if ( m_dialogStyle & wxICON_EXCLAMATION )
94 stockIcon = "qgn_note_gene_syswarning";
95 else if ( m_dialogStyle & wxICON_INFORMATION )
96 stockIcon = "qgn_note_info";
97 else if ( m_dialogStyle & wxICON_QUESTION )
98 stockIcon = "qgn_note_confirm";
99 else
100 stockIcon = "";
101
102 // there is no generic note creation function in public API so we have no
103 // choice but to use g_object_new() directly
104 m_widget = (GtkWidget *)g_object_new
105 (
106 HILDON_TYPE_NOTE,
107 "note_type", HILDON_NOTE_CONFIRMATION_BUTTON_TYPE,
108 "description", (const char *)GetFullMessage().utf8_str(),
109 "icon", stockIcon,
110 NULL
111 );
112#else // !wxUSE_LIBHILDON
b63b07a8 113 GtkMessageType type = GTK_MESSAGE_ERROR;
92763588 114 GtkButtonsType buttons = GTK_BUTTONS_NONE;
13a7abf9 115
92763588
VZ
116 // when using custom labels, we have to add all the buttons ourselves
117 if ( !HasCustomLabels() )
13a7abf9 118 {
92763588
VZ
119 if ( m_dialogStyle & wxYES_NO )
120 {
121 if ( !(m_dialogStyle & wxCANCEL) )
122 buttons = GTK_BUTTONS_YES_NO;
123 //else: no standard GTK_BUTTONS_YES_NO_CANCEL so leave as NONE
124 }
125 else if ( m_dialogStyle & wxOK )
126 {
127 buttons = m_dialogStyle & wxCANCEL ? GTK_BUTTONS_OK_CANCEL
128 : GTK_BUTTONS_OK;
129 }
13a7abf9 130 }
b63b07a8 131
7e3204b4
VZ
132#ifdef __WXGTK210__
133 if ( gtk_check_version(2, 10, 0) == NULL && (m_dialogStyle & wxICON_NONE))
134 type = GTK_MESSAGE_OTHER;
135 else
136#endif // __WXGTK210__
2afb9e16 137 if (m_dialogStyle & wxICON_EXCLAMATION)
13a7abf9 138 type = GTK_MESSAGE_WARNING;
2afb9e16 139 else if (m_dialogStyle & wxICON_ERROR)
13a7abf9 140 type = GTK_MESSAGE_ERROR;
2afb9e16 141 else if (m_dialogStyle & wxICON_INFORMATION)
13a7abf9 142 type = GTK_MESSAGE_INFO;
2afb9e16 143 else if (m_dialogStyle & wxICON_QUESTION)
13a7abf9 144 type = GTK_MESSAGE_QUESTION;
b63b07a8 145 else
e0ae1a0a 146 {
7e3204b4
VZ
147 // if no style is explicitly specified, detect the suitable icon
148 // ourselves (this can be disabled by using wxICON_NONE)
2afb9e16 149 type = m_dialogStyle & wxYES ? GTK_MESSAGE_QUESTION : GTK_MESSAGE_INFO;
e0ae1a0a 150 }
13a7abf9 151
2afb9e16 152 wxString message;
ff9bb105 153#if GTK_CHECK_VERSION(2, 6, 0)
2afb9e16 154 bool needsExtMessage = false;
1878d1ec 155 if ( gtk_check_version(2, 6, 0) == NULL && !m_extendedMessage.empty() )
2afb9e16
VZ
156 {
157 message = m_message;
158 needsExtMessage = true;
159 }
160 else // extended message not needed or not supported
c96d7bec 161#endif // GTK+ 2.6+
2afb9e16
VZ
162 {
163 message = GetFullMessage();
164 }
165
c96d7bec 166 m_widget = gtk_message_dialog_new(parent,
b2ce5e1b 167 GTK_DIALOG_MODAL,
2afb9e16
VZ
168 type,
169 buttons,
170 "%s",
171 (const char*)wxGTK_CONV(message));
172
ff9bb105 173#if GTK_CHECK_VERSION(2, 6, 0)
2afb9e16
VZ
174 if ( needsExtMessage )
175 {
176 gtk_message_dialog_format_secondary_text
177 (
178 (GtkMessageDialog *)m_widget,
179 "%s",
180 (const char *)wxGTK_CONV(m_extendedMessage)
181 );
182 }
c96d7bec
VZ
183#endif // GTK+ 2.6+
184#endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
2afb9e16 185
9ff9d30c
PC
186 g_object_ref(m_widget);
187
13a7abf9 188 if (m_caption != wxMessageBoxCaptionStr)
b2ce5e1b 189 gtk_window_set_title(GTK_WINDOW(m_widget), wxGTK_CONV(m_caption));
13a7abf9 190
c96d7bec 191 GtkDialog * const dlg = GTK_DIALOG(m_widget);
92763588 192
4566dcbe
VZ
193 if ( m_dialogStyle & wxSTAY_ON_TOP )
194 {
195 gtk_window_set_keep_above(GTK_WINDOW(m_widget), TRUE);
196 }
197
92763588
VZ
198 // we need to add buttons manually if we use custom labels or always for
199 // Yes/No/Cancel dialog as GTK+ doesn't support it natively and when using
200 // Hildon we add all the buttons manually as it doesn't support too many of
201 // the combinations we may have
c96d7bec 202#if wxUSE_LIBHILDON
92763588
VZ
203 static const bool addButtons = true;
204#else // !wxUSE_LIBHILDON
205 const bool addButtons = buttons == GTK_BUTTONS_NONE;
206#endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON
207
208 if ( m_dialogStyle & wxYES_NO ) // Yes/No or Yes/No/Cancel dialog
209 {
210 if ( addButtons )
c96d7bec 211 {
150c8eb9
VZ
212 gtk_dialog_add_button(dlg, wxGTK_CONV(GetNoLabel()),
213 GTK_RESPONSE_NO);
214 gtk_dialog_add_button(dlg, wxGTK_CONV(GetYesLabel()),
215 GTK_RESPONSE_YES);
92763588
VZ
216
217 if ( m_dialogStyle & wxCANCEL )
218 {
150c8eb9 219 gtk_dialog_add_button(dlg, wxGTK_CONV(GetCancelLabel()),
92763588
VZ
220 GTK_RESPONSE_CANCEL);
221 }
c96d7bec 222 }
c96d7bec 223
24689293
VZ
224 // it'd probably be harmless to call gtk_dialog_set_default_response()
225 // twice but why do it if we're going to change the default below
226 // anyhow
227 if ( !(m_dialogStyle & wxCANCEL_DEFAULT) )
228 {
229 gtk_dialog_set_default_response(dlg,
230 m_dialogStyle & wxNO_DEFAULT
231 ? GTK_RESPONSE_NO
232 : GTK_RESPONSE_YES);
233 }
c96d7bec 234 }
92763588 235 else if ( addButtons ) // Ok or Ok/Cancel dialog
c96d7bec 236 {
150c8eb9 237 gtk_dialog_add_button(dlg, wxGTK_CONV(GetOKLabel()), GTK_RESPONSE_OK);
c96d7bec 238 if ( m_dialogStyle & wxCANCEL )
150c8eb9
VZ
239 {
240 gtk_dialog_add_button(dlg, wxGTK_CONV(GetCancelLabel()),
241 GTK_RESPONSE_CANCEL);
242 }
13a7abf9 243 }
24689293
VZ
244
245 if ( m_dialogStyle & wxCANCEL_DEFAULT )
246 {
247 gtk_dialog_set_default_response(dlg, GTK_RESPONSE_CANCEL);
248 }
b2ce5e1b 249}
e5b50758 250
b2ce5e1b
RD
251int wxMessageDialog::ShowModal()
252{
7738af59
VZ
253 // break the mouse capture as it would interfere with modal dialog (see
254 // wxDialog::ShowModal)
255 wxWindow * const win = wxWindow::GetCapture();
256 if ( win )
257 win->GTKReleaseMouseAndNotify();
258
2afb9e16
VZ
259 if ( !m_widget )
260 {
261 GTKCreateMsgDialog();
262 wxCHECK_MSG( m_widget, wxID_CANCEL,
9a83f860 263 wxT("failed to create GtkMessageDialog") );
2afb9e16
VZ
264 }
265
3b439e60
RR
266 // This should be necessary, but otherwise the
267 // parent TLW will disappear..
268 if (m_parent)
269 gtk_window_present( GTK_WINDOW(m_parent->m_widget) );
e5b50758 270
b2ce5e1b
RD
271 gint result = gtk_dialog_run(GTK_DIALOG(m_widget));
272 gtk_widget_destroy(m_widget);
9ff9d30c 273 g_object_unref(m_widget);
b2ce5e1b 274 m_widget = NULL;
13a7abf9
VS
275
276 switch (result)
277 {
278 default:
9a83f860 279 wxFAIL_MSG(wxT("unexpected GtkMessageDialog return code"));
13a7abf9
VS
280 // fall through
281
b63b07a8 282 case GTK_RESPONSE_CANCEL:
0e4a7045
VS
283 case GTK_RESPONSE_DELETE_EVENT:
284 case GTK_RESPONSE_CLOSE:
13a7abf9
VS
285 return wxID_CANCEL;
286 case GTK_RESPONSE_OK:
287 return wxID_OK;
288 case GTK_RESPONSE_YES:
289 return wxID_YES;
290 case GTK_RESPONSE_NO:
291 return wxID_NO;
292 }
293}
294
b2ce5e1b 295
ff654490 296#endif // wxUSE_MSGDLG && !defined(__WXGPE__)