{"id":3961,"date":"2021-12-25T14:30:28","date_gmt":"2021-12-25T14:30:28","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=3961"},"modified":"2021-12-25T14:30:29","modified_gmt":"2021-12-25T14:30:29","slug":"6d-wxwidgets-event-propagation","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2021\/12\/25\/6d-wxwidgets-event-propagation\/","title":{"rendered":"6d. wxWidgets &#8211; Event Propagation"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>In the previous section we saw how events are generated and handled. Most events are derived from the wxCommandEvent class. There are some events which are not derived from wxCommandEvent like wxMouseEvent or wxClose. So we have two kinds of events &#8211; basic events and command events. Command events are propagated upwards within the GUI hierarchy, by default. For eg.if a button is contained with a panel and the panel is contained within a frame, then by default the button click will be detected by the button and then by the panel and then by the frame. But this propagation happens only if you have not added an event handler of your own to deal with the event. In case you have your own event handler but still want the event to propagate upwards then you can use <em>Event.Skip().<\/em><\/p>\n\n\n\n<p>The sample code below has the following GUI hierarchy: frame->panel1->panel11->button. The button has an event handler for a click event. We are then propagating this event up through the hierarchy by using <em>event.Skip().<\/em> It reaches the event handler of the top level window frame.<\/p>\n\n\n\n<p>You can try changing the code and by removing the <em>Skip()<\/em> method in any of the event handlers and check that the event propagation stops at that point.<\/p>\n\n\n\n<p><strong>SAMPLE CODE<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">events-propagate.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    #include &lt;wx\/wx.h&gt;\n#endif\n\nclass EventsPropagate: public wxFrame {\n\n    public:\n      EventsPropagate(const wxString &amp;title);\n      void OnClick(wxCommandEvent &amp;event);\n      void OnClick2(wxCommandEvent &amp;event);\n      void OnClick3(wxCommandEvent &amp;event);\n      void OnClick4(wxCommandEvent &amp;event);\n \n};\n\nconst int ID_PANEL_1 = 101;\nconst int ID_PANEL_1_1 = 102;\nconst int ID_BUTTON_1 = 200;\n\n\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">events-propagate.cpp\n<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;events-propagate.h&quot;\n\n\nEventsPropagate::EventsPropagate(const wxString &amp;title):\n\n    wxFrame(NULL, -1, title, wxDefaultPosition, wxSize(500,200)) {\n    wxPanel *basePanel = new wxPanel(this, -1);\n    wxBoxSizer *baseSizer = new wxBoxSizer(wxVERTICAL);\n    basePanel-&gt;SetSizer(baseSizer);\n\n    wxPanel *panel1 = new wxPanel(basePanel, ID_PANEL_1);\n    panel1-&gt;SetBackgroundColour(wxColour(100,200,100));\n    wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);\n    panel1-&gt;SetSizer(vbox);\n\n    wxPanel *panel11 = new wxPanel(panel1, ID_PANEL_1_1);\n    panel11-&gt;SetBackgroundColour(wxColour(50,180,45));\n    wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);\n    panel11-&gt;SetSizer(hbox);\n\n    wxButton *btn = new wxButton(panel11, ID_BUTTON_1, wxT(&quot;Click this button&quot;));\n    hbox-&gt;Add(btn, 0, wxALIGN_CENTER);\n\n    vbox-&gt;Add(panel11, 1, wxALL | wxEXPAND, 50);\n\n    baseSizer-&gt;Add(panel1, 1, wxALL | wxEXPAND,5);\n\n    btn-&gt;Bind(wxEVT_BUTTON, &amp;EventsPropagate::OnClick, this);\n    panel11-&gt;Bind(wxEVT_COMMAND_BUTTON_CLICKED, &amp;EventsPropagate::OnClick2, this);\n    panel1-&gt;Bind(wxEVT_COMMAND_BUTTON_CLICKED, &amp;EventsPropagate::OnClick3, this);\n    this-&gt;Bind(wxEVT_COMMAND_BUTTON_CLICKED, &amp;EventsPropagate::OnClick4, this);\n\n    Center();\n\n}\n\nvoid EventsPropagate::OnClick(wxCommandEvent &amp;event) {\n\twxString msg= &quot;Click detected in btn &quot;;\n\twxMessageBox(msg);\n\n        event.Skip();\n}\n\nvoid EventsPropagate::OnClick2(wxCommandEvent &amp;event) {\n\twxString msg= &quot;Click detected in panel11 &quot;;\n\twxMessageBox(msg);\n\tevent.Skip();\n\n}\n\nvoid EventsPropagate::OnClick3(wxCommandEvent &amp;event) {\n\twxString msg= &quot;Click detected in panel1&quot;;\n\twxMessageBox(msg);\n\tevent.Skip();\n\n}\n\nvoid EventsPropagate::OnClick4(wxCommandEvent &amp;event) {\n\twxString msg= &quot;Click detected in window frame &quot;;\n\twxMessageBox(msg);\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    #include &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;events-propagate.h&quot;\n \nIMPLEMENT_APP(Main)\n\nbool Main::OnInit() {\n\n    EventsPropagate *app = new EventsPropagate(wxT(&quot;Events Propagation&quot;));\n    app-&gt;Show(true);\n}\n<\/pre><\/div>\n\n\n<p>The output is given 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-25-19-57-10.png\" alt=\"\" class=\"wp-image-3969\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-25-19-57-10.png 500w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-25-19-57-10-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=\"254\" height=\"166\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-25-19-57-30.png\" alt=\"\" class=\"wp-image-3970\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"297\" height=\"166\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-25-19-58-30.png\" alt=\"\" class=\"wp-image-3971\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW In the previous section we saw how events are generated and handled. Most events are derived from the wxCommandEvent class. There are some events <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2021\/12\/25\/6d-wxwidgets-event-propagation\/\" title=\"6d. wxWidgets &#8211; Event Propagation\">[&#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-3961","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\/3961","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=3961"}],"version-history":[{"count":8,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3961\/revisions"}],"predecessor-version":[{"id":3972,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3961\/revisions\/3972"}],"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=3961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=3961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=3961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}