1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/univ/checklst.cpp
3 // Purpose: wxCheckListBox implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
26 #if wxUSE_CHECKLISTBOX
28 #include "wx/checklst.h"
32 #include "wx/dcclient.h"
33 #include "wx/validate.h"
36 #include "wx/univ/renderer.h"
37 #include "wx/univ/inphand.h"
38 #include "wx/univ/theme.h"
40 // ----------------------------------------------------------------------------
41 // wxStdCheckListBoxInputHandler
42 // ----------------------------------------------------------------------------
44 class WXDLLEXPORT wxStdCheckListboxInputHandler
: public wxStdInputHandler
47 wxStdCheckListboxInputHandler(wxInputHandler
*inphand
);
49 virtual bool HandleKey(wxInputConsumer
*consumer
,
50 const wxKeyEvent
& event
,
52 virtual bool HandleMouse(wxInputConsumer
*consumer
,
53 const wxMouseEvent
& event
);
56 // ============================================================================
57 // implementation of wxCheckListBox
58 // ============================================================================
60 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
)
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 void wxCheckListBox::Init()
70 wxCheckListBox::wxCheckListBox(wxWindow
*parent
,
74 const wxArrayString
& choices
,
76 const wxValidator
& validator
,
81 Create(parent
, id
, pos
, size
, choices
, style
, validator
, name
);
84 bool wxCheckListBox::Create(wxWindow
*parent
,
88 const wxArrayString
& choices
,
90 const wxValidator
& validator
,
93 wxCArrayString
chs(choices
);
95 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
96 style
, validator
, name
);
99 bool wxCheckListBox::Create(wxWindow
*parent
,
104 const wxString choices
[],
106 const wxValidator
& validator
,
107 const wxString
&name
)
109 if ( !wxListBox::Create(parent
, id
, pos
, size
,
110 n
, choices
, style
, validator
, name
) )
113 CreateInputHandler(wxINP_HANDLER_CHECKLISTBOX
);
118 // ----------------------------------------------------------------------------
119 // wxCheckListBox functions
120 // ----------------------------------------------------------------------------
122 bool wxCheckListBox::IsChecked(unsigned int item
) const
124 wxCHECK_MSG( IsValid(item
), false,
125 wxT("invalid index in wxCheckListBox::IsChecked") );
127 return m_checks
[item
] != 0;
130 void wxCheckListBox::Check(unsigned int item
, bool check
)
132 wxCHECK_RET( IsValid(item
),
133 wxT("invalid index in wxCheckListBox::Check") );
135 // intermediate var is needed to avoid compiler warning with VC++
136 bool isChecked
= m_checks
[item
] != 0;
137 if ( check
!= isChecked
)
139 m_checks
[item
] = check
;
145 // ----------------------------------------------------------------------------
146 // methods forwarded to wxListBox
147 // ----------------------------------------------------------------------------
149 void wxCheckListBox::DoDeleteOneItem(unsigned int n
)
151 wxListBox::DoDeleteOneItem(n
);
153 m_checks
.RemoveAt(n
);
156 void wxCheckListBox::OnItemInserted(unsigned int pos
)
158 m_checks
.Insert(false, pos
);
161 void wxCheckListBox::DoClear()
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
170 wxSize
wxCheckListBox::DoGetBestClientSize() const
172 wxSize size
= wxListBox::DoGetBestClientSize();
173 size
.x
+= GetRenderer()->GetCheckBitmapSize().x
;
178 void wxCheckListBox::DoDrawRange(wxControlRenderer
*renderer
,
179 int itemFirst
, int itemLast
)
181 renderer
->DrawCheckItems(this, itemFirst
, itemLast
);
184 // ----------------------------------------------------------------------------
186 // ----------------------------------------------------------------------------
188 bool wxCheckListBox::PerformAction(const wxControlAction
& action
,
190 const wxString
& strArg
)
192 if ( action
== wxACTION_CHECKLISTBOX_TOGGLE
)
194 int sel
= (int)numArg
;
197 sel
= GetSelection();
202 Check(sel
, !IsChecked(sel
));
204 SendEvent(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, sel
);
209 return wxListBox::PerformAction(action
, numArg
, strArg
);
216 wxInputHandler
*wxCheckListBox::GetStdInputHandler(wxInputHandler
*handlerDef
)
218 static wxStdCheckListboxInputHandler
s_handler(handlerDef
);
223 // ----------------------------------------------------------------------------
224 // wxStdCheckListboxInputHandler
225 // ----------------------------------------------------------------------------
227 wxStdCheckListboxInputHandler::
228 wxStdCheckListboxInputHandler(wxInputHandler
*inphand
)
229 : wxStdInputHandler(wxListBox::GetStdInputHandler(inphand
))
233 bool wxStdCheckListboxInputHandler::HandleKey(wxInputConsumer
*consumer
,
234 const wxKeyEvent
& event
,
237 if ( pressed
&& (event
.GetKeyCode() == WXK_SPACE
) )
238 consumer
->PerformAction(wxACTION_CHECKLISTBOX_TOGGLE
);
240 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
243 bool wxStdCheckListboxInputHandler::HandleMouse(wxInputConsumer
*consumer
,
244 const wxMouseEvent
& event
)
246 if ( event
.LeftDown() || event
.LeftDClick() )
248 wxCheckListBox
*lbox
= wxStaticCast(consumer
->GetInputWindow(), wxCheckListBox
);
251 wxPoint pt
= event
.GetPosition();
252 pt
-= consumer
->GetInputWindow()->GetClientAreaOrigin();
253 lbox
->CalcUnscrolledPosition(pt
.x
, pt
.y
, &x
, &y
);
255 wxRenderer
*renderer
= lbox
->GetRenderer();
256 x
-= renderer
->GetCheckItemMargin();
258 int item
= y
/ lbox
->GetLineHeight();
260 x
< renderer
->GetCheckBitmapSize().x
&&
262 (unsigned int)item
< lbox
->GetCount() )
264 lbox
->PerformAction(wxACTION_CHECKLISTBOX_TOGGLE
, item
);
270 return wxStdInputHandler::HandleMouse(consumer
, event
);
273 #endif // wxUSE_CHECKLISTBOX