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