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