]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/checklst.cpp
new carbon implementation (unicode support)
[wxWidgets.git] / src / mac / carbon / checklst.cpp
CommitLineData
e9576ca5
SC
1///////////////////////////////////////////////////////////////////////////////
2// Name: checklst.cpp
3// Purpose: implementation of wxCheckListBox class
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
e9576ca5
SC
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;
e40298d5
JS
76 GetPort(&savePort);
77 SetPort((**listHandle).port);
78 grafPtr = (**listHandle).port ;
dbfc5b97
SC
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
e40298d5 105 wxFontRefData * font = (wxFontRefData*) (list->GetFont().GetRefData()) ;
50b30d83 106
e40298d5
JS
107 if ( font )
108 {
109 ::TextFont( font->m_macFontNum ) ;
110 ::TextSize( font->m_macFontSize) ;
111 ::TextFace( font->m_macFontStyle ) ;
50b30d83
SC
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 125 DrawThemeButton(&checkRect,kThemeCheckBox,
e40298d5
JS
126 &info,NULL,NULL, NULL,0);
127
128 MoveTo(drawRect->left + 2 + list->m_checkBoxWidth+2, drawRect->top + list->m_TextBaseLineOffset );
129
130 DrawText(text, 0 , text.Length());
dbfc5b97
SC
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 );
e40298d5
JS
145 }
146 break;
dbfc5b97
SC
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{
b45ed7a2
VZ
186 if ( !wxCheckListBoxBase::Create(parent, id, pos, size,
187 n, choices, style, validator, name) )
188 return false;
189
dbfc5b97
SC
190 m_noItems = 0 ; // this will be increased by our append command
191 m_selected = 0;
192
50b30d83
SC
193 m_checkBoxWidth = 12;
194 m_checkBoxHeight= 10;
195
196 long h = m_checkBoxHeight ;
197#if TARGET_CARBON
198 GetThemeMetric(kThemeMetricCheckBoxWidth,(long *)&m_checkBoxWidth);
199 GetThemeMetric(kThemeMetricCheckBoxHeight,&h);
200#endif
e40298d5 201 wxFontRefData * font = (wxFontRefData*) (GetFont().GetRefData()) ;
50b30d83
SC
202
203 FontInfo finfo;
204 FetchFontInfo(font->m_macFontNum,short(font->m_macFontSize),font->m_macFontStyle,&finfo);
205
206 m_TextBaseLineOffset= finfo.leading+finfo.ascent;
207 m_checkBoxHeight= finfo.leading+finfo.ascent+finfo.descent;
208
e40298d5
JS
209 if (m_checkBoxHeight<h)
210 {
211 m_TextBaseLineOffset+= (h-m_checkBoxHeight)/2;
212 m_checkBoxHeight= h;
213 }
214
dbfc5b97
SC
215 Rect bounds ;
216 Str255 title ;
217
427ff662 218 MacPreControlCreate( parent , id , wxEmptyString , pos , size ,style, validator , name , &bounds , title ) ;
dbfc5b97
SC
219
220 ListDefSpec listDef;
221 listDef.defType = kListDefUserProcType;
222 if ( macCheckListDefUPP == NULL )
223 {
224 macCheckListDefUPP = NewListDefUPP( wxMacCheckListDefinition );
225 }
226 listDef.u.userProc = macCheckListDefUPP ;
227
228#if TARGET_CARBON
229 Size asize;
230
231
962cbf2e 232 CreateListBoxControl( MAC_WXHWND(parent->MacGetRootWindow()), &bounds, false, 0, 1, false, true,
50b30d83 233 m_checkBoxHeight+2, 14, false, &listDef, (ControlRef *)&m_macControl );
dbfc5b97 234
76a5e5d2 235 GetControlData( (ControlHandle) m_macControl, kControlNoPart, kControlListBoxListHandleTag,
dbfc5b97
SC
236 sizeof(ListHandle), (Ptr) &m_macList, &asize);
237
76a5e5d2
SC
238 SetControlReference( (ControlHandle) m_macControl, (long) this);
239 SetControlVisibility( (ControlHandle) m_macControl, false, false);
dbfc5b97
SC
240
241#else
242
243 long result ;
244
fe3fcb05 245 wxStAppResource resload ;
76a5e5d2 246 m_macControl = ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , title , false ,
dbfc5b97
SC
247 kwxMacListWithVerticalScrollbar , 0 , 0,
248 kControlListBoxProc , (long) this ) ;
76a5e5d2 249 ::GetControlData( (ControlHandle) m_macControl , kControlNoPart , kControlListBoxListHandleTag ,
dbfc5b97
SC
250 sizeof( ListHandle ) , (char*) &m_macList , &result ) ;
251
252 HLock( (Handle) m_macList ) ;
253 ldefHandle ldef ;
254 ldef = (ldefHandle) NewHandle( sizeof(ldefRec) ) ;
76a5e5d2 255 if ( (**(ListHandle)m_macList).listDefProc != NULL )
dbfc5b97
SC
256 {
257 (**ldef).instruction = 0x4EF9; /* JMP instruction */
258 (**ldef).function = (void(*)()) listDef.u.userProc;
76a5e5d2 259 (**(ListHandle)m_macList).listDefProc = (Handle) ldef ;
dbfc5b97
SC
260 }
261
76a5e5d2 262 Point pt = (**(ListHandle)m_macList).cellSize ;
dbfc5b97 263 pt.v = 14 ;
76a5e5d2
SC
264 LCellSize( pt , (ListHandle)m_macList ) ;
265 LAddColumn( 1 , 0 , (ListHandle)m_macList ) ;
2f1ae414 266#endif
dbfc5b97
SC
267 OptionBits options = 0;
268 if ( style & wxLB_MULTIPLE )
269 {
270 options += lNoExtend ;
271 }
272 else if ( style & wxLB_EXTENDED )
273 {
274 options += lExtendDrag ;
275 }
276 else
277 {
2b5f62a0 278 options = (OptionBits) lOnlyOne ;
dbfc5b97 279 }
76a5e5d2 280 SetListSelectionFlags((ListHandle)m_macList, options);
dbfc5b97
SC
281
282 MacPostControlCreate() ;
283
284 for ( int i = 0 ; i < n ; i++ )
285 {
286 Append( choices[i] ) ;
287 }
288
76a5e5d2 289 LSetDrawingMode( true , (ListHandle) m_macList ) ;
dbfc5b97
SC
290
291 return TRUE;
292}
e9576ca5
SC
293
294// ----------------------------------------------------------------------------
dbfc5b97 295// wxCheckListBox functions
e9576ca5
SC
296// ----------------------------------------------------------------------------
297
dbfc5b97
SC
298bool wxCheckListBox::IsChecked(size_t item) const
299{
300 wxCHECK_MSG( item < m_checks.GetCount(), FALSE,
301 _T("invalid index in wxCheckListBox::IsChecked") );
302
303 return m_checks[item] != 0;
304}
305
306void wxCheckListBox::Check(size_t item, bool check)
307{
308 wxCHECK_RET( item < m_checks.GetCount(),
309 _T("invalid index in wxCheckListBox::Check") );
310
311 // intermediate var is needed to avoid compiler warning with VC++
312 bool isChecked = m_checks[item] != 0;
313 if ( check != isChecked )
314 {
315 m_checks[item] = check;
316
317 MacRedrawControl() ;
318 }
319}
320
321// ----------------------------------------------------------------------------
322// methods forwarded to wxListBox
323// ----------------------------------------------------------------------------
324
325void wxCheckListBox::Delete(int n)
326{
327 wxCHECK_RET( n < GetCount(), _T("invalid index in wxListBox::Delete") );
e9576ca5 328
dbfc5b97 329 wxListBox::Delete(n);
e9576ca5 330
dbfc5b97
SC
331 m_checks.RemoveAt(n);
332}
333
334int wxCheckListBox::DoAppend(const wxString& item)
e9576ca5 335{
50b30d83 336 LSetDrawingMode( false , (ListHandle) m_macList ) ;
dbfc5b97
SC
337 int pos = wxListBox::DoAppend(item);
338
339 // the item is initially unchecked
340 m_checks.Insert(FALSE, pos);
50b30d83 341 LSetDrawingMode( true , (ListHandle) m_macList ) ;
dbfc5b97
SC
342
343 return pos;
e9576ca5
SC
344}
345
dbfc5b97 346void wxCheckListBox::DoInsertItems(const wxArrayString& items, int pos)
e9576ca5 347{
dbfc5b97
SC
348 wxListBox::DoInsertItems(items, pos);
349
350 size_t count = items.GetCount();
351 for ( size_t n = 0; n < count; n++ )
352 {
353 m_checks.Insert(FALSE, pos + n);
354 }
e9576ca5
SC
355}
356
dbfc5b97
SC
357void wxCheckListBox::DoSetItems(const wxArrayString& items, void **clientData)
358{
359 // call it first as it does DoClear()
360 wxListBox::DoSetItems(items, clientData);
361
362 size_t count = items.GetCount();
363 for ( size_t n = 0; n < count; n++ )
364 {
365 m_checks.Add(FALSE);
366 }
367}
e9576ca5 368
dbfc5b97 369void wxCheckListBox::DoClear()
e9576ca5 370{
dbfc5b97 371 m_checks.Empty();
e9576ca5
SC
372}
373
dbfc5b97
SC
374BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
375 EVT_CHAR(wxCheckListBox::OnChar)
376 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
377END_EVENT_TABLE()
378
379// this will only work as soon as
380
381void wxCheckListBox::OnChar(wxKeyEvent& event)
e9576ca5 382{
e40298d5 383 if ( event.GetKeyCode() == WXK_SPACE )
dbfc5b97 384 {
e40298d5
JS
385 int index = GetSelection() ;
386 if ( index >= 0 )
387 {
388 Check(index, !IsChecked(index) ) ;
389 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, GetId());
390 event.SetInt(index);
391 event.SetEventObject(this);
392 GetEventHandler()->ProcessEvent(event);
393 }
dbfc5b97 394 }
e40298d5
JS
395 else
396 event.Skip();
e9576ca5
SC
397}
398
dbfc5b97
SC
399void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
400{
e40298d5
JS
401 // clicking on the item selects it, clicking on the checkmark toggles
402 if ( event.GetX() <= 20 /*check width*/ ) {
403 int lineheight ;
404 int topcell ;
dbfc5b97 405#if TARGET_CARBON
e40298d5
JS
406 Point pt ;
407 GetListCellSize( (ListHandle)m_macList , &pt ) ;
408 lineheight = pt.v ;
409 ListBounds visible ;
410 GetListVisibleCells( (ListHandle)m_macList , &visible ) ;
411 topcell = visible.top ;
dbfc5b97 412#else
e40298d5
JS
413 lineheight = (**(ListHandle)m_macList).cellSize.v ;
414 topcell = (**(ListHandle)m_macList).visible.top ;
dbfc5b97 415#endif
e40298d5
JS
416 size_t nItem = ((size_t)event.GetY()) / lineheight + topcell ;
417
418 if ( nItem < (size_t)m_noItems )
419 {
420 Check(nItem, !IsChecked(nItem) ) ;
421 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, GetId());
422 event.SetInt(nItem);
423 event.SetEventObject(this);
424 GetEventHandler()->ProcessEvent(event);
425 }
426 //else: it's not an error, just click outside of client zone
427 }
428 else {
429 // implement default behaviour: clicking on the item selects it
430 event.Skip();
dbfc5b97 431 }
dbfc5b97 432}
e9576ca5 433
dbfc5b97 434#endif // wxUSE_CHECKLISTBOX