]> git.saurik.com Git - wxWidgets.git/blob - src/mac/checklst.cpp
suppressed wrong debug error messages from SetFocus
[wxWidgets.git] / src / mac / 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 typedef struct {
40 unsigned short instruction;
41 void (*function)();
42 } ldefRec, *ldefPtr, **ldefHandle;
43
44 extern "C"
45 {
46 static pascal void wxMacCheckListDefinition( short message, Boolean isSelected, Rect *drawRect,
47 Cell cell, short dataOffset, short dataLength,
48 ListHandle listHandle ) ;
49 }
50
51 static pascal void wxMacCheckListDefinition( short message, Boolean isSelected, Rect *drawRect,
52 Cell cell, short dataOffset, short dataLength,
53 ListHandle listHandle )
54 {
55 FontInfo fontInfo;
56 GrafPtr savePort;
57 GrafPtr grafPtr;
58 RgnHandle savedClipRegion;
59 SInt32 savedPenMode;
60 wxCheckListBox* list;
61 GetPort(&savePort);
62 SetPort((**listHandle).port);
63 grafPtr = (**listHandle).port ;
64 // typecast our refCon
65 list = (wxCheckListBox*) GetControlReference( (ControlHandle) GetListRefCon(listHandle) );
66
67 // Calculate the cell rect.
68
69 switch( message ) {
70 case lInitMsg:
71 break;
72
73 case lCloseMsg:
74 break;
75
76 case lDrawMsg:
77 {
78 const wxString text = list->m_stringArray[cell.v] ;
79 int checked = list->m_checks[cell.v] ;
80
81 // Save the current clip region, and set the clip region to the area we are about
82 // to draw.
83
84 savedClipRegion = NewRgn();
85 GetClip( savedClipRegion );
86
87 ClipRect( drawRect );
88 EraseRect( drawRect );
89
90 ::TextFont( kFontIDMonaco ) ;
91 ::TextSize( 9 );
92 ::TextFace( 0 ) ;
93 ThemeButtonDrawInfo info ;
94 info.state = kThemeStateActive ;
95 info.value = checked ? kThemeButtonOn : kThemeButtonOff ;
96 info.adornment = kThemeAdornmentNone ;
97 Rect checkRect = *drawRect ;
98 checkRect.left +=0 ;
99 checkRect.top +=2 ;
100 checkRect.right = checkRect.left + 12 ;
101 checkRect.bottom = checkRect.top + 10 ;
102 DrawThemeButton(&checkRect,kThemeCheckBox,
103 &info,NULL,NULL, NULL,0);
104
105 MoveTo(drawRect->left + 4 + kwxMacListCheckboxWidth, drawRect->top + 10 );
106
107 DrawText(text, 0 , text.Length());
108 // If the cell is hilited, do the hilite now. Paint the cell contents with the
109 // appropriate QuickDraw transform mode.
110
111 if( isSelected ) {
112 savedPenMode = GetPortPenMode( grafPtr );
113 SetPortPenMode( grafPtr, hilitetransfermode );
114 PaintRect( drawRect );
115 SetPortPenMode( grafPtr, savedPenMode );
116 }
117
118 // Restore the saved clip region.
119
120 SetClip( savedClipRegion );
121 DisposeRgn( savedClipRegion );
122 }
123 break;
124 case lHiliteMsg:
125
126 // Hilite or unhilite the cell. Paint the cell contents with the
127 // appropriate QuickDraw transform mode.
128
129 GetPort( &grafPtr );
130 savedPenMode = GetPortPenMode( grafPtr );
131 SetPortPenMode( grafPtr, hilitetransfermode );
132 PaintRect( drawRect );
133 SetPortPenMode( grafPtr, savedPenMode );
134 break;
135 default :
136 break ;
137 }
138 SetPort(savePort);
139 }
140
141 extern "C" void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon) ;
142
143 static ListDefUPP macCheckListDefUPP = NULL ;
144
145 // ----------------------------------------------------------------------------
146 // creation
147 // ----------------------------------------------------------------------------
148
149 void wxCheckListBox::Init()
150 {
151 }
152
153 bool wxCheckListBox::Create(wxWindow *parent,
154 wxWindowID id,
155 const wxPoint &pos,
156 const wxSize &size,
157 int n,
158 const wxString choices[],
159 long style,
160 const wxValidator& validator,
161 const wxString &name)
162 {
163 m_noItems = 0 ; // this will be increased by our append command
164 m_selected = 0;
165
166 Rect bounds ;
167 Str255 title ;
168
169 MacPreControlCreate( parent , id , "" , pos , size ,style, validator , name , &bounds , title ) ;
170
171 ListDefSpec listDef;
172 listDef.defType = kListDefUserProcType;
173 if ( macCheckListDefUPP == NULL )
174 {
175 macCheckListDefUPP = NewListDefUPP( wxMacCheckListDefinition );
176 }
177 listDef.u.userProc = macCheckListDefUPP ;
178
179 #if TARGET_CARBON
180 Size asize;
181
182
183 CreateListBoxControl( parent->MacGetRootWindow(), &bounds, false, 0, 1, false, true,
184 14, 14, false, &listDef, &m_macControl );
185
186 GetControlData(m_macControl, kControlNoPart, kControlListBoxListHandleTag,
187 sizeof(ListHandle), (Ptr) &m_macList, &asize);
188
189 SetControlReference(m_macControl, (long) this);
190 SetControlVisibility(m_macControl, false, false);
191
192 #else
193
194 long result ;
195
196 m_macControl = ::NewControl( parent->MacGetRootWindow() , &bounds , title , false ,
197 kwxMacListWithVerticalScrollbar , 0 , 0,
198 kControlListBoxProc , (long) this ) ;
199 ::GetControlData( m_macControl , kControlNoPart , kControlListBoxListHandleTag ,
200 sizeof( ListHandle ) , (char*) &m_macList , &result ) ;
201
202 HLock( (Handle) m_macList ) ;
203 ldefHandle ldef ;
204 ldef = (ldefHandle) NewHandle( sizeof(ldefRec) ) ;
205 if ( (**m_macList).listDefProc != NULL )
206 {
207 (**ldef).instruction = 0x4EF9; /* JMP instruction */
208 (**ldef).function = (void(*)()) listDef.u.userProc;
209 (**m_macList).listDefProc = (Handle) ldef ;
210 }
211
212 Point pt = (**m_macList).cellSize ;
213 pt.v = 14 ;
214 LCellSize( pt , m_macList ) ;
215 LAddColumn( 1 , 0 , m_macList ) ;
216 #endif
217 OptionBits options = 0;
218 if ( style & wxLB_MULTIPLE )
219 {
220 options += lNoExtend ;
221 }
222 else if ( style & wxLB_EXTENDED )
223 {
224 options += lExtendDrag ;
225 }
226 else
227 {
228 options = lOnlyOne ;
229 }
230 SetListSelectionFlags(m_macList, options);
231
232 MacPostControlCreate() ;
233
234 for ( int i = 0 ; i < n ; i++ )
235 {
236 Append( choices[i] ) ;
237 }
238
239 LSetDrawingMode( true , m_macList ) ;
240
241 return TRUE;
242 }
243
244 // ----------------------------------------------------------------------------
245 // wxCheckListBox functions
246 // ----------------------------------------------------------------------------
247
248 bool wxCheckListBox::IsChecked(size_t item) const
249 {
250 wxCHECK_MSG( item < m_checks.GetCount(), FALSE,
251 _T("invalid index in wxCheckListBox::IsChecked") );
252
253 return m_checks[item] != 0;
254 }
255
256 void wxCheckListBox::Check(size_t item, bool check)
257 {
258 wxCHECK_RET( item < m_checks.GetCount(),
259 _T("invalid index in wxCheckListBox::Check") );
260
261 // intermediate var is needed to avoid compiler warning with VC++
262 bool isChecked = m_checks[item] != 0;
263 if ( check != isChecked )
264 {
265 m_checks[item] = check;
266
267 MacRedrawControl() ;
268 }
269 }
270
271 // ----------------------------------------------------------------------------
272 // methods forwarded to wxListBox
273 // ----------------------------------------------------------------------------
274
275 void wxCheckListBox::Delete(int n)
276 {
277 wxCHECK_RET( n < GetCount(), _T("invalid index in wxListBox::Delete") );
278
279 wxListBox::Delete(n);
280
281 m_checks.RemoveAt(n);
282 }
283
284 int wxCheckListBox::DoAppend(const wxString& item)
285 {
286 int pos = wxListBox::DoAppend(item);
287
288 // the item is initially unchecked
289 m_checks.Insert(FALSE, pos);
290
291 return pos;
292 }
293
294 void wxCheckListBox::DoInsertItems(const wxArrayString& items, int pos)
295 {
296 wxListBox::DoInsertItems(items, pos);
297
298 size_t count = items.GetCount();
299 for ( size_t n = 0; n < count; n++ )
300 {
301 m_checks.Insert(FALSE, pos + n);
302 }
303 }
304
305 void wxCheckListBox::DoSetItems(const wxArrayString& items, void **clientData)
306 {
307 // call it first as it does DoClear()
308 wxListBox::DoSetItems(items, clientData);
309
310 size_t count = items.GetCount();
311 for ( size_t n = 0; n < count; n++ )
312 {
313 m_checks.Add(FALSE);
314 }
315 }
316
317 void wxCheckListBox::DoClear()
318 {
319 m_checks.Empty();
320 }
321
322 BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
323 EVT_CHAR(wxCheckListBox::OnChar)
324 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
325 END_EVENT_TABLE()
326
327 // this will only work as soon as
328
329 void wxCheckListBox::OnChar(wxKeyEvent& event)
330 {
331 if ( event.KeyCode() == WXK_SPACE )
332 {
333 int index = GetSelection() ;
334 if ( index >= 0 )
335 {
336 Check(index, !IsChecked(index) ) ;
337 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, GetId());
338 event.SetInt(index);
339 event.SetEventObject(this);
340 GetEventHandler()->ProcessEvent(event);
341 }
342 }
343 else
344 event.Skip();
345 }
346
347 void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
348 {
349 // clicking on the item selects it, clicking on the checkmark toggles
350 if ( event.GetX() <= 20 /*check width*/ ) {
351 int lineheight ;
352 int topcell ;
353 #if TARGET_CARBON
354 Point pt ;
355 GetListCellSize( m_macList , &pt ) ;
356 lineheight = pt.v ;
357 ListBounds visible ;
358 GetListVisibleCells( m_macList , &visible ) ;
359 topcell = visible.top ;
360 #else
361 lineheight = (**m_macList).cellSize.v ;
362 topcell = (**m_macList).visible.top ;
363 #endif
364 size_t nItem = ((size_t)event.GetY()) / lineheight + topcell ;
365
366 if ( nItem < (size_t)m_noItems )
367 {
368 Check(nItem, !IsChecked(nItem) ) ;
369 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, GetId());
370 event.SetInt(nItem);
371 event.SetEventObject(this);
372 GetEventHandler()->ProcessEvent(event);
373 }
374 //else: it's not an error, just click outside of client zone
375 }
376 else {
377 // implement default behaviour: clicking on the item selects it
378 event.Skip();
379 }
380 }
381
382 #endif // wxUSE_CHECKLISTBOX