|
These are the header and implementation files for the PersonalRecordDialog class described in Chapter 9, "Creating Custom Dialogs." It can be found in examples/chap09 on the CD-ROM. Listing J-1. Header File for PersonalRecordDialog//////////////////////////////////////////////////////////////////// // Name: personalrecord.h // Purpose: Dialog to get name, age, sex, and voting preference // Author: Julian Smart // Created: 02/28/04 06:52:49 // Copyright: (c) 2004, Julian Smart // Licence: wxWindows license //////////////////////////////////////////////////////////////////// #ifndef _PERSONALRECORD_H_ #define _PERSONALRECORD_H_ #ifdef __GNUG__ #pragma interface "personalrecord.cpp" #endif /*! * Includes */ #include "wx/spinctrl.h" #include "wx/statline.h" /*! * Control identifiers */ enum { ID_PERSONAL_RECORD = 10000, ID_NAME = 10001, ID_AGE = 10002, ID_SEX = 10003, ID_VOTE = 10006, ID_RESET = 10004 }; /*! * PersonalRecordDialog class declaration */ class PersonalRecordDialog: public wxDialog { DECLARE_CLASS( PersonalRecordDialog ) DECLARE_EVENT_TABLE() public: //// Constructors PersonalRecordDialog( ); PersonalRecordDialog( wxWindow* parent, wxWindowID id = ID_PERSONAL_RECORD, const wxString& caption = wxT("Personal Record"), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxCAPTION|wxRESIZE_BORDER|wxSYSTEM_MENU ); /// Member initialization void Init(); /// Creation bool Create( wxWindow* parent, wxWindowID id = ID_PERSONAL_RECORD, const wxString& caption = wxT("Personal Record"), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxCAPTION|wxRESIZE_BORDER|wxSYSTEM_MENU ); /// Creates the controls and sizers void CreateControls(); /// Sets the validators for the dialog controls void SetDialogValidators(); /// Sets the help text for the dialog controls void SetDialogHelp(); /// Name accessors void SetName(const wxString& name) { m_name = name; } wxString GetName() const { return m_name; } /// Age accessors void SetAge(int age) { m_age = age; } int GetAge() const { return m_age; } /// Sex accessors (male = false, female = true) void SetSex(bool sex) { sex ? m_sex = 1 : m_sex = 0; } bool GetSex() const { return m_sex == 1; } /// Does the person vote? void SetVote(bool vote) { m_vote = vote; } bool GetVote() const { return m_vote; } //// PersonalRecordDialog event handler declarations /// wxEVT_UPDATE_UI event handler for ID_VOTE void OnVoteUpdate( wxUpdateUIEvent& event ); /// wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_RESET void OnResetClick( wxCommandEvent& event ); /// wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_HELP void OnHelpClick( wxCommandEvent& event ); //// PersonalRecordDialog member variables /// Data members wxString m_name; int m_age; int m_sex; bool m_vote; }; #endif // _PERSONALRECORD_H_ Listing J-2. Implementation File for PersonalRecordDialog[View full width] //////////////////////////////////////////////////////////////////// // Name: personalrecord.cpp // Purpose: Dialog to get name, age, sex, and voting preference // Author: Julian Smart // Created: 02/28/04 06:52:49 // Copyright: (c) 2004, Julian Smart // Licence: wxWindows license //////////////////////////////////////////////////////////////////// #ifdef __GNUG__ #pragma implementation "personalrecord.h" #endif // For compilers that support precompilation, includes "wx/wx.h". #include "wx/wxprec.h" #ifdef __BORLANDC__ #pragma hdrstop #endif #include "wx/valtext.h" #include "wx/valgen.h" #include "personalrecord.h" /*! * PersonalRecordDialog type definition */ IMPLEMENT_CLASS( PersonalRecordDialog, wxDialog ) /*! * PersonalRecordDialog event table definition */ BEGIN_EVENT_TABLE( PersonalRecordDialog, wxDialog ) EVT_UPDATE_UI( ID_VOTE, PersonalRecordDialog::OnVoteUpdate ) EVT_BUTTON( ID_RESET, PersonalRecordDialog::OnResetClick ) EVT_BUTTON( wxID_HELP, PersonalRecordDialog::OnHelpClick ) END_EVENT_TABLE() /*! * PersonalRecordDialog constructors */ PersonalRecordDialog::PersonalRecordDialog( ) { Init(); } PersonalRecordDialog::PersonalRecordDialog( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style ) { Init(); Create(parent, id, caption, pos, size, style); } /// Initialization void PersonalRecordDialog::Init( ) { m_name = wxEmptyString; m_age = 25; m_sex = false; m_vote = false; } /*! * PersonalRecord creator */ bool PersonalRecordDialog::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style ) { // We have to set extra styles before creating the // dialog SetExtraStyle(wxWS_EX_BLOCK_EVENTS|wxDIALOG_EX_CONTEXTHELP); if (!wxDialog::Create( parent, id, caption, pos, size, style )) return false; CreateControls(); SetDialogHelp(); SetDialogValidators(); // This fits the dialog to the minimum size dictated by // the sizers GetSizer()->Fit(this); // This ensures that the dialog cannot be sized smaller // than the minimum size GetSizer()->SetSizeHints(this); // Centre the dialog on the parent or (if none) screen Centre(); return true; } /*! * Control creation for PersonalRecordDialog */ void PersonalRecordDialog::CreateControls() { // A top-level sizer wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL); this->SetSizer(topSizer); // A second box sizer to give more space around the controls wxBoxSizer* boxSizer = new wxBoxSizer(wxVERTICAL); topSizer->Add(boxSizer, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5); // A friendly message wxStaticText* descr = new wxStaticText( this, wxID_STATIC, wxT("Please enter your name, age and sex, and specify whether you wish to\nvote in |
|