]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/checklst.cpp
new carbon implementation (unicode support)
[wxWidgets.git] / src / mac / carbon / checklst.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: checklst.cpp
3 // Purpose: implementation of wxCheckListBox class
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // headers & declarations
14 // ============================================================================
15
16 #ifdef __GNUG__
17 #pragma implementation "checklst.h"
18 #endif
19
20 #include "wx/defs.h"
21
22 #if wxUSE_CHECKLISTBOX
23
24 #include "wx/checklst.h"
25
26 #include "wx/mac/uma.h"
27 #include "Appearance.h"
28
29 // ============================================================================
30 // implementation of wxCheckListBox
31 // ============================================================================
32
33 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
34
35 const short kwxMacListWithVerticalScrollbar = 128 ;
36 const short kwxMacListItemHeight = 14 ;
37 const short kwxMacListCheckboxWidth = 14 ;
38
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
47 typedef struct {
48 unsigned short instruction;
49 void (*function)();
50 } ldefRec, *ldefPtr, **ldefHandle;
51
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
60 extern "C"
61 {
62 static pascal void wxMacCheckListDefinition( short message, Boolean isSelected, Rect *drawRect,
63 Cell cell, short dataOffset, short dataLength,
64 ListHandle listHandle ) ;
65 }
66
67 static pascal void wxMacCheckListDefinition( short message, Boolean isSelected, Rect *drawRect,
68 Cell cell, short dataOffset, short dataLength,
69 ListHandle listHandle )
70 {
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
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
114 ThemeButtonDrawInfo info ;
115 info.state = kThemeStateActive ;
116 info.value = checked ? kThemeButtonOn : kThemeButtonOff ;
117 info.adornment = kThemeAdornmentNone ;
118 Rect checkRect = *drawRect ;
119
120
121 checkRect.left +=0 ;
122 checkRect.top +=0 ;
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);
127
128 MoveTo(drawRect->left + 2 + list->m_checkBoxWidth+2, drawRect->top + list->m_TextBaseLineOffset );
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 ) {
135 savedPenMode = GetPortPenMode( (CGrafPtr) grafPtr );
136 SetPortPenMode( (CGrafPtr) grafPtr, hilitetransfermode );
137 PaintRect( drawRect );
138 SetPortPenMode( (CGrafPtr) grafPtr, savedPenMode );
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 );
153 savedPenMode = GetPortPenMode( (CGrafPtr) grafPtr );
154 SetPortPenMode( (CGrafPtr) grafPtr, hilitetransfermode );
155 PaintRect( drawRect );
156 SetPortPenMode( (CGrafPtr) grafPtr, savedPenMode );
157 break;
158 default :
159 break ;
160 }
161 SetPort(savePort);
162 }
163
164 extern "C" void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon) ;
165
166 static ListDefUPP macCheckListDefUPP = NULL ;
167
168 // ----------------------------------------------------------------------------
169 // creation
170 // ----------------------------------------------------------------------------
171
172 void wxCheckListBox::Init()
173 {
174 }
175
176 bool 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 if ( !wxCheckListBoxBase::Create(parent, id, pos, size,
187 n, choices, style, validator, name) )
188 return false;
189
190 m_noItems = 0 ; // this will be increased by our append command
191 m_selected = 0;
192
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
201 wxFontRefData * font = (wxFontRefData*) (GetFont().GetRefData()) ;
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
209 if (m_checkBoxHeight<h)
210 {
211 m_TextBaseLineOffset+= (h-m_checkBoxHeight)/2;
212 m_checkBoxHeight= h;
213 }
214
215 Rect bounds ;
216 Str255 title ;
217
218 MacPreControlCreate( parent , id , wxEmptyString , pos , size ,style, validator , name , &bounds , title ) ;
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
232 CreateListBoxControl( MAC_WXHWND(parent->MacGetRootWindow()), &bounds, false, 0, 1, false, true,
233 m_checkBoxHeight+2, 14, false, &listDef, (ControlRef *)&m_macControl );
234
235 GetControlData( (ControlHandle) m_macControl, kControlNoPart, kControlListBoxListHandleTag,
236 sizeof(ListHandle), (Ptr) &m_macList, &asize);
237
238 SetControlReference( (ControlHandle) m_macControl, (long) this);
239 SetControlVisibility( (ControlHandle) m_macControl, false, false);
240
241 #else
242
243 long result ;
244
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 ) ;
251
252 HLock( (Handle) m_macList ) ;
253 ldefHandle ldef ;
254 ldef = (ldefHandle) NewHandle( sizeof(ldefRec) ) ;
255 if ( (**(ListHandle)m_macList).listDefProc != NULL )
256 {
257 (**ldef).instruction = 0x4EF9; /* JMP instruction */
258 (**ldef).function = (void(*)()) listDef.u.userProc;
259 (**(ListHandle)m_macList).listDefProc = (Handle) ldef ;
260 }
261
262 Point pt = (**(ListHandle)m_macList).cellSize ;
263 pt.v = 14 ;
264 LCellSize( pt , (ListHandle)m_macList ) ;
265 LAddColumn( 1 , 0 , (ListHandle)m_macList ) ;
266 #endif
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 {
278 options = (OptionBits) lOnlyOne ;
279 }
280 SetListSelectionFlags((ListHandle)m_macList, options);
281
282 MacPostControlCreate() ;
283
284 for ( int i = 0 ; i < n ; i++ )
285 {
286 Append( choices[i] ) ;
287 }
288
289 LSetDrawingMode( true , (ListHandle) m_macList ) ;
290
291 return TRUE;
292 }
293
294 // ----------------------------------------------------------------------------
295 // wxCheckListBox functions
296 // ----------------------------------------------------------------------------
297
298 bool 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
306 void 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
325 void wxCheckListBox::Delete(int n)
326 {
327 wxCHECK_RET( n < GetCount(), _T("invalid index in wxListBox::Delete") );
328
329 wxListBox::Delete(n);
330
331 m_checks.RemoveAt(n);
332 }
333
334 int wxCheckListBox::DoAppend(const wxString& item)
335 {
336 LSetDrawingMode( false , (ListHandle) m_macList ) ;
337 int pos = wxListBox::DoAppend(item);
338
339 // the item is initially unchecked
340 m_checks.Insert(FALSE, pos);
341 LSetDrawingMode( true , (ListHandle) m_macList ) ;
342
343 return pos;
344 }
345
346 void wxCheckListBox::DoInsertItems(const wxArrayString& items, int pos)
347 {
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 }
355 }
356
357 void 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 }
368
369 void wxCheckListBox::DoClear()
370 {
371 m_checks.Empty();
372 }
373
374 BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
375 EVT_CHAR(wxCheckListBox::OnChar)
376 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
377 END_EVENT_TABLE()
378
379 // this will only work as soon as
380
381 void wxCheckListBox::OnChar(wxKeyEvent& event)
382 {
383 if ( event.GetKeyCode() == WXK_SPACE )
384 {
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 }
394 }
395 else
396 event.Skip();
397 }
398
399 void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
400 {
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 ;
405 #if TARGET_CARBON
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 ;
412 #else
413 lineheight = (**(ListHandle)m_macList).cellSize.v ;
414 topcell = (**(ListHandle)m_macList).visible.top ;
415 #endif
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();
431 }
432 }
433
434 #endif // wxUSE_CHECKLISTBOX