]> git.saurik.com Git - wxWidgets.git/blame - samples/checklst/checklst.cpp
Trying to repair what CVS can do to source code
[wxWidgets.git] / samples / checklst / checklst.cpp
CommitLineData
457814b5
JS
1///////////////////////////////////////////////////////////////////////////////
2// Name: checklst.cpp
3// Purpose: wxCheckListBox sample
4// Author: Vadim Zeitlin
655822f3 5// Modified by:
457814b5
JS
6// Created: 13.11.97
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9// Licence: wxWindows license
10///////////////////////////////////////////////////////////////////////////////
11
3349fe92
UU
12#ifdef __GNUG__
13//#pragma implementation
14#endif
457814b5
JS
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20 #pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
24 #include "wx/wx.h"
25#endif
26
b292e2f5 27#ifdef __WXMSW__
457814b5 28#include "wx/ownerdrw.h"
b292e2f5 29#endif
457814b5 30#include "wx/menuitem.h"
b292e2f5
RR
31#include "wx/checklst.h"
32
457814b5
JS
33// Define a new application type
34class CheckListBoxApp: public wxApp
35{
36public:
37 bool OnInit();
38};
39
40// Define a new frame type
41class CheckListBoxFrame : public wxFrame
42{
43public:
44 // ctor & dtor
45 CheckListBoxFrame(wxFrame *frame, char *title, int x, int y, int w, int h);
46 ~CheckListBoxFrame();
47
48 // notifications
49 void OnQuit (wxCommandEvent& event);
50 void OnAbout (wxCommandEvent& event);
51 void OnListboxSelect (wxCommandEvent& event);
52 void OnCheckboxToggle (wxCommandEvent& event);
53 void OnListboxDblClick (wxCommandEvent& event);
54 bool OnClose () { return TRUE; }
55
56 DECLARE_EVENT_TABLE()
57
58private:
59 wxCheckListBox *m_pListBox;
60};
61
655822f3
VZ
62enum
63{
457814b5
JS
64 Menu_Quit = 1,
65 Control_First = 1000,
66 Control_Listbox, Control_Listbox2,
67};
68
69BEGIN_EVENT_TABLE(CheckListBoxFrame, wxFrame)
70 EVT_MENU(Menu_Quit, CheckListBoxFrame::OnQuit)
71 EVT_LISTBOX(Control_Listbox, CheckListBoxFrame::OnListboxSelect)
72 EVT_CHECKLISTBOX(Control_Listbox, CheckListBoxFrame::OnCheckboxToggle)
ccfd0a25 73 EVT_LISTBOX_DCLICK(Control_Listbox, CheckListBoxFrame::OnListboxDblClick)
457814b5
JS
74END_EVENT_TABLE()
75
76IMPLEMENT_APP(CheckListBoxApp);
77
78// init our app: create windows
79bool CheckListBoxApp::OnInit(void)
80{
81 CheckListBoxFrame *pFrame = new CheckListBoxFrame(NULL, "wxWindows Ownerdraw Sample",
82 50, 50, 450, 320);
83 SetTopWindow(pFrame);
84
85 return TRUE;
86}
87
88// main frame constructor
89CheckListBoxFrame::CheckListBoxFrame(wxFrame *frame, char *title, int x, int y, int w, int h)
90 : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
91{
ccfd0a25
VZ
92 // create the status line
93 const int widths[] = { -1, 60 };
94 CreateStatusBar(2);
95 SetStatusWidths(2, widths);
96 SetStatusText("no selection", 0);
457814b5
JS
97
98 // Make a menubar
99 wxMenu *file_menu = new wxMenu;
100
101 // construct submenu
102 file_menu->Append(Menu_Quit, "E&xit");
103
104 wxMenuBar *menu_bar = new wxMenuBar;
105 menu_bar->Append(file_menu, "&File");
106 SetMenuBar(menu_bar);
107
108 // make a panel with some controls
655822f3 109 wxPanel *pPanel = new wxPanel(this, -1, wxPoint(0, 0),
457814b5
JS
110 wxSize(400, 200), wxTAB_TRAVERSAL);
111
112 // check list box
655822f3 113 static const char* aszChoices[] = { "Hello", "world", "and",
457814b5
JS
114 "goodbye", "cruel", "world",
115 "-------", "owner-drawn", "listbox" };
116
117 wxString *astrChoices = new wxString[WXSIZEOF(aszChoices)];
8d5e3034 118 unsigned int ui;
457814b5
JS
119 for ( ui = 0; ui < WXSIZEOF(aszChoices); ui++ )
120 astrChoices[ui] = aszChoices[ui];
121
122 m_pListBox = new wxCheckListBox
655822f3
VZ
123 (
124 pPanel, // parent
125 Control_Listbox, // control id
126 wxPoint(10, 10), // listbox poistion
457814b5 127 wxSize(400, 200), // listbox size
655822f3 128 WXSIZEOF(aszChoices), // number of strings
457814b5 129 astrChoices // array of strings
655822f3
VZ
130 );
131
457814b5
JS
132 delete [] astrChoices;
133
ccfd0a25
VZ
134 // not implemented in wxGTK yet
135#ifndef __WXGTK__
457814b5 136 for ( ui = 0; ui < WXSIZEOF(aszChoices); ui += 2 ) {
8d5e3034 137 m_pListBox->GetItem(ui)->SetBackgroundColour(wxColor(200, 200, 200));
457814b5 138 }
ccfd0a25 139#endif // wxGTK
457814b5
JS
140
141 m_pListBox->Check(2);
142
457814b5
JS
143 Show(TRUE);
144}
145
146CheckListBoxFrame::~CheckListBoxFrame()
147{
148}
149
4f22cf8d 150void CheckListBoxFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
457814b5
JS
151{
152 Close(TRUE);
153}
154
4f22cf8d 155void CheckListBoxFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
457814b5
JS
156{
157 wxMessageDialog dialog(this, "Demo of wxCheckListBox control\n"
158 "About wxCheckListBox", wxYES_NO | wxCANCEL);
159 dialog.ShowModal();
160}
161
162void CheckListBoxFrame::OnListboxSelect(wxCommandEvent& event)
163{
164 wxString strSelection;
8d5e3034 165 unsigned int nSel = event.GetSelection();
457814b5 166 strSelection.sprintf("item %d selected (%schecked)", nSel,
b292e2f5 167 m_pListBox->IsChecked((int)nSel) ? "" : "not ");
457814b5
JS
168 SetStatusText(strSelection);
169}
170
4f22cf8d 171void CheckListBoxFrame::OnListboxDblClick(wxCommandEvent& WXUNUSED(event))
457814b5
JS
172{
173 wxString strSelection;
174 strSelection.sprintf("item %d double clicked", m_pListBox->GetSelection());
175 wxMessageDialog dialog(this, strSelection);
176 dialog.ShowModal();
177}
178
179void CheckListBoxFrame::OnCheckboxToggle(wxCommandEvent& event)
180{
181 wxString strSelection;
8d5e3034 182 unsigned int nItem = event.GetInt();
655822f3 183
457814b5
JS
184 strSelection.sprintf("item %d was %schecked", nItem,
185 m_pListBox->IsChecked(nItem) ? "" : "un");
186 SetStatusText(strSelection);
655822f3 187}