## Thursday, August 22, 2013

### Cross-platform C++ programming with wxWidgets

wxWidgets is a C++ library for creating cross-platform applications. It enables developers to create GUI code to compile and run on several computer platforms such as Windows, OS X, Linux and UNIX with minimal or no code changes. It is free and open source software which satisfies those who wish to produce for GPL and proprietary software. Here, we would like to discuss our experience of using wxWidgets on Windows, Ubuntu Linux and Mac OSX Lion.
On Windows, wxDev-C++ is a good IDE to create wxWidgets applications. It has a nice book called Pragramming with wxDev-C++ so that you can learn to create wxWidgets applications easily. Another alternative IDE on Windows is CodeLite. There is an installer which includes codelite IDE + MinGW suite + wxWidgets SDK. It includes everything necessary for you to get started with wxWidgets.
If you are new to wxWidgets and if you want to try a few example applications, here is a good wxWidgets tutorial website.
In a Windows application, you can set an icon to your form. That icon file is usually included in xpm format. For example, if we want to use favicon.xpm, we can include it at the top of the cpp file of the form as follows.
#include "favicon.xpm"

Then, set the icon in the code as follows.
SetIcon(wxIcon(favicon_xpm));

An icon file in xpm format can be created using the image manipulation program, GIMP. After creating the xpm file, you might need to open it using a text editor program and change the path that can be found near the top of the file. In our case, deleting all the path in front and leaving only the file name worked well.
To change the icon of your program that shows in the Windows explorer, you need to edit the resource file with extension rc. For example, if you want to use favicon48.ico, you can edit the file by adding a line as follows.
aaaaAppIcon ICON "favicon48.ico"
#include <wx/msw/wx.rc>

The name of the icon in this case is aaaaAppIcon. The reason of having too many 'a' in front of the name is to make sure that it will be first when ordering icon files alphabetically.
On Ubuntu Linux, we tried Code::Blocks and found no problem creating wxWidgets applications using it.
On Mac OSX, installing wxWidgets is a bit tricky. We install Mac Ports first. Then the following command is used to install wxWidgets.
port search wxWidgets
sudo port -v install wxWidgets-3.0
wx-config

After that, we install CodeLite on Mac. To properly compile wxWidgets code, Compiler settings in CodeLite need to be changed. Go to Settings menu -> Build Settings command. When the Bulid Settings pop up windows appeared, select gnu g++ -> Tools. And change the PATH environment variable: field as follows.
/opt/local/bin:/usr/local/bin:/usr/bin:/bin

To produce the output of your project properly, right click on the project folder in the Workspace windows of the CodeLite IDE and click Make this project output a bundle command. Then, you can select both Debug and Release targets to bundle-ize. You can also define the icon file for your application there. The icon file must be icns format. To create an icon with icns extension, you can use a free version application that is available in the App store called Img2icns.

Related post: