]>
Commit | Line | Data |
---|---|---|
2646f485 | 1 | /////////////////////////////////////////////////////////////////////////////// |
8228b893 | 2 | // Name: src/mac/classic/checklst.cpp |
2646f485 SC |
3 | // Purpose: implementation of wxCheckListBox class |
4 | // Author: Stefan Csomor | |
8228b893 | 5 | // Modified by: |
2646f485 SC |
6 | // Created: 1998-01-01 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Stefan Csomor | |
65571936 | 9 | // Licence: wxWindows licence |
2646f485 SC |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // headers & declarations | |
14 | // ============================================================================ | |
15 | ||
8228b893 | 16 | #include "wx/wxprec.h" |
2646f485 SC |
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 ; | |
8228b893 | 72 | |
2646f485 SC |
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 | |
8228b893 | 81 | |
2646f485 | 82 | // Calculate the cell rect. |
8228b893 | 83 | |
2646f485 SC |
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. | |
8228b893 | 98 | |
2646f485 SC |
99 | savedClipRegion = NewRgn(); |
100 | GetClip( savedClipRegion ); | |
101 | ||
102 | ClipRect( drawRect ); | |
103 | EraseRect( drawRect ); | |
8228b893 | 104 | |
2646f485 SC |
105 | const wxFont& font = list->GetFont(); |
106 | if ( font.Ok() ) | |
107 | { | |
108 | ::TextFont( font.GetMacFontNum() ) ; | |
109 | ::TextSize( font.GetMacFontSize()) ; | |
110 | ::TextFace( font.GetMacFontStyle() ) ; | |
111 | } | |
8228b893 | 112 | |
2646f485 SC |
113 | ThemeButtonDrawInfo info ; |
114 | info.state = kThemeStateActive ; | |
115 | info.value = checked ? kThemeButtonOn : kThemeButtonOff ; | |
116 | info.adornment = kThemeAdornmentNone ; | |
117 | Rect checkRect = *drawRect ; | |
8228b893 WS |
118 | |
119 | ||
2646f485 SC |
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); | |
8228b893 | 126 | |
2646f485 | 127 | MoveTo(drawRect->left + 2 + list->m_checkBoxWidth+2, drawRect->top + list->m_TextBaseLineOffset ); |
8228b893 WS |
128 | |
129 | DrawText(text, 0 , text.length()); | |
2646f485 SC |
130 | // If the cell is hilited, do the hilite now. Paint the cell contents with the |
131 | // appropriate QuickDraw transform mode. | |
8228b893 | 132 | |
2646f485 SC |
133 | if( isSelected ) { |
134 | savedPenMode = GetPortPenMode( (CGrafPtr) grafPtr ); | |
135 | SetPortPenMode( (CGrafPtr) grafPtr, hilitetransfermode ); | |
136 | PaintRect( drawRect ); | |
137 | SetPortPenMode( (CGrafPtr) grafPtr, savedPenMode ); | |
138 | } | |
8228b893 | 139 | |
2646f485 | 140 | // Restore the saved clip region. |
8228b893 | 141 | |
2646f485 SC |
142 | SetClip( savedClipRegion ); |
143 | DisposeRgn( savedClipRegion ); | |
144 | } | |
145 | break; | |
146 | case lHiliteMsg: | |
8228b893 | 147 | |
2646f485 SC |
148 | // Hilite or unhilite the cell. Paint the cell contents with the |
149 | // appropriate QuickDraw transform mode. | |
8228b893 | 150 | |
2646f485 SC |
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 | } | |
8228b893 | 160 | SetPort(savePort); |
2646f485 SC |
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; | |
8228b893 | 206 | |
2646f485 SC |
207 | m_checkBoxWidth = 12; |
208 | m_checkBoxHeight= 10; | |
8228b893 | 209 | |
2646f485 SC |
210 | long h = m_checkBoxHeight ; |
211 | #if TARGET_CARBON | |
8228b893 | 212 | GetThemeMetric(kThemeMetricCheckBoxWidth,(long *)&m_checkBoxWidth); |
2646f485 SC |
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); | |
8228b893 | 220 | |
2646f485 SC |
221 | m_TextBaseLineOffset= finfo.leading+finfo.ascent; |
222 | m_checkBoxHeight= finfo.leading+finfo.ascent+finfo.descent; | |
8228b893 | 223 | |
2646f485 SC |
224 | if (m_checkBoxHeight<h) |
225 | { | |
226 | m_TextBaseLineOffset+= (h-m_checkBoxHeight)/2; | |
227 | m_checkBoxHeight= h; | |
228 | } | |
8228b893 | 229 | |
2646f485 SC |
230 | Rect bounds ; |
231 | Str255 title ; | |
8228b893 | 232 | |
2646f485 SC |
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 | { | |
8228b893 | 239 | macCheckListDefUPP = NewListDefUPP( wxMacCheckListDefinition ); |
2646f485 SC |
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 ; | |
6bc3b8e9 | 261 | m_macControl = (WXWidget) ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , title , false , |
8228b893 | 262 | kwxMacListWithVerticalScrollbar , 0 , 0, |
2646f485 SC |
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 | } | |
8228b893 | 276 | |
2646f485 SC |
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); | |
8228b893 | 296 | |
2646f485 | 297 | MacPostControlCreate() ; |
8228b893 | 298 | |
2646f485 SC |
299 | for ( int i = 0 ; i < n ; i++ ) |
300 | { | |
301 | Append( choices[i] ) ; | |
302 | } | |
8228b893 | 303 | |
2646f485 SC |
304 | LSetDrawingMode( true , (ListHandle) m_macList ) ; |
305 | ||
8228b893 | 306 | return true; |
2646f485 SC |
307 | } |
308 | ||
309 | // ---------------------------------------------------------------------------- | |
310 | // wxCheckListBox functions | |
311 | // ---------------------------------------------------------------------------- | |
312 | ||
aa61d352 | 313 | bool wxCheckListBox::IsChecked(unsigned int item) const |
2646f485 | 314 | { |
8228b893 | 315 | wxCHECK_MSG( IsValid(item), false, |
2646f485 SC |
316 | _T("invalid index in wxCheckListBox::IsChecked") ); |
317 | ||
318 | return m_checks[item] != 0; | |
319 | } | |
320 | ||
aa61d352 | 321 | void wxCheckListBox::Check(unsigned int item, bool check) |
2646f485 | 322 | { |
8228b893 | 323 | wxCHECK_RET( IsValid(item), |
2646f485 SC |
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 | ||
aa61d352 | 340 | void wxCheckListBox::Delete(unsigned int n) |
2646f485 | 341 | { |
8228b893 | 342 | wxCHECK_RET( IsValid(n), _T("invalid index in wxListBox::Delete") ); |
2646f485 SC |
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 | |
8228b893 | 355 | m_checks.Insert(false, pos); |
2646f485 SC |
356 | LSetDrawingMode( true , (ListHandle) m_macList ) ; |
357 | ||
358 | return pos; | |
359 | } | |
360 | ||
aa61d352 | 361 | void wxCheckListBox::DoInsertItems(const wxArrayString& items, unsigned int pos) |
2646f485 SC |
362 | { |
363 | wxListBox::DoInsertItems(items, pos); | |
364 | ||
aa61d352 VZ |
365 | unsigned int count = items.GetCount(); |
366 | for ( unsigned int n = 0; n < count; n++ ) | |
2646f485 | 367 | { |
8228b893 | 368 | m_checks.Insert(false, pos + n); |
2646f485 SC |
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 | ||
aa61d352 VZ |
377 | unsigned int count = items.GetCount(); |
378 | for ( unsigned int n = 0; n < count; n++ ) | |
2646f485 | 379 | { |
8228b893 | 380 | m_checks.Add(false); |
2646f485 SC |
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 | ||
8228b893 | 394 | // this will only work as soon as |
2646f485 SC |
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 ; | |
8228b893 WS |
432 | |
433 | if ( nItem < m_noItems ) | |
2646f485 SC |
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 |