1998-05-22 19:57:05 +00:00
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Name: checklst.cpp
|
|
|
|
|
// Purpose: wxCheckListBox sample
|
|
|
|
|
// Author: Vadim Zeitlin
|
1998-12-15 22:59:11 +00:00
|
|
|
|
// Modified by:
|
1998-05-22 19:57:05 +00:00
|
|
|
|
// Created: 13.11.97
|
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
|
// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
|
|
|
|
|
// Licence: wxWindows license
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
1998-10-21 08:34:32 +00:00
|
|
|
|
#ifdef __GNUG__
|
1999-02-25 13:50:16 +00:00
|
|
|
|
//#pragma implementation
|
1998-10-21 08:34:32 +00:00
|
|
|
|
#endif
|
1998-05-22 19:57:05 +00:00
|
|
|
|
|
|
|
|
|
// For compilers that support precompilation, includes "wx.h".
|
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
1999-02-25 13:50:16 +00:00
|
|
|
|
#pragma hdrstop
|
1998-05-22 19:57:05 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
1999-02-25 13:50:16 +00:00
|
|
|
|
#include "wx/wx.h"
|
1998-05-22 19:57:05 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
1998-12-17 08:40:34 +00:00
|
|
|
|
#ifdef __WXMSW__
|
1999-02-25 13:50:16 +00:00
|
|
|
|
#include "wx/ownerdrw.h"
|
1998-12-17 08:40:34 +00:00
|
|
|
|
#endif
|
1999-02-25 13:50:16 +00:00
|
|
|
|
|
|
|
|
|
#include "wx/log.h"
|
|
|
|
|
|
1998-05-22 19:57:05 +00:00
|
|
|
|
#include "wx/menuitem.h"
|
1998-12-17 08:40:34 +00:00
|
|
|
|
#include "wx/checklst.h"
|
|
|
|
|
|
1998-05-22 19:57:05 +00:00
|
|
|
|
// Define a new application type
|
|
|
|
|
class CheckListBoxApp: public wxApp
|
|
|
|
|
{
|
|
|
|
|
public:
|
1999-02-25 13:50:16 +00:00
|
|
|
|
bool OnInit();
|
1998-05-22 19:57:05 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Define a new frame type
|
|
|
|
|
class CheckListBoxFrame : public wxFrame
|
|
|
|
|
{
|
|
|
|
|
public:
|
1999-02-25 13:50:16 +00: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 19:57:05 +00:00
|
|
|
|
|
1999-02-25 13:50:16 +00:00
|
|
|
|
private:
|
|
|
|
|
void OnButtonMove(bool up);
|
1998-05-22 19:57:05 +00:00
|
|
|
|
|
1999-02-25 13:50:16 +00:00
|
|
|
|
wxCheckListBox *m_pListBox;
|
1998-05-22 19:57:05 +00:00
|
|
|
|
|
1999-02-25 13:50:16 +00:00
|
|
|
|
DECLARE_EVENT_TABLE()
|
1998-05-22 19:57:05 +00:00
|
|
|
|
};
|
|
|
|
|
|
1998-12-15 22:59:11 +00:00
|
|
|
|
enum
|
|
|
|
|
{
|
1999-02-25 13:50:16 +00:00
|
|
|
|
Menu_Quit = 1,
|
|
|
|
|
Control_First = 1000,
|
|
|
|
|
Control_Listbox,
|
|
|
|
|
Btn_Up,
|
|
|
|
|
Btn_Down
|
1998-05-22 19:57:05 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
BEGIN_EVENT_TABLE(CheckListBoxFrame, wxFrame)
|
1999-02-25 13:50:16 +00: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 19:57:05 +00:00
|
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
|
|
IMPLEMENT_APP(CheckListBoxApp);
|
|
|
|
|
|
|
|
|
|
// init our app: create windows
|
|
|
|
|
bool CheckListBoxApp::OnInit(void)
|
|
|
|
|
{
|
1999-02-25 13:50:16 +00:00
|
|
|
|
CheckListBoxFrame *pFrame = new CheckListBoxFrame
|
|
|
|
|
(
|
|
|
|
|
NULL,
|
|
|
|
|
"wxWindows Checklistbox Sample",
|
|
|
|
|
50, 50, 480, 320
|
|
|
|
|
);
|
|
|
|
|
SetTopWindow(pFrame);
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
1998-05-22 19:57:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// main frame constructor
|
1999-02-25 13:50:16 +00: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 19:57:05 +00:00
|
|
|
|
{
|
1999-02-25 13:50:16 +00:00
|
|
|
|
// create the status line
|
|
|
|
|
const int widths[] = { -1, 60 };
|
|
|
|
|
CreateStatusBar(2);
|
|
|
|
|
SetStatusWidths(2, widths);
|
1999-07-25 06:04:20 +00:00
|
|
|
|
wxLogStatus(this, _T("no selection"));
|
1999-02-25 13:50:16 +00: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
|
|
|
|
|
wxPanel *panel = new wxPanel(this, -1, wxPoint(0, 0),
|
|
|
|
|
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 14:44:03 +00:00
|
|
|
|
wxSize(400, 100), // listbox size
|
1999-02-25 13:50:16 +00:00
|
|
|
|
WXSIZEOF(aszChoices), // number of strings
|
|
|
|
|
astrChoices // array of strings
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
//m_pListBox->SetBackgroundColour(*wxGREEN);
|
|
|
|
|
|
|
|
|
|
delete [] astrChoices;
|
|
|
|
|
|
1999-04-12 11:51:36 +00:00
|
|
|
|
// not implemented in other ports yet
|
|
|
|
|
#ifdef __WXMSW__
|
1999-02-25 13:50:16 +00:00
|
|
|
|
// set grey background for every second entry
|
|
|
|
|
for ( ui = 0; ui < WXSIZEOF(aszChoices); ui += 2 ) {
|
|
|
|
|
m_pListBox->GetItem(ui)->SetBackgroundColour(wxColor(200, 200, 200));
|
|
|
|
|
}
|
1998-12-17 14:45:22 +00:00
|
|
|
|
#endif // wxGTK
|
1998-05-22 19:57:05 +00:00
|
|
|
|
|
1999-02-25 13:50:16 +00:00
|
|
|
|
m_pListBox->Check(2);
|
1998-05-22 19:57:05 +00:00
|
|
|
|
|
1999-02-25 13:50:16 +00:00
|
|
|
|
// create buttons for moving the items around
|
|
|
|
|
(void)new wxButton(panel, Btn_Up, " &Up ", wxPoint(420, 90));
|
|
|
|
|
(void)new wxButton(panel, Btn_Down, "&Down", wxPoint(420, 120));
|
|
|
|
|
|
|
|
|
|
Show(TRUE);
|
1998-05-22 19:57:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CheckListBoxFrame::~CheckListBoxFrame()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
1998-12-17 14:07:46 +00:00
|
|
|
|
void CheckListBoxFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
|
1998-05-22 19:57:05 +00:00
|
|
|
|
{
|
1999-02-25 13:50:16 +00:00
|
|
|
|
Close(TRUE);
|
1998-05-22 19:57:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-12-17 14:07:46 +00:00
|
|
|
|
void CheckListBoxFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
|
1998-05-22 19:57:05 +00:00
|
|
|
|
{
|
1999-07-25 06:04:20 +00:00
|
|
|
|
wxMessageBox(_T("Demo of wxCheckListBox control\n"
|
|
|
|
|
"<EFBFBD> Vadim Zeitlin 1998-1999"),
|
|
|
|
|
_T("About wxCheckListBox"),
|
1999-02-25 13:50:16 +00:00
|
|
|
|
wxICON_INFORMATION, this);
|
1998-05-22 19:57:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CheckListBoxFrame::OnListboxSelect(wxCommandEvent& event)
|
|
|
|
|
{
|
1999-02-25 13:50:16 +00:00
|
|
|
|
int nSel = event.GetSelection();
|
1999-07-25 06:04:20 +00:00
|
|
|
|
wxLogStatus(this, _T("item %d selected (%schecked)"), nSel,
|
|
|
|
|
m_pListBox->IsChecked(nSel) ? _T("") : _T("not "));
|
1998-05-22 19:57:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-12-17 14:07:46 +00:00
|
|
|
|
void CheckListBoxFrame::OnListboxDblClick(wxCommandEvent& WXUNUSED(event))
|
1998-05-22 19:57:05 +00:00
|
|
|
|
{
|
1999-02-25 13:50:16 +00:00
|
|
|
|
wxString strSelection;
|
1999-07-25 06:04:20 +00:00
|
|
|
|
strSelection.sprintf(_T("item %d double clicked"), m_pListBox->GetSelection());
|
1999-02-25 13:50:16 +00:00
|
|
|
|
wxMessageDialog dialog(this, strSelection);
|
|
|
|
|
dialog.ShowModal();
|
1998-05-22 19:57:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CheckListBoxFrame::OnCheckboxToggle(wxCommandEvent& event)
|
|
|
|
|
{
|
1999-02-25 13:50:16 +00:00
|
|
|
|
unsigned int nItem = event.GetInt();
|
1998-12-15 22:59:11 +00:00
|
|
|
|
|
1999-07-25 06:04:20 +00:00
|
|
|
|
wxLogStatus(this, _T("item %d was %schecked"), nItem,
|
|
|
|
|
m_pListBox->IsChecked(nItem) ? _T("") : _T("un"));
|
1999-02-25 13:50:16 +00: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;
|
|
|
|
|
if ( positionNew < 0 || positionNew > m_pListBox->Number() )
|
|
|
|
|
{
|
1999-07-25 06:04:20 +00:00
|
|
|
|
wxLogStatus(this, _T("Can't move this item %s"), up ? _T("up") : _T("down"));
|
1999-02-25 13:50:16 +00: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-07-25 06:04:20 +00:00
|
|
|
|
wxLogStatus(this, _T("Item moved %s"), up ? _T("up") : _T("down"));
|
1999-02-25 13:50:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
1999-07-25 06:04:20 +00:00
|
|
|
|
wxLogStatus(this, _T("Please select an item"));
|
1999-02-25 13:50:16 +00:00
|
|
|
|
}
|
1998-12-15 22:59:11 +00:00
|
|
|
|
}
|