Fruit basket program in C++ with WxWidgets

  • From: Jamal Mazrui <empower@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Wed, 28 Nov 2007 14:04:02 -0500 (EST)

From the archive at
http://www.EmpowermentZone.com/cwx_fruit.zip

This fruit basket program is written in C++, using the open source
WxWidgets GUI library and application framework available from
http://WxWidgets.org

The program is compiled with the free Microsoft Visual C++ Express
Edition, available from
http://msdn.microsoft.com
Another download needed from there is the Windows Platform SDK.

This is my first nontrivial program in C++, so I encourage others to
suggest improvements to the code.  I am also interested in comparing
binaries produced by other free C++ compilers, including MinGW and Borland
C++ Builder, which should be able to compile the same code if properly
configured for Wx application development.

To aid comprehension, I have tried to make the code minimalist in nature.
It is contained in a single file, cwx_fruit.cpp (also pasted below), with
no custom include or resource files.  I have not yet learned how to
compile a Wx program by writing a make file or configuring Visual Studio,
but was able to use the make file that comes with the "Minimal" sample
program in the Wx distribution.  I simply replaced the minimal.cpp file
with the content of cwx_fruit.cpp, and then renamed the resulting
executable from minimal.exe to cwx_fruit.exe.

With Unicode and release compilation options, that executable was about
1.1 MB in size.  I reduced it by two thirds -- to about 373KB -- using the
free "Ultimate Packer for Executables" utility available from
http://upx.sourceforge.net

Besides the executable, a manifest file, under 1KB in size, is currently
needed in the same directory in order to run the program.  A .NET
Framework dependency might also exist, since this C++ compiler can produce
executables with such a dependency if proper parameters were not used (I
have not understood the make file enough to know).  I would appreciate
assistance in eliminating the need for a seperate cwx_fruit.exe.manifest
file, as well as ensuring that no .NET dependency exists.  I think this
compiler is capable of producing a self-standing Win32 executable, though
the file size may increase.

Jamal

/*
Content of cwx_fruit.cpp
Fruit basket program in C++ with WxWidgets 2.8
Compiled with Microsoft Visual C++ 2005 Express
Public domain by Jamal Mazrui
*/

// Import headers
#include "wx/wx.h"

// Derive custom dialog class
class FbDialog : public wxDialog {

public:
// Declare control variables
wxTextCtrl *txtFruit;
wxListBox *lstBasket;
wxButton *btnAdd, *btnDelete;

// Define add event handler
void OnAdd(wxCommandEvent& WXUNUSED(event)) {
wxString sValue = txtFruit->GetValue();
if (sValue == wxEmptyString) {
wxMessageBox(wxT("No fruit to add!"), wxT("Alert"));
}
else {
lstBasket->Append(sValue);
txtFruit->Clear();
int iFruit = lstBasket->GetCount() - 1;
lstBasket->SetSelection(iFruit);
}
} // OnAdd event handler

// Define delete event handler
void OnDelete(wxCommandEvent& WXUNUSED(event)) {
int iFruit = lstBasket->GetSelection();
if (iFruit == -1) {
wxMessageBox(wxT("No fruit to delete!"), wxT("Alert"));
}
else {
lstBasket->Delete(iFruit);
if (iFruit == lstBasket->GetCount()) {iFruit--;}
lstBasket->SetSelection(iFruit);
}
} // OnDelete event handler

// Define close event handler
void OnClose(wxCloseEvent& event) {
if (wxMessageBox(wxT("Exit program?"), wxT("Confirm"), wxYES_NO |
wxCANCEL) == wxYES) {
Destroy();
event.Skip();
}
else {
event.Veto();
}
} // OnClose event handler

// Define dialog constructor
//FbDialog::FbDialog() : wxDialog(NULL, wxID_ANY, wxT("Fruit Basket"),
wxDefaultPosition, wxSize(513, 176)) {
FbDialog() : wxDialog(NULL, wxID_ANY, wxT("Fruit Basket"),
wxDefaultPosition, wxSize(513, 176)) {
new wxStaticText(this, wxID_ANY, wxT("&Fruit:"), wxPoint(14, 14),
wxDefaultSize);
txtFruit = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxPoint(43, 14),
wxDefaultSize, wxTE_LEFT);

new wxStaticText(this, wxID_ANY, wxT("&Basket:"), wxPoint(251, 14),
wxDefaultSize);
lstBasket = new wxListBox(this, wxID_ANY, wxPoint(293,14), wxDefaultSize,
0);

btnAdd = new wxButton(this, wxID_ANY, wxT("&Add"), wxPoint(190, 121),
wxDefaultSize);
btnAdd->SetDefault();
Connect(btnAdd->GetId(), wxEVT_COMMAND_BUTTON_CLICKED,
wxCommandEventHandler(FbDialog::OnAdd));

btnDelete = new wxButton(this, wxID_ANY, wxT("&Delete"), wxPoint(217,
121), wxDefaultSize);
Connect(btnDelete->GetId(), wxEVT_COMMAND_BUTTON_CLICKED,
wxCommandEventHandler(FbDialog::OnDelete));

Connect(wxID_ANY, wxEVT_CLOSE_WINDOW,
wxCloseEventHandler(FbDialog::OnClose));

Centre();
ShowModal();
} // FbDialog constructor

}; // FbDialog class

// Derive custom application class
class FbApp : public wxApp {

public:
bool OnInit() {
new FbDialog();
return true;
} // Init method

}; // FbApp class

// Define main entry point via compiler macro
IMPLEMENT_APP(FbApp)

// End of cwx_fruit.cpp

__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

Other related posts: