]> git.saurik.com Git - wxWidgets.git/blame - src/mac/checklst.cpp
removing dependancy on mac headers from public wx headers (eventually adding wx/mac...
[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
39typedef struct {
40 unsigned short instruction;
41 void (*function)();
42} ldefRec, *ldefPtr, **ldefHandle;
43
44extern "C"
45{
46static pascal void wxMacCheckListDefinition( short message, Boolean isSelected, Rect *drawRect,
47 Cell cell, short dataOffset, short dataLength,
48 ListHandle listHandle ) ;
49}
50
51static pascal void wxMacCheckListDefinition( short message, Boolean isSelected, Rect *drawRect,
52 Cell cell, short dataOffset, short dataLength,
53 ListHandle listHandle )
54{
dbfc5b97
SC
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 ) {
76a5e5d2
SC
111 savedPenMode = GetPortPenMode( (CGrafPtr) grafPtr );
112 SetPortPenMode( (CGrafPtr) grafPtr, hilitetransfermode );
dbfc5b97 113 PaintRect( drawRect );
76a5e5d2 114 SetPortPenMode( (CGrafPtr) grafPtr, savedPenMode );
dbfc5b97
SC
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 );
76a5e5d2
SC
129 savedPenMode = GetPortPenMode( (CGrafPtr) grafPtr );
130 SetPortPenMode( (CGrafPtr) grafPtr, hilitetransfermode );
dbfc5b97 131 PaintRect( drawRect );
76a5e5d2 132 SetPortPenMode( (CGrafPtr) grafPtr, savedPenMode );
dbfc5b97
SC
133 break;
134 default :
135 break ;
136 }
137 SetPort(savePort);
138}
139
140extern "C" void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon) ;
141
142static ListDefUPP macCheckListDefUPP = NULL ;
143
144// ----------------------------------------------------------------------------
145// creation
146// ----------------------------------------------------------------------------
147
148void wxCheckListBox::Init()
149{
150}
151
152bool 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
962cbf2e
GD
182 CreateListBoxControl( MAC_WXHWND(parent->MacGetRootWindow()), &bounds, false, 0, 1, false, true,
183 14, 14, false, &listDef, (ControlRef *)&m_macControl );
dbfc5b97 184
76a5e5d2 185 GetControlData( (ControlHandle) m_macControl, kControlNoPart, kControlListBoxListHandleTag,
dbfc5b97
SC
186 sizeof(ListHandle), (Ptr) &m_macList, &asize);
187
76a5e5d2
SC
188 SetControlReference( (ControlHandle) m_macControl, (long) this);
189 SetControlVisibility( (ControlHandle) m_macControl, false, false);
dbfc5b97
SC
190
191#else
192
193 long result ;
194
76a5e5d2 195 m_macControl = ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , title , false ,
dbfc5b97
SC
196 kwxMacListWithVerticalScrollbar , 0 , 0,
197 kControlListBoxProc , (long) this ) ;
76a5e5d2 198 ::GetControlData( (ControlHandle) m_macControl , kControlNoPart , kControlListBoxListHandleTag ,
dbfc5b97
SC
199 sizeof( ListHandle ) , (char*) &m_macList , &result ) ;
200
201 HLock( (Handle) m_macList ) ;
202 ldefHandle ldef ;
203 ldef = (ldefHandle) NewHandle( sizeof(ldefRec) ) ;
76a5e5d2 204 if ( (**(ListHandle)m_macList).listDefProc != NULL )
dbfc5b97
SC
205 {
206 (**ldef).instruction = 0x4EF9; /* JMP instruction */
207 (**ldef).function = (void(*)()) listDef.u.userProc;
76a5e5d2 208 (**(ListHandle)m_macList).listDefProc = (Handle) ldef ;
dbfc5b97
SC
209 }
210
76a5e5d2 211 Point pt = (**(ListHandle)m_macList).cellSize ;
dbfc5b97 212 pt.v = 14 ;
76a5e5d2
SC
213 LCellSize( pt , (ListHandle)m_macList ) ;
214 LAddColumn( 1 , 0 , (ListHandle)m_macList ) ;
2f1ae414 215#endif
dbfc5b97
SC
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 }
76a5e5d2 229 SetListSelectionFlags((ListHandle)m_macList, options);
dbfc5b97
SC
230
231 MacPostControlCreate() ;
232
233 for ( int i = 0 ; i < n ; i++ )
234 {
235 Append( choices[i] ) ;
236 }
237
76a5e5d2 238 LSetDrawingMode( true , (ListHandle) m_macList ) ;
dbfc5b97
SC
239
240 return TRUE;
241}
e9576ca5
SC
242
243// ----------------------------------------------------------------------------
dbfc5b97 244// wxCheckListBox functions
e9576ca5
SC
245// ----------------------------------------------------------------------------
246
dbfc5b97
SC
247bool 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
255void 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
274void wxCheckListBox::Delete(int n)
275{
276 wxCHECK_RET( n < GetCount(), _T("invalid index in wxListBox::Delete") );
e9576ca5 277
dbfc5b97 278 wxListBox::Delete(n);
e9576ca5 279
dbfc5b97
SC
280 m_checks.RemoveAt(n);
281}
282
283int wxCheckListBox::DoAppend(const wxString& item)
e9576ca5 284{
dbfc5b97
SC
285 int pos = wxListBox::DoAppend(item);
286
287 // the item is initially unchecked
288 m_checks.Insert(FALSE, pos);
289
290 return pos;
e9576ca5
SC
291}
292
dbfc5b97 293void wxCheckListBox::DoInsertItems(const wxArrayString& items, int pos)
e9576ca5 294{
dbfc5b97
SC
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 }
e9576ca5
SC
302}
303
dbfc5b97
SC
304void 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}
e9576ca5 315
dbfc5b97 316void wxCheckListBox::DoClear()
e9576ca5 317{
dbfc5b97 318 m_checks.Empty();
e9576ca5
SC
319}
320
dbfc5b97
SC
321BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
322 EVT_CHAR(wxCheckListBox::OnChar)
323 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
324END_EVENT_TABLE()
325
326// this will only work as soon as
327
328void wxCheckListBox::OnChar(wxKeyEvent& event)
e9576ca5 329{
dbfc5b97
SC
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();
e9576ca5
SC
344}
345
dbfc5b97
SC
346void 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 ;
962cbf2e 354 GetListCellSize( (ListHandle)m_macList , &pt ) ;
dbfc5b97
SC
355 lineheight = pt.v ;
356 ListBounds visible ;
962cbf2e 357 GetListVisibleCells( (ListHandle)m_macList , &visible ) ;
dbfc5b97
SC
358 topcell = visible.top ;
359#else
76a5e5d2
SC
360 lineheight = (**(ListHandle)m_macList).cellSize.v ;
361 topcell = (**(ListHandle)m_macList).visible.top ;
dbfc5b97
SC
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}
e9576ca5 380
dbfc5b97 381#endif // wxUSE_CHECKLISTBOX