added more files (unchanged) from wxUniv branch

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@10674 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2001-06-26 21:05:06 +00:00
parent 1e6feb95a7
commit 32b8ec418a
78 changed files with 14777 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
#
# File: makefile.unx
# Author: Julian Smart
# Created: 1998
# Updated:
# Copyright: (c) 1998 Julian Smart
#
# "%W% %G%"
#
# Makefile for minimal example (UNIX).
top_srcdir = @top_srcdir@/..
top_builddir = ../..
program_dir = samples/widgets
PROGRAM=widgets
OBJECTS=button.o \
combobox.o \
gauge.o \
listbox.o \
notebook.o \
radiobox.o \
slider.o \
spinbtn.o \
static.o \
textctrl.o \
widgets.o
include ../../src/makeprog.env

371
samples/widgets/button.cpp Normal file
View File

@@ -0,0 +1,371 @@
/////////////////////////////////////////////////////////////////////////////
// Program: wxWindows Widgets Sample
// Name: button.cpp
// Purpose: Part of the widgets sample showing wxButton
// Author: Vadim Zeitlin
// Created: 10.04.01
// Id: $Id$
// Copyright: (c) 2001 Vadim Zeitlin
// License: wxWindows license
/////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
// for compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
// for all others, include the necessary headers
#ifndef WX_PRECOMP
#include "wx/log.h"
#include "wx/button.h"
#include "wx/checkbox.h"
#include "wx/radiobox.h"
#include "wx/statbox.h"
#include "wx/textctrl.h"
#endif
#include "wx/sizer.h"
#include "widgets.h"
#include "icons/button.xpm"
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
// control ids
enum
{
ButtonPage_Reset = 100,
ButtonPage_ChangeLabel,
ButtonPage_Button
};
// radio boxes
enum
{
ButtonHAlign_Left,
ButtonHAlign_Centre,
ButtonHAlign_Right
};
enum
{
ButtonVAlign_Top,
ButtonVAlign_Centre,
ButtonVAlign_Bottom
};
// ----------------------------------------------------------------------------
// ButtonWidgetsPage
// ----------------------------------------------------------------------------
class ButtonWidgetsPage : public WidgetsPage
{
public:
ButtonWidgetsPage(wxNotebook *notebook, wxImageList *imaglist);
virtual ~ButtonWidgetsPage();
protected:
// event handlers
void OnCheckOrRadioBox(wxCommandEvent& event);
void OnButton(wxCommandEvent& event);
void OnButtonReset(wxCommandEvent& event);
void OnButtonChangeLabel(wxCommandEvent& event);
// reset the wxButton parameters
void Reset();
// (re)create the wxButton
void CreateButton();
// the controls
// ------------
// the check/radio boxes for styles
wxCheckBox *m_chkImage,
*m_chkFit,
*m_chkDefault;
wxRadioBox *m_radioHAlign,
*m_radioVAlign;
// the gauge itself and the sizer it is in
wxButton *m_button;
wxSizer *m_sizerButton;
// the text entries for command parameters
wxTextCtrl *m_textLabel;
private:
DECLARE_EVENT_TABLE();
DECLARE_WIDGETS_PAGE(ButtonWidgetsPage);
};
// ----------------------------------------------------------------------------
// event tables
// ----------------------------------------------------------------------------
BEGIN_EVENT_TABLE(ButtonWidgetsPage, WidgetsPage)
EVT_BUTTON(ButtonPage_Button, ButtonWidgetsPage::OnButton)
EVT_BUTTON(ButtonPage_Reset, ButtonWidgetsPage::OnButtonReset)
EVT_BUTTON(ButtonPage_ChangeLabel, ButtonWidgetsPage::OnButtonChangeLabel)
EVT_CHECKBOX(-1, ButtonWidgetsPage::OnCheckOrRadioBox)
EVT_RADIOBOX(-1, ButtonWidgetsPage::OnCheckOrRadioBox)
END_EVENT_TABLE()
// ============================================================================
// implementation
// ============================================================================
IMPLEMENT_WIDGETS_PAGE(ButtonWidgetsPage, _T("Button"));
ButtonWidgetsPage::ButtonWidgetsPage(wxNotebook *notebook,
wxImageList *imaglist)
: WidgetsPage(notebook)
{
imaglist->Add(wxBitmap(button_xpm));
// init everything
m_chkImage =
m_chkFit =
m_chkDefault = (wxCheckBox *)NULL;
m_radioHAlign =
m_radioVAlign = (wxRadioBox *)NULL;
m_textLabel = (wxTextCtrl *)NULL;
m_button = (wxButton *)NULL;
m_sizerButton = (wxSizer *)NULL;
wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
// left pane
wxStaticBox *box = new wxStaticBox(this, -1, _T("&Set style"));
wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
m_chkImage = CreateCheckBoxAndAddToSizer(sizerLeft, _T("With &image"));
m_chkFit = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Fit exactly"));
m_chkDefault = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Default"));
#ifndef __WXUNIVERSAL__
// only wxUniv currently supoprts buttons with images
m_chkImage->Disable();
#endif // !wxUniv
sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
// should be in sync with enums Button[HV]Align!
static const wxString halign[] =
{
_T("left"),
_T("centre"),
_T("right"),
};
static const wxString valign[] =
{
_T("top"),
_T("centre"),
_T("bottom"),
};
m_radioHAlign = new wxRadioBox(this, -1, _T("&Horz alignment"),
wxDefaultPosition, wxDefaultSize,
WXSIZEOF(halign), halign);
m_radioVAlign = new wxRadioBox(this, -1, _T("&Vert alignment"),
wxDefaultPosition, wxDefaultSize,
WXSIZEOF(valign), valign);
sizerLeft->Add(m_radioHAlign, 0, wxGROW | wxALL, 5);
sizerLeft->Add(m_radioVAlign, 0, wxGROW | wxALL, 5);
sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
wxButton *btn = new wxButton(this, ButtonPage_Reset, _T("&Reset"));
sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
// middle pane
wxStaticBox *box2 = new wxStaticBox(this, -1, _T("&Operations"));
wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
wxSizer *sizerRow = CreateSizerWithTextAndButton(ButtonPage_ChangeLabel,
_T("Change label"),
-1,
&m_textLabel);
sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
// right pane
wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
m_button = new wxButton(this, ButtonPage_Button, _T("&Press me!"));
sizerRight->Add(0, 0, 1, wxCENTRE);
sizerRight->Add(m_button, 1, wxCENTRE);
sizerRight->Add(0, 0, 1, wxCENTRE);
sizerRight->SetMinSize(250, 0);
m_sizerButton = 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();
SetAutoLayout(TRUE);
SetSizer(sizerTop);
sizerTop->Fit(this);
}
ButtonWidgetsPage::~ButtonWidgetsPage()
{
}
// ----------------------------------------------------------------------------
// operations
// ----------------------------------------------------------------------------
void ButtonWidgetsPage::Reset()
{
m_chkFit->SetValue(TRUE);
m_chkImage->SetValue(FALSE);
m_chkDefault->SetValue(FALSE);
m_radioHAlign->SetSelection(ButtonHAlign_Centre);
m_radioVAlign->SetSelection(ButtonVAlign_Centre);
}
void ButtonWidgetsPage::CreateButton()
{
wxString label;
if ( m_button )
{
label = m_button->GetLabel();
size_t count = m_sizerButton->GetChildren().GetCount();
for ( size_t n = 0; n < count; n++ )
{
m_sizerButton->Remove(0);
}
delete m_button;
}
else
{
label = _T("&Press me!");
}
int flags = 0;
switch ( m_radioHAlign->GetSelection() )
{
case ButtonHAlign_Left:
flags |= wxALIGN_LEFT;
break;
default:
wxFAIL_MSG(_T("unexpected radiobox selection"));
// fall through
case ButtonHAlign_Centre:
flags |= wxALIGN_CENTRE_HORIZONTAL;
break;
case ButtonHAlign_Right:
flags |= wxALIGN_RIGHT;
break;
}
switch ( m_radioVAlign->GetSelection() )
{
case ButtonVAlign_Top:
flags |= wxALIGN_TOP;
break;
default:
wxFAIL_MSG(_T("unexpected radiobox selection"));
// fall through
case ButtonVAlign_Centre:
flags |= wxALIGN_CENTRE_VERTICAL;
break;
case ButtonVAlign_Bottom:
flags |= wxALIGN_BOTTOM;
break;
}
m_button = new wxButton(this, ButtonPage_Button, label,
wxDefaultPosition, wxDefaultSize,
flags);
#ifdef __WXUNIVERSAL__
if ( m_chkImage->GetValue() )
{
m_button->SetImageLabel(wxTheApp->GetStdIcon(wxICON_INFORMATION));
}
#endif // wxUniv
if ( m_chkDefault->GetValue() )
{
m_button->SetDefault();
}
if ( m_chkFit->GetValue() )
{
m_sizerButton->Add(0, 0, 1, wxCENTRE);
m_sizerButton->Add(m_button, 1, wxCENTRE);
m_sizerButton->Add(0, 0, 1, wxCENTRE);
}
else
{
m_sizerButton->Add(m_button, 1, wxGROW | wxALL, 5);
}
m_sizerButton->Layout();
}
// ----------------------------------------------------------------------------
// event handlers
// ----------------------------------------------------------------------------
void ButtonWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
{
Reset();
CreateButton();
}
void ButtonWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& event)
{
CreateButton();
}
void ButtonWidgetsPage::OnButtonChangeLabel(wxCommandEvent& WXUNUSED(event))
{
m_button->SetLabel(m_textLabel->GetValue());
}
void ButtonWidgetsPage::OnButton(wxCommandEvent& event)
{
wxLogMessage(_T("Test button clicked."));
}

View File

@@ -0,0 +1,494 @@
/////////////////////////////////////////////////////////////////////////////
// Program: wxWindows Widgets Sample
// Name: combobox.cpp
// Purpose: Part of the widgets sample showing wxComboBox
// Author: Vadim Zeitlin
// Created: 27.03.01
// Id: $Id$
// Copyright: (c) 2001 Vadim Zeitlin
// License: wxWindows license
/////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
// for compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
// for all others, include the necessary headers
#ifndef WX_PRECOMP
#include "wx/log.h"
#include "wx/button.h"
#include "wx/checkbox.h"
#include "wx/combobox.h"
#include "wx/radiobox.h"
#include "wx/statbox.h"
#include "wx/textctrl.h"
#endif
#include "wx/sizer.h"
#include "widgets.h"
#include "icons/combobox.xpm"
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
// control ids
enum
{
ComboPage_Reset = 100,
ComboPage_CurText,
ComboPage_Add,
ComboPage_AddText,
ComboPage_AddSeveral,
ComboPage_AddMany,
ComboPage_Clear,
ComboPage_Change,
ComboPage_ChangeText,
ComboPage_Delete,
ComboPage_DeleteText,
ComboPage_DeleteSel,
ComboPage_Combo
};
// kinds of comboboxes
enum
{
ComboKind_Default,
ComboKind_Simple,
ComboKind_DropDown
};
// ----------------------------------------------------------------------------
// ComboboxWidgetsPage
// ----------------------------------------------------------------------------
class ComboboxWidgetsPage : public WidgetsPage
{
public:
ComboboxWidgetsPage(wxNotebook *notebook, wxImageList *imaglist);
protected:
// event handlers
void OnButtonReset(wxCommandEvent& event);
void OnButtonChange(wxCommandEvent& event);
void OnButtonDelete(wxCommandEvent& event);
void OnButtonDeleteSel(wxCommandEvent& event);
void OnButtonClear(wxCommandEvent& event);
void OnButtonAdd(wxCommandEvent& event);
void OnButtonAddSeveral(wxCommandEvent& event);
void OnButtonAddMany(wxCommandEvent& event);
void OnComboBox(wxCommandEvent& event);
void OnComboText(wxCommandEvent& event);
void OnCheckOrRadioBox(wxCommandEvent& event);
void OnUpdateUICurText(wxUpdateUIEvent& event);
void OnUpdateUIAddSeveral(wxUpdateUIEvent& event);
void OnUpdateUIClearButton(wxUpdateUIEvent& event);
void OnUpdateUIDeleteButton(wxUpdateUIEvent& event);
void OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event);
void OnUpdateUIResetButton(wxUpdateUIEvent& event);
// reset the combobox parameters
void Reset();
// (re)create the combobox
void CreateCombo();
// the controls
// ------------
// the sel mode radiobox
wxRadioBox *m_radioKind;
// the checkboxes for styles
wxCheckBox *m_chkSort,
*m_chkReadonly;
// the combobox itself and the sizer it is in
wxComboBox *m_combobox;
wxSizer *m_sizerCombo;
// the text entries for "Add/change string" and "Delete" buttons
wxTextCtrl *m_textAdd,
*m_textChange,
*m_textDelete;
private:
DECLARE_EVENT_TABLE();
DECLARE_WIDGETS_PAGE(ComboboxWidgetsPage);
};
// ----------------------------------------------------------------------------
// event tables
// ----------------------------------------------------------------------------
BEGIN_EVENT_TABLE(ComboboxWidgetsPage, WidgetsPage)
EVT_BUTTON(ComboPage_Reset, ComboboxWidgetsPage::OnButtonReset)
EVT_BUTTON(ComboPage_Change, ComboboxWidgetsPage::OnButtonChange)
EVT_BUTTON(ComboPage_Delete, ComboboxWidgetsPage::OnButtonDelete)
EVT_BUTTON(ComboPage_DeleteSel, ComboboxWidgetsPage::OnButtonDeleteSel)
EVT_BUTTON(ComboPage_Clear, ComboboxWidgetsPage::OnButtonClear)
EVT_BUTTON(ComboPage_Add, ComboboxWidgetsPage::OnButtonAdd)
EVT_BUTTON(ComboPage_AddSeveral, ComboboxWidgetsPage::OnButtonAddSeveral)
EVT_BUTTON(ComboPage_AddMany, ComboboxWidgetsPage::OnButtonAddMany)
EVT_TEXT_ENTER(ComboPage_AddText, ComboboxWidgetsPage::OnButtonAdd)
EVT_TEXT_ENTER(ComboPage_DeleteText, ComboboxWidgetsPage::OnButtonDelete)
EVT_UPDATE_UI(ComboPage_CurText, ComboboxWidgetsPage::OnUpdateUICurText)
EVT_UPDATE_UI(ComboPage_Reset, ComboboxWidgetsPage::OnUpdateUIResetButton)
EVT_UPDATE_UI(ComboPage_AddSeveral, ComboboxWidgetsPage::OnUpdateUIAddSeveral)
EVT_UPDATE_UI(ComboPage_Clear, ComboboxWidgetsPage::OnUpdateUIClearButton)
EVT_UPDATE_UI(ComboPage_DeleteText, ComboboxWidgetsPage::OnUpdateUIClearButton)
EVT_UPDATE_UI(ComboPage_Delete, ComboboxWidgetsPage::OnUpdateUIDeleteButton)
EVT_UPDATE_UI(ComboPage_Change, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton)
EVT_UPDATE_UI(ComboPage_ChangeText, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton)
EVT_UPDATE_UI(ComboPage_DeleteSel, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton)
EVT_COMBOBOX(ComboPage_Combo, ComboboxWidgetsPage::OnComboBox)
EVT_TEXT(ComboPage_Combo, ComboboxWidgetsPage::OnComboText)
EVT_CHECKBOX(-1, ComboboxWidgetsPage::OnCheckOrRadioBox)
EVT_RADIOBOX(-1, ComboboxWidgetsPage::OnCheckOrRadioBox)
END_EVENT_TABLE()
// ============================================================================
// implementation
// ============================================================================
IMPLEMENT_WIDGETS_PAGE(ComboboxWidgetsPage, _T("Combobox"));
ComboboxWidgetsPage::ComboboxWidgetsPage(wxNotebook *notebook,
wxImageList *imaglist)
: WidgetsPage(notebook)
{
imaglist->Add(wxBitmap(combobox_xpm));
// init everything
m_chkSort =
m_chkReadonly = (wxCheckBox *)NULL;
m_combobox = (wxComboBox *)NULL;
m_sizerCombo = (wxSizer *)NULL;
/*
What we create here is a frame having 3 panes: style pane is the
leftmost one, in the middle the pane with buttons allowing to perform
miscellaneous combobox operations and the pane containing the combobox
itself to the right
*/
wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
// left pane
wxStaticBox *box = new wxStaticBox(this, -1, _T("&Set style"));
// should be in sync with ComboKind_XXX values
static const wxString kinds[] =
{
_T("default"),
_T("simple"),
_T("drop down"),
};
m_radioKind = new wxRadioBox(this, -1, _T("Combobox &kind:"),
wxDefaultPosition, wxDefaultSize,
WXSIZEOF(kinds), kinds,
1, wxRA_SPECIFY_COLS);
wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
m_chkSort = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Sort items"));
m_chkReadonly = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Read only"));
sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
sizerLeft->Add(m_radioKind, 0, wxGROW | wxALL, 5);
wxButton *btn = new wxButton(this, ComboPage_Reset, _T("&Reset"));
sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
// middle pane
wxStaticBox *box2 = new wxStaticBox(this, -1, _T("&Change combobox contents"));
wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
wxSizer *sizerRow;
wxTextCtrl *text;
sizerRow = CreateSizerWithTextAndLabel(_T("Current selection"),
ComboPage_CurText,
&text);
text->SetEditable(FALSE);
sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
sizerRow = CreateSizerWithTextAndButton(ComboPage_Add,
_T("&Add this string"),
ComboPage_AddText,
&m_textAdd);
sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
btn = new wxButton(this, ComboPage_AddSeveral, _T("&Insert a few strings"));
sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
btn = new wxButton(this, ComboPage_AddMany, _T("Add &many strings"));
sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
sizerRow = CreateSizerWithTextAndButton(ComboPage_Change,
_T("C&hange current"),
ComboPage_ChangeText,
&m_textChange);
sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
sizerRow = CreateSizerWithTextAndButton(ComboPage_Delete,
_T("&Delete this item"),
ComboPage_DeleteText,
&m_textDelete);
sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
btn = new wxButton(this, ComboPage_DeleteSel, _T("Delete &selection"));
sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
btn = new wxButton(this, ComboPage_Clear, _T("&Clear"));
sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
// right pane
wxSizer *sizerRight = new wxBoxSizer(wxVERTICAL);
m_combobox = new wxComboBox(this, ComboPage_Combo, _T(""),
wxDefaultPosition, wxDefaultSize,
0, NULL,
0);
sizerRight->Add(m_combobox, 1, wxGROW | wxALL, 5);
sizerRight->SetMinSize(250, 0);
m_sizerCombo = 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();
SetAutoLayout(TRUE);
SetSizer(sizerTop);
sizerTop->Fit(this);
}
// ----------------------------------------------------------------------------
// operations
// ----------------------------------------------------------------------------
void ComboboxWidgetsPage::Reset()
{
m_chkSort->SetValue(FALSE);
m_chkReadonly->SetValue(FALSE);
}
void ComboboxWidgetsPage::CreateCombo()
{
int flags = 0;
if ( m_chkSort->GetValue() )
flags |= wxCB_SORT;
if ( m_chkReadonly->GetValue() )
flags |= wxCB_READONLY;
switch ( m_radioKind->GetSelection() )
{
default:
wxFAIL_MSG( _T("unknown combo kind") );
// fall through
case ComboKind_Default:
break;
case ComboKind_Simple:
flags |= wxCB_SIMPLE;
break;
case ComboKind_DropDown:
flags = wxCB_DROPDOWN;
break;
}
wxArrayString items;
if ( m_combobox )
{
int count = m_combobox->GetCount();
for ( int n = 0; n < count; n++ )
{
items.Add(m_combobox->GetString(n));
}
m_sizerCombo->Remove(m_combobox);
delete m_combobox;
}
m_combobox = new wxComboBox(this, ComboPage_Combo, _T(""),
wxDefaultPosition, wxDefaultSize,
0, NULL,
flags);
size_t count = items.GetCount();
for ( size_t n = 0; n < count; n++ )
{
m_combobox->Append(items[n]);
}
m_sizerCombo->Add(m_combobox, 1, wxGROW | wxALL, 5);
m_sizerCombo->Layout();
}
// ----------------------------------------------------------------------------
// event handlers
// ----------------------------------------------------------------------------
void ComboboxWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
{
Reset();
CreateCombo();
}
void ComboboxWidgetsPage::OnButtonChange(wxCommandEvent& WXUNUSED(event))
{
int sel = m_combobox->GetSelection();
if ( sel != -1 )
{
#ifndef __WXGTK__
m_combobox->SetString(sel, m_textChange->GetValue());
#else
wxLogMessage(_T("Not implemented in wxGTK"));
#endif
}
}
void ComboboxWidgetsPage::OnButtonDelete(wxCommandEvent& WXUNUSED(event))
{
unsigned long n;
if ( !m_textDelete->GetValue().ToULong(&n) ||
(n >= (unsigned)m_combobox->GetCount()) )
{
return;
}
m_combobox->Delete(n);
}
void ComboboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent& WXUNUSED(event))
{
int sel = m_combobox->GetSelection();
if ( sel != -1 )
{
m_combobox->Delete(sel);
}
}
void ComboboxWidgetsPage::OnButtonClear(wxCommandEvent& event)
{
m_combobox->Clear();
}
void ComboboxWidgetsPage::OnButtonAdd(wxCommandEvent& event)
{
static size_t s_item = 0;
wxString s = m_textAdd->GetValue();
if ( !m_textAdd->IsModified() )
{
// update the default string
m_textAdd->SetValue(wxString::Format(_T("test item %u"), ++s_item));
}
m_combobox->Append(s);
}
void ComboboxWidgetsPage::OnButtonAddMany(wxCommandEvent& WXUNUSED(event))
{
// "many" means 1000 here
for ( size_t n = 0; n < 1000; n++ )
{
m_combobox->Append(wxString::Format(_T("item #%u"), n));
}
}
void ComboboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent& event)
{
m_combobox->Append(_T("First"));
m_combobox->Append(_T("another one"));
m_combobox->Append(_T("and the last (very very very very very very very very very very long) one"));
}
void ComboboxWidgetsPage::OnUpdateUICurText(wxUpdateUIEvent& event)
{
event.SetText( wxString::Format(_T("%d"), m_combobox->GetSelection()) );
}
void ComboboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
{
event.Enable( m_chkSort->GetValue() || m_chkReadonly->GetValue() );
}
void ComboboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent& event)
{
unsigned long n;
event.Enable(m_textDelete->GetValue().ToULong(&n) &&
(n < (unsigned)m_combobox->GetCount()));
}
void ComboboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event)
{
event.Enable(m_combobox->GetSelection() != -1);
}
void ComboboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent& event)
{
event.Enable(m_combobox->GetCount() != 0);
}
void ComboboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent& event)
{
event.Enable(!(m_combobox->GetWindowStyle() & wxCB_SORT));
}
void ComboboxWidgetsPage::OnComboText(wxCommandEvent& event)
{
wxString s = event.GetString();
wxASSERT_MSG( s == m_combobox->GetValue(),
_T("event and combobox values should be the same") );
wxLogMessage(_T("Combobox text changed (now '%s')"), s.c_str());
}
void ComboboxWidgetsPage::OnComboBox(wxCommandEvent& event)
{
int sel = event.GetInt();
m_textDelete->SetValue(wxString::Format(_T("%ld"), sel));
wxLogMessage(_T("Combobox item %d selected"), sel);
}
void ComboboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& event)
{
CreateCombo();
}

