{"id":4242,"date":"2022-02-09T09:47:45","date_gmt":"2022-02-09T09:47:45","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=4242"},"modified":"2022-02-09T09:47:46","modified_gmt":"2022-02-09T09:47:46","slug":"13e-wxwidgets-persistent-objects","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2022\/02\/09\/13e-wxwidgets-persistent-objects\/","title":{"rendered":"13e. wxWidgets &#8211; Persistent Objects"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>Persistent objects are widgets in wxWidgets which have the ability to save and restore their visual state between different program invocations. For instance, if a window is moved or sized during a program run, then using persistence we can restore the window to the same state during subsequent runs.<\/p>\n\n\n\n<p>Only a few wxWidgets classes have this ability so far : top level windows like wxFrame, wxDialog and objects which are based on wxBookCtrlBase like wxNoteBook, wxListBook etc.<\/p>\n\n\n\n<p>Apart from the existing classes which support persistence, you can add persistence to your own custom wxWidget classes too.<\/p>\n\n\n\n<p>In the sample code below, we create a simple wxFrame and restore it back if it was saved. The three screenshots are for the first initial load, then moving and resizing it, exiting and running it a second time.<\/p>\n\n\n\n<p><strong>SAMPLE CODE<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">persistent.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 Persistent:public wxFrame {\n\n\tpublic:\n\t\tPersistent(const wxString &amp;title);\n};\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">persistent.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;persistent.h&quot;\n\n\nPersistent::Persistent(const wxString&amp; title):\n\twxFrame(NULL, -1, title, wxDefaultPosition, wxSize(500,200)) {\n        Center();\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\t#include &lt;wx\/wx.h&gt;\n#endif\n\nclass Main: public wxApp {\n\n\tpublic:\n\t\tvirtual 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;persistent.h&quot;\n#include &lt;wx\/persist.h&gt;\n#include &lt;wx\/persist\/toplevel.h&gt;\n\nIMPLEMENT_APP(Main)\n\nbool Main::OnInit() {\n\tPersistent *app = new Persistent(wxT(&quot;Persistent Objects&quot;));\n\tapp-&gt;SetName(wxT(&quot;testobject&quot;));\n\tif (!wxPersistenceManager::Get().RegisterAndRestore((wxFrame*) app))\n\t\tapp-&gt;Show(true);\n\telse\n\t\tapp-&gt;Show(true);\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=\"549\" height=\"406\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/1.png\" alt=\"\" class=\"wp-image-4247\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/1.png 549w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/1-300x222.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/1-80x60.png 80w\" sizes=\"auto, (max-width: 549px) 100vw, 549px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"869\" height=\"213\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-09-15-15-56.png\" alt=\"\" class=\"wp-image-4248\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-09-15-15-56.png 869w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-09-15-15-56-620x152.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-09-15-15-56-300x74.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-09-15-15-56-768x188.png 768w\" sizes=\"auto, (max-width: 869px) 100vw, 869px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"869\" height=\"213\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-09-15-16-12.png\" alt=\"\" class=\"wp-image-4249\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-09-15-16-12.png 869w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-09-15-16-12-620x152.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-09-15-16-12-300x74.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/02\/Screenshot-from-2022-02-09-15-16-12-768x188.png 768w\" sizes=\"auto, (max-width: 869px) 100vw, 869px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW Persistent objects are widgets in wxWidgets which have the ability to save and restore their visual state between different program invocations. For instance, if <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2022\/02\/09\/13e-wxwidgets-persistent-objects\/\" title=\"13e. wxWidgets &#8211; Persistent Objects\">[&#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-4242","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\/4242","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=4242"}],"version-history":[{"count":5,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4242\/revisions"}],"predecessor-version":[{"id":4250,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4242\/revisions\/4250"}],"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=4242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=4242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=4242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}