| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/gtk/msgdlg.cpp |
| 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 |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // For compilers that support precompilation, includes "wx.h". |
| 13 | #include "wx/wxprec.h" |
| 14 | |
| 15 | #ifdef __BORLANDC__ |
| 16 | #pragma hdrstop |
| 17 | #endif |
| 18 | |
| 19 | #if wxUSE_MSGDLG && !defined(__WXGPE__) |
| 20 | |
| 21 | #include "wx/msgdlg.h" |
| 22 | |
| 23 | #ifndef WX_PRECOMP |
| 24 | #include "wx/intl.h" |
| 25 | #endif |
| 26 | |
| 27 | #include "wx/gtk/private.h" |
| 28 | #include "wx/gtk/private/messagetype.h" |
| 29 | #include "wx/gtk/private/mnemonics.h" |
| 30 | #include <gtk/gtk.h> |
| 31 | |
| 32 | #if wxUSE_LIBHILDON |
| 33 | #include <hildon-widgets/hildon-note.h> |
| 34 | #endif // wxUSE_LIBHILDON |
| 35 | |
| 36 | #if wxUSE_LIBHILDON2 |
| 37 | #include <hildon/hildon.h> |
| 38 | #endif // wxUSE_LIBHILDON2 |
| 39 | |
| 40 | IMPLEMENT_CLASS(wxMessageDialog, wxDialog) |
| 41 | |
| 42 | wxMessageDialog::wxMessageDialog(wxWindow *parent, |
| 43 | const wxString& message, |
| 44 | const wxString& caption, |
| 45 | long style, |
| 46 | const wxPoint& WXUNUSED(pos)) |
| 47 | : wxMessageDialogBase |
| 48 | ( |
| 49 | GetParentForModalDialog(parent, style), |
| 50 | message, |
| 51 | caption, |
| 52 | style |
| 53 | ) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | wxString wxMessageDialog::GetDefaultYesLabel() const |
| 58 | { |
| 59 | return GTK_STOCK_YES; |
| 60 | } |
| 61 | |
| 62 | wxString wxMessageDialog::GetDefaultNoLabel() const |
| 63 | { |
| 64 | return GTK_STOCK_NO; |
| 65 | } |
| 66 | |
| 67 | wxString wxMessageDialog::GetDefaultOKLabel() const |
| 68 | { |
| 69 | return GTK_STOCK_OK; |
| 70 | } |
| 71 | |
| 72 | wxString wxMessageDialog::GetDefaultCancelLabel() const |
| 73 | { |
| 74 | return GTK_STOCK_CANCEL; |
| 75 | } |
| 76 | |
| 77 | wxString wxMessageDialog::GetDefaultHelpLabel() const |
| 78 | { |
| 79 | return GTK_STOCK_HELP; |
| 80 | } |
| 81 | |
| 82 | void wxMessageDialog::DoSetCustomLabel(wxString& var, const ButtonLabel& label) |
| 83 | { |
| 84 | int stockId = label.GetStockId(); |
| 85 | if ( stockId == wxID_NONE ) |
| 86 | { |
| 87 | wxMessageDialogBase::DoSetCustomLabel(var, label); |
| 88 | var = wxConvertMnemonicsToGTK(var); |
| 89 | } |
| 90 | else // stock label |
| 91 | { |
| 92 | var = wxGetStockGtkID(stockId); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void wxMessageDialog::GTKCreateMsgDialog() |
| 97 | { |
| 98 | GtkWindow * const parent = m_parent ? GTK_WINDOW(m_parent->m_widget) : NULL; |
| 99 | |
| 100 | #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2 |
| 101 | const char *stockIcon = ""; |
| 102 | |
| 103 | switch ( GetEffectiveIcon() ) |
| 104 | { |
| 105 | case wxICON_ERROR: |
| 106 | stockIcon = "qgn_note_gene_syserror"; |
| 107 | break; |
| 108 | |
| 109 | case wxICON_WARNING: |
| 110 | stockIcon = "qgn_note_gene_syswarning"; |
| 111 | break; |
| 112 | |
| 113 | case wxICON_QUESTION: |
| 114 | stockIcon = "qgn_note_confirm"; |
| 115 | break; |
| 116 | |
| 117 | case wxICON_INFORMATION: |
| 118 | stockIcon = "qgn_note_info"; |
| 119 | break; |
| 120 | } |
| 121 | |
| 122 | // there is no generic note creation function in public API so we have no |
| 123 | // choice but to use g_object_new() directly |
| 124 | m_widget = (GtkWidget *)g_object_new |
| 125 | ( |
| 126 | HILDON_TYPE_NOTE, |
| 127 | #if wxUSE_LIBHILDON |
| 128 | "note_type", HILDON_NOTE_CONFIRMATION_BUTTON_TYPE, |
| 129 | #else // wxUSE_LIBHILDON |
| 130 | "note_type", HILDON_NOTE_TYPE_CONFIRMATION_BUTTON, |
| 131 | #endif // wxUSE_LIBHILDON /wxUSE_LIBHILDON2 |
| 132 | "description", (const char *)GetFullMessage().utf8_str(), |
| 133 | "icon", stockIcon, |
| 134 | NULL |
| 135 | ); |
| 136 | #else // !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2 |
| 137 | GtkMessageType type = GTK_MESSAGE_ERROR; |
| 138 | GtkButtonsType buttons = GTK_BUTTONS_NONE; |
| 139 | |
| 140 | // when using custom labels, we have to add all the buttons ourselves |
| 141 | if ( !HasCustomLabels() ) |
| 142 | { |
| 143 | // "Help" button is not supported by predefined combinations so we |
| 144 | // always need to create the buttons manually when it's used. |
| 145 | if ( !(m_dialogStyle & wxHELP) ) |
| 146 | { |
| 147 | if ( m_dialogStyle & wxYES_NO ) |
| 148 | { |
| 149 | if ( !(m_dialogStyle & wxCANCEL) ) |
| 150 | buttons = GTK_BUTTONS_YES_NO; |
| 151 | //else: no standard GTK_BUTTONS_YES_NO_CANCEL so leave as NONE |
| 152 | } |
| 153 | else if ( m_dialogStyle & wxOK ) |
| 154 | { |
| 155 | buttons = m_dialogStyle & wxCANCEL ? GTK_BUTTONS_OK_CANCEL |
| 156 | : GTK_BUTTONS_OK; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | if ( !wxGTKImpl::ConvertMessageTypeFromWX(GetEffectiveIcon(), &type) ) |
| 162 | { |
| 163 | // if no style is explicitly specified, detect the suitable icon |
| 164 | // ourselves (this can be disabled by using wxICON_NONE) |
| 165 | type = m_dialogStyle & wxYES ? GTK_MESSAGE_QUESTION : GTK_MESSAGE_INFO; |
| 166 | } |
| 167 | |
| 168 | wxString message; |
| 169 | #if GTK_CHECK_VERSION(2, 6, 0) |
| 170 | bool needsExtMessage = false; |
| 171 | if ( gtk_check_version(2, 6, 0) == NULL && !m_extendedMessage.empty() ) |
| 172 | { |
| 173 | message = m_message; |
| 174 | needsExtMessage = true; |
| 175 | } |
| 176 | else // extended message not needed or not supported |
| 177 | #endif // GTK+ 2.6+ |
| 178 | { |
| 179 | message = GetFullMessage(); |
| 180 | } |
| 181 | |
| 182 | m_widget = gtk_message_dialog_new(parent, |
| 183 | GTK_DIALOG_MODAL, |
| 184 | type, |
| 185 | buttons, |
| 186 | "%s", |
| 187 | (const char*)wxGTK_CONV(message)); |
| 188 | |
| 189 | #if GTK_CHECK_VERSION(2, 6, 0) |
| 190 | if ( needsExtMessage ) |
| 191 | { |
| 192 | gtk_message_dialog_format_secondary_text |
| 193 | ( |
| 194 | (GtkMessageDialog *)m_widget, |
| 195 | "%s", |
| 196 | (const char *)wxGTK_CONV(m_extendedMessage) |
| 197 | ); |
| 198 | } |
| 199 | #endif // GTK+ 2.6+ |
| 200 | #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2/!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2 |
| 201 | |
| 202 | g_object_ref(m_widget); |
| 203 | |
| 204 | if (m_caption != wxMessageBoxCaptionStr) |
| 205 | gtk_window_set_title(GTK_WINDOW(m_widget), wxGTK_CONV(m_caption)); |
| 206 | |
| 207 | GtkDialog * const dlg = GTK_DIALOG(m_widget); |
| 208 | |
| 209 | if ( m_dialogStyle & wxSTAY_ON_TOP ) |
| 210 | { |
| 211 | gtk_window_set_keep_above(GTK_WINDOW(m_widget), TRUE); |
| 212 | } |
| 213 | |
| 214 | // we need to add buttons manually if we use custom labels or always for |
| 215 | // Yes/No/Cancel dialog as GTK+ doesn't support it natively and when using |
| 216 | // Hildon we add all the buttons manually as it doesn't support too many of |
| 217 | // the combinations we may have |
| 218 | #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2 |
| 219 | static const bool addButtons = true; |
| 220 | #else // !wxUSE_LIBHILDON |
| 221 | const bool addButtons = buttons == GTK_BUTTONS_NONE; |
| 222 | #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON |
| 223 | |
| 224 | |
| 225 | if ( addButtons ) |
| 226 | { |
| 227 | if ( m_dialogStyle & wxHELP ) |
| 228 | { |
| 229 | gtk_dialog_add_button(dlg, wxGTK_CONV(GetHelpLabel()), |
| 230 | GTK_RESPONSE_HELP); |
| 231 | } |
| 232 | |
| 233 | if ( m_dialogStyle & wxYES_NO ) // Yes/No or Yes/No/Cancel dialog |
| 234 | { |
| 235 | // Add the buttons in the correct order which is, according to |
| 236 | // http://library.gnome.org/devel/hig-book/stable/windows-alert.html.en |
| 237 | // the following one: |
| 238 | // |
| 239 | // [Help] [Alternative] [Cancel] [Affirmative] |
| 240 | |
| 241 | gtk_dialog_add_button(dlg, wxGTK_CONV(GetNoLabel()), |
| 242 | GTK_RESPONSE_NO); |
| 243 | |
| 244 | if ( m_dialogStyle & wxCANCEL ) |
| 245 | { |
| 246 | gtk_dialog_add_button(dlg, wxGTK_CONV(GetCancelLabel()), |
| 247 | GTK_RESPONSE_CANCEL); |
| 248 | } |
| 249 | |
| 250 | gtk_dialog_add_button(dlg, wxGTK_CONV(GetYesLabel()), |
| 251 | GTK_RESPONSE_YES); |
| 252 | } |
| 253 | else // Ok or Ok/Cancel dialog |
| 254 | { |
| 255 | gtk_dialog_add_button(dlg, wxGTK_CONV(GetOKLabel()), GTK_RESPONSE_OK); |
| 256 | if ( m_dialogStyle & wxCANCEL ) |
| 257 | { |
| 258 | gtk_dialog_add_button(dlg, wxGTK_CONV(GetCancelLabel()), |
| 259 | GTK_RESPONSE_CANCEL); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | gint defaultButton; |
| 265 | if ( m_dialogStyle & wxCANCEL_DEFAULT ) |
| 266 | defaultButton = GTK_RESPONSE_CANCEL; |
| 267 | else if ( m_dialogStyle & wxNO_DEFAULT ) |
| 268 | defaultButton = GTK_RESPONSE_NO; |
| 269 | else if ( m_dialogStyle & wxYES_NO ) |
| 270 | defaultButton = GTK_RESPONSE_YES; |
| 271 | else // No need to change the default value, whatever it is. |
| 272 | defaultButton = GTK_RESPONSE_NONE; |
| 273 | |
| 274 | if ( defaultButton != GTK_RESPONSE_NONE ) |
| 275 | gtk_dialog_set_default_response(dlg, defaultButton); |
| 276 | } |
| 277 | |
| 278 | int wxMessageDialog::ShowModal() |
| 279 | { |
| 280 | // break the mouse capture as it would interfere with modal dialog (see |
| 281 | // wxDialog::ShowModal) |
| 282 | wxWindow * const win = wxWindow::GetCapture(); |
| 283 | if ( win ) |
| 284 | win->GTKReleaseMouseAndNotify(); |
| 285 | |
| 286 | if ( !m_widget ) |
| 287 | { |
| 288 | GTKCreateMsgDialog(); |
| 289 | wxCHECK_MSG( m_widget, wxID_CANCEL, |
| 290 | wxT("failed to create GtkMessageDialog") ); |
| 291 | } |
| 292 | |
| 293 | // This should be necessary, but otherwise the |
| 294 | // parent TLW will disappear.. |
| 295 | if (m_parent) |
| 296 | gtk_window_present( GTK_WINDOW(m_parent->m_widget) ); |
| 297 | |
| 298 | gint result = gtk_dialog_run(GTK_DIALOG(m_widget)); |
| 299 | gtk_widget_destroy(m_widget); |
| 300 | g_object_unref(m_widget); |
| 301 | m_widget = NULL; |
| 302 | |
| 303 | switch (result) |
| 304 | { |
| 305 | default: |
| 306 | wxFAIL_MSG(wxT("unexpected GtkMessageDialog return code")); |
| 307 | // fall through |
| 308 | |
| 309 | case GTK_RESPONSE_CANCEL: |
| 310 | case GTK_RESPONSE_DELETE_EVENT: |
| 311 | case GTK_RESPONSE_CLOSE: |
| 312 | return wxID_CANCEL; |
| 313 | case GTK_RESPONSE_OK: |
| 314 | return wxID_OK; |
| 315 | case GTK_RESPONSE_YES: |
| 316 | return wxID_YES; |
| 317 | case GTK_RESPONSE_NO: |
| 318 | return wxID_NO; |
| 319 | case GTK_RESPONSE_HELP: |
| 320 | return wxID_HELP; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | |
| 325 | #endif // wxUSE_MSGDLG && !defined(__WXGPE__) |