{"id":3808,"date":"2021-12-13T07:30:13","date_gmt":"2021-12-13T07:30:13","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=3808"},"modified":"2021-12-13T07:30:14","modified_gmt":"2021-12-13T07:30:14","slug":"3e-wxwidgets-preview-to-event-handling","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2021\/12\/13\/3e-wxwidgets-preview-to-event-handling\/","title":{"rendered":"3e.wxWidgets- Preview to Event Handling"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>We take a first look at handling events in wxWidgets. The simplest event is a button click. There are 3 ways of handling events:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Using an Event Table<\/li><li>Using Connect()<\/li><li>Using Bind()<\/li><\/ul>\n\n\n\n<p>The first two methods are not recommended anymore and wxWidgets encourages using Bind() for all event handling as its simpler and safer . So all sample code in this series, will only use Bind().<\/p>\n\n\n\n<p>The sample code below, creates two buttons in two panels. Clicking on them will change the parent panel background color to red.<\/p>\n\n\n\n<p><strong>SAMPLE CODE<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">events.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 TestEvents: public wxFrame {\n\tpublic:\n\t\tTestEvents(const wxString&amp; title);\n\t\tvoid leftBtnClick(wxCommandEvent &amp;evt); \n\t\tvoid rightBtnClick(wxCommandEvent &amp;evt); \n\t\twxPanel *panLeft;\n\t\twxPanel *panRight; \n};\n\n\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">events.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &quot;events.h&quot;\n\nTestEvents::TestEvents(const wxString&amp;amp; title):\n\twxFrame(NULL, -1, title, wxDefaultPosition, wxSize(500,200)) {\n\n\tpanLeft = new wxPanel(this, -1, wxPoint(0,0), wxSize(250,100));\n\tpanRight = new wxPanel(this, -1, wxPoint(251,0), wxSize(250,100));\n\tpanLeft-&gt;SetBackgroundColour(wxColour(100,200,200));\n\tpanRight-&gt;SetBackgroundColour(wxColour(100,100,100));\n\n\twxButton *btn1 = new wxButton(panLeft,-1, wxT(&quot;Left Panel&quot;), wxPoint(5,5));\n\twxButton *btn2 = new wxButton(panRight,-1, wxT(&quot;Right Panel&quot;), wxPoint(5,5));\n\n\tbtn1-&gt;Bind(wxEVT_BUTTON, &amp;amp;TestEvents::leftBtnClick, this);\n\tbtn2-&gt;Bind(wxEVT_BUTTON, &amp;amp;TestEvents::rightBtnClick, this);\n\n\tSetMinSize(GetSize());\n\tSetMaxSize(GetSize());\n\tCenter();\n}\n\nvoid TestEvents::leftBtnClick(wxCommandEvent &amp;amp;evt) {\n\n\tpanLeft-&gt;SetBackgroundColour(wxColour(255,0,0));\t\n}\n\nvoid TestEvents::rightBtnClick(wxCommandEvent &amp;amp;evt) {\n\n\tpanRight-&gt;SetBackgroundColour(wxColour(255,0,0));\t\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: plain; title: ; notranslate\" title=\"\">\n#include &quot;main.h&quot;\n#include &quot;events.h&quot;\n\nIMPLEMENT_APP(Main)\n\nbool Main::OnInit() {\n\tTestEvents *app = new TestEvents(wxT(&quot;Test Events&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-13-12-57-14.png\" alt=\"\" class=\"wp-image-3817\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-13-12-57-14.png 500w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-13-12-57-14-300x120.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><figcaption>Before clicking buttons<\/figcaption><\/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-13-12-57-32.png\" alt=\"\" class=\"wp-image-3816\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-13-12-57-32.png 500w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-13-12-57-32-300x120.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><figcaption>After clicking buttons<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW We take a first look at handling events in wxWidgets. The simplest event is a button click. There are 3 ways of handling events: <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2021\/12\/13\/3e-wxwidgets-preview-to-event-handling\/\" title=\"3e.wxWidgets- Preview to Event Handling\">[&#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":[365],"tags":[],"class_list":["post-3808","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wxwidgets"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3808","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=3808"}],"version-history":[{"count":6,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3808\/revisions"}],"predecessor-version":[{"id":3818,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3808\/revisions\/3818"}],"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=3808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=3808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=3808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}