402
samples/widgets/gauge.cpp Normal file
View File

@@ -0,0 +1,402 @@
/////////////////////////////////////////////////////////////////////////////
// Program: wxWindows Widgets Sample
// Name: gauge.cpp
// Purpose: Part of the widgets sample showing wxGauge
// Author: Vadim Zeitlin
// Created: 27.03.01
// Id: $Id$
// Copyright: (c) 2001 Vadim Zeitlin
// License: wxWindows license
/////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
// for compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
// for all others, include the necessary headers
#ifndef WX_PRECOMP
#include "wx/log.h"
#include "wx/timer.h"
#include "wx/button.h"
#include "wx/checkbox.h"
#include "wx/combobox.h"
#include "wx/gauge.h"
#include "wx/radiobox.h"
#include "wx/statbox.h"
#include "wx/textctrl.h"
#endif
#include "wx/sizer.h"
#include "widgets.h"
#include "icons/gauge.xpm"
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
// control ids
enum
{
GaugePage_Reset = 100,
GaugePage_Progress,
GaugePage_Clear,
GaugePage_SetValue,
GaugePage_SetRange,
GaugePage_CurValueText,
GaugePage_ValueText,
GaugePage_RangeText,
GaugePage_Timer,
GaugePage_Gauge
};
// ----------------------------------------------------------------------------
// GaugeWidgetsPage
// ----------------------------------------------------------------------------
class GaugeWidgetsPage : public WidgetsPage
{
public:
GaugeWidgetsPage(wxNotebook *notebook, wxImageList *imaglist);
virtual ~GaugeWidgetsPage();
protected:
// event handlers
void OnButtonReset(wxCommandEvent& event);
void OnButtonProgress(wxCommandEvent& event);
void OnButtonClear(wxCommandEvent& event);
void OnButtonSetValue(wxCommandEvent& event);
void OnButtonSetRange(wxCommandEvent& event);
void OnCheckOrRadioBox(wxCommandEvent& event);
void OnUpdateUIValueButton(wxUpdateUIEvent& event);
void OnUpdateUIRangeButton(wxUpdateUIEvent& event);
void OnUpdateUIResetButton(wxUpdateUIEvent& event);
void OnUpdateUICurValueText(wxUpdateUIEvent& event);
void OnProgressTimer(wxTimerEvent& event);
// reset the gauge parameters
void Reset();
// (re)create the gauge
void CreateGauge();
// stop the progress timer
void StopTimer();
// the gauge range
unsigned long m_range;
// the controls
// ------------
// the checkboxes for styles
wxCheckBox *m_chkVert,
*m_chkSmooth;
// the gauge itself and the sizer it is in
wxGauge *m_gauge;
wxSizer *m_sizerGauge;
// the text entries for set value/range
wxTextCtrl *m_textValue,
*m_textRange;
// the timer for simulating gauge progress
wxTimer *m_timer;
private:
DECLARE_EVENT_TABLE();
DECLARE_WIDGETS_PAGE(GaugeWidgetsPage);
};
// ----------------------------------------------------------------------------
// event tables
// ----------------------------------------------------------------------------
BEGIN_EVENT_TABLE(GaugeWidgetsPage, WidgetsPage)
EVT_BUTTON(GaugePage_Reset, GaugeWidgetsPage::OnButtonReset)
EVT_BUTTON(GaugePage_Progress, GaugeWidgetsPage::OnButtonProgress)
EVT_BUTTON(GaugePage_Clear, GaugeWidgetsPage::OnButtonClear)
EVT_BUTTON(GaugePage_SetValue, GaugeWidgetsPage::OnButtonSetValue)
EVT_BUTTON(GaugePage_SetRange, GaugeWidgetsPage::OnButtonSetRange)
EVT_UPDATE_UI(GaugePage_SetValue, GaugeWidgetsPage::OnUpdateUIValueButton)
EVT_UPDATE_UI(GaugePage_SetRange, GaugeWidgetsPage::OnUpdateUIRangeButton)
EVT_UPDATE_UI(GaugePage_Reset, GaugeWidgetsPage::OnUpdateUIResetButton)
EVT_UPDATE_UI(GaugePage_CurValueText, GaugeWidgetsPage::OnUpdateUICurValueText)
EVT_CHECKBOX(-1, GaugeWidgetsPage::OnCheckOrRadioBox)
EVT_RADIOBOX(-1, GaugeWidgetsPage::OnCheckOrRadioBox)
EVT_TIMER(GaugePage_Timer, GaugeWidgetsPage::OnProgressTimer)
END_EVENT_TABLE()
// ============================================================================
// implementation
// ============================================================================
IMPLEMENT_WIDGETS_PAGE(GaugeWidgetsPage, _T("Gauge"));
GaugeWidgetsPage::GaugeWidgetsPage(wxNotebook *notebook,
wxImageList *imaglist)
: WidgetsPage(notebook)
{
imaglist->Add(wxBitmap(gauge_xpm));
// init everything
m_range = 100;
m_timer = (wxTimer *)NULL;
m_chkVert =
m_chkSmooth = (wxCheckBox *)NULL;
m_gauge = (wxGauge *)NULL;
m_sizerGauge = (wxSizer *)NULL;
wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
// left pane
wxStaticBox *box = new wxStaticBox(this, -1, _T("&Set style"));
wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
m_chkVert = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Vertical"));
m_chkSmooth = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Smooth"));
sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
wxButton *btn = new wxButton(this, GaugePage_Reset, _T("&Reset"));
sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
// middle pane
wxStaticBox *box2 = new wxStaticBox(this, -1, _T("&Change gauge value"));
wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
wxTextCtrl *text;
wxSizer *sizerRow = CreateSizerWithTextAndLabel(_T("Current value"),
GaugePage_CurValueText,
&text);
text->SetEditable(FALSE);
sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
sizerRow = CreateSizerWithTextAndButton(GaugePage_SetValue,
_T("Set &value"),
GaugePage_ValueText,
&m_textValue);
sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
sizerRow = CreateSizerWithTextAndButton(GaugePage_SetRange,
_T("Set &range"),
GaugePage_RangeText,
&m_textRange);
m_textRange->SetValue(wxString::Format(_T("%lu"), m_range));
sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
btn = new wxButton(this, GaugePage_Progress, _T("Simulate &progress"));
sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
btn = new wxButton(this, GaugePage_Clear, _T("&Clear"));
sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
// right pane
wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
m_gauge = new wxGauge(this, GaugePage_Gauge, m_range);
sizerRight->Add(m_gauge, 1, wxCENTRE | wxALL, 5);
sizerRight->SetMinSize(250, 0);
m_sizerGauge = 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();
SetAutoLayout(TRUE);
SetSizer(sizerTop);
sizerTop->Fit(this);
}
GaugeWidgetsPage::~GaugeWidgetsPage()
{
delete m_timer;
}
// ----------------------------------------------------------------------------
// operations
// ----------------------------------------------------------------------------
void GaugeWidgetsPage::Reset()
{
m_chkVert->SetValue(FALSE);
m_chkSmooth->SetValue(FALSE);
}
void GaugeWidgetsPage::CreateGauge()
{
int flags = 0;
if ( m_chkVert->GetValue() )
flags |= wxGA_VERTICAL;
else
flags |= wxGA_HORIZONTAL;
if ( m_chkSmooth->GetValue() )
flags |= wxGA_SMOOTH;
int val = 0;
if ( m_gauge )
{
val = m_gauge->GetValue();
m_sizerGauge->Remove(m_gauge);
delete m_gauge;
}
m_gauge = new wxGauge(this, GaugePage_Gauge, m_range,
wxDefaultPosition, wxDefaultSize,
flags);
m_gauge->SetValue(val);
if ( flags & wxGA_VERTICAL )
m_sizerGauge->Add(m_gauge, 0, wxGROW | wxALL, 5);
else
m_sizerGauge->Add(m_gauge, 1, wxCENTRE | wxALL, 5);
m_sizerGauge->Layout();
}
// ----------------------------------------------------------------------------
// event handlers
// ----------------------------------------------------------------------------
void GaugeWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
{
Reset();
CreateGauge();
}
void GaugeWidgetsPage::OnButtonProgress(wxCommandEvent& event)
{
if ( !m_timer )
{
static const int INTERVAL = 300;
wxLogMessage(_T("Launched progress timer (interval = %d ms)"), INTERVAL);
m_timer = new wxTimer(this, GaugePage_Timer);
m_timer->Start(INTERVAL);
wxButton *btn = (wxButton *)event.GetEventObject();
btn->SetLabel(_T("&Stop timer"));
}
else // stop the running timer
{
StopTimer();
wxLogMessage(_T("Stopped the timer."));
}
}
void GaugeWidgetsPage::OnButtonClear(wxCommandEvent& WXUNUSED(event))
{
m_gauge->SetValue(0);
}
void GaugeWidgetsPage::OnButtonSetRange(wxCommandEvent& WXUNUSED(event))
{
unsigned long val;
if ( !m_textRange->GetValue().ToULong(&val) )
return;
m_gauge->SetRange(val);
}
void GaugeWidgetsPage::OnButtonSetValue(wxCommandEvent& WXUNUSED(event))
{
unsigned long val;
if ( !m_textValue->GetValue().ToULong(&val) )
return;
m_gauge->SetValue(val);
}
void GaugeWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent& event)
{
unsigned long val;
event.Enable( m_textValue->GetValue().ToULong(&val) && (val <= m_range) );
}
void GaugeWidgetsPage::OnUpdateUIRangeButton(wxUpdateUIEvent& event)
{
unsigned long val;
event.Enable( m_textRange->GetValue().ToULong(&val) );
}
void GaugeWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
{
event.Enable( m_chkVert->GetValue() || m_chkSmooth->GetValue() );
}
void GaugeWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& event)
{
CreateGauge();
}
void GaugeWidgetsPage::OnProgressTimer(wxTimerEvent& WXUNUSED(event))
{
int val = m_gauge->GetValue();
if ( (unsigned)val < m_range )
{
m_gauge->SetValue(val + 1);
}
else // reached the end
{
StopTimer();
}
}
void GaugeWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent& event)
{
event.SetText( wxString::Format(_T("%d"), m_gauge->GetValue()));
}
void GaugeWidgetsPage::StopTimer()
{
wxCHECK_RET( m_timer, _T("shouldn't be called") );
m_timer->Stop();
delete m_timer;
m_timer = NULL;
wxButton *btn = (wxButton *)FindWindow(GaugePage_Progress);
wxCHECK_RET( btn, _T("no progress button?") );
btn->SetLabel(_T("Simulate &progress"));
wxLogMessage(_T("Progress finished."));
}

View File

@@ -0,0 +1 @@
*.bmp

View File

@@ -0,0 +1,37 @@
/* XPM */
static char *magick[] = {
/* columns rows colors chars-per-pixel */
"16 15 16 1",
" c Gray0",
". c #808000",
"X c #000080",
"o c #808080",
"O c #000000",
"+ c #808000",
"@ c #000080",
"# c #808080",
"$ c #c0c0c0",
"% c Red",
"& c Green",
"* c Yellow",
"= c Blue",
"- c Magenta",
"; c Cyan",
": c Gray100",
/* pixels */
" ",
" $$$$$$$$$$$$$$ ",
" $$$# #$$$# #$ ",
" $$$ $ $$$ $$ $ ",
" $# $ $$$ $$ $ ",
" $ $ $ $# #$ ",
" $ $ $ $ $$$$$ ",
" $# $$ $ $$$$$ ",
" $ $ ",
" $$$ $$ $$$$$$$ ",
" $$$ $ $$ $ $ ",
" $$$ $ $ $$ $$$ ",
" $$ $ $$$ ",
" $ $$$$$$$$$ ",
" "
};

View File

@@ -0,0 +1,54 @@
/* XPM */
static char *button_xpm[] = {
/* columns rows colors chars-per-pixel */
"32 32 16 1",
" c Gray0",
". c #808000",
"X c #000080",
"o c #808080",
"O c #000000",
"+ c #808000",
"@ c #000080",
"# c #c0c0c0",
"$ c #808080",
"% c Red",
"& c Green",
"* c Yellow",
"= c Blue",
"- c Magenta",
"; c Cyan",
": c Gray100",
/* pixels */
"################################",
"################################",
"################################",
"################################",
"################################",
"################################",
"##### #####",
"#### :::::::::::::::::::::: ####",
"### :::::::::::::::::::::::$ ###",
"## ::######################$$ ##",
"## ::######################$$ ##",
"## ::######################$$ ##",
"## ::######################$$ ##",
"## ::######################$$ ##",
"## ::######################$$ ##",
"## ::######################$$ ##",
"## ::######################$$ ##",
"## ::######################$$ ##",
"## ::######################$$ ##",
"## ::######################$$ ##",
"## ::######################$$ ##",
"## ::######################$$ ##",
"### $$$$$$$$$$$$$$$$$$$$$$$$ ###",
"#### $$$$$$$$$$$$$$$$$$$$$$ ####",
"##### #####",
"################################",
"################################",
"################################",
"################################",
"################################",
"################################",
"################################"
};

View File

@@ -0,0 +1,54 @@
/* XPM */
static char *checkbox_xpm[] = {
/* columns rows colors chars-per-pixel */
"32 32 16 1",
" c Gray0",
". c #808000",
"X c #000080",
"o c #808080",
"O c #000000",
"+ c #808000",
"@ c #000080",
"# c #c0c0c0",
"$ c #808080",
"% c Red",
"& c Green",
"* c Yellow",
"= c Blue",
"- c Magenta",
"; c Cyan",
": c Gray100",
/* pixels */
"################################",
"################################",
"################################",
"################################",
"#####$$$$$$$$$$$$$$$$$$$$$$#####",
"####$ #:####",
"####$ ::::::::::::::::::::#:####",
"####$ ::::::::::::::::::::#:####",
"####$ ::::::::::::::::::::#:####",
"####$ ::: :::::::::: :::#:####",
"####$ ::: :::::::: :::#:####",
"####$ :::: :::::: ::::#:####",
"####$ ::::: :::: :::::#:####",
"####$ :::::: :: ::::::#:####",
"####$ ::::::: :::::::#:####",
"####$ :::::::: ::::::::#:####",
"####$ :::::::: ::::::::#:####",
"####$ ::::::: :::::::#:####",
"####$ :::::: :: ::::::#:####",
"####$ ::::: :::: :::::#:####",
"####$ :::: :::::: ::::#:####",
"####$ ::: :::::::: :::#:####",
"####$ ::: :::::::::: :::#:####",
"####$ ::::::::::::::::::::#:####",
"####$ ::::::::::::::::::::#:####",
"####$ ::::::::::::::::::::#:####",
"####$ #####################:####",
"#####::::::::::::::::::::::#####",
"################################",
"################################",
"################################",
"################################"
};

View File

@@ -0,0 +1,54 @@
/* XPM */
static char *combobox_xpm[] = {
/* columns rows colors chars-per-pixel */
"32 32 16 1",
" c Gray0",
". c #808000",
"X c #000080",
"o c #808080",
"O c #000000",
"+ c #808000",
"@ c #000080",
"# c #c0c0c0",
"$ c #808080",
"% c Red",
"& c Green",
"* c Yellow",
"= c Blue",
"- c Magenta",
"; c Cyan",
": c Gray100",
/* pixels */
"################################",
"################################",
"## ##",
"## ################ ##",
"## ################ ##### ##",
"## ################ ### ##",
"## ################ # ##",
"## ################ ##",
"## ##",
"#### ############## ######### ##",
"#### ############## ######### ##",
"#### ############## #### #### ##",
"#### ############## ### ### ##",
"#### ## ##### ## ## ##",
"#### ############## ######### ##",
"#### ## #### ######### ##",
"#### ############## ######### ##",
"#### ## ### ##",
"#### ############## ######### ##",
"#### ## #### ######### ##",
"#### ############## ######### ##",
"#### ## ##### ## ## ##",
"#### ############## ### ### ##",
"#### ## #### #### #### ##",
"#### ############## ######### ##",
"#### ############## ######### ##",
"#### ############## ######### ##",
"#### ##",
"################################",
"################################",
"################################",
"################################"
};

View File

@@ -0,0 +1,54 @@
/* XPM */
static char *gauge_xpm[] = {
/* columns rows colors chars-per-pixel */
"32 32 16 1",
" c Gray0",
". c #808000",
"X c #000080",
"o c #808080",
"O c #000000",
"+ c #808000",
"@ c #000080",
"# c #c0c0c0",
"$ c #808080",
"% c Red",
"& c Green",
"* c Yellow",
"= c Blue",
"- c Magenta",
"; c Cyan",
": c Gray100",
/* pixels */
"################################",
"################################",
"################################",
"################################",
"################################",
"################################",
"################################",
"################################",
"## ##",
"## $$$$$$$$$$$$$$$$$::::::::: ##",
"## $$$$$$$$$$$$$$$$$::::::::: ##",
"## $$$$$$$$$$$$$$$$$::::::::: ##",
"## $$$$ $$$$$ $$$$$ :::: :::: ##",
"## $$$$ $$$$$ $$$$$ :::: :::: ##",
"## $$$$ $$$$$ $$$$$ :::: :::: ##",
"## $$$$ $$$$$ $$$$$ :::: :::: ##",
"## $$$$ $$$$$ $$$$$ :::: :::: ##",
"## $$$$ $$$$$ $$$$$ :::: :::: ##",
"## $$$$ $$$$$ $$$$$ :::: :::: ##",
"## $$$$ $$$$$ $$$$$ :::: :::: ##",
"## $$$$$$$$$$$$$$$$$::::::::: ##",
"## $$$$$$$$$$$$$$$$$::::::::: ##",
"## $$$$$$$$$$$$$$$$$::::::::: ##",
"## ##",
"################################",
"################################",
"################################",
"################################",
"################################",
"################################",
"################################",
"################################"
};

