Toolbar button to append textctrl Topic is solved

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.
Post Reply
vielmaj
Earned a small fee
Earned a small fee
Posts: 10
Joined: Thu Feb 08, 2018 5:56 am

Toolbar button to append textctrl

Post by vielmaj »

I would like to be able to press a toolbar button and have some text output to a richtextctrl. Here is the minimal header and source file that includes the issue I am having. I purposely left out includes, header information, etc..

TestMain.h

Code: Select all

class MainPanel;
class MyTextCtrl;

class TestFrame: public wxFrame
{
    public:
        TestFrame(wxFrame *frame, const wxString& title);
        ~TestFrame();
    private:
        enum
        {
            idToolBarButton = 1000,
        };
        wxToolBar *m_toolBar;

        MainPanel *m_mainPanel;
        void AddPi(wxCommandEvent& event);
};

class MainPanel : public wxPanel
{
    public:
        MainPanel(wxWindow *parent,
                  wxWindowID id,
                  const wxPoint& pos,
                  const wxSize& size);

        virtual ~MainPanel();

        void AddPi();

    private:
        MyTextCtrl *m_textCtrl;
};

class MyTextCtrl : public wxRichTextCtrl
{
    public:
        MyTextCtrl(wxWindow* parent,
                 wxWindowID id,// = -1,
                 const wxString& value,// = wxEmptyString,
                 const wxPoint& pos,// = wxDefaultPosition,
                 const wxSize& size,// = wxDefaultSize,
                 long style,// = wxRE_MULTILINE,
                 const wxValidator& validator,// = wxDefaultValidator,
                 const wxString& name );// = wxTextCtrlNameStr););
        virtual ~MyTextCtrl();

        void AddPi();
};
TestMain.cpp

Code: Select all

////Test Frame//////////////////////////////
TestFrame::TestFrame(wxFrame *frame, const wxString& title)
    : wxFrame(frame, -1, title)
{
    wxImage::AddHandler(new wxPNGHandler);

    wxBitmap bitmap(wxT("Resource/toolbar.png"),wxBITMAP_TYPE_PNG);
    m_toolBar = CreateToolBar();
    m_toolBar->AddTool(idToolBarButton,wxT(""), bitmap);
    m_toolBar->Realize();

    m_mainPanel = new MainPanel(this, wxID_ANY, wxDefaultPosition, wxSize(300,300));

    m_toolBar->Connect(idToolBarButton, wxEVT_COMMAND_TOOL_CLICKED,
                       wxCommandEventHandler(TestFrame::AddPi));
}

TestFrame::~TestFrame()
{}

void TestFrame::AddPi(wxCommandEvent& event)
{
    m_mainPanel->AddPi();
}

////Main Panel//////////////////////////////
MainPanel::MainPanel(wxWindow *parent,
    wxWindowID id,
    const wxPoint& pos,
    const wxSize& size) :
    wxPanel(parent, id, pos, size)
{

    m_textCtrl = new MyTextCtrl(this,
                              wxID_ANY,
                              wxT(""),
                              wxDefaultPosition,
                              wxSize(300,300),
                              wxRE_MULTILINE,
                              wxDefaultValidator,
                              "tc");
}

MainPanel::~MainPanel()
{}

void MainPanel::AddPi()
{
    std::cout << "gets here" << std::endl;
    m_textCtrl->AddPi();
}

////Text Ctrl//////////////////////////////
MyTextCtrl::MyTextCtrl(wxWindow* parent,
                   wxWindowID id,
                   const wxString& value,
                   const wxPoint& pos,
                   const wxSize& size,
                   long style,
                   const wxValidator& validator,
                   const wxString& name ):
                   wxRichTextCtrl(parent, id, value, pos, size, style, validator, name)
{

}

MyTextCtrl::~MyTextCtrl()
{}

void MyTextCtrl::AddPi()
{
    std::cout << "but not here" << std::endl;
    AppendText(wxString("pi"));
}
As you can see, I have an event setup to descend down the child widgets with the pressing of the toolbar button to output "pi" to the richtextctrl. It gets to the std::cout statement in the MainPanel class function AddPi(), but it crashes when it gets to the AddPi() function in the MyTextCtrl class. Thank you for your help.

J
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: Toolbar button to append textctrl

Post by PB »

I think you ran into a common Connect bug - you did not specify the event sink so it was set to the caller, i.e., m_toolbar. However, the method is called for wxFrame instance. If it is as I say it is, change the connect call to

Code: Select all

m_toolBar->Connect(idToolBarButton, wxEVT_COMMAND_TOOL_CLICKED,
                       wxCommandEventHandler(TestFrame::AddPi), this);
But, I believe that a much better solution is to not use Connect() and use Bind() instead.
vielmaj
Earned a small fee
Earned a small fee
Posts: 10
Joined: Thu Feb 08, 2018 5:56 am

Re: Toolbar button to append textctrl

Post by vielmaj »

I got your Bind recommendation to work.

Code: Select all

Bind(wxEVT_COMMAND_TOOL_CLICKED, &TestFrame::AddPi, this, idToolBarButton);
Thank you.
Post Reply