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 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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"
50 #include "wx/msw/wrapwin.h"
53 #include "wx/msw/private.h"
55 #if defined(__GNUWIN32_OLD__)
56 #include "wx/msw/gnuwin32/extra.h"
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 // get item (converted to right type)
64 #define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n)))
66 // ============================================================================
68 // ============================================================================
70 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
)
77 // ----------------------------------------------------------------------------
78 // declaration and implementation of wxCheckListBoxItem class
79 // ----------------------------------------------------------------------------
81 class wxCheckListBoxItem
: public wxOwnerDrawn
83 friend class WXDLLEXPORT wxCheckListBox
;
86 wxCheckListBoxItem(wxCheckListBox
*pParent
, size_t nIndex
);
89 virtual bool OnDrawItem(wxDC
& dc
, const wxRect
& rc
, wxODAction act
, wxODStatus stat
);
91 // simple accessors and operations
92 bool IsChecked() const { return m_bChecked
; }
94 void Check(bool bCheck
);
95 void Toggle() { Check(!IsChecked()); }
101 DECLARE_NO_COPY_CLASS(wxCheckListBoxItem
)
103 wxCheckListBox
*m_pParent
;
107 wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox
*pParent
, size_t nIndex
)
108 : wxOwnerDrawn(wxEmptyString
, TRUE
) // checkable
114 // we don't initialize m_nCheckHeight/Width vars because it's
115 // done in OnMeasure while they are used only in OnDraw and we
116 // know that there will always be OnMeasure before OnDraw
119 SetMarginWidth(GetDefaultMarginWidth());
123 * JACS - I've got the owner-draw stuff partially working with WIN16,
124 * with a really horrible-looking cross for wxCheckListBox instead of a
125 * check - could use a bitmap check-mark instead, defined in wx.rc.
126 * Also there's a refresh problem whereby it doesn't always draw the
127 * check until you click to the right of it, which is OK for WIN32.
130 bool wxCheckListBoxItem::OnDrawItem(wxDC
& dc
, const wxRect
& rc
,
131 wxODAction act
, wxODStatus stat
)
134 stat
= (wxOwnerDrawn::wxODStatus
)(stat
| wxOwnerDrawn::wxODChecked
);
136 if ( wxOwnerDrawn::OnDrawItem(dc
, rc
, act
, stat
) ) {
137 // ## using native API for performance and precision
138 size_t nCheckWidth
= GetDefaultMarginWidth(),
139 nCheckHeight
= m_pParent
->GetItemHeight();
144 HDC hdc
= (HDC
)dc
.GetHDC();
147 HPEN hpenBack
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_WINDOW
)),
148 hpenGray
= CreatePen(PS_SOLID
, 0, RGB(128, 128, 128)),
149 hpenPrev
= (HPEN
)SelectObject(hdc
, hpenBack
);
151 // we erase the 1-pixel border
152 Rectangle(hdc
, x
, y
, x
+ nCheckWidth
, y
+ nCheckHeight
);
154 // shift check mark 1 pixel to the right (it looks better like this)
158 // first create a monochrome bitmap in a memory DC
159 HDC hdcMem
= CreateCompatibleDC(hdc
);
160 HBITMAP hbmpCheck
= CreateBitmap(nCheckWidth
, nCheckHeight
, 1, 1, 0);
161 HBITMAP hbmpOld
= (HBITMAP
)SelectObject(hdcMem
, hbmpCheck
);
163 // then draw a check mark into it
168 rect
.right
= nCheckWidth
;
169 rect
.bottom
= nCheckHeight
;
172 DrawFrameControl(hdcMem
, &rect
, DFC_BUTTON
, DFCS_BUTTONCHECK
);
174 DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
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 changed 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
);
258 // send an "item checked" event
259 void wxCheckListBoxItem::SendEvent()
261 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, m_pParent
->GetId());
262 event
.SetInt(m_nIndex
);
263 event
.SetEventObject(m_pParent
);
264 m_pParent
->ProcessCommand(event
);
267 // ----------------------------------------------------------------------------
268 // implementation of wxCheckListBox class
269 // ----------------------------------------------------------------------------
271 // define event table
272 // ------------------
273 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
274 EVT_KEY_DOWN(wxCheckListBox::OnKeyDown
)
275 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick
)
281 // def ctor: use Create() to really create the control
282 wxCheckListBox::wxCheckListBox()
286 // ctor which creates the associated control
287 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
288 const wxPoint
& pos
, const wxSize
& size
,
289 int nStrings
, const wxString choices
[],
290 long style
, const wxValidator
& val
,
291 const wxString
& name
)
293 Create(parent
, id
, pos
, size
, nStrings
, choices
, style
, val
, name
);
296 bool wxCheckListBox::Create(wxWindow
*parent
, wxWindowID id
,
297 const wxPoint
& pos
, const wxSize
& size
,
298 int n
, const wxString choices
[],
300 const wxValidator
& validator
, const wxString
& name
)
302 return wxListBox::Create(parent
, id
, pos
, size
, n
, choices
,
303 style
| wxLB_OWNERDRAW
, validator
, name
);
306 // misc overloaded methods
307 // -----------------------
309 void wxCheckListBox::Delete(int N
)
311 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
312 wxT("invalid index in wxListBox::Delete") );
314 wxListBox::Delete(N
);
319 m_aItems
.RemoveAt(N
);
322 bool wxCheckListBox::SetFont( const wxFont
&font
)
325 for ( i
= 0; i
< m_aItems
.GetCount(); i
++ )
326 m_aItems
[i
]->SetFont(font
);
328 wxListBox::SetFont(font
);
333 // create/retrieve item
334 // --------------------
336 // create a check list box item
337 wxOwnerDrawn
*wxCheckListBox::CreateLboxItem(size_t nIndex
)
339 wxCheckListBoxItem
*pItem
= new wxCheckListBoxItem(this, nIndex
);
345 bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT
*item
)
347 if ( wxListBox::MSWOnMeasure(item
) ) {
348 MEASUREITEMSTRUCT
*pStruct
= (MEASUREITEMSTRUCT
*)item
;
351 m_nItemHeight
= pStruct
->itemHeight
;
353 // add place for the check mark
354 pStruct
->itemWidth
+= wxOwnerDrawn::GetDefaultMarginWidth();
365 bool wxCheckListBox::IsChecked(size_t uiIndex
) const
367 wxCHECK_MSG( uiIndex
< (size_t)GetCount(), FALSE
, _T("bad wxCheckListBox index") );
369 return GetItem(uiIndex
)->IsChecked();
372 void wxCheckListBox::Check(size_t uiIndex
, bool bCheck
)
374 wxCHECK_RET( uiIndex
< (size_t)GetCount(), _T("bad wxCheckListBox index") );
376 GetItem(uiIndex
)->Check(bCheck
);
382 void wxCheckListBox::OnKeyDown(wxKeyEvent
& event
)
393 switch ( event
.GetKeyCode() )
404 case WXK_NUMPAD_SUBTRACT
:
415 wxArrayInt selections
;
417 if ( HasMultipleSelection() )
419 count
= GetSelections(selections
);
423 int sel
= GetSelection();
431 for ( int i
= 0; i
< count
; i
++ )
433 wxCheckListBoxItem
*item
= GetItem(selections
[i
]);
436 wxFAIL_MSG( _T("no wxCheckListBoxItem?") );
448 item
->Check( oper
== Set
);
452 wxFAIL_MSG( _T("what should this key do?") );
455 // we should send an event as this has been done by the user and
456 // not by the program
460 else // nothing to do
466 void wxCheckListBox::OnLeftClick(wxMouseEvent
& event
)
468 // clicking on the item selects it, clicking on the checkmark toggles
469 if ( event
.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) {
470 int nItem
= HitTest(event
.GetX(), event
.GetY());
472 if ( nItem
!= wxNOT_FOUND
) {
473 wxCheckListBoxItem
*item
= GetItem(nItem
);
477 //else: it's not an error, just click outside of client zone
480 // implement default behaviour: clicking on the item selects it
485 int wxCheckListBox::DoHitTestItem(wxCoord x
, wxCoord y
) const
488 int nItem
= (int)::SendMessage
496 // FIXME this doesn't work when the listbox is scrolled!
497 int nItem
= y
/ m_nItemHeight
;
500 return nItem
>= m_noItems
? wxNOT_FOUND
: nItem
;