1/////////////////////////////////////////////////////////////////////////////
2// Name:        src/msw/msgdlg.cpp
3// Purpose:     wxMessageDialog
4// Author:      Julian Smart
5// Modified by:
6// Created:     04/01/98
7// RCS-ID:      $Id: msgdlg.cpp 50855 2007-12-20 10:51:33Z JS $
8// Copyright:   (c) Julian Smart
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#include "wx/msgdlg.h"
20
21#ifndef WX_PRECOMP
22    #include "wx/app.h"
23    #include "wx/utils.h"
24    #include "wx/dialog.h"
25#endif
26
27#include "wx/msw/private.h"
28
29// For MB_TASKMODAL
30#ifdef __WXWINCE__
31#include "wx/msw/wince/missing.h"
32#endif
33
34IMPLEMENT_CLASS(wxMessageDialog, wxDialog)
35
36wxMessageDialog::wxMessageDialog(wxWindow *parent,
37                                 const wxString& message,
38                                 const wxString& caption,
39                                 long style,
40                                 const wxPoint& WXUNUSED(pos))
41{
42    m_caption = caption;
43    m_message = message;
44    m_parent = parent;
45    SetMessageDialogStyle(style);
46}
47
48int wxMessageDialog::ShowModal()
49{
50    if ( !wxTheApp->GetTopWindow() )
51    {
52        // when the message box is shown from wxApp::OnInit() (i.e. before the
53        // message loop is entered), this must be done or the next message box
54        // will never be shown - just try putting 2 calls to wxMessageBox() in
55        // OnInit() to see it
56        while ( wxTheApp->Pending() )
57            wxTheApp->Dispatch();
58    }
59
60    // use the top level window as parent if none specified
61    if ( !m_parent )
62        m_parent = FindSuitableParent();
63    HWND hWnd = m_parent ? GetHwndOf(m_parent) : NULL;
64
65    // translate wx style in MSW
66    unsigned int msStyle = MB_OK;
67    const long wxStyle = GetMessageDialogStyle();
68    if (wxStyle & wxYES_NO)
69    {
70#if !(defined(__SMARTPHONE__) && defined(__WXWINCE__))
71        if (wxStyle & wxCANCEL)
72            msStyle = MB_YESNOCANCEL;
73        else
74#endif // !(__SMARTPHONE__ && __WXWINCE__)
75            msStyle = MB_YESNO;
76
77        if (wxStyle & wxNO_DEFAULT)
78            msStyle |= MB_DEFBUTTON2;
79    }
80
81    if (wxStyle & wxOK)
82    {
83        if (wxStyle & wxCANCEL)
84            msStyle = MB_OKCANCEL;
85        else
86            msStyle = MB_OK;
87    }
88    if (wxStyle & wxICON_EXCLAMATION)
89        msStyle |= MB_ICONEXCLAMATION;
90    else if (wxStyle & wxICON_HAND)
91        msStyle |= MB_ICONHAND;
92    else if (wxStyle & wxICON_INFORMATION)
93        msStyle |= MB_ICONINFORMATION;
94    else if (wxStyle & wxICON_QUESTION)
95        msStyle |= MB_ICONQUESTION;
96    else
97    {
98        int majorVersion, minorVersion;
99        wxGetOsVersion(& majorVersion, & minorVersion);
100
101        if ( majorVersion >= 6 )
102            msStyle |= MB_ICONINFORMATION;
103    }
104
105    if ( wxStyle & wxSTAY_ON_TOP )
106        msStyle |= MB_TOPMOST;
107
108#ifndef __WXWINCE__
109    if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
110        msStyle |= MB_RTLREADING | MB_RIGHT;
111#endif
112
113    if (hWnd)
114        msStyle |= MB_APPLMODAL;
115    else
116        msStyle |= MB_TASKMODAL;
117
118    // per MSDN documentation for MessageBox() we can prefix the message with 2
119    // right-to-left mark characters to tell the function to use RTL layout
120    // (unfortunately this only works in Unicode builds)
121    wxString message = m_message;
122#if wxUSE_UNICODE
123    if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
124    {
125        // NB: not all compilers support \u escapes
126        static const wchar_t wchRLM = 0x200f;
127        message.Prepend(wxString(wchRLM, 2));
128    }
129#endif // wxUSE_UNICODE
130
131    // do show the dialog
132    int msAns = MessageBox(hWnd, message, m_caption, msStyle);
133    int ans;
134    switch (msAns)
135    {
136        default:
137            wxFAIL_MSG(_T("unexpected ::MessageBox() return code"));
138            // fall through
139
140        case IDCANCEL:
141            ans = wxID_CANCEL;
142            break;
143        case IDOK:
144            ans = wxID_OK;
145            break;
146        case IDYES:
147            ans = wxID_YES;
148            break;
149        case IDNO:
150            ans = wxID_NO;
151            break;
152    }
153    return ans;
154}
155