Allow non-toplevel-window to given as the
[wxWidgets.git] / src / gtk / msgdlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "msgdlg.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #if wxUSE_MSGDLG && defined(__WXGTK20__) && !defined(__WXGPE__)
24
25 #include "wx/gtk/private.h"
26 #include <gtk/gtk.h>
27
28 #include "wx/msgdlg.h"
29 #include "wx/intl.h"
30
31 IMPLEMENT_CLASS(wxMessageDialog, wxDialog)
32
33 wxMessageDialog::wxMessageDialog(wxWindow *parent,
34 const wxString& message,
35 const wxString& caption,
36 long style,
37 const wxPoint& WXUNUSED(pos))
38 {
39 m_caption = caption;
40 m_message = message;
41 m_dialogStyle = style;
42
43 while (!parent->IsTopLevel())
44 parent = parent->GetParent();
45
46 m_parent = parent;
47 }
48
49 int wxMessageDialog::ShowModal()
50 {
51 GtkWidget *dlg;
52 GtkMessageType type = GTK_MESSAGE_ERROR;
53 GtkButtonsType buttons = GTK_BUTTONS_OK;
54
55 if (m_dialogStyle & wxYES_NO)
56 {
57 buttons = GTK_BUTTONS_YES_NO;
58 }
59
60 if (m_dialogStyle & wxOK)
61 {
62 if (m_dialogStyle & wxCANCEL)
63 buttons = GTK_BUTTONS_OK_CANCEL;
64 else
65 buttons = GTK_BUTTONS_OK;
66 }
67
68 if (m_dialogStyle & wxICON_EXCLAMATION)
69 type = GTK_MESSAGE_WARNING;
70 else if (m_dialogStyle & wxICON_ERROR)
71 type = GTK_MESSAGE_ERROR;
72 else if (m_dialogStyle & wxICON_INFORMATION)
73 type = GTK_MESSAGE_INFO;
74 else if (m_dialogStyle & wxICON_QUESTION)
75 type = GTK_MESSAGE_QUESTION;
76 else
77 wxFAIL_MSG( _T("Unknown wxMessageDialog type") );
78
79 dlg = gtk_message_dialog_new(m_parent ?
80 GTK_WINDOW(m_parent->m_widget) : NULL,
81 GTK_DIALOG_MODAL,
82 type, buttons,
83 "%s", (const char*)wxGTK_CONV(m_message));
84 if (m_caption != wxMessageBoxCaptionStr)
85 gtk_window_set_title(GTK_WINDOW(dlg), wxGTK_CONV(m_caption));
86
87 if (m_dialogStyle & wxYES_NO)
88 {
89 if (m_dialogStyle & wxCANCEL)
90 gtk_dialog_add_button(GTK_DIALOG(dlg), GTK_STOCK_CANCEL,
91 GTK_RESPONSE_CANCEL);
92 if (m_dialogStyle & wxNO_DEFAULT)
93 gtk_dialog_set_default_response(GTK_DIALOG(dlg), GTK_RESPONSE_NO);
94 else
95 gtk_dialog_set_default_response(GTK_DIALOG(dlg), GTK_RESPONSE_YES);
96 }
97
98 gint result = gtk_dialog_run(GTK_DIALOG(dlg));
99 gtk_widget_destroy(dlg);
100
101 switch (result)
102 {
103 default:
104 wxFAIL_MSG(_T("unexpected GtkMessageDialog return code"));
105 // fall through
106
107 case GTK_RESPONSE_CANCEL:
108 return wxID_CANCEL;
109 case GTK_RESPONSE_OK:
110 return wxID_OK;
111 case GTK_RESPONSE_YES:
112 return wxID_YES;
113 case GTK_RESPONSE_NO:
114 return wxID_NO;
115 }
116 }
117
118 #endif // wxUSE_MSGDLG && defined(__WXGTK20__)
119