]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/listbox.cpp
fixing right click
[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
facd6764
SC
717void wxListBox::MacSetRedraw( bool doDraw )
718{
719 // nothing to do in compositing mode
720}
721
722void wxListBox::MacDoClick()
83ce5634 723{/*
facd6764
SC
724 wxArrayInt aSelections;
725 int n ;
726 size_t count = GetSelections(aSelections);
727
728 if ( count == m_selectionPreImage.GetCount() )
729 {
730 bool hasChanged = false ;
731 for ( size_t i = 0 ; i < count ; ++i )
732 {
733 if ( aSelections[i] != m_selectionPreImage[i] )
734 {
735 hasChanged = true ;
736 break ;
737 }
738 }
739 if ( !hasChanged )
740 {
741 return ;
742 }
743 }
744
745 m_selectionPreImage = aSelections;
746
747 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
748 event.SetEventObject( this );
749
750 if ( count > 0 )
751 {
752 n = aSelections[0];
753 if ( HasClientObjectData() )
754 event.SetClientObject( GetClientObject(n) );
755 else if ( HasClientUntypedData() )
756 event.SetClientData( GetClientData(n) );
757 event.SetString( GetString(n) );
758 }
759 else
760 {
761 n = -1;
762 }
763
764 event.m_commandInt = n;
765
766 GetEventHandler()->ProcessEvent(event);
83ce5634 767*/
facd6764
SC
768}
769
770void wxListBox::MacDoDoubleClick()
771{
83ce5634 772/*
facd6764
SC
773 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId);
774 event.SetEventObject( this );
775 GetEventHandler()->ProcessEvent(event) ;
83ce5634 776*/
facd6764
SC
777}
778
779void wxListBox::OnChar(wxKeyEvent& event)
780{
781 // todo trigger proper events here
782 event.Skip() ;
783 return ;
784
785 if ( event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER)
786 {
787 wxWindow* parent = GetParent() ;
788 while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL )
789 parent = parent->GetParent() ;
790
791 if ( parent && parent->GetDefaultItem() )
792 {
793 wxButton *def = wxDynamicCast(parent->GetDefaultItem(),
794 wxButton);
795 if ( def && def->IsEnabled() )
796 {
797 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
798 event.SetEventObject(def);
799 def->Command(event);
800 return ;
801 }
802 }
803 event.Skip() ;
804 }
805 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
806 else if (event.GetKeyCode() == WXK_ESCAPE || (event.GetKeyCode() == '.' && event.MetaDown() ) )
807 {
808 // FIXME: look in ancestors, not just parent.
809 wxWindow* win = GetParent()->FindWindow( wxID_CANCEL ) ;
810 if (win)
811 {
812 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
813 new_event.SetEventObject( win );
814 win->GetEventHandler()->ProcessEvent( new_event );
815 }
816 }
817 else if ( event.GetKeyCode() == WXK_TAB )
818 {
819 wxNavigationKeyEvent new_event;
820 new_event.SetEventObject( this );
821 new_event.SetDirection( !event.ShiftDown() );
822 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
823 new_event.SetWindowChange( event.ControlDown() );
824 new_event.SetCurrentFocus( this );
825 if ( !GetEventHandler()->ProcessEvent( new_event ) )
826 event.Skip() ;
827 }
828 else if ( event.GetKeyCode() == WXK_DOWN || event.GetKeyCode() == WXK_UP )
829 {
830 // perform the default key handling first
831 wxControl::OnKeyDown( event ) ;
832
833 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
834 event.SetEventObject( this );
835
836 wxArrayInt aSelections;
837 int n, count = GetSelections(aSelections);
838 if ( count > 0 )
839 {
840 n = aSelections[0];
841 if ( HasClientObjectData() )
842 event.SetClientObject( GetClientObject(n) );
843 else if ( HasClientUntypedData() )
844 event.SetClientData( GetClientData(n) );
845 event.SetString( GetString(n) );
846 }
847 else
848 {
849 n = -1;
850 }
851
852 event.m_commandInt = n;
853
854 GetEventHandler()->ProcessEvent(event);
855 }
856 else
857 {
858 if ( event.GetTimestamp() > m_lastTypeIn + 60 )
859 {
860 m_typeIn = wxEmptyString ;
861 }
862 m_lastTypeIn = event.GetTimestamp() ;
863 m_typeIn += (char) event.GetKeyCode() ;
864 int line = FindString(wxT("*")+m_typeIn+wxT("*")) ;
865 if ( line >= 0 )
866 {
867 if ( GetSelection() != line )
868 {
869 SetSelection(line) ;
870 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
871 event.SetEventObject( this );
872
873 if ( HasClientObjectData() )
874 event.SetClientObject( GetClientObject( line ) );
875 else if ( HasClientUntypedData() )
876 event.SetClientData( GetClientData(line) );
877 event.SetString( GetString(line) );
878
879 event.m_commandInt = line ;
880
881 GetEventHandler()->ProcessEvent(event);
882 }
883 }
884 }
885}
573ac9dc 886
202848fe 887#else
facd6764
SC
888
889// old carbon version
890
891const short kwxMacListItemHeight = 19 ;
202848fe 892
e42e45a9
SC
893extern "C"
894{
895static pascal void wxMacListDefinition( short message, Boolean isSelected, Rect *drawRect,
a8e6bf8a
RR
896 Cell cell, short dataOffset, short dataLength,
897 ListHandle listHandle ) ;
e42e45a9
SC
898}
899
facd6764 900static pascal void wxMacListDefinition( short message, Boolean isSelected, Rect *d,
a8e6bf8a
RR
901 Cell cell, short dataOffset, short dataLength,
902 ListHandle listHandle )
903{
21956470 904 wxListBox* list;
facd6764
SC
905 Rect r = *d ;
906 Rect* drawRect = &r ;
907
908 list = (wxListBox*) GetControlReference( (ControlRef) GetListRefCon(listHandle) );
909 if ( list == NULL || list->GetHandle() == NULL || GetControlReference( ( ControlRef )list->GetHandle() ) == NULL )
21956470
SC
910 return ;
911
facd6764
SC
912 // the bounds passed in are not correct, adjust the right border
913 int x = 0 , y = 0 ;
914 Rect bounds ;
915 GetControlBounds( (ControlRef) list->GetHandle() , &bounds ) ;
916 r.right = r.left + (bounds.right - bounds.left ) - 16 ;
917
a8e6bf8a
RR
918 GrafPtr savePort;
919 GrafPtr grafPtr;
920 RgnHandle savedClipRegion;
921 SInt32 savedPenMode;
e40298d5
JS
922 GetPort(&savePort);
923 SetPort((**listHandle).port);
924 grafPtr = (**listHandle).port ;
a8e6bf8a 925 // typecast our refCon
e40298d5 926
a8e6bf8a 927 // Calculate the cell rect.
e40298d5 928
a8e6bf8a 929 switch( message ) {
e40298d5
JS
930 case lInitMsg:
931 break;
932
933 case lCloseMsg:
934 break;
935
936 case lDrawMsg:
a8e6bf8a 937 {
427ff662 938 const wxString linetext = list->m_stringArray[cell.v] ;
e40298d5 939
a8e6bf8a
RR
940 // Save the current clip region, and set the clip region to the area we are about
941 // to draw.
e40298d5 942
a8e6bf8a
RR
943 savedClipRegion = NewRgn();
944 GetClip( savedClipRegion );
facd6764 945
a8e6bf8a
RR
946 ClipRect( drawRect );
947 EraseRect( drawRect );
e40298d5 948
fcb35beb
VZ
949 const wxFont& font = list->GetFont();
950 if ( font.Ok() )
e40298d5 951 {
facd6764
SC
952 ::TextFont( font.MacGetFontNum() ) ;
953 ::TextSize( font.MacGetFontSize() ) ;
954 ::TextFace( font.MacGetFontStyle() ) ;
e40298d5
JS
955 }
956 else
957 {
958 ::TextFont( kFontIDMonaco ) ;
959 ::TextSize( 9 );
960 ::TextFace( 0 ) ;
961 }
962
427ff662
SC
963 {
964 Rect frame = { drawRect->top, drawRect->left + 4,
965 drawRect->top + kwxMacListItemHeight, drawRect->right + 10000 } ;
a9412f8f 966 CFMutableStringRef mString = CFStringCreateMutableCopy( NULL , 0 , wxMacCFStringHolder(linetext , list->GetFont().GetEncoding()) ) ;
427ff662 967 ::TruncateThemeText( mString , kThemeCurrentPortFont, kThemeStateActive, drawRect->right - drawRect->left , truncEnd , NULL ) ;
facd6764 968
427ff662
SC
969 ::DrawThemeTextBox( mString,
970 kThemeCurrentPortFont,
971 kThemeStateActive,
972 false,
973 &frame,
974 teJustLeft,
975 nil );
facd6764 976
427ff662
SC
977 CFRelease( mString ) ;
978 }
facd6764 979
a8e6bf8a
RR
980 // If the cell is hilited, do the hilite now. Paint the cell contents with the
981 // appropriate QuickDraw transform mode.
e40298d5 982
a8e6bf8a 983 if( isSelected ) {
76a5e5d2
SC
984 savedPenMode = GetPortPenMode( (CGrafPtr) grafPtr );
985 SetPortPenMode( (CGrafPtr)grafPtr, hilitetransfermode );
a8e6bf8a 986 PaintRect( drawRect );
76a5e5d2 987 SetPortPenMode( (CGrafPtr)grafPtr, savedPenMode );
a8e6bf8a 988 }
e40298d5 989
a8e6bf8a 990 // Restore the saved clip region.
e40298d5 991
a8e6bf8a
RR
992 SetClip( savedClipRegion );
993 DisposeRgn( savedClipRegion );
e40298d5
JS
994 }
995 break;
996 case lHiliteMsg:
997
998 // Hilite or unhilite the cell. Paint the cell contents with the
999 // appropriate QuickDraw transform mode.
1000
1001 GetPort( &grafPtr );
1002 savedPenMode = GetPortPenMode( (CGrafPtr)grafPtr );
1003 SetPortPenMode( (CGrafPtr)grafPtr, hilitetransfermode );
1004 PaintRect( drawRect );
1005 SetPortPenMode( (CGrafPtr)grafPtr, savedPenMode );
1006 break;
1007 default :
1008 break ;
a8e6bf8a 1009 }
dc0ace7c 1010 SetPort(savePort);
e42e45a9
SC
1011}
1012
519cb848 1013extern "C" void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon) ;
f81127c5 1014// resources ldef ids
519cb848 1015const short kwxMacListWithVerticalScrollbar = 128 ;
f81127c5 1016const short kwxMacListWithVerticalAndHorizontalScrollbar = 129 ;
519cb848 1017
e9576ca5
SC
1018// ============================================================================
1019// list box control implementation
1020// ============================================================================
1021
1022// Listbox item
1023wxListBox::wxListBox()
1024{
1025 m_noItems = 0;
1026 m_selected = 0;
2f1ae414 1027 m_macList = NULL ;
e9576ca5
SC
1028}
1029
e42e45a9
SC
1030static ListDefUPP macListDefUPP = NULL ;
1031
584ad2a3
MB
1032bool wxListBox::Create(wxWindow *parent, wxWindowID id,
1033 const wxPoint& pos,
1034 const wxSize& size,
1035 const wxArrayString& choices,
1036 long style,
1037 const wxValidator& validator,
1038 const wxString& name)
1039{
1040 wxCArrayString chs(choices);
1041
1042 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
1043 style, validator, name);
1044}
1045
e9576ca5
SC
1046bool wxListBox::Create(wxWindow *parent, wxWindowID id,
1047 const wxPoint& pos,
1048 const wxSize& size,
1049 int n, const wxString choices[],
1050 long style,
1051 const wxValidator& validator,
1052 const wxString& name)
1053{
facd6764
SC
1054 m_macIsUserPane = FALSE ;
1055
b657d5db 1056 if ( !wxListBoxBase::Create(parent, id, pos, size, style & ~(wxHSCROLL|wxVSCROLL), validator, name) )
b45ed7a2
VZ
1057 return false;
1058
60149370
GD
1059 m_noItems = 0 ; // this will be increased by our append command
1060 m_selected = 0;
dc0ace7c 1061
facd6764 1062 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
e40298d5 1063
60149370 1064 ListDefSpec listDef;
e42e45a9
SC
1065 listDef.defType = kListDefUserProcType;
1066 if ( macListDefUPP == NULL )
1067 {
e40298d5 1068 macListDefUPP = NewListDefUPP( wxMacListDefinition );
e42e45a9 1069 }
e40298d5
JS
1070 listDef.u.userProc = macListDefUPP ;
1071
2b5f62a0
VZ
1072 Str255 fontName ;
1073 SInt16 fontSize ;
1074 Style fontStyle ;
facd6764 1075
e40298d5 1076 GetThemeFont(kThemeViewsFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
facd6764 1077
427ff662 1078 SetFont( wxFont (fontSize, wxSWISS, wxNORMAL, wxNORMAL , false , wxMacMakeStringFromPascal( fontName ) ) ) ;
facd6764 1079
60149370 1080 Size asize;
519cb848 1081
519cb848 1082
facd6764 1083 CreateListBoxControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds, false, 0, 1, (style & wxLB_HSCROLL), true,
962cbf2e 1084 kwxMacListItemHeight, kwxMacListItemHeight, false, &listDef, (ControlRef *)&m_macControl );
519cb848 1085
facd6764 1086 GetControlData( (ControlRef) m_macControl, kControlNoPart, kControlListBoxListHandleTag,
60149370 1087 sizeof(ListHandle), (Ptr) &m_macList, &asize);
519cb848 1088
facd6764 1089 SetControlReference( (ControlRef) m_macControl, (long) this);
60149370 1090
dc0ace7c 1091
e42e45a9 1092 OptionBits options = 0;
60149370
GD
1093 if ( style & wxLB_MULTIPLE )
1094 {
09322209 1095 options += lExtendDrag + lUseSense ;
60149370
GD
1096 }
1097 else if ( style & wxLB_EXTENDED )
1098 {
09322209 1099 // default behaviour
60149370
GD
1100 }
1101 else
1102 {
2b5f62a0 1103 options = (OptionBits) lOnlyOne ;
60149370 1104 }
76a5e5d2 1105 SetListSelectionFlags((ListHandle)m_macList, options);
dc0ace7c 1106
60149370
GD
1107 for ( int i = 0 ; i < n ; i++ )
1108 {
a8e6bf8a 1109 Append( choices[i] ) ;
60149370 1110 }
dc0ace7c 1111
facd6764 1112 MacPostControlCreate(pos,size) ;
2b5f62a0 1113
76a5e5d2 1114 LSetDrawingMode( true , (ListHandle)m_macList ) ;
519cb848 1115
60149370 1116 return TRUE;
e9576ca5
SC
1117}
1118
1119wxListBox::~wxListBox()
1120{
facd6764 1121 SetControlReference( (ControlRef) m_macControl , NULL ) ;
4b651a46 1122 FreeData() ;
21956470 1123 // avoid access during destruction
a8e6bf8a
RR
1124 if ( m_macList )
1125 {
a8e6bf8a
RR
1126 m_macList = NULL ;
1127 }
e9576ca5
SC
1128}
1129
4b651a46 1130void wxListBox::FreeData()
e9576ca5 1131{
e7549107
SC
1132#if wxUSE_OWNER_DRAWN
1133 if ( m_windowStyle & wxLB_OWNERDRAW )
1134 {
1135 size_t uiCount = m_aItems.Count();
1136 while ( uiCount-- != 0 ) {
1137 delete m_aItems[uiCount];
f5bb2251 1138 m_aItems[uiCount] = NULL;
e7549107
SC
1139 }
1140
1141 m_aItems.Clear();
1142 }
1143 else
1144#endif // wxUSE_OWNER_DRAWN
1145 if ( HasClientObjectData() )
1146 {
1147 for ( size_t n = 0; n < (size_t)m_noItems; n++ )
1148 {
1149 delete GetClientObject(n);
1150 }
1151 }
e9576ca5
SC
1152}
1153
8614041b
SC
1154void wxListBox::DoSetSize(int x, int y,
1155 int width, int height,
1156 int sizeFlags )
1157{
a8e6bf8a 1158 wxControl::DoSetSize( x , y , width , height , sizeFlags ) ;
8614041b 1159#if TARGET_CARBON
a8e6bf8a 1160 Rect bounds ;
facd6764 1161 GetControlBounds( (ControlRef) m_macControl , &bounds ) ;
962cbf2e 1162 ControlRef control = GetListVerticalScrollBar( (ListHandle)m_macList ) ;
a8e6bf8a
RR
1163 if ( control )
1164 {
1165 Rect scrollbounds ;
1166 GetControlBounds( control , &scrollbounds ) ;
1167 if( scrollbounds.right != bounds.right + 1 )
1168 {
dc0ace7c 1169 UMAMoveControl( control , bounds.right - (scrollbounds.right - scrollbounds.left) + 1 ,
a8e6bf8a
RR
1170 scrollbounds.top ) ;
1171 }
1172 }
8614041b
SC
1173#endif
1174}
e7549107 1175void wxListBox::DoSetFirstItem(int N)
e9576ca5 1176{
a8e6bf8a 1177 MacScrollTo( N ) ;
e9576ca5
SC
1178}
1179
1180void wxListBox::Delete(int N)
1181{
e7549107
SC
1182 wxCHECK_RET( N >= 0 && N < m_noItems,
1183 wxT("invalid index in wxListBox::Delete") );
1184
1185#if wxUSE_OWNER_DRAWN
1186 delete m_aItems[N];
0baac61e 1187 m_aItems.RemoveAt(N);
e7549107
SC
1188#else // !wxUSE_OWNER_DRAWN
1189 if ( HasClientObjectData() )
1190 {
1191 delete GetClientObject(N);
1192 }
1193#endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
ecaf6c18 1194 m_stringArray.RemoveAt( N ) ;
a8e6bf8a
RR
1195 m_dataArray.RemoveAt( N ) ;
1196 m_noItems --;
dc0ace7c 1197
a8e6bf8a 1198 MacDelete( N ) ;
e9576ca5
SC
1199}
1200
e7549107 1201int wxListBox::DoAppend(const wxString& item)
e9576ca5 1202{
a8e6bf8a 1203 int index = m_noItems ;
427ff662
SC
1204 m_stringArray.Add( item ) ;
1205 m_dataArray.Add( NULL );
a8e6bf8a
RR
1206 m_noItems ++;
1207 DoSetItemClientData( index , NULL ) ;
1208 MacAppend( item ) ;
e7549107 1209
a8e6bf8a 1210 return index ;
e9576ca5
SC
1211}
1212
e7549107 1213void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
dc0ace7c 1214{
e40298d5
JS
1215 MacSetRedraw( false ) ;
1216 Clear() ;
1217 int n = choices.GetCount();
1218
1219 for( int i = 0 ; i < n ; ++i )
a8e6bf8a 1220 {
e40298d5
JS
1221 if ( clientData )
1222 {
e7549107
SC
1223#if wxUSE_OWNER_DRAWN
1224 wxASSERT_MSG(clientData[i] == NULL,
e40298d5 1225 wxT("Can't use client data with owner-drawn listboxes"));
e7549107 1226#else // !wxUSE_OWNER_DRAWN
e40298d5
JS
1227 Append( choices[i] , clientData[i] ) ;
1228#endif
1229 }
1230 else
1231 Append( choices[i] ) ;
a8e6bf8a 1232 }
e40298d5 1233
e7549107
SC
1234#if wxUSE_OWNER_DRAWN
1235 if ( m_windowStyle & wxLB_OWNERDRAW ) {
1236 // first delete old items
1237 size_t ui = m_aItems.Count();
1238 while ( ui-- != 0 ) {
1239 delete m_aItems[ui];
f5bb2251 1240 m_aItems[ui] = NULL;
e7549107
SC
1241 }
1242 m_aItems.Empty();
e40298d5 1243
e7549107
SC
1244 // then create new ones
1245 for ( ui = 0; ui < (size_t)m_noItems; ui++ ) {
1246 wxOwnerDrawn *pNewItem = CreateItem(ui);
1247 pNewItem->SetName(choices[ui]);
1248 m_aItems.Add(pNewItem);
1249 }
1250 }
1251#endif // wxUSE_OWNER_DRAWN
e40298d5 1252 MacSetRedraw( true ) ;
e7549107
SC
1253}
1254
427ff662 1255int wxListBox::FindString(const wxString& s) const
e9576ca5 1256{
e40298d5 1257
427ff662 1258 if ( s.Right(1) == wxT("*") )
a8e6bf8a
RR
1259 {
1260 wxString search = s.Left( s.Length() - 1 ) ;
1261 int len = search.Length() ;
1262 Str255 s1 , s2 ;
427ff662 1263 wxMacStringToPascal( search , s2 ) ;
e40298d5 1264
a8e6bf8a
RR
1265 for ( int i = 0 ; i < m_noItems ; ++ i )
1266 {
427ff662
SC
1267 wxMacStringToPascal( m_stringArray[i].Left( len ) , s1 ) ;
1268
a8e6bf8a
RR
1269 if ( EqualString( s1 , s2 , false , false ) )
1270 return i ;
1271 }
427ff662 1272 if ( s.Left(1) == wxT("*") && s.Length() > 1 )
a8e6bf8a 1273 {
427ff662
SC
1274 wxString st = s ;
1275 st.MakeLower() ;
a8e6bf8a
RR
1276 for ( int i = 0 ; i < m_noItems ; ++i )
1277 {
427ff662 1278 if ( GetString(i).Lower().Matches(st) )
a8e6bf8a
RR
1279 return i ;
1280 }
dc0ace7c 1281 }
e40298d5 1282
a8e6bf8a
RR
1283 }
1284 else
1285 {
1286 Str255 s1 , s2 ;
e40298d5 1287
427ff662 1288 wxMacStringToPascal( s , s2 ) ;
e40298d5 1289
a8e6bf8a
RR
1290 for ( int i = 0 ; i < m_noItems ; ++ i )
1291 {
427ff662
SC
1292 wxMacStringToPascal( m_stringArray[i] , s1 ) ;
1293
a8e6bf8a
RR
1294 if ( EqualString( s1 , s2 , false , false ) )
1295 return i ;
1296 }
e40298d5
JS
1297 }
1298 return -1;
e9576ca5
SC
1299}
1300
1301void wxListBox::Clear()
1302{
e40298d5
JS
1303 FreeData();
1304 m_noItems = 0;
1305 m_stringArray.Empty() ;
1306 m_dataArray.Empty() ;
1307 MacClear() ;
e9576ca5
SC
1308}
1309
1310void wxListBox::SetSelection(int N, bool select)
1311{
519cb848 1312 wxCHECK_RET( N >= 0 && N < m_noItems,
427ff662 1313 wxT("invalid index in wxListBox::SetSelection") );
e40298d5
JS
1314 MacSetSelection( N , select ) ;
1315 GetSelections( m_selectionPreImage ) ;
e9576ca5
SC
1316}
1317
e7549107 1318bool wxListBox::IsSelected(int N) const
e9576ca5 1319{
519cb848 1320 wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE,
427ff662 1321 wxT("invalid index in wxListBox::Selected") );
e40298d5
JS
1322
1323 return MacIsSelected( N ) ;
e9576ca5
SC
1324}
1325
e7549107 1326void *wxListBox::DoGetItemClientData(int N) const
e9576ca5 1327{
519cb848 1328 wxCHECK_MSG( N >= 0 && N < m_noItems, NULL,
e40298d5
JS
1329 wxT("invalid index in wxListBox::GetClientData"));
1330
e7549107 1331 return (void *)m_dataArray[N];
e9576ca5
SC
1332}
1333
51abe921
SC
1334wxClientData *wxListBox::DoGetItemClientObject(int N) const
1335{
a8e6bf8a 1336 return (wxClientData *) DoGetItemClientData( N ) ;
51abe921
SC
1337}
1338
e7549107 1339void wxListBox::DoSetItemClientData(int N, void *Client_data)
e9576ca5 1340{
519cb848 1341 wxCHECK_RET( N >= 0 && N < m_noItems,
427ff662 1342 wxT("invalid index in wxListBox::SetClientData") );
e40298d5 1343
e7549107
SC
1344#if wxUSE_OWNER_DRAWN
1345 if ( m_windowStyle & wxLB_OWNERDRAW )
1346 {
1347 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
1348 // in OnMeasure/OnDraw.
1349 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
1350 }
1351#endif // wxUSE_OWNER_DRAWN
427ff662 1352 wxASSERT_MSG( m_dataArray.GetCount() >= (size_t) N , wxT("invalid client_data array") ) ;
e40298d5 1353
68a9d9d0 1354 if ( m_dataArray.GetCount() > (size_t) N )
a8e6bf8a
RR
1355 {
1356 m_dataArray[N] = (char*) Client_data ;
2f1ae414 1357 }
8208e181
SC
1358 else
1359 {
a8e6bf8a 1360 m_dataArray.Add( (char*) Client_data ) ;
8208e181 1361 }
e7549107
SC
1362}
1363
1364void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
1365{
1366 DoSetItemClientData(n, clientData);
e9576ca5
SC
1367}
1368
1369// Return number of selections and an array of selected integers
1370int wxListBox::GetSelections(wxArrayInt& aSelections) const
1371{
a8e6bf8a 1372 return MacGetSelections( aSelections ) ;
e9576ca5
SC
1373}
1374
1375// Get single selection, for single choice list items
1376int wxListBox::GetSelection() const
1377{
a8e6bf8a 1378 return MacGetSelection() ;
e9576ca5
SC
1379}
1380
1381// Find string for position
1382wxString wxListBox::GetString(int N) const
1383{
427ff662 1384 return m_stringArray[N] ;
e9576ca5
SC
1385}
1386
e7549107 1387void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
e9576ca5 1388{
e7549107 1389 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
e40298d5
JS
1390 wxT("invalid index in wxListBox::InsertItems") );
1391
e7549107 1392 int nItems = items.GetCount();
e40298d5 1393
a8e6bf8a
RR
1394 for ( int i = 0 ; i < nItems ; i++ )
1395 {
1396 m_stringArray.Insert( items[i] , pos + i ) ;
1397 m_dataArray.Insert( NULL , pos + i ) ;
1398 MacInsert( pos + i , items[i] ) ;
1399 }
e40298d5 1400
519cb848 1401 m_noItems += nItems;
e9576ca5
SC
1402}
1403
1404void wxListBox::SetString(int N, const wxString& s)
1405{
427ff662 1406 m_stringArray[N] = s ;
a8e6bf8a 1407 MacSet( N , s ) ;
e9576ca5
SC
1408}
1409
37e2cb08 1410wxSize wxListBox::DoGetBestSize() const
e9576ca5 1411{
2b5f62a0
VZ
1412 int lbWidth = 100; // some defaults
1413 int lbHeight = 110;
1414 int wLine;
facd6764 1415
e40298d5 1416 {
facd6764 1417 wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetTopLevelWindowRef() ) ) ;
e40298d5 1418
fcb35beb 1419 if ( m_font.Ok() )
e40298d5 1420 {
facd6764
SC
1421 ::TextFont( m_font.MacGetFontNum() ) ;
1422 ::TextSize( m_font.MacGetFontSize() ) ;
1423 ::TextFace( m_font.MacGetFontStyle() ) ;
e40298d5
JS
1424 }
1425 else
1426 {
1427 ::TextFont( kFontIDMonaco ) ;
1428 ::TextSize( 9 );
1429 ::TextFace( 0 ) ;
1430 }
1431
1432 // Find the widest line
1433 for(int i = 0; i < GetCount(); i++) {
1434 wxString str(GetString(i));
2c1a3312
SC
1435 #if wxUSE_UNICODE
1436 Point bounds={0,0} ;
1437 SInt16 baseline ;
a9412f8f 1438 ::GetThemeTextDimensions( wxMacCFStringHolder( str , m_font.GetEncoding() ) ,
2c1a3312
SC
1439 kThemeCurrentPortFont,
1440 kThemeStateActive,
1441 false,
1442 &bounds,
1443 &baseline );
1444 wLine = bounds.h ;
1445 #else
939fba6c 1446 wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ;
2c1a3312 1447 #endif
e40298d5
JS
1448 lbWidth = wxMax(lbWidth, wLine);
1449 }
1450
1451 // Add room for the scrollbar
1452 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
1453
1454 // And just a bit more
1455 int cy = 12 ;
1456 int cx = ::TextWidth( "X" , 0 , 1 ) ;
1457 lbWidth += cx ;
1458
1459 // don't make the listbox too tall (limit height to around 10 items) but don't
1460 // make it too small neither
1461 lbHeight = (cy+4) * wxMin(wxMax(GetCount(), 3), 10);
1462 }
facd6764 1463
2b5f62a0 1464 return wxSize(lbWidth, lbHeight);
e9576ca5
SC
1465}
1466
51abe921
SC
1467int wxListBox::GetCount() const
1468{
1469 return m_noItems;
1470}
1471
60149370
GD
1472void wxListBox::Refresh(bool eraseBack, const wxRect *rect)
1473{
de043984 1474 wxControl::Refresh( eraseBack , rect ) ;
e40298d5 1475 // MacRedrawControl() ;
60149370
GD
1476}
1477
51abe921
SC
1478#if wxUSE_OWNER_DRAWN
1479
1480class wxListBoxItem : public wxOwnerDrawn
1481{
1482public:
1483 wxListBoxItem(const wxString& str = "");
1484};
1485
1486wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE)
1487{
1488 // no bitmaps/checkmarks
1489 SetMarginWidth(0);
1490}
1491
1492wxOwnerDrawn *wxListBox::CreateItem(size_t n)
1493{
1494 return new wxListBoxItem();
1495}
1496
1497#endif //USE_OWNER_DRAWN
e9576ca5 1498
519cb848
SC
1499// ============================================================================
1500// list box control implementation
1501// ============================================================================
1502
2b5f62a0 1503/*
519cb848
SC
1504void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon)
1505{
e40298d5
JS
1506wxListBox* list;
1507// typecast our refCon
1508list = (wxListBox*)refCon;
1509
1510 MoveTo(cellRect->left + 4 , cellRect->top + 10 );
1511 const wxString text = list->m_stringArray[lCell.v] ;
1512 ::TextFont( kFontIDMonaco ) ;
1513 ::TextSize( 9 );
1514 ::TextFace( 0 ) ;
1515 DrawText(text, 0 , text.Length());
1516
1517 }
2b5f62a0 1518*/
519cb848
SC
1519void wxListBox::MacDelete( int N )
1520{
76a5e5d2 1521 LDelRow( 1 , N , (ListHandle)m_macList) ;
60149370 1522 Refresh();
519cb848
SC
1523}
1524
427ff662 1525void wxListBox::MacInsert( int n , const wxString& text)
519cb848 1526{
60149370
GD
1527 Cell cell = { 0 , 0 } ;
1528 cell.v = n ;
76a5e5d2 1529 LAddRow( 1 , cell.v , (ListHandle)m_macList ) ;
e40298d5 1530 // LSetCell(text, strlen(text), cell, m_macList);
60149370 1531 Refresh();
519cb848
SC
1532}
1533
427ff662 1534void wxListBox::MacAppend( const wxString& text)
519cb848 1535{
60149370 1536 Cell cell = { 0 , 0 } ;
76a5e5d2
SC
1537 cell.v = (**(ListHandle)m_macList).dataBounds.bottom ;
1538 LAddRow( 1 , cell.v , (ListHandle)m_macList ) ;
e40298d5 1539 // LSetCell(text, strlen(text), cell, m_macList);
60149370 1540 Refresh();
519cb848
SC
1541}
1542
dc0ace7c 1543void wxListBox::MacClear()
519cb848 1544{
76a5e5d2 1545 LDelRow( (**(ListHandle)m_macList).dataBounds.bottom , 0 ,(ListHandle) m_macList ) ;
60149370 1546 Refresh();
519cb848
SC
1547}
1548
1549void wxListBox::MacSetSelection( int n , bool select )
1550{
a8e6bf8a
RR
1551 Cell cell = { 0 , 0 } ;
1552 if ( ! (m_windowStyle & wxLB_MULTIPLE) )
1553 {
9f081f02
GD
1554 if ( LGetSelect( true , &cell , (ListHandle)m_macList ) )
1555 {
1556 LSetSelect( false , cell , (ListHandle)m_macList ) ;
1557 }
a8e6bf8a 1558 }
e40298d5 1559
a8e6bf8a 1560 cell.v = n ;
76a5e5d2
SC
1561 LSetSelect( select , cell , (ListHandle)m_macList ) ;
1562 LAutoScroll( (ListHandle)m_macList ) ;
a8e6bf8a 1563 Refresh();
519cb848
SC
1564}
1565
1566bool wxListBox::MacIsSelected( int n ) const
1567{
a8e6bf8a
RR
1568 Cell cell = { 0 , 0 } ;
1569 cell.v = n ;
76a5e5d2 1570 return LGetSelect( false , &cell , (ListHandle)m_macList ) ;
519cb848
SC
1571}
1572
519cb848
SC
1573int wxListBox::MacGetSelection() const
1574{
a8e6bf8a 1575 Cell cell = { 0 , 0 } ;
76a5e5d2 1576 if ( LGetSelect( true , &cell , (ListHandle)m_macList ) )
a8e6bf8a
RR
1577 return cell.v ;
1578 else
1579 return -1 ;
519cb848
SC
1580}
1581
1582int wxListBox::MacGetSelections( wxArrayInt& aSelections ) const
1583{
a8e6bf8a 1584 int no_sel = 0 ;
e40298d5 1585
519cb848 1586 aSelections.Empty();
e40298d5 1587
a8e6bf8a
RR
1588 Cell cell = { 0 , 0 } ;
1589 cell.v = 0 ;
e40298d5 1590
76a5e5d2 1591 while ( LGetSelect( true , &cell ,(ListHandle) m_macList ) )
a8e6bf8a
RR
1592 {
1593 aSelections.Add( cell.v ) ;
1594 no_sel++ ;
1595 cell.v++ ;
1596 }
1597 return no_sel ;
519cb848
SC
1598}
1599
427ff662 1600void wxListBox::MacSet( int n , const wxString& text )
519cb848 1601{
a8e6bf8a
RR
1602 // our implementation does not store anything in the list
1603 // so we just have to redraw
1604 Cell cell = { 0 , 0 } ;
1605 cell.v = n ;
e40298d5 1606 // LSetCell(text, strlen(text), cell, m_macList);
a8e6bf8a 1607 Refresh();
519cb848
SC
1608}
1609
1610void wxListBox::MacScrollTo( int n )
1611{
a8e6bf8a 1612 // TODO implement scrolling
519cb848
SC
1613}
1614
864db5de 1615void wxListBox::OnSize( wxSizeEvent &event)
519cb848 1616{
60149370 1617 Point pt;
e40298d5 1618
60149370 1619#if TARGET_CARBON
962cbf2e 1620 GetListCellSize((ListHandle)m_macList, &pt);
60149370 1621#else
76a5e5d2 1622 pt = (**(ListHandle)m_macList).cellSize ;
60149370 1623#endif
facd6764
SC
1624 int w, h ;
1625 GetSize( &w , &h ) ;
1626 pt.h = w - 15 ;
76a5e5d2 1627 LCellSize( pt , (ListHandle)m_macList ) ;
519cb848
SC
1628}
1629
4b26b60f 1630void wxListBox::MacHandleControlClick( WXWidget control , wxInt16 controlpart , bool WXUNUSED(mouseStillDown))
519cb848 1631{
a8e6bf8a
RR
1632 Boolean wasDoubleClick = false ;
1633 long result ;
e40298d5 1634
facd6764 1635 ::GetControlData( (ControlRef) m_macControl , kControlNoPart , kControlListBoxDoubleClickTag , sizeof( wasDoubleClick ) , (char*) &wasDoubleClick , &result ) ;
a8e6bf8a
RR
1636 if ( !wasDoubleClick )
1637 {
1638 MacDoClick() ;
1639 }
1640 else
1641 {
1642 MacDoDoubleClick() ;
1643 }
519cb848
SC
1644}
1645
dc0ace7c 1646void wxListBox::MacSetRedraw( bool doDraw )
519cb848 1647{
76a5e5d2 1648 LSetDrawingMode( doDraw , (ListHandle)m_macList ) ;
e40298d5 1649
519cb848
SC
1650}
1651
1652void wxListBox::MacDoClick()
1653{
a8e6bf8a 1654 wxArrayInt aSelections;
68a9d9d0
SC
1655 int n ;
1656 size_t count = GetSelections(aSelections);
e40298d5 1657
a8e6bf8a
RR
1658 if ( count == m_selectionPreImage.GetCount() )
1659 {
1660 bool hasChanged = false ;
68a9d9d0 1661 for ( size_t i = 0 ; i < count ; ++i )
a8e6bf8a
RR
1662 {
1663 if ( aSelections[i] != m_selectionPreImage[i] )
1664 {
1665 hasChanged = true ;
1666 break ;
1667 }
1668 }
1669 if ( !hasChanged )
1670 {
1671 return ;
1672 }
1673 }
e40298d5 1674
a8e6bf8a 1675 m_selectionPreImage = aSelections;
e40298d5 1676
a8e6bf8a
RR
1677 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
1678 event.SetEventObject( this );
e40298d5 1679
a8e6bf8a
RR
1680 if ( count > 0 )
1681 {
1682 n = aSelections[0];
1683 if ( HasClientObjectData() )
1684 event.SetClientObject( GetClientObject(n) );
1685 else if ( HasClientUntypedData() )
1686 event.SetClientData( GetClientData(n) );
1687 event.SetString( GetString(n) );
1688 }
1689 else
1690 {
e40298d5 1691 n = -1;
a8e6bf8a 1692 }
e40298d5 1693
e7549107 1694 event.m_commandInt = n;
e40298d5 1695
e7549107 1696 GetEventHandler()->ProcessEvent(event);
519cb848
SC
1697}
1698
1699void wxListBox::MacDoDoubleClick()
1700{
1701 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId);
1702 event.SetEventObject( this );
e40298d5 1703 GetEventHandler()->ProcessEvent(event) ;
519cb848 1704}
ecaf6c18 1705
ecaf6c18
SC
1706void wxListBox::OnChar(wxKeyEvent& event)
1707{
eb22f2a6 1708 if ( event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER)
92104223 1709 {
e40298d5
JS
1710 wxWindow* parent = GetParent() ;
1711 while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL )
1712 parent = parent->GetParent() ;
1713
1714 if ( parent && parent->GetDefaultItem() )
1715 {
1716 wxButton *def = wxDynamicCast(parent->GetDefaultItem(),
1717 wxButton);
1718 if ( def && def->IsEnabled() )
1719 {
1720 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1721 event.SetEventObject(def);
1722 def->Command(event);
1723 return ;
1724 }
1725 }
1726 event.Skip() ;
92104223
SC
1727 }
1728 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
eb22f2a6 1729 else if (event.GetKeyCode() == WXK_ESCAPE || (event.GetKeyCode() == '.' && event.MetaDown() ) )
92104223 1730 {
44553322 1731 // FIXME: look in ancestors, not just parent.
e40298d5 1732 wxWindow* win = GetParent()->FindWindow( wxID_CANCEL ) ;
44553322
JS
1733 if (win)
1734 {
1735 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1736 new_event.SetEventObject( win );
1737 win->GetEventHandler()->ProcessEvent( new_event );
1738 }
92104223 1739 }
eb22f2a6 1740 else if ( event.GetKeyCode() == WXK_TAB )
92104223
SC
1741 {
1742 wxNavigationKeyEvent new_event;
1743 new_event.SetEventObject( this );
1744 new_event.SetDirection( !event.ShiftDown() );
1745 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1746 new_event.SetWindowChange( event.ControlDown() );
1747 new_event.SetCurrentFocus( this );
1748 if ( !GetEventHandler()->ProcessEvent( new_event ) )
e40298d5 1749 event.Skip() ;
92104223 1750 }
e40298d5
JS
1751 else if ( event.GetKeyCode() == WXK_DOWN || event.GetKeyCode() == WXK_UP )
1752 {
1753 // perform the default key handling first
1754 wxControl::OnKeyDown( event ) ;
1755
ecaf6c18
SC
1756 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
1757 event.SetEventObject( this );
e40298d5 1758
ecaf6c18
SC
1759 wxArrayInt aSelections;
1760 int n, count = GetSelections(aSelections);
1761 if ( count > 0 )
1762 {
e40298d5
JS
1763 n = aSelections[0];
1764 if ( HasClientObjectData() )
1765 event.SetClientObject( GetClientObject(n) );
1766 else if ( HasClientUntypedData() )
1767 event.SetClientData( GetClientData(n) );
1768 event.SetString( GetString(n) );
ecaf6c18
SC
1769 }
1770 else
1771 {
e40298d5 1772 n = -1;
ecaf6c18 1773 }
e40298d5 1774
ecaf6c18 1775 event.m_commandInt = n;
e40298d5 1776
ecaf6c18 1777 GetEventHandler()->ProcessEvent(event);
e40298d5
JS
1778 }
1779 else
1780 {
1781 if ( event.GetTimestamp() > m_lastTypeIn + 60 )
1782 {
427ff662 1783 m_typeIn = wxEmptyString ;
e40298d5
JS
1784 }
1785 m_lastTypeIn = event.GetTimestamp() ;
1786 m_typeIn += (char) event.GetKeyCode() ;
427ff662 1787 int line = FindString(wxT("*")+m_typeIn+wxT("*")) ;
e40298d5
JS
1788 if ( line >= 0 )
1789 {
1790 if ( GetSelection() != line )
1791 {
1792 SetSelection(line) ;
1793 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
1794 event.SetEventObject( this );
1795
1796 if ( HasClientObjectData() )
1797 event.SetClientObject( GetClientObject( line ) );
1798 else if ( HasClientUntypedData() )
1799 event.SetClientData( GetClientData(line) );
1800 event.SetString( GetString(line) );
1801
1802 event.m_commandInt = line ;
1803
1804 GetEventHandler()->ProcessEvent(event);
1805 }
1806 }
1807 }
ecaf6c18
SC
1808}
1809
facd6764 1810#endif