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
);
84 // simple accessors and operations
85 bool IsChecked() const { return m_bChecked
; }
87 void Check(bool bCheck
);
88 void Toggle() { Check(!IsChecked()); }
94 wxCheckListBox
*m_pParent
;
98 wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox
*pParent
, size_t nIndex
)
99 : wxOwnerDrawn(wxEmptyString
, TRUE
) // checkable
105 // we don't initialize m_nCheckHeight/Width vars because it's
106 // done in OnMeasure while they are used only in OnDraw and we
107 // know that there will always be OnMeasure before OnDraw
110 SetMarginWidth(GetDefaultMarginWidth());
114 * JACS - I've got the owner-draw stuff partially working with WIN16,
115 * with a really horrible-looking cross for wxCheckListBox instead of a
116 * check - could use a bitmap check-mark instead, defined in wx.rc.
117 * Also there's a refresh problem whereby it doesn't always draw the
118 * check until you click to the right of it, which is OK for WIN32.
121 bool wxCheckListBoxItem::OnDrawItem(wxDC
& dc
, const wxRect
& rc
,
122 wxODAction act
, wxODStatus stat
)
125 stat
= (wxOwnerDrawn::wxODStatus
)(stat
| wxOwnerDrawn::wxODChecked
);
127 if ( wxOwnerDrawn::OnDrawItem(dc
, rc
, act
, stat
) ) {
128 // ## using native API for performance and precision
129 size_t nCheckWidth
= GetDefaultMarginWidth(),
130 nCheckHeight
= m_pParent
->GetItemHeight();
135 HDC hdc
= (HDC
)dc
.GetHDC();
138 HPEN hpenBack
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_WINDOW
)),
139 hpenGray
= CreatePen(PS_SOLID
, 0, RGB(128, 128, 128)),
140 hpenPrev
= (HPEN
)SelectObject(hdc
, hpenBack
);
142 // we erase the 1-pixel border
143 Rectangle(hdc
, x
, y
, x
+ nCheckWidth
, y
+ nCheckHeight
);
145 // shift check mark 1 pixel to the right (it looks better like this)
149 // first create a monochrome bitmap in a memory DC
150 HDC hdcMem
= CreateCompatibleDC(hdc
);
151 HBITMAP hbmpCheck
= CreateBitmap(nCheckWidth
, nCheckHeight
, 1, 1, 0);
152 HBITMAP hbmpOld
= (HBITMAP
)SelectObject(hdcMem
, hbmpCheck
);
154 // then draw a check mark into it
155 #if defined(__WIN32__) && !defined(__SC__)
159 rect
.right
= nCheckWidth
;
160 rect
.bottom
= nCheckHeight
;
162 DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
164 // In WIN16, draw a cross
165 HPEN blackPen
= CreatePen(PS_SOLID
, 1, RGB(0, 0, 0));
166 HPEN whiteBrush
= (HPEN
)GetStockObject(WHITE_BRUSH
);
167 HPEN hPenOld
= (HPEN
)::SelectObject(hdcMem
, blackPen
);
168 HPEN hBrushOld
= (HPEN
)::SelectObject(hdcMem
, whiteBrush
);
169 ::SetROP2(hdcMem
, R2_COPYPEN
);
170 Rectangle(hdcMem
, 0, 0, nCheckWidth
, nCheckHeight
);
171 MoveTo(hdcMem
, 0, 0);
172 LineTo(hdcMem
, nCheckWidth
, nCheckHeight
);
173 MoveTo(hdcMem
, nCheckWidth
, 0);
174 LineTo(hdcMem
, 0, nCheckHeight
);
175 ::SelectObject(hdcMem
, hPenOld
);
176 ::SelectObject(hdcMem
, hBrushOld
);
177 ::DeleteObject(blackPen
);
180 // finally copy it to screen DC and clean up
181 BitBlt(hdc
, x
, y
, nCheckWidth
- 1, nCheckHeight
,
182 hdcMem
, 0, 0, SRCCOPY
);
184 SelectObject(hdcMem
, hbmpOld
);
185 DeleteObject(hbmpCheck
);
189 // now we draw the smaller rectangle
194 // draw hollow gray rectangle
195 (void)SelectObject(hdc
, hpenGray
);
196 HBRUSH hbrPrev
= (HBRUSH
)SelectObject(hdc
, GetStockObject(NULL_BRUSH
));
197 Rectangle(hdc
, x
, y
, x
+ nCheckWidth
, y
+ nCheckHeight
);
200 (void)SelectObject(hdc
, hpenPrev
);
201 (void)SelectObject(hdc
, hbrPrev
);
203 DeleteObject(hpenBack
);
204 DeleteObject(hpenGray
);
207 dc.DrawRectangle(x, y, nCheckWidth, nCheckHeight);
210 dc.DrawLine(x, y, x + nCheckWidth, y + nCheckHeight);
211 dc.DrawLine(x, y + nCheckHeight, x + nCheckWidth, y);
221 // change the state of the item and redraw it
222 void wxCheckListBoxItem::Check(bool check
)
226 // index may be changed because new items were added/deleted
227 if ( m_pParent
->GetItemIndex(this) != (int)m_nIndex
)
230 int index
= m_pParent
->GetItemIndex(this);
232 wxASSERT_MSG( index
!= wxNOT_FOUND
, wxT("what does this item do here?") );
234 m_nIndex
= (size_t)index
;
237 HWND hwndListbox
= (HWND
)m_pParent
->GetHWND();
242 if ( ::SendMessage(hwndListbox
, LB_GETITEMRECT
,
243 m_nIndex
, (LPARAM
)&rcUpdate
) == LB_ERR
)
245 wxLogDebug(wxT("LB_GETITEMRECT failed"));
248 // FIXME this doesn't work if the listbox is scrolled!
249 size_t nHeight
= m_pParent
->GetItemHeight();
250 size_t y
= m_nIndex
* nHeight
;
254 rcUpdate
.right
= GetDefaultMarginWidth() ;
255 rcUpdate
.bottom
= y
+ nHeight
;
258 InvalidateRect(hwndListbox
, &rcUpdate
, FALSE
);
261 // send an "item checked" event
262 void wxCheckListBoxItem::SendEvent()
264 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, m_pParent
->GetId());
265 event
.SetInt(m_nIndex
);
266 event
.SetEventObject(m_pParent
);
267 m_pParent
->ProcessCommand(event
);
270 // ----------------------------------------------------------------------------
271 // implementation of wxCheckListBox class
272 // ----------------------------------------------------------------------------
274 // define event table
275 // ------------------
276 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
277 EVT_KEY_DOWN(wxCheckListBox::OnKeyDown
)
278 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick
)
284 // def ctor: use Create() to really create the control
285 wxCheckListBox::wxCheckListBox()
289 // ctor which creates the associated control
290 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
291 const wxPoint
& pos
, const wxSize
& size
,
292 int nStrings
, const wxString choices
[],
293 long style
, const wxValidator
& val
,
294 const wxString
& name
)
296 Create(parent
, id
, pos
, size
, nStrings
, choices
, style
, val
, name
);
299 bool wxCheckListBox::Create(wxWindow
*parent
, wxWindowID id
,
300 const wxPoint
& pos
, const wxSize
& size
,
301 int n
, const wxString choices
[],
303 const wxValidator
& validator
, const wxString
& name
)
305 return wxListBox::Create(parent
, id
, pos
, size
, n
, choices
,
306 style
| wxLB_OWNERDRAW
, validator
, name
);
309 // misc overloaded methods
310 // -----------------------
312 void wxCheckListBox::Delete(int N
)
314 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
315 wxT("invalid index in wxListBox::Delete") );
317 wxListBox::Delete(N
);
322 m_aItems
.RemoveAt(N
);
325 bool wxCheckListBox::SetFont( const wxFont
&font
)
328 for ( i
= 0; i
< m_aItems
.GetCount(); i
++ )
329 m_aItems
[i
]->SetFont(font
);
331 wxListBox::SetFont(font
);
336 // create/retrieve item
337 // --------------------
339 // create a check list box item
340 wxOwnerDrawn
*wxCheckListBox::CreateLboxItem(size_t nIndex
)
342 wxCheckListBoxItem
*pItem
= new wxCheckListBoxItem(this, nIndex
);
348 bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT
*item
)
350 if ( wxListBox::MSWOnMeasure(item
) ) {
351 MEASUREITEMSTRUCT
*pStruct
= (MEASUREITEMSTRUCT
*)item
;
354 m_nItemHeight
= pStruct
->itemHeight
;
356 // add place for the check mark
357 pStruct
->itemWidth
+= wxOwnerDrawn::GetDefaultMarginWidth();
368 bool wxCheckListBox::IsChecked(size_t uiIndex
) const
370 wxCHECK_MSG( uiIndex
< (size_t)GetCount(), FALSE
, _T("bad wxCheckListBox index") );
372 return GetItem(uiIndex
)->IsChecked();
375 void wxCheckListBox::Check(size_t uiIndex
, bool bCheck
)
377 wxCHECK_RET( uiIndex
< (size_t)GetCount(), _T("bad wxCheckListBox index") );
379 GetItem(uiIndex
)->Check(bCheck
);
385 void wxCheckListBox::OnKeyDown(wxKeyEvent
& event
)
396 switch ( event
.KeyCode() )
407 case WXK_NUMPAD_SUBTRACT
:
418 wxArrayInt selections
;
420 if ( HasMultipleSelection() )
422 count
= GetSelections(selections
);
427 selections
.Add(GetSelection());
430 for ( int i
= 0; i
< count
; i
++ )
432 wxCheckListBoxItem
*item
= GetItem(selections
[i
]);
435 wxFAIL_MSG( _T("no wxCheckListBoxItem?") );
447 item
->Check( oper
== Set
);
451 wxFAIL_MSG( _T("what should this key do?") );
454 // we should send an event as this has been done by the user and
455 // not by the program
459 else // nothing to do
465 void wxCheckListBox::OnLeftClick(wxMouseEvent
& event
)
467 // clicking on the item selects it, clicking on the checkmark toggles
468 if ( event
.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) {
469 int nItem
= HitTest(event
.GetX(), event
.GetY());
471 if ( nItem
!= wxNOT_FOUND
) {
472 wxCheckListBoxItem
*item
= GetItem(nItem
);
476 //else: it's not an error, just click outside of client zone
479 // implement default behaviour: clicking on the item selects it
484 int wxCheckListBox::DoHitTestItem(wxCoord x
, wxCoord y
) const
487 int nItem
= (int)::SendMessage
495 // FIXME this doesn't work when the listbox is scrolled!
496 int nItem
= y
/ m_nItemHeight
;
499 return nItem
>= m_noItems
? wxNOT_FOUND
: nItem
;