1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: implementation of wxCheckListBox class
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
13 // headers & declarations
14 // ============================================================================
17 #pragma implementation "checklst.h"
22 #if wxUSE_CHECKLISTBOX
24 #include "wx/checklst.h"
26 #include "wx/mac/uma.h"
27 #include "Appearance.h"
29 // ============================================================================
30 // implementation of wxCheckListBox
31 // ============================================================================
33 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
)
35 const short kwxMacListWithVerticalScrollbar
= 128 ;
36 const short kwxMacListItemHeight
= 14 ;
37 const short kwxMacListCheckboxWidth
= 14 ;
40 unsigned short instruction
;
42 } ldefRec
, *ldefPtr
, **ldefHandle
;
46 static pascal void wxMacCheckListDefinition( short message
, Boolean isSelected
, Rect
*drawRect
,
47 Cell cell
, short dataOffset
, short dataLength
,
48 ListHandle listHandle
) ;
51 static pascal void wxMacCheckListDefinition( short message
, Boolean isSelected
, Rect
*drawRect
,
52 Cell cell
, short dataOffset
, short dataLength
,
53 ListHandle listHandle
)
57 RgnHandle savedClipRegion
;
61 SetPort((**listHandle
).port
);
62 grafPtr
= (**listHandle
).port
;
63 // typecast our refCon
64 list
= (wxCheckListBox
*) GetControlReference( (ControlHandle
) GetListRefCon(listHandle
) );
66 // Calculate the cell rect.
77 const wxString text
= list
->m_stringArray
[cell
.v
] ;
78 int checked
= list
->m_checks
[cell
.v
] ;
80 // Save the current clip region, and set the clip region to the area we are about
83 savedClipRegion
= NewRgn();
84 GetClip( savedClipRegion
);
87 EraseRect( drawRect
);
89 ::TextFont( kFontIDMonaco
) ;
92 ThemeButtonDrawInfo info
;
93 info
.state
= kThemeStateActive
;
94 info
.value
= checked
? kThemeButtonOn
: kThemeButtonOff
;
95 info
.adornment
= kThemeAdornmentNone
;
96 Rect checkRect
= *drawRect
;
99 checkRect
.right
= checkRect
.left
+ 12 ;
100 checkRect
.bottom
= checkRect
.top
+ 10 ;
101 DrawThemeButton(&checkRect
,kThemeCheckBox
,
102 &info
,NULL
,NULL
, NULL
,0);
104 MoveTo(drawRect
->left
+ 4 + kwxMacListCheckboxWidth
, drawRect
->top
+ 10 );
106 DrawText(text
, 0 , text
.Length());
107 // If the cell is hilited, do the hilite now. Paint the cell contents with the
108 // appropriate QuickDraw transform mode.
111 savedPenMode
= GetPortPenMode( (CGrafPtr
) grafPtr
);
112 SetPortPenMode( (CGrafPtr
) grafPtr
, hilitetransfermode
);
113 PaintRect( drawRect
);
114 SetPortPenMode( (CGrafPtr
) grafPtr
, savedPenMode
);
117 // Restore the saved clip region.
119 SetClip( savedClipRegion
);
120 DisposeRgn( savedClipRegion
);
125 // Hilite or unhilite the cell. Paint the cell contents with the
126 // appropriate QuickDraw transform mode.
129 savedPenMode
= GetPortPenMode( (CGrafPtr
) grafPtr
);
130 SetPortPenMode( (CGrafPtr
) grafPtr
, hilitetransfermode
);
131 PaintRect( drawRect
);
132 SetPortPenMode( (CGrafPtr
) grafPtr
, savedPenMode
);
140 extern "C" void MacDrawStringCell(Rect
*cellRect
, Cell lCell
, ListHandle theList
, long refCon
) ;
142 static ListDefUPP macCheckListDefUPP
= NULL
;
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
148 void wxCheckListBox::Init()
152 bool wxCheckListBox::Create(wxWindow
*parent
,
157 const wxString choices
[],
159 const wxValidator
& validator
,
160 const wxString
&name
)
162 m_noItems
= 0 ; // this will be increased by our append command
168 MacPreControlCreate( parent
, id
, "" , pos
, size
,style
, validator
, name
, &bounds
, title
) ;
171 listDef
.defType
= kListDefUserProcType
;
172 if ( macCheckListDefUPP
== NULL
)
174 macCheckListDefUPP
= NewListDefUPP( wxMacCheckListDefinition
);
176 listDef
.u
.userProc
= macCheckListDefUPP
;
182 CreateListBoxControl( MAC_WXHWND(parent
->MacGetRootWindow()), &bounds
, false, 0, 1, false, true,
183 14, 14, false, &listDef
, (ControlRef
*)&m_macControl
);
185 GetControlData( (ControlHandle
) m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
,
186 sizeof(ListHandle
), (Ptr
) &m_macList
, &asize
);
188 SetControlReference( (ControlHandle
) m_macControl
, (long) this);
189 SetControlVisibility( (ControlHandle
) m_macControl
, false, false);
195 m_macControl
= ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false ,
196 kwxMacListWithVerticalScrollbar
, 0 , 0,
197 kControlListBoxProc
, (long) this ) ;
198 ::GetControlData( (ControlHandle
) m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
,
199 sizeof( ListHandle
) , (char*) &m_macList
, &result
) ;
201 HLock( (Handle
) m_macList
) ;
203 ldef
= (ldefHandle
) NewHandle( sizeof(ldefRec
) ) ;
204 if ( (**(ListHandle
)m_macList
).listDefProc
!= NULL
)
206 (**ldef
).instruction
= 0x4EF9; /* JMP instruction */
207 (**ldef
).function
= (void(*)()) listDef
.u
.userProc
;
208 (**(ListHandle
)m_macList
).listDefProc
= (Handle
) ldef
;
211 Point pt
= (**(ListHandle
)m_macList
).cellSize
;
213 LCellSize( pt
, (ListHandle
)m_macList
) ;
214 LAddColumn( 1 , 0 , (ListHandle
)m_macList
) ;
216 OptionBits options
= 0;
217 if ( style
& wxLB_MULTIPLE
)
219 options
+= lNoExtend
;
221 else if ( style
& wxLB_EXTENDED
)
223 options
+= lExtendDrag
;
229 SetListSelectionFlags((ListHandle
)m_macList
, options
);
231 MacPostControlCreate() ;
233 for ( int i
= 0 ; i
< n
; i
++ )
235 Append( choices
[i
] ) ;
238 LSetDrawingMode( true , (ListHandle
) m_macList
) ;
243 // ----------------------------------------------------------------------------
244 // wxCheckListBox functions
245 // ----------------------------------------------------------------------------
247 bool wxCheckListBox::IsChecked(size_t item
) const
249 wxCHECK_MSG( item
< m_checks
.GetCount(), FALSE
,
250 _T("invalid index in wxCheckListBox::IsChecked") );
252 return m_checks
[item
] != 0;
255 void wxCheckListBox::Check(size_t item
, bool check
)
257 wxCHECK_RET( item
< m_checks
.GetCount(),
258 _T("invalid index in wxCheckListBox::Check") );
260 // intermediate var is needed to avoid compiler warning with VC++
261 bool isChecked
= m_checks
[item
] != 0;
262 if ( check
!= isChecked
)
264 m_checks
[item
] = check
;
270 // ----------------------------------------------------------------------------
271 // methods forwarded to wxListBox
272 // ----------------------------------------------------------------------------
274 void wxCheckListBox::Delete(int n
)
276 wxCHECK_RET( n
< GetCount(), _T("invalid index in wxListBox::Delete") );
278 wxListBox::Delete(n
);
280 m_checks
.RemoveAt(n
);
283 int wxCheckListBox::DoAppend(const wxString
& item
)
285 int pos
= wxListBox::DoAppend(item
);
287 // the item is initially unchecked
288 m_checks
.Insert(FALSE
, pos
);
293 void wxCheckListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
295 wxListBox::DoInsertItems(items
, pos
);
297 size_t count
= items
.GetCount();
298 for ( size_t n
= 0; n
< count
; n
++ )
300 m_checks
.Insert(FALSE
, pos
+ n
);
304 void wxCheckListBox::DoSetItems(const wxArrayString
& items
, void **clientData
)
306 // call it first as it does DoClear()
307 wxListBox::DoSetItems(items
, clientData
);
309 size_t count
= items
.GetCount();
310 for ( size_t n
= 0; n
< count
; n
++ )
316 void wxCheckListBox::DoClear()
321 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
322 EVT_CHAR(wxCheckListBox::OnChar
)
323 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick
)
326 // this will only work as soon as
328 void wxCheckListBox::OnChar(wxKeyEvent
& event
)
330 if ( event
.KeyCode() == WXK_SPACE
)
332 int index
= GetSelection() ;
335 Check(index
, !IsChecked(index
) ) ;
336 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, GetId());
338 event
.SetEventObject(this);
339 GetEventHandler()->ProcessEvent(event
);
346 void wxCheckListBox::OnLeftClick(wxMouseEvent
& event
)
348 // clicking on the item selects it, clicking on the checkmark toggles
349 if ( event
.GetX() <= 20 /*check width*/ ) {
354 GetListCellSize( (ListHandle
)m_macList
, &pt
) ;
357 GetListVisibleCells( (ListHandle
)m_macList
, &visible
) ;
358 topcell
= visible
.top
;
360 lineheight
= (**(ListHandle
)m_macList
).cellSize
.v
;
361 topcell
= (**(ListHandle
)m_macList
).visible
.top
;
363 size_t nItem
= ((size_t)event
.GetY()) / lineheight
+ topcell
;
365 if ( nItem
< (size_t)m_noItems
)
367 Check(nItem
, !IsChecked(nItem
) ) ;
368 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, GetId());
370 event
.SetEventObject(this);
371 GetEventHandler()->ProcessEvent(event
);
373 //else: it's not an error, just click outside of client zone
376 // implement default behaviour: clicking on the item selects it
381 #endif // wxUSE_CHECKLISTBOX