]> git.saurik.com Git - wxWidgets.git/blob - src/osx/listbox_osx.cpp
Add support for VT_I8 (long long) values to wxAutomationObject.
[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 // 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 BEGIN_EVENT_TABLE(wxListBox, wxControl)
28 END_EVENT_TABLE()
29
30 #include "wx/osx/private.h"
31
32 // ============================================================================
33 // list box control implementation
34 // ============================================================================
35
36 wxListBox::wxListBox()
37 {
38 }
39
40 bool wxListBox::Create(
41 wxWindow *parent,
42 wxWindowID id,
43 const wxPoint& pos,
44 const wxSize& size,
45 const wxArrayString& choices,
46 long style,
47 const wxValidator& validator,
48 const wxString& name )
49 {
50 wxCArrayString chs(choices);
51
52 return Create(
53 parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
54 style, validator, name );
55 }
56
57 wxListWidgetImpl* wxListBox::GetListPeer() const
58 {
59 wxListWidgetImpl* impl = dynamic_cast<wxListWidgetImpl*> ( GetPeer() );
60 return impl;
61 }
62
63 bool wxListBox::Create(
64 wxWindow *parent,
65 wxWindowID id,
66 const wxPoint& pos,
67 const wxSize& size,
68 int n,
69 const wxString choices[],
70 long style,
71 const wxValidator& validator,
72 const wxString& name )
73 {
74 DontCreatePeer();
75 m_blockEvents = false;
76
77 if ( ! (style & wxNO_BORDER) )
78 style = (style & ~wxBORDER_MASK) | wxSUNKEN_BORDER ;
79
80 wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED),
81 wxT("only a single listbox selection mode can be specified") );
82
83 if ( !wxListBoxBase::Create( parent, id, pos, size, style & ~(wxHSCROLL | wxVSCROLL), validator, name ) )
84 return false;
85
86 if ( IsSorted() )
87 m_strings.sorted = new wxSortedArrayString;
88 else
89 m_strings.unsorted = new wxArrayString;
90
91 SetPeer(wxWidgetImpl::CreateListBox( this, parent, id, pos, size, style, GetExtraStyle() ));
92
93 MacPostControlCreate( pos, size );
94
95 m_textColumn = GetListPeer()->InsertTextColumn(0,wxEmptyString);
96
97 Append(n, choices);
98
99 // Needed because it is a wxControlWithItems
100 SetInitialSize( size );
101
102 return true;
103 }
104
105 wxListBox::~wxListBox()
106 {
107 m_blockEvents = true;
108 FreeData();
109 m_blockEvents = false;
110
111 // make sure no native events get sent to a object in destruction
112 SetPeer(NULL);
113
114 if ( IsSorted() )
115 delete m_strings.sorted;
116 else
117 delete m_strings.unsorted;
118
119 m_strings.sorted = NULL;
120 }
121
122 void wxListBox::FreeData()
123 {
124 if ( IsSorted() )
125 m_strings.sorted->Clear();
126 else
127 m_strings.unsorted->Clear();
128
129 m_itemsClientData.Clear();
130
131 GetListPeer()->ListClear();
132 }
133
134 void wxListBox::DoSetFirstItem(int n)
135 {
136 GetListPeer()->ListScrollTo( n );
137 }
138
139 void wxListBox::EnsureVisible(int n)
140 {
141 GetListPeer()->ListScrollTo( n );
142 }
143
144 void wxListBox::DoDeleteOneItem(unsigned int n)
145 {
146 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::Delete") );
147
148 m_blockEvents = true;
149 if ( IsSorted() )
150 m_strings.sorted->RemoveAt(n);
151 else
152 m_strings.unsorted->RemoveAt(n);
153
154 m_itemsClientData.RemoveAt(n);
155
156 GetListPeer()->ListDelete( n );
157 m_blockEvents = false;
158
159 UpdateOldSelections();
160 }
161
162 void wxListBox::DoClear()
163 {
164 m_blockEvents = true;
165 FreeData();
166 m_blockEvents = false;
167
168 UpdateOldSelections();
169 }
170
171 // ----------------------------------------------------------------------------
172 // selection
173 // ----------------------------------------------------------------------------
174
175 void wxListBox::DoSetSelection(int n, bool select)
176 {
177 wxCHECK_RET( n == wxNOT_FOUND || IsValid(n),
178 wxT("invalid index in wxListBox::SetSelection") );
179
180 m_blockEvents = true;
181
182 if ( n == wxNOT_FOUND )
183 GetListPeer()->ListDeselectAll();
184 else
185 GetListPeer()->ListSetSelection( n, select, HasMultipleSelection() );
186
187 m_blockEvents = false;
188
189 UpdateOldSelections();
190 }
191
192 bool wxListBox::IsSelected(int n) const
193 {
194 wxCHECK_MSG( IsValid(n), false, wxT("invalid index in wxListBox::Selected") );
195
196 return GetListPeer()->ListIsSelected( n );
197 }
198
199 // Return number of selections and an array of selected integers
200 int wxListBox::GetSelections(wxArrayInt& aSelections) const
201 {
202 return GetListPeer()->ListGetSelections( aSelections );
203 }
204
205 // Get single selection, for single choice list items
206 int wxListBox::GetSelection() const
207 {
208 return GetListPeer()->ListGetSelection();
209 }
210
211 int wxListBox::DoListHitTest(const wxPoint& inpoint) const
212 {
213 return GetListPeer()->DoListHitTest( inpoint );
214 }
215
216 // ----------------------------------------------------------------------------
217 // display
218 // ----------------------------------------------------------------------------
219
220 void wxListBox::GetValueCallback( unsigned int n, wxListWidgetColumn* col , wxListWidgetCellValue& value )
221 {
222 if ( col == m_textColumn )
223 value.Set( GetString( n ) );
224 }
225
226 void wxListBox::SetValueCallback( unsigned int WXUNUSED(n), wxListWidgetColumn* WXUNUSED(col) , wxListWidgetCellValue& WXUNUSED(value) )
227 {
228 }
229
230 wxSize wxListBox::DoGetBestSize() const
231 {
232 int lbWidth = 100; // some defaults
233 int lbHeight;
234 int wLine;
235
236 {
237 wxClientDC dc(const_cast<wxListBox*>(this));
238 dc.SetFont(GetFont());
239
240 // Find the widest line
241 for (unsigned int i = 0; i < GetCount(); i++)
242 {
243 wxString str( GetString( i ) );
244
245 wxCoord width, height ;
246 dc.GetTextExtent( str , &width, &height);
247 wLine = width ;
248 lbWidth = wxMax( lbWidth, wLine );
249 }
250
251 // Add room for the scrollbar
252 lbWidth += wxSystemSettings::GetMetric( wxSYS_VSCROLL_X );
253
254 // And just a bit more
255 int cy = 12;
256
257 wxCoord width, height ;
258 dc.GetTextExtent( wxT("XX") , &width, &height);
259 int cx = width ;
260 lbWidth += cx;
261
262 // don't make the listbox too tall (limit height to around 10 items)
263 // but don't make it too small neither
264 lbHeight = wxMax( (cy + 4) * wxMin( wxMax( GetCount(), 3 ), 10 ), 70 );
265 }
266
267 return wxSize( lbWidth, lbHeight );
268 }
269
270 void wxListBox::Refresh(bool eraseBack, const wxRect *rect)
271 {
272 wxControl::Refresh( eraseBack, rect );
273 }
274
275 // Some custom controls depend on this
276 /* static */ wxVisualAttributes
277 wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
278 {
279 wxVisualAttributes attr;
280
281 attr.colFg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
282 attr.colBg = wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX );
283 static wxFont font = wxFont(wxOSX_SYSTEM_FONT_VIEWS);
284 attr.font = font;
285
286 return attr;
287 }
288
289 // below is all code copied from univ
290
291 // ----------------------------------------------------------------------------
292 // client data handling
293 // ----------------------------------------------------------------------------
294
295 void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
296 {
297 m_itemsClientData[n] = clientData;
298 }
299
300 void *wxListBox::DoGetItemClientData(unsigned int n) const
301 {
302 return m_itemsClientData[n];
303 }
304
305 // ----------------------------------------------------------------------------
306 // accessing strings
307 // ----------------------------------------------------------------------------
308
309 unsigned int wxListBox::GetCount() const
310 {
311 return IsSorted() ? m_strings.sorted->size()
312 : m_strings.unsorted->size();
313 }
314
315 wxString wxListBox::GetString(unsigned int n) const
316 {
317 return IsSorted() ? m_strings.sorted->Item(n)
318 : m_strings.unsorted->Item(n);
319 }
320
321 int wxListBox::FindString(const wxString& s, bool bCase) const
322 {
323 return IsSorted() ? m_strings.sorted->Index(s, bCase)
324 : m_strings.unsorted->Index(s, bCase);
325 }
326
327 // ----------------------------------------------------------------------------
328 // adding/inserting strings
329 // ----------------------------------------------------------------------------
330
331 void wxListBox::OnItemInserted(unsigned int WXUNUSED(pos))
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