]> git.saurik.com Git - wxWidgets.git/blame - src/osx/listbox_osx.cpp
guarding open combo box against AppDefined NSEvents issued by wxEventLoop::WakeUp...
[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 76
69fa3d26
JS
77 if ( ! (style & wxNO_BORDER) )
78 style = (style & ~wxBORDER_MASK) | wxSUNKEN_BORDER ;
79
524c47aa
SC
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
22756322 91 SetPeer(wxWidgetImpl::CreateListBox( this, parent, id, pos, size, style, GetExtraStyle() ));
03647350 92
524c47aa
SC
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
105wxListBox::~wxListBox()
106{
7a21d83c 107 m_blockEvents = true;
524c47aa 108 FreeData();
7a21d83c
SC
109 m_blockEvents = false;
110
524c47aa 111 // make sure no native events get sent to a object in destruction
22756322 112 SetPeer(NULL);
524c47aa
SC
113
114 if ( IsSorted() )
115 delete m_strings.sorted;
116 else
117 delete m_strings.unsorted;
118
119 m_strings.sorted = NULL;
120}
121
122void 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
134void wxListBox::DoSetFirstItem(int n)
135{
8e19598f
SC
136 // osx actually only has an implementation for ensuring the visibility of a row, it does so
137 // by scrolling the minimal amount necessary from the current scrolling position.
138 // in order to get the same behaviour I'd have to make sure first that the last line is visible,
139 // followed by a scrollRowToVisible for the desired line
140 GetListPeer()->ListScrollTo( GetCount()-1 );
524c47aa
SC
141 GetListPeer()->ListScrollTo( n );
142}
143
144void wxListBox::EnsureVisible(int n)
145{
146 GetListPeer()->ListScrollTo( n );
147}
148
149void wxListBox::DoDeleteOneItem(unsigned int n)
150{
151 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::Delete") );
152
153 m_blockEvents = true;
154 if ( IsSorted() )
155 m_strings.sorted->RemoveAt(n);
156 else
157 m_strings.unsorted->RemoveAt(n);
158
159 m_itemsClientData.RemoveAt(n);
160
161 GetListPeer()->ListDelete( n );
162 m_blockEvents = false;
03647350 163
524c47aa
SC
164 UpdateOldSelections();
165}
166
167void wxListBox::DoClear()
168{
169 m_blockEvents = true;
170 FreeData();
171 m_blockEvents = false;
03647350 172
524c47aa
SC
173 UpdateOldSelections();
174}
175
176// ----------------------------------------------------------------------------
177// selection
178// ----------------------------------------------------------------------------
179
180void wxListBox::DoSetSelection(int n, bool select)
181{
182 wxCHECK_RET( n == wxNOT_FOUND || IsValid(n),
183 wxT("invalid index in wxListBox::SetSelection") );
184
185 m_blockEvents = true;
03647350 186
524c47aa
SC
187 if ( n == wxNOT_FOUND )
188 GetListPeer()->ListDeselectAll();
189 else
190 GetListPeer()->ListSetSelection( n, select, HasMultipleSelection() );
03647350 191
524c47aa 192 m_blockEvents = false;
03647350 193
524c47aa
SC
194 UpdateOldSelections();
195}
196
197bool wxListBox::IsSelected(int n) const
198{
199 wxCHECK_MSG( IsValid(n), false, wxT("invalid index in wxListBox::Selected") );
200
201 return GetListPeer()->ListIsSelected( n );
202}
203
204// Return number of selections and an array of selected integers
205int wxListBox::GetSelections(wxArrayInt& aSelections) const
206{
207 return GetListPeer()->ListGetSelections( aSelections );
208}
209
210// Get single selection, for single choice list items
211int wxListBox::GetSelection() const
212{
213 return GetListPeer()->ListGetSelection();
214}
215
20196e15
SC
216int wxListBox::DoListHitTest(const wxPoint& inpoint) const
217{
218 return GetListPeer()->DoListHitTest( inpoint );
219}
220
524c47aa
SC
221// ----------------------------------------------------------------------------
222// display
223// ----------------------------------------------------------------------------
224
225void wxListBox::GetValueCallback( unsigned int n, wxListWidgetColumn* col , wxListWidgetCellValue& value )
226{
227 if ( col == m_textColumn )
228 value.Set( GetString( n ) );
229}
230
0faf03bf 231void wxListBox::SetValueCallback( unsigned int WXUNUSED(n), wxListWidgetColumn* WXUNUSED(col) , wxListWidgetCellValue& WXUNUSED(value) )
524c47aa
SC
232{
233}
234
235wxSize wxListBox::DoGetBestSize() const
236{
237 int lbWidth = 100; // some defaults
65391c8f 238 int lbHeight;
524c47aa
SC
239 int wLine;
240
241 {
242 wxClientDC dc(const_cast<wxListBox*>(this));
243 dc.SetFont(GetFont());
244
245 // Find the widest line
246 for (unsigned int i = 0; i < GetCount(); i++)
247 {
248 wxString str( GetString( i ) );
249
250 wxCoord width, height ;
251 dc.GetTextExtent( str , &width, &height);
252 wLine = width ;
253 lbWidth = wxMax( lbWidth, wLine );
254 }
255
256 // Add room for the scrollbar
257 lbWidth += wxSystemSettings::GetMetric( wxSYS_VSCROLL_X );
258
259 // And just a bit more
260 int cy = 12;
261
262 wxCoord width, height ;
263 dc.GetTextExtent( wxT("XX") , &width, &height);
264 int cx = width ;
265 lbWidth += cx;
266
267 // don't make the listbox too tall (limit height to around 10 items)
268 // but don't make it too small neither
269 lbHeight = wxMax( (cy + 4) * wxMin( wxMax( GetCount(), 3 ), 10 ), 70 );
270 }
271
272 return wxSize( lbWidth, lbHeight );
273}
274
275void wxListBox::Refresh(bool eraseBack, const wxRect *rect)
276{
277 wxControl::Refresh( eraseBack, rect );
278}
279
280// Some custom controls depend on this
281/* static */ wxVisualAttributes
282wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
283{
284 wxVisualAttributes attr;
285
286 attr.colFg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
287 attr.colBg = wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX );
7eb8aeb8
SC
288 static wxFont font = wxFont(wxOSX_SYSTEM_FONT_VIEWS);
289 attr.font = font;
524c47aa
SC
290
291 return attr;
292}
293
294// below is all code copied from univ
295
296// ----------------------------------------------------------------------------
297// client data handling
298// ----------------------------------------------------------------------------
299
300void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
301{
302 m_itemsClientData[n] = clientData;
303}
304
305void *wxListBox::DoGetItemClientData(unsigned int n) const
306{
307 return m_itemsClientData[n];
308}
309
310// ----------------------------------------------------------------------------
311// accessing strings
312// ----------------------------------------------------------------------------
313
314unsigned int wxListBox::GetCount() const
315{
316 return IsSorted() ? m_strings.sorted->size()
317 : m_strings.unsorted->size();
318}
319
320wxString wxListBox::GetString(unsigned int n) const
321{
322 return IsSorted() ? m_strings.sorted->Item(n)
323 : m_strings.unsorted->Item(n);
324}
325
326int wxListBox::FindString(const wxString& s, bool bCase) const
327{
328 return IsSorted() ? m_strings.sorted->Index(s, bCase)
329 : m_strings.unsorted->Index(s, bCase);
330}
331
332// ----------------------------------------------------------------------------
333// adding/inserting strings
334// ----------------------------------------------------------------------------
335
336void wxListBox::OnItemInserted(unsigned int WXUNUSED(pos))
337{
524c47aa
SC
338}
339
340int wxListBox::DoInsertItems(const wxArrayStringsAdapter& items,
341 unsigned int pos,
342 void **clientData,
343 wxClientDataType type)
344{
345 int idx = wxNOT_FOUND;
346 unsigned int startpos = pos;
347
348 const unsigned int numItems = items.GetCount();
349 for ( unsigned int i = 0; i < numItems; ++i )
350 {
351 const wxString& item = items[i];
352 idx = IsSorted() ? m_strings.sorted->Add(item)
353 : (m_strings.unsorted->Insert(item, pos), pos++);
354
355 m_itemsClientData.Insert(NULL, idx);
356 AssignNewItemClientData(idx, clientData, i, type);
357
358 GetListPeer()->ListInsert(startpos+i);
359
360 OnItemInserted(idx);
361 }
362
363 GetListPeer()->UpdateLineToEnd(startpos);
364
f5843805
VZ
365 // Inserting the items may scroll the listbox down to show the last
366 // selected one but we don't want to do it as it could result in e.g. the
367 // first items of a listbox be hidden immediately after its creation so
368 // show the first selected item instead. Ideal would probably be to
369 // preserve the old selection unchanged, in fact, but I don't know how to
370 // get the first visible item so for now do at least this.
371 SetFirstItem(startpos);
372
524c47aa
SC
373 UpdateOldSelections();
374
375 return idx;
376}
377
378void wxListBox::SetString(unsigned int n, const wxString& s)
379{
9a83f860 380 wxCHECK_RET( !IsSorted(), wxT("can't set string in sorted listbox") );
524c47aa
SC
381
382 if ( IsSorted() )
383 (*m_strings.sorted)[n] = s;
384 else
385 (*m_strings.unsorted)[n] = s;
386
387 GetListPeer()->UpdateLine(n);
388}
389
21267321
SC
390//
391// common event handling
392//
393
394void wxListBox::HandleLineEvent( unsigned int n, bool doubleClick )
395{
ce7fe42e
VZ
396 wxCommandEvent event( doubleClick ? wxEVT_LISTBOX_DCLICK :
397 wxEVT_LISTBOX, GetId() );
21267321
SC
398 event.SetEventObject( this );
399 if ( HasClientObjectData() )
400 event.SetClientObject( GetClientObject(n) );
401 else if ( HasClientUntypedData() )
402 event.SetClientData( GetClientData(n) );
403 event.SetString( GetString(n) );
404 event.SetInt( n );
405 event.SetExtraLong( 1 );
406 HandleWindowEvent(event);
407}
408
60ebcb8a
SC
409//
410// common list cell value operations
03647350 411//
60ebcb8a
SC
412
413void wxListWidgetCellValue::Check( bool check )
414{
415 Set( check ? 1 : 0 );
416}
417
418bool wxListWidgetCellValue::IsChecked() const
419{
420 return GetIntValue() != 0;
421}
03647350 422
60ebcb8a
SC
423
424
524c47aa 425#endif // wxUSE_LISTBOX