+#if wxUSE_MSGBOX_HOOK
+
+// there can potentially be one message box per thread so we use a hash map
+// with thread ids as keys and (currently shown) message boxes as values
+//
+// TODO: replace this with wxTLS once it's available
+WX_DECLARE_HASH_MAP(unsigned long, wxMessageDialog *,
+ wxIntegerHash, wxIntegerEqual,
+ wxMessageDialogMap);
+
+// the order in this array is the one in which buttons appear in the
+// message box
+const wxMessageDialog::ButtonAccessors wxMessageDialog::ms_buttons[] =
+{
+ { IDYES, &wxMessageDialog::GetYesLabel },
+ { IDNO, &wxMessageDialog::GetNoLabel },
+ { IDOK, &wxMessageDialog::GetOKLabel },
+ { IDCANCEL, &wxMessageDialog::GetCancelLabel },
+};
+
+namespace
+{
+
+wxMessageDialogMap& HookMap()
+{
+ static wxMessageDialogMap s_Map;
+
+ return s_Map;
+}
+
+/*
+ All this code is used for adjusting the message box layout when we mess
+ with its contents. It's rather complicated because we try hard to avoid
+ assuming much about the standard layout details and so, instead of just
+ laying out everything ourselves (which would have been so much simpler!)
+ we try to only modify the existing controls positions by offsetting them
+ from their default ones in the hope that this will continue to work with
+ the future Windows versions.
+ */
+
+// convert the given RECT from screen to client coordinates in place
+void ScreenRectToClient(HWND hwnd, RECT& rc)
+{
+ // map from desktop (i.e. screen) coordinates to ones of this window
+ //
+ // notice that a RECT is laid out as 2 consecutive POINTs so the cast is
+ // valid
+ ::MapWindowPoints(HWND_DESKTOP, hwnd, reinterpret_cast<POINT *>(&rc), 2);
+}
+
+// set window position to the given rect
+inline void SetWindowRect(HWND hwnd, const RECT& rc)
+{
+ ::MoveWindow(hwnd,
+ rc.left, rc.top,
+ rc.right - rc.left, rc.bottom - rc.top,
+ FALSE);
+}
+
+// set window position expressed in screen coordinates, whether the window is
+// child or top level
+void MoveWindowToScreenRect(HWND hwnd, RECT rc)
+{
+ ScreenRectToClient(::GetParent(hwnd), rc);
+
+ SetWindowRect(hwnd, rc);
+}
+
+// helper of AdjustButtonLabels(): move the given window by dx
+//
+// works for both child and top level windows
+void OffsetWindow(HWND hwnd, int dx)
+{
+ RECT rc = wxGetWindowRect(hwnd);
+
+ rc.left += dx;
+ rc.right += dx;
+
+ MoveWindowToScreenRect(hwnd, rc);
+}
+
+} // anonymous namespace
+
+/* static */
+WXLRESULT wxCALLBACK
+wxMessageDialog::HookFunction(int code, WXWPARAM wParam, WXLPARAM lParam)
+{
+ // Find the thread-local instance of wxMessageDialog
+ const DWORD tid = ::GetCurrentThreadId();
+ wxMessageDialogMap::iterator node = HookMap().find(tid);
+ wxCHECK_MSG( node != HookMap().end(), false,
+ wxT("bogus thread id in wxMessageDialog::Hook") );
+
+ wxMessageDialog * const wnd = node->second;
+
+ const HHOOK hhook = (HHOOK)wnd->m_hook;
+ const LRESULT rc = ::CallNextHookEx(hhook, code, wParam, lParam);
+
+ if ( code == HCBT_ACTIVATE )
+ {
+ // we won't need this hook any longer
+ ::UnhookWindowsHookEx(hhook);
+ wnd->m_hook = NULL;
+ HookMap().erase(tid);
+
+ wnd->SetHWND((HWND)wParam);
+
+ // replace the static text with an edit control if the message box is
+ // too big to fit the display
+ wnd->ReplaceStaticWithEdit();
+
+ // update the labels if necessary: we need to do it before centering
+ // the dialog as this can change its size
+ if ( wnd->HasCustomLabels() )
+ wnd->AdjustButtonLabels();
+
+ // centre the message box on its parent if requested
+ if ( wnd->GetMessageDialogStyle() & wxCENTER )
+ wnd->Center(); // center on parent
+ //else: default behaviour, center on screen
+
+ // there seems to be no reason to leave it set
+ wnd->SetHWND(NULL);
+ }
+
+ return rc;
+}
+
+void wxMessageDialog::ReplaceStaticWithEdit()
+{
+ // check if the message box fits the display
+ int nDisplay = wxDisplay::GetFromWindow(this);
+ if ( nDisplay == wxNOT_FOUND )
+ nDisplay = 0;
+ const wxRect rectDisplay = wxDisplay(nDisplay).GetClientArea();
+
+ if ( rectDisplay.Contains(GetRect()) )
+ {
+ // nothing to do
+ return;
+ }
+
+
+ // find the static control to replace: normally there are two of them, the
+ // icon and the text itself so search for all of them and ignore the icon
+ // ones
+ HWND hwndStatic = ::FindWindowEx(GetHwnd(), NULL, wxT("STATIC"), NULL);
+ if ( ::GetWindowLong(hwndStatic, GWL_STYLE) & SS_ICON )
+ hwndStatic = ::FindWindowEx(GetHwnd(), hwndStatic, wxT("STATIC"), NULL);
+
+ if ( !hwndStatic )
+ {
+ wxLogDebug("Failed to find the static text control in message box.");
+ return;
+ }
+
+ // set the right font for GetCharHeight() call below
+ wxWindowBase::SetFont(GetMessageFont());
+
+ // put the new edit control at the same place
+ RECT rc = wxGetWindowRect(hwndStatic);
+ ScreenRectToClient(GetHwnd(), rc);
+
+ // but make it less tall so that the message box fits on the screen: we try
+ // to make the message box take no more than 7/8 of the screen to leave
+ // some space above and below it
+ const int hText = (7*rectDisplay.height)/8 -
+ (
+ 2*::GetSystemMetrics(SM_CYFIXEDFRAME) +
+ ::GetSystemMetrics(SM_CYCAPTION) +
+ 5*GetCharHeight() // buttons + margins
+ );
+ const int dh = (rc.bottom - rc.top) - hText; // vertical space we save
+ rc.bottom -= dh;
+
+ // and it also must be wider as it needs a vertical scrollbar (in order
+ // to preserve the word wrap, otherwise the number of lines would change
+ // and we want the control to look as similar as possible to the original)
+ //
+ // NB: you would have thought that 2*SM_CXEDGE would be enough but it
+ // isn't, somehow, and the text control breaks lines differently from
+ // the static one so fudge by adding some extra space
+ const int dw = ::GetSystemMetrics(SM_CXVSCROLL) +
+ 4*::GetSystemMetrics(SM_CXEDGE);
+ rc.right += dw;
+
+
+ // chop of the trailing new line(s) from the message box text, they are
+ // ignored by the static control but result in extra lines and hence extra
+ // scrollbar position in the edit one
+ wxString text(wxGetWindowText(hwndStatic));
+ for ( wxString::reverse_iterator i = text.rbegin(); i != text.rend(); ++i )
+ {
+ if ( *i != '\n' )
+ {
+ // found last non-newline char, remove everything after it and stop
+ text.erase(i.base() + 1, text.end());
+ break;
+ }
+ }
+
+ // do create the new control
+ HWND hwndEdit = ::CreateWindow
+ (
+ wxT("EDIT"),
+ wxTextBuffer::Translate(text).wx_str(),
+ WS_CHILD | WS_VSCROLL | WS_VISIBLE |
+ ES_MULTILINE | ES_READONLY | ES_AUTOVSCROLL,
+ rc.left, rc.top,
+ rc.right - rc.left, rc.bottom - rc.top,
+ GetHwnd(),
+ NULL,
+ wxGetInstance(),
+ NULL
+ );
+
+ if ( !hwndEdit )
+ {
+ wxLogDebug("Creation of replacement edit control failed in message box");
+ return;
+ }
+
+ // copy the font from the original control
+ LRESULT hfont = ::SendMessage(hwndStatic, WM_GETFONT, 0, 0);
+ ::SendMessage(hwndEdit, WM_SETFONT, hfont, 0);
+
+ // and get rid of it
+ ::DestroyWindow(hwndStatic);
+
+
+ // shrink and centre the message box vertically and widen it box to account
+ // for the extra scrollbar
+ RECT rcBox = wxGetWindowRect(GetHwnd());
+ const int hMsgBox = rcBox.bottom - rcBox.top - dh;
+ rcBox.top = (rectDisplay.height - hMsgBox)/2;
+ rcBox.bottom = rcBox.top + hMsgBox + (rectDisplay.height - hMsgBox)%2;
+ rcBox.left -= dw/2;
+ rcBox.right += dw - dw/2;
+ SetWindowRect(GetHwnd(), rcBox);
+
+ // and adjust all the buttons positions
+ for ( unsigned n = 0; n < WXSIZEOF(ms_buttons); n++ )
+ {
+ const HWND hwndBtn = ::GetDlgItem(GetHwnd(), ms_buttons[n].id);
+ if ( !hwndBtn )
+ continue; // it's ok, not all buttons are always present
+
+ RECT rc = wxGetWindowRect(hwndBtn);
+ rc.top -= dh;
+ rc.bottom -= dh;
+ rc.left += dw/2;
+ rc.right += dw/2;
+ MoveWindowToScreenRect(hwndBtn, rc);
+ }
+}
+
+void wxMessageDialog::AdjustButtonLabels()