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