]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/listbox.cpp
Instead of m_menuBar use GetMenuBar() which will be modified (with next patch)
[wxWidgets.git] / src / mac / carbon / listbox.cpp
CommitLineData
e9576ca5
SC
1///////////////////////////////////////////////////////////////////////////////
2// Name: listbox.cpp
3// Purpose: wxListBox
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
e9576ca5
SC
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "listbox.h"
14#endif
15
03e11df5 16#include "wx/app.h"
e9576ca5 17#include "wx/listbox.h"
dc0ace7c 18#include "wx/button.h"
e9576ca5 19#include "wx/settings.h"
422644a3 20#include "wx/toplevel.h"
e9576ca5
SC
21#include "wx/dynarray.h"
22#include "wx/log.h"
23
519cb848 24#include "wx/utils.h"
519cb848 25
2f1ae414 26#if !USE_SHARED_LIBRARY
e40298d5 27IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
519cb848
SC
28
29BEGIN_EVENT_TABLE(wxListBox, wxControl)
facd6764 30#if !TARGET_API_MAC_OSX
e40298d5
JS
31 EVT_SIZE( wxListBox::OnSize )
32 EVT_CHAR( wxListBox::OnChar )
facd6764 33#endif
519cb848 34END_EVENT_TABLE()
2f1ae414 35#endif
e9576ca5 36
facd6764
SC
37#include "wx/mac/uma.h"
38
39#if TARGET_API_MAC_OSX
40
41// new databrowserbased version
42
43// Listbox item
44wxListBox::wxListBox()
45{
46 m_noItems = 0;
47 m_selected = 0;
48 m_macList = NULL ;
49}
50
51bool wxListBox::Create(wxWindow *parent, wxWindowID id,
52 const wxPoint& pos,
53 const wxSize& size,
54 const wxArrayString& choices,
55 long style,
56 const wxValidator& validator,
57 const wxString& name)
58{
59 wxCArrayString chs(choices);
60
61 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
62 style, validator, name);
63}
64
65static pascal OSStatus ListBoxGetSetItemData(ControlRef browser,
66 DataBrowserItemID itemID, DataBrowserPropertyID property,
67 DataBrowserItemDataRef itemData, Boolean changeValue)
68{
69 OSStatus err = errDataBrowserPropertyNotSupported;
70
71 if ( ! changeValue )
72 {
73 switch (property)
74 {
75
76 case 1024:
77 {
78 long ref = GetControlReference( browser ) ;
79 if ( ref )
80 {
81 wxListBox* list = wxDynamicCast( ref , wxListBox ) ;
82 for ( size_t i = 0 ; i < list->m_idArray.GetCount() ; ++i )
83 if ( list->m_idArray[i] == (long) itemID )
84 {
85 wxMacCFStringHolder cf( list->GetString(i) , list->GetFont().GetEncoding() ) ;
86 verify_noerr( ::SetDataBrowserItemDataText( itemData , cf ) ) ;
87 err = noErr ;
88 break ;
89 }
90 }
91 }
92 break;
93
94 default:
95
96 break;
97 }
98 }
99
100 return err;
101}
102bool wxListBox::Create(wxWindow *parent, wxWindowID id,
103 const wxPoint& pos,
104 const wxSize& size,
105 int n, const wxString choices[],
106 long style,
107 const wxValidator& validator,
108 const wxString& name)
109{
110 m_macIsUserPane = FALSE ;
111
112 if ( !wxListBoxBase::Create(parent, id, pos, size, style & ~(wxHSCROLL|wxVSCROLL), validator, name) )
113 return false;
114
115 m_noItems = 0 ; // this will be increased by our append command
116 m_selected = 0;
117 m_nextId = 1 ;
118
119
120 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
121 ControlRef browser ;
122
123 verify_noerr( ::CreateDataBrowserControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds, kDataBrowserListView , (ControlRef *)&m_macControl ) );
124 browser = (ControlRef) m_macControl ;
125
126 DataBrowserSelectionFlags options = kDataBrowserDragSelect ;
127 if ( style & wxLB_MULTIPLE )
128 {
129 options += kDataBrowserAlwaysExtendSelection + kDataBrowserCmdTogglesSelection ;
130 }
131 else if ( style & wxLB_EXTENDED )
132 {
133 // default behaviour
134 }
135 else
136 {
137 options += kDataBrowserSelectOnlyOne ;
138 }
139 verify_noerr(SetDataBrowserSelectionFlags (browser, options ) );
140
141 DataBrowserListViewColumnDesc columnDesc ;
142 columnDesc.headerBtnDesc.titleOffset = 0;
143 columnDesc.headerBtnDesc.version = kDataBrowserListViewLatestHeaderDesc;
144
145 columnDesc.headerBtnDesc.btnFontStyle.flags =
146 kControlUseFontMask | kControlUseJustMask;
147
148 columnDesc.headerBtnDesc.btnContentInfo.contentType = kControlNoContent;
149 columnDesc.propertyDesc.propertyType = kDataBrowserTextType;
150 columnDesc.headerBtnDesc.btnFontStyle.just = teFlushDefault;
151 columnDesc.headerBtnDesc.minimumWidth = 0;
152 columnDesc.headerBtnDesc.maximumWidth = 10000;
153
154 columnDesc.headerBtnDesc.btnFontStyle.font = kControlFontViewSystemFont;
155 columnDesc.headerBtnDesc.btnFontStyle.style = normal;
156 columnDesc.headerBtnDesc.titleString = NULL ; // CFSTR( "" );
157
158 columnDesc.propertyDesc.propertyID = 1024;
159 columnDesc.propertyDesc.propertyType = kDataBrowserTextType;
c2697b87
SC
160 columnDesc.propertyDesc.propertyFlags =
161#ifdef MAC_OS_X_VERSION_10_3
162 kDataBrowserListViewTypeSelectColumn |
163#endif
164 kDataBrowserTableViewSelectionColumn ;
facd6764
SC
165
166
167 verify_noerr(::AddDataBrowserListViewColumn(browser, &columnDesc, kDataBrowserListViewAppendColumn) ) ;
168 verify_noerr(::AutoSizeDataBrowserListViewColumns( browser ) ) ;
169 verify_noerr(::SetDataBrowserHasScrollBars( browser , false , true ) ) ;
170 verify_noerr(::SetDataBrowserTableViewHiliteStyle( browser, kDataBrowserTableViewFillHilite ) ) ;
171 verify_noerr(::SetDataBrowserListViewHeaderBtnHeight( browser , 0 ) ) ;
172 DataBrowserCallbacks callbacks ;
173
174 callbacks.version = kDataBrowserLatestCallbacks;
175
176 InitDataBrowserCallbacks(&callbacks);
177
178 callbacks.u.v1.itemDataCallback =
179 NewDataBrowserItemDataUPP(ListBoxGetSetItemData);
180
181 SetDataBrowserCallbacks(browser, &callbacks);
182
183 MacPostControlCreate(pos,size) ;
184
185 for ( int i = 0 ; i < n ; i++ )
186 {
187 Append( choices[i] ) ;
188 }
189
190 return TRUE;
191}
192
193wxListBox::~wxListBox()
194{
195 SetControlReference( (ControlRef) m_macControl , NULL ) ;
196 FreeData() ;
197 // avoid access during destruction
198 if ( m_macList )
199 {
200 m_macList = NULL ;
201 }
202}
203
204void wxListBox::FreeData()
205{
206#if wxUSE_OWNER_DRAWN
207 if ( m_windowStyle & wxLB_OWNERDRAW )
208 {
209 size_t uiCount = m_aItems.Count();
210 while ( uiCount-- != 0 ) {
211 delete m_aItems[uiCount];
212 m_aItems[uiCount] = NULL;
213 }
214
215 m_aItems.Clear();
216 }
217 else
218#endif // wxUSE_OWNER_DRAWN
219 if ( HasClientObjectData() )
220 {
221 for ( size_t n = 0; n < (size_t)m_noItems; n++ )
222 {
223 delete GetClientObject(n);
224 }
225 }
226}
227
228void wxListBox::DoSetSize(int x, int y,
229 int width, int height,
230 int sizeFlags )
231{
232 wxControl::DoSetSize( x , y , width , height , sizeFlags ) ;
233}
234
235void wxListBox::DoSetFirstItem(int N)
236{
237 MacScrollTo( N ) ;
238}
239
240void wxListBox::Delete(int N)
241{
242 wxCHECK_RET( N >= 0 && N < m_noItems,
243 wxT("invalid index in wxListBox::Delete") );
244
245#if wxUSE_OWNER_DRAWN
246 delete m_aItems[N];
247 m_aItems.RemoveAt(N);
248#else // !wxUSE_OWNER_DRAWN
249 if ( HasClientObjectData() )
250 {
251 delete GetClientObject(N);
252 }
253#endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
254 m_stringArray.RemoveAt( N ) ;
255 m_dataArray.RemoveAt( N ) ;
256 m_noItems --;
257
258 MacDelete( N ) ;
259}
260
261int wxListBox::DoAppend(const wxString& item)
262{
263 int index = m_noItems ;
264 m_stringArray.Add( item ) ;
265 m_dataArray.Add( NULL );
266 m_noItems ++;
267 DoSetItemClientData( index , NULL ) ;
268 MacAppend( item ) ;
269
270 return index ;
271}
272
273void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
274{
275 Clear() ;
276 int n = choices.GetCount();
277
278 for( int i = 0 ; i < n ; ++i )
279 {
280 if ( clientData )
281 {
282#if wxUSE_OWNER_DRAWN
283 wxASSERT_MSG(clientData[i] == NULL,
284 wxT("Can't use client data with owner-drawn listboxes"));
285#else // !wxUSE_OWNER_DRAWN
286 Append( choices[i] , clientData[i] ) ;
287#endif
288 }
289 else
290 Append( choices[i] ) ;
291 }
292
293#if wxUSE_OWNER_DRAWN
294 if ( m_windowStyle & wxLB_OWNERDRAW ) {
295 // first delete old items
296 size_t ui = m_aItems.Count();
297 while ( ui-- != 0 ) {
298 delete m_aItems[ui];
299 m_aItems[ui] = NULL;
300 }
301 m_aItems.Empty();
302
303 // then create new ones
304 for ( ui = 0; ui < (size_t)m_noItems; ui++ ) {
305 wxOwnerDrawn *pNewItem = CreateItem(ui);
306 pNewItem->SetName(choices[ui]);
307 m_aItems.Add(pNewItem);
308 }
309 }
310#endif // wxUSE_OWNER_DRAWN
311}
312
313bool wxListBox::HasMultipleSelection() const
314{
315 return (m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED);
316}
317
318int wxListBox::FindString(const wxString& s) const
319{
320
321 if ( s.Right(1) == wxT("*") )
322 {
323 wxString search = s.Left( s.Length() - 1 ) ;
324 int len = search.Length() ;
325 Str255 s1 , s2 ;
326 wxMacStringToPascal( search , s2 ) ;
327
328 for ( int i = 0 ; i < m_noItems ; ++ i )
329 {
330 wxMacStringToPascal( m_stringArray[i].Left( len ) , s1 ) ;
331
332 if ( EqualString( s1 , s2 , false , false ) )
333 return i ;
334 }
335 if ( s.Left(1) == wxT("*") && s.Length() > 1 )
336 {
337 wxString st = s ;
338 st.MakeLower() ;
339 for ( int i = 0 ; i < m_noItems ; ++i )
340 {
341 if ( GetString(i).Lower().Matches(st) )
342 return i ;
343 }
344 }
345
346 }
347 else
348 {
349 Str255 s1 , s2 ;
350
351 wxMacStringToPascal( s , s2 ) ;
352
353 for ( int i = 0 ; i < m_noItems ; ++ i )
354 {
355 wxMacStringToPascal( m_stringArray[i] , s1 ) ;
356
357 if ( EqualString( s1 , s2 , false , false ) )
358 return i ;
359 }
360 }
361 return -1;
362}
363
364void wxListBox::Clear()
365{
366 FreeData();
367 m_noItems = 0;
368 m_stringArray.Empty() ;
369 m_dataArray.Empty() ;
370 MacClear() ;
371}
372
373void wxListBox::SetSelection(int N, bool select)
374{
375 wxCHECK_RET( N >= 0 && N < m_noItems,
376 wxT("invalid index in wxListBox::SetSelection") );
377 MacSetSelection( N , select ) ;
378 GetSelections( m_selectionPreImage ) ;
379}
380
381bool wxListBox::IsSelected(int N) const
382{
383 wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE,
384 wxT("invalid index in wxListBox::Selected") );
385
386 return MacIsSelected( N ) ;
387}
388
389void *wxListBox::DoGetItemClientData(int N) const
390{
391 wxCHECK_MSG( N >= 0 && N < m_noItems, NULL,
392 wxT("invalid index in wxListBox::GetClientData"));
393
394 return (void *)m_dataArray[N];
395}
396
397wxClientData *wxListBox::DoGetItemClientObject(int N) const
398{
399 return (wxClientData *) DoGetItemClientData( N ) ;
400}
401
402void wxListBox::DoSetItemClientData(int N, void *Client_data)
403{
404 wxCHECK_RET( N >= 0 && N < m_noItems,
405 wxT("invalid index in wxListBox::SetClientData") );
406
407#if wxUSE_OWNER_DRAWN
408 if ( m_windowStyle & wxLB_OWNERDRAW )
409 {
410 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
411 // in OnMeasure/OnDraw.
412 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
413 }
414#endif // wxUSE_OWNER_DRAWN
415 wxASSERT_MSG( m_dataArray.GetCount() >= (size_t) N , wxT("invalid client_data array") ) ;
416
417 if ( m_dataArray.GetCount() > (size_t) N )
418 {
419 m_dataArray[N] = (char*) Client_data ;
420 }
421 else
422 {
423 m_dataArray.Add( (char*) Client_data ) ;
424 }
425}
426
427void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
428{
429 DoSetItemClientData(n, clientData);
430}
431
432// Return number of selections and an array of selected integers
433int wxListBox::GetSelections(wxArrayInt& aSelections) const
434{
435 return MacGetSelections( aSelections ) ;
436}
437
438// Get single selection, for single choice list items
439int wxListBox::GetSelection() const
440{
441 return MacGetSelection() ;
442}
443
444// Find string for position
445wxString wxListBox::GetString(int N) const
446{
447 return m_stringArray[N] ;
448}
449
450void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
451{
452 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
453 wxT("invalid index in wxListBox::InsertItems") );
454
455 int nItems = items.GetCount();
456
457 for ( int i = 0 ; i < nItems ; i++ )
458 {
459 m_stringArray.Insert( items[i] , pos + i ) ;
460 m_dataArray.Insert( NULL , pos + i ) ;
461 MacInsert( pos + i , items[i] ) ;
462 }
463
464 m_noItems += nItems;
465}
466
467void wxListBox::SetString(int N, const wxString& s)
468{
469 m_stringArray[N] = s ;
470 MacSet( N , s ) ;
471}
472
473wxSize wxListBox::DoGetBestSize() const
474{
475 int lbWidth = 100; // some defaults
476 int lbHeight = 110;
477 int wLine;
478
479 {
480 wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetTopLevelWindowRef() ) ) ;
481
482 if ( m_font.Ok() )
483 {
484 ::TextFont( m_font.MacGetFontNum() ) ;
485 ::TextSize( m_font.MacGetFontSize() ) ;
486 ::TextFace( m_font.MacGetFontStyle() ) ;
487 }
488 else
489 {
490 ::TextFont( kFontIDMonaco ) ;
491 ::TextSize( 9 );
492 ::TextFace( 0 ) ;
493 }
494
495 // Find the widest line
496 for(int i = 0; i < GetCount(); i++) {
497 wxString str(GetString(i));
498 #if wxUSE_UNICODE
499 Point bounds={0,0} ;
500 SInt16 baseline ;
501 ::GetThemeTextDimensions( wxMacCFStringHolder( str , m_font.GetEncoding() ) ,
502 kThemeCurrentPortFont,
503 kThemeStateActive,
504 false,
505 &bounds,
506 &baseline );
507 wLine = bounds.h ;
508 #else
509 wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ;
510 #endif
511 lbWidth = wxMax(lbWidth, wLine);
512 }
513
514 // Add room for the scrollbar
515 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
516
517 // And just a bit more
518 int cy = 12 ;
519 int cx = ::TextWidth( "X" , 0 , 1 ) ;
520 lbWidth += cx ;
521
522 // don't make the listbox too tall (limit height to around 10 items) but don't
523 // make it too small neither
524 lbHeight = (cy+4) * wxMin(wxMax(GetCount(), 3), 10);
525 }
526
527 return wxSize(lbWidth, lbHeight);
528}
529
530int wxListBox::GetCount() const
531{
532 return m_noItems;
533}
534
535void wxListBox::Refresh(bool eraseBack, const wxRect *rect)
536{
537 wxControl::Refresh( eraseBack , rect ) ;
538 // MacRedrawControl() ;
539}
540
541#if wxUSE_OWNER_DRAWN
542
543class wxListBoxItem : public wxOwnerDrawn
544{
545public:
546 wxListBoxItem(const wxString& str = "");
547};
548
549wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE)
550{
551 // no bitmaps/checkmarks
552 SetMarginWidth(0);
553}
554
555wxOwnerDrawn *wxListBox::CreateItem(size_t n)
556{
557 return new wxListBoxItem();
558}
559
560#endif //USE_OWNER_DRAWN
561
562// ============================================================================
563// list box control implementation
564// ============================================================================
565
566void wxListBox::MacDelete( int N )
567{
568 UInt32 id = m_idArray[N] ;
569 verify_noerr(::RemoveDataBrowserItems((ControlRef) m_macControl , kDataBrowserNoItem , 1 , (UInt32*) &id , kDataBrowserItemNoProperty ) ) ;
570 m_idArray.RemoveAt( N ) ;
571}
572
573void wxListBox::MacInsert( int n , const wxString& text)
574{
575 verify_noerr(::AddDataBrowserItems( (ControlRef) m_macControl , kDataBrowserNoItem , 1 , (UInt32*) &m_nextId , kDataBrowserItemNoProperty ) ) ;
576 m_idArray.Insert( m_nextId , n ) ;
577 ++m_nextId ;
578}
579
580void wxListBox::MacAppend( const wxString& text)
581{
582 verify_noerr(::AddDataBrowserItems( (ControlRef) m_macControl , kDataBrowserNoItem , 1 , (UInt32*) &m_nextId , kDataBrowserItemNoProperty ) ) ;
583 m_idArray.Add( m_nextId ) ;
584 ++m_nextId ;
585}
586
587void wxListBox::MacClear()
588{
589 verify_noerr(::RemoveDataBrowserItems((ControlRef) m_macControl , kDataBrowserNoItem , 0 , NULL , kDataBrowserItemNoProperty ) ) ;
590 m_dataArray.Empty() ;
591}
592
593void wxListBox::MacSetSelection( int n , bool select )
594{
595 UInt32 id = m_idArray[n] ;
596 if ( ::IsDataBrowserItemSelected( (ControlRef) m_macControl , id ) != select )
597 {
598 verify_noerr(::SetDataBrowserSelectedItems((ControlRef) m_macControl , 1 , & id , kDataBrowserItemsToggle ) ) ;
599 }
600 MacScrollTo( n ) ;
601}
602
603bool wxListBox::MacIsSelected( int n ) const
604{
605 return ::IsDataBrowserItemSelected( (ControlRef) m_macControl , m_idArray[n] ) ;
606}
607
608int wxListBox::MacGetSelection() const
609{
610 for ( size_t i = 0 ; i < m_idArray.GetCount() ; ++i )
611 {
612 if ( ::IsDataBrowserItemSelected((ControlRef) m_macControl , m_idArray[i] ) )
613 {
614 return i ;
615 }
616 }
617 return -1 ;
618}
619
620int wxListBox::MacGetSelections( wxArrayInt& aSelections ) const
621{
622 int no_sel = 0 ;
623
624 aSelections.Empty();
625 for ( size_t i = 0 ; i < m_idArray.GetCount() ; ++i )
626 {
627 if ( ::IsDataBrowserItemSelected((ControlRef) m_macControl , m_idArray[i] ) )
628 {
629 aSelections.Add( i ) ;
630 no_sel++ ;
631 }
632 }
633 return no_sel ;
634}
519cb848 635
facd6764
SC
636void wxListBox::MacSet( int n , const wxString& text )
637{
638 // as we don't store the strings we only have to issue a redraw
639 UInt32 id = m_idArray[n] ;
640 verify_noerr( ::UpdateDataBrowserItems( (ControlRef) m_macControl , kDataBrowserNoItem , 1 , &id , kDataBrowserItemNoProperty , kDataBrowserItemNoProperty ) ) ;
641}
e42e45a9 642
facd6764
SC
643void wxListBox::MacScrollTo( int n )
644{
645 // TODO implement scrolling
646}
647
648void wxListBox::OnSize( wxSizeEvent &event)
649{
650}
651
652void wxListBox::MacHandleControlClick( WXWidget control , wxInt16 controlpart , bool WXUNUSED(mouseStillDown))
653{
654 Boolean wasDoubleClick = false ;
655 long result ;
656
657 ::GetControlData( (ControlRef) m_macControl , kControlNoPart , kControlListBoxDoubleClickTag , sizeof( wasDoubleClick ) , (char*) &wasDoubleClick , &result ) ;
658 if ( !wasDoubleClick )
659 {
660 MacDoClick() ;
661 }
662 else
663 {
664 MacDoDoubleClick() ;
665 }
666}
667
668void wxListBox::MacSetRedraw( bool doDraw )
669{
670 // nothing to do in compositing mode
671}
672
673void wxListBox::MacDoClick()
674{
675 wxArrayInt aSelections;
676 int n ;
677 size_t count = GetSelections(aSelections);
678
679 if ( count == m_selectionPreImage.GetCount() )
680 {
681 bool hasChanged = false ;
682 for ( size_t i = 0 ; i < count ; ++i )
683 {
684 if ( aSelections[i] != m_selectionPreImage[i] )
685 {
686 hasChanged = true ;
687 break ;
688 }
689 }
690 if ( !hasChanged )
691 {
692 return ;
693 }
694 }
695
696 m_selectionPreImage = aSelections;
697
698 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
699 event.SetEventObject( this );
700
701 if ( count > 0 )
702 {
703 n = aSelections[0];
704 if ( HasClientObjectData() )
705 event.SetClientObject( GetClientObject(n) );
706 else if ( HasClientUntypedData() )
707 event.SetClientData( GetClientData(n) );
708 event.SetString( GetString(n) );
709 }
710 else
711 {
712 n = -1;
713 }
714
715 event.m_commandInt = n;
716
717 GetEventHandler()->ProcessEvent(event);
718}
719
720void wxListBox::MacDoDoubleClick()
721{
722 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId);
723 event.SetEventObject( this );
724 GetEventHandler()->ProcessEvent(event) ;
725}
726
727void wxListBox::OnChar(wxKeyEvent& event)
728{
729 // todo trigger proper events here
730 event.Skip() ;
731 return ;
732
733 if ( event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER)
734 {
735 wxWindow* parent = GetParent() ;
736 while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL )
737 parent = parent->GetParent() ;
738
739 if ( parent && parent->GetDefaultItem() )
740 {
741 wxButton *def = wxDynamicCast(parent->GetDefaultItem(),
742 wxButton);
743 if ( def && def->IsEnabled() )
744 {
745 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
746 event.SetEventObject(def);
747 def->Command(event);
748 return ;
749 }
750 }
751 event.Skip() ;
752 }
753 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
754 else if (event.GetKeyCode() == WXK_ESCAPE || (event.GetKeyCode() == '.' && event.MetaDown() ) )
755 {
756 // FIXME: look in ancestors, not just parent.
757 wxWindow* win = GetParent()->FindWindow( wxID_CANCEL ) ;
758 if (win)
759 {
760 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
761 new_event.SetEventObject( win );
762 win->GetEventHandler()->ProcessEvent( new_event );
763 }
764 }
765 else if ( event.GetKeyCode() == WXK_TAB )
766 {
767 wxNavigationKeyEvent new_event;
768 new_event.SetEventObject( this );
769 new_event.SetDirection( !event.ShiftDown() );
770 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
771 new_event.SetWindowChange( event.ControlDown() );
772 new_event.SetCurrentFocus( this );
773 if ( !GetEventHandler()->ProcessEvent( new_event ) )
774 event.Skip() ;
775 }
776 else if ( event.GetKeyCode() == WXK_DOWN || event.GetKeyCode() == WXK_UP )
777 {
778 // perform the default key handling first
779 wxControl::OnKeyDown( event ) ;
780
781 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
782 event.SetEventObject( this );
783
784 wxArrayInt aSelections;
785 int n, count = GetSelections(aSelections);
786 if ( count > 0 )
787 {
788 n = aSelections[0];
789 if ( HasClientObjectData() )
790 event.SetClientObject( GetClientObject(n) );
791 else if ( HasClientUntypedData() )
792 event.SetClientData( GetClientData(n) );
793 event.SetString( GetString(n) );
794 }
795 else
796 {
797 n = -1;
798 }
799
800 event.m_commandInt = n;
801
802 GetEventHandler()->ProcessEvent(event);
803 }
804 else
805 {
806 if ( event.GetTimestamp() > m_lastTypeIn + 60 )
807 {
808 m_typeIn = wxEmptyString ;
809 }
810 m_lastTypeIn = event.GetTimestamp() ;
811 m_typeIn += (char) event.GetKeyCode() ;
812 int line = FindString(wxT("*")+m_typeIn+wxT("*")) ;
813 if ( line >= 0 )
814 {
815 if ( GetSelection() != line )
816 {
817 SetSelection(line) ;
818 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
819 event.SetEventObject( this );
820
821 if ( HasClientObjectData() )
822 event.SetClientObject( GetClientObject( line ) );
823 else if ( HasClientUntypedData() )
824 event.SetClientData( GetClientData(line) );
825 event.SetString( GetString(line) );
826
827 event.m_commandInt = line ;
828
829 GetEventHandler()->ProcessEvent(event);
830 }
831 }
832 }
833}
573ac9dc 834
202848fe 835#else
facd6764
SC
836
837// old carbon version
838
839const short kwxMacListItemHeight = 19 ;
202848fe 840
e42e45a9
SC
841extern "C"
842{
843static pascal void wxMacListDefinition( short message, Boolean isSelected, Rect *drawRect,
a8e6bf8a
RR
844 Cell cell, short dataOffset, short dataLength,
845 ListHandle listHandle ) ;
e42e45a9
SC
846}
847
facd6764 848static pascal void wxMacListDefinition( short message, Boolean isSelected, Rect *d,
a8e6bf8a
RR
849 Cell cell, short dataOffset, short dataLength,
850 ListHandle listHandle )
851{
21956470 852 wxListBox* list;
facd6764
SC
853 Rect r = *d ;
854 Rect* drawRect = &r ;
855
856 list = (wxListBox*) GetControlReference( (ControlRef) GetListRefCon(listHandle) );
857 if ( list == NULL || list->GetHandle() == NULL || GetControlReference( ( ControlRef )list->GetHandle() ) == NULL )
21956470
SC
858 return ;
859
facd6764
SC
860 // the bounds passed in are not correct, adjust the right border
861 int x = 0 , y = 0 ;
862 Rect bounds ;
863 GetControlBounds( (ControlRef) list->GetHandle() , &bounds ) ;
864 r.right = r.left + (bounds.right - bounds.left ) - 16 ;
865
a8e6bf8a
RR
866 GrafPtr savePort;
867 GrafPtr grafPtr;
868 RgnHandle savedClipRegion;
869 SInt32 savedPenMode;
e40298d5
JS
870 GetPort(&savePort);
871 SetPort((**listHandle).port);
872 grafPtr = (**listHandle).port ;
a8e6bf8a 873 // typecast our refCon
e40298d5 874
a8e6bf8a 875 // Calculate the cell rect.
e40298d5 876
a8e6bf8a 877 switch( message ) {
e40298d5
JS
878 case lInitMsg:
879 break;
880
881 case lCloseMsg:
882 break;
883
884 case lDrawMsg:
a8e6bf8a 885 {
427ff662 886 const wxString linetext = list->m_stringArray[cell.v] ;
e40298d5 887
a8e6bf8a
RR
888 // Save the current clip region, and set the clip region to the area we are about
889 // to draw.
e40298d5 890
a8e6bf8a
RR
891 savedClipRegion = NewRgn();
892 GetClip( savedClipRegion );
facd6764 893
a8e6bf8a
RR
894 ClipRect( drawRect );
895 EraseRect( drawRect );
e40298d5 896
fcb35beb
VZ
897 const wxFont& font = list->GetFont();
898 if ( font.Ok() )
e40298d5 899 {
facd6764
SC
900 ::TextFont( font.MacGetFontNum() ) ;
901 ::TextSize( font.MacGetFontSize() ) ;
902 ::TextFace( font.MacGetFontStyle() ) ;
e40298d5
JS
903 }
904 else
905 {
906 ::TextFont( kFontIDMonaco ) ;
907 ::TextSize( 9 );
908 ::TextFace( 0 ) ;
909 }
910
427ff662
SC
911 {
912 Rect frame = { drawRect->top, drawRect->left + 4,
913 drawRect->top + kwxMacListItemHeight, drawRect->right + 10000 } ;
a9412f8f 914 CFMutableStringRef mString = CFStringCreateMutableCopy( NULL , 0 , wxMacCFStringHolder(linetext , list->GetFont().GetEncoding()) ) ;
427ff662 915 ::TruncateThemeText( mString , kThemeCurrentPortFont, kThemeStateActive, drawRect->right - drawRect->left , truncEnd , NULL ) ;
facd6764 916
427ff662
SC
917 ::DrawThemeTextBox( mString,
918 kThemeCurrentPortFont,
919 kThemeStateActive,
920 false,
921 &frame,
922 teJustLeft,
923 nil );
facd6764 924
427ff662
SC
925 CFRelease( mString ) ;
926 }
facd6764 927
a8e6bf8a
RR
928 // If the cell is hilited, do the hilite now. Paint the cell contents with the
929 // appropriate QuickDraw transform mode.
e40298d5 930
a8e6bf8a 931 if( isSelected ) {
76a5e5d2
SC
932 savedPenMode = GetPortPenMode( (CGrafPtr) grafPtr );
933 SetPortPenMode( (CGrafPtr)grafPtr, hilitetransfermode );
a8e6bf8a 934 PaintRect( drawRect );
76a5e5d2 935 SetPortPenMode( (CGrafPtr)grafPtr, savedPenMode );
a8e6bf8a 936 }
e40298d5 937
a8e6bf8a 938 // Restore the saved clip region.
e40298d5 939
a8e6bf8a
RR
940 SetClip( savedClipRegion );
941 DisposeRgn( savedClipRegion );
e40298d5
JS
942 }
943 break;
944 case lHiliteMsg:
945
946 // Hilite or unhilite the cell. Paint the cell contents with the
947 // appropriate QuickDraw transform mode.
948
949 GetPort( &grafPtr );
950 savedPenMode = GetPortPenMode( (CGrafPtr)grafPtr );
951 SetPortPenMode( (CGrafPtr)grafPtr, hilitetransfermode );
952 PaintRect( drawRect );
953 SetPortPenMode( (CGrafPtr)grafPtr, savedPenMode );
954 break;
955 default :
956 break ;
a8e6bf8a 957 }
dc0ace7c 958 SetPort(savePort);
e42e45a9
SC
959}
960
519cb848 961extern "C" void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon) ;
f81127c5 962// resources ldef ids
519cb848 963const short kwxMacListWithVerticalScrollbar = 128 ;
f81127c5 964const short kwxMacListWithVerticalAndHorizontalScrollbar = 129 ;
519cb848 965
e9576ca5
SC
966// ============================================================================
967// list box control implementation
968// ============================================================================
969
970// Listbox item
971wxListBox::wxListBox()
972{
973 m_noItems = 0;
974 m_selected = 0;
2f1ae414 975 m_macList = NULL ;
e9576ca5
SC
976}
977
e42e45a9
SC
978static ListDefUPP macListDefUPP = NULL ;
979
584ad2a3
MB
980bool wxListBox::Create(wxWindow *parent, wxWindowID id,
981 const wxPoint& pos,
982 const wxSize& size,
983 const wxArrayString& choices,
984 long style,
985 const wxValidator& validator,
986 const wxString& name)
987{
988 wxCArrayString chs(choices);
989
990 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
991 style, validator, name);
992}
993
e9576ca5
SC
994bool wxListBox::Create(wxWindow *parent, wxWindowID id,
995 const wxPoint& pos,
996 const wxSize& size,
997 int n, const wxString choices[],
998 long style,
999 const wxValidator& validator,
1000 const wxString& name)
1001{
facd6764
SC
1002 m_macIsUserPane = FALSE ;
1003
b657d5db 1004 if ( !wxListBoxBase::Create(parent, id, pos, size, style & ~(wxHSCROLL|wxVSCROLL), validator, name) )
b45ed7a2
VZ
1005 return false;
1006
60149370
GD
1007 m_noItems = 0 ; // this will be increased by our append command
1008 m_selected = 0;
dc0ace7c 1009
facd6764 1010 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
e40298d5 1011
60149370 1012 ListDefSpec listDef;
e42e45a9
SC
1013 listDef.defType = kListDefUserProcType;
1014 if ( macListDefUPP == NULL )
1015 {
e40298d5 1016 macListDefUPP = NewListDefUPP( wxMacListDefinition );
e42e45a9 1017 }
e40298d5
JS
1018 listDef.u.userProc = macListDefUPP ;
1019
2b5f62a0
VZ
1020 Str255 fontName ;
1021 SInt16 fontSize ;
1022 Style fontStyle ;
facd6764 1023
e40298d5 1024 GetThemeFont(kThemeViewsFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
facd6764 1025
427ff662 1026 SetFont( wxFont (fontSize, wxSWISS, wxNORMAL, wxNORMAL , false , wxMacMakeStringFromPascal( fontName ) ) ) ;
facd6764 1027
60149370 1028 Size asize;
519cb848 1029
519cb848 1030
facd6764 1031 CreateListBoxControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds, false, 0, 1, (style & wxLB_HSCROLL), true,
962cbf2e 1032 kwxMacListItemHeight, kwxMacListItemHeight, false, &listDef, (ControlRef *)&m_macControl );
519cb848 1033
facd6764 1034 GetControlData( (ControlRef) m_macControl, kControlNoPart, kControlListBoxListHandleTag,
60149370 1035 sizeof(ListHandle), (Ptr) &m_macList, &asize);
519cb848 1036
facd6764 1037 SetControlReference( (ControlRef) m_macControl, (long) this);
60149370 1038
dc0ace7c 1039
e42e45a9 1040 OptionBits options = 0;
60149370
GD
1041 if ( style & wxLB_MULTIPLE )
1042 {
09322209 1043 options += lExtendDrag + lUseSense ;
60149370
GD
1044 }
1045 else if ( style & wxLB_EXTENDED )
1046 {
09322209 1047 // default behaviour
60149370
GD
1048 }
1049 else
1050 {
2b5f62a0 1051 options = (OptionBits) lOnlyOne ;
60149370 1052 }
76a5e5d2 1053 SetListSelectionFlags((ListHandle)m_macList, options);
dc0ace7c 1054
60149370
GD
1055 for ( int i = 0 ; i < n ; i++ )
1056 {
a8e6bf8a 1057 Append( choices[i] ) ;
60149370 1058 }
dc0ace7c 1059
facd6764 1060 MacPostControlCreate(pos,size) ;
2b5f62a0 1061
76a5e5d2 1062 LSetDrawingMode( true , (ListHandle)m_macList ) ;
519cb848 1063
60149370 1064 return TRUE;
e9576ca5
SC
1065}
1066
1067wxListBox::~wxListBox()
1068{
facd6764 1069 SetControlReference( (ControlRef) m_macControl , NULL ) ;
4b651a46 1070 FreeData() ;
21956470 1071 // avoid access during destruction
a8e6bf8a
RR
1072 if ( m_macList )
1073 {
a8e6bf8a
RR
1074 m_macList = NULL ;
1075 }
e9576ca5
SC
1076}
1077
4b651a46 1078void wxListBox::FreeData()
e9576ca5 1079{
e7549107
SC
1080#if wxUSE_OWNER_DRAWN
1081 if ( m_windowStyle & wxLB_OWNERDRAW )
1082 {
1083 size_t uiCount = m_aItems.Count();
1084 while ( uiCount-- != 0 ) {
1085 delete m_aItems[uiCount];
f5bb2251 1086 m_aItems[uiCount] = NULL;
e7549107
SC
1087 }
1088
1089 m_aItems.Clear();
1090 }
1091 else
1092#endif // wxUSE_OWNER_DRAWN
1093 if ( HasClientObjectData() )
1094 {
1095 for ( size_t n = 0; n < (size_t)m_noItems; n++ )
1096 {
1097 delete GetClientObject(n);
1098 }
1099 }
e9576ca5
SC
1100}
1101
8614041b
SC
1102void wxListBox::DoSetSize(int x, int y,
1103 int width, int height,
1104 int sizeFlags )
1105{
a8e6bf8a 1106 wxControl::DoSetSize( x , y , width , height , sizeFlags ) ;
8614041b 1107#if TARGET_CARBON
a8e6bf8a 1108 Rect bounds ;
facd6764 1109 GetControlBounds( (ControlRef) m_macControl , &bounds ) ;
962cbf2e 1110 ControlRef control = GetListVerticalScrollBar( (ListHandle)m_macList ) ;
a8e6bf8a
RR
1111 if ( control )
1112 {
1113 Rect scrollbounds ;
1114 GetControlBounds( control , &scrollbounds ) ;
1115 if( scrollbounds.right != bounds.right + 1 )
1116 {
dc0ace7c 1117 UMAMoveControl( control , bounds.right - (scrollbounds.right - scrollbounds.left) + 1 ,
a8e6bf8a
RR
1118 scrollbounds.top ) ;
1119 }
1120 }
8614041b
SC
1121#endif
1122}
e7549107 1123void wxListBox::DoSetFirstItem(int N)
e9576ca5 1124{
a8e6bf8a 1125 MacScrollTo( N ) ;
e9576ca5
SC
1126}
1127
1128void wxListBox::Delete(int N)
1129{
e7549107
SC
1130 wxCHECK_RET( N >= 0 && N < m_noItems,
1131 wxT("invalid index in wxListBox::Delete") );
1132
1133#if wxUSE_OWNER_DRAWN
1134 delete m_aItems[N];
0baac61e 1135 m_aItems.RemoveAt(N);
e7549107
SC
1136#else // !wxUSE_OWNER_DRAWN
1137 if ( HasClientObjectData() )
1138 {
1139 delete GetClientObject(N);
1140 }
1141#endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
ecaf6c18 1142 m_stringArray.RemoveAt( N ) ;
a8e6bf8a
RR
1143 m_dataArray.RemoveAt( N ) ;
1144 m_noItems --;
dc0ace7c 1145
a8e6bf8a 1146 MacDelete( N ) ;
e9576ca5
SC
1147}
1148
e7549107 1149int wxListBox::DoAppend(const wxString& item)
e9576ca5 1150{
a8e6bf8a 1151 int index = m_noItems ;
427ff662
SC
1152 m_stringArray.Add( item ) ;
1153 m_dataArray.Add( NULL );
a8e6bf8a
RR
1154 m_noItems ++;
1155 DoSetItemClientData( index , NULL ) ;
1156 MacAppend( item ) ;
e7549107 1157
a8e6bf8a 1158 return index ;
e9576ca5
SC
1159}
1160
e7549107 1161void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
dc0ace7c 1162{
e40298d5
JS
1163 MacSetRedraw( false ) ;
1164 Clear() ;
1165 int n = choices.GetCount();
1166
1167 for( int i = 0 ; i < n ; ++i )
a8e6bf8a 1168 {
e40298d5
JS
1169 if ( clientData )
1170 {
e7549107
SC
1171#if wxUSE_OWNER_DRAWN
1172 wxASSERT_MSG(clientData[i] == NULL,
e40298d5 1173 wxT("Can't use client data with owner-drawn listboxes"));
e7549107 1174#else // !wxUSE_OWNER_DRAWN
e40298d5
JS
1175 Append( choices[i] , clientData[i] ) ;
1176#endif
1177 }
1178 else
1179 Append( choices[i] ) ;
a8e6bf8a 1180 }
e40298d5 1181
e7549107
SC
1182#if wxUSE_OWNER_DRAWN
1183 if ( m_windowStyle & wxLB_OWNERDRAW ) {
1184 // first delete old items
1185 size_t ui = m_aItems.Count();
1186 while ( ui-- != 0 ) {
1187 delete m_aItems[ui];
f5bb2251 1188 m_aItems[ui] = NULL;
e7549107
SC
1189 }
1190 m_aItems.Empty();
e40298d5 1191
e7549107
SC
1192 // then create new ones
1193 for ( ui = 0; ui < (size_t)m_noItems; ui++ ) {
1194 wxOwnerDrawn *pNewItem = CreateItem(ui);
1195 pNewItem->SetName(choices[ui]);
1196 m_aItems.Add(pNewItem);
1197 }
1198 }
1199#endif // wxUSE_OWNER_DRAWN
e40298d5 1200 MacSetRedraw( true ) ;
e7549107
SC
1201}
1202
1203bool wxListBox::HasMultipleSelection() const
1204{
1205 return (m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED);
e9576ca5
SC
1206}
1207
427ff662 1208int wxListBox::FindString(const wxString& s) const
e9576ca5 1209{
e40298d5 1210
427ff662 1211 if ( s.Right(1) == wxT("*") )
a8e6bf8a
RR
1212 {
1213 wxString search = s.Left( s.Length() - 1 ) ;
1214 int len = search.Length() ;
1215 Str255 s1 , s2 ;
427ff662 1216 wxMacStringToPascal( search , s2 ) ;
e40298d5 1217
a8e6bf8a
RR
1218 for ( int i = 0 ; i < m_noItems ; ++ i )
1219 {
427ff662
SC
1220 wxMacStringToPascal( m_stringArray[i].Left( len ) , s1 ) ;
1221
a8e6bf8a
RR
1222 if ( EqualString( s1 , s2 , false , false ) )
1223 return i ;
1224 }
427ff662 1225 if ( s.Left(1) == wxT("*") && s.Length() > 1 )
a8e6bf8a 1226 {
427ff662
SC
1227 wxString st = s ;
1228 st.MakeLower() ;
a8e6bf8a
RR
1229 for ( int i = 0 ; i < m_noItems ; ++i )
1230 {
427ff662 1231 if ( GetString(i).Lower().Matches(st) )
a8e6bf8a
RR
1232 return i ;
1233 }
dc0ace7c 1234 }
e40298d5 1235
a8e6bf8a
RR
1236 }
1237 else
1238 {
1239 Str255 s1 , s2 ;
e40298d5 1240
427ff662 1241 wxMacStringToPascal( s , s2 ) ;
e40298d5 1242
a8e6bf8a
RR
1243 for ( int i = 0 ; i < m_noItems ; ++ i )
1244 {
427ff662
SC
1245 wxMacStringToPascal( m_stringArray[i] , s1 ) ;
1246
a8e6bf8a
RR
1247 if ( EqualString( s1 , s2 , false , false ) )
1248 return i ;
1249 }
e40298d5
JS
1250 }
1251 return -1;
e9576ca5
SC
1252}
1253
1254void wxListBox::Clear()
1255{
e40298d5
JS
1256 FreeData();
1257 m_noItems = 0;
1258 m_stringArray.Empty() ;
1259 m_dataArray.Empty() ;
1260 MacClear() ;
e9576ca5
SC
1261}
1262
1263void wxListBox::SetSelection(int N, bool select)
1264{
519cb848 1265 wxCHECK_RET( N >= 0 && N < m_noItems,
427ff662 1266 wxT("invalid index in wxListBox::SetSelection") );
e40298d5
JS
1267 MacSetSelection( N , select ) ;
1268 GetSelections( m_selectionPreImage ) ;
e9576ca5
SC
1269}
1270
e7549107 1271bool wxListBox::IsSelected(int N) const
e9576ca5 1272{
519cb848 1273 wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE,
427ff662 1274 wxT("invalid index in wxListBox::Selected") );
e40298d5
JS
1275
1276 return MacIsSelected( N ) ;
e9576ca5
SC
1277}
1278
e7549107 1279void *wxListBox::DoGetItemClientData(int N) const
e9576ca5 1280{
519cb848 1281 wxCHECK_MSG( N >= 0 && N < m_noItems, NULL,
e40298d5
JS
1282 wxT("invalid index in wxListBox::GetClientData"));
1283
e7549107 1284 return (void *)m_dataArray[N];
e9576ca5
SC
1285}
1286
51abe921
SC
1287wxClientData *wxListBox::DoGetItemClientObject(int N) const
1288{
a8e6bf8a 1289 return (wxClientData *) DoGetItemClientData( N ) ;
51abe921
SC
1290}
1291
e7549107 1292void wxListBox::DoSetItemClientData(int N, void *Client_data)
e9576ca5 1293{
519cb848 1294 wxCHECK_RET( N >= 0 && N < m_noItems,
427ff662 1295 wxT("invalid index in wxListBox::SetClientData") );
e40298d5 1296
e7549107
SC
1297#if wxUSE_OWNER_DRAWN
1298 if ( m_windowStyle & wxLB_OWNERDRAW )
1299 {
1300 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
1301 // in OnMeasure/OnDraw.
1302 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
1303 }
1304#endif // wxUSE_OWNER_DRAWN
427ff662 1305 wxASSERT_MSG( m_dataArray.GetCount() >= (size_t) N , wxT("invalid client_data array") ) ;
e40298d5 1306
68a9d9d0 1307 if ( m_dataArray.GetCount() > (size_t) N )
a8e6bf8a
RR
1308 {
1309 m_dataArray[N] = (char*) Client_data ;
2f1ae414 1310 }
8208e181
SC
1311 else
1312 {
a8e6bf8a 1313 m_dataArray.Add( (char*) Client_data ) ;
8208e181 1314 }
e7549107
SC
1315}
1316
1317void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
1318{
1319 DoSetItemClientData(n, clientData);
e9576ca5
SC
1320}
1321
1322// Return number of selections and an array of selected integers
1323int wxListBox::GetSelections(wxArrayInt& aSelections) const
1324{
a8e6bf8a 1325 return MacGetSelections( aSelections ) ;
e9576ca5
SC
1326}
1327
1328// Get single selection, for single choice list items
1329int wxListBox::GetSelection() const
1330{
a8e6bf8a 1331 return MacGetSelection() ;
e9576ca5
SC
1332}
1333
1334// Find string for position
1335wxString wxListBox::GetString(int N) const
1336{
427ff662 1337 return m_stringArray[N] ;
e9576ca5
SC
1338}
1339
e7549107 1340void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
e9576ca5 1341{
e7549107 1342 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
e40298d5
JS
1343 wxT("invalid index in wxListBox::InsertItems") );
1344
e7549107 1345 int nItems = items.GetCount();
e40298d5 1346
a8e6bf8a
RR
1347 for ( int i = 0 ; i < nItems ; i++ )
1348 {
1349 m_stringArray.Insert( items[i] , pos + i ) ;
1350 m_dataArray.Insert( NULL , pos + i ) ;
1351 MacInsert( pos + i , items[i] ) ;
1352 }
e40298d5 1353
519cb848 1354 m_noItems += nItems;
e9576ca5
SC
1355}
1356
1357void wxListBox::SetString(int N, const wxString& s)
1358{
427ff662 1359 m_stringArray[N] = s ;
a8e6bf8a 1360 MacSet( N , s ) ;
e9576ca5
SC
1361}
1362
37e2cb08 1363wxSize wxListBox::DoGetBestSize() const
e9576ca5 1364{
2b5f62a0
VZ
1365 int lbWidth = 100; // some defaults
1366 int lbHeight = 110;
1367 int wLine;
facd6764 1368
e40298d5 1369 {
facd6764 1370 wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetTopLevelWindowRef() ) ) ;
e40298d5 1371
fcb35beb 1372 if ( m_font.Ok() )
e40298d5 1373 {
facd6764
SC
1374 ::TextFont( m_font.MacGetFontNum() ) ;
1375 ::TextSize( m_font.MacGetFontSize() ) ;
1376 ::TextFace( m_font.MacGetFontStyle() ) ;
e40298d5
JS
1377 }
1378 else
1379 {
1380 ::TextFont( kFontIDMonaco ) ;
1381 ::TextSize( 9 );
1382 ::TextFace( 0 ) ;
1383 }
1384
1385 // Find the widest line
1386 for(int i = 0; i < GetCount(); i++) {
1387 wxString str(GetString(i));
2c1a3312
SC
1388 #if wxUSE_UNICODE
1389 Point bounds={0,0} ;
1390 SInt16 baseline ;
a9412f8f 1391 ::GetThemeTextDimensions( wxMacCFStringHolder( str , m_font.GetEncoding() ) ,
2c1a3312
SC
1392 kThemeCurrentPortFont,
1393 kThemeStateActive,
1394 false,
1395 &bounds,
1396 &baseline );
1397 wLine = bounds.h ;
1398 #else
939fba6c 1399 wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ;
2c1a3312 1400 #endif
e40298d5
JS
1401 lbWidth = wxMax(lbWidth, wLine);
1402 }
1403
1404 // Add room for the scrollbar
1405 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
1406
1407 // And just a bit more
1408 int cy = 12 ;
1409 int cx = ::TextWidth( "X" , 0 , 1 ) ;
1410 lbWidth += cx ;
1411
1412 // don't make the listbox too tall (limit height to around 10 items) but don't
1413 // make it too small neither
1414 lbHeight = (cy+4) * wxMin(wxMax(GetCount(), 3), 10);
1415 }
facd6764 1416
2b5f62a0 1417 return wxSize(lbWidth, lbHeight);
e9576ca5
SC
1418}
1419
51abe921
SC
1420int wxListBox::GetCount() const
1421{
1422 return m_noItems;
1423}
1424
60149370
GD
1425void wxListBox::Refresh(bool eraseBack, const wxRect *rect)
1426{
de043984 1427 wxControl::Refresh( eraseBack , rect ) ;
e40298d5 1428 // MacRedrawControl() ;
60149370
GD
1429}
1430
51abe921
SC
1431#if wxUSE_OWNER_DRAWN
1432
1433class wxListBoxItem : public wxOwnerDrawn
1434{
1435public:
1436 wxListBoxItem(const wxString& str = "");
1437};
1438
1439wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE)
1440{
1441 // no bitmaps/checkmarks
1442 SetMarginWidth(0);
1443}
1444
1445wxOwnerDrawn *wxListBox::CreateItem(size_t n)
1446{
1447 return new wxListBoxItem();
1448}
1449
1450#endif //USE_OWNER_DRAWN
e9576ca5 1451
519cb848
SC
1452// ============================================================================
1453// list box control implementation
1454// ============================================================================
1455
2b5f62a0 1456/*
519cb848
SC
1457void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon)
1458{
e40298d5
JS
1459wxListBox* list;
1460// typecast our refCon
1461list = (wxListBox*)refCon;
1462
1463 MoveTo(cellRect->left + 4 , cellRect->top + 10 );
1464 const wxString text = list->m_stringArray[lCell.v] ;
1465 ::TextFont( kFontIDMonaco ) ;
1466 ::TextSize( 9 );
1467 ::TextFace( 0 ) ;
1468 DrawText(text, 0 , text.Length());
1469
1470 }
2b5f62a0 1471*/
519cb848
SC
1472void wxListBox::MacDelete( int N )
1473{
76a5e5d2 1474 LDelRow( 1 , N , (ListHandle)m_macList) ;
60149370 1475 Refresh();
519cb848
SC
1476}
1477
427ff662 1478void wxListBox::MacInsert( int n , const wxString& text)
519cb848 1479{
60149370
GD
1480 Cell cell = { 0 , 0 } ;
1481 cell.v = n ;
76a5e5d2 1482 LAddRow( 1 , cell.v , (ListHandle)m_macList ) ;
e40298d5 1483 // LSetCell(text, strlen(text), cell, m_macList);
60149370 1484 Refresh();
519cb848
SC
1485}
1486
427ff662 1487void wxListBox::MacAppend( const wxString& text)
519cb848 1488{
60149370 1489 Cell cell = { 0 , 0 } ;
76a5e5d2
SC
1490 cell.v = (**(ListHandle)m_macList).dataBounds.bottom ;
1491 LAddRow( 1 , cell.v , (ListHandle)m_macList ) ;
e40298d5 1492 // LSetCell(text, strlen(text), cell, m_macList);
60149370 1493 Refresh();
519cb848
SC
1494}
1495
dc0ace7c 1496void wxListBox::MacClear()
519cb848 1497{
76a5e5d2 1498 LDelRow( (**(ListHandle)m_macList).dataBounds.bottom , 0 ,(ListHandle) m_macList ) ;
60149370 1499 Refresh();
519cb848
SC
1500}
1501
1502void wxListBox::MacSetSelection( int n , bool select )
1503{
a8e6bf8a
RR
1504 Cell cell = { 0 , 0 } ;
1505 if ( ! (m_windowStyle & wxLB_MULTIPLE) )
1506 {
9f081f02
GD
1507 if ( LGetSelect( true , &cell , (ListHandle)m_macList ) )
1508 {
1509 LSetSelect( false , cell , (ListHandle)m_macList ) ;
1510 }
a8e6bf8a 1511 }
e40298d5 1512
a8e6bf8a 1513 cell.v = n ;
76a5e5d2
SC
1514 LSetSelect( select , cell , (ListHandle)m_macList ) ;
1515 LAutoScroll( (ListHandle)m_macList ) ;
a8e6bf8a 1516 Refresh();
519cb848
SC
1517}
1518
1519bool wxListBox::MacIsSelected( int n ) const
1520{
a8e6bf8a
RR
1521 Cell cell = { 0 , 0 } ;
1522 cell.v = n ;
76a5e5d2 1523 return LGetSelect( false , &cell , (ListHandle)m_macList ) ;
519cb848
SC
1524}
1525
519cb848
SC
1526int wxListBox::MacGetSelection() const
1527{
a8e6bf8a 1528 Cell cell = { 0 , 0 } ;
76a5e5d2 1529 if ( LGetSelect( true , &cell , (ListHandle)m_macList ) )
a8e6bf8a
RR
1530 return cell.v ;
1531 else
1532 return -1 ;
519cb848
SC
1533}
1534
1535int wxListBox::MacGetSelections( wxArrayInt& aSelections ) const
1536{
a8e6bf8a 1537 int no_sel = 0 ;
e40298d5 1538
519cb848 1539 aSelections.Empty();
e40298d5 1540
a8e6bf8a
RR
1541 Cell cell = { 0 , 0 } ;
1542 cell.v = 0 ;
e40298d5 1543
76a5e5d2 1544 while ( LGetSelect( true , &cell ,(ListHandle) m_macList ) )
a8e6bf8a
RR
1545 {
1546 aSelections.Add( cell.v ) ;
1547 no_sel++ ;
1548 cell.v++ ;
1549 }
1550 return no_sel ;
519cb848
SC
1551}
1552
427ff662 1553void wxListBox::MacSet( int n , const wxString& text )
519cb848 1554{
a8e6bf8a
RR
1555 // our implementation does not store anything in the list
1556 // so we just have to redraw
1557 Cell cell = { 0 , 0 } ;
1558 cell.v = n ;
e40298d5 1559 // LSetCell(text, strlen(text), cell, m_macList);
a8e6bf8a 1560 Refresh();
519cb848
SC
1561}
1562
1563void wxListBox::MacScrollTo( int n )
1564{
a8e6bf8a 1565 // TODO implement scrolling
519cb848
SC
1566}
1567
864db5de 1568void wxListBox::OnSize( wxSizeEvent &event)
519cb848 1569{
60149370 1570 Point pt;
e40298d5 1571
60149370 1572#if TARGET_CARBON
962cbf2e 1573 GetListCellSize((ListHandle)m_macList, &pt);
60149370 1574#else
76a5e5d2 1575 pt = (**(ListHandle)m_macList).cellSize ;
60149370 1576#endif
facd6764
SC
1577 int w, h ;
1578 GetSize( &w , &h ) ;
1579 pt.h = w - 15 ;
76a5e5d2 1580 LCellSize( pt , (ListHandle)m_macList ) ;
519cb848
SC
1581}
1582
4b26b60f 1583void wxListBox::MacHandleControlClick( WXWidget control , wxInt16 controlpart , bool WXUNUSED(mouseStillDown))
519cb848 1584{
a8e6bf8a
RR
1585 Boolean wasDoubleClick = false ;
1586 long result ;
e40298d5 1587
facd6764 1588 ::GetControlData( (ControlRef) m_macControl , kControlNoPart , kControlListBoxDoubleClickTag , sizeof( wasDoubleClick ) , (char*) &wasDoubleClick , &result ) ;
a8e6bf8a
RR
1589 if ( !wasDoubleClick )
1590 {
1591 MacDoClick() ;
1592 }
1593 else
1594 {
1595 MacDoDoubleClick() ;
1596 }
519cb848
SC
1597}
1598
dc0ace7c 1599void wxListBox::MacSetRedraw( bool doDraw )
519cb848 1600{
76a5e5d2 1601 LSetDrawingMode( doDraw , (ListHandle)m_macList ) ;
e40298d5 1602
519cb848
SC
1603}
1604
1605void wxListBox::MacDoClick()
1606{
a8e6bf8a 1607 wxArrayInt aSelections;
68a9d9d0
SC
1608 int n ;
1609 size_t count = GetSelections(aSelections);
e40298d5 1610
a8e6bf8a
RR
1611 if ( count == m_selectionPreImage.GetCount() )
1612 {
1613 bool hasChanged = false ;
68a9d9d0 1614 for ( size_t i = 0 ; i < count ; ++i )
a8e6bf8a
RR
1615 {
1616 if ( aSelections[i] != m_selectionPreImage[i] )
1617 {
1618 hasChanged = true ;
1619 break ;
1620 }
1621 }
1622 if ( !hasChanged )
1623 {
1624 return ;
1625 }
1626 }
e40298d5 1627
a8e6bf8a 1628 m_selectionPreImage = aSelections;
e40298d5 1629
a8e6bf8a
RR
1630 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
1631 event.SetEventObject( this );
e40298d5 1632
a8e6bf8a
RR
1633 if ( count > 0 )
1634 {
1635 n = aSelections[0];
1636 if ( HasClientObjectData() )
1637 event.SetClientObject( GetClientObject(n) );
1638 else if ( HasClientUntypedData() )
1639 event.SetClientData( GetClientData(n) );
1640 event.SetString( GetString(n) );
1641 }
1642 else
1643 {
e40298d5 1644 n = -1;
a8e6bf8a 1645 }
e40298d5 1646
e7549107 1647 event.m_commandInt = n;
e40298d5 1648
e7549107 1649 GetEventHandler()->ProcessEvent(event);
519cb848
SC
1650}
1651
1652void wxListBox::MacDoDoubleClick()
1653{
1654 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId);
1655 event.SetEventObject( this );
e40298d5 1656 GetEventHandler()->ProcessEvent(event) ;
519cb848 1657}
ecaf6c18 1658
ecaf6c18
SC
1659void wxListBox::OnChar(wxKeyEvent& event)
1660{
eb22f2a6 1661 if ( event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER)
92104223 1662 {
e40298d5
JS
1663 wxWindow* parent = GetParent() ;
1664 while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL )
1665 parent = parent->GetParent() ;
1666
1667 if ( parent && parent->GetDefaultItem() )
1668 {
1669 wxButton *def = wxDynamicCast(parent->GetDefaultItem(),
1670 wxButton);
1671 if ( def && def->IsEnabled() )
1672 {
1673 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1674 event.SetEventObject(def);
1675 def->Command(event);
1676 return ;
1677 }
1678 }
1679 event.Skip() ;
92104223
SC
1680 }
1681 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
eb22f2a6 1682 else if (event.GetKeyCode() == WXK_ESCAPE || (event.GetKeyCode() == '.' && event.MetaDown() ) )
92104223 1683 {
44553322 1684 // FIXME: look in ancestors, not just parent.
e40298d5 1685 wxWindow* win = GetParent()->FindWindow( wxID_CANCEL ) ;
44553322
JS
1686 if (win)
1687 {
1688 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1689 new_event.SetEventObject( win );
1690 win->GetEventHandler()->ProcessEvent( new_event );
1691 }
92104223 1692 }
eb22f2a6 1693 else if ( event.GetKeyCode() == WXK_TAB )
92104223
SC
1694 {
1695 wxNavigationKeyEvent new_event;
1696 new_event.SetEventObject( this );
1697 new_event.SetDirection( !event.ShiftDown() );
1698 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1699 new_event.SetWindowChange( event.ControlDown() );
1700 new_event.SetCurrentFocus( this );
1701 if ( !GetEventHandler()->ProcessEvent( new_event ) )
e40298d5 1702 event.Skip() ;
92104223 1703 }
e40298d5
JS
1704 else if ( event.GetKeyCode() == WXK_DOWN || event.GetKeyCode() == WXK_UP )
1705 {
1706 // perform the default key handling first
1707 wxControl::OnKeyDown( event ) ;
1708
ecaf6c18
SC
1709 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
1710 event.SetEventObject( this );
e40298d5 1711
ecaf6c18
SC
1712 wxArrayInt aSelections;
1713 int n, count = GetSelections(aSelections);
1714 if ( count > 0 )
1715 {
e40298d5
JS
1716 n = aSelections[0];
1717 if ( HasClientObjectData() )
1718 event.SetClientObject( GetClientObject(n) );
1719 else if ( HasClientUntypedData() )
1720 event.SetClientData( GetClientData(n) );
1721 event.SetString( GetString(n) );
ecaf6c18
SC
1722 }
1723 else
1724 {
e40298d5 1725 n = -1;
ecaf6c18 1726 }
e40298d5 1727
ecaf6c18 1728 event.m_commandInt = n;
e40298d5 1729
ecaf6c18 1730 GetEventHandler()->ProcessEvent(event);
e40298d5
JS
1731 }
1732 else
1733 {
1734 if ( event.GetTimestamp() > m_lastTypeIn + 60 )
1735 {
427ff662 1736 m_typeIn = wxEmptyString ;
e40298d5
JS
1737 }
1738 m_lastTypeIn = event.GetTimestamp() ;
1739 m_typeIn += (char) event.GetKeyCode() ;
427ff662 1740 int line = FindString(wxT("*")+m_typeIn+wxT("*")) ;
e40298d5
JS
1741 if ( line >= 0 )
1742 {
1743 if ( GetSelection() != line )
1744 {
1745 SetSelection(line) ;
1746 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
1747 event.SetEventObject( this );
1748
1749 if ( HasClientObjectData() )
1750 event.SetClientObject( GetClientObject( line ) );
1751 else if ( HasClientUntypedData() )
1752 event.SetClientData( GetClientData(line) );
1753 event.SetString( GetString(line) );
1754
1755 event.m_commandInt = line ;
1756
1757 GetEventHandler()->ProcessEvent(event);
1758 }
1759 }
1760 }
ecaf6c18
SC
1761}
1762
facd6764 1763#endif