1998-05-22 15:57:05 -04:00
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Name: checklst.cpp
|
|
|
|
|
// Purpose: wxCheckListBox sample
|
|
|
|
|
// Author: Vadim Zeitlin
|
1998-12-15 17:59:11 -05:00
|
|
|
|
// Modified by:
|
1998-05-22 15:57:05 -04:00
|
|
|
|
// Created: 13.11.97
|
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
|
// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
|
|
|
|
|
// Licence: wxWindows license
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
1998-10-21 04:34:32 -04:00
|
|
|
|
#ifdef __GNUG__
|
1999-02-25 08:50:16 -05:00
|
|
|
|
//#pragma implementation
|
1998-10-21 04:34:32 -04:00
|
|
|
|
#endif
|
1998-05-22 15:57:05 -04:00
|
|
|
|
|
|
|
|
|
// For compilers that support precompilation, includes "wx.h".
|
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
1999-02-25 08:50:16 -05:00
|
|
|
|
#pragma hdrstop
|
1998-05-22 15:57:05 -04:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
1999-02-25 08:50:16 -05:00
|
|
|
|
#include "wx/wx.h"
|
1998-05-22 15:57:05 -04:00
|
|
|
|
#endif
|
|
|
|
|
|
1998-12-17 03:40:34 -05:00
|
|
|
|
#ifdef __WXMSW__
|
1999-02-25 08:50:16 -05:00
|
|
|
|
#include "wx/ownerdrw.h"
|
1998-12-17 03:40:34 -05:00
|
|
|
|
#endif
|
1999-02-25 08:50:16 -05:00
|
|
|
|
|
|
|
|
|
#include "wx/log.h"
|
|
|
|
|
|
1999-10-17 10:55:39 -04:00
|
|
|
|
#include "wx/sizer.h"
|
1998-05-22 15:57:05 -04:00
|
|
|
|
#include "wx/menuitem.h"
|
1998-12-17 03:40:34 -05:00
|
|
|
|
#include "wx/checklst.h"
|
|
|
|
|
|
1998-05-22 15:57:05 -04:00
|
|
|
|
// Define a new application type
|
|
|
|
|
class CheckListBoxApp: public wxApp
|
|
|
|
|
{
|
|
|
|
|
public:
|
1999-02-25 08:50:16 -05:00
|
|
|
|
bool OnInit();
|
1998-05-22 15:57:05 -04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Define a new frame type
|
|
|
|
|
class CheckListBoxFrame : public wxFrame
|
|
|
|
|
{
|
|
|
|
|
public:
|
1999-02-25 08:50:16 -05:00
|
|
|
|
// ctor & dtor
|
|
|
|
|
CheckListBoxFrame(wxFrame *frame, const char *title,
|
|
|
|
|
int x, int y, int w, int h);
|
|
|
|
|
~CheckListBoxFrame();
|
|
|
|
|
|
|
|
|
|
// notifications
|
|
|
|
|
void OnQuit (wxCommandEvent& event);
|
|
|
|
|
void OnAbout (wxCommandEvent& event);
|
|
|
|
|
void OnListboxSelect (wxCommandEvent& event);
|
|
|
|
|
void OnCheckboxToggle (wxCommandEvent& event);
|
|
|
|
|
void OnListboxDblClick(wxCommandEvent& event);
|
|
|
|
|
void OnButtonUp (wxCommandEvent& event);
|
|
|
|
|
void OnButtonDown (wxCommandEvent& event);
|
1998-05-22 15:57:05 -04:00
|
|
|
|
|
1999-02-25 08:50:16 -05:00
|
|
|
|
private:
|
|
|
|
|
void OnButtonMove(bool up);
|
1998-05-22 15:57:05 -04:00
|
|
|
|
|
1999-11-25 18:28:41 -05:00
|
|
|
|
void AdjustColour(size_t index);
|
|
|
|
|
|
1999-02-25 08:50:16 -05:00
|
|
|
|
wxCheckListBox *m_pListBox;
|
1998-05-22 15:57:05 -04:00
|
|
|
|
|
1999-02-25 08:50:16 -05:00
|
|
|
|
DECLARE_EVENT_TABLE()
|
1998-05-22 15:57:05 -04:00
|
|
|
|
};
|
|
|
|
|
|
1998-12-15 17:59:11 -05:00
|
|
|
|
enum
|
|
|
|
|
{
|
1999-02-25 08:50:16 -05:00
|
|
|
|
Menu_Quit = 1,
|
|
|
|
|
Control_First = 1000,
|
|
|
|
|
Control_Listbox,
|
|
|
|
|
Btn_Up,
|
|
|
|
|
Btn_Down
|
1998-05-22 15:57:05 -04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
BEGIN_EVENT_TABLE(CheckListBoxFrame, wxFrame)
|
1999-02-25 08:50:16 -05:00
|
|
|
|
EVT_MENU(Menu_Quit, CheckListBoxFrame::OnQuit)
|
|
|
|
|
|
|
|
|
|
EVT_LISTBOX(Control_Listbox, CheckListBoxFrame::OnListboxSelect)
|
|
|
|
|
EVT_CHECKLISTBOX(Control_Listbox, CheckListBoxFrame::OnCheckboxToggle)
|
|
|
|
|
EVT_LISTBOX_DCLICK(Control_Listbox, CheckListBoxFrame::OnListboxDblClick)
|
|
|
|
|
|
|
|
|
|
EVT_BUTTON(Btn_Up, CheckListBoxFrame::OnButtonUp)
|
|
|
|
|
EVT_BUTTON(Btn_Down, CheckListBoxFrame::OnButtonDown)
|
1998-05-22 15:57:05 -04:00
|
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
|
|
IMPLEMENT_APP(CheckListBoxApp);
|
|
|
|
|
|
|
|
|
|
// init our app: create windows
|
|
|
|
|
bool CheckListBoxApp::OnInit(void)
|
|
|
|
|
{
|
1999-02-25 08:50:16 -05:00
|
|
|
|
CheckListBoxFrame *pFrame = new CheckListBoxFrame
|
|
|
|
|
(
|
|
|
|
|
NULL,
|
|
|
|
|
"wxWindows Checklistbox Sample",
|
|
|
|
|
50, 50, 480, 320
|
|
|
|
|
);
|
|
|
|
|
SetTopWindow(pFrame);
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
1998-05-22 15:57:05 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// main frame constructor
|
1999-02-25 08:50:16 -05:00
|
|
|
|
CheckListBoxFrame::CheckListBoxFrame(wxFrame *frame,
|
|
|
|
|
const char *title,
|
|
|
|
|
int x, int y, int w, int h)
|
|
|
|
|
: wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
|
1998-05-22 15:57:05 -04:00
|
|
|
|
{
|
1999-02-25 08:50:16 -05:00
|
|
|
|
// create the status line
|
|
|
|
|
const int widths[] = { -1, 60 };
|
|
|
|
|
CreateStatusBar(2);
|
|
|
|
|
SetStatusWidths(2, widths);
|
1999-07-25 02:04:20 -04:00
|
|
|
|
wxLogStatus(this, _T("no selection"));
|
1999-02-25 08:50:16 -05:00
|
|
|
|
|
|
|
|
|
// Make a menubar
|
|
|
|
|
wxMenu *file_menu = new wxMenu;
|
|
|
|
|
|
|
|
|
|
// construct submenu
|
|
|
|
|
file_menu->Append(Menu_Quit, "E&xit");
|
|
|
|
|
|
|
|
|
|
wxMenuBar *menu_bar = new wxMenuBar;
|
|
|
|
|
menu_bar->Append(file_menu, "&File");
|
|
|
|
|
SetMenuBar(menu_bar);
|
|
|
|
|
|
|
|
|
|
// make a panel with some controls
|
1999-10-20 16:38:47 -04:00
|
|
|
|
wxPanel *panel = new wxPanel(this, -1, wxPoint(0, 0),
|
1999-02-25 08:50:16 -05:00
|
|
|
|
wxSize(400, 200), wxTAB_TRAVERSAL);
|
|
|
|
|
|
|
|
|
|
// check list box
|
|
|
|
|
static const char* aszChoices[] =
|
|
|
|
|
{
|
|
|
|
|
"Zeroth",
|
|
|
|
|
"First", "Second", "Third",
|
|
|
|
|
"Fourth", "Fifth", "Sixth",
|
|
|
|
|
"Seventh", "Eighth", "Nineth"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
wxString *astrChoices = new wxString[WXSIZEOF(aszChoices)];
|
|
|
|
|
unsigned int ui;
|
|
|
|
|
for ( ui = 0; ui < WXSIZEOF(aszChoices); ui++ )
|
|
|
|
|
astrChoices[ui] = aszChoices[ui];
|
|
|
|
|
|
|
|
|
|
m_pListBox = new wxCheckListBox
|
|
|
|
|
(
|
|
|
|
|
panel, // parent
|
|
|
|
|
Control_Listbox, // control id
|
|
|
|
|
wxPoint(10, 10), // listbox poistion
|
1999-05-14 10:44:03 -04:00
|
|
|
|
wxSize(400, 100), // listbox size
|
1999-02-25 08:50:16 -05:00
|
|
|
|
WXSIZEOF(aszChoices), // number of strings
|
|
|
|
|
astrChoices // array of strings
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
//m_pListBox->SetBackgroundColour(*wxGREEN);
|
|
|
|
|
|
|
|
|
|
delete [] astrChoices;
|
|
|
|
|
|
|
|
|
|
// set grey background for every second entry
|
|
|
|
|
for ( ui = 0; ui < WXSIZEOF(aszChoices); ui += 2 ) {
|
1999-11-25 18:28:41 -05:00
|
|
|
|
AdjustColour(ui);
|
1999-02-25 08:50:16 -05:00
|
|
|
|
}
|
1998-05-22 15:57:05 -04:00
|
|
|
|
|
1999-02-25 08:50:16 -05:00
|
|
|
|
m_pListBox->Check(2);
|
1998-05-22 15:57:05 -04:00
|
|
|
|
|
1999-02-25 08:50:16 -05:00
|
|
|
|
// create buttons for moving the items around
|
1999-10-17 10:55:39 -04:00
|
|
|
|
wxButton *button1 = new wxButton(panel, Btn_Up, " &Up ", wxPoint(420, 90));
|
|
|
|
|
wxButton *button2 = new wxButton(panel, Btn_Down, "&Down", wxPoint(420, 120));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
|
|
|
|
|
|
|
|
|
|
mainsizer->Add( m_pListBox, 1, wxGROW|wxALL, 10 );
|
|
|
|
|
|
|
|
|
|
wxBoxSizer *bottomsizer = new wxBoxSizer( wxHORIZONTAL );
|
|
|
|
|
|
|
|
|
|
bottomsizer->Add( button1, 0, wxALL, 10 );
|
|
|
|
|
bottomsizer->Add( button2, 0, wxALL, 10 );
|
|
|
|
|
|
|
|
|
|
mainsizer->Add( bottomsizer, 0, wxCENTER );
|
|
|
|
|
|
|
|
|
|
// tell frame to make use of sizer (or constraints, if any)
|
1999-10-17 11:59:20 -04:00
|
|
|
|
panel->SetAutoLayout( TRUE );
|
|
|
|
|
panel->SetSizer( mainsizer );
|
1999-10-17 10:55:39 -04:00
|
|
|
|
|
|
|
|
|
// don't allow frame to get smaller than what the sizers tell ye
|
|
|
|
|
mainsizer->SetSizeHints( this );
|
|
|
|
|
|
|
|
|
|
|
1999-02-25 08:50:16 -05:00
|
|
|
|
Show(TRUE);
|
1998-05-22 15:57:05 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CheckListBoxFrame::~CheckListBoxFrame()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
1998-12-17 09:07:46 -05:00
|
|
|
|
void CheckListBoxFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
|
1998-05-22 15:57:05 -04:00
|
|
|
|
{
|
1999-02-25 08:50:16 -05:00
|
|
|
|
Close(TRUE);
|
1998-05-22 15:57:05 -04:00
|
|
|
|
}
|
|
|
|
|
|
1998-12-17 09:07:46 -05:00
|
|
|
|
void CheckListBoxFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
|
1998-05-22 15:57:05 -04:00
|
|
|
|
{
|
2000-01-22 17:47:30 -05:00
|
|
|
|
wxMessageBox(wxT("Demo of wxCheckListBox control\n"
|
1999-07-25 02:04:20 -04:00
|
|
|
|
"<EFBFBD> Vadim Zeitlin 1998-1999"),
|
2000-01-22 17:47:30 -05:00
|
|
|
|
wxT("About wxCheckListBox"),
|
1999-02-25 08:50:16 -05:00
|
|
|
|
wxICON_INFORMATION, this);
|
1998-05-22 15:57:05 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CheckListBoxFrame::OnListboxSelect(wxCommandEvent& event)
|
|
|
|
|
{
|
1999-02-25 08:50:16 -05:00
|
|
|
|
int nSel = event.GetSelection();
|
2000-01-22 17:47:30 -05:00
|
|
|
|
wxLogStatus(this, wxT("Item %d selected (%schecked)"), nSel,
|
|
|
|
|
m_pListBox->IsChecked(nSel) ? _T("") : wxT("not "));
|
1998-05-22 15:57:05 -04:00
|
|
|
|
}
|
|
|
|
|
|
1998-12-17 09:07:46 -05:00
|
|
|
|
void CheckListBoxFrame::OnListboxDblClick(wxCommandEvent& WXUNUSED(event))
|
1998-05-22 15:57:05 -04:00
|
|
|
|
{
|
1999-02-25 08:50:16 -05:00
|
|
|
|
wxString strSelection;
|
2000-01-22 17:47:30 -05:00
|
|
|
|
strSelection.sprintf(wxT("Item %d double clicked"), m_pListBox->GetSelection());
|
|
|
|
|
wxMessageDialog dialog(this, strSelection, wxT("wxCheckListBox message"), wxICON_INFORMATION);
|
1999-02-25 08:50:16 -05:00
|
|
|
|
dialog.ShowModal();
|
1998-05-22 15:57:05 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CheckListBoxFrame::OnCheckboxToggle(wxCommandEvent& event)
|
|
|
|
|
{
|
1999-02-25 08:50:16 -05:00
|
|
|
|
unsigned int nItem = event.GetInt();
|
1998-12-15 17:59:11 -05:00
|
|
|
|
|
2000-01-22 17:47:30 -05:00
|
|
|
|
wxLogStatus(this, wxT("item %d was %schecked"), nItem,
|
|
|
|
|
m_pListBox->IsChecked(nItem) ? wxT("") : wxT("un"));
|
1999-02-25 08:50:16 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CheckListBoxFrame::OnButtonUp(wxCommandEvent& WXUNUSED(event))
|
|
|
|
|
{
|
|
|
|
|
OnButtonMove(TRUE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CheckListBoxFrame::OnButtonDown(wxCommandEvent& WXUNUSED(event))
|
|
|
|
|
{
|
|
|
|
|
OnButtonMove(FALSE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CheckListBoxFrame::OnButtonMove(bool up)
|
|
|
|
|
{
|
|
|
|
|
int selection = m_pListBox->GetSelection();
|
|
|
|
|
if ( selection != -1 )
|
|
|
|
|
{
|
|
|
|
|
wxString label = m_pListBox->GetString(selection);
|
|
|
|
|
|
|
|
|
|
int positionNew = up ? selection - 1 : selection + 2;
|
1999-11-25 18:28:41 -05:00
|
|
|
|
if ( positionNew < 0 || positionNew > m_pListBox->GetCount() )
|
1999-02-25 08:50:16 -05:00
|
|
|
|
{
|
2000-01-22 17:47:30 -05:00
|
|
|
|
wxLogStatus(this, wxT("Can't move this item %s"), up ? wxT("up") : wxT("down"));
|
1999-02-25 08:50:16 -05:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
bool wasChecked = m_pListBox->IsChecked(selection);
|
|
|
|
|
|
|
|
|
|
int positionOld = up ? selection + 1 : selection;
|
|
|
|
|
|
|
|
|
|
// insert the item
|
|
|
|
|
m_pListBox->InsertItems(1, &label, positionNew);
|
|
|
|
|
|
|
|
|
|
// and delete the old one
|
|
|
|
|
m_pListBox->Delete(positionOld);
|
|
|
|
|
|
|
|
|
|
int selectionNew = up ? positionNew : positionNew - 1;
|
|
|
|
|
m_pListBox->Check(selectionNew, wasChecked);
|
|
|
|
|
m_pListBox->SetSelection(selectionNew);
|
|
|
|
|
|
1999-11-25 18:28:41 -05:00
|
|
|
|
AdjustColour(selection);
|
|
|
|
|
AdjustColour(selectionNew);
|
|
|
|
|
|
2000-01-22 17:47:30 -05:00
|
|
|
|
wxLogStatus(this, wxT("Item moved %s"), up ? wxT("up") : wxT("down"));
|
1999-02-25 08:50:16 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2000-01-22 17:47:30 -05:00
|
|
|
|
wxLogStatus(this, wxT("Please select an item"));
|
1999-02-25 08:50:16 -05:00
|
|
|
|
}
|
1998-12-15 17:59:11 -05:00
|
|
|
|
}
|
1999-11-25 18:28:41 -05:00
|
|
|
|
|
|
|
|
|
void CheckListBoxFrame::AdjustColour(size_t index)
|
|
|
|
|
{
|
|
|
|
|
// not implemented in other ports yet
|
|
|
|
|
#ifdef __WXMSW__
|
|
|
|
|
// even items have grey backround, odd ones - white
|
|
|
|
|
unsigned char c = index % 2 ? 255 : 200;
|
|
|
|
|
m_pListBox->GetItem(index)->SetBackgroundColour(wxColor(c, c, c));
|
|
|
|
|
#endif // wxMSW
|
|
|
|
|
}
|