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"
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
)
72 // ----------------------------------------------------------------------------
73 // declaration and implementation of wxCheckListBoxItem class
74 // ----------------------------------------------------------------------------
76 class wxCheckListBoxItem
: public wxOwnerDrawn
78 friend class WXDLLEXPORT wxCheckListBox
;
81 wxCheckListBoxItem(wxCheckListBox
*pParent
, size_t nIndex
);
84 virtual bool OnDrawItem(wxDC
& dc
, const wxRect
& rc
, wxODAction act
, wxODStatus stat
);
86 // simple accessors and operations
87 bool IsChecked() const { return m_bChecked
; }
89 void Check(bool bCheck
);
90 void Toggle() { Check(!IsChecked()); }
96 DECLARE_NO_COPY_CLASS(wxCheckListBoxItem
)
98 wxCheckListBox
*m_pParent
;
102 wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox
*pParent
, size_t nIndex
)
103 : wxOwnerDrawn(wxEmptyString
, TRUE
) // checkable
109 // we don't initialize m_nCheckHeight/Width vars because it's
110 // done in OnMeasure while they are used only in OnDraw and we
111 // know that there will always be OnMeasure before OnDraw
114 SetMarginWidth(GetDefaultMarginWidth());
118 * JACS - I've got the owner-draw stuff partially working with WIN16,
119 * with a really horrible-looking cross for wxCheckListBox instead of a
120 * check - could use a bitmap check-mark instead, defined in wx.rc.
121 * Also there's a refresh problem whereby it doesn't always draw the
122 * check until you click to the right of it, which is OK for WIN32.
125 bool wxCheckListBoxItem::OnDrawItem(wxDC
& dc
, const wxRect
& rc
,
126 wxODAction act
, wxODStatus stat
)
129 stat
= (wxOwnerDrawn::wxODStatus
)(stat
| wxOwnerDrawn::wxODChecked
);
131 if ( wxOwnerDrawn::OnDrawItem(dc
, rc
, act
, stat
) ) {
132 // ## using native API for performance and precision
133 size_t nCheckWidth
= GetDefaultMarginWidth(),
134 nCheckHeight
= m_pParent
->GetItemHeight();
139 HDC hdc
= (HDC
)dc
.GetHDC();
142 HPEN hpenBack
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_WINDOW
)),
143 hpenGray
= CreatePen(PS_SOLID
, 0, RGB(128, 128, 128)),
144 hpenPrev
= (HPEN
)SelectObject(hdc
, hpenBack
);
146 // we erase the 1-pixel border
147 Rectangle(hdc
, x
, y
, x
+ nCheckWidth
, y
+ nCheckHeight
);
149 // shift check mark 1 pixel to the right (it looks better like this)
153 // first create a monochrome bitmap in a memory DC
154 HDC hdcMem
= CreateCompatibleDC(hdc
);
155 HBITMAP hbmpCheck
= CreateBitmap(nCheckWidth
, nCheckHeight
, 1, 1, 0);
156 HBITMAP hbmpOld
= (HBITMAP
)SelectObject(hdcMem
, hbmpCheck
);
158 // then draw a check mark into it
163 rect
.right
= nCheckWidth
;
164 rect
.bottom
= nCheckHeight
;
167 DrawFrameControl(hdcMem
, &rect
, DFC_BUTTON
, DFCS_BUTTONCHECK
);
169 DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
172 // finally copy it to screen DC and clean up
173 BitBlt(hdc
, x
, y
, nCheckWidth
- 1, nCheckHeight
,
174 hdcMem
, 0, 0, SRCCOPY
);
176 SelectObject(hdcMem
, hbmpOld
);
177 DeleteObject(hbmpCheck
);
181 // now we draw the smaller rectangle
186 // draw hollow gray rectangle
187 (void)SelectObject(hdc
, hpenGray
);
188 HBRUSH hbrPrev
= (HBRUSH
)SelectObject(hdc
, GetStockObject(NULL_BRUSH
));
189 Rectangle(hdc
, x
, y
, x
+ nCheckWidth
, y
+ nCheckHeight
);
192 (void)SelectObject(hdc
, hpenPrev
);
193 (void)SelectObject(hdc
, hbrPrev
);
195 DeleteObject(hpenBack
);
196 DeleteObject(hpenGray
);
199 dc.DrawRectangle(x, y, nCheckWidth, nCheckHeight);
202 dc.DrawLine(x, y, x + nCheckWidth, y + nCheckHeight);
203 dc.DrawLine(x, y + nCheckHeight, x + nCheckWidth, y);
213 // change the state of the item and redraw it
214 void wxCheckListBoxItem::Check(bool check
)
218 // index may be changed because new items were added/deleted
219 if ( m_pParent
->GetItemIndex(this) != (int)m_nIndex
)
222 int index
= m_pParent
->GetItemIndex(this);
224 wxASSERT_MSG( index
!= wxNOT_FOUND
, wxT("what does this item do here?") );
226 m_nIndex
= (size_t)index
;
229 HWND hwndListbox
= (HWND
)m_pParent
->GetHWND();
234 if ( ::SendMessage(hwndListbox
, LB_GETITEMRECT
,
235 m_nIndex
, (LPARAM
)&rcUpdate
) == LB_ERR
)
237 wxLogDebug(wxT("LB_GETITEMRECT failed"));
240 // FIXME this doesn't work if the listbox is scrolled!
241 size_t nHeight
= m_pParent
->GetItemHeight();
242 size_t y
= m_nIndex
* nHeight
;
246 rcUpdate
.right
= GetDefaultMarginWidth() ;
247 rcUpdate
.bottom
= y
+ nHeight
;
250 InvalidateRect(hwndListbox
, &rcUpdate
, FALSE
);
253 // send an "item checked" event
254 void wxCheckListBoxItem::SendEvent()
256 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, m_pParent
->GetId());
257 event
.SetInt(m_nIndex
);
258 event
.SetEventObject(m_pParent
);
259 m_pParent
->ProcessCommand(event
);
262 // ----------------------------------------------------------------------------
263 // implementation of wxCheckListBox class
264 // ----------------------------------------------------------------------------
266 // define event table
267 // ------------------
268 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
269 EVT_KEY_DOWN(wxCheckListBox::OnKeyDown
)
270 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick
)
276 // def ctor: use Create() to really create the control
277 wxCheckListBox::wxCheckListBox()
281 // ctor which creates the associated control
282 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
283 const wxPoint
& pos
, const wxSize
& size
,
284 int nStrings
, const wxString choices
[],
285 long style
, const wxValidator
& val
,
286 const wxString
& name
)
288 Create(parent
, id
, pos
, size
, nStrings
, choices
, style
, val
, name
);
291 bool wxCheckListBox::Create(wxWindow
*parent
, wxWindowID id
,
292 const wxPoint
& pos
, const wxSize
& size
,
293 int n
, const wxString choices
[],
295 const wxValidator
& validator
, const wxString
& name
)
297 return wxListBox::Create(parent
, id
, pos
, size
, n
, choices
,
298 style
| wxLB_OWNERDRAW
, validator
, name
);
301 // misc overloaded methods
302 // -----------------------
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
.GetKeyCode() )
399 case WXK_NUMPAD_SUBTRACT
:
410 wxArrayInt selections
;
412 if ( HasMultipleSelection() )
414 count
= GetSelections(selections
);
418 int sel
= GetSelection();
426 for ( int i
= 0; i
< count
; i
++ )
428 wxCheckListBoxItem
*item
= GetItem(selections
[i
]);
431 wxFAIL_MSG( _T("no wxCheckListBoxItem?") );
443 item
->Check( oper
== Set
);
447 wxFAIL_MSG( _T("what should this key do?") );
450 // we should send an event as this has been done by the user and
451 // not by the program
455 else // nothing to do
461 void wxCheckListBox::OnLeftClick(wxMouseEvent
& event
)
463 // clicking on the item selects it, clicking on the checkmark toggles
464 if ( event
.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) {
465 int nItem
= HitTest(event
.GetX(), event
.GetY());
467 if ( nItem
!= wxNOT_FOUND
) {
468 wxCheckListBoxItem
*item
= GetItem(nItem
);
472 //else: it's not an error, just click outside of client zone
475 // implement default behaviour: clicking on the item selects it
480 int wxCheckListBox::DoHitTestItem(wxCoord x
, wxCoord y
) const
483 int nItem
= (int)::SendMessage
491 // FIXME this doesn't work when the listbox is scrolled!
492 int nItem
= y
/ m_nItemHeight
;
495 return nItem
>= m_noItems
? wxNOT_FOUND
: nItem
;