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 ///////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "checklst.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #include "wx/object.h"
28 #include "wx/colour.h"
30 #include "wx/bitmap.h"
31 #include "wx/window.h"
32 #include "wx/listbox.h"
33 #include "wx/ownerdrw.h"
34 #include "wx/settings.h"
35 #include "wx/dcmemory.h"
36 #include "wx/msw/checklst.h"
38 // ============================================================================
40 // ============================================================================
42 #if !USE_SHARED_LIBRARY
43 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
)
46 // ----------------------------------------------------------------------------
47 // declaration and implementation of wxCheckListBoxItem class
48 // ----------------------------------------------------------------------------
50 class wxCheckListBoxItem
: public wxOwnerDrawn
54 wxCheckListBoxItem(wxCheckListBox
*pParent
, size_t nIndex
);
57 virtual bool OnDrawItem(wxDC
& dc
, const wxRect
& rc
, wxODAction act
, wxODStatus stat
);
60 bool IsChecked() const { return m_bChecked
; }
61 void Check(bool bCheck
) { m_bChecked
= bCheck
; }
66 wxCheckListBox
*m_pParent
;
70 wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox
*pParent
, size_t nIndex
)
71 : wxOwnerDrawn("", TRUE
) // checkable
77 // we don't initialize m_nCheckHeight/Width vars because it's
78 // done in OnMeasure while they are used only in OnDraw and we
79 // know that there will always be OnMeasure before OnDraw
82 SetFont(wxSystemSettings::GetSystemFont(wxSYS_ANSI_VAR_FONT
));
83 SetMarginWidth(GetDefaultMarginWidth());
87 * JACS - I've got the owner-draw stuff partially working with WIN16,
88 * with a really horrible-looking cross for wxCheckListBox instead of a
89 * check - could use a bitmap check-mark instead, defined in wx.rc.
90 * Also there's a refresh problem whereby it doesn't always draw the
91 * check until you click to the right of it, which is OK for WIN32.
94 bool wxCheckListBoxItem::OnDrawItem(wxDC
& dc
, const wxRect
& rc
,
95 wxODAction act
, wxODStatus stat
)
98 stat
= (wxOwnerDrawn::wxODStatus
)(stat
| wxOwnerDrawn::wxODChecked
);
100 if ( wxOwnerDrawn::OnDrawItem(dc
, rc
, act
, stat
) ) {
101 // ## using native API for performance and precision
102 size_t nCheckWidth
= GetDefaultMarginWidth(),
103 nCheckHeight
= m_pParent
->GetItemHeight();
108 HDC hdc
= (HDC
)dc
.GetHDC();
111 HPEN hpenBack
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_WINDOW
)),
112 hpenGray
= CreatePen(PS_SOLID
, 0, RGB(128, 128, 128)),
113 hpenPrev
= (HPEN
)SelectObject(hdc
, hpenBack
);
115 // we erase the 1-pixel border
116 Rectangle(hdc
, x
, y
, x
+ nCheckWidth
, y
+ nCheckHeight
);
118 // shift check mark 1 pixel to the right (it looks better like this)
122 // first create a monochrome bitmap in a memory DC
123 HDC hdcMem
= CreateCompatibleDC(hdc
);
124 HBITMAP hbmpCheck
= CreateBitmap(nCheckWidth
, nCheckHeight
, 1, 1, 0);
125 HBITMAP hbmpOld
= (HBITMAP
)SelectObject(hdcMem
, hbmpCheck
);
127 // then draw a check mark into it
128 RECT rect
= { 0, 0, nCheckWidth
, nCheckHeight
};
132 DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
135 // In WIN16, draw a cross
136 HPEN blackPen
= CreatePen(PS_SOLID
, 1, RGB(0, 0, 0));
137 HPEN whiteBrush
= GetStockObject(WHITE_BRUSH
);
138 HPEN hPenOld
= ::SelectObject(hdcMem
, blackPen
);
139 HPEN hBrushOld
= ::SelectObject(hdcMem
, whiteBrush
);
140 ::SetROP2(hdcMem
, R2_COPYPEN
);
141 Rectangle(hdcMem
, 0, 0, nCheckWidth
, nCheckHeight
);
142 MoveTo(hdcMem
, 0, 0);
143 LineTo(hdcMem
, nCheckWidth
, nCheckHeight
);
144 MoveTo(hdcMem
, nCheckWidth
, 0);
145 LineTo(hdcMem
, 0, nCheckHeight
);
146 ::SelectObject(hdcMem
, hPenOld
);
147 ::SelectObject(hdcMem
, hBrushOld
);
148 ::DeleteObject(blackPen
);
151 // finally copy it to screen DC and clean up
152 BitBlt(hdc
, x
, y
, nCheckWidth
- 1, nCheckHeight
,
153 hdcMem
, 0, 0, SRCCOPY
);
155 SelectObject(hdcMem
, hbmpOld
);
156 DeleteObject(hbmpCheck
);
160 // now we draw the smaller rectangle
165 // draw hollow gray rectangle
166 (void)SelectObject(hdc
, hpenGray
);
167 HBRUSH hbrPrev
= (HBRUSH
)SelectObject(hdc
, GetStockObject(NULL_BRUSH
));
168 Rectangle(hdc
, x
, y
, x
+ nCheckWidth
, y
+ nCheckHeight
);
171 (void)SelectObject(hdc
, hpenPrev
);
172 (void)SelectObject(hdc
, hbrPrev
);
174 DeleteObject(hpenBack
);
175 DeleteObject(hpenGray
);
178 dc.DrawRectangle(x, y, nCheckWidth, nCheckHeight);
181 dc.DrawLine(x, y, x + nCheckWidth, y + nCheckHeight);
182 dc.DrawLine(x, y + nCheckHeight, x + nCheckWidth, y);
192 // change the state of the item and redraw it
193 void wxCheckListBoxItem::Toggle()
195 m_bChecked
= !m_bChecked
;
197 size_t nHeight
= m_pParent
->GetItemHeight();
198 size_t y
= m_nIndex
* nHeight
;
199 RECT rcUpdate
= { 0, y
, GetDefaultMarginWidth(), y
+ nHeight
};
200 InvalidateRect((HWND
)m_pParent
->GetHWND(), &rcUpdate
, FALSE
);
202 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, m_pParent
->GetId());
203 event
.SetInt(m_nIndex
);
204 event
.SetEventObject(m_pParent
);
205 m_pParent
->ProcessCommand(event
);
208 // ----------------------------------------------------------------------------
209 // implementation of wxCheckListBox class
210 // ----------------------------------------------------------------------------
212 // define event table
213 // ------------------
214 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
215 EVT_CHAR(wxCheckListBox::OnChar
)
216 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick
)
222 // def ctor: use Create() to really create the control
223 wxCheckListBox::wxCheckListBox() : wxListBox()
227 // ctor which creates the associated control
228 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
229 const wxPoint
& pos
, const wxSize
& size
,
230 int nStrings
, const wxString choices
[],
231 long style
, const wxValidator
& val
,
232 const wxString
& name
) // , const wxFont& font)
233 // don't use ctor with arguments! we must call Create()
234 // ourselves from here.
238 Create(parent
, id
, pos
, size
, nStrings
, choices
, style
|wxLB_OWNERDRAW
, val
, name
);
241 // create/retrieve item
242 // --------------------
244 // create a check list box item
245 wxOwnerDrawn
*wxCheckListBox::CreateItem(size_t nIndex
)
247 wxCheckListBoxItem
*pItem
= new wxCheckListBoxItem(this, nIndex
);
248 if ( m_windowFont
.Ok() )
249 pItem
->SetFont(m_windowFont
);
254 // get item (converted to right type)
255 #define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n)))
259 bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT
*item
)
261 if ( wxListBox::MSWOnMeasure(item
) ) {
262 MEASUREITEMSTRUCT
*pStruct
= (MEASUREITEMSTRUCT
*)item
;
265 m_nItemHeight
= pStruct
->itemHeight
;
267 // add place for the check mark
268 pStruct
->itemWidth
+= wxOwnerDrawn::GetDefaultMarginWidth();
279 bool wxCheckListBox::IsChecked(size_t uiIndex
) const
281 return GetItem(uiIndex
)->IsChecked();
284 void wxCheckListBox::Check(size_t uiIndex
, bool bCheck
)
286 GetItem(uiIndex
)->Check(bCheck
);
292 void wxCheckListBox::OnChar(wxKeyEvent
& event
)
294 if ( event
.KeyCode() == WXK_SPACE
)
295 GetItem(GetSelection())->Toggle();
297 wxListBox::OnChar(event
);
300 void wxCheckListBox::OnLeftClick(wxMouseEvent
& event
)
302 // clicking on the item selects it, clicking on the checkmark toggles
303 if ( event
.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) {
304 // # better use LB_ITEMFROMPOINT perhaps?
305 size_t nItem
= ((size_t)event
.GetY()) / m_nItemHeight
;
306 if ( nItem
< (size_t)m_noItems
)
307 GetItem(nItem
)->Toggle();
308 //else: it's not an error, just click outside of client zone
311 // implement default behaviour: clicking on the item selects it