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