View File

@@ -0,0 +1,54 @@
/* XPM */
static char *listbox_xpm[] = {
/* columns rows colors chars-per-pixel */
"32 32 16 1",
" c Gray0",
". c #808000",
"X c #000080",
"o c #808080",
"O c #000000",
"+ c #808000",
"@ c #000080",
"# c #c0c0c0",
"$ c #808080",
"% c Red",
"& c Green",
"* c Yellow",
"= c Blue",
"- c Magenta",
"; c Cyan",
": c Gray100",
/* pixels */
"################################",
"################################",
"################################",
"################################",
"################################",
"################################",
"### ###",
"### ############## ######### ###",
"### ############## ######### ###",
"### ############## #### #### ###",
"### ############## ### ### ###",
"### ## ##### ## ## ###",
"### ############## ######### ###",
"### ## #### ######### ###",
"### ############## ######### ###",
"### ## ### ###",
"### ############## ######### ###",
"### ## #### ######### ###",
"### ############## ######### ###",
"### ## ##### ## ## ###",
"### ############## ### ### ###",
"### ## #### #### #### ###",
"### ############## ######### ###",
"### ############## ######### ###",
"### ############## ######### ###",
"### ###",
"################################",
"################################",
"################################",
"################################",
"################################",
"################################"
};

View File

@@ -0,0 +1,54 @@
/* XPM */
static char *notebook_xpm[] = {
/* columns rows colors chars-per-pixel */
"32 32 16 1",
" c Gray0",
". c #808000",
"X c #000080",
"o c #808080",
"O c #000000",
"+ c #808000",
"@ c #000080",
"# c #c0c0c0",
"$ c #808080",
"% c Red",
"& c Green",
"* c Yellow",
"= c Blue",
"- c Magenta",
"; c Cyan",
": c Gray100",
/* pixels */
"################################",
"################################",
"################################",
"################################",
"################################",
"### # # ###",
"## :::::::$ :::::::$ :::::::$ ##",
"## :######$ :######$ :######$ ##",
"## :######$ :######$ :######$ ##",
"## :######$ ##",
"## :########################$ ##",
"## :########################$ ##",
"## :########################$ ##",
"## :########################$ ##",
"## :########################$ ##",
"## :########################$ ##",
"## :########################$ ##",
"## :########################$ ##",
"## :########################$ ##",
"## :########################$ ##",
"## :########################$ ##",
"## :########################$ ##",
"## :########################$ ##",
"## $$$$$$$$$$$$$$$$$$$$$$$$$$ ##",
"## ##",
"################################",
"################################",
"################################",
"################################",
"################################",
"################################",
"################################"
};

View File

@@ -0,0 +1,54 @@
/* XPM */
static char *radio_xpm[] = {
/* columns rows colors chars-per-pixel */
"32 32 16 1",
" c Gray0",
". c #808000",
"X c #000080",
"o c #808080",
"O c #000000",
"+ c #808000",
"@ c #000080",
"# c #c0c0c0",
"$ c #808080",
"% c Red",
"& c Green",
"* c Yellow",
"= c Blue",
"- c Magenta",
"; c Cyan",
": c Gray100",
/* pixels */
"################################",
"################################",
"################################",
"################################",
"###########$$$$$$$$$#:##########",
"#########$$$ $$#:#########",
"########$$ $:::::: $::########",
"#######$$ :::::::::::#$::#######",
"######$$ :::::::::::::#$::######",
"#####$$ :::::::::::::::#$::#####",
"####$$ ::::::#####::::::#$:#####",
"####$ $:::::$ $#:::::$#:####",
"###$$ :::::$ $#::::$#:####",
"###$ :::::$ $:::::$:####",
"###$ ::::: #::::$:####",
"###$ ::::: #::::$:####",
"###$ ::::: #::::$:####",
"###$ ::::: #::::$:####",
"###$ ::::: #::::$:####",
"###$ :::::$ $:::::$:####",
"###$$ :::::$ $#::::$#:####",
"####$ ::::::$ $#:::::$#:####",
"####$$#::::::#####::::::$#:#####",
"####:$$#:::::::::::::::$#:######",
"#####:$$$:::::::::::::$#:#######",
"######:$$$:::::::::::$#:########",
"#######::$$$::::::$$$::#########",
"#########:$$$$$$$$$::###########",
"###########::::::::#############",
"################################",
"################################",
"################################"
};

View File

@@ -0,0 +1,54 @@
/* XPM */
static char *magick[] = {
/* columns rows colors chars-per-pixel */
"32 32 16 1",
" c Gray0",
". c #808000",
"X c #000080",
"o c #808080",
"O c #000000",
"+ c #808000",
"@ c #000080",
"# c #c0c0c0",
"$ c #808080",
"% c Red",
"& c Green",
"* c Yellow",
"= c Blue",
"- c Magenta",
"; c Cyan",
": c Gray100",
/* pixels */
"################################",
"################################",
"################################",
"################################",
"################################",
"################################",
"################################",
"################################",
"## ###",
"## ############ ############ ###",
"## ############ ############ ###",
"## ############ ############ ###",
"## ####### #### #### ####### ###",
"## ###### #### #### ###### ###",
"## ##### #### #### ##### ###",
"## #### #### #### #### ###",
"## ### #### #### ### ###",
"## #### #### #### #### ###",
"## ##### #### #### ##### ###",
"## ###### #### #### ###### ###",
"## ####### #### #### ####### ###",
"## ############ ############ ###",
"## ############ ############ ###",
"## ############ ############ ###",
"## ###",
"################################",
"################################",
"################################",
"################################",
"################################",
"################################",
"################################"
};

View File

@@ -0,0 +1,54 @@
/* XPM */
static char *slider_xpm[] = {
/* columns rows colors chars-per-pixel */
"32 32 16 1",
" c Gray0",
". c #808000",
"X c #000080",
"o c #808080",
"O c #000000",
"+ c #808000",
"@ c #000080",
"# c #c0c0c0",
"$ c #808080",
"% c Red",
"& c Green",
"* c Yellow",
"= c Blue",
"- c Magenta",
"; c Cyan",
": c Gray100",
/* pixels */
"################################",
"################################",
"################################",
"################################",
"################################",
"################################",
"################################",
"################################",
"####### ###################",
"###### :::::$ ##################",
"###### :####$ ##################",
"###### :####$ ##################",
"###### :####$ ##################",
"###### :####$ ##################",
"###### :####$ ##################",
"## :####$ ##",
"## $$$ :####$ $$$$$$$$$$$$$$$ ##",
"## ### :####$ ############### ##",
"## ### :####$ ############### ##",
"## ### :####$ ############### ##",
"###### :####$ ##################",
"###### :####$ ##################",
"###### :$$$$$ ##################",
"####### ###################",
"################################",
"################################",
"################################",
"################################",
"################################",
"################################",
"################################",
"################################"
};

View File

@@ -0,0 +1,40 @@
/* XPM */
static char *spinbtn_xpm[] = {
/* columns rows colors chars-per-pixel */
"32 32 2 1",
"$ c None",
" c Black",
/* pixels */
"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$",
"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$",
"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$",
"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$",
"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$",
"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$",
"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$",
"$$ $$$$",
"$$ $$$$",
"$$ $$$$$$$$$$ $$$$$$$$$$ $$$$",
"$$ $$$$$$$$$$ $$$$$$$$$$ $$$$",
"$$ $$$$$$ $$ $$ $$$$$$ $$$$",
"$$ $$$$$$ $$ $$ $$$$$$ $$$$",
"$$ $$$$ $$ $$ $$$$ $$$$",
"$$ $$$$ $$ $$ $$$$ $$$$",
"$$ $$ $$ $$ $$ $$$$",
"$$ $$ $$ $$ $$ $$$$",
"$$ $$$$ $$ $$ $$$$ $$$$",
"$$ $$$$ $$ $$ $$$$ $$$$",
"$$ $$$$$$ $$ $$ $$$$$$ $$$$",
"$$ $$$$$$ $$ $$ $$$$$$ $$$$",
"$$ $$$$$$$$$$ $$$$$$$$$$ $$$$",
"$$ $$$$$$$$$$ $$$$$$$$$$ $$$$",
"$$ $$$$",
"$$ $$$$",
"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$",
"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$",
"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$",
"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$",
"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$",
"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$",
"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"
};

View File

@@ -0,0 +1,54 @@
/* XPM */
static char *statbox_xpm[] = {
/* columns rows colors chars-per-pixel */
"32 32 16 1",
" c Gray0",
". c #808000",
"X c #000080",
"o c #808080",
"O c #000000",
"+ c #808000",
"@ c #000080",
"# c #c0c0c0",
"$ c #808080",
"% c Red",
"& c Green",
"* c Yellow",
"= c Blue",
"- c Magenta",
"; c Cyan",
": c Gray100",
/* pixels */
"################################",
"################################",
"################################",
"######## ### # ### # ########",
"######## ### # ### #### ########",
"######### # ### # #### #########",
"## #### ##### #### #### ##",
"## ###### # #### ### ######## ##",
"## ##### ### ### ### ######## ##",
"## ##### ### ### ### ##### ##",
"## ########################## ##",
"## ########################## ##",
"## ########################## ##",
"## ########################## ##",
"## ########################## ##",
"## ########################## ##",
"## ########################## ##",
"## ########################## ##",
"## ########################## ##",
"## ########################## ##",
"## ########################## ##",
"## ########################## ##",
"## ########################## ##",
"## ########################## ##",
"## ########################## ##",
"## ########################## ##",
"## ########################## ##",
"## ########################## ##",
"## ##",
"################################",
"################################",
"################################"
};

View File

@@ -0,0 +1,54 @@
/* XPM */
static char *stattext_xpm[] = {
/* columns rows colors chars-per-pixel */
"32 32 16 1",
" c Gray0",
". c #808000",
"X c #000080",
"o c #808080",
"O c #000000",
"+ c #808000",
"@ c #000080",
"# c #c0c0c0",
"$ c #808080",
"% c Red",
"& c Green",
"* c Yellow",
"= c Blue",
"- c Magenta",
"; c Cyan",
": c Gray100",
/* pixels */
"################################",
"################################",
"################################",
"################################",
"################################",
"################################",
"############### ################",
"############## ################",
"############# ################",
"############ ################",
"###########$ ################",
"##########$ # ################",
"########## ## ######## # #",
"######### ## ###### # ##",
"########$ ### ###### ### ##",
"#######$ #### ##### #### ##",
"####### ##### #### ### ##",
"######$ #### ### ###",
"#####$ ###### ### #### ###",
"####$ ####### ### ### ###",
"#### ######## ### ### ####",
"### ######## ### ## ####",
"## ######### ## # ##",
"# ##### # ## ###",
"################################",
"################################",
"################################",
"################################",
"################################",
"################################",
"################################",
"################################"
};

View File

@@ -0,0 +1,54 @@
/* XPM */
static char *text_xpm[] = {
/* columns rows colors chars-per-pixel */
"32 32 16 1",
" c Gray0",
". c #808000",
"X c #000080",
"o c #808080",
"O c #000000",
"+ c #808000",
"@ c #000080",
"# c #c0c0c0",
"$ c #808080",
"% c Red",
"& c Green",
"* c Yellow",
"= c Blue",
"- c Magenta",
"; c Cyan",
": c Gray100",
/* pixels */
"################################",
"################################",
"################################",
"################################",
"################################",
"############################ ###",
"############################ ###",
"############### ########## ###",
"############### ########## ###",
"############### ########## ###",
"############### ########## ###",
"############### ########## ###",
"############### ########## ###",
"#### ### #### ###",
"### ##### ## #### ### ###",
"### ##### ## #### ### ###",
"###### ## #### ### ###",
"#### ## #### ### ###",
"### #### ## #### ### ###",
"### #### ## #### ### ###",
"### #### ## #### ### ###",
"### #### ## #### ### ###",
"### #### ## #### ### ###",
"### #### ## #### ### ###",
"#### ## #### ###",
"############################ ###",
"############################ ###",
"################################",
"################################",
"################################",
"################################",
"################################"
};

512
samples/widgets/listbox.cpp Normal file
View File

@@ -0,0 +1,512 @@
/////////////////////////////////////////////////////////////////////////////
// Program: wxWindows Widgets Sample
// Name: listbox.cpp
// Purpose: Part of the widgets sample showing wxListbox
// Author: Vadim Zeitlin
// Created: 27.03.01
// Id: $Id$
// Copyright: (c) 2001 Vadim Zeitlin
// License: wxWindows license
/////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
// for compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
// for all others, include the necessary headers
#ifndef WX_PRECOMP
#include "wx/log.h"
#include "wx/button.h"
#include "wx/checkbox.h"
#include "wx/combobox.h"
#include "wx/listbox.h"
#include "wx/radiobox.h"
#include "wx/statbox.h"
#include "wx/textctrl.h"
#endif
#include "wx/sizer.h"
#include "wx/checklst.h"
#include "widgets.h"
#include "icons/listbox.xpm"
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
// control ids
enum
{
ListboxPage_Reset = 100,
ListboxPage_Add,
ListboxPage_AddText,
ListboxPage_AddSeveral,
ListboxPage_AddMany,
ListboxPage_Clear,
ListboxPage_Change,
ListboxPage_ChangeText,
ListboxPage_Delete,
ListboxPage_DeleteText,
ListboxPage_DeleteSel,
ListboxPage_Listbox
};
// ----------------------------------------------------------------------------
// ListboxWidgetsPage
// ----------------------------------------------------------------------------
class ListboxWidgetsPage : public WidgetsPage
{
public:
ListboxWidgetsPage(wxNotebook *notebook, wxImageList *imaglist);
protected:
// event handlers
void OnButtonReset(wxCommandEvent& event);
void OnButtonChange(wxCommandEvent& event);
void OnButtonDelete(wxCommandEvent& event);
void OnButtonDeleteSel(wxCommandEvent& event);
void OnButtonClear(wxCommandEvent& event);
void OnButtonAdd(wxCommandEvent& event);
void OnButtonAddSeveral(wxCommandEvent& event);
void OnButtonAddMany(wxCommandEvent& event);
void OnListbox(wxCommandEvent& event);
void OnListboxDClick(wxCommandEvent& event);
void OnCheckListbox(wxCommandEvent& event);
void OnCheckOrRadioBox(wxCommandEvent& event);
void OnUpdateUIAddSeveral(wxUpdateUIEvent& event);
void OnUpdateUIClearButton(wxUpdateUIEvent& event);
void OnUpdateUIDeleteButton(wxUpdateUIEvent& event);
void OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event);
void OnUpdateUIResetButton(wxUpdateUIEvent& event);
// reset the listbox parameters
void Reset();
// (re)create the listbox
void CreateLbox();
// listbox parameters
// ------------------
// the selection mode
enum LboxSelection
{
LboxSel_Single,
LboxSel_Extended,
LboxSel_Multiple
} m_lboxSelMode;
// should it be sorted?
bool m_sorted;
// should it have horz scroll/vert scrollbar permanently shown?
bool m_horzScroll,
m_vertScrollAlways;
// the controls
// ------------
// the sel mode radiobox
wxRadioBox *m_radioSelMode;
// the checkboxes
wxCheckBox *m_chkSort,
*m_chkCheck,
*m_chkHScroll,
*m_chkVScroll;
// the listbox itself and the sizer it is in
wxListBox *m_lbox;
wxSizer *m_sizerLbox;
// the text entries for "Add/change string" and "Delete" buttons
wxTextCtrl *m_textAdd,
*m_textChange,
*m_textDelete;
private:
DECLARE_EVENT_TABLE();
DECLARE_WIDGETS_PAGE(ListboxWidgetsPage);
};
// ----------------------------------------------------------------------------
// event tables
// ----------------------------------------------------------------------------
BEGIN_EVENT_TABLE(ListboxWidgetsPage, WidgetsPage)
EVT_BUTTON(ListboxPage_Reset, ListboxWidgetsPage::OnButtonReset)
EVT_BUTTON(ListboxPage_Change, ListboxWidgetsPage::OnButtonChange)
EVT_BUTTON(ListboxPage_Delete, ListboxWidgetsPage::OnButtonDelete)
EVT_BUTTON(ListboxPage_DeleteSel, ListboxWidgetsPage::OnButtonDeleteSel)
EVT_BUTTON(ListboxPage_Clear, ListboxWidgetsPage::OnButtonClear)
EVT_BUTTON(ListboxPage_Add, ListboxWidgetsPage::OnButtonAdd)
EVT_BUTTON(ListboxPage_AddSeveral, ListboxWidgetsPage::OnButtonAddSeveral)
EVT_BUTTON(ListboxPage_AddMany, ListboxWidgetsPage::OnButtonAddMany)
EVT_TEXT_ENTER(ListboxPage_AddText, ListboxWidgetsPage::OnButtonAdd)
EVT_TEXT_ENTER(ListboxPage_DeleteText, ListboxWidgetsPage::OnButtonDelete)
EVT_UPDATE_UI(ListboxPage_Reset, ListboxWidgetsPage::OnUpdateUIResetButton)
EVT_UPDATE_UI(ListboxPage_AddSeveral, ListboxWidgetsPage::OnUpdateUIAddSeveral)
EVT_UPDATE_UI(ListboxPage_Clear, ListboxWidgetsPage::OnUpdateUIClearButton)
EVT_UPDATE_UI(ListboxPage_DeleteText, ListboxWidgetsPage::OnUpdateUIClearButton)
EVT_UPDATE_UI(ListboxPage_Delete, ListboxWidgetsPage::OnUpdateUIDeleteButton)
EVT_UPDATE_UI(ListboxPage_Change, ListboxWidgetsPage::OnUpdateUIDeleteSelButton)
EVT_UPDATE_UI(ListboxPage_ChangeText, ListboxWidgetsPage::OnUpdateUIDeleteSelButton)
EVT_UPDATE_UI(ListboxPage_DeleteSel, ListboxWidgetsPage::OnUpdateUIDeleteSelButton)
EVT_LISTBOX(ListboxPage_Listbox, ListboxWidgetsPage::OnListbox)
EVT_LISTBOX_DCLICK(ListboxPage_Listbox, ListboxWidgetsPage::OnListboxDClick)
EVT_CHECKLISTBOX(ListboxPage_Listbox, ListboxWidgetsPage::OnCheckListbox)
EVT_CHECKBOX(-1, ListboxWidgetsPage::OnCheckOrRadioBox)
EVT_RADIOBOX(-1, ListboxWidgetsPage::OnCheckOrRadioBox)
END_EVENT_TABLE()
// ============================================================================
// implementation
// ============================================================================
IMPLEMENT_WIDGETS_PAGE(ListboxWidgetsPage, _T("Listbox"));
ListboxWidgetsPage::ListboxWidgetsPage(wxNotebook *notebook,
wxImageList *imaglist)
: WidgetsPage(notebook)
{
imaglist->Add(wxBitmap(listbox_xpm));
// init everything
m_radioSelMode = (wxRadioBox *)NULL;
m_chkVScroll =
m_chkHScroll =
m_chkCheck =
m_chkSort = (wxCheckBox *)NULL;
m_lbox = (wxListBox *)NULL;
m_sizerLbox = (wxSizer *)NULL;
/*
What we create here is a frame having 3 panes: style pane is the
leftmost one, in the middle the pane with buttons allowing to perform
miscellaneous listbox operations and the pane containing the listbox
itself to the right
*/
wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
// left pane
wxStaticBox *box = new wxStaticBox(this, -1, _T("&Set listbox parameters"));
wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
static const wxString modes[] =
{
_T("single"),
_T("extended"),
_T("multiple"),
};
m_radioSelMode = new wxRadioBox(this, -1, _T("Selection &mode:"),
wxDefaultPosition, wxDefaultSize,
WXSIZEOF(modes), modes,
1, wxRA_SPECIFY_COLS);
m_chkVScroll = CreateCheckBoxAndAddToSizer
(
sizerLeft,
_T("Always show &vertical scrollbar")
);
m_chkHScroll = CreateCheckBoxAndAddToSizer
(
sizerLeft,
_T("Show &horizontal scrollbar")
);
m_chkCheck = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Check list box"));
m_chkSort = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Sort items"));
sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
sizerLeft->Add(m_radioSelMode, 0, wxGROW | wxALL, 5);
wxButton *btn = new wxButton(this, ListboxPage_Reset, _T("&Reset"));
sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
// middle pane
wxStaticBox *box2 = new wxStaticBox(this, -1, _T("&Change listbox contents"));
wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL);
btn = new wxButton(this, ListboxPage_Add, _T("&Add this string"));
m_textAdd = new wxTextCtrl(this, ListboxPage_AddText, _T("test item 0"));
sizerRow->Add(btn, 0, wxRIGHT, 5);
sizerRow->Add(m_textAdd, 1, wxLEFT, 5);
sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
btn = new wxButton(this, ListboxPage_AddSeveral, _T("&Insert a few strings"));
sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
btn = new wxButton(this, ListboxPage_AddMany, _T("Add &many strings"));
sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
sizerRow = new wxBoxSizer(wxHORIZONTAL);
btn = new wxButton(this, ListboxPage_Change, _T("C&hange current"));
m_textChange = new wxTextCtrl(this, ListboxPage_ChangeText, _T(""));
sizerRow->Add(btn, 0, wxRIGHT, 5);
sizerRow->Add(m_textChange, 1, wxLEFT, 5);
sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
sizerRow = new wxBoxSizer(wxHORIZONTAL);
btn = new wxButton(this, ListboxPage_Delete, _T("&Delete this item"));
m_textDelete = new wxTextCtrl(this, ListboxPage_DeleteText, _T(""));
sizerRow->Add(btn, 0, wxRIGHT, 5);
sizerRow->Add(m_textDelete, 1, wxLEFT, 5);
sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
btn = new wxButton(this, ListboxPage_DeleteSel, _T("Delete &selection"));
sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
btn = new wxButton(this, ListboxPage_Clear, _T("&Clear"));
sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
// right pane
wxSizer *sizerRight = new wxBoxSizer(wxVERTICAL);
m_lbox = new wxListBox(this, ListboxPage_Listbox,
wxDefaultPosition, wxDefaultSize,
0, NULL,
wxLB_HSCROLL);
sizerRight->Add(m_lbox, 1, wxGROW | wxALL, 5);
sizerRight->SetMinSize(250, 0);
m_sizerLbox = 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();
SetAutoLayout(TRUE);
SetSizer(sizerTop);
sizerTop->Fit(this);
}
// ----------------------------------------------------------------------------
// operations
// ----------------------------------------------------------------------------
void ListboxWidgetsPage::Reset()
{
m_radioSelMode->SetSelection(LboxSel_Single);
m_chkSort->SetValue(FALSE);
m_chkCheck->SetValue(FALSE);
m_chkHScroll->SetValue(TRUE);
m_chkVScroll->SetValue(FALSE);
}
void ListboxWidgetsPage::CreateLbox()
{
int flags = 0;
switch ( m_radioSelMode->GetSelection() )
{
default:
wxFAIL_MSG( _T("unexpected radio box selection") );
case LboxSel_Single: flags |= wxLB_SINGLE; break;
case LboxSel_Extended: flags |= wxLB_EXTENDED; break;
case LboxSel_Multiple: flags |= wxLB_MULTIPLE; break;
}
if ( m_chkVScroll->GetValue() )
flags |= wxLB_ALWAYS_SB;
if ( m_chkHScroll->GetValue() )
flags |= wxLB_HSCROLL;
if ( m_chkSort->GetValue() )
flags |= wxLB_SORT;
wxArrayString items;
if ( m_lbox )
{
int count = m_lbox->GetCount();
for ( int n = 0; n < count; n++ )
{
items.Add(m_lbox->GetString(n));
}
m_sizerLbox->Remove(m_lbox);
delete m_lbox;
}
if ( m_chkCheck->GetValue() )
{
m_lbox = new wxCheckListBox(this, ListboxPage_Listbox,
wxDefaultPosition, wxDefaultSize,
0, NULL,
flags);
}
else // just a listbox
{
m_lbox = new wxListBox(this, ListboxPage_Listbox,
wxDefaultPosition, wxDefaultSize,
0, NULL,
flags);
}
m_lbox->Set(items);
m_sizerLbox->Add(m_lbox, 1, wxGROW | wxALL, 5);
m_sizerLbox->Layout();
}
// ----------------------------------------------------------------------------
// event handlers
// ----------------------------------------------------------------------------
void ListboxWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
{
Reset();
CreateLbox();
}
void ListboxWidgetsPage::OnButtonChange(wxCommandEvent& WXUNUSED(event))
{
wxArrayInt selections;
int count = m_lbox->GetSelections(selections);
wxString s = m_textChange->GetValue();
for ( int n = 0; n < count; n++ )
{
m_lbox->SetString(selections[n], s);
}
}
void ListboxWidgetsPage::OnButtonDelete(wxCommandEvent& WXUNUSED(event))
{
unsigned long n;
if ( !m_textDelete->GetValue().ToULong(&n) ||
(n >= (unsigned)m_lbox->GetCount()) )
{
return;
}
m_lbox->Delete(n);
}
void ListboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent& WXUNUSED(event))
{
wxArrayInt selections;
int n = m_lbox->GetSelections(selections);
while ( n > 0 )
{
m_lbox->Delete(selections[--n]);
}
}
void ListboxWidgetsPage::OnButtonClear(wxCommandEvent& event)
{
m_lbox->Clear();
}
void ListboxWidgetsPage::OnButtonAdd(wxCommandEvent& event)
{
static size_t s_item = 0;
wxString s = m_textAdd->GetValue();
if ( !m_textAdd->IsModified() )
{
// update the default string
m_textAdd->SetValue(wxString::Format(_T("test item %u"), ++s_item));
}
m_lbox->Append(s);
}
void ListboxWidgetsPage::OnButtonAddMany(wxCommandEvent& WXUNUSED(event))
{
// "many" means 1000 here
for ( size_t n = 0; n < 1000; n++ )
{
m_lbox->Append(wxString::Format(_T("item #%u"), n));
}
}
void ListboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent& event)
{
wxArrayString items;
items.Add(_T("First"));
items.Add(_T("another one"));
items.Add(_T("and the last (very very very very very very very very very very long) one"));
m_lbox->InsertItems(items, 0);
}
void ListboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
{
event.Enable( (m_radioSelMode->GetSelection() != LboxSel_Single) ||
m_chkSort->GetValue() ||
!m_chkHScroll->GetValue() ||
m_chkVScroll->GetValue() );
}
void ListboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent& event)
{
unsigned long n;
event.Enable(m_textDelete->GetValue().ToULong(&n) &&
(n < (unsigned)m_lbox->GetCount()));
}
void ListboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event)
{
wxArrayInt selections;
event.Enable(m_lbox->GetSelections(selections) != 0);
}
void ListboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent& event)
{
event.Enable(m_lbox->GetCount() != 0);
}
void ListboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent& event)
{
event.Enable(!(m_lbox->GetWindowStyle() & wxLB_SORT));
}
void ListboxWidgetsPage::OnListbox(wxCommandEvent& event)
{
int sel = event.GetInt();
m_textDelete->SetValue(wxString::Format(_T("%ld"), sel));
wxLogMessage(_T("Listbox item %d selected"), sel);
}
void ListboxWidgetsPage::OnListboxDClick(wxCommandEvent& event)
{
wxLogMessage(_T("Listbox item %d double clicked"), event.GetInt());
}
void ListboxWidgetsPage::OnCheckListbox(wxCommandEvent& event)
{
wxLogMessage(_T("Listbox item %d toggled"), event.GetInt());
}
void ListboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& event)
{
CreateLbox();
}

