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"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 // get item (converted to right type)
56 #define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n)))
58 // ============================================================================
60 // ============================================================================
63 #if wxUSE_EXTENDED_RTTI
64 WX_DEFINE_FLAGS( wxCheckListBoxStyle
)
66 wxBEGIN_FLAGS( wxCheckListBoxStyle
)
67 // new style border flags, we put them first to
68 // use them for streaming out
69 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
70 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
71 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
72 wxFLAGS_MEMBER(wxBORDER_RAISED
)
73 wxFLAGS_MEMBER(wxBORDER_STATIC
)
74 wxFLAGS_MEMBER(wxBORDER_NONE
)
76 // old style border flags
77 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
78 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
79 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
80 wxFLAGS_MEMBER(wxRAISED_BORDER
)
81 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
82 wxFLAGS_MEMBER(wxBORDER
)
84 // standard window styles
85 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
86 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
87 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
88 wxFLAGS_MEMBER(wxWANTS_CHARS
)
89 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
90 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
91 wxFLAGS_MEMBER(wxVSCROLL
)
92 wxFLAGS_MEMBER(wxHSCROLL
)
94 wxFLAGS_MEMBER(wxLB_SINGLE
)
95 wxFLAGS_MEMBER(wxLB_MULTIPLE
)
96 wxFLAGS_MEMBER(wxLB_EXTENDED
)
97 wxFLAGS_MEMBER(wxLB_HSCROLL
)
98 wxFLAGS_MEMBER(wxLB_ALWAYS_SB
)
99 wxFLAGS_MEMBER(wxLB_NEEDED_SB
)
100 wxFLAGS_MEMBER(wxLB_SORT
)
101 wxFLAGS_MEMBER(wxLB_OWNERDRAW
)
103 wxEND_FLAGS( wxCheckListBoxStyle
)
105 IMPLEMENT_DYNAMIC_CLASS_XTI(wxCheckListBox
, wxListBox
,"wx/checklst.h")
107 wxBEGIN_PROPERTIES_TABLE(wxCheckListBox
)
108 wxEVENT_PROPERTY( Toggle
, wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, wxCommandEvent
)
109 wxPROPERTY_FLAGS( WindowStyle
, wxCheckListBoxStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, wxLB_OWNERDRAW
/*flags*/ , wxT("Helpstring") , wxT("group")) // style
110 wxEND_PROPERTIES_TABLE()
112 wxBEGIN_HANDLERS_TABLE(wxCheckListBox
)
113 wxEND_HANDLERS_TABLE()
115 wxCONSTRUCTOR_4( wxCheckListBox
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
)
118 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
)
121 // ----------------------------------------------------------------------------
122 // declaration and implementation of wxCheckListBoxItem class
123 // ----------------------------------------------------------------------------
125 class wxCheckListBoxItem
: public wxOwnerDrawn
127 friend class WXDLLIMPEXP_FWD_CORE wxCheckListBox
;
130 wxCheckListBoxItem(wxCheckListBox
*pParent
, size_t nIndex
);
133 virtual bool OnDrawItem(wxDC
& dc
, const wxRect
& rc
, wxODAction act
, wxODStatus stat
);
135 // simple accessors and operations
136 bool IsChecked() const { return m_bChecked
; }
138 void Check(bool bCheck
);
139 void Toggle() { Check(!IsChecked()); }
145 wxCheckListBox
*m_pParent
;
148 DECLARE_NO_COPY_CLASS(wxCheckListBoxItem
)
151 wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox
*pParent
, size_t nIndex
)
152 : wxOwnerDrawn(wxEmptyString
, true) // checkable
158 // we don't initialize m_nCheckHeight/Width vars because it's
159 // done in OnMeasure while they are used only in OnDraw and we
160 // know that there will always be OnMeasure before OnDraw
162 SetMarginWidth(::GetSystemMetrics(SM_CXMENUCHECK
) - 2);
164 SetBackgroundColour(pParent
->GetBackgroundColour());
167 bool wxCheckListBoxItem::OnDrawItem(wxDC
& dc
, const wxRect
& rc
,
168 wxODAction act
, wxODStatus stat
)
170 // first draw the label
172 stat
= (wxOwnerDrawn::wxODStatus
)(stat
| wxOwnerDrawn::wxODChecked
);
174 if ( !wxOwnerDrawn::OnDrawItem(dc
, rc
, act
, stat
) )
177 // now draw the check mark part
178 HDC hdc
= GetHdcOf(dc
);
180 int nBmpWidth
= ::GetSystemMetrics(SM_CXMENUCHECK
),
181 nBmpHeight
= ::GetSystemMetrics(SM_CYMENUCHECK
);
184 // first create bitmap in a memory DC
185 MemoryHDC
hdcMem(hdc
);
186 CompatibleBitmap
hBmpCheck(hdc
, nBmpWidth
, nBmpHeight
);
188 // then draw a check mark into it
190 SelectInHDC
selBmp(hdcMem
, hBmpCheck
);
192 int flags
= wxCONTROL_FLAT
;
194 flags
|= wxCONTROL_CHECKED
;
196 wxDCTemp
dcMem(hdcMem
);
197 wxRendererNative::Get().DrawCheckBox(
198 m_pParent
, dcMem
, wxRect(0, 0, nBmpWidth
, nBmpHeight
), flags
);
199 } // select hBmpCheck out of hdcMem
201 // shift check mark 2 pixel to the right and bottom, looks better like this
202 int x
= rc
.GetX() + 2,
205 // finally draw bitmap to screen: uses image list functions to blend
206 // the bitmap with the background colour (better for the selected items)
207 HIMAGELIST himl
= ImageList_Create(nBmpWidth
, nBmpHeight
,
208 ILC_COLOR32
| ILC_MASK
, 1, 1);
209 ImageList_Add(himl
, hBmpCheck
, NULL
);
211 UINT fStyle
= stat
& wxOwnerDrawn::wxODSelected
? ILD_SELECTED
: ILD_NORMAL
;
212 ImageList_Draw(himl
, 0, hdc
, x
, y
, fStyle
);
214 ImageList_Destroy(himl
);
219 // change the state of the item and redraw it
220 void wxCheckListBoxItem::Check(bool check
)
224 // index may be changed because new items were added/deleted
225 if ( m_pParent
->GetItemIndex(this) != (int)m_nIndex
)
228 int index
= m_pParent
->GetItemIndex(this);
230 wxASSERT_MSG( index
!= wxNOT_FOUND
, wxT("what does this item do here?") );
232 m_nIndex
= (size_t)index
;
235 HWND hwndListbox
= (HWND
)m_pParent
->GetHWND();
239 if ( ::SendMessage(hwndListbox
, LB_GETITEMRECT
,
240 m_nIndex
, (LPARAM
)&rcUpdate
) == LB_ERR
)
242 wxLogDebug(wxT("LB_GETITEMRECT failed"));
245 ::InvalidateRect(hwndListbox
, &rcUpdate
, FALSE
);
248 // send an "item checked" event
249 void wxCheckListBoxItem::SendEvent()
251 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, m_pParent
->GetId());
252 event
.SetInt(m_nIndex
);
253 event
.SetEventObject(m_pParent
);
254 event
.SetString(m_pParent
->GetString(m_nIndex
));
255 m_pParent
->ProcessCommand(event
);
258 // ----------------------------------------------------------------------------
259 // implementation of wxCheckListBox class
260 // ----------------------------------------------------------------------------
262 // define event table
263 // ------------------
264 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
265 EVT_KEY_DOWN(wxCheckListBox::OnKeyDown
)
266 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick
)
272 // def ctor: use Create() to really create the control
273 wxCheckListBox::wxCheckListBox()
277 // ctor which creates the associated control
278 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
279 const wxPoint
& pos
, const wxSize
& size
,
280 int nStrings
, const wxString choices
[],
281 long style
, const wxValidator
& val
,
282 const wxString
& name
)
284 Create(parent
, id
, pos
, size
, nStrings
, choices
, style
, val
, name
);
287 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
288 const wxPoint
& pos
, const wxSize
& size
,
289 const wxArrayString
& choices
,
290 long style
, const wxValidator
& val
,
291 const wxString
& name
)
293 Create(parent
, id
, pos
, size
, choices
, style
, val
, name
);
296 bool wxCheckListBox::Create(wxWindow
*parent
, wxWindowID id
,
297 const wxPoint
& pos
, const wxSize
& size
,
298 int n
, const wxString choices
[],
300 const wxValidator
& validator
, const wxString
& name
)
302 return wxListBox::Create(parent
, id
, pos
, size
, n
, choices
,
303 style
| wxLB_OWNERDRAW
, validator
, name
);
306 bool wxCheckListBox::Create(wxWindow
*parent
, wxWindowID id
,
307 const wxPoint
& pos
, const wxSize
& size
,
308 const wxArrayString
& choices
,
310 const wxValidator
& validator
, const wxString
& name
)
312 return wxListBox::Create(parent
, id
, pos
, size
, choices
,
313 style
| wxLB_OWNERDRAW
, validator
, name
);
316 // misc overloaded methods
317 // -----------------------
319 void wxCheckListBox::Delete(unsigned int n
)
321 wxCHECK_RET( IsValid(n
),
322 wxT("invalid index in wxListBox::Delete") );
324 wxListBox::Delete(n
);
329 m_aItems
.RemoveAt(n
);
332 bool wxCheckListBox::SetFont( const wxFont
&font
)
335 for ( i
= 0; i
< m_aItems
.GetCount(); i
++ )
336 m_aItems
[i
]->SetFont(font
);
338 wxListBox::SetFont(font
);
343 // create/retrieve item
344 // --------------------
346 // create a check list box item
347 wxOwnerDrawn
*wxCheckListBox::CreateLboxItem(size_t nIndex
)
349 wxCheckListBoxItem
*pItem
= new wxCheckListBoxItem(this, nIndex
);
355 bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT
*item
)
357 if ( wxListBox::MSWOnMeasure(item
) ) {
358 MEASUREITEMSTRUCT
*pStruct
= (MEASUREITEMSTRUCT
*)item
;
360 // add place for the check mark
361 pStruct
->itemWidth
+= wxOwnerDrawn::GetDefaultMarginWidth();
362 pStruct
->itemHeight
+= 1;
365 m_nItemHeight
= pStruct
->itemHeight
;
376 bool wxCheckListBox::IsChecked(unsigned int uiIndex
) const
378 wxCHECK_MSG( IsValid(uiIndex
), false, _T("bad wxCheckListBox index") );
380 return GetItem(uiIndex
)->IsChecked();
383 void wxCheckListBox::Check(unsigned int uiIndex
, bool bCheck
)
385 wxCHECK_RET( IsValid(uiIndex
), _T("bad wxCheckListBox index") );
387 GetItem(uiIndex
)->Check(bCheck
);
393 void wxCheckListBox::OnKeyDown(wxKeyEvent
& event
)
404 switch ( event
.GetKeyCode() )
415 case WXK_NUMPAD_SUBTRACT
:
426 wxArrayInt selections
;
428 if ( HasMultipleSelection() )
430 count
= GetSelections(selections
);
434 int sel
= GetSelection();
442 for ( int i
= 0; i
< count
; i
++ )
444 wxCheckListBoxItem
*item
= GetItem(selections
[i
]);
447 wxFAIL_MSG( _T("no wxCheckListBoxItem?") );
459 item
->Check( oper
== Set
);
463 wxFAIL_MSG( _T("what should this key do?") );
466 // we should send an event as this has been done by the user and
467 // not by the program
471 else // nothing to do
477 void wxCheckListBox::OnLeftClick(wxMouseEvent
& event
)
479 // clicking on the item selects it, clicking on the checkmark toggles
480 if ( event
.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() )
482 int nItem
= HitTest(event
.GetX(), event
.GetY());
484 if ( nItem
!= wxNOT_FOUND
)
486 wxCheckListBoxItem
*item
= GetItem(nItem
);
490 //else: it's not an error, just click outside of client zone
494 // implement default behaviour: clicking on the item selects it
499 int wxCheckListBox::DoHitTestItem(wxCoord x
, wxCoord y
) const
501 int nItem
= (int)::SendMessage
509 return nItem
>= (int)m_noItems
? wxNOT_FOUND
: nItem
;
513 wxSize
wxCheckListBox::DoGetBestSize() const
515 wxSize best
= wxListBox::DoGetBestSize();
516 best
.x
+= wxOwnerDrawn::GetDefaultMarginWidth(); // add room for the checkbox