{"id":4198,"date":"2022-01-26T15:53:00","date_gmt":"2022-01-26T15:53:00","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=4198"},"modified":"2022-01-26T15:53:01","modified_gmt":"2022-01-26T15:53:01","slug":"12-wxwidgets-creating-a-custom-widget","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2022\/01\/26\/12-wxwidgets-creating-a-custom-widget\/","title":{"rendered":"12.wxWidgets &#8211; Creating a Custom Widget"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>We now look at how to create a custom widget. A custom widget is something which adds in a custom UI which can be re-used in applications. In the sample code below we create a custom widget called CustomColor which extends a wxPanel. It acts as a color preview widget comprising of 3 wxSlider controls for red, green and blue colors. Each of the sliders can move between 0 and 255. Depending on the current value of the sliders, the background panel is painted with the resulting color.<\/p>\n\n\n\n<p><strong>SAMPLE CODE<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">custom.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\n\nclass CustomColor: public wxPanel {\n\n\t\tpublic:\n\t\t\tCustomColor(wxWindow *parent, wxWindowID id, int r, int g, int b);\n\t\t\tvoid setRed(int r);\n\t\t\tvoid setGreen(int g);\n\t\t\tvoid setBlue(int b);\n\t\t\tint getRed();\n\t\t\tint getGreen();\n\t\t\tint getBlue();\n\n\t\tprivate:\n\t\t   void onScroll(wxCommandEvent&amp; event);\t\n\t\t   void showColor();\t\n\t\t   wxSlider *mSliderRed, *mSliderGreen, *mSliderBlue;\n\t   \t   int mRed, mGreen, mBlue;\t   \n};\n\n#define SLIDER_ID_RED 2001\n#define SLIDER_ID_GREEN 2002\n#define SLIDER_ID_BLUE 2003\n\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">custom.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;custom.h&quot;\n\nCustomColor::CustomColor(wxWindow *parent, wxWindowID id, int r, int g, int b):\n\t\twxPanel(parent, id) {\n\t    \t\n\t\t\tmRed = r; mGreen = g; mBlue = b;\n\t\t\twxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);\n\t\t\tSetSizer(hbox);\n\t\t\tmSliderRed = new wxSlider(this ,SLIDER_ID_RED, mRed, 0, 255, wxDefaultPosition, wxDefaultSize, wxSL_VERTICAL);\n\t\t\tmSliderGreen = new wxSlider(this ,SLIDER_ID_GREEN, mGreen, 0, 255, wxDefaultPosition, wxDefaultSize, wxSL_VERTICAL);\n\t\t\tmSliderBlue = new wxSlider(this ,SLIDER_ID_BLUE, mBlue, 0, 255, wxDefaultPosition, wxDefaultSize, wxSL_VERTICAL);\n\n\t\t\thbox-&gt;Add(mSliderRed, 1, wxEXPAND | wxALL, 5);\n\t\t\thbox-&gt;Add(mSliderGreen, 1, wxEXPAND | wxALL, 5);\n\t\t\thbox-&gt;Add(mSliderBlue, 1, wxEXPAND | wxALL, 5);\n\n\t\t\tmSliderRed-&gt;Bind(wxEVT_COMMAND_SLIDER_UPDATED, &amp;CustomColor::onScroll, this);\n\t\t\tmSliderGreen-&gt;Bind(wxEVT_COMMAND_SLIDER_UPDATED, &amp;CustomColor::onScroll, this);\n\t\t\tmSliderBlue-&gt;Bind(wxEVT_COMMAND_SLIDER_UPDATED, &amp;CustomColor::onScroll, this);\n\n\t\t\tshowColor();\n }\n\nvoid CustomColor::onScroll(wxCommandEvent&amp; event) {\n   int id = event.GetId();\n   if (id == SLIDER_ID_RED)\n\t   mRed = mSliderRed-&gt;GetValue();\n   else if (id == SLIDER_ID_GREEN)\n\t   mGreen = mSliderGreen-&gt;GetValue();\n   else if (id == SLIDER_ID_BLUE)\n\t   mBlue = mSliderBlue-&gt;GetValue();\n   showColor();\n\n}\n\nvoid CustomColor::setRed(int r) { mRed = r;}\nvoid CustomColor::setGreen(int g) { mGreen = g;}\nvoid CustomColor::setBlue(int b) { mBlue = b;}\nint CustomColor::getRed() { return mRed;}\nint CustomColor::getGreen() { return mGreen;}\nint CustomColor::getBlue() { return mBlue;}\n\nvoid CustomColor::showColor() {\n SetBackgroundColour(wxColour(mRed, mGreen, mBlue));\n}\n\n\n\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">custommain.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 Main: public wxApp {\n\n\tpublic:\n\t\tvirtual bool OnInit();\n};\n\nclass CustomApp:public wxFrame {\n\tpublic:\n\t\tCustomApp(const wxString &amp;title);\n\t\n};\n\nCustomApp::CustomApp(const wxString &amp;title):\n\twxFrame(NULL,-1, title, wxDefaultPosition, wxSize(400,200)) {\n\t\n\t\tCenter();\n\t} \n\n\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">custommain.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;custommain.h&quot;\n#include &quot;custom.h&quot;\n\nIMPLEMENT_APP(Main)\n\nbool Main::OnInit() {\n\tCustomApp *app = new CustomApp(wxT(&quot;Custom Widget&quot;));\n\twxPanel *panel = new wxPanel(app, -1);\n\twxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);\n\tpanel-&gt;SetSizer(hbox);\n\twxStaticText *label = new wxStaticText(panel, -1, wxT(&quot;Custom Widget Text&quot;));\n\tCustomColor *color = new CustomColor(panel, -1, 100,10,200);\n\thbox-&gt;Add(color, 1, wxEXPAND | wxALL, 5);\n\thbox-&gt;Add(label, 2, wxEXPAND | wxALL, 5);\n\n\tapp-&gt;Show(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=\"400\" height=\"200\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-26-21-20-20.png\" alt=\"\" class=\"wp-image-4202\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-26-21-20-20.png 400w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-26-21-20-20-300x150.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"400\" height=\"200\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-26-21-20-38.png\" alt=\"\" class=\"wp-image-4203\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-26-21-20-38.png 400w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-26-21-20-38-300x150.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"400\" height=\"200\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-26-21-21-10.png\" alt=\"\" class=\"wp-image-4204\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-26-21-21-10.png 400w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-26-21-21-10-300x150.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW We now look at how to create a custom widget. A custom widget is something which adds in a custom UI which can be <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2022\/01\/26\/12-wxwidgets-creating-a-custom-widget\/\" title=\"12.wxWidgets &#8211; Creating a Custom Widget\">[&#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-4198","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\/4198","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=4198"}],"version-history":[{"count":4,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4198\/revisions"}],"predecessor-version":[{"id":4206,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4198\/revisions\/4206"}],"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=4198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=4198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=4198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}