1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_CHECKLISTBOX && wxUSE_OWNER_DRAWN
29 #include "wx/checklst.h"
32 #include "wx/msw/wrapcctl.h"
33 #include "wx/object.h"
34 #include "wx/colour.h"
36 #include "wx/bitmap.h"
37 #include "wx/window.h"
38 #include "wx/listbox.h"
39 #include "wx/dcmemory.h"
40 #include "wx/settings.h"
44 #include "wx/ownerdrw.h"
48 #include "wx/renderer.h"
49 #include "wx/msw/private.h"
50 #include "wx/msw/dc.h"
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 // get item (converted to right type)
57 #define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n)))
59 // ============================================================================
61 // ============================================================================
64 #if wxUSE_EXTENDED_RTTI
65 WX_DEFINE_FLAGS( wxCheckListBoxStyle
)
67 wxBEGIN_FLAGS( wxCheckListBoxStyle
)
68 // new style border flags, we put them first to
69 // use them for streaming out
70 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
71 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
72 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
73 wxFLAGS_MEMBER(wxBORDER_RAISED
)
74 wxFLAGS_MEMBER(wxBORDER_STATIC
)
75 wxFLAGS_MEMBER(wxBORDER_NONE
)
77 // old style border flags
78 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
79 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
80 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
81 wxFLAGS_MEMBER(wxRAISED_BORDER
)
82 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
83 wxFLAGS_MEMBER(wxBORDER
)
85 // standard window styles
86 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
87 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
88 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
89 wxFLAGS_MEMBER(wxWANTS_CHARS
)
90 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
91 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
92 wxFLAGS_MEMBER(wxVSCROLL
)
93 wxFLAGS_MEMBER(wxHSCROLL
)
95 wxFLAGS_MEMBER(wxLB_SINGLE
)
96 wxFLAGS_MEMBER(wxLB_MULTIPLE
)
97 wxFLAGS_MEMBER(wxLB_EXTENDED
)
98 wxFLAGS_MEMBER(wxLB_HSCROLL
)
99 wxFLAGS_MEMBER(wxLB_ALWAYS_SB
)
100 wxFLAGS_MEMBER(wxLB_NEEDED_SB
)
101 wxFLAGS_MEMBER(wxLB_SORT
)
102 wxFLAGS_MEMBER(wxLB_OWNERDRAW
)
104 wxEND_FLAGS( wxCheckListBoxStyle
)
106 IMPLEMENT_DYNAMIC_CLASS_XTI(wxCheckListBox
, wxListBox
,"wx/checklst.h")
108 wxBEGIN_PROPERTIES_TABLE(wxCheckListBox
)
109 wxEVENT_PROPERTY( Toggle
, wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, wxCommandEvent
)
110 wxPROPERTY_FLAGS( WindowStyle
, wxCheckListBoxStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, wxLB_OWNERDRAW
/*flags*/ , wxT("Helpstring") , wxT("group")) // style
111 wxEND_PROPERTIES_TABLE()
113 wxBEGIN_HANDLERS_TABLE(wxCheckListBox
)
114 wxEND_HANDLERS_TABLE()
116 wxCONSTRUCTOR_4( wxCheckListBox
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
)
119 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
)
122 // ----------------------------------------------------------------------------
123 // declaration and implementation of wxCheckListBoxItem class
124 // ----------------------------------------------------------------------------
126 class wxCheckListBoxItem
: public wxOwnerDrawn
128 friend class WXDLLIMPEXP_FWD_CORE wxCheckListBox
;
131 wxCheckListBoxItem(wxCheckListBox
*pParent
, size_t nIndex
);
134 virtual bool OnDrawItem(wxDC
& dc
, const wxRect
& rc
, wxODAction act
, wxODStatus stat
);
136 // simple accessors and operations
137 bool IsChecked() const { return m_bChecked
; }
139 void Check(bool bCheck
);
140 void Toggle() { Check(!IsChecked()); }
146 wxCheckListBox
*m_pParent
;
149 DECLARE_NO_COPY_CLASS(wxCheckListBoxItem
)
152 wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox
*pParent
, size_t nIndex
)
153 : wxOwnerDrawn(wxEmptyString
, true) // checkable
159 // we don't initialize m_nCheckHeight/Width vars because it's
160 // done in OnMeasure while they are used only in OnDraw and we
161 // know that there will always be OnMeasure before OnDraw
163 SetMarginWidth(::GetSystemMetrics(SM_CXMENUCHECK
) - 2);
165 SetBackgroundColour(pParent
->GetBackgroundColour());
168 bool wxCheckListBoxItem::OnDrawItem(wxDC
& dc
, const wxRect
& rc
,
169 wxODAction act
, wxODStatus stat
)
171 // first draw the label
173 stat
= (wxOwnerDrawn::wxODStatus
)(stat
| wxOwnerDrawn::wxODChecked
);
175 if ( !wxOwnerDrawn::OnDrawItem(dc
, rc
, act
, stat
) )
178 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
179 // now draw the check mark part
180 HDC hdc
= GetHdcOf(*impl
);
182 int nBmpWidth
= ::GetSystemMetrics(SM_CXMENUCHECK
),
183 nBmpHeight
= ::GetSystemMetrics(SM_CYMENUCHECK
);
186 // first create bitmap in a memory DC
187 MemoryHDC
hdcMem(hdc
);
188 CompatibleBitmap
hBmpCheck(hdc
, nBmpWidth
, nBmpHeight
);
190 // then draw a check mark into it
192 SelectInHDC
selBmp(hdcMem
, hBmpCheck
);
194 int flags
= wxCONTROL_FLAT
;
196 flags
|= wxCONTROL_CHECKED
;
198 wxDCTemp
dcMem(hdcMem
);
199 wxRendererNative::Get().DrawCheckBox(
200 m_pParent
, dcMem
, wxRect(0, 0, nBmpWidth
, nBmpHeight
), flags
);
201 } // select hBmpCheck out of hdcMem
203 // shift check mark 2 pixel to the right and bottom, looks better like this
204 int x
= rc
.GetX() + 2,
207 // finally draw bitmap to screen: uses image list functions to blend
208 // the bitmap with the background colour (better for the selected items)
209 HIMAGELIST himl
= ImageList_Create(nBmpWidth
, nBmpHeight
,
210 ILC_COLOR32
| ILC_MASK
, 1, 1);
211 ImageList_Add(himl
, hBmpCheck
, NULL
);
213 UINT fStyle
= stat
& wxOwnerDrawn::wxODSelected
? ILD_SELECTED
: ILD_NORMAL
;
214 ImageList_Draw(himl
, 0, hdc
, x
, y
, fStyle
);
216 ImageList_Destroy(himl
);
221 // change the state of the item and redraw it
222 void wxCheckListBoxItem::Check(bool check
)
226 // index may be changed because new items were added/deleted
227 if ( m_pParent
->GetItemIndex(this) != (int)m_nIndex
)
230 int index
= m_pParent
->GetItemIndex(this);
232 wxASSERT_MSG( index
!= wxNOT_FOUND
, wxT("what does this item do here?") );
234 m_nIndex
= (size_t)index
;
237 HWND hwndListbox
= (HWND
)m_pParent
->GetHWND();
241 if ( ::SendMessage(hwndListbox
, LB_GETITEMRECT
,
242 m_nIndex
, (LPARAM
)&rcUpdate
) == LB_ERR
)
244 wxLogDebug(wxT("LB_GETITEMRECT failed"));
247 ::InvalidateRect(hwndListbox
, &rcUpdate
, FALSE
);
250 // send an "item checked" event
251 void wxCheckListBoxItem::SendEvent()
253 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, m_pParent
->GetId());
254 event
.SetInt(m_nIndex
);
255 event
.SetEventObject(m_pParent
);
256 event
.SetString(m_pParent
->GetString(m_nIndex
));
257 m_pParent
->ProcessCommand(event
);
260 // ----------------------------------------------------------------------------
261 // implementation of wxCheckListBox class
262 // ----------------------------------------------------------------------------
264 // define event table
265 // ------------------
266 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
267 EVT_KEY_DOWN(wxCheckListBox::OnKeyDown
)
268 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick
)
274 // def ctor: use Create() to really create the control
275 wxCheckListBox::wxCheckListBox()
279 // ctor which creates the associated control
280 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
281 const wxPoint
& pos
, const wxSize
& size
,
282 int nStrings
, const wxString choices
[],
283 long style
, const wxValidator
& val
,
284 const wxString
& name
)
286 Create(parent
, id
, pos
, size
, nStrings
, choices
, style
, val
, name
);
289 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
290 const wxPoint
& pos
, const wxSize
& size
,
291 const wxArrayString
& choices
,
292 long style
, const wxValidator
& val
,
293 const wxString
& name
)
295 Create(parent
, id
, pos
, size
, choices
, style
, val
, name
);
298 bool wxCheckListBox::Create(wxWindow
*parent
, wxWindowID id
,
299 const wxPoint
& pos
, const wxSize
& size
,
300 int n
, const wxString choices
[],
302 const wxValidator
& validator
, const wxString
& name
)
304 return wxListBox::Create(parent
, id
, pos
, size
, n
, choices
,
305 style
| wxLB_OWNERDRAW
, validator
, name
);
308 bool wxCheckListBox::Create(wxWindow
*parent
, wxWindowID id
,
309 const wxPoint
& pos
, const wxSize
& size
,
310 const wxArrayString
& choices
,
312 const wxValidator
& validator
, const wxString
& name
)
314 return wxListBox::Create(parent
, id
, pos
, size
, choices
,
315 style
| wxLB_OWNERDRAW
, validator
, name
);
318 // misc overloaded methods
319 // -----------------------
321 void wxCheckListBox::Delete(unsigned int n
)
323 wxCHECK_RET( IsValid(n
),
324 wxT("invalid index in wxListBox::Delete") );
326 wxListBox::Delete(n
);
331 m_aItems
.RemoveAt(n
);
334 bool wxCheckListBox::SetFont( const wxFont
&font
)
337 for ( i
= 0; i
< m_aItems
.GetCount(); i
++ )
338 m_aItems
[i
]->SetFont(font
);
340 wxListBox::SetFont(font
);
345 // create/retrieve item
346 // --------------------
348 // create a check list box item
349 wxOwnerDrawn
*wxCheckListBox::CreateLboxItem(size_t nIndex
)
351 wxCheckListBoxItem
*pItem
= new wxCheckListBoxItem(this, nIndex
);
357 bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT
*item
)
359 if ( wxListBox::MSWOnMeasure(item
) ) {
360 MEASUREITEMSTRUCT
*pStruct
= (MEASUREITEMSTRUCT
*)item
;
362 // add place for the check mark
363 pStruct
->itemWidth
+= wxOwnerDrawn::GetDefaultMarginWidth();
364 pStruct
->itemHeight
+= 1;
367 m_nItemHeight
= pStruct
->itemHeight
;
378 bool wxCheckListBox::IsChecked(unsigned int uiIndex
) const
380 wxCHECK_MSG( IsValid(uiIndex
), false, _T("bad wxCheckListBox index") );
382 return GetItem(uiIndex
)->IsChecked();
385 void wxCheckListBox::Check(unsigned int uiIndex
, bool bCheck
)
387 wxCHECK_RET( IsValid(uiIndex
), _T("bad wxCheckListBox index") );
389 GetItem(uiIndex
)->Check(bCheck
);
395 void wxCheckListBox::OnKeyDown(wxKeyEvent
& event
)
406 switch ( event
.GetKeyCode() )
417 case WXK_NUMPAD_SUBTRACT
:
428 wxArrayInt selections
;
430 if ( HasMultipleSelection() )
432 count
= GetSelections(selections
);
436 int sel
= GetSelection();
444 for ( int i
= 0; i
< count
; i
++ )
446 wxCheckListBoxItem
*item
= GetItem(selections
[i
]);
449 wxFAIL_MSG( _T("no wxCheckListBoxItem?") );
461 item
->Check( oper
== Set
);
465 wxFAIL_MSG( _T("what should this key do?") );
468 // we should send an event as this has been done by the user and
469 // not by the program
473 else // nothing to do
479 void wxCheckListBox::OnLeftClick(wxMouseEvent
& event
)
481 // clicking on the item selects it, clicking on the checkmark toggles
482 if ( event
.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() )
484 int nItem
= HitTest(event
.GetX(), event
.GetY());
486 if ( nItem
!= wxNOT_FOUND
)
488 // people expect to get "kill focus" event for the currently
489 // focused control before getting events from the other controls
490 // and, equally importantly, they may prevent the focus change from
491 // taking place at all (e.g. because the old control contents is
492 // invalid and needs to be corrected) in which case we shouldn't
493 // generate this event at all
495 if ( FindFocus() == this )
497 wxCheckListBoxItem
*item
= GetItem(nItem
);
502 //else: it's not an error, just click outside of client zone
506 // implement default behaviour: clicking on the item selects it
511 int wxCheckListBox::DoHitTestItem(wxCoord x
, wxCoord y
) const
513 int nItem
= (int)::SendMessage
521 return nItem
>= (int)m_noItems
? wxNOT_FOUND
: nItem
;
525 wxSize
wxCheckListBox::DoGetBestSize() const
527 wxSize best
= wxListBox::DoGetBestSize();
528 best
.x
+= wxOwnerDrawn::GetDefaultMarginWidth(); // add room for the checkbox