1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/msgdlg.cpp
3 // Purpose: wxMessageDialog
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 // there is no hook support under CE so we can't use the code for message box
24 #define wxUSE_MSGBOX_HOOK 1
26 #define wxUSE_MSGBOX_HOOK 0
30 #include "wx/msgdlg.h"
34 #include "wx/msw/private.h"
36 #include "wx/hashmap.h"
40 #include "wx/ptr_scpd.h"
41 #include "wx/dynlib.h"
42 #include "wx/msw/private/button.h"
43 #include "wx/msw/private/metrics.h"
44 #include "wx/msw/private/msgdlg.h"
47 #include "wx/fontutil.h"
48 #include "wx/textbuf.h"
49 #include "wx/display.h"
54 #include "wx/msw/wince/missing.h"
57 using namespace wxMSWMessageDialog
;
59 IMPLEMENT_CLASS(wxMessageDialog
, wxDialog
)
63 // there can potentially be one message box per thread so we use a hash map
64 // with thread ids as keys and (currently shown) message boxes as values
66 // TODO: replace this with wxTLS once it's available
67 WX_DECLARE_HASH_MAP(unsigned long, wxMessageDialog
*,
68 wxIntegerHash
, wxIntegerEqual
,
71 // the order in this array is the one in which buttons appear in the
73 const wxMessageDialog::ButtonAccessors
wxMessageDialog::ms_buttons
[] =
75 { IDYES
, &wxMessageDialog::GetYesLabel
},
76 { IDNO
, &wxMessageDialog::GetNoLabel
},
77 { IDOK
, &wxMessageDialog::GetOKLabel
},
78 { IDCANCEL
, &wxMessageDialog::GetCancelLabel
},
84 wxMessageDialogMap
& HookMap()
86 static wxMessageDialogMap s_Map
;
92 All this code is used for adjusting the message box layout when we mess
93 with its contents. It's rather complicated because we try hard to avoid
94 assuming much about the standard layout details and so, instead of just
95 laying out everything ourselves (which would have been so much simpler!)
96 we try to only modify the existing controls positions by offsetting them
97 from their default ones in the hope that this will continue to work with
98 the future Windows versions.
101 // convert the given RECT from screen to client coordinates in place
102 void ScreenRectToClient(HWND hwnd
, RECT
& rc
)
104 // map from desktop (i.e. screen) coordinates to ones of this window
106 // notice that a RECT is laid out as 2 consecutive POINTs so the cast is
108 ::MapWindowPoints(HWND_DESKTOP
, hwnd
, reinterpret_cast<POINT
*>(&rc
), 2);
111 // set window position to the given rect
112 inline void SetWindowRect(HWND hwnd
, const RECT
& rc
)
116 rc
.right
- rc
.left
, rc
.bottom
- rc
.top
,
120 // set window position expressed in screen coordinates, whether the window is
121 // child or top level
122 void MoveWindowToScreenRect(HWND hwnd
, RECT rc
)
124 ScreenRectToClient(::GetParent(hwnd
), rc
);
126 SetWindowRect(hwnd
, rc
);
129 // helper of AdjustButtonLabels(): move the given window by dx
131 // works for both child and top level windows
132 void OffsetWindow(HWND hwnd
, int dx
)
134 RECT rc
= wxGetWindowRect(hwnd
);
139 MoveWindowToScreenRect(hwnd
, rc
);
142 } // anonymous namespace
146 wxMessageDialog::HookFunction(int code
, WXWPARAM wParam
, WXLPARAM lParam
)
148 // Find the thread-local instance of wxMessageDialog
149 const DWORD tid
= ::GetCurrentThreadId();
150 wxMessageDialogMap::iterator node
= HookMap().find(tid
);
151 wxCHECK_MSG( node
!= HookMap().end(), false,
152 wxT("bogus thread id in wxMessageDialog::Hook") );
154 wxMessageDialog
* const wnd
= node
->second
;
156 const HHOOK hhook
= (HHOOK
)wnd
->m_hook
;
157 const LRESULT rc
= ::CallNextHookEx(hhook
, code
, wParam
, lParam
);
159 if ( code
== HCBT_ACTIVATE
)
161 // we won't need this hook any longer
162 ::UnhookWindowsHookEx(hhook
);
164 HookMap().erase(tid
);
166 wnd
->SetHWND((HWND
)wParam
);
168 // replace the static text with an edit control if the message box is
169 // too big to fit the display
170 wnd
->ReplaceStaticWithEdit();
172 // update the labels if necessary: we need to do it before centering
173 // the dialog as this can change its size
174 if ( wnd
->HasCustomLabels() )
175 wnd
->AdjustButtonLabels();
177 // centre the message box on its parent if requested
178 if ( wnd
->GetMessageDialogStyle() & wxCENTER
)
179 wnd
->Center(); // center on parent
180 //else: default behaviour, center on screen
182 // there seems to be no reason to leave it set
189 void wxMessageDialog::ReplaceStaticWithEdit()
191 // check if the message box fits the display
192 int nDisplay
= wxDisplay::GetFromWindow(this);
193 if ( nDisplay
== wxNOT_FOUND
)
195 const wxRect rectDisplay
= wxDisplay(nDisplay
).GetClientArea();
197 if ( rectDisplay
.Contains(GetRect()) )
204 // find the static control to replace: normally there are two of them, the
205 // icon and the text itself so search for all of them and ignore the icon
207 HWND hwndStatic
= ::FindWindowEx(GetHwnd(), NULL
, wxT("STATIC"), NULL
);
208 if ( ::GetWindowLong(hwndStatic
, GWL_STYLE
) & SS_ICON
)
209 hwndStatic
= ::FindWindowEx(GetHwnd(), hwndStatic
, wxT("STATIC"), NULL
);
213 wxLogDebug("Failed to find the static text control in message box.");
217 // set the right font for GetCharHeight() call below
218 wxWindowBase::SetFont(GetMessageFont());
220 // put the new edit control at the same place
221 RECT rc
= wxGetWindowRect(hwndStatic
);
222 ScreenRectToClient(GetHwnd(), rc
);
224 // but make it less tall so that the message box fits on the screen: we try
225 // to make the message box take no more than 7/8 of the screen to leave
226 // some space above and below it
227 const int hText
= (7*rectDisplay
.height
)/8 -
229 2*::GetSystemMetrics(SM_CYFIXEDFRAME
) +
230 ::GetSystemMetrics(SM_CYCAPTION
) +
231 5*GetCharHeight() // buttons + margins
233 const int dh
= (rc
.bottom
- rc
.top
) - hText
; // vertical space we save
236 // and it also must be wider as it needs a vertical scrollbar (in order
237 // to preserve the word wrap, otherwise the number of lines would change
238 // and we want the control to look as similar as possible to the original)
240 // NB: you would have thought that 2*SM_CXEDGE would be enough but it
241 // isn't, somehow, and the text control breaks lines differently from
242 // the static one so fudge by adding some extra space
243 const int dw
= ::GetSystemMetrics(SM_CXVSCROLL
) +
244 4*::GetSystemMetrics(SM_CXEDGE
);
248 // chop of the trailing new line(s) from the message box text, they are
249 // ignored by the static control but result in extra lines and hence extra
250 // scrollbar position in the edit one
251 wxString
text(wxGetWindowText(hwndStatic
));
252 for ( wxString::reverse_iterator i
= text
.rbegin(); i
!= text
.rend(); ++i
)
256 // found last non-newline char, remove anything after it if
257 // necessary and stop in any case
258 if ( i
!= text
.rbegin() )
259 text
.erase(i
.base() + 1, text
.end());
264 // do create the new control
265 HWND hwndEdit
= ::CreateWindow
268 wxTextBuffer::Translate(text
).wx_str(),
269 WS_CHILD
| WS_VSCROLL
| WS_VISIBLE
|
270 ES_MULTILINE
| ES_READONLY
| ES_AUTOVSCROLL
,
272 rc
.right
- rc
.left
, rc
.bottom
- rc
.top
,
281 wxLogDebug("Creation of replacement edit control failed in message box");
285 // copy the font from the original control
286 LRESULT hfont
= ::SendMessage(hwndStatic
, WM_GETFONT
, 0, 0);
287 ::SendMessage(hwndEdit
, WM_SETFONT
, hfont
, 0);
290 ::DestroyWindow(hwndStatic
);
293 // shrink and centre the message box vertically and widen it box to account
294 // for the extra scrollbar
295 RECT rcBox
= wxGetWindowRect(GetHwnd());
296 const int hMsgBox
= rcBox
.bottom
- rcBox
.top
- dh
;
297 rcBox
.top
= (rectDisplay
.height
- hMsgBox
)/2;
298 rcBox
.bottom
= rcBox
.top
+ hMsgBox
+ (rectDisplay
.height
- hMsgBox
)%2
;
300 rcBox
.right
+= dw
- dw
/2;
301 SetWindowRect(GetHwnd(), rcBox
);
303 // and adjust all the buttons positions
304 for ( unsigned n
= 0; n
< WXSIZEOF(ms_buttons
); n
++ )
306 const HWND hwndBtn
= ::GetDlgItem(GetHwnd(), ms_buttons
[n
].id
);
308 continue; // it's ok, not all buttons are always present
310 RECT rc
= wxGetWindowRect(hwndBtn
);
315 MoveWindowToScreenRect(hwndBtn
, rc
);
319 void wxMessageDialog::AdjustButtonLabels()
321 // changing the button labels is the easy part but we also need to ensure
322 // that the buttons are big enough for the label strings and increase their
323 // size (and maybe the size of the message box itself) if they are not
325 // TODO-RTL: check whether this works correctly in RTL
327 // we want to use this font in GetTextExtent() calls below but we don't
328 // want to send WM_SETFONT to the message box, who knows how is it going to
329 // react to it (right now it doesn't seem to do anything but what if this
331 wxWindowBase::SetFont(GetMessageFont());
333 // first iteration: find the widest button and update the buttons labels
334 int wBtnOld
= 0, // current buttons width
335 wBtnNew
= 0; // required new buttons width
336 RECT rcBtn
; // stores the button height and y positions
337 unsigned numButtons
= 0; // total number of buttons in the message box
339 for ( n
= 0; n
< WXSIZEOF(ms_buttons
); n
++ )
341 const HWND hwndBtn
= ::GetDlgItem(GetHwnd(), ms_buttons
[n
].id
);
343 continue; // it's ok, not all buttons are always present
347 const wxString label
= (this->*ms_buttons
[n
].getter
)();
348 const wxSize sizeLabel
= wxWindowBase::GetTextExtent(label
);
350 // check if the button is big enough for this label
351 const RECT rc
= wxGetWindowRect(hwndBtn
);
354 // initialize wBtnOld using the first button width, all the other
355 // ones should have the same one
356 wBtnOld
= rc
.right
- rc
.left
;
358 rcBtn
= rc
; // remember for use below when we reposition the buttons
362 wxASSERT_MSG( wBtnOld
== rc
.right
- rc
.left
,
363 "all buttons are supposed to be of same width" );
366 const int widthNeeded
= wxMSWButton::GetFittingSize(this, sizeLabel
).x
;
367 if ( widthNeeded
> wBtnNew
)
368 wBtnNew
= widthNeeded
;
370 ::SetWindowText(hwndBtn
, label
.wx_str());
373 if ( wBtnNew
<= wBtnOld
)
375 // all buttons fit, nothing else to do
379 // resize the message box to be wider if needed
380 const int wBoxOld
= wxGetClientRect(GetHwnd()).right
;
382 const int CHAR_WIDTH
= GetCharWidth();
383 const int MARGIN_OUTER
= 2*CHAR_WIDTH
; // margin between box and buttons
384 const int MARGIN_INNER
= CHAR_WIDTH
; // margin between buttons
386 RECT rcBox
= wxGetWindowRect(GetHwnd());
388 const int wAllButtons
= numButtons
*(wBtnNew
+ MARGIN_INNER
) - MARGIN_INNER
;
389 int wBoxNew
= 2*MARGIN_OUTER
+ wAllButtons
;
390 if ( wBoxNew
> wBoxOld
)
392 const int dw
= wBoxNew
- wBoxOld
;
394 rcBox
.right
+= dw
- dw
/2;
396 SetWindowRect(GetHwnd(), rcBox
);
398 // surprisingly, we don't need to resize the static text control, it
399 // seems to adjust itself to the new size, at least under Windows 2003
400 // (TODO: test if this happens on older Windows versions)
402 else // the current width is big enough
408 // finally position all buttons
410 // notice that we have to take into account the difference between window
412 rcBtn
.left
= (rcBox
.left
+ rcBox
.right
- wxGetClientRect(GetHwnd()).right
+
413 wBoxNew
- wAllButtons
) / 2;
414 rcBtn
.right
= rcBtn
.left
+ wBtnNew
;
416 for ( n
= 0; n
< WXSIZEOF(ms_buttons
); n
++ )
418 const HWND hwndBtn
= ::GetDlgItem(GetHwnd(), ms_buttons
[n
].id
);
422 MoveWindowToScreenRect(hwndBtn
, rcBtn
);
424 rcBtn
.left
+= wBtnNew
+ MARGIN_INNER
;
425 rcBtn
.right
+= wBtnNew
+ MARGIN_INNER
;
429 #endif // wxUSE_MSGBOX_HOOK
432 wxFont
wxMessageDialog::GetMessageFont()
434 const NONCLIENTMETRICS
& ncm
= wxMSWImpl::GetNonClientMetrics();
435 return wxNativeFontInfo(ncm
.lfMessageFont
);
438 int wxMessageDialog::ShowMessageBox()
440 if ( !wxTheApp
->GetTopWindow() )
442 // when the message box is shown from wxApp::OnInit() (i.e. before the
443 // message loop is entered), this must be done or the next message box
444 // will never be shown - just try putting 2 calls to wxMessageBox() in
445 // OnInit() to see it
446 while ( wxTheApp
->Pending() )
447 wxTheApp
->Dispatch();
450 // use the top level window as parent if none specified
451 m_parent
= GetParentForModalDialog();
452 HWND hWnd
= m_parent
? GetHwndOf(m_parent
) : NULL
;
455 // native message box always uses the current user locale but the program
456 // may be using a different one and in this case we need to manually
457 // translate the default button labels (if they're non default we have no
458 // way to translate them and so we must assume they were already
459 // translated) to avoid mismatch between the language of the message box
460 // text and its buttons
461 wxLocale
* const loc
= wxGetLocale();
462 if ( loc
&& loc
->GetLanguage() != wxLocale::GetSystemLanguage() )
464 if ( m_dialogStyle
& wxYES_NO
&&
465 (GetCustomYesLabel().empty() && GetCustomNoLabel().empty()) )
468 // use the strings with mnemonics here as the native message box
470 SetYesNoLabels(_("&Yes"), _("&No"));
473 // we may or not have the Ok/Cancel buttons but either we do have them
474 // or we already made the labels custom because we called
475 // SetYesNoLabels() above so doing this does no harm -- and is
476 // necessary in wxYES_NO | wxCANCEL case
478 // note that we don't use mnemonics here for consistency with the
479 // native message box (which probably doesn't use them because
480 // Enter/Esc keys can be already used to dismiss the message box
482 if ( GetCustomOKLabel().empty() && GetCustomCancelLabel().empty() )
483 SetOKCancelLabels(_("OK"), _("Cancel"));
487 // translate wx style in MSW
488 unsigned int msStyle
;
489 const long wxStyle
= GetMessageDialogStyle();
490 if ( wxStyle
& wxYES_NO
)
492 #if !(defined(__SMARTPHONE__) && defined(__WXWINCE__))
493 if (wxStyle
& wxCANCEL
)
494 msStyle
= MB_YESNOCANCEL
;
496 #endif // !(__SMARTPHONE__ && __WXWINCE__)
499 if ( wxStyle
& wxNO_DEFAULT
)
500 msStyle
|= MB_DEFBUTTON2
;
501 else if ( wxStyle
& wxCANCEL_DEFAULT
)
502 msStyle
|= MB_DEFBUTTON3
;
504 else // without Yes/No we're going to have an OK button
506 if ( wxStyle
& wxCANCEL
)
508 msStyle
= MB_OKCANCEL
;
510 if ( wxStyle
& wxCANCEL_DEFAULT
)
511 msStyle
|= MB_DEFBUTTON2
;
519 if ( wxStyle
& wxHELP
)
524 // set the icon style
525 switch ( GetEffectiveIcon() )
528 msStyle
|= MB_ICONHAND
;
532 msStyle
|= MB_ICONEXCLAMATION
;
535 case wxICON_QUESTION
:
536 msStyle
|= MB_ICONQUESTION
;
539 case wxICON_INFORMATION
:
540 msStyle
|= MB_ICONINFORMATION
;
544 if ( wxStyle
& wxSTAY_ON_TOP
)
545 msStyle
|= MB_TOPMOST
;
548 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
549 msStyle
|= MB_RTLREADING
| MB_RIGHT
;
553 msStyle
|= MB_APPLMODAL
;
555 msStyle
|= MB_TASKMODAL
;
557 // per MSDN documentation for MessageBox() we can prefix the message with 2
558 // right-to-left mark characters to tell the function to use RTL layout
559 // (unfortunately this only works in Unicode builds)
560 wxString message
= GetFullMessage();
562 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
564 // NB: not all compilers support \u escapes
565 static const wchar_t wchRLM
= 0x200f;
566 message
.Prepend(wxString(wchRLM
, 2));
568 #endif // wxUSE_UNICODE
570 #if wxUSE_MSGBOX_HOOK
571 // install the hook in any case as we don't know in advance if the message
572 // box is not going to be too big (requiring the replacement of the static
573 // control with an edit one)
574 const DWORD tid
= ::GetCurrentThreadId();
575 m_hook
= ::SetWindowsHookEx(WH_CBT
,
576 &wxMessageDialog::HookFunction
, NULL
, tid
);
577 HookMap()[tid
] = this;
578 #endif // wxUSE_MSGBOX_HOOK
580 // do show the dialog
581 int msAns
= MessageBox(hWnd
, message
.wx_str(), m_caption
.wx_str(), msStyle
);
583 return MSWTranslateReturnCode(msAns
);
586 int wxMessageDialog::ShowModal()
588 #ifdef wxHAS_MSW_TASKDIALOG
589 if ( HasNativeTaskDialog() )
591 TaskDialogIndirect_t taskDialogIndirect
= GetTaskDialogIndirectFunc();
592 wxCHECK_MSG( taskDialogIndirect
, wxID_CANCEL
, wxS("no task dialog?") );
594 WinStruct
<TASKDIALOGCONFIG
> tdc
;
595 wxMSWTaskDialogConfig
wxTdc( *this );
596 wxTdc
.MSWCommonTaskDialogInit( tdc
);
599 HRESULT hr
= taskDialogIndirect( &tdc
, &msAns
, NULL
, NULL
);
602 wxLogApiError( "TaskDialogIndirect", hr
);
606 // In case only an "OK" button was specified we actually created a
607 // "Cancel" button (see comment in MSWCommonTaskDialogInit). This
608 // results in msAns being IDCANCEL while we want IDOK (just like
609 // how the native MessageBox function does with only an "OK" button).
610 if ( (msAns
== IDCANCEL
)
611 && !(GetMessageDialogStyle() & (wxYES_NO
|wxCANCEL
)) )
616 return MSWTranslateReturnCode( msAns
);
618 #endif // wxHAS_MSW_TASKDIALOG
620 return ShowMessageBox();
623 void wxMessageDialog::DoCentre(int dir
)
625 #ifdef wxHAS_MSW_TASKDIALOG
626 // Task dialog is always centered on its parent window and trying to center
627 // it manually doesn't work because its HWND is not created yet so don't
628 // even try as this would only result in (debug) error messages.
629 if ( HasNativeTaskDialog() )
631 #endif // wxHAS_MSW_TASKDIALOG
633 wxMessageDialogBase::DoCentre(dir
);
636 // ----------------------------------------------------------------------------
637 // Helpers of the wxMSWMessageDialog namespace
638 // ----------------------------------------------------------------------------
640 #ifdef wxHAS_MSW_TASKDIALOG
642 wxMSWTaskDialogConfig::wxMSWTaskDialogConfig(const wxMessageDialogBase
& dlg
)
643 : buttons(new TASKDIALOG_BUTTON
[MAX_BUTTONS
])
645 parent
= dlg
.GetParentForModalDialog();
646 caption
= dlg
.GetCaption();
647 message
= dlg
.GetMessage();
648 extendedMessage
= dlg
.GetExtendedMessage();
650 // Before wxMessageDialog added support for extended message it was common
651 // practice to have long multiline texts in the message box with the first
652 // line playing the role of the main message and the rest of the extended
653 // one. Try to detect such usage automatically here by synthesizing the
654 // extended message on our own if it wasn't given.
655 if ( extendedMessage
.empty() )
657 // Check if there is a blank separating line after the first line (this
658 // is not the same as searching for "\n\n" as we want the automatically
659 // recognized main message be single line to avoid embarrassing false
661 const size_t posNL
= message
.find('\n');
662 if ( posNL
!= wxString::npos
&&
663 posNL
< message
.length() - 1 &&
664 message
[posNL
+ 1 ] == '\n' )
666 extendedMessage
.assign(message
, posNL
+ 2, wxString::npos
);
667 message
.erase(posNL
);
671 iconId
= dlg
.GetEffectiveIcon();
672 style
= dlg
.GetMessageDialogStyle();
673 useCustomLabels
= dlg
.HasCustomLabels();
674 btnYesLabel
= dlg
.GetYesLabel();
675 btnNoLabel
= dlg
.GetNoLabel();
676 btnOKLabel
= dlg
.GetOKLabel();
677 btnCancelLabel
= dlg
.GetCancelLabel();
678 btnHelpLabel
= dlg
.GetHelpLabel();
681 void wxMSWTaskDialogConfig::MSWCommonTaskDialogInit(TASKDIALOGCONFIG
&tdc
)
683 tdc
.dwFlags
= TDF_EXPAND_FOOTER_AREA
| TDF_POSITION_RELATIVE_TO_WINDOW
;
684 tdc
.hInstance
= wxGetInstance();
685 tdc
.pszWindowTitle
= caption
.wx_str();
687 // use the top level window as parent if none specified
688 tdc
.hwndParent
= parent
? GetHwndOf(parent
) : NULL
;
690 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
691 tdc
.dwFlags
|= TDF_RTL_LAYOUT
;
693 // If we have both the main and extended messages, just use them as
694 // intended. However if only one message is given we normally use it as the
695 // content and not as the main instruction because the latter is supposed
696 // to stand out compared to the former and doesn't look good if there is
697 // nothing for it to contrast with. Finally, notice that the extended
698 // message we use here might be automatically extracted from the main
699 // message in our ctor, see comment there.
700 if ( !extendedMessage
.empty() )
702 tdc
.pszMainInstruction
= message
.wx_str();
703 tdc
.pszContent
= extendedMessage
.wx_str();
707 tdc
.pszContent
= message
.wx_str();
710 // set an icon to be used, if possible
714 tdc
.pszMainIcon
= TD_ERROR_ICON
;
718 tdc
.pszMainIcon
= TD_WARNING_ICON
;
721 case wxICON_INFORMATION
:
722 tdc
.pszMainIcon
= TD_INFORMATION_ICON
;
726 // custom label button array that can hold all buttons in use
727 tdc
.pButtons
= buttons
.get();
729 if ( style
& wxYES_NO
)
731 AddTaskDialogButton(tdc
, IDYES
, TDCBF_YES_BUTTON
, btnYesLabel
);
732 AddTaskDialogButton(tdc
, IDNO
, TDCBF_NO_BUTTON
, btnNoLabel
);
734 if (style
& wxCANCEL
)
735 AddTaskDialogButton(tdc
, IDCANCEL
,
736 TDCBF_CANCEL_BUTTON
, btnCancelLabel
);
738 if ( style
& wxNO_DEFAULT
)
739 tdc
.nDefaultButton
= IDNO
;
740 else if ( style
& wxCANCEL_DEFAULT
)
741 tdc
.nDefaultButton
= IDCANCEL
;
743 else // without Yes/No we're going to have an OK button
745 if ( style
& wxCANCEL
)
747 AddTaskDialogButton(tdc
, IDOK
, TDCBF_OK_BUTTON
, btnOKLabel
);
748 AddTaskDialogButton(tdc
, IDCANCEL
,
749 TDCBF_CANCEL_BUTTON
, btnCancelLabel
);
751 if ( style
& wxCANCEL_DEFAULT
)
752 tdc
.nDefaultButton
= IDCANCEL
;
756 // We actually create a "Cancel" button instead because we want to
757 // allow closing the dialog box with Escape (and also Alt-F4 or
758 // clicking the close button in the title bar) which wouldn't work
759 // without a Cancel button.
760 if ( !useCustomLabels
)
762 useCustomLabels
= true;
763 btnOKLabel
= _("OK");
766 AddTaskDialogButton(tdc
, IDCANCEL
, TDCBF_CANCEL_BUTTON
, btnOKLabel
);
770 if ( style
& wxHELP
)
772 // There is no support for "Help" button in the task dialog, it can
773 // only show "Retry" or "Close" ones.
774 useCustomLabels
= true;
776 AddTaskDialogButton(tdc
, IDHELP
, 0 /* not used */, btnHelpLabel
);
780 void wxMSWTaskDialogConfig::AddTaskDialogButton(TASKDIALOGCONFIG
&tdc
,
783 const wxString
& customLabel
)
785 if ( useCustomLabels
)
787 // use custom buttons to implement custom labels
788 TASKDIALOG_BUTTON
&tdBtn
= buttons
[tdc
.cButtons
];
790 tdBtn
.nButtonID
= btnCustomId
;
791 tdBtn
.pszButtonText
= customLabel
.wx_str();
794 // We should never have more than 4 buttons currently as this is the
795 // maximal number of buttons supported by the message dialog.
796 wxASSERT_MSG( tdc
.cButtons
<= MAX_BUTTONS
, wxT("Too many buttons") );
800 tdc
.dwCommonButtons
|= btnCommonId
;
804 // Task dialog can be used from different threads (and wxProgressDialog always
805 // uses it from another thread in fact) so protect access to the static
806 // variable below with a critical section.
807 wxCRIT_SECT_DECLARE(gs_csTaskDialogIndirect
);
809 TaskDialogIndirect_t
wxMSWMessageDialog::GetTaskDialogIndirectFunc()
811 // Initialize the function pointer to an invalid value different from NULL
812 // to avoid reloading comctl32.dll and trying to resolve it every time
813 // we're called if task dialog is not available (notice that this may
814 // happen even under Vista+ if we don't use comctl32.dll v6).
815 static const TaskDialogIndirect_t
816 INVALID_TASKDIALOG_FUNC
= reinterpret_cast<TaskDialogIndirect_t
>(-1);
817 static TaskDialogIndirect_t s_TaskDialogIndirect
= INVALID_TASKDIALOG_FUNC
;
819 wxCRIT_SECT_LOCKER(lock
, gs_csTaskDialogIndirect
);
821 if ( s_TaskDialogIndirect
== INVALID_TASKDIALOG_FUNC
)
823 wxLoadedDLL
dllComCtl32("comctl32.dll");
824 wxDL_INIT_FUNC(s_
, TaskDialogIndirect
, dllComCtl32
);
827 return s_TaskDialogIndirect
;
830 #endif // wxHAS_MSW_TASKDIALOG
832 bool wxMSWMessageDialog::HasNativeTaskDialog()
834 #ifdef wxHAS_MSW_TASKDIALOG
835 if ( wxGetWinVersion() >= wxWinVersion_6
)
837 if ( wxMSWMessageDialog::GetTaskDialogIndirectFunc() )
840 #endif // wxHAS_MSW_TASKDIALOG
845 int wxMSWMessageDialog::MSWTranslateReturnCode(int msAns
)
851 wxFAIL_MSG(wxT("unexpected return code"));
874 #endif // wxUSE_MSGDLG