Main/Primary Frame/Window should not get focus if secondary Frame/Window is opened 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
Harsh
Experienced Solver
Experienced Solver
Posts: 70
Joined: Tue Apr 13, 2021 5:03 am

Main/Primary Frame/Window should not get focus if secondary Frame/Window is opened

Post by Harsh »

Is there any Flag or Managed window/frame or dialog which do not loose focus to Main window until closed?

I tried few but could not find the combination. I also tried ChildFocusEvent on main window but it do not work on grand children or crashes.
User avatar
doublemax
Moderator
Moderator
Posts: 19117
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Main/Primary Frame/Window should not get focus if secondary Frame/Window is opened

Post by doublemax »

wxDialog should work. Make sure to open it with ShowModal(), not Show().
Use the source, Luke!
Harsh
Experienced Solver
Experienced Solver
Posts: 70
Joined: Tue Apr 13, 2021 5:03 am

Re: Main/Primary Frame/Window should not get focus if secondary Frame/Window is opened

Post by Harsh »

doublemax wrote: Sun Jun 06, 2021 7:44 am wxDialog should work. Make sure to open it with ShowModal(), not Show().
Thank you very much @doublemax.

But now txt->SetFocus() not working in wxDialog close event.
txt=FindWindow(1);
Tried this->Layout()/Refresh()/Update(); before it.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7460
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Main/Primary Frame/Window should not get focus if secondary Frame/Window is opened

Post by ONEEYEMAN »

Hi,
Can we see some code?

Thank you.
Harsh
Experienced Solver
Experienced Solver
Posts: 70
Joined: Tue Apr 13, 2021 5:03 am

Re: Main/Primary Frame/Window should not get focus if secondary Frame/Window is opened

Post by Harsh »

ONEEYEMAN wrote: Mon Jun 07, 2021 12:00 pm Hi,
Can we see some code?

Thank you.
It works in demo but not in my project. I checked Event.Skip() is given in all event function at first line, its fine.

I will check for other bugs.
Any suggestion?
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7460
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Main/Primary Frame/Window should not get focus if secondary Frame/Window is opened

Post by ONEEYEMAN »

Hi,
What do you do differently?
Can you make a reproducer?

Also - you don't have to use event.Skip() everywhere. Sometimes it can be dangerous.

You absolutely have to use for all non command events, unless you know what you are doing. But wxCommandEvent-derived functions can survive without it.

Thank you.
Harsh
Experienced Solver
Experienced Solver
Posts: 70
Joined: Tue Apr 13, 2021 5:03 am

Re: Main/Primary Frame/Window should not get focus if secondary Frame/Window is opened

Post by Harsh »

ONEEYEMAN wrote: Mon Jun 07, 2021 12:00 pm Hi,
Can we see some code?

Thank you.
I have reproducted the Issue here.

Code: Select all

#include<wx/wx.h>

class MyFrame : public wxFrame{
	    wxPanel *panel;
        wxBoxSizer *vbox;
		wxTextCtrl *txt;
		wxDialog *dlg;
		
		void OnChar(wxKeyEvent &event){
			event.Skip();
			if(event.GetKeyCode()==13){
				std::cout<<"13";
				txt=wxDynamicCast(event.GetEventObject(),wxTextCtrl);
				txt->Navigate(wxNavigationKeyEvent::IsForward);
			}
		}
		void OnFocusOut(wxFocusEvent &event){
			event.Skip();
			ShowInfo();
		}
		void ShowInfo(){
			dlg=new wxDialog(wxDynamicCast(this,wxWindow),wxID_ANY,wxT("Dialog Example"));

			vbox=new wxBoxSizer(wxVERTICAL);

			txt=new wxTextCtrl(dlg,wxID_ANY,wxT("1234567890"),wxDefaultPosition,wxSize(200,30));
			vbox->Add(txt,0,wxEXPAND,5);

			dlg->SetSizer(vbox);
			dlg->ShowModal();
		}
public : 
	MyFrame():wxFrame(NULL,wxID_ANY,"Child Focus"){

		panel=new wxPanel(this);
		panel->Bind(wxEVT_CHAR_HOOK,&MyFrame::OnChar,this);
		vbox=new wxBoxSizer(wxVERTICAL);

		txt=new wxTextCtrl(panel,wxID_ANY,wxT(""),wxDefaultPosition,wxDefaultSize,wxTE_PROCESS_ENTER);
		txt->Bind(wxEVT_KILL_FOCUS,&MyFrame::OnFocusOut,this);
		vbox->Add(txt,0,wxEXPAND);

		vbox->AddSpacer(5);

		txt=new wxTextCtrl(panel,wxID_ANY,wxT(""));
		vbox->Add(txt,0,wxEXPAND);

		panel->SetSizer(vbox);
	}
};

