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