]>
Commit | Line | Data |
---|---|---|
e9576ca5 SC |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: listbox.cpp | |
3 | // Purpose: wxListBox | |
4 | // Author: AUTHOR | |
5 | // Modified by: | |
6 | // Created: ??/??/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) AUTHOR | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "listbox.h" | |
14 | #endif | |
15 | ||
03e11df5 | 16 | #include "wx/app.h" |
e9576ca5 SC |
17 | #include "wx/listbox.h" |
18 | #include "wx/settings.h" | |
422644a3 | 19 | #include "wx/toplevel.h" |
e9576ca5 SC |
20 | #include "wx/dynarray.h" |
21 | #include "wx/log.h" | |
22 | ||
519cb848 | 23 | #include "wx/utils.h" |
519cb848 | 24 | |
2f1ae414 | 25 | #if !USE_SHARED_LIBRARY |
e9576ca5 | 26 | IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl) |
519cb848 SC |
27 | |
28 | BEGIN_EVENT_TABLE(wxListBox, wxControl) | |
a8e6bf8a | 29 | EVT_SIZE( wxListBox::OnSize ) |
519cb848 | 30 | END_EVENT_TABLE() |
2f1ae414 | 31 | #endif |
e9576ca5 | 32 | |
d497dca4 | 33 | #include "wx/mac/uma.h" |
519cb848 | 34 | |
573ac9dc SC |
35 | #if PRAGMA_STRUCT_ALIGN |
36 | #pragma options align=mac68k | |
37 | #elif PRAGMA_STRUCT_PACKPUSH | |
38 | #pragma pack(push, 2) | |
39 | #elif PRAGMA_STRUCT_PACK | |
40 | #pragma pack(2) | |
41 | #endif | |
e42e45a9 SC |
42 | |
43 | typedef struct { | |
44 | unsigned short instruction; | |
45 | void (*function)(); | |
46 | } ldefRec, *ldefPtr, **ldefHandle; | |
47 | ||
573ac9dc SC |
48 | #if PRAGMA_STRUCT_ALIGN |
49 | #pragma options align=reset | |
50 | #elif PRAGMA_STRUCT_PACKPUSH | |
51 | #pragma pack(pop) | |
52 | #elif PRAGMA_STRUCT_PACK | |
53 | #pragma pack() | |
54 | #endif | |
55 | ||
e42e45a9 SC |
56 | extern "C" |
57 | { | |
58 | static pascal void wxMacListDefinition( short message, Boolean isSelected, Rect *drawRect, | |
a8e6bf8a RR |
59 | Cell cell, short dataOffset, short dataLength, |
60 | ListHandle listHandle ) ; | |
e42e45a9 SC |
61 | } |
62 | ||
63 | static pascal void wxMacListDefinition( short message, Boolean isSelected, Rect *drawRect, | |
a8e6bf8a RR |
64 | Cell cell, short dataOffset, short dataLength, |
65 | ListHandle listHandle ) | |
66 | { | |
67 | FontInfo fontInfo; | |
68 | GrafPtr savePort; | |
69 | GrafPtr grafPtr; | |
70 | RgnHandle savedClipRegion; | |
71 | SInt32 savedPenMode; | |
72 | wxListBox* list; | |
e42e45a9 SC |
73 | GetPort(&savePort); |
74 | SetPort((**listHandle).port); | |
75 | grafPtr = (**listHandle).port ; | |
a8e6bf8a RR |
76 | // typecast our refCon |
77 | list = (wxListBox*) GetControlReference( (ControlHandle) GetListRefCon(listHandle) ); | |
78 | ||
79 | // Calculate the cell rect. | |
80 | ||
81 | switch( message ) { | |
82 | case lInitMsg: | |
83 | break; | |
84 | ||
85 | case lCloseMsg: | |
86 | break; | |
87 | ||
88 | case lDrawMsg: | |
89 | { | |
90 | const wxString text = list->m_stringArray[cell.v] ; | |
91 | ||
92 | // Save the current clip region, and set the clip region to the area we are about | |
93 | // to draw. | |
94 | ||
95 | savedClipRegion = NewRgn(); | |
96 | GetClip( savedClipRegion ); | |
97 | ||
98 | ClipRect( drawRect ); | |
99 | EraseRect( drawRect ); | |
100 | ||
101 | MoveTo(drawRect->left + 4 , drawRect->top + 10 ); | |
102 | ::TextFont( kFontIDMonaco ) ; | |
103 | ::TextSize( 9 ); | |
104 | ::TextFace( 0 ) ; | |
e42e45a9 | 105 | |
a8e6bf8a RR |
106 | DrawText(text, 0 , text.Length()); |
107 | // If the cell is hilited, do the hilite now. Paint the cell contents with the | |
108 | // appropriate QuickDraw transform mode. | |
109 | ||
110 | if( isSelected ) { | |
76a5e5d2 SC |
111 | savedPenMode = GetPortPenMode( (CGrafPtr) grafPtr ); |
112 | SetPortPenMode( (CGrafPtr)grafPtr, hilitetransfermode ); | |
a8e6bf8a | 113 | PaintRect( drawRect ); |
76a5e5d2 | 114 | SetPortPenMode( (CGrafPtr)grafPtr, savedPenMode ); |
a8e6bf8a RR |
115 | } |
116 | ||
117 | // Restore the saved clip region. | |
118 | ||
119 | SetClip( savedClipRegion ); | |
120 | DisposeRgn( savedClipRegion ); | |
121 | } | |
122 | break; | |
123 | case lHiliteMsg: | |
124 | ||
125 | // Hilite or unhilite the cell. Paint the cell contents with the | |
126 | // appropriate QuickDraw transform mode. | |
127 | ||
128 | GetPort( &grafPtr ); | |
76a5e5d2 SC |
129 | savedPenMode = GetPortPenMode( (CGrafPtr)grafPtr ); |
130 | SetPortPenMode( (CGrafPtr)grafPtr, hilitetransfermode ); | |
a8e6bf8a | 131 | PaintRect( drawRect ); |
76a5e5d2 | 132 | SetPortPenMode( (CGrafPtr)grafPtr, savedPenMode ); |
a8e6bf8a RR |
133 | break; |
134 | default : | |
135 | break ; | |
136 | } | |
137 | SetPort(savePort); | |
e42e45a9 SC |
138 | } |
139 | ||
519cb848 SC |
140 | extern "C" void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon) ; |
141 | const short kwxMacListWithVerticalScrollbar = 128 ; | |
dbfc5b97 | 142 | const short kwxMacListItemHeight = 14 ; |
519cb848 | 143 | |
e9576ca5 SC |
144 | // ============================================================================ |
145 | // list box control implementation | |
146 | // ============================================================================ | |
147 | ||
148 | // Listbox item | |
149 | wxListBox::wxListBox() | |
150 | { | |
151 | m_noItems = 0; | |
152 | m_selected = 0; | |
2f1ae414 | 153 | m_macList = NULL ; |
e9576ca5 SC |
154 | } |
155 | ||
e42e45a9 SC |
156 | static ListDefUPP macListDefUPP = NULL ; |
157 | ||
e9576ca5 SC |
158 | bool wxListBox::Create(wxWindow *parent, wxWindowID id, |
159 | const wxPoint& pos, | |
160 | const wxSize& size, | |
161 | int n, const wxString choices[], | |
162 | long style, | |
163 | const wxValidator& validator, | |
164 | const wxString& name) | |
165 | { | |
60149370 GD |
166 | m_noItems = 0 ; // this will be increased by our append command |
167 | m_selected = 0; | |
168 | ||
169 | Rect bounds ; | |
170 | Str255 title ; | |
171 | ||
172 | MacPreControlCreate( parent , id , "" , pos , size ,style, validator , name , &bounds , title ) ; | |
e9576ca5 | 173 | |
60149370 | 174 | ListDefSpec listDef; |
e42e45a9 SC |
175 | listDef.defType = kListDefUserProcType; |
176 | if ( macListDefUPP == NULL ) | |
177 | { | |
178 | macListDefUPP = NewListDefUPP( wxMacListDefinition ); | |
179 | } | |
a8e6bf8a | 180 | listDef.u.userProc = macListDefUPP ; |
de043984 | 181 | |
e42e45a9 | 182 | #if TARGET_CARBON |
60149370 | 183 | Size asize; |
519cb848 | 184 | |
519cb848 | 185 | |
962cbf2e GD |
186 | CreateListBoxControl( MAC_WXHWND(parent->MacGetRootWindow()), &bounds, false, 0, 1, false, true, |
187 | kwxMacListItemHeight, kwxMacListItemHeight, false, &listDef, (ControlRef *)&m_macControl ); | |
519cb848 | 188 | |
76a5e5d2 | 189 | GetControlData( (ControlHandle) m_macControl, kControlNoPart, kControlListBoxListHandleTag, |
60149370 | 190 | sizeof(ListHandle), (Ptr) &m_macList, &asize); |
519cb848 | 191 | |
76a5e5d2 SC |
192 | SetControlReference( (ControlHandle) m_macControl, (long) this); |
193 | SetControlVisibility( (ControlHandle) m_macControl, false, false); | |
519cb848 | 194 | |
60149370 | 195 | #else |
de043984 | 196 | |
a8e6bf8a | 197 | long result ; |
fe3fcb05 | 198 | wxStAppResource resload ; |
76a5e5d2 | 199 | m_macControl = ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , title , false , |
a8e6bf8a RR |
200 | kwxMacListWithVerticalScrollbar , 0 , 0, |
201 | kControlListBoxProc , (long) this ) ; | |
76a5e5d2 | 202 | ::GetControlData( (ControlHandle) m_macControl , kControlNoPart , kControlListBoxListHandleTag , |
a8e6bf8a | 203 | sizeof( ListHandle ) , (char*) &m_macList , &result ) ; |
60149370 GD |
204 | |
205 | HLock( (Handle) m_macList ) ; | |
e42e45a9 SC |
206 | ldefHandle ldef ; |
207 | ldef = (ldefHandle) NewHandle( sizeof(ldefRec) ) ; | |
76a5e5d2 | 208 | if ( (**(ListHandle)m_macList).listDefProc != NULL ) |
e42e45a9 SC |
209 | { |
210 | (**ldef).instruction = 0x4EF9; /* JMP instruction */ | |
211 | (**ldef).function = (void(*)()) listDef.u.userProc; | |
76a5e5d2 | 212 | (**(ListHandle)m_macList).listDefProc = (Handle) ldef ; |
e42e45a9 SC |
213 | } |
214 | ||
76a5e5d2 | 215 | Point pt = (**(ListHandle)m_macList).cellSize ; |
dbfc5b97 | 216 | pt.v = kwxMacListItemHeight ; |
76a5e5d2 SC |
217 | LCellSize( pt , (ListHandle)m_macList ) ; |
218 | LAddColumn( 1 , 0 , (ListHandle)m_macList ) ; | |
e42e45a9 SC |
219 | #endif |
220 | OptionBits options = 0; | |
60149370 GD |
221 | if ( style & wxLB_MULTIPLE ) |
222 | { | |
e42e45a9 | 223 | options += lNoExtend ; |
60149370 GD |
224 | } |
225 | else if ( style & wxLB_EXTENDED ) | |
226 | { | |
e42e45a9 | 227 | options += lExtendDrag ; |
60149370 GD |
228 | } |
229 | else | |
230 | { | |
e42e45a9 | 231 | options = lOnlyOne ; |
60149370 | 232 | } |
76a5e5d2 | 233 | SetListSelectionFlags((ListHandle)m_macList, options); |
60149370 GD |
234 | |
235 | MacPostControlCreate() ; | |
236 | ||
237 | for ( int i = 0 ; i < n ; i++ ) | |
238 | { | |
a8e6bf8a | 239 | Append( choices[i] ) ; |
60149370 GD |
240 | } |
241 | ||
76a5e5d2 | 242 | LSetDrawingMode( true , (ListHandle)m_macList ) ; |
519cb848 | 243 | |
60149370 | 244 | return TRUE; |
e9576ca5 SC |
245 | } |
246 | ||
247 | wxListBox::~wxListBox() | |
248 | { | |
a8e6bf8a RR |
249 | Free() ; |
250 | if ( m_macList ) | |
251 | { | |
60149370 | 252 | #if !TARGET_CARBON |
76a5e5d2 SC |
253 | DisposeHandle( (**(ListHandle)m_macList).listDefProc ) ; |
254 | (**(ListHandle)m_macList).listDefProc = NULL ; | |
60149370 | 255 | #endif |
a8e6bf8a RR |
256 | m_macList = NULL ; |
257 | } | |
e9576ca5 SC |
258 | } |
259 | ||
e7549107 | 260 | void wxListBox::Free() |
e9576ca5 | 261 | { |
e7549107 SC |
262 | #if wxUSE_OWNER_DRAWN |
263 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
264 | { | |
265 | size_t uiCount = m_aItems.Count(); | |
266 | while ( uiCount-- != 0 ) { | |
267 | delete m_aItems[uiCount]; | |
268 | } | |
269 | ||
270 | m_aItems.Clear(); | |
271 | } | |
272 | else | |
273 | #endif // wxUSE_OWNER_DRAWN | |
274 | if ( HasClientObjectData() ) | |
275 | { | |
276 | for ( size_t n = 0; n < (size_t)m_noItems; n++ ) | |
277 | { | |
278 | delete GetClientObject(n); | |
279 | } | |
280 | } | |
e9576ca5 SC |
281 | } |
282 | ||
8614041b SC |
283 | void wxListBox::DoSetSize(int x, int y, |
284 | int width, int height, | |
285 | int sizeFlags ) | |
286 | { | |
a8e6bf8a | 287 | wxControl::DoSetSize( x , y , width , height , sizeFlags ) ; |
8614041b | 288 | #if TARGET_CARBON |
a8e6bf8a | 289 | Rect bounds ; |
76a5e5d2 | 290 | GetControlBounds( (ControlHandle) m_macControl , &bounds ) ; |
962cbf2e | 291 | ControlRef control = GetListVerticalScrollBar( (ListHandle)m_macList ) ; |
a8e6bf8a RR |
292 | if ( control ) |
293 | { | |
294 | Rect scrollbounds ; | |
295 | GetControlBounds( control , &scrollbounds ) ; | |
296 | if( scrollbounds.right != bounds.right + 1 ) | |
297 | { | |
298 | UMAMoveControl( control , bounds.right - (scrollbounds.right - scrollbounds.left) + 1 , | |
299 | scrollbounds.top ) ; | |
300 | } | |
301 | } | |
8614041b SC |
302 | #endif |
303 | } | |
e7549107 | 304 | void wxListBox::DoSetFirstItem(int N) |
e9576ca5 | 305 | { |
a8e6bf8a | 306 | MacScrollTo( N ) ; |
e9576ca5 SC |
307 | } |
308 | ||
309 | void wxListBox::Delete(int N) | |
310 | { | |
e7549107 SC |
311 | wxCHECK_RET( N >= 0 && N < m_noItems, |
312 | wxT("invalid index in wxListBox::Delete") ); | |
313 | ||
314 | #if wxUSE_OWNER_DRAWN | |
315 | delete m_aItems[N]; | |
0baac61e | 316 | m_aItems.RemoveAt(N); |
e7549107 SC |
317 | #else // !wxUSE_OWNER_DRAWN |
318 | if ( HasClientObjectData() ) | |
319 | { | |
320 | delete GetClientObject(N); | |
321 | } | |
322 | #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN | |
a8e6bf8a RR |
323 | m_stringArray.Remove( N ) ; |
324 | m_dataArray.RemoveAt( N ) ; | |
325 | m_noItems --; | |
326 | ||
327 | MacDelete( N ) ; | |
e9576ca5 SC |
328 | } |
329 | ||
e7549107 | 330 | int wxListBox::DoAppend(const wxString& item) |
e9576ca5 | 331 | { |
a8e6bf8a RR |
332 | int index = m_noItems ; |
333 | if( wxApp::s_macDefaultEncodingIsPC ) | |
334 | { | |
335 | m_stringArray.Add( wxMacMakeMacStringFromPC( item ) ) ; | |
336 | m_dataArray.Add( NULL ); | |
337 | } | |
338 | else { | |
339 | m_stringArray.Add( item ) ; | |
340 | m_dataArray.Add( NULL ); | |
341 | } | |
342 | m_noItems ++; | |
343 | DoSetItemClientData( index , NULL ) ; | |
344 | MacAppend( item ) ; | |
e7549107 | 345 | |
a8e6bf8a | 346 | return index ; |
e9576ca5 SC |
347 | } |
348 | ||
e7549107 SC |
349 | void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData) |
350 | { | |
351 | MacSetRedraw( false ) ; | |
519cb848 | 352 | Clear() ; |
e7549107 SC |
353 | int n = choices.GetCount(); |
354 | ||
519cb848 SC |
355 | for( int i = 0 ; i < n ; ++i ) |
356 | { | |
a8e6bf8a RR |
357 | if ( clientData ) |
358 | { | |
e7549107 SC |
359 | #if wxUSE_OWNER_DRAWN |
360 | wxASSERT_MSG(clientData[i] == NULL, | |
361 | wxT("Can't use client data with owner-drawn listboxes")); | |
362 | #else // !wxUSE_OWNER_DRAWN | |
a8e6bf8a | 363 | Append( choices[i] , clientData[i] ) ; |
e7549107 | 364 | #endif |
a8e6bf8a RR |
365 | } |
366 | else | |
367 | Append( choices[i] ) ; | |
519cb848 | 368 | } |
e7549107 SC |
369 | |
370 | #if wxUSE_OWNER_DRAWN | |
371 | if ( m_windowStyle & wxLB_OWNERDRAW ) { | |
372 | // first delete old items | |
373 | size_t ui = m_aItems.Count(); | |
374 | while ( ui-- != 0 ) { | |
375 | delete m_aItems[ui]; | |
376 | } | |
377 | m_aItems.Empty(); | |
378 | ||
379 | // then create new ones | |
380 | for ( ui = 0; ui < (size_t)m_noItems; ui++ ) { | |
381 | wxOwnerDrawn *pNewItem = CreateItem(ui); | |
382 | pNewItem->SetName(choices[ui]); | |
383 | m_aItems.Add(pNewItem); | |
384 | } | |
385 | } | |
386 | #endif // wxUSE_OWNER_DRAWN | |
387 | MacSetRedraw( true ) ; | |
388 | } | |
389 | ||
390 | bool wxListBox::HasMultipleSelection() const | |
391 | { | |
392 | return (m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED); | |
e9576ca5 SC |
393 | } |
394 | ||
519cb848 | 395 | int wxListBox::FindString(const wxString& st) const |
e9576ca5 | 396 | { |
a8e6bf8a RR |
397 | wxString s ; |
398 | if( wxApp::s_macDefaultEncodingIsPC ) | |
399 | { | |
400 | s = wxMacMakeMacStringFromPC( st ) ; | |
401 | } | |
402 | else | |
403 | s = st ; | |
404 | ||
405 | if ( s.Right(1) == "*" ) | |
406 | { | |
407 | wxString search = s.Left( s.Length() - 1 ) ; | |
408 | int len = search.Length() ; | |
409 | Str255 s1 , s2 ; | |
03e11df5 GD |
410 | |
411 | #if TARGET_CARBON | |
a8e6bf8a | 412 | c2pstrcpy( (StringPtr) s2 , search.c_str() ) ; |
03e11df5 | 413 | #else |
a8e6bf8a RR |
414 | strcpy( (char *) s2 , search.c_str() ) ; |
415 | c2pstr( (char *) s2 ) ; | |
03e11df5 GD |
416 | #endif |
417 | ||
a8e6bf8a RR |
418 | for ( int i = 0 ; i < m_noItems ; ++ i ) |
419 | { | |
03e11df5 | 420 | #if TARGET_CARBON |
a8e6bf8a | 421 | c2pstrcpy( (StringPtr) s1 , m_stringArray[i].Left( len ).c_str() ) ; |
03e11df5 | 422 | #else |
a8e6bf8a RR |
423 | strcpy( (char *) s1 , m_stringArray[i].Left( len ).c_str() ) ; |
424 | c2pstr( (char *) s1 ) ; | |
03e11df5 | 425 | #endif |
a8e6bf8a RR |
426 | if ( EqualString( s1 , s2 , false , false ) ) |
427 | return i ; | |
428 | } | |
429 | if ( s.Left(1) == "*" && s.Length() > 1 ) | |
430 | { | |
431 | s.MakeLower() ; | |
432 | for ( int i = 0 ; i < m_noItems ; ++i ) | |
433 | { | |
434 | if ( GetString(i).Lower().Matches(s) ) | |
435 | return i ; | |
436 | } | |
437 | } | |
438 | ||
439 | } | |
440 | else | |
441 | { | |
442 | Str255 s1 , s2 ; | |
03e11df5 GD |
443 | |
444 | #if TARGET_CARBON | |
a8e6bf8a | 445 | c2pstrcpy( (StringPtr) s2 , s.c_str() ) ; |
03e11df5 | 446 | #else |
a8e6bf8a RR |
447 | strcpy( (char *) s2 , s.c_str() ) ; |
448 | c2pstr( (char *) s2 ) ; | |
03e11df5 GD |
449 | #endif |
450 | ||
a8e6bf8a RR |
451 | for ( int i = 0 ; i < m_noItems ; ++ i ) |
452 | { | |
03e11df5 | 453 | #if TARGET_CARBON |
a8e6bf8a | 454 | c2pstrcpy( (StringPtr) s1 , m_stringArray[i].c_str() ) ; |
03e11df5 | 455 | #else |
a8e6bf8a RR |
456 | strcpy( (char *) s1 , m_stringArray[i].c_str() ) ; |
457 | c2pstr( (char *) s1 ) ; | |
03e11df5 | 458 | #endif |
a8e6bf8a RR |
459 | if ( EqualString( s1 , s2 , false , false ) ) |
460 | return i ; | |
461 | } | |
519cb848 SC |
462 | } |
463 | return -1; | |
e9576ca5 SC |
464 | } |
465 | ||
466 | void wxListBox::Clear() | |
467 | { | |
e7549107 | 468 | Free(); |
e9576ca5 | 469 | m_noItems = 0; |
519cb848 SC |
470 | m_stringArray.Empty() ; |
471 | m_dataArray.Empty() ; | |
472 | MacClear() ; | |
e9576ca5 SC |
473 | } |
474 | ||
475 | void wxListBox::SetSelection(int N, bool select) | |
476 | { | |
519cb848 SC |
477 | wxCHECK_RET( N >= 0 && N < m_noItems, |
478 | "invalid index in wxListBox::SetSelection" ); | |
a8e6bf8a RR |
479 | MacSetSelection( N , select ) ; |
480 | GetSelections( m_selectionPreImage ) ; | |
e9576ca5 SC |
481 | } |
482 | ||
e7549107 | 483 | bool wxListBox::IsSelected(int N) const |
e9576ca5 | 484 | { |
519cb848 SC |
485 | wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE, |
486 | "invalid index in wxListBox::Selected" ); | |
487 | ||
a8e6bf8a | 488 | return MacIsSelected( N ) ; |
e9576ca5 SC |
489 | } |
490 | ||
e7549107 | 491 | void *wxListBox::DoGetItemClientData(int N) const |
e9576ca5 | 492 | { |
519cb848 | 493 | wxCHECK_MSG( N >= 0 && N < m_noItems, NULL, |
60149370 | 494 | wxT("invalid index in wxListBox::GetClientData")); |
519cb848 | 495 | |
e7549107 | 496 | return (void *)m_dataArray[N]; |
e9576ca5 SC |
497 | } |
498 | ||
51abe921 SC |
499 | wxClientData *wxListBox::DoGetItemClientObject(int N) const |
500 | { | |
a8e6bf8a | 501 | return (wxClientData *) DoGetItemClientData( N ) ; |
51abe921 SC |
502 | } |
503 | ||
e7549107 | 504 | void wxListBox::DoSetItemClientData(int N, void *Client_data) |
e9576ca5 | 505 | { |
519cb848 SC |
506 | wxCHECK_RET( N >= 0 && N < m_noItems, |
507 | "invalid index in wxListBox::SetClientData" ); | |
508 | ||
e7549107 SC |
509 | #if wxUSE_OWNER_DRAWN |
510 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
511 | { | |
512 | // client data must be pointer to wxOwnerDrawn, otherwise we would crash | |
513 | // in OnMeasure/OnDraw. | |
514 | wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes")); | |
515 | } | |
516 | #endif // wxUSE_OWNER_DRAWN | |
a8e6bf8a RR |
517 | wxASSERT_MSG( m_dataArray.GetCount() >= N , "invalid client_data array" ) ; |
518 | ||
519 | if ( m_dataArray.GetCount() > N ) | |
520 | { | |
521 | m_dataArray[N] = (char*) Client_data ; | |
2f1ae414 | 522 | } |
8208e181 SC |
523 | else |
524 | { | |
a8e6bf8a | 525 | m_dataArray.Add( (char*) Client_data ) ; |
8208e181 | 526 | } |
e7549107 SC |
527 | } |
528 | ||
529 | void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData) | |
530 | { | |
531 | DoSetItemClientData(n, clientData); | |
e9576ca5 SC |
532 | } |
533 | ||
534 | // Return number of selections and an array of selected integers | |
535 | int wxListBox::GetSelections(wxArrayInt& aSelections) const | |
536 | { | |
a8e6bf8a | 537 | return MacGetSelections( aSelections ) ; |
e9576ca5 SC |
538 | } |
539 | ||
540 | // Get single selection, for single choice list items | |
541 | int wxListBox::GetSelection() const | |
542 | { | |
a8e6bf8a | 543 | return MacGetSelection() ; |
e9576ca5 SC |
544 | } |
545 | ||
546 | // Find string for position | |
547 | wxString wxListBox::GetString(int N) const | |
548 | { | |
a8e6bf8a RR |
549 | if( wxApp::s_macDefaultEncodingIsPC ) |
550 | { | |
551 | return wxMacMakePCStringFromMac( m_stringArray[N] ) ; | |
552 | } | |
553 | else | |
554 | return m_stringArray[N] ; | |
e9576ca5 SC |
555 | } |
556 | ||
e7549107 | 557 | void wxListBox::DoInsertItems(const wxArrayString& items, int pos) |
e9576ca5 | 558 | { |
e7549107 SC |
559 | wxCHECK_RET( pos >= 0 && pos <= m_noItems, |
560 | wxT("invalid index in wxListBox::InsertItems") ); | |
561 | ||
562 | int nItems = items.GetCount(); | |
563 | ||
a8e6bf8a RR |
564 | for ( int i = 0 ; i < nItems ; i++ ) |
565 | { | |
566 | m_stringArray.Insert( items[i] , pos + i ) ; | |
567 | m_dataArray.Insert( NULL , pos + i ) ; | |
568 | MacInsert( pos + i , items[i] ) ; | |
569 | } | |
e9576ca5 | 570 | |
519cb848 | 571 | m_noItems += nItems; |
e9576ca5 SC |
572 | } |
573 | ||
574 | void wxListBox::SetString(int N, const wxString& s) | |
575 | { | |
a8e6bf8a RR |
576 | wxString str ; |
577 | if( wxApp::s_macDefaultEncodingIsPC ) | |
578 | { | |
579 | str = wxMacMakeMacStringFromPC( s ) ; | |
580 | } | |
581 | else | |
582 | str = s ; | |
583 | m_stringArray[N] = str ; | |
584 | MacSet( N , s ) ; | |
e9576ca5 SC |
585 | } |
586 | ||
37e2cb08 | 587 | wxSize wxListBox::DoGetBestSize() const |
e9576ca5 | 588 | { |
e7549107 | 589 | return wxSize(100, 100); |
e9576ca5 SC |
590 | } |
591 | ||
51abe921 SC |
592 | int wxListBox::GetCount() const |
593 | { | |
594 | return m_noItems; | |
595 | } | |
596 | ||
597 | void wxListBox::SetupColours() | |
598 | { | |
a756f210 | 599 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
51abe921 SC |
600 | SetForegroundColour(GetParent()->GetForegroundColour()); |
601 | } | |
602 | ||
60149370 GD |
603 | void wxListBox::Refresh(bool eraseBack, const wxRect *rect) |
604 | { | |
de043984 SC |
605 | wxControl::Refresh( eraseBack , rect ) ; |
606 | // MacRedrawControl() ; | |
60149370 GD |
607 | } |
608 | ||
51abe921 SC |
609 | #if wxUSE_OWNER_DRAWN |
610 | ||
611 | class wxListBoxItem : public wxOwnerDrawn | |
612 | { | |
613 | public: | |
614 | wxListBoxItem(const wxString& str = ""); | |
615 | }; | |
616 | ||
617 | wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE) | |
618 | { | |
619 | // no bitmaps/checkmarks | |
620 | SetMarginWidth(0); | |
621 | } | |
622 | ||
623 | wxOwnerDrawn *wxListBox::CreateItem(size_t n) | |
624 | { | |
625 | return new wxListBoxItem(); | |
626 | } | |
627 | ||
628 | #endif //USE_OWNER_DRAWN | |
e9576ca5 | 629 | |
519cb848 SC |
630 | // ============================================================================ |
631 | // list box control implementation | |
632 | // ============================================================================ | |
633 | ||
634 | void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon) | |
635 | { | |
a8e6bf8a RR |
636 | wxListBox* list; |
637 | // typecast our refCon | |
638 | list = (wxListBox*)refCon; | |
639 | ||
640 | MoveTo(cellRect->left + 4 , cellRect->top + 10 ); | |
641 | const wxString text = list->m_stringArray[lCell.v] ; | |
642 | ::TextFont( kFontIDMonaco ) ; | |
643 | ::TextSize( 9 ); | |
644 | ::TextFace( 0 ) ; | |
645 | DrawText(text, 0 , text.Length()); | |
646 | ||
519cb848 SC |
647 | } |
648 | ||
649 | void wxListBox::MacDelete( int N ) | |
650 | { | |
76a5e5d2 | 651 | LDelRow( 1 , N , (ListHandle)m_macList) ; |
60149370 | 652 | Refresh(); |
519cb848 SC |
653 | } |
654 | ||
655 | void wxListBox::MacInsert( int n , const char * text) | |
656 | { | |
60149370 GD |
657 | Cell cell = { 0 , 0 } ; |
658 | cell.v = n ; | |
76a5e5d2 | 659 | LAddRow( 1 , cell.v , (ListHandle)m_macList ) ; |
e42e45a9 | 660 | // LSetCell(text, strlen(text), cell, m_macList); |
60149370 | 661 | Refresh(); |
519cb848 SC |
662 | } |
663 | ||
664 | void wxListBox::MacAppend( const char * text) | |
665 | { | |
60149370 | 666 | Cell cell = { 0 , 0 } ; |
76a5e5d2 SC |
667 | cell.v = (**(ListHandle)m_macList).dataBounds.bottom ; |
668 | LAddRow( 1 , cell.v , (ListHandle)m_macList ) ; | |
e42e45a9 | 669 | // LSetCell(text, strlen(text), cell, m_macList); |
60149370 | 670 | Refresh(); |
519cb848 SC |
671 | } |
672 | ||
673 | void wxListBox::MacClear() | |
674 | { | |
76a5e5d2 | 675 | LDelRow( (**(ListHandle)m_macList).dataBounds.bottom , 0 ,(ListHandle) m_macList ) ; |
60149370 | 676 | Refresh(); |
519cb848 SC |
677 | } |
678 | ||
679 | void wxListBox::MacSetSelection( int n , bool select ) | |
680 | { | |
a8e6bf8a RR |
681 | Cell cell = { 0 , 0 } ; |
682 | if ( ! (m_windowStyle & wxLB_MULTIPLE) ) | |
683 | { | |
76a5e5d2 | 684 | if ( LGetSelect( true , &cell , (ListHandle)m_macList ) ) |
a8e6bf8a | 685 | { |
76a5e5d2 | 686 | LSetSelect( false , cell , (ListHandle)m_macList ) ; |
a8e6bf8a RR |
687 | } |
688 | } | |
689 | ||
690 | cell.v = n ; | |
76a5e5d2 SC |
691 | LSetSelect( select , cell , (ListHandle)m_macList ) ; |
692 | LAutoScroll( (ListHandle)m_macList ) ; | |
a8e6bf8a | 693 | Refresh(); |
519cb848 SC |
694 | } |
695 | ||
696 | bool wxListBox::MacIsSelected( int n ) const | |
697 | { | |
a8e6bf8a RR |
698 | Cell cell = { 0 , 0 } ; |
699 | cell.v = n ; | |
76a5e5d2 | 700 | return LGetSelect( false , &cell , (ListHandle)m_macList ) ; |
519cb848 SC |
701 | } |
702 | ||
703 | void wxListBox::MacDestroy() | |
704 | { | |
60149370 | 705 | // DisposeExtLDEFInfo( m_macList ) ; |
519cb848 SC |
706 | } |
707 | ||
708 | int wxListBox::MacGetSelection() const | |
709 | { | |
a8e6bf8a | 710 | Cell cell = { 0 , 0 } ; |
76a5e5d2 | 711 | if ( LGetSelect( true , &cell , (ListHandle)m_macList ) ) |
a8e6bf8a RR |
712 | return cell.v ; |
713 | else | |
714 | return -1 ; | |
519cb848 SC |
715 | } |
716 | ||
717 | int wxListBox::MacGetSelections( wxArrayInt& aSelections ) const | |
718 | { | |
a8e6bf8a RR |
719 | int no_sel = 0 ; |
720 | ||
519cb848 SC |
721 | aSelections.Empty(); |
722 | ||
a8e6bf8a RR |
723 | Cell cell = { 0 , 0 } ; |
724 | cell.v = 0 ; | |
725 | ||
76a5e5d2 | 726 | while ( LGetSelect( true , &cell ,(ListHandle) m_macList ) ) |
a8e6bf8a RR |
727 | { |
728 | aSelections.Add( cell.v ) ; | |
729 | no_sel++ ; | |
730 | cell.v++ ; | |
731 | } | |
732 | return no_sel ; | |
519cb848 SC |
733 | } |
734 | ||
735 | void wxListBox::MacSet( int n , const char * text ) | |
736 | { | |
a8e6bf8a RR |
737 | // our implementation does not store anything in the list |
738 | // so we just have to redraw | |
739 | Cell cell = { 0 , 0 } ; | |
740 | cell.v = n ; | |
741 | // LSetCell(text, strlen(text), cell, m_macList); | |
742 | Refresh(); | |
519cb848 SC |
743 | } |
744 | ||
745 | void wxListBox::MacScrollTo( int n ) | |
746 | { | |
a8e6bf8a | 747 | // TODO implement scrolling |
519cb848 SC |
748 | } |
749 | ||
750 | void wxListBox::OnSize( const wxSizeEvent &event) | |
751 | { | |
60149370 GD |
752 | Point pt; |
753 | ||
754 | #if TARGET_CARBON | |
962cbf2e | 755 | GetListCellSize((ListHandle)m_macList, &pt); |
60149370 | 756 | #else |
76a5e5d2 | 757 | pt = (**(ListHandle)m_macList).cellSize ; |
60149370 GD |
758 | #endif |
759 | pt.h = m_width - 15 ; | |
76a5e5d2 | 760 | LCellSize( pt , (ListHandle)m_macList ) ; |
519cb848 SC |
761 | } |
762 | ||
76a5e5d2 | 763 | void wxListBox::MacHandleControlClick( WXWidget control , wxInt16 controlpart ) |
519cb848 | 764 | { |
a8e6bf8a RR |
765 | Boolean wasDoubleClick = false ; |
766 | long result ; | |
519cb848 | 767 | |
76a5e5d2 | 768 | ::GetControlData( (ControlHandle) m_macControl , kControlNoPart , kControlListBoxDoubleClickTag , sizeof( wasDoubleClick ) , (char*) &wasDoubleClick , &result ) ; |
a8e6bf8a RR |
769 | if ( !wasDoubleClick ) |
770 | { | |
771 | MacDoClick() ; | |
772 | } | |
773 | else | |
774 | { | |
775 | MacDoDoubleClick() ; | |
776 | } | |
519cb848 SC |
777 | } |
778 | ||
779 | void wxListBox::MacSetRedraw( bool doDraw ) | |
780 | { | |
76a5e5d2 | 781 | LSetDrawingMode( doDraw , (ListHandle)m_macList ) ; |
a8e6bf8a | 782 | |
519cb848 SC |
783 | } |
784 | ||
785 | void wxListBox::MacDoClick() | |
786 | { | |
a8e6bf8a RR |
787 | wxArrayInt aSelections; |
788 | int n, count = GetSelections(aSelections); | |
789 | ||
790 | if ( count == m_selectionPreImage.GetCount() ) | |
791 | { | |
792 | bool hasChanged = false ; | |
793 | for ( int i = 0 ; i < count ; ++i ) | |
794 | { | |
795 | if ( aSelections[i] != m_selectionPreImage[i] ) | |
796 | { | |
797 | hasChanged = true ; | |
798 | break ; | |
799 | } | |
800 | } | |
801 | if ( !hasChanged ) | |
802 | { | |
803 | return ; | |
804 | } | |
805 | } | |
806 | ||
807 | m_selectionPreImage = aSelections; | |
808 | ||
809 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId); | |
810 | event.SetEventObject( this ); | |
811 | ||
812 | if ( count > 0 ) | |
813 | { | |
814 | n = aSelections[0]; | |
815 | if ( HasClientObjectData() ) | |
816 | event.SetClientObject( GetClientObject(n) ); | |
817 | else if ( HasClientUntypedData() ) | |
818 | event.SetClientData( GetClientData(n) ); | |
819 | event.SetString( GetString(n) ); | |
820 | } | |
821 | else | |
822 | { | |
e7549107 | 823 | n = -1; |
a8e6bf8a RR |
824 | } |
825 | ||
e7549107 SC |
826 | event.m_commandInt = n; |
827 | ||
828 | GetEventHandler()->ProcessEvent(event); | |
519cb848 SC |
829 | } |
830 | ||
831 | void wxListBox::MacDoDoubleClick() | |
832 | { | |
833 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId); | |
834 | event.SetEventObject( this ); | |
a8e6bf8a | 835 | GetEventHandler()->ProcessEvent(event) ; |
519cb848 | 836 | } |