]>
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 | |
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 | ||
27 | IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControlWithItems) | |
28 | ||
29 | BEGIN_EVENT_TABLE(wxListBox, wxControl) | |
30 | END_EVENT_TABLE() | |
31 | ||
32 | #include "wx/osx/private.h" | |
33 | ||
34 | // ============================================================================ | |
35 | // list box control implementation | |
36 | // ============================================================================ | |
37 | ||
38 | wxListBox::wxListBox() | |
39 | { | |
40 | } | |
41 | ||
42 | bool 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 | ||
59 | wxListWidgetImpl* wxListBox::GetListPeer() const | |
60 | { | |
61 | wxListWidgetImpl* impl = dynamic_cast<wxListWidgetImpl*> ( GetPeer() ); | |
62 | return impl; | |
63 | } | |
64 | ||
65 | bool 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 | ||
104 | wxListBox::~wxListBox() | |
105 | { | |
7a21d83c | 106 | m_blockEvents = true; |
524c47aa | 107 | FreeData(); |
7a21d83c SC |
108 | m_blockEvents = false; |
109 | ||
524c47aa SC |
110 | // make sure no native events get sent to a object in destruction |
111 | delete m_peer; | |
112 | m_peer = 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 | GetListPeer()->ListScrollTo( n ); | |
137 | } | |
138 | ||
139 | void wxListBox::EnsureVisible(int n) | |
140 | { | |
141 | GetListPeer()->ListScrollTo( n ); | |
142 | } | |
143 | ||
144 | void wxListBox::DoDeleteOneItem(unsigned int n) | |
145 | { | |
146 | wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::Delete") ); | |
147 | ||
148 | m_blockEvents = true; | |
149 | if ( IsSorted() ) | |
150 | m_strings.sorted->RemoveAt(n); | |
151 | else | |
152 | m_strings.unsorted->RemoveAt(n); | |
153 | ||
154 | m_itemsClientData.RemoveAt(n); | |
155 | ||
156 | GetListPeer()->ListDelete( n ); | |
157 | m_blockEvents = false; | |
03647350 | 158 | |
524c47aa SC |
159 | UpdateOldSelections(); |
160 | } | |
161 | ||
162 | void wxListBox::DoClear() | |
163 | { | |
164 | m_blockEvents = true; | |
165 | FreeData(); | |
166 | m_blockEvents = false; | |
03647350 | 167 | |
524c47aa SC |
168 | UpdateOldSelections(); |
169 | } | |
170 | ||
171 | // ---------------------------------------------------------------------------- | |
172 | // selection | |
173 | // ---------------------------------------------------------------------------- | |
174 | ||
175 | void wxListBox::DoSetSelection(int n, bool select) | |
176 | { | |
177 | wxCHECK_RET( n == wxNOT_FOUND || IsValid(n), | |
178 | wxT("invalid index in wxListBox::SetSelection") ); | |
179 | ||
180 | m_blockEvents = true; | |
03647350 | 181 | |
524c47aa SC |
182 | if ( n == wxNOT_FOUND ) |
183 | GetListPeer()->ListDeselectAll(); | |
184 | else | |
185 | GetListPeer()->ListSetSelection( n, select, HasMultipleSelection() ); | |
03647350 | 186 | |
524c47aa | 187 | m_blockEvents = false; |
03647350 | 188 | |
524c47aa SC |
189 | UpdateOldSelections(); |
190 | } | |
191 | ||
192 | bool wxListBox::IsSelected(int n) const | |
193 | { | |
194 | wxCHECK_MSG( IsValid(n), false, wxT("invalid index in wxListBox::Selected") ); | |
195 | ||
196 | return GetListPeer()->ListIsSelected( n ); | |
197 | } | |
198 | ||
199 | // Return number of selections and an array of selected integers | |
200 | int wxListBox::GetSelections(wxArrayInt& aSelections) const | |
201 | { | |
202 | return GetListPeer()->ListGetSelections( aSelections ); | |
203 | } | |
204 | ||
205 | // Get single selection, for single choice list items | |
206 | int wxListBox::GetSelection() const | |
207 | { | |
208 | return GetListPeer()->ListGetSelection(); | |
209 | } | |
210 | ||
211 | // ---------------------------------------------------------------------------- | |
212 | // display | |
213 | // ---------------------------------------------------------------------------- | |
214 | ||
215 | void wxListBox::GetValueCallback( unsigned int n, wxListWidgetColumn* col , wxListWidgetCellValue& value ) | |
216 | { | |
217 | if ( col == m_textColumn ) | |
218 | value.Set( GetString( n ) ); | |
219 | } | |
220 | ||
0faf03bf | 221 | void wxListBox::SetValueCallback( unsigned int WXUNUSED(n), wxListWidgetColumn* WXUNUSED(col) , wxListWidgetCellValue& WXUNUSED(value) ) |
524c47aa SC |
222 | { |
223 | } | |
224 | ||
225 | wxSize wxListBox::DoGetBestSize() const | |
226 | { | |
227 | int lbWidth = 100; // some defaults | |
228 | int lbHeight = 110; | |
229 | int wLine; | |
230 | ||
231 | { | |
232 | wxClientDC dc(const_cast<wxListBox*>(this)); | |
233 | dc.SetFont(GetFont()); | |
234 | ||
235 | // Find the widest line | |
236 | for (unsigned int i = 0; i < GetCount(); i++) | |
237 | { | |
238 | wxString str( GetString( i ) ); | |
239 | ||
240 | wxCoord width, height ; | |
241 | dc.GetTextExtent( str , &width, &height); | |
242 | wLine = width ; | |
243 | lbWidth = wxMax( lbWidth, wLine ); | |
244 | } | |
245 | ||
246 | // Add room for the scrollbar | |
247 | lbWidth += wxSystemSettings::GetMetric( wxSYS_VSCROLL_X ); | |
248 | ||
249 | // And just a bit more | |
250 | int cy = 12; | |
251 | ||
252 | wxCoord width, height ; | |
253 | dc.GetTextExtent( wxT("XX") , &width, &height); | |
254 | int cx = width ; | |
255 | lbWidth += cx; | |
256 | ||
257 | // don't make the listbox too tall (limit height to around 10 items) | |
258 | // but don't make it too small neither | |
259 | lbHeight = wxMax( (cy + 4) * wxMin( wxMax( GetCount(), 3 ), 10 ), 70 ); | |
260 | } | |
261 | ||
262 | return wxSize( lbWidth, lbHeight ); | |
263 | } | |
264 | ||
265 | void wxListBox::Refresh(bool eraseBack, const wxRect *rect) | |
266 | { | |
267 | wxControl::Refresh( eraseBack, rect ); | |
268 | } | |
269 | ||
270 | // Some custom controls depend on this | |
271 | /* static */ wxVisualAttributes | |
272 | wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
273 | { | |
274 | wxVisualAttributes attr; | |
275 | ||
276 | attr.colFg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT ); | |
277 | attr.colBg = wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX ); | |
f1c40652 | 278 | attr.font.CreateSystemFont(wxOSX_SYSTEM_FONT_VIEWS); |
524c47aa SC |
279 | |
280 | return attr; | |
281 | } | |
282 | ||
283 | // below is all code copied from univ | |
284 | ||
285 | // ---------------------------------------------------------------------------- | |
286 | // client data handling | |
287 | // ---------------------------------------------------------------------------- | |
288 | ||
289 | void wxListBox::DoSetItemClientData(unsigned int n, void* clientData) | |
290 | { | |
291 | m_itemsClientData[n] = clientData; | |
292 | } | |
293 | ||
294 | void *wxListBox::DoGetItemClientData(unsigned int n) const | |
295 | { | |
296 | return m_itemsClientData[n]; | |
297 | } | |
298 | ||
299 | // ---------------------------------------------------------------------------- | |
300 | // accessing strings | |
301 | // ---------------------------------------------------------------------------- | |
302 | ||
303 | unsigned int wxListBox::GetCount() const | |
304 | { | |
305 | return IsSorted() ? m_strings.sorted->size() | |
306 | : m_strings.unsorted->size(); | |
307 | } | |
308 | ||
309 | wxString wxListBox::GetString(unsigned int n) const | |
310 | { | |
311 | return IsSorted() ? m_strings.sorted->Item(n) | |
312 | : m_strings.unsorted->Item(n); | |
313 | } | |
314 | ||
315 | int wxListBox::FindString(const wxString& s, bool bCase) const | |
316 | { | |
317 | return IsSorted() ? m_strings.sorted->Index(s, bCase) | |
318 | : m_strings.unsorted->Index(s, bCase); | |
319 | } | |
320 | ||
321 | // ---------------------------------------------------------------------------- | |
322 | // adding/inserting strings | |
323 | // ---------------------------------------------------------------------------- | |
324 | ||
325 | void wxListBox::OnItemInserted(unsigned int WXUNUSED(pos)) | |
326 | { | |
327 | ||
328 | } | |
329 | ||
330 | int wxListBox::DoInsertItems(const wxArrayStringsAdapter& items, | |
331 | unsigned int pos, | |
332 | void **clientData, | |
333 | wxClientDataType type) | |
334 | { | |
335 | int idx = wxNOT_FOUND; | |
336 | unsigned int startpos = pos; | |
337 | ||
338 | const unsigned int numItems = items.GetCount(); | |
339 | for ( unsigned int i = 0; i < numItems; ++i ) | |
340 | { | |
341 | const wxString& item = items[i]; | |
342 | idx = IsSorted() ? m_strings.sorted->Add(item) | |
343 | : (m_strings.unsorted->Insert(item, pos), pos++); | |
344 | ||
345 | m_itemsClientData.Insert(NULL, idx); | |
346 | AssignNewItemClientData(idx, clientData, i, type); | |
347 | ||
348 | GetListPeer()->ListInsert(startpos+i); | |
349 | ||
350 | OnItemInserted(idx); | |
351 | } | |
352 | ||
353 | GetListPeer()->UpdateLineToEnd(startpos); | |
354 | ||
355 | UpdateOldSelections(); | |
356 | ||
357 | return idx; | |
358 | } | |
359 | ||
360 | void wxListBox::SetString(unsigned int n, const wxString& s) | |
361 | { | |
9a83f860 | 362 | wxCHECK_RET( !IsSorted(), wxT("can't set string in sorted listbox") ); |
524c47aa SC |
363 | |
364 | if ( IsSorted() ) | |
365 | (*m_strings.sorted)[n] = s; | |
366 | else | |
367 | (*m_strings.unsorted)[n] = s; | |
368 | ||
369 | GetListPeer()->UpdateLine(n); | |
370 | } | |
371 | ||
21267321 SC |
372 | // |
373 | // common event handling | |
374 | // | |
375 | ||
376 | void wxListBox::HandleLineEvent( unsigned int n, bool doubleClick ) | |
377 | { | |
03647350 | 378 | wxCommandEvent event( doubleClick ? wxEVT_COMMAND_LISTBOX_DOUBLECLICKED : |
21267321 SC |
379 | wxEVT_COMMAND_LISTBOX_SELECTED, GetId() ); |
380 | event.SetEventObject( this ); | |
381 | if ( HasClientObjectData() ) | |
382 | event.SetClientObject( GetClientObject(n) ); | |
383 | else if ( HasClientUntypedData() ) | |
384 | event.SetClientData( GetClientData(n) ); | |
385 | event.SetString( GetString(n) ); | |
386 | event.SetInt( n ); | |
387 | event.SetExtraLong( 1 ); | |
388 | HandleWindowEvent(event); | |
389 | } | |
390 | ||
60ebcb8a SC |
391 | // |
392 | // common list cell value operations | |
03647350 | 393 | // |
60ebcb8a SC |
394 | |
395 | void wxListWidgetCellValue::Check( bool check ) | |
396 | { | |
397 | Set( check ? 1 : 0 ); | |
398 | } | |
399 | ||
400 | bool wxListWidgetCellValue::IsChecked() const | |
401 | { | |
402 | return GetIntValue() != 0; | |
403 | } | |
03647350 | 404 | |
60ebcb8a SC |
405 | |
406 | ||
524c47aa | 407 | #endif // wxUSE_LISTBOX |