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