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 ;
39 #if PRAGMA_STRUCT_ALIGN
40 #pragma options align=mac68k
41 #elif PRAGMA_STRUCT_PACKPUSH
43 #elif PRAGMA_STRUCT_PACK
48 unsigned short instruction
;
50 } ldefRec
, *ldefPtr
, **ldefHandle
;
52 #if PRAGMA_STRUCT_ALIGN
53 #pragma options align=reset
54 #elif PRAGMA_STRUCT_PACKPUSH
56 #elif PRAGMA_STRUCT_PACK
62 static pascal void wxMacCheckListDefinition( short message
, Boolean isSelected
, Rect
*drawRect
,
63 Cell cell
, short dataOffset
, short dataLength
,
64 ListHandle listHandle
) ;
67 static pascal void wxMacCheckListDefinition( short message
, Boolean isSelected
, Rect
*drawRect
,
68 Cell cell
, short dataOffset
, short dataLength
,
69 ListHandle listHandle
)
73 RgnHandle savedClipRegion
;
77 SetPort((**listHandle
).port
);
78 grafPtr
= (**listHandle
).port
;
79 // typecast our refCon
80 list
= (wxCheckListBox
*) GetControlReference( (ControlHandle
) GetListRefCon(listHandle
) );
82 // Calculate the cell rect.
93 const wxString text
= list
->m_stringArray
[cell
.v
] ;
94 int checked
= list
->m_checks
[cell
.v
] ;
96 // Save the current clip region, and set the clip region to the area we are about
99 savedClipRegion
= NewRgn();
100 GetClip( savedClipRegion
);
102 ClipRect( drawRect
);
103 EraseRect( drawRect
);
105 wxFontRefData
* font
= (wxFontRefData
*) (list
->GetFont().GetRefData()) ;
109 ::TextFont( font
->m_macFontNum
) ;
110 ::TextSize( font
->m_macFontSize
) ;
111 ::TextFace( font
->m_macFontStyle
) ;
114 ThemeButtonDrawInfo info
;
115 info
.state
= kThemeStateActive
;
116 info
.value
= checked
? kThemeButtonOn
: kThemeButtonOff
;
117 info
.adornment
= kThemeAdornmentNone
;
118 Rect checkRect
= *drawRect
;
123 checkRect
.right
= checkRect
.left
+ list
->m_checkBoxWidth
;
124 checkRect
.bottom
= checkRect
.top
+ list
->m_checkBoxHeight
;
125 DrawThemeButton(&checkRect
,kThemeCheckBox
,
126 &info
,NULL
,NULL
, NULL
,0);
128 MoveTo(drawRect
->left
+ 2 + list
->m_checkBoxWidth
+2, drawRect
->top
+ list
->m_TextBaseLineOffset
);
130 DrawText(text
, 0 , text
.Length());
131 // If the cell is hilited, do the hilite now. Paint the cell contents with the
132 // appropriate QuickDraw transform mode.
135 savedPenMode
= GetPortPenMode( (CGrafPtr
) grafPtr
);
136 SetPortPenMode( (CGrafPtr
) grafPtr
, hilitetransfermode
);
137 PaintRect( drawRect
);
138 SetPortPenMode( (CGrafPtr
) grafPtr
, savedPenMode
);
141 // Restore the saved clip region.
143 SetClip( savedClipRegion
);
144 DisposeRgn( savedClipRegion
);
149 // Hilite or unhilite the cell. Paint the cell contents with the
150 // appropriate QuickDraw transform mode.
153 savedPenMode
= GetPortPenMode( (CGrafPtr
) grafPtr
);
154 SetPortPenMode( (CGrafPtr
) grafPtr
, hilitetransfermode
);
155 PaintRect( drawRect
);
156 SetPortPenMode( (CGrafPtr
) grafPtr
, savedPenMode
);
164 extern "C" void MacDrawStringCell(Rect
*cellRect
, Cell lCell
, ListHandle theList
, long refCon
) ;
166 static ListDefUPP macCheckListDefUPP
= NULL
;
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
172 void wxCheckListBox::Init()
176 bool wxCheckListBox::Create(wxWindow
*parent
,
181 const wxString choices
[],
183 const wxValidator
& validator
,
184 const wxString
&name
)
186 m_noItems
= 0 ; // this will be increased by our append command
189 m_checkBoxWidth
= 12;
190 m_checkBoxHeight
= 10;
192 long h
= m_checkBoxHeight
;
194 GetThemeMetric(kThemeMetricCheckBoxWidth
,(long *)&m_checkBoxWidth
);
195 GetThemeMetric(kThemeMetricCheckBoxHeight
,&h
);
197 wxFontRefData
* font
= (wxFontRefData
*) (GetFont().GetRefData()) ;
200 FetchFontInfo(font
->m_macFontNum
,short(font
->m_macFontSize
),font
->m_macFontStyle
,&finfo
);
202 m_TextBaseLineOffset
= finfo
.leading
+finfo
.ascent
;
203 m_checkBoxHeight
= finfo
.leading
+finfo
.ascent
+finfo
.descent
;
205 if (m_checkBoxHeight
<h
)
207 m_TextBaseLineOffset
+= (h
-m_checkBoxHeight
)/2;
214 MacPreControlCreate( parent
, id
, "" , pos
, size
,style
, validator
, name
, &bounds
, title
) ;
217 listDef
.defType
= kListDefUserProcType
;
218 if ( macCheckListDefUPP
== NULL
)
220 macCheckListDefUPP
= NewListDefUPP( wxMacCheckListDefinition
);
222 listDef
.u
.userProc
= macCheckListDefUPP
;
228 CreateListBoxControl( MAC_WXHWND(parent
->MacGetRootWindow()), &bounds
, false, 0, 1, false, true,
229 m_checkBoxHeight
+2, 14, false, &listDef
, (ControlRef
*)&m_macControl
);
231 GetControlData( (ControlHandle
) m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
,
232 sizeof(ListHandle
), (Ptr
) &m_macList
, &asize
);
234 SetControlReference( (ControlHandle
) m_macControl
, (long) this);
235 SetControlVisibility( (ControlHandle
) m_macControl
, false, false);
241 wxStAppResource resload
;
242 m_macControl
= ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false ,
243 kwxMacListWithVerticalScrollbar
, 0 , 0,
244 kControlListBoxProc
, (long) this ) ;
245 ::GetControlData( (ControlHandle
) m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
,
246 sizeof( ListHandle
) , (char*) &m_macList
, &result
) ;
248 HLock( (Handle
) m_macList
) ;
250 ldef
= (ldefHandle
) NewHandle( sizeof(ldefRec
) ) ;
251 if ( (**(ListHandle
)m_macList
).listDefProc
!= NULL
)
253 (**ldef
).instruction
= 0x4EF9; /* JMP instruction */
254 (**ldef
).function
= (void(*)()) listDef
.u
.userProc
;
255 (**(ListHandle
)m_macList
).listDefProc
= (Handle
) ldef
;
258 Point pt
= (**(ListHandle
)m_macList
).cellSize
;
260 LCellSize( pt
, (ListHandle
)m_macList
) ;
261 LAddColumn( 1 , 0 , (ListHandle
)m_macList
) ;
263 OptionBits options
= 0;
264 if ( style
& wxLB_MULTIPLE
)
266 options
+= lNoExtend
;
268 else if ( style
& wxLB_EXTENDED
)
270 options
+= lExtendDrag
;
276 SetListSelectionFlags((ListHandle
)m_macList
, options
);
278 MacPostControlCreate() ;
280 for ( int i
= 0 ; i
< n
; i
++ )
282 Append( choices
[i
] ) ;
285 LSetDrawingMode( true , (ListHandle
) m_macList
) ;
290 // ----------------------------------------------------------------------------
291 // wxCheckListBox functions
292 // ----------------------------------------------------------------------------
294 bool wxCheckListBox::IsChecked(size_t item
) const
296 wxCHECK_MSG( item
< m_checks
.GetCount(), FALSE
,
297 _T("invalid index in wxCheckListBox::IsChecked") );
299 return m_checks
[item
] != 0;
302 void wxCheckListBox::Check(size_t item
, bool check
)
304 wxCHECK_RET( item
< m_checks
.GetCount(),
305 _T("invalid index in wxCheckListBox::Check") );
307 // intermediate var is needed to avoid compiler warning with VC++
308 bool isChecked
= m_checks
[item
] != 0;
309 if ( check
!= isChecked
)
311 m_checks
[item
] = check
;
317 // ----------------------------------------------------------------------------
318 // methods forwarded to wxListBox
319 // ----------------------------------------------------------------------------
321 void wxCheckListBox::Delete(int n
)
323 wxCHECK_RET( n
< GetCount(), _T("invalid index in wxListBox::Delete") );
325 wxListBox::Delete(n
);
327 m_checks
.RemoveAt(n
);
330 int wxCheckListBox::DoAppend(const wxString
& item
)
332 LSetDrawingMode( false , (ListHandle
) m_macList
) ;
333 int pos
= wxListBox::DoAppend(item
);
335 // the item is initially unchecked
336 m_checks
.Insert(FALSE
, pos
);
337 LSetDrawingMode( true , (ListHandle
) m_macList
) ;
342 void wxCheckListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
344 wxListBox::DoInsertItems(items
, pos
);
346 size_t count
= items
.GetCount();
347 for ( size_t n
= 0; n
< count
; n
++ )
349 m_checks
.Insert(FALSE
, pos
+ n
);
353 void wxCheckListBox::DoSetItems(const wxArrayString
& items
, void **clientData
)
355 // call it first as it does DoClear()
356 wxListBox::DoSetItems(items
, clientData
);
358 size_t count
= items
.GetCount();
359 for ( size_t n
= 0; n
< count
; n
++ )
365 void wxCheckListBox::DoClear()
370 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
371 EVT_CHAR(wxCheckListBox::OnChar
)
372 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick
)
375 // this will only work as soon as
377 void wxCheckListBox::OnChar(wxKeyEvent
& event
)
379 if ( event
.KeyCode() == WXK_SPACE
)
381 int index
= GetSelection() ;
384 Check(index
, !IsChecked(index
) ) ;
385 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, GetId());
387 event
.SetEventObject(this);
388 GetEventHandler()->ProcessEvent(event
);
395 void wxCheckListBox::OnLeftClick(wxMouseEvent
& event
)
397 // clicking on the item selects it, clicking on the checkmark toggles
398 if ( event
.GetX() <= 20 /*check width*/ ) {
403 GetListCellSize( (ListHandle
)m_macList
, &pt
) ;
406 GetListVisibleCells( (ListHandle
)m_macList
, &visible
) ;
407 topcell
= visible
.top
;
409 lineheight
= (**(ListHandle
)m_macList
).cellSize
.v
;
410 topcell
= (**(ListHandle
)m_macList
).visible
.top
;
412 size_t nItem
= ((size_t)event
.GetY()) / lineheight
+ topcell
;
414 if ( nItem
< (size_t)m_noItems
)
416 Check(nItem
, !IsChecked(nItem
) ) ;
417 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, GetId());
419 event
.SetEventObject(this);
420 GetEventHandler()->ProcessEvent(event
);
422 //else: it's not an error, just click outside of client zone
425 // implement default behaviour: clicking on the item selects it
430 #endif // wxUSE_CHECKLISTBOX