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