]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/listbox.cpp
compile fix
[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( 0 ) ;
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::DoSetSelection(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 }
613
614 #if wxUSE_OWNER_DRAWN
615
616 class wxListBoxItem : public wxOwnerDrawn
617 {
618 public:
619 wxListBoxItem(const wxString& str = "");
620 };
621
622 wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE)
623 {
624 // no bitmaps/checkmarks
625 SetMarginWidth(0);
626 }
627
628 wxOwnerDrawn *wxListBox::CreateItem(size_t n)
629 {
630 return new wxListBoxItem();
631 }
632
633 #endif //USE_OWNER_DRAWN
634
635
636 // Some custom controls depend on this
637 /* static */ wxVisualAttributes
638 wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
639 {
640 wxVisualAttributes attr;
641 attr.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
642 attr.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX);
643 attr.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
644 return attr;
645 }
646
647 // ============================================================================
648 // list box control implementation
649 // ============================================================================
650
651 void wxListBox::MacDelete( int n )
652 {
653 wxArrayInt selectionBefore ;
654 MacGetSelections( selectionBefore ) ;
655
656 UInt32 id = m_noItems+1 ;
657 verify_noerr( m_peer->RemoveItems( kDataBrowserNoItem , 1 , (UInt32*) &id , kDataBrowserItemNoProperty ) ) ;
658 for ( size_t i = 0 ; i < selectionBefore.GetCount() ; ++i )
659 {
660 int current = selectionBefore[i] ;
661 if ( current == n )
662 {
663 // selection was deleted
664 MacSetSelection( current , false ) ;
665 }
666 else if ( current > n )
667 {
668 // something behind the deleted item was selected -> move up
669 MacSetSelection( current - 1 , true ) ;
670 MacSetSelection( current , false ) ;
671 }
672 }
673 // refresh all
674 verify_noerr( m_peer->UpdateItems( kDataBrowserNoItem , 1 , (UInt32*) kDataBrowserNoItem , kDataBrowserItemNoProperty , kDataBrowserItemNoProperty ) ) ;
675 }
676
677 void wxListBox::MacInsert( int n , const wxString& text)
678 {
679 wxArrayInt selectionBefore ;
680 MacGetSelections( selectionBefore ) ;
681
682 UInt32 id = m_noItems ; // this has already been increased
683 verify_noerr( m_peer->AddItems( kDataBrowserNoItem , 1 , (UInt32*) &id , kDataBrowserItemNoProperty ) ) ;
684
685 for ( int i = selectionBefore.GetCount()-1 ; i >= 0 ; --i )
686 {
687 int current = selectionBefore[i] ;
688 if ( current >= n )
689 {
690 MacSetSelection( current + 1 , true ) ;
691 MacSetSelection( current , false ) ;
692 }
693 }
694
695 // refresh all
696 verify_noerr( m_peer->UpdateItems( kDataBrowserNoItem , 1 , (UInt32*) kDataBrowserNoItem , kDataBrowserItemNoProperty , kDataBrowserItemNoProperty ) ) ;
697 }
698
699 void wxListBox::MacAppend( const wxString& text)
700 {
701 UInt32 id = m_noItems ; // this has already been increased
702 verify_noerr( m_peer->AddItems( kDataBrowserNoItem , 1 , (UInt32*) &id , kDataBrowserItemNoProperty ) ) ;
703 // no need to deal with selections nor refreshed, as we have appended
704 }
705
706 void wxListBox::MacClear()
707 {
708 verify_noerr( m_peer->RemoveItems( kDataBrowserNoItem , 0 , NULL , kDataBrowserItemNoProperty ) ) ;
709 }
710
711 void wxListBox::MacDeselectAll()
712 {
713 bool former = MacSuppressSelection( true ) ;
714 verify_noerr(m_peer->SetSelectedItems( 0 , NULL , kDataBrowserItemsRemove ) ) ;
715 MacSuppressSelection( former ) ;
716 }
717
718 void wxListBox::MacSetSelection( int n , bool select )
719 {
720 bool former = MacSuppressSelection( true ) ;
721 UInt32 id = n + 1 ;
722
723 if ( m_peer->IsItemSelected( id ) != select )
724 {
725 if ( select )
726 verify_noerr(m_peer->SetSelectedItems( 1 , & id , HasMultipleSelection() ? kDataBrowserItemsAdd : kDataBrowserItemsAssign ) ) ;
727 else
728 verify_noerr(m_peer->SetSelectedItems( 1 , & id , kDataBrowserItemsRemove ) ) ;
729 }
730 MacScrollTo( n ) ;
731 MacSuppressSelection( former ) ;
732 }
733
734 bool wxListBox::MacSuppressSelection( bool suppress )
735 {
736 bool former = m_suppressSelection ;
737 m_suppressSelection = suppress ;
738 return former ;
739 }
740
741 bool wxListBox::MacIsSelected( int n ) const
742 {
743 return m_peer->IsItemSelected( n + 1 ) ;
744 }
745
746 int wxListBox::MacGetSelection() const
747 {
748 for ( int i = 0 ; i < GetCount() ; ++i )
749 {
750 if ( m_peer->IsItemSelected( i + 1 ) )
751 {
752 return i ;
753 }
754 }
755 return -1 ;
756 }
757
758 int wxListBox::MacGetSelections( wxArrayInt& aSelections ) const
759 {
760 int no_sel = 0 ;
761
762 aSelections.Empty();
763
764 UInt32 first , last ;
765 m_peer->GetSelectionAnchor( &first , &last ) ;
766 if ( first != kDataBrowserNoItem )
767 {
768 for ( size_t i = first ; i <= last ; ++i )
769 {
770 if ( m_peer->IsItemSelected( i ) )
771 {
772 aSelections.Add( i - 1 ) ;
773 no_sel++ ;
774 }
775 }
776 }
777 return no_sel ;
778 }
779
780 void wxListBox::MacSet( int n , const wxString& text )
781 {
782 // as we don't store the strings we only have to issue a redraw
783 UInt32 id = n + 1 ;
784 verify_noerr( m_peer->UpdateItems( kDataBrowserNoItem , 1 , &id , kDataBrowserItemNoProperty , kDataBrowserItemNoProperty ) ) ;
785 }
786
787 void wxListBox::MacScrollTo( int n )
788 {
789 UInt32 id = n + 1 ;
790 verify_noerr( m_peer->RevealItem( id , kTextColumnId , kDataBrowserRevealWithoutSelecting ) ) ;
791 }
792
793 #if !TARGET_API_MAC_OSX
794
795 void wxListBox::OnChar(wxKeyEvent& event)
796 {
797 // todo trigger proper events here
798 event.Skip() ;
799 return ;
800
801 if ( event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER)
802 {
803 wxWindow* parent = GetParent() ;
804 while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL )
805 parent = parent->GetParent() ;
806
807 if ( parent && parent->GetDefaultItem() )
808 {
809 wxButton *def = wxDynamicCast(parent->GetDefaultItem(),
810 wxButton);
811 if ( def && def->IsEnabled() )
812 {
813 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
814 event.SetEventObject(def);
815 def->Command(event);
816 return ;
817 }
818 }
819 event.Skip() ;
820 }
821 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
822 else if (event.GetKeyCode() == WXK_ESCAPE || (event.GetKeyCode() == '.' && event.MetaDown() ) )
823 {
824 // FIXME: look in ancestors, not just parent.
825 wxWindow* win = GetParent()->FindWindow( wxID_CANCEL ) ;
826 if (win)
827 {
828 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
829 new_event.SetEventObject( win );
830 win->GetEventHandler()->ProcessEvent( new_event );
831 }
832 }
833 else if ( event.GetKeyCode() == WXK_TAB )
834 {
835 wxNavigationKeyEvent new_event;
836 new_event.SetEventObject( this );
837 new_event.SetDirection( !event.ShiftDown() );
838 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
839 new_event.SetWindowChange( event.ControlDown() );
840 new_event.SetCurrentFocus( this );
841 if ( !GetEventHandler()->ProcessEvent( new_event ) )
842 event.Skip() ;
843 }
844 else if ( event.GetKeyCode() == WXK_DOWN || event.GetKeyCode() == WXK_UP )
845 {
846 // perform the default key handling first
847 wxControl::OnKeyDown( event ) ;
848
849 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
850 event.SetEventObject( this );
851
852 wxArrayInt aSelections;
853 int n, count = GetSelections(aSelections);
854 if ( count > 0 )
855 {
856 n = aSelections[0];
857 if ( HasClientObjectData() )
858 event.SetClientObject( GetClientObject(n) );
859 else if ( HasClientUntypedData() )
860 event.SetClientData( GetClientData(n) );
861 event.SetString( GetString(n) );
862 }
863 else
864 {
865 n = -1;
866 }
867
868 event.SetInt(n);
869
870 GetEventHandler()->ProcessEvent(event);
871 }
872 else
873 {
874 if ( event.GetTimestamp() > m_lastTypeIn + 60 )
875 {
876 m_typeIn = wxEmptyString ;
877 }
878 m_lastTypeIn = event.GetTimestamp() ;
879 m_typeIn += (char) event.GetKeyCode() ;
880 int line = FindString(wxT("*")+m_typeIn+wxT("*")) ;
881 if ( line >= 0 )
882 {
883 if ( GetSelection() != line )
884 {
885 SetSelection(line) ;
886 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
887 event.SetEventObject( this );
888
889 if ( HasClientObjectData() )
890 event.SetClientObject( GetClientObject( line ) );
891 else if ( HasClientUntypedData() )
892 event.SetClientData( GetClientData(line) );
893 event.SetString( GetString(line) );
894
895 event.SetInt(line);
896
897 GetEventHandler()->ProcessEvent(event);
898 }
899 }
900 }
901 }
902
903 #endif // !TARGET_API_MAC_OSX
904