]>
git.saurik.com Git - wxWidgets.git/blob - samples/checklst/checklst.cpp
cba00d91aecba55817c290b1def9bc4fcf634bf0
   1 /////////////////////////////////////////////////////////////////////////////// 
   3 // Purpose:     wxCheckListBox sample 
   4 // Author:      Vadim Zeitlin 
   8 // Copyright:   (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> 
   9 // Licence:     wxWindows license 
  10 /////////////////////////////////////////////////////////////////////////////// 
  13     //#pragma implementation 
  16 // For compilers that support precompilation, includes "wx.h". 
  17 #include "wx/wxprec.h" 
  28     #include  "wx/ownerdrw.h" 
  34 #include  "wx/menuitem.h" 
  35 #include  "wx/checklst.h" 
  37 // Define a new application type 
  38 class CheckListBoxApp
: public wxApp
 
  44 // Define a new frame type 
  45 class CheckListBoxFrame 
: public wxFrame
 
  49     CheckListBoxFrame(wxFrame 
*frame
, const char *title
, 
  50                       int x
, int y
, int w
, int h
); 
  51     virtual ~CheckListBoxFrame(); 
  54     void OnQuit           (wxCommandEvent
& event
); 
  55     void OnAbout          (wxCommandEvent
& event
); 
  56     void OnToggleSelection(wxCommandEvent
& event
); 
  57     void OnListboxSelect  (wxCommandEvent
& event
); 
  58     void OnCheckboxToggle (wxCommandEvent
& event
); 
  59     void OnListboxDblClick(wxCommandEvent
& event
); 
  60     void OnButtonUp       (wxCommandEvent
& event
); 
  61     void OnButtonDown     (wxCommandEvent
& event
); 
  64     void CreateCheckListbox(long flags 
= 0); 
  66     void OnButtonMove(bool up
); 
  68     void AdjustColour(size_t index
); 
  72     wxCheckListBox 
*m_pListBox
; 
  89 BEGIN_EVENT_TABLE(CheckListBoxFrame
, wxFrame
) 
  90     EVT_MENU(Menu_About
, CheckListBoxFrame::OnAbout
) 
  91     EVT_MENU(Menu_Quit
, CheckListBoxFrame::OnQuit
) 
  93     EVT_MENU(Menu_Selection
, CheckListBoxFrame::OnToggleSelection
) 
  95     EVT_LISTBOX(Control_Listbox
, CheckListBoxFrame::OnListboxSelect
) 
  96     EVT_CHECKLISTBOX(Control_Listbox
, CheckListBoxFrame::OnCheckboxToggle
) 
  97     EVT_LISTBOX_DCLICK(Control_Listbox
, CheckListBoxFrame::OnListboxDblClick
) 
  99     EVT_BUTTON(Btn_Up
, CheckListBoxFrame::OnButtonUp
) 
 100     EVT_BUTTON(Btn_Down
, CheckListBoxFrame::OnButtonDown
) 
 103 IMPLEMENT_APP(CheckListBoxApp
); 
 105 // init our app: create windows 
 106 bool CheckListBoxApp::OnInit(void) 
 108     CheckListBoxFrame 
*pFrame 
= new CheckListBoxFrame
 
 111                                      _T("wxWindows Checklistbox Sample"), 
 114     SetTopWindow(pFrame
); 
 119 // main frame constructor 
 120 CheckListBoxFrame::CheckListBoxFrame(wxFrame 
*frame
, 
 122                                      int x
, int y
, int w
, int h
) 
 123                  : wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
)) 
 125     // create the status line 
 126     const int widths
[] = { -1, 60 }; 
 128     SetStatusWidths(2, widths
); 
 129     wxLogStatus(this, _T("no selection")); 
 135     wxMenu 
*menuFile 
= new wxMenu
; 
 136     menuFile
->Append(Menu_About
, _T("&About...\tF1")); 
 137     menuFile
->AppendSeparator(); 
 138     menuFile
->Append(Menu_Quit
, _T("E&xit\tAlt-X")); 
 141     wxMenu 
*menuList 
= new wxMenu
; 
 142     menuList
->AppendCheckItem(Menu_Selection
, _T("Multiple selection\tCtrl-M")); 
 144     // put it all together 
 145     wxMenuBar 
*menu_bar 
= new wxMenuBar
; 
 146     menu_bar
->Append(menuFile
, _T("&File")); 
 147     menu_bar
->Append(menuList
, _T("&List")); 
 148     SetMenuBar(menu_bar
); 
 150     // make a panel with some controls 
 151     m_panel 
= new wxPanel(this, -1, wxPoint(0, 0), 
 152                           wxSize(400, 200), wxTAB_TRAVERSAL
); 
 154     CreateCheckListbox(); 
 156     // create buttons for moving the items around 
 157     wxButton 
*button1 
= new wxButton(m_panel
, Btn_Up
, _T("   &Up  "), wxPoint(420, 90)); 
 158     wxButton 
*button2 
= new wxButton(m_panel
, Btn_Down
, _T("&Down"), wxPoint(420, 120)); 
 161     wxBoxSizer 
*mainsizer 
= new wxBoxSizer( wxVERTICAL 
); 
 163     mainsizer
->Add( m_pListBox
, 1, wxGROW
|wxALL
, 10 ); 
 165     wxBoxSizer 
*bottomsizer 
= new wxBoxSizer( wxHORIZONTAL 
); 
 167     bottomsizer
->Add( button1
, 0, wxALL
, 10 ); 
 168     bottomsizer
->Add( button2
, 0, wxALL
, 10 ); 
 170     mainsizer
->Add( bottomsizer
, 0, wxCENTER 
); 
 172     // tell frame to make use of sizer (or constraints, if any) 
 173     m_panel
->SetAutoLayout( TRUE 
); 
 174     m_panel
->SetSizer( mainsizer 
); 
 176     // don't allow frame to get smaller than what the sizers tell ye 
 177     mainsizer
->SetSizeHints( this ); 
 182 void CheckListBoxFrame::CreateCheckListbox(long flags
) 
 185     static const wxChar 
*aszChoices
[] = 
 188         _T("First"), _T("Second"), _T("Third"), 
 189         _T("Fourth"), _T("Fifth"), _T("Sixth"), 
 190         _T("Seventh"), _T("Eighth"), _T("Nineth") 
 193     wxString 
*astrChoices 
= new wxString
[WXSIZEOF(aszChoices
)]; 
 195     for ( ui 
= 0; ui 
< WXSIZEOF(aszChoices
); ui
++ ) 
 196         astrChoices
[ui
] = aszChoices
[ui
]; 
 198     m_pListBox 
= new wxCheckListBox
 
 201          Control_Listbox
,       // control id 
 202          wxPoint(10, 10),       // listbox poistion 
 203          wxSize(400, 100),      // listbox size 
 204          WXSIZEOF(aszChoices
),  // number of strings 
 205          astrChoices
,           // array of strings 
 209     //m_pListBox->SetBackgroundColour(*wxGREEN); 
 211     delete [] astrChoices
; 
 213     // set grey background for every second entry 
 214     for ( ui 
= 0; ui 
< WXSIZEOF(aszChoices
); ui 
+= 2 ) { 
 218     m_pListBox
->Check(2); 
 219     m_pListBox
->Select(3); 
 222 CheckListBoxFrame::~CheckListBoxFrame() 
 226 void CheckListBoxFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
)) 
 231 void CheckListBoxFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
)) 
 233     wxMessageBox(wxT("Demo of wxCheckListBox control\n© Vadim Zeitlin 1998-2002"), 
 234                  wxT("About wxCheckListBox"), 
 235                  wxICON_INFORMATION
, this); 
 238 void CheckListBoxFrame::OnToggleSelection(wxCommandEvent
& event
) 
 240     wxSizer 
*sizer 
= m_panel
->GetSizer(); 
 242     sizer
->Remove(m_pListBox
); 
 245     CreateCheckListbox(event
.IsChecked() ? wxLB_EXTENDED 
: 0); 
 247     sizer
->Insert(0, m_pListBox
, 1, wxGROW 
| wxALL
, 10); 
 252 void CheckListBoxFrame::OnListboxSelect(wxCommandEvent
& event
) 
 254     int nSel 
= event
.GetSelection(); 
 255     wxLogStatus(this, wxT("Item %d selected (%schecked)"), nSel
, 
 256                       m_pListBox
->IsChecked(nSel
) ? _T("") : wxT("not ")); 
 259 void CheckListBoxFrame::OnListboxDblClick(wxCommandEvent
& WXUNUSED(event
)) 
 261     wxString strSelection
; 
 262     strSelection
.sprintf(wxT("Item %d double clicked"), m_pListBox
->GetSelection()); 
 263     wxMessageDialog 
dialog(this, strSelection
, wxT("wxCheckListBox message"), wxICON_INFORMATION
); 
 267 void CheckListBoxFrame::OnCheckboxToggle(wxCommandEvent
& event
) 
 269     unsigned int nItem 
= event
.GetInt(); 
 271     wxLogStatus(this, wxT("item %d was %schecked"), nItem
, 
 272                       m_pListBox
->IsChecked(nItem
) ? wxT("") : wxT("un")); 
 275 void CheckListBoxFrame::OnButtonUp(wxCommandEvent
& WXUNUSED(event
)) 
 280 void CheckListBoxFrame::OnButtonDown(wxCommandEvent
& WXUNUSED(event
)) 
 285 void CheckListBoxFrame::OnButtonMove(bool up
) 
 287     int selection 
= m_pListBox
->GetSelection(); 
 288     if ( selection 
!= -1 ) 
 290         wxString label 
= m_pListBox
->GetString(selection
); 
 292         int positionNew 
= up 
? selection 
- 1 : selection 
+ 2; 
 293         if ( positionNew 
< 0 || positionNew 
> m_pListBox
->GetCount() ) 
 295             wxLogStatus(this, wxT("Can't move this item %s"), up 
? wxT("up") : wxT("down")); 
 299             bool wasChecked 
= m_pListBox
->IsChecked(selection
); 
 301             int positionOld 
= up 
? selection 
+ 1 : selection
; 
 304             m_pListBox
->InsertItems(1, &label
, positionNew
); 
 306             // and delete the old one 
 307             m_pListBox
->Delete(positionOld
); 
 309             int selectionNew 
= up 
? positionNew 
: positionNew 
- 1; 
 310             m_pListBox
->Check(selectionNew
, wasChecked
); 
 311             m_pListBox
->SetSelection(selectionNew
); 
 313             AdjustColour(selection
); 
 314             AdjustColour(selectionNew
); 
 316             wxLogStatus(this, wxT("Item moved %s"), up 
? wxT("up") : wxT("down")); 
 321         wxLogStatus(this, wxT("Please select an item")); 
 325 void CheckListBoxFrame::AdjustColour(size_t index
) 
 327     // not implemented in other ports yet 
 329     // even items have grey backround, odd ones - white 
 330     unsigned char c 
= index 
% 2 ? 255 : 200; 
 331     m_pListBox
->GetItem(index
)->SetBackgroundColour(wxColor(c
, c
, c
));