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
[],
262 long style
, const wxValidator
& val
,
263 const wxString
& name
)
266 Create(parent
, id
, pos
, size
, nStrings
, choices
,
267 style
| wxLB_OWNERDRAW
, val
, name
);
270 void wxCheckListBox::Delete(int N
)
272 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
273 wxT("invalid index in wxListBox::Delete") );
275 wxListBox::Delete(N
);
283 void wxCheckListBox::InsertItems(int nItems
, const wxString items
[], int pos
)
285 wxCHECK_RET( pos
>= 0 && pos
<= m_noItems
,
286 wxT("invalid index in wxCheckListBox::InsertItems") );
288 wxListBox::InsertItems(nItems
, items
, pos
);
291 for ( i
= 0; i
< nItems
; i
++ ) {
292 wxOwnerDrawn
*pNewItem
= CreateItem((size_t)(pos
+ i
));
293 pNewItem
->SetName(items
[i
]);
294 m_aItems
.Insert(pNewItem
, (size_t)(pos
+ i
));
295 // ListBox_SetItemData((HWND)GetHWND(), i + pos, pNewItem);
300 bool wxCheckListBox::SetFont( const wxFont
&font
)
303 for (i
=0; i
< m_aItems
.GetCount(); i
++)
304 m_aItems
[i
]->SetFont(font
);
305 wxListBox::SetFont(font
);
309 // create/retrieve item
310 // --------------------
312 // create a check list box item
313 wxOwnerDrawn
*wxCheckListBox::CreateItem(size_t nIndex
)
315 wxCheckListBoxItem
*pItem
= new wxCheckListBoxItem(this, nIndex
);
323 bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
325 if ( wxListBox::MSWOnMeasure(item) ) {
326 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
329 m_nItemHeight = pStruct->itemHeight;
331 // add place for the check mark
332 pStruct->itemWidth += wxOwnerDrawn::GetDefaultMarginWidth();
343 bool wxCheckListBox::IsChecked(size_t uiIndex
) const
345 return GetItem(uiIndex
)->IsChecked();
348 void wxCheckListBox::Check(size_t uiIndex
, bool bCheck
)
350 GetItem(uiIndex
)->Check(bCheck
);
356 void wxCheckListBox::OnChar(wxKeyEvent
& event
)
358 if ( event
.KeyCode() == WXK_SPACE
)
359 GetItem(GetSelection())->Toggle();
364 void wxCheckListBox::OnLeftClick(wxMouseEvent
& event
)
366 // clicking on the item selects it, clicking on the checkmark toggles
367 if ( event
.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) {
370 size_t nItem = (size_t)::SendMessage
375 MAKELPARAM(event.GetX(), event.GetY())
380 if ( nItem
< (size_t)m_noItems
)
381 GetItem(nItem
)->Toggle();
382 //else: it's not an error, just click outside of client zone
385 // implement default behaviour: clicking on the item selects it