How to bring a window in the back to the front

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
shinoshino
Earned a small fee
Earned a small fee
Posts: 14
Joined: Wed Sep 28, 2022 9:27 am

How to bring a window in the back to the front

Post by shinoshino »

Hello

I have two windows whose coordinates are specified by SetPoint() without using a sizer.
When they overlap, I want to bring the one clicked on to the front. However, the window generated later is always in the foreground.
How can I do this?

I am using wxWidgets3.1.4 on a Raspberry Pi 3 (GTK3).
Thank you in advance for your help.

Code: Select all

class myPanel : public wxPanel
{
public:
	myPanel( wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize )
		:wxPanel( parent, id, pos, size )
	{
		SetMinSize( wxSize(400, 600) );

		m_pnl1 = new wxPanel( this, 0, wxPoint( 0, 40 ), wxSize( 120, 50 ) );
		m_pnl2 = new wxPanel( this, 1, wxPoint( 90, 50 ), wxSize( 120, 50 ) );

		m_pnl1->SetBackgroundColour( *wxRED );
		m_pnl2->SetBackgroundColour( *wxBLUE );

		m_pnl1->Bind( wxEVT_LEFT_DOWN, [&]( wxMouseEvent& event )
			{
				//I want to put m_pnl1 in front.
			} );
		m_pnl2->Bind( wxEVT_LEFT_DOWN, [&]( wxMouseEvent& event )
			{
				//I want to put m_pnl2 in front.
			} );
	}

private:
	wxPanel* m_pnl1;
	wxPanel* m_pnl2;
};
catalin
Moderator
Moderator
Posts: 1618
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: How to bring a window in the back to the front

Post by catalin »

You cannot (reliably) do that. Overlapping widgets are not supported. You can only lay them out side by side. Only top-level wxWindow-s can overlap.
User avatar
doublemax@work
Super wx Problem Solver
Super wx Problem Solver
Posts: 474
Joined: Wed Jul 29, 2020 6:06 pm
Location: NRW, Germany

Re: How to bring a window in the back to the front

Post by doublemax@work »

In general wxWidgets does not support overlapping siblings, but you can try wxWindow::Raise(). If it works, good for you, otherwise you might be out of luck or have to look for a platform-specific solution.
catalin
Moderator
Moderator
Posts: 1618
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: How to bring a window in the back to the front

Post by catalin »

doublemax@work wrote: Wed Sep 28, 2022 10:30 am you can try wxWindow::Raise()
That would still be unreliable. I remember an app that was available as part of the old wxCode, which created overlapping widgets. When using 2.8.x the last clicked widget would be drawn on top. With 2.9.x that changed, and the last clicked widget went underneath... Events were also messed up.
Just saying, even if it works today there's no guarantee it will continue to work tomorrow.

Platform-specific code would be the only way here IMO.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: How to bring a window in the back to the front

Post by ONEEYEMAN »

Hi,
Any reason why you are not using sizers?

Thank you.
shinoshino
Earned a small fee
Earned a small fee
Posts: 14
Joined: Wed Sep 28, 2022 9:27 am

Re: How to bring a window in the back to the front

Post by shinoshino »

Thank you for your response.

I thought there was a way because the MSW wxTextCtrl came to the front on hover.

I have a number line and multiple values placed on it. The values are placed in a WINDOW, not in PAINT, because they are to be edited by clicking, etc. I did not use a sizer because it was easier to calculate the positional adjustments. I tried to bring them to the top by clicking or hovering because the values are too large and close together and overlap.

I will try a different method, such as shrinking with the sizer instead of overlapping.

Translated with www.DeepL.com/Translator (free version)
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: How to bring a window in the back to the front

Post by ONEEYEMAN »

Hi,
And what you will do if yo app will go to the HighDPI monitor?
Or the theme will change and font size will?

You just re-inventing the wheel..

Use sizers and you application will work everywhere.

Thank you.
Post Reply