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"
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 #if defined(__GNUWIN32_OLD__)
56 #include "wx/msw/gnuwin32/extra.h"
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 // get item (converted to right type)
64 #define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n)))
66 // ============================================================================
67 // implementation of wxCheckListBoxBase
68 // ============================================================================
70 wxCheckListBoxBase::wxCheckListBoxBase()
74 // ============================================================================
75 // implementation of wxCheckListBox
76 // ============================================================================
79 #if wxUSE_EXTENDED_RTTI
80 WX_DEFINE_FLAGS( wxCheckListBoxStyle
)
82 wxBEGIN_FLAGS( wxCheckListBoxStyle
)
83 // new style border flags, we put them first to
84 // use them for streaming out
85 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
86 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
87 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
88 wxFLAGS_MEMBER(wxBORDER_RAISED
)
89 wxFLAGS_MEMBER(wxBORDER_STATIC
)
90 wxFLAGS_MEMBER(wxBORDER_NONE
)
92 // old style border flags
93 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
94 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
95 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
96 wxFLAGS_MEMBER(wxRAISED_BORDER
)
97 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
98 wxFLAGS_MEMBER(wxBORDER
)
100 // standard window styles
101 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
102 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
103 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
104 wxFLAGS_MEMBER(wxWANTS_CHARS
)
105 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
106 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
107 wxFLAGS_MEMBER(wxVSCROLL
)
108 wxFLAGS_MEMBER(wxHSCROLL
)
110 wxFLAGS_MEMBER(wxLB_SINGLE
)
111 wxFLAGS_MEMBER(wxLB_MULTIPLE
)
112 wxFLAGS_MEMBER(wxLB_EXTENDED
)
113 wxFLAGS_MEMBER(wxLB_HSCROLL
)
114 wxFLAGS_MEMBER(wxLB_ALWAYS_SB
)
115 wxFLAGS_MEMBER(wxLB_NEEDED_SB
)
116 wxFLAGS_MEMBER(wxLB_SORT
)
117 wxFLAGS_MEMBER(wxLB_OWNERDRAW
)
119 wxEND_FLAGS( wxCheckListBoxStyle
)
121 IMPLEMENT_DYNAMIC_CLASS_XTI(wxCheckListBox
, wxListBox
,"wx/checklst.h")
123 wxBEGIN_PROPERTIES_TABLE(wxCheckListBox
)
124 wxEVENT_PROPERTY( Toggle
, wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, wxCommandEvent
)
125 wxPROPERTY_FLAGS( WindowStyle
, wxCheckListBoxStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , wxLB_OWNERDRAW
/*flags*/ , wxT("Helpstring") , wxT("group")) // style
126 wxEND_PROPERTIES_TABLE()
128 wxBEGIN_HANDLERS_TABLE(wxCheckListBox
)
129 wxEND_HANDLERS_TABLE()
131 wxCONSTRUCTOR_4( wxCheckListBox
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
)
134 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
)
137 // ----------------------------------------------------------------------------
138 // declaration and implementation of wxCheckListBoxItem class
139 // ----------------------------------------------------------------------------
141 class wxCheckListBoxItem
: public wxOwnerDrawn
143 friend class WXDLLEXPORT wxCheckListBox
;
146 wxCheckListBoxItem(wxCheckListBox
*pParent
, size_t nIndex
);
149 virtual bool OnDrawItem(wxDC
& dc
, const wxRect
& rc
, wxODAction act
, wxODStatus stat
);
151 // simple accessors and operations
152 bool IsChecked() const { return m_bChecked
; }
154 void Check(bool bCheck
);
155 void Toggle() { Check(!IsChecked()); }
161 DECLARE_NO_COPY_CLASS(wxCheckListBoxItem
)
163 wxCheckListBox
*m_pParent
;
167 wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox
*pParent
, size_t nIndex
)
168 : wxOwnerDrawn(wxEmptyString
, TRUE
) // checkable
174 // we don't initialize m_nCheckHeight/Width vars because it's
175 // done in OnMeasure while they are used only in OnDraw and we
176 // know that there will always be OnMeasure before OnDraw
179 SetMarginWidth(GetDefaultMarginWidth());
183 * JACS - I've got the owner-draw stuff partially working with WIN16,
184 * with a really horrible-looking cross for wxCheckListBox instead of a
185 * check - could use a bitmap check-mark instead, defined in wx.rc.
186 * Also there's a refresh problem whereby it doesn't always draw the
187 * check until you click to the right of it, which is OK for WIN32.
190 bool wxCheckListBoxItem::OnDrawItem(wxDC
& dc
, const wxRect
& rc
,
191 wxODAction act
, wxODStatus stat
)
194 stat
= (wxOwnerDrawn::wxODStatus
)(stat
| wxOwnerDrawn::wxODChecked
);
196 if ( wxOwnerDrawn::OnDrawItem(dc
, rc
, act
, stat
) ) {
197 // ## using native API for performance and precision
198 size_t nCheckWidth
= GetDefaultMarginWidth(),
199 nCheckHeight
= m_pParent
->GetItemHeight();
204 HDC hdc
= (HDC
)dc
.GetHDC();
207 HPEN hpenBack
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_WINDOW
)),
208 hpenGray
= CreatePen(PS_SOLID
, 0, RGB(128, 128, 128)),
209 hpenPrev
= (HPEN
)SelectObject(hdc
, hpenBack
);
211 // we erase the 1-pixel border
212 Rectangle(hdc
, x
, y
, x
+ nCheckWidth
, y
+ nCheckHeight
);
214 // shift check mark 1 pixel to the right (it looks better like this)
218 // first create a monochrome bitmap in a memory DC
219 HDC hdcMem
= CreateCompatibleDC(hdc
);
220 HBITMAP hbmpCheck
= CreateBitmap(nCheckWidth
, nCheckHeight
, 1, 1, 0);
221 HBITMAP hbmpOld
= (HBITMAP
)SelectObject(hdcMem
, hbmpCheck
);
223 // then draw a check mark into it
228 rect
.right
= nCheckWidth
;
229 rect
.bottom
= nCheckHeight
;
232 DrawFrameControl(hdcMem
, &rect
, DFC_BUTTON
, DFCS_BUTTONCHECK
);
234 DrawFrameControl(hdcMem
, &rect
, DFC_MENU
, DFCS_MENUCHECK
);
237 // finally copy it to screen DC and clean up
238 BitBlt(hdc
, x
, y
, nCheckWidth
- 1, nCheckHeight
,
239 hdcMem
, 0, 0, SRCCOPY
);
241 SelectObject(hdcMem
, hbmpOld
);
242 DeleteObject(hbmpCheck
);
246 // now we draw the smaller rectangle
251 // draw hollow gray rectangle
252 (void)SelectObject(hdc
, hpenGray
);
253 HBRUSH hbrPrev
= (HBRUSH
)SelectObject(hdc
, GetStockObject(NULL_BRUSH
));
254 Rectangle(hdc
, x
, y
, x
+ nCheckWidth
, y
+ nCheckHeight
);
257 (void)SelectObject(hdc
, hpenPrev
);
258 (void)SelectObject(hdc
, hbrPrev
);
260 DeleteObject(hpenBack
);
261 DeleteObject(hpenGray
);
264 dc.DrawRectangle(x, y, nCheckWidth, nCheckHeight);
267 dc.DrawLine(x, y, x + nCheckWidth, y + nCheckHeight);
268 dc.DrawLine(x, y + nCheckHeight, x + nCheckWidth, y);
278 // change the state of the item and redraw it
279 void wxCheckListBoxItem::Check(bool check
)
283 // index may be changed because new items were added/deleted
284 if ( m_pParent
->GetItemIndex(this) != (int)m_nIndex
)
287 int index
= m_pParent
->GetItemIndex(this);
289 wxASSERT_MSG( index
!= wxNOT_FOUND
, wxT("what does this item do here?") );
291 m_nIndex
= (size_t)index
;
294 HWND hwndListbox
= (HWND
)m_pParent
->GetHWND();
299 if ( ::SendMessage(hwndListbox
, LB_GETITEMRECT
,
300 m_nIndex
, (LPARAM
)&rcUpdate
) == LB_ERR
)
302 wxLogDebug(wxT("LB_GETITEMRECT failed"));
305 // FIXME this doesn't work if the listbox is scrolled!
306 size_t nHeight
= m_pParent
->GetItemHeight();
307 size_t y
= m_nIndex
* nHeight
;
311 rcUpdate
.right
= GetDefaultMarginWidth() ;
312 rcUpdate
.bottom
= y
+ nHeight
;
315 InvalidateRect(hwndListbox
, &rcUpdate
, FALSE
);
318 // send an "item checked" event
319 void wxCheckListBoxItem::SendEvent()
321 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, m_pParent
->GetId());
322 event
.SetInt(m_nIndex
);
323 event
.SetEventObject(m_pParent
);
324 m_pParent
->ProcessCommand(event
);
327 // ----------------------------------------------------------------------------
328 // implementation of wxCheckListBox class
329 // ----------------------------------------------------------------------------
331 // define event table
332 // ------------------
333 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
334 EVT_KEY_DOWN(wxCheckListBox::OnKeyDown
)
335 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick
)
341 // def ctor: use Create() to really create the control
342 wxCheckListBox::wxCheckListBox()
346 // ctor which creates the associated control
347 wxCheckListBox::wxCheckListBox(wxWindow
*parent
, wxWindowID id
,
348 const wxPoint
& pos
, const wxSize
& size
,
349 int nStrings
, const wxString choices
[],
350 long style
, const wxValidator
& val
,
351 const wxString
& name
)
353 Create(parent
, id
, pos
, size
, nStrings
, choices
, style
, val
, name
);
356 bool wxCheckListBox::Create(wxWindow
*parent
, wxWindowID id
,
357 const wxPoint
& pos
, const wxSize
& size
,
358 int n
, const wxString choices
[],
360 const wxValidator
& validator
, const wxString
& name
)
362 return wxListBox::Create(parent
, id
, pos
, size
, n
, choices
,
363 style
| wxLB_OWNERDRAW
, validator
, name
);
366 // misc overloaded methods
367 // -----------------------
369 void wxCheckListBox::Delete(int N
)
371 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
372 wxT("invalid index in wxListBox::Delete") );
374 wxListBox::Delete(N
);
379 m_aItems
.RemoveAt(N
);
382 bool wxCheckListBox::SetFont( const wxFont
&font
)
385 for ( i
= 0; i
< m_aItems
.GetCount(); i
++ )
386 m_aItems
[i
]->SetFont(font
);
388 wxListBox::SetFont(font
);
393 // create/retrieve item
394 // --------------------
396 // create a check list box item
397 wxOwnerDrawn
*wxCheckListBox::CreateLboxItem(size_t nIndex
)
399 wxCheckListBoxItem
*pItem
= new wxCheckListBoxItem(this, nIndex
);
405 bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT
*item
)
407 if ( wxListBox::MSWOnMeasure(item
) ) {
408 MEASUREITEMSTRUCT
*pStruct
= (MEASUREITEMSTRUCT
*)item
;
411 m_nItemHeight
= pStruct
->itemHeight
;
413 // add place for the check mark
414 pStruct
->itemWidth
+= wxOwnerDrawn::GetDefaultMarginWidth();
425 bool wxCheckListBox::IsChecked(size_t uiIndex
) const
427 wxCHECK_MSG( uiIndex
< (size_t)GetCount(), FALSE
, _T("bad wxCheckListBox index") );
429 return GetItem(uiIndex
)->IsChecked();
432 void wxCheckListBox::Check(size_t uiIndex
, bool bCheck
)
434 wxCHECK_RET( uiIndex
< (size_t)GetCount(), _T("bad wxCheckListBox index") );
436 GetItem(uiIndex
)->Check(bCheck
);
442 void wxCheckListBox::OnKeyDown(wxKeyEvent
& event
)
453 switch ( event
.GetKeyCode() )
464 case WXK_NUMPAD_SUBTRACT
:
475 wxArrayInt selections
;
477 if ( HasMultipleSelection() )
479 count
= GetSelections(selections
);
483 int sel
= GetSelection();
491 for ( int i
= 0; i
< count
; i
++ )
493 wxCheckListBoxItem
*item
= GetItem(selections
[i
]);
496 wxFAIL_MSG( _T("no wxCheckListBoxItem?") );
508 item
->Check( oper
== Set
);
512 wxFAIL_MSG( _T("what should this key do?") );
515 // we should send an event as this has been done by the user and
516 // not by the program
520 else // nothing to do
526 void wxCheckListBox::OnLeftClick(wxMouseEvent
& event
)
528 // clicking on the item selects it, clicking on the checkmark toggles
529 if ( event
.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) {
530 int nItem
= HitTest(event
.GetX(), event
.GetY());
532 if ( nItem
!= wxNOT_FOUND
) {
533 wxCheckListBoxItem
*item
= GetItem(nItem
);
537 //else: it's not an error, just click outside of client zone
540 // implement default behaviour: clicking on the item selects it
545 int wxCheckListBox::DoHitTestItem(wxCoord x
, wxCoord y
) const
548 int nItem
= (int)::SendMessage
556 // FIXME this doesn't work when the listbox is scrolled!
557 int nItem
= y
/ m_nItemHeight
;
560 return nItem
>= m_noItems
? wxNOT_FOUND
: nItem
;