How to check if a window is valid? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Frank
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Jan 01, 2005 6:19 pm

How to check if a window is valid?

Post by Frank » Wed Mar 04, 2009 10:13 pm

Hi,

I have a function that gets a wxWindow*. I need to test if the pointee is sill a valid window. Because it can happen that the window is closed by the user between saving the window pointer and calling AddPendingEvent() for this window.

Or in Short: I need a wx equavilent for the WinAPI IsWindow()

Or, as alternative: Is there a way to install some sort of global "Hook" with wich I can automatically add EventHandlers for Creation/Destruction for every created Window, so I can manage a global std::set<> of valid windows to check against.

JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Post by JimFairway » Wed Mar 04, 2009 10:25 pm

Hi,

I suspect you have to catch the close event for the window (EVT_CLOSE) and notify the object that recorded the pointer to the window that it's being destroyed. Otherwise, the pointer will most likely point to random data.

See http://docs.wxwidgets.org/2.6/wx_window ... rview.html for the sequence regarding which deletion.

Hope that helps,


Jim
OS: Vista SP1, wxWidgets 2.8.7.

User avatar
doublemax
Moderator
Moderator
Posts: 15828
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax » Wed Mar 04, 2009 10:28 pm

Another option could be to store the window id instead of the pointer and then use wxWindow::FindWindowById()

http://docs.wxwidgets.org/stable/wx_wxw ... windowbyid
Use the source, Luke!

Frank
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Jan 01, 2005 6:19 pm

Post by Frank » Wed Mar 04, 2009 10:46 pm

Unluckily, most of my windows are created with wxID_ANY, so I can't use the ID.

And since half of the pointee's are wxPanels in a notebook (sort of), I can't use the name either.

The other half are TopLevel windows, but sadly there is no wxWindow wich holds HWND_DESKTOP, so I also can't use wxWindow::GetChilds() to check against those.

But wait: If wx can find a window by it's ID, that means it must have a list of all windows somewhere. Maybee I can just patch in a function to check against this list.

JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Post by JimFairway » Wed Mar 04, 2009 10:53 pm

Hi
Unluckily, most of my windows are created with wxID_ANY, so I can't use the ID.
If you go with doublemax's idea (which looks fairly straightforward), it doesn't matter if you use wxID_ANY, just call GetId() after you create the window and store that.

Jim
OS: Vista SP1, wxWidgets 2.8.7.

User avatar
doublemax
Moderator
Moderator
Posts: 15828
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax » Wed Mar 04, 2009 11:09 pm

But wait: If wx can find a window by it's ID, that means it must have a list of all windows somewhere. Maybee I can just patch in a function to check against this list.
wx keeps a list of toplevel windows, from there it searches recursively.

Check wxFindWindowHelper() in /src/common/wincmn.cpp
Use the source, Luke!

computerquip
Experienced Solver
Experienced Solver
Posts: 72
Joined: Fri Feb 20, 2009 7:13 pm
Location: $(#wx)\src

Post by computerquip » Wed Mar 04, 2009 11:22 pm

Can't you make a reference and then check the reference to see whether or not the data still exists?

EDIT: Might be valid but not very convenient >.>

Frank
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Jan 01, 2005 6:19 pm

Post by Frank » Thu Mar 05, 2009 12:33 am

Thanks, I take a closer look into these. The ID-Thingy seems to be the esiest way to do this. I look into it tommorow.

Frank
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Jan 01, 2005 6:19 pm

Post by Frank » Thu Mar 05, 2009 1:31 pm

I've written my own dialog class, from wich I derive the Non-Modal Dialogs.

This class connects a wxEVT_DESTROY to the to-be-notified window, so it can set the pointer to 0, when it gets destroyed.

Also, I just can make a member PostData (const boost::any&), without any need for the derived class to check if the window still exists or not.

Post Reply