1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: implementation of wxCheckListBox class
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
13 // headers & declarations
14 // ============================================================================
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
21 #include "wx/object.h"
22 #include "wx/colour.h"
24 #include "wx/bitmap.h"
25 #include "wx/window.h"
26 #include "wx/listbox.h"
27 #include "wx/ownerdrw.h"
28 #include "wx/settings.h"
29 #include "wx/dcmemory.h"
30 #include "wx/os2/checklst.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 // get item (converted to right type)
41 #define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n)))
43 // ============================================================================
45 // ============================================================================
47 #if !USE_SHARED_LIBRARY
48 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
)
51 // ----------------------------------------------------------------------------
52 // declaration and implementation of wxCheckListBoxItem class
53 // ----------------------------------------------------------------------------
55 class wxCheckListBoxItem
: public wxOwnerDrawn
57 friend class wxCheckListBox
;
60 wxCheckListBoxItem(wxCheckListBox
*pParent
, size_t nIndex
);
63 virtual bool OnDrawItem(wxDC
& dc
, const wxRect
& rc
, wxODAction act
, wxODStatus stat
);
66 bool IsChecked() const { return m_bChecked
; }
67 void Check(bool bCheck
);
68 void Toggle() { Check(!IsChecked()); }
72 wxCheckListBox
*m_pParent
;
76 wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox
*pParent
, size_t nIndex
)
77 : wxOwnerDrawn("", TRUE
) // checkable
83 // we don't initialize m_nCheckHeight/Width vars because it's
84 // done in OnMeasure while they are used only in OnDraw and we
85 // know that there will always be OnMeasure before OnDraw
88 SetMarginWidth(GetDefaultMarginWidth());
92 * JACS - I've got the owner-draw stuff partially working with WIN16,
93 * with a really horrible-looking cross for wxCheckListBox instead of a
94 * check - could use a bitmap check-mark instead, defined in wx.rc.
95 * Also there's a refresh problem whereby it doesn't always draw the
96 * check until you click to the right of it, which is OK for WIN32.
99 bool wxCheckListBoxItem::OnDrawItem(wxDC
& dc
, const wxRect
& rc
,
100 wxODAction act
, wxODStatus stat
)
103 stat
= (wxOwnerDrawn::wxODStatus
)(stat
| wxOwnerDrawn::wxODChecked
);
108 if ( wxOwnerDrawn::OnDrawItem(dc, rc, act, stat) ) {
109 // ## using native API for performance and precision
110 size_t nCheckWidth = GetDefaultMarginWidth(),
111 nCheckHeight = m_pParent->GetItemHeight();
116 HDC hdc = (HDC)dc.GetHDC();
119 HPEN hpenBack = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_WINDOW)),
120 hpenGray = CreatePen(PS_SOLID, 0, RGB(128, 128, 128)),
121 hpenPrev = (HPEN)SelectObject(hdc, hpenBack);
123 // we erase the 1-pixel border
124 Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight);
126 // shift check mark 1 pixel to the right (it looks better like this)
130 // first create a monochrome bitmap in a memory DC
131 HDC hdcMem = CreateCompatibleDC(hdc);
132 HBITMAP hbmpCheck = CreateBitmap(nCheckWidth, nCheckHeight, 1, 1, 0);
133 HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcMem, hbmpCheck);
135 // then draw a check mark into it
139 rect.right = nCheckWidth ;
140 rect.bottom = nCheckHeight ;
144 DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK);
147 // In WIN16, draw a cross
148 HPEN blackPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
149 HPEN whiteBrush = (HPEN)GetStockObject(WHITE_BRUSH);
150 HPEN hPenOld = (HPEN)::SelectObject(hdcMem, blackPen);
151 HPEN hBrushOld = (HPEN)::SelectObject(hdcMem, whiteBrush);
152 ::SetROP2(hdcMem, R2_COPYPEN);
153 Rectangle(hdcMem, 0, 0, nCheckWidth, nCheckHeight);
154 MoveTo(hdcMem, 0, 0);
155 LineTo(hdcMem, nCheckWidth, nCheckHeight);
156 MoveTo(hdcMem, nCheckWidth, 0);
157 LineTo(hdcMem, 0, nCheckHeight);
158 ::SelectObject(hdcMem, hPenOld);
159 ::SelectObject(hdcMem, hBrushOld);
160 ::DeleteObject(blackPen);
163 // finally copy it to screen DC and clean up
164 BitBlt(hdc, x, y, nCheckWidth - 1, nCheckHeight,
165 hdcMem, 0, 0, SRCCOPY);
167 SelectObject(hdcMem, hbmpOld);
168 DeleteObject(hbmpCheck);
172 // now we draw the smaller rectangle
177 // draw hollow gray rectangle
178 (void)SelectObject(hdc, hpenGray);
179 HBRUSH hbrPrev = (HBRUSH)SelectObject(hdc, GetStockObject(NULL_BRUSH));
180 Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight);
183 (void)SelectObject(hdc, hpenPrev);
184 (void)SelectObject(hdc, hbrPrev);
186 DeleteObject(hpenBack);
187 DeleteObject(hpenGray);
190 // dc.DrawRectangle(x, y, nCheckWidth, nCheckHeight);
192 // if ( IsChecked() ) {
193 // dc.DrawLine(x, y, x + nCheckWidth, y + nCheckHeight);
194 // dc.DrawLine(x, y + nCheckHeight, x + nCheckWidth, y);
204 // change the state of the item and redraw it
205 void wxCheckListBoxItem::Check(bool check
)
209 // index may be chanegd because new items were added/deleted
210 if ( m_pParent
->GetItemIndex(this) != (int)m_nIndex
)
213 int index
= m_pParent
->GetItemIndex(this);
215 wxASSERT_MSG( index
!= wxNOT_FOUND
, wxT("what does this item do here?") );
217 m_nIndex
= (size_t)index
;
220 HWND hwndListbox
= (HWND
)m_pParent
->GetHWND();
225 if ( ::SendMessage(hwndListbox, LB_GETITEMRECT,
226 m_nIndex, (LPARAM)&rcUpdate) == LB_ERR )
228 wxLogDebug(wxT("LB_GETITEMRECT failed"));
231 InvalidateRect(hwndListbox, &rcUpdate, FALSE);
233 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, m_pParent
->GetId());
234 event
.SetInt(m_nIndex
);
235 event
.SetEventObject(m_pParent
);
236 m_pParent
->ProcessCommand(event
);
239 // ----------------------------------------------------------------------------
240 // implementation of wxCheckListBox class
241 // ----------------------------------------------------------------------------
243 // define event table
244 // ------------------
245 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
246 EVT_CHAR(wxCheckListBox::OnChar
)
247 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick
)
253 // def ctor: use Create() to really create the control
254 wxCheckListBox::wxCheckListBox() : wxListBox()
258 // ctor which creates the associated control
259 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
260 const wxPoint
& pos
, const wxSize
& size
,
261 int nStrings
, const wxString choices
[],
263 long style
, const wxValidator
& val
,
265 const wxString
& name
)
268 Create(parent
, id
, pos
, size
, nStrings
, choices
,
269 style
| wxLB_OWNERDRAW
, val
, name
);
272 void wxCheckListBox::Delete(int N
)
274 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
275 wxT("invalid index in wxListBox::Delete") );
277 wxListBox::Delete(N
);
285 void wxCheckListBox::InsertItems(int nItems
, const wxString items
[], int pos
)
287 wxCHECK_RET( pos
>= 0 && pos
<= m_noItems
,
288 wxT("invalid index in wxCheckListBox::InsertItems") );
290 wxListBox::InsertItems(nItems
, items
, pos
);
293 for ( i
= 0; i
< nItems
; i
++ ) {
294 wxOwnerDrawn
*pNewItem
= CreateItem((size_t)(pos
+ i
));
295 pNewItem
->SetName(items
[i
]);
296 m_aItems
.Insert(pNewItem
, (size_t)(pos
+ i
));
297 // ListBox_SetItemData((HWND)GetHWND(), i + pos, pNewItem);
302 bool wxCheckListBox::SetFont( const wxFont
&font
)
305 for (i
=0; i
< m_aItems
.GetCount(); i
++)
306 m_aItems
[i
]->SetFont(font
);
307 wxListBox::SetFont(font
);
311 // create/retrieve item
312 // --------------------
314 // create a check list box item
315 wxOwnerDrawn
*wxCheckListBox::CreateItem(size_t nIndex
)
317 wxCheckListBoxItem
*pItem
= new wxCheckListBoxItem(this, nIndex
);
325 bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
327 if ( wxListBox::MSWOnMeasure(item) ) {
328 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
331 m_nItemHeight = pStruct->itemHeight;
333 // add place for the check mark
334 pStruct->itemWidth += wxOwnerDrawn::GetDefaultMarginWidth();
345 bool wxCheckListBox::IsChecked(size_t uiIndex
) const
347 return GetItem(uiIndex
)->IsChecked();
350 void wxCheckListBox::Check(size_t uiIndex
, bool bCheck
)
352 GetItem(uiIndex
)->Check(bCheck
);
358 void wxCheckListBox::OnChar(wxKeyEvent
& event
)
360 if ( event
.KeyCode() == WXK_SPACE
)
361 GetItem(GetSelection())->Toggle();
366 void wxCheckListBox::OnLeftClick(wxMouseEvent
& event
)
368 // clicking on the item selects it, clicking on the checkmark toggles
369 if ( event
.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) {
372 size_t nItem = (size_t)::SendMessage
377 MAKELPARAM(event.GetX(), event.GetY())
382 if ( nItem
< (size_t)m_noItems
)
383 GetItem(nItem
)->Toggle();
384 //else: it's not an error, just click outside of client zone
387 // implement default behaviour: clicking on the item selects it