"Slow focus" when handling wxEVT_SET_FOCUS

Zokbor zokbor at gmail.com
Sat Dec 2 18:11:56 PST 2006


I created a class to conveniently create two wxStaticText, a wxTextCtrl
and a wxSpinButton widgets in a single call. Also, the class needs to
handle SetFocus() events on the wxTextCtrl. Although the code seems to
work, connecting to the wxEVT_SET_FOCUS event results in slow focusing:
when I click on a text control, the callback is immediately called
(i.e. the printf() message appears immediately), however, it takes
about a second before the widget on screen is redrawn with the "focus
rectangle" (using wxWidgets 2.6.1, gcc 4.0.3, Ubuntu Linux 6.06).

Please find a stripped down code snippet below that illustrates the
behaviour. Click alternating on the upper and lower text control to see
the "slow focus". Try again with the m_text->Connect() commented out to
see the normal focusing.

Would someone please be so kind to point out what I am doing wrong?
Thanks in advance.
-- Zokbor

//-------------------------------------------------
// Example code to illustrate "slow focusing"
// when wxEVT_SET_FOCUS handler is connected.
//

#include <wx/wx.h>

//-------------------------------------------------
// MyTextCtrl declaration
//
class MyTextCtrl : public wxWindow
{
public:
   MyTextCtrl(wxWindow *parent,
              const wxString &text) ;
   wxTextCtrl *GetTextCtrl(void) ;
   void OnSetFocus(wxFocusEvent &event) ;

private:
   wxTextCtrl *m_text ;
} ;

//-------------------------------------------------
// Focus declaration
//
class Focus : public wxApp
{
public:
   bool OnInit() ;

private:
   wxFrame *m_mainFrame ;
   MyTextCtrl *m_text1 ;
   MyTextCtrl *m_text2 ;
} ;

DECLARE_APP(Focus)

//-------------------------------------------------
// MyTextCtrl implementation
//
MyTextCtrl::MyTextCtrl(
   wxWindow *parent,
   const wxString &text)
{
   m_text = new wxTextCtrl(parent, -1, text) ;
   m_text->Connect(-1, wxEVT_SET_FOCUS,
                   wxFocusEventHandler(
                      MyTextCtrl::OnSetFocus),
                   NULL, this) ;
}

wxTextCtrl *MyTextCtrl::GetTextCtrl(void)
{
   return m_text ;
}

void MyTextCtrl::OnSetFocus(
   wxFocusEvent &WXUNUSED(event))
{
   printf("OnSetFocus(%p)\n", this) ;
}

//-------------------------------------------------
// Focus implementation
//
IMPLEMENT_APP(Focus)

bool Focus::OnInit()
{
   m_mainFrame = new wxFrame((wxWindow *)NULL,
                             wxID_ANY,
                             wxT("Slow Focus")) ;
   wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL) ;
   m_text1 = new MyTextCtrl(m_mainFrame,
                            wxT("foo")) ;
   sizer->Add(m_text1->GetTextCtrl(), 0, 0) ;
   m_text2 = new MyTextCtrl(m_mainFrame,
                            wxT("bar")) ;
   sizer->Add(m_text2->GetTextCtrl(), 0, 0) ;
   m_mainFrame->SetSizer(sizer) ;
   m_mainFrame->Layout() ;
   m_mainFrame->Show() ;
   SetTopWindow(m_mainFrame) ;
   return true ;
}







More information about the wx-users mailing list