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"
25 #include "wx/arrstr.h"
27 #include "wx/mac/uma.h"
28 #include "Appearance.h"
30 // ============================================================================
31 // implementation of wxCheckListBox
32 // ============================================================================
34 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
)
36 const short kwxMacListWithVerticalScrollbar
= 128 ;
37 const short kwxMacListItemHeight
= 14 ;
38 const short kwxMacListCheckboxWidth
= 14 ;
40 #if PRAGMA_STRUCT_ALIGN
41 #pragma options align=mac68k
42 #elif PRAGMA_STRUCT_PACKPUSH
44 #elif PRAGMA_STRUCT_PACK
49 unsigned short instruction
;
51 } ldefRec
, *ldefPtr
, **ldefHandle
;
53 #if PRAGMA_STRUCT_ALIGN
54 #pragma options align=reset
55 #elif PRAGMA_STRUCT_PACKPUSH
57 #elif PRAGMA_STRUCT_PACK
63 static pascal void wxMacCheckListDefinition( short message
, Boolean isSelected
, Rect
*drawRect
,
64 Cell cell
, short dataOffset
, short dataLength
,
65 ListHandle listHandle
) ;
68 static pascal void wxMacCheckListDefinition( short message
, Boolean isSelected
, Rect
*drawRect
,
69 Cell cell
, short dataOffset
, short dataLength
,
70 ListHandle listHandle
)
74 RgnHandle savedClipRegion
;
78 SetPort((**listHandle
).port
);
79 grafPtr
= (**listHandle
).port
;
80 // typecast our refCon
81 list
= (wxCheckListBox
*) GetControlReference( (ControlHandle
) GetListRefCon(listHandle
) );
83 // Calculate the cell rect.
94 const wxString text
= list
->m_stringArray
[cell
.v
] ;
95 int checked
= list
->m_checks
[cell
.v
] ;
97 // Save the current clip region, and set the clip region to the area we are about
100 savedClipRegion
= NewRgn();
101 GetClip( savedClipRegion
);
103 ClipRect( drawRect
);
104 EraseRect( drawRect
);
106 const wxFont
& font
= list
->GetFont();
109 ::TextFont( font
.GetMacFontNum() ) ;
110 ::TextSize( font
.GetMacFontSize()) ;
111 ::TextFace( font
.GetMacFontStyle() ) ;
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
,
180 const wxArrayString
& choices
,
182 const wxValidator
& validator
,
183 const wxString
&name
)
185 wxCArrayString
chs(choices
);
187 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
188 style
, validator
, name
);
191 bool wxCheckListBox::Create(wxWindow
*parent
,
196 const wxString choices
[],
198 const wxValidator
& validator
,
199 const wxString
&name
)
201 if ( !wxCheckListBoxBase::Create(parent
, id
, pos
, size
,
202 n
, choices
, style
, validator
, name
) )
205 m_noItems
= 0 ; // this will be increased by our append command
208 m_checkBoxWidth
= 12;
209 m_checkBoxHeight
= 10;
211 long h
= m_checkBoxHeight
;
213 GetThemeMetric(kThemeMetricCheckBoxWidth
,(long *)&m_checkBoxWidth
);
214 GetThemeMetric(kThemeMetricCheckBoxHeight
,&h
);
217 const wxFont
& font
= GetFont();
220 FetchFontInfo(font
.GetMacFontNum(),font
.GetMacFontSize(),font
.GetMacFontStyle(),&finfo
);
222 m_TextBaseLineOffset
= finfo
.leading
+finfo
.ascent
;
223 m_checkBoxHeight
= finfo
.leading
+finfo
.ascent
+finfo
.descent
;
225 if (m_checkBoxHeight
<h
)
227 m_TextBaseLineOffset
+= (h
-m_checkBoxHeight
)/2;
234 MacPreControlCreate( parent
, id
, wxEmptyString
, pos
, size
,style
, validator
, name
, &bounds
, title
) ;
237 listDef
.defType
= kListDefUserProcType
;
238 if ( macCheckListDefUPP
== NULL
)
240 macCheckListDefUPP
= NewListDefUPP( wxMacCheckListDefinition
);
242 listDef
.u
.userProc
= macCheckListDefUPP
;
248 CreateListBoxControl( MAC_WXHWND(parent
->MacGetRootWindow()), &bounds
, false, 0, 1, false, true,
249 m_checkBoxHeight
+2, 14, false, &listDef
, (ControlRef
*)&m_macControl
);
251 GetControlData( (ControlHandle
) m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
,
252 sizeof(ListHandle
), (Ptr
) &m_macList
, &asize
);
254 SetControlReference( (ControlHandle
) m_macControl
, (long) this);
255 SetControlVisibility( (ControlHandle
) m_macControl
, false, false);
261 wxStAppResource resload
;
262 m_macControl
= ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false ,
263 kwxMacListWithVerticalScrollbar
, 0 , 0,
264 kControlListBoxProc
, (long) this ) ;
265 ::GetControlData( (ControlHandle
) m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
,
266 sizeof( ListHandle
) , (char*) &m_macList
, &result
) ;
268 HLock( (Handle
) m_macList
) ;
270 ldef
= (ldefHandle
) NewHandle( sizeof(ldefRec
) ) ;
271 if ( (**(ListHandle
)m_macList
).listDefProc
!= NULL
)
273 (**ldef
).instruction
= 0x4EF9; /* JMP instruction */
274 (**ldef
).function
= (void(*)()) listDef
.u
.userProc
;
275 (**(ListHandle
)m_macList
).listDefProc
= (Handle
) ldef
;
278 Point pt
= (**(ListHandle
)m_macList
).cellSize
;
280 LCellSize( pt
, (ListHandle
)m_macList
) ;
281 LAddColumn( 1 , 0 , (ListHandle
)m_macList
) ;
283 OptionBits options
= 0;
284 if ( style
& wxLB_MULTIPLE
)
286 options
+= lNoExtend
;
288 else if ( style
& wxLB_EXTENDED
)
290 options
+= lExtendDrag
;
294 options
= (OptionBits
) lOnlyOne
;
296 SetListSelectionFlags((ListHandle
)m_macList
, options
);
298 MacPostControlCreate() ;
300 for ( int i
= 0 ; i
< n
; i
++ )
302 Append( choices
[i
] ) ;
305 LSetDrawingMode( true , (ListHandle
) m_macList
) ;
310 // ----------------------------------------------------------------------------
311 // wxCheckListBox functions
312 // ----------------------------------------------------------------------------
314 bool wxCheckListBox::IsChecked(size_t item
) const
316 wxCHECK_MSG( item
< m_checks
.GetCount(), FALSE
,
317 _T("invalid index in wxCheckListBox::IsChecked") );
319 return m_checks
[item
] != 0;
322 void wxCheckListBox::Check(size_t item
, bool check
)
324 wxCHECK_RET( item
< m_checks
.GetCount(),
325 _T("invalid index in wxCheckListBox::Check") );
327 // intermediate var is needed to avoid compiler warning with VC++
328 bool isChecked
= m_checks
[item
] != 0;
329 if ( check
!= isChecked
)
331 m_checks
[item
] = check
;
337 // ----------------------------------------------------------------------------
338 // methods forwarded to wxListBox
339 // ----------------------------------------------------------------------------
341 void wxCheckListBox::Delete(int n
)
343 wxCHECK_RET( n
< GetCount(), _T("invalid index in wxListBox::Delete") );
345 wxListBox::Delete(n
);
347 m_checks
.RemoveAt(n
);
350 int wxCheckListBox::DoAppend(const wxString
& item
)
352 LSetDrawingMode( false , (ListHandle
) m_macList
) ;
353 int pos
= wxListBox::DoAppend(item
);
355 // the item is initially unchecked
356 m_checks
.Insert(FALSE
, pos
);
357 LSetDrawingMode( true , (ListHandle
) m_macList
) ;
362 void wxCheckListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
364 wxListBox::DoInsertItems(items
, pos
);
366 size_t count
= items
.GetCount();
367 for ( size_t n
= 0; n
< count
; n
++ )
369 m_checks
.Insert(FALSE
, pos
+ n
);
373 void wxCheckListBox::DoSetItems(const wxArrayString
& items
, void **clientData
)
375 // call it first as it does DoClear()
376 wxListBox::DoSetItems(items
, clientData
);
378 size_t count
= items
.GetCount();
379 for ( size_t n
= 0; n
< count
; n
++ )
385 void wxCheckListBox::DoClear()
390 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
391 EVT_CHAR(wxCheckListBox::OnChar
)
392 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick
)
395 // this will only work as soon as
397 void wxCheckListBox::OnChar(wxKeyEvent
& event
)
399 if ( event
.GetKeyCode() == WXK_SPACE
)
401 int index
= GetSelection() ;
404 Check(index
, !IsChecked(index
) ) ;
405 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, GetId());
407 event
.SetEventObject(this);
408 GetEventHandler()->ProcessEvent(event
);
415 void wxCheckListBox::OnLeftClick(wxMouseEvent
& event
)
417 // clicking on the item selects it, clicking on the checkmark toggles
418 if ( event
.GetX() <= 20 /*check width*/ ) {
423 GetListCellSize( (ListHandle
)m_macList
, &pt
) ;
426 GetListVisibleCells( (ListHandle
)m_macList
, &visible
) ;
427 topcell
= visible
.top
;
429 lineheight
= (**(ListHandle
)m_macList
).cellSize
.v
;
430 topcell
= (**(ListHandle
)m_macList
).visible
.top
;
432 size_t nItem
= ((size_t)event
.GetY()) / lineheight
+ topcell
;
434 if ( nItem
< (size_t)m_noItems
)
436 Check(nItem
, !IsChecked(nItem
) ) ;
437 wxCommandEvent
event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, GetId());
439 event
.SetEventObject(this);
440 GetEventHandler()->ProcessEvent(event
);
442 //else: it's not an error, just click outside of client zone
445 // implement default behaviour: clicking on the item selects it
450 #endif // wxUSE_CHECKLISTBOX