Raising a wxTextCtrl? 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
clyde729
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Mon May 29, 2006 10:50 pm
Location: Jena, Germany

Raising a wxTextCtrl?

Post by clyde729 » Wed Oct 11, 2006 1:23 pm

Hi,

I have placed several textfields on a panel very close together. Now, if one textfield receives the focus, I want to increment the size and the font to make it appear bigger. Also on "lost focus" I want to bring it back to the original size.

I wrote an eventhandler and gave each textfield a new instance of it. Sizing and font changing works fine. Now the problem: I want to bring the focussed textfield to the top of the panels window hierarchy so that the bigger textfield will be painted over the other textfields.

In my sample below it will only work, if I reverse the creation / taborder. But this is strange behaviour to the user. So how to "raise" the textfield? I tried some update / refresh methods without success.

Any ideas?

Here is the code:

Code: Select all

#ifndef __MY_APP__
#define __MY_APP__

#ifdef __BORLANDC__
	#pragma hdrstop
#endif

#ifndef WX_PRECOMP
	#include <wx/wx.h>
#else
	#include <wx/wxprec.h>
#endif

#include <wx/textctrl.h>
#include <wx/panel.h>
#include <wx/event.h>
#include <wx/window.h>
#include <wx/frame.h>

class MyFrameApp : public wxApp
{
	public:
		bool OnInit();
		int OnExit();
};

class MyFrame : public wxFrame
{
	private:
		DECLARE_EVENT_TABLE();
		
	public:
		MyFrame(wxWindow *parent, wxWindowID id = 1, 
            const wxString &title = wxT("test"), 
            const wxPoint& pos = wxDefaultPosition, 
            const wxSize& size = wxDefaultSize, 
            long style = wxDEFAULT_FRAME_STYLE);
		virtual ~MyFrame();
	public:
		wxTextCtrl *WxEdit6;
		wxTextCtrl *WxEdit5;
		wxTextCtrl *WxEdit4;
		wxTextCtrl *WxEdit3;
		wxTextCtrl *WxEdit2;
		wxTextCtrl *WxEdit1;
		wxPanel *WxPanel1;
	public:
		enum
		{
			ID_WXEDIT6 = 1007,
			ID_WXEDIT5 = 1006,
			ID_WXEDIT4 = 1005,
			ID_WXEDIT3 = 1004,
			ID_WXEDIT2 = 1003,
			ID_WXEDIT1 = 1002,
			ID_WXPANEL1 = 1001,
			ID_DUMMY_VALUE_ 
		};
		
	public:
		void MyFrameClose(wxCloseEvent& event);
		void CreateGUIControls(void);
};

class GrowHandler : public wxEvtHandler
{
  DECLARE_EVENT_TABLE()
  
  public:
    
    GrowHandler(wxWindow* owner);

    void onSetFocus(wxFocusEvent& event);
    
    void onLostFocus(wxFocusEvent& event);  
    
    wxWindow* m_owner;
};

IMPLEMENT_APP(MyFrameApp)

bool MyFrameApp::OnInit()
{
    MyFrame* frame = new MyFrame(NULL);
    SetTopWindow(frame);
    frame->Show(true);		
    return true;
}
 
int MyFrameApp::OnExit()
{
	return 0;
}

BEGIN_EVENT_TABLE(MyFrame,wxFrame)
	EVT_CLOSE(MyFrame::MyFrameClose)
END_EVENT_TABLE()

MyFrame::MyFrame(wxWindow *parent, wxWindowID id, const wxString &title, 
                 const wxPoint &position, const wxSize& size, long style)
        :wxFrame(parent, id, title, position, size, style)
{
	CreateGUIControls();
}

MyFrame::~MyFrame() {}

void MyFrame::CreateGUIControls(void)
{
	WxPanel1 = new wxPanel(this, ID_WXPANEL1, wxPoint(0,0), wxSize(262,83));

	WxEdit1 = new wxTextCtrl(WxPanel1, ID_WXEDIT1, wxT("WxEdit1"), 
                           wxPoint(8,8), wxSize(121,21), wxSIMPLE_BORDER, 
                           wxDefaultValidator, wxT("WxEdit1"));

	WxEdit2 = new wxTextCtrl(WxPanel1, ID_WXEDIT2, wxT("WxEdit2"), 
                           wxPoint(131,8), wxSize(121,21), wxSIMPLE_BORDER, 
                           wxDefaultValidator, wxT("WxEdit2"));

	WxEdit3 = new wxTextCtrl(WxPanel1, ID_WXEDIT3, wxT("WxEdit3"), 
                           wxPoint(8,31), wxSize(121,21), wxSIMPLE_BORDER, 
                           wxDefaultValidator, wxT("WxEdit3"));

	WxEdit4 = new wxTextCtrl(WxPanel1, ID_WXEDIT4, wxT("WxEdit4"), 
                           wxPoint(131,31), wxSize(121,21), wxSIMPLE_BORDER, 
                           wxDefaultValidator, wxT("WxEdit4"));

	WxEdit5 = new wxTextCtrl(WxPanel1, ID_WXEDIT5, wxT("WxEdit5"), 
                           wxPoint(8,53), wxSize(121,21), wxSIMPLE_BORDER, 
                           wxDefaultValidator, wxT("WxEdit5"));

	WxEdit6 = new wxTextCtrl(WxPanel1, ID_WXEDIT6, wxT("WxEdit6"), 
                           wxPoint(131,53), wxSize(121,21), wxSIMPLE_BORDER, 
                           wxDefaultValidator, wxT("WxEdit6"));

	this->SetSize(8,8,270,117);
	this->SetTitle(wxT("test"));
	this->Center();
	this->SetIcon(wxNullIcon);
	
	WxEdit1->PushEventHandler(new GrowHandler(WxEdit1));
	WxEdit2->PushEventHandler(new GrowHandler(WxEdit2));
	WxEdit3->PushEventHandler(new GrowHandler(WxEdit3));
	WxEdit4->PushEventHandler(new GrowHandler(WxEdit4));
	WxEdit5->PushEventHandler(new GrowHandler(WxEdit5));
	WxEdit6->PushEventHandler(new GrowHandler(WxEdit6));
}

void MyFrame::MyFrameClose(wxCloseEvent& event)
{
	WxEdit1->PopEventHandler(true);
	WxEdit2->PopEventHandler(true);
	WxEdit3->PopEventHandler(true);
	WxEdit4->PopEventHandler(true);
	WxEdit5->PopEventHandler(true);
	WxEdit6->PopEventHandler(true);

  Destroy();
}
 
BEGIN_EVENT_TABLE(GrowHandler,wxEvtHandler)
	EVT_SET_FOCUS(GrowHandler::onSetFocus)
	EVT_KILL_FOCUS(GrowHandler::onLostFocus)
END_EVENT_TABLE()

GrowHandler::GrowHandler(wxWindow* owner)
{
  m_owner = owner;
}

void GrowHandler::onSetFocus(wxFocusEvent& event)
{
  if(m_owner)
  {
    wxSize s = m_owner->GetSize();
    s.Set(s.GetWidth() + 10, s.GetHeight() + 10);
    m_owner->SetSize(s);
    
    wxFont f = m_owner->GetFont();
    f.SetPointSize(f.GetPointSize() + 5);
    m_owner->SetFont(f);

    m_owner->Raise();
    //m_owner->GetParent()->Update();
    //m_owner->GetParent()->Refresh();
    //m_owner->GetParent()->UpdateWindowUI(wxUPDATE_UI_RECURSE);
  }
  
  event.Skip();
}

void GrowHandler::onLostFocus(wxFocusEvent& event)
{
  if(m_owner)
  {
    wxSize s = m_owner->GetSize();
    s.Set(s.GetWidth() - 10, s.GetHeight() - 10);
    m_owner->SetSize(s);

    wxFont f = m_owner->GetFont();
    f.SetPointSize(f.GetPointSize() - 5);
    m_owner->SetFont(f);

    m_owner->Lower();
    //m_owner->GetParent()->Update();
    //m_owner->GetParent()->Refresh();
    //m_owner->GetParent()->UpdateWindowUI(wxUPDATE_UI_RECURSE);
  }
  
  event.Skip();
}
#endif
OS: Windows XP Home, Compiler: MingW, Version: wxWidgets 2.8.0, IDE: wx-Devcpp

Saurabh
Earned some good credits
Earned some good credits
Posts: 117
Joined: Thu Sep 07, 2006 10:25 am
Location: India
Contact:

Post by Saurabh » Fri Oct 13, 2006 3:25 am

AFAIK you cannot direcly change the display controls from another clas while its in another. The change must take place while in idle mode only. SO U need to make those event handlers a part of the mainframe class

tiwag
Earned some good credits
Earned some good credits
Posts: 123
Joined: Tue Dec 21, 2004 8:51 pm
Location: Austria

Post by tiwag » Fri Oct 13, 2006 8:04 am

seems to be not possible

i've also tried using sizers - no success

there are a lot of postings in
http://groups.google.com/group/comp.soft-sys.wxwindows

regarding the Raise/Lower and z-order problems, but no solutions

the code for wxMSW Raise() looks pretty forward, it must be overwritten somewhere "behind" the scenes ...
-tiwag

clyde729
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Mon May 29, 2006 10:50 pm
Location: Jena, Germany

Post by clyde729 » Mon Oct 16, 2006 7:27 am

OK, thanks anyway
OS: Windows XP Home, Compiler: MingW, Version: wxWidgets 2.8.0, IDE: wx-Devcpp

Post Reply