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