]> git.saurik.com Git - wxWidgets.git/blob - src/mac/classic/checklst.cpp
Back to previous call with -1 replacement.
[wxWidgets.git] / src / mac / classic / checklst.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/classic/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 #include "wx/wxprec.h"
17
18 #if wxUSE_CHECKLISTBOX
19
20 #include "wx/checklst.h"
21
22 #ifndef WX_PRECOMP
23 #include "wx/arrstr.h"
24 #endif
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 wxCheckListBox* list;
72 list = (wxCheckListBox*) GetControlReference( (ControlHandle) GetListRefCon(listHandle) );
73 if ( list == NULL )
74 return ;
75
76 GrafPtr savePort;
77 GrafPtr grafPtr;
78 RgnHandle savedClipRegion;
79 SInt32 savedPenMode;
80 GetPort(&savePort);
81 SetPort((**listHandle).port);
82 grafPtr = (**listHandle).port ;
83 // typecast our refCon
84
85 // Calculate the cell rect.
86
87 switch( message ) {
88 case lInitMsg:
89 break;
90
91 case lCloseMsg:
92 break;
93
94 case lDrawMsg:
95 {
96 const wxString text = list->m_stringArray[cell.v] ;
97 int checked = list->m_checks[cell.v] ;
98
99 // Save the current clip region, and set the clip region to the area we are about
100 // to draw.
101
102 savedClipRegion = NewRgn();
103 GetClip( savedClipRegion );
104
105 ClipRect( drawRect );
106 EraseRect( drawRect );
107
108 const wxFont& font = list->GetFont();
109 if ( font.Ok() )
110 {
111 ::TextFont( font.GetMacFontNum() ) ;
112 ::TextSize( font.GetMacFontSize()) ;
113 ::TextFace( font.GetMacFontStyle() ) ;
114 }
115
116 ThemeButtonDrawInfo info ;
117 info.state = kThemeStateActive ;
118 info.value = checked ? kThemeButtonOn : kThemeButtonOff ;
119 info.adornment = kThemeAdornmentNone ;
120 Rect checkRect = *drawRect ;
121
122
123 checkRect.left +=0 ;
124 checkRect.top +=0 ;
125 checkRect.right = checkRect.left + list->m_checkBoxWidth ;
126 checkRect.bottom = checkRect.top + list->m_checkBoxHeight ;
127 DrawThemeButton(&checkRect,kThemeCheckBox,
128 &info,NULL,NULL, NULL,0);
129
130 MoveTo(drawRect->left + 2 + list->m_checkBoxWidth+2, drawRect->top + list->m_TextBaseLineOffset );
131
132 DrawText(text, 0 , text.length());
133 // If the cell is hilited, do the hilite now. Paint the cell contents with the
134 // appropriate QuickDraw transform mode.
135
136 if( isSelected ) {
137 savedPenMode = GetPortPenMode( (CGrafPtr) grafPtr );
138 SetPortPenMode( (CGrafPtr) grafPtr, hilitetransfermode );
139 PaintRect( drawRect );
140 SetPortPenMode( (CGrafPtr) grafPtr, savedPenMode );
141 }
142
143 // Restore the saved clip region.
144
145 SetClip( savedClipRegion );
146 DisposeRgn( savedClipRegion );
147 }
148 break;
149 case lHiliteMsg:
150
151 // Hilite or unhilite the cell. Paint the cell contents with the
152 // appropriate QuickDraw transform mode.
153
154 GetPort( &grafPtr );
155 savedPenMode = GetPortPenMode( (CGrafPtr) grafPtr );
156 SetPortPenMode( (CGrafPtr) grafPtr, hilitetransfermode );
157 PaintRect( drawRect );
158 SetPortPenMode( (CGrafPtr) grafPtr, savedPenMode );
159 break;
160 default :
161 break ;
162 }
163 SetPort(savePort);
164 }
165
166 extern "C" void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon) ;
167
168 static ListDefUPP macCheckListDefUPP = NULL ;
169
170 // ----------------------------------------------------------------------------
171 // creation
172 // ----------------------------------------------------------------------------
173
174 void wxCheckListBox::Init()
175 {
176 }
177
178 bool wxCheckListBox::Create(wxWindow *parent,
179 wxWindowID id,
180 const wxPoint &pos,
181 const wxSize &size,
182 const wxArrayString& choices,
183 long style,
184 const wxValidator& validator,
185 const wxString &name)
186 {
187 wxCArrayString chs(choices);
188
189 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
190 style, validator, name);
191 }
192
193 bool wxCheckListBox::Create(wxWindow *parent,
194 wxWindowID id,
195 const wxPoint &pos,
196 const wxSize &size,
197 int n,
198 const wxString choices[],
199 long style,
200 const wxValidator& validator,
201 const wxString &name)
202 {
203 if ( !wxCheckListBoxBase::Create(parent, id, pos, size,
204 n, choices, style, validator, name) )
205 return false;
206
207 m_noItems = 0 ; // this will be increased by our append command
208 m_selected = 0;
209
210 m_checkBoxWidth = 12;
211 m_checkBoxHeight= 10;
212
213 long h = m_checkBoxHeight ;
214 #if TARGET_CARBON
215 GetThemeMetric(kThemeMetricCheckBoxWidth,(long *)&m_checkBoxWidth);
216 GetThemeMetric(kThemeMetricCheckBoxHeight,&h);
217 #endif
218
219 const wxFont& font = GetFont();
220
221 FontInfo finfo;
222 FetchFontInfo(font.GetMacFontNum(),font.GetMacFontSize(),font.GetMacFontStyle(),&finfo);
223
224 m_TextBaseLineOffset= finfo.leading+finfo.ascent;
225 m_checkBoxHeight= finfo.leading+finfo.ascent+finfo.descent;
226
227 if (m_checkBoxHeight<h)
228 {
229 m_TextBaseLineOffset+= (h-m_checkBoxHeight)/2;
230 m_checkBoxHeight= h;
231 }
232
233 Rect bounds ;
234 Str255 title ;
235
236 MacPreControlCreate( parent , id , wxEmptyString , pos , size ,style, validator , name , &bounds , title ) ;
237
238 ListDefSpec listDef;
239 listDef.defType = kListDefUserProcType;
240 if ( macCheckListDefUPP == NULL )
241 {
242 macCheckListDefUPP = NewListDefUPP( wxMacCheckListDefinition );
243 }
244 listDef.u.userProc = macCheckListDefUPP ;
245
246 #if TARGET_CARBON
247 Size asize;
248
249
250 CreateListBoxControl( MAC_WXHWND(parent->MacGetRootWindow()), &bounds, false, 0, 1, false, true,
251 m_checkBoxHeight+2, 14, false, &listDef, (ControlRef *)&m_macControl );
252
253 GetControlData( (ControlHandle) m_macControl, kControlNoPart, kControlListBoxListHandleTag,
254 sizeof(ListHandle), (Ptr) &m_macList, &asize);
255
256 SetControlReference( (ControlHandle) m_macControl, (long) this);
257 SetControlVisibility( (ControlHandle) m_macControl, false, false);
258
259 #else
260
261 long result ;
262
263 wxStAppResource resload ;
264 m_macControl = (WXWidget) ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , title , false ,
265 kwxMacListWithVerticalScrollbar , 0 , 0,
266 kControlListBoxProc , (long) this ) ;
267 ::GetControlData( (ControlHandle) m_macControl , kControlNoPart , kControlListBoxListHandleTag ,
268 sizeof( ListHandle ) , (char*) &m_macList , &result ) ;
269
270 HLock( (Handle) m_macList ) ;
271 ldefHandle ldef ;
272 ldef = (ldefHandle) NewHandle( sizeof(ldefRec) ) ;
273 if ( (**(ListHandle)m_macList).listDefProc != NULL )
274 {
275 (**ldef).instruction = 0x4EF9; /* JMP instruction */
276 (**ldef).function = (void(*)()) listDef.u.userProc;
277 (**(ListHandle)m_macList).listDefProc = (Handle) ldef ;
278 }
279
280 Point pt = (**(ListHandle)m_macList).cellSize ;
281 pt.v = 14 ;
282 LCellSize( pt , (ListHandle)m_macList ) ;
283 LAddColumn( 1 , 0 , (ListHandle)m_macList ) ;
284 #endif
285 OptionBits options = 0;
286 if ( style & wxLB_MULTIPLE )
287 {
288 options += lNoExtend ;
289 }
290 else if ( style & wxLB_EXTENDED )
291 {
292 options += lExtendDrag ;
293 }
294 else
295 {
296 options = (OptionBits) lOnlyOne ;
297 }
298 SetListSelectionFlags((ListHandle)m_macList, options);
299
300 MacPostControlCreate() ;
301
302 for ( int i = 0 ; i < n ; i++ )
303 {
304 Append( choices[i] ) ;
305 }
306
307 LSetDrawingMode( true , (ListHandle) m_macList ) ;
308
309 return true;
310 }
311
312 // ----------------------------------------------------------------------------
313 // wxCheckListBox functions
314 // ----------------------------------------------------------------------------
315
316 bool wxCheckListBox::IsChecked(unsigned int item) const
317 {
318 wxCHECK_MSG( IsValid(item), false,
319 _T("invalid index in wxCheckListBox::IsChecked") );
320
321 return m_checks[item] != 0;
322 }
323
324 void wxCheckListBox::Check(unsigned int item, bool check)
325 {
326 wxCHECK_RET( IsValid(item),
327 _T("invalid index in wxCheckListBox::Check") );
328
329 // intermediate var is needed to avoid compiler warning with VC++
330 bool isChecked = m_checks[item] != 0;
331 if ( check != isChecked )
332 {
333 m_checks[item] = check;
334
335 MacRedrawControl() ;
336 }
337 }
338
339 // ----------------------------------------------------------------------------
340 // methods forwarded to wxListBox
341 // ----------------------------------------------------------------------------
342
343 void wxCheckListBox::Delete(unsigned int n)
344 {
345 wxCHECK_RET( IsValid(n), _T("invalid index in wxListBox::Delete") );
346
347 wxListBox::Delete(n);
348
349 m_checks.RemoveAt(n);
350 }
351
352 int wxCheckListBox::DoAppend(const wxString& item)
353 {
354 LSetDrawingMode( false , (ListHandle) m_macList ) ;
355 int pos = wxListBox::DoAppend(item);
356
357 // the item is initially unchecked
358 m_checks.Insert(false, pos);
359 LSetDrawingMode( true , (ListHandle) m_macList ) ;
360
361 return pos;
362 }
363
364 void wxCheckListBox::DoInsertItems(const wxArrayString& items, unsigned int pos)
365 {
366 wxListBox::DoInsertItems(items, pos);
367
368 unsigned int count = items.GetCount();
369 for ( unsigned int n = 0; n < count; n++ )
370 {
371 m_checks.Insert(false, pos + n);
372 }
373 }
374
375 void wxCheckListBox::DoSetItems(const wxArrayString& items, void **clientData)
376 {
377 // call it first as it does DoClear()
378 wxListBox::DoSetItems(items, clientData);
379
380 unsigned int count = items.GetCount();
381 for ( unsigned int n = 0; n < count; n++ )
382 {
383 m_checks.Add(false);
384 }
385 }
386
387 void wxCheckListBox::DoClear()
388 {
389 m_checks.Empty();
390 }
391
392 BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
393 EVT_CHAR(wxCheckListBox::OnChar)
394 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
395 END_EVENT_TABLE()
396
397 // this will only work as soon as
398
399 void wxCheckListBox::OnChar(wxKeyEvent& event)
400 {
401 if ( event.GetKeyCode() == WXK_SPACE )
402 {
403 int index = GetSelection() ;
404 if ( index >= 0 )
405 {
406 Check(index, !IsChecked(index) ) ;
407 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, GetId());
408 event.SetInt(index);
409 event.SetEventObject(this);
410 GetEventHandler()->ProcessEvent(event);
411 }
412 }
413 else
414 event.Skip();
415 }
416
417 void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
418 {
419 // clicking on the item selects it, clicking on the checkmark toggles
420 if ( event.GetX() <= 20 /*check width*/ ) {
421 int lineheight ;
422 int topcell ;
423 #if TARGET_CARBON
424 Point pt ;
425 GetListCellSize( (ListHandle)m_macList , &pt ) ;
426 lineheight = pt.v ;
427 ListBounds visible ;
428 GetListVisibleCells( (ListHandle)m_macList , &visible ) ;
429 topcell = visible.top ;
430 #else
431 lineheight = (**(ListHandle)m_macList).cellSize.v ;
432 topcell = (**(ListHandle)m_macList).visible.top ;
433 #endif
434 size_t nItem = ((size_t)event.GetY()) / lineheight + topcell ;
435
436 if ( nItem < m_noItems )
437 {
438 Check(nItem, !IsChecked(nItem) ) ;
439 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, GetId());
440 event.SetInt(nItem);
441 event.SetEventObject(this);
442 GetEventHandler()->ProcessEvent(event);
443 }
444 //else: it's not an error, just click outside of client zone
445 }
446 else {
447 // implement default behaviour: clicking on the item selects it
448 event.Skip();
449 }
450 }
451
452 #endif // wxUSE_CHECKLISTBOX