{"id":3836,"date":"2021-12-14T11:37:06","date_gmt":"2021-12-14T11:37:06","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=3836"},"modified":"2021-12-14T11:37:07","modified_gmt":"2021-12-14T11:37:07","slug":"4c-wxwidgets-toolbar","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2021\/12\/14\/4c-wxwidgets-toolbar\/","title":{"rendered":"4c. wxWidgets &#8211; Toolbar"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>Toolbars are a band of icons, with or without text which span across the length or breadth of a window. They can contain controls like labels or dropdowns and they can be dockable. Here we look at how to make a single toolbar with icons and handle clicks on the icons. The icons are generated from PNG files. In the sample code, we let wxWidgets size the icons as required, but otherwise you can set the size of the icons also.<\/p>\n\n\n\n<p><strong>SAMPLE CODE<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">toolbars.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\nclass Toolbar: public wxFrame {\n\n\tpublic:\n\t\tToolbar(const wxString&amp; title);\n\t\tvoid OnToolItem(wxCommandEvent &amp;evt);\n\n};\n\nconst int ID_NEW = 101;\nconst int ID_SAVE = 102;\nconst int ID_OPEN = 103;\nconst int ID_EXPORT = 104;\n\n\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">toolbars.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;toolbars.h&quot;\n\nToolbar::Toolbar(const wxString&amp; title):\n\twxFrame(NULL, -1, title, wxDefaultPosition, wxSize(500,300)) {\n\n\tSetBackgroundColour(wxColour(255,100,200));\n\twxImage::AddHandler(new wxPNGHandler());\t\n\twxBitmap bmpAdd (wxT(&quot;bulb.png&quot;), wxBITMAP_TYPE_PNG);\n\twxBitmap bmpSave (wxT(&quot;folder.png&quot;), wxBITMAP_TYPE_PNG);\n\twxBitmap bmpOpen (wxT(&quot;edit.png&quot;), wxBITMAP_TYPE_PNG);\n\twxBitmap bmpExport (wxT(&quot;export.png&quot;), wxBITMAP_TYPE_PNG);\n\t\n\twxToolBar *tbr = CreateToolBar();\n\ttbr-&gt;AddTool(ID_NEW, wxT(&quot;Add&quot;), bmpAdd);\n\ttbr-&gt;AddTool(ID_SAVE, wxT(&quot;Save&quot;), bmpSave);\n\ttbr-&gt;AddTool(ID_OPEN, wxT(&quot;Open&quot;), bmpOpen);\n\ttbr-&gt;AddSeparator();\n\ttbr-&gt;AddTool(ID_EXPORT, wxT(&quot;Export&quot;), bmpExport);\n\n\ttbr-&gt;Realize();\n\n\ttbr-&gt;Bind(wxEVT_TOOL, &amp;Toolbar::OnToolItem, this);\n\n}\n\nvoid Toolbar::OnToolItem(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 item&quot;);\n\telse if (eventId == ID_SAVE )\n\t\twxLogMessage(&quot;Save file item&quot;);\n\telse if (eventId == ID_OPEN) \n\t\twxLogMessage(&quot;Open file item&quot;);\n\telse if (eventId == ID_EXPORT) \n\t\twxLogMessage(&quot;Export file item&quot;);\n\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=\"500\" height=\"300\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-14-17-04-02.png\" alt=\"\" class=\"wp-image-3838\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-14-17-04-02.png 500w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-14-17-04-02-300x180.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/figure>\n\n\n\n<p>The icons used here are provided below for download.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"48\" height=\"48\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/bulb.png\" alt=\"\" class=\"wp-image-3839\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"48\" height=\"48\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/edit.png\" alt=\"\" class=\"wp-image-3840\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"48\" height=\"48\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/export.png\" alt=\"\" class=\"wp-image-3841\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"48\" height=\"48\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/folder.png\" alt=\"\" class=\"wp-image-3842\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW Toolbars are a band of icons, with or without text which span across the length or breadth of a window. They can contain controls <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2021\/12\/14\/4c-wxwidgets-toolbar\/\" title=\"4c. wxWidgets &#8211; Toolbar\">[&#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],"tags":[],"class_list":["post-3836","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cc"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3836","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=3836"}],"version-history":[{"count":2,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3836\/revisions"}],"predecessor-version":[{"id":3843,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3836\/revisions\/3843"}],"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=3836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=3836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=3836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}