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 license
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
);
85 bool IsChecked() const { return m_bChecked
; }
86 void Check(bool bCheck
);
87 void Toggle() { Check(!IsChecked()); }
91 wxCheckListBox
*m_pParent
;
95 wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox
*pParent
, size_t nIndex
)
96 : wxOwnerDrawn(wxEmptyString
, TRUE
) // checkable
102 // we don't initialize m_nCheckHeight/Width vars because it's
103 // done in OnMeasure while they are used only in OnDraw and we
104 // know that there will always be OnMeasure before OnDraw
107 SetMarginWidth(GetDefaultMarginWidth());
111 * JACS - I've got the owner-draw stuff partially working with WIN16,
112 * with a really horrible-looking cross for wxCheckListBox instead of a
113 * check - could use a bitmap check-mark instead, defined in wx.rc.
114 * Also there's a refresh problem whereby it doesn't always draw the
115 * check until you click to the right of it, which is OK for WIN32.
118 bool wxCheckListBoxItem::OnDrawItem(wxDC
& dc
, const wxRect
& rc
,
119 wxODAction act
, wxODStatus stat
)
122 stat
= (wxOwnerDrawn::wxODStatus
)(stat
| wxOwnerDrawn::wxODChecked
);
124 if ( wxOwnerDrawn::OnDrawItem(dc
, rc
, act
, stat
) ) {
125 // ## using native API for performance and precision
126 size_t nCheckWidth
= GetDefaultMarginWidth(),
127 nCheckHeight
= m_pParent
->GetItemHeight();
132 HDC hdc
= (HDC
)dc
.GetHDC();
135 HPEN hpenBack
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_WINDOW
)),
136 hpenGray
= CreatePen(PS_SOLID
, 0, RGB(128, 128, 128)),
137 hpenPrev
= (HPEN
)SelectObject(hdc
, hpenBack
);
139 // we erase the 1-pixel border
140 Rectangle(hdc
, x
, y
, x
+ nCheckWidth
, y
+ nCheckHeight
);
142 // shift check mark 1 pixel to the right (it looks better like this)
146 // first create a monochrome bitmap in a memory DC
147 HDC hdcMem
= CreateCompatibleDC(hdc
);
148 HBITMAP hbmpCheck
= CreateBitmap(nCheckWidth
, nCheckHeight
, 1, 1, 0);
149 HBITMAP hbmpOld
= (HBITMAP
)SelectObject(hdcMem
, hbmpCheck
);
151 // then draw a check mark into it
152 #if defined(__WIN32__) && !defined(__SC__)
156 rect
.right
= nCheckWidth
;
157 rect
.bottom
= nCheckHeight
;
159 DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
161 // In WIN16, draw a cross
162 HPEN blackPen
= CreatePen(PS_SOLID
, 1, RGB(0, 0, 0));
163 HPEN whiteBrush
= (HPEN
)GetStockObject(WHITE_BRUSH
);
164 HPEN hPenOld
= (HPEN
)::SelectObject(hdcMem
, blackPen
);
165 HPEN hBrushOld
= (HPEN
)::SelectObject(hdcMem
, whiteBrush
);
166 ::SetROP2(hdcMem
, R2_COPYPEN
);
167 Rectangle(hdcMem
, 0, 0, nCheckWidth
, nCheckHeight
);
168 MoveTo(hdcMem
, 0, 0);
169 LineTo(hdcMem
, nCheckWidth
, nCheckHeight
);
170 MoveTo(hdcMem
, nCheckWidth
, 0);
171 LineTo(hdcMem
, 0, nCheckHeight
);
172 ::SelectObject(hdcMem
, hPenOld
);
173 ::SelectObject(hdcMem
, hBrushOld
);
174 ::DeleteObject(blackPen
);
177 // finally copy it to screen DC and clean up
178 BitBlt(hdc
, x
, y
, nCheckWidth
- 1, nCheckHeight
,
179 hdcMem
, 0, 0, SRCCOPY
);
181 SelectObject(hdcMem
, hbmpOld
);
182 DeleteObject(hbmpCheck
);
186 // now we draw the smaller rectangle
191 // draw hollow gray rectangle
192 (void)SelectObject(hdc
, hpenGray
);
193 HBRUSH hbrPrev
= (HBRUSH
)SelectObject(hdc
, GetStockObject(NULL_BRUSH
));
194 Rectangle(hdc
, x
, y
, x
+ nCheckWidth
, y
+ nCheckHeight
);
197 (void)SelectObject(hdc
, hpenPrev
);
198 (void)SelectObject(hdc
, hbrPrev
);
200 DeleteObject(hpenBack
);
201 DeleteObject(hpenGray
);
204 dc.DrawRectangle(x, y, nCheckWidth, nCheckHeight);
207 dc.DrawLine(x, y, x + nCheckWidth, y + nCheckHeight);
208 dc.DrawLine(x, y + nCheckHeight, x + nCheckWidth, y);
218 // change the state of the item and redraw it
219 void wxCheckListBoxItem::Check(bool check
)
223 // index may be chanegd because new items were added/deleted
224 if ( m_pParent
->GetItemIndex(this) != (int)m_nIndex
)
227 int index
= m_pParent
->GetItemIndex(this);
229 wxASSERT_MSG( index
!= wxNOT_FOUND
, wxT("what does this item do here?") );
231 m_nIndex
= (size_t)index
;
234 HWND hwndListbox
= (HWND
)m_pParent
->GetHWND();
239 if ( ::SendMessage(hwndListbox
, LB_GETITEMRECT
,
240 m_nIndex
, (LPARAM
)&rcUpdate
) == LB_ERR
)
242 wxLogDebug(wxT("LB_GETITEMRECT failed"));
245 // FIXME this doesn't work if the listbox is scrolled!
246 size_t nHeight
= m_pParent
->GetItemHeight();
247 size_t y
= m_nIndex
* nHeight
;
251 rcUpdate
.right
= GetDefaultMarginWidth() ;
252 rcUpdate
.bottom
= y
+ nHeight
;
255 InvalidateRect(hwndListbox
, &rcUpdate
, FALSE
);
257 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, m_pParent
->GetId());
258 event
.SetInt(m_nIndex
);
259 event
.SetEventObject(m_pParent
);
260 m_pParent
->ProcessCommand(event
);
263 // ----------------------------------------------------------------------------
264 // implementation of wxCheckListBox class
265 // ----------------------------------------------------------------------------
267 // define event table
268 // ------------------
269 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
270 EVT_KEY_DOWN(wxCheckListBox::OnKeyDown
)
271 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick
)
277 // def ctor: use Create() to really create the control
278 wxCheckListBox::wxCheckListBox()
282 // ctor which creates the associated control
283 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
284 const wxPoint
& pos
, const wxSize
& size
,
285 int nStrings
, const wxString choices
[],
286 long style
, const wxValidator
& val
,
287 const wxString
& name
)
289 Create(parent
, id
, pos
, size
, nStrings
, choices
, style
, val
, name
);
292 bool wxCheckListBox::Create(wxWindow
*parent
, wxWindowID id
,
293 const wxPoint
& pos
, const wxSize
& size
,
294 int n
, const wxString choices
[],
296 const wxValidator
& validator
, const wxString
& name
)
298 return wxListBox::Create(parent
, id
, pos
, size
, n
, choices
,
299 style
| wxLB_OWNERDRAW
, validator
, name
);
304 void wxCheckListBox::Delete(int N
)
306 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
307 wxT("invalid index in wxListBox::Delete") );
309 wxListBox::Delete(N
);
314 m_aItems
.RemoveAt(N
);
317 bool wxCheckListBox::SetFont( const wxFont
&font
)
320 for ( i
= 0; i
< m_aItems
.GetCount(); i
++ )
321 m_aItems
[i
]->SetFont(font
);
323 wxListBox::SetFont(font
);
328 // create/retrieve item
329 // --------------------
331 // create a check list box item
332 wxOwnerDrawn
*wxCheckListBox::CreateLboxItem(size_t nIndex
)
334 wxCheckListBoxItem
*pItem
= new wxCheckListBoxItem(this, nIndex
);
340 bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT
*item
)
342 if ( wxListBox::MSWOnMeasure(item
) ) {
343 MEASUREITEMSTRUCT
*pStruct
= (MEASUREITEMSTRUCT
*)item
;
346 m_nItemHeight
= pStruct
->itemHeight
;
348 // add place for the check mark
349 pStruct
->itemWidth
+= wxOwnerDrawn::GetDefaultMarginWidth();
360 bool wxCheckListBox::IsChecked(size_t uiIndex
) const
362 wxCHECK_MSG( uiIndex
< (size_t)GetCount(), FALSE
, _T("bad wxCheckListBox index") );
364 return GetItem(uiIndex
)->IsChecked();
367 void wxCheckListBox::Check(size_t uiIndex
, bool bCheck
)
369 wxCHECK_RET( uiIndex
< (size_t)GetCount(), _T("bad wxCheckListBox index") );
371 GetItem(uiIndex
)->Check(bCheck
);
377 void wxCheckListBox::OnKeyDown(wxKeyEvent
& event
)
388 switch ( event
.KeyCode() )
399 case WXK_NUMPAD_SUBTRACT
:
410 wxArrayInt selections
;
412 if ( HasMultipleSelection() )
414 count
= GetSelections(selections
);
419 selections
.Add(GetSelection());
422 for ( int i
= 0; i
< count
; i
++ )
424 wxCheckListBoxItem
*item
= GetItem(selections
[i
]);
427 wxFAIL_MSG( _T("no wxCheckListBoxItem?") );
439 item
->Check( oper
== Set
);
443 wxFAIL_MSG( _T("what should this key do?") );
447 else // nothing to do
453 void wxCheckListBox::OnLeftClick(wxMouseEvent
& event
)
455 // clicking on the item selects it, clicking on the checkmark toggles
456 if ( event
.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) {
457 int nItem
= HitTest(event
.GetX(), event
.GetY());
459 if ( nItem
!= wxNOT_FOUND
)
460 GetItem(nItem
)->Toggle();
461 //else: it's not an error, just click outside of client zone
464 // implement default behaviour: clicking on the item selects it
469 int wxCheckListBox::DoHitTestItem(wxCoord x
, wxCoord y
) const
472 int nItem
= (int)::SendMessage
480 // FIXME this doesn't work when the listbox is scrolled!
481 int nItem
= y
/ m_nItemHeight
;
484 return nItem
>= m_noItems
? wxNOT_FOUND
: nItem
;