]>
Commit | Line | Data |
---|---|---|
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 | |
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 |
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 | m_blockEvents = false; | |
75 | m_macIsUserPane = false; | |
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 | ||
88 | m_peer = 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 | ||
102 | wxListBox::~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 |
5276b0a5 | 109 | wxDELETE(m_peer); |
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 | ||
119 | void 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 | ||
131 | void wxListBox::DoSetFirstItem(int n) | |
132 | { | |
133 | GetListPeer()->ListScrollTo( n ); | |
134 | } | |
135 | ||
136 | void wxListBox::EnsureVisible(int n) | |
137 | { | |
138 | GetListPeer()->ListScrollTo( n ); | |
139 | } | |
140 | ||
141 | void 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 | ||
159 | void 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 | ||
172 | void 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 | ||
189 | bool 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 | |
197 | int wxListBox::GetSelections(wxArrayInt& aSelections) const | |
198 | { | |
199 | return GetListPeer()->ListGetSelections( aSelections ); | |
200 | } | |
201 | ||
202 | // Get single selection, for single choice list items | |
203 | int wxListBox::GetSelection() const | |
204 | { | |
205 | return GetListPeer()->ListGetSelection(); | |
206 | } | |
207 | ||
20196e15 SC |
208 | int wxListBox::DoListHitTest(const wxPoint& inpoint) const |
209 | { | |
210 | return GetListPeer()->DoListHitTest( inpoint ); | |
211 | } | |
212 | ||
524c47aa SC |
213 | // ---------------------------------------------------------------------------- |
214 | // display | |
215 | // ---------------------------------------------------------------------------- | |
216 | ||
217 | void wxListBox::GetValueCallback( unsigned int n, wxListWidgetColumn* col , wxListWidgetCellValue& value ) | |
218 | { | |
219 | if ( col == m_textColumn ) | |
220 | value.Set( GetString( n ) ); | |
221 | } | |
222 | ||
0faf03bf | 223 | void wxListBox::SetValueCallback( unsigned int WXUNUSED(n), wxListWidgetColumn* WXUNUSED(col) , wxListWidgetCellValue& WXUNUSED(value) ) |
524c47aa SC |
224 | { |
225 | } | |
226 | ||
227 | wxSize 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 | ||
267 | void 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 | |
274 | wxListBox::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 | ||
292 | void wxListBox::DoSetItemClientData(unsigned int n, void* clientData) | |
293 | { | |
294 | m_itemsClientData[n] = clientData; | |
295 | } | |
296 | ||
297 | void *wxListBox::DoGetItemClientData(unsigned int n) const | |
298 | { | |
299 | return m_itemsClientData[n]; | |
300 | } | |
301 | ||
302 | // ---------------------------------------------------------------------------- | |
303 | // accessing strings | |
304 | // ---------------------------------------------------------------------------- | |
305 | ||
306 | unsigned int wxListBox::GetCount() const | |
307 | { | |
308 | return IsSorted() ? m_strings.sorted->size() | |
309 | : m_strings.unsorted->size(); | |
310 | } | |
311 | ||
312 | wxString wxListBox::GetString(unsigned int n) const | |
313 | { | |
314 | return IsSorted() ? m_strings.sorted->Item(n) | |
315 | : m_strings.unsorted->Item(n); | |
316 | } | |
317 | ||
318 | int 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 | ||
328 | void wxListBox::OnItemInserted(unsigned int WXUNUSED(pos)) | |
329 | { | |
330 | ||
331 | } | |
332 | ||
333 | int wxListBox::DoInsertItems(const wxArrayStringsAdapter& items, | |
334 | unsigned int pos, | |
335 | void **clientData, | |
336 | wxClientDataType type) | |
337 | { | |
338 | int idx = wxNOT_FOUND; | |
339 | unsigned int startpos = pos; | |
340 | ||
341 | const unsigned int numItems = items.GetCount(); | |
342 | for ( unsigned int i = 0; i < numItems; ++i ) | |
343 | { | |
344 | const wxString& item = items[i]; | |
345 | idx = IsSorted() ? m_strings.sorted->Add(item) | |
346 | : (m_strings.unsorted->Insert(item, pos), pos++); | |
347 | ||
348 | m_itemsClientData.Insert(NULL, idx); | |
349 | AssignNewItemClientData(idx, clientData, i, type); | |
350 | ||
351 | GetListPeer()->ListInsert(startpos+i); | |
352 | ||
353 | OnItemInserted(idx); | |
354 | } | |
355 | ||
356 | GetListPeer()->UpdateLineToEnd(startpos); | |
357 | ||
f5843805 VZ |
358 | // Inserting the items may scroll the listbox down to show the last |
359 | // selected one but we don't want to do it as it could result in e.g. the | |
360 | // first items of a listbox be hidden immediately after its creation so | |
361 | // show the first selected item instead. Ideal would probably be to | |
362 | // preserve the old selection unchanged, in fact, but I don't know how to | |
363 | // get the first visible item so for now do at least this. | |
364 | SetFirstItem(startpos); | |
365 | ||
524c47aa SC |
366 | UpdateOldSelections(); |
367 | ||
368 | return idx; | |
369 | } | |
370 | ||
371 | void wxListBox::SetString(unsigned int n, const wxString& s) | |
372 | { | |
9a83f860 | 373 | wxCHECK_RET( !IsSorted(), wxT("can't set string in sorted listbox") ); |
524c47aa SC |
374 | |
375 | if ( IsSorted() ) | |
376 | (*m_strings.sorted)[n] = s; | |
377 | else | |
378 | (*m_strings.unsorted)[n] = s; | |
379 | ||
380 | GetListPeer()->UpdateLine(n); | |
381 | } | |
382 | ||
21267321 SC |
383 | // |
384 | // common event handling | |
385 | // | |
386 | ||
387 | void wxListBox::HandleLineEvent( unsigned int n, bool doubleClick ) | |
388 | { | |
03647350 | 389 | wxCommandEvent event( doubleClick ? wxEVT_COMMAND_LISTBOX_DOUBLECLICKED : |
21267321 SC |
390 | wxEVT_COMMAND_LISTBOX_SELECTED, GetId() ); |
391 | event.SetEventObject( this ); | |
392 | if ( HasClientObjectData() ) | |
393 | event.SetClientObject( GetClientObject(n) ); | |
394 | else if ( HasClientUntypedData() ) | |
395 | event.SetClientData( GetClientData(n) ); | |
396 | event.SetString( GetString(n) ); | |
397 | event.SetInt( n ); | |
398 | event.SetExtraLong( 1 ); | |
399 | HandleWindowEvent(event); | |
400 | } | |
401 | ||
60ebcb8a SC |
402 | // |
403 | // common list cell value operations | |
03647350 | 404 | // |
60ebcb8a SC |
405 | |
406 | void wxListWidgetCellValue::Check( bool check ) | |
407 | { | |
408 | Set( check ? 1 : 0 ); | |
409 | } | |
410 | ||
411 | bool wxListWidgetCellValue::IsChecked() const | |
412 | { | |
413 | return GetIntValue() != 0; | |
414 | } | |
03647350 | 415 | |
60ebcb8a SC |
416 | |
417 | ||
524c47aa | 418 | #endif // wxUSE_LISTBOX |