]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/listbox.cpp
different approaches implemented
[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)
facd6764 30#if !TARGET_API_MAC_OSX
e40298d5
JS
31 EVT_SIZE( wxListBox::OnSize )
32 EVT_CHAR( wxListBox::OnChar )
facd6764 33#endif
519cb848 34END_EVENT_TABLE()
2f1ae414 35#endif
e9576ca5 36
facd6764
SC
37#include "wx/mac/uma.h"
38
39#if TARGET_API_MAC_OSX
40
41// new databrowserbased version
42
43// Listbox item
44wxListBox::wxListBox()
45{
46 m_noItems = 0;
47 m_selected = 0;
48 m_macList = NULL ;
49}
50
51bool wxListBox::Create(wxWindow *parent, wxWindowID id,
52 const wxPoint& pos,
53 const wxSize& size,
54 const wxArrayString& choices,
55 long style,
56 const wxValidator& validator,
57 const wxString& name)
58{
59 wxCArrayString chs(choices);
60
61 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
62 style, validator, name);
63}
64
83ce5634
SC
65#if TARGET_API_MAC_OSX
66static pascal void DataBrowserItemNotificationProc(ControlRef browser, DataBrowserItemID itemID,
67 DataBrowserItemNotification message, DataBrowserItemDataRef itemData)
68#else
69static pascal void DataBrowserItemNotificationProc(ControlRef browser, DataBrowserItemID itemID,
70 DataBrowserItemNotification message)
71#endif
72{
73 long ref = GetControlReference( browser ) ;
74 if ( ref )
75 {
76 wxListBox* list = wxDynamicCast( ref , wxListBox ) ;
77 for ( size_t i = 0 ; i < list->m_idArray.GetCount() ; ++i )
78 if ( list->m_idArray[i] == (long) itemID )
79 {
80 bool trigger = false ;
81 wxCommandEvent event(
82 wxEVT_COMMAND_LISTBOX_SELECTED, list->GetId() );
83 switch( message )
84 {
85 case kDataBrowserItemDeselected :
86 if ( list->HasMultipleSelection() )
87 trigger = true ;
88 break ;
89 case kDataBrowserItemSelected :
90 trigger = true ;
91 break ;
92 case kDataBrowserItemDoubleClicked :
93 event.SetEventType(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED) ;
94 trigger = true ;
95 break ;
96 default :
97 break ;
98 }
99 if ( trigger )
100 {
101 event.SetEventObject( list );
102 if ( list->HasClientObjectData() )
103 event.SetClientObject( list->GetClientObject(i) );
104 else if ( list->HasClientUntypedData() )
105 event.SetClientData( list->GetClientData(i) );
106 event.SetString( list->GetString(i) );
107 event.SetInt(i) ;
108 event.SetExtraLong( list->HasMultipleSelection() ? message == kDataBrowserItemSelected : TRUE );
109 list->GetEventHandler()->ProcessEvent(event) ;
110 }
111 break ;
112 }
113 }
114}
115
116
facd6764
SC
117static pascal OSStatus ListBoxGetSetItemData(ControlRef browser,
118 DataBrowserItemID itemID, DataBrowserPropertyID property,
119 DataBrowserItemDataRef itemData, Boolean changeValue)
120{
121 OSStatus err = errDataBrowserPropertyNotSupported;
122
123 if ( ! changeValue )
124 {
125 switch (property)
126 {
127
128 case 1024:
129 {
130 long ref = GetControlReference( browser ) ;
131 if ( ref )
132 {
133 wxListBox* list = wxDynamicCast( ref , wxListBox ) ;
134 for ( size_t i = 0 ; i < list->m_idArray.GetCount() ; ++i )
135 if ( list->m_idArray[i] == (long) itemID )
136 {
137 wxMacCFStringHolder cf( list->GetString(i) , list->GetFont().GetEncoding() ) ;
138 verify_noerr( ::SetDataBrowserItemDataText( itemData , cf ) ) ;
139 err = noErr ;
140 break ;
141 }
142 }
143 }
144 break;
145
146 default:
147
148 break;
149 }
150 }
151
152 return err;
153}
154bool wxListBox::Create(wxWindow *parent, wxWindowID id,
155 const wxPoint& pos,
156 const wxSize& size,
157 int n, const wxString choices[],
158 long style,
159 const wxValidator& validator,
160 const wxString& name)
161{
162 m_macIsUserPane = FALSE ;
163
164 if ( !wxListBoxBase::Create(parent, id, pos, size, style & ~(wxHSCROLL|wxVSCROLL), validator, name) )
165 return false;
166
167 m_noItems = 0 ; // this will be increased by our append command
168 m_selected = 0;
169 m_nextId = 1 ;
170
171
172 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
173 ControlRef browser ;
174
175 verify_noerr( ::CreateDataBrowserControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds, kDataBrowserListView , (ControlRef *)&m_macControl ) );
176 browser = (ControlRef) m_macControl ;
177
178 DataBrowserSelectionFlags options = kDataBrowserDragSelect ;
179 if ( style & wxLB_MULTIPLE )
180 {
181 options += kDataBrowserAlwaysExtendSelection + kDataBrowserCmdTogglesSelection ;
182 }
183 else if ( style & wxLB_EXTENDED )
184 {
185 // default behaviour
186 }
187 else
188 {
189 options += kDataBrowserSelectOnlyOne ;
190 }
191 verify_noerr(SetDataBrowserSelectionFlags (browser, options ) );
192
193 DataBrowserListViewColumnDesc columnDesc ;
194 columnDesc.headerBtnDesc.titleOffset = 0;
195 columnDesc.headerBtnDesc.version = kDataBrowserListViewLatestHeaderDesc;
196
197 columnDesc.headerBtnDesc.btnFontStyle.flags =
198 kControlUseFontMask | kControlUseJustMask;
199
200 columnDesc.headerBtnDesc.btnContentInfo.contentType = kControlNoContent;
201 columnDesc.propertyDesc.propertyType = kDataBrowserTextType;
202 columnDesc.headerBtnDesc.btnFontStyle.just = teFlushDefault;
203 columnDesc.headerBtnDesc.minimumWidth = 0;
204 columnDesc.headerBtnDesc.maximumWidth = 10000;
205
206 columnDesc.headerBtnDesc.btnFontStyle.font = kControlFontViewSystemFont;
207 columnDesc.headerBtnDesc.btnFontStyle.style = normal;
208 columnDesc.headerBtnDesc.titleString = NULL ; // CFSTR( "" );
209
210 columnDesc.propertyDesc.propertyID = 1024;
211 columnDesc.propertyDesc.propertyType = kDataBrowserTextType;
c2697b87 212 columnDesc.propertyDesc.propertyFlags =
9bd2d050 213#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
c2697b87
SC
214 kDataBrowserListViewTypeSelectColumn |
215#endif
216 kDataBrowserTableViewSelectionColumn ;
facd6764
SC
217
218
219 verify_noerr(::AddDataBrowserListViewColumn(browser, &columnDesc, kDataBrowserListViewAppendColumn) ) ;
220 verify_noerr(::AutoSizeDataBrowserListViewColumns( browser ) ) ;
221 verify_noerr(::SetDataBrowserHasScrollBars( browser , false , true ) ) ;
222 verify_noerr(::SetDataBrowserTableViewHiliteStyle( browser, kDataBrowserTableViewFillHilite ) ) ;
223 verify_noerr(::SetDataBrowserListViewHeaderBtnHeight( browser , 0 ) ) ;
224 DataBrowserCallbacks callbacks ;
225
226 callbacks.version = kDataBrowserLatestCallbacks;
227
228 InitDataBrowserCallbacks(&callbacks);
229
230 callbacks.u.v1.itemDataCallback =
231 NewDataBrowserItemDataUPP(ListBoxGetSetItemData);
232
83ce5634
SC
233 callbacks.u.v1.itemNotificationCallback =
234#if TARGET_API_MAC_OSX
235 (DataBrowserItemNotificationUPP) NewDataBrowserItemNotificationWithItemUPP(DataBrowserItemNotificationProc) ;
236#else
237 NewDataBrowserItemNotificationUPP(DataBrowserItemNotificationProc) ;
238#endif
facd6764
SC
239 SetDataBrowserCallbacks(browser, &callbacks);
240
241 MacPostControlCreate(pos,size) ;
242
243 for ( int i = 0 ; i < n ; i++ )
244 {
245 Append( choices[i] ) ;
246 }
247
248 return TRUE;
249}
250
251wxListBox::~wxListBox()
252{
253 SetControlReference( (ControlRef) m_macControl , NULL ) ;
254 FreeData() ;
255 // avoid access during destruction
256 if ( m_macList )
257 {
258 m_macList = NULL ;
259 }
260}
261
262void wxListBox::FreeData()
263{
264#if wxUSE_OWNER_DRAWN
265 if ( m_windowStyle & wxLB_OWNERDRAW )
266 {
267 size_t uiCount = m_aItems.Count();
268 while ( uiCount-- != 0 ) {
269 delete m_aItems[uiCount];
270 m_aItems[uiCount] = NULL;
271 }
272
273 m_aItems.Clear();
274 }
275 else
276#endif // wxUSE_OWNER_DRAWN
277 if ( HasClientObjectData() )
278 {
279 for ( size_t n = 0; n < (size_t)m_noItems; n++ )
280 {
281 delete GetClientObject(n);
282 }
283 }
284}
285
286void wxListBox::DoSetSize(int x, int y,
287 int width, int height,
288 int sizeFlags )
289{
290 wxControl::DoSetSize( x , y , width , height , sizeFlags ) ;
291}
292
293void wxListBox::DoSetFirstItem(int N)
294{
295 MacScrollTo( N ) ;
296}
297
298void wxListBox::Delete(int N)
299{
300 wxCHECK_RET( N >= 0 && N < m_noItems,
301 wxT("invalid index in wxListBox::Delete") );
302
303#if wxUSE_OWNER_DRAWN
304 delete m_aItems[N];
305 m_aItems.RemoveAt(N);
306#else // !wxUSE_OWNER_DRAWN
307 if ( HasClientObjectData() )
308 {
309 delete GetClientObject(N);
310 }
311#endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
312 m_stringArray.RemoveAt( N ) ;
313 m_dataArray.RemoveAt( N ) ;
314 m_noItems --;
315
316 MacDelete( N ) ;
317}
318
319int wxListBox::DoAppend(const wxString& item)
320{
321 int index = m_noItems ;
322 m_stringArray.Add( item ) ;
323 m_dataArray.Add( NULL );
324 m_noItems ++;
325 DoSetItemClientData( index , NULL ) ;
326 MacAppend( item ) ;
327
328 return index ;
329}
330
331void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
332{
333 Clear() ;
334 int n = choices.GetCount();
335
336 for( int i = 0 ; i < n ; ++i )
337 {
338 if ( clientData )
339 {
340#if wxUSE_OWNER_DRAWN
341 wxASSERT_MSG(clientData[i] == NULL,
342 wxT("Can't use client data with owner-drawn listboxes"));
343#else // !wxUSE_OWNER_DRAWN
344 Append( choices[i] , clientData[i] ) ;
345#endif
346 }
347 else
348 Append( choices[i] ) ;
349 }
350
351#if wxUSE_OWNER_DRAWN
352 if ( m_windowStyle & wxLB_OWNERDRAW ) {
353 // first delete old items
354 size_t ui = m_aItems.Count();
355 while ( ui-- != 0 ) {
356 delete m_aItems[ui];
357 m_aItems[ui] = NULL;
358 }
359 m_aItems.Empty();
360
361 // then create new ones
362 for ( ui = 0; ui < (size_t)m_noItems; ui++ ) {
363 wxOwnerDrawn *pNewItem = CreateItem(ui);
364 pNewItem->SetName(choices[ui]);
365 m_aItems.Add(pNewItem);
366 }
367 }
368#endif // wxUSE_OWNER_DRAWN
369}
370
facd6764
SC
371int wxListBox::FindString(const wxString& s) const
372{
373
374 if ( s.Right(1) == wxT("*") )
375 {
376 wxString search = s.Left( s.Length() - 1 ) ;
377 int len = search.Length() ;
378 Str255 s1 , s2 ;
379 wxMacStringToPascal( search , s2 ) ;
380
381 for ( int i = 0 ; i < m_noItems ; ++ i )
382 {
383 wxMacStringToPascal( m_stringArray[i].Left( len ) , s1 ) ;
384
385 if ( EqualString( s1 , s2 , false , false ) )
386 return i ;
387 }
388 if ( s.Left(1) == wxT("*") && s.Length() > 1 )
389 {
390 wxString st = s ;
391 st.MakeLower() ;
392 for ( int i = 0 ; i < m_noItems ; ++i )
393 {
394 if ( GetString(i).Lower().Matches(st) )
395 return i ;
396 }
397 }
398
399 }
400 else
401 {
402 Str255 s1 , s2 ;
403
404 wxMacStringToPascal( s , s2 ) ;
405
406 for ( int i = 0 ; i < m_noItems ; ++ i )
407 {
408 wxMacStringToPascal( m_stringArray[i] , s1 ) ;
409
410 if ( EqualString( s1 , s2 , false , false ) )
411 return i ;
412 }
413 }
414 return -1;
415}
416
417void wxListBox::Clear()
418{
419 FreeData();
420 m_noItems = 0;
421 m_stringArray.Empty() ;
422 m_dataArray.Empty() ;
423 MacClear() ;
424}
425
426void wxListBox::SetSelection(int N, bool select)
427{
428 wxCHECK_RET( N >= 0 && N < m_noItems,
429 wxT("invalid index in wxListBox::SetSelection") );
430 MacSetSelection( N , select ) ;
431 GetSelections( m_selectionPreImage ) ;
432}
433
434bool wxListBox::IsSelected(int N) const
435{
436 wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE,
437 wxT("invalid index in wxListBox::Selected") );
438
439 return MacIsSelected( N ) ;
440}
441
442void *wxListBox::DoGetItemClientData(int N) const
443{
444 wxCHECK_MSG( N >= 0 && N < m_noItems, NULL,
445 wxT("invalid index in wxListBox::GetClientData"));
446
447 return (void *)m_dataArray[N];
448}
449
450wxClientData *wxListBox::DoGetItemClientObject(int N) const
451{
452 return (wxClientData *) DoGetItemClientData( N ) ;
453}
454
455void wxListBox::DoSetItemClientData(int N, void *Client_data)
456{
457 wxCHECK_RET( N >= 0 && N < m_noItems,
458 wxT("invalid index in wxListBox::SetClientData") );
459
460#if wxUSE_OWNER_DRAWN
461 if ( m_windowStyle & wxLB_OWNERDRAW )
462 {
463 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
464 // in OnMeasure/OnDraw.
465 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
466 }
467#endif // wxUSE_OWNER_DRAWN
468 wxASSERT_MSG( m_dataArray.GetCount() >= (size_t) N , wxT("invalid client_data array") ) ;
469
470 if ( m_dataArray.GetCount() > (size_t) N )
471 {
472 m_dataArray[N] = (char*) Client_data ;
473 }
474 else
475 {
476 m_dataArray.Add( (char*) Client_data ) ;
477 }
478}
479
480void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
481{
482 DoSetItemClientData(n, clientData);
483}
484
485// Return number of selections and an array of selected integers
486int wxListBox::GetSelections(wxArrayInt& aSelections) const
487{
488 return MacGetSelections( aSelections ) ;
489}
490
491// Get single selection, for single choice list items
492int wxListBox::GetSelection() const
493{
494 return MacGetSelection() ;
495}
496
497// Find string for position
498wxString wxListBox::GetString(int N) const
499{
500 return m_stringArray[N] ;
501}
502
503void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
504{
505 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
506 wxT("invalid index in wxListBox::InsertItems") );
507
508 int nItems = items.GetCount();
509
510 for ( int i = 0 ; i < nItems ; i++ )
511 {
512 m_stringArray.Insert( items[i] , pos + i ) ;
513 m_dataArray.Insert( NULL , pos + i ) ;
514 MacInsert( pos + i , items[i] ) ;
515 }
516
517 m_noItems += nItems;
518}
519
520void wxListBox::SetString(int N, const wxString& s)
521{
522 m_stringArray[N] = s ;
523 MacSet( N , s ) ;
524}
525
526wxSize wxListBox::DoGetBestSize() const
527{
528 int lbWidth = 100; // some defaults
529 int lbHeight = 110;
530 int wLine;
531
532 {
533 wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetTopLevelWindowRef() ) ) ;
534
535 if ( m_font.Ok() )
536 {
537 ::TextFont( m_font.MacGetFontNum() ) ;
538 ::TextSize( m_font.MacGetFontSize() ) ;
539 ::TextFace( m_font.MacGetFontStyle() ) ;
540 }
541 else
542 {
543 ::TextFont( kFontIDMonaco ) ;
544 ::TextSize( 9 );
545 ::TextFace( 0 ) ;
546 }
547
548 // Find the widest line
549 for(int i = 0; i < GetCount(); i++) {
550 wxString str(GetString(i));
551 #if wxUSE_UNICODE
552 Point bounds={0,0} ;
553 SInt16 baseline ;
554 ::GetThemeTextDimensions( wxMacCFStringHolder( str , m_font.GetEncoding() ) ,
555 kThemeCurrentPortFont,
556 kThemeStateActive,
557 false,
558 &bounds,
559 &baseline );
560 wLine = bounds.h ;
561 #else
562 wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ;
563 #endif
564 lbWidth = wxMax(lbWidth, wLine);
565 }
566
567 // Add room for the scrollbar
568 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
569
570 // And just a bit more
571 int cy = 12 ;
572 int cx = ::TextWidth( "X" , 0 , 1 ) ;
573 lbWidth += cx ;
574
575 // don't make the listbox too tall (limit height to around 10 items) but don't
576 // make it too small neither
577 lbHeight = (cy+4) * wxMin(wxMax(GetCount(), 3), 10);
578 }
579
580 return wxSize(lbWidth, lbHeight);
581}
582
583int wxListBox::GetCount() const
584{
585 return m_noItems;
586}
587
588void wxListBox::Refresh(bool eraseBack, const wxRect *rect)
589{
590 wxControl::Refresh( eraseBack , rect ) ;
591 // MacRedrawControl() ;
592}
593
594#if wxUSE_OWNER_DRAWN
595
596class wxListBoxItem : public wxOwnerDrawn
597{
598public:
599 wxListBoxItem(const wxString& str = "");
600};
601
602wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE)
603{
604 // no bitmaps/checkmarks
605 SetMarginWidth(0);
606}
607
608wxOwnerDrawn *wxListBox::CreateItem(size_t n)
609{
610 return new wxListBoxItem();
611}
612
613#endif //USE_OWNER_DRAWN
614
615// ============================================================================
616// list box control implementation
617// ============================================================================
618
619void wxListBox::MacDelete( int N )
620{
621 UInt32 id = m_idArray[N] ;
622 verify_noerr(::RemoveDataBrowserItems((ControlRef) m_macControl , kDataBrowserNoItem , 1 , (UInt32*) &id , kDataBrowserItemNoProperty ) ) ;
623 m_idArray.RemoveAt( N ) ;
624}
625
626void wxListBox::MacInsert( int n , const wxString& text)
627{
628 verify_noerr(::AddDataBrowserItems( (ControlRef) m_macControl , kDataBrowserNoItem , 1 , (UInt32*) &m_nextId , kDataBrowserItemNoProperty ) ) ;
629 m_idArray.Insert( m_nextId , n ) ;
630 ++m_nextId ;
631}
632
633void wxListBox::MacAppend( const wxString& text)
634{
635 verify_noerr(::AddDataBrowserItems( (ControlRef) m_macControl , kDataBrowserNoItem , 1 , (UInt32*) &m_nextId , kDataBrowserItemNoProperty ) ) ;
636 m_idArray.Add( m_nextId ) ;
637 ++m_nextId ;
638}
639
640void wxListBox::MacClear()
641{
642 verify_noerr(::RemoveDataBrowserItems((ControlRef) m_macControl , kDataBrowserNoItem , 0 , NULL , kDataBrowserItemNoProperty ) ) ;
83ce5634 643 m_idArray.Empty() ;
facd6764
SC
644}
645
646void wxListBox::MacSetSelection( int n , bool select )
647{
648 UInt32 id = m_idArray[n] ;
649 if ( ::IsDataBrowserItemSelected( (ControlRef) m_macControl , id ) != select )
650 {
651 verify_noerr(::SetDataBrowserSelectedItems((ControlRef) m_macControl , 1 , & id , kDataBrowserItemsToggle ) ) ;
652 }
653 MacScrollTo( n ) ;
654}
655
656bool wxListBox::MacIsSelected( int n ) const
657{
658 return ::IsDataBrowserItemSelected( (ControlRef) m_macControl , m_idArray[n] ) ;
659}
660
661int wxListBox::MacGetSelection() const
662{
663 for ( size_t i = 0 ; i < m_idArray.GetCount() ; ++i )
664 {
665 if ( ::IsDataBrowserItemSelected((ControlRef) m_macControl , m_idArray[i] ) )
666 {
667 return i ;
668 }
669 }
670 return -1 ;
671}
672
673int wxListBox::MacGetSelections( wxArrayInt& aSelections ) const
674{
675 int no_sel = 0 ;
676
677 aSelections.Empty();
678 for ( size_t i = 0 ; i < m_idArray.GetCount() ; ++i )
679 {
680 if ( ::IsDataBrowserItemSelected((ControlRef) m_macControl , m_idArray[i] ) )
681 {
682 aSelections.Add( i ) ;
683 no_sel++ ;
684 }
685 }
686 return no_sel ;
687}
519cb848 688
facd6764
SC
689void wxListBox::MacSet( int n , const wxString& text )
690{
691 // as we don't store the strings we only have to issue a redraw
692 UInt32 id = m_idArray[n] ;
693 verify_noerr( ::UpdateDataBrowserItems( (ControlRef) m_macControl , kDataBrowserNoItem , 1 , &id , kDataBrowserItemNoProperty , kDataBrowserItemNoProperty ) ) ;
694}
e42e45a9 695
facd6764
SC
696void wxListBox::MacScrollTo( int n )
697{
698 // TODO implement scrolling
699}
700
701void wxListBox::OnSize( wxSizeEvent &event)
702{
703}
704
705void wxListBox::MacHandleControlClick( WXWidget control , wxInt16 controlpart , bool WXUNUSED(mouseStillDown))
706{
83ce5634 707 /*
facd6764
SC
708 Boolean wasDoubleClick = false ;
709 long result ;
710
711 ::GetControlData( (ControlRef) m_macControl , kControlNoPart , kControlListBoxDoubleClickTag , sizeof( wasDoubleClick ) , (char*) &wasDoubleClick , &result ) ;
712 if ( !wasDoubleClick )
713 {
714 MacDoClick() ;
715 }
716 else
717 {
718 MacDoDoubleClick() ;
719 }
83ce5634 720 */
facd6764
SC
721}
722
723void wxListBox::MacSetRedraw( bool doDraw )
724{
725 // nothing to do in compositing mode
726}
727
728void wxListBox::MacDoClick()
83ce5634 729{/*
facd6764
SC
730 wxArrayInt aSelections;
731 int n ;
732 size_t count = GetSelections(aSelections);
733
734 if ( count == m_selectionPreImage.GetCount() )
735 {
736 bool hasChanged = false ;
737 for ( size_t i = 0 ; i < count ; ++i )
738 {
739 if ( aSelections[i] != m_selectionPreImage[i] )
740 {
741 hasChanged = true ;
742 break ;
743 }
744 }
745 if ( !hasChanged )
746 {
747 return ;
748 }
749 }
750
751 m_selectionPreImage = aSelections;
752
753 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
754 event.SetEventObject( this );
755
756 if ( count > 0 )
757 {
758 n = aSelections[0];
759 if ( HasClientObjectData() )
760 event.SetClientObject( GetClientObject(n) );
761 else if ( HasClientUntypedData() )
762 event.SetClientData( GetClientData(n) );
763 event.SetString( GetString(n) );
764 }
765 else
766 {
767 n = -1;
768 }
769
770 event.m_commandInt = n;
771
772 GetEventHandler()->ProcessEvent(event);
83ce5634 773*/
facd6764
SC
774}
775
776void wxListBox::MacDoDoubleClick()
777{
83ce5634 778/*
facd6764
SC
779 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId);
780 event.SetEventObject( this );
781 GetEventHandler()->ProcessEvent(event) ;
83ce5634 782*/
facd6764
SC
783}
784
785void wxListBox::OnChar(wxKeyEvent& event)
786{
787 // todo trigger proper events here
788 event.Skip() ;
789 return ;
790
791 if ( event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER)
792 {
793 wxWindow* parent = GetParent() ;
794 while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL )
795 parent = parent->GetParent() ;
796
797 if ( parent && parent->GetDefaultItem() )
798 {
799 wxButton *def = wxDynamicCast(parent->GetDefaultItem(),
800 wxButton);
801 if ( def && def->IsEnabled() )
802 {
803 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
804 event.SetEventObject(def);
805 def->Command(event);
806 return ;
807 }
808 }
809 event.Skip() ;
810 }
811 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
812 else if (event.GetKeyCode() == WXK_ESCAPE || (event.GetKeyCode() == '.' && event.MetaDown() ) )
813 {
814 // FIXME: look in ancestors, not just parent.
815 wxWindow* win = GetParent()->FindWindow( wxID_CANCEL ) ;
816 if (win)
817 {
818 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
819 new_event.SetEventObject( win );
820 win->GetEventHandler()->ProcessEvent( new_event );
821 }
822 }
823 else if ( event.GetKeyCode() == WXK_TAB )
824 {
825 wxNavigationKeyEvent new_event;
826 new_event.SetEventObject( this );
827 new_event.SetDirection( !event.ShiftDown() );
828 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
829 new_event.SetWindowChange( event.ControlDown() );
830 new_event.SetCurrentFocus( this );
831 if ( !GetEventHandler()->ProcessEvent( new_event ) )
832 event.Skip() ;
833 }
834 else if ( event.GetKeyCode() == WXK_DOWN || event.GetKeyCode() == WXK_UP )
835 {
836 // perform the default key handling first
837 wxControl::OnKeyDown( event ) ;
838
839 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
840 event.SetEventObject( this );
841
842 wxArrayInt aSelections;
843 int n, count = GetSelections(aSelections);
844 if ( count > 0 )
845 {
846 n = aSelections[0];
847 if ( HasClientObjectData() )
848 event.SetClientObject( GetClientObject(n) );
849 else if ( HasClientUntypedData() )
850 event.SetClientData( GetClientData(n) );
851 event.SetString( GetString(n) );
852 }
853 else
854 {
855 n = -1;
856 }
857
858 event.m_commandInt = n;
859
860 GetEventHandler()->ProcessEvent(event);
861 }
862 else
863 {
864 if ( event.GetTimestamp() > m_lastTypeIn + 60 )
865 {
866 m_typeIn = wxEmptyString ;
867 }
868 m_lastTypeIn = event.GetTimestamp() ;
869 m_typeIn += (char) event.GetKeyCode() ;
870 int line = FindString(wxT("*")+m_typeIn+wxT("*")) ;
871 if ( line >= 0 )
872 {
873 if ( GetSelection() != line )
874 {
875 SetSelection(line) ;
876 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
877 event.SetEventObject( this );
878
879 if ( HasClientObjectData() )
880 event.SetClientObject( GetClientObject( line ) );
881 else if ( HasClientUntypedData() )
882 event.SetClientData( GetClientData(line) );
883 event.SetString( GetString(line) );
884
885 event.m_commandInt = line ;
886
887 GetEventHandler()->ProcessEvent(event);
888 }
889 }
890 }
891}
573ac9dc 892
202848fe 893#else
facd6764
SC
894
895// old carbon version
896
897const short kwxMacListItemHeight = 19 ;
202848fe 898
e42e45a9
SC
899extern "C"
900{
901static pascal void wxMacListDefinition( short message, Boolean isSelected, Rect *drawRect,
a8e6bf8a
RR
902 Cell cell, short dataOffset, short dataLength,
903 ListHandle listHandle ) ;
e42e45a9
SC
904}
905
facd6764 906static pascal void wxMacListDefinition( short message, Boolean isSelected, Rect *d,
a8e6bf8a
RR
907 Cell cell, short dataOffset, short dataLength,
908 ListHandle listHandle )
909{
21956470 910 wxListBox* list;
facd6764
SC
911 Rect r = *d ;
912 Rect* drawRect = &r ;
913
914 list = (wxListBox*) GetControlReference( (ControlRef) GetListRefCon(listHandle) );
915 if ( list == NULL || list->GetHandle() == NULL || GetControlReference( ( ControlRef )list->GetHandle() ) == NULL )
21956470
SC
916 return ;
917
facd6764
SC
918 // the bounds passed in are not correct, adjust the right border
919 int x = 0 , y = 0 ;
920 Rect bounds ;
921 GetControlBounds( (ControlRef) list->GetHandle() , &bounds ) ;
922 r.right = r.left + (bounds.right - bounds.left ) - 16 ;
923
a8e6bf8a
RR
924 GrafPtr savePort;
925 GrafPtr grafPtr;
926 RgnHandle savedClipRegion;
927 SInt32 savedPenMode;
e40298d5
JS
928 GetPort(&savePort);
929 SetPort((**listHandle).port);
930 grafPtr = (**listHandle).port ;
a8e6bf8a 931 // typecast our refCon
e40298d5 932
a8e6bf8a 933 // Calculate the cell rect.
e40298d5 934
a8e6bf8a 935 switch( message ) {
e40298d5
JS
936 case lInitMsg:
937 break;
938
939 case lCloseMsg:
940 break;
941
942 case lDrawMsg:
a8e6bf8a 943 {
427ff662 944 const wxString linetext = list->m_stringArray[cell.v] ;
e40298d5 945
a8e6bf8a
RR
946 // Save the current clip region, and set the clip region to the area we are about
947 // to draw.
e40298d5 948
a8e6bf8a
RR
949 savedClipRegion = NewRgn();
950 GetClip( savedClipRegion );
facd6764 951
a8e6bf8a
RR
952 ClipRect( drawRect );
953 EraseRect( drawRect );
e40298d5 954
fcb35beb
VZ
955 const wxFont& font = list->GetFont();
956 if ( font.Ok() )
e40298d5 957 {
facd6764
SC
958 ::TextFont( font.MacGetFontNum() ) ;
959 ::TextSize( font.MacGetFontSize() ) ;
960 ::TextFace( font.MacGetFontStyle() ) ;
e40298d5
JS
961 }
962 else
963 {
964 ::TextFont( kFontIDMonaco ) ;
965 ::TextSize( 9 );
966 ::TextFace( 0 ) ;
967 }
968
427ff662
SC
969 {
970 Rect frame = { drawRect->top, drawRect->left + 4,
971 drawRect->top + kwxMacListItemHeight, drawRect->right + 10000 } ;
a9412f8f 972 CFMutableStringRef mString = CFStringCreateMutableCopy( NULL , 0 , wxMacCFStringHolder(linetext , list->GetFont().GetEncoding()) ) ;
427ff662 973 ::TruncateThemeText( mString , kThemeCurrentPortFont, kThemeStateActive, drawRect->right - drawRect->left , truncEnd , NULL ) ;
facd6764 974
427ff662
SC
975 ::DrawThemeTextBox( mString,
976 kThemeCurrentPortFont,
977 kThemeStateActive,
978 false,
979 &frame,
980 teJustLeft,
981 nil );
facd6764 982
427ff662
SC
983 CFRelease( mString ) ;
984 }
facd6764 985
a8e6bf8a
RR
986 // If the cell is hilited, do the hilite now. Paint the cell contents with the
987 // appropriate QuickDraw transform mode.
e40298d5 988
a8e6bf8a 989 if( isSelected ) {
76a5e5d2
SC
990 savedPenMode = GetPortPenMode( (CGrafPtr) grafPtr );
991 SetPortPenMode( (CGrafPtr)grafPtr, hilitetransfermode );
a8e6bf8a 992 PaintRect( drawRect );
76a5e5d2 993 SetPortPenMode( (CGrafPtr)grafPtr, savedPenMode );
a8e6bf8a 994 }
e40298d5 995
a8e6bf8a 996 // Restore the saved clip region.
e40298d5 997
a8e6bf8a
RR
998 SetClip( savedClipRegion );
999 DisposeRgn( savedClipRegion );
e40298d5
JS
1000 }
1001 break;
1002 case lHiliteMsg:
1003
1004 // Hilite or unhilite the cell. Paint the cell contents with the
1005 // appropriate QuickDraw transform mode.
1006
1007 GetPort( &grafPtr );
1008 savedPenMode = GetPortPenMode( (CGrafPtr)grafPtr );
1009 SetPortPenMode( (CGrafPtr)grafPtr, hilitetransfermode );
1010 PaintRect( drawRect );
1011 SetPortPenMode( (CGrafPtr)grafPtr, savedPenMode );
1012 break;
1013 default :
1014 break ;
a8e6bf8a 1015 }
dc0ace7c 1016 SetPort(savePort);
e42e45a9
SC
1017}
1018
519cb848 1019extern "C" void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon) ;
f81127c5 1020// resources ldef ids
519cb848 1021const short kwxMacListWithVerticalScrollbar = 128 ;
f81127c5 1022const short kwxMacListWithVerticalAndHorizontalScrollbar = 129 ;
519cb848 1023
e9576ca5
SC
1024// ============================================================================
1025// list box control implementation
1026// ============================================================================
1027
1028// Listbox item
1029wxListBox::wxListBox()
1030{
1031 m_noItems = 0;
1032 m_selected = 0;
2f1ae414 1033 m_macList = NULL ;
e9576ca5
SC
1034}
1035
e42e45a9
SC
1036static ListDefUPP macListDefUPP = NULL ;
1037
584ad2a3
MB
1038bool wxListBox::Create(wxWindow *parent, wxWindowID id,
1039 const wxPoint& pos,
1040 const wxSize& size,
1041 const wxArrayString& choices,
1042 long style,
1043 const wxValidator& validator,
1044 const wxString& name)
1045{
1046 wxCArrayString chs(choices);
1047
1048 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
1049 style, validator, name);
1050}
1051
e9576ca5
SC
1052bool wxListBox::Create(wxWindow *parent, wxWindowID id,
1053 const wxPoint& pos,
1054 const wxSize& size,
1055 int n, const wxString choices[],
1056 long style,
1057 const wxValidator& validator,
1058 const wxString& name)
1059{
facd6764
SC
1060 m_macIsUserPane = FALSE ;
1061
b657d5db 1062 if ( !wxListBoxBase::Create(parent, id, pos, size, style & ~(wxHSCROLL|wxVSCROLL), validator, name) )
b45ed7a2
VZ
1063 return false;
1064
60149370
GD
1065 m_noItems = 0 ; // this will be increased by our append command
1066 m_selected = 0;
dc0ace7c 1067
facd6764 1068 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
e40298d5 1069
60149370 1070 ListDefSpec listDef;
e42e45a9
SC
1071 listDef.defType = kListDefUserProcType;
1072 if ( macListDefUPP == NULL )
1073 {
e40298d5 1074 macListDefUPP = NewListDefUPP( wxMacListDefinition );
e42e45a9 1075 }
e40298d5
JS
1076 listDef.u.userProc = macListDefUPP ;
1077
2b5f62a0
VZ
1078 Str255 fontName ;
1079 SInt16 fontSize ;
1080 Style fontStyle ;
facd6764 1081
e40298d5 1082 GetThemeFont(kThemeViewsFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
facd6764 1083
427ff662 1084 SetFont( wxFont (fontSize, wxSWISS, wxNORMAL, wxNORMAL , false , wxMacMakeStringFromPascal( fontName ) ) ) ;
facd6764 1085
60149370 1086 Size asize;
519cb848 1087
519cb848 1088
facd6764 1089 CreateListBoxControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds, false, 0, 1, (style & wxLB_HSCROLL), true,
962cbf2e 1090 kwxMacListItemHeight, kwxMacListItemHeight, false, &listDef, (ControlRef *)&m_macControl );
519cb848 1091
facd6764 1092 GetControlData( (ControlRef) m_macControl, kControlNoPart, kControlListBoxListHandleTag,
60149370 1093 sizeof(ListHandle), (Ptr) &m_macList, &asize);
519cb848 1094
facd6764 1095 SetControlReference( (ControlRef) m_macControl, (long) this);
60149370 1096
dc0ace7c 1097
e42e45a9 1098 OptionBits options = 0;
60149370
GD
1099 if ( style & wxLB_MULTIPLE )
1100 {
09322209 1101 options += lExtendDrag + lUseSense ;
60149370
GD
1102 }
1103 else if ( style & wxLB_EXTENDED )
1104 {
09322209 1105 // default behaviour
60149370
GD
1106 }
1107 else
1108 {
2b5f62a0 1109 options = (OptionBits) lOnlyOne ;
60149370 1110 }
76a5e5d2 1111 SetListSelectionFlags((ListHandle)m_macList, options);
dc0ace7c 1112
60149370
GD
1113 for ( int i = 0 ; i < n ; i++ )
1114 {
a8e6bf8a 1115 Append( choices[i] ) ;
60149370 1116 }
dc0ace7c 1117
facd6764 1118 MacPostControlCreate(pos,size) ;
2b5f62a0 1119
76a5e5d2 1120 LSetDrawingMode( true , (ListHandle)m_macList ) ;
519cb848 1121
60149370 1122 return TRUE;
e9576ca5
SC
1123}
1124
1125wxListBox::~wxListBox()
1126{
facd6764 1127 SetControlReference( (ControlRef) m_macControl , NULL ) ;
4b651a46 1128 FreeData() ;
21956470 1129 // avoid access during destruction
a8e6bf8a
RR
1130 if ( m_macList )
1131 {
a8e6bf8a
RR
1132 m_macList = NULL ;
1133 }
e9576ca5
SC
1134}
1135
4b651a46 1136void wxListBox::FreeData()
e9576ca5 1137{
e7549107
SC
1138#if wxUSE_OWNER_DRAWN
1139 if ( m_windowStyle & wxLB_OWNERDRAW )
1140 {
1141 size_t uiCount = m_aItems.Count();
1142 while ( uiCount-- != 0 ) {
1143 delete m_aItems[uiCount];
f5bb2251 1144 m_aItems[uiCount] = NULL;
e7549107
SC
1145 }
1146
1147 m_aItems.Clear();
1148 }
1149 else
1150#endif // wxUSE_OWNER_DRAWN
1151 if ( HasClientObjectData() )
1152 {
1153 for ( size_t n = 0; n < (size_t)m_noItems; n++ )
1154 {
1155 delete GetClientObject(n);
1156 }
1157 }
e9576ca5
SC
1158}
1159
8614041b
SC
1160void wxListBox::DoSetSize(int x, int y,
1161 int width, int height,
1162 int sizeFlags )
1163{
a8e6bf8a 1164 wxControl::DoSetSize( x , y , width , height , sizeFlags ) ;
8614041b 1165#if TARGET_CARBON
a8e6bf8a 1166 Rect bounds ;
facd6764 1167 GetControlBounds( (ControlRef) m_macControl , &bounds ) ;
962cbf2e 1168 ControlRef control = GetListVerticalScrollBar( (ListHandle)m_macList ) ;
a8e6bf8a
RR
1169 if ( control )
1170 {
1171 Rect scrollbounds ;
1172 GetControlBounds( control , &scrollbounds ) ;
1173 if( scrollbounds.right != bounds.right + 1 )
1174 {
dc0ace7c 1175 UMAMoveControl( control , bounds.right - (scrollbounds.right - scrollbounds.left) + 1 ,
a8e6bf8a
RR
1176 scrollbounds.top ) ;
1177 }
1178 }
8614041b
SC
1179#endif
1180}
e7549107 1181void wxListBox::DoSetFirstItem(int N)
e9576ca5 1182{
a8e6bf8a 1183 MacScrollTo( N ) ;
e9576ca5
SC
1184}
1185
1186void wxListBox::Delete(int N)
1187{
e7549107
SC
1188 wxCHECK_RET( N >= 0 && N < m_noItems,
1189 wxT("invalid index in wxListBox::Delete") );
1190
1191#if wxUSE_OWNER_DRAWN
1192 delete m_aItems[N];
0baac61e 1193 m_aItems.RemoveAt(N);
e7549107
SC
1194#else // !wxUSE_OWNER_DRAWN
1195 if ( HasClientObjectData() )
1196 {
1197 delete GetClientObject(N);
1198 }
1199#endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
ecaf6c18 1200 m_stringArray.RemoveAt( N ) ;
a8e6bf8a
RR
1201 m_dataArray.RemoveAt( N ) ;
1202 m_noItems --;
dc0ace7c 1203
a8e6bf8a 1204 MacDelete( N ) ;
e9576ca5
SC
1205}
1206
e7549107 1207int wxListBox::DoAppend(const wxString& item)
e9576ca5 1208{
a8e6bf8a 1209 int index = m_noItems ;
427ff662
SC
1210 m_stringArray.Add( item ) ;
1211 m_dataArray.Add( NULL );
a8e6bf8a
RR
1212 m_noItems ++;
1213 DoSetItemClientData( index , NULL ) ;
1214 MacAppend( item ) ;
e7549107 1215
a8e6bf8a 1216 return index ;
e9576ca5
SC
1217}
1218
e7549107 1219void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
dc0ace7c 1220{
e40298d5
JS
1221 MacSetRedraw( false ) ;
1222 Clear() ;
1223 int n = choices.GetCount();
1224
1225 for( int i = 0 ; i < n ; ++i )
a8e6bf8a 1226 {
e40298d5
JS
1227 if ( clientData )
1228 {
e7549107
SC
1229#if wxUSE_OWNER_DRAWN
1230 wxASSERT_MSG(clientData[i] == NULL,
e40298d5 1231 wxT("Can't use client data with owner-drawn listboxes"));
e7549107 1232#else // !wxUSE_OWNER_DRAWN
e40298d5
JS
1233 Append( choices[i] , clientData[i] ) ;
1234#endif
1235 }
1236 else
1237 Append( choices[i] ) ;
a8e6bf8a 1238 }
e40298d5 1239
e7549107
SC
1240#if wxUSE_OWNER_DRAWN
1241 if ( m_windowStyle & wxLB_OWNERDRAW ) {
1242 // first delete old items
1243 size_t ui = m_aItems.Count();
1244 while ( ui-- != 0 ) {
1245 delete m_aItems[ui];
f5bb2251 1246 m_aItems[ui] = NULL;
e7549107
SC
1247 }
1248 m_aItems.Empty();
e40298d5 1249
e7549107
SC
1250 // then create new ones
1251 for ( ui = 0; ui < (size_t)m_noItems; ui++ ) {
1252 wxOwnerDrawn *pNewItem = CreateItem(ui);
1253 pNewItem->SetName(choices[ui]);
1254 m_aItems.Add(pNewItem);
1255 }
1256 }
1257#endif // wxUSE_OWNER_DRAWN
e40298d5 1258 MacSetRedraw( true ) ;
e7549107
SC
1259}
1260
1261bool wxListBox::HasMultipleSelection() const
1262{
1263 return (m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED);
e9576ca5
SC
1264}
1265
427ff662 1266int wxListBox::FindString(const wxString& s) const
e9576ca5 1267{
e40298d5 1268
427ff662 1269 if ( s.Right(1) == wxT("*") )
a8e6bf8a
RR
1270 {
1271 wxString search = s.Left( s.Length() - 1 ) ;
1272 int len = search.Length() ;
1273 Str255 s1 , s2 ;
427ff662 1274 wxMacStringToPascal( search , s2 ) ;
e40298d5 1275
a8e6bf8a
RR
1276 for ( int i = 0 ; i < m_noItems ; ++ i )
1277 {
427ff662
SC
1278 wxMacStringToPascal( m_stringArray[i].Left( len ) , s1 ) ;
1279
a8e6bf8a
RR
1280 if ( EqualString( s1 , s2 , false , false ) )
1281 return i ;
1282 }
427ff662 1283 if ( s.Left(1) == wxT("*") && s.Length() > 1 )
a8e6bf8a 1284 {
427ff662
SC
1285 wxString st = s ;
1286 st.MakeLower() ;
a8e6bf8a
RR
1287 for ( int i = 0 ; i < m_noItems ; ++i )
1288 {
427ff662 1289 if ( GetString(i).Lower().Matches(st) )
a8e6bf8a
RR
1290 return i ;
1291 }
dc0ace7c 1292 }
e40298d5 1293
a8e6bf8a
RR
1294 }
1295 else
1296 {
1297 Str255 s1 , s2 ;
e40298d5 1298
427ff662 1299 wxMacStringToPascal( s , s2 ) ;
e40298d5 1300
a8e6bf8a
RR
1301 for ( int i = 0 ; i < m_noItems ; ++ i )
1302 {
427ff662
SC
1303 wxMacStringToPascal( m_stringArray[i] , s1 ) ;
1304
a8e6bf8a
RR
1305 if ( EqualString( s1 , s2 , false , false ) )
1306 return i ;
1307 }
e40298d5
JS
1308 }
1309 return -1;
e9576ca5
SC
1310}
1311
1312void wxListBox::Clear()
1313{
e40298d5
JS
1314 FreeData();
1315 m_noItems = 0;
1316 m_stringArray.Empty() ;
1317 m_dataArray.Empty() ;
1318 MacClear() ;
e9576ca5
SC
1319}
1320
1321void wxListBox::SetSelection(int N, bool select)
1322{
519cb848 1323 wxCHECK_RET( N >= 0 && N < m_noItems,
427ff662 1324 wxT("invalid index in wxListBox::SetSelection") );
e40298d5
JS
1325 MacSetSelection( N , select ) ;
1326 GetSelections( m_selectionPreImage ) ;
e9576ca5
SC
1327}
1328
e7549107 1329bool wxListBox::IsSelected(int N) const
e9576ca5 1330{
519cb848 1331 wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE,
427ff662 1332 wxT("invalid index in wxListBox::Selected") );
e40298d5
JS
1333
1334 return MacIsSelected( N ) ;
e9576ca5
SC
1335}
1336
e7549107 1337void *wxListBox::DoGetItemClientData(int N) const
e9576ca5 1338{
519cb848 1339 wxCHECK_MSG( N >= 0 && N < m_noItems, NULL,
e40298d5
JS
1340 wxT("invalid index in wxListBox::GetClientData"));
1341
e7549107 1342 return (void *)m_dataArray[N];
e9576ca5
SC
1343}
1344
51abe921
SC
1345wxClientData *wxListBox::DoGetItemClientObject(int N) const
1346{
a8e6bf8a 1347 return (wxClientData *) DoGetItemClientData( N ) ;
51abe921
SC
1348}
1349
e7549107 1350void wxListBox::DoSetItemClientData(int N, void *Client_data)
e9576ca5 1351{
519cb848 1352 wxCHECK_RET( N >= 0 && N < m_noItems,
427ff662 1353 wxT("invalid index in wxListBox::SetClientData") );
e40298d5 1354
e7549107
SC
1355#if wxUSE_OWNER_DRAWN
1356 if ( m_windowStyle & wxLB_OWNERDRAW )
1357 {
1358 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
1359 // in OnMeasure/OnDraw.
1360 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
1361 }
1362#endif // wxUSE_OWNER_DRAWN
427ff662 1363 wxASSERT_MSG( m_dataArray.GetCount() >= (size_t) N , wxT("invalid client_data array") ) ;
e40298d5 1364
68a9d9d0 1365 if ( m_dataArray.GetCount() > (size_t) N )
a8e6bf8a
RR
1366 {
1367 m_dataArray[N] = (char*) Client_data ;
2f1ae414 1368 }
8208e181
SC
1369 else
1370 {
a8e6bf8a 1371 m_dataArray.Add( (char*) Client_data ) ;
8208e181 1372 }
e7549107
SC
1373}
1374
1375void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
1376{
1377 DoSetItemClientData(n, clientData);
e9576ca5
SC
1378}
1379
1380// Return number of selections and an array of selected integers
1381int wxListBox::GetSelections(wxArrayInt& aSelections) const
1382{
a8e6bf8a 1383 return MacGetSelections( aSelections ) ;
e9576ca5
SC
1384}
1385
1386// Get single selection, for single choice list items
1387int wxListBox::GetSelection() const
1388{
a8e6bf8a 1389 return MacGetSelection() ;
e9576ca5
SC
1390}
1391
1392// Find string for position
1393wxString wxListBox::GetString(int N) const
1394{
427ff662 1395 return m_stringArray[N] ;
e9576ca5
SC
1396}
1397
e7549107 1398void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
e9576ca5 1399{
e7549107 1400 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
e40298d5
JS
1401 wxT("invalid index in wxListBox::InsertItems") );
1402
e7549107 1403 int nItems = items.GetCount();
e40298d5 1404
a8e6bf8a
RR
1405 for ( int i = 0 ; i < nItems ; i++ )
1406 {
1407 m_stringArray.Insert( items[i] , pos + i ) ;
1408 m_dataArray.Insert( NULL , pos + i ) ;
1409 MacInsert( pos + i , items[i] ) ;
1410 }
e40298d5 1411
519cb848 1412 m_noItems += nItems;
e9576ca5
SC
1413}
1414
1415void wxListBox::SetString(int N, const wxString& s)
1416{
427ff662 1417 m_stringArray[N] = s ;
a8e6bf8a 1418 MacSet( N , s ) ;
e9576ca5
SC
1419}
1420
37e2cb08 1421wxSize wxListBox::DoGetBestSize() const
e9576ca5 1422{
2b5f62a0
VZ
1423 int lbWidth = 100; // some defaults
1424 int lbHeight = 110;
1425 int wLine;
facd6764 1426
e40298d5 1427 {
facd6764 1428 wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetTopLevelWindowRef() ) ) ;
e40298d5 1429
fcb35beb 1430 if ( m_font.Ok() )
e40298d5 1431 {
facd6764
SC
1432 ::TextFont( m_font.MacGetFontNum() ) ;
1433 ::TextSize( m_font.MacGetFontSize() ) ;
1434 ::TextFace( m_font.MacGetFontStyle() ) ;
e40298d5
JS
1435 }
1436 else
1437 {
1438 ::TextFont( kFontIDMonaco ) ;
1439 ::TextSize( 9 );
1440 ::TextFace( 0 ) ;
1441 }
1442
1443 // Find the widest line
1444 for(int i = 0; i < GetCount(); i++) {
1445 wxString str(GetString(i));
2c1a3312
SC
1446 #if wxUSE_UNICODE
1447 Point bounds={0,0} ;
1448 SInt16 baseline ;
a9412f8f 1449 ::GetThemeTextDimensions( wxMacCFStringHolder( str , m_font.GetEncoding() ) ,
2c1a3312
SC
1450 kThemeCurrentPortFont,
1451 kThemeStateActive,
1452 false,
1453 &bounds,
1454 &baseline );
1455 wLine = bounds.h ;
1456 #else
939fba6c 1457 wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ;
2c1a3312 1458 #endif
e40298d5
JS
1459 lbWidth = wxMax(lbWidth, wLine);
1460 }
1461
1462 // Add room for the scrollbar
1463 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
1464
1465 // And just a bit more
1466 int cy = 12 ;
1467 int cx = ::TextWidth( "X" , 0 , 1 ) ;
1468 lbWidth += cx ;
1469
1470 // don't make the listbox too tall (limit height to around 10 items) but don't
1471 // make it too small neither
1472 lbHeight = (cy+4) * wxMin(wxMax(GetCount(), 3), 10);
1473 }
facd6764 1474
2b5f62a0 1475 return wxSize(lbWidth, lbHeight);
e9576ca5
SC
1476}
1477
51abe921
SC
1478int wxListBox::GetCount() const
1479{
1480 return m_noItems;
1481}
1482
60149370
GD
1483void wxListBox::Refresh(bool eraseBack, const wxRect *rect)
1484{
de043984 1485 wxControl::Refresh( eraseBack , rect ) ;
e40298d5 1486 // MacRedrawControl() ;
60149370
GD
1487}
1488
51abe921
SC
1489#if wxUSE_OWNER_DRAWN
1490
1491class wxListBoxItem : public wxOwnerDrawn
1492{
1493public:
1494 wxListBoxItem(const wxString& str = "");
1495};
1496
1497wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE)
1498{
1499 // no bitmaps/checkmarks
1500 SetMarginWidth(0);
1501}
1502
1503wxOwnerDrawn *wxListBox::CreateItem(size_t n)
1504{
1505 return new wxListBoxItem();
1506}
1507
1508#endif //USE_OWNER_DRAWN
e9576ca5 1509
519cb848
SC
1510// ============================================================================
1511// list box control implementation
1512// ============================================================================
1513
2b5f62a0 1514/*
519cb848
SC
1515void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon)
1516{
e40298d5
JS
1517wxListBox* list;
1518// typecast our refCon
1519list = (wxListBox*)refCon;
1520
1521 MoveTo(cellRect->left + 4 , cellRect->top + 10 );
1522 const wxString text = list->m_stringArray[lCell.v] ;
1523 ::TextFont( kFontIDMonaco ) ;
1524 ::TextSize( 9 );
1525 ::TextFace( 0 ) ;
1526 DrawText(text, 0 , text.Length());
1527
1528 }
2b5f62a0 1529*/
519cb848
SC
1530void wxListBox::MacDelete( int N )
1531{
76a5e5d2 1532 LDelRow( 1 , N , (ListHandle)m_macList) ;
60149370 1533 Refresh();
519cb848
SC
1534}
1535
427ff662 1536void wxListBox::MacInsert( int n , const wxString& text)
519cb848 1537{
60149370
GD
1538 Cell cell = { 0 , 0 } ;
1539 cell.v = n ;
76a5e5d2 1540 LAddRow( 1 , cell.v , (ListHandle)m_macList ) ;
e40298d5 1541 // LSetCell(text, strlen(text), cell, m_macList);
60149370 1542 Refresh();
519cb848
SC
1543}
1544
427ff662 1545void wxListBox::MacAppend( const wxString& text)
519cb848 1546{
60149370 1547 Cell cell = { 0 , 0 } ;
76a5e5d2
SC
1548 cell.v = (**(ListHandle)m_macList).dataBounds.bottom ;
1549 LAddRow( 1 , cell.v , (ListHandle)m_macList ) ;
e40298d5 1550 // LSetCell(text, strlen(text), cell, m_macList);
60149370 1551 Refresh();
519cb848
SC
1552}
1553
dc0ace7c 1554void wxListBox::MacClear()
519cb848 1555{
76a5e5d2 1556 LDelRow( (**(ListHandle)m_macList).dataBounds.bottom , 0 ,(ListHandle) m_macList ) ;
60149370 1557 Refresh();
519cb848
SC
1558}
1559
1560void wxListBox::MacSetSelection( int n , bool select )
1561{
a8e6bf8a
RR
1562 Cell cell = { 0 , 0 } ;
1563 if ( ! (m_windowStyle & wxLB_MULTIPLE) )
1564 {
9f081f02
GD
1565 if ( LGetSelect( true , &cell , (ListHandle)m_macList ) )
1566 {
1567 LSetSelect( false , cell , (ListHandle)m_macList ) ;
1568 }
a8e6bf8a 1569 }
e40298d5 1570
a8e6bf8a 1571 cell.v = n ;
76a5e5d2
SC
1572 LSetSelect( select , cell , (ListHandle)m_macList ) ;
1573 LAutoScroll( (ListHandle)m_macList ) ;
a8e6bf8a 1574 Refresh();
519cb848
SC
1575}
1576
1577bool wxListBox::MacIsSelected( int n ) const
1578{
a8e6bf8a
RR
1579 Cell cell = { 0 , 0 } ;
1580 cell.v = n ;
76a5e5d2 1581 return LGetSelect( false , &cell , (ListHandle)m_macList ) ;
519cb848
SC
1582}
1583
519cb848
SC
1584int wxListBox::MacGetSelection() const
1585{
a8e6bf8a 1586 Cell cell = { 0 , 0 } ;
76a5e5d2 1587 if ( LGetSelect( true , &cell , (ListHandle)m_macList ) )
a8e6bf8a
RR
1588 return cell.v ;
1589 else
1590 return -1 ;
519cb848
SC
1591}
1592
1593int wxListBox::MacGetSelections( wxArrayInt& aSelections ) const
1594{
a8e6bf8a 1595 int no_sel = 0 ;
e40298d5 1596
519cb848 1597 aSelections.Empty();
e40298d5 1598
a8e6bf8a
RR
1599 Cell cell = { 0 , 0 } ;
1600 cell.v = 0 ;
e40298d5 1601
76a5e5d2 1602 while ( LGetSelect( true , &cell ,(ListHandle) m_macList ) )
a8e6bf8a
RR
1603 {
1604 aSelections.Add( cell.v ) ;
1605 no_sel++ ;
1606 cell.v++ ;
1607 }
1608 return no_sel ;
519cb848
SC
1609}
1610
427ff662 1611void wxListBox::MacSet( int n , const wxString& text )
519cb848 1612{
a8e6bf8a
RR
1613 // our implementation does not store anything in the list
1614 // so we just have to redraw
1615 Cell cell = { 0 , 0 } ;
1616 cell.v = n ;
e40298d5 1617 // LSetCell(text, strlen(text), cell, m_macList);
a8e6bf8a 1618 Refresh();
519cb848
SC
1619}
1620
1621void wxListBox::MacScrollTo( int n )
1622{
a8e6bf8a 1623 // TODO implement scrolling
519cb848
SC
1624}
1625
864db5de 1626void wxListBox::OnSize( wxSizeEvent &event)
519cb848 1627{
60149370 1628 Point pt;
e40298d5 1629
60149370 1630#if TARGET_CARBON
962cbf2e 1631 GetListCellSize((ListHandle)m_macList, &pt);
60149370 1632#else
76a5e5d2 1633 pt = (**(ListHandle)m_macList).cellSize ;
60149370 1634#endif
facd6764
SC
1635 int w, h ;
1636 GetSize( &w , &h ) ;
1637 pt.h = w - 15 ;
76a5e5d2 1638 LCellSize( pt , (ListHandle)m_macList ) ;
519cb848
SC
1639}
1640
4b26b60f 1641void wxListBox::MacHandleControlClick( WXWidget control , wxInt16 controlpart , bool WXUNUSED(mouseStillDown))
519cb848 1642{
a8e6bf8a
RR
1643 Boolean wasDoubleClick = false ;
1644 long result ;
e40298d5 1645
facd6764 1646 ::GetControlData( (ControlRef) m_macControl , kControlNoPart , kControlListBoxDoubleClickTag , sizeof( wasDoubleClick ) , (char*) &wasDoubleClick , &result ) ;
a8e6bf8a
RR
1647 if ( !wasDoubleClick )
1648 {
1649 MacDoClick() ;
1650 }
1651 else
1652 {
1653 MacDoDoubleClick() ;
1654 }
519cb848
SC
1655}
1656
dc0ace7c 1657void wxListBox::MacSetRedraw( bool doDraw )
519cb848 1658{
76a5e5d2 1659 LSetDrawingMode( doDraw , (ListHandle)m_macList ) ;
e40298d5 1660
519cb848
SC
1661}
1662
1663void wxListBox::MacDoClick()
1664{
a8e6bf8a 1665 wxArrayInt aSelections;
68a9d9d0
SC
1666 int n ;
1667 size_t count = GetSelections(aSelections);
e40298d5 1668
a8e6bf8a
RR
1669 if ( count == m_selectionPreImage.GetCount() )
1670 {
1671 bool hasChanged = false ;
68a9d9d0 1672 for ( size_t i = 0 ; i < count ; ++i )
a8e6bf8a
RR
1673 {
1674 if ( aSelections[i] != m_selectionPreImage[i] )
1675 {
1676 hasChanged = true ;
1677 break ;
1678 }
1679 }
1680 if ( !hasChanged )
1681 {
1682 return ;
1683 }
1684 }
e40298d5 1685
a8e6bf8a 1686 m_selectionPreImage = aSelections;
e40298d5 1687
a8e6bf8a
RR
1688 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
1689 event.SetEventObject( this );
e40298d5 1690
a8e6bf8a
RR
1691 if ( count > 0 )
1692 {
1693 n = aSelections[0];
1694 if ( HasClientObjectData() )
1695 event.SetClientObject( GetClientObject(n) );
1696 else if ( HasClientUntypedData() )
1697 event.SetClientData( GetClientData(n) );
1698 event.SetString( GetString(n) );
1699 }
1700 else
1701 {
e40298d5 1702 n = -1;
a8e6bf8a 1703 }
e40298d5 1704
e7549107 1705 event.m_commandInt = n;
e40298d5 1706
e7549107 1707 GetEventHandler()->ProcessEvent(event);
519cb848
SC
1708}
1709
1710void wxListBox::MacDoDoubleClick()
1711{
1712 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId);
1713 event.SetEventObject( this );
e40298d5 1714 GetEventHandler()->ProcessEvent(event) ;
519cb848 1715}
ecaf6c18 1716
ecaf6c18
SC
1717void wxListBox::OnChar(wxKeyEvent& event)
1718{
eb22f2a6 1719 if ( event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER)
92104223 1720 {
e40298d5
JS
1721 wxWindow* parent = GetParent() ;
1722 while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL )
1723 parent = parent->GetParent() ;
1724
1725 if ( parent && parent->GetDefaultItem() )
1726 {
1727 wxButton *def = wxDynamicCast(parent->GetDefaultItem(),
1728 wxButton);
1729 if ( def && def->IsEnabled() )
1730 {
1731 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1732 event.SetEventObject(def);
1733 def->Command(event);
1734 return ;
1735 }
1736 }
1737 event.Skip() ;
92104223
SC
1738 }
1739 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
eb22f2a6 1740 else if (event.GetKeyCode() == WXK_ESCAPE || (event.GetKeyCode() == '.' && event.MetaDown() ) )
92104223 1741 {
44553322 1742 // FIXME: look in ancestors, not just parent.
e40298d5 1743 wxWindow* win = GetParent()->FindWindow( wxID_CANCEL ) ;
44553322
JS
1744 if (win)
1745 {
1746 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1747 new_event.SetEventObject( win );
1748 win->GetEventHandler()->ProcessEvent( new_event );
1749 }
92104223 1750 }
eb22f2a6 1751 else if ( event.GetKeyCode() == WXK_TAB )
92104223
SC
1752 {
1753 wxNavigationKeyEvent new_event;
1754 new_event.SetEventObject( this );
1755 new_event.SetDirection( !event.ShiftDown() );
1756 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1757 new_event.SetWindowChange( event.ControlDown() );
1758 new_event.SetCurrentFocus( this );
1759 if ( !GetEventHandler()->ProcessEvent( new_event ) )
e40298d5 1760 event.Skip() ;
92104223 1761 }
e40298d5
JS
1762 else if ( event.GetKeyCode() == WXK_DOWN || event.GetKeyCode() == WXK_UP )
1763 {
1764 // perform the default key handling first
1765 wxControl::OnKeyDown( event ) ;
1766
ecaf6c18
SC
1767 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
1768 event.SetEventObject( this );
e40298d5 1769
ecaf6c18
SC
1770 wxArrayInt aSelections;
1771 int n, count = GetSelections(aSelections);
1772 if ( count > 0 )
1773 {
e40298d5
JS
1774 n = aSelections[0];
1775 if ( HasClientObjectData() )
1776 event.SetClientObject( GetClientObject(n) );
1777 else if ( HasClientUntypedData() )
1778 event.SetClientData( GetClientData(n) );
1779 event.SetString( GetString(n) );
ecaf6c18
SC
1780 }
1781 else
1782 {
e40298d5 1783 n = -1;
ecaf6c18 1784 }
e40298d5 1785
ecaf6c18 1786 event.m_commandInt = n;
e40298d5 1787
ecaf6c18 1788 GetEventHandler()->ProcessEvent(event);
e40298d5
JS
1789 }
1790 else
1791 {
1792 if ( event.GetTimestamp() > m_lastTypeIn + 60 )
1793 {
427ff662 1794 m_typeIn = wxEmptyString ;
e40298d5
JS
1795 }
1796 m_lastTypeIn = event.GetTimestamp() ;
1797 m_typeIn += (char) event.GetKeyCode() ;
427ff662 1798 int line = FindString(wxT("*")+m_typeIn+wxT("*")) ;
e40298d5
JS
1799 if ( line >= 0 )
1800 {
1801 if ( GetSelection() != line )
1802 {
1803 SetSelection(line) ;
1804 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
1805 event.SetEventObject( this );
1806
1807 if ( HasClientObjectData() )
1808 event.SetClientObject( GetClientObject( line ) );
1809 else if ( HasClientUntypedData() )
1810 event.SetClientData( GetClientData(line) );
1811 event.SetString( GetString(line) );
1812
1813 event.m_commandInt = line ;
1814
1815 GetEventHandler()->ProcessEvent(event);
1816 }
1817 }
1818 }
ecaf6c18
SC
1819}
1820
facd6764 1821#endif