In the following code:
void Script::OnLeftUp( wxMouseEvent& event )
{
int currentPos = GetCurrentPos();
int wordStartPos = WordStartPosition( currentPos, true );
int wordEndPos=WordEndPosition(wordStartPos, true);
wxString identifier=GetRange(wordStartPos,wordEndPos);
SetSelection(wordStartPos, wordEndPos );
event.Skip();
}
When I click on a point inside a word (say for example the word is hello
, and I left click between e
and l
) the identifier is correctly identified as hello
. However, only he
is selected, whereas I would expect the whole word hello
to be selected. What could be going wrong? If the positions were wrong, then the value of the identifier should have been wrong, which is not the case.
By the way, I am using wxWidgets 3.1 on Windows 10.
What you want to do isn't exactly possible. In scintilla/wxStyledTextCtrl a selection always runs from somewhere to the current caret position. You're trying to make a selection that starts before the current position and ends after it, and that just can't be done.
Also this is basically the default behavior when you double click a word with the exception that the double click will move the caret to the end of the word. Do you really need to do this with a single click? If you really want to, you can use multiple selections to give the appearance of this however. Basically you will have one selection that runs from the start of the word to the current position, and a second selection from the current position until the end.
Put these commands somewhere that gets called before the mouse handler (probably in the constructor) and also declare a method with this prototype "void SelectCurWord();"
SetSelBackground(true, wxColour(192,192,192) );
SetSelForeground(true, wxColour(0,0,0) );
SetAdditionalSelBackground( wxColor(192,192,192) );
SetAdditionalSelForeground( wxColour(0,0,0) );
SetAdditionalCaretsVisible(false);
You can change the colors to whatever you want, but make sure the primary and additional selections use the same colors. The mouse handler should do something like this.
void Script::OnLeftUp( wxMouseEvent& event )
{
CallAfter(&Script::SelectCurWord);
event.Skip();
}
The reason we have to use the CallAfter is to let the event processing complete its work before trying to add selections. The SelectCurWord method is basically what you had before but uses multiple selections instead:
void Script::SelectCurWord()
{
int currentPos = GetCurrentPos();
int wordStartPos = WordStartPosition( currentPos, true );
int wordEndPos=WordEndPosition(wordStartPos, true);
AddSelection(wordStartPos,currentPos);
AddSelection(currentPos,wordEndPos);
}