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