Program Development with wxWidgets

advertisement
Developing wxWidgets Applications
with
Visual Studio 20101, 2, 3
and wxFormBuilder
Overview: Before getting started, you may want read some background material about
developing application with a graphical user interface (GUI). You can find such an overview at
http://people.cedarville.edu/Employee/kshomper/cs1220_web/lecture_notes/gui_intro.htm.
Using wxWidgets: wxWidgets is one of several GUI development libraries which allow
programmers to add a modern GUI interface to their C++ applications. However, before you can
use wxWidgets, it needs to be installed on your computer or laptop4. If wxWidgets is not
installed, follow the directions in the following paragraph; otherwise, skip ahead to the section
entitled Using Visual Studio.

1
Installation: To install wxWidgets on your laptop or desktop computer:
1. Copy the file wxPack_v2.8.12.01.exe from network drive
S:\DEPT\EG\Computer Science\wxWidgets to your computer.
2. Double-click on wxPack_v2.8.12.01.exe to install wxWidgets.
a. Click Run, if prompted.
b. Click Yes, if prompted.
c. Click Next.
d. Click I accept the agreement and click Next.
e. In the drop down box (where it says Full Installation) choose Visual C++
Only Runtime Version 10.05 and click Next.
f. Click Next to create a start menu folder.
g. By default, wxWidgets does not install in C:\Program Files. If you want
wxWidgets to install here, click the Browse button and select this folder6,
then click Next.
h. Click Install.
i. Click Finish.
The solution referenced in this document is for Visual Studio 2010. The solution discussed here will work equally
well for later editions of Visual Studio (however, see footnote 3). Notes for Visual Studio 2008 and earlier are at
http://people.cedarville.edu/Employee/kshomper/cs1220_web/developing_wxwidgets_with_visual_studio_and_wxf
ormbuilder_old.doc.
2
wxWidgets and wxFormBuilder has been installed on john. See the last page of this document for specific notes
for that platform.
3
Instructions for downloading Visual Studio 2010 Express to allow linking your VS2013 project code to wxWidgets
appear on the next to last page of this document.
4
wxWidgets has already been installed on all CS lab computers. You can confirm installation by verifying the
existence of C:\Program Files\wxWidgets2.8.
5
If you choose, you can install a full version. However, this choice is only useful if you compile C++ program
using the MinGW compiler (e.g, for developers using Eclipse rather than Visual Studio).
6
Windows7 users may wish to choose “Program Files (x86)” for 32-bit applications.
Using Visual Studio: Although wxWidgets is a multi-platform widget library, for this project
we will use it within the Windows family of operating systems (XP and Windows7, these notes
have not been tested for Windows8). Therefore, we will also be using Visual Studio as our
development environment (tested with both Visual Studio 2010, 2012 and 2013). Visual Studio
provides good integration with wxWidgets, if you’ve installed wxWidgets as specified above7.
 The First wxWidgets Project: To get started with your wxWidgets application, copy
the FormBuilderApps folder from S:\DEPT\EG\Computer Science\CS1220 to a folder of
your own choosing.
1. Now, with the open the above destination folder opened in Windows Explorer,
double click on the file FormBuilderApps.sln.
2. You should now see a new Visual Studio 2010 C++ project called wxWidgetsApp
with five folders: External Dependencies, Header Files, Resource Files, Source
Files, and wxFormBuilder Files.
3. If you are using Visual Studio 2012 or 2013 and are prompted to update the
project, go ahead and click OK, and see the next to last page in this document for
additional instructions.
7

Project Folder Structure: As noted in the previous paragraph, there are five folders
associated with the Visual Studio wxWidgets project (although you only really care about
the first three folders listed below). The five folders are:
1. wxFormBuilder Files: The wxFormBuilder files are where we keep the
wxFormBuilder project file (not yet created) which defines the GUI and the GUI
class files which wxFormBuilder will create automatically.
2. Source Files: It is in this folder we keep the rest of the project source files which
are not automatically generated by wxFormBuilder. These source files are the
template implementation for the application class, wxWidgetsApp.cpp, and the
files (not yet created) which implement the event handlers.
3. Header Files: In this folder we keep the rest of the project header files which are
not automatically generated by wxFormBuilder. These header files are the
template specification for the application class, wxWidgetsApp.h, and the files
(not yet created) which specify the event handlers.
4. Resource Files: Our project will not use any resources. This folder will remain
empty.
5. External Dependencies: Our project will set all necessary dependencies
automatically on compilation; therefore, you can safely ignore this folder.

The Application Class Files: The files wxWidgetsApp.cpp and wxWidgetsApp.h which
appear in the Source and Header Files folders, respectively, are the template application
files. We’ll have more to say about these later.
These notes have been tested on Windows XP, Vista and Windows7 with Visual Studio 2008, 2010, and 2013.

Building the GUI: In this step you will use wxFormBuilder to create the GUI for your
application. Detailed instructions for building the GUI are beyond the scope of this
document. However, below you’ll find a brief Getting Started which lists the things to
keep in mind as you build the GUI. Also, during one of your CS1220 class periods, your
instructor will demonstrate building a GUI using wxFormBuilder.
1. Getting Started with wxFormBuilder: Begin by starting wxFormBuilder (e.g.,
Start … All Programs … wxPack … wxFormBuilder … wxFormBuilder).
 First, because we’ll be using wxFormBuilder to generate the GUI class
code, we want to set a few project properties immediately, so the code is
generated in the same folder as our application class. So as soon as
wxFormBuilder is ready, set the following properties (the property sheet
appears on the right):
1. name: Set this value to “Sub.”
2. file: This property’s value determines the file name of the
generated code. Since we are generating code for the GUI Class,
an appropriate value here would be “GUIClass.”
 Next, save the project to the wxWidgetsApp sub-folder in the Visual Studio
2010 wxWidgetsApp solution you copied in the step The First wxWidgets
Project above8.
 Finally, we will create the main frame (top-level window) for the GUI
class to test the GUI class code generation. Do the following:
1. Select the Forms tab.
2. Click on the first icon (it looks like a tiny application window)
appearing below the tabs. This will cause a top-level window to
appear in the center of the screen.
3. Change the name property to GUIClass.
4. Click the gear-shaped icon in the tool bar, or (or select File …
Generate Code, or press the F8 key) to generate the GUI class.
5. Select Tools … Generate Inherited Class (or press the F6 key),
select the checkbox, and press OK to generate a subclass for the
GUI class.
If after following the above steps, you have .h and .cpp files for the application
class (called wxWidgetsApp.h/cpp), the GUI class (GUIClass.h/cpp), and a
subclass of the GUI class (SubGUIClass.h/cpp) in your Visual Studio
wxWidgetsApp project folder, then your wxFormBuilder project is set up
correctly.
8
At this time, wxFormBuilder is limited to generating code into local folders (e.g., the C: drive). Therefore, if you
want to move your project from computer to computer, we suggest you write it to a USB drive or network drive.
However, development must be done locally.

Adding the wxFormBuilder Files to the Visual Studio Project: After generating the
GUI class files we need add this code to the Visual Studio project. Add the code as
follows:
 From the Visual Studio Solution Explorer, right-click on the wxFormBuilder Files
folder, choose Add … Existing Item, and select all files with “GUIClass”
appearing in their name.

Important Notes Regarding the GUI Subclass Files: The code for the GUIclass (i.e.,
GUIClass.h/cpp) determines how the GUI is drawn. Since we regenerate the GUIclass
each time we make changes to the GUI with wxFormBuilder, we do not want to make
custom changes to the GUIclass event handlers, because those changes will be
overwritten when the GUIclass is regenerated. Rather, wxFormBuilder encourages
developers to subclass the GUIclass and override the event handlers in the subclass.
Above, we discussed how to automatically create a GUI subclass (e.g., select Tools …
Generate Inherited Class or press the F6 key which created the files
SubGUIClass.h/cpp). Once you begin to edit these sub-classed files, you should not
automatically regenerate them, or you will overwrite your custom changes.

Making the Application Class Reference the GUI Subclass: The last step in building
the project is to make the application class, wxWidgetsApp, reference the GUI subclass.
We do this by modifying the source file wxWidgetsApp.cpp. Open wxWidgetsApp.cpp in
Visual Studio and edit the defined values for FILENAME and CLASSNAME (they
appear lines 10 and 11, respectively) to refer to the GUI subclass file (if you’ve followed
this directions carefully, that value should be “SubGUIClass.h”) and GUI subclass class
name (i.e., SubGUIClass).

Testing the Project: To test the project in Visual Studio select Debug … Start Without
Debugging or press CTRL-F5. If the project compiles, links, and displays a dialog that
looks similar to the GUI you designed in wxFormBuilder, then you are ready to begin
adding additional widgets to your top-level form and custom event handlers.
Additional Help: Assuming that wxWidgets was installed as specified above, the following
applications and help are available from the Windows Start menu.

Start … All Programs … wxPack … wxWidgets Compiled … wxWidgets Help:
Comprehensive help documentation for using the wxWidgets library.

Start … All Programs … wxPack … wxFormBuilder: Access to the wxFormBuilder
application.
Compile/Link a Visual Studio 2012 or 2013 Project w/wxWidgets
Issue: The version of wxWidgets on the S: drive has been compiled with Visual Studio 2010
and not yet updated for Visual Studio 2013 (VS2013). Therefore VS2012 or VS2013 users,
upon linking your project with the wxWidgets libraries, will see errors of the following type:
I'm trying to figure out how to get the wxFormBuilder code to compile. I'm using Visual
Studio 2012. When I tried to compile the first time, Visual Studio told me to update the
project to be compatible with Visual Studio 2012. I did this but when I tried to compile
again, I got this error:
1>GUIClass.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to
'/SAFESEH' specification
1>wxbase28d.lib(baselib_appbase.obj) : error LNK2038: mismatch detected for
'_MSC_VER': value '1600' doesn't match value '1700' in GUIClass.obj
1>wxbase28d.lib(baselib_string.obj) : error LNK2038: mismatch detected for
'_MSC_VER': value '1600' doesn't match value '1700' in GUIClass.obj
To correct this problem follow the directions in the “Solution” paragraph below.
Solution: The solution to this problem is to install Visual Studio 2010 Express (located here).
Take care--there are five choices under the Visual Studio 2010 Express heading. You want the
first one: Visual C++ 2010 Express.
Within the version of Visual Studio you are using (either 2012 or 2013), make sure the
wxWidgetsApp project is loaded and then choose Project ... wxWidgetApp Properties from the
menu (or alternatively press ALT-F7) to bring up the project properties dialog.
On the left-hand side of the window, if necessary, expand the Configuration Properties drop
down arrow and click on General (note, there are several General items, you want the one that is
immediately below the word Configuration Properties).
With General selected, you'll now see a Platform Toolset property on the right--it's about the
seventh property listed on the right. That property should be set to Visual Studio 2010 (v100). If
it is not, click on the property and select v100 from the dropdown menu that appears.
After changing the Platform Toolset, you should no longer see the referenced link error. Please
note, if you have any other errors, which you cannot resolve, please contact your instructor.
Developing wxWidgets Applications under Ubuntu Linux
Using wxWidgets: wxWidgets and wxFormBuilder are already installed under linux, so no
installation is required. However, because they display a GUI, it is necessary to have XMing
running locally on your PC or laptop and to have PuTTY properly configured for X11.
Instructions for doing this can be found on the CS1210 web site in paragraphs on “Using
PuTTY” and “Using XMing.”
Developing Your C++ Code: You have complete flexibility over the structure of your project
in the linux environment whether you use Chameleon, PuTTY with vi, or gedit to create your
program source. However, I recommend you use a new directory for your project source code.
Begin your project by copying the wxWidgetsApp.h and wxWidgetsApp.cpp files to your project
directory. These files are available by hyperlink or from ~shomperk/CS1220/public/wxWidgets.
To develop your GUI, follow the notes on Building the GUI on page three of this document.
Incidentally, to start wxFormBuilder on john, simply type: “wxformbuilder &” Also, be also
sure to read the paragraphs Important Notes Regarding the GUI Subclass Files and Making
the Application Class Reference the GUI Subclass on page four.
To build your wxWidgets application, append the following to your g++ command line:
`wx-config --cxxflags` `wx-config --libs`
Be careful to note that the text above MUST be APPENDED to the command line and that the
appended text is encased in ` ` (i.e., back quote) characters and these characters are significant
(i.e., you must type them exactly as they appear).9
Once you’ve successfully built your application, you can run it, like any other linux application,
by typing the application name at the linux shell prompt and pressing <ENTER>.
Windows versus Linux Incompatibilities: There are some differences between the code
developed in Visual Studio and that developed under linux. These differences are:
1. String literals, e.g., "like this" are NOT converted automatically on the linux platform.
Use the macro wxT("like this") to convert them. This macro also exists as _T().
2. The wxMessageBox() macro is not automatically included. We need to explicitly include
the header file wx/msgdlg.h, e.g. #include “wx/msgdlg.h”
3. Some functions, such as _iota(), are only on one platform or the other, so we need to be
aware of these platform dependencies.
4. This last point is not required, but makes the code cleaner. Windows uses \n\r to
terminate lines and linux just uses \n. So clean up the extra \r characters using the vi
editor command :1,$s/<CTRL>-V<ENTER>//g
5. The wxFormBuilder application is newer on Windows than on linux. Therefore, you
cannot edit a Windows .fbp file on the linux system.
You cannot use Chameleon’s compile button to build your wxWidgets application, because Chameleon’s compile
command does not include the additional compile parameters. Therefore, you must type the compile command in
Chameleon’s terminal window or in PuTTY.
9
Download