wxTextCtrl currently doesn't support CTRL+A to select all text - this code provides support for it by deriving a new class myTextCtrl from wxTextCtrl which can be easily substituted for existing instances of wxTextCtrl to add the support.
The class handles the EVT_CHAR event, with OnCharEvent(), checks for the CTRL+A key code WXK_CONTROL_A, and selects all the text if appropriate. You could also add more features to the derived class if required, by adding to the OnCharEvent function.
To use it, just include the myTextCtrl.cpp and myTextCtrl.h files in your project, and include the header myTextCtrl.h in your code files where the class is used.
The attachment contains, in addition to the files for the derived class, a demo program Demo_text.cpp and a Code::Blocks project file for it myTextCtrlDemo.cbp that shows the class working for a single and a multiple line text control.
Test environment:
IDE: Code::Blocks Release 16.01 rev 10702 (2016-01-25 19:50:14) gcc 4.9.2 Windows/unicode - 32 bit
wxWidgets version: 3.0.2, Unicode build, DLL linked
OS: MS Windows 7 (64-bit)
Related posts:
Ctrl+A for wxtextctrl
viewtopic.php?t=20238
How to let the wxTextCtrl can select all...
viewtopic.php?t=20350
Select all text in a wxTextCtrl
viewtopic.php?t=35698
Derived class to add Ctrl+A functionality to any wxTextCtrl
Derived class to add Ctrl+A functionality to any wxTextCtrl
- Attachments
-
- myTextCtrlDemo.zip
- (6.75 KiB) Downloaded 312 times
Re: Derived class to add Ctrl+A functionality to any wxTextCtrl
Thanks for the contribution.
I think in the "error" case you should return from the function, otherwise the code continues with an uninitialized "pTextCtrl".
In general, i think the event handler can be simplified like this:
I think in the "error" case you should return from the function, otherwise the code continues with an uninitialized "pTextCtrl".
In general, i think the event handler can be simplified like this:
Code: Select all
void myTextCtrl::OnCharEvent(wxKeyEvent& event)
{
// Get ptr to control
wxTextCtrl* pTextCtrl = wxDynamicCast(event.GetEventObject(), wxTextCtrl);
if (pTextCtrl)
{
wxChar uc = event.GetUnicodeKey();
if ( uc == WXK_CONTROL_A) // User pressed CTRL+A in control
{
// Use "Select All" function (neatly avoiding issue of newline chars not being included in control contents)
pTextCtrl->SelectAll();
return;
}
}
event.Skip(); // allow event to be handled by normal handler
}
Use the source, Luke!
Re: Derived class to add Ctrl+A functionality to any wxTextCtrl
FWIW a wxTextCtrl using wxTE_RICH or wxTE_RICH2 already selects all text with Ctrl+A.
That being said, your control should only handle the char event when native selection is missing. You can do that by writing your own Create() function and Connect() / Bind() the char event handler if appropriate.
Another benefit of the dynamic handler is that you can connect it after calling wxTextCtrl::Create(), use GetId(), and make sure you don't need the dynamic cast in your handler (which IMO, in general, should only be used as a last resort).
Well, you don't really need that dynamic cast even now - where could other char events come from ?
Finally, if you want your control to be a drop-in replacement for other wxTextCtrl-s in a software, you should always skip the event in the char event handler. By not doing so you decide that a Ctrl+A char event cannot be handled by the rest of the app, and that may be interfering with already established event handling.
That being said, your control should only handle the char event when native selection is missing. You can do that by writing your own Create() function and Connect() / Bind() the char event handler if appropriate.
Another benefit of the dynamic handler is that you can connect it after calling wxTextCtrl::Create(), use GetId(), and make sure you don't need the dynamic cast in your handler (which IMO, in general, should only be used as a last resort).
Well, you don't really need that dynamic cast even now - where could other char events come from ?
Finally, if you want your control to be a drop-in replacement for other wxTextCtrl-s in a software, you should always skip the event in the char event handler. By not doing so you decide that a Ctrl+A char event cannot be handled by the rest of the app, and that may be interfering with already established event handling.
Re: Derived class to add Ctrl+A functionality to any wxTextCtrl
I agree. Fixes that reduce the line count have a particular elegance....In general, i think the event handler can be simplified like this:...
I have attached an updated zip.
- Attachments
-
- myTextCtrlDemo2.zip
- (6.75 KiB) Downloaded 287 times