]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/checklst.cpp
removed unused variable
[wxWidgets.git] / src / mac / carbon / checklst.cpp
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
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 m_noItems = 0 ; // this will be increased by our append command
187 m_selected = 0;
188
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
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
228 CreateListBoxControl( MAC_WXHWND(parent->MacGetRootWindow()), &bounds, false, 0, 1, false, true,
229 m_checkBoxHeight+2, 14, false, &listDef, (ControlRef *)&m_macControl );
230
231 GetControlData( (ControlHandle) m_macControl, kControlNoPart, kControlListBoxListHandleTag,
232 sizeof(ListHandle), (Ptr) &m_macList, &asize);
233
234 SetControlReference( (ControlHandle) m_macControl, (long) this);
235 SetControlVisibility( (ControlHandle) m_macControl, false, false);
236
237 #else
238
239 long result ;
240
241 wxStAppResource resload ;
242 m_macControl = ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , title , false ,
243 kwxMacListWithVerticalScrollbar , 0 , 0,
244 kControlListBoxProc , (long) this ) ;
245 ::GetControlData( (ControlHandle) m_macControl , kControlNoPart , kControlListBoxListHandleTag ,
246 sizeof( ListHandle ) , (char*) &m_macList , &result ) ;
247
248 HLock( (Handle) m_macList ) ;
249 ldefHandle ldef ;
250 ldef = (ldefHandle) NewHandle( sizeof(ldefRec) ) ;
251 if ( (**(ListHandle)m_macList).listDefProc != NULL )
252 {
253 (**ldef).instruction = 0x4EF9; /* JMP instruction */
254 (**ldef).function = (void(*)()) listDef.u.userProc;
255 (**(ListHandle)m_macList).listDefProc = (Handle) ldef ;
256 }
257
258 Point pt = (**(ListHandle)m_macList).cellSize ;
259 pt.v = 14 ;
260 LCellSize( pt , (ListHandle)m_macList ) ;
261 LAddColumn( 1 , 0 , (ListHandle)m_macList ) ;
262 #endif
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 }
276 SetListSelectionFlags((ListHandle)m_macList, options);
277
278 MacPostControlCreate() ;
279
280 for ( int i = 0 ; i < n ; i++ )
281 {
282 Append( choices[i] ) ;
283 }
284
285 LSetDrawingMode( true , (ListHandle) m_macList ) ;
286
287 return TRUE;
288 }
289
290 // ----------------------------------------------------------------------------
291 // wxCheckListBox functions
292 // ----------------------------------------------------------------------------
293
294 bool 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
302 void 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
321 void wxCheckListBox::Delete(int n)
322 {
323 wxCHECK_RET( n < GetCount(), _T("invalid index in wxListBox::Delete") );
324
325 wxListBox::Delete(n);
326
327 m_checks.RemoveAt(n);
328 }
329
330 int wxCheckListBox::DoAppend(const wxString& item)
331 {
332 LSetDrawingMode( false , (ListHandle) m_macList ) ;
333 int pos = wxListBox::DoAppend(item);
334
335 // the item is initially unchecked
336 m_checks.Insert(FALSE, pos);
337 LSetDrawingMode( true , (ListHandle) m_macList ) ;
338
339 return pos;
340 }
341
342 void wxCheckListBox::DoInsertItems(const wxArrayString& items, int pos)
343 {
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 }
351 }
352
353 void 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 }
364
365 void wxCheckListBox::DoClear()
366 {
367 m_checks.Empty();
368 }
369
370 BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
371 EVT_CHAR(wxCheckListBox::OnChar)
372 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
373 END_EVENT_TABLE()
374
375 // this will only work as soon as
376
377 void wxCheckListBox::OnChar(wxKeyEvent& event)
378 {
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();
393 }
394
395 void 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 ;
403 GetListCellSize( (ListHandle)m_macList , &pt ) ;
404 lineheight = pt.v ;
405 ListBounds visible ;
406 GetListVisibleCells( (ListHandle)m_macList , &visible ) ;
407 topcell = visible.top ;
408 #else
409 lineheight = (**(ListHandle)m_macList).cellSize.v ;
410 topcell = (**(ListHandle)m_macList).visible.top ;
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 }
429
430 #endif // wxUSE_CHECKLISTBOX