class MyApp: public wxApp
{
    wxFrame* m_frame;
public:
    
    bool OnInit()
    {
        m_frame = new MyFrame();
        m_frame->Show();
        return true;
    } 
    
};

IMPLEMENT_APP(MyApp);
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7460
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Main/Primary Frame/Window should not get focus if secondary Frame/Window is opened

Post by ONEEYEMAN »

Hi,
Why do you have 2 textcontrol created, but only one pointer?

Also, unrelated, you don't have to cast the pointer that you pass to the dialog constructor - just use this.

Thank you.
Harsh
Experienced Solver
Experienced Solver
Posts: 70
Joined: Tue Apr 13, 2021 5:03 am

Re: Main/Primary Frame/Window should not get focus if secondary Frame/Window is opened

Post by Harsh »

ONEEYEMAN wrote: Mon Jun 07, 2021 2:58 pm Hi,
Why do you have 2 textcontrol created, but only one pointer?

Also, unrelated, you don't have to cast the pointer that you pass to the dialog constructor - just use this.

Thank you.
Added this as parent and txt1 as second field.
Still not working.
User avatar
doublemax
Moderator
Moderator
Posts: 19117
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Main/Primary Frame/Window should not get focus if secondary Frame/Window is opened

Post by doublemax »

Please explain in words what you want to happen.
Use the source, Luke!
Harsh
Experienced Solver
Experienced Solver
Posts: 70
Joined: Tue Apr 13, 2021 5:03 am

Re: Main/Primary Frame/Window should not get focus if secondary Frame/Window is opened

Post by Harsh »

doublemax wrote: Mon Jun 07, 2021 4:29 pm Please explain in words what you want to happen.
When I close wxDialog, main frame do not show cursor automatically.
User avatar
doublemax
Moderator
Moderator
Posts: 19117
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Main/Primary Frame/Window should not get focus if secondary Frame/Window is opened

Post by doublemax »

Just for the record: Displaying a modal dialog when a textcontrol loses focus, is horrible GUI design. When ever i encounter a program that's does it, it'll get uninstalled immediately ;)

The problem is that inside a wxEVT_KILL_FOCUS handler, you're doing something that will cause more focus events. So you'll need some wx magic to delay setting the focus back to the text control.

Code: Select all

void OnFocusOut(wxFocusEvent &event){
  event.Skip();
  
  CallAfter( [this] {
    // this will get executed asynchronously after all events have been processed
    ShowInfo();
  });
}

void ShowInfo(){
  // better: subclass wxDialog and put all the initialization in its constructor
  // and create it on the stack instead of the heap
  dlg=new wxDialog(this, wxID_ANY, wxT("Dialog Example"));

  vbox=new wxBoxSizer(wxVERTICAL);

  txt=new wxTextCtrl(dlg,wxID_ANY,wxT("1234567890"),wxDefaultPosition,wxSize(200,30));
  vbox->Add(txt,0,wxEXPAND,5);

  dlg->SetSizer(vbox);
  dlg->ShowModal();
  
  // if you create the dialog on the heap, you need to destroy it
  dlg->Destroy();
}
Use the source, Luke!
Harsh
Experienced Solver
Experienced Solver
Posts: 70
Joined: Tue Apr 13, 2021 5:03 am

Re: Main/Primary Frame/Window should not get focus if secondary Frame/Window is opened

Post by Harsh »

Just for the record: Displaying a modal dialog when a textcontrol loses focus, is horrible GUI design. When ever i encounter a program that's does it, it'll get uninstalled immediately ;)
I will remember it. @doublemax
Thank you.. =D> =D> =D> =D>
Post Reply