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