]> git.saurik.com Git - wxWidgets.git/blob - src/osx/listbox_osx.cpp
wxMSW: Use TBSTYLE_AUTOSIZE for toolbar buttons with horizontal text.
[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 // 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 );
141 GetListPeer()->ListScrollTo( n );
142 }
143
144 void wxListBox::EnsureVisible(int n)
145 {
146 GetListPeer()->ListScrollTo( n );
147 }
148
149 void 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;
163
164 UpdateOldSelections();
165 }
166
167 void wxListBox::DoClear()
168 {
169 m_blockEvents = true;
170 FreeData();
171 m_blockEvents = false;
172
173 UpdateOldSelections();
174 }
175
176 // ----------------------------------------------------------------------------
177 // selection
178 // ----------------------------------------------------------------------------
179
180 void 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;
186
187 if ( n == wxNOT_FOUND )
188 GetListPeer()->ListDeselectAll();
189 else
190 GetListPeer()->ListSetSelection( n, select, HasMultipleSelection() );
191
192 m_blockEvents = false;
193
194 UpdateOldSelections();
195 }
196
197 bool 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
205 int wxListBox::GetSelections(wxArrayInt& aSelections) const
206 {
207 return GetListPeer()->ListGetSelections( aSelections );
208 }
209
210 // Get single selection, for single choice list items
211 int wxListBox::GetSelection() const
212 {
213 return GetListPeer()->ListGetSelection();
214 }
215
216 int wxListBox::DoListHitTest(const wxPoint& inpoint) const
217 {
218 return GetListPeer()->DoListHitTest( inpoint );
219 }
220
221 // ----------------------------------------------------------------------------
222 // display
223 // ----------------------------------------------------------------------------
224
225 void wxListBox::GetValueCallback( unsigned int n, wxListWidgetColumn* col , wxListWidgetCellValue& value )
226 {
227 if ( col == m_textColumn )
228 value.Set( GetString( n ) );
229 }
230
231 void wxListBox::SetValueCallback( unsigned int WXUNUSED(n), wxListWidgetColumn* WXUNUSED(col) , wxListWidgetCellValue& WXUNUSED(value) )
232 {
233 }
234
235 wxSize wxListBox::DoGetBestSize() const
236 {
237 int lbWidth = 100; // some defaults
238 int lbHeight;
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
275 void 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
282 wxListBox::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 );
288 static wxFont font = wxFont(wxOSX_SYSTEM_FONT_VIEWS);
289 attr.font = font;
290
291 return attr;
292 }
293
294 // below is all code copied from univ
295
296 // ----------------------------------------------------------------------------
297 // client data handling
298 // ----------------------------------------------------------------------------
299
300 void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
301 {
302 m_itemsClientData[n] = clientData;
303 }
304
305 void *wxListBox::DoGetItemClientData(unsigned int n) const
306 {
307 return m_itemsClientData[n];
308 }
309
310 // ----------------------------------------------------------------------------
311 // accessing strings
312 // ----------------------------------------------------------------------------
313
314 unsigned int wxListBox::GetCount() const
315 {
316 return IsSorted() ? m_strings.sorted->size()
317 : m_strings.unsorted->size();
318 }
319
320 wxString wxListBox::GetString(unsigned int n) const
321 {
322 return IsSorted() ? m_strings.sorted->Item(n)
323 : m_strings.unsorted->Item(n);
324 }
325
326 int 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
336 void wxListBox::OnItemInserted(unsigned int WXUNUSED(pos))
337 {
338 }
339
340 int 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
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
373 UpdateOldSelections();
374
375 return idx;
376 }
377
378 void wxListBox::SetString(unsigned int n, const wxString& s)
379 {
380 wxCHECK_RET( !IsSorted(), wxT("can't set string in sorted listbox") );
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
390 //
391 // common event handling
392 //
393
394 void wxListBox::HandleLineEvent( unsigned int n, bool doubleClick )
395 {
396 wxCommandEvent event( doubleClick ? wxEVT_COMMAND_LISTBOX_DOUBLECLICKED :
397 wxEVT_COMMAND_LISTBOX_SELECTED, GetId() );
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
409 //
410 // common list cell value operations
411 //
412
413 void wxListWidgetCellValue::Check( bool check )
414 {
415 Set( check ? 1 : 0 );
416 }
417
418 bool wxListWidgetCellValue::IsChecked() const
419 {
420 return GetIntValue() != 0;
421 }
422
423
424
425 #endif // wxUSE_LISTBOX