don't set focus to a hidden notebook (patch 1299005)
[wxWidgets.git] / src / mac / carbon / listbox.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: listbox.cpp
3 // Purpose: wxListBox
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxUSE_LISTBOX
15
16 #include "wx/app.h"
17 #include "wx/listbox.h"
18 #include "wx/button.h"
19 #include "wx/settings.h"
20 #include "wx/toplevel.h"
21 #include "wx/dynarray.h"
22 #include "wx/log.h"
23
24 #include "wx/utils.h"
25
26 IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
27
28 BEGIN_EVENT_TABLE(wxListBox, wxControl)
29 #ifndef __WXMAC_OSX__
30 // EVT_SIZE( wxListBox::OnSize )
31 EVT_CHAR( wxListBox::OnChar )
32 #endif
33 END_EVENT_TABLE()
34
35 #include "wx/mac/uma.h"
36
37 const short kTextColumnId = 1024 ;
38
39 // new databrowserbased version
40 // because of the limited insert
41 // functionality of DataBrowser,
42 // we just introduce id s corresponding
43 // to the line number
44
45 DataBrowserItemDataUPP gDataBrowserItemDataUPP = NULL ;
46 DataBrowserItemNotificationUPP gDataBrowserItemNotificationUPP = NULL ;
47 DataBrowserDrawItemUPP gDataBrowserDrawItemUPP = NULL ;
48
49 #if TARGET_API_MAC_OSX
50 static pascal void DataBrowserItemNotificationProc(ControlRef browser, DataBrowserItemID itemID,
51 DataBrowserItemNotification message, DataBrowserItemDataRef itemData)
52 #else
53 static pascal void DataBrowserItemNotificationProc(ControlRef browser, DataBrowserItemID itemID,
54 DataBrowserItemNotification message)
55 #endif
56 {
57 long ref = GetControlReference( browser ) ;
58 if ( ref )
59 {
60 wxListBox* list = wxDynamicCast( (wxObject*) ref , wxListBox ) ;
61 int i = itemID - 1 ;
62 if (i >= 0 && i < list->GetCount() )
63 {
64 bool trigger = false ;
65 wxCommandEvent event(
66 wxEVT_COMMAND_LISTBOX_SELECTED, list->GetId() );
67 switch( message )
68 {
69 case kDataBrowserItemDeselected :
70 if ( list->HasMultipleSelection() )
71 trigger = !list->MacIsSelectionSuppressed() ;
72 break ;
73 case kDataBrowserItemSelected :
74 trigger = !list->MacIsSelectionSuppressed() ;
75 break ;
76 case kDataBrowserItemDoubleClicked :
77 event.SetEventType(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED) ;
78 trigger = true ;
79 break ;
80 default :
81 break ;
82 }
83 if ( trigger )
84 {
85 event.SetEventObject( list );
86 if ( list->HasClientObjectData() )
87 event.SetClientObject( list->GetClientObject(i) );
88 else if ( list->HasClientUntypedData() )
89 event.SetClientData( list->GetClientData(i) );
90 event.SetString( list->GetString(i) );
91 event.SetInt(i) ;
92 event.SetExtraLong( list->HasMultipleSelection() ? message == kDataBrowserItemSelected : TRUE );
93 wxPostEvent( list->GetEventHandler() , event ) ;
94 // direct notification is not always having the listbox GetSelection() having in synch with event
95 // list->GetEventHandler()->ProcessEvent(event) ;
96 }
97 }
98 }
99 }
100
101 static pascal OSStatus ListBoxGetSetItemData(ControlRef browser,
102 DataBrowserItemID itemID, DataBrowserPropertyID property,
103 DataBrowserItemDataRef itemData, Boolean changeValue)
104 {
105 OSStatus err = errDataBrowserPropertyNotSupported;
106
107 if ( ! changeValue )
108 {
109 switch (property)
110 {
111
112 case kTextColumnId:
113 {
114 long ref = GetControlReference( browser ) ;
115 if ( ref )
116 {
117 wxListBox* list = wxDynamicCast( (wxObject*) ref , wxListBox ) ;
118 int i = itemID - 1 ;
119 if (i >= 0 && i < list->GetCount() )
120 {
121 wxMacCFStringHolder cf( list->GetString(i) , list->GetFont().GetEncoding() ) ;
122 verify_noerr( ::SetDataBrowserItemDataText( itemData , cf ) ) ;
123 err = noErr ;
124 }
125 }
126 }
127 break;
128
129 default:
130
131 break;
132 }
133 }
134
135 return err;
136 }
137
138 static pascal void ListBoxDrawProc( ControlRef browser , DataBrowserItemID item , DataBrowserPropertyID property ,
139 DataBrowserItemState itemState , const Rect *itemRect , SInt16 depth , Boolean isColorDevice )
140 {
141
142 CFStringRef cfString;
143 long systemVersion;
144
145 cfString = CFStringCreateWithFormat( NULL, NULL, CFSTR("Row %d"), item );
146
147 ThemeDrawingState themeState ;
148 GetThemeDrawingState( &themeState ) ;
149
150 if ( itemState == kDataBrowserItemIsSelected ) // In this sample we handle the "selected" state, all others fall through to our "active" state
151 {
152 Gestalt( gestaltSystemVersion, &systemVersion );
153 if ( (systemVersion >= 0x00001030) && (IsControlActive( browser ) == false) ) // Panther DB starts using kThemeBrushSecondaryHighlightColor for inactive browser hilighting
154 SetThemePen( kThemeBrushSecondaryHighlightColor, 32, true );
155 else
156 SetThemePen( kThemeBrushPrimaryHighlightColor, 32, true );
157
158 PaintRect( itemRect ); // First paint the hilite rect, then the text on top
159 SetThemeDrawingState( themeState , false ) ;
160 }
161 DrawThemeTextBox( cfString, kThemeApplicationFont, kThemeStateActive, true, itemRect, teFlushDefault, NULL );
162 if ( cfString != NULL )
163 CFRelease( cfString );
164 SetThemeDrawingState( themeState , true ) ;
165 }
166
167 // Listbox item
168 wxListBox::wxListBox()
169 {
170 m_noItems = 0;
171 m_selected = 0;
172 m_macList = NULL ;
173 m_suppressSelection = false ;
174 }
175
176 bool wxListBox::Create(wxWindow *parent, wxWindowID id,
177 const wxPoint& pos,
178 const wxSize& size,
179 const wxArrayString& choices,
180 long style,
181 const wxValidator& validator,
182 const wxString& name)
183 {
184 wxCArrayString chs(choices);
185
186 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
187 style, validator, name);
188 }
189
190 bool wxListBox::Create(wxWindow *parent, wxWindowID id,
191 const wxPoint& pos,
192 const wxSize& size,
193 int n, const wxString choices[],
194 long style,
195 const wxValidator& validator,
196 const wxString& name)
197 {
198 m_macIsUserPane = FALSE ;
199
200 wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED),
201 _T("only one of listbox selection modes can be specified") );
202
203 if ( !wxListBoxBase::Create(parent, id, pos, size, style & ~(wxHSCROLL|wxVSCROLL), validator, name) )
204 return false;
205
206 m_noItems = 0 ; // this will be increased by our append command
207 m_selected = 0;
208
209 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
210
211 m_peer = new wxMacControl(this) ;
212 verify_noerr( ::CreateDataBrowserControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds, kDataBrowserListView , m_peer->GetControlRefAddr() ) );
213
214 DataBrowserSelectionFlags options = kDataBrowserDragSelect ;
215 if ( style & wxLB_MULTIPLE )
216 {
217 options += kDataBrowserAlwaysExtendSelection + kDataBrowserCmdTogglesSelection ;
218 }
219 else if ( style & wxLB_EXTENDED )
220 {
221 // default behaviour
222 }
223 else
224 {
225 options += kDataBrowserSelectOnlyOne ;
226 }
227 verify_noerr(m_peer->SetSelectionFlags( options ) );
228
229 if ( gDataBrowserItemDataUPP == NULL ) gDataBrowserItemDataUPP = NewDataBrowserItemDataUPP(ListBoxGetSetItemData) ;
230 if ( gDataBrowserItemNotificationUPP == NULL )
231 {
232 gDataBrowserItemNotificationUPP =
233 #if TARGET_API_MAC_OSX
234 (DataBrowserItemNotificationUPP) NewDataBrowserItemNotificationWithItemUPP(DataBrowserItemNotificationProc) ;
235 #else
236 NewDataBrowserItemNotificationUPP(DataBrowserItemNotificationProc) ;
237 #endif
238 }
239 if ( gDataBrowserDrawItemUPP == NULL ) gDataBrowserDrawItemUPP = NewDataBrowserDrawItemUPP(ListBoxDrawProc) ;
240
241 DataBrowserCallbacks callbacks ;
242 InitializeDataBrowserCallbacks( &callbacks , kDataBrowserLatestCallbacks ) ;
243
244 callbacks.u.v1.itemDataCallback = gDataBrowserItemDataUPP;
245 callbacks.u.v1.itemNotificationCallback = gDataBrowserItemNotificationUPP;
246 m_peer->SetCallbacks( &callbacks);
247
248 DataBrowserCustomCallbacks customCallbacks ;
249 InitializeDataBrowserCustomCallbacks( &customCallbacks , kDataBrowserLatestCustomCallbacks ) ;
250
251 customCallbacks.u.v1.drawItemCallback = gDataBrowserDrawItemUPP ;
252
253 SetDataBrowserCustomCallbacks( m_peer->GetControlRef() , &customCallbacks ) ;
254
255 DataBrowserListViewColumnDesc columnDesc ;
256 columnDesc.headerBtnDesc.titleOffset = 0;
257 columnDesc.headerBtnDesc.version = kDataBrowserListViewLatestHeaderDesc;
258
259 columnDesc.headerBtnDesc.btnFontStyle.flags =
260 kControlUseFontMask | kControlUseJustMask;
261
262 columnDesc.headerBtnDesc.btnContentInfo.contentType = kControlNoContent;
263 columnDesc.headerBtnDesc.btnFontStyle.just = teFlushDefault;
264 columnDesc.headerBtnDesc.minimumWidth = 0;
265 columnDesc.headerBtnDesc.maximumWidth = 10000;
266
267 columnDesc.headerBtnDesc.btnFontStyle.font = kControlFontViewSystemFont;
268 columnDesc.headerBtnDesc.btnFontStyle.style = normal;
269 columnDesc.headerBtnDesc.titleString = NULL ; // CFSTR( "" );
270
271 columnDesc.propertyDesc.propertyID = kTextColumnId;
272 columnDesc.propertyDesc.propertyType = kDataBrowserTextType ; // kDataBrowserCustomType;
273 columnDesc.propertyDesc.propertyFlags =
274 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
275 kDataBrowserListViewTypeSelectColumn |
276 #endif
277 kDataBrowserTableViewSelectionColumn ;
278
279 verify_noerr(m_peer->AddListViewColumn( &columnDesc, kDataBrowserListViewAppendColumn) ) ;
280 verify_noerr(m_peer->AutoSizeListViewColumns() ) ;
281 verify_noerr(m_peer->SetHasScrollBars(false , true ) ) ;
282 verify_noerr(m_peer->SetTableViewHiliteStyle(kDataBrowserTableViewFillHilite ) ) ;
283 verify_noerr(m_peer->SetListViewHeaderBtnHeight( 0 ) ) ;
284
285 #if 0
286 // shouldn't be necessary anymore under 10.2
287 m_peer->SetData( kControlNoPart, kControlDataBrowserIncludesFrameAndFocusTag, (Boolean) false ) ;
288 m_peer->SetNeedsFocusRect( true ) ;
289 #endif
290
291 MacPostControlCreate(pos,size) ;
292
293 for ( int i = 0 ; i < n ; i++ )
294 {
295 Append( choices[i] ) ;
296 }
297
298 SetBestSize(size); // Needed because it is a wxControlWithItems
299
300 return TRUE;
301 }
302
303 wxListBox::~wxListBox()
304 {
305 m_peer->SetReference( 0 ) ;
306 FreeData() ;
307 // avoid access during destruction
308 if ( m_macList )
309 {
310 m_macList = NULL ;
311 }
312 }
313
314 void wxListBox::FreeData()
315 {
316 if ( HasClientObjectData() )
317 {
318 for ( size_t n = 0; n < (size_t)m_noItems; n++ )
319 {
320 delete GetClientObject(n);
321 }
322 }
323 }
324
325 void wxListBox::DoSetSize(int x, int y,
326 int width, int height,
327 int sizeFlags )
328 {
329 wxControl::DoSetSize( x , y , width , height , sizeFlags ) ;
330 }
331
332 void wxListBox::DoSetFirstItem(int N)
333 {
334 MacScrollTo( N ) ;
335 }
336
337 void wxListBox::Delete(int N)
338 {
339 wxCHECK_RET( N >= 0 && N < m_noItems,
340 wxT("invalid index in wxListBox::Delete") );
341
342 if ( HasClientObjectData() )
343 {
344 delete GetClientObject(N);
345 }
346 m_stringArray.RemoveAt( N ) ;
347 m_dataArray.RemoveAt( N ) ;
348 m_noItems --;
349
350 MacDelete( N ) ;
351 }
352
353 int wxListBox::DoAppend(const wxString& item)
354 {
355 InvalidateBestSize();
356
357 int index = m_noItems ;
358 m_stringArray.Add( item ) ;
359 m_dataArray.Add( NULL );
360 m_noItems ++;
361 DoSetItemClientData( index , NULL ) ;
362 MacAppend( item ) ;
363
364 return index ;
365 }
366
367 void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
368 {
369 Clear() ;
370 int n = choices.GetCount();
371
372 for( int i = 0 ; i < n ; ++i )
373 {
374 if ( clientData )
375 {
376 Append( choices[i] , clientData[i] ) ;
377 }
378 else
379 Append( choices[i] ) ;
380 }
381 }
382
383 int wxListBox::FindString(const wxString& s) const
384 {
385
386 if ( s.Right(1) == wxT("*") )
387 {
388 wxString search = s.Left( s.Length() - 1 ) ;
389 int len = search.Length() ;
390 Str255 s1 , s2 ;
391 wxMacStringToPascal( search , s2 ) ;
392
393 for ( int i = 0 ; i < m_noItems ; ++ i )
394 {
395 wxMacStringToPascal( m_stringArray[i].Left( len ) , s1 ) ;
396
397 if ( EqualString( s1 , s2 , false , false ) )
398 return i ;
399 }
400 if ( s.Left(1) == wxT("*") && s.Length() > 1 )
401 {
402 wxString st = s ;
403 st.MakeLower() ;
404 for ( int i = 0 ; i < m_noItems ; ++i )
405 {
406 if ( GetString(i).Lower().Matches(st) )
407 return i ;
408 }
409 }
410
411 }
412 else
413 {
414 Str255 s1 , s2 ;
415
416 wxMacStringToPascal( s , s2 ) ;
417
418 for ( int i = 0 ; i < m_noItems ; ++ i )
419 {
420 wxMacStringToPascal( m_stringArray[i] , s1 ) ;
421
422 if ( EqualString( s1 , s2 , false , false ) )
423 return i ;
424 }
425 }
426 return -1;
427 }
428
429 void wxListBox::Clear()
430 {
431 FreeData();
432 m_noItems = 0;
433 m_stringArray.Empty() ;
434 m_dataArray.Empty() ;
435 MacClear() ;
436 }
437
438 void wxListBox::DoSetSelection(int N, bool select)
439 {
440 wxCHECK_RET( N == wxNOT_FOUND || (N >= 0 && N < m_noItems) ,
441 wxT("invalid index in wxListBox::SetSelection") );
442
443 if ( N == wxNOT_FOUND )
444 MacDeselectAll() ;
445 else
446 MacSetSelection( N , select ) ;
447 }
448
449 bool wxListBox::IsSelected(int N) const
450 {
451 wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE,
452 wxT("invalid index in wxListBox::Selected") );
453
454 return MacIsSelected( N ) ;
455 }
456
457 void *wxListBox::DoGetItemClientData(int N) const
458 {
459 wxCHECK_MSG( N >= 0 && N < m_noItems, NULL,
460 wxT("invalid index in wxListBox::GetClientData"));
461
462 return (void *)m_dataArray[N];
463 }
464
465 wxClientData *wxListBox::DoGetItemClientObject(int N) const
466 {
467 return (wxClientData *) DoGetItemClientData( N ) ;
468 }
469
470 void wxListBox::DoSetItemClientData(int N, void *Client_data)
471 {
472 wxCHECK_RET( N >= 0 && N < m_noItems,
473 wxT("invalid index in wxListBox::SetClientData") );
474
475 wxASSERT_MSG( m_dataArray.GetCount() >= (size_t) N , wxT("invalid client_data array") ) ;
476
477 if ( m_dataArray.GetCount() > (size_t) N )
478 {
479 m_dataArray[N] = (char*) Client_data ;
480 }
481 else
482 {
483 m_dataArray.Add( (char*) Client_data ) ;
484 }
485 }
486
487 void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
488 {
489 DoSetItemClientData(n, clientData);
490 }
491
492 // Return number of selections and an array of selected integers
493 int wxListBox::GetSelections(wxArrayInt& aSelections) const
494 {
495 return MacGetSelections( aSelections ) ;
496 }
497
498 // Get single selection, for single choice list items
499 int wxListBox::GetSelection() const
500 {
501 return MacGetSelection() ;
502 }
503
504 // Find string for position
505 wxString wxListBox::GetString(int N) const
506 {
507 wxCHECK_MSG( N >= 0 && N < m_noItems, wxEmptyString,
508 wxT("invalid index in wxListBox::GetString") );
509
510 return m_stringArray[N] ;
511 }
512
513 void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
514 {
515 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
516 wxT("invalid index in wxListBox::InsertItems") );
517
518 InvalidateBestSize();
519
520 int nItems = items.GetCount();
521
522 for ( int i = 0 ; i < nItems ; i++ )
523 {
524 m_stringArray.Insert( items[i] , pos + i ) ;
525 m_dataArray.Insert( NULL , pos + i ) ;
526 m_noItems++ ;
527 MacInsert( pos + i , items[i] ) ;
528 }
529 }
530
531 void wxListBox::SetString(int N, const wxString& s)
532 {
533 m_stringArray[N] = s ;
534 MacSet( N , s ) ;
535 }
536
537 wxSize wxListBox::DoGetBestSize() const
538 {
539 int lbWidth = 100; // some defaults
540 int lbHeight = 110;
541 int wLine;
542
543 {
544 wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetTopLevelWindowRef() ) ) ;
545
546 if ( m_font.Ok() )
547 {
548 ::TextFont( m_font.MacGetFontNum() ) ;
549 ::TextSize( m_font.MacGetFontSize() ) ;
550 ::TextFace( m_font.MacGetFontStyle() ) ;
551 }
552 else
553 {
554 ::TextFont( kFontIDMonaco ) ;
555 ::TextSize( 9 );
556 ::TextFace( 0 ) ;
557 }
558
559 // Find the widest line
560 for(int i = 0; i < GetCount(); i++) {
561 wxString str(GetString(i));
562 #if wxUSE_UNICODE
563 Point bounds={0,0} ;
564 SInt16 baseline ;
565 ::GetThemeTextDimensions( wxMacCFStringHolder( str , m_font.GetEncoding() ) ,
566 kThemeCurrentPortFont,
567 kThemeStateActive,
568 false,
569 &bounds,
570 &baseline );
571 wLine = bounds.h ;
572 #else
573 wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ;
574 #endif
575 lbWidth = wxMax(lbWidth, wLine);
576 }
577
578 // Add room for the scrollbar
579 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
580
581 // And just a bit more
582 int cy = 12 ;
583 int cx = ::TextWidth( "X" , 0 , 1 ) ;
584 lbWidth += cx ;
585
586 // don't make the listbox too tall (limit height to around 10 items) but don't
587 // make it too small neither
588 lbHeight = (cy+4) * wxMin(wxMax(GetCount(), 3), 10);
589 }
590
591 return wxSize(lbWidth, lbHeight);
592 }
593
594 int wxListBox::GetCount() const
595 {
596 return m_noItems;
597 }
598
599 void wxListBox::Refresh(bool eraseBack, const wxRect *rect)
600 {
601 wxControl::Refresh( eraseBack , rect ) ;
602 }
603
604
605 // Some custom controls depend on this
606 /* static */ wxVisualAttributes
607 wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
608 {
609 wxVisualAttributes attr;
610 attr.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
611 attr.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX);
612 attr.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
613 return attr;
614 }
615
616 // ============================================================================
617 // list box control implementation
618 // ============================================================================
619
620 void wxListBox::MacDelete( int n )
621 {
622 wxArrayInt selectionBefore ;
623 MacGetSelections( selectionBefore ) ;
624
625 UInt32 id = m_noItems+1 ;
626 verify_noerr( m_peer->RemoveItems( kDataBrowserNoItem , 1 , (UInt32*) &id , kDataBrowserItemNoProperty ) ) ;
627 for ( size_t i = 0 ; i < selectionBefore.GetCount() ; ++i )
628 {
629 int current = selectionBefore[i] ;
630 if ( current == n )
631 {
632 // selection was deleted
633 MacSetSelection( current , false ) ;
634 }
635 else if ( current > n )
636 {
637 // something behind the deleted item was selected -> move up
638 MacSetSelection( current - 1 , true ) ;
639 MacSetSelection( current , false ) ;
640 }
641 }
642 // refresh all
643 verify_noerr( m_peer->UpdateItems( kDataBrowserNoItem , 1 , (UInt32*) kDataBrowserNoItem , kDataBrowserItemNoProperty , kDataBrowserItemNoProperty ) ) ;
644 }
645
646 void wxListBox::MacInsert( int n , const wxString& text)
647 {
648 wxArrayInt selectionBefore ;
649 MacGetSelections( selectionBefore ) ;
650
651 UInt32 id = m_noItems ; // this has already been increased
652 verify_noerr( m_peer->AddItems( kDataBrowserNoItem , 1 , (UInt32*) &id , kDataBrowserItemNoProperty ) ) ;
653
654 for ( int i = selectionBefore.GetCount()-1 ; i >= 0 ; --i )
655 {
656 int current = selectionBefore[i] ;
657 if ( current >= n )
658 {
659 MacSetSelection( current + 1 , true ) ;
660 MacSetSelection( current , false ) ;
661 }
662 }
663
664 // refresh all
665 verify_noerr( m_peer->UpdateItems( kDataBrowserNoItem , 1 , (UInt32*) kDataBrowserNoItem , kDataBrowserItemNoProperty , kDataBrowserItemNoProperty ) ) ;
666 }
667
668 void wxListBox::MacAppend( const wxString& text)
669 {
670 UInt32 id = m_noItems ; // this has already been increased
671 verify_noerr( m_peer->AddItems( kDataBrowserNoItem , 1 , (UInt32*) &id , kDataBrowserItemNoProperty ) ) ;
672 // no need to deal with selections nor refreshed, as we have appended
673 }
674
675 void wxListBox::MacClear()
676 {
677 verify_noerr( m_peer->RemoveItems( kDataBrowserNoItem , 0 , NULL , kDataBrowserItemNoProperty ) ) ;
678 }
679
680 void wxListBox::MacDeselectAll()
681 {
682 bool former = MacSuppressSelection( true ) ;
683 verify_noerr(m_peer->SetSelectedItems( 0 , NULL , kDataBrowserItemsRemove ) ) ;
684 MacSuppressSelection( former ) ;
685 }
686
687 void wxListBox::MacSetSelection( int n , bool select )
688 {
689 bool former = MacSuppressSelection( true ) ;
690 UInt32 id = n + 1 ;
691
692 if ( m_peer->IsItemSelected( id ) != select )
693 {
694 if ( select )
695 verify_noerr(m_peer->SetSelectedItems( 1 , & id , HasMultipleSelection() ? kDataBrowserItemsAdd : kDataBrowserItemsAssign ) ) ;
696 else
697 verify_noerr(m_peer->SetSelectedItems( 1 , & id , kDataBrowserItemsRemove ) ) ;
698 }
699 MacScrollTo( n ) ;
700 MacSuppressSelection( former ) ;
701 }
702
703 bool wxListBox::MacSuppressSelection( bool suppress )
704 {
705 bool former = m_suppressSelection ;
706 m_suppressSelection = suppress ;
707 return former ;
708 }
709
710 bool wxListBox::MacIsSelected( int n ) const
711 {
712 return m_peer->IsItemSelected( n + 1 ) ;
713 }
714
715 int wxListBox::MacGetSelection() const
716 {
717 for ( int i = 0 ; i < GetCount() ; ++i )
718 {
719 if ( m_peer->IsItemSelected( i + 1 ) )
720 {
721 return i ;
722 }
723 }
724 return -1 ;
725 }
726
727 int wxListBox::MacGetSelections( wxArrayInt& aSelections ) const
728 {
729 int no_sel = 0 ;
730
731 aSelections.Empty();
732
733 UInt32 first , last ;
734 m_peer->GetSelectionAnchor( &first , &last ) ;
735 if ( first != kDataBrowserNoItem )
736 {
737 for ( size_t i = first ; i <= last ; ++i )
738 {
739 if ( m_peer->IsItemSelected( i ) )
740 {
741 aSelections.Add( i - 1 ) ;
742 no_sel++ ;
743 }
744 }
745 }
746 return no_sel ;
747 }
748
749 void wxListBox::MacSet( int n , const wxString& text )
750 {
751 // as we don't store the strings we only have to issue a redraw
752 UInt32 id = n + 1 ;
753 verify_noerr( m_peer->UpdateItems( kDataBrowserNoItem , 1 , &id , kDataBrowserItemNoProperty , kDataBrowserItemNoProperty ) ) ;
754 }
755
756 void wxListBox::MacScrollTo( int n )
757 {
758 UInt32 id = n + 1 ;
759 verify_noerr( m_peer->RevealItem( id , kTextColumnId , kDataBrowserRevealWithoutSelecting ) ) ;
760 }
761
762 #if !TARGET_API_MAC_OSX
763
764 void wxListBox::OnChar(wxKeyEvent& event)
765 {
766 // todo trigger proper events here
767 event.Skip() ;
768 return ;
769
770 if ( event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER)
771 {
772 wxWindow* parent = GetParent() ;
773 while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL )
774 parent = parent->GetParent() ;
775
776 if ( parent && parent->GetDefaultItem() )
777 {
778 wxButton *def = wxDynamicCast(parent->GetDefaultItem(),
779 wxButton);
780 if ( def && def->IsEnabled() )
781 {
782 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
783 event.SetEventObject(def);
784 def->Command(event);
785 return ;
786 }
787 }
788 event.Skip() ;
789 }
790 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
791 else if (event.GetKeyCode() == WXK_ESCAPE || (event.GetKeyCode() == '.' && event.MetaDown() ) )
792 {
793 // FIXME: look in ancestors, not just parent.
794 wxWindow* win = GetParent()->FindWindow( wxID_CANCEL ) ;
795 if (win)
796 {
797 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
798 new_event.SetEventObject( win );
799 win->GetEventHandler()->ProcessEvent( new_event );
800 }
801 }
802 else if ( event.GetKeyCode() == WXK_TAB )
803 {
804 wxNavigationKeyEvent new_event;
805 new_event.SetEventObject( this );
806 new_event.SetDirection( !event.ShiftDown() );
807 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
808 new_event.SetWindowChange( event.ControlDown() );
809 new_event.SetCurrentFocus( this );
810 if ( !GetEventHandler()->ProcessEvent( new_event ) )
811 event.Skip() ;
812 }
813 else if ( event.GetKeyCode() == WXK_DOWN || event.GetKeyCode() == WXK_UP )
814 {
815 // perform the default key handling first
816 wxControl::OnKeyDown( event ) ;
817
818 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
819 event.SetEventObject( this );
820
821 wxArrayInt aSelections;
822 int n, count = GetSelections(aSelections);
823 if ( count > 0 )
824 {
825 n = aSelections[0];
826 if ( HasClientObjectData() )
827 event.SetClientObject( GetClientObject(n) );
828 else if ( HasClientUntypedData() )
829 event.SetClientData( GetClientData(n) );
830 event.SetString( GetString(n) );
831 }
832 else
833 {
834 n = -1;
835 }
836
837 event.SetInt(n);
838
839 GetEventHandler()->ProcessEvent(event);
840 }
841 else
842 {
843 if ( event.GetTimestamp() > m_lastTypeIn + 60 )
844 {
845 m_typeIn = wxEmptyString ;
846 }
847 m_lastTypeIn = event.GetTimestamp() ;
848 m_typeIn += (char) event.GetKeyCode() ;
849 int line = FindString(wxT("*")+m_typeIn+wxT("*")) ;
850 if ( line >= 0 )
851 {
852 if ( GetSelection() != line )
853 {
854 SetSelection(line) ;
855 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
856 event.SetEventObject( this );
857
858 if ( HasClientObjectData() )
859 event.SetClientObject( GetClientObject( line ) );
860 else if ( HasClientUntypedData() )
861 event.SetClientData( GetClientData(line) );
862 event.SetString( GetString(line) );
863
864 event.SetInt(line);
865
866 GetEventHandler()->ProcessEvent(event);
867 }
868 }
869 }
870 }
871
872 #endif // !TARGET_API_MAC_OSX
873
874 #endif
875