wxTextCtrl cursor position Topic is solved

This forum can be used to talk about general design strategies, new ideas and questions in general related to wxWidgets. If you feel your questions doesn't fit anywhere, put it here.
Post Reply
Swoop
Earned a small fee
Earned a small fee
Posts: 20
Joined: Wed Nov 01, 2006 10:22 pm
Location: Earth

wxTextCtrl cursor position

Post by Swoop » Fri Apr 30, 2010 12:41 am

I have a wxTextCtrl when enter is hit I Clear() the text and need to set the cursor position back to the start, how can I do that?

Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria » Fri Apr 30, 2010 1:32 am

Isn't this already the case? What happens to the cursor?
"Keyboard not detected. Press F1 to continue"
-- Windows

Swoop
Earned a small fee
Earned a small fee
Posts: 20
Joined: Wed Nov 01, 2006 10:22 pm
Location: Earth

Post by Swoop » Fri Apr 30, 2010 6:37 am

You type a few words, enter is hit, the function to handle enter is called, in there I copy the text, use it, then call
the wxTextCtrls Clear(), the text clears but the cursor stays on the second line where enter placed it. Now if I were to start typing again, Im typing on the second line, I want the cursor to go back to the top left of the text ctrl.

Mojo
Super wx Problem Solver
Super wx Problem Solver
Posts: 401
Joined: Wed Sep 21, 2005 8:17 am
Location: Rostov-on-Don, Southern Russia

Post by Mojo » Fri Apr 30, 2010 8:22 am

Swoop wrote:You type a few words, enter is hit, the function to handle enter is called, in there I copy the text, use it, then call
the wxTextCtrls Clear(), the text clears but the cursor stays on the second line where enter placed it. Now if I were to start typing again, Im typing on the second line, I want the cursor to go back to the top left of the text ctrl.

Code: Select all

m_textCtrl->SetFocus();
Win XP HE SP3, Vista
Xubuntu 12.04 LTS
wxWidgets-2.9.5
wxWidgets-3.0.0

Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria » Fri Apr 30, 2010 3:31 pm

Swoop wrote:You type a few words, enter is hit, the function to handle enter is called, in there I copy the text, use it, then call
the wxTextCtrls Clear(), the text clears but the cursor stays on the second line where enter placed it. Now if I were to start typing again, Im typing on the second line, I want the cursor to go back to the top left of the text ctrl.
This kinda sounds like a bug, the cursor should probably not stay at a position where there is no text anymore :(
"Keyboard not detected. Press F1 to continue"
-- Windows

Swoop
Earned a small fee
Earned a small fee
Posts: 20
Joined: Wed Nov 01, 2006 10:22 pm
Location: Earth

Post by Swoop » Fri Apr 30, 2010 7:44 pm

SetFocus is a no go.

timg
Earned some good credits
Earned some good credits
Posts: 148
Joined: Mon Jan 23, 2006 6:52 pm

Post by timg » Sat May 01, 2010 5:50 am

Maybe try:

wxTextCtrl::SetInsertionPoint(long pos)

Sets the insertion point at the given position.

Ridmix
Knows some wx things
Knows some wx things
Posts: 40
Joined: Sat Dec 01, 2007 9:27 am

Post by Ridmix » Sat May 01, 2010 9:21 am

I had the same problem.

@Swoop, try this;

Code: Select all

void TextCtrl::OnEnter(wxRichTextEvent& event)
{
	Clear();
	MoveToParagraphStart();
	
	event.Skip();
}
wxWidgets 2.8.10 & WX_TRUNK
Ubuntu 9.10
GCC 4.4

Mojo
Super wx Problem Solver
Super wx Problem Solver
Posts: 401
Joined: Wed Sep 21, 2005 8:17 am
Location: Rostov-on-Don, Southern Russia

Post by Mojo » Sat May 01, 2010 9:54 am

Ridmix wrote:I had the same problem.

@Swoop, try this;

Code: Select all

void TextCtrl::OnEnter(wxRichTextEvent& event)
{
	Clear();
	MoveToParagraphStart();
	
	event.Skip();
}
wxTextCtrl hasn't MoveToParagraphStart() member.

There is MoveToParagraphStart() in wxRichTextCtrl only.
Win XP HE SP3, Vista
Xubuntu 12.04 LTS
wxWidgets-2.9.5
wxWidgets-3.0.0

Mojo
Super wx Problem Solver
Super wx Problem Solver
Posts: 401
Joined: Wed Sep 21, 2005 8:17 am
Location: Rostov-on-Don, Southern Russia

Post by Mojo » Sat May 01, 2010 10:40 am

If I get right, the problem is double action when you push Enter key.

The first action - m_textCtrl1->Clear(); and simultaneously goes the second one - pushing Enter key removes cursor on next line (this native action in windows textcontrol).


So, it takes to convert the question: "How to disable the second action i.e. removing cursor (or caret?) on next line when you push Enter-key ?
Win XP HE SP3, Vista
Xubuntu 12.04 LTS
wxWidgets-2.9.5
wxWidgets-3.0.0

Swoop
Earned a small fee
Earned a small fee
Posts: 20
Joined: Wed Nov 01, 2006 10:22 pm
Location: Earth

Post by Swoop » Sat May 01, 2010 6:38 pm

Thanks for the suggestions.

m_textCtrl->SetFocus(); is a no go
wxTextCtrl::SetInsertionPoint(long pos) is a no go
event.Skip(); is a no go

I cant believe something so simply has no methods to accomplish this. Clear erases all 'X' chars before the cursor, yet the cursor remains at 'X' chars position, usually to accomplish this you would have to hold space to push the cursor to this position. It seems like a bug, whats keeping the cursor at the same position once the text before it is erased?

DavidHart
Site Admin
Site Admin
Posts: 3881
Joined: Thu Jan 12, 2006 6:23 pm
Location: IoW, UK

Post by DavidHart » Sat May 01, 2010 8:35 pm

Hi,
I cant believe something so simply has no methods to accomplish this.
I can't believe nobody else has noticed it before. Which platform/wx version are you using?

Can you demonstrate this problem in e.g. the 'text' sample (that comes with the wx source) or, failing that, create and post the code of a minimal compilable sample, so that we can try this at home?

Regards,

David

Swoop
Earned a small fee
Earned a small fee
Posts: 20
Joined: Wed Nov 01, 2006 10:22 pm
Location: Earth

Post by Swoop » Sun May 02, 2010 6:57 pm

I was running 2.8.10 when I made the original post, then I changed to 2.8.11, same thing. Im on Windows XP x64.

My example doesnt derive from wxTextCtrl, I call clear in OnTextEnter() { ctrl->Clear(); }

I reproduced it using the text sample, in text.cpp starting at line 807

Code: Select all

void MyTextCtrl::OnTextEnter(wxCommandEvent& event)
{
    if ( !ms_logText )
        return;

    MyTextCtrl *win = (MyTextCtrl *)event.GetEventObject();
    const wxChar *data = (const wxChar *)(win->GetClientData());
    if ( data )
    {
        wxLogMessage(_T("Enter pressed in control '%s'"), data);
    }
    else
    {
        wxLogMessage(_T("Enter pressed in some control"));
    }
    
    win->Clear();  // I added this, the text clears, the
    // cursor sits, just like in mine
}

DavidHart
Site Admin
Site Admin
Posts: 3881
Joined: Thu Jan 12, 2006 6:23 pm
Location: IoW, UK

Post by DavidHart » Sun May 02, 2010 7:48 pm

My example doesnt derive from wxTextCtrl
Huh?
I reproduced it using the text sample, in text.cpp starting at line 807
Hmm. The only textctrl instance in the sample that includes the wxTE_PROCESS_ENTER style-flag is a single-line textctrl, so I'm not sure how you managed to demonstrate the problem.

Adding wxTE_PROCESS_ENTER to several other textctrls, so that MyTextCtrl::OnTextEnter is called, does clear those textctrls, and the cursor is then correctly placed at the beginning of the control.

That's on wxGTK-2.8.11, so maybe you've found a MSWin issue.

Swoop
Earned a small fee
Earned a small fee
Posts: 20
Joined: Wed Nov 01, 2006 10:22 pm
Location: Earth

Post by Swoop » Sun May 02, 2010 8:57 pm

Well Im more confused then before now, I checked those text controls, MyTextCtrl from the sample has an event table entry for OnTextEnter(), but it doesnt seem to use the flag wxTE_PROCESS_ENTER for the multiline ctrls that go down the middle column of that sample window, yet I break inside OnTextEnter() for those multi line ctrls and it does fire, I clear like I said in the last post, the cursor stays, I guess its a bug, what else is new.

Post Reply