X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b6bcaaca0ffa311a5a8d635255dbf8508b99aa3b..107defe36a68091303be9a11c8abb83432acee8c:/src/msw/msgdlg.cpp diff --git a/src/msw/msgdlg.cpp b/src/msw/msgdlg.cpp index 9d91acab81..108beb7d1b 100644 --- a/src/msw/msgdlg.cpp +++ b/src/msw/msgdlg.cpp @@ -30,6 +30,7 @@ #ifndef WX_PRECOMP #include "wx/app.h" + #include "wx/intl.h" #include "wx/utils.h" #include "wx/dialog.h" #if wxUSE_MSGBOX_HOOK @@ -43,6 +44,8 @@ #if wxUSE_MSGBOX_HOOK #include "wx/fontutil.h" + #include "wx/textbuf.h" + #include "wx/display.h" #endif // For MB_TASKMODAL @@ -62,6 +65,16 @@ 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 { @@ -72,6 +85,57 @@ wxMessageDialogMap& HookMap() 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(&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 */ @@ -98,6 +162,10 @@ wxMessageDialog::HookFunction(int code, WXWPARAM wParam, WXLPARAM lParam) 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() ) @@ -115,123 +183,251 @@ wxMessageDialog::HookFunction(int code, WXWPARAM wParam, WXLPARAM lParam) return rc; } -namespace +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(); -// helper of AdjustButtonLabels(): set window position expressed in screen -// coordinates, whether the window is child or top level -void MoveWindowToScreenRect(HWND hwnd, RECT rc) -{ - if ( const HWND hwndParent = ::GetAncestor(hwnd, GA_PARENT) ) + if ( rectDisplay.Contains(GetRect()) ) { - // map to parent window coordinates (notice that a RECT is laid out as - // 2 consecutive POINTs) - ::MapWindowPoints(HWND_DESKTOP, hwndParent, - reinterpret_cast(&rc), 2); + // nothing to do + return; } - ::MoveWindow(hwnd, - rc.left, rc.top, - rc.right - rc.left, rc.bottom - rc.top, - FALSE); -} -// 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); + // 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); - rc.left += dx; - rc.right += dx; + if ( !hwndStatic ) + { + wxLogDebug("Failed to find the static text control in message box."); + return; + } - MoveWindowToScreenRect(hwnd, rc); -} + // 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::iterator i = text.end() - 1; i != text.begin(); --i ) + { + if ( *i != '\n' ) + { + text.erase(i + 1, text.end()); + break; + } + } -} // anonymous namespace + // 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, + wxhInstance, + NULL + ); + + if ( !hwndEdit ) + { + wxLogDebug("Creation of replacement edit control failed in message box"); + return; + } -void wxMessageDialog::AdjustButtonLabels() -{ - // changing the button labels is the easy part but we also need to ensure - // that the buttons are big enough for the label strings and increase their - // size (and hence the size of the message box itself) if they are not + // copy the font from the original control + LRESULT hfont = ::SendMessage(hwndStatic, WM_GETFONT, 0, 0); + ::SendMessage(hwndEdit, WM_SETFONT, hfont, 0); - // TODO-RTL: check whether this works correctly in RTL + // and get rid of it + ::DestroyWindow(hwndStatic); - // the order in this array is the one in which buttons appear in the - // message box - const static struct ButtonAccessors + + // 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++ ) { - int id; - wxString (wxMessageDialog::*getter)() const; + 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); } - buttons[] = - { - { IDYES, &wxMessageDialog::GetYesLabel }, - { IDNO, &wxMessageDialog::GetNoLabel }, - { IDOK, &wxMessageDialog::GetOKLabel }, - { IDCANCEL, &wxMessageDialog::GetCancelLabel }, - }; +} - // this contains the amount by which we increased the message box width - int dx = 0; +void wxMessageDialog::AdjustButtonLabels() +{ + // changing the button labels is the easy part but we also need to ensure + // that the buttons are big enough for the label strings and increase their + // size (and maybe the size of the message box itself) if they are not - const NONCLIENTMETRICS& ncm = wxMSWImpl::GetNonClientMetrics(); - const wxFont fontMsgBox(wxNativeFontInfo(ncm.lfMessageFont)); + // TODO-RTL: check whether this works correctly in RTL // we want to use this font in GetTextExtent() calls below but we don't // want to send WM_SETFONT to the message box, who knows how is it going to // react to it (right now it doesn't seem to do anything but what if this // changes) - wxWindowBase::SetFont(fontMsgBox); - - for ( unsigned n = 0; n < WXSIZEOF(buttons); n++ ) + wxWindowBase::SetFont(GetMessageFont()); + + // first iteration: find the widest button and update the buttons labels + int wBtnOld = 0, // current buttons width + wBtnNew = 0; // required new buttons width + RECT rcBtn; // stores the button height and y positions + unsigned numButtons = 0; // total number of buttons in the message box + unsigned n; + for ( n = 0; n < WXSIZEOF(ms_buttons); n++ ) { - const HWND hwndBtn = ::GetDlgItem(GetHwnd(), buttons[n].id); + const HWND hwndBtn = ::GetDlgItem(GetHwnd(), ms_buttons[n].id); if ( !hwndBtn ) continue; // it's ok, not all buttons are always present - const wxString label = (this->*buttons[n].getter)(); + numButtons++; + + const wxString label = (this->*ms_buttons[n].getter)(); const wxSize sizeLabel = wxWindowBase::GetTextExtent(label); // check if the button is big enough for this label - RECT rc = wxGetWindowRect(hwndBtn); - const int widthOld = rc.right - rc.left; - const int widthNew = wxMSWButton::GetFittingSize(this, sizeLabel).x; - const int dw = widthNew - widthOld; - if ( dw > 0 ) + const RECT rc = wxGetWindowRect(hwndBtn); + if ( !wBtnOld ) + { + // initialize wBtnOld using the first button width, all the other + // ones should have the same one + wBtnOld = rc.right - rc.left; + + rcBtn = rc; // remember for use below when we reposition the buttons + } + else { - // we need to resize the button - rc.right += dw; - MoveWindowToScreenRect(hwndBtn, rc); - - // and also move all the other buttons - for ( unsigned m = n + 1; m < WXSIZEOF(buttons); m++ ) - { - const HWND hwndBtnNext = ::GetDlgItem(GetHwnd(), buttons[m].id); - if ( hwndBtnNext ) - OffsetWindow(hwndBtnNext, dw); - } - - dx += dw; + wxASSERT_MSG( wBtnOld == rc.right - rc.left, + "all buttons are supposed to be of same width" ); } + const int widthNeeded = wxMSWButton::GetFittingSize(this, sizeLabel).x; + if ( widthNeeded > wBtnNew ) + wBtnNew = widthNeeded; + ::SetWindowText(hwndBtn, label.wx_str()); } + if ( wBtnNew <= wBtnOld ) + { + // all buttons fit, nothing else to do + return; + } + + // resize the message box to be wider if needed + const int wBoxOld = wxGetClientRect(GetHwnd()).right; + + const int CHAR_WIDTH = GetCharWidth(); + const int MARGIN_OUTER = 2*CHAR_WIDTH; // margin between box and buttons + const int MARGIN_INNER = CHAR_WIDTH; // margin between buttons + + RECT rcBox = wxGetWindowRect(GetHwnd()); + + const int wAllButtons = numButtons*(wBtnNew + MARGIN_INNER) - MARGIN_INNER; + int wBoxNew = 2*MARGIN_OUTER + wAllButtons; + if ( wBoxNew > wBoxOld ) + { + const int dw = wBoxNew - wBoxOld; + rcBox.left -= dw/2; + rcBox.right += dw - dw/2; - // resize the message box itself if needed - if ( dx ) - OffsetWindow(GetHwnd(), dx); + SetWindowRect(GetHwnd(), rcBox); - // surprisingly, we don't need to resize the static text control, it seems - // to adjust itself to the new size, at least under Windows 2003 - // (TODO: test if this happens on older Windows versions) + // surprisingly, we don't need to resize the static text control, it + // seems to adjust itself to the new size, at least under Windows 2003 + // (TODO: test if this happens on older Windows versions) + } + else // the current width is big enough + { + wBoxNew = wBoxOld; + } + + + // finally position all buttons + + // notice that we have to take into account the difference between window + // and client width + rcBtn.left = (rcBox.left + rcBox.right - wxGetClientRect(GetHwnd()).right + + wBoxNew - wAllButtons) / 2; + rcBtn.right = rcBtn.left + wBtnNew; + + for ( n = 0; n < WXSIZEOF(ms_buttons); n++ ) + { + const HWND hwndBtn = ::GetDlgItem(GetHwnd(), ms_buttons[n].id); + if ( !hwndBtn ) + continue; + + MoveWindowToScreenRect(hwndBtn, rcBtn); + + rcBtn.left += wBtnNew + MARGIN_INNER; + rcBtn.right += wBtnNew + MARGIN_INNER; + } } #endif // wxUSE_MSGBOX_HOOK +/* static */ +wxFont wxMessageDialog::GetMessageFont() +{ + const NONCLIENTMETRICS& ncm = wxMSWImpl::GetNonClientMetrics(); + return wxNativeFontInfo(ncm.lfMessageFont); +} int wxMessageDialog::ShowModal() { @@ -247,13 +443,41 @@ int wxMessageDialog::ShowModal() // use the top level window as parent if none specified if ( !m_parent ) - m_parent = FindSuitableParent(); + m_parent = GetParentForModalDialog(); HWND hWnd = m_parent ? GetHwndOf(m_parent) : NULL; +#if wxUSE_INTL + // native message box always uses the current user locale but the program + // may be using a different one and in this case we need to manually + // translate the button labels to avoid mismatch between the language of + // the message box text and its buttons + wxLocale * const loc = wxGetLocale(); + if ( loc && loc->GetLanguage() != wxLocale::GetSystemLanguage() ) + { + if ( m_dialogStyle & wxYES_NO ) + { + // use the strings with mnemonics here as the native message box + // does + SetYesNoLabels(_("&Yes"), _("&No")); + } + + // we may or not have the Ok/Cancel buttons but either we do have them + // or we already made the labels custom because we called + // SetYesNoLabels() above so doing this does no harm -- and is + // necessary in wxYES_NO | wxCANCEL case + // + // note that we don't use mnemonics here for consistency with the + // native message box (which probably doesn't use them because + // Enter/Esc keys can be already used to dismiss the message box + // using keyboard) + SetOKCancelLabels(_("OK"), _("Cancel")); + } +#endif // wxUSE_INTL + // translate wx style in MSW - unsigned int msStyle = MB_OK; + unsigned int msStyle; const long wxStyle = GetMessageDialogStyle(); - if (wxStyle & wxYES_NO) + if ( wxStyle & wxYES_NO ) { #if !(defined(__SMARTPHONE__) && defined(__WXWINCE__)) if (wxStyle & wxCANCEL) @@ -262,17 +486,26 @@ int wxMessageDialog::ShowModal() #endif // !(__SMARTPHONE__ && __WXWINCE__) msStyle = MB_YESNO; - if (wxStyle & wxNO_DEFAULT) + if ( wxStyle & wxNO_DEFAULT ) msStyle |= MB_DEFBUTTON2; + else if ( wxStyle & wxCANCEL_DEFAULT ) + msStyle |= MB_DEFBUTTON3; } - - if (wxStyle & wxOK) + else // without Yes/No we're going to have an OK button { - if (wxStyle & wxCANCEL) + if ( wxStyle & wxCANCEL ) + { msStyle = MB_OKCANCEL; - else + + if ( wxStyle & wxCANCEL_DEFAULT ) + msStyle |= MB_DEFBUTTON2; + } + else // just "OK" + { msStyle = MB_OK; + } } + if (wxStyle & wxICON_EXCLAMATION) msStyle |= MB_ICONEXCLAMATION; else if (wxStyle & wxICON_HAND) @@ -309,15 +542,13 @@ int wxMessageDialog::ShowModal() #endif // wxUSE_UNICODE #if wxUSE_MSGBOX_HOOK - // install the hook if we need to position the dialog in a non-default way - // or change the labels - if ( (wxStyle & wxCENTER) || HasCustomLabels() ) - { - const DWORD tid = ::GetCurrentThreadId(); - m_hook = ::SetWindowsHookEx(WH_CBT, - &wxMessageDialog::HookFunction, NULL, tid); - HookMap()[tid] = this; - } + // install the hook in any case as we don't know in advance if the message + // box is not going to be too big (requiring the replacement of the static + // control with an edit one) + const DWORD tid = ::GetCurrentThreadId(); + m_hook = ::SetWindowsHookEx(WH_CBT, + &wxMessageDialog::HookFunction, NULL, tid); + HookMap()[tid] = this; #endif // wxUSE_MSGBOX_HOOK // do show the dialog @@ -326,7 +557,7 @@ int wxMessageDialog::ShowModal() switch (msAns) { default: - wxFAIL_MSG(_T("unexpected ::MessageBox() return code")); + wxFAIL_MSG(wxT("unexpected ::MessageBox() return code")); // fall through case IDCANCEL: