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