View File

@@ -0,0 +1,543 @@
/////////////////////////////////////////////////////////////////////////////
// Program: wxWindows Widgets Sample
// Name: notebook.cpp
// Purpose: Part of the widgets sample showing wxNotebook
// Author: Vadim Zeitlin
// Created: 06.04.01
// Id: $Id$
// Copyright: (c) 2001 Vadim Zeitlin
// License: wxWindows license
/////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
// for compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
// for all others, include the necessary headers
#ifndef WX_PRECOMP
#include "wx/app.h"
#include "wx/log.h"
#include "wx/button.h"
#include "wx/checkbox.h"
#include "wx/combobox.h"
#include "wx/radiobox.h"
#include "wx/statbox.h"
#include "wx/textctrl.h"
#include "wx/dynarray.h"
#endif
#include "wx/sizer.h"
#include "wx/notebook.h"
#include "widgets.h"
#include "icons/notebook.xpm"
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
// control ids
enum
{
NotebookPage_Reset = 100,
NotebookPage_SelectPage,
NotebookPage_AddPage,
NotebookPage_InsertPage,
NotebookPage_RemovePage,
NotebookPage_DeleteAll,
NotebookPage_InsertText,
NotebookPage_RemoveText,
NotebookPage_SelectText,
NotebookPage_NumPagesText,
NotebookPage_CurSelectText,
NotebookPage_Notebook
};
// notebook orientations
enum Orient
{
Orient_Top,
Orient_Bottom,
Orient_Left,
Orient_Right,
Orient_Max
};
// old versions of wxWindows don't define this style
#ifndef wxNB_TOP
#define wxNB_TOP (0)
#endif
// ----------------------------------------------------------------------------
// NotebookWidgetsPage
// ----------------------------------------------------------------------------
class NotebookWidgetsPage : public WidgetsPage
{
public:
NotebookWidgetsPage(wxNotebook *notebook, wxImageList *imaglist);
virtual ~NotebookWidgetsPage();
protected:
// event handlers
void OnPageChanging(wxNotebookEvent& event);
void OnPageChanged(wxNotebookEvent& event);
void OnButtonReset(wxCommandEvent& event);
void OnButtonDeleteAll(wxCommandEvent& event);
void OnButtonSelectPage(wxCommandEvent& event);
void OnButtonAddPage(wxCommandEvent& event);
void OnButtonInsertPage(wxCommandEvent& event);
void OnButtonRemovePage(wxCommandEvent& event);
void OnCheckOrRadioBox(wxCommandEvent& event);
void OnUpdateUINumPagesText(wxUpdateUIEvent& event);
void OnUpdateUICurSelectText(wxUpdateUIEvent& event);
void OnUpdateUISelectButton(wxUpdateUIEvent& event);
void OnUpdateUIInsertButton(wxUpdateUIEvent& event);
void OnUpdateUIRemoveButton(wxUpdateUIEvent& event);
void OnUpdateUIResetButton(wxUpdateUIEvent& event);
// reset the wxNotebook parameters
void Reset();
// (re)create the wxNotebook
void CreateNotebook();
// create or destroy the image list
void CreateImageList();
// create a new page
wxWindow *CreateNewPage();
// get the image index for the new page
int GetIconIndex() const;
// get the numeric value of text ctrl
int GetTextValue(wxTextCtrl *text) const;
// the controls
// ------------
// the check/radio boxes for styles
wxCheckBox *m_chkImages;
wxRadioBox *m_radioOrient;
// the text controls containing input for various commands
wxTextCtrl *m_textInsert,
*m_textRemove,
*m_textSelect;
// the notebook itself and the sizer it is in
wxNotebook *m_notebook;
wxSizer *m_sizerNotebook;
// thei mage list for our notebook
wxImageList *m_imageList;
private:
DECLARE_EVENT_TABLE();
DECLARE_WIDGETS_PAGE(NotebookWidgetsPage);
};
// ----------------------------------------------------------------------------
// event tables
// ----------------------------------------------------------------------------
BEGIN_EVENT_TABLE(NotebookWidgetsPage, WidgetsPage)
EVT_BUTTON(NotebookPage_Reset, NotebookWidgetsPage::OnButtonReset)
EVT_BUTTON(NotebookPage_SelectPage, NotebookWidgetsPage::OnButtonSelectPage)
EVT_BUTTON(NotebookPage_AddPage, NotebookWidgetsPage::OnButtonAddPage)
EVT_BUTTON(NotebookPage_InsertPage, NotebookWidgetsPage::OnButtonInsertPage)
EVT_BUTTON(NotebookPage_RemovePage, NotebookWidgetsPage::OnButtonRemovePage)
EVT_BUTTON(NotebookPage_DeleteAll, NotebookWidgetsPage::OnButtonDeleteAll)
EVT_UPDATE_UI(NotebookPage_NumPagesText, NotebookWidgetsPage::OnUpdateUINumPagesText)
EVT_UPDATE_UI(NotebookPage_CurSelectText, NotebookWidgetsPage::OnUpdateUICurSelectText)
EVT_UPDATE_UI(NotebookPage_SelectPage, NotebookWidgetsPage::OnUpdateUISelectButton)
EVT_UPDATE_UI(NotebookPage_InsertPage, NotebookWidgetsPage::OnUpdateUIInsertButton)
EVT_UPDATE_UI(NotebookPage_RemovePage, NotebookWidgetsPage::OnUpdateUIRemoveButton)
EVT_NOTEBOOK_PAGE_CHANGING(-1, NotebookWidgetsPage::OnPageChanging)
EVT_NOTEBOOK_PAGE_CHANGED(-1, NotebookWidgetsPage::OnPageChanged)
EVT_CHECKBOX(-1, NotebookWidgetsPage::OnCheckOrRadioBox)
EVT_RADIOBOX(-1, NotebookWidgetsPage::OnCheckOrRadioBox)
END_EVENT_TABLE()
// ============================================================================
// implementation
// ============================================================================
IMPLEMENT_WIDGETS_PAGE(NotebookWidgetsPage, _T("Notebook"));
NotebookWidgetsPage::NotebookWidgetsPage(wxNotebook *notebook,
wxImageList *imaglist)
: WidgetsPage(notebook)
{
imaglist->Add(wxBitmap(notebook_xpm));
// init everything
m_chkImages = NULL;
m_imageList = NULL;
m_notebook = (wxNotebook *)NULL;
m_sizerNotebook = (wxSizer *)NULL;
wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
// left pane
wxStaticBox *box = new wxStaticBox(this, -1, _T("&Set style"));
// must be in sync with Orient enum
wxString orientations[] =
{
_T("&top"),
_T("&bottom"),
_T("&left"),
_T("&right"),
};
wxASSERT_MSG( WXSIZEOF(orientations) == Orient_Max,
_T("forgot to update something") );
m_chkImages = new wxCheckBox(this, -1, _T("Show &images"));
m_radioOrient = new wxRadioBox(this, -1, _T("&Tab orientation"),
wxDefaultPosition, wxDefaultSize,
WXSIZEOF(orientations), orientations,
1, wxRA_SPECIFY_COLS);
wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
sizerLeft->Add(m_chkImages, 0, wxALL, 5);
sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
sizerLeft->Add(m_radioOrient, 0, wxALL, 5);
wxButton *btn = new wxButton(this, NotebookPage_Reset, _T("&Reset"));
sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
// middle pane
wxStaticBox *box2 = new wxStaticBox(this, -1, _T("&Contents"));
wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
wxTextCtrl *text;
wxSizer *sizerRow = CreateSizerWithTextAndLabel(_T("Number of pages: "),
NotebookPage_NumPagesText,
&text);
text->SetEditable(FALSE);
sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
sizerRow = CreateSizerWithTextAndLabel(_T("Current selection: "),
NotebookPage_CurSelectText,
&text);
text->SetEditable(FALSE);
sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
sizerRow = CreateSizerWithTextAndButton(NotebookPage_SelectPage,
_T("&Select page"),
NotebookPage_SelectText,
&m_textSelect);
sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
btn = new wxButton(this, NotebookPage_AddPage, _T("&Add page"));
sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
sizerRow = CreateSizerWithTextAndButton(NotebookPage_InsertPage,
_T("&Insert page at"),
NotebookPage_InsertText,
&m_textInsert);
sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
sizerRow = CreateSizerWithTextAndButton(NotebookPage_RemovePage,
_T("&Remove page"),
NotebookPage_RemoveText,
&m_textRemove);
sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
btn = new wxButton(this, NotebookPage_DeleteAll, _T("&Delete All"));
sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5);
// right pane
wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
m_notebook = new wxNotebook(this, NotebookPage_Notebook);
sizerRight->Add(m_notebook, 1, wxGROW | wxALL, 5);
sizerRight->SetMinSize(250, 0);
m_sizerNotebook = 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, 0, wxGROW | wxALL, 10);
sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
// final initializations
Reset();
CreateImageList();
SetAutoLayout(TRUE);
SetSizer(sizerTop);
sizerTop->Fit(this);
}
NotebookWidgetsPage::~NotebookWidgetsPage()
{
delete m_imageList;
}
// ----------------------------------------------------------------------------
// operations
// ----------------------------------------------------------------------------
void NotebookWidgetsPage::Reset()
{
m_chkImages->SetValue(TRUE);
m_radioOrient->SetSelection(Orient_Top);
}
void NotebookWidgetsPage::CreateImageList()
{
if ( m_chkImages->GetValue() )
{
if ( !m_imageList )
{
// create a dummy image list with a few icons
m_imageList = new wxImageList(32, 32);
m_imageList->Add(wxTheApp->GetStdIcon(wxICON_INFORMATION));
m_imageList->Add(wxTheApp->GetStdIcon(wxICON_QUESTION));
m_imageList->Add(wxTheApp->GetStdIcon(wxICON_WARNING));
m_imageList->Add(wxTheApp->GetStdIcon(wxICON_ERROR));
}
m_notebook->SetImageList(m_imageList);
}
else // no images
{
if ( m_imageList )
{
delete m_imageList;
m_imageList = NULL;
}
}
// because of the bug in wxMSW we can't use SetImageList(NULL) - although
// it would be logical if this removed the image list from notebook, under
// MSW it crashes instead
}
void NotebookWidgetsPage::CreateNotebook()
{
int flags;
switch ( m_radioOrient->GetSelection() )
{
default:
wxFAIL_MSG( _T("unknown notebok orientation") );
// fall through
case Orient_Top:
flags = wxNB_TOP;
break;
case Orient_Bottom:
flags = wxNB_BOTTOM;
break;
case Orient_Left:
flags = wxNB_LEFT;
break;
case Orient_Right:
flags = wxNB_RIGHT;
break;
}
wxNotebook *notebook = m_notebook;
m_notebook = new wxNotebook(this, NotebookPage_Notebook,
wxDefaultPosition, wxDefaultSize,
flags);
CreateImageList();
if ( notebook )
{
int sel = notebook->GetSelection();
int count = notebook->GetPageCount();
for ( int n = 0; n < count; n++ )
{
wxNotebookPage *page = notebook->GetPage(0);
page->Reparent(m_notebook);
m_notebook->AddPage(page, notebook->GetPageText(0), FALSE,
notebook->GetPageImage(0));
notebook->RemovePage(0);
}
m_sizerNotebook->Remove(notebook);
delete notebook;
// restore selection
if ( sel != -1 )
{
m_notebook->SetSelection(sel);
}
}
m_sizerNotebook->Add(m_notebook, 1, wxGROW | wxALL, 5);
m_sizerNotebook->Layout();
}
// ----------------------------------------------------------------------------
// helpers
// ----------------------------------------------------------------------------
int NotebookWidgetsPage::GetTextValue(wxTextCtrl *text) const
{
long pos;
if ( !text->GetValue().ToLong(&pos) )
pos = -1;
return (int)pos;
}
int NotebookWidgetsPage::GetIconIndex() const
{
if ( m_imageList )
{
int nImages = m_imageList->GetImageCount();
if ( nImages > 0 )
{
return m_notebook->GetPageCount() % nImages;
}
}
return -1;
}
wxWindow *NotebookWidgetsPage::CreateNewPage()
{
return new wxTextCtrl(m_notebook, -1, _T("I'm a notebook page"));
}
// ----------------------------------------------------------------------------
// event handlers
// ----------------------------------------------------------------------------
void NotebookWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
{
Reset();
CreateNotebook();
}
void NotebookWidgetsPage::OnButtonDeleteAll(wxCommandEvent& WXUNUSED(event))
{
m_notebook->DeleteAllPages();
}
void NotebookWidgetsPage::OnButtonSelectPage(wxCommandEvent& event)
{
int pos = GetTextValue(m_textSelect);
wxCHECK_RET( pos >= 0, _T("button should be disabled") );
m_notebook->SetSelection(pos);
}
void NotebookWidgetsPage::OnButtonAddPage(wxCommandEvent& WXUNUSED(event))
{
m_notebook->AddPage(CreateNewPage(), _T("Added page"), FALSE,
GetIconIndex());
}
void NotebookWidgetsPage::OnButtonInsertPage(wxCommandEvent& WXUNUSED(event))
{
int pos = GetTextValue(m_textInsert);
wxCHECK_RET( pos >= 0, _T("button should be disabled") );
m_notebook->InsertPage(pos, CreateNewPage(), _T("Inserted page"), FALSE,
GetIconIndex());
}
void NotebookWidgetsPage::OnButtonRemovePage(wxCommandEvent& WXUNUSED(event))
{
int pos = GetTextValue(m_textRemove);
wxCHECK_RET( pos >= 0, _T("button should be disabled") );
m_notebook->DeletePage(pos);
}
void NotebookWidgetsPage::OnUpdateUISelectButton(wxUpdateUIEvent& event)
{
event.Enable( GetTextValue(m_textSelect) >= 0 );
}
void NotebookWidgetsPage::OnUpdateUIInsertButton(wxUpdateUIEvent& event)
{
event.Enable( GetTextValue(m_textInsert) >= 0 );
}
void NotebookWidgetsPage::OnUpdateUIRemoveButton(wxUpdateUIEvent& event)
{
event.Enable( GetTextValue(m_textRemove) >= 0 );
}
void NotebookWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
{
event.Enable( !m_chkImages->GetValue() ||
m_radioOrient->GetSelection() != wxNB_TOP );
}
void NotebookWidgetsPage::OnUpdateUINumPagesText(wxUpdateUIEvent& event)
{
event.SetText( wxString::Format(_T("%d"), m_notebook->GetPageCount()) );
}
void NotebookWidgetsPage::OnUpdateUICurSelectText(wxUpdateUIEvent& event)
{
event.SetText( wxString::Format(_T("%d"), m_notebook->GetSelection()) );
}
void NotebookWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& event)
{
CreateNotebook();
}
void NotebookWidgetsPage::OnPageChanging(wxNotebookEvent& event)
{
wxLogMessage(_T("Notebook page changing from %d to %d (currently %d)."),
event.GetOldSelection(),
event.GetSelection(),
m_notebook->GetSelection());
event.Skip();
}
void NotebookWidgetsPage::OnPageChanged(wxNotebookEvent& event)
{
wxLogMessage(_T("Notebook page changed from %d to %d (currently %d)."),
event.GetOldSelection(),
event.GetSelection(),
m_notebook->GetSelection());
event.Skip();
}

