1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/listbox.cpp
4 // Author: Stefan Csomor
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()
106 m_blockEvents
= true;
108 m_blockEvents
= false;
110 // make sure no native events get sent to a object in destruction
114 delete m_strings
.sorted
;
116 delete m_strings
.unsorted
;
118 m_strings
.sorted
= NULL
;
121 void wxListBox::FreeData()
124 m_strings
.sorted
->Clear();
126 m_strings
.unsorted
->Clear();
128 m_itemsClientData
.Clear();
130 GetListPeer()->ListClear();
133 void wxListBox::DoSetFirstItem(int n
)
135 GetListPeer()->ListScrollTo( n
);
138 void wxListBox::EnsureVisible(int n
)
140 GetListPeer()->ListScrollTo( n
);
143 void wxListBox::DoDeleteOneItem(unsigned int n
)
145 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::Delete") );
147 m_blockEvents
= true;
149 m_strings
.sorted
->RemoveAt(n
);
151 m_strings
.unsorted
->RemoveAt(n
);
153 m_itemsClientData
.RemoveAt(n
);
155 GetListPeer()->ListDelete( n
);
156 m_blockEvents
= false;
158 UpdateOldSelections();
161 void wxListBox::DoClear()
163 m_blockEvents
= true;
165 m_blockEvents
= false;
167 UpdateOldSelections();
170 // ----------------------------------------------------------------------------
172 // ----------------------------------------------------------------------------
174 void wxListBox::DoSetSelection(int n
, bool select
)
176 wxCHECK_RET( n
== wxNOT_FOUND
|| IsValid(n
),
177 wxT("invalid index in wxListBox::SetSelection") );
179 m_blockEvents
= true;
181 if ( n
== wxNOT_FOUND
)
182 GetListPeer()->ListDeselectAll();
184 GetListPeer()->ListSetSelection( n
, select
, HasMultipleSelection() );
186 m_blockEvents
= false;
188 UpdateOldSelections();
191 bool wxListBox::IsSelected(int n
) const
193 wxCHECK_MSG( IsValid(n
), false, wxT("invalid index in wxListBox::Selected") );
195 return GetListPeer()->ListIsSelected( n
);
198 // Return number of selections and an array of selected integers
199 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
201 return GetListPeer()->ListGetSelections( aSelections
);
204 // Get single selection, for single choice list items
205 int wxListBox::GetSelection() const
207 return GetListPeer()->ListGetSelection();
210 int wxListBox::DoListHitTest(const wxPoint
& inpoint
) const
212 return GetListPeer()->DoListHitTest( inpoint
);
215 // ----------------------------------------------------------------------------
217 // ----------------------------------------------------------------------------
219 void wxListBox::GetValueCallback( unsigned int n
, wxListWidgetColumn
* col
, wxListWidgetCellValue
& value
)
221 if ( col
== m_textColumn
)
222 value
.Set( GetString( n
) );
225 void wxListBox::SetValueCallback( unsigned int WXUNUSED(n
), wxListWidgetColumn
* WXUNUSED(col
) , wxListWidgetCellValue
& WXUNUSED(value
) )
229 wxSize
wxListBox::DoGetBestSize() const
231 int lbWidth
= 100; // some defaults
236 wxClientDC
dc(const_cast<wxListBox
*>(this));
237 dc
.SetFont(GetFont());
239 // Find the widest line
240 for (unsigned int i
= 0; i
< GetCount(); i
++)
242 wxString
str( GetString( i
) );
244 wxCoord width
, height
;
245 dc
.GetTextExtent( str
, &width
, &height
);
247 lbWidth
= wxMax( lbWidth
, wLine
);
250 // Add room for the scrollbar
251 lbWidth
+= wxSystemSettings::GetMetric( wxSYS_VSCROLL_X
);
253 // And just a bit more
256 wxCoord width
, height
;
257 dc
.GetTextExtent( wxT("XX") , &width
, &height
);
261 // don't make the listbox too tall (limit height to around 10 items)
262 // but don't make it too small neither
263 lbHeight
= wxMax( (cy
+ 4) * wxMin( wxMax( GetCount(), 3 ), 10 ), 70 );
266 return wxSize( lbWidth
, lbHeight
);
269 void wxListBox::Refresh(bool eraseBack
, const wxRect
*rect
)
271 wxControl::Refresh( eraseBack
, rect
);
274 // Some custom controls depend on this
275 /* static */ wxVisualAttributes
276 wxListBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
278 wxVisualAttributes attr
;
280 attr
.colFg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
281 attr
.colBg
= wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX
);
282 static wxFont font
= wxFont(wxOSX_SYSTEM_FONT_VIEWS
);
288 // below is all code copied from univ
290 // ----------------------------------------------------------------------------
291 // client data handling
292 // ----------------------------------------------------------------------------
294 void wxListBox::DoSetItemClientData(unsigned int n
, void* clientData
)
296 m_itemsClientData
[n
] = clientData
;
299 void *wxListBox::DoGetItemClientData(unsigned int n
) const
301 return m_itemsClientData
[n
];
304 // ----------------------------------------------------------------------------
306 // ----------------------------------------------------------------------------
308 unsigned int wxListBox::GetCount() const
310 return IsSorted() ? m_strings
.sorted
->size()
311 : m_strings
.unsorted
->size();
314 wxString
wxListBox::GetString(unsigned int n
) const
316 return IsSorted() ? m_strings
.sorted
->Item(n
)
317 : m_strings
.unsorted
->Item(n
);
320 int wxListBox::FindString(const wxString
& s
, bool bCase
) const
322 return IsSorted() ? m_strings
.sorted
->Index(s
, bCase
)
323 : m_strings
.unsorted
->Index(s
, bCase
);
326 // ----------------------------------------------------------------------------
327 // adding/inserting strings
328 // ----------------------------------------------------------------------------
330 void wxListBox::OnItemInserted(unsigned int WXUNUSED(pos
))
335 int wxListBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
338 wxClientDataType type
)
340 int idx
= wxNOT_FOUND
;
341 unsigned int startpos
= pos
;
343 const unsigned int numItems
= items
.GetCount();
344 for ( unsigned int i
= 0; i
< numItems
; ++i
)
346 const wxString
& item
= items
[i
];
347 idx
= IsSorted() ? m_strings
.sorted
->Add(item
)
348 : (m_strings
.unsorted
->Insert(item
, pos
), pos
++);
350 m_itemsClientData
.Insert(NULL
, idx
);
351 AssignNewItemClientData(idx
, clientData
, i
, type
);
353 GetListPeer()->ListInsert(startpos
+i
);
358 GetListPeer()->UpdateLineToEnd(startpos
);
360 UpdateOldSelections();
365 void wxListBox::SetString(unsigned int n
, const wxString
& s
)
367 wxCHECK_RET( !IsSorted(), wxT("can't set string in sorted listbox") );
370 (*m_strings
.sorted
)[n
] = s
;
372 (*m_strings
.unsorted
)[n
] = s
;
374 GetListPeer()->UpdateLine(n
);
378 // common event handling
381 void wxListBox::HandleLineEvent( unsigned int n
, bool doubleClick
)
383 wxCommandEvent
event( doubleClick
? wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
:
384 wxEVT_COMMAND_LISTBOX_SELECTED
, GetId() );
385 event
.SetEventObject( this );
386 if ( HasClientObjectData() )
387 event
.SetClientObject( GetClientObject(n
) );
388 else if ( HasClientUntypedData() )
389 event
.SetClientData( GetClientData(n
) );
390 event
.SetString( GetString(n
) );
392 event
.SetExtraLong( 1 );
393 HandleWindowEvent(event
);
397 // common list cell value operations
400 void wxListWidgetCellValue::Check( bool check
)
402 Set( check
? 1 : 0 );
405 bool wxListWidgetCellValue::IsChecked() const
407 return GetIntValue() != 0;
412 #endif // wxUSE_LISTBOX