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