]> git.saurik.com Git - wxWidgets.git/blob - src/mac/checklst.cpp
Use a compatible bitmap if a depth of -1 is given
[wxWidgets.git] / src / mac / 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 #include "wx/arrstr.h"
26
27 #include "wx/mac/uma.h"
28 #include "Appearance.h"
29
30 // ============================================================================
31 // implementation of wxCheckListBox
32 // ============================================================================
33
34 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
35
36 const short kwxMacListWithVerticalScrollbar = 128 ;
37 const short kwxMacListItemHeight = 14 ;
38 const short kwxMacListCheckboxWidth = 14 ;
39
40 #if PRAGMA_STRUCT_ALIGN
41 #pragma options align=mac68k
42 #elif PRAGMA_STRUCT_PACKPUSH
43 #pragma pack(push, 2)
44 #elif PRAGMA_STRUCT_PACK
45 #pragma pack(2)
46 #endif
47
48 typedef struct {
49 unsigned short instruction;
50 void (*function)();
51 } ldefRec, *ldefPtr, **ldefHandle;
52
53 #if PRAGMA_STRUCT_ALIGN
54 #pragma options align=reset
55 #elif PRAGMA_STRUCT_PACKPUSH
56 #pragma pack(pop)
57 #elif PRAGMA_STRUCT_PACK
58 #pragma pack()
59 #endif
60
61 extern "C"
62 {
63 static pascal void wxMacCheckListDefinition( short message, Boolean isSelected, Rect *drawRect,
64 Cell cell, short dataOffset, short dataLength,
65 ListHandle listHandle ) ;
66 }
67
68 static pascal void wxMacCheckListDefinition( short message, Boolean isSelected, Rect *drawRect,
69 Cell cell, short dataOffset, short dataLength,
70 ListHandle listHandle )
71 {
72 GrafPtr savePort;
73 GrafPtr grafPtr;
74 RgnHandle savedClipRegion;
75 SInt32 savedPenMode;
76 wxCheckListBox* list;
77 GetPort(&savePort);
78 SetPort((**listHandle).port);
79 grafPtr = (**listHandle).port ;
80 // typecast our refCon
81 list = (wxCheckListBox*) GetControlReference( (ControlHandle) GetListRefCon(listHandle) );
82
83 // Calculate the cell rect.
84
85 switch( message ) {
86 case lInitMsg:
87 break;
88
89 case lCloseMsg:
90 break;
91
92 case lDrawMsg:
93 {
94 const wxString text = list->m_stringArray[cell.v] ;
95 int checked = list->m_checks[cell.v] ;
96
97 // Save the current clip region, and set the clip region to the area we are about
98 // to draw.
99
100 savedClipRegion = NewRgn();
101 GetClip( savedClipRegion );
102
103 ClipRect( drawRect );
104 EraseRect( drawRect );
105
106 const wxFont& font = list->GetFont();
107 if ( font.Ok() )
108 {
109 ::TextFont( font.GetMacFontNum() ) ;
110 ::TextSize( font.GetMacFontSize()) ;
111 ::TextFace( font.GetMacFontStyle() ) ;
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 const wxArrayString& choices,
181 long style,
182 const wxValidator& validator,
183 const wxString &name)
184 {
185 wxCArrayString chs(choices);
186
187 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
188 style, validator, name);
189 }
190
191 bool wxCheckListBox::Create(wxWindow *parent,
192 wxWindowID id,
193 const wxPoint &pos,
194 const wxSize &size,
195 int n,
196 const wxString choices[],
197 long style,
198 const wxValidator& validator,
199 const wxString &name)
200 {
201 if ( !wxCheckListBoxBase::Create(parent, id, pos, size,
202 n, choices, style, validator, name) )
203 return false;
204
205 m_noItems = 0 ; // this will be increased by our append command
206 m_selected = 0;
207
208 m_checkBoxWidth = 12;
209 m_checkBoxHeight= 10;
210
211 long h = m_checkBoxHeight ;
212 #if TARGET_CARBON
213 GetThemeMetric(kThemeMetricCheckBoxWidth,(long *)&m_checkBoxWidth);
214 GetThemeMetric(kThemeMetricCheckBoxHeight,&h);
215 #endif
216
217 const wxFont& font = GetFont();
218
219 FontInfo finfo;
220 FetchFontInfo(font.GetMacFontNum(),font.GetMacFontSize(),font.GetMacFontStyle(),&finfo);
221
222 m_TextBaseLineOffset= finfo.leading+finfo.ascent;
223 m_checkBoxHeight= finfo.leading+finfo.ascent+finfo.descent;
224
225 if (m_checkBoxHeight<h)
226 {
227 m_TextBaseLineOffset+= (h-m_checkBoxHeight)/2;
228 m_checkBoxHeight= h;
229 }
230
231 Rect bounds ;
232 Str255 title ;
233
234 MacPreControlCreate( parent , id , wxEmptyString , pos , size ,style, validator , name , &bounds , title ) ;
235
236 ListDefSpec listDef;
237 listDef.defType = kListDefUserProcType;
238 if ( macCheckListDefUPP == NULL )
239 {
240 macCheckListDefUPP = NewListDefUPP( wxMacCheckListDefinition );
241 }
242 listDef.u.userProc = macCheckListDefUPP ;
243
244 #if TARGET_CARBON
245 Size asize;
246
247
248 CreateListBoxControl( MAC_WXHWND(parent->MacGetRootWindow()), &bounds, false, 0, 1, false, true,
249 m_checkBoxHeight+2, 14, false, &listDef, (ControlRef *)&m_macControl );
250
251 GetControlData( (ControlHandle) m_macControl, kControlNoPart, kControlListBoxListHandleTag,
252 sizeof(ListHandle), (Ptr) &m_macList, &asize);
253
254 SetControlReference( (ControlHandle) m_macControl, (long) this);
255 SetControlVisibility( (ControlHandle) m_macControl, false, false);
256
257 #else
258
259 long result ;
260
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 ) ;
267
268 HLock( (Handle) m_macList ) ;
269 ldefHandle ldef ;
270 ldef = (ldefHandle) NewHandle( sizeof(ldefRec) ) ;
271 if ( (**(ListHandle)m_macList).listDefProc != NULL )
272 {
273 (**ldef).instruction = 0x4EF9; /* JMP instruction */
274 (**ldef).function = (void(*)()) listDef.u.userProc;
275 (**(ListHandle)m_macList).listDefProc = (Handle) ldef ;
276 }
277
278 Point pt = (**(ListHandle)m_macList).cellSize ;
279 pt.v = 14 ;
280 LCellSize( pt , (ListHandle)m_macList ) ;
281 LAddColumn( 1 , 0 , (ListHandle)m_macList ) ;
282 #endif
283 OptionBits options = 0;
284 if ( style & wxLB_MULTIPLE )
285 {
286 options += lNoExtend ;
287 }
288 else if ( style & wxLB_EXTENDED )
289 {
290 options += lExtendDrag ;
291 }
292 else
293 {
294 options = (OptionBits) lOnlyOne ;
295 }
296 SetListSelectionFlags((ListHandle)m_macList, options);
297
298 MacPostControlCreate() ;
299
300 for ( int i = 0 ; i < n ; i++ )
301 {
302 Append( choices[i] ) ;
303 }
304
305 LSetDrawingMode( true , (ListHandle) m_macList ) ;
306
307 return TRUE;
308 }
309
310 // ----------------------------------------------------------------------------
311 // wxCheckListBox functions
312 // ----------------------------------------------------------------------------
313
314 bool wxCheckListBox::IsChecked(size_t item) const
315 {
316 wxCHECK_MSG( item < m_checks.GetCount(), FALSE,
317 _T("invalid index in wxCheckListBox::IsChecked") );
318
319 return m_checks[item] != 0;
320 }
321
322 void wxCheckListBox::Check(size_t item, bool check)
323 {
324 wxCHECK_RET( item < m_checks.GetCount(),
325 _T("invalid index in wxCheckListBox::Check") );
326
327 // intermediate var is needed to avoid compiler warning with VC++
328 bool isChecked = m_checks[item] != 0;
329 if ( check != isChecked )
330 {
331 m_checks[item] = check;
332
333 MacRedrawControl() ;
334 }
335 }
336
337 // ----------------------------------------------------------------------------
338 // methods forwarded to wxListBox
339 // ----------------------------------------------------------------------------
340
341 void wxCheckListBox::Delete(int n)
342 {
343 wxCHECK_RET( n < GetCount(), _T("invalid index in wxListBox::Delete") );
344
345 wxListBox::Delete(n);
346
347 m_checks.RemoveAt(n);
348 }
349
350 int wxCheckListBox::DoAppend(const wxString& item)
351 {
352 LSetDrawingMode( false , (ListHandle) m_macList ) ;
353 int pos = wxListBox::DoAppend(item);
354
355 // the item is initially unchecked
356 m_checks.Insert(FALSE, pos);
357 LSetDrawingMode( true , (ListHandle) m_macList ) ;
358
359 return pos;
360 }
361
362 void wxCheckListBox::DoInsertItems(const wxArrayString& items, int pos)
363 {
364 wxListBox::DoInsertItems(items, pos);
365
366 size_t count = items.GetCount();
367 for ( size_t n = 0; n < count; n++ )
368 {
369 m_checks.Insert(FALSE, pos + n);
370 }
371 }
372
373 void wxCheckListBox::DoSetItems(const wxArrayString& items, void **clientData)
374 {
375 // call it first as it does DoClear()
376 wxListBox::DoSetItems(items, clientData);
377
378 size_t count = items.GetCount();
379 for ( size_t n = 0; n < count; n++ )
380 {
381 m_checks.Add(FALSE);
382 }
383 }
384
385 void wxCheckListBox::DoClear()
386 {
387 m_checks.Empty();
388 }
389
390 BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
391 EVT_CHAR(wxCheckListBox::OnChar)
392 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
393 END_EVENT_TABLE()
394
395 // this will only work as soon as
396
397 void wxCheckListBox::OnChar(wxKeyEvent& event)
398 {
399 if ( event.GetKeyCode() == WXK_SPACE )
400 {
401 int index = GetSelection() ;
402 if ( index >= 0 )
403 {
404 Check(index, !IsChecked(index) ) ;
405 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, GetId());
406 event.SetInt(index);
407 event.SetEventObject(this);
408 GetEventHandler()->ProcessEvent(event);
409 }
410 }
411 else
412 event.Skip();
413 }
414
415 void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
416 {
417 // clicking on the item selects it, clicking on the checkmark toggles
418 if ( event.GetX() <= 20 /*check width*/ ) {
419 int lineheight ;
420 int topcell ;
421 #if TARGET_CARBON
422 Point pt ;
423 GetListCellSize( (ListHandle)m_macList , &pt ) ;
424 lineheight = pt.v ;
425 ListBounds visible ;
426 GetListVisibleCells( (ListHandle)m_macList , &visible ) ;
427 topcell = visible.top ;
428 #else
429 lineheight = (**(ListHandle)m_macList).cellSize.v ;
430 topcell = (**(ListHandle)m_macList).visible.top ;
431 #endif
432 size_t nItem = ((size_t)event.GetY()) / lineheight + topcell ;
433
434 if ( nItem < (size_t)m_noItems )
435 {
436 Check(nItem, !IsChecked(nItem) ) ;
437 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, GetId());
438 event.SetInt(nItem);
439 event.SetEventObject(this);
440 GetEventHandler()->ProcessEvent(event);
441 }
442 //else: it's not an error, just click outside of client zone
443 }
444 else {
445 // implement default behaviour: clicking on the item selects it
446 event.Skip();
447 }
448 }
449
450 #endif // wxUSE_CHECKLISTBOX