Get the Window under the Cursor. Debug wrong Window overlaps. 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
Wolfram
In need of some credit
In need of some credit
Posts: 4
Joined: Wed Jul 24, 2019 2:14 pm

Get the Window under the Cursor. Debug wrong Window overlaps.

Post by Wolfram » Wed Dec 04, 2019 10:50 am

I am working on an issue where clicking on a wxButton leads to no reaction at all. Experience tells this is normally due to another window overlapping the button. For example the bug is that a wxStaticText was created much too large. You only see the text, which does not overlap the button so you think everything is alright. But clicking on a part of the button that is overlapped, nothing will happen as the click goes to the wxStaticText. Until now we "debugged" these kinds of issues by looking at the code with our eyes.

Is there a better way to debug such an issue? For example a debug mode where you would click somewhere and it would tell you the window under the cursor would be very helpful.
Some ideas:
* You would need to catch all mouse events in one place (I think there is a filter function for this?)
* When receiving a say "left down" event, you would need to find the window you just clicked on. I searched, but did not find any solution on how to do this.
* You would then display the position, size and label of the window. This would normally be enough to understand which other window overlaps your button.

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 3580
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Get the Window under the Cursor. Debug wrong Window overlaps.

Post by ONEEYEMAN » Wed Dec 04, 2019 1:42 pm

Hi,
Could you please post some code?

Thank you.

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

Re: Get the Window under the Cursor. Debug wrong Window overlaps.

Post by doublemax » Wed Dec 04, 2019 2:08 pm

Things you need:

wxAppConsole::FilterEvent
https://docs.wxwidgets.org/trunk/classw ... c6f602e20c

wxWindow* wxFindWindowAtPoint
https://docs.wxwidgets.org/trunk/group_ ... f3d3e406f0

However, if you're using sizers, sibling controls normally can't overlap (unless the window is too small to display them). And if you see the button visually getting pressed, the issue is somewhere else and not caused by overlapping windows.
Use the source, Luke!

Wolfram
In need of some credit
In need of some credit
Posts: 4
Joined: Wed Jul 24, 2019 2:14 pm

Re: Get the Window under the Cursor. Debug wrong Window overlaps.

Post by Wolfram » Wed Dec 04, 2019 5:55 pm

EXCELLENT! That works as hoped for and will be a good tool in our toolbox for looking at all kind of windows / window properties.

Somehow my google fu broke down and I did not find the function wxFindWindowAtPoint().

We indeed do not use sizers. I hope to get around to it one day, but right now we are simply struggling to create a new version of our software before XMas.

With the information from doublemax (THANK YOU!) it is almost trivial to write such a debug tool but just in case someone is interested in my code:

Code: Select all

int WX_App::FilterEvent(wxEvent& event) //override
{
	if (wxMouseEvent* pMouseEvent = dynamic_cast<wxMouseEvent*>(&event))
	{
		if (pMouseEvent->ButtonDown(wxMOUSE_BTN_LEFT))
		{
			wxCoord x = pMouseEvent->GetX();
			wxCoord y = pMouseEvent->GetY();
			wxString s;
			s << x << L", " << y;

			wxWindow* pWnd = wxFindWindowAtPointer(wxPoint(x, y));
			if (pWnd)
			{
				wxPoint pt = pWnd->GetScreenPosition();
				wxSize size = pWnd->GetSize();
				s << ", x,y,w,h == " << pt.x << ", " << pt.y << ", " << size.x << ", " << size.y;
				wxString sName = typeid(*pWnd).name();
				s << ", typename == " << sName;
				
				// Test for classes we derived from wxWindow like CX_Button
				if (CX_Button* pBtn = dynamic_cast<CX_Button*>(pWnd))
				{
				 handle CX_Button here
				}
				else
				{
					s << ", pWnd->GetLabel() == " << pWnd->GetLabel();
				}
			}
			else
			{
				s << L", pWnd == nullptr";
			}
			Somehow output "s" here. I put it into our StatusBar.
		}
	}
	return -1;
}

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 3580
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Get the Window under the Cursor. Debug wrong Window overlaps.

Post by ONEEYEMAN » Wed Dec 04, 2019 6:18 pm

Hi,
You should start using sizers. Then all those things will disappear. ;-)

Thank, you.

Post Reply