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"
31 #include "wx/ownerdrw.h"
32 #include "wx/msw/checklst.h"
34 // ============================================================================
36 // ============================================================================
38 #if !USE_SHARED_LIBRARY
39 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
)
42 // ----------------------------------------------------------------------------
43 // declaration and implementation of wxCheckListBoxItem class
44 // ----------------------------------------------------------------------------
46 class wxCheckListBoxItem
: public wxOwnerDrawn
50 wxCheckListBoxItem(wxCheckListBox
*pParent
, size_t nIndex
);
53 virtual bool OnDrawItem(wxDC
& dc
, const wxRect
& rc
, wxODAction act
, wxODStatus stat
);
56 bool IsChecked() const { return m_bChecked
; }
57 void Check(bool bCheck
) { m_bChecked
= bCheck
; }
62 wxCheckListBox
*m_pParent
;
66 wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox
*pParent
, size_t nIndex
)
67 : wxOwnerDrawn("", TRUE
) // checkable
73 // we don't initialize m_nCheckHeight/Width vars because it's
74 // done in OnMeasure while they are used only in OnDraw and we
75 // know that there will always be OnMeasure before OnDraw
78 SetFont(wxSystemSettings::GetSystemFont(wxSYS_ANSI_VAR_FONT
));
79 SetMarginWidth(GetDefaultMarginWidth());
83 * JACS - I've got the owner-draw stuff partially working with WIN16,
84 * with a really horrible-looking cross for wxCheckListBox instead of a
85 * check - could use a bitmap check-mark instead, defined in wx.rc.
86 * Also there's a refresh problem whereby it doesn't always draw the
87 * check until you click to the right of it, which is OK for WIN32.
90 bool wxCheckListBoxItem::OnDrawItem(wxDC
& dc
, const wxRect
& rc
,
91 wxODAction act
, wxODStatus stat
)
94 stat
= (wxOwnerDrawn::wxODStatus
)(stat
| wxOwnerDrawn::wxODChecked
);
96 if ( wxOwnerDrawn::OnDrawItem(dc
, rc
, act
, stat
) ) {
97 // ## using native API for performance and precision
98 size_t nCheckWidth
= GetDefaultMarginWidth(),
99 nCheckHeight
= m_pParent
->GetItemHeight();
104 HDC hdc
= (HDC
)dc
.GetHDC();
107 HPEN hpenBack
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_WINDOW
)),
108 hpenGray
= CreatePen(PS_SOLID
, 0, RGB(128, 128, 128)),
109 hpenPrev
= (HPEN
)SelectObject(hdc
, hpenBack
);
111 // we erase the 1-pixel border
112 Rectangle(hdc
, x
, y
, x
+ nCheckWidth
, y
+ nCheckHeight
);
114 // shift check mark 1 pixel to the right (it looks better like this)
118 // first create a monochrome bitmap in a memory DC
119 HDC hdcMem
= CreateCompatibleDC(hdc
);
120 HBITMAP hbmpCheck
= CreateBitmap(nCheckWidth
, nCheckHeight
, 1, 1, 0);
121 HBITMAP hbmpOld
= (HBITMAP
)SelectObject(hdcMem
, hbmpCheck
);
123 // then draw a check mark into it
124 RECT rect
= { 0, 0, nCheckWidth
, nCheckHeight
};
127 DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
129 // In WIN16, draw a cross
130 HPEN blackPen
= CreatePen(PS_SOLID
, 1, RGB(0, 0, 0));
131 HPEN whiteBrush
= GetStockObject(WHITE_BRUSH
);
132 HPEN hPenOld
= ::SelectObject(hdcMem
, blackPen
);
133 HPEN hBrushOld
= ::SelectObject(hdcMem
, whiteBrush
);
134 ::SetROP2(hdcMem
, R2_COPYPEN
);
135 Rectangle(hdcMem
, 0, 0, nCheckWidth
, nCheckHeight
);
136 MoveTo(hdcMem
, 0, 0);
137 LineTo(hdcMem
, nCheckWidth
, nCheckHeight
);
138 MoveTo(hdcMem
, nCheckWidth
, 0);
139 LineTo(hdcMem
, 0, nCheckHeight
);
140 ::SelectObject(hdcMem
, hPenOld
);
141 ::SelectObject(hdcMem
, hBrushOld
);
142 ::DeleteObject(blackPen
);
145 // finally copy it to screen DC and clean up
146 BitBlt(hdc
, x
, y
, nCheckWidth
- 1, nCheckHeight
,
147 hdcMem
, 0, 0, SRCCOPY
);
149 SelectObject(hdcMem
, hbmpOld
);
150 DeleteObject(hbmpCheck
);
154 // now we draw the smaller rectangle
159 // draw hollow gray rectangle
160 (void)SelectObject(hdc
, hpenGray
);
161 HBRUSH hbrPrev
= (HBRUSH
)SelectObject(hdc
, GetStockObject(NULL_BRUSH
));
162 Rectangle(hdc
, x
, y
, x
+ nCheckWidth
, y
+ nCheckHeight
);
165 (void)SelectObject(hdc
, hpenPrev
);
166 (void)SelectObject(hdc
, hbrPrev
);
168 DeleteObject(hpenBack
);
169 DeleteObject(hpenGray
);
172 dc.DrawRectangle(x, y, nCheckWidth, nCheckHeight);
175 dc.DrawLine(x, y, x + nCheckWidth, y + nCheckHeight);
176 dc.DrawLine(x, y + nCheckHeight, x + nCheckWidth, y);
186 // change the state of the item and redraw it
187 void wxCheckListBoxItem::Toggle()
189 m_bChecked
= !m_bChecked
;
191 size_t nHeight
= m_pParent
->GetItemHeight();
192 size_t y
= m_nIndex
* nHeight
;
193 RECT rcUpdate
= { 0, y
, GetDefaultMarginWidth(), y
+ nHeight
};
194 InvalidateRect((HWND
)m_pParent
->GetHWND(), &rcUpdate
, FALSE
);
196 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, m_pParent
->GetId());
197 event
.SetInt(m_nIndex
);
198 event
.SetEventObject(m_pParent
);
199 m_pParent
->ProcessCommand(event
);
202 // ----------------------------------------------------------------------------
203 // implementation of wxCheckListBox class
204 // ----------------------------------------------------------------------------
206 // define event table
207 // ------------------
208 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
209 EVT_CHAR(wxCheckListBox::OnChar
)
210 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick
)
216 // def ctor: use Create() to really create the control
217 wxCheckListBox::wxCheckListBox() : wxListBox()
221 // ctor which creates the associated control
222 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
223 const wxPoint
& pos
, const wxSize
& size
,
224 int nStrings
, const wxString choices
[],
225 long style
, const wxValidator
& val
,
226 const wxString
& name
) // , const wxFont& font)
227 // don't use ctor with arguments! we must call Create()
228 // ourselves from here.
232 Create(parent
, id
, pos
, size
, nStrings
, choices
, style
|wxLB_OWNERDRAW
, val
, name
);
235 // create/retrieve item
236 // --------------------
238 // create a check list box item
239 wxOwnerDrawn
*wxCheckListBox::CreateItem(size_t nIndex
)
241 wxCheckListBoxItem
*pItem
= new wxCheckListBoxItem(this, nIndex
);
242 if ( m_windowFont
.Ok() )
243 pItem
->SetFont(m_windowFont
);
248 // get item (converted to right type)
249 #define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n)))
253 bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT
*item
)
255 if ( wxListBox::MSWOnMeasure(item
) ) {
256 MEASUREITEMSTRUCT
*pStruct
= (MEASUREITEMSTRUCT
*)item
;
259 m_nItemHeight
= pStruct
->itemHeight
;
261 // add place for the check mark
262 pStruct
->itemWidth
+= wxOwnerDrawn::GetDefaultMarginWidth();
273 bool wxCheckListBox::IsChecked(size_t uiIndex
) const
275 return GetItem(uiIndex
)->IsChecked();
278 void wxCheckListBox::Check(size_t uiIndex
, bool bCheck
)
280 GetItem(uiIndex
)->Check(bCheck
);
286 void wxCheckListBox::OnChar(wxKeyEvent
& event
)
288 if ( event
.KeyCode() == WXK_SPACE
)
289 GetItem(GetSelection())->Toggle();
291 wxListBox::OnChar(event
);
294 void wxCheckListBox::OnLeftClick(wxMouseEvent
& event
)
296 // clicking on the item selects it, clicking on the checkmark toggles
297 if ( event
.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) {
298 // # better use LB_ITEMFROMPOINT perhaps?
299 size_t nItem
= ((size_t)event
.GetY()) / m_nItemHeight
;
300 if ( nItem
< (size_t)m_noItems
)
301 GetItem(nItem
)->Toggle();
302 //else: it's not an error, just click outside of client zone
305 // implement default behaviour: clicking on the item selects it