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 licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "checklst.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 #if wxUSE_CHECKLISTBOX && wxUSE_OWNER_DRAWN
34 #include "wx/object.h"
35 #include "wx/colour.h"
37 #include "wx/bitmap.h"
38 #include "wx/window.h"
39 #include "wx/listbox.h"
40 #include "wx/dcmemory.h"
42 #include "wx/settings.h"
47 #include "wx/ownerdrw.h"
48 #include "wx/checklst.h"
50 #include "wx/msw/wrapwin.h"
53 #include "wx/msw/private.h"
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // get item (converted to right type)
60 #define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n)))
62 // ============================================================================
64 // ============================================================================
67 #if wxUSE_EXTENDED_RTTI
68 WX_DEFINE_FLAGS( wxCheckListBoxStyle
)
70 wxBEGIN_FLAGS( wxCheckListBoxStyle
)
71 // new style border flags, we put them first to
72 // use them for streaming out
73 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
74 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
75 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
76 wxFLAGS_MEMBER(wxBORDER_RAISED
)
77 wxFLAGS_MEMBER(wxBORDER_STATIC
)
78 wxFLAGS_MEMBER(wxBORDER_NONE
)
80 // old style border flags
81 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
82 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
83 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
84 wxFLAGS_MEMBER(wxRAISED_BORDER
)
85 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
86 wxFLAGS_MEMBER(wxBORDER
)
88 // standard window styles
89 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
90 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
91 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
92 wxFLAGS_MEMBER(wxWANTS_CHARS
)
93 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
94 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
95 wxFLAGS_MEMBER(wxVSCROLL
)
96 wxFLAGS_MEMBER(wxHSCROLL
)
98 wxFLAGS_MEMBER(wxLB_SINGLE
)
99 wxFLAGS_MEMBER(wxLB_MULTIPLE
)
100 wxFLAGS_MEMBER(wxLB_EXTENDED
)
101 wxFLAGS_MEMBER(wxLB_HSCROLL
)
102 wxFLAGS_MEMBER(wxLB_ALWAYS_SB
)
103 wxFLAGS_MEMBER(wxLB_NEEDED_SB
)
104 wxFLAGS_MEMBER(wxLB_SORT
)
105 wxFLAGS_MEMBER(wxLB_OWNERDRAW
)
107 wxEND_FLAGS( wxCheckListBoxStyle
)
109 IMPLEMENT_DYNAMIC_CLASS_XTI(wxCheckListBox
, wxListBox
,"wx/checklst.h")
111 wxBEGIN_PROPERTIES_TABLE(wxCheckListBox
)
112 wxEVENT_PROPERTY( Toggle
, wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, wxCommandEvent
)
113 wxPROPERTY_FLAGS( WindowStyle
, wxCheckListBoxStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, wxLB_OWNERDRAW
/*flags*/ , wxT("Helpstring") , wxT("group")) // style
114 wxEND_PROPERTIES_TABLE()
116 wxBEGIN_HANDLERS_TABLE(wxCheckListBox
)
117 wxEND_HANDLERS_TABLE()
119 wxCONSTRUCTOR_4( wxCheckListBox
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
)
122 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
)
125 // ----------------------------------------------------------------------------
126 // declaration and implementation of wxCheckListBoxItem class
127 // ----------------------------------------------------------------------------
129 class wxCheckListBoxItem
: public wxOwnerDrawn
131 friend class WXDLLEXPORT wxCheckListBox
;
134 wxCheckListBoxItem(wxCheckListBox
*pParent
, size_t nIndex
);
137 virtual bool OnDrawItem(wxDC
& dc
, const wxRect
& rc
, wxODAction act
, wxODStatus stat
);
139 // simple accessors and operations
140 bool IsChecked() const { return m_bChecked
; }
142 void Check(bool bCheck
);
143 void Toggle() { Check(!IsChecked()); }
149 DECLARE_NO_COPY_CLASS(wxCheckListBoxItem
)
151 wxCheckListBox
*m_pParent
;
155 wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox
*pParent
, size_t nIndex
)
156 : wxOwnerDrawn(wxEmptyString
, true) // checkable
162 // we don't initialize m_nCheckHeight/Width vars because it's
163 // done in OnMeasure while they are used only in OnDraw and we
164 // know that there will always be OnMeasure before OnDraw
167 SetMarginWidth(GetDefaultMarginWidth());
170 bool wxCheckListBoxItem::OnDrawItem(wxDC
& dc
, const wxRect
& rc
,
171 wxODAction act
, wxODStatus stat
)
174 stat
= (wxOwnerDrawn::wxODStatus
)(stat
| wxOwnerDrawn::wxODChecked
);
176 if ( wxOwnerDrawn::OnDrawItem(dc
, rc
, act
, stat
) ) {
177 // ## using native API for performance and precision
178 size_t nCheckWidth
= GetDefaultMarginWidth(),
179 nCheckHeight
= m_pParent
->GetItemHeight();
184 HDC hdc
= (HDC
)dc
.GetHDC();
187 HPEN hpenBack
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_WINDOW
)),
188 hpenGray
= CreatePen(PS_SOLID
, 0, RGB(128, 128, 128)),
189 hpenPrev
= (HPEN
)SelectObject(hdc
, hpenBack
);
191 // we erase the 1-pixel border
192 Rectangle(hdc
, x
, y
, x
+ nCheckWidth
, y
+ nCheckHeight
);
194 // shift check mark 1 pixel to the right (it looks better like this)
198 // first create a monochrome bitmap in a memory DC
199 HDC hdcMem
= CreateCompatibleDC(hdc
);
200 HBITMAP hbmpCheck
= CreateBitmap(nCheckWidth
, nCheckHeight
, 1, 1, 0);
201 HBITMAP hbmpOld
= (HBITMAP
)SelectObject(hdcMem
, hbmpCheck
);
203 // then draw a check mark into it
208 rect
.right
= nCheckWidth
;
209 rect
.bottom
= nCheckHeight
;
212 DrawFrameControl(hdcMem
, &rect
, DFC_BUTTON
, DFCS_BUTTONCHECK
);
214 DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
217 // finally copy it to screen DC and clean up
218 BitBlt(hdc
, x
, y
, nCheckWidth
- 1, nCheckHeight
,
219 hdcMem
, 0, 0, SRCCOPY
);
221 SelectObject(hdcMem
, hbmpOld
);
222 DeleteObject(hbmpCheck
);
226 // now we draw the smaller rectangle
231 // draw hollow gray rectangle
232 (void)SelectObject(hdc
, hpenGray
);
233 HBRUSH hbrPrev
= (HBRUSH
)SelectObject(hdc
, GetStockObject(NULL_BRUSH
));
234 Rectangle(hdc
, x
, y
, x
+ nCheckWidth
, y
+ nCheckHeight
);
237 (void)SelectObject(hdc
, hpenPrev
);
238 (void)SelectObject(hdc
, hbrPrev
);
240 DeleteObject(hpenBack
);
241 DeleteObject(hpenGray
);
244 dc.DrawRectangle(x, y, nCheckWidth, nCheckHeight);
247 dc.DrawLine(x, y, x + nCheckWidth, y + nCheckHeight);
248 dc.DrawLine(x, y + nCheckHeight, x + nCheckWidth, y);
258 // change the state of the item and redraw it
259 void wxCheckListBoxItem::Check(bool check
)
263 // index may be changed because new items were added/deleted
264 if ( m_pParent
->GetItemIndex(this) != (int)m_nIndex
)
267 int index
= m_pParent
->GetItemIndex(this);
269 wxASSERT_MSG( index
!= wxNOT_FOUND
, wxT("what does this item do here?") );
271 m_nIndex
= (size_t)index
;
274 HWND hwndListbox
= (HWND
)m_pParent
->GetHWND();
278 if ( ::SendMessage(hwndListbox
, LB_GETITEMRECT
,
279 m_nIndex
, (LPARAM
)&rcUpdate
) == LB_ERR
)
281 wxLogDebug(wxT("LB_GETITEMRECT failed"));
284 ::InvalidateRect(hwndListbox
, &rcUpdate
, FALSE
);
287 // send an "item checked" event
288 void wxCheckListBoxItem::SendEvent()
290 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, m_pParent
->GetId());
291 event
.SetInt(m_nIndex
);
292 event
.SetEventObject(m_pParent
);
293 m_pParent
->ProcessCommand(event
);
296 // ----------------------------------------------------------------------------
297 // implementation of wxCheckListBox class
298 // ----------------------------------------------------------------------------
300 // define event table
301 // ------------------
302 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
303 EVT_KEY_DOWN(wxCheckListBox::OnKeyDown
)
304 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick
)
310 // def ctor: use Create() to really create the control
311 wxCheckListBox::wxCheckListBox()
315 // ctor which creates the associated control
316 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
317 const wxPoint
& pos
, const wxSize
& size
,
318 int nStrings
, const wxString choices
[],
319 long style
, const wxValidator
& val
,
320 const wxString
& name
)
322 Create(parent
, id
, pos
, size
, nStrings
, choices
, style
, val
, name
);
325 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
326 const wxPoint
& pos
, const wxSize
& size
,
327 const wxArrayString
& choices
,
328 long style
, const wxValidator
& val
,
329 const wxString
& name
)
331 Create(parent
, id
, pos
, size
, choices
, style
, val
, name
);
334 bool wxCheckListBox::Create(wxWindow
*parent
, wxWindowID id
,
335 const wxPoint
& pos
, const wxSize
& size
,
336 int n
, const wxString choices
[],
338 const wxValidator
& validator
, const wxString
& name
)
340 return wxListBox::Create(parent
, id
, pos
, size
, n
, choices
,
341 style
| wxLB_OWNERDRAW
, validator
, name
);
344 bool wxCheckListBox::Create(wxWindow
*parent
, wxWindowID id
,
345 const wxPoint
& pos
, const wxSize
& size
,
346 const wxArrayString
& choices
,
348 const wxValidator
& validator
, const wxString
& name
)
350 return wxListBox::Create(parent
, id
, pos
, size
, choices
,
351 style
| wxLB_OWNERDRAW
, validator
, name
);
354 // misc overloaded methods
355 // -----------------------
357 void wxCheckListBox::Delete(int N
)
359 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
360 wxT("invalid index in wxListBox::Delete") );
362 wxListBox::Delete(N
);
367 m_aItems
.RemoveAt(N
);
370 bool wxCheckListBox::SetFont( const wxFont
&font
)
373 for ( i
= 0; i
< m_aItems
.GetCount(); i
++ )
374 m_aItems
[i
]->SetFont(font
);
376 wxListBox::SetFont(font
);
381 // create/retrieve item
382 // --------------------
384 // create a check list box item
385 wxOwnerDrawn
*wxCheckListBox::CreateLboxItem(size_t nIndex
)
387 wxCheckListBoxItem
*pItem
= new wxCheckListBoxItem(this, nIndex
);
393 bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT
*item
)
395 if ( wxListBox::MSWOnMeasure(item
) ) {
396 MEASUREITEMSTRUCT
*pStruct
= (MEASUREITEMSTRUCT
*)item
;
399 m_nItemHeight
= pStruct
->itemHeight
;
401 // add place for the check mark
402 pStruct
->itemWidth
+= wxOwnerDrawn::GetDefaultMarginWidth();
413 bool wxCheckListBox::IsChecked(size_t uiIndex
) const
415 wxCHECK_MSG( uiIndex
< (size_t)GetCount(), false, _T("bad wxCheckListBox index") );
417 return GetItem(uiIndex
)->IsChecked();
420 void wxCheckListBox::Check(size_t uiIndex
, bool bCheck
)
422 wxCHECK_RET( uiIndex
< (size_t)GetCount(), _T("bad wxCheckListBox index") );
424 GetItem(uiIndex
)->Check(bCheck
);
430 void wxCheckListBox::OnKeyDown(wxKeyEvent
& event
)
441 switch ( event
.GetKeyCode() )
452 case WXK_NUMPAD_SUBTRACT
:
463 wxArrayInt selections
;
465 if ( HasMultipleSelection() )
467 count
= GetSelections(selections
);
471 int sel
= GetSelection();
479 for ( int i
= 0; i
< count
; i
++ )
481 wxCheckListBoxItem
*item
= GetItem(selections
[i
]);
484 wxFAIL_MSG( _T("no wxCheckListBoxItem?") );
496 item
->Check( oper
== Set
);
500 wxFAIL_MSG( _T("what should this key do?") );
503 // we should send an event as this has been done by the user and
504 // not by the program
508 else // nothing to do
514 void wxCheckListBox::OnLeftClick(wxMouseEvent
& event
)
516 // clicking on the item selects it, clicking on the checkmark toggles
517 if ( event
.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) {
518 int nItem
= HitTest(event
.GetX(), event
.GetY());
520 if ( nItem
!= wxNOT_FOUND
) {
521 wxCheckListBoxItem
*item
= GetItem(nItem
);
525 //else: it's not an error, just click outside of client zone
528 // implement default behaviour: clicking on the item selects it
533 int wxCheckListBox::DoHitTestItem(wxCoord x
, wxCoord y
) const
535 int nItem
= (int)::SendMessage
543 return nItem
>= m_noItems
? wxNOT_FOUND
: nItem
;
547 wxSize
wxCheckListBox::DoGetBestSize() const
549 wxSize best
= wxListBox::DoGetBestSize();
550 best
.x
+= wxOwnerDrawn::GetDefaultMarginWidth(); // add room for the checkbox