]> git.saurik.com Git - wxWidgets.git/blame - src/osx/listbox_osx.cpp
Move code removing "-psn_xxx" command line arguments to common code.
[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
524c47aa
SC
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
524c47aa
SC
26BEGIN_EVENT_TABLE(wxListBox, wxControl)
27END_EVENT_TABLE()
28
29#include "wx/osx/private.h"
30
31// ============================================================================
32// list box control implementation
33// ============================================================================
34
35wxListBox::wxListBox()
36{
37}
38
39bool 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
56wxListWidgetImpl* wxListBox::GetListPeer() const
57{
58 wxListWidgetImpl* impl = dynamic_cast<wxListWidgetImpl*> ( GetPeer() );
59 return impl;
60}
61
62bool 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{
d15694e8 73 DontCreatePeer();
524c47aa 74 m_blockEvents = false;
524c47aa 75
69fa3d26
JS
76 if ( ! (style & wxNO_BORDER) )
77 style = (style & ~wxBORDER_MASK) | wxSUNKEN_BORDER ;
78
524c47aa
SC
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
22756322 90 SetPeer(wxWidgetImpl::CreateListBox( this, parent, id, pos, size, style, GetExtraStyle() ));
03647350 91
524c47aa
SC
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
104wxListBox::~wxListBox()
105{
7a21d83c 106 m_blockEvents = true;
524c47aa 107 FreeData();
7a21d83c
SC
108 m_blockEvents = false;
109
524c47aa 110 // make sure no native events get sent to a object in destruction
22756322 111 SetPeer(NULL);
524c47aa
SC
112
113 if ( IsSorted() )
114 delete m_strings.sorted;
115 else
116 delete m_strings.unsorted;
117
118 m_strings.sorted = NULL;
119}
120
121void 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
133void wxListBox::DoSetFirstItem(int n)
134{
8e19598f
SC
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 );
524c47aa
SC
140 GetListPeer()->ListScrollTo( n );
141}
142
143void wxListBox::EnsureVisible(int n)
144{
145 GetListPeer()->ListScrollTo( n );
146}
147
148void 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;
03647350 162
524c47aa
SC
163 UpdateOldSelections();
164}
165
166void wxListBox::DoClear()
167{
168 m_blockEvents = true;
169 FreeData();
170 m_blockEvents = false;
03647350 171
524c47aa
SC
172 UpdateOldSelections();
173}
174
175// ----------------------------------------------------------------------------
176// selection
177// ----------------------------------------------------------------------------
178
179void 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;
03647350 185
524c47aa
SC
186 if ( n == wxNOT_FOUND )
187 GetListPeer()->ListDeselectAll();
188 else
189 GetListPeer()->ListSetSelection( n, select, HasMultipleSelection() );
03647350 190
524c47aa 191 m_blockEvents = false;
03647350 192
524c47aa
SC
193 UpdateOldSelections();
194}
195
196bool 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
204int wxListBox::GetSelections(wxArrayInt& aSelections) const
205{
206 return GetListPeer()->ListGetSelections( aSelections );
207}
208
209// Get single selection, for single choice list items
210int wxListBox::GetSelection() const
211{
212 return GetListPeer()->ListGetSelection();
213}
214
20196e15
SC
215int wxListBox::DoListHitTest(const wxPoint& inpoint) const
216{
217 return GetListPeer()->DoListHitTest( inpoint );
218}
219
524c47aa
SC
220// ----------------------------------------------------------------------------
221// display
222// ----------------------------------------------------------------------------
223
224void wxListBox::GetValueCallback( unsigned int n, wxListWidgetColumn* col , wxListWidgetCellValue& value )
225{
226 if ( col == m_textColumn )
227 value.Set( GetString( n ) );
228}
229
0faf03bf 230void wxListBox::SetValueCallback( unsigned int WXUNUSED(n), wxListWidgetColumn* WXUNUSED(col) , wxListWidgetCellValue& WXUNUSED(value) )
524c47aa
SC
231{
232}
233
234wxSize wxListBox::DoGetBestSize() const
235{
236 int lbWidth = 100; // some defaults
65391c8f 237 int lbHeight;
524c47aa
SC
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
274void 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
281wxListBox::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 );
7eb8aeb8
SC
287 static wxFont font = wxFont(wxOSX_SYSTEM_FONT_VIEWS);
288 attr.font = font;
524c47aa
SC
289
290 return attr;
291}
292
293// below is all code copied from univ
294
295// ----------------------------------------------------------------------------
296// client data handling
297// ----------------------------------------------------------------------------
298
299void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
300{
301 m_itemsClientData[n] = clientData;
302}
303
304void *wxListBox::DoGetItemClientData(unsigned int n) const
305{
306 return m_itemsClientData[n];
307}
308
309// ----------------------------------------------------------------------------
310// accessing strings
311// ----------------------------------------------------------------------------
312
313unsigned int wxListBox::GetCount() const
314{
315 return IsSorted() ? m_strings.sorted->size()
316 : m_strings.unsorted->size();
317}
318
319wxString wxListBox::GetString(unsigned int n) const
320{
321 return IsSorted() ? m_strings.sorted->Item(n)
322 : m_strings.unsorted->Item(n);
323}
324
325int 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
335void wxListBox::OnItemInserted(unsigned int WXUNUSED(pos))
336{
524c47aa
SC
337}
338
339int 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
f5843805
VZ
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
524c47aa
SC
372 UpdateOldSelections();
373
374 return idx;
375}
376
377void wxListBox::SetString(unsigned int n, const wxString& s)
378{
9a83f860 379 wxCHECK_RET( !IsSorted(), wxT("can't set string in sorted listbox") );
524c47aa
SC
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
21267321
SC
389//
390// common event handling
391//
392
393void wxListBox::HandleLineEvent( unsigned int n, bool doubleClick )
394{
ce7fe42e
VZ
395 wxCommandEvent event( doubleClick ? wxEVT_LISTBOX_DCLICK :
396 wxEVT_LISTBOX, GetId() );
21267321
SC
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
60ebcb8a
SC
408//
409// common list cell value operations
03647350 410//
60ebcb8a
SC
411
412void wxListWidgetCellValue::Check( bool check )
413{
414 Set( check ? 1 : 0 );
415}
416
417bool wxListWidgetCellValue::IsChecked() const
418{
419 return GetIntValue() != 0;
420}
03647350 421
60ebcb8a
SC
422
423
524c47aa 424#endif // wxUSE_LISTBOX