{"id":3831,"date":"2021-12-14T09:01:06","date_gmt":"2021-12-14T09:01:06","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=3831"},"modified":"2021-12-14T09:01:07","modified_gmt":"2021-12-14T09:01:07","slug":"4b-wxwidgets-submenu","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2021\/12\/14\/4b-wxwidgets-submenu\/","title":{"rendered":"4b. wxWidgets &#8211; Submenu"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>In this section, we will add a submenu to the File menu from the previous section and we will put the event handler on the menubar object so that all the menu items will be handled within a single click handler.<\/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: 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#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\twxMenu *submenu;\n\n};\n\nconst int ID_NEW = 101;\nconst int ID_SAVE = 102;\nconst int ID_OPEN = 103;\n\nconst int ID_SUB_ONE = 201;\nconst int ID_SUB_TWO = 202;\nconst int ID_SUB_THREE = 203;\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\tsubmenu = new wxMenu();\n\tsubmenu-&gt;Append(ID_SUB_ONE, wxT(&quot;Menu item one&quot;));\n\tsubmenu-&gt;Append(ID_SUB_TWO, wxT(&quot;Menu item two&quot;));\n\tsubmenu-&gt;Append(ID_SUB_THREE, wxT(&quot;Menu item three&quot;));\n\n\tmnuFile-&gt;AppendSubMenu(submenu, wxT(&quot;&amp;Submenu&quot;));\n\t\n\tmnuFile-&gt;AppendSeparator();\n\tmnuFile-&gt;Append(wxID_EXIT, wxT(&quot;&amp;Quit&quot;));\n\n\tmb-&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 given below<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"504\" height=\"308\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/screen.png\" alt=\"\" class=\"wp-image-3834\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/screen.png 504w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/screen-300x183.png 300w\" sizes=\"auto, (max-width: 504px) 100vw, 504px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW In this section, we will add a submenu to the File menu from the previous section and we will put the event handler on <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2021\/12\/14\/4b-wxwidgets-submenu\/\" title=\"4b. wxWidgets &#8211; Submenu\">[&#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-3831","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\/3831","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=3831"}],"version-history":[{"count":3,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3831\/revisions"}],"predecessor-version":[{"id":3835,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3831\/revisions\/3835"}],"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=3831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=3831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=3831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}