The wxWindows Cross-Platform Framework        
by Vadim Zeitlin


Listing One
// vCardFrame: the main application window
// ctor: create the frame and its child controls
vCardFrame::vCardFrame()
          : wxFrame(
                    NULL,                   // no parent
                    -1,                     // default id
                    "",                     // title (will be set later)
                    wxDefaultPosition,
                    wxSize(400, 250)
                   )
{
    // create the menu
    wxMenu *menuFile = new wxMenu("", wxMENU_TEAROFF);
    menuFile->Append(vCard_File_New, _("&New\tCtrl-N"), 
                                          _("Create new card file"));
    menuFile->Append(vCard_File_Open, _("&Open...\tCtrl-O"), 
                                          _("Open a card file"));
    menuFile->Append(vCard_File_Save, _("&Save\tCtrl-S"),
                                      _("Save the card file"));
    menuFile->AppendSeparator();
    menuFile->Append(vCard_File_Quit, _("E&xit\tAlt-X"), 
                                      _("Quit this program"));
    wxMenu *menuEdit = new wxMenu("", wxMENU_TEAROFF);
    menuEdit->Append(vCard_Edit_Add,  _("&Add card..."), 
                                      _("Add a new card to the file"));
    menuEdit->Append(vCard_Edit_Edit, _("&Edit card..."), 
                                      _("Edit the selected card"));
    menuEdit->Append(vCard_Edit_Delete, _("&Delete card"), 
                                       _("Delete the selected card"));
    wxMenu *menuHelp = new wxMenu("", wxMENU_TEAROFF);
    menuHelp->Append(vCard_Help_About, _("&About...\tF1"), 
                                       _("Show about dialog"));
    wxMenuBar *menuBar = new wxMenuBar;
    menuBar->Append(menuFile, _("&File"));
    menuBar->Append(menuEdit, _("&Edit"));
    menuBar->Append(menuHelp, _("&Help"));

Listing Two
BEGIN_EVENT_TABLE(vCardFrame, wxFrame)
            EVT_MENU(vCard_File_New,    vCardFrame::OnNewFile)
             ... 
            EVT_MENU(vCard_Help_About,  vCardFrame::OnAbout)
            EVT_UPDATE_UI_RANGE(vCard_Edit_Edit, vCard_Edit_Delete,
                                vCardFrame::OnUpdateEditMenu)
            EVT_LIST_ITEM_ACTIVATED(vCard_ListCtrl,
                                    vCardFrame::OnListItemActivated)
            EVT_CLOSE(vCardFrame::OnClose)
END_EVENT_TABLE()

Listing Three
void vCardFrame::OnUpdateEditMenu(wxUpdateUIEvent& event)
        {
            // edit and delete operations only make sense 
            //  if there is a selected card
            event.Enable( GetSelection() != -1 );
        }

Listing Four
// vCard editing dialog
class wxVCardDialog : public wxDialog
{
public:
    wxVCardDialog(wxWindow *parent, wxVCard *vcard);
    virtual ~wxVCardDialog() { delete m_imagelist; }
    virtual bool TransferDataToWindow();
    virtual bool TransferDataFromWindow();
protected:
    // event handlers
    void OnEmailAdd(wxCommandEvent&);
    void OnEmailModify(wxCommandEvent&);
    void OnEmailDelete(wxCommandEvent&);
    void OnAddrAdd(wxCommandEvent&);
    void OnAddrModify(wxCommandEvent&);
    void OnAddrDelete(wxCommandEvent&);
    void OnPhoneAdd(wxCommandEvent&);
    void OnPhoneModify(wxCommandEvent&);
    void OnPhoneDelete(wxCommandEvent&);
    void OnUpdateEMail(wxUpdateUIEvent&);
    void OnUpdateAddress(wxUpdateUIEvent&);
    void OnUpdatePhone(wxUpdateUIEvent&);
    // creates a vertical sizer containing the buttons used with listboxes:
    // add/modify/remove
    wxSizer *CreateLboxButtonsSizer(wxWindow *parent, int idFirstButton);

    // creates a horizontal box sizer containg a label and the control
    wxSizer *CreateLabelSizer(wxWindow *parent, const wxString& label,
                              wxControl *control, bool resizeable = FALSE);
    // returns the label in the lbox for this address/phone
    wxString GetAddressLabel(const wxVCardAddressData& data) const;
    wxString GetPhoneLabel(const wxVCardPhoneData& data) const;

    // adds an address/phone to the list
    void AddAddress(const wxVCardAddressData& data);
    void AddPhone(const wxVCardPhoneData& data);
private:
    // the GUI controls holding vCard data
    wxTextCtrl *m_fullname, *m_firstname, *m_lastname, *m_middlename,
               *m_prefixname, *m_suffixname, *m_orgname, *m_department,
               *m_title, *m_birthday, *m_comments, *m_url, *m_key;
    wxRadioBox *m_keyKind;
    wxListBox *m_emails, *m_addresses, *m_phones;
    wxImageButton *m_photo, *m_logo;
    wxImageList *m_imagelist;
    // the card we're editing
    wxVCard *m_vcard;

    // all addresses/phones
    wxVCardAddresses m_addrData;
    wxVCardPhones m_phoneData;

    bool m_emailDirty, m_addrDirty, m_phoneDirty;
    DECLARE_EVENT_TABLE()
};


Listing Five
// wxDateDialog and wxDateBrowseButton
wxDateDialog::wxDateDialog(wxWindow *parent, const wxString& title,
                           const wxDateTime& dt)
            : wxDialog(parent, -1, title)
{
    wxBoxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
    wxStaticBoxSizer *sizerAddr = new wxStaticBoxSizer
            ( new wxStaticBox(this, -1, _("&Choose date")), wxVERTICAL );
    m_calendar = new wxCalendarCtrl(this, -1);
    sizerAddr->Add(m_calendar, 1, wxGROW | wxALL, 5);
    sizerTop->Add(sizerAddr, 1, wxGROW | wxALL, 5);
    sizerTop->Add(CreateButtonSizer(wxOK | wxCANCEL), 0,
                  wxALIGN_RIGHT | (wxALL & ~wxRIGHT), 10);
    SetAutoLayout(TRUE);
    SetSizer(sizerTop);
    sizerTop->Fit(this);
    sizerTop->SetSizeHints(this);
    Centre();
}
void wxDateDialog::OnCalendar(wxCalendarEvent& event)
{
    EndModal(wxID_OK);
}
void wxDateBrowseButton::DoBrowse()
{
    wxString str = m_text->GetValue();
    wxDateTime dt;
    if ( str.length() )
        dt.ParseDate(str);

    wxDateDialog dialog(this, _("Select the birthday date"), dt);
    if ( dialog.ShowModal() == wxID_OK )
    {
        m_text->SetValue(dialog.GetDate().FormatISODate());
    }
}


Listing Six
from wxPython.wx import *
class MyFrame(wxFrame):
    def __init__(self):
        wxFrame.__init__(self, None, -1, "Hello Sample", size = (150, 100))
        self.Center()
        b = wxButton(self, -1, "Click Me")
        EVT_BUTTON(self, b.GetId(), self.OnClick)
    def OnClick(self, event):
        dlg = wxMessageDialog(self, "Hello World!")
        dlg.ShowModal()
        dlg.Destroy()
app = wxPySimpleApp()
frame = MyFrame()
frame.Show(true)
app.MainLoop()





1


