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