lmi
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lmi] Catching exceptions thrown in OnInit()


From: Greg Chicares
Subject: Re: [lmi] Catching exceptions thrown in OnInit()
Date: Wed, 28 Jun 2006 19:00:26 +0000
User-agent: Thunderbird 1.5.0.4 (Windows/20060516)

On 2006-6-28 18:43 UTC, Vadim Zeitlin wrote:
> On Wed, 28 Jun 2006 17:03:17 +0000 Greg Chicares <address@hidden> wrote:
> 
> GC> One change I certainly must make anyway is in my safe-messagebox
> GC> function for msw:
> GC> 
> GC>     HWND handle = 0;
> GC>     wxWindow* top_window = wxTheApp->GetTopWindow();
> GC>     if(top_window)
> GC>         handle = reinterpret_cast<HWND>(top_window->GetHandle());
> GC>     ::MessageBox(handle, message, "Error", MB_OK | MB_ICONSTOP | 
> MB_TASKMODAL);
> GC> 
> GC> which clearly should check whether wxTheApp is null before trying
> GC> to dereference it.
> 
>  Yes, certainly. I think you can safely use NULL as parent HWND here anyhow
> because you use MB_TASKMODAL style which, I believe, ignores this argument.

Let me quote my whole implementation and its inline documentation,
below, to explain why I'm doing this. Perhaps you'll find a flaw in
my reasoning, though I do believe I tested everything carefully
before writing the comment. Or perhaps you'll tell me that wx's own
::wxSafeShowMessage() is now perfectly equivalent to this, so that I
can remove this code and never have to maintain it--though, again,
I do believe I checked that first, probably with wx-2.5.4 . Or maybe
there's some idea here that you'd find useful for wxmsw, in which
case I release this code under the wx license and you can use it
without attribution.

/// Show a message reliably, even before initialization is complete or
/// after destruction has begun.
///
/// The msw implementation of wxSafeShowMessage() uses ::MessageBox()
/// with a null parent, which adds an undesirable extra "task" to the
/// alt-Tab order, yet doesn't disable the application's top window.
///
/// If MB_TASKMODAL is specified, then the extra "task" is still
/// added, but all of the application's top windows are disabled.
/// Unfortunately, MB_TASKMODAL is in effect ignored unless the parent
/// is null.
///
/// If the main top window (the one returned by wxApp::GetTopWindow())
/// is used as the messagebox's parent, then the extra "task" is not
/// added, but only the parent is disabled. Any other top windows the
/// application may have are not disabled.
///
/// The extra "task" seeming to be the worse evil, this implementation
/// specifies a non-null parent wherever possible. MB_TASKMODAL is
/// nevertheless specified as well, though its beneficial effect is
/// realized only if no parent can be found.

void safe_message_alert(char const* message)
{
#if !defined LMI_MSW
    std::fputs(message, stderr);
    std::fputc('\n', stderr);
    // Flush explicitly. C99 7.19.3/7 says only that stderr is
    // "not fully buffered", not that it is 'unbuffered'. See:
    //   http://sourceforge.net/mailarchive/message.php?msg_id=10388832
    //   http://sourceforge.net/mailarchive/message.php?msg_id=10826040
    std::fflush(stderr);
#else  // defined LMI_MSW
    HWND handle = 0;
    if(wxTheApp)
        {
        wxWindow* top_window = wxTheApp->GetTopWindow();
        if(top_window)
            {
            handle = reinterpret_cast<HWND>(top_window->GetHandle());
            }
        }
    ::MessageBox(handle, message, "Error", MB_OK | MB_ICONSTOP | MB_TASKMODAL);
#endif // defined LMI_MSW
}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]