]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/checklst.cpp
change in wxFileDataObject::SetData behaviour broke DnD of multiple files
[wxWidgets.git] / src / mac / carbon / 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 #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 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 ) {
127 savedPenMode = GetPortPenMode( (CGrafPtr) grafPtr );
128 SetPortPenMode( (CGrafPtr) grafPtr, hilitetransfermode );
129 PaintRect( drawRect );
130 SetPortPenMode( (CGrafPtr) grafPtr, savedPenMode );
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 );
145 savedPenMode = GetPortPenMode( (CGrafPtr) grafPtr );
146 SetPortPenMode( (CGrafPtr) grafPtr, hilitetransfermode );
147 PaintRect( drawRect );
148 SetPortPenMode( (CGrafPtr) grafPtr, savedPenMode );
149 break;
150 default :
151 break ;
152 }
153 SetPort(savePort);
154 }
155
156 extern "C" void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon) ;
157
158 static ListDefUPP macCheckListDefUPP = NULL ;
159
160 // ----------------------------------------------------------------------------
161 // creation
162 // ----------------------------------------------------------------------------
163
164 void wxCheckListBox::Init()
165 {
166 }
167
168 bool 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
198 CreateListBoxControl( MAC_WXHWND(parent->MacGetRootWindow()), &bounds, false, 0, 1, false, true,
199 14, 14, false, &listDef, (ControlRef *)&m_macControl );
200
201 GetControlData( (ControlHandle) m_macControl, kControlNoPart, kControlListBoxListHandleTag,
202 sizeof(ListHandle), (Ptr) &m_macList, &asize);
203
204 SetControlReference( (ControlHandle) m_macControl, (long) this);
205 SetControlVisibility( (ControlHandle) m_macControl, false, false);
206
207 #else
208
209 long result ;
210
211 wxStAppResource resload ;
212 m_macControl = ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , title , false ,
213 kwxMacListWithVerticalScrollbar , 0 , 0,
214 kControlListBoxProc , (long) this ) ;
215 ::GetControlData( (ControlHandle) m_macControl , kControlNoPart , kControlListBoxListHandleTag ,
216 sizeof( ListHandle ) , (char*) &m_macList , &result ) ;
217
218 HLock( (Handle) m_macList ) ;
219 ldefHandle ldef ;
220 ldef = (ldefHandle) NewHandle( sizeof(ldefRec) ) ;
221 if ( (**(ListHandle)m_macList).listDefProc != NULL )
222 {
223 (**ldef).instruction = 0x4EF9; /* JMP instruction */
224 (**ldef).function = (void(*)()) listDef.u.userProc;
225 (**(ListHandle)m_macList).listDefProc = (Handle) ldef ;
226 }
227
228 Point pt = (**(ListHandle)m_macList).cellSize ;
229 pt.v = 14 ;
230 LCellSize( pt , (ListHandle)m_macList ) ;
231 LAddColumn( 1 , 0 , (ListHandle)m_macList ) ;
232 #endif
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 }
246 SetListSelectionFlags((ListHandle)m_macList, options);
247
248 MacPostControlCreate() ;
249
250 for ( int i = 0 ; i < n ; i++ )
251 {
252 Append( choices[i] ) ;
253 }
254
255 LSetDrawingMode( true , (ListHandle) m_macList ) ;
256
257 return TRUE;
258 }
259
260 // ----------------------------------------------------------------------------
261 // wxCheckListBox functions
262 // ----------------------------------------------------------------------------
263
264 bool 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
272 void 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
291 void wxCheckListBox::Delete(int n)
292 {
293 wxCHECK_RET( n < GetCount(), _T("invalid index in wxListBox::Delete") );
294
295 wxListBox::Delete(n);
296
297 m_checks.RemoveAt(n);
298 }
299
300 int wxCheckListBox::DoAppend(const wxString& item)
301 {
302 int pos = wxListBox::DoAppend(item);
303
304 // the item is initially unchecked
305 m_checks.Insert(FALSE, pos);
306
307 return pos;
308 }
309
310 void wxCheckListBox::DoInsertItems(const wxArrayString& items, int pos)
311 {
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 }
319 }
320
321 void 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 }
332
333 void wxCheckListBox::DoClear()
334 {
335 m_checks.Empty();
336 }
337
338 BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
339 EVT_CHAR(wxCheckListBox::OnChar)
340 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
341 END_EVENT_TABLE()
342
343 // this will only work as soon as
344
345 void wxCheckListBox::OnChar(wxKeyEvent& event)
346 {
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();
361 }
362
363 void 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 ;
371 GetListCellSize( (ListHandle)m_macList , &pt ) ;
372 lineheight = pt.v ;
373 ListBounds visible ;
374 GetListVisibleCells( (ListHandle)m_macList , &visible ) ;
375 topcell = visible.top ;
376 #else
377 lineheight = (**(ListHandle)m_macList).cellSize.v ;
378 topcell = (**(ListHandle)m_macList).visible.top ;
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 }
397
398 #endif // wxUSE_CHECKLISTBOX