Show the first, not the last, inserted item in wxListBox in wxOSX.
[wxWidgets.git] / src / osx / listbox_osx.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/listbox.cpp
3 // Purpose: wxListBox
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxUSE_LISTBOX
15
16 #include "wx/listbox.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/log.h"
20 #include "wx/intl.h"
21 #include "wx/utils.h"
22 #include "wx/settings.h"
23 #include "wx/arrstr.h"
24 #include "wx/dcclient.h"
25 #endif
26
27 IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControlWithItems)
28
29 BEGIN_EVENT_TABLE(wxListBox, wxControl)
30 END_EVENT_TABLE()
31
32 #include "wx/osx/private.h"
33
34 // ============================================================================
35 // list box control implementation
36 // ============================================================================
37
38 wxListBox::wxListBox()
39 {
40 }
41
42 bool wxListBox::Create(
43 wxWindow *parent,
44 wxWindowID id,
45 const wxPoint& pos,
46 const wxSize& size,
47 const wxArrayString& choices,
48 long style,
49 const wxValidator& validator,
50 const wxString& name )
51 {
52 wxCArrayString chs(choices);
53
54 return Create(
55 parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
56 style, validator, name );
57 }
58
59 wxListWidgetImpl* wxListBox::GetListPeer() const
60 {
61 wxListWidgetImpl* impl = dynamic_cast<wxListWidgetImpl*> ( GetPeer() );
62 return impl;
63 }
64
65 bool wxListBox::Create(
66 wxWindow *parent,
67 wxWindowID id,
68 const wxPoint& pos,
69 const wxSize& size,
70 int n,
71 const wxString choices[],
72 long style,
73 const wxValidator& validator,
74 const wxString& name )
75 {
76 m_blockEvents = false;
77 m_macIsUserPane = false;
78
79 wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED),
80 wxT("only a single listbox selection mode can be specified") );
81
82 if ( !wxListBoxBase::Create( parent, id, pos, size, style & ~(wxHSCROLL | wxVSCROLL), validator, name ) )
83 return false;
84
85 if ( IsSorted() )
86 m_strings.sorted = new wxSortedArrayString;
87 else
88 m_strings.unsorted = new wxArrayString;
89
90 m_peer = wxWidgetImpl::CreateListBox( this, parent, id, pos, size, style, GetExtraStyle() );
91
92 MacPostControlCreate( pos, size );
93
94 m_textColumn = GetListPeer()->InsertTextColumn(0,wxEmptyString);
95
96 Append(n, choices);
97
98 // Needed because it is a wxControlWithItems
99 SetInitialSize( size );
100
101 return true;
102 }
103
104 wxListBox::~wxListBox()
105 {
106 m_blockEvents = true;
107 FreeData();
108 m_blockEvents = false;
109
110 // make sure no native events get sent to a object in destruction
111 wxDELETE(m_peer);
112
113 if ( IsSorted() )
114 delete m_strings.sorted;
115 else
116 delete m_strings.unsorted;
117
118 m_strings.sorted = NULL;
119 }
120
121 void wxListBox::FreeData()
122 {
123 if ( IsSorted() )
124 m_strings.sorted->Clear();
125 else
126 m_strings.unsorted->Clear();
127
128 m_itemsClientData.Clear();
129
130 GetListPeer()->ListClear();
131 }
132
133 void wxListBox::DoSetFirstItem(int n)
134 {
135 GetListPeer()->ListScrollTo( n );
136 }
137
138 void wxListBox::EnsureVisible(int n)
139 {
140 GetListPeer()->ListScrollTo( n );
141 }
142
143 void wxListBox::DoDeleteOneItem(unsigned int n)
144 {
145 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::Delete") );
146
147 m_blockEvents = true;
148 if ( IsSorted() )
149 m_strings.sorted->RemoveAt(n);
150 else
151 m_strings.unsorted->RemoveAt(n);
152
153 m_itemsClientData.RemoveAt(n);
154
155 GetListPeer()->ListDelete( n );
156 m_blockEvents = false;
157
158 UpdateOldSelections();
159 }
160
161 void wxListBox::DoClear()
162 {
163 m_blockEvents = true;
164 FreeData();
165 m_blockEvents = false;
166
167 UpdateOldSelections();
168 }
169
170 // ----------------------------------------------------------------------------
171 // selection
172 // ----------------------------------------------------------------------------
173
174 void wxListBox::DoSetSelection(int n, bool select)
175 {
176 wxCHECK_RET( n == wxNOT_FOUND || IsValid(n),
177 wxT("invalid index in wxListBox::SetSelection") );
178
179 m_blockEvents = true;
180
181 if ( n == wxNOT_FOUND )
182 GetListPeer()->ListDeselectAll();
183 else
184 GetListPeer()->ListSetSelection( n, select, HasMultipleSelection() );
185
186 m_blockEvents = false;
187
188 UpdateOldSelections();
189 }
190
191 bool wxListBox::IsSelected(int n) const
192 {
193 wxCHECK_MSG( IsValid(n), false, wxT("invalid index in wxListBox::Selected") );
194
195 return GetListPeer()->ListIsSelected( n );
196 }
197
198 // Return number of selections and an array of selected integers
199 int wxListBox::GetSelections(wxArrayInt& aSelections) const
200 {
201 return GetListPeer()->ListGetSelections( aSelections );
202 }
203
204 // Get single selection, for single choice list items
205 int wxListBox::GetSelection() const
206 {
207 return GetListPeer()->ListGetSelection();
208 }
209
210 int wxListBox::DoListHitTest(const wxPoint& inpoint) const
211 {
212 return GetListPeer()->DoListHitTest( inpoint );
213 }
214
215 // ----------------------------------------------------------------------------
216 // display
217 // ----------------------------------------------------------------------------
218
219 void wxListBox::GetValueCallback( unsigned int n, wxListWidgetColumn* col , wxListWidgetCellValue& value )
220 {
221 if ( col == m_textColumn )
222 value.Set( GetString( n ) );
223 }
224
225 void wxListBox::SetValueCallback( unsigned int WXUNUSED(n), wxListWidgetColumn* WXUNUSED(col) , wxListWidgetCellValue& WXUNUSED(value) )
226 {
227 }
228
229 wxSize wxListBox::DoGetBestSize() const
230 {
231 int lbWidth = 100; // some defaults
232 int lbHeight;
233 int wLine;
234
235 {
236 wxClientDC dc(const_cast<wxListBox*>(this));
237 dc.SetFont(GetFont());
238
239 // Find the widest line
240 for (unsigned int i = 0; i < GetCount(); i++)
241 {
242 wxString str( GetString( i ) );
243
244 wxCoord width, height ;
245 dc.GetTextExtent( str , &width, &height);
246 wLine = width ;
247 lbWidth = wxMax( lbWidth, wLine );
248 }
249
250 // Add room for the scrollbar
251 lbWidth += wxSystemSettings::GetMetric( wxSYS_VSCROLL_X );
252
253 // And just a bit more
254 int cy = 12;
255
256 wxCoord width, height ;
257 dc.GetTextExtent( wxT("XX") , &width, &height);
258 int cx = width ;
259 lbWidth += cx;
260
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 );
264 }
265
266 return wxSize( lbWidth, lbHeight );
267 }
268
269 void wxListBox::Refresh(bool eraseBack, const wxRect *rect)
270 {
271 wxControl::Refresh( eraseBack, rect );
272 }
273
274 // Some custom controls depend on this
275 /* static */ wxVisualAttributes
276 wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
277 {
278 wxVisualAttributes attr;
279
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);
283 attr.font = font;
284
285 return attr;
286 }
287
288 // below is all code copied from univ
289
290 // ----------------------------------------------------------------------------
291 // client data handling
292 // ----------------------------------------------------------------------------
293
294 void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
295 {
296 m_itemsClientData[n] = clientData;
297 }
298
299 void *wxListBox::DoGetItemClientData(unsigned int n) const
300 {
301 return m_itemsClientData[n];
302 }
303
304 // ----------------------------------------------------------------------------
305 // accessing strings
306 // ----------------------------------------------------------------------------
307
308 unsigned int wxListBox::GetCount() const
309 {
310 return IsSorted() ? m_strings.sorted->size()
311 : m_strings.unsorted->size();
312 }
313
314 wxString wxListBox::GetString(unsigned int n) const
315 {
316 return IsSorted() ? m_strings.sorted->Item(n)
317 : m_strings.unsorted->Item(n);
318 }
319
320 int wxListBox::FindString(const wxString& s, bool bCase) const
321 {
322 return IsSorted() ? m_strings.sorted->Index(s, bCase)
323 : m_strings.unsorted->Index(s, bCase);
324 }
325
326 // ----------------------------------------------------------------------------
327 // adding/inserting strings
328 // ----------------------------------------------------------------------------
329
330 void wxListBox::OnItemInserted(unsigned int WXUNUSED(pos))
331 {
332
333 }
334
335 int wxListBox::DoInsertItems(const wxArrayStringsAdapter& items,
336 unsigned int pos,
337 void **clientData,
338 wxClientDataType type)
339 {
340 int idx = wxNOT_FOUND;
341 unsigned int startpos = pos;
342
343 const unsigned int numItems = items.GetCount();
344 for ( unsigned int i = 0; i < numItems; ++i )
345 {
346 const wxString& item = items[i];
347 idx = IsSorted() ? m_strings.sorted->Add(item)
348 : (m_strings.unsorted->Insert(item, pos), pos++);
349
350 m_itemsClientData.Insert(NULL, idx);
351 AssignNewItemClientData(idx, clientData, i, type);
352
353 GetListPeer()->ListInsert(startpos+i);
354
355 OnItemInserted(idx);
356 }
357
358 GetListPeer()->UpdateLineToEnd(startpos);
359
360 // Inserting the items may scroll the listbox down to show the last
361 // selected one but we don't want to do it as it could result in e.g. the
362 // first items of a listbox be hidden immediately after its creation so
363 // show the first selected item instead. Ideal would probably be to
364 // preserve the old selection unchanged, in fact, but I don't know how to
365 // get the first visible item so for now do at least this.
366 SetFirstItem(startpos);
367
368 UpdateOldSelections();
369
370 return idx;
371 }
372
373 void wxListBox::SetString(unsigned int n, const wxString& s)
374 {
375 wxCHECK_RET( !IsSorted(), wxT("can't set string in sorted listbox") );
376
377 if ( IsSorted() )
378 (*m_strings.sorted)[n] = s;
379 else
380 (*m_strings.unsorted)[n] = s;
381
382 GetListPeer()->UpdateLine(n);
383 }
384
385 //
386 // common event handling
387 //
388
389 void wxListBox::HandleLineEvent( unsigned int n, bool doubleClick )
390 {
391 wxCommandEvent event( doubleClick ? wxEVT_COMMAND_LISTBOX_DOUBLECLICKED :
392 wxEVT_COMMAND_LISTBOX_SELECTED, GetId() );
393 event.SetEventObject( this );
394 if ( HasClientObjectData() )
395 event.SetClientObject( GetClientObject(n) );
396 else if ( HasClientUntypedData() )
397 event.SetClientData( GetClientData(n) );
398 event.SetString( GetString(n) );
399 event.SetInt( n );
400 event.SetExtraLong( 1 );
401 HandleWindowEvent(event);
402 }
403
404 //
405 // common list cell value operations
406 //
407
408 void wxListWidgetCellValue::Check( bool check )
409 {
410 Set( check ? 1 : 0 );
411 }
412
413 bool wxListWidgetCellValue::IsChecked() const
414 {
415 return GetIntValue() != 0;
416 }
417
418
419
420 #endif // wxUSE_LISTBOX