{"id":3974,"date":"2021-12-25T17:13:09","date_gmt":"2021-12-25T17:13:09","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=3974"},"modified":"2021-12-25T17:13:11","modified_gmt":"2021-12-25T17:13:11","slug":"6e-wxwidgets-vetoing-events","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2021\/12\/25\/6e-wxwidgets-vetoing-events\/","title":{"rendered":"6e. wxWidgets &#8211; Vetoing Events"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>Continuing from the previous section where we looked at event propagation, in this section we see how to cancel processing events. Event processing may be cancelled by either the <em>Veto()<\/em> method or S<em>kip(false) <\/em>method. Events derived from wxCommandEvent do not have a <em>Veto<\/em> method. They use the <em>Skip<\/em> method. Events which are not derived from wxCommandEvent have a <em>Veto<\/em> method(). If you dont specify the false argument in <em>Skip()<\/em> then the event is propagated.<\/p>\n\n\n\n<p>The sample code below uses <em>Skip<\/em> to veto command events and <em>Veto<\/em> to cancel a wxCloseEvent.<\/p>\n\n\n\n<p><strong>SAMPLE CODE<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">events-veto.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 EventsVeto: public wxFrame {\n\n    public:\n      EventsVeto(const wxString &amp;title);\n      void OnClose(wxCloseEvent &amp;event);\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<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">events-veto.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;events-veto.h&quot;\n\n\nEventsVeto::EventsVeto(const wxString &amp;title):\n\n    wxFrame(NULL, -1, title, wxDefaultPosition, wxSize(500,200)) {\n\n    this-&gt;Bind(wxEVT_CLOSE_WINDOW, &amp;EventsVeto::OnClose, this);\n\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;EventsVeto::OnClick, this);\n    panel11-&gt;Bind(wxEVT_COMMAND_BUTTON_CLICKED, &amp;EventsVeto::OnClick2, this);\n    panel1-&gt;Bind(wxEVT_COMMAND_BUTTON_CLICKED, &amp;EventsVeto::OnClick3, this);\n    this-&gt;Bind(wxEVT_COMMAND_BUTTON_CLICKED, &amp;EventsVeto::OnClick4, this);\n\n    Center();\n\n}\n\nvoid EventsVeto::OnClose(wxCloseEvent &amp;event) {\n     wxMessageDialog *dlg = new wxMessageDialog(NULL,\n     \t wxT(&quot;Are you sure you want to close?&quot;), wxT(&quot;Question&quot;), wxYES_NO);\n\n     int retVal = dlg-&gt;ShowModal();\n     dlg-&gt;Destroy();\n     if (retVal == wxID_NO) {\n       event.Veto();\n       return;\n     }\n     Destroy();\n\n}\n\nvoid EventsVeto::OnClick(wxCommandEvent &amp;event) {\n\n     wxMessageDialog *dlg = new wxMessageDialog(NULL,\n     \t wxT(&quot;Veto at the button level?&quot;), wxT(&quot;Question&quot;), wxYES_NO);\n\n     int retVal = dlg-&gt;ShowModal();\n     dlg-&gt;Destroy();\n     if (retVal == wxID_YES) {\n       event.Skip(false);\n       return;\n     }\n     wxString msg= &quot;Click detected in btn &quot;;\n     wxMessageBox(msg);\n\n     event.Skip();\n}\n\nvoid EventsVeto::OnClick2(wxCommandEvent &amp;event) {\n     wxMessageDialog *dlg = new wxMessageDialog(NULL,\n     \t wxT(&quot;Veto at the panel11 level?&quot;), wxT(&quot;Question&quot;), wxYES_NO);\n\n     int retVal = dlg-&gt;ShowModal();\n     dlg-&gt;Destroy();\n     if (retVal == wxID_YES) {\n       event.Skip(false);\n       return;\n     }\n\n\twxString msg= &quot;Click detected in panel11 &quot;;\n\twxMessageBox(msg);\n\tevent.Skip();\n\n}\n\nvoid EventsVeto::OnClick3(wxCommandEvent &amp;event) {\n     wxMessageDialog *dlg = new wxMessageDialog(NULL,\n     \t wxT(&quot;Veto at the panel1 level?&quot;), wxT(&quot;Question&quot;), wxYES_NO);\n\n     int retVal = dlg-&gt;ShowModal();\n     dlg-&gt;Destroy();\n     if (retVal == wxID_YES) {\n       event.Skip(false);\n       return;\n     }\n\n\twxString msg= &quot;Click detected in panel1&quot;;\n\twxMessageBox(msg);\n\tevent.Skip();\n\n}\n\nvoid EventsVeto::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-veto.h&quot;\n \nIMPLEMENT_APP(Main)\n\nbool Main::OnInit() {\n\n    EventsVeto *app = new EventsVeto(wxT(&quot;Events Veto&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-25-22-41-44.png\" alt=\"\" class=\"wp-image-3978\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-25-22-41-44.png 500w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-25-22-41-44-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=\"247\" height=\"166\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-25-22-42-01.png\" alt=\"\" class=\"wp-image-3979\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"292\" height=\"166\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-25-22-42-19.png\" alt=\"\" class=\"wp-image-3980\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW Continuing from the previous section where we looked at event propagation, in this section we see how to cancel processing events. Event processing may <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2021\/12\/25\/6e-wxwidgets-vetoing-events\/\" title=\"6e. wxWidgets &#8211; Vetoing Events\">[&#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-3974","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\/3974","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=3974"}],"version-history":[{"count":4,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3974\/revisions"}],"predecessor-version":[{"id":3981,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3974\/revisions\/3981"}],"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=3974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=3974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=3974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}