]>
Commit | Line | Data |
---|---|---|
e9576ca5 SC |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: checklst.cpp | |
3 | // Purpose: implementation of wxCheckListBox class | |
a31a5f85 | 4 | // Author: Stefan Csomor |
e9576ca5 | 5 | // Modified by: |
a31a5f85 | 6 | // Created: 1998-01-01 |
e9576ca5 | 7 | // RCS-ID: $Id$ |
a31a5f85 | 8 | // Copyright: (c) Stefan Csomor |
e9576ca5 SC |
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 |
33 | IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox) |
34 | ||
35 | const short kwxMacListWithVerticalScrollbar = 128 ; | |
36 | const short kwxMacListItemHeight = 14 ; | |
37 | const 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 |
47 | typedef 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 |
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 | { | |
dbfc5b97 SC |
71 | GrafPtr savePort; |
72 | GrafPtr grafPtr; | |
73 | RgnHandle savedClipRegion; | |
74 | SInt32 savedPenMode; | |
75 | wxCheckListBox* list; | |
e40298d5 JS |
76 | GetPort(&savePort); |
77 | SetPort((**listHandle).port); | |
78 | grafPtr = (**listHandle).port ; | |
dbfc5b97 SC |
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 | ||
fcb35beb VZ |
105 | const wxFont& font = list->GetFont(); |
106 | if ( font.Ok() ) | |
e40298d5 | 107 | { |
fcb35beb VZ |
108 | ::TextFont( font.GetMacFontNum() ) ; |
109 | ::TextSize( font.GetMacFontSize()) ; | |
110 | ::TextFace( font.GetMacFontStyle() ) ; | |
50b30d83 SC |
111 | } |
112 | ||
dbfc5b97 SC |
113 | ThemeButtonDrawInfo info ; |
114 | info.state = kThemeStateActive ; | |
115 | info.value = checked ? kThemeButtonOn : kThemeButtonOff ; | |
116 | info.adornment = kThemeAdornmentNone ; | |
117 | Rect checkRect = *drawRect ; | |
50b30d83 SC |
118 | |
119 | ||
dbfc5b97 | 120 | checkRect.left +=0 ; |
50b30d83 SC |
121 | checkRect.top +=0 ; |
122 | checkRect.right = checkRect.left + list->m_checkBoxWidth ; | |
123 | checkRect.bottom = checkRect.top + list->m_checkBoxHeight ; | |
dbfc5b97 | 124 | DrawThemeButton(&checkRect,kThemeCheckBox, |
e40298d5 JS |
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()); | |
dbfc5b97 SC |
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 ) { | |
76a5e5d2 SC |
134 | savedPenMode = GetPortPenMode( (CGrafPtr) grafPtr ); |
135 | SetPortPenMode( (CGrafPtr) grafPtr, hilitetransfermode ); | |
dbfc5b97 | 136 | PaintRect( drawRect ); |
76a5e5d2 | 137 | SetPortPenMode( (CGrafPtr) grafPtr, savedPenMode ); |
dbfc5b97 SC |
138 | } |
139 | ||
140 | // Restore the saved clip region. | |
141 | ||
142 | SetClip( savedClipRegion ); | |
143 | DisposeRgn( savedClipRegion ); | |
e40298d5 JS |
144 | } |
145 | break; | |
dbfc5b97 SC |
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 ); | |
76a5e5d2 SC |
152 | savedPenMode = GetPortPenMode( (CGrafPtr) grafPtr ); |
153 | SetPortPenMode( (CGrafPtr) grafPtr, hilitetransfermode ); | |
dbfc5b97 | 154 | PaintRect( drawRect ); |
76a5e5d2 | 155 | SetPortPenMode( (CGrafPtr) grafPtr, savedPenMode ); |
dbfc5b97 SC |
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 | int n, | |
180 | const wxString choices[], | |
181 | long style, | |
182 | const wxValidator& validator, | |
183 | const wxString &name) | |
184 | { | |
b45ed7a2 VZ |
185 | if ( !wxCheckListBoxBase::Create(parent, id, pos, size, |
186 | n, choices, style, validator, name) ) | |
187 | return false; | |
188 | ||
dbfc5b97 SC |
189 | m_noItems = 0 ; // this will be increased by our append command |
190 | m_selected = 0; | |
191 | ||
50b30d83 SC |
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 | |
fcb35beb VZ |
200 | |
201 | const wxFont& font = GetFont(); | |
50b30d83 SC |
202 | |
203 | FontInfo finfo; | |
fcb35beb | 204 | FetchFontInfo(font.GetMacFontNum(),font.GetMacFontSize(),font.GetMacFontStyle(),&finfo); |
50b30d83 SC |
205 | |
206 | m_TextBaseLineOffset= finfo.leading+finfo.ascent; | |
207 | m_checkBoxHeight= finfo.leading+finfo.ascent+finfo.descent; | |
208 | ||
e40298d5 JS |
209 | if (m_checkBoxHeight<h) |
210 | { | |
211 | m_TextBaseLineOffset+= (h-m_checkBoxHeight)/2; | |
212 | m_checkBoxHeight= h; | |
213 | } | |
214 | ||
dbfc5b97 SC |
215 | Rect bounds ; |
216 | Str255 title ; | |
217 | ||
427ff662 | 218 | MacPreControlCreate( parent , id , wxEmptyString , pos , size ,style, validator , name , &bounds , title ) ; |
dbfc5b97 SC |
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 | ||
962cbf2e | 232 | CreateListBoxControl( MAC_WXHWND(parent->MacGetRootWindow()), &bounds, false, 0, 1, false, true, |
50b30d83 | 233 | m_checkBoxHeight+2, 14, false, &listDef, (ControlRef *)&m_macControl ); |
dbfc5b97 | 234 | |
76a5e5d2 | 235 | GetControlData( (ControlHandle) m_macControl, kControlNoPart, kControlListBoxListHandleTag, |
dbfc5b97 SC |
236 | sizeof(ListHandle), (Ptr) &m_macList, &asize); |
237 | ||
76a5e5d2 SC |
238 | SetControlReference( (ControlHandle) m_macControl, (long) this); |
239 | SetControlVisibility( (ControlHandle) m_macControl, false, false); | |
dbfc5b97 SC |
240 | |
241 | #else | |
242 | ||
243 | long result ; | |
244 | ||
fe3fcb05 | 245 | wxStAppResource resload ; |
76a5e5d2 | 246 | m_macControl = ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , title , false , |
dbfc5b97 SC |
247 | kwxMacListWithVerticalScrollbar , 0 , 0, |
248 | kControlListBoxProc , (long) this ) ; | |
76a5e5d2 | 249 | ::GetControlData( (ControlHandle) m_macControl , kControlNoPart , kControlListBoxListHandleTag , |
dbfc5b97 SC |
250 | sizeof( ListHandle ) , (char*) &m_macList , &result ) ; |
251 | ||
252 | HLock( (Handle) m_macList ) ; | |
253 | ldefHandle ldef ; | |
254 | ldef = (ldefHandle) NewHandle( sizeof(ldefRec) ) ; | |
76a5e5d2 | 255 | if ( (**(ListHandle)m_macList).listDefProc != NULL ) |
dbfc5b97 SC |
256 | { |
257 | (**ldef).instruction = 0x4EF9; /* JMP instruction */ | |
258 | (**ldef).function = (void(*)()) listDef.u.userProc; | |
76a5e5d2 | 259 | (**(ListHandle)m_macList).listDefProc = (Handle) ldef ; |
dbfc5b97 SC |
260 | } |
261 | ||
76a5e5d2 | 262 | Point pt = (**(ListHandle)m_macList).cellSize ; |
dbfc5b97 | 263 | pt.v = 14 ; |
76a5e5d2 SC |
264 | LCellSize( pt , (ListHandle)m_macList ) ; |
265 | LAddColumn( 1 , 0 , (ListHandle)m_macList ) ; | |
2f1ae414 | 266 | #endif |
dbfc5b97 SC |
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 | { | |
2b5f62a0 | 278 | options = (OptionBits) lOnlyOne ; |
dbfc5b97 | 279 | } |
76a5e5d2 | 280 | SetListSelectionFlags((ListHandle)m_macList, options); |
dbfc5b97 SC |
281 | |
282 | MacPostControlCreate() ; | |
283 | ||
284 | for ( int i = 0 ; i < n ; i++ ) | |
285 | { | |
286 | Append( choices[i] ) ; | |
287 | } | |
288 | ||
76a5e5d2 | 289 | LSetDrawingMode( true , (ListHandle) m_macList ) ; |
dbfc5b97 SC |
290 | |
291 | return TRUE; | |
292 | } | |
e9576ca5 SC |
293 | |
294 | // ---------------------------------------------------------------------------- | |
dbfc5b97 | 295 | // wxCheckListBox functions |
e9576ca5 SC |
296 | // ---------------------------------------------------------------------------- |
297 | ||
dbfc5b97 SC |
298 | bool 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 | ||
306 | void 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 | ||
325 | void wxCheckListBox::Delete(int n) | |
326 | { | |
327 | wxCHECK_RET( n < GetCount(), _T("invalid index in wxListBox::Delete") ); | |
e9576ca5 | 328 | |
dbfc5b97 | 329 | wxListBox::Delete(n); |
e9576ca5 | 330 | |
dbfc5b97 SC |
331 | m_checks.RemoveAt(n); |
332 | } | |
333 | ||
334 | int wxCheckListBox::DoAppend(const wxString& item) | |
e9576ca5 | 335 | { |
50b30d83 | 336 | LSetDrawingMode( false , (ListHandle) m_macList ) ; |
dbfc5b97 SC |
337 | int pos = wxListBox::DoAppend(item); |
338 | ||
339 | // the item is initially unchecked | |
340 | m_checks.Insert(FALSE, pos); | |
50b30d83 | 341 | LSetDrawingMode( true , (ListHandle) m_macList ) ; |
dbfc5b97 SC |
342 | |
343 | return pos; | |
e9576ca5 SC |
344 | } |
345 | ||
dbfc5b97 | 346 | void wxCheckListBox::DoInsertItems(const wxArrayString& items, int pos) |
e9576ca5 | 347 | { |
dbfc5b97 SC |
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 | } | |
e9576ca5 SC |
355 | } |
356 | ||
dbfc5b97 SC |
357 | void 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 | } | |
e9576ca5 | 368 | |
dbfc5b97 | 369 | void wxCheckListBox::DoClear() |
e9576ca5 | 370 | { |
dbfc5b97 | 371 | m_checks.Empty(); |
e9576ca5 SC |
372 | } |
373 | ||
dbfc5b97 SC |
374 | BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox) |
375 | EVT_CHAR(wxCheckListBox::OnChar) | |
376 | EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick) | |
377 | END_EVENT_TABLE() | |
378 | ||
379 | // this will only work as soon as | |
380 | ||
381 | void wxCheckListBox::OnChar(wxKeyEvent& event) | |
e9576ca5 | 382 | { |
e40298d5 | 383 | if ( event.GetKeyCode() == WXK_SPACE ) |
dbfc5b97 | 384 | { |
e40298d5 JS |
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 | } | |
dbfc5b97 | 394 | } |
e40298d5 JS |
395 | else |
396 | event.Skip(); | |
e9576ca5 SC |
397 | } |
398 | ||
dbfc5b97 SC |
399 | void wxCheckListBox::OnLeftClick(wxMouseEvent& event) |
400 | { | |
e40298d5 JS |
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 ; | |
dbfc5b97 | 405 | #if TARGET_CARBON |
e40298d5 JS |
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 ; | |
dbfc5b97 | 412 | #else |
e40298d5 JS |
413 | lineheight = (**(ListHandle)m_macList).cellSize.v ; |
414 | topcell = (**(ListHandle)m_macList).visible.top ; | |
dbfc5b97 | 415 | #endif |
e40298d5 JS |
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(); | |
dbfc5b97 | 431 | } |
dbfc5b97 | 432 | } |
e9576ca5 | 433 | |
dbfc5b97 | 434 | #endif // wxUSE_CHECKLISTBOX |