View File

@@ -0,0 +1,455 @@
/////////////////////////////////////////////////////////////////////////////
// Program: wxWindows Widgets Sample
// Name: radiobox.cpp
// Purpose: Part of the widgets sample showing wxRadioBox
// Author: Vadim Zeitlin
// Created: 15.04.01
// Id: $Id$
// Copyright: (c) 2001 Vadim Zeitlin
// License: wxWindows license
/////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
// for compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
// for all others, include the necessary headers
#ifndef WX_PRECOMP
#include "wx/log.h"
#include "wx/button.h"
#include "wx/checkbox.h"
#include "wx/radiobox.h"
#include "wx/statbox.h"
#include "wx/textctrl.h"
#endif
#include "wx/sizer.h"
#include "widgets.h"
#include "icons/radiobox.xpm"
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
// control ids
enum
{
RadioPage_Reset = 100,
RadioPage_Update,
RadioPage_Selection,
RadioPage_Label,
RadioPage_LabelBtn,
RadioPage_Radio
};
// layout direction radiobox selections
enum
{
RadioDir_Default,
RadioDir_LtoR,
RadioDir_TtoB
};
// default values for the number of radiobox items
static const size_t DEFAULT_NUM_ENTRIES = 12;
static const size_t DEFAULT_MAJOR_DIM = 4;
// ----------------------------------------------------------------------------
// RadioWidgetsPage
// ----------------------------------------------------------------------------
class RadioWidgetsPage : public WidgetsPage
{
public:
RadioWidgetsPage(wxNotebook *notebook, wxImageList *imaglist);
virtual ~RadioWidgetsPage();
protected:
// event handlers
void OnCheckOrRadioBox(wxCommandEvent& event);
void OnRadioBox(wxCommandEvent& event);
void OnButtonReset(wxCommandEvent& event);
void OnButtonRecreate(wxCommandEvent& event);
void OnButtonSelection(wxCommandEvent& event);
void OnButtonSetLabel(wxCommandEvent& event);
void OnUpdateUIReset(wxUpdateUIEvent& event);
void OnUpdateUIUpdate(wxUpdateUIEvent& event);
void OnUpdateUISelection(wxUpdateUIEvent& event);
// reset the wxRadioBox parameters
void Reset();
// (re)create the wxRadioBox
void CreateRadio();
// the controls
// ------------
// the check/radio boxes for styles
wxCheckBox *m_chkVert;
wxRadioBox *m_radioDir;
// the gauge itself and the sizer it is in
wxRadioBox *m_radio;
wxSizer *m_sizerRadio;
// the text entries for command parameters
wxTextCtrl *m_textNumBtns,
*m_textMajorDim,
*m_textCurSel,
*m_textSel,
*m_textLabel,
*m_textLabelBtns;
private:
DECLARE_EVENT_TABLE();
DECLARE_WIDGETS_PAGE(RadioWidgetsPage);
};
// ----------------------------------------------------------------------------
// event tables
// ----------------------------------------------------------------------------
BEGIN_EVENT_TABLE(RadioWidgetsPage, WidgetsPage)
EVT_BUTTON(RadioPage_Reset, RadioWidgetsPage::OnButtonReset)
EVT_BUTTON(RadioPage_Update, RadioWidgetsPage::OnButtonRecreate)
EVT_BUTTON(RadioPage_LabelBtn, RadioWidgetsPage::OnButtonRecreate)
EVT_BUTTON(RadioPage_Selection, RadioWidgetsPage::OnButtonSelection)
EVT_BUTTON(RadioPage_Label, RadioWidgetsPage::OnButtonSetLabel)
EVT_UPDATE_UI(RadioPage_Update, RadioWidgetsPage::OnUpdateUIUpdate)
EVT_UPDATE_UI(RadioPage_Selection, RadioWidgetsPage::OnUpdateUISelection)
EVT_RADIOBOX(RadioPage_Radio, RadioWidgetsPage::OnRadioBox)
EVT_CHECKBOX(-1, RadioWidgetsPage::OnCheckOrRadioBox)
EVT_RADIOBOX(-1, RadioWidgetsPage::OnCheckOrRadioBox)
END_EVENT_TABLE()
// ============================================================================
// implementation
// ============================================================================
IMPLEMENT_WIDGETS_PAGE(RadioWidgetsPage, _T("Radio"));
RadioWidgetsPage::RadioWidgetsPage(wxNotebook *notebook,
wxImageList *imaglist)
: WidgetsPage(notebook)
{
imaglist->Add(wxBitmap(radio_xpm));
// init everything
m_chkVert = (wxCheckBox *)NULL;
m_textNumBtns =
m_textLabelBtns =
m_textLabel = (wxTextCtrl *)NULL;
m_radio =
m_radioDir = (wxRadioBox *)NULL;
m_sizerRadio = (wxSizer *)NULL;
wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
// left pane
wxStaticBox *box = new wxStaticBox(this, -1, _T("&Set style"));
wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
m_chkVert = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Vertical layout"));
static const wxString layoutDir[] =
{
_T("default"),
_T("left to right"),
_T("top to bottom")
};
m_radioDir = new wxRadioBox(this, -1, _T("Numbering:"),
wxDefaultPosition, wxDefaultSize,
WXSIZEOF(layoutDir), layoutDir);
sizerLeft->Add(m_radioDir, 0, wxGROW | wxALL, 5);
// if it's not defined, we can't change the radiobox direction
#ifndef wxRA_LEFTTORIGHT
m_radioDir->Disable();
#endif // wxRA_LEFTTORIGHT
wxSizer *sizerRow;
sizerRow = CreateSizerWithTextAndLabel(_T("&Major dimension"),
-1,
&m_textMajorDim);
sizerLeft->Add(sizerRow, 0, wxGROW | wxALL, 5);
sizerRow = CreateSizerWithTextAndLabel(_T("&Number of buttons"),
-1,
&m_textNumBtns);
sizerLeft->Add(sizerRow, 0, wxGROW | wxALL, 5);
wxButton *btn;
btn = new wxButton(this, RadioPage_Update, _T("&Update"));
sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 5);
sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
btn = new wxButton(this, RadioPage_Reset, _T("&Reset"));
sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
// middle pane
wxStaticBox *box2 = new wxStaticBox(this, -1, _T("&Change parameters"));
wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
sizerRow = CreateSizerWithTextAndLabel(_T("Current selection"),
-1,
&m_textCurSel);
sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
sizerRow = CreateSizerWithTextAndButton(RadioPage_Selection,
_T("&Change selection"),
-1,
&m_textSel);
sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
sizerRow = CreateSizerWithTextAndButton(RadioPage_Label,
_T("&Label for box"),
-1,
&m_textLabel);
sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
sizerRow = CreateSizerWithTextAndButton(RadioPage_LabelBtn,
_T("&Label for buttons"),
-1,
&m_textLabelBtns);
sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
// right pane
wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
sizerRight->SetMinSize(250, 0);
m_sizerRadio = sizerRight; // save it to modify it later
Reset();
CreateRadio();
// 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
SetAutoLayout(TRUE);
SetSizer(sizerTop);
sizerTop->Fit(this);
}
RadioWidgetsPage::~RadioWidgetsPage()
{
}
// ----------------------------------------------------------------------------
// operations
// ----------------------------------------------------------------------------
void RadioWidgetsPage::Reset()
{
m_textMajorDim->SetValue(wxString::Format(_T("%d"), DEFAULT_MAJOR_DIM));
m_textNumBtns->SetValue(wxString::Format(_T("%d"), DEFAULT_NUM_ENTRIES));
m_textLabel->SetValue(_T("I'm a radiobox"));
m_textLabelBtns->SetValue(_T("item"));
m_chkVert->SetValue(FALSE);
m_radioDir->SetSelection(RadioDir_Default);
}
void RadioWidgetsPage::CreateRadio()
{
int sel;
if ( m_radio )
{
sel = m_radio->GetSelection();
m_sizerRadio->Remove(m_radio);
delete m_radio;
}
else // first time creation, no old selection to preserve
{
sel = -1;
}
unsigned long count;
if ( !m_textNumBtns->GetValue().ToULong(&count) )
{
wxLogWarning(_T("Should have a valid number for number of items."));
// fall back to default
count = DEFAULT_NUM_ENTRIES;
}
unsigned long majorDim;
if ( !m_textMajorDim->GetValue().ToULong(&majorDim) )
{
wxLogWarning(_T("Should have a valid major dimension number."));
// fall back to default
majorDim = DEFAULT_MAJOR_DIM;
}
wxString *items = new wxString[count];
wxString labelBtn = m_textLabelBtns->GetValue();
for ( size_t n = 0; n < count; n++ )
{
items[n] = wxString::Format(_T("%s %u"), labelBtn.c_str(), n + 1);
}
int flags = m_chkVert->GetValue() ? wxRA_VERTICAL
: wxRA_HORIZONTAL;
#ifdef wxRA_LEFTTORIGHT
switch ( m_radioDir->GetSelection() )
{
default:
wxFAIL_MSG( _T("unexpected wxRadioBox layout direction") );
// fall through
case RadioDir_Default:
break;
case RadioDir_LtoR:
flags |= wxRA_LEFTTORIGHT;
break;
case RadioDir_TtoB:
flags |= wxRA_TOPTOBOTTOM;
break;
}
#endif // wxRA_LEFTTORIGHT
m_radio = new wxRadioBox(this, RadioPage_Radio,
m_textLabel->GetValue(),
wxDefaultPosition, wxDefaultSize,
count, items,
majorDim,
flags);
delete [] items;
if ( sel >= 0 && (size_t)sel < count )
{
m_radio->SetSelection(sel);
}
m_sizerRadio->Add(m_radio, 1, wxGROW);
m_sizerRadio->Layout();
}
// ----------------------------------------------------------------------------
// event handlers
// ----------------------------------------------------------------------------
void RadioWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
{
Reset();
CreateRadio();
}
void RadioWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& event)
{
CreateRadio();
}
void RadioWidgetsPage::OnRadioBox(wxCommandEvent& event)
{
int sel = m_radio->GetSelection();
wxLogMessage(_T("Radiobox selection changed, now %d"), sel);
wxASSERT_MSG( sel == event.GetSelection(),
_T("selection should be the same in event and radiobox") );
m_textCurSel->SetValue(wxString::Format(_T("%d"), sel));
}
void RadioWidgetsPage::OnButtonRecreate(wxCommandEvent& WXUNUSED(event))
{
CreateRadio();
}
void RadioWidgetsPage::OnButtonSetLabel(wxCommandEvent& WXUNUSED(event))
{
m_radio->wxControl::SetLabel(m_textLabel->GetValue());
}
void RadioWidgetsPage::OnButtonSelection(wxCommandEvent& WXUNUSED(event))
{
unsigned long sel;
if ( !m_textSel->GetValue().ToULong(&sel) ||
(sel >= (size_t)m_radio->GetCount()) )
{
wxLogWarning(_T("Invalid number specified as new selection."));
}
else
{
m_radio->SetSelection(sel);
}
}
void RadioWidgetsPage::OnUpdateUIUpdate(wxUpdateUIEvent& event)
{
unsigned long n;
event.Enable( m_textNumBtns->GetValue().ToULong(&n) &&
m_textMajorDim->GetValue().ToULong(&n) );
}
void RadioWidgetsPage::OnUpdateUISelection(wxUpdateUIEvent& event)
{
unsigned long n;
event.Enable( m_textSel->GetValue().ToULong(&n) &&
(n < (size_t)m_radio->GetCount()) );
}
void RadioWidgetsPage::OnUpdateUIReset(wxUpdateUIEvent& event)
{
// only enable it if something is not set to default
bool enable = m_chkVert->GetValue();
if ( !enable )
{
unsigned long numEntries;
enable = !m_textNumBtns->GetValue().ToULong(&numEntries) ||
numEntries != DEFAULT_NUM_ENTRIES;
if ( !enable )
{
unsigned long majorDim;
enable = !m_textMajorDim->GetValue().ToULong(&majorDim) ||
majorDim != DEFAULT_MAJOR_DIM;
}
}
event.Enable(enable);
}

470
samples/widgets/slider.cpp Normal file
View File

@@ -0,0 +1,470 @@
/////////////////////////////////////////////////////////////////////////////
// Program: wxWindows Widgets Sample
// Name: slider.cpp
// Purpose: Part of the widgets sample showing wxSlider
// Author: Vadim Zeitlin
// Created: 16.04.01
// Id: $Id$
// Copyright: (c) 2001 Vadim Zeitlin
// License: wxWindows license
/////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
// for compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
// for all others, include the necessary headers
#ifndef WX_PRECOMP
#include "wx/log.h"
#include "wx/button.h"
#include "wx/checkbox.h"
#include "wx/radiobox.h"
#include "wx/slider.h"
#include "wx/statbox.h"
#include "wx/textctrl.h"
#endif
#include "wx/sizer.h"
#include "widgets.h"
#include "icons/slider.xpm"
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
// control ids
enum
{
SliderPage_Reset = 100,
SliderPage_Clear,
SliderPage_SetValue,
SliderPage_SetMinAndMax,
SliderPage_SetTickFreq,
SliderPage_CurValueText,
SliderPage_ValueText,
SliderPage_MinText,
SliderPage_MaxText,
SliderPage_TickFreqText,
SliderPage_OtherSide,
SliderPage_Slider
};
// ----------------------------------------------------------------------------
// SliderWidgetsPage
// ----------------------------------------------------------------------------
class SliderWidgetsPage : public WidgetsPage
{
public:
SliderWidgetsPage(wxNotebook *notebook, wxImageList *imaglist);
virtual ~SliderWidgetsPage();
protected:
// event handlers
void OnButtonReset(wxCommandEvent& event);
void OnButtonClear(wxCommandEvent& event);
void OnButtonSetValue(wxCommandEvent& event);
void OnButtonSetMinAndMax(wxCommandEvent& event);
void OnButtonSetTickFreq(wxCommandEvent& event);
void OnCheckOrRadioBox(wxCommandEvent& event);
void OnSlider(wxCommandEvent& event);
void OnUpdateUIOtherSide(wxUpdateUIEvent& event);
void OnUpdateUIValueButton(wxUpdateUIEvent& event);
void OnUpdateUIMinMaxButton(wxUpdateUIEvent& event);
void OnUpdateUITickFreq(wxUpdateUIEvent& event);
void OnUpdateUIResetButton(wxUpdateUIEvent& event);
void OnUpdateUICurValueText(wxUpdateUIEvent& event);
// reset the slider parameters
void Reset();
// (re)create the slider
void CreateSlider();
// set the tick frequency from the text field value
void DoSetTickFreq();
// is this slider value in range?
bool IsValidValue(int val) const
{ return (val >= m_min) && (val <= m_max); }
// the slider range
int m_min, m_max;
// the controls
// ------------
// the check/radio boxes for styles
wxCheckBox *m_chkLabels,
*m_chkOtherSide,
*m_chkVert,
*m_chkTicks;
// the slider itself and the sizer it is in
wxSlider *m_slider;
wxSizer *m_sizerSlider;
// the text entries for set value/range
wxTextCtrl *m_textValue,
*m_textMin,
*m_textMax,
*m_textTickFreq;
private:
DECLARE_EVENT_TABLE();
DECLARE_WIDGETS_PAGE(SliderWidgetsPage);
};
// ----------------------------------------------------------------------------
// event tables
// ----------------------------------------------------------------------------
BEGIN_EVENT_TABLE(SliderWidgetsPage, WidgetsPage)
EVT_BUTTON(SliderPage_Reset, SliderWidgetsPage::OnButtonReset)
EVT_BUTTON(SliderPage_SetValue, SliderWidgetsPage::OnButtonSetValue)
EVT_BUTTON(SliderPage_SetMinAndMax, SliderWidgetsPage::OnButtonSetMinAndMax)
EVT_BUTTON(SliderPage_SetTickFreq, SliderWidgetsPage::OnButtonSetTickFreq)
EVT_UPDATE_UI(SliderPage_OtherSide, SliderWidgetsPage::OnUpdateUIOtherSide)
EVT_UPDATE_UI(SliderPage_SetValue, SliderWidgetsPage::OnUpdateUIValueButton)
EVT_UPDATE_UI(SliderPage_SetMinAndMax, SliderWidgetsPage::OnUpdateUIMinMaxButton)
EVT_UPDATE_UI(SliderPage_SetTickFreq, SliderWidgetsPage::OnUpdateUITickFreq)
EVT_UPDATE_UI(SliderPage_TickFreqText, SliderWidgetsPage::OnUpdateUITickFreq)
EVT_UPDATE_UI(SliderPage_Reset, SliderWidgetsPage::OnUpdateUIResetButton)
EVT_UPDATE_UI(SliderPage_CurValueText, SliderWidgetsPage::OnUpdateUICurValueText)
EVT_SLIDER(SliderPage_Slider, SliderWidgetsPage::OnSlider)
EVT_CHECKBOX(-1, SliderWidgetsPage::OnCheckOrRadioBox)
EVT_RADIOBOX(-1, SliderWidgetsPage::OnCheckOrRadioBox)
END_EVENT_TABLE()
// ============================================================================
// implementation
// ============================================================================
IMPLEMENT_WIDGETS_PAGE(SliderWidgetsPage, _T("Slider"));
SliderWidgetsPage::SliderWidgetsPage(wxNotebook *notebook,
wxImageList *imaglist)
: WidgetsPage(notebook)
{
imaglist->Add(wxBitmap(slider_xpm));
// init everything
m_min = 0;
m_max = 100;
m_chkVert =
m_chkTicks =
m_chkLabels =
m_chkOtherSide = (wxCheckBox *)NULL;
m_slider = (wxSlider *)NULL;
m_sizerSlider = (wxSizer *)NULL;
wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
// left pane
wxStaticBox *box = new wxStaticBox(this, -1, _T("&Set style"));
wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
m_chkVert = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Vertical"));
m_chkTicks = CreateCheckBoxAndAddToSizer(sizerLeft, _T("Show &ticks"));
m_chkLabels = CreateCheckBoxAndAddToSizer(sizerLeft, _T("Show &labels"));
m_chkOtherSide = CreateCheckBoxAndAddToSizer
(
sizerLeft,
_T("On &other side"),
SliderPage_OtherSide
);
sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
wxButton *btn = new wxButton(this, SliderPage_Reset, _T("&Reset"));
sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
// middle pane
wxStaticBox *box2 = new wxStaticBox(this, -1, _T("&Change slider value"));
wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
wxTextCtrl *text;
wxSizer *sizerRow = CreateSizerWithTextAndLabel(_T("Current value"),
SliderPage_CurValueText,
&text);
text->SetEditable(FALSE);
sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
sizerRow = CreateSizerWithTextAndButton(SliderPage_SetValue,
_T("Set &value"),
SliderPage_ValueText,
&m_textValue);
sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
sizerRow = CreateSizerWithTextAndButton(SliderPage_SetMinAndMax,
_T("&Min and max"),
SliderPage_MinText,
&m_textMin);
m_textMax = new wxTextCtrl(this, SliderPage_MaxText, _T(""));
sizerRow->Add(m_textMax, 1, wxLEFT | wxALIGN_CENTRE_VERTICAL, 5);
m_textMin->SetValue(wxString::Format(_T("%lu"), m_min));
m_textMax->SetValue(wxString::Format(_T("%lu"), m_max));
sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
sizerRow = CreateSizerWithTextAndButton(SliderPage_SetTickFreq,
_T("Tick &frequency"),
SliderPage_TickFreqText,
&m_textTickFreq);
m_textTickFreq->SetValue(_T("10"));
sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
// right pane
wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
sizerRight->SetMinSize(250, 0);
m_sizerSlider = sizerRight; // save it to modify it later
Reset();
CreateSlider();
// 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
SetAutoLayout(TRUE);
SetSizer(sizerTop);
sizerTop->Fit(this);
}
SliderWidgetsPage::~SliderWidgetsPage()
{
}
// ----------------------------------------------------------------------------
// operations
// ----------------------------------------------------------------------------
void SliderWidgetsPage::Reset()
{
m_chkLabels->SetValue(TRUE);
m_chkTicks->SetValue(FALSE);
m_chkVert->SetValue(FALSE);
m_chkOtherSide->SetValue(FALSE);
}
void SliderWidgetsPage::CreateSlider()
{
int flags = 0;
bool isVert = m_chkVert->GetValue();
if ( isVert )
flags |= wxSL_VERTICAL;
else
flags |= wxSL_HORIZONTAL;
if ( m_chkLabels->GetValue() )
{
flags |= wxSL_LABELS;
if ( m_chkOtherSide->GetValue() )
flags |= isVert ? wxSL_RIGHT : wxSL_BOTTOM;
else
flags |= isVert ? wxSL_LEFT : wxSL_TOP;
}
if ( m_chkTicks->GetValue() )
{
flags |= wxSL_AUTOTICKS;
}
int val = m_min;
if ( m_slider )
{
int valOld = m_slider->GetValue();
if ( !IsValidValue(valOld) )
{
val = valOld;
}
m_sizerSlider->Remove(m_slider);
if ( m_sizerSlider->GetChildren().GetCount() )
{
// we have 2 spacers, remove them too
m_sizerSlider->Remove((int)0);
m_sizerSlider->Remove((int)0);
}
delete m_slider;
}
m_slider = new wxSlider(this, SliderPage_Slider,
val, m_min, m_max,
wxDefaultPosition, wxDefaultSize,
flags);
if ( isVert )
{
m_sizerSlider->Add(0, 0, 1);
m_sizerSlider->Add(m_slider, 0, wxGROW | wxALL, 5);
m_sizerSlider->Add(0, 0, 1);
}
else
{
m_sizerSlider->Add(m_slider, 1, wxCENTRE | wxALL, 5);
}
if ( m_chkTicks->GetValue() )
{
DoSetTickFreq();
}
m_sizerSlider->Layout();
}
void SliderWidgetsPage::DoSetTickFreq()
{
long freq;
if ( !m_textTickFreq->GetValue().ToLong(&freq) )
{
wxLogWarning(_T("Invalid slider tick frequency"));
return;
}
m_slider->SetTickFreq(freq, 0 /* unused */);
}
// ----------------------------------------------------------------------------
// event handlers
// ----------------------------------------------------------------------------
void SliderWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
{
Reset();
CreateSlider();
}
void SliderWidgetsPage::OnButtonSetTickFreq(wxCommandEvent& WXUNUSED(event))
{
DoSetTickFreq();
}
void SliderWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent& WXUNUSED(event))
{
long minNew,
maxNew = 0; // init to suppress compiler warning
if ( !m_textMin->GetValue().ToLong(&minNew) ||
!m_textMax->GetValue().ToLong(&maxNew) ||
minNew >= maxNew )
{
wxLogWarning(_T("Invalid min/max values for the slider."));
return;
}
m_min = minNew;
m_max = maxNew;
m_slider->SetRange(minNew, maxNew);
}
void SliderWidgetsPage::OnButtonSetValue(wxCommandEvent& WXUNUSED(event))
{
long val;
if ( !m_textValue->GetValue().ToLong(&val) || !IsValidValue(val) )
{
wxLogWarning(_T("Invalid slider value."));
return;
}
m_slider->SetValue(val);
}
void SliderWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent& event)
{
long val;
event.Enable( m_textValue->GetValue().ToLong(&val) && IsValidValue(val) );
}
void SliderWidgetsPage::OnUpdateUITickFreq(wxUpdateUIEvent& event)
{
long freq;
event.Enable( m_chkTicks->GetValue() &&
m_textTickFreq->GetValue().ToLong(&freq) &&
(freq > 0) && (freq <= m_max - m_min) );
}
void SliderWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent& event)
{
long mn, mx;
event.Enable( m_textMin->GetValue().ToLong(&mn) &&
m_textMax->GetValue().ToLong(&mx) &&
mn < mx);
}
void SliderWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
{
event.Enable( m_chkVert->GetValue() ||
!m_chkLabels->GetValue() ||
m_chkOtherSide->GetValue() ||
m_chkTicks->GetValue() );
}
void SliderWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& event)
{
CreateSlider();
}
void SliderWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent& event)
{
event.SetText( wxString::Format(_T("%d"), m_slider->GetValue()));
}
void SliderWidgetsPage::OnUpdateUIOtherSide(wxUpdateUIEvent& event)
{
event.Enable( m_chkLabels->GetValue() );
}
void SliderWidgetsPage::OnSlider(wxCommandEvent& event)
{
int value = event.GetInt();
wxASSERT_MSG( value == m_slider->GetValue(),
_T("slider value should be the same") );
wxLogMessage(_T("Slider value changed, now %d"), value);
}

419
samples/widgets/spinbtn.cpp Normal file
View File

@@ -0,0 +1,419 @@
/////////////////////////////////////////////////////////////////////////////
// Program: wxWindows Widgets Sample
// Name: spinbtn.cpp
// Purpose: Part of the widgets sample showing wxSpinButton
// Author: Vadim Zeitlin
// Created: 16.04.01
// Id: $Id$
// Copyright: (c) 2001 Vadim Zeitlin
// License: wxWindows license
/////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
// for compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
// for all others, include the necessary headers
#ifndef WX_PRECOMP
#include "wx/log.h"
#include "wx/button.h"
#include "wx/checkbox.h"
#include "wx/radiobox.h"
#include "wx/statbox.h"
#include "wx/textctrl.h"
#endif
#include "wx/spinbutt.h"
#include "wx/spinctrl.h"
#include "wx/sizer.h"
#include "widgets.h"
#include "icons/spinbtn.xpm"
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
// control ids
enum
{
SpinBtnPage_Reset = 100,
SpinBtnPage_Clear,
SpinBtnPage_SetValue,
SpinBtnPage_SetMinAndMax,
SpinBtnPage_CurValueText,
SpinBtnPage_ValueText,
SpinBtnPage_MinText,
SpinBtnPage_MaxText,
SpinBtnPage_SpinBtn,
SpinBtnPage_SpinCtrl
};
// ----------------------------------------------------------------------------
// SpinBtnWidgetsPage
// ----------------------------------------------------------------------------
class SpinBtnWidgetsPage : public WidgetsPage
{
public:
SpinBtnWidgetsPage(wxNotebook *notebook, wxImageList *imaglist);
virtual ~SpinBtnWidgetsPage();
protected:
// event handlers
void OnButtonReset(wxCommandEvent& event);
void OnButtonClear(wxCommandEvent& event);
void OnButtonSetValue(wxCommandEvent& event);
void OnButtonSetMinAndMax(wxCommandEvent& event);
void OnCheckOrRadioBox(wxCommandEvent& event);
void OnSpinBtn(wxCommandEvent& event);
void OnSpinBtnUp(wxCommandEvent& event);
void OnSpinBtnDown(wxCommandEvent& event);
void OnSpinCtrl(wxCommandEvent& event);
void OnUpdateUIValueButton(wxUpdateUIEvent& event);
void OnUpdateUIMinMaxButton(wxUpdateUIEvent& event);
void OnUpdateUIResetButton(wxUpdateUIEvent& event);
void OnUpdateUICurValueText(wxUpdateUIEvent& event);
// reset the spinbtn parameters
void Reset();
// (re)create the spinbtn
void CreateSpin();
// is this spinbtn value in range?
bool IsValidValue(int val) const
{ return (val >= m_min) && (val <= m_max); }
// the spinbtn range
int m_min, m_max;
// the controls
// ------------
// the check/radio boxes for styles
wxCheckBox *m_chkVert,
*m_chkWrap;
// the spinbtn and the spinctrl and the sizer containing them
wxSpinButton *m_spinbtn;
wxSpinCtrl *m_spinctrl;
wxSizer *m_sizerSpin;
// the text entries for set value/range
wxTextCtrl *m_textValue,
*m_textMin,
*m_textMax;
private:
DECLARE_EVENT_TABLE();
DECLARE_WIDGETS_PAGE(SpinBtnWidgetsPage);
};
// ----------------------------------------------------------------------------
// event tables
// ----------------------------------------------------------------------------
BEGIN_EVENT_TABLE(SpinBtnWidgetsPage, WidgetsPage)
EVT_BUTTON(SpinBtnPage_Reset, SpinBtnWidgetsPage::OnButtonReset)
EVT_BUTTON(SpinBtnPage_SetValue, SpinBtnWidgetsPage::OnButtonSetValue)
EVT_BUTTON(SpinBtnPage_SetMinAndMax, SpinBtnWidgetsPage::OnButtonSetMinAndMax)
EVT_UPDATE_UI(SpinBtnPage_SetValue, SpinBtnWidgetsPage::OnUpdateUIValueButton)
EVT_UPDATE_UI(SpinBtnPage_SetMinAndMax, SpinBtnWidgetsPage::OnUpdateUIMinMaxButton)
EVT_UPDATE_UI(SpinBtnPage_Reset, SpinBtnWidgetsPage::OnUpdateUIResetButton)
EVT_UPDATE_UI(SpinBtnPage_CurValueText, SpinBtnWidgetsPage::OnUpdateUICurValueText)
EVT_SPIN(SpinBtnPage_SpinBtn, SpinBtnWidgetsPage::OnSpinBtn)
EVT_SPIN_UP(SpinBtnPage_SpinBtn, SpinBtnWidgetsPage::OnSpinBtnUp)
EVT_SPIN_DOWN(SpinBtnPage_SpinBtn, SpinBtnWidgetsPage::OnSpinBtnDown)
EVT_SPINCTRL(SpinBtnPage_SpinCtrl, SpinBtnWidgetsPage::OnSpinCtrl)
EVT_CHECKBOX(-1, SpinBtnWidgetsPage::OnCheckOrRadioBox)
EVT_RADIOBOX(-1, SpinBtnWidgetsPage::OnCheckOrRadioBox)
END_EVENT_TABLE()
// ============================================================================
// implementation
// ============================================================================
IMPLEMENT_WIDGETS_PAGE(SpinBtnWidgetsPage, _T("Spin"));
SpinBtnWidgetsPage::SpinBtnWidgetsPage(wxNotebook *notebook,
wxImageList *imaglist)
: WidgetsPage(notebook)
{
imaglist->Add(wxBitmap(spinbtn_xpm));
// init everything
m_min = 0;
m_max = 10;
m_chkVert =
m_chkWrap = (wxCheckBox *)NULL;
m_spinbtn = (wxSpinButton *)NULL;
m_sizerSpin = (wxSizer *)NULL;
wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
// left pane
wxStaticBox *box = new wxStaticBox(this, -1, _T("&Set style"));
wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
m_chkVert = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Vertical"));
m_chkWrap = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Wrap"));
sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
wxButton *btn = new wxButton(this, SpinBtnPage_Reset, _T("&Reset"));
sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
// middle pane
wxStaticBox *box2 = new wxStaticBox(this, -1, _T("&Change spinbtn value"));
wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
wxTextCtrl *text;
wxSizer *sizerRow = CreateSizerWithTextAndLabel(_T("Current value"),
SpinBtnPage_CurValueText,
&text);
text->SetEditable(FALSE);
sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
sizerRow = CreateSizerWithTextAndButton(SpinBtnPage_SetValue,
_T("Set &value"),
SpinBtnPage_ValueText,
&m_textValue);
sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
sizerRow = CreateSizerWithTextAndButton(SpinBtnPage_SetMinAndMax,
_T("&Min and max"),
SpinBtnPage_MinText,
&m_textMin);
m_textMax = new wxTextCtrl(this, SpinBtnPage_MaxText, _T(""));
sizerRow->Add(m_textMax, 1, wxLEFT | wxALIGN_CENTRE_VERTICAL, 5);
m_textMin->SetValue(wxString::Format(_T("%lu"), m_min));
m_textMax->SetValue(wxString::Format(_T("%lu"), m_max));
sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5);
// right pane
wxSizer *sizerRight = new wxBoxSizer(wxVERTICAL);
sizerRight->SetMinSize(250, 0);
m_sizerSpin = sizerRight; // save it to modify it later
Reset();
CreateSpin();
// 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
SetAutoLayout(TRUE);
SetSizer(sizerTop);
sizerTop->Fit(this);
}
SpinBtnWidgetsPage::~SpinBtnWidgetsPage()
{
}
// ----------------------------------------------------------------------------
// operations
// ----------------------------------------------------------------------------
void SpinBtnWidgetsPage::Reset()
{
m_chkVert->SetValue(TRUE);
m_chkWrap->SetValue(FALSE);
}
void SpinBtnWidgetsPage::CreateSpin()
{
int flags = 0;
bool isVert = m_chkVert->GetValue();
if ( isVert )
flags |= wxSP_VERTICAL;
else
flags |= wxSP_HORIZONTAL;
if ( m_chkWrap->GetValue() )
flags |= wxSP_WRAP;
int val = m_min;
if ( m_spinbtn )
{
int valOld = m_spinbtn->GetValue();
if ( !IsValidValue(valOld) )
{
val = valOld;
}
m_sizerSpin->Remove(m_spinbtn);
m_sizerSpin->Remove(m_spinctrl);
// there are 3 spacers left
m_sizerSpin->Remove((int)0);
m_sizerSpin->Remove((int)0);
m_sizerSpin->Remove((int)0);
delete m_spinbtn;
delete m_spinctrl;
}
m_spinbtn = new wxSpinButton(this, SpinBtnPage_SpinBtn,
wxDefaultPosition, wxDefaultSize,
flags);
m_spinbtn->SetValue(val);
m_spinbtn->SetRange(m_min, m_max);
m_spinctrl = new wxSpinCtrl(this, SpinBtnPage_SpinCtrl,
wxString::Format(_T("%d"), val),
wxDefaultPosition, wxDefaultSize,
flags,
m_min, m_max, val);
m_sizerSpin->Add(0, 0, 1);
m_sizerSpin->Add(m_spinbtn, 0, wxALIGN_CENTRE | wxALL, 5);
m_sizerSpin->Add(0, 0, 1);
m_sizerSpin->Add(m_spinctrl, 0, wxALIGN_CENTRE | wxALL, 5);
m_sizerSpin->Add(0, 0, 1);
m_sizerSpin->Layout();
}
// ----------------------------------------------------------------------------
// event handlers
// ----------------------------------------------------------------------------
void SpinBtnWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
{
Reset();
CreateSpin();
}
void SpinBtnWidgetsPage::OnButtonSetMinAndMax(wxCommandEvent& WXUNUSED(event))
{
long minNew,
maxNew = 0; // init to suppress compiler warning
if ( !m_textMin->GetValue().ToLong(&minNew) ||
!m_textMax->GetValue().ToLong(&maxNew) ||
minNew >= maxNew )
{
wxLogWarning(_T("Invalid min/max values for the spinbtn."));
return;
}
m_min = minNew;
m_max = maxNew;
m_spinbtn->SetRange(minNew, maxNew);
m_spinctrl->SetRange(minNew, maxNew);
}
void SpinBtnWidgetsPage::OnButtonSetValue(wxCommandEvent& WXUNUSED(event))
{
long val;
if ( !m_textValue->GetValue().ToLong(&val) || !IsValidValue(val) )
{
wxLogWarning(_T("Invalid spinbtn value."));
return;
}
m_spinbtn->SetValue(val);
m_spinctrl->SetValue(val);
}
void SpinBtnWidgetsPage::OnUpdateUIValueButton(wxUpdateUIEvent& event)
{
long val;
event.Enable( m_textValue->GetValue().ToLong(&val) && IsValidValue(val) );
}
void SpinBtnWidgetsPage::OnUpdateUIMinMaxButton(wxUpdateUIEvent& event)
{
long mn, mx;
event.Enable( m_textMin->GetValue().ToLong(&mn) &&
m_textMax->GetValue().ToLong(&mx) &&
mn < mx);
}
void SpinBtnWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
{
event.Enable( !m_chkVert->GetValue() || m_chkWrap->GetValue() );
}
void SpinBtnWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& event)
{
CreateSpin();
}
void SpinBtnWidgetsPage::OnUpdateUICurValueText(wxUpdateUIEvent& event)
{
event.SetText( wxString::Format(_T("%d"), m_spinbtn->GetValue()));
}
void SpinBtnWidgetsPage::OnSpinBtn(wxCommandEvent& event)
{
int value = event.GetInt();
wxASSERT_MSG( value == m_spinbtn->GetValue(),
_T("spinbtn value should be the same") );
wxLogMessage(_T("Spin button value changed, now %d"), value);
}
void SpinBtnWidgetsPage::OnSpinBtnUp(wxCommandEvent& event)
{
wxLogMessage(_T("Spin button value incremented, will be %d (was %d)"),
event.GetInt(), m_spinbtn->GetValue());
}
void SpinBtnWidgetsPage::OnSpinBtnDown(wxCommandEvent& event)
{
wxLogMessage(_T("Spin button value decremented, will be %d (was %d)"),
event.GetInt(), m_spinbtn->GetValue());
}
void SpinBtnWidgetsPage::OnSpinCtrl(wxCommandEvent& event)
{
int value = event.GetInt();
wxASSERT_MSG( value == m_spinctrl->GetValue(),
_T("spinctrl value should be the same") );
wxLogMessage(_T("Spin control value changed, now %d"), value);
}

369
samples/widgets/static.cpp Normal file
View File

@@ -0,0 +1,369 @@
/////////////////////////////////////////////////////////////////////////////
// Program: wxWindows Widgets Sample
// Name: static.cpp
// Purpose: Part of the widgets sample showing various static controls
// Author: Vadim Zeitlin
// Created: 11.04.01
// Id: $Id$
// Copyright: (c) 2001 Vadim Zeitlin
// License: wxWindows license
/////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
// for compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
// for all others, include the necessary headers
#ifndef WX_PRECOMP
#include "wx/log.h"
#include "wx/button.h"
#include "wx/checkbox.h"
#include "wx/radiobox.h"
#include "wx/statbox.h"
#include "wx/stattext.h"
#include "wx/textctrl.h"
#endif
#include "wx/sizer.h"
#include "wx/statline.h"
#include "widgets.h"
#include "icons/statbox.xpm"
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
// control ids
enum
{
StaticPage_Reset = 100,
StaticPage_BoxText,
StaticPage_LabelText
};
// alignment radiobox values
enum
{
StaticHAlign_Left,
StaticHAlign_Centre,
StaticHAlign_Right,
StaticHAlign_Max
};
enum
{
StaticVAlign_Top,
StaticVAlign_Centre,
StaticVAlign_Bottom,
StaticVAlign_Max
};
// ----------------------------------------------------------------------------
// StaticWidgetsPage
// ----------------------------------------------------------------------------
class StaticWidgetsPage : public WidgetsPage
{
public:
StaticWidgetsPage(wxNotebook *notebook, wxImageList *imaglist);
virtual ~StaticWidgetsPage();
protected:
// event handlers
void OnCheckOrRadioBox(wxCommandEvent& event);
void OnButtonReset(wxCommandEvent& event);
void OnButtonBoxText(wxCommandEvent& event);
void OnButtonLabelText(wxCommandEvent& event);
// reset all parameters
void Reset();
// (re)create all controls
void CreateStatic();
// the controls
// ------------
// the check/radio boxes for styles
wxCheckBox *m_chkVert,
*m_chkAutoResize;
wxRadioBox *m_radioHAlign,
*m_radioVAlign;
// the controls and the sizer containing them
wxStaticBoxSizer *m_sizerStatBox;
wxStaticText *m_statText;
wxStaticLine *m_statLine;
wxSizer *m_sizerStatic;
// the text entries for command parameters
wxTextCtrl *m_textBox,
*m_textLabel;
private:
DECLARE_EVENT_TABLE();
DECLARE_WIDGETS_PAGE(StaticWidgetsPage);
};
// ----------------------------------------------------------------------------
// event tables
// ----------------------------------------------------------------------------
BEGIN_EVENT_TABLE(StaticWidgetsPage, WidgetsPage)
EVT_BUTTON(StaticPage_Reset, StaticWidgetsPage::OnButtonReset)
EVT_BUTTON(StaticPage_LabelText, StaticWidgetsPage::OnButtonLabelText)
EVT_BUTTON(StaticPage_BoxText, StaticWidgetsPage::OnButtonBoxText)
EVT_CHECKBOX(-1, StaticWidgetsPage::OnCheckOrRadioBox)
EVT_RADIOBOX(-1, StaticWidgetsPage::OnCheckOrRadioBox)
END_EVENT_TABLE()
// ============================================================================
// implementation
// ============================================================================
IMPLEMENT_WIDGETS_PAGE(StaticWidgetsPage, _T("Static"));
StaticWidgetsPage::StaticWidgetsPage(wxNotebook *notebook,
wxImageList *imaglist)
: WidgetsPage(notebook)
{
imaglist->Add(wxBitmap(statbox_xpm));
// init everything
m_chkVert =
m_chkAutoResize = (wxCheckBox *)NULL;
m_radioHAlign =
m_radioVAlign = (wxRadioBox *)NULL;
m_statLine = (wxStaticLine *)NULL;
m_statText = (wxStaticText *)NULL;
m_sizerStatBox = (wxStaticBoxSizer *)NULL;
m_sizerStatic = (wxSizer *)NULL;
wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
// left pane
wxStaticBox *box = new wxStaticBox(this, -1, _T("&Set style"));
wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
m_chkVert = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Vertical line"));
m_chkAutoResize = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Fit to text"));
sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
static const wxString halign[] =
{
_T("left"),
_T("centre"),
_T("right"),
};
static const wxString valign[] =
{
_T("top"),
_T("centre"),
_T("bottom"),
};
m_radioHAlign = new wxRadioBox(this, -1, _T("&Horz alignment"),
wxDefaultPosition, wxDefaultSize,
WXSIZEOF(halign), halign);
m_radioVAlign = new wxRadioBox(this, -1, _T("&Vert alignment"),
wxDefaultPosition, wxDefaultSize,
WXSIZEOF(valign), valign);
sizerLeft->Add(m_radioHAlign, 0, wxGROW | wxALL, 5);
sizerLeft->Add(m_radioVAlign, 0, wxGROW | wxALL, 5);
wxButton *btn = new wxButton(this, StaticPage_Reset, _T("&Reset"));
sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
// middle pane
wxStaticBox *box2 = new wxStaticBox(this, -1, _T("&Change labels"));
wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL);
wxSizer *sizerRow;
sizerRow = CreateSizerWithTextAndButton(StaticPage_BoxText,
_T("Change &box label"),
-1, &m_textBox);
sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
sizerRow = CreateSizerWithTextAndButton(StaticPage_LabelText,
_T("Change &text label"),
-1, &m_textLabel);
sizerMiddle->Add(sizerRow, 0, wxGROW | wxALL, 5);
m_textBox->SetValue(_T("This is a box"));
m_textLabel->SetValue(_T("And this is a label\ninside the box"));
// right pane
wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
sizerRight->SetMinSize(250, 0);
m_sizerStatic = sizerRight;
CreateStatic();
// 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();
SetAutoLayout(TRUE);
SetSizer(sizerTop);
sizerTop->Fit(this);
}
StaticWidgetsPage::~StaticWidgetsPage()
{
}
// ----------------------------------------------------------------------------
// operations
// ----------------------------------------------------------------------------
void StaticWidgetsPage::Reset()
{
m_chkVert->SetValue(FALSE);
m_chkAutoResize->SetValue(TRUE);
m_radioHAlign->SetSelection(StaticHAlign_Left);
m_radioVAlign->SetSelection(StaticVAlign_Top);
}
void StaticWidgetsPage::CreateStatic()
{
bool isVert = m_chkVert->GetValue();
if ( m_sizerStatBox )
{
m_sizerStatic->Remove(m_sizerStatBox);
// delete m_sizerStatBox; -- deleted by Remove()
delete m_statText;
delete m_statLine;
}
int flagsBox = 0,
flagsText = 0;
if ( !m_chkAutoResize->GetValue() )
{
flagsText |= wxST_NO_AUTORESIZE;
}
int align = 0;
switch ( m_radioHAlign->GetSelection() )
{
default:
wxFAIL_MSG(_T("unexpected radiobox selection"));
// fall through
case StaticHAlign_Left:
align |= wxALIGN_LEFT;
break;
case StaticHAlign_Centre:
align |= wxALIGN_CENTRE_HORIZONTAL;
break;
case StaticHAlign_Right:
align |= wxALIGN_RIGHT;
break;
}
switch ( m_radioVAlign->GetSelection() )
{
default:
wxFAIL_MSG(_T("unexpected radiobox selection"));
// fall through
case StaticVAlign_Top:
align |= wxALIGN_TOP;
break;
case StaticVAlign_Centre:
align |= wxALIGN_CENTRE_VERTICAL;
break;
case StaticVAlign_Bottom:
align |= wxALIGN_BOTTOM;
break;
}
flagsText |= align;
flagsBox |= align;
wxStaticBox *box = new wxStaticBox(this, -1, m_textBox->GetValue(),
wxDefaultPosition, wxDefaultSize,
flagsBox);
m_sizerStatBox = new wxStaticBoxSizer(box, isVert ? wxHORIZONTAL
: wxVERTICAL);
m_statText = new wxStaticText(this, -1, m_textLabel->GetValue(),
wxDefaultPosition, wxDefaultSize,
flagsText);
m_statLine = new wxStaticLine(this, -1,
wxDefaultPosition, wxDefaultSize,
isVert ? wxLI_VERTICAL : wxLI_HORIZONTAL);
m_sizerStatBox->Add(m_statText, 1, wxGROW | wxALL, 5);
m_sizerStatBox->Add(m_statLine, 0, wxGROW | wxALL, 5);
m_sizerStatBox->Add(0, 0, 1);
m_sizerStatic->Add(m_sizerStatBox, 1, wxGROW);
m_sizerStatic->Layout();
}
// ----------------------------------------------------------------------------
// event handlers
// ----------------------------------------------------------------------------
void StaticWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
{
Reset();
CreateStatic();
}
void StaticWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& event)
{
CreateStatic();
}
void StaticWidgetsPage::OnButtonBoxText(wxCommandEvent& event)
{
m_sizerStatBox->GetStaticBox()->SetLabel(m_textBox->GetValue());
}
void StaticWidgetsPage::OnButtonLabelText(wxCommandEvent& event)
{
m_statText->SetLabel(m_textLabel->GetValue());
}

View File

@@ -0,0 +1,658 @@
/////////////////////////////////////////////////////////////////////////////
// Program: wxWindows Widgets Sample
// Name: textctrl.cpp
// Purpose: part of the widgets sample showing wxTextCtrl
// Author: Vadim Zeitlin
// Created: 27.03.01
// Id: $Id$
// Copyright: (c) 2001 Vadim Zeitlin
// License: wxWindows license
/////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
// for compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
// for all others, include the necessary headers
#ifndef WX_PRECOMP
#include "wx/log.h"
#include "wx/timer.h"
#include "wx/button.h"
#include "wx/checkbox.h"
#include "wx/radiobox.h"
#include "wx/statbox.h"
#include "wx/stattext.h"
#include "wx/textctrl.h"
#endif
#include "wx/sizer.h"
#include "widgets.h"
#include "icons/text.xpm"
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
// control ids
enum
{
TextPage_Reset = 100,
TextPage_Set,
TextPage_Add,
TextPage_Insert,
TextPage_Clear,
TextPage_Load,
TextPage_Password,
TextPage_WrapLines,
TextPage_Textctrl
};
// textctrl line number radiobox values
enum TextLines
{
TextLines_Single,
TextLines_Multi
};
// default values for the controls
static const struct ControlValues
{
TextLines textLines;
bool password;
bool wraplines;
bool readonly;
} DEFAULTS =
{
TextLines_Multi, // multiline
FALSE, // not password
TRUE, // do wrap lines
FALSE // not readonly
};
// ----------------------------------------------------------------------------
// TextWidgetsPage
// ----------------------------------------------------------------------------
// Define a new frame type: this is going to be our main frame
class TextWidgetsPage : public WidgetsPage
{
public:
// ctor(s) and dtor
TextWidgetsPage(wxNotebook *notebook, wxImageList *imaglist);
virtual ~TextWidgetsPage();
protected:
// create an info text contorl
wxTextCtrl *CreateInfoText();
// create a horz sizer holding a static text and this text control
wxSizer *CreateTextWithLabelSizer(const wxString& label,
wxTextCtrl *text,
const wxString& label2 = wxEmptyString,
wxTextCtrl *text2 = NULL);
// event handlers
void OnButtonReset(wxCommandEvent& event);
void OnButtonClearLog(wxCommandEvent& event);
void OnButtonSet(wxCommandEvent& event);
void OnButtonAdd(wxCommandEvent& event);
void OnButtonInsert(wxCommandEvent& event);
void OnButtonClear(wxCommandEvent& event);
void OnButtonLoad(wxCommandEvent& event);
void OnButtonQuit(wxCommandEvent& event);
void OnText(wxCommandEvent& event);
void OnTextEnter(wxCommandEvent& event);
void OnCheckOrRadioBox(wxCommandEvent& event);
void OnUpdateUIClearButton(wxUpdateUIEvent& event);
void OnUpdateUIPasswordCheckbox(wxUpdateUIEvent& event);
void OnUpdateUIWrapLinesCheckbox(wxUpdateUIEvent& event);
void OnUpdateUIResetButton(wxUpdateUIEvent& event);
void OnIdle(wxIdleEvent& event);
// reset the textctrl parameters
void Reset();
// (re)create the textctrl
void CreateText();
// is the control currently single line?
bool IsSingleLine() const
{
return m_radioTextLines->GetSelection() == TextLines_Single;
}
// the controls
// ------------
// the radiobox to choose between single and multi line
wxRadioBox *m_radioTextLines;
// the checkboxes controlling text ctrl styles
wxCheckBox *m_chkPassword,
*m_chkWrapLines,
*m_chkReadonly;
// the textctrl itself and the sizer it is in
wxTextCtrl *m_text;
wxSizer *m_sizerText;
// the information text zones
wxTextCtrl *m_textPosCur,
*m_textRowCur,
*m_textColCur,
*m_textPosLast,
*m_textLineLast,
*m_textSelFrom,
*m_textSelTo;
// and the data to show in them
long m_posCur,
m_posLast,
m_selFrom,
m_selTo;
private:
// any class wishing to process wxWindows events must use this macro
DECLARE_EVENT_TABLE();
DECLARE_WIDGETS_PAGE(TextWidgetsPage);
};
// ----------------------------------------------------------------------------
// event tables
// ----------------------------------------------------------------------------
BEGIN_EVENT_TABLE(TextWidgetsPage, WidgetsPage)
EVT_IDLE(TextWidgetsPage::OnIdle)
EVT_BUTTON(TextPage_Reset, TextWidgetsPage::OnButtonReset)
EVT_BUTTON(TextPage_Clear, TextWidgetsPage::OnButtonClear)
EVT_BUTTON(TextPage_Set, TextWidgetsPage::OnButtonSet)
EVT_BUTTON(TextPage_Add, TextWidgetsPage::OnButtonAdd)
EVT_BUTTON(TextPage_Insert, TextWidgetsPage::OnButtonInsert)
EVT_BUTTON(TextPage_Load, TextWidgetsPage::OnButtonLoad)
EVT_UPDATE_UI(TextPage_Clear, TextWidgetsPage::OnUpdateUIClearButton)
EVT_UPDATE_UI(TextPage_Password, TextWidgetsPage::OnUpdateUIPasswordCheckbox)
EVT_UPDATE_UI(TextPage_WrapLines, TextWidgetsPage::OnUpdateUIWrapLinesCheckbox)
EVT_UPDATE_UI(TextPage_Reset, TextWidgetsPage::OnUpdateUIResetButton)
EVT_TEXT(TextPage_Textctrl, TextWidgetsPage::OnText)
EVT_TEXT_ENTER(TextPage_Textctrl, TextWidgetsPage::OnTextEnter)
EVT_CHECKBOX(-1, TextWidgetsPage::OnCheckOrRadioBox)
EVT_RADIOBOX(-1, TextWidgetsPage::OnCheckOrRadioBox)
END_EVENT_TABLE()
// ============================================================================
// implementation
// ============================================================================
IMPLEMENT_WIDGETS_PAGE(TextWidgetsPage, _T("Text"));
// ----------------------------------------------------------------------------
// TextWidgetsPage creation
// ----------------------------------------------------------------------------
TextWidgetsPage::TextWidgetsPage(wxNotebook *notebook, wxImageList *imaglist)
: WidgetsPage(notebook)
{
imaglist->Add(wxBitmap(text_xpm));
// init everything
m_radioTextLines = (wxRadioBox *)NULL;
m_chkPassword =
m_chkWrapLines =
m_chkReadonly = (wxCheckBox *)NULL;
m_text =
m_textPosCur =
m_textRowCur =
m_textColCur =
m_textPosLast =
m_textLineLast =
m_textSelFrom =
m_textSelTo = (wxTextCtrl *)NULL;
m_sizerText = (wxSizer *)NULL;
m_posCur =
m_posLast =
m_selFrom =
m_selTo = -2; // not -1 which means "no selection"
// left pane
static const wxString modes[] =
{
_T("single line"),
_T("multi line"),
};
wxStaticBox *box = new wxStaticBox(this, -1, _T("&Set textctrl parameters"));
m_radioTextLines = new wxRadioBox(this, -1, _T("&Number of lines:"),
wxDefaultPosition, wxDefaultSize,
WXSIZEOF(modes), modes,
1, wxRA_SPECIFY_COLS);
wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
sizerLeft->Add(m_radioTextLines, 0, wxGROW | wxALL, 5);
sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
m_chkPassword = CreateCheckBoxAndAddToSizer(
sizerLeft, _T("&Password control"), TextPage_Password
);
m_chkWrapLines = CreateCheckBoxAndAddToSizer(
sizerLeft, _T("Line &wrap"), TextPage_WrapLines
);
m_chkReadonly = CreateCheckBoxAndAddToSizer(
sizerLeft, _T("&Read-only mode")
);
wxButton *btn = new wxButton(this, TextPage_Reset, _T("&Reset"));
sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
// middle pane
wxStaticBox *box2 = new wxStaticBox(this, -1, _T("&Change contents:"));
wxSizer *sizerMiddleUp = new wxStaticBoxSizer(box2, wxVERTICAL);
btn = new wxButton(this, TextPage_Set, _T("&Set text value"));
sizerMiddleUp->Add(btn, 0, wxALL | wxGROW, 5);
btn = new wxButton(this, TextPage_Add, _T("&Append text"));
sizerMiddleUp->Add(btn, 0, wxALL | wxGROW, 5);
btn = new wxButton(this, TextPage_Insert, _T("&Insert text"));
sizerMiddleUp->Add(btn, 0, wxALL | wxGROW, 5);
btn = new wxButton(this, TextPage_Load, _T("&Load file"));
sizerMiddleUp->Add(btn, 0, wxALL | wxGROW, 5);
btn = new wxButton(this, TextPage_Clear, _T("&Clear"));
sizerMiddleUp->Add(btn, 0, wxALL | wxGROW, 5);
wxStaticBox *box4 = new wxStaticBox(this, -1, _T("&Info:"));
wxSizer *sizerMiddleDown = new wxStaticBoxSizer(box4, wxVERTICAL);
m_textPosCur = CreateInfoText();
m_textRowCur = CreateInfoText();
m_textColCur = CreateInfoText();
wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL);
sizerRow->Add(CreateTextWithLabelSizer
(
_T("Current pos:"),
m_textPosCur
),
0, wxRIGHT, 5);
sizerRow->Add(CreateTextWithLabelSizer
(
_T("Col:"),
m_textColCur
),
0, wxLEFT | wxRIGHT, 5);
sizerRow->Add(CreateTextWithLabelSizer
(
_T("Row:"),
m_textRowCur
),
0, wxLEFT, 5);
sizerMiddleDown->Add(sizerRow, 0, wxALL, 5);
m_textLineLast = CreateInfoText();
m_textPosLast = CreateInfoText();
sizerMiddleDown->Add
(
CreateTextWithLabelSizer
(
_T("Number of lines:"),
m_textLineLast,
_T("Last position:"),
m_textPosLast
),
0, wxALL, 5
);
m_textSelFrom = CreateInfoText();
m_textSelTo = CreateInfoText();
sizerMiddleDown->Add
(
CreateTextWithLabelSizer
(
_T("Selection: from"),
m_textSelFrom,
_T("to"),
m_textSelTo
),
0, wxALL, 5
);
wxSizer *sizerMiddle = new wxBoxSizer(wxVERTICAL);
sizerMiddle->Add(sizerMiddleUp, 0, wxGROW);
sizerMiddle->Add(sizerMiddleDown, 1, wxGROW | wxTOP, 5);
// right pane
wxStaticBox *box3 = new wxStaticBox(this, -1, _T("&Text:"));
m_sizerText = new wxStaticBoxSizer(box3, wxHORIZONTAL);
Reset();
CreateText();
m_sizerText->SetMinSize(250, 0);
// the 3 panes panes compose the upper part of the window
wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
sizerTop->Add(sizerMiddle, 0, wxGROW | wxALL, 10);
sizerTop->Add(m_sizerText, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
SetAutoLayout(TRUE);
SetSizer(sizerTop);
sizerTop->Fit(this);
}
TextWidgetsPage::~TextWidgetsPage()
{
}
// ----------------------------------------------------------------------------
// creation helpers
// ----------------------------------------------------------------------------
wxTextCtrl *TextWidgetsPage::CreateInfoText()
{
static int s_maxWidth = 0;
if ( !s_maxWidth )
{
// calc it once only
GetTextExtent(_T("9999999"), &s_maxWidth, NULL);
}
wxTextCtrl *text = new wxTextCtrl(this, -1, _T(""),
wxDefaultPosition,
wxSize(s_maxWidth, -1),
wxTE_READONLY);
return text;
}
wxSizer *TextWidgetsPage::CreateTextWithLabelSizer(const wxString& label,
wxTextCtrl *text,
const wxString& label2,
wxTextCtrl *text2)
{
wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL);
sizerRow->Add(new wxStaticText(this, -1, label), 0,
wxALIGN_CENTRE_VERTICAL | wxRIGHT, 5);
sizerRow->Add(text, 0, wxALIGN_CENTRE_VERTICAL);
if ( text2 )
{
sizerRow->Add(new wxStaticText(this, -1, label2), 0,
wxALIGN_CENTRE_VERTICAL | wxLEFT | wxRIGHT, 5);
sizerRow->Add(text2, 0, wxALIGN_CENTRE_VERTICAL);
}
return sizerRow;
}
// ----------------------------------------------------------------------------
// operations
// ----------------------------------------------------------------------------
void TextWidgetsPage::Reset()
{
m_radioTextLines->SetSelection(DEFAULTS.textLines);
m_chkPassword->SetValue(DEFAULTS.password);
m_chkWrapLines->SetValue(DEFAULTS.wraplines);
m_chkReadonly->SetValue(DEFAULTS.readonly);
}
void TextWidgetsPage::CreateText()
{
int flags = 0;
switch ( m_radioTextLines->GetSelection() )
{
default:
wxFAIL_MSG( _T("unexpected radio box selection") );
case TextLines_Single:
break;
case TextLines_Multi:
flags |= wxTE_MULTILINE;
m_chkPassword->SetValue(FALSE);
break;
}
if ( m_chkPassword->GetValue() )
flags |= wxTE_PASSWORD;
if ( m_chkReadonly->GetValue() )
flags |= wxTE_READONLY;
if ( !m_chkWrapLines->GetValue() )
flags |= wxHSCROLL;
wxString valueOld;
if ( m_text )
{
valueOld = m_text->GetValue();
m_sizerText->Remove(m_text);
delete m_text;
}
else
{
valueOld = _T("Hello, Universe!");
}
m_text = new wxTextCtrl(this, TextPage_Textctrl,
valueOld,
wxDefaultPosition, wxDefaultSize,
flags);
m_sizerText->Add(m_text, 1, wxALL |
(flags & wxTE_MULTILINE ? wxGROW
: wxALIGN_TOP), 5);
m_sizerText->Layout();
}
// ----------------------------------------------------------------------------
// event handlers
// ----------------------------------------------------------------------------
void TextWidgetsPage::OnIdle(wxIdleEvent& WXUNUSED(event))
{
// update all info texts
if ( m_textPosCur )
{
long posCur = m_text->GetInsertionPoint();
if ( posCur != m_posCur )
{
m_textPosCur->Clear();
m_textRowCur->Clear();
m_textColCur->Clear();
long col, row;
m_text->PositionToXY(posCur, &col, &row);
*m_textPosCur << posCur;
*m_textRowCur << row;
*m_textColCur << col;
m_posCur = posCur;
}
}
if ( m_textPosLast )
{
long posLast = m_text->GetLastPosition();
if ( posLast != m_posLast )
{
m_textPosLast->Clear();
*m_textPosLast << posLast;
m_posLast = posLast;
}
}
if ( m_textLineLast )
{
m_textLineLast->SetValue(
wxString::Format(_T("%ld"), m_text->GetNumberOfLines()));
}
if ( m_textSelFrom && m_textSelTo )
{
long selFrom, selTo;
m_text->GetSelection(&selFrom, &selTo);
if ( selFrom != m_selFrom )
{
m_textSelFrom->Clear();
*m_textSelFrom << selFrom;
m_selFrom = selFrom;
}
if ( selTo != m_selTo )
{
m_textSelTo->Clear();
*m_textSelTo << selTo;
m_selTo = selTo;
}
}
}
void TextWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
{
Reset();
CreateText();
}
void TextWidgetsPage::OnButtonSet(wxCommandEvent& WXUNUSED(event))
{
m_text->SetValue(_T("Yellow submarine"));
m_text->SetFocus();
}
void TextWidgetsPage::OnButtonAdd(wxCommandEvent& WXUNUSED(event))
{
m_text->AppendText(_T("here, there and everywhere"));
m_text->SetFocus();
}
void TextWidgetsPage::OnButtonInsert(wxCommandEvent& WXUNUSED(event))
{
m_text->WriteText(_T("is there anybody going to listen to my story"));
m_text->SetFocus();
}
void TextWidgetsPage::OnButtonClear(wxCommandEvent& WXUNUSED(event))
{
m_text->Clear();
m_text->SetFocus();
}
void TextWidgetsPage::OnButtonLoad(wxCommandEvent& WXUNUSED(event))
{
// search for the file in several dirs where it's likely to be
wxPathList pathlist;
pathlist.Add(_T("."));
pathlist.Add(_T(".."));
pathlist.Add(_T("../../../samples/widgets"));
wxString filename = pathlist.FindValidPath(_T("textctrl.cpp"));
if ( !filename )
{
wxLogError(_T("File textctrl.cpp not found."));
}
else // load it
{
wxStopWatch sw;
if ( !m_text->LoadFile(filename) )
{
// this is not supposed to happen ...
wxLogError(_T("Error loading file."));
}
else
{
long elapsed = sw.Time();
wxLogMessage(_T("Loaded file '%s' in %u.%us"),
filename.c_str(), elapsed / 1000, elapsed % 1000);
}
}
}
void TextWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent& event)
{
event.Enable(!m_text->GetValue().empty());
}
void TextWidgetsPage::OnUpdateUIWrapLinesCheckbox(wxUpdateUIEvent& event)
{
event.Enable( !IsSingleLine() );
}
void TextWidgetsPage::OnUpdateUIPasswordCheckbox(wxUpdateUIEvent& event)
{
// can't put multiline control in password mode
event.Enable( IsSingleLine() );
}
void TextWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
{
event.Enable( (m_radioTextLines->GetSelection() != DEFAULTS.textLines) ||
(m_chkReadonly->GetValue() != DEFAULTS.readonly) ||
(m_chkPassword->GetValue() != DEFAULTS.password) ||
(m_chkWrapLines->GetValue() != DEFAULTS.wraplines) );
}
void TextWidgetsPage::OnText(wxCommandEvent& event)
{
// small hack to suppress the very first message: by then the logging is
// not yet redirected and so initial setting of the text value results in
// an annoying message box
static bool s_firstTime = TRUE;
if ( s_firstTime )
{
s_firstTime = FALSE;
return;
}
wxLogMessage(_T("Text ctrl value changed"));
}
void TextWidgetsPage::OnTextEnter(wxCommandEvent& event)
{
wxLogMessage(_T("Text entered: '%s'"), event.GetString().c_str());
}
void TextWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& event)
{
CreateText();
}

404
samples/widgets/widgets.cpp Normal file
View File

@@ -0,0 +1,404 @@
/////////////////////////////////////////////////////////////////////////////
// Program: wxWindows Widgets Sample
// Name: widgets.cpp
// Purpose: Sample showing most of the simple wxWindows widgets
// Author: Vadim Zeitlin
// Created: 27.03.01
// Id: $Id$
// Copyright: (c) 2001 Vadim Zeitlin
// License: wxWindows license
/////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
// for compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
// for all others, include the necessary headers
#ifndef WX_PRECOMP
#include "wx/app.h"
#include "wx/log.h"
#include "wx/panel.h"
#include "wx/frame.h"
#include "wx/button.h"
#include "wx/checkbox.h"
#include "wx/listbox.h"
#include "wx/statbox.h"
#include "wx/stattext.h"
#include "wx/textctrl.h"
#endif
#include "wx/notebook.h"
#include "wx/sizer.h"
#include "widgets.h"
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
// control ids
enum
{
Widgets_ClearLog = 100,
Widgets_Quit
};
// ----------------------------------------------------------------------------
// our classes
// ----------------------------------------------------------------------------
// Define a new application type, each program should derive a class from wxApp
class WidgetsApp : public wxApp
{
public:
// override base class virtuals
// ----------------------------
// this one is called on application startup and is a good place for the app
// initialization (doing it here and not in the ctor allows to have an error
// return: if OnInit() returns false, the application terminates)
virtual bool OnInit();
};
// Define a new frame type: this is going to be our main frame
class WidgetsFrame : public wxFrame
{
public:
// ctor(s) and dtor
WidgetsFrame(const wxString& title);
virtual ~WidgetsFrame();
protected:
// event handlers
void OnButtonClearLog(wxCommandEvent& event);
void OnButtonQuit(wxCommandEvent& event);
// initialize the notebook: add all pages to it
void InitNotebook();
private:
// the panel containing everything
wxPanel *m_panel;
// the listbox for logging messages
wxListBox *m_lboxLog;
// the log target we use to redirect messages to the listbox
wxLog *m_logTarget;
// the notebook containing the test pages
wxNotebook *m_notebook;
// and the image list for it
wxImageList *m_imaglist;
// any class wishing to process wxWindows events must use this macro
DECLARE_EVENT_TABLE()
};
// A log target which just redirects the messages to a listbox
class LboxLogger : public wxLog
{
public:
LboxLogger(wxListBox *lbox, wxLog *logOld)
{
m_lbox = lbox;
//m_lbox->Disable(); -- looks ugly under MSW
m_logOld = logOld;
}
virtual ~LboxLogger()
{
wxLog::SetActiveTarget(m_logOld);
}
private:
// implement sink functions
virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t)
{
// don't put trace messages into listbox or we can get into infinite
// recursion
if ( level == wxLOG_Trace )
{
if ( m_logOld )
{
// cast is needed to call protected method
((LboxLogger *)m_logOld)->DoLog(level, szString, t);
}
}
else
{
wxLog::DoLog(level, szString, t);
}
}
virtual void DoLogString(const wxChar *szString, time_t t)
{
wxString msg;
TimeStamp(&msg);
msg += szString;
#ifdef __WXUNIVERSAL__
m_lbox->AppendAndEnsureVisible(msg);
#else // other ports don't have this method yet
m_lbox->Append(msg);
m_lbox->SetFirstItem(m_lbox->GetCount() - 1);
#endif
}
// the control we use
wxListBox *m_lbox;
// the old log target
wxLog *m_logOld;
};
// array of pages
WX_DEFINE_ARRAY(WidgetsPage *, ArrayWidgetsPage);
// ----------------------------------------------------------------------------
// misc macros
// ----------------------------------------------------------------------------
IMPLEMENT_APP(WidgetsApp)
#ifdef __WXUNIVERSAL__
#include "wx/univ/theme.h"
WX_USE_THEME(win32);
WX_USE_THEME(gtk);
#endif // __WXUNIVERSAL__
// ----------------------------------------------------------------------------
// event tables
// ----------------------------------------------------------------------------
BEGIN_EVENT_TABLE(WidgetsFrame, wxFrame)
EVT_BUTTON(Widgets_ClearLog, WidgetsFrame::OnButtonClearLog)
EVT_BUTTON(Widgets_Quit, WidgetsFrame::OnButtonQuit)
END_EVENT_TABLE()
// ============================================================================
// implementation
// ============================================================================
// ----------------------------------------------------------------------------
// app class
// ----------------------------------------------------------------------------
bool WidgetsApp::OnInit()
{
// the reason for having these ifdef's is that I often run two copies of
// this sample side by side and it is useful to see which one is which
wxString title =
#if defined(__WXUNIVERSAL__)
_T("wxUniv")
#elif defined(__WXMSW__)
_T("wxMSW")
#elif defined(__WXGTK__)
_T("wxGTK")
#else
_T("wxWindows")
#endif
;
wxFrame *frame = new WidgetsFrame(title + _T(" widgets demo"));
frame->Show();
//wxLog::AddTraceMask(_T("listbox"));
//wxLog::AddTraceMask(_T("scrollbar"));
return TRUE;
}
// ----------------------------------------------------------------------------
// WidgetsFrame construction
// ----------------------------------------------------------------------------
WidgetsFrame::WidgetsFrame(const wxString& title)
: wxFrame(NULL, -1, title, wxPoint(0, 50))
{
// init everything
m_lboxLog = (wxListBox *)NULL;
m_logTarget = (wxLog *)NULL;
m_notebook = (wxNotebook *)NULL;
m_imaglist = (wxImageList *)NULL;
// create controls
m_panel = new wxPanel(this, -1);
wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
// we have 2 panes: notebook which pages demonstrating the controls in the
// upper one and the log window with some buttons in the lower
m_notebook = new wxNotebook(m_panel, -1);
InitNotebook();
wxSizer *sizerUp = new wxNotebookSizer(m_notebook);
// the lower one only has the log listbox and a button to clear it
wxSizer *sizerDown = new wxStaticBoxSizer
(
new wxStaticBox(m_panel, -1, _T("&Log window")),
wxVERTICAL
);
m_lboxLog = new wxListBox(m_panel, -1);
sizerDown->Add(m_lboxLog, 1, wxGROW | wxALL, 5);
wxBoxSizer *sizerBtns = new wxBoxSizer(wxHORIZONTAL);
wxButton *btn = new wxButton(m_panel, Widgets_ClearLog, _T("Clear &log"));
sizerBtns->Add(btn);
sizerBtns->Add(10, 0); // spacer
btn = new wxButton(m_panel, Widgets_Quit, _T("E&xit"));
sizerBtns->Add(btn);
sizerDown->Add(sizerBtns, 0, wxALL | wxALIGN_RIGHT, 5);
// put everything together
sizerTop->Add(sizerUp, 1, wxGROW | (wxALL & ~(wxTOP | wxBOTTOM)), 10);
sizerTop->Add(0, 5, 0, wxGROW); // spacer in between
sizerTop->Add(sizerDown, 0, wxGROW | (wxALL & ~wxTOP), 10);
m_panel->SetAutoLayout(TRUE);
m_panel->SetSizer(sizerTop);
sizerTop->Fit(this);
sizerTop->SetSizeHints(this);
// now that everything is created we can redirect the log messages to the
// listbox
m_logTarget = new LboxLogger(m_lboxLog, wxLog::GetActiveTarget());
wxLog::SetActiveTarget(m_logTarget);
}
void WidgetsFrame::InitNotebook()
{
m_imaglist = new wxImageList(32, 32);
ArrayWidgetsPage pages;
wxArrayString labels;
// we need to first create all pages and only then add them to the notebook
// as we need the image list first
WidgetsPageInfo *info = WidgetsPage::ms_widgetPages;
while ( info )
{
WidgetsPage *page = (*info->GetCtor())(m_notebook, m_imaglist);
pages.Add(page);
labels.Add(info->GetLabel());
info = info->GetNext();
}
m_notebook->SetImageList(m_imaglist);
// now do add them
size_t count = pages.GetCount();
for ( size_t n = 0; n < count; n++ )
{
m_notebook->AddPage(
pages[n],
labels[n],
FALSE, // don't select
n // image id
);
}
}
WidgetsFrame::~WidgetsFrame()
{
delete m_logTarget;
delete m_imaglist;
}
// ----------------------------------------------------------------------------
// WidgetsFrame event handlers
// ----------------------------------------------------------------------------
void WidgetsFrame::OnButtonQuit(wxCommandEvent& WXUNUSED(event))
{
Close();
}
void WidgetsFrame::OnButtonClearLog(wxCommandEvent& event)
{
m_lboxLog->Clear();
}
// ----------------------------------------------------------------------------
// WidgetsPageInfo
// ----------------------------------------------------------------------------
WidgetsPageInfo *WidgetsPage::ms_widgetPages = NULL;
WidgetsPageInfo::WidgetsPageInfo(Constructor ctor, const wxChar *label)
: m_label(label)
{
m_ctor = ctor;
m_next = WidgetsPage::ms_widgetPages;
WidgetsPage::ms_widgetPages = this;
}
// ----------------------------------------------------------------------------
// WidgetsPage
// ----------------------------------------------------------------------------
WidgetsPage::WidgetsPage(wxNotebook *notebook)
: wxPanel(notebook, -1)
{
}
wxSizer *WidgetsPage::CreateSizerWithText(wxControl *control,
wxWindowID id,
wxTextCtrl **ppText)
{
wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL);
wxTextCtrl *text = new wxTextCtrl(this, id, _T(""));
sizerRow->Add(control, 0, wxRIGHT | wxALIGN_CENTRE_VERTICAL, 5);
sizerRow->Add(text, 1, wxLEFT | wxALIGN_CENTRE_VERTICAL, 5);
if ( ppText )
*ppText = text;
return sizerRow;
}
// create a sizer containing a label and a text ctrl
wxSizer *WidgetsPage::CreateSizerWithTextAndLabel(const wxString& label,
wxWindowID id,
wxTextCtrl **ppText)
{
return CreateSizerWithText(new wxStaticText(this, -1, label), id, ppText);
}
// create a sizer containing a button and a text ctrl
wxSizer *WidgetsPage::CreateSizerWithTextAndButton(wxWindowID idBtn,
const wxString& label,
wxWindowID id,
wxTextCtrl **ppText)
{
return CreateSizerWithText(new wxButton(this, idBtn, label), id, ppText);
}
wxCheckBox *WidgetsPage::CreateCheckBoxAndAddToSizer(wxSizer *sizer,
const wxString& label,
wxWindowID id)
{
wxCheckBox *checkbox = new wxCheckBox(this, id, label);
sizer->Add(checkbox, 0, wxLEFT | wxRIGHT, 5);
sizer->Add(0, 2, 0, wxGROW); // spacer
return checkbox;
}

110
samples/widgets/widgets.h Normal file
View File

@@ -0,0 +1,110 @@
/////////////////////////////////////////////////////////////////////////////
// Program: wxWindows Widgets Sample
// Name: widgets.h
// Purpose: Common stuff for all widgets project files
// Author: Vadim Zeitlin
// Created: 27.03.01
// Id: $Id$
// Copyright: (c) 2001 Vadim Zeitlin
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#ifndef _WX_SAMPLE_WIDGETS_H_
#define _WX_SAMPLE_WIDGETS_H_
class WXDLLEXPORT wxCheckBox;
class WXDLLEXPORT wxNotebook;
class WXDLLEXPORT wxSizer;
class WXDLLEXPORT wxTextCtrl;
class WXDLLEXPORT WidgetsPageInfo;
// all source files use wxImageList
#include "wx/imaglist.h"
// ----------------------------------------------------------------------------
// WidgetsPage: a notebook page demonstrating some widget
// ----------------------------------------------------------------------------
class WidgetsPage : public wxPanel
{
public:
WidgetsPage(wxNotebook *notebook);
protected:
// several helper functions for page creation
// create a horz sizer containing the given control and the text ctrl
// (pointer to which will be saved in the provided variable if not NULL)
// with the specified id
wxSizer *CreateSizerWithText(wxControl *control,
wxWindowID id = -1,
wxTextCtrl **ppText = NULL);
// create a sizer containing a label and a text ctrl
wxSizer *CreateSizerWithTextAndLabel(const wxString& label,
wxWindowID id = -1,
wxTextCtrl **ppText = NULL);
// create a sizer containing a button and a text ctrl
wxSizer *CreateSizerWithTextAndButton(wxWindowID idBtn,
const wxString& labelBtn,
wxWindowID id = -1,
wxTextCtrl **ppText = NULL);
// create a checkbox and add it to the sizer
wxCheckBox *CreateCheckBoxAndAddToSizer(wxSizer *sizer,
const wxString& label,
wxWindowID id = -1);
public:
// the head of the linked list containinginfo about all pages
static WidgetsPageInfo *ms_widgetPages;
};
// ----------------------------------------------------------------------------
// dynamic WidgetsPage creation helpers
// ----------------------------------------------------------------------------
class WXDLLEXPORT WidgetsPageInfo
{
public:
typedef WidgetsPage *(*Constructor)(wxNotebook *notebook,
wxImageList *imaglist);
// our ctor
WidgetsPageInfo(Constructor ctor, const wxChar *label);
// accessors
const wxString& GetLabel() const { return m_label; }
Constructor GetCtor() const { return m_ctor; }
WidgetsPageInfo *GetNext() const { return m_next; }
private:
// the label of the page
wxString m_label;
// the function to create this page
Constructor m_ctor;
// next node in the linked list or NULL
WidgetsPageInfo *m_next;
};
// to declare a page, this macro must be used in the class declaration
#define DECLARE_WIDGETS_PAGE(classname) \
private: \
static WidgetsPageInfo ms_info##classname; \
public: \
const WidgetsPageInfo *GetPageInfo() const \
{ return &ms_info##classname; }
// and this one must be inserted somewhere in the source file
#define IMPLEMENT_WIDGETS_PAGE(classname, label) \
WidgetsPage *wxCtorFor##classname(wxNotebook *notebook, \
wxImageList *imaglist) \
{ return new classname(notebook, imaglist); } \
WidgetsPageInfo classname:: \
ms_info##classname(wxCtorFor##classname, label)
#endif // _WX_SAMPLE_WIDGETS_H_

View File

@@ -0,0 +1 @@
#include "wx/msw/wx.rc"