views:

194

answers:

2

Preface: wxWidgets 2.8.10 project on Windows.

I have a main application window (controls in a frame). If the user presses a hotkey, a pop-up window (implemented as a wxDialog) shows centered within the parent.

The behavior I want is if the user clicks outside of the pop-up window, on to the parent window, then the pop-up will dismiss, ala EndModal(wxID_CANCEL).

But if the user clicks outside the pop-up into another app, the pop-up should stay there.

Any thoughts?

A: 

If you want the dialog to close when the mouse is pressed outside of its screen area you need to catch the mouse clicks. Unfortunately the parent form won't receive them, as it is disabled while a modal dialog is shown. This happens on the system level, so there won't be any mouse messages sent to disabled windows in your app (actually my first idea was to use wxApp::FilterEvents(), but it's useless for this purpose due to this).

One idea would be to use the CaptureMouse() method, which can be used to direct all mouse events to the window having the capture, even when the mouse cursor is outside its screen area but over other windows of the application.

mghie
"Unfortunately it will receive all mouse events, whether they would go to another window of the same application, or to a window of another application."CaptureMouse() doesn't block clicks from going to other apps.When clicking in another application, mouse capture is lost (and you get a message to that effect which you MUST process, or else wxWidgets asserts.)
Adam Vandenberg
@Adam: Right, so it may be really worth looking into. I know about the capture lost event but didn't make the mental connection, I'll edit the answer accordingly. Thanks for the comment.
mghie
A: 

What about handling EVT_KILL_FOCUS in your popup window? A click outside your window will make the window which was clicked on receive the focus.

BuschnicK
Is there a way to tell if the focus kill was due to clicking into another window in my app, or clicking into another app entirely?
Adam Vandenberg
Well there is a global variable storing the current focus window somewhere. I have stumbled over it while debugging through the source code. Forgot were it was and whether it was publicly accessible though. wxApp::IsActive may be the better choice anyways. Handle EVT_KILL_FOCUS and use wxApp::IsActive to tell whether the whole app or just the window lost focus.
BuschnicK