2003-09-27 20:23:08 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2004-05-25 07:20:37 -04:00
|
|
|
// Program: wxWidgets Widgets Sample
|
2003-09-27 20:23:08 -04:00
|
|
|
// Name: checkbox.cpp
|
|
|
|
// Purpose: Part of the widgets sample showing wxCheckBox
|
2004-07-25 08:24:42 -04:00
|
|
|
// Author: Dimitri Schoolwerth, Vadim Zeitlin
|
2003-09-27 20:23:08 -04:00
|
|
|
// Created: 27 Sep 2003
|
|
|
|
// Copyright: (c) 2003 wxWindows team
|
2010-07-13 09:29:13 -04:00
|
|
|
// Licence: wxWindows licence
|
2003-09-27 20:23:08 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// declarations
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// for compilers that support precompilation, includes "wx/wx.h".
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
|
|
|
|
// for all others, include the necessary headers
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/app.h"
|
|
|
|
#include "wx/log.h"
|
|
|
|
|
2003-09-28 09:53:07 -04:00
|
|
|
#include "wx/bitmap.h"
|
2003-09-27 20:23:08 -04:00
|
|
|
#include "wx/button.h"
|
|
|
|
#include "wx/checkbox.h"
|
2004-07-27 14:12:13 -04:00
|
|
|
#include "wx/radiobox.h"
|
|
|
|
#include "wx/statbox.h"
|
|
|
|
#include "wx/textctrl.h"
|
2003-09-27 20:23:08 -04:00
|
|
|
|
|
|
|
#include "wx/sizer.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "widgets.h"
|
|
|
|
|
|
|
|
#include "icons/checkbox.xpm"
|
|
|
|
|
2004-07-25 08:24:42 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// constants
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// control ids
|
|
|
|
enum
|
|
|
|
{
|
2006-05-22 06:39:40 -04:00
|
|
|
CheckboxPage_Reset = wxID_HIGHEST,
|
2004-07-25 08:24:42 -04:00
|
|
|
CheckboxPage_ChangeLabel,
|
|
|
|
CheckboxPage_Check,
|
|
|
|
CheckboxPage_Uncheck,
|
|
|
|
CheckboxPage_PartCheck,
|
|
|
|
CheckboxPage_ChkRight,
|
|
|
|
CheckboxPage_Checkbox
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
CheckboxKind_2State,
|
|
|
|
CheckboxKind_3State,
|
|
|
|
CheckboxKind_3StateUser
|
|
|
|
};
|
|
|
|
|
2003-09-27 20:23:08 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// CheckBoxWidgetsPage
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class CheckBoxWidgetsPage : public WidgetsPage
|
|
|
|
{
|
|
|
|
public:
|
2006-05-17 15:49:09 -04:00
|
|
|
CheckBoxWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
|
2003-09-27 20:23:08 -04:00
|
|
|
|
2015-07-31 11:53:55 -04:00
|
|
|
virtual wxWindow *GetWidget() const wxOVERRIDE { return m_checkbox; }
|
2014-03-29 20:02:23 -04:00
|
|
|
virtual void RecreateWidget() wxOVERRIDE { CreateCheckbox(); }
|
2004-11-19 14:03:04 -05:00
|
|
|
|
2006-06-06 10:10:06 -04:00
|
|
|
// lazy creation of the content
|
2014-03-29 20:02:23 -04:00
|
|
|
virtual void CreateContent() wxOVERRIDE;
|
2006-06-06 10:10:06 -04:00
|
|
|
|
2003-09-27 20:23:08 -04:00
|
|
|
protected:
|
|
|
|
// event handlers
|
|
|
|
void OnCheckBox(wxCommandEvent& event);
|
|
|
|
|
2004-07-25 08:24:42 -04:00
|
|
|
void OnStyleChange(wxCommandEvent& event);
|
|
|
|
void OnButtonReset(wxCommandEvent& event);
|
|
|
|
void OnButtonChangeLabel(wxCommandEvent& event);
|
|
|
|
|
|
|
|
void OnButtonCheck(wxCommandEvent&) { m_checkbox->SetValue(true); }
|
|
|
|
void OnButtonUncheck(wxCommandEvent&) { m_checkbox->SetValue(false); }
|
|
|
|
void OnButtonPartCheck(wxCommandEvent&)
|
|
|
|
{
|
|
|
|
m_checkbox->Set3StateValue(wxCHK_UNDETERMINED);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Is3State(wxUpdateUIEvent& event)
|
|
|
|
{
|
|
|
|
event.Enable( m_checkbox->Is3State() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// reset the wxCheckBox parameters
|
|
|
|
void Reset();
|
|
|
|
|
|
|
|
// (re)create the wxCheckBox
|
|
|
|
void CreateCheckbox();
|
2003-09-27 20:23:08 -04:00
|
|
|
|
|
|
|
// the controls
|
|
|
|
// ------------
|
|
|
|
|
2011-04-03 16:31:32 -04:00
|
|
|
// the controls to choose the checkbox style
|
2004-07-25 08:24:42 -04:00
|
|
|
wxCheckBox *m_chkRight;
|
|
|
|
wxRadioBox *m_radioKind;
|
2003-09-27 20:23:08 -04:00
|
|
|
|
2004-07-25 08:24:42 -04:00
|
|
|
// the checkbox itself and the sizer it is in
|
|
|
|
wxCheckBox *m_checkbox;
|
|
|
|
wxSizer *m_sizerCheckbox;
|
|
|
|
|
|
|
|
// the text entries for command parameters
|
|
|
|
wxTextCtrl *m_textLabel;
|
2003-09-27 20:23:08 -04:00
|
|
|
|
|
|
|
private:
|
2014-03-30 03:07:55 -04:00
|
|
|
wxDECLARE_EVENT_TABLE();
|
2003-09-27 20:23:08 -04:00
|
|
|
DECLARE_WIDGETS_PAGE(CheckBoxWidgetsPage)
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// event tables
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2014-03-30 03:07:55 -04:00
|
|
|
wxBEGIN_EVENT_TABLE(CheckBoxWidgetsPage, WidgetsPage)
|
2004-07-25 08:24:42 -04:00
|
|
|
EVT_CHECKBOX(CheckboxPage_Checkbox, CheckBoxWidgetsPage::OnCheckBox)
|
|
|
|
|
|
|
|
EVT_BUTTON(CheckboxPage_Reset, CheckBoxWidgetsPage::OnButtonReset)
|
|
|
|
EVT_BUTTON(CheckboxPage_ChangeLabel, CheckBoxWidgetsPage::OnButtonChangeLabel)
|
|
|
|
EVT_BUTTON(CheckboxPage_Check, CheckBoxWidgetsPage::OnButtonCheck)
|
|
|
|
EVT_BUTTON(CheckboxPage_Uncheck, CheckBoxWidgetsPage::OnButtonUncheck)
|
|
|
|
EVT_BUTTON(CheckboxPage_PartCheck, CheckBoxWidgetsPage::OnButtonPartCheck)
|
|
|
|
|
|
|
|
EVT_UPDATE_UI(CheckboxPage_PartCheck, CheckBoxWidgetsPage::Is3State)
|
|
|
|
|
|
|
|
EVT_RADIOBOX(wxID_ANY, CheckBoxWidgetsPage::OnStyleChange)
|
|
|
|
EVT_CHECKBOX(CheckboxPage_ChkRight, CheckBoxWidgetsPage::OnStyleChange)
|
2014-03-30 03:07:55 -04:00
|
|
|
wxEND_EVENT_TABLE()
|
2003-09-27 20:23:08 -04:00
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// implementation
|
|
|
|
// ============================================================================
|
|
|
|
|
2006-05-22 06:39:40 -04:00
|
|
|
#if defined(__WXUNIVERSAL__)
|
|
|
|
#define FAMILY_CTRLS UNIVERSAL_CTRLS
|
|
|
|
#else
|
|
|
|
#define FAMILY_CTRLS NATIVE_CTRLS
|
|
|
|
#endif
|
|
|
|
|
2018-09-28 21:45:15 -04:00
|
|
|
IMPLEMENT_WIDGETS_PAGE(CheckBoxWidgetsPage, "CheckBox", FAMILY_CTRLS );
|
2003-09-27 20:23:08 -04:00
|
|
|
|
2006-05-17 15:49:09 -04:00
|
|
|
CheckBoxWidgetsPage::CheckBoxWidgetsPage(WidgetsBookCtrl *book,
|
2004-11-23 06:45:07 -05:00
|
|
|
wxImageList *imaglist)
|
2006-05-26 10:07:47 -04:00
|
|
|
: WidgetsPage(book, imaglist, checkbox_xpm)
|
2006-06-06 10:10:06 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckBoxWidgetsPage::CreateContent()
|
2003-09-27 20:23:08 -04:00
|
|
|
{
|
2004-07-25 08:24:42 -04:00
|
|
|
wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
|
2003-09-27 20:23:08 -04:00
|
|
|
|
2004-07-25 08:24:42 -04:00
|
|
|
// left pane
|
2018-09-28 21:45:15 -04:00
|
|
|
wxStaticBox *box = new wxStaticBox(this, wxID_ANY, "&Set style");
|
2004-07-25 08:24:42 -04:00
|
|
|
|
|
|
|
wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
|
|
|
|
|
|
|
|
m_chkRight = CreateCheckBoxAndAddToSizer
|
|
|
|
(
|
|
|
|
sizerLeft,
|
2018-09-28 21:45:15 -04:00
|
|
|
"&Right aligned",
|
2004-07-25 08:24:42 -04:00
|
|
|
CheckboxPage_ChkRight
|
|
|
|
);
|
|
|
|
|
|
|
|
sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
|
|
|
|
|
|
|
|
static const wxString kinds[] =
|
|
|
|
{
|
2018-09-28 21:45:15 -04:00
|
|
|
"usual &2-state checkbox",
|
|
|
|
"&3rd state settable by program",
|
|
|
|
"&user-settable 3rd state",
|
2004-07-25 08:24:42 -04:00
|
|
|
};
|
2003-09-27 20:23:08 -04:00
|
|
|
|
2018-09-28 21:45:15 -04:00
|
|
|
m_radioKind = new wxRadioBox(this, wxID_ANY, "&Kind",
|
2004-07-25 08:24:42 -04:00
|
|
|
wxDefaultPosition, wxDefaultSize,
|
|
|
|
WXSIZEOF(kinds), kinds,
|
|
|
|
1);
|
|
|
|
sizerLeft->Add(m_radioKind, 0, wxGROW | wxALL, 5);
|
2018-09-28 21:45:15 -04:00
|
|
|
wxButton *btn = new wxButton(this, CheckboxPage_Reset, "&Reset");
|
2004-07-25 08:24:42 -04:00
|
|
|
sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
|
|
|
|
|
|
|
|
// middle pane
|
2018-09-28 21:45:15 -04:00
|
|
|
wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, "&Operations");
|
2004-07-25 08:24:42 -04:00
|
|
|
wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
|
|
|
|
|
|
|
|
sizerMiddle->Add(CreateSizerWithTextAndButton(CheckboxPage_ChangeLabel,
|
2018-09-28 21:45:15 -04:00
|
|
|
"Change label",
|
2004-07-25 08:24:42 -04:00
|
|
|
wxID_ANY,
|
|
|
|
&m_textLabel),
|
|
|
|
0, wxALL | wxGROW, 5);
|
2018-09-28 21:45:15 -04:00
|
|
|
sizerMiddle->Add(new wxButton(this, CheckboxPage_Check, "&Check it"),
|
2004-07-25 08:24:42 -04:00
|
|
|
0, wxALL | wxGROW, 5);
|
2018-09-28 21:45:15 -04:00
|
|
|
sizerMiddle->Add(new wxButton(this, CheckboxPage_Uncheck, "&Uncheck it"),
|
2004-07-25 08:24:42 -04:00
|
|
|
0, wxALL | wxGROW, 5);
|
|
|
|
sizerMiddle->Add(new wxButton(this, CheckboxPage_PartCheck,
|
2018-09-28 21:45:15 -04:00
|
|
|
"Put in &3rd state"),
|
2004-07-25 08:24:42 -04:00
|
|
|
0, wxALL | wxGROW, 5);
|
|
|
|
|
|
|
|
// right pane
|
|
|
|
wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
|
2018-09-28 21:45:15 -04:00
|
|
|
m_checkbox = new wxCheckBox(this, CheckboxPage_Checkbox, "&Check me!");
|
2004-07-25 08:24:42 -04:00
|
|
|
sizerRight->Add(0, 0, 1, wxCENTRE);
|
|
|
|
sizerRight->Add(m_checkbox, 1, wxCENTRE);
|
|
|
|
sizerRight->Add(0, 0, 1, wxCENTRE);
|
|
|
|
sizerRight->SetMinSize(150, 0);
|
|
|
|
m_sizerCheckbox = sizerRight; // save it to modify it later
|
|
|
|
|
|
|
|
// the 3 panes panes compose the window
|
|
|
|
sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
|
|
|
|
sizerTop->Add(sizerMiddle, 1, wxGROW | wxALL, 10);
|
|
|
|
sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
|
|
|
|
|
|
|
|
// final initializations
|
|
|
|
Reset();
|
2003-09-27 20:23:08 -04:00
|
|
|
|
|
|
|
SetSizer(sizerTop);
|
|
|
|
}
|
|
|
|
|
2004-07-25 08:24:42 -04:00
|
|
|
void CheckBoxWidgetsPage::Reset()
|
|
|
|
{
|
|
|
|
m_chkRight->SetValue(false);
|
|
|
|
m_radioKind->SetSelection(CheckboxKind_2State);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckBoxWidgetsPage::CreateCheckbox()
|
|
|
|
{
|
|
|
|
wxString label = m_checkbox->GetLabel();
|
|
|
|
|
|
|
|
size_t count = m_sizerCheckbox->GetChildren().GetCount();
|
|
|
|
for ( size_t n = 0; n < count; n++ )
|
|
|
|
{
|
|
|
|
m_sizerCheckbox->Remove(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete m_checkbox;
|
|
|
|
|
2015-01-02 09:26:53 -05:00
|
|
|
int flags = GetAttrs().m_defaultFlags;
|
2004-07-25 08:24:42 -04:00
|
|
|
if ( m_chkRight->IsChecked() )
|
|
|
|
flags |= wxALIGN_RIGHT;
|
|
|
|
|
|
|
|
switch ( m_radioKind->GetSelection() )
|
|
|
|
{
|
|
|
|
default:
|
2018-09-28 21:45:15 -04:00
|
|
|
wxFAIL_MSG("unexpected radiobox selection");
|
2018-11-25 14:22:44 -05:00
|
|
|
wxFALLTHROUGH;
|
2004-07-25 08:24:42 -04:00
|
|
|
|
|
|
|
case CheckboxKind_2State:
|
|
|
|
flags |= wxCHK_2STATE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CheckboxKind_3StateUser:
|
|
|
|
flags |= wxCHK_ALLOW_3RD_STATE_FOR_USER;
|
2018-11-25 14:22:44 -05:00
|
|
|
wxFALLTHROUGH;
|
2004-07-25 08:24:42 -04:00
|
|
|
|
|
|
|
case CheckboxKind_3State:
|
|
|
|
flags |= wxCHK_3STATE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_checkbox = new wxCheckBox(this, CheckboxPage_Checkbox, label,
|
|
|
|
wxDefaultPosition, wxDefaultSize,
|
|
|
|
flags);
|
|
|
|
|
|
|
|
m_sizerCheckbox->Add(0, 0, 1, wxCENTRE);
|
|
|
|
m_sizerCheckbox->Add(m_checkbox, 1, wxCENTRE);
|
|
|
|
m_sizerCheckbox->Add(0, 0, 1, wxCENTRE);
|
|
|
|
m_sizerCheckbox->Layout();
|
|
|
|
}
|
|
|
|
|
2003-09-27 20:23:08 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// event handlers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2004-07-25 08:24:42 -04:00
|
|
|
void CheckBoxWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
|
2003-09-27 20:23:08 -04:00
|
|
|
{
|
2004-07-25 08:24:42 -04:00
|
|
|
Reset();
|
2003-09-27 20:23:08 -04:00
|
|
|
|
2004-07-25 08:24:42 -04:00
|
|
|
CreateCheckbox();
|
|
|
|
}
|
2003-09-27 20:23:08 -04:00
|
|
|
|
2004-07-25 08:24:42 -04:00
|
|
|
void CheckBoxWidgetsPage::OnStyleChange(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
CreateCheckbox();
|
2003-09-27 20:23:08 -04:00
|
|
|
}
|
|
|
|
|
2004-07-25 08:24:42 -04:00
|
|
|
void CheckBoxWidgetsPage::OnButtonChangeLabel(wxCommandEvent& WXUNUSED(event))
|
2003-09-27 20:23:08 -04:00
|
|
|
{
|
2004-07-25 08:24:42 -04:00
|
|
|
m_checkbox->SetLabel(m_textLabel->GetValue());
|
2003-09-27 20:23:08 -04:00
|
|
|
}
|
2004-07-25 08:24:42 -04:00
|
|
|
|
|
|
|
void CheckBoxWidgetsPage::OnCheckBox(wxCommandEvent& event)
|
|
|
|
{
|
2018-09-28 21:45:15 -04:00
|
|
|
wxLogMessage("Test checkbox %schecked (value = %d).",
|
|
|
|
event.IsChecked() ? "" : "un",
|
2004-07-25 08:24:42 -04:00
|
|
|
(int)m_checkbox->Get3StateValue());
|
|
|
|
}
|
|
|
|
|