Easily enable select all (CTRL+A), cut (CTRL+X), copy (CTRL+C), paste (CTRL+V) in a wxTextCtrl

Are you having trouble enabling the use of keyboard shortcuts CTRL+A, CTRL+X, CTRL+C, CTRL+V to select all, cut, copy, or paste text within a wxWidgets MSW wxTextCtrl? At least in wxWidgets 3.0.0, there is a quick and dirty style configuration that can do this without having to declare or connect to any events: make your wxTextCtrl use the wxTE_RICH style (wxTE_AUTO_URL also does the trick, and the wxTextCtrl documentation refers to wxTE_RICH). Note that this little article was written without knowledge of whether this is the designed and intended behavior of wxTextCtrl. The wxWidgets 3.0.0 documentation does indicate: "The following commands are processed by default event handlers in wxTextCtrl: wxID_CUT, wxID_COPY, wxID_PASTE, wxID_UNDO, wxID_REDO. The associated UI update events are also processed automatically, when the control has the focus." So, if you have a wxTextCtrl that doesn't process select all/cut/copy/paste in the expected manner by default, but does so when wxTE_RICH is used, is this a bug?  Maybe wxTE_RICH gives it focus somehow? In any case, the trade-off in using wxTE_RICH is, well, your control is now a rich-text control. Does this impact your application? Arguably in the majority of cases it won't :)  If use of wxTE_RICH is confirmed as the intended style, then this post may be updated to reflect that in the future. Please leave a comment if you know the answer and preferably add a supporting URL!



--- Start Sample Code ---


//-------------------------------------
// File: TextCtrlTestApp.h
//-------------------------------------
#ifndef TextCtrlTestApp_h
#define TextCtrlTestApp_h


#include <wx/app.h>

class TextCtrlTestApp : public wxApp
{

public:

    TextCtrlTestApp();
   
    virtual ~TextCtrlTestApp();
   
    virtual bool OnInit();
};

#endif // TextCtrlTestApp_h



//-------------------------------------
// File: TextCtrlTestApp.cpp
//-------------------------------------
#include "TextCtrlTestApp.h"
#include "TextCtrlTestMain.h"

IMPLEMENT_APP(TextCtrlTestApp);

TextCtrlTestApp::TextCtrlTestApp()
{
}
 
TextCtrlTestApp::~TextCtrlTestApp()
{
}

bool TextCtrlTestApp::OnInit()
{
      TextCtrlTestFrame* Frame = new TextCtrlTestFrame(0);
     
      Frame->Show();
     
      SetTopWindow(Frame);

    return true;
}

//-------------------------------------
// File: TextCtrlTestMain.h
//-------------------------------------
#ifndef TextCtrlTestMain_h
#define TextCtrlTestMain_h


#include <wx/frame.h>
#include <wx/sizer.h>
#include <wx/textctrl.h>


class TextCtrlTestFrame : public wxFrame
{
public:

    TextCtrlTestFrame::TextCtrlTestFrame(
        wxWindow* parent, wxWindowID = wxID_ANY)
    {   
        Create(parent, wxID_ANY,
            "Text Ctrl Test!",
            wxDefaultPosition, wxDefaultSize,
            wxDEFAULT_FRAME_STYLE|
            wxSYSTEM_MENU|wxRESIZE_BORDER|
            wxCLOSE_BOX|wxMAXIMIZE_BOX|
            wxMINIMIZE_BOX|wxTAB_TRAVERSAL,
            wxEmptyString);

        wxBoxSizer *pSizer = new wxBoxSizer(wxHORIZONTAL);

        wxTextCtrl *pTextCtrl =
            new wxTextCtrl( this, wxID_ANY,
            "Lorem ipsum dolor sit amet, consectetur "
            "adipiscing elit. Fusce sed nulla ac turpis "
            "aliquam luctus.",
            wxDefaultPosition,
            wxDefaultSize,
            wxTE_MULTILINE|wxTE_WORDWRAP|
            wxSIMPLE_BORDER|wxTAB_TRAVERSAL|
            wxVSCROLL|wxALWAYS_SHOW_SB|wxTE_RICH, //Try removing wxTE_RICH (v3.0.0)
            wxDefaultValidator,
            wxEmptyString );

        pSizer->Add( pTextCtrl, 1,
wxALL|wxEXPAND|wxALIGN_CENTER_VERTICAL, 0, 0 );

        SetSizer( pSizer );
        pSizer->Fit( this );
        pSizer->SetSizeHints( this );
    }


    virtual TextCtrlTestFrame::~TextCtrlTestFrame() {}

};

#endif // TextCtrlTestMain_h


--- End Sample Code ---

Permalink:
http://coderomp.blogspot.com/2014/03/wxTextCtrl-select-all-cut-copy-paste-ctrl-c.html

Post a Comment

What are your thoughts?