{"id":3995,"date":"2021-12-26T05:18:30","date_gmt":"2021-12-26T05:18:30","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=3995"},"modified":"2021-12-26T05:18:32","modified_gmt":"2021-12-26T05:18:32","slug":"6g-wxwidgets-using-bind-and-unbind","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2021\/12\/26\/6g-wxwidgets-using-bind-and-unbind\/","title":{"rendered":"6g. wxWidgets &#8211; Using Bind and Unbind"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>As mentioned before in a previous section, <em>Bind<\/em> is the preferred way to do event handling. The main advantage of <em>Bind<\/em> is that you can bind events at runtime. We look at an example of this in the code below. We use Bind and Unbind to attach 3 different event handlers to a single button , one at a time.<\/p>\n\n\n\n<p>We use an inbuilt function called CallAfter, which we will explore in later sections. The basic purpose of CallAfter is to execute a method when wxWidgets is idle. The reason why we dont call the RemoveHandler directly is because it is not a good idea to remove an event handler from a widget while it is still executing the event handler. Using CallAfter makes sure the event is unbound after it has finished executing.<\/p>\n\n\n\n<p><strong>SAMPLE CODE<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">bind-unbind.h\n<\/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    #include &lt;wx\/wx.h&gt;\n#endif\n\nclass BindUnbind: public wxFrame {\n\n    public:\n      BindUnbind(const wxString &amp;title);\n      void OnClick1(wxCommandEvent &amp;event);\n      void OnClick2(wxCommandEvent &amp;event);\n      void OnClick3(wxCommandEvent &amp;event);\n      void RemoveHandler(int remove, int add);\n    private:\n      wxButton *btn1;\n \n};\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">bind-unbind.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;bind-unbind.h&quot;\n\n\nBindUnbind::BindUnbind(const wxString &amp;title):\n\n    wxFrame(NULL, -1, title, wxDefaultPosition, wxSize(500,200)) {\n \n    wxPanel *panel = new wxPanel(this, -1);    \n    wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);\n    panel-&gt;SetSizer(hbox);\n\n    btn1 = new wxButton(panel, -1, wxT(&quot;click here&quot;));\n   \n    hbox-&gt;Add(btn1, 1, wxEXPAND | wxALL, 35);\n\n    btn1-&gt;Bind(wxEVT_BUTTON, &amp;BindUnbind::OnClick1, this);\n    Center();\n\n}\n\nvoid BindUnbind::OnClick1(wxCommandEvent &amp;event) {\n\tbtn1-&gt;SetLabel(wxT(&quot;in OnClick1&quot;));\n\tCallAfter(&amp;BindUnbind::RemoveHandler, 1, 2);\n\n}\n\nvoid BindUnbind::OnClick2(wxCommandEvent &amp;event) {\n\tbtn1-&gt;SetLabel(wxT(&quot;in OnClick2&quot;));\n\tCallAfter(&amp;BindUnbind::RemoveHandler, 2, 3);\n\n\n}\nvoid BindUnbind::OnClick3(wxCommandEvent &amp;event) {\n\tbtn1-&gt;SetLabel(wxT(&quot;in OnClick3&quot;));\n\tCallAfter(&amp;BindUnbind::RemoveHandler, 3, 1);\n\n}\n\nvoid BindUnbind::RemoveHandler(int remove, int add) {\n   if (remove == 1)\n\t     btn1-&gt;Unbind(wxEVT_BUTTON, &amp;BindUnbind::OnClick1, this);\n   else if (remove == 2)\n\t     btn1-&gt;Unbind(wxEVT_BUTTON, &amp;BindUnbind::OnClick2, this);\n   else if (remove == 3)\n\t     btn1-&gt;Unbind(wxEVT_BUTTON, &amp;BindUnbind::OnClick3, this);\n\n   if (add == 1)\n\t     btn1-&gt;Bind(wxEVT_BUTTON, &amp;BindUnbind::OnClick1, this);\n   else if (add == 2)\n\t     btn1-&gt;Bind(wxEVT_BUTTON, &amp;BindUnbind::OnClick2, this);\n   else if (add == 3)\n\t     btn1-&gt;Bind(wxEVT_BUTTON, &amp;BindUnbind::OnClick3, this);\n\n\n\n\n}\n\n\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: plain; title: ; notranslate\" title=\"\">\n#include &amp;lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n    #include &amp;lt;wx\/wx.h&gt;\n#endif\n\nclass Main: public wxApp {\n\n\tpublic:\n\t        virtual 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;bind-unbind.h&quot;\n \nIMPLEMENT_APP(Main)\n\nbool Main::OnInit() {\n\n    BindUnbind *app = new BindUnbind(wxT(&quot;Bind Unbind&quot;));\n    app-&gt;Show(true);\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-26-10-45-45.png\" alt=\"\" class=\"wp-image-3999\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-26-10-45-45.png 500w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-26-10-45-45-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-26-10-46-01.png\" alt=\"\" class=\"wp-image-4000\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-26-10-46-01.png 500w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-26-10-46-01-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-26-10-46-15.png\" alt=\"\" class=\"wp-image-4001\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-26-10-46-15.png 500w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-26-10-46-15-300x120.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW As mentioned before in a previous section, Bind is the preferred way to do event handling. The main advantage of Bind is that you <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2021\/12\/26\/6g-wxwidgets-using-bind-and-unbind\/\" title=\"6g. wxWidgets &#8211; Using Bind and Unbind\">[&#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-3995","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\/3995","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=3995"}],"version-history":[{"count":5,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3995\/revisions"}],"predecessor-version":[{"id":4003,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3995\/revisions\/4003"}],"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=3995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=3995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=3995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}