]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/listbox.cpp
If we have multiple filters in a Save dialog, don't use
[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
65571936 9// Licence: wxWindows licence
e9576ca5
SC
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)
36ba5f7c 30#if !__WXMAC_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
a9fc5eec
SC
39const short kTextColumnId = 1024 ;
40
facd6764
SC
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 {
469d8d5d 76 wxListBox* list = wxDynamicCast( (wxObject*) ref , wxListBox ) ;
83ce5634
SC
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 );
be1aa07f
SC
109 wxPostEvent( list->GetEventHandler() , event ) ;
110 // direct notification is not always having the listbox GetSelection() having in synch with event
111 // list->GetEventHandler()->ProcessEvent(event) ;
83ce5634
SC
112 }
113 break ;
114 }
115 }
116}
117
118
facd6764
SC
119static pascal OSStatus ListBoxGetSetItemData(ControlRef browser,
120 DataBrowserItemID itemID, DataBrowserPropertyID property,
121 DataBrowserItemDataRef itemData, Boolean changeValue)
122{
123 OSStatus err = errDataBrowserPropertyNotSupported;
124
125 if ( ! changeValue )
126 {
127 switch (property)
128 {
129
a9fc5eec 130 case kTextColumnId:
facd6764
SC
131 {
132 long ref = GetControlReference( browser ) ;
133 if ( ref )
134 {
469d8d5d 135 wxListBox* list = wxDynamicCast( (wxObject*) ref , wxListBox ) ;
facd6764
SC
136 for ( size_t i = 0 ; i < list->m_idArray.GetCount() ; ++i )
137 if ( list->m_idArray[i] == (long) itemID )
138 {
139 wxMacCFStringHolder cf( list->GetString(i) , list->GetFont().GetEncoding() ) ;
140 verify_noerr( ::SetDataBrowserItemDataText( itemData , cf ) ) ;
141 err = noErr ;
142 break ;
143 }
144 }
145 }
146 break;
147
148 default:
149
150 break;
151 }
152 }
153
154 return err;
155}
156bool wxListBox::Create(wxWindow *parent, wxWindowID id,
157 const wxPoint& pos,
158 const wxSize& size,
159 int n, const wxString choices[],
160 long style,
161 const wxValidator& validator,
162 const wxString& name)
163{
164 m_macIsUserPane = FALSE ;
5e6f42cd
SC
165
166 wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED),
167 _T("only one of listbox selection modes can be specified") );
facd6764
SC
168
169 if ( !wxListBoxBase::Create(parent, id, pos, size, style & ~(wxHSCROLL|wxVSCROLL), validator, name) )
170 return false;
171
172 m_noItems = 0 ; // this will be increased by our append command
173 m_selected = 0;
174 m_nextId = 1 ;
175
176
177 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
facd6764 178
21fd5529
SC
179 m_peer = new wxMacControl() ;
180 verify_noerr( ::CreateDataBrowserControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds, kDataBrowserListView , *m_peer ) );
facd6764
SC
181
182 DataBrowserSelectionFlags options = kDataBrowserDragSelect ;
183 if ( style & wxLB_MULTIPLE )
184 {
185 options += kDataBrowserAlwaysExtendSelection + kDataBrowserCmdTogglesSelection ;
186 }
187 else if ( style & wxLB_EXTENDED )
188 {
189 // default behaviour
190 }
191 else
192 {
193 options += kDataBrowserSelectOnlyOne ;
194 }
21fd5529 195 verify_noerr(SetDataBrowserSelectionFlags (*m_peer, options ) );
facd6764
SC
196
197 DataBrowserListViewColumnDesc columnDesc ;
198 columnDesc.headerBtnDesc.titleOffset = 0;
199 columnDesc.headerBtnDesc.version = kDataBrowserListViewLatestHeaderDesc;
200
201 columnDesc.headerBtnDesc.btnFontStyle.flags =
202 kControlUseFontMask | kControlUseJustMask;
203
204 columnDesc.headerBtnDesc.btnContentInfo.contentType = kControlNoContent;
205 columnDesc.propertyDesc.propertyType = kDataBrowserTextType;
206 columnDesc.headerBtnDesc.btnFontStyle.just = teFlushDefault;
207 columnDesc.headerBtnDesc.minimumWidth = 0;
208 columnDesc.headerBtnDesc.maximumWidth = 10000;
209
210 columnDesc.headerBtnDesc.btnFontStyle.font = kControlFontViewSystemFont;
211 columnDesc.headerBtnDesc.btnFontStyle.style = normal;
212 columnDesc.headerBtnDesc.titleString = NULL ; // CFSTR( "" );
213
a9fc5eec 214 columnDesc.propertyDesc.propertyID = kTextColumnId;
facd6764 215 columnDesc.propertyDesc.propertyType = kDataBrowserTextType;
c2697b87 216 columnDesc.propertyDesc.propertyFlags =
9bd2d050 217#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
c2697b87
SC
218 kDataBrowserListViewTypeSelectColumn |
219#endif
220 kDataBrowserTableViewSelectionColumn ;
facd6764
SC
221
222
21fd5529
SC
223 verify_noerr(::AddDataBrowserListViewColumn(*m_peer, &columnDesc, kDataBrowserListViewAppendColumn) ) ;
224 verify_noerr(::AutoSizeDataBrowserListViewColumns( *m_peer ) ) ;
225 verify_noerr(::SetDataBrowserHasScrollBars( *m_peer , false , true ) ) ;
226 verify_noerr(::SetDataBrowserTableViewHiliteStyle( *m_peer, kDataBrowserTableViewFillHilite ) ) ;
227 verify_noerr(::SetDataBrowserListViewHeaderBtnHeight( *m_peer , 0 ) ) ;
facd6764
SC
228 DataBrowserCallbacks callbacks ;
229
230 callbacks.version = kDataBrowserLatestCallbacks;
231
232 InitDataBrowserCallbacks(&callbacks);
233
234 callbacks.u.v1.itemDataCallback =
235 NewDataBrowserItemDataUPP(ListBoxGetSetItemData);
236
83ce5634
SC
237 callbacks.u.v1.itemNotificationCallback =
238#if TARGET_API_MAC_OSX
239 (DataBrowserItemNotificationUPP) NewDataBrowserItemNotificationWithItemUPP(DataBrowserItemNotificationProc) ;
240#else
241 NewDataBrowserItemNotificationUPP(DataBrowserItemNotificationProc) ;
242#endif
21fd5529 243 SetDataBrowserCallbacks(*m_peer, &callbacks);
facd6764
SC
244
245 MacPostControlCreate(pos,size) ;
246
247 for ( int i = 0 ; i < n ; i++ )
248 {
249 Append( choices[i] ) ;
250 }
251
d3b5db4b
RD
252 SetBestSize(size); // Needed because it is a wxControlWithItems
253
facd6764
SC
254 return TRUE;
255}
256
257wxListBox::~wxListBox()
258{
21fd5529 259 SetControlReference( *m_peer , NULL ) ;
facd6764
SC
260 FreeData() ;
261 // avoid access during destruction
262 if ( m_macList )
263 {
264 m_macList = NULL ;
265 }
266}
267
268void wxListBox::FreeData()
269{
270#if wxUSE_OWNER_DRAWN
271 if ( m_windowStyle & wxLB_OWNERDRAW )
272 {
273 size_t uiCount = m_aItems.Count();
274 while ( uiCount-- != 0 ) {
275 delete m_aItems[uiCount];
276 m_aItems[uiCount] = NULL;
277 }
278
279 m_aItems.Clear();
280 }
281 else
282#endif // wxUSE_OWNER_DRAWN
283 if ( HasClientObjectData() )
284 {
285 for ( size_t n = 0; n < (size_t)m_noItems; n++ )
286 {
287 delete GetClientObject(n);
288 }
289 }
290}
291
292void wxListBox::DoSetSize(int x, int y,
293 int width, int height,
294 int sizeFlags )
295{
296 wxControl::DoSetSize( x , y , width , height , sizeFlags ) ;
297}
298
299void wxListBox::DoSetFirstItem(int N)
300{
301 MacScrollTo( N ) ;
302}
303
304void wxListBox::Delete(int N)
305{
306 wxCHECK_RET( N >= 0 && N < m_noItems,
307 wxT("invalid index in wxListBox::Delete") );
308
309#if wxUSE_OWNER_DRAWN
310 delete m_aItems[N];
311 m_aItems.RemoveAt(N);
312#else // !wxUSE_OWNER_DRAWN
313 if ( HasClientObjectData() )
314 {
315 delete GetClientObject(N);
316 }
317#endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
318 m_stringArray.RemoveAt( N ) ;
319 m_dataArray.RemoveAt( N ) ;
320 m_noItems --;
321
322 MacDelete( N ) ;
323}
324
325int wxListBox::DoAppend(const wxString& item)
326{
327 int index = m_noItems ;
328 m_stringArray.Add( item ) ;
329 m_dataArray.Add( NULL );
330 m_noItems ++;
331 DoSetItemClientData( index , NULL ) ;
332 MacAppend( item ) ;
333
334 return index ;
335}
336
337void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
338{
339 Clear() ;
340 int n = choices.GetCount();
341
342 for( int i = 0 ; i < n ; ++i )
343 {
344 if ( clientData )
345 {
346#if wxUSE_OWNER_DRAWN
347 wxASSERT_MSG(clientData[i] == NULL,
348 wxT("Can't use client data with owner-drawn listboxes"));
349#else // !wxUSE_OWNER_DRAWN
350 Append( choices[i] , clientData[i] ) ;
351#endif
352 }
353 else
354 Append( choices[i] ) ;
355 }
356
357#if wxUSE_OWNER_DRAWN
358 if ( m_windowStyle & wxLB_OWNERDRAW ) {
359 // first delete old items
360 size_t ui = m_aItems.Count();
361 while ( ui-- != 0 ) {
362 delete m_aItems[ui];
363 m_aItems[ui] = NULL;
364 }
365 m_aItems.Empty();
366
367 // then create new ones
368 for ( ui = 0; ui < (size_t)m_noItems; ui++ ) {
369 wxOwnerDrawn *pNewItem = CreateItem(ui);
370 pNewItem->SetName(choices[ui]);
371 m_aItems.Add(pNewItem);
372 }
373 }
374#endif // wxUSE_OWNER_DRAWN
375}
376
facd6764
SC
377int wxListBox::FindString(const wxString& s) const
378{
379
380 if ( s.Right(1) == wxT("*") )
381 {
382 wxString search = s.Left( s.Length() - 1 ) ;
383 int len = search.Length() ;
384 Str255 s1 , s2 ;
385 wxMacStringToPascal( search , s2 ) ;
386
387 for ( int i = 0 ; i < m_noItems ; ++ i )
388 {
389 wxMacStringToPascal( m_stringArray[i].Left( len ) , s1 ) ;
390
391 if ( EqualString( s1 , s2 , false , false ) )
392 return i ;
393 }
394 if ( s.Left(1) == wxT("*") && s.Length() > 1 )
395 {
396 wxString st = s ;
397 st.MakeLower() ;
398 for ( int i = 0 ; i < m_noItems ; ++i )
399 {
400 if ( GetString(i).Lower().Matches(st) )
401 return i ;
402 }
403 }
404
405 }
406 else
407 {
408 Str255 s1 , s2 ;
409
410 wxMacStringToPascal( s , s2 ) ;
411
412 for ( int i = 0 ; i < m_noItems ; ++ i )
413 {
414 wxMacStringToPascal( m_stringArray[i] , s1 ) ;
415
416 if ( EqualString( s1 , s2 , false , false ) )
417 return i ;
418 }
419 }
420 return -1;
421}
422
423void wxListBox::Clear()
424{
425 FreeData();
426 m_noItems = 0;
427 m_stringArray.Empty() ;
428 m_dataArray.Empty() ;
429 MacClear() ;
430}
431
432void wxListBox::SetSelection(int N, bool select)
433{
434 wxCHECK_RET( N >= 0 && N < m_noItems,
435 wxT("invalid index in wxListBox::SetSelection") );
436 MacSetSelection( N , select ) ;
437 GetSelections( m_selectionPreImage ) ;
438}
439
440bool wxListBox::IsSelected(int N) const
441{
442 wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE,
443 wxT("invalid index in wxListBox::Selected") );
444
445 return MacIsSelected( N ) ;
446}
447
448void *wxListBox::DoGetItemClientData(int N) const
449{
450 wxCHECK_MSG( N >= 0 && N < m_noItems, NULL,
451 wxT("invalid index in wxListBox::GetClientData"));
452
453 return (void *)m_dataArray[N];
454}
455
456wxClientData *wxListBox::DoGetItemClientObject(int N) const
457{
458 return (wxClientData *) DoGetItemClientData( N ) ;
459}
460
461void wxListBox::DoSetItemClientData(int N, void *Client_data)
462{
463 wxCHECK_RET( N >= 0 && N < m_noItems,
464 wxT("invalid index in wxListBox::SetClientData") );
465
466#if wxUSE_OWNER_DRAWN
467 if ( m_windowStyle & wxLB_OWNERDRAW )
468 {
469 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
470 // in OnMeasure/OnDraw.
471 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
472 }
473#endif // wxUSE_OWNER_DRAWN
474 wxASSERT_MSG( m_dataArray.GetCount() >= (size_t) N , wxT("invalid client_data array") ) ;
475
476 if ( m_dataArray.GetCount() > (size_t) N )
477 {
478 m_dataArray[N] = (char*) Client_data ;
479 }
480 else
481 {
482 m_dataArray.Add( (char*) Client_data ) ;
483 }
484}
485
486void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
487{
488 DoSetItemClientData(n, clientData);
489}
490
491// Return number of selections and an array of selected integers
492int wxListBox::GetSelections(wxArrayInt& aSelections) const
493{
494 return MacGetSelections( aSelections ) ;
495}
496
497// Get single selection, for single choice list items
498int wxListBox::GetSelection() const
499{
500 return MacGetSelection() ;
501}
502
503// Find string for position
504wxString wxListBox::GetString(int N) const
505{
506 return m_stringArray[N] ;
507}
508
509void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
510{
511 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
512 wxT("invalid index in wxListBox::InsertItems") );
513
514 int nItems = items.GetCount();
515
516 for ( int i = 0 ; i < nItems ; i++ )
517 {
518 m_stringArray.Insert( items[i] , pos + i ) ;
519 m_dataArray.Insert( NULL , pos + i ) ;
520 MacInsert( pos + i , items[i] ) ;
521 }
522
523 m_noItems += nItems;
524}
525
526void wxListBox::SetString(int N, const wxString& s)
527{
528 m_stringArray[N] = s ;
529 MacSet( N , s ) ;
530}
531
532wxSize wxListBox::DoGetBestSize() const
533{
534 int lbWidth = 100; // some defaults
535 int lbHeight = 110;
536 int wLine;
537
538 {
539 wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetTopLevelWindowRef() ) ) ;
540
541 if ( m_font.Ok() )
542 {
543 ::TextFont( m_font.MacGetFontNum() ) ;
544 ::TextSize( m_font.MacGetFontSize() ) ;
545 ::TextFace( m_font.MacGetFontStyle() ) ;
546 }
547 else
548 {
549 ::TextFont( kFontIDMonaco ) ;
550 ::TextSize( 9 );
551 ::TextFace( 0 ) ;
552 }
553
554 // Find the widest line
555 for(int i = 0; i < GetCount(); i++) {
556 wxString str(GetString(i));
557 #if wxUSE_UNICODE
558 Point bounds={0,0} ;
559 SInt16 baseline ;
560 ::GetThemeTextDimensions( wxMacCFStringHolder( str , m_font.GetEncoding() ) ,
561 kThemeCurrentPortFont,
562 kThemeStateActive,
563 false,
564 &bounds,
565 &baseline );
566 wLine = bounds.h ;
567 #else
568 wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ;
569 #endif
570 lbWidth = wxMax(lbWidth, wLine);
571 }
572
573 // Add room for the scrollbar
574 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
575
576 // And just a bit more
577 int cy = 12 ;
578 int cx = ::TextWidth( "X" , 0 , 1 ) ;
579 lbWidth += cx ;
580
581 // don't make the listbox too tall (limit height to around 10 items) but don't
582 // make it too small neither
583 lbHeight = (cy+4) * wxMin(wxMax(GetCount(), 3), 10);
584 }
585
586 return wxSize(lbWidth, lbHeight);
587}
588
589int wxListBox::GetCount() const
590{
591 return m_noItems;
592}
593
594void wxListBox::Refresh(bool eraseBack, const wxRect *rect)
595{
596 wxControl::Refresh( eraseBack , rect ) ;
597 // MacRedrawControl() ;
598}
599
600#if wxUSE_OWNER_DRAWN
601
602class wxListBoxItem : public wxOwnerDrawn
603{
604public:
605 wxListBoxItem(const wxString& str = "");
606};
607
608wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE)
609{
610 // no bitmaps/checkmarks
611 SetMarginWidth(0);
612}
613
614wxOwnerDrawn *wxListBox::CreateItem(size_t n)
615{
616 return new wxListBoxItem();
617}
618
619#endif //USE_OWNER_DRAWN
620
621// ============================================================================
622// list box control implementation
623// ============================================================================
624
625void wxListBox::MacDelete( int N )
626{
627 UInt32 id = m_idArray[N] ;
21fd5529 628 verify_noerr(::RemoveDataBrowserItems(*m_peer , kDataBrowserNoItem , 1 , (UInt32*) &id , kDataBrowserItemNoProperty ) ) ;
facd6764
SC
629 m_idArray.RemoveAt( N ) ;
630}
631
632void wxListBox::MacInsert( int n , const wxString& text)
633{
21fd5529 634 verify_noerr(::AddDataBrowserItems( *m_peer , kDataBrowserNoItem , 1 , (UInt32*) &m_nextId , kDataBrowserItemNoProperty ) ) ;
facd6764
SC
635 m_idArray.Insert( m_nextId , n ) ;
636 ++m_nextId ;
637}
638
639void wxListBox::MacAppend( const wxString& text)
640{
21fd5529 641 verify_noerr(::AddDataBrowserItems( *m_peer , kDataBrowserNoItem , 1 , (UInt32*) &m_nextId , kDataBrowserItemNoProperty ) ) ;
facd6764
SC
642 m_idArray.Add( m_nextId ) ;
643 ++m_nextId ;
644}
645
646void wxListBox::MacClear()
647{
21fd5529 648 verify_noerr(::RemoveDataBrowserItems(*m_peer , kDataBrowserNoItem , 0 , NULL , kDataBrowserItemNoProperty ) ) ;
83ce5634 649 m_idArray.Empty() ;
facd6764
SC
650}
651
652void wxListBox::MacSetSelection( int n , bool select )
653{
654 UInt32 id = m_idArray[n] ;
5e6f42cd
SC
655 if ( !(GetWindowStyle() & (wxLB_MULTIPLE|wxLB_EXTENDED) ) )
656 {
657 int n = MacGetSelection() ;
658 if ( n >= 0 )
659 {
660 UInt32 idOld = m_idArray[n] ;
21fd5529 661 SetDataBrowserSelectedItems(*m_peer , 1 , & idOld , kDataBrowserItemsRemove ) ;
5e6f42cd
SC
662 }
663 }
21fd5529 664 if ( ::IsDataBrowserItemSelected( *m_peer , id ) != select )
facd6764 665 {
21fd5529 666 verify_noerr(::SetDataBrowserSelectedItems(*m_peer , 1 , & id , kDataBrowserItemsToggle ) ) ;
facd6764
SC
667 }
668 MacScrollTo( n ) ;
669}
670
671bool wxListBox::MacIsSelected( int n ) const
672{
21fd5529 673 return ::IsDataBrowserItemSelected( *m_peer , m_idArray[n] ) ;
facd6764
SC
674}
675
676int wxListBox::MacGetSelection() const
677{
678 for ( size_t i = 0 ; i < m_idArray.GetCount() ; ++i )
679 {
21fd5529 680 if ( ::IsDataBrowserItemSelected(*m_peer , m_idArray[i] ) )
facd6764
SC
681 {
682 return i ;
683 }
684 }
685 return -1 ;
686}
687
688int wxListBox::MacGetSelections( wxArrayInt& aSelections ) const
689{
690 int no_sel = 0 ;
691
692 aSelections.Empty();
693 for ( size_t i = 0 ; i < m_idArray.GetCount() ; ++i )
694 {
21fd5529 695 if ( ::IsDataBrowserItemSelected(*m_peer , m_idArray[i] ) )
facd6764
SC
696 {
697 aSelections.Add( i ) ;
698 no_sel++ ;
699 }
700 }
701 return no_sel ;
702}
519cb848 703
facd6764
SC
704void wxListBox::MacSet( int n , const wxString& text )
705{
706 // as we don't store the strings we only have to issue a redraw
707 UInt32 id = m_idArray[n] ;
21fd5529 708 verify_noerr( ::UpdateDataBrowserItems( *m_peer , kDataBrowserNoItem , 1 , &id , kDataBrowserItemNoProperty , kDataBrowserItemNoProperty ) ) ;
facd6764 709}
e42e45a9 710
facd6764
SC
711void wxListBox::MacScrollTo( int n )
712{
a9fc5eec 713 UInt32 id = m_idArray[n] ;
21fd5529 714 verify_noerr( ::RevealDataBrowserItem(*m_peer , id , kTextColumnId , kDataBrowserRevealWithoutSelecting ) ) ;
facd6764
SC
715}
716
5ecae0b7 717#if !TARGET_API_MAC_OSX
facd6764
SC
718void wxListBox::OnSize( wxSizeEvent &event)
719{
720}
5ecae0b7 721#endif
facd6764 722
facd6764
SC
723void wxListBox::MacSetRedraw( bool doDraw )
724{
725 // nothing to do in compositing mode
726}
727
728void wxListBox::MacDoClick()
83ce5634 729{/*
facd6764
SC
730 wxArrayInt aSelections;
731 int n ;
732 size_t count = GetSelections(aSelections);
733
734 if ( count == m_selectionPreImage.GetCount() )
735 {
736 bool hasChanged = false ;
737 for ( size_t i = 0 ; i < count ; ++i )
738 {
739 if ( aSelections[i] != m_selectionPreImage[i] )
740 {
741 hasChanged = true ;
742 break ;
743 }
744 }
745 if ( !hasChanged )
746 {
747 return ;
748 }
749 }
750
751 m_selectionPreImage = aSelections;
752
753 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
754 event.SetEventObject( this );
755
756 if ( count > 0 )
757 {
758 n = aSelections[0];
759 if ( HasClientObjectData() )
760 event.SetClientObject( GetClientObject(n) );
761 else if ( HasClientUntypedData() )
762 event.SetClientData( GetClientData(n) );
763 event.SetString( GetString(n) );
764 }
765 else
766 {
767 n = -1;
768 }
769
770 event.m_commandInt = n;
771
772 GetEventHandler()->ProcessEvent(event);
83ce5634 773*/
facd6764
SC
774}
775
776void wxListBox::MacDoDoubleClick()
777{
83ce5634 778/*
facd6764
SC
779 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId);
780 event.SetEventObject( this );
781 GetEventHandler()->ProcessEvent(event) ;
83ce5634 782*/
facd6764
SC
783}
784
5ecae0b7
SC
785#if !TARGET_API_MAC_OSX
786
facd6764
SC
787void wxListBox::OnChar(wxKeyEvent& event)
788{
789 // todo trigger proper events here
790 event.Skip() ;
791 return ;
792
793 if ( event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER)
794 {
795 wxWindow* parent = GetParent() ;
796 while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL )
797 parent = parent->GetParent() ;
798
799 if ( parent && parent->GetDefaultItem() )
800 {
801 wxButton *def = wxDynamicCast(parent->GetDefaultItem(),
802 wxButton);
803 if ( def && def->IsEnabled() )
804 {
805 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
806 event.SetEventObject(def);
807 def->Command(event);
808 return ;
809 }
810 }
811 event.Skip() ;
812 }
813 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
814 else if (event.GetKeyCode() == WXK_ESCAPE || (event.GetKeyCode() == '.' && event.MetaDown() ) )
815 {
816 // FIXME: look in ancestors, not just parent.
817 wxWindow* win = GetParent()->FindWindow( wxID_CANCEL ) ;
818 if (win)
819 {
820 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
821 new_event.SetEventObject( win );
822 win->GetEventHandler()->ProcessEvent( new_event );
823 }
824 }
825 else if ( event.GetKeyCode() == WXK_TAB )
826 {
827 wxNavigationKeyEvent new_event;
828 new_event.SetEventObject( this );
829 new_event.SetDirection( !event.ShiftDown() );
830 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
831 new_event.SetWindowChange( event.ControlDown() );
832 new_event.SetCurrentFocus( this );
833 if ( !GetEventHandler()->ProcessEvent( new_event ) )
834 event.Skip() ;
835 }
836 else if ( event.GetKeyCode() == WXK_DOWN || event.GetKeyCode() == WXK_UP )
837 {
838 // perform the default key handling first
839 wxControl::OnKeyDown( event ) ;
840
841 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
842 event.SetEventObject( this );
843
844 wxArrayInt aSelections;
845 int n, count = GetSelections(aSelections);
846 if ( count > 0 )
847 {
848 n = aSelections[0];
849 if ( HasClientObjectData() )
850 event.SetClientObject( GetClientObject(n) );
851 else if ( HasClientUntypedData() )
852 event.SetClientData( GetClientData(n) );
853 event.SetString( GetString(n) );
854 }
855 else
856 {
857 n = -1;
858 }
859
860 event.m_commandInt = n;
861
862 GetEventHandler()->ProcessEvent(event);
863 }
864 else
865 {
866 if ( event.GetTimestamp() > m_lastTypeIn + 60 )
867 {
868 m_typeIn = wxEmptyString ;
869 }
870 m_lastTypeIn = event.GetTimestamp() ;
871 m_typeIn += (char) event.GetKeyCode() ;
872 int line = FindString(wxT("*")+m_typeIn+wxT("*")) ;
873 if ( line >= 0 )
874 {
875 if ( GetSelection() != line )
876 {
877 SetSelection(line) ;
878 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
879 event.SetEventObject( this );
880
881 if ( HasClientObjectData() )
882 event.SetClientObject( GetClientObject( line ) );
883 else if ( HasClientUntypedData() )
884 event.SetClientData( GetClientData(line) );
885 event.SetString( GetString(line) );
886
887 event.m_commandInt = line ;
888
889 GetEventHandler()->ProcessEvent(event);
890 }
891 }
892 }
893}
573ac9dc 894
5ecae0b7
SC
895#endif
896