1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/univ/checklst.cpp
3 // Purpose: wxCheckListBox implementation
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #include "wx/wxprec.h"
25 #if wxUSE_CHECKLISTBOX
27 #include "wx/checklst.h"
31 #include "wx/dcclient.h"
32 #include "wx/validate.h"
35 #include "wx/univ/renderer.h"
36 #include "wx/univ/inphand.h"
37 #include "wx/univ/theme.h"
39 // ----------------------------------------------------------------------------
40 // wxStdCheckListBoxInputHandler
41 // ----------------------------------------------------------------------------
43 class WXDLLEXPORT wxStdCheckListboxInputHandler
: public wxStdInputHandler
46 wxStdCheckListboxInputHandler(wxInputHandler
*inphand
);
48 virtual bool HandleKey(wxInputConsumer
*consumer
,
49 const wxKeyEvent
& event
,
51 virtual bool HandleMouse(wxInputConsumer
*consumer
,
52 const wxMouseEvent
& event
);
55 // ============================================================================
56 // implementation of wxCheckListBox
57 // ============================================================================
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 void wxCheckListBox::Init()
67 wxCheckListBox::wxCheckListBox(wxWindow
*parent
,
71 const wxArrayString
& choices
,
73 const wxValidator
& validator
,
78 Create(parent
, id
, pos
, size
, choices
, style
, validator
, name
);
81 bool wxCheckListBox::Create(wxWindow
*parent
,
85 const wxArrayString
& choices
,
87 const wxValidator
& validator
,
90 wxCArrayString
chs(choices
);
92 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
93 style
, validator
, name
);
96 bool wxCheckListBox::Create(wxWindow
*parent
,
101 const wxString choices
[],
103 const wxValidator
& validator
,
104 const wxString
&name
)
106 if ( !wxListBox::Create(parent
, id
, pos
, size
,
107 n
, choices
, style
, validator
, name
) )
110 CreateInputHandler(wxINP_HANDLER_CHECKLISTBOX
);
115 // ----------------------------------------------------------------------------
116 // wxCheckListBox functions
117 // ----------------------------------------------------------------------------
119 bool wxCheckListBox::IsChecked(unsigned int item
) const
121 wxCHECK_MSG( IsValid(item
), false,
122 wxT("invalid index in wxCheckListBox::IsChecked") );
124 return m_checks
[item
] != 0;
127 void wxCheckListBox::Check(unsigned int item
, bool check
)
129 wxCHECK_RET( IsValid(item
),
130 wxT("invalid index in wxCheckListBox::Check") );
132 // intermediate var is needed to avoid compiler warning with VC++
133 bool isChecked
= m_checks
[item
] != 0;
134 if ( check
!= isChecked
)
136 m_checks
[item
] = check
;
142 // ----------------------------------------------------------------------------
143 // methods forwarded to wxListBox
144 // ----------------------------------------------------------------------------
146 void wxCheckListBox::DoDeleteOneItem(unsigned int n
)
148 wxListBox::DoDeleteOneItem(n
);
150 m_checks
.RemoveAt(n
);
153 void wxCheckListBox::OnItemInserted(unsigned int pos
)
155 m_checks
.Insert(false, pos
);
158 void wxCheckListBox::DoClear()
163 // ----------------------------------------------------------------------------
165 // ----------------------------------------------------------------------------
167 wxSize
wxCheckListBox::DoGetBestClientSize() const
169 wxSize size
= wxListBox::DoGetBestClientSize();
170 size
.x
+= GetRenderer()->GetCheckBitmapSize().x
;
175 void wxCheckListBox::DoDrawRange(wxControlRenderer
*renderer
,
176 int itemFirst
, int itemLast
)
178 renderer
->DrawCheckItems(this, itemFirst
, itemLast
);
181 // ----------------------------------------------------------------------------
183 // ----------------------------------------------------------------------------
185 bool wxCheckListBox::PerformAction(const wxControlAction
& action
,
187 const wxString
& strArg
)
189 if ( action
== wxACTION_CHECKLISTBOX_TOGGLE
)
191 int sel
= (int)numArg
;
194 sel
= GetSelection();
199 Check(sel
, !IsChecked(sel
));
201 SendEvent(wxEVT_CHECKLISTBOX
, sel
);
206 return wxListBox::PerformAction(action
, numArg
, strArg
);
213 wxInputHandler
*wxCheckListBox::GetStdInputHandler(wxInputHandler
*handlerDef
)
215 static wxStdCheckListboxInputHandler
s_handler(handlerDef
);
220 // ----------------------------------------------------------------------------
221 // wxStdCheckListboxInputHandler
222 // ----------------------------------------------------------------------------
224 wxStdCheckListboxInputHandler::
225 wxStdCheckListboxInputHandler(wxInputHandler
*inphand
)
226 : wxStdInputHandler(wxListBox::GetStdInputHandler(inphand
))
230 bool wxStdCheckListboxInputHandler::HandleKey(wxInputConsumer
*consumer
,
231 const wxKeyEvent
& event
,
234 if ( pressed
&& (event
.GetKeyCode() == WXK_SPACE
) )
235 consumer
->PerformAction(wxACTION_CHECKLISTBOX_TOGGLE
);
237 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
240 bool wxStdCheckListboxInputHandler::HandleMouse(wxInputConsumer
*consumer
,
241 const wxMouseEvent
& event
)
243 if ( event
.LeftDown() || event
.LeftDClick() )
245 wxCheckListBox
*lbox
= wxStaticCast(consumer
->GetInputWindow(), wxCheckListBox
);
248 wxPoint pt
= event
.GetPosition();
249 pt
-= consumer
->GetInputWindow()->GetClientAreaOrigin();
250 lbox
->CalcUnscrolledPosition(pt
.x
, pt
.y
, &x
, &y
);
252 wxRenderer
*renderer
= lbox
->GetRenderer();
253 x
-= renderer
->GetCheckItemMargin();
255 int item
= y
/ lbox
->GetLineHeight();
257 x
< renderer
->GetCheckBitmapSize().x
&&
259 (unsigned int)item
< lbox
->GetCount() )
261 lbox
->PerformAction(wxACTION_CHECKLISTBOX_TOGGLE
, item
);
267 return wxStdInputHandler::HandleMouse(consumer
, event
);
270 #endif // wxUSE_CHECKLISTBOX