1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/listbox.cpp
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: listbox.cpp 54820 2008-07-29 20:04:11Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/listbox.h"
22 #include "wx/settings.h"
23 #include "wx/arrstr.h"
24 #include "wx/dcclient.h"
27 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControlWithItems
)
29 BEGIN_EVENT_TABLE(wxListBox
, wxControl
)
32 #include "wx/osx/private.h"
34 // ============================================================================
35 // list box control implementation
36 // ============================================================================
38 wxListBox::wxListBox()
42 bool wxListBox::Create(
47 const wxArrayString
& choices
,
49 const wxValidator
& validator
,
50 const wxString
& name
)
52 wxCArrayString
chs(choices
);
55 parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
56 style
, validator
, name
);
59 wxListWidgetImpl
* wxListBox::GetListPeer() const
61 wxListWidgetImpl
* impl
= dynamic_cast<wxListWidgetImpl
*> ( GetPeer() );
65 bool wxListBox::Create(
71 const wxString choices
[],
73 const wxValidator
& validator
,
74 const wxString
& name
)
76 m_blockEvents
= false;
77 m_macIsUserPane
= false;
79 wxASSERT_MSG( !(style
& wxLB_MULTIPLE
) || !(style
& wxLB_EXTENDED
),
80 wxT("only a single listbox selection mode can be specified") );
82 if ( !wxListBoxBase::Create( parent
, id
, pos
, size
, style
& ~(wxHSCROLL
| wxVSCROLL
), validator
, name
) )
86 m_strings
.sorted
= new wxSortedArrayString
;
88 m_strings
.unsorted
= new wxArrayString
;
90 m_peer
= wxWidgetImpl::CreateListBox( this, parent
, id
, pos
, size
, style
, GetExtraStyle() );
92 MacPostControlCreate( pos
, size
);
94 m_textColumn
= GetListPeer()->InsertTextColumn(0,wxEmptyString
);
98 // Needed because it is a wxControlWithItems
99 SetInitialSize( size
);
104 wxListBox::~wxListBox()
107 // make sure no native events get sent to a object in destruction
112 delete m_strings
.sorted
;
114 delete m_strings
.unsorted
;
116 m_strings
.sorted
= NULL
;
119 void wxListBox::FreeData()
122 m_strings
.sorted
->Clear();
124 m_strings
.unsorted
->Clear();
126 m_itemsClientData
.Clear();
128 GetListPeer()->ListClear();
131 void wxListBox::DoSetFirstItem(int n
)
133 GetListPeer()->ListScrollTo( n
);
136 void wxListBox::EnsureVisible(int n
)
138 GetListPeer()->ListScrollTo( n
);
141 void wxListBox::DoDeleteOneItem(unsigned int n
)
143 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::Delete") );
145 m_blockEvents
= true;
147 m_strings
.sorted
->RemoveAt(n
);
149 m_strings
.unsorted
->RemoveAt(n
);
151 m_itemsClientData
.RemoveAt(n
);
153 GetListPeer()->ListDelete( n
);
154 m_blockEvents
= false;
156 UpdateOldSelections();
159 void wxListBox::DoClear()
161 m_blockEvents
= true;
163 m_blockEvents
= false;
165 UpdateOldSelections();
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
172 void wxListBox::DoSetSelection(int n
, bool select
)
174 wxCHECK_RET( n
== wxNOT_FOUND
|| IsValid(n
),
175 wxT("invalid index in wxListBox::SetSelection") );
177 m_blockEvents
= true;
179 if ( n
== wxNOT_FOUND
)
180 GetListPeer()->ListDeselectAll();
182 GetListPeer()->ListSetSelection( n
, select
, HasMultipleSelection() );
184 m_blockEvents
= false;
186 UpdateOldSelections();
189 bool wxListBox::IsSelected(int n
) const
191 wxCHECK_MSG( IsValid(n
), false, wxT("invalid index in wxListBox::Selected") );
193 return GetListPeer()->ListIsSelected( n
);
196 // Return number of selections and an array of selected integers
197 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
199 return GetListPeer()->ListGetSelections( aSelections
);
202 // Get single selection, for single choice list items
203 int wxListBox::GetSelection() const
205 return GetListPeer()->ListGetSelection();
208 // ----------------------------------------------------------------------------
210 // ----------------------------------------------------------------------------
212 void wxListBox::GetValueCallback( unsigned int n
, wxListWidgetColumn
* col
, wxListWidgetCellValue
& value
)
214 if ( col
== m_textColumn
)
215 value
.Set( GetString( n
) );
218 void wxListBox::SetValueCallback( unsigned int n
, wxListWidgetColumn
* col
, wxListWidgetCellValue
& value
)
222 wxSize
wxListBox::DoGetBestSize() const
224 int lbWidth
= 100; // some defaults
229 wxClientDC
dc(const_cast<wxListBox
*>(this));
230 dc
.SetFont(GetFont());
232 // Find the widest line
233 for (unsigned int i
= 0; i
< GetCount(); i
++)
235 wxString
str( GetString( i
) );
237 wxCoord width
, height
;
238 dc
.GetTextExtent( str
, &width
, &height
);
240 lbWidth
= wxMax( lbWidth
, wLine
);
243 // Add room for the scrollbar
244 lbWidth
+= wxSystemSettings::GetMetric( wxSYS_VSCROLL_X
);
246 // And just a bit more
249 wxCoord width
, height
;
250 dc
.GetTextExtent( wxT("XX") , &width
, &height
);
254 // don't make the listbox too tall (limit height to around 10 items)
255 // but don't make it too small neither
256 lbHeight
= wxMax( (cy
+ 4) * wxMin( wxMax( GetCount(), 3 ), 10 ), 70 );
259 return wxSize( lbWidth
, lbHeight
);
262 void wxListBox::Refresh(bool eraseBack
, const wxRect
*rect
)
264 wxControl::Refresh( eraseBack
, rect
);
267 // Some custom controls depend on this
268 /* static */ wxVisualAttributes
269 wxListBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
271 wxVisualAttributes attr
;
273 attr
.colFg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
274 attr
.colBg
= wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX
);
275 #if wxOSX_USE_ATSU_TEXT
276 attr
.font
.MacCreateFromThemeFont(kThemeViewsFont
);
278 attr
.font
.MacCreateFromUIFont(kCTFontViewsFontType
);
284 // below is all code copied from univ
286 // ----------------------------------------------------------------------------
287 // client data handling
288 // ----------------------------------------------------------------------------
290 void wxListBox::DoSetItemClientData(unsigned int n
, void* clientData
)
292 m_itemsClientData
[n
] = clientData
;
295 void *wxListBox::DoGetItemClientData(unsigned int n
) const
297 return m_itemsClientData
[n
];
300 // ----------------------------------------------------------------------------
302 // ----------------------------------------------------------------------------
304 unsigned int wxListBox::GetCount() const
306 return IsSorted() ? m_strings
.sorted
->size()
307 : m_strings
.unsorted
->size();
310 wxString
wxListBox::GetString(unsigned int n
) const
312 return IsSorted() ? m_strings
.sorted
->Item(n
)
313 : m_strings
.unsorted
->Item(n
);
316 int wxListBox::FindString(const wxString
& s
, bool bCase
) const
318 return IsSorted() ? m_strings
.sorted
->Index(s
, bCase
)
319 : m_strings
.unsorted
->Index(s
, bCase
);
322 // ----------------------------------------------------------------------------
323 // adding/inserting strings
324 // ----------------------------------------------------------------------------
326 void wxListBox::OnItemInserted(unsigned int WXUNUSED(pos
))
331 int wxListBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
334 wxClientDataType type
)
336 int idx
= wxNOT_FOUND
;
337 unsigned int startpos
= pos
;
339 const unsigned int numItems
= items
.GetCount();
340 for ( unsigned int i
= 0; i
< numItems
; ++i
)
342 const wxString
& item
= items
[i
];
343 idx
= IsSorted() ? m_strings
.sorted
->Add(item
)
344 : (m_strings
.unsorted
->Insert(item
, pos
), pos
++);
346 m_itemsClientData
.Insert(NULL
, idx
);
347 AssignNewItemClientData(idx
, clientData
, i
, type
);
349 GetListPeer()->ListInsert(startpos
+i
);
354 GetListPeer()->UpdateLineToEnd(startpos
);
356 UpdateOldSelections();
361 void wxListBox::SetString(unsigned int n
, const wxString
& s
)
363 wxCHECK_RET( !IsSorted(), _T("can't set string in sorted listbox") );
366 (*m_strings
.sorted
)[n
] = s
;
368 (*m_strings
.unsorted
)[n
] = s
;
370 GetListPeer()->UpdateLine(n
);
374 // common event handling
377 void wxListBox::HandleLineEvent( unsigned int n
, bool doubleClick
)
379 wxCommandEvent
event( doubleClick
? wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
:
380 wxEVT_COMMAND_LISTBOX_SELECTED
, GetId() );
381 event
.SetEventObject( this );
382 if ( HasClientObjectData() )
383 event
.SetClientObject( GetClientObject(n
) );
384 else if ( HasClientUntypedData() )
385 event
.SetClientData( GetClientData(n
) );
386 event
.SetString( GetString(n
) );
388 event
.SetExtraLong( 1 );
389 HandleWindowEvent(event
);
392 #endif // wxUSE_LISTBOX