1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/checklst.cpp
3 // Purpose: implementation of wxCheckListBox class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "checklst.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
34 #include "wx/object.h"
35 #include "wx/colour.h"
37 #include "wx/bitmap.h"
38 #include "wx/window.h"
39 #include "wx/listbox.h"
40 #include "wx/dcmemory.h"
42 #include "wx/settings.h"
47 #include "wx/ownerdrw.h"
48 #include "wx/checklst.h"
53 #if defined(__GNUWIN32_OLD__)
54 #include "wx/msw/gnuwin32/extra.h"
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // get item (converted to right type)
62 #define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n)))
64 // ============================================================================
66 // ============================================================================
68 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
)
70 // ----------------------------------------------------------------------------
71 // declaration and implementation of wxCheckListBoxItem class
72 // ----------------------------------------------------------------------------
74 class wxCheckListBoxItem
: public wxOwnerDrawn
76 friend class WXDLLEXPORT wxCheckListBox
;
79 wxCheckListBoxItem(wxCheckListBox
*pParent
, size_t nIndex
);
82 virtual bool OnDrawItem(wxDC
& dc
, const wxRect
& rc
, wxODAction act
, wxODStatus stat
);
84 // simple accessors and operations
85 bool IsChecked() const { return m_bChecked
; }
87 void Check(bool bCheck
);
88 void Toggle() { Check(!IsChecked()); }
94 DECLARE_NO_COPY_CLASS(wxCheckListBoxItem
)
96 wxCheckListBox
*m_pParent
;
100 wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox
*pParent
, size_t nIndex
)
101 : wxOwnerDrawn(wxEmptyString
, TRUE
) // checkable
107 // we don't initialize m_nCheckHeight/Width vars because it's
108 // done in OnMeasure while they are used only in OnDraw and we
109 // know that there will always be OnMeasure before OnDraw
112 SetMarginWidth(GetDefaultMarginWidth());
116 * JACS - I've got the owner-draw stuff partially working with WIN16,
117 * with a really horrible-looking cross for wxCheckListBox instead of a
118 * check - could use a bitmap check-mark instead, defined in wx.rc.
119 * Also there's a refresh problem whereby it doesn't always draw the
120 * check until you click to the right of it, which is OK for WIN32.
123 bool wxCheckListBoxItem::OnDrawItem(wxDC
& dc
, const wxRect
& rc
,
124 wxODAction act
, wxODStatus stat
)
127 stat
= (wxOwnerDrawn::wxODStatus
)(stat
| wxOwnerDrawn::wxODChecked
);
129 if ( wxOwnerDrawn::OnDrawItem(dc
, rc
, act
, stat
) ) {
130 // ## using native API for performance and precision
131 size_t nCheckWidth
= GetDefaultMarginWidth(),
132 nCheckHeight
= m_pParent
->GetItemHeight();
137 HDC hdc
= (HDC
)dc
.GetHDC();
140 HPEN hpenBack
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_WINDOW
)),
141 hpenGray
= CreatePen(PS_SOLID
, 0, RGB(128, 128, 128)),
142 hpenPrev
= (HPEN
)SelectObject(hdc
, hpenBack
);
144 // we erase the 1-pixel border
145 Rectangle(hdc
, x
, y
, x
+ nCheckWidth
, y
+ nCheckHeight
);
147 // shift check mark 1 pixel to the right (it looks better like this)
151 // first create a monochrome bitmap in a memory DC
152 HDC hdcMem
= CreateCompatibleDC(hdc
);
153 HBITMAP hbmpCheck
= CreateBitmap(nCheckWidth
, nCheckHeight
, 1, 1, 0);
154 HBITMAP hbmpOld
= (HBITMAP
)SelectObject(hdcMem
, hbmpCheck
);
156 // then draw a check mark into it
161 rect
.right
= nCheckWidth
;
162 rect
.bottom
= nCheckHeight
;
164 DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
166 // finally copy it to screen DC and clean up
167 BitBlt(hdc
, x
, y
, nCheckWidth
- 1, nCheckHeight
,
168 hdcMem
, 0, 0, SRCCOPY
);
170 SelectObject(hdcMem
, hbmpOld
);
171 DeleteObject(hbmpCheck
);
175 // now we draw the smaller rectangle
180 // draw hollow gray rectangle
181 (void)SelectObject(hdc
, hpenGray
);
182 HBRUSH hbrPrev
= (HBRUSH
)SelectObject(hdc
, GetStockObject(NULL_BRUSH
));
183 Rectangle(hdc
, x
, y
, x
+ nCheckWidth
, y
+ nCheckHeight
);
186 (void)SelectObject(hdc
, hpenPrev
);
187 (void)SelectObject(hdc
, hbrPrev
);
189 DeleteObject(hpenBack
);
190 DeleteObject(hpenGray
);
193 dc.DrawRectangle(x, y, nCheckWidth, nCheckHeight);
196 dc.DrawLine(x, y, x + nCheckWidth, y + nCheckHeight);
197 dc.DrawLine(x, y + nCheckHeight, x + nCheckWidth, y);
207 // change the state of the item and redraw it
208 void wxCheckListBoxItem::Check(bool check
)
212 // index may be changed because new items were added/deleted
213 if ( m_pParent
->GetItemIndex(this) != (int)m_nIndex
)
216 int index
= m_pParent
->GetItemIndex(this);
218 wxASSERT_MSG( index
!= wxNOT_FOUND
, wxT("what does this item do here?") );
220 m_nIndex
= (size_t)index
;
223 HWND hwndListbox
= (HWND
)m_pParent
->GetHWND();
228 if ( ::SendMessage(hwndListbox
, LB_GETITEMRECT
,
229 m_nIndex
, (LPARAM
)&rcUpdate
) == LB_ERR
)
231 wxLogDebug(wxT("LB_GETITEMRECT failed"));
234 // FIXME this doesn't work if the listbox is scrolled!
235 size_t nHeight
= m_pParent
->GetItemHeight();
236 size_t y
= m_nIndex
* nHeight
;
240 rcUpdate
.right
= GetDefaultMarginWidth() ;
241 rcUpdate
.bottom
= y
+ nHeight
;
244 InvalidateRect(hwndListbox
, &rcUpdate
, FALSE
);
247 // send an "item checked" event
248 void wxCheckListBoxItem::SendEvent()
250 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, m_pParent
->GetId());
251 event
.SetInt(m_nIndex
);
252 event
.SetEventObject(m_pParent
);
253 m_pParent
->ProcessCommand(event
);
256 // ----------------------------------------------------------------------------
257 // implementation of wxCheckListBox class
258 // ----------------------------------------------------------------------------
260 // define event table
261 // ------------------
262 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
263 EVT_KEY_DOWN(wxCheckListBox::OnKeyDown
)
264 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick
)
270 // def ctor: use Create() to really create the control
271 wxCheckListBox::wxCheckListBox()
275 // ctor which creates the associated control
276 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
277 const wxPoint
& pos
, const wxSize
& size
,
278 int nStrings
, const wxString choices
[],
279 long style
, const wxValidator
& val
,
280 const wxString
& name
)
282 Create(parent
, id
, pos
, size
, nStrings
, choices
, style
, val
, name
);
285 bool wxCheckListBox::Create(wxWindow
*parent
, wxWindowID id
,
286 const wxPoint
& pos
, const wxSize
& size
,
287 int n
, const wxString choices
[],
289 const wxValidator
& validator
, const wxString
& name
)
291 return wxListBox::Create(parent
, id
, pos
, size
, n
, choices
,
292 style
| wxLB_OWNERDRAW
, validator
, name
);
295 // misc overloaded methods
296 // -----------------------
298 void wxCheckListBox::Delete(int N
)
300 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
301 wxT("invalid index in wxListBox::Delete") );
303 wxListBox::Delete(N
);
308 m_aItems
.RemoveAt(N
);
311 bool wxCheckListBox::SetFont( const wxFont
&font
)
314 for ( i
= 0; i
< m_aItems
.GetCount(); i
++ )
315 m_aItems
[i
]->SetFont(font
);
317 wxListBox::SetFont(font
);
322 // create/retrieve item
323 // --------------------
325 // create a check list box item
326 wxOwnerDrawn
*wxCheckListBox::CreateLboxItem(size_t nIndex
)
328 wxCheckListBoxItem
*pItem
= new wxCheckListBoxItem(this, nIndex
);
334 bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT
*item
)
336 if ( wxListBox::MSWOnMeasure(item
) ) {
337 MEASUREITEMSTRUCT
*pStruct
= (MEASUREITEMSTRUCT
*)item
;
340 m_nItemHeight
= pStruct
->itemHeight
;
342 // add place for the check mark
343 pStruct
->itemWidth
+= wxOwnerDrawn::GetDefaultMarginWidth();
354 bool wxCheckListBox::IsChecked(size_t uiIndex
) const
356 wxCHECK_MSG( uiIndex
< (size_t)GetCount(), FALSE
, _T("bad wxCheckListBox index") );
358 return GetItem(uiIndex
)->IsChecked();
361 void wxCheckListBox::Check(size_t uiIndex
, bool bCheck
)
363 wxCHECK_RET( uiIndex
< (size_t)GetCount(), _T("bad wxCheckListBox index") );
365 GetItem(uiIndex
)->Check(bCheck
);
371 void wxCheckListBox::OnKeyDown(wxKeyEvent
& event
)
382 switch ( event
.GetKeyCode() )
393 case WXK_NUMPAD_SUBTRACT
:
404 wxArrayInt selections
;
406 if ( HasMultipleSelection() )
408 count
= GetSelections(selections
);
412 int sel
= GetSelection();
420 for ( int i
= 0; i
< count
; i
++ )
422 wxCheckListBoxItem
*item
= GetItem(selections
[i
]);
425 wxFAIL_MSG( _T("no wxCheckListBoxItem?") );
437 item
->Check( oper
== Set
);
441 wxFAIL_MSG( _T("what should this key do?") );
444 // we should send an event as this has been done by the user and
445 // not by the program
449 else // nothing to do
455 void wxCheckListBox::OnLeftClick(wxMouseEvent
& event
)
457 // clicking on the item selects it, clicking on the checkmark toggles
458 if ( event
.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) {
459 int nItem
= HitTest(event
.GetX(), event
.GetY());
461 if ( nItem
!= wxNOT_FOUND
) {
462 wxCheckListBoxItem
*item
= GetItem(nItem
);
466 //else: it's not an error, just click outside of client zone
469 // implement default behaviour: clicking on the item selects it
474 int wxCheckListBox::DoHitTestItem(wxCoord x
, wxCoord y
) const
477 int nItem
= (int)::SendMessage
485 // FIXME this doesn't work when the listbox is scrolled!
486 int nItem
= y
/ m_nItemHeight
;
489 return nItem
>= m_noItems
? wxNOT_FOUND
: nItem
;