1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: implementation of wxCheckListBox class
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
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 const wxFont
& font
= list
->GetFont();
108 ::TextFont( font
.GetMacFontNum() ) ;
109 ::TextSize( font
.GetMacFontSize()) ;
110 ::TextFace( font
.GetMacFontStyle() ) ;
113 ThemeButtonDrawInfo info
;
114 info
.state
= kThemeStateActive
;
115 info
.value
= checked
? kThemeButtonOn
: kThemeButtonOff
;
116 info
.adornment
= kThemeAdornmentNone
;
117 Rect checkRect
= *drawRect
;
122 checkRect
.right
= checkRect
.left
+ list
->m_checkBoxWidth
;
123 checkRect
.bottom
= checkRect
.top
+ list
->m_checkBoxHeight
;
124 DrawThemeButton(&checkRect
,kThemeCheckBox
,
125 &info
,NULL
,NULL
, NULL
,0);
127 MoveTo(drawRect
->left
+ 2 + list
->m_checkBoxWidth
+2, drawRect
->top
+ list
->m_TextBaseLineOffset
);
129 DrawText(text
, 0 , text
.Length());
130 // If the cell is hilited, do the hilite now. Paint the cell contents with the
131 // appropriate QuickDraw transform mode.
134 savedPenMode
= GetPortPenMode( (CGrafPtr
) grafPtr
);
135 SetPortPenMode( (CGrafPtr
) grafPtr
, hilitetransfermode
);
136 PaintRect( drawRect
);
137 SetPortPenMode( (CGrafPtr
) grafPtr
, savedPenMode
);
140 // Restore the saved clip region.
142 SetClip( savedClipRegion
);
143 DisposeRgn( savedClipRegion
);
148 // Hilite or unhilite the cell. Paint the cell contents with the
149 // appropriate QuickDraw transform mode.
152 savedPenMode
= GetPortPenMode( (CGrafPtr
) grafPtr
);
153 SetPortPenMode( (CGrafPtr
) grafPtr
, hilitetransfermode
);
154 PaintRect( drawRect
);
155 SetPortPenMode( (CGrafPtr
) grafPtr
, savedPenMode
);
163 extern "C" void MacDrawStringCell(Rect
*cellRect
, Cell lCell
, ListHandle theList
, long refCon
) ;
165 static ListDefUPP macCheckListDefUPP
= NULL
;
167 // ----------------------------------------------------------------------------
169 // ----------------------------------------------------------------------------
171 void wxCheckListBox::Init()
175 bool wxCheckListBox::Create(wxWindow
*parent
,
180 const wxString choices
[],
182 const wxValidator
& validator
,
183 const wxString
&name
)
185 if ( !wxCheckListBoxBase::Create(parent
, id
, pos
, size
,
186 n
, choices
, style
, validator
, name
) )
189 m_noItems
= 0 ; // this will be increased by our append command
192 m_checkBoxWidth
= 12;
193 m_checkBoxHeight
= 10;
195 long h
= m_checkBoxHeight
;
197 GetThemeMetric(kThemeMetricCheckBoxWidth
,(long *)&m_checkBoxWidth
);
198 GetThemeMetric(kThemeMetricCheckBoxHeight
,&h
);
201 const wxFont
& font
= GetFont();
204 FetchFontInfo(font
.GetMacFontNum(),font
.GetMacFontSize(),font
.GetMacFontStyle(),&finfo
);
206 m_TextBaseLineOffset
= finfo
.leading
+finfo
.ascent
;
207 m_checkBoxHeight
= finfo
.leading
+finfo
.ascent
+finfo
.descent
;
209 if (m_checkBoxHeight
<h
)
211 m_TextBaseLineOffset
+= (h
-m_checkBoxHeight
)/2;
218 MacPreControlCreate( parent
, id
, wxEmptyString
, pos
, size
,style
, validator
, name
, &bounds
, title
) ;
221 listDef
.defType
= kListDefUserProcType
;
222 if ( macCheckListDefUPP
== NULL
)
224 macCheckListDefUPP
= NewListDefUPP( wxMacCheckListDefinition
);
226 listDef
.u
.userProc
= macCheckListDefUPP
;
232 CreateListBoxControl( MAC_WXHWND(parent
->MacGetRootWindow()), &bounds
, false, 0, 1, false, true,
233 m_checkBoxHeight
+2, 14, false, &listDef
, (ControlRef
*)&m_macControl
);
235 GetControlData( (ControlHandle
) m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
,
236 sizeof(ListHandle
), (Ptr
) &m_macList
, &asize
);
238 SetControlReference( (ControlHandle
) m_macControl
, (long) this);
239 SetControlVisibility( (ControlHandle
) m_macControl
, false, false);
245 wxStAppResource resload
;
246 m_macControl
= ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false ,
247 kwxMacListWithVerticalScrollbar
, 0 , 0,
248 kControlListBoxProc
, (long) this ) ;
249 ::GetControlData( (ControlHandle
) m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
,
250 sizeof( ListHandle
) , (char*) &m_macList
, &result
) ;
252 HLock( (Handle
) m_macList
) ;
254 ldef
= (ldefHandle
) NewHandle( sizeof(ldefRec
) ) ;
255 if ( (**(ListHandle
)m_macList
).listDefProc
!= NULL
)
257 (**ldef
).instruction
= 0x4EF9; /* JMP instruction */
258 (**ldef
).function
= (void(*)()) listDef
.u
.userProc
;
259 (**(ListHandle
)m_macList
).listDefProc
= (Handle
) ldef
;
262 Point pt
= (**(ListHandle
)m_macList
).cellSize
;
264 LCellSize( pt
, (ListHandle
)m_macList
) ;
265 LAddColumn( 1 , 0 , (ListHandle
)m_macList
) ;
267 OptionBits options
= 0;
268 if ( style
& wxLB_MULTIPLE
)
270 options
+= lNoExtend
;
272 else if ( style
& wxLB_EXTENDED
)
274 options
+= lExtendDrag
;
278 options
= (OptionBits
) lOnlyOne
;
280 SetListSelectionFlags((ListHandle
)m_macList
, options
);
282 MacPostControlCreate() ;
284 for ( int i
= 0 ; i
< n
; i
++ )
286 Append( choices
[i
] ) ;
289 LSetDrawingMode( true , (ListHandle
) m_macList
) ;
294 // ----------------------------------------------------------------------------
295 // wxCheckListBox functions
296 // ----------------------------------------------------------------------------
298 bool wxCheckListBox::IsChecked(size_t item
) const
300 wxCHECK_MSG( item
< m_checks
.GetCount(), FALSE
,
301 _T("invalid index in wxCheckListBox::IsChecked") );
303 return m_checks
[item
] != 0;
306 void wxCheckListBox::Check(size_t item
, bool check
)
308 wxCHECK_RET( item
< m_checks
.GetCount(),
309 _T("invalid index in wxCheckListBox::Check") );
311 // intermediate var is needed to avoid compiler warning with VC++
312 bool isChecked
= m_checks
[item
] != 0;
313 if ( check
!= isChecked
)
315 m_checks
[item
] = check
;
321 // ----------------------------------------------------------------------------
322 // methods forwarded to wxListBox
323 // ----------------------------------------------------------------------------
325 void wxCheckListBox::Delete(int n
)
327 wxCHECK_RET( n
< GetCount(), _T("invalid index in wxListBox::Delete") );
329 wxListBox::Delete(n
);
331 m_checks
.RemoveAt(n
);
334 int wxCheckListBox::DoAppend(const wxString
& item
)
336 LSetDrawingMode( false , (ListHandle
) m_macList
) ;
337 int pos
= wxListBox::DoAppend(item
);
339 // the item is initially unchecked
340 m_checks
.Insert(FALSE
, pos
);
341 LSetDrawingMode( true , (ListHandle
) m_macList
) ;
346 void wxCheckListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
348 wxListBox::DoInsertItems(items
, pos
);
350 size_t count
= items
.GetCount();
351 for ( size_t n
= 0; n
< count
; n
++ )
353 m_checks
.Insert(FALSE
, pos
+ n
);
357 void wxCheckListBox::DoSetItems(const wxArrayString
& items
, void **clientData
)
359 // call it first as it does DoClear()
360 wxListBox::DoSetItems(items
, clientData
);
362 size_t count
= items
.GetCount();
363 for ( size_t n
= 0; n
< count
; n
++ )
369 void wxCheckListBox::DoClear()
374 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
375 EVT_CHAR(wxCheckListBox::OnChar
)
376 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick
)
379 // this will only work as soon as
381 void wxCheckListBox::OnChar(wxKeyEvent
& event
)
383 if ( event
.GetKeyCode() == WXK_SPACE
)
385 int index
= GetSelection() ;
388 Check(index
, !IsChecked(index
) ) ;
389 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, GetId());
391 event
.SetEventObject(this);
392 GetEventHandler()->ProcessEvent(event
);
399 void wxCheckListBox::OnLeftClick(wxMouseEvent
& event
)
401 // clicking on the item selects it, clicking on the checkmark toggles
402 if ( event
.GetX() <= 20 /*check width*/ ) {
407 GetListCellSize( (ListHandle
)m_macList
, &pt
) ;
410 GetListVisibleCells( (ListHandle
)m_macList
, &visible
) ;
411 topcell
= visible
.top
;
413 lineheight
= (**(ListHandle
)m_macList
).cellSize
.v
;
414 topcell
= (**(ListHandle
)m_macList
).visible
.top
;
416 size_t nItem
= ((size_t)event
.GetY()) / lineheight
+ topcell
;
418 if ( nItem
< (size_t)m_noItems
)
420 Check(nItem
, !IsChecked(nItem
) ) ;
421 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, GetId());
423 event
.SetEventObject(this);
424 GetEventHandler()->ProcessEvent(event
);
426 //else: it's not an error, just click outside of client zone
429 // implement default behaviour: clicking on the item selects it
434 #endif // wxUSE_CHECKLISTBOX