]> git.saurik.com Git - wxWidgets.git/blame - src/osx/listbox_osx.cpp
Compilation fix for wxOSX.
[wxWidgets.git] / src / osx / listbox_osx.cpp
CommitLineData
524c47aa
SC
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/carbon/listbox.cpp
3// Purpose: wxListBox
4// Author: Stefan Csomor
5// Modified by:
6// Created: 1998-01-01
7// RCS-ID: $Id: listbox.cpp 54820 2008-07-29 20:04:11Z 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
27IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControlWithItems)
28
29BEGIN_EVENT_TABLE(wxListBox, wxControl)
30END_EVENT_TABLE()
31
32#include "wx/osx/private.h"
33
34// ============================================================================
35// list box control implementation
36// ============================================================================
37
38wxListBox::wxListBox()
39{
40}
41
42bool wxListBox::Create(
43 wxWindow *parent,
44 wxWindowID id,
45 const wxPoint& pos,
46 const wxSize& size,
47 const wxArrayString& choices,
48 long style,
49 const wxValidator& validator,
50 const wxString& name )
51{
52 wxCArrayString chs(choices);
53
54 return Create(
55 parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
56 style, validator, name );
57}
58
59wxListWidgetImpl* wxListBox::GetListPeer() const
60{
61 wxListWidgetImpl* impl = dynamic_cast<wxListWidgetImpl*> ( GetPeer() );
62 return impl;
63}
64
65bool wxListBox::Create(
66 wxWindow *parent,
67 wxWindowID id,
68 const wxPoint& pos,
69 const wxSize& size,
70 int n,
71 const wxString choices[],
72 long style,
73 const wxValidator& validator,
74 const wxString& name )
75{
76 m_blockEvents = false;
77 m_macIsUserPane = false;
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 m_peer = 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
5276b0a5 111 wxDELETE(m_peer);
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{
135 GetListPeer()->ListScrollTo( n );
136}
137
138void wxListBox::EnsureVisible(int n)
139{
140 GetListPeer()->ListScrollTo( n );
141}
142
143void wxListBox::DoDeleteOneItem(unsigned int n)
144{
145 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::Delete") );
146
147 m_blockEvents = true;
148 if ( IsSorted() )
149 m_strings.sorted->RemoveAt(n);
150 else
151 m_strings.unsorted->RemoveAt(n);
152
153 m_itemsClientData.RemoveAt(n);
154
155 GetListPeer()->ListDelete( n );
156 m_blockEvents = false;
03647350 157
524c47aa
SC
158 UpdateOldSelections();
159}
160
161void wxListBox::DoClear()
162{
163 m_blockEvents = true;
164 FreeData();
165 m_blockEvents = false;
03647350 166
524c47aa
SC
167 UpdateOldSelections();
168}
169
170// ----------------------------------------------------------------------------
171// selection
172// ----------------------------------------------------------------------------
173
174void wxListBox::DoSetSelection(int n, bool select)
175{
176 wxCHECK_RET( n == wxNOT_FOUND || IsValid(n),
177 wxT("invalid index in wxListBox::SetSelection") );
178
179 m_blockEvents = true;
03647350 180
524c47aa
SC
181 if ( n == wxNOT_FOUND )
182 GetListPeer()->ListDeselectAll();
183 else
184 GetListPeer()->ListSetSelection( n, select, HasMultipleSelection() );
03647350 185
524c47aa 186 m_blockEvents = false;
03647350 187
524c47aa
SC
188 UpdateOldSelections();
189}
190
191bool wxListBox::IsSelected(int n) const
192{
193 wxCHECK_MSG( IsValid(n), false, wxT("invalid index in wxListBox::Selected") );
194
195 return GetListPeer()->ListIsSelected( n );
196}
197
198// Return number of selections and an array of selected integers
199int wxListBox::GetSelections(wxArrayInt& aSelections) const
200{
201 return GetListPeer()->ListGetSelections( aSelections );
202}
203
204// Get single selection, for single choice list items
205int wxListBox::GetSelection() const
206{
207 return GetListPeer()->ListGetSelection();
208}
209
20196e15
SC
210int wxListBox::DoListHitTest(const wxPoint& inpoint) const
211{
212 return GetListPeer()->DoListHitTest( inpoint );
213}
214
524c47aa
SC
215// ----------------------------------------------------------------------------
216// display
217// ----------------------------------------------------------------------------
218
219void wxListBox::GetValueCallback( unsigned int n, wxListWidgetColumn* col , wxListWidgetCellValue& value )
220{
221 if ( col == m_textColumn )
222 value.Set( GetString( n ) );
223}
224
0faf03bf 225void wxListBox::SetValueCallback( unsigned int WXUNUSED(n), wxListWidgetColumn* WXUNUSED(col) , wxListWidgetCellValue& WXUNUSED(value) )
524c47aa
SC
226{
227}
228
229wxSize wxListBox::DoGetBestSize() const
230{
231 int lbWidth = 100; // some defaults
65391c8f 232 int lbHeight;
524c47aa
SC
233 int wLine;
234
235 {
236 wxClientDC dc(const_cast<wxListBox*>(this));
237 dc.SetFont(GetFont());
238
239 // Find the widest line
240 for (unsigned int i = 0; i < GetCount(); i++)
241 {
242 wxString str( GetString( i ) );
243
244 wxCoord width, height ;
245 dc.GetTextExtent( str , &width, &height);
246 wLine = width ;
247 lbWidth = wxMax( lbWidth, wLine );
248 }
249
250 // Add room for the scrollbar
251 lbWidth += wxSystemSettings::GetMetric( wxSYS_VSCROLL_X );
252
253 // And just a bit more
254 int cy = 12;
255
256 wxCoord width, height ;
257 dc.GetTextExtent( wxT("XX") , &width, &height);
258 int cx = width ;
259 lbWidth += cx;
260
261 // don't make the listbox too tall (limit height to around 10 items)
262 // but don't make it too small neither
263 lbHeight = wxMax( (cy + 4) * wxMin( wxMax( GetCount(), 3 ), 10 ), 70 );
264 }
265
266 return wxSize( lbWidth, lbHeight );
267}
268
269void wxListBox::Refresh(bool eraseBack, const wxRect *rect)
270{
271 wxControl::Refresh( eraseBack, rect );
272}
273
274// Some custom controls depend on this
275/* static */ wxVisualAttributes
276wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
277{
278 wxVisualAttributes attr;
279
280 attr.colFg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
281 attr.colBg = wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX );
7eb8aeb8
SC
282 static wxFont font = wxFont(wxOSX_SYSTEM_FONT_VIEWS);
283 attr.font = font;
524c47aa
SC
284
285 return attr;
286}
287
288// below is all code copied from univ
289
290// ----------------------------------------------------------------------------
291// client data handling
292// ----------------------------------------------------------------------------
293
294void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
295{
296 m_itemsClientData[n] = clientData;
297}
298
299void *wxListBox::DoGetItemClientData(unsigned int n) const
300{
301 return m_itemsClientData[n];
302}
303
304// ----------------------------------------------------------------------------
305// accessing strings
306// ----------------------------------------------------------------------------
307
308unsigned int wxListBox::GetCount() const
309{
310 return IsSorted() ? m_strings.sorted->size()
311 : m_strings.unsorted->size();
312}
313
314wxString wxListBox::GetString(unsigned int n) const
315{
316 return IsSorted() ? m_strings.sorted->Item(n)
317 : m_strings.unsorted->Item(n);
318}
319
320int wxListBox::FindString(const wxString& s, bool bCase) const
321{
322 return IsSorted() ? m_strings.sorted->Index(s, bCase)
323 : m_strings.unsorted->Index(s, bCase);
324}
325
326// ----------------------------------------------------------------------------
327// adding/inserting strings
328// ----------------------------------------------------------------------------
329
330void wxListBox::OnItemInserted(unsigned int WXUNUSED(pos))
331{
332
333}
334
335int 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 UpdateOldSelections();
361
362 return idx;
363}
364
365void wxListBox::SetString(unsigned int n, const wxString& s)
366{
9a83f860 367 wxCHECK_RET( !IsSorted(), wxT("can't set string in sorted listbox") );
524c47aa
SC
368
369 if ( IsSorted() )
370 (*m_strings.sorted)[n] = s;
371 else
372 (*m_strings.unsorted)[n] = s;
373
374 GetListPeer()->UpdateLine(n);
375}
376
21267321
SC
377//
378// common event handling
379//
380
381void wxListBox::HandleLineEvent( unsigned int n, bool doubleClick )
382{
03647350 383 wxCommandEvent event( doubleClick ? wxEVT_COMMAND_LISTBOX_DOUBLECLICKED :
21267321
SC
384 wxEVT_COMMAND_LISTBOX_SELECTED, GetId() );
385 event.SetEventObject( this );
386 if ( HasClientObjectData() )
387 event.SetClientObject( GetClientObject(n) );
388 else if ( HasClientUntypedData() )
389 event.SetClientData( GetClientData(n) );
390 event.SetString( GetString(n) );
391 event.SetInt( n );
392 event.SetExtraLong( 1 );
393 HandleWindowEvent(event);
394}
395
60ebcb8a
SC
396//
397// common list cell value operations
03647350 398//
60ebcb8a
SC
399
400void wxListWidgetCellValue::Check( bool check )
401{
402 Set( check ? 1 : 0 );
403}
404
405bool wxListWidgetCellValue::IsChecked() const
406{
407 return GetIntValue() != 0;
408}
03647350 409
60ebcb8a
SC
410
411
524c47aa 412#endif // wxUSE_LISTBOX