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