]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/listbox.cpp
Applied patch to avoid g_lib wanrings.
[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 179 m_peer = new wxMacControl() ;
5ca0d812 180 verify_noerr( ::CreateDataBrowserControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds, kDataBrowserListView , m_peer->GetControlRefAddr() ) );
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 }
5ca0d812 195 verify_noerr(m_peer->SetSelectionFlags( 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
5ca0d812
SC
223 verify_noerr(m_peer->AddListViewColumn( &columnDesc, kDataBrowserListViewAppendColumn) ) ;
224 verify_noerr(m_peer->AutoSizeListViewColumns() ) ;
225 verify_noerr(m_peer->SetHasScrollBars(false , true ) ) ;
226 verify_noerr(m_peer->SetTableViewHiliteStyle(kDataBrowserTableViewFillHilite ) ) ;
227 verify_noerr(m_peer->SetListViewHeaderBtnHeight( 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
5ca0d812 243 m_peer->SetCallbacks( &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{
5ca0d812 259 m_peer->SetReference( 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{
9f884528
RD
327 InvalidateBestSize();
328
facd6764
SC
329 int index = m_noItems ;
330 m_stringArray.Add( item ) ;
331 m_dataArray.Add( NULL );
332 m_noItems ++;
333 DoSetItemClientData( index , NULL ) ;
334 MacAppend( item ) ;
335
336 return index ;
337}
338
339void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
340{
341 Clear() ;
342 int n = choices.GetCount();
343
344 for( int i = 0 ; i < n ; ++i )
345 {
346 if ( clientData )
347 {
348#if wxUSE_OWNER_DRAWN
349 wxASSERT_MSG(clientData[i] == NULL,
350 wxT("Can't use client data with owner-drawn listboxes"));
351#else // !wxUSE_OWNER_DRAWN
352 Append( choices[i] , clientData[i] ) ;
353#endif
354 }
355 else
356 Append( choices[i] ) ;
357 }
358
359#if wxUSE_OWNER_DRAWN
360 if ( m_windowStyle & wxLB_OWNERDRAW ) {
361 // first delete old items
362 size_t ui = m_aItems.Count();
363 while ( ui-- != 0 ) {
364 delete m_aItems[ui];
365 m_aItems[ui] = NULL;
366 }
367 m_aItems.Empty();
368
369 // then create new ones
370 for ( ui = 0; ui < (size_t)m_noItems; ui++ ) {
371 wxOwnerDrawn *pNewItem = CreateItem(ui);
372 pNewItem->SetName(choices[ui]);
373 m_aItems.Add(pNewItem);
374 }
375 }
376#endif // wxUSE_OWNER_DRAWN
377}
378
facd6764
SC
379int wxListBox::FindString(const wxString& s) const
380{
381
382 if ( s.Right(1) == wxT("*") )
383 {
384 wxString search = s.Left( s.Length() - 1 ) ;
385 int len = search.Length() ;
386 Str255 s1 , s2 ;
387 wxMacStringToPascal( search , s2 ) ;
388
389 for ( int i = 0 ; i < m_noItems ; ++ i )
390 {
391 wxMacStringToPascal( m_stringArray[i].Left( len ) , s1 ) ;
392
393 if ( EqualString( s1 , s2 , false , false ) )
394 return i ;
395 }
396 if ( s.Left(1) == wxT("*") && s.Length() > 1 )
397 {
398 wxString st = s ;
399 st.MakeLower() ;
400 for ( int i = 0 ; i < m_noItems ; ++i )
401 {
402 if ( GetString(i).Lower().Matches(st) )
403 return i ;
404 }
405 }
406
407 }
408 else
409 {
410 Str255 s1 , s2 ;
411
412 wxMacStringToPascal( s , s2 ) ;
413
414 for ( int i = 0 ; i < m_noItems ; ++ i )
415 {
416 wxMacStringToPascal( m_stringArray[i] , s1 ) ;
417
418 if ( EqualString( s1 , s2 , false , false ) )
419 return i ;
420 }
421 }
422 return -1;
423}
424
425void wxListBox::Clear()
426{
427 FreeData();
428 m_noItems = 0;
429 m_stringArray.Empty() ;
430 m_dataArray.Empty() ;
431 MacClear() ;
432}
433
434void wxListBox::SetSelection(int N, bool select)
435{
436 wxCHECK_RET( N >= 0 && N < m_noItems,
437 wxT("invalid index in wxListBox::SetSelection") );
438 MacSetSelection( N , select ) ;
439 GetSelections( m_selectionPreImage ) ;
440}
441
442bool wxListBox::IsSelected(int N) const
443{
444 wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE,
445 wxT("invalid index in wxListBox::Selected") );
446
447 return MacIsSelected( N ) ;
448}
449
450void *wxListBox::DoGetItemClientData(int N) const
451{
452 wxCHECK_MSG( N >= 0 && N < m_noItems, NULL,
453 wxT("invalid index in wxListBox::GetClientData"));
454
455 return (void *)m_dataArray[N];
456}
457
458wxClientData *wxListBox::DoGetItemClientObject(int N) const
459{
460 return (wxClientData *) DoGetItemClientData( N ) ;
461}
462
463void wxListBox::DoSetItemClientData(int N, void *Client_data)
464{
465 wxCHECK_RET( N >= 0 && N < m_noItems,
466 wxT("invalid index in wxListBox::SetClientData") );
467
468#if wxUSE_OWNER_DRAWN
469 if ( m_windowStyle & wxLB_OWNERDRAW )
470 {
471 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
472 // in OnMeasure/OnDraw.
473 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
474 }
475#endif // wxUSE_OWNER_DRAWN
476 wxASSERT_MSG( m_dataArray.GetCount() >= (size_t) N , wxT("invalid client_data array") ) ;
477
478 if ( m_dataArray.GetCount() > (size_t) N )
479 {
480 m_dataArray[N] = (char*) Client_data ;
481 }
482 else
483 {
484 m_dataArray.Add( (char*) Client_data ) ;
485 }
486}
487
488void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
489{
490 DoSetItemClientData(n, clientData);
491}
492
493// Return number of selections and an array of selected integers
494int wxListBox::GetSelections(wxArrayInt& aSelections) const
495{
496 return MacGetSelections( aSelections ) ;
497}
498
499// Get single selection, for single choice list items
500int wxListBox::GetSelection() const
501{
502 return MacGetSelection() ;
503}
504
505// Find string for position
506wxString wxListBox::GetString(int N) const
507{
508 return m_stringArray[N] ;
509}
510
511void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
512{
513 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
514 wxT("invalid index in wxListBox::InsertItems") );
515
9f884528
RD
516 InvalidateBestSize();
517
facd6764
SC
518 int nItems = items.GetCount();
519
520 for ( int i = 0 ; i < nItems ; i++ )
521 {
522 m_stringArray.Insert( items[i] , pos + i ) ;
523 m_dataArray.Insert( NULL , pos + i ) ;
524 MacInsert( pos + i , items[i] ) ;
525 }
526
527 m_noItems += nItems;
528}
529
530void wxListBox::SetString(int N, const wxString& s)
531{
532 m_stringArray[N] = s ;
533 MacSet( N , s ) ;
534}
535
536wxSize wxListBox::DoGetBestSize() const
537{
538 int lbWidth = 100; // some defaults
539 int lbHeight = 110;
540 int wLine;
541
542 {
543 wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetTopLevelWindowRef() ) ) ;
544
545 if ( m_font.Ok() )
546 {
547 ::TextFont( m_font.MacGetFontNum() ) ;
548 ::TextSize( m_font.MacGetFontSize() ) ;
549 ::TextFace( m_font.MacGetFontStyle() ) ;
550 }
551 else
552 {
553 ::TextFont( kFontIDMonaco ) ;
554 ::TextSize( 9 );
555 ::TextFace( 0 ) ;
556 }
557
558 // Find the widest line
559 for(int i = 0; i < GetCount(); i++) {
560 wxString str(GetString(i));
561 #if wxUSE_UNICODE
562 Point bounds={0,0} ;
563 SInt16 baseline ;
564 ::GetThemeTextDimensions( wxMacCFStringHolder( str , m_font.GetEncoding() ) ,
565 kThemeCurrentPortFont,
566 kThemeStateActive,
567 false,
568 &bounds,
569 &baseline );
570 wLine = bounds.h ;
571 #else
572 wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ;
573 #endif
574 lbWidth = wxMax(lbWidth, wLine);
575 }
576
577 // Add room for the scrollbar
578 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
579
580 // And just a bit more
581 int cy = 12 ;
582 int cx = ::TextWidth( "X" , 0 , 1 ) ;
583 lbWidth += cx ;
584
585 // don't make the listbox too tall (limit height to around 10 items) but don't
586 // make it too small neither
587 lbHeight = (cy+4) * wxMin(wxMax(GetCount(), 3), 10);
588 }
589
590 return wxSize(lbWidth, lbHeight);
591}
592
593int wxListBox::GetCount() const
594{
595 return m_noItems;
596}
597
598void wxListBox::Refresh(bool eraseBack, const wxRect *rect)
599{
600 wxControl::Refresh( eraseBack , rect ) ;
601 // MacRedrawControl() ;
602}
603
604#if wxUSE_OWNER_DRAWN
605
606class wxListBoxItem : public wxOwnerDrawn
607{
608public:
609 wxListBoxItem(const wxString& str = "");
610};
611
612wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE)
613{
614 // no bitmaps/checkmarks
615 SetMarginWidth(0);
616}
617
618wxOwnerDrawn *wxListBox::CreateItem(size_t n)
619{
620 return new wxListBoxItem();
621}
622
623#endif //USE_OWNER_DRAWN
624
b6a20a20
RD
625
626// Some custom controls depend on this
627/* static */ wxVisualAttributes
628wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
629{
630 wxVisualAttributes attr;
631 attr.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
632 attr.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX);
633 attr.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
634 return attr;
635}
636
facd6764
SC
637// ============================================================================
638// list box control implementation
639// ============================================================================
640
641void wxListBox::MacDelete( int N )
642{
643 UInt32 id = m_idArray[N] ;
5ca0d812 644 verify_noerr( m_peer->RemoveItems( kDataBrowserNoItem , 1 , (UInt32*) &id , kDataBrowserItemNoProperty ) ) ;
facd6764
SC
645 m_idArray.RemoveAt( N ) ;
646}
647
648void wxListBox::MacInsert( int n , const wxString& text)
649{
5ca0d812 650 verify_noerr( m_peer->AddItems( kDataBrowserNoItem , 1 , (UInt32*) &m_nextId , kDataBrowserItemNoProperty ) ) ;
facd6764
SC
651 m_idArray.Insert( m_nextId , n ) ;
652 ++m_nextId ;
653}
654
655void wxListBox::MacAppend( const wxString& text)
656{
5ca0d812 657 verify_noerr( m_peer->AddItems( kDataBrowserNoItem , 1 , (UInt32*) &m_nextId , kDataBrowserItemNoProperty ) ) ;
facd6764
SC
658 m_idArray.Add( m_nextId ) ;
659 ++m_nextId ;
660}
661
662void wxListBox::MacClear()
663{
5ca0d812 664 verify_noerr( m_peer->RemoveItems( kDataBrowserNoItem , 0 , NULL , kDataBrowserItemNoProperty ) ) ;
83ce5634 665 m_idArray.Empty() ;
facd6764
SC
666}
667
668void wxListBox::MacSetSelection( int n , bool select )
669{
670 UInt32 id = m_idArray[n] ;
5e6f42cd
SC
671 if ( !(GetWindowStyle() & (wxLB_MULTIPLE|wxLB_EXTENDED) ) )
672 {
673 int n = MacGetSelection() ;
674 if ( n >= 0 )
675 {
676 UInt32 idOld = m_idArray[n] ;
5ca0d812 677 m_peer->SetSelectedItems( 1 , & idOld , kDataBrowserItemsRemove ) ;
5e6f42cd
SC
678 }
679 }
5ca0d812 680 if ( m_peer->IsItemSelected( id ) != select )
facd6764 681 {
5ca0d812 682 verify_noerr(m_peer->SetSelectedItems( 1 , & id , kDataBrowserItemsToggle ) ) ;
facd6764
SC
683 }
684 MacScrollTo( n ) ;
685}
686
687bool wxListBox::MacIsSelected( int n ) const
688{
5ca0d812 689 return m_peer->IsItemSelected( m_idArray[n] ) ;
facd6764
SC
690}
691
692int wxListBox::MacGetSelection() const
693{
694 for ( size_t i = 0 ; i < m_idArray.GetCount() ; ++i )
695 {
5ca0d812 696 if ( m_peer->IsItemSelected( m_idArray[i] ) )
facd6764
SC
697 {
698 return i ;
699 }
700 }
701 return -1 ;
702}
703
704int wxListBox::MacGetSelections( wxArrayInt& aSelections ) const
705{
706 int no_sel = 0 ;
707
708 aSelections.Empty();
709 for ( size_t i = 0 ; i < m_idArray.GetCount() ; ++i )
710 {
5ca0d812 711 if ( m_peer->IsItemSelected( m_idArray[i] ) )
facd6764
SC
712 {
713 aSelections.Add( i ) ;
714 no_sel++ ;
715 }
716 }
717 return no_sel ;
718}
519cb848 719
facd6764
SC
720void wxListBox::MacSet( int n , const wxString& text )
721{
722 // as we don't store the strings we only have to issue a redraw
723 UInt32 id = m_idArray[n] ;
5ca0d812 724 verify_noerr( m_peer->UpdateItems( kDataBrowserNoItem , 1 , &id , kDataBrowserItemNoProperty , kDataBrowserItemNoProperty ) ) ;
facd6764 725}
e42e45a9 726
facd6764
SC
727void wxListBox::MacScrollTo( int n )
728{
a9fc5eec 729 UInt32 id = m_idArray[n] ;
5ca0d812 730 verify_noerr( m_peer->RevealItem( id , kTextColumnId , kDataBrowserRevealWithoutSelecting ) ) ;
facd6764
SC
731}
732
5ecae0b7 733#if !TARGET_API_MAC_OSX
facd6764
SC
734void wxListBox::OnSize( wxSizeEvent &event)
735{
736}
5ecae0b7 737#endif
facd6764 738
facd6764
SC
739void wxListBox::MacSetRedraw( bool doDraw )
740{
741 // nothing to do in compositing mode
742}
743
744void wxListBox::MacDoClick()
83ce5634 745{/*
facd6764
SC
746 wxArrayInt aSelections;
747 int n ;
748 size_t count = GetSelections(aSelections);
749
750 if ( count == m_selectionPreImage.GetCount() )
751 {
752 bool hasChanged = false ;
753 for ( size_t i = 0 ; i < count ; ++i )
754 {
755 if ( aSelections[i] != m_selectionPreImage[i] )
756 {
757 hasChanged = true ;
758 break ;
759 }
760 }
761 if ( !hasChanged )
762 {
763 return ;
764 }
765 }
766
767 m_selectionPreImage = aSelections;
768
769 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
770 event.SetEventObject( this );
771
772 if ( count > 0 )
773 {
774 n = aSelections[0];
775 if ( HasClientObjectData() )
776 event.SetClientObject( GetClientObject(n) );
777 else if ( HasClientUntypedData() )
778 event.SetClientData( GetClientData(n) );
779 event.SetString( GetString(n) );
780 }
781 else
782 {
783 n = -1;
784 }
785
786 event.m_commandInt = n;
787
788 GetEventHandler()->ProcessEvent(event);
83ce5634 789*/
facd6764
SC
790}
791
792void wxListBox::MacDoDoubleClick()
793{
83ce5634 794/*
facd6764
SC
795 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId);
796 event.SetEventObject( this );
797 GetEventHandler()->ProcessEvent(event) ;
83ce5634 798*/
facd6764
SC
799}
800
5ecae0b7
SC
801#if !TARGET_API_MAC_OSX
802
facd6764
SC
803void wxListBox::OnChar(wxKeyEvent& event)
804{
805 // todo trigger proper events here
806 event.Skip() ;
807 return ;
808
809 if ( event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER)
810 {
811 wxWindow* parent = GetParent() ;
812 while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL )
813 parent = parent->GetParent() ;
814
815 if ( parent && parent->GetDefaultItem() )
816 {
817 wxButton *def = wxDynamicCast(parent->GetDefaultItem(),
818 wxButton);
819 if ( def && def->IsEnabled() )
820 {
821 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
822 event.SetEventObject(def);
823 def->Command(event);
824 return ;
825 }
826 }
827 event.Skip() ;
828 }
829 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
830 else if (event.GetKeyCode() == WXK_ESCAPE || (event.GetKeyCode() == '.' && event.MetaDown() ) )
831 {
832 // FIXME: look in ancestors, not just parent.
833 wxWindow* win = GetParent()->FindWindow( wxID_CANCEL ) ;
834 if (win)
835 {
836 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
837 new_event.SetEventObject( win );
838 win->GetEventHandler()->ProcessEvent( new_event );
839 }
840 }
841 else if ( event.GetKeyCode() == WXK_TAB )
842 {
843 wxNavigationKeyEvent new_event;
844 new_event.SetEventObject( this );
845 new_event.SetDirection( !event.ShiftDown() );
846 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
847 new_event.SetWindowChange( event.ControlDown() );
848 new_event.SetCurrentFocus( this );
849 if ( !GetEventHandler()->ProcessEvent( new_event ) )
850 event.Skip() ;
851 }
852 else if ( event.GetKeyCode() == WXK_DOWN || event.GetKeyCode() == WXK_UP )
853 {
854 // perform the default key handling first
855 wxControl::OnKeyDown( event ) ;
856
857 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
858 event.SetEventObject( this );
859
860 wxArrayInt aSelections;
861 int n, count = GetSelections(aSelections);
862 if ( count > 0 )
863 {
864 n = aSelections[0];
865 if ( HasClientObjectData() )
866 event.SetClientObject( GetClientObject(n) );
867 else if ( HasClientUntypedData() )
868 event.SetClientData( GetClientData(n) );
869 event.SetString( GetString(n) );
870 }
871 else
872 {
873 n = -1;
874 }
875
876 event.m_commandInt = n;
877
878 GetEventHandler()->ProcessEvent(event);
879 }
880 else
881 {
882 if ( event.GetTimestamp() > m_lastTypeIn + 60 )
883 {
884 m_typeIn = wxEmptyString ;
885 }
886 m_lastTypeIn = event.GetTimestamp() ;
887 m_typeIn += (char) event.GetKeyCode() ;
888 int line = FindString(wxT("*")+m_typeIn+wxT("*")) ;
889 if ( line >= 0 )
890 {
891 if ( GetSelection() != line )
892 {
893 SetSelection(line) ;
894 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
895 event.SetEventObject( this );
896
897 if ( HasClientObjectData() )
898 event.SetClientObject( GetClientObject( line ) );
899 else if ( HasClientUntypedData() )
900 event.SetClientData( GetClientData(line) );
901 event.SetString( GetString(line) );
902
903 event.m_commandInt = line ;
904
905 GetEventHandler()->ProcessEvent(event);
906 }
907 }
908 }
909}
573ac9dc 910
5ecae0b7
SC
911#endif
912