Creating own control - focus issue

Are you writing your own components and need help with how to set them up or have questions about the components you are deriving from ? Ask them here.
Post Reply
jhruby
In need of some credit
In need of some credit
Posts: 7
Joined: Wed May 14, 2008 11:12 am
Location: Czech Republic, Prag
Contact:

Creating own control - focus issue

Post by jhruby »

Hi,
i have a big problem. I wrote my own control. It was derived from wxControl and it was initialized through wxControl::Create method.

It works great, but Windows never sets focus to this compoment. When the control is derived from wxListCtrl windows sets focus normally.

Do somebody known the solution?
:roll:
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

What is this control?

A general tip might be to derive from wxPanel instead of wxControl. wxControl is a low-level abstract class that expects you to do everything, while wxPanel will manage quite a bit of that stuff for you
jhruby
In need of some credit
In need of some credit
Posts: 7
Joined: Wed May 14, 2008 11:12 am
Location: Czech Republic, Prag
Contact:

Post by jhruby »

It is a calendar. I will try your tip. Thanks..
jhruby
In need of some credit
In need of some credit
Posts: 7
Joined: Wed May 14, 2008 11:12 am
Location: Czech Republic, Prag
Contact:

Post by jhruby »

So, it did not work. The behaviour of this component is the same as by using wxControl. :(

onKeyDown handler gets WXK_TAB, so i call Navigate method and nothing happens.

Where is a mistake?
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

Can you show code?
jhruby
In need of some credit
In need of some credit
Posts: 7
Joined: Wed May 14, 2008 11:12 am
Location: Czech Republic, Prag
Contact:

Post by jhruby »

This is my source example. CalendarCtrl is derived from wxPanel.

Code: Select all

bool CalendarCtrl::Create(wxWindow *parent,
                      wxWindowID id,
                      const wxPoint& pos)
{	
    
	return wxPanel::Create(parent, id, pos,wxDefaultSize,wxBORDER_NONE|wxWANTS_CHARS);
}

void CalendarCtrl::onKeyPress(wxKeyEvent & event) {
	int code = event.GetKeyCode();
	if((code == WXK_EXECUTE || code == WXK_SPACE || code ==  WXK_RETURN)&&(movedCol != -1 && movedRow != -1)) {
		if (date) {
			findDate(movedRow, movedCol, date);
		}
		else {
			date = new wxDateTime();
			findDate(movedRow, movedCol, date);
		}
		selectPosition();
	}else if (code == WXK_TAB){
		Navigate((event.ShiftDown()?(wxNavigationKeyEvent::IsBackward):(wxNavigationKeyEvent::IsForward)));
	} else ...
[/code]
frank_frl
Earned some good credits
Earned some good credits
Posts: 139
Joined: Sat Feb 18, 2006 1:41 pm
Location: Germany

Post by frank_frl »

Hi jhruby,

for TAB key handling try this

Code: Select all

switch(event.GetKeyCode())
{
	case WXK_TAB:
	{
		wxNavigationKeyEvent evt;
		int dir = event.ShiftDown()?0:1;
		evt.SetFlags(wxNavigationKeyEvent::FromTab|
					 (dir?wxNavigationKeyEvent::IsForward:
						  wxNavigationKeyEvent::IsBackward));
		evt.SetEventObject(this);
	GetParent()->GetEventHandler()->AddPendingEvent(evt);

	}	
	break;
}
How do you know that your control has no focus, you have to draw this state yourself in your OnPaint handler.

Code: Select all

if(FindFocus() == this)
	DrawFocusRect(dc);
Regards,

Frank
Post Reply