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