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.
In my current project I want to implement a 'find' feature within one of several a wxTextCtrl with the following code.
FWIW, the code makes sure the appropriate window is selected.
void MyFrame::DoFind( const wxString arc_ws2Find, wxTextCtrl* a_pTextCtrl )
{
long lStart;
long lFoundPos;
long Last;
bool bDown = true;
bool bMatchCase = false;
a_pTextCtrl->ShowPosition( 0 );
wxString wsContent = a_pTextCtrl->GetValue();
if ( !bMatchCase )
wsContent = wsContent.MakeLower();
if (bMatchCase )
lFoundPos = (bDown) ? wsContent.find(arc_ws2Find) : wsContent.rfind(arc_ws2Find);
else
lFoundPos = (bDown) ? wsContent.Lower().find(arc_ws2Find) : wsContent.Lower().rfind(arc_ws2Find);
if ( lFoundPos == wxNOT_FOUND )
return;
long lStartPos = (bDown) ? a_pTextCtrl->GetInsertionPoint() + lFoundPos : lFoundPos;
long lEndPos = lStartPos + arc_ws2Find.length();
a_pTextCtrl->SetSelection(lFoundPos, lFoundPos + arc_ws2Find.length());
a_pTextCtrl->ShowPosition( lFoundPos + arc_ws2Find.length() ); // <<<<<everything works up to here
// none of these show the selection
// a_pTextCtrl->Update();//SetFocus();//Layout();//Refresh();
// a_pTextCtrl->SendSizeEventToParent();
// a_pTextCtrl->LineDown();
a_pTextCtrl->Refresh();
::wxYield();
// a_pTextCtrl->FitInside();
// a_pTextCtrl->ShowPosition( lFoundPos + arc_ws2Find.length() );
}
The code works well enough, except it does not show the selection as highlighted until I switch to a different text ctrl ( within the same wxNotebook) and then back to the one of interest.
The big question: how can I make the selection appear without the extra switching. In fact, the only way I noticed that the selection had been done, was when I by chance ended up switching between the 2 windows
It gets curioser all the time.
When I strip all code following the SetSelection(), I can execute the search by clicking on the 'search 'button' - the magnifying glass - the control's text is positioned to make the line with the search string visible at the top of the window.
If I then right-click, I get a pop up context menu, and the selection shows.
If I add a call to a_pTextCtrl->SetFocus(); then, if I execute the search with pressing 'Enter' after the search string, it shows the line with the search string, and the selection is highlighted.
But if I click on the 'search' button, the search is executed, the expected line becomes the top line in the window, but the selection is NOT highlighted - unless I right-click in the window to pop up the context menu.
FWIW, it does not seem to matter whether I have handlers for wxEVT_COMMAND_TEXT_ENTER (I am using wxCrafter to set things up) and/or wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN, as long as I have one. Neither does it seem to matter whether I call event.Skip() after the call to search.