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"
33 #include "wx/object.h"
34 #include "wx/colour.h"
36 #include "wx/bitmap.h"
37 #include "wx/window.h"
38 #include "wx/listbox.h"
39 #include "wx/ownerdrw.h"
40 #include "wx/settings.h"
41 #include "wx/dcmemory.h"
42 #include "wx/msw/checklst.h"
48 #if defined(__GNUWIN32_OLD__)
49 #include "wx/msw/gnuwin32/extra.h"
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 // get item (converted to right type)
57 #define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n)))
59 // ============================================================================
61 // ============================================================================
63 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
)
65 // ----------------------------------------------------------------------------
66 // declaration and implementation of wxCheckListBoxItem class
67 // ----------------------------------------------------------------------------
69 class wxCheckListBoxItem
: public wxOwnerDrawn
71 friend class wxCheckListBox
;
74 wxCheckListBoxItem(wxCheckListBox
*pParent
, size_t nIndex
);
77 virtual bool OnDrawItem(wxDC
& dc
, const wxRect
& rc
, wxODAction act
, wxODStatus stat
);
80 bool IsChecked() const { return m_bChecked
; }
81 void Check(bool bCheck
);
82 void Toggle() { Check(!IsChecked()); }
86 wxCheckListBox
*m_pParent
;
90 wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox
*pParent
, size_t nIndex
)
91 : wxOwnerDrawn("", TRUE
) // checkable
97 // we don't initialize m_nCheckHeight/Width vars because it's
98 // done in OnMeasure while they are used only in OnDraw and we
99 // know that there will always be OnMeasure before OnDraw
102 SetMarginWidth(GetDefaultMarginWidth());
106 * JACS - I've got the owner-draw stuff partially working with WIN16,
107 * with a really horrible-looking cross for wxCheckListBox instead of a
108 * check - could use a bitmap check-mark instead, defined in wx.rc.
109 * Also there's a refresh problem whereby it doesn't always draw the
110 * check until you click to the right of it, which is OK for WIN32.
113 bool wxCheckListBoxItem::OnDrawItem(wxDC
& dc
, const wxRect
& rc
,
114 wxODAction act
, wxODStatus stat
)
117 stat
= (wxOwnerDrawn::wxODStatus
)(stat
| wxOwnerDrawn::wxODChecked
);
119 if ( wxOwnerDrawn::OnDrawItem(dc
, rc
, act
, stat
) ) {
120 // ## using native API for performance and precision
121 size_t nCheckWidth
= GetDefaultMarginWidth(),
122 nCheckHeight
= m_pParent
->GetItemHeight();
127 HDC hdc
= (HDC
)dc
.GetHDC();
130 HPEN hpenBack
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_WINDOW
)),
131 hpenGray
= CreatePen(PS_SOLID
, 0, RGB(128, 128, 128)),
132 hpenPrev
= (HPEN
)SelectObject(hdc
, hpenBack
);
134 // we erase the 1-pixel border
135 Rectangle(hdc
, x
, y
, x
+ nCheckWidth
, y
+ nCheckHeight
);
137 // shift check mark 1 pixel to the right (it looks better like this)
141 // first create a monochrome bitmap in a memory DC
142 HDC hdcMem
= CreateCompatibleDC(hdc
);
143 HBITMAP hbmpCheck
= CreateBitmap(nCheckWidth
, nCheckHeight
, 1, 1, 0);
144 HBITMAP hbmpOld
= (HBITMAP
)SelectObject(hdcMem
, hbmpCheck
);
146 // then draw a check mark into it
147 #if defined(__WIN32__) && !defined(__SC__)
151 rect
.right
= nCheckWidth
;
152 rect
.bottom
= nCheckHeight
;
154 DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
156 // In WIN16, draw a cross
157 HPEN blackPen
= CreatePen(PS_SOLID
, 1, RGB(0, 0, 0));
158 HPEN whiteBrush
= (HPEN
)GetStockObject(WHITE_BRUSH
);
159 HPEN hPenOld
= (HPEN
)::SelectObject(hdcMem
, blackPen
);
160 HPEN hBrushOld
= (HPEN
)::SelectObject(hdcMem
, whiteBrush
);
161 ::SetROP2(hdcMem
, R2_COPYPEN
);
162 Rectangle(hdcMem
, 0, 0, nCheckWidth
, nCheckHeight
);
163 MoveTo(hdcMem
, 0, 0);
164 LineTo(hdcMem
, nCheckWidth
, nCheckHeight
);
165 MoveTo(hdcMem
, nCheckWidth
, 0);
166 LineTo(hdcMem
, 0, nCheckHeight
);
167 ::SelectObject(hdcMem
, hPenOld
);
168 ::SelectObject(hdcMem
, hBrushOld
);
169 ::DeleteObject(blackPen
);
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 chanegd 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
);
252 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, m_pParent
->GetId());
253 event
.SetInt(m_nIndex
);
254 event
.SetEventObject(m_pParent
);
255 m_pParent
->ProcessCommand(event
);
258 // ----------------------------------------------------------------------------
259 // implementation of wxCheckListBox class
260 // ----------------------------------------------------------------------------
262 // define event table
263 // ------------------
264 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
265 EVT_CHAR(wxCheckListBox::OnChar
)
266 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick
)
272 // def ctor: use Create() to really create the control
273 wxCheckListBox::wxCheckListBox() : wxListBox()
277 // ctor which creates the associated control
278 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
279 const wxPoint
& pos
, const wxSize
& size
,
280 int nStrings
, const wxString choices
[],
281 long style
, const wxValidator
& val
,
282 const wxString
& name
)
285 Create(parent
, id
, pos
, size
, nStrings
, choices
,
286 style
| wxLB_OWNERDRAW
, val
, name
);
289 void wxCheckListBox::Delete(int N
)
291 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
292 wxT("invalid index in wxListBox::Delete") );
294 wxListBox::Delete(N
);
302 void wxCheckListBox::InsertItems(int nItems
, const wxString items
[], int pos
)
304 wxCHECK_RET( pos
>= 0 && pos
<= m_noItems
,
305 wxT("invalid index in wxCheckListBox::InsertItems") );
307 wxListBox::InsertItems(nItems
, items
, pos
);
310 for ( i
= 0; i
< nItems
; i
++ ) {
311 wxOwnerDrawn
*pNewItem
= CreateItem((size_t)(pos
+ i
));
312 pNewItem
->SetName(items
[i
]);
313 pNewItem
->SetFont(GetFont());
315 m_aItems
.Insert(pNewItem
, (size_t)(pos
+ i
));
317 ListBox_SetItemData((HWND
)GetHWND(), i
+ pos
, pNewItem
);
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::CreateItem(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 return GetItem(uiIndex
)->IsChecked();
370 void wxCheckListBox::Check(size_t uiIndex
, bool bCheck
)
372 GetItem(uiIndex
)->Check(bCheck
);
378 void wxCheckListBox::OnChar(wxKeyEvent
& event
)
380 if ( event
.KeyCode() == WXK_SPACE
)
381 GetItem(GetSelection())->Toggle();
386 void wxCheckListBox::OnLeftClick(wxMouseEvent
& event
)
388 // clicking on the item selects it, clicking on the checkmark toggles
389 if ( event
.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) {
391 size_t nItem
= (size_t)::SendMessage
396 MAKELPARAM(event
.GetX(), event
.GetY())
399 // FIXME this doesn't work when the listbox is scrolled!
400 size_t nItem
= ((size_t)event
.GetY()) / m_nItemHeight
;
403 if ( nItem
< (size_t)m_noItems
)
404 GetItem(nItem
)->Toggle();
405 //else: it's not an error, just click outside of client zone
408 // implement default behaviour: clicking on the item selects it