{"id":3940,"date":"2021-12-24T07:38:56","date_gmt":"2021-12-24T07:38:56","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=3940"},"modified":"2021-12-24T07:38:57","modified_gmt":"2021-12-24T07:38:57","slug":"6a-wxwidgets-using-event-tables","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2021\/12\/24\/6a-wxwidgets-using-event-tables\/","title":{"rendered":"6a. wxWidgets &#8211; Using Event Tables"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>We setup a simple GUI in this section and use an event table to handle various events. The event table handles the clicks on three buttons and the text change event in the textbox. We have used specific IDs to identify each control since its used to connect or identify the source object in the event handler.<\/p>\n\n\n\n<p><strong>SAMPLE CODE<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">events-table.h<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n\t#include &lt;wx\/wx.h&gt;\n#endif\n\n\nclass EventsTable: public wxFrame {\n\tpublic:\n\t\tEventsTable(const wxString&amp; title);\n\t\tvoid OnClick(wxCommandEvent &amp;event);\n\t\tvoid OnText(wxCommandEvent &amp;event);\n\n\tprivate:\n\t\twxStaticText *m_lbl;\t\n\t\twxTextCtrl *m_edit;\n\t\tDECLARE_EVENT_TABLE()\n};\n\nconst int ID_BTN_1 = 101;\nconst int ID_BTN_2 = 102;\nconst int ID_BTN_3 = 103;\nconst int ID_TXT_1 = 104;\n\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">events-table.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;events-table.h&quot;\n\nEventsTable::EventsTable(const wxString&amp; title):\n\twxFrame(NULL, -1, title, wxDefaultPosition, wxSize(500,200)) {\n\n\twxPanel *panelMain = new wxPanel(this, -1);\n\twxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);\n\tpanelMain-&gt;SetSizer(vbox);\n\t\n\twxPanel *panelBase = new wxPanel(panelMain, -1);\n\twxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);\n\n\tpanelBase-&gt;SetSizer(hbox);\n\n\twxButton *btn1 = new wxButton(panelBase, ID_BTN_1, wxT(&quot;Button 1&quot;));\n\twxButton *btn2 = new wxButton(panelBase, ID_BTN_2, wxT(&quot;Button 2&quot;));\n\twxButton *btn3 = new wxButton(panelBase, ID_BTN_3, wxT(&quot;Button 3&quot;));\n\n\tm_edit = new wxTextCtrl(panelBase, ID_TXT_1, wxT(&quot;&quot;));\n\n\thbox-&gt;Add(btn1, 1, wxALL,5);\n\thbox-&gt;Add(btn2, 1, wxALL,5);\n\thbox-&gt;Add(btn3, 1, wxALL,5);\n\thbox-&gt;Add(m_edit, 2, wxALL,5);\n\n\tvbox-&gt;Add(panelBase,1, wxALL,2);\n\n\tm_lbl = new wxStaticText(panelMain, -1, wxT(&quot;show event handling here&quot;));\n\tvbox-&gt;Add(m_lbl, 1, wxALL | wxALIGN_CENTER);\n\t\n\tCenter();\n}\n\nBEGIN_EVENT_TABLE(EventsTable, wxFrame)\n\tEVT_BUTTON(ID_BTN_1, EventsTable::OnClick)\n\tEVT_BUTTON(ID_BTN_2, EventsTable::OnClick)\n\tEVT_BUTTON(ID_BTN_3, EventsTable::OnClick)\n\tEVT_TEXT(ID_TXT_1,EventsTable::OnText)\nEND_EVENT_TABLE()\n\t\nvoid EventsTable::OnClick(wxCommandEvent &amp;event) {\n\t\n\twxString strId = wxString::Format(wxT(&quot;%d&quot;), event.GetId());\n\tstrId.Append(wxString(&quot; clicked&quot;));\n\tm_lbl-&gt;SetLabel(strId);\n}\n\nvoid EventsTable::OnText(wxCommandEvent &amp;event) {\n\t  m_lbl-&gt;SetLabel(m_edit-&gt;GetValue());\n\n}\n\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">main.h<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n\t#include &lt;wx\/wx.h&gt;\n#endif\n\nclass Main: public wxApp {\n\n\tpublic:\n\t\tvirtual bool OnInit();\n};\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">main.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;main.h&quot;\n#include &quot;events-table.h&quot;\n\nIMPLEMENT_APP(Main)\n\nbool Main::OnInit() {\n\tEventsTable *app = new EventsTable(wxT(&quot;Events Table&quot;));\n\tapp-&gt;Show(true);\n\n}\n\n<\/pre><\/div>\n\n\n<p>The output is shown below<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"200\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-24-13-06-43.png\" alt=\"\" class=\"wp-image-3942\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-24-13-06-43.png 500w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-24-13-06-43-300x120.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"200\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-24-13-07-02.png\" alt=\"\" class=\"wp-image-3943\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-24-13-07-02.png 500w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-24-13-07-02-300x120.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW We setup a simple GUI in this section and use an event table to handle various events. The event table handles the clicks on <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2021\/12\/24\/6a-wxwidgets-using-event-tables\/\" title=\"6a. wxWidgets &#8211; Using Event Tables\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":3595,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[302,365],"tags":[],"class_list":["post-3940","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cc","category-wxwidgets"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3940","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=3940"}],"version-history":[{"count":2,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3940\/revisions"}],"predecessor-version":[{"id":3944,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3940\/revisions\/3944"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/3595"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=3940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=3940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=3940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}