1 ///////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/gtk/msgdlg.cpp 
   3 // Purpose:     wxMessageDialog for GTK+2 
   4 // Author:      Vaclav Slavik 
   8 // Copyright:   (c) Vaclav Slavik, 2003 
   9 // Licence:     wxWindows licence 
  10 ///////////////////////////////////////////////////////////////////////////// 
  12 // For compilers that support precompilation, includes "wx.h". 
  13 #include "wx/wxprec.h" 
  19 #if wxUSE_MSGDLG && !defined(__WXGPE__) 
  21 #include "wx/msgdlg.h" 
  27 #include "wx/gtk/private.h" 
  28 #include "wx/gtk/private/messagetype.h" 
  29 #include "wx/gtk/private/mnemonics.h" 
  33     #include <hildon-widgets/hildon-note.h> 
  34 #endif // wxUSE_LIBHILDON 
  37     #include <hildon/hildon.h> 
  38 #endif // wxUSE_LIBHILDON2 
  40 IMPLEMENT_CLASS(wxMessageDialog
, wxDialog
) 
  42 wxMessageDialog::wxMessageDialog(wxWindow 
*parent
, 
  43                                  const wxString
& message
, 
  44                                  const wxString
& caption
, 
  46                                  const wxPoint
& WXUNUSED(pos
)) 
  47                : wxMessageDialogWithCustomLabels(GetParentForModalDialog(parent
), 
  54 wxString 
wxMessageDialog::GetDefaultYesLabel() const 
  59 wxString 
wxMessageDialog::GetDefaultNoLabel() const 
  64 wxString 
wxMessageDialog::GetDefaultOKLabel() const 
  69 wxString 
wxMessageDialog::GetDefaultCancelLabel() const 
  71     return GTK_STOCK_CANCEL
; 
  74 void wxMessageDialog::DoSetCustomLabel(wxString
& var
, const ButtonLabel
& label
) 
  76     int stockId 
= label
.GetStockId(); 
  77     if ( stockId 
== wxID_NONE 
) 
  79         wxMessageDialogWithCustomLabels::DoSetCustomLabel(var
, label
); 
  80         var 
= wxConvertMnemonicsToGTK(var
); 
  84         var 
= wxGetStockGtkID(stockId
); 
  88 void wxMessageDialog::GTKCreateMsgDialog() 
  90     GtkWindow 
* const parent 
= m_parent 
? GTK_WINDOW(m_parent
->m_widget
) : NULL
; 
  92 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2 
  93     const char *stockIcon
; 
  94     if ( m_dialogStyle 
& wxICON_NONE 
) 
  96     else if ( m_dialogStyle 
& wxICON_ERROR 
) 
  97         stockIcon 
= "qgn_note_gene_syserror"; 
  98     else if ( m_dialogStyle 
& wxICON_EXCLAMATION 
) 
  99         stockIcon 
= "qgn_note_gene_syswarning"; 
 100     else if ( m_dialogStyle 
& wxICON_INFORMATION 
) 
 101         stockIcon 
= "qgn_note_info"; 
 102     else if ( m_dialogStyle 
& wxICON_QUESTION 
) 
 103         stockIcon 
= "qgn_note_confirm"; 
 107     // there is no generic note creation function in public API so we have no 
 108     // choice but to use g_object_new() directly 
 109     m_widget 
= (GtkWidget 
*)g_object_new
 
 113                 "note_type", HILDON_NOTE_CONFIRMATION_BUTTON_TYPE
, 
 114 #else // wxUSE_LIBHILDON 
 115                 "note_type", HILDON_NOTE_TYPE_CONFIRMATION_BUTTON
, 
 116 #endif // wxUSE_LIBHILDON /wxUSE_LIBHILDON2 
 117                 "description", (const char *)GetFullMessage().utf8_str(), 
 121 #else // !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2 
 122     GtkMessageType type 
= GTK_MESSAGE_ERROR
; 
 123     GtkButtonsType buttons 
= GTK_BUTTONS_NONE
; 
 125     // when using custom labels, we have to add all the buttons ourselves 
 126     if ( !HasCustomLabels() ) 
 128         if ( m_dialogStyle 
& wxYES_NO 
) 
 130             if ( !(m_dialogStyle 
& wxCANCEL
) ) 
 131                 buttons 
= GTK_BUTTONS_YES_NO
; 
 132             //else: no standard GTK_BUTTONS_YES_NO_CANCEL so leave as NONE 
 134         else if ( m_dialogStyle 
& wxOK 
) 
 136             buttons 
= m_dialogStyle 
& wxCANCEL 
? GTK_BUTTONS_OK_CANCEL
 
 141     if ( !wxGTKImpl::ConvertMessageTypeFromWX(m_dialogStyle
, &type
) ) 
 143         // if no style is explicitly specified, detect the suitable icon 
 144         // ourselves (this can be disabled by using wxICON_NONE) 
 145         type 
= m_dialogStyle 
& wxYES 
? GTK_MESSAGE_QUESTION 
: GTK_MESSAGE_INFO
; 
 149 #if GTK_CHECK_VERSION(2, 6, 0) 
 150     bool needsExtMessage 
= false; 
 151     if ( gtk_check_version(2, 6, 0) == NULL 
&& !m_extendedMessage
.empty() ) 
 154         needsExtMessage 
= true; 
 156     else // extended message not needed or not supported 
 159         message 
= GetFullMessage(); 
 162     m_widget 
= gtk_message_dialog_new(parent
, 
 167                                       (const char*)wxGTK_CONV(message
)); 
 169 #if GTK_CHECK_VERSION(2, 6, 0) 
 170     if ( needsExtMessage 
) 
 172         gtk_message_dialog_format_secondary_text
 
 174             (GtkMessageDialog 
*)m_widget
, 
 176             (const char *)wxGTK_CONV(m_extendedMessage
) 
 180 #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2/!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2 
 182     g_object_ref(m_widget
); 
 184     if (m_caption 
!= wxMessageBoxCaptionStr
) 
 185         gtk_window_set_title(GTK_WINDOW(m_widget
), wxGTK_CONV(m_caption
)); 
 187     GtkDialog 
* const dlg 
= GTK_DIALOG(m_widget
); 
 189     if ( m_dialogStyle 
& wxSTAY_ON_TOP 
) 
 191         gtk_window_set_keep_above(GTK_WINDOW(m_widget
), TRUE
); 
 194     // we need to add buttons manually if we use custom labels or always for 
 195     // Yes/No/Cancel dialog as GTK+ doesn't support it natively and when using 
 196     // Hildon we add all the buttons manually as it doesn't support too many of 
 197     // the combinations we may have 
 198 #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2 
 199     static const bool addButtons 
= true; 
 200 #else // !wxUSE_LIBHILDON 
 201     const bool addButtons 
= buttons 
== GTK_BUTTONS_NONE
; 
 202 #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON 
 204     if ( m_dialogStyle 
& wxYES_NO 
) // Yes/No or Yes/No/Cancel dialog 
 208             gtk_dialog_add_button(dlg
, wxGTK_CONV(GetNoLabel()), 
 210             gtk_dialog_add_button(dlg
, wxGTK_CONV(GetYesLabel()), 
 213             if ( m_dialogStyle 
& wxCANCEL 
) 
 215                 gtk_dialog_add_button(dlg
, wxGTK_CONV(GetCancelLabel()), 
 216                                       GTK_RESPONSE_CANCEL
); 
 220         // it'd probably be harmless to call gtk_dialog_set_default_response() 
 221         // twice but why do it if we're going to change the default below 
 223         if ( !(m_dialogStyle 
& wxCANCEL_DEFAULT
) ) 
 225             gtk_dialog_set_default_response(dlg
, 
 226                                             m_dialogStyle 
& wxNO_DEFAULT
 
 231     else if ( addButtons 
) // Ok or Ok/Cancel dialog 
 233         gtk_dialog_add_button(dlg
, wxGTK_CONV(GetOKLabel()), GTK_RESPONSE_OK
); 
 234         if ( m_dialogStyle 
& wxCANCEL 
) 
 236             gtk_dialog_add_button(dlg
, wxGTK_CONV(GetCancelLabel()), 
 237                                   GTK_RESPONSE_CANCEL
); 
 241     if ( m_dialogStyle 
& wxCANCEL_DEFAULT 
) 
 243         gtk_dialog_set_default_response(dlg
, GTK_RESPONSE_CANCEL
); 
 247 int wxMessageDialog::ShowModal() 
 249     // break the mouse capture as it would interfere with modal dialog (see 
 250     // wxDialog::ShowModal) 
 251     wxWindow 
* const win 
= wxWindow::GetCapture(); 
 253         win
->GTKReleaseMouseAndNotify(); 
 257         GTKCreateMsgDialog(); 
 258         wxCHECK_MSG( m_widget
, wxID_CANCEL
, 
 259                      wxT("failed to create GtkMessageDialog") ); 
 262     // This should be necessary, but otherwise the 
 263     // parent TLW will disappear.. 
 265         gtk_window_present( GTK_WINDOW(m_parent
->m_widget
) ); 
 267     gint result 
= gtk_dialog_run(GTK_DIALOG(m_widget
)); 
 268     gtk_widget_destroy(m_widget
); 
 269     g_object_unref(m_widget
); 
 275             wxFAIL_MSG(wxT("unexpected GtkMessageDialog return code")); 
 278         case GTK_RESPONSE_CANCEL
: 
 279         case GTK_RESPONSE_DELETE_EVENT
: 
 280         case GTK_RESPONSE_CLOSE
: 
 282         case GTK_RESPONSE_OK
: 
 284         case GTK_RESPONSE_YES
: 
 286         case GTK_RESPONSE_NO
: 
 292 #endif // wxUSE_MSGDLG && !defined(__WXGPE__)