]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/checklst.cpp
Updated script + FAQs
[wxWidgets.git] / src / mac / carbon / checklst.cpp
CommitLineData
e9576ca5
SC
1///////////////////////////////////////////////////////////////////////////////
2// Name: checklst.cpp
3// Purpose: implementation of wxCheckListBox class
4// Author: AUTHOR
5// Modified by:
6// Created: ??/??/98
7// RCS-ID: $Id$
8// Copyright: (c) AUTHOR
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// headers & declarations
14// ============================================================================
15
16#ifdef __GNUG__
17#pragma implementation "checklst.h"
18#endif
19
b698c8e9 20#include "wx/defs.h"
e9576ca5 21
55d60746
GD
22#if wxUSE_CHECKLISTBOX
23
b698c8e9
GD
24#include "wx/checklst.h"
25
dbfc5b97
SC
26#include "wx/mac/uma.h"
27#include "Appearance.h"
28
e9576ca5 29// ============================================================================
dbfc5b97 30// implementation of wxCheckListBox
e9576ca5
SC
31// ============================================================================
32
dbfc5b97
SC
33IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
34
35const short kwxMacListWithVerticalScrollbar = 128 ;
36const short kwxMacListItemHeight = 14 ;
37const short kwxMacListCheckboxWidth = 14 ;
38
573ac9dc
SC
39#if PRAGMA_STRUCT_ALIGN
40 #pragma options align=mac68k
41#elif PRAGMA_STRUCT_PACKPUSH
42 #pragma pack(push, 2)
43#elif PRAGMA_STRUCT_PACK
44 #pragma pack(2)
45#endif
46
dbfc5b97
SC
47typedef struct {
48 unsigned short instruction;
49 void (*function)();
50} ldefRec, *ldefPtr, **ldefHandle;
51
573ac9dc
SC
52#if PRAGMA_STRUCT_ALIGN
53 #pragma options align=reset
54#elif PRAGMA_STRUCT_PACKPUSH
55 #pragma pack(pop)
56#elif PRAGMA_STRUCT_PACK
57 #pragma pack()
58#endif
59
dbfc5b97
SC
60extern "C"
61{
62static pascal void wxMacCheckListDefinition( short message, Boolean isSelected, Rect *drawRect,
63 Cell cell, short dataOffset, short dataLength,
64 ListHandle listHandle ) ;
65}
66
67static pascal void wxMacCheckListDefinition( short message, Boolean isSelected, Rect *drawRect,
68 Cell cell, short dataOffset, short dataLength,
69 ListHandle listHandle )
70{
dbfc5b97
SC
71 GrafPtr savePort;
72 GrafPtr grafPtr;
73 RgnHandle savedClipRegion;
74 SInt32 savedPenMode;
75 wxCheckListBox* list;
76 GetPort(&savePort);
77 SetPort((**listHandle).port);
78 grafPtr = (**listHandle).port ;
79 // typecast our refCon
80 list = (wxCheckListBox*) GetControlReference( (ControlHandle) GetListRefCon(listHandle) );
81
82 // Calculate the cell rect.
83
84 switch( message ) {
85 case lInitMsg:
86 break;
87
88 case lCloseMsg:
89 break;
90
91 case lDrawMsg:
92 {
93 const wxString text = list->m_stringArray[cell.v] ;
94 int checked = list->m_checks[cell.v] ;
95
96 // Save the current clip region, and set the clip region to the area we are about
97 // to draw.
98
99 savedClipRegion = NewRgn();
100 GetClip( savedClipRegion );
101
102 ClipRect( drawRect );
103 EraseRect( drawRect );
104
50b30d83
SC
105 wxFontRefData * font = (wxFontRefData*) (list->GetFont().GetRefData()) ;
106
107 if ( font )
108 {
109 ::TextFont( font->m_macFontNum ) ;
110 ::TextSize( font->m_macFontSize) ;
111 ::TextFace( font->m_macFontStyle ) ;
112 }
113
dbfc5b97
SC
114 ThemeButtonDrawInfo info ;
115 info.state = kThemeStateActive ;
116 info.value = checked ? kThemeButtonOn : kThemeButtonOff ;
117 info.adornment = kThemeAdornmentNone ;
118 Rect checkRect = *drawRect ;
50b30d83
SC
119
120
dbfc5b97 121 checkRect.left +=0 ;
50b30d83
SC
122 checkRect.top +=0 ;
123 checkRect.right = checkRect.left + list->m_checkBoxWidth ;
124 checkRect.bottom = checkRect.top + list->m_checkBoxHeight ;
dbfc5b97
SC
125 DrawThemeButton(&checkRect,kThemeCheckBox,
126 &info,NULL,NULL, NULL,0);
127
50b30d83 128 MoveTo(drawRect->left + 2 + list->m_checkBoxWidth+2, drawRect->top + list->m_TextBaseLineOffset );
dbfc5b97
SC
129
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.
133
134 if( isSelected ) {
76a5e5d2
SC
135 savedPenMode = GetPortPenMode( (CGrafPtr) grafPtr );
136 SetPortPenMode( (CGrafPtr) grafPtr, hilitetransfermode );
dbfc5b97 137 PaintRect( drawRect );
76a5e5d2 138 SetPortPenMode( (CGrafPtr) grafPtr, savedPenMode );
dbfc5b97
SC
139 }
140
141 // Restore the saved clip region.
142
143 SetClip( savedClipRegion );
144 DisposeRgn( savedClipRegion );
145 }
146 break;
147 case lHiliteMsg:
148
149 // Hilite or unhilite the cell. Paint the cell contents with the
150 // appropriate QuickDraw transform mode.
151
152 GetPort( &grafPtr );
76a5e5d2
SC
153 savedPenMode = GetPortPenMode( (CGrafPtr) grafPtr );
154 SetPortPenMode( (CGrafPtr) grafPtr, hilitetransfermode );
dbfc5b97 155 PaintRect( drawRect );
76a5e5d2 156 SetPortPenMode( (CGrafPtr) grafPtr, savedPenMode );
dbfc5b97
SC
157 break;
158 default :
159 break ;
160 }
161 SetPort(savePort);
162}
163
164extern "C" void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon) ;
165
166static ListDefUPP macCheckListDefUPP = NULL ;
167
168// ----------------------------------------------------------------------------
169// creation
170// ----------------------------------------------------------------------------
171
172void wxCheckListBox::Init()
173{
174}
175
176bool wxCheckListBox::Create(wxWindow *parent,
177 wxWindowID id,
178 const wxPoint &pos,
179 const wxSize &size,
180 int n,
181 const wxString choices[],
182 long style,
183 const wxValidator& validator,
184 const wxString &name)
185{
186 m_noItems = 0 ; // this will be increased by our append command
187 m_selected = 0;
188
50b30d83
SC
189 m_checkBoxWidth = 12;
190 m_checkBoxHeight= 10;
191
192 long h = m_checkBoxHeight ;
193#if TARGET_CARBON
194 GetThemeMetric(kThemeMetricCheckBoxWidth,(long *)&m_checkBoxWidth);
195 GetThemeMetric(kThemeMetricCheckBoxHeight,&h);
196#endif
197 wxFontRefData * font = (wxFontRefData*) (GetFont().GetRefData()) ;
198
199 FontInfo finfo;
200 FetchFontInfo(font->m_macFontNum,short(font->m_macFontSize),font->m_macFontStyle,&finfo);
201
202 m_TextBaseLineOffset= finfo.leading+finfo.ascent;
203 m_checkBoxHeight= finfo.leading+finfo.ascent+finfo.descent;
204
205 if (m_checkBoxHeight<h)
206 {
207 m_TextBaseLineOffset+= (h-m_checkBoxHeight)/2;
208 m_checkBoxHeight= h;
209 }
210
dbfc5b97
SC
211 Rect bounds ;
212 Str255 title ;
213
214 MacPreControlCreate( parent , id , "" , pos , size ,style, validator , name , &bounds , title ) ;
215
216 ListDefSpec listDef;
217 listDef.defType = kListDefUserProcType;
218 if ( macCheckListDefUPP == NULL )
219 {
220 macCheckListDefUPP = NewListDefUPP( wxMacCheckListDefinition );
221 }
222 listDef.u.userProc = macCheckListDefUPP ;
223
224#if TARGET_CARBON
225 Size asize;
226
227
962cbf2e 228 CreateListBoxControl( MAC_WXHWND(parent->MacGetRootWindow()), &bounds, false, 0, 1, false, true,
50b30d83 229 m_checkBoxHeight+2, 14, false, &listDef, (ControlRef *)&m_macControl );
dbfc5b97 230
76a5e5d2 231 GetControlData( (ControlHandle) m_macControl, kControlNoPart, kControlListBoxListHandleTag,
dbfc5b97
SC
232 sizeof(ListHandle), (Ptr) &m_macList, &asize);
233
76a5e5d2
SC
234 SetControlReference( (ControlHandle) m_macControl, (long) this);
235 SetControlVisibility( (ControlHandle) m_macControl, false, false);
dbfc5b97
SC
236
237#else
238
239 long result ;
240
fe3fcb05 241 wxStAppResource resload ;
76a5e5d2 242 m_macControl = ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , title , false ,
dbfc5b97
SC
243 kwxMacListWithVerticalScrollbar , 0 , 0,
244 kControlListBoxProc , (long) this ) ;
76a5e5d2 245 ::GetControlData( (ControlHandle) m_macControl , kControlNoPart , kControlListBoxListHandleTag ,
dbfc5b97
SC
246 sizeof( ListHandle ) , (char*) &m_macList , &result ) ;
247
248 HLock( (Handle) m_macList ) ;
249 ldefHandle ldef ;
250 ldef = (ldefHandle) NewHandle( sizeof(ldefRec) ) ;
76a5e5d2 251 if ( (**(ListHandle)m_macList).listDefProc != NULL )
dbfc5b97
SC
252 {
253 (**ldef).instruction = 0x4EF9; /* JMP instruction */
254 (**ldef).function = (void(*)()) listDef.u.userProc;
76a5e5d2 255 (**(ListHandle)m_macList).listDefProc = (Handle) ldef ;
dbfc5b97
SC
256 }
257
76a5e5d2 258 Point pt = (**(ListHandle)m_macList).cellSize ;
dbfc5b97 259 pt.v = 14 ;
76a5e5d2
SC
260 LCellSize( pt , (ListHandle)m_macList ) ;
261 LAddColumn( 1 , 0 , (ListHandle)m_macList ) ;
2f1ae414 262#endif
dbfc5b97
SC
263 OptionBits options = 0;
264 if ( style & wxLB_MULTIPLE )
265 {
266 options += lNoExtend ;
267 }
268 else if ( style & wxLB_EXTENDED )
269 {
270 options += lExtendDrag ;
271 }
272 else
273 {
274 options = lOnlyOne ;
275 }
76a5e5d2 276 SetListSelectionFlags((ListHandle)m_macList, options);
dbfc5b97
SC
277
278 MacPostControlCreate() ;
279
280 for ( int i = 0 ; i < n ; i++ )
281 {
282 Append( choices[i] ) ;
283 }
284
76a5e5d2 285 LSetDrawingMode( true , (ListHandle) m_macList ) ;
dbfc5b97
SC
286
287 return TRUE;
288}
e9576ca5
SC
289
290// ----------------------------------------------------------------------------
dbfc5b97 291// wxCheckListBox functions
e9576ca5
SC
292// ----------------------------------------------------------------------------
293
dbfc5b97
SC
294bool wxCheckListBox::IsChecked(size_t item) const
295{
296 wxCHECK_MSG( item < m_checks.GetCount(), FALSE,
297 _T("invalid index in wxCheckListBox::IsChecked") );
298
299 return m_checks[item] != 0;
300}
301
302void wxCheckListBox::Check(size_t item, bool check)
303{
304 wxCHECK_RET( item < m_checks.GetCount(),
305 _T("invalid index in wxCheckListBox::Check") );
306
307 // intermediate var is needed to avoid compiler warning with VC++
308 bool isChecked = m_checks[item] != 0;
309 if ( check != isChecked )
310 {
311 m_checks[item] = check;
312
313 MacRedrawControl() ;
314 }
315}
316
317// ----------------------------------------------------------------------------
318// methods forwarded to wxListBox
319// ----------------------------------------------------------------------------
320
321void wxCheckListBox::Delete(int n)
322{
323 wxCHECK_RET( n < GetCount(), _T("invalid index in wxListBox::Delete") );
e9576ca5 324
dbfc5b97 325 wxListBox::Delete(n);
e9576ca5 326
dbfc5b97
SC
327 m_checks.RemoveAt(n);
328}
329
330int wxCheckListBox::DoAppend(const wxString& item)
e9576ca5 331{
50b30d83 332 LSetDrawingMode( false , (ListHandle) m_macList ) ;
dbfc5b97
SC
333 int pos = wxListBox::DoAppend(item);
334
335 // the item is initially unchecked
336 m_checks.Insert(FALSE, pos);
50b30d83 337 LSetDrawingMode( true , (ListHandle) m_macList ) ;
dbfc5b97
SC
338
339 return pos;
e9576ca5
SC
340}
341
dbfc5b97 342void wxCheckListBox::DoInsertItems(const wxArrayString& items, int pos)
e9576ca5 343{
dbfc5b97
SC
344 wxListBox::DoInsertItems(items, pos);
345
346 size_t count = items.GetCount();
347 for ( size_t n = 0; n < count; n++ )
348 {
349 m_checks.Insert(FALSE, pos + n);
350 }
e9576ca5
SC
351}
352
dbfc5b97
SC
353void wxCheckListBox::DoSetItems(const wxArrayString& items, void **clientData)
354{
355 // call it first as it does DoClear()
356 wxListBox::DoSetItems(items, clientData);
357
358 size_t count = items.GetCount();
359 for ( size_t n = 0; n < count; n++ )
360 {
361 m_checks.Add(FALSE);
362 }
363}
e9576ca5 364
dbfc5b97 365void wxCheckListBox::DoClear()
e9576ca5 366{
dbfc5b97 367 m_checks.Empty();
e9576ca5
SC
368}
369
dbfc5b97
SC
370BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
371 EVT_CHAR(wxCheckListBox::OnChar)
372 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
373END_EVENT_TABLE()
374
375// this will only work as soon as
376
377void wxCheckListBox::OnChar(wxKeyEvent& event)
e9576ca5 378{
dbfc5b97
SC
379 if ( event.KeyCode() == WXK_SPACE )
380 {
381 int index = GetSelection() ;
382 if ( index >= 0 )
383 {
384 Check(index, !IsChecked(index) ) ;
385 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, GetId());
386 event.SetInt(index);
387 event.SetEventObject(this);
388 GetEventHandler()->ProcessEvent(event);
389 }
390 }
391 else
392 event.Skip();
e9576ca5
SC
393}
394
dbfc5b97
SC
395void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
396{
397 // clicking on the item selects it, clicking on the checkmark toggles
398 if ( event.GetX() <= 20 /*check width*/ ) {
399 int lineheight ;
400 int topcell ;
401#if TARGET_CARBON
402 Point pt ;
962cbf2e 403 GetListCellSize( (ListHandle)m_macList , &pt ) ;
dbfc5b97
SC
404 lineheight = pt.v ;
405 ListBounds visible ;
962cbf2e 406 GetListVisibleCells( (ListHandle)m_macList , &visible ) ;
dbfc5b97
SC
407 topcell = visible.top ;
408#else
76a5e5d2
SC
409 lineheight = (**(ListHandle)m_macList).cellSize.v ;
410 topcell = (**(ListHandle)m_macList).visible.top ;
dbfc5b97
SC
411#endif
412 size_t nItem = ((size_t)event.GetY()) / lineheight + topcell ;
413
414 if ( nItem < (size_t)m_noItems )
415 {
416 Check(nItem, !IsChecked(nItem) ) ;
417 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, GetId());
418 event.SetInt(nItem);
419 event.SetEventObject(this);
420 GetEventHandler()->ProcessEvent(event);
421 }
422 //else: it's not an error, just click outside of client zone
423 }
424 else {
425 // implement default behaviour: clicking on the item selects it
426 event.Skip();
427 }
428}
e9576ca5 429
dbfc5b97 430#endif // wxUSE_CHECKLISTBOX