Listing 2: controls.cpp — Creates controls on a wxWindows panel

#include <wx/wxprec.h>
    
#ifdef __BORLANDC__
#pragma hdrstop
#endif
    
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif

#include <cstdlib>
using std::atof;

const int ID_panel  = 1;
const int ID_label  = 2;
const int ID_input  = 3;
const int ID_output = 4;
const int ID_FtoC   = 5;
const int ID_CtoF   = 6;


class MainFrame;

class SampleApp : public wxApp
{
private:
    virtual bool OnInit();
    MainFrame * frmMain;
};

IMPLEMENT_APP(SampleApp)


class MainFrame : public wxFrame
{
public:
    MainFrame (const wxString & title,
               const wxPoint & position,
               const wxSize & size);

    void OnFtoC (wxCommandEvent &);
    void OnCtoF (wxCommandEvent &);

private:
    wxPanel *       panel;
    wxStaticText *  label;
    wxTextCtrl *    input;      // input from user
    wxStaticText *  output;     // result
    wxButton *      FtoC;
    wxButton *      CtoF;

    DECLARE_EVENT_TABLE()
};


BEGIN_EVENT_TABLE (MainFrame, wxFrame)
    EVT_BUTTON (ID_FtoC, MainFrame::OnFtoC)
    EVT_BUTTON (ID_CtoF, MainFrame::OnCtoF)
END_EVENT_TABLE ()


MainFrame::MainFrame (const wxString & title,
                      const wxPoint & position,
                      const wxSize & size)
    : wxFrame (NULL, -1, title, position, size)
{
    panel = new wxPanel       (this, ID_panel, wxPoint(0,0), size);
    
    label = new wxStaticText  (panel, ID_label, "Temperature:",
                               wxPoint(20,20), wxDefaultSize);
    input = new wxTextCtrl    (panel, ID_input, "",
                               wxPoint(150,20), wxSize(80,20));
    output = new wxStaticText (panel, ID_output, "Result: ",
                               wxPoint(20,50), wxSize(150,20));
    FtoC = new wxButton       (panel, ID_FtoC, "F to C",
                               wxPoint(250,20), wxDefaultSize);
    CtoF = new wxButton       (panel, ID_CtoF, "C to F",
                               wxPoint(250,50), wxDefaultSize);
}

void MainFrame::OnFtoC (wxCommandEvent &)
{
    double Tf = atof(input->GetValue().c_str());
    wxString result;
    result << "Output: " << (Tf - 32) * 5 / 9;
        // wxString::operator<< facility is simlar 
        // to that of string stream objects -- no 
        // formatting facilities are provided.
        // However, wxString::Printf does provide 
        // formatting (precision, width, etc.)

    output->SetLabel (result);
}

void MainFrame::OnCtoF (wxCommandEvent &)
{
    double Tc = atof(input->GetValue().c_str());
    wxString result;
    result << "Output: " << 32 + Tc * 9 / 5;
    output->SetLabel (result);
}

bool SampleApp::OnInit()
{
    frmMain = new MainFrame ("Temperature Conversion",
                             wxPoint(20,20),
                             wxSize(350,90));
    frmMain->Show (true);
    SetTopWindow (frmMain);

    return true;
}
— End of Listing —