1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/classic/checklst.cpp
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 // ============================================================================
16 #include "wx/wxprec.h"
18 #if wxUSE_CHECKLISTBOX
20 #include "wx/checklst.h"
21 #include "wx/arrstr.h"
23 #include "wx/mac/uma.h"
24 #include <Appearance.h>
26 // ============================================================================
27 // implementation of wxCheckListBox
28 // ============================================================================
30 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
)
32 const short kwxMacListWithVerticalScrollbar
= 128 ;
33 const short kwxMacListItemHeight
= 14 ;
34 const short kwxMacListCheckboxWidth
= 14 ;
36 #if PRAGMA_STRUCT_ALIGN
37 #pragma options align=mac68k
38 #elif PRAGMA_STRUCT_PACKPUSH
40 #elif PRAGMA_STRUCT_PACK
45 unsigned short instruction
;
47 } ldefRec
, *ldefPtr
, **ldefHandle
;
49 #if PRAGMA_STRUCT_ALIGN
50 #pragma options align=reset
51 #elif PRAGMA_STRUCT_PACKPUSH
53 #elif PRAGMA_STRUCT_PACK
59 static pascal void wxMacCheckListDefinition( short message
, Boolean isSelected
, Rect
*drawRect
,
60 Cell cell
, short dataOffset
, short dataLength
,
61 ListHandle listHandle
) ;
64 static pascal void wxMacCheckListDefinition( short message
, Boolean isSelected
, Rect
*drawRect
,
65 Cell cell
, short dataOffset
, short dataLength
,
66 ListHandle listHandle
)
69 list
= (wxCheckListBox
*) GetControlReference( (ControlHandle
) GetListRefCon(listHandle
) );
75 RgnHandle savedClipRegion
;
78 SetPort((**listHandle
).port
);
79 grafPtr
= (**listHandle
).port
;
80 // typecast our refCon
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
,
179 const wxArrayString
& choices
,
181 const wxValidator
& validator
,
182 const wxString
&name
)
184 wxCArrayString
chs(choices
);
186 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
187 style
, validator
, name
);
190 bool wxCheckListBox::Create(wxWindow
*parent
,
195 const wxString choices
[],
197 const wxValidator
& validator
,
198 const wxString
&name
)
200 if ( !wxCheckListBoxBase::Create(parent
, id
, pos
, size
,
201 n
, choices
, style
, validator
, name
) )
204 m_noItems
= 0 ; // this will be increased by our append command
207 m_checkBoxWidth
= 12;
208 m_checkBoxHeight
= 10;
210 long h
= m_checkBoxHeight
;
212 GetThemeMetric(kThemeMetricCheckBoxWidth
,(long *)&m_checkBoxWidth
);
213 GetThemeMetric(kThemeMetricCheckBoxHeight
,&h
);
216 const wxFont
& font
= GetFont();
219 FetchFontInfo(font
.GetMacFontNum(),font
.GetMacFontSize(),font
.GetMacFontStyle(),&finfo
);
221 m_TextBaseLineOffset
= finfo
.leading
+finfo
.ascent
;
222 m_checkBoxHeight
= finfo
.leading
+finfo
.ascent
+finfo
.descent
;
224 if (m_checkBoxHeight
<h
)
226 m_TextBaseLineOffset
+= (h
-m_checkBoxHeight
)/2;
233 MacPreControlCreate( parent
, id
, wxEmptyString
, pos
, size
,style
, validator
, name
, &bounds
, title
) ;
236 listDef
.defType
= kListDefUserProcType
;
237 if ( macCheckListDefUPP
== NULL
)
239 macCheckListDefUPP
= NewListDefUPP( wxMacCheckListDefinition
);
241 listDef
.u
.userProc
= macCheckListDefUPP
;
247 CreateListBoxControl( MAC_WXHWND(parent
->MacGetRootWindow()), &bounds
, false, 0, 1, false, true,
248 m_checkBoxHeight
+2, 14, false, &listDef
, (ControlRef
*)&m_macControl
);
250 GetControlData( (ControlHandle
) m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
,
251 sizeof(ListHandle
), (Ptr
) &m_macList
, &asize
);
253 SetControlReference( (ControlHandle
) m_macControl
, (long) this);
254 SetControlVisibility( (ControlHandle
) m_macControl
, false, false);
260 wxStAppResource resload
;
261 m_macControl
= (WXWidget
) ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false ,
262 kwxMacListWithVerticalScrollbar
, 0 , 0,
263 kControlListBoxProc
, (long) this ) ;
264 ::GetControlData( (ControlHandle
) m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
,
265 sizeof( ListHandle
) , (char*) &m_macList
, &result
) ;
267 HLock( (Handle
) m_macList
) ;
269 ldef
= (ldefHandle
) NewHandle( sizeof(ldefRec
) ) ;
270 if ( (**(ListHandle
)m_macList
).listDefProc
!= NULL
)
272 (**ldef
).instruction
= 0x4EF9; /* JMP instruction */
273 (**ldef
).function
= (void(*)()) listDef
.u
.userProc
;
274 (**(ListHandle
)m_macList
).listDefProc
= (Handle
) ldef
;
277 Point pt
= (**(ListHandle
)m_macList
).cellSize
;
279 LCellSize( pt
, (ListHandle
)m_macList
) ;
280 LAddColumn( 1 , 0 , (ListHandle
)m_macList
) ;
282 OptionBits options
= 0;
283 if ( style
& wxLB_MULTIPLE
)
285 options
+= lNoExtend
;
287 else if ( style
& wxLB_EXTENDED
)
289 options
+= lExtendDrag
;
293 options
= (OptionBits
) lOnlyOne
;
295 SetListSelectionFlags((ListHandle
)m_macList
, options
);
297 MacPostControlCreate() ;
299 for ( int i
= 0 ; i
< n
; i
++ )
301 Append( choices
[i
] ) ;
304 LSetDrawingMode( true , (ListHandle
) m_macList
) ;
309 // ----------------------------------------------------------------------------
310 // wxCheckListBox functions
311 // ----------------------------------------------------------------------------
313 bool wxCheckListBox::IsChecked(size_t item
) const
315 wxCHECK_MSG( IsValid(item
), false,
316 _T("invalid index in wxCheckListBox::IsChecked") );
318 return m_checks
[item
] != 0;
321 void wxCheckListBox::Check(size_t item
, bool check
)
323 wxCHECK_RET( IsValid(item
),
324 _T("invalid index in wxCheckListBox::Check") );
326 // intermediate var is needed to avoid compiler warning with VC++
327 bool isChecked
= m_checks
[item
] != 0;
328 if ( check
!= isChecked
)
330 m_checks
[item
] = check
;
336 // ----------------------------------------------------------------------------
337 // methods forwarded to wxListBox
338 // ----------------------------------------------------------------------------
340 void wxCheckListBox::Delete(int n
)
342 wxCHECK_RET( IsValid(n
), _T("invalid index in wxListBox::Delete") );
344 wxListBox::Delete(n
);
346 m_checks
.RemoveAt(n
);
349 int wxCheckListBox::DoAppend(const wxString
& item
)
351 LSetDrawingMode( false , (ListHandle
) m_macList
) ;
352 int pos
= wxListBox::DoAppend(item
);
354 // the item is initially unchecked
355 m_checks
.Insert(false, pos
);
356 LSetDrawingMode( true , (ListHandle
) m_macList
) ;
361 void wxCheckListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
363 wxListBox::DoInsertItems(items
, pos
);
365 size_t count
= items
.GetCount();
366 for ( size_t n
= 0; n
< count
; n
++ )
368 m_checks
.Insert(false, pos
+ n
);
372 void wxCheckListBox::DoSetItems(const wxArrayString
& items
, void **clientData
)
374 // call it first as it does DoClear()
375 wxListBox::DoSetItems(items
, clientData
);
377 size_t count
= items
.GetCount();
378 for ( size_t n
= 0; n
< count
; n
++ )
384 void wxCheckListBox::DoClear()
389 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
390 EVT_CHAR(wxCheckListBox::OnChar
)
391 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick
)
394 // this will only work as soon as
396 void wxCheckListBox::OnChar(wxKeyEvent
& event
)
398 if ( event
.GetKeyCode() == WXK_SPACE
)
400 int index
= GetSelection() ;
403 Check(index
, !IsChecked(index
) ) ;
404 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, GetId());
406 event
.SetEventObject(this);
407 GetEventHandler()->ProcessEvent(event
);
414 void wxCheckListBox::OnLeftClick(wxMouseEvent
& event
)
416 // clicking on the item selects it, clicking on the checkmark toggles
417 if ( event
.GetX() <= 20 /*check width*/ ) {
422 GetListCellSize( (ListHandle
)m_macList
, &pt
) ;
425 GetListVisibleCells( (ListHandle
)m_macList
, &visible
) ;
426 topcell
= visible
.top
;
428 lineheight
= (**(ListHandle
)m_macList
).cellSize
.v
;
429 topcell
= (**(ListHandle
)m_macList
).visible
.top
;
431 size_t nItem
= ((size_t)event
.GetY()) / lineheight
+ topcell
;
433 if ( nItem
< m_noItems
)
435 Check(nItem
, !IsChecked(nItem
) ) ;
436 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, GetId());
438 event
.SetEventObject(this);
439 GetEventHandler()->ProcessEvent(event
);
441 //else: it's not an error, just click outside of client zone
444 // implement default behaviour: clicking on the item selects it
449 #endif // wxUSE_CHECKLISTBOX