{"id":3770,"date":"2021-12-12T05:28:25","date_gmt":"2021-12-12T05:28:25","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=3770"},"modified":"2022-02-17T08:53:57","modified_gmt":"2022-02-17T08:53:57","slug":"3-wxwidgets-windowing-basics","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2021\/12\/12\/3-wxwidgets-windowing-basics\/","title":{"rendered":"3.wxWidgets &#8211; Windowing Basics"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>Now we can actually start using wxWidgets for building GUI screens. The example code in this section will show how to create windows and panels and use of common controls like labels and buttons. We are not going to look at layout management as that is dealt with in later sections. The objective here is to get familiar with creating basic GUI applications.<\/p>\n\n\n\n<p>If you have followed the posts in the Introduction page on how to setup wxWidgets and run a sample program, then you now know how to build an application which has a single cpp source file. <\/p>\n\n\n\n<p>In later examples in this series, you will have to work with multiple cpp files. We first look at how to build an executable with multiple source files before we go into the windowing examples.<\/p>\n\n\n\n<p>We have two files simple.cpp which is the wxWidget GUI window and main.cpp which is the main wxApp that calls the Window object in simple.cpp. As per standard C++ practice , we define header files for each class.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>SAMPLE CODE<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">simple.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\nclass Simple: public wxFrame {\n\tpublic:\n\t\tSimple(const wxString&amp; title);\n};\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">simple.cpp\n<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;simple.h&quot;\n\nSimple::Simple(const wxString&amp; title):\n\twxFrame(NULL, -1, title, wxDefaultPosition, wxSize(700,300)) {\n\n\tCentre();\n}\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">main.h\n<\/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 MyApp: public wxApp {\n\tpublic:\n\t  virtual bool OnInit();\n};\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">main.cpp\n<\/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;simple.h&quot;\n\nIMPLEMENT_APP(MyApp)\n\nbool MyApp::OnInit() {\n\tSimple *simple = new Simple(wxT(&quot;Simple Window test&quot;));\n\tsimple-&gt;Show(true);\n\n\treturn \ttrue;\n}\n<\/pre><\/div>\n\n\n<p>To build the executable under Linux use the command<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">g++ main.cpp simple.cpp ``<code>wx-config --cxxflags --libs<\/code>`` -o simple<\/pre>\n\n\n\n<p>Use only single surrounding backticks in the command above and not the double backticks as shown .This will create an executable called simple which you can run via <em>.\/simple<\/em><\/p>\n\n\n\n<p>To build the executable under Windows , make the following changes to the sample makefile from the earlier post:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n# =========================================================================\n#     This makefile was generated by\n#     Bakefile 0.2.12 (http:\/\/www.bakefile.org)\n#     Do not modify, all changes will be overwritten!\n# =========================================================================\n\n\n\n\nprefix = \/mingw64\nexec_prefix = ${prefix}\ndatarootdir = ${prefix}\/share\nINSTALL = \/usr\/bin\/install -c\nEXEEXT = .exe\nWINDRES = windres\nNM = nm\nBK_DEPS = \/d\/wxWidgets\/build-debug\/bk-deps\nsrcdir = .\ntop_srcdir = ..\nLIBS = -lz -lrpcrt4 -loleaut32 -lole32 -luuid -llzma -luxtheme -lwinspool -lwinmm -lshell32 -lshlwapi -lcomctl32 -lcomdlg32 -ladvapi32 -lversion -lwsock32 -lgdi32 -loleacc -lwinhttp\nLDFLAGS_GUI = -mwindows\nCXX = g++\nCXXFLAGS = \nCPPFLAGS = \nLDFLAGS = \nUSE_DPI_AWARE_MANIFEST = 2\nWX_LIB_FLAVOUR = \nTOOLKIT = MSW\nTOOLKIT_LOWERCASE = msw\nTOOLKIT_VERSION = \nTOOLCHAIN_FULLNAME = msw-unicode-static-3.1\nEXTRALIBS =    -lz -lrpcrt4 -loleaut32 -lole32 -luuid -llzma -luxtheme -lwinspool -lwinmm -lshell32 -lshlwapi -lcomctl32 -lcomdlg32 -ladvapi32 -lversion -lwsock32 -lgdi32 -loleacc -lwinhttp \nEXTRALIBS_XML =  -lexpat\nEXTRALIBS_GUI = -llzma\nWX_CPPFLAGS = -I${wx_top_builddir}\/lib\/wx\/include\/msw-unicode-static-3.1 -I${top_srcdir}\/include -D_FILE_OFFSET_BITS=64 \nWX_CXXFLAGS = -Wall -Wundef -Wunused-parameter -Wno-ctor-dtor-privacy -Woverloaded-virtual -g -O0   \nWX_LDFLAGS =  \nHOST_SUFFIX = \nSAMPLES_RPATH_FLAG = \nSAMPLES_CXXFLAGS =  \nwx_top_builddir = D:\/wxWidgets\/build-debug\n\n### Variables: ###\n\nDESTDIR = \nWX_RELEASE = 3.1\nWX_VERSION = $(WX_RELEASE).5\nLIBDIRNAME = $(wx_top_builddir)\/lib\nMAINAPP_CXXFLAGS = $(WX_CPPFLAGS) -D__WX$(TOOLKIT)__ $(__WXUNIV_DEFINE_p) \\\n\t$(__DEBUG_DEFINE_p) $(__EXCEPTIONS_DEFINE_p) $(__RTTI_DEFINE_p) \\\n\t$(__THREAD_DEFINE_p) -I$(srcdir) $(__DLLFLAG_p) -I$(srcdir). \\\n\t$(WX_CXXFLAGS) $(CPPFLAGS) $(CXXFLAGS)\nMAINAPP_OBJECTS =  \\\n\t$(__MAINAPP___win32rc) \\\n\tMAINAPP_main.o \\\n\tMAINAPP_simple.o\n\n### Conditionally set variables: ###\n\n#CXXC = $(CXX)\nCXXC = $(BK_DEPS) $(CXX)\n#PORTNAME = base\nPORTNAME = $(TOOLKIT_LOWERCASE)$(TOOLKIT_VERSION)\n#WXBASEPORT = _carbon\n#WXDEBUGFLAG = d\nWXUNICODEFLAG = u\n#WXUNIVNAME = univ\nEXTRALIBS_FOR_BASE = $(EXTRALIBS)\n#EXTRALIBS_FOR_BASE = $(EXTRALIBS) \\\n#\t$(EXTRALIBS_XML) $(EXTRALIBS_GUI)\nEXTRALIBS_FOR_GUI = $(EXTRALIBS_GUI)\n#EXTRALIBS_FOR_GUI = \n#__WXUNIV_DEFINE_p = -D__WXUNIVERSAL__\n#__WXUNIV_DEFINE_p_1 = --define __WXUNIVERSAL__\n#__DEBUG_DEFINE_p = -DwxDEBUG_LEVEL=0\n#__DEBUG_DEFINE_p_1 = --define wxDEBUG_LEVEL=0\n#__EXCEPTIONS_DEFINE_p = -DwxNO_EXCEPTIONS\n#__EXCEPTIONS_DEFINE_p_1 = --define wxNO_EXCEPTIONS\n#__RTTI_DEFINE_p = -DwxNO_RTTI\n#__RTTI_DEFINE_p_1 = --define wxNO_RTTI\n#__THREAD_DEFINE_p = -DwxNO_THREADS\n#__THREAD_DEFINE_p_1 = --define wxNO_THREADS\n#__DLLFLAG_p = -DWXUSINGDLL\n#__DLLFLAG_p_1 = --define WXUSINGDLL\n__WIN32_DPI_MANIFEST_p = \\\n\t--define \\\n\twxUSE_DPI_AWARE_MANIFEST=$(USE_DPI_AWARE_MANIFEST)\nCOND_PLATFORM_OS2_1___MAINAPP___os2_emxbindcmd = $(NM) simplewindow$(EXEEXT) \\\n\t| if grep -q pmwin.763 ; then emxbind -ep simplewindow$(EXEEXT) ; fi\n#__MAINAPP___os2_emxbindcmd = $(COND_PLATFORM_OS2_1___MAINAPP___os2_emxbindcmd)\n__RCDEFDIR_p = --include-dir \\\n\t$(LIBDIRNAME)\/wx\/include\/$(TOOLCHAIN_FULLNAME)\n#__MAINAPP___win32rc = MAINAPP_MAINAPP_rc.o\n#__MAINAPP_app_Contents_PkgInfo___depname \\\n#\t= simplewindow.app\/Contents\/PkgInfo\n#__MAINAPP_bundle___depname = MAINAPP_bundle\n#____MAINAPP_BUNDLE_TGT_REF_DEP = \\\n#\t$(__MAINAPP_app_Contents_PkgInfo___depname)\n#____MAINAPP_BUNDLE_TGT_REF_DEP \\\n#\t= $(__MAINAPP_app_Contents_PkgInfo___depname)\n#____MAINAPP_BUNDLE_TGT_REF_DEP \\\n#\t= $(__MAINAPP_app_Contents_PkgInfo___depname)\n#____MAINAPP_BUNDLE_TGT_REF_DEP \\\n#\t= $(__MAINAPP_app_Contents_PkgInfo___depname)\n#____MAINAPP_BUNDLE_TGT_REF_DEP = \\\n#\t$(__MAINAPP_app_Contents_PkgInfo___depname)\nCOND_MONOLITHIC_0___WXLIB_CORE_p = \\\n\t-lwx_$(PORTNAME)$(WXUNIVNAME)$(WXUNICODEFLAG)$(WXDEBUGFLAG)$(WX_LIB_FLAVOUR)_core-$(WX_RELEASE)$(HOST_SUFFIX)\n__WXLIB_CORE_p = $(COND_MONOLITHIC_0___WXLIB_CORE_p)\nCOND_MONOLITHIC_0___WXLIB_BASE_p = \\\n\t-lwx_base$(WXBASEPORT)$(WXUNICODEFLAG)$(WXDEBUGFLAG)$(WX_LIB_FLAVOUR)-$(WX_RELEASE)$(HOST_SUFFIX)\n__WXLIB_BASE_p = $(COND_MONOLITHIC_0___WXLIB_BASE_p)\nCOND_MONOLITHIC_1___WXLIB_MONO_p = \\\n\t-lwx_$(PORTNAME)$(WXUNIVNAME)$(WXUNICODEFLAG)$(WXDEBUGFLAG)$(WX_LIB_FLAVOUR)-$(WX_RELEASE)$(HOST_SUFFIX)\n#__WXLIB_MONO_p = $(COND_MONOLITHIC_1___WXLIB_MONO_p)\n#__LIB_SCINTILLA_IF_MONO_p \\\n#\t= \\\n#\t-lwxscintilla$(WXDEBUGFLAG)$(WX_LIB_FLAVOUR)-$(WX_RELEASE)$(HOST_SUFFIX)\n__LIB_TIFF_p \\\n\t= \\\n\t-lwxtiff$(WXDEBUGFLAG)$(WX_LIB_FLAVOUR)-$(WX_RELEASE)$(HOST_SUFFIX)\n__LIB_JPEG_p \\\n\t= \\\n\t-lwxjpeg$(WXDEBUGFLAG)$(WX_LIB_FLAVOUR)-$(WX_RELEASE)$(HOST_SUFFIX)\n__LIB_PNG_p \\\n\t= \\\n\t-lwxpng$(WXDEBUGFLAG)$(WX_LIB_FLAVOUR)-$(WX_RELEASE)$(HOST_SUFFIX)\n#__LIB_ZLIB_p = \\\n#\t-lwxzlib$(WXDEBUGFLAG)$(WX_LIB_FLAVOUR)-$(WX_RELEASE)$(HOST_SUFFIX)\nCOND_wxUSE_REGEX_builtin___LIB_REGEX_p = \\\n\t-lwxregex$(WXUNICODEFLAG)$(WXDEBUGFLAG)$(WX_LIB_FLAVOUR)-$(WX_RELEASE)$(HOST_SUFFIX)\n__LIB_REGEX_p = $(COND_wxUSE_REGEX_builtin___LIB_REGEX_p)\n#__LIB_EXPAT_p = \\\n#\t-lwxexpat$(WXDEBUGFLAG)$(WX_LIB_FLAVOUR)-$(WX_RELEASE)$(HOST_SUFFIX)\n\n### Targets: ###\n\nall: mainapp$(EXEEXT) $(__MAINAPP_bundle___depname)\n\ninstall: \n\nuninstall: \n\ninstall-strip: install\n\nclean: \n\trm -rf .\/.deps .\/.pch\n\trm -f .\/*.o\n\trm -f mainapp$(EXEEXT)\n\trm -rf mainapp.app\n\ndistclean: clean\n\trm -f config.cache config.log config.status bk-deps bk-make-pch shared-ld-sh Makefile\n\nmainapp$(EXEEXT): $(MAINAPP_OBJECTS) \n\t$(CXX) -o $@ $(MAINAPP_OBJECTS)    -L$(LIBDIRNAME)  $(LDFLAGS_GUI) $(LDFLAGS)  $(WX_LDFLAGS) $(__WXLIB_CORE_p)  $(__WXLIB_BASE_p)  $(__WXLIB_MONO_p) $(__LIB_SCINTILLA_IF_MONO_p) $(__LIB_TIFF_p) $(__LIB_JPEG_p) $(__LIB_PNG_p)  $(EXTRALIBS_FOR_GUI) $(__LIB_REGEX_p) $(__LIB_EXPAT_p) $(EXTRALIBS_FOR_BASE) $(LIBS)\n\t$(__MAINAPP___os2_emxbindcmd)\n\n#simplewindow.app\/Contents\/PkgInfo: simplewindow$(EXEEXT) $(top_srcdir)\/src\/osx\/carbon\/Info.plist.in $(top_srcdir)\/src\/osx\/carbon\/wxmac.icns\n#\tmkdir -p simplewindow.app\/Contents\n#\tmkdir -p simplewindow.app\/Contents\/MacOS\n#\tmkdir -p simplewindow.app\/Contents\/Resources\n#\t\n#\t\n#\tsed -e &quot;s\/IDENTIFIER\/`echo $(srcdir) | sed -e &#039;s,\\.\\.\/,,g&#039; | sed -e &#039;s,\/,.,g&#039;`\/&quot; \\\n#\t-e &quot;s\/EXECUTABLE\/simplewindow\/&quot; \\\n#\t-e &quot;s\/VERSION\/$(WX_VERSION)\/&quot; \\\n#\t$(top_srcdir)\/src\/osx\/carbon\/Info.plist.in &gt;simplewindow.app\/Contents\/Info.plist\n#\t\n#\t\n#\t\/bin\/echo &quot;APPL????&quot; &gt;simplewindow.app\/Contents\/PkgInfo\n#\t\n#\t\n#\tln -f simplewindow$(EXEEXT) simplewindow.app\/Contents\/MacOS\/simplewindow\n#\t\n#\t\n#\tcp -f $(top_srcdir)\/src\/osx\/carbon\/wxmac.icns simplewindow.app\/Contents\/Resources\/wxmac.icns\n\n#MAINAPP_bundle: $(____MAINAPP_BUNDLE_TGT_REF_DEP)\n\n#\n#MAINAPP_MAINAPP_rc.o: $(srcdir)\/main.rc\n#\t$(WINDRES) -i$&lt; -o$@    --define __WX$(TOOLKIT)__ $(__WXUNIV_DEFINE_p_1) $(__DEBUG_DEFINE_p_1)  $(__EXCEPTIONS_DEFINE_p_1) $(__RTTI_DEFINE_p_1) $(__THREAD_DEFINE_p_1)  --include-dir $(srcdir) $(__DLLFLAG_p_1) $(__WIN32_DPI_MANIFEST_p) --include-dir $(srcdir). $(__RCDEFDIR_p) --include-dir $(top_srcdir)\/include\n\n\t\nMAINAPP_simple.o: $(srcdir)\/simple.cpp \n\t$(CXXC) -c -o $@ $(MAINAPP_CXXFLAGS) $(srcdir)\/simple.cpp\n\n\nMAINAPP_main.o: $(srcdir)\/main.cpp \n\t$(CXXC) -c -o $@ $(MAINAPP_CXXFLAGS) $(srcdir)\/main.cpp\n\n\n\n\n# Include dependency info, if present:\n-include .\/.deps\/*.d\n\n.PHONY: all install uninstall clean distclean MAINAPP_bundle\n\n<\/pre><\/div>\n\n\n<p>Run the makefile within MSYS2 MingW64-bit console by typing<\/p>\n\n\n\n<p><em>make<\/em><\/p>\n\n\n\n<p>On successful completion run the executable from within MSYS2 console with <em>.\/simple<\/em><\/p>\n\n\n\n<p>If you want to run it from outside the MSYS console, make sure you have the supporting dlls in place and double click simple.exe<\/p>\n\n\n\n<p>This is how the app will look under Ubuntu with a Dark theme:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"300\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-12-10-52-56.png\" alt=\"\" class=\"wp-image-3776\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-12-10-52-56.png 700w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-12-10-52-56-620x266.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2021\/12\/Screenshot-from-2021-12-12-10-52-56-300x129.png 300w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/figure>\n\n\n\n<p>The following topics are covered further in this section:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>3a. <a href=\"https:\/\/truelogic.org\/wordpress\/2021\/12\/13\/3a-wxwidgets-simple-window\/\">Simple Window <\/a><\/li><li>3b. <a href=\"https:\/\/truelogic.org\/wordpress\/2021\/12\/13\/3b-wxwidgets-application-icon\/\">Application Icon<\/a><\/li><li>3c.<a href=\"https:\/\/truelogic.org\/wordpress\/2021\/12\/13\/3c-wxwidgets-buttons-labels\/\"> Buttons &amp; Labels<\/a><\/li><li>3d.<a href=\"https:\/\/truelogic.org\/wordpress\/2021\/12\/13\/3d-wxwidgets-panels\/\"> Panels<\/a><\/li><li>3e  <a href=\"https:\/\/truelogic.org\/wordpress\/2021\/12\/13\/3e-wxwidgets-preview-to-event-handling\/\">Preview to Event Handling<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW Now we can actually start using wxWidgets for building GUI screens. The example code in this section will show how to create windows and <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2021\/12\/12\/3-wxwidgets-windowing-basics\/\" title=\"3.wxWidgets &#8211; Windowing Basics\">[&#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-3770","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\/3770","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=3770"}],"version-history":[{"count":12,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3770\/revisions"}],"predecessor-version":[{"id":4286,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3770\/revisions\/4286"}],"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=3770"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=3770"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=3770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}