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