]> git.saurik.com Git - wxWidgets.git/blob - src/mac/classic/checklst.cpp
Include wx/msgdlg.h according to precompiled headers of wx/wx.h (with other minor...
[wxWidgets.git] / src / mac / classic / checklst.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/classic/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 #include "wx/wxprec.h"
17
18 #if wxUSE_CHECKLISTBOX
19
20 #include "wx/checklst.h"
21 #include "wx/arrstr.h"
22
23 #include "wx/mac/uma.h"
24 #include <Appearance.h>
25
26 // ============================================================================
27 // implementation of wxCheckListBox
28 // ============================================================================
29
30 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
31
32 const short kwxMacListWithVerticalScrollbar = 128 ;
33 const short kwxMacListItemHeight = 14 ;
34 const short kwxMacListCheckboxWidth = 14 ;
35
36 #if PRAGMA_STRUCT_ALIGN
37 #pragma options align=mac68k
38 #elif PRAGMA_STRUCT_PACKPUSH
39 #pragma pack(push, 2)
40 #elif PRAGMA_STRUCT_PACK
41 #pragma pack(2)
42 #endif
43
44 typedef struct {
45 unsigned short instruction;
46 void (*function)();
47 } ldefRec, *ldefPtr, **ldefHandle;
48
49 #if PRAGMA_STRUCT_ALIGN
50 #pragma options align=reset
51 #elif PRAGMA_STRUCT_PACKPUSH
52 #pragma pack(pop)
53 #elif PRAGMA_STRUCT_PACK
54 #pragma pack()
55 #endif
56
57 extern "C"
58 {
59 static pascal void wxMacCheckListDefinition( short message, Boolean isSelected, Rect *drawRect,
60 Cell cell, short dataOffset, short dataLength,
61 ListHandle listHandle ) ;
62 }
63
64 static pascal void wxMacCheckListDefinition( short message, Boolean isSelected, Rect *drawRect,
65 Cell cell, short dataOffset, short dataLength,
66 ListHandle listHandle )
67 {
68 wxCheckListBox* list;
69 list = (wxCheckListBox*) GetControlReference( (ControlHandle) GetListRefCon(listHandle) );
70 if ( list == NULL )
71 return ;
72
73 GrafPtr savePort;
74 GrafPtr grafPtr;
75 RgnHandle savedClipRegion;
76 SInt32 savedPenMode;
77 GetPort(&savePort);
78 SetPort((**listHandle).port);
79 grafPtr = (**listHandle).port ;
80 // typecast our refCon
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
163 extern "C" void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon) ;
164
165 static ListDefUPP macCheckListDefUPP = NULL ;
166
167 // ----------------------------------------------------------------------------
168 // creation
169 // ----------------------------------------------------------------------------
170
171 void wxCheckListBox::Init()
172 {
173 }
174
175 bool wxCheckListBox::Create(wxWindow *parent,
176 wxWindowID id,
177 const wxPoint &pos,
178 const wxSize &size,
179 const wxArrayString& choices,
180 long style,
181 const wxValidator& validator,
182 const wxString &name)
183 {
184 wxCArrayString chs(choices);
185
186 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
187 style, validator, name);
188 }
189
190 bool wxCheckListBox::Create(wxWindow *parent,
191 wxWindowID id,
192 const wxPoint &pos,
193 const wxSize &size,
194 int n,
195 const wxString choices[],
196 long style,
197 const wxValidator& validator,
198 const wxString &name)
199 {
200 if ( !wxCheckListBoxBase::Create(parent, id, pos, size,
201 n, choices, style, validator, name) )
202 return false;
203
204 m_noItems = 0 ; // this will be increased by our append command
205 m_selected = 0;
206
207 m_checkBoxWidth = 12;
208 m_checkBoxHeight= 10;
209
210 long h = m_checkBoxHeight ;
211 #if TARGET_CARBON
212 GetThemeMetric(kThemeMetricCheckBoxWidth,(long *)&m_checkBoxWidth);
213 GetThemeMetric(kThemeMetricCheckBoxHeight,&h);
214 #endif
215
216 const wxFont& font = GetFont();
217
218 FontInfo finfo;
219 FetchFontInfo(font.GetMacFontNum(),font.GetMacFontSize(),font.GetMacFontStyle(),&finfo);
220
221 m_TextBaseLineOffset= finfo.leading+finfo.ascent;
222 m_checkBoxHeight= finfo.leading+finfo.ascent+finfo.descent;
223
224 if (m_checkBoxHeight<h)
225 {
226 m_TextBaseLineOffset+= (h-m_checkBoxHeight)/2;
227 m_checkBoxHeight= h;
228 }
229
230 Rect bounds ;
231 Str255 title ;
232
233 MacPreControlCreate( parent , id , wxEmptyString , pos , size ,style, validator , name , &bounds , title ) ;
234
235 ListDefSpec listDef;
236 listDef.defType = kListDefUserProcType;
237 if ( macCheckListDefUPP == NULL )
238 {
239 macCheckListDefUPP = NewListDefUPP( wxMacCheckListDefinition );
240 }
241 listDef.u.userProc = macCheckListDefUPP ;
242
243 #if TARGET_CARBON
244 Size asize;
245
246
247 CreateListBoxControl( MAC_WXHWND(parent->MacGetRootWindow()), &bounds, false, 0, 1, false, true,
248 m_checkBoxHeight+2, 14, false, &listDef, (ControlRef *)&m_macControl );
249
250 GetControlData( (ControlHandle) m_macControl, kControlNoPart, kControlListBoxListHandleTag,
251 sizeof(ListHandle), (Ptr) &m_macList, &asize);
252
253 SetControlReference( (ControlHandle) m_macControl, (long) this);
254 SetControlVisibility( (ControlHandle) m_macControl, false, false);
255
256 #else
257
258 long result ;
259
260 wxStAppResource resload ;
261 m_macControl = (WXWidget) ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , title , false ,
262 kwxMacListWithVerticalScrollbar , 0 , 0,
263 kControlListBoxProc , (long) this ) ;
264 ::GetControlData( (ControlHandle) m_macControl , kControlNoPart , kControlListBoxListHandleTag ,
265 sizeof( ListHandle ) , (char*) &m_macList , &result ) ;
266
267 HLock( (Handle) m_macList ) ;
268 ldefHandle ldef ;
269 ldef = (ldefHandle) NewHandle( sizeof(ldefRec) ) ;
270 if ( (**(ListHandle)m_macList).listDefProc != NULL )
271 {
272 (**ldef).instruction = 0x4EF9; /* JMP instruction */
273 (**ldef).function = (void(*)()) listDef.u.userProc;
274 (**(ListHandle)m_macList).listDefProc = (Handle) ldef ;
275 }
276
277 Point pt = (**(ListHandle)m_macList).cellSize ;
278 pt.v = 14 ;
279 LCellSize( pt , (ListHandle)m_macList ) ;
280 LAddColumn( 1 , 0 , (ListHandle)m_macList ) ;
281 #endif
282 OptionBits options = 0;
283 if ( style & wxLB_MULTIPLE )
284 {
285 options += lNoExtend ;
286 }
287 else if ( style & wxLB_EXTENDED )
288 {
289 options += lExtendDrag ;
290 }
291 else
292 {
293 options = (OptionBits) lOnlyOne ;
294 }
295 SetListSelectionFlags((ListHandle)m_macList, options);
296
297 MacPostControlCreate() ;
298
299 for ( int i = 0 ; i < n ; i++ )
300 {
301 Append( choices[i] ) ;
302 }
303
304 LSetDrawingMode( true , (ListHandle) m_macList ) ;
305
306 return true;
307 }
308
309 // ----------------------------------------------------------------------------
310 // wxCheckListBox functions
311 // ----------------------------------------------------------------------------
312
313 bool wxCheckListBox::IsChecked(unsigned int item) const
314 {
315 wxCHECK_MSG( IsValid(item), false,
316 _T("invalid index in wxCheckListBox::IsChecked") );
317
318 return m_checks[item] != 0;
319 }
320
321 void wxCheckListBox::Check(unsigned int item, bool check)
322 {
323 wxCHECK_RET( IsValid(item),
324 _T("invalid index in wxCheckListBox::Check") );
325
326 // intermediate var is needed to avoid compiler warning with VC++
327 bool isChecked = m_checks[item] != 0;
328 if ( check != isChecked )
329 {
330 m_checks[item] = check;
331
332 MacRedrawControl() ;
333 }
334 }
335
336 // ----------------------------------------------------------------------------
337 // methods forwarded to wxListBox
338 // ----------------------------------------------------------------------------
339
340 void wxCheckListBox::Delete(unsigned int n)
341 {
342 wxCHECK_RET( IsValid(n), _T("invalid index in wxListBox::Delete") );
343
344 wxListBox::Delete(n);
345
346 m_checks.RemoveAt(n);
347 }
348
349 int wxCheckListBox::DoAppend(const wxString& item)
350 {
351 LSetDrawingMode( false , (ListHandle) m_macList ) ;
352 int pos = wxListBox::DoAppend(item);
353
354 // the item is initially unchecked
355 m_checks.Insert(false, pos);
356 LSetDrawingMode( true , (ListHandle) m_macList ) ;
357
358 return pos;
359 }
360
361 void wxCheckListBox::DoInsertItems(const wxArrayString& items, unsigned int pos)
362 {
363 wxListBox::DoInsertItems(items, pos);
364
365 unsigned int count = items.GetCount();
366 for ( unsigned int n = 0; n < count; n++ )
367 {
368 m_checks.Insert(false, pos + n);
369 }
370 }
371
372 void wxCheckListBox::DoSetItems(const wxArrayString& items, void **clientData)
373 {
374 // call it first as it does DoClear()
375 wxListBox::DoSetItems(items, clientData);
376
377 unsigned int count = items.GetCount();
378 for ( unsigned int n = 0; n < count; n++ )
379 {
380 m_checks.Add(false);
381 }
382 }
383
384 void wxCheckListBox::DoClear()
385 {
386 m_checks.Empty();
387 }
388
389 BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
390 EVT_CHAR(wxCheckListBox::OnChar)
391 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
392 END_EVENT_TABLE()
393
394 // this will only work as soon as
395
396 void wxCheckListBox::OnChar(wxKeyEvent& event)
397 {
398 if ( event.GetKeyCode() == WXK_SPACE )
399 {
400 int index = GetSelection() ;
401 if ( index >= 0 )
402 {
403 Check(index, !IsChecked(index) ) ;
404 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, GetId());
405 event.SetInt(index);
406 event.SetEventObject(this);
407 GetEventHandler()->ProcessEvent(event);
408 }
409 }
410 else
411 event.Skip();
412 }
413
414 void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
415 {
416 // clicking on the item selects it, clicking on the checkmark toggles
417 if ( event.GetX() <= 20 /*check width*/ ) {
418 int lineheight ;
419 int topcell ;
420 #if TARGET_CARBON
421 Point pt ;
422 GetListCellSize( (ListHandle)m_macList , &pt ) ;
423 lineheight = pt.v ;
424 ListBounds visible ;
425 GetListVisibleCells( (ListHandle)m_macList , &visible ) ;
426 topcell = visible.top ;
427 #else
428 lineheight = (**(ListHandle)m_macList).cellSize.v ;
429 topcell = (**(ListHandle)m_macList).visible.top ;
430 #endif
431 size_t nItem = ((size_t)event.GetY()) / lineheight + topcell ;
432
433 if ( nItem < m_noItems )
434 {
435 Check(nItem, !IsChecked(nItem) ) ;
436 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, GetId());
437 event.SetInt(nItem);
438 event.SetEventObject(this);
439 GetEventHandler()->ProcessEvent(event);
440 }
441 //else: it's not an error, just click outside of client zone
442 }
443 else {
444 // implement default behaviour: clicking on the item selects it
445 event.Skip();
446 }
447 }
448
449 #endif // wxUSE_CHECKLISTBOX