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