]> git.saurik.com Git - wxWidgets.git/blame - src/mac/listbox.cpp
bug fixes
[wxWidgets.git] / src / mac / listbox.cpp
CommitLineData
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 17#include "wx/listbox.h"
dc0ace7c 18#include "wx/button.h"
e9576ca5 19#include "wx/settings.h"
422644a3 20#include "wx/toplevel.h"
e9576ca5
SC
21#include "wx/dynarray.h"
22#include "wx/log.h"
23
519cb848 24#include "wx/utils.h"
519cb848 25
2f1ae414 26#if !USE_SHARED_LIBRARY
e9576ca5 27 IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
519cb848
SC
28
29BEGIN_EVENT_TABLE(wxListBox, wxControl)
dc0ace7c 30 EVT_SIZE( wxListBox::OnSize )
ecaf6c18 31 EVT_CHAR( wxListBox::OnChar )
519cb848 32END_EVENT_TABLE()
2f1ae414 33#endif
e9576ca5 34
d497dca4 35#include "wx/mac/uma.h"
519cb848 36
573ac9dc
SC
37#if PRAGMA_STRUCT_ALIGN
38 #pragma options align=mac68k
39#elif PRAGMA_STRUCT_PACKPUSH
40 #pragma pack(push, 2)
41#elif PRAGMA_STRUCT_PACK
42 #pragma pack(2)
43#endif
e42e45a9
SC
44
45typedef struct {
46 unsigned short instruction;
47 void (*function)();
48} ldefRec, *ldefPtr, **ldefHandle;
49
573ac9dc
SC
50#if PRAGMA_STRUCT_ALIGN
51 #pragma options align=reset
52#elif PRAGMA_STRUCT_PACKPUSH
53 #pragma pack(pop)
54#elif PRAGMA_STRUCT_PACK
55 #pragma pack()
56#endif
57
202848fe
SC
58#if TARGET_CARBON
59const short kwxMacListItemHeight = 19 ;
60#else
61const short kwxMacListItemHeight = 14 ;
62#endif
63
e42e45a9
SC
64extern "C"
65{
66static pascal void wxMacListDefinition( short message, Boolean isSelected, Rect *drawRect,
a8e6bf8a
RR
67 Cell cell, short dataOffset, short dataLength,
68 ListHandle listHandle ) ;
e42e45a9
SC
69}
70
71static pascal void wxMacListDefinition( short message, Boolean isSelected, Rect *drawRect,
a8e6bf8a
RR
72 Cell cell, short dataOffset, short dataLength,
73 ListHandle listHandle )
74{
a8e6bf8a
RR
75 GrafPtr savePort;
76 GrafPtr grafPtr;
77 RgnHandle savedClipRegion;
78 SInt32 savedPenMode;
79 wxListBox* list;
e42e45a9
SC
80 GetPort(&savePort);
81 SetPort((**listHandle).port);
82 grafPtr = (**listHandle).port ;
a8e6bf8a
RR
83 // typecast our refCon
84 list = (wxListBox*) GetControlReference( (ControlHandle) GetListRefCon(listHandle) );
dc0ace7c 85
a8e6bf8a 86 // Calculate the cell rect.
dc0ace7c 87
a8e6bf8a
RR
88 switch( message ) {
89 case lInitMsg:
90 break;
91
92 case lCloseMsg:
93 break;
94
95 case lDrawMsg:
96 {
97 const wxString text = list->m_stringArray[cell.v] ;
98
99 // Save the current clip region, and set the clip region to the area we are about
100 // to draw.
dc0ace7c 101
a8e6bf8a
RR
102 savedClipRegion = NewRgn();
103 GetClip( savedClipRegion );
104
105 ClipRect( drawRect );
106 EraseRect( drawRect );
dc0ace7c 107
2b5f62a0
VZ
108 wxFontRefData * font = (wxFontRefData*) list->GetFont().GetRefData() ;
109
110 if ( font )
111 {
112 ::TextFont( font->m_macFontNum ) ;
113 ::TextSize( short(font->m_macFontSize) ) ;
114 ::TextFace( font->m_macFontStyle ) ;
115 }
116 else
117 {
118 ::TextFont( kFontIDMonaco ) ;
119 ::TextSize( 9 );
120 ::TextFace( 0 ) ;
121 }
122
202848fe
SC
123#if TARGET_CARBON
124 bool useDrawThemeText = ( DrawThemeTextBox != (void*) kUnresolvedCFragSymbolAddress ) ;
125
126 if ( useDrawThemeText )
127 {
128 Rect frame = { drawRect->top, drawRect->left + 4,
2b5f62a0
VZ
129 drawRect->top + kwxMacListItemHeight, drawRect->right + 10000 } ;
130 CFStringRef sString = CFStringCreateWithBytes( NULL , (UInt8*) text.c_str(), text.Length(), CFStringGetSystemEncoding(), false ) ;
131 CFMutableStringRef mString = CFStringCreateMutableCopy( NULL , 0 , sString ) ;
132 CFRelease( sString ) ;
133 ::TruncateThemeText( mString , kThemeCurrentPortFont, kThemeStateActive, drawRect->right - drawRect->left , truncEnd , NULL ) ;
202848fe 134 ::DrawThemeTextBox( mString,
2b5f62a0 135 kThemeCurrentPortFont,
202848fe
SC
136 kThemeStateActive,
137 false,
138 &frame,
139 teJustLeft,
140 nil );
141 CFRelease( mString ) ;
142 }
143 else
144#endif
145 {
146 MoveTo(drawRect->left + 4 , drawRect->top + 10 );
202848fe
SC
147 DrawText(text, 0 , text.Length());
148 }
dc0ace7c 149
a8e6bf8a
RR
150 // If the cell is hilited, do the hilite now. Paint the cell contents with the
151 // appropriate QuickDraw transform mode.
dc0ace7c 152
a8e6bf8a 153 if( isSelected ) {
76a5e5d2
SC
154 savedPenMode = GetPortPenMode( (CGrafPtr) grafPtr );
155 SetPortPenMode( (CGrafPtr)grafPtr, hilitetransfermode );
a8e6bf8a 156 PaintRect( drawRect );
76a5e5d2 157 SetPortPenMode( (CGrafPtr)grafPtr, savedPenMode );
a8e6bf8a 158 }
dc0ace7c 159
a8e6bf8a 160 // Restore the saved clip region.
dc0ace7c 161
a8e6bf8a
RR
162 SetClip( savedClipRegion );
163 DisposeRgn( savedClipRegion );
164 }
165 break;
166 case lHiliteMsg:
dc0ace7c 167
a8e6bf8a
RR
168 // Hilite or unhilite the cell. Paint the cell contents with the
169 // appropriate QuickDraw transform mode.
dc0ace7c 170
a8e6bf8a 171 GetPort( &grafPtr );
76a5e5d2
SC
172 savedPenMode = GetPortPenMode( (CGrafPtr)grafPtr );
173 SetPortPenMode( (CGrafPtr)grafPtr, hilitetransfermode );
a8e6bf8a 174 PaintRect( drawRect );
76a5e5d2 175 SetPortPenMode( (CGrafPtr)grafPtr, savedPenMode );
a8e6bf8a
RR
176 break;
177 default :
178 break ;
179 }
dc0ace7c 180 SetPort(savePort);
e42e45a9
SC
181}
182
519cb848 183extern "C" void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon) ;
f81127c5 184// resources ldef ids
519cb848 185const short kwxMacListWithVerticalScrollbar = 128 ;
f81127c5 186const short kwxMacListWithVerticalAndHorizontalScrollbar = 129 ;
519cb848 187
e9576ca5
SC
188// ============================================================================
189// list box control implementation
190// ============================================================================
191
192// Listbox item
193wxListBox::wxListBox()
194{
195 m_noItems = 0;
196 m_selected = 0;
2f1ae414 197 m_macList = NULL ;
e9576ca5
SC
198}
199
e42e45a9
SC
200static ListDefUPP macListDefUPP = NULL ;
201
e9576ca5
SC
202bool wxListBox::Create(wxWindow *parent, wxWindowID id,
203 const wxPoint& pos,
204 const wxSize& size,
205 int n, const wxString choices[],
206 long style,
207 const wxValidator& validator,
208 const wxString& name)
209{
60149370
GD
210 m_noItems = 0 ; // this will be increased by our append command
211 m_selected = 0;
dc0ace7c 212
60149370
GD
213 Rect bounds ;
214 Str255 title ;
dc0ace7c 215
60149370 216 MacPreControlCreate( parent , id , "" , pos , size ,style, validator , name , &bounds , title ) ;
e9576ca5 217
60149370 218 ListDefSpec listDef;
e42e45a9
SC
219 listDef.defType = kListDefUserProcType;
220 if ( macListDefUPP == NULL )
221 {
dc0ace7c 222 macListDefUPP = NewListDefUPP( wxMacListDefinition );
e42e45a9 223 }
a8e6bf8a 224 listDef.u.userProc = macListDefUPP ;
de043984 225
2b5f62a0
VZ
226 Str255 fontName ;
227 SInt16 fontSize ;
228 Style fontStyle ;
f81127c5 229 SInt16 fontNum ;
2b5f62a0
VZ
230#if TARGET_CARBON
231 GetThemeFont(kThemeViewsFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
232#else
233 GetFontName( kFontIDMonaco , fontName ) ;
234 fontSize = 9 ;
235 fontStyle = normal ;
236#endif
237 CopyPascalStringToC( fontName , (char*) fontName ) ;
238 SetFont( wxFont (fontSize, wxSWISS, wxNORMAL, wxNORMAL , false , fontName ) ) ;
e42e45a9 239#if TARGET_CARBON
60149370 240 Size asize;
519cb848 241
519cb848 242
f81127c5 243 CreateListBoxControl( MAC_WXHWND(parent->MacGetRootWindow()), &bounds, false, 0, 1, (style & wxLB_HSCROLL), true,
962cbf2e 244 kwxMacListItemHeight, kwxMacListItemHeight, false, &listDef, (ControlRef *)&m_macControl );
519cb848 245
76a5e5d2 246 GetControlData( (ControlHandle) m_macControl, kControlNoPart, kControlListBoxListHandleTag,
60149370 247 sizeof(ListHandle), (Ptr) &m_macList, &asize);
519cb848 248
76a5e5d2
SC
249 SetControlReference( (ControlHandle) m_macControl, (long) this);
250 SetControlVisibility( (ControlHandle) m_macControl, false, false);
519cb848 251
60149370 252#else
de043984 253
a8e6bf8a 254 long result ;
fe3fcb05 255 wxStAppResource resload ;
76a5e5d2 256 m_macControl = ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , title , false ,
f81127c5
SC
257 (style & wxLB_HSCROLL) ? kwxMacListWithVerticalAndHorizontalScrollbar : kwxMacListWithVerticalScrollbar ,
258 0 , 0, kControlListBoxProc , (long) this ) ;
76a5e5d2 259 ::GetControlData( (ControlHandle) m_macControl , kControlNoPart , kControlListBoxListHandleTag ,
a8e6bf8a 260 sizeof( ListHandle ) , (char*) &m_macList , &result ) ;
60149370
GD
261
262 HLock( (Handle) m_macList ) ;
e42e45a9
SC
263 ldefHandle ldef ;
264 ldef = (ldefHandle) NewHandle( sizeof(ldefRec) ) ;
76a5e5d2 265 if ( (**(ListHandle)m_macList).listDefProc != NULL )
e42e45a9
SC
266 {
267 (**ldef).instruction = 0x4EF9; /* JMP instruction */
268 (**ldef).function = (void(*)()) listDef.u.userProc;
76a5e5d2 269 (**(ListHandle)m_macList).listDefProc = (Handle) ldef ;
e42e45a9 270 }
dc0ace7c 271
76a5e5d2 272 Point pt = (**(ListHandle)m_macList).cellSize ;
dbfc5b97 273 pt.v = kwxMacListItemHeight ;
76a5e5d2
SC
274 LCellSize( pt , (ListHandle)m_macList ) ;
275 LAddColumn( 1 , 0 , (ListHandle)m_macList ) ;
e42e45a9
SC
276#endif
277 OptionBits options = 0;
60149370
GD
278 if ( style & wxLB_MULTIPLE )
279 {
e42e45a9 280 options += lNoExtend ;
60149370
GD
281 }
282 else if ( style & wxLB_EXTENDED )
283 {
e42e45a9 284 options += lExtendDrag ;
60149370
GD
285 }
286 else
287 {
2b5f62a0 288 options = (OptionBits) lOnlyOne ;
60149370 289 }
76a5e5d2 290 SetListSelectionFlags((ListHandle)m_macList, options);
dc0ace7c 291
60149370
GD
292 for ( int i = 0 ; i < n ; i++ )
293 {
a8e6bf8a 294 Append( choices[i] ) ;
60149370 295 }
dc0ace7c 296
2b5f62a0
VZ
297 MacPostControlCreate() ;
298
76a5e5d2 299 LSetDrawingMode( true , (ListHandle)m_macList ) ;
519cb848 300
60149370 301 return TRUE;
e9576ca5
SC
302}
303
304wxListBox::~wxListBox()
305{
4b651a46 306 FreeData() ;
a8e6bf8a
RR
307 if ( m_macList )
308 {
60149370 309#if !TARGET_CARBON
76a5e5d2
SC
310 DisposeHandle( (**(ListHandle)m_macList).listDefProc ) ;
311 (**(ListHandle)m_macList).listDefProc = NULL ;
60149370 312#endif
a8e6bf8a
RR
313 m_macList = NULL ;
314 }
e9576ca5
SC
315}
316
4b651a46 317void wxListBox::FreeData()
e9576ca5 318{
e7549107
SC
319#if wxUSE_OWNER_DRAWN
320 if ( m_windowStyle & wxLB_OWNERDRAW )
321 {
322 size_t uiCount = m_aItems.Count();
323 while ( uiCount-- != 0 ) {
324 delete m_aItems[uiCount];
f5bb2251 325 m_aItems[uiCount] = NULL;
e7549107
SC
326 }
327
328 m_aItems.Clear();
329 }
330 else
331#endif // wxUSE_OWNER_DRAWN
332 if ( HasClientObjectData() )
333 {
334 for ( size_t n = 0; n < (size_t)m_noItems; n++ )
335 {
336 delete GetClientObject(n);
337 }
338 }
e9576ca5
SC
339}
340
8614041b
SC
341void wxListBox::DoSetSize(int x, int y,
342 int width, int height,
343 int sizeFlags )
344{
a8e6bf8a 345 wxControl::DoSetSize( x , y , width , height , sizeFlags ) ;
8614041b 346#if TARGET_CARBON
a8e6bf8a 347 Rect bounds ;
76a5e5d2 348 GetControlBounds( (ControlHandle) m_macControl , &bounds ) ;
962cbf2e 349 ControlRef control = GetListVerticalScrollBar( (ListHandle)m_macList ) ;
a8e6bf8a
RR
350 if ( control )
351 {
352 Rect scrollbounds ;
353 GetControlBounds( control , &scrollbounds ) ;
354 if( scrollbounds.right != bounds.right + 1 )
355 {
dc0ace7c 356 UMAMoveControl( control , bounds.right - (scrollbounds.right - scrollbounds.left) + 1 ,
a8e6bf8a
RR
357 scrollbounds.top ) ;
358 }
359 }
8614041b
SC
360#endif
361}
e7549107 362void wxListBox::DoSetFirstItem(int N)
e9576ca5 363{
a8e6bf8a 364 MacScrollTo( N ) ;
e9576ca5
SC
365}
366
367void wxListBox::Delete(int N)
368{
e7549107
SC
369 wxCHECK_RET( N >= 0 && N < m_noItems,
370 wxT("invalid index in wxListBox::Delete") );
371
372#if wxUSE_OWNER_DRAWN
373 delete m_aItems[N];
0baac61e 374 m_aItems.RemoveAt(N);
e7549107
SC
375#else // !wxUSE_OWNER_DRAWN
376 if ( HasClientObjectData() )
377 {
378 delete GetClientObject(N);
379 }
380#endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
ecaf6c18 381 m_stringArray.RemoveAt( N ) ;
a8e6bf8a
RR
382 m_dataArray.RemoveAt( N ) ;
383 m_noItems --;
dc0ace7c 384
a8e6bf8a 385 MacDelete( N ) ;
e9576ca5
SC
386}
387
e7549107 388int wxListBox::DoAppend(const wxString& item)
e9576ca5 389{
a8e6bf8a
RR
390 int index = m_noItems ;
391 if( wxApp::s_macDefaultEncodingIsPC )
392 {
393 m_stringArray.Add( wxMacMakeMacStringFromPC( item ) ) ;
394 m_dataArray.Add( NULL );
395 }
396 else {
397 m_stringArray.Add( item ) ;
398 m_dataArray.Add( NULL );
399 }
400 m_noItems ++;
401 DoSetItemClientData( index , NULL ) ;
402 MacAppend( item ) ;
e7549107 403
a8e6bf8a 404 return index ;
e9576ca5
SC
405}
406
e7549107 407void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
dc0ace7c 408{
e7549107 409 MacSetRedraw( false ) ;
dc0ace7c 410 Clear() ;
e7549107
SC
411 int n = choices.GetCount();
412
519cb848
SC
413 for( int i = 0 ; i < n ; ++i )
414 {
a8e6bf8a
RR
415 if ( clientData )
416 {
e7549107
SC
417#if wxUSE_OWNER_DRAWN
418 wxASSERT_MSG(clientData[i] == NULL,
419 wxT("Can't use client data with owner-drawn listboxes"));
420#else // !wxUSE_OWNER_DRAWN
a8e6bf8a 421 Append( choices[i] , clientData[i] ) ;
e7549107 422 #endif
a8e6bf8a
RR
423 }
424 else
425 Append( choices[i] ) ;
519cb848 426 }
e7549107
SC
427
428#if wxUSE_OWNER_DRAWN
429 if ( m_windowStyle & wxLB_OWNERDRAW ) {
430 // first delete old items
431 size_t ui = m_aItems.Count();
432 while ( ui-- != 0 ) {
433 delete m_aItems[ui];
f5bb2251 434 m_aItems[ui] = NULL;
e7549107
SC
435 }
436 m_aItems.Empty();
437
438 // then create new ones
439 for ( ui = 0; ui < (size_t)m_noItems; ui++ ) {
440 wxOwnerDrawn *pNewItem = CreateItem(ui);
441 pNewItem->SetName(choices[ui]);
442 m_aItems.Add(pNewItem);
443 }
444 }
445#endif // wxUSE_OWNER_DRAWN
446 MacSetRedraw( true ) ;
447}
448
449bool wxListBox::HasMultipleSelection() const
450{
451 return (m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED);
e9576ca5
SC
452}
453
519cb848 454int wxListBox::FindString(const wxString& st) const
e9576ca5 455{
a8e6bf8a
RR
456 wxString s ;
457 if( wxApp::s_macDefaultEncodingIsPC )
458 {
459 s = wxMacMakeMacStringFromPC( st ) ;
460 }
461 else
462 s = st ;
dc0ace7c 463
a8e6bf8a
RR
464 if ( s.Right(1) == "*" )
465 {
466 wxString search = s.Left( s.Length() - 1 ) ;
467 int len = search.Length() ;
468 Str255 s1 , s2 ;
03e11df5
GD
469
470#if TARGET_CARBON
a8e6bf8a 471 c2pstrcpy( (StringPtr) s2 , search.c_str() ) ;
03e11df5 472#else
a8e6bf8a
RR
473 strcpy( (char *) s2 , search.c_str() ) ;
474 c2pstr( (char *) s2 ) ;
03e11df5
GD
475#endif
476
a8e6bf8a
RR
477 for ( int i = 0 ; i < m_noItems ; ++ i )
478 {
03e11df5 479#if TARGET_CARBON
a8e6bf8a 480 c2pstrcpy( (StringPtr) s1 , m_stringArray[i].Left( len ).c_str() ) ;
03e11df5 481#else
a8e6bf8a
RR
482 strcpy( (char *) s1 , m_stringArray[i].Left( len ).c_str() ) ;
483 c2pstr( (char *) s1 ) ;
03e11df5 484#endif
a8e6bf8a
RR
485 if ( EqualString( s1 , s2 , false , false ) )
486 return i ;
487 }
488 if ( s.Left(1) == "*" && s.Length() > 1 )
489 {
ecaf6c18 490 s = st ;
a8e6bf8a
RR
491 s.MakeLower() ;
492 for ( int i = 0 ; i < m_noItems ; ++i )
493 {
494 if ( GetString(i).Lower().Matches(s) )
495 return i ;
496 }
dc0ace7c 497 }
a8e6bf8a
RR
498
499 }
500 else
501 {
502 Str255 s1 , s2 ;
03e11df5
GD
503
504#if TARGET_CARBON
a8e6bf8a 505 c2pstrcpy( (StringPtr) s2 , s.c_str() ) ;
03e11df5 506#else
a8e6bf8a
RR
507 strcpy( (char *) s2 , s.c_str() ) ;
508 c2pstr( (char *) s2 ) ;
03e11df5
GD
509#endif
510
a8e6bf8a
RR
511 for ( int i = 0 ; i < m_noItems ; ++ i )
512 {
03e11df5 513#if TARGET_CARBON
a8e6bf8a 514 c2pstrcpy( (StringPtr) s1 , m_stringArray[i].c_str() ) ;
03e11df5 515#else
a8e6bf8a
RR
516 strcpy( (char *) s1 , m_stringArray[i].c_str() ) ;
517 c2pstr( (char *) s1 ) ;
03e11df5 518#endif
a8e6bf8a
RR
519 if ( EqualString( s1 , s2 , false , false ) )
520 return i ;
521 }
519cb848
SC
522 }
523 return -1;
e9576ca5
SC
524}
525
526void wxListBox::Clear()
527{
4b651a46 528 FreeData();
e9576ca5 529 m_noItems = 0;
519cb848
SC
530 m_stringArray.Empty() ;
531 m_dataArray.Empty() ;
532 MacClear() ;
e9576ca5
SC
533}
534
535void wxListBox::SetSelection(int N, bool select)
536{
519cb848
SC
537 wxCHECK_RET( N >= 0 && N < m_noItems,
538 "invalid index in wxListBox::SetSelection" );
a8e6bf8a
RR
539 MacSetSelection( N , select ) ;
540 GetSelections( m_selectionPreImage ) ;
e9576ca5
SC
541}
542
e7549107 543bool wxListBox::IsSelected(int N) const
e9576ca5 544{
519cb848
SC
545 wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE,
546 "invalid index in wxListBox::Selected" );
547
a8e6bf8a 548 return MacIsSelected( N ) ;
e9576ca5
SC
549}
550
e7549107 551void *wxListBox::DoGetItemClientData(int N) const
e9576ca5 552{
519cb848 553 wxCHECK_MSG( N >= 0 && N < m_noItems, NULL,
60149370 554 wxT("invalid index in wxListBox::GetClientData"));
519cb848 555
e7549107 556 return (void *)m_dataArray[N];
e9576ca5
SC
557}
558
51abe921
SC
559wxClientData *wxListBox::DoGetItemClientObject(int N) const
560{
a8e6bf8a 561 return (wxClientData *) DoGetItemClientData( N ) ;
51abe921
SC
562}
563
e7549107 564void wxListBox::DoSetItemClientData(int N, void *Client_data)
e9576ca5 565{
519cb848
SC
566 wxCHECK_RET( N >= 0 && N < m_noItems,
567 "invalid index in wxListBox::SetClientData" );
568
e7549107
SC
569#if wxUSE_OWNER_DRAWN
570 if ( m_windowStyle & wxLB_OWNERDRAW )
571 {
572 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
573 // in OnMeasure/OnDraw.
574 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
575 }
576#endif // wxUSE_OWNER_DRAWN
68a9d9d0 577 wxASSERT_MSG( m_dataArray.GetCount() >= (size_t) N , "invalid client_data array" ) ;
dc0ace7c 578
68a9d9d0 579 if ( m_dataArray.GetCount() > (size_t) N )
a8e6bf8a
RR
580 {
581 m_dataArray[N] = (char*) Client_data ;
2f1ae414 582 }
8208e181
SC
583 else
584 {
a8e6bf8a 585 m_dataArray.Add( (char*) Client_data ) ;
8208e181 586 }
e7549107
SC
587}
588
589void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
590{
591 DoSetItemClientData(n, clientData);
e9576ca5
SC
592}
593
594// Return number of selections and an array of selected integers
595int wxListBox::GetSelections(wxArrayInt& aSelections) const
596{
a8e6bf8a 597 return MacGetSelections( aSelections ) ;
e9576ca5
SC
598}
599
600// Get single selection, for single choice list items
601int wxListBox::GetSelection() const
602{
a8e6bf8a 603 return MacGetSelection() ;
e9576ca5
SC
604}
605
606// Find string for position
607wxString wxListBox::GetString(int N) const
608{
a8e6bf8a
RR
609 if( wxApp::s_macDefaultEncodingIsPC )
610 {
611 return wxMacMakePCStringFromMac( m_stringArray[N] ) ;
612 }
613 else
614 return m_stringArray[N] ;
e9576ca5
SC
615}
616
e7549107 617void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
e9576ca5 618{
e7549107
SC
619 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
620 wxT("invalid index in wxListBox::InsertItems") );
621
622 int nItems = items.GetCount();
623
a8e6bf8a
RR
624 for ( int i = 0 ; i < nItems ; i++ )
625 {
626 m_stringArray.Insert( items[i] , pos + i ) ;
627 m_dataArray.Insert( NULL , pos + i ) ;
628 MacInsert( pos + i , items[i] ) ;
629 }
e9576ca5 630
519cb848 631 m_noItems += nItems;
e9576ca5
SC
632}
633
634void wxListBox::SetString(int N, const wxString& s)
635{
a8e6bf8a
RR
636 wxString str ;
637 if( wxApp::s_macDefaultEncodingIsPC )
638 {
639 str = wxMacMakeMacStringFromPC( s ) ;
640 }
641 else
642 str = s ;
643 m_stringArray[N] = str ;
644 MacSet( N , s ) ;
e9576ca5
SC
645}
646
37e2cb08 647wxSize wxListBox::DoGetBestSize() const
e9576ca5 648{
2b5f62a0
VZ
649 int lbWidth = 100; // some defaults
650 int lbHeight = 110;
651 int wLine;
652
653 {
654 wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetRootWindow() ) ) ;
2b5f62a0
VZ
655
656 wxFontRefData * font = (wxFontRefData*) m_font.GetRefData() ;
657
658 if ( font )
659 {
660 ::TextFont( font->m_macFontNum ) ;
661 ::TextSize( short(font->m_macFontSize) ) ;
662 ::TextFace( font->m_macFontStyle ) ;
663 }
664 else
665 {
666 ::TextFont( kFontIDMonaco ) ;
667 ::TextSize( 9 );
668 ::TextFace( 0 ) ;
669 }
670
671 // Find the widest line
672 for(int i = 0; i < GetCount(); i++) {
673 wxString str(GetString(i));
674 wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ;
675 lbWidth = wxMax(lbWidth, wLine);
676 }
677
678 // Add room for the scrollbar
679 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
680
681 // And just a bit more
682 int cy = 12 ;
683 int cx = ::TextWidth( "X" , 0 , 1 ) ;
684 lbWidth += cx ;
685
686 // don't make the listbox too tall (limit height to around 10 items) but don't
687 // make it too small neither
688 lbHeight = (cy+4) * wxMin(wxMax(GetCount(), 3), 10);
689 }
690 return wxSize(lbWidth, lbHeight);
e9576ca5
SC
691}
692
51abe921
SC
693int wxListBox::GetCount() const
694{
695 return m_noItems;
696}
697
698void wxListBox::SetupColours()
699{
a756f210 700 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
51abe921
SC
701 SetForegroundColour(GetParent()->GetForegroundColour());
702}
703
60149370
GD
704void wxListBox::Refresh(bool eraseBack, const wxRect *rect)
705{
de043984
SC
706 wxControl::Refresh( eraseBack , rect ) ;
707// MacRedrawControl() ;
60149370
GD
708}
709
51abe921
SC
710#if wxUSE_OWNER_DRAWN
711
712class wxListBoxItem : public wxOwnerDrawn
713{
714public:
715 wxListBoxItem(const wxString& str = "");
716};
717
718wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE)
719{
720 // no bitmaps/checkmarks
721 SetMarginWidth(0);
722}
723
724wxOwnerDrawn *wxListBox::CreateItem(size_t n)
725{
726 return new wxListBoxItem();
727}
728
729#endif //USE_OWNER_DRAWN
e9576ca5 730
519cb848
SC
731// ============================================================================
732// list box control implementation
733// ============================================================================
734
2b5f62a0 735/*
519cb848
SC
736void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon)
737{
a8e6bf8a
RR
738 wxListBox* list;
739 // typecast our refCon
740 list = (wxListBox*)refCon;
dc0ace7c 741
a8e6bf8a
RR
742 MoveTo(cellRect->left + 4 , cellRect->top + 10 );
743 const wxString text = list->m_stringArray[lCell.v] ;
744 ::TextFont( kFontIDMonaco ) ;
745 ::TextSize( 9 );
746 ::TextFace( 0 ) ;
747 DrawText(text, 0 , text.Length());
dc0ace7c 748
519cb848 749}
2b5f62a0 750*/
519cb848
SC
751void wxListBox::MacDelete( int N )
752{
76a5e5d2 753 LDelRow( 1 , N , (ListHandle)m_macList) ;
60149370 754 Refresh();
519cb848
SC
755}
756
dc0ace7c 757void wxListBox::MacInsert( int n , const char * text)
519cb848 758{
60149370
GD
759 Cell cell = { 0 , 0 } ;
760 cell.v = n ;
76a5e5d2 761 LAddRow( 1 , cell.v , (ListHandle)m_macList ) ;
e42e45a9 762// LSetCell(text, strlen(text), cell, m_macList);
60149370 763 Refresh();
519cb848
SC
764}
765
dc0ace7c 766void wxListBox::MacAppend( const char * text)
519cb848 767{
60149370 768 Cell cell = { 0 , 0 } ;
76a5e5d2
SC
769 cell.v = (**(ListHandle)m_macList).dataBounds.bottom ;
770 LAddRow( 1 , cell.v , (ListHandle)m_macList ) ;
e42e45a9 771 // LSetCell(text, strlen(text), cell, m_macList);
60149370 772 Refresh();
519cb848
SC
773}
774
dc0ace7c 775void wxListBox::MacClear()
519cb848 776{
76a5e5d2 777 LDelRow( (**(ListHandle)m_macList).dataBounds.bottom , 0 ,(ListHandle) m_macList ) ;
60149370 778 Refresh();
519cb848
SC
779}
780
781void wxListBox::MacSetSelection( int n , bool select )
782{
a8e6bf8a
RR
783 Cell cell = { 0 , 0 } ;
784 if ( ! (m_windowStyle & wxLB_MULTIPLE) )
785 {
9f081f02
GD
786 if ( LGetSelect( true , &cell , (ListHandle)m_macList ) )
787 {
788 LSetSelect( false , cell , (ListHandle)m_macList ) ;
789 }
a8e6bf8a 790 }
dc0ace7c 791
a8e6bf8a 792 cell.v = n ;
76a5e5d2
SC
793 LSetSelect( select , cell , (ListHandle)m_macList ) ;
794 LAutoScroll( (ListHandle)m_macList ) ;
a8e6bf8a 795 Refresh();
519cb848
SC
796}
797
798bool wxListBox::MacIsSelected( int n ) const
799{
a8e6bf8a
RR
800 Cell cell = { 0 , 0 } ;
801 cell.v = n ;
76a5e5d2 802 return LGetSelect( false , &cell , (ListHandle)m_macList ) ;
519cb848
SC
803}
804
805void wxListBox::MacDestroy()
806{
60149370 807// DisposeExtLDEFInfo( m_macList ) ;
519cb848
SC
808}
809
810int wxListBox::MacGetSelection() const
811{
a8e6bf8a 812 Cell cell = { 0 , 0 } ;
76a5e5d2 813 if ( LGetSelect( true , &cell , (ListHandle)m_macList ) )
a8e6bf8a
RR
814 return cell.v ;
815 else
816 return -1 ;
519cb848
SC
817}
818
819int wxListBox::MacGetSelections( wxArrayInt& aSelections ) const
820{
a8e6bf8a 821 int no_sel = 0 ;
dc0ace7c 822
519cb848
SC
823 aSelections.Empty();
824
a8e6bf8a
RR
825 Cell cell = { 0 , 0 } ;
826 cell.v = 0 ;
dc0ace7c 827
76a5e5d2 828 while ( LGetSelect( true , &cell ,(ListHandle) m_macList ) )
a8e6bf8a
RR
829 {
830 aSelections.Add( cell.v ) ;
831 no_sel++ ;
832 cell.v++ ;
833 }
834 return no_sel ;
519cb848
SC
835}
836
837void wxListBox::MacSet( int n , const char * text )
838{
a8e6bf8a
RR
839 // our implementation does not store anything in the list
840 // so we just have to redraw
841 Cell cell = { 0 , 0 } ;
842 cell.v = n ;
843// LSetCell(text, strlen(text), cell, m_macList);
844 Refresh();
519cb848
SC
845}
846
847void wxListBox::MacScrollTo( int n )
848{
a8e6bf8a 849 // TODO implement scrolling
519cb848
SC
850}
851
852void wxListBox::OnSize( const wxSizeEvent &event)
853{
60149370
GD
854 Point pt;
855
856#if TARGET_CARBON
962cbf2e 857 GetListCellSize((ListHandle)m_macList, &pt);
60149370 858#else
76a5e5d2 859 pt = (**(ListHandle)m_macList).cellSize ;
60149370
GD
860#endif
861 pt.h = m_width - 15 ;
76a5e5d2 862 LCellSize( pt , (ListHandle)m_macList ) ;
519cb848
SC
863}
864
dc0ace7c 865void wxListBox::MacHandleControlClick( WXWidget control , wxInt16 controlpart )
519cb848 866{
a8e6bf8a
RR
867 Boolean wasDoubleClick = false ;
868 long result ;
519cb848 869
76a5e5d2 870 ::GetControlData( (ControlHandle) m_macControl , kControlNoPart , kControlListBoxDoubleClickTag , sizeof( wasDoubleClick ) , (char*) &wasDoubleClick , &result ) ;
a8e6bf8a
RR
871 if ( !wasDoubleClick )
872 {
873 MacDoClick() ;
874 }
875 else
876 {
877 MacDoDoubleClick() ;
878 }
519cb848
SC
879}
880
dc0ace7c 881void wxListBox::MacSetRedraw( bool doDraw )
519cb848 882{
76a5e5d2 883 LSetDrawingMode( doDraw , (ListHandle)m_macList ) ;
dc0ace7c 884
519cb848
SC
885}
886
887void wxListBox::MacDoClick()
888{
a8e6bf8a 889 wxArrayInt aSelections;
68a9d9d0
SC
890 int n ;
891 size_t count = GetSelections(aSelections);
dc0ace7c 892
a8e6bf8a
RR
893 if ( count == m_selectionPreImage.GetCount() )
894 {
895 bool hasChanged = false ;
68a9d9d0 896 for ( size_t i = 0 ; i < count ; ++i )
a8e6bf8a
RR
897 {
898 if ( aSelections[i] != m_selectionPreImage[i] )
899 {
900 hasChanged = true ;
901 break ;
902 }
903 }
904 if ( !hasChanged )
905 {
906 return ;
907 }
908 }
dc0ace7c 909
a8e6bf8a 910 m_selectionPreImage = aSelections;
dc0ace7c 911
a8e6bf8a
RR
912 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
913 event.SetEventObject( this );
914
915 if ( count > 0 )
916 {
917 n = aSelections[0];
918 if ( HasClientObjectData() )
919 event.SetClientObject( GetClientObject(n) );
920 else if ( HasClientUntypedData() )
921 event.SetClientData( GetClientData(n) );
922 event.SetString( GetString(n) );
923 }
924 else
925 {
e7549107 926 n = -1;
a8e6bf8a 927 }
dc0ace7c 928
e7549107
SC
929 event.m_commandInt = n;
930
931 GetEventHandler()->ProcessEvent(event);
519cb848
SC
932}
933
934void wxListBox::MacDoDoubleClick()
935{
936 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId);
937 event.SetEventObject( this );
dc0ace7c 938 GetEventHandler()->ProcessEvent(event) ;
519cb848 939}
ecaf6c18 940
ecaf6c18
SC
941void wxListBox::OnChar(wxKeyEvent& event)
942{
eb22f2a6 943 if ( event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER)
92104223
SC
944 {
945 wxWindow* parent = GetParent() ;
946 while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL )
947 parent = parent->GetParent() ;
dc0ace7c 948
92104223
SC
949 if ( parent && parent->GetDefaultItem() )
950 {
951 wxButton *def = wxDynamicCast(parent->GetDefaultItem(),
952 wxButton);
953 if ( def && def->IsEnabled() )
954 {
955 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
956 event.SetEventObject(def);
957 def->Command(event);
958 return ;
959 }
960 }
961 event.Skip() ;
962 }
963 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
eb22f2a6 964 else if (event.GetKeyCode() == WXK_ESCAPE || (event.GetKeyCode() == '.' && event.MetaDown() ) )
92104223
SC
965 {
966 wxWindow* win = GetParent()->FindWindow( wxID_CANCEL ) ;
967 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
968 new_event.SetEventObject( win );
969 win->GetEventHandler()->ProcessEvent( new_event );
970 }
eb22f2a6 971 else if ( event.GetKeyCode() == WXK_TAB )
92104223
SC
972 {
973 wxNavigationKeyEvent new_event;
974 new_event.SetEventObject( this );
975 new_event.SetDirection( !event.ShiftDown() );
976 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
977 new_event.SetWindowChange( event.ControlDown() );
978 new_event.SetCurrentFocus( this );
979 if ( !GetEventHandler()->ProcessEvent( new_event ) )
980 event.Skip() ;
981 }
eb22f2a6 982 else if ( event.GetKeyCode() == WXK_DOWN || event.GetKeyCode() == WXK_UP )
ecaf6c18 983 {
92104223 984 // perform the default key handling first
202848fe 985 wxControl::OnKeyDown( event ) ;
dc0ace7c 986
ecaf6c18
SC
987 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
988 event.SetEventObject( this );
989
990 wxArrayInt aSelections;
991 int n, count = GetSelections(aSelections);
992 if ( count > 0 )
993 {
994 n = aSelections[0];
995 if ( HasClientObjectData() )
996 event.SetClientObject( GetClientObject(n) );
997 else if ( HasClientUntypedData() )
998 event.SetClientData( GetClientData(n) );
999 event.SetString( GetString(n) );
1000 }
1001 else
1002 {
1003 n = -1;
1004 }
1005
1006 event.m_commandInt = n;
1007
1008 GetEventHandler()->ProcessEvent(event);
1009 }
ecaf6c18
SC
1010 else
1011 {
92104223 1012 if ( event.GetTimestamp() > m_lastTypeIn + 60 )
ecaf6c18
SC
1013 {
1014 m_typeIn = "" ;
1015 }
92104223 1016 m_lastTypeIn = event.GetTimestamp() ;
eb22f2a6 1017 m_typeIn += (char) event.GetKeyCode() ;
ecaf6c18
SC
1018 int line = FindString("*"+m_typeIn+"*") ;
1019 if ( line >= 0 )
1020 {
92104223
SC
1021 if ( GetSelection() != line )
1022 {
1023 SetSelection(line) ;
1024 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
1025 event.SetEventObject( this );
1026
1027 if ( HasClientObjectData() )
1028 event.SetClientObject( GetClientObject( line ) );
1029 else if ( HasClientUntypedData() )
1030 event.SetClientData( GetClientData(line) );
1031 event.SetString( GetString(line) );
1032
1033 event.m_commandInt = line ;
1034
1035 GetEventHandler()->ProcessEvent(event);
1036 }
ecaf6c18
SC
1037 }
1038 }
1039}
1040