| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/motif/msgdlg.cpp |
| 3 | // Purpose: wxMessageDialog |
| 4 | // Author: Julian Smart |
| 5 | // Modified by: |
| 6 | // Created: 04/01/98 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Julian Smart |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================ |
| 13 | // declarations |
| 14 | // ============================================================================ |
| 15 | |
| 16 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
| 17 | #pragma implementation "msgdlg.h" |
| 18 | #endif |
| 19 | |
| 20 | // ---------------------------------------------------------------------------- |
| 21 | // headers |
| 22 | // ---------------------------------------------------------------------------- |
| 23 | |
| 24 | // For compilers that support precompilation, includes "wx.h". |
| 25 | #include "wx/wxprec.h" |
| 26 | |
| 27 | #include "wx/defs.h" |
| 28 | |
| 29 | #ifdef __VMS |
| 30 | #define XtDisplay XTDISPLAY |
| 31 | #pragma message disable nosimpint |
| 32 | #include <wx/vms_x_fix.h> |
| 33 | #endif |
| 34 | #include <X11/Xlib.h> |
| 35 | |
| 36 | #include <Xm/Xm.h> |
| 37 | #include <Xm/MessageB.h> |
| 38 | #ifdef __VMS |
| 39 | #pragma message enable nosimpint |
| 40 | #endif |
| 41 | |
| 42 | #include "wx/app.h" |
| 43 | #include "wx/intl.h" |
| 44 | #include "wx/msgdlg.h" |
| 45 | #include "wx/motif/private.h" |
| 46 | |
| 47 | // ---------------------------------------------------------------------------- |
| 48 | // macros |
| 49 | // ---------------------------------------------------------------------------- |
| 50 | |
| 51 | IMPLEMENT_CLASS(wxMessageDialog, wxDialog) |
| 52 | |
| 53 | // ============================================================================ |
| 54 | // implementation |
| 55 | // ============================================================================ |
| 56 | |
| 57 | // ---------------------------------------------------------------------------- |
| 58 | // the callbacks for message box buttons |
| 59 | // ---------------------------------------------------------------------------- |
| 60 | |
| 61 | // the common part |
| 62 | static void msgboxCallBack(Widget w, void* client_data, int id) |
| 63 | { |
| 64 | // close the dialog |
| 65 | XtUnmanageChild(w); |
| 66 | |
| 67 | wxMessageDialog *dlg = (wxMessageDialog *)client_data; |
| 68 | dlg->SetResult(id); |
| 69 | } |
| 70 | |
| 71 | static void msgboxCallBackOk(Widget w, |
| 72 | void* client_data, |
| 73 | XmAnyCallbackStruct *WXUNUSED(call_data)) |
| 74 | { |
| 75 | msgboxCallBack(w, client_data, wxID_OK); |
| 76 | } |
| 77 | |
| 78 | static void msgboxCallBackCancel(Widget w, |
| 79 | void* client_data, |
| 80 | XmAnyCallbackStruct *WXUNUSED(call_data)) |
| 81 | { |
| 82 | msgboxCallBack(w, client_data, wxID_CANCEL); |
| 83 | } |
| 84 | |
| 85 | static void msgboxCallBackHelp(Widget w, |
| 86 | void* client_data, |
| 87 | XmAnyCallbackStruct *WXUNUSED(call_data)) |
| 88 | { |
| 89 | msgboxCallBack(w, client_data, wxID_HELP); |
| 90 | } |
| 91 | |
| 92 | static void msgboxCallBackClose(Widget w, |
| 93 | void* client_data, |
| 94 | XmAnyCallbackStruct *WXUNUSED(call_data)) |
| 95 | { |
| 96 | msgboxCallBack(w, client_data, wxID_CANCEL); |
| 97 | } |
| 98 | |
| 99 | // ---------------------------------------------------------------------------- |
| 100 | // wxMessageDialog |
| 101 | // ---------------------------------------------------------------------------- |
| 102 | |
| 103 | wxMessageDialog::wxMessageDialog(wxWindow *parent, |
| 104 | const wxString& message, |
| 105 | const wxString& caption, |
| 106 | long style, |
| 107 | const wxPoint& WXUNUSED(pos)) |
| 108 | { |
| 109 | m_caption = caption; |
| 110 | m_message = message; |
| 111 | m_parent = parent; |
| 112 | SetMessageDialogStyle(style); |
| 113 | } |
| 114 | |
| 115 | int wxMessageDialog::ShowModal() |
| 116 | { |
| 117 | Widget (*dialogCreateFunction)(Widget, String, ArgList, Cardinal) = NULL; |
| 118 | const long style = GetMessageDialogStyle(); |
| 119 | |
| 120 | if ( style & wxYES_NO ) |
| 121 | { |
| 122 | // if we have [Yes], it must be a question |
| 123 | dialogCreateFunction = XmCreateQuestionDialog; |
| 124 | } |
| 125 | else if ( style & wxICON_STOP ) |
| 126 | { |
| 127 | // error dialog is the one with error icon... |
| 128 | dialogCreateFunction = XmCreateErrorDialog; |
| 129 | } |
| 130 | else if ( style & wxICON_EXCLAMATION ) |
| 131 | { |
| 132 | // ...and the warning dialog too |
| 133 | dialogCreateFunction = XmCreateWarningDialog; |
| 134 | } |
| 135 | else |
| 136 | { |
| 137 | // finally, use the info dialog by default |
| 138 | dialogCreateFunction = XmCreateInformationDialog; |
| 139 | } |
| 140 | |
| 141 | Widget wParent = m_parent ? GetWidget(m_parent) : (Widget) 0; |
| 142 | if ( !wParent ) |
| 143 | { |
| 144 | wxWindow *window = wxTheApp->GetTopWindow(); |
| 145 | if ( !window ) |
| 146 | { |
| 147 | wxFAIL_MSG("can't show message box without parent window"); |
| 148 | |
| 149 | return wxID_CANCEL; |
| 150 | } |
| 151 | |
| 152 | wParent = GetWidget(window); |
| 153 | } |
| 154 | |
| 155 | // prepare the arg list |
| 156 | Arg args[10]; |
| 157 | int ac = 0; |
| 158 | |
| 159 | wxXmString text(m_message); |
| 160 | wxXmString title(m_caption); |
| 161 | XtSetArg(args[ac], XmNmessageString, text()); ac++; |
| 162 | XtSetArg(args[ac], XmNdialogTitle, title()); ac++; |
| 163 | |
| 164 | wxComputeColours (XtDisplay(wParent), & m_backgroundColour, |
| 165 | (wxColour*) NULL); |
| 166 | |
| 167 | XtSetArg(args[ac], XmNbackground, g_itemColors[wxBACK_INDEX].pixel); ac++; |
| 168 | XtSetArg(args[ac], XmNtopShadowColor, g_itemColors[wxTOPS_INDEX].pixel); ac++; |
| 169 | XtSetArg(args[ac], XmNbottomShadowColor, g_itemColors[wxBOTS_INDEX].pixel); ac++; |
| 170 | XtSetArg(args[ac], XmNforeground, g_itemColors[wxFORE_INDEX].pixel); ac++; |
| 171 | |
| 172 | // do create message box |
| 173 | |
| 174 | Widget wMsgBox = (*dialogCreateFunction)(wParent, "", args, ac); |
| 175 | |
| 176 | wxCHECK_MSG( wMsgBox, wxID_CANCEL, "msg box creation failed" ); |
| 177 | |
| 178 | // get the buttons which we might either remove or rename |
| 179 | // depending on the requested style |
| 180 | // |
| 181 | Widget wBtnOk = XmMessageBoxGetChild(wMsgBox, XmDIALOG_OK_BUTTON); |
| 182 | Widget wBtnHelp = XmMessageBoxGetChild(wMsgBox, XmDIALOG_HELP_BUTTON); |
| 183 | Widget wBtnCancel = XmMessageBoxGetChild(wMsgBox, XmDIALOG_CANCEL_BUTTON); |
| 184 | |
| 185 | if ( style & wxYES_NO ) |
| 186 | { |
| 187 | wxXmString yes(_("Yes")), no(_("No")), cancel(_("Cancel")); |
| 188 | |
| 189 | if ( style & wxCANCEL ) |
| 190 | { |
| 191 | // use the cancel button for No and the help button for |
| 192 | // Cancel Yuk :-) MB |
| 193 | // |
| 194 | XtVaSetValues(wBtnOk, XmNlabelString, yes(), NULL); |
| 195 | XtVaSetValues(wBtnCancel, XmNlabelString, no(), NULL); |
| 196 | XtVaSetValues(wBtnHelp, XmNlabelString, cancel(), NULL); |
| 197 | } |
| 198 | else |
| 199 | { |
| 200 | // no cancel button requested... |
| 201 | // remove the help button and use cancel for no |
| 202 | // |
| 203 | XtVaSetValues(wBtnCancel, XmNlabelString, no(), NULL); |
| 204 | XtUnmanageChild(wBtnHelp); |
| 205 | } |
| 206 | } |
| 207 | else |
| 208 | { |
| 209 | // remove the help button and the cancel button (unless it was |
| 210 | // requested) |
| 211 | // |
| 212 | XtUnmanageChild(wBtnHelp); |
| 213 | if ( !(style & wxCANCEL ) ) XtUnmanageChild(wBtnCancel); |
| 214 | } |
| 215 | |
| 216 | // set the callbacks for the message box buttons |
| 217 | XtAddCallback(wMsgBox, XmNokCallback, |
| 218 | (XtCallbackProc)msgboxCallBackOk, (XtPointer)this); |
| 219 | XtAddCallback(wMsgBox, XmNcancelCallback, |
| 220 | (XtCallbackProc)msgboxCallBackCancel, (XtPointer)this); |
| 221 | XtAddCallback(wMsgBox, XmNhelpCallback, |
| 222 | (XtCallbackProc)msgboxCallBackHelp, (XtPointer)this); |
| 223 | XtAddCallback(wMsgBox, XmNunmapCallback, |
| 224 | (XtCallbackProc)msgboxCallBackClose, (XtPointer)this); |
| 225 | |
| 226 | // show it as a modal dialog |
| 227 | XtManageChild(wMsgBox); |
| 228 | XtAddGrab(wMsgBox, True, False); |
| 229 | |
| 230 | // the m_result will be changed when message box goes away |
| 231 | m_result = -1; |
| 232 | |
| 233 | // local message loop |
| 234 | XtAppContext context = XtWidgetToApplicationContext(wParent); |
| 235 | XEvent event; |
| 236 | while ( m_result == -1 ) |
| 237 | { |
| 238 | XtAppNextEvent(context, &event); |
| 239 | XtDispatchEvent(&event); |
| 240 | } |
| 241 | |
| 242 | // translate the result if necessary |
| 243 | if ( style & wxYES_NO ) |
| 244 | { |
| 245 | if ( m_result == wxID_OK ) |
| 246 | m_result = wxID_YES; |
| 247 | else if ( m_result == wxID_CANCEL ) |
| 248 | m_result = wxID_NO; |
| 249 | else if ( m_result == wxID_HELP ) |
| 250 | m_result = wxID_CANCEL; |
| 251 | } |
| 252 | |
| 253 | return m_result; |
| 254 | } |
| 255 | |