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 wxCheckListBox
*m_pParent
;
152 DECLARE_NO_COPY_CLASS(wxCheckListBoxItem
)
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
166 // fix appearance for check list boxes: they don't look quite the same as
168 SetMarginWidth(::GetSystemMetrics(SM_CXMENUCHECK
) -
169 2*wxSystemSettings::GetMetric(wxSYS_EDGE_X
) + 1);
170 SetBackgroundColour(pParent
->GetBackgroundColour());
173 bool wxCheckListBoxItem::OnDrawItem(wxDC
& dc
, const wxRect
& rc
,
174 wxODAction act
, wxODStatus stat
)
176 // first draw the label
178 stat
= (wxOwnerDrawn::wxODStatus
)(stat
| wxOwnerDrawn::wxODChecked
);
180 if ( !wxOwnerDrawn::OnDrawItem(dc
, rc
, act
, stat
) )
184 // now draw the check mark part
185 size_t nCheckWidth
= GetDefaultMarginWidth(),
186 nCheckHeight
= m_pParent
->GetItemHeight();
191 HDC hdc
= (HDC
)dc
.GetHDC();
193 // create pens, brushes &c
194 COLORREF colBg
= ::GetSysColor(COLOR_WINDOW
);
195 AutoHPEN
hpenBack(colBg
),
196 hpenGray(RGB(0xc0, 0xc0, 0xc0));
198 SelectInHDC
selPen(hdc
, (HGDIOBJ
)hpenBack
);
199 AutoHBRUSH
hbrBack(colBg
);
200 SelectInHDC
selBrush(hdc
, hbrBack
);
202 // erase the background: it could have been filled with the selected colour
203 Rectangle(hdc
, x
, y
, x
+ nCheckWidth
+ 1, rc
.GetBottom() + 1);
205 // shift check mark 1 pixel to the right, looks better like this
210 // first create a monochrome bitmap in a memory DC
211 MemoryHDC
hdcMem(hdc
);
212 MonoBitmap
hbmpCheck(nCheckWidth
, nCheckHeight
);
213 SelectInHDC
selBmp(hdcMem
, hbmpCheck
);
215 // then draw a check mark into it
216 RECT rect
= { 0, 0, nCheckWidth
, nCheckHeight
};
217 ::DrawFrameControl(hdcMem
, &rect
,
219 DFC_BUTTON
, DFCS_BUTTONCHECK
221 DFC_MENU
, DFCS_MENUCHECK
225 // finally copy it to screen DC
226 ::BitBlt(hdc
, x
, y
, nCheckWidth
, nCheckHeight
, hdcMem
, 0, 0, SRCCOPY
);
229 // now we draw the smaller rectangle
234 // draw hollow gray rectangle
235 (void)::SelectObject(hdc
, (HGDIOBJ
)hpenGray
);
237 SelectInHDC
selBrush2(hdc
, ::GetStockObject(NULL_BRUSH
));
238 Rectangle(hdc
, x
, y
, x
+ nCheckWidth
, y
+ nCheckHeight
);
243 // change the state of the item and redraw it
244 void wxCheckListBoxItem::Check(bool check
)
248 // index may be changed because new items were added/deleted
249 if ( m_pParent
->GetItemIndex(this) != (int)m_nIndex
)
252 int index
= m_pParent
->GetItemIndex(this);
254 wxASSERT_MSG( index
!= wxNOT_FOUND
, wxT("what does this item do here?") );
256 m_nIndex
= (size_t)index
;
259 HWND hwndListbox
= (HWND
)m_pParent
->GetHWND();
263 if ( ::SendMessage(hwndListbox
, LB_GETITEMRECT
,
264 m_nIndex
, (LPARAM
)&rcUpdate
) == LB_ERR
)
266 wxLogDebug(wxT("LB_GETITEMRECT failed"));
269 ::InvalidateRect(hwndListbox
, &rcUpdate
, FALSE
);
272 // send an "item checked" event
273 void wxCheckListBoxItem::SendEvent()
275 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, m_pParent
->GetId());
276 event
.SetInt(m_nIndex
);
277 event
.SetEventObject(m_pParent
);
278 m_pParent
->ProcessCommand(event
);
281 // ----------------------------------------------------------------------------
282 // implementation of wxCheckListBox class
283 // ----------------------------------------------------------------------------
285 // define event table
286 // ------------------
287 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
288 EVT_KEY_DOWN(wxCheckListBox::OnKeyDown
)
289 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick
)
295 // def ctor: use Create() to really create the control
296 wxCheckListBox::wxCheckListBox()
300 // ctor which creates the associated control
301 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
302 const wxPoint
& pos
, const wxSize
& size
,
303 int nStrings
, const wxString choices
[],
304 long style
, const wxValidator
& val
,
305 const wxString
& name
)
307 Create(parent
, id
, pos
, size
, nStrings
, choices
, style
, val
, name
);
310 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
311 const wxPoint
& pos
, const wxSize
& size
,
312 const wxArrayString
& choices
,
313 long style
, const wxValidator
& val
,
314 const wxString
& name
)
316 Create(parent
, id
, pos
, size
, choices
, style
, val
, name
);
319 bool wxCheckListBox::Create(wxWindow
*parent
, wxWindowID id
,
320 const wxPoint
& pos
, const wxSize
& size
,
321 int n
, const wxString choices
[],
323 const wxValidator
& validator
, const wxString
& name
)
325 return wxListBox::Create(parent
, id
, pos
, size
, n
, choices
,
326 style
| wxLB_OWNERDRAW
, validator
, name
);
329 bool wxCheckListBox::Create(wxWindow
*parent
, wxWindowID id
,
330 const wxPoint
& pos
, const wxSize
& size
,
331 const wxArrayString
& choices
,
333 const wxValidator
& validator
, const wxString
& name
)
335 return wxListBox::Create(parent
, id
, pos
, size
, choices
,
336 style
| wxLB_OWNERDRAW
, validator
, name
);
339 // misc overloaded methods
340 // -----------------------
342 void wxCheckListBox::Delete(int N
)
344 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
345 wxT("invalid index in wxListBox::Delete") );
347 wxListBox::Delete(N
);
352 m_aItems
.RemoveAt(N
);
355 bool wxCheckListBox::SetFont( const wxFont
&font
)
358 for ( i
= 0; i
< m_aItems
.GetCount(); i
++ )
359 m_aItems
[i
]->SetFont(font
);
361 wxListBox::SetFont(font
);
366 // create/retrieve item
367 // --------------------
369 // create a check list box item
370 wxOwnerDrawn
*wxCheckListBox::CreateLboxItem(size_t nIndex
)
372 wxCheckListBoxItem
*pItem
= new wxCheckListBoxItem(this, nIndex
);
378 bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT
*item
)
380 if ( wxListBox::MSWOnMeasure(item
) ) {
381 MEASUREITEMSTRUCT
*pStruct
= (MEASUREITEMSTRUCT
*)item
;
384 m_nItemHeight
= pStruct
->itemHeight
;
386 // add place for the check mark
387 pStruct
->itemWidth
+= wxOwnerDrawn::GetDefaultMarginWidth();
398 bool wxCheckListBox::IsChecked(size_t uiIndex
) const
400 wxCHECK_MSG( uiIndex
< (size_t)GetCount(), false, _T("bad wxCheckListBox index") );
402 return GetItem(uiIndex
)->IsChecked();
405 void wxCheckListBox::Check(size_t uiIndex
, bool bCheck
)
407 wxCHECK_RET( uiIndex
< (size_t)GetCount(), _T("bad wxCheckListBox index") );
409 GetItem(uiIndex
)->Check(bCheck
);
415 void wxCheckListBox::OnKeyDown(wxKeyEvent
& event
)
426 switch ( event
.GetKeyCode() )
437 case WXK_NUMPAD_SUBTRACT
:
448 wxArrayInt selections
;
450 if ( HasMultipleSelection() )
452 count
= GetSelections(selections
);
456 int sel
= GetSelection();
464 for ( int i
= 0; i
< count
; i
++ )
466 wxCheckListBoxItem
*item
= GetItem(selections
[i
]);
469 wxFAIL_MSG( _T("no wxCheckListBoxItem?") );
481 item
->Check( oper
== Set
);
485 wxFAIL_MSG( _T("what should this key do?") );
488 // we should send an event as this has been done by the user and
489 // not by the program
493 else // nothing to do
499 void wxCheckListBox::OnLeftClick(wxMouseEvent
& event
)
501 // clicking on the item selects it, clicking on the checkmark toggles
502 if ( event
.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) {
503 int nItem
= HitTest(event
.GetX(), event
.GetY());
505 if ( nItem
!= wxNOT_FOUND
) {
506 wxCheckListBoxItem
*item
= GetItem(nItem
);
510 //else: it's not an error, just click outside of client zone
513 // implement default behaviour: clicking on the item selects it
518 int wxCheckListBox::DoHitTestItem(wxCoord x
, wxCoord y
) const
520 int nItem
= (int)::SendMessage
528 return nItem
>= m_noItems
? wxNOT_FOUND
: nItem
;
532 wxSize
wxCheckListBox::DoGetBestSize() const
534 wxSize best
= wxListBox::DoGetBestSize();
535 best
.x
+= wxOwnerDrawn::GetDefaultMarginWidth(); // add room for the checkbox