]>
git.saurik.com Git - wxWidgets.git/blob - src/osx/listbox_osx.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/listbox_osx.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 BEGIN_EVENT_TABLE(wxListBox
, wxControl
)
30 #include "wx/osx/private.h"
32 // ============================================================================
33 // list box control implementation
34 // ============================================================================
36 wxListBox::wxListBox()
40 bool wxListBox::Create(
45 const wxArrayString
& choices
,
47 const wxValidator
& validator
,
48 const wxString
& name
)
50 wxCArrayString
chs(choices
);
53 parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
54 style
, validator
, name
);
57 wxListWidgetImpl
* wxListBox::GetListPeer() const
59 wxListWidgetImpl
* impl
= dynamic_cast<wxListWidgetImpl
*> ( GetPeer() );
63 bool wxListBox::Create(
69 const wxString choices
[],
71 const wxValidator
& validator
,
72 const wxString
& name
)
75 m_blockEvents
= false;
77 if ( ! (style
& wxNO_BORDER
) )
78 style
= (style
& ~wxBORDER_MASK
) | wxSUNKEN_BORDER
;
80 wxASSERT_MSG( !(style
& wxLB_MULTIPLE
) || !(style
& wxLB_EXTENDED
),
81 wxT("only a single listbox selection mode can be specified") );
83 if ( !wxListBoxBase::Create( parent
, id
, pos
, size
, style
& ~(wxHSCROLL
| wxVSCROLL
), validator
, name
) )
87 m_strings
.sorted
= new wxSortedArrayString
;
89 m_strings
.unsorted
= new wxArrayString
;
91 SetPeer(wxWidgetImpl::CreateListBox( this, parent
, id
, pos
, size
, style
, GetExtraStyle() ));
93 MacPostControlCreate( pos
, size
);
95 m_textColumn
= GetListPeer()->InsertTextColumn(0,wxEmptyString
);
99 // Needed because it is a wxControlWithItems
100 SetInitialSize( size
);
105 wxListBox::~wxListBox()
107 m_blockEvents
= true;
109 m_blockEvents
= false;
111 // make sure no native events get sent to a object in destruction
115 delete m_strings
.sorted
;
117 delete m_strings
.unsorted
;
119 m_strings
.sorted
= NULL
;
122 void wxListBox::FreeData()
125 m_strings
.sorted
->Clear();
127 m_strings
.unsorted
->Clear();
129 m_itemsClientData
.Clear();
131 GetListPeer()->ListClear();
134 void wxListBox::DoSetFirstItem(int n
)
136 // osx actually only has an implementation for ensuring the visibility of a row, it does so
137 // by scrolling the minimal amount necessary from the current scrolling position.
138 // in order to get the same behaviour I'd have to make sure first that the last line is visible,
139 // followed by a scrollRowToVisible for the desired line
140 GetListPeer()->ListScrollTo( GetCount()-1 );
141 GetListPeer()->ListScrollTo( n
);
144 void wxListBox::EnsureVisible(int n
)
146 GetListPeer()->ListScrollTo( n
);
149 void wxListBox::DoDeleteOneItem(unsigned int n
)
151 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::Delete") );
153 m_blockEvents
= true;
155 m_strings
.sorted
->RemoveAt(n
);
157 m_strings
.unsorted
->RemoveAt(n
);
159 m_itemsClientData
.RemoveAt(n
);
161 GetListPeer()->ListDelete( n
);
162 m_blockEvents
= false;
164 UpdateOldSelections();
167 void wxListBox::DoClear()
169 m_blockEvents
= true;
171 m_blockEvents
= false;
173 UpdateOldSelections();
176 // ----------------------------------------------------------------------------
178 // ----------------------------------------------------------------------------
180 void wxListBox::DoSetSelection(int n
, bool select
)
182 wxCHECK_RET( n
== wxNOT_FOUND
|| IsValid(n
),
183 wxT("invalid index in wxListBox::SetSelection") );
185 m_blockEvents
= true;
187 if ( n
== wxNOT_FOUND
)
188 GetListPeer()->ListDeselectAll();
190 GetListPeer()->ListSetSelection( n
, select
, HasMultipleSelection() );
192 m_blockEvents
= false;
194 UpdateOldSelections();
197 bool wxListBox::IsSelected(int n
) const
199 wxCHECK_MSG( IsValid(n
), false, wxT("invalid index in wxListBox::Selected") );
201 return GetListPeer()->ListIsSelected( n
);
204 // Return number of selections and an array of selected integers
205 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
207 return GetListPeer()->ListGetSelections( aSelections
);
210 // Get single selection, for single choice list items
211 int wxListBox::GetSelection() const
213 return GetListPeer()->ListGetSelection();
216 int wxListBox::DoListHitTest(const wxPoint
& inpoint
) const
218 return GetListPeer()->DoListHitTest( inpoint
);
221 // ----------------------------------------------------------------------------
223 // ----------------------------------------------------------------------------
225 void wxListBox::GetValueCallback( unsigned int n
, wxListWidgetColumn
* col
, wxListWidgetCellValue
& value
)
227 if ( col
== m_textColumn
)
228 value
.Set( GetString( n
) );
231 void wxListBox::SetValueCallback( unsigned int WXUNUSED(n
), wxListWidgetColumn
* WXUNUSED(col
) , wxListWidgetCellValue
& WXUNUSED(value
) )
235 wxSize
wxListBox::DoGetBestSize() const
237 int lbWidth
= 100; // some defaults
242 wxClientDC
dc(const_cast<wxListBox
*>(this));
243 dc
.SetFont(GetFont());
245 // Find the widest line
246 for (unsigned int i
= 0; i
< GetCount(); i
++)
248 wxString
str( GetString( i
) );
250 wxCoord width
, height
;
251 dc
.GetTextExtent( str
, &width
, &height
);
253 lbWidth
= wxMax( lbWidth
, wLine
);
256 // Add room for the scrollbar
257 lbWidth
+= wxSystemSettings::GetMetric( wxSYS_VSCROLL_X
);
259 // And just a bit more
262 wxCoord width
, height
;
263 dc
.GetTextExtent( wxT("XX") , &width
, &height
);
267 // don't make the listbox too tall (limit height to around 10 items)
268 // but don't make it too small neither
269 lbHeight
= wxMax( (cy
+ 4) * wxMin( wxMax( GetCount(), 3 ), 10 ), 70 );
272 return wxSize( lbWidth
, lbHeight
);
275 void wxListBox::Refresh(bool eraseBack
, const wxRect
*rect
)
277 wxControl::Refresh( eraseBack
, rect
);
280 // Some custom controls depend on this
281 /* static */ wxVisualAttributes
282 wxListBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
284 wxVisualAttributes attr
;
286 attr
.colFg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
287 attr
.colBg
= wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX
);
288 static wxFont font
= wxFont(wxOSX_SYSTEM_FONT_VIEWS
);
294 // below is all code copied from univ
296 // ----------------------------------------------------------------------------
297 // client data handling
298 // ----------------------------------------------------------------------------
300 void wxListBox::DoSetItemClientData(unsigned int n
, void* clientData
)
302 m_itemsClientData
[n
] = clientData
;
305 void *wxListBox::DoGetItemClientData(unsigned int n
) const
307 return m_itemsClientData
[n
];
310 // ----------------------------------------------------------------------------
312 // ----------------------------------------------------------------------------
314 unsigned int wxListBox::GetCount() const
316 return IsSorted() ? m_strings
.sorted
->size()
317 : m_strings
.unsorted
->size();
320 wxString
wxListBox::GetString(unsigned int n
) const
322 return IsSorted() ? m_strings
.sorted
->Item(n
)
323 : m_strings
.unsorted
->Item(n
);
326 int wxListBox::FindString(const wxString
& s
, bool bCase
) const
328 return IsSorted() ? m_strings
.sorted
->Index(s
, bCase
)
329 : m_strings
.unsorted
->Index(s
, bCase
);
332 // ----------------------------------------------------------------------------
333 // adding/inserting strings
334 // ----------------------------------------------------------------------------
336 void wxListBox::OnItemInserted(unsigned int WXUNUSED(pos
))
340 int wxListBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
343 wxClientDataType type
)
345 int idx
= wxNOT_FOUND
;
346 unsigned int startpos
= pos
;
348 const unsigned int numItems
= items
.GetCount();
349 for ( unsigned int i
= 0; i
< numItems
; ++i
)
351 const wxString
& item
= items
[i
];
352 idx
= IsSorted() ? m_strings
.sorted
->Add(item
)
353 : (m_strings
.unsorted
->Insert(item
, pos
), pos
++);
355 m_itemsClientData
.Insert(NULL
, idx
);
356 AssignNewItemClientData(idx
, clientData
, i
, type
);
358 GetListPeer()->ListInsert(startpos
+i
);
363 GetListPeer()->UpdateLineToEnd(startpos
);
365 // Inserting the items may scroll the listbox down to show the last
366 // selected one but we don't want to do it as it could result in e.g. the
367 // first items of a listbox be hidden immediately after its creation so
368 // show the first selected item instead. Ideal would probably be to
369 // preserve the old selection unchanged, in fact, but I don't know how to
370 // get the first visible item so for now do at least this.
371 SetFirstItem(startpos
);
373 UpdateOldSelections();
378 void wxListBox::SetString(unsigned int n
, const wxString
& s
)
380 wxCHECK_RET( !IsSorted(), wxT("can't set string in sorted listbox") );
383 (*m_strings
.sorted
)[n
] = s
;
385 (*m_strings
.unsorted
)[n
] = s
;
387 GetListPeer()->UpdateLine(n
);
391 // common event handling
394 void wxListBox::HandleLineEvent( unsigned int n
, bool doubleClick
)
396 wxCommandEvent
event( doubleClick
? wxEVT_LISTBOX_DCLICK
:
397 wxEVT_LISTBOX
, GetId() );
398 event
.SetEventObject( this );
399 if ( HasClientObjectData() )
400 event
.SetClientObject( GetClientObject(n
) );
401 else if ( HasClientUntypedData() )
402 event
.SetClientData( GetClientData(n
) );
403 event
.SetString( GetString(n
) );
405 event
.SetExtraLong( 1 );
406 HandleWindowEvent(event
);
410 // common list cell value operations
413 void wxListWidgetCellValue::Check( bool check
)
415 Set( check
? 1 : 0 );
418 bool wxListWidgetCellValue::IsChecked() const
420 return GetIntValue() != 0;
425 #endif // wxUSE_LISTBOX