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 // ============================================================================
13 // headers & declarations
14 // ============================================================================
17 #pragma implementation "checklst.h"
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/ownerdrw.h"
30 #include "wx/msw/checklst.h"
32 // ============================================================================
34 // ============================================================================
36 #if !USE_SHARED_LIBRARY
37 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
)
40 // ----------------------------------------------------------------------------
41 // declaration and implementation of wxCheckListBoxItem class
42 // ----------------------------------------------------------------------------
44 class wxCheckListBoxItem
: public wxOwnerDrawn
48 wxCheckListBoxItem(wxCheckListBox
*pParent
, uint nIndex
);
51 virtual bool OnDrawItem(wxDC
& dc
, const wxRect
& rc
, wxODAction act
, wxODStatus stat
);
54 bool IsChecked() const { return m_bChecked
; }
55 void Check(bool bCheck
) { m_bChecked
= bCheck
; }
60 wxCheckListBox
*m_pParent
;
64 wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox
*pParent
, uint nIndex
)
65 : wxOwnerDrawn("", TRUE
) // checkable
71 // we don't initialize m_nCheckHeight/Width vars because it's
72 // done in OnMeasure while they are used only in OnDraw and we
73 // know that there will always be OnMeasure before OnDraw
76 SetFont(wxSystemSettings::GetSystemFont(wxSYS_ANSI_VAR_FONT
));
77 SetMarginWidth(GetDefaultMarginWidth());
81 * JACS - I've got the owner-draw stuff partially working with WIN16,
82 * with a really horrible-looking cross for wxCheckListBox instead of a
83 * check - could use a bitmap check-mark instead, defined in wx.rc.
84 * Also there's a refresh problem whereby it doesn't always draw the
85 * check until you click to the right of it, which is OK for WIN32.
88 bool wxCheckListBoxItem::OnDrawItem(wxDC
& dc
, const wxRect
& rc
,
89 wxODAction act
, wxODStatus stat
)
92 stat
= (wxOwnerDrawn::wxODStatus
)(stat
| wxOwnerDrawn::wxODChecked
);
94 if ( wxOwnerDrawn::OnDrawItem(dc
, rc
, act
, stat
) ) {
95 // ## using native API for performance and precision
96 uint nCheckWidth
= GetDefaultMarginWidth(),
97 nCheckHeight
= m_pParent
->GetItemHeight();
102 HDC hdc
= (HDC
)dc
.GetHDC();
105 HPEN hpenBack
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_WINDOW
)),
106 hpenGray
= CreatePen(PS_SOLID
, 0, RGB(128, 128, 128)),
107 hpenPrev
= SelectObject(hdc
, hpenBack
);
109 // we erase the 1-pixel border
110 Rectangle(hdc
, x
, y
, x
+ nCheckWidth
, y
+ nCheckHeight
);
112 // shift check mark 1 pixel to the right (it looks better like this)
116 // first create a monochrome bitmap in a memory DC
117 HDC hdcMem
= CreateCompatibleDC(hdc
);
118 HBITMAP hbmpCheck
= CreateBitmap(nCheckWidth
, nCheckHeight
, 1, 1, 0);
119 HBITMAP hbmpOld
= SelectObject(hdcMem
, hbmpCheck
);
121 // then draw a check mark into it
122 RECT rect
= { 0, 0, nCheckWidth
, nCheckHeight
};
125 DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
127 // In WIN16, draw a cross
128 HPEN blackPen
= CreatePen(PS_SOLID
, 1, RGB(0, 0, 0));
129 HPEN whiteBrush
= GetStockObject(WHITE_BRUSH
);
130 HPEN hPenOld
= ::SelectObject(hdcMem
, blackPen
);
131 HPEN hBrushOld
= ::SelectObject(hdcMem
, whiteBrush
);
132 ::SetROP2(hdcMem
, R2_COPYPEN
);
133 Rectangle(hdcMem
, 0, 0, nCheckWidth
, nCheckHeight
);
134 MoveTo(hdcMem
, 0, 0);
135 LineTo(hdcMem
, nCheckWidth
, nCheckHeight
);
136 MoveTo(hdcMem
, nCheckWidth
, 0);
137 LineTo(hdcMem
, 0, nCheckHeight
);
138 ::SelectObject(hdcMem
, hPenOld
);
139 ::SelectObject(hdcMem
, hBrushOld
);
140 ::DeleteObject(blackPen
);
143 // finally copy it to screen DC and clean up
144 BitBlt(hdc
, x
, y
, nCheckWidth
- 1, nCheckHeight
,
145 hdcMem
, 0, 0, SRCCOPY
);
147 SelectObject(hdcMem
, hbmpOld
);
148 DeleteObject(hbmpCheck
);
152 // now we draw the smaller rectangle
157 // draw hollow gray rectangle
158 (void)SelectObject(hdc
, hpenGray
);
159 HBRUSH hbrPrev
= SelectObject(hdc
, GetStockObject(NULL_BRUSH
));
160 Rectangle(hdc
, x
, y
, x
+ nCheckWidth
, y
+ nCheckHeight
);
163 (void)SelectObject(hdc
, hpenPrev
);
164 (void)SelectObject(hdc
, hbrPrev
);
166 DeleteObject(hpenBack
);
167 DeleteObject(hpenGray
);
170 dc.DrawRectangle(x, y, nCheckWidth, nCheckHeight);
173 dc.DrawLine(x, y, x + nCheckWidth, y + nCheckHeight);
174 dc.DrawLine(x, y + nCheckHeight, x + nCheckWidth, y);
184 // change the state of the item and redraw it
185 void wxCheckListBoxItem::Toggle()
187 m_bChecked
= !m_bChecked
;
189 uint nHeight
= m_pParent
->GetItemHeight();
190 uint y
= m_nIndex
* nHeight
;
191 RECT rcUpdate
= { 0, y
, GetDefaultMarginWidth(), y
+ nHeight
};
192 InvalidateRect((HWND
)m_pParent
->GetHWND(), &rcUpdate
, FALSE
);
194 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, m_pParent
->GetId());
195 event
.SetInt(m_nIndex
);
196 event
.SetEventObject(m_pParent
);
197 m_pParent
->ProcessCommand(event
);
200 // ----------------------------------------------------------------------------
201 // implementation of wxCheckListBox class
202 // ----------------------------------------------------------------------------
204 // define event table
205 // ------------------
206 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
207 EVT_CHAR(wxCheckListBox::OnChar
)
208 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick
)
214 // def ctor: use Create() to really create the control
215 wxCheckListBox::wxCheckListBox() : wxListBox()
219 // ctor which creates the associated control
220 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, const wxWindowID id
,
221 const wxPoint
& pos
, const wxSize
& size
,
222 const int nStrings
, const wxString choices
[],
223 const long style
, const wxValidator
& val
,
224 const wxString
& name
) // , const wxFont& font)
225 // don't use ctor with arguments! we must call Create()
226 // ourselves from here.
230 Create(parent
, id
, pos
, size
, nStrings
, choices
, style
|wxLB_OWNERDRAW
, val
, name
);
233 // create/retrieve item
234 // --------------------
236 // create a check list box item
237 wxOwnerDrawn
*wxCheckListBox::CreateItem(uint nIndex
)
239 wxCheckListBoxItem
*pItem
= new wxCheckListBoxItem(this, nIndex
);
240 if ( m_windowFont
.Ok() )
241 pItem
->SetFont(m_windowFont
);
246 // get item (converted to right type)
247 #define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n)))
251 bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT
*item
)
253 if ( wxListBox::MSWOnMeasure(item
) ) {
254 MEASUREITEMSTRUCT
*pStruct
= (MEASUREITEMSTRUCT
*)item
;
257 m_nItemHeight
= pStruct
->itemHeight
;
259 // add place for the check mark
260 pStruct
->itemWidth
+= wxOwnerDrawn::GetDefaultMarginWidth();
271 bool wxCheckListBox::IsChecked(uint uiIndex
) const
273 return GetItem(uiIndex
)->IsChecked();
276 void wxCheckListBox::Check(uint uiIndex
, bool bCheck
)
278 GetItem(uiIndex
)->Check(bCheck
);
284 void wxCheckListBox::OnChar(wxKeyEvent
& event
)
286 if ( event
.KeyCode() == WXK_SPACE
)
287 GetItem(GetSelection())->Toggle();
289 wxListBox::OnChar(event
);
292 void wxCheckListBox::OnLeftClick(wxMouseEvent
& event
)
294 // clicking on the item selects it, clicking on the checkmark toggles
295 if ( event
.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) {
296 // # better use LB_ITEMFROMPOINT perhaps?
297 uint nItem
= ((uint
)event
.GetY()) / m_nItemHeight
;
298 if ( nItem
< (uint
)m_noItems
)
299 GetItem(nItem
)->Toggle();
300 //else: it's not an error, just click outside of client zone
303 // implement default behaviour: clicking on the item selects it