{"id":3826,"date":"2021-12-14T08:49:00","date_gmt":"2021-12-14T08:49:00","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=3826"},"modified":"2021-12-14T08:49:01","modified_gmt":"2021-12-14T08:49:01","slug":"4a-wxwidgets-menu-and-menuitems","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2021\/12\/14\/4a-wxwidgets-menu-and-menuitems\/","title":{"rendered":"4a. wxWidgets &#8211; Menu and MenuItems"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>We are going to create an application with a menubar containing menus. Each menu will contain some menu items and we will add an event handler to handle clicks on the menu items. We have added an event handler only to the File menu and not the Edit Menu in this case.<\/p>\n\n\n\n<p><strong>SAMPLE CODE<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">menus.h<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: clojure; 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#include &lt;wx\/menu.h&gt;\n\nclass Menus: public wxFrame {\n\n\tpublic:\n\t\tMenus(const wxString&amp; title);\n\t\tvoid OnMenuItem(wxCommandEvent &amp;evt);\n\n\t\twxMenuBar *mb;\n\t\twxMenu *mnuFile;\n\t\twxMenu *mnuEdit;\n\t\t\n};\n\nconst int ID_NEW = 101;\nconst int ID_SAVE = 102;\nconst int ID_OPEN = 103;\n\n\n\n\n\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">menus.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;menus.h&quot;\n\nMenus::Menus(const wxString&amp; title):\n\twxFrame(NULL, -1, title, wxDefaultPosition, wxSize(500,300)) {\n\n\tmb = new wxMenuBar();\n\tmnuFile = new wxMenu();\n\twxMenuItem *mniFileNew = new wxMenuItem(mnuFile, ID_NEW, wxT(&quot;&amp;New..&quot;));\n\n\tmnuFile-&gt;Append(mniFileNew);\n\tmnuFile-&gt;Append(ID_SAVE, wxT(&quot;&amp;Save..&quot;));\n\tmnuFile-&gt;Append(ID_OPEN, wxT(&quot;&amp;Open..&quot;));\n\tmnuFile-&gt;AppendSeparator();\n\n\t\n\tmnuFile-&gt;Append(wxID_EXIT, wxT(&quot;&amp;Quit&quot;));\n\n\tmnuFile-&gt;Bind(wxEVT_MENU, &amp;Menus::OnMenuItem, this);\n\n\tmnuEdit = new wxMenu();\n\tmnuEdit-&gt;Append(-1, wxT(&quot;Copy\\tCtrl-C&quot;));\n\tmnuEdit-&gt;Append(-1, wxT(&quot;Paste\\tCtrl-V&quot;));\n\tmnuEdit-&gt;Append(-1, wxT(&quot;Cut\\tCtrl-X&quot;));\n\n\tmb-&gt;Append(mnuFile, wxT(&quot;&amp;File&quot;));\n\tmb-&gt;Append(mnuEdit, wxT(&quot;&amp;Edit&quot;));\n\n\tSetMenuBar(mb);\n}\n\nvoid Menus::OnMenuItem(wxCommandEvent &amp;evt) {\n\n\tint eventId= evt.GetId();\n\twxString strId = wxT(&quot;&quot;);\n\tstrId = wxString::Format(wxT(&quot;%d&quot;), evt.GetId());\n\twxPuts(strId);\n\tif (eventId == ID_NEW) \n\t\twxLogMessage(&quot;New file menuitem&quot;);\n\telse if (eventId == ID_SAVE )\n\t\twxLogMessage(&quot;Save file menuitem&quot;);\n\telse if (eventId == ID_OPEN) \n\t\twxLogMessage(&quot;Open file menuitem&quot;);\n\telse if (eventId == wxID_EXIT)\n\t\tClose(true);\n}\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=\"298\" height=\"152\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-14-14-16-04.png\" alt=\"\" class=\"wp-image-3827\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"300\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-14-14-16-19.png\" alt=\"\" class=\"wp-image-3828\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-14-14-16-19.png 500w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-14-14-16-19-300x180.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW We are going to create an application with a menubar containing menus. Each menu will contain some menu items and we will add an <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2021\/12\/14\/4a-wxwidgets-menu-and-menuitems\/\" title=\"4a. wxWidgets &#8211; Menu and MenuItems\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3826","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3826","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=3826"}],"version-history":[{"count":2,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3826\/revisions"}],"predecessor-version":[{"id":3830,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3826\/revisions\/3830"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=3826"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=3826"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=3826"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}