]>
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() ); | |
91 | ||
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 | { | |
106 | FreeData(); | |
107 | // make sure no native events get sent to a object in destruction | |
108 | delete m_peer; | |
109 | m_peer = NULL; | |
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; | |
155 | ||
156 | UpdateOldSelections(); | |
157 | } | |
158 | ||
159 | void wxListBox::DoClear() | |
160 | { | |
161 | m_blockEvents = true; | |
162 | FreeData(); | |
163 | m_blockEvents = false; | |
164 | ||
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; | |
178 | ||
179 | if ( n == wxNOT_FOUND ) | |
180 | GetListPeer()->ListDeselectAll(); | |
181 | else | |
182 | GetListPeer()->ListSetSelection( n, select, HasMultipleSelection() ); | |
183 | ||
184 | m_blockEvents = false; | |
185 | ||
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 | ||
208 | // ---------------------------------------------------------------------------- | |
209 | // display | |
210 | // ---------------------------------------------------------------------------- | |
211 | ||
212 | void wxListBox::GetValueCallback( unsigned int n, wxListWidgetColumn* col , wxListWidgetCellValue& value ) | |
213 | { | |
214 | if ( col == m_textColumn ) | |
215 | value.Set( GetString( n ) ); | |
216 | } | |
217 | ||
0faf03bf | 218 | void wxListBox::SetValueCallback( unsigned int WXUNUSED(n), wxListWidgetColumn* WXUNUSED(col) , wxListWidgetCellValue& WXUNUSED(value) ) |
524c47aa SC |
219 | { |
220 | } | |
221 | ||
222 | wxSize wxListBox::DoGetBestSize() const | |
223 | { | |
224 | int lbWidth = 100; // some defaults | |
225 | int lbHeight = 110; | |
226 | int wLine; | |
227 | ||
228 | { | |
229 | wxClientDC dc(const_cast<wxListBox*>(this)); | |
230 | dc.SetFont(GetFont()); | |
231 | ||
232 | // Find the widest line | |
233 | for (unsigned int i = 0; i < GetCount(); i++) | |
234 | { | |
235 | wxString str( GetString( i ) ); | |
236 | ||
237 | wxCoord width, height ; | |
238 | dc.GetTextExtent( str , &width, &height); | |
239 | wLine = width ; | |
240 | lbWidth = wxMax( lbWidth, wLine ); | |
241 | } | |
242 | ||
243 | // Add room for the scrollbar | |
244 | lbWidth += wxSystemSettings::GetMetric( wxSYS_VSCROLL_X ); | |
245 | ||
246 | // And just a bit more | |
247 | int cy = 12; | |
248 | ||
249 | wxCoord width, height ; | |
250 | dc.GetTextExtent( wxT("XX") , &width, &height); | |
251 | int cx = width ; | |
252 | lbWidth += cx; | |
253 | ||
254 | // don't make the listbox too tall (limit height to around 10 items) | |
255 | // but don't make it too small neither | |
256 | lbHeight = wxMax( (cy + 4) * wxMin( wxMax( GetCount(), 3 ), 10 ), 70 ); | |
257 | } | |
258 | ||
259 | return wxSize( lbWidth, lbHeight ); | |
260 | } | |
261 | ||
262 | void wxListBox::Refresh(bool eraseBack, const wxRect *rect) | |
263 | { | |
264 | wxControl::Refresh( eraseBack, rect ); | |
265 | } | |
266 | ||
267 | // Some custom controls depend on this | |
268 | /* static */ wxVisualAttributes | |
269 | wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
270 | { | |
271 | wxVisualAttributes attr; | |
272 | ||
273 | attr.colFg = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT ); | |
274 | attr.colBg = wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX ); | |
f1c40652 | 275 | attr.font.CreateSystemFont(wxOSX_SYSTEM_FONT_VIEWS); |
524c47aa SC |
276 | |
277 | return attr; | |
278 | } | |
279 | ||
280 | // below is all code copied from univ | |
281 | ||
282 | // ---------------------------------------------------------------------------- | |
283 | // client data handling | |
284 | // ---------------------------------------------------------------------------- | |
285 | ||
286 | void wxListBox::DoSetItemClientData(unsigned int n, void* clientData) | |
287 | { | |
288 | m_itemsClientData[n] = clientData; | |
289 | } | |
290 | ||
291 | void *wxListBox::DoGetItemClientData(unsigned int n) const | |
292 | { | |
293 | return m_itemsClientData[n]; | |
294 | } | |
295 | ||
296 | // ---------------------------------------------------------------------------- | |
297 | // accessing strings | |
298 | // ---------------------------------------------------------------------------- | |
299 | ||
300 | unsigned int wxListBox::GetCount() const | |
301 | { | |
302 | return IsSorted() ? m_strings.sorted->size() | |
303 | : m_strings.unsorted->size(); | |
304 | } | |
305 | ||
306 | wxString wxListBox::GetString(unsigned int n) const | |
307 | { | |
308 | return IsSorted() ? m_strings.sorted->Item(n) | |
309 | : m_strings.unsorted->Item(n); | |
310 | } | |
311 | ||
312 | int wxListBox::FindString(const wxString& s, bool bCase) const | |
313 | { | |
314 | return IsSorted() ? m_strings.sorted->Index(s, bCase) | |
315 | : m_strings.unsorted->Index(s, bCase); | |
316 | } | |
317 | ||
318 | // ---------------------------------------------------------------------------- | |
319 | // adding/inserting strings | |
320 | // ---------------------------------------------------------------------------- | |
321 | ||
322 | void wxListBox::OnItemInserted(unsigned int WXUNUSED(pos)) | |
323 | { | |
324 | ||
325 | } | |
326 | ||
327 | int wxListBox::DoInsertItems(const wxArrayStringsAdapter& items, | |
328 | unsigned int pos, | |
329 | void **clientData, | |
330 | wxClientDataType type) | |
331 | { | |
332 | int idx = wxNOT_FOUND; | |
333 | unsigned int startpos = pos; | |
334 | ||
335 | const unsigned int numItems = items.GetCount(); | |
336 | for ( unsigned int i = 0; i < numItems; ++i ) | |
337 | { | |
338 | const wxString& item = items[i]; | |
339 | idx = IsSorted() ? m_strings.sorted->Add(item) | |
340 | : (m_strings.unsorted->Insert(item, pos), pos++); | |
341 | ||
342 | m_itemsClientData.Insert(NULL, idx); | |
343 | AssignNewItemClientData(idx, clientData, i, type); | |
344 | ||
345 | GetListPeer()->ListInsert(startpos+i); | |
346 | ||
347 | OnItemInserted(idx); | |
348 | } | |
349 | ||
350 | GetListPeer()->UpdateLineToEnd(startpos); | |
351 | ||
352 | UpdateOldSelections(); | |
353 | ||
354 | return idx; | |
355 | } | |
356 | ||
357 | void wxListBox::SetString(unsigned int n, const wxString& s) | |
358 | { | |
359 | wxCHECK_RET( !IsSorted(), _T("can't set string in sorted listbox") ); | |
360 | ||
361 | if ( IsSorted() ) | |
362 | (*m_strings.sorted)[n] = s; | |
363 | else | |
364 | (*m_strings.unsorted)[n] = s; | |
365 | ||
366 | GetListPeer()->UpdateLine(n); | |
367 | } | |
368 | ||
21267321 SC |
369 | // |
370 | // common event handling | |
371 | // | |
372 | ||
373 | void wxListBox::HandleLineEvent( unsigned int n, bool doubleClick ) | |
374 | { | |
375 | wxCommandEvent event( doubleClick ? wxEVT_COMMAND_LISTBOX_DOUBLECLICKED : | |
376 | wxEVT_COMMAND_LISTBOX_SELECTED, GetId() ); | |
377 | event.SetEventObject( this ); | |
378 | if ( HasClientObjectData() ) | |
379 | event.SetClientObject( GetClientObject(n) ); | |
380 | else if ( HasClientUntypedData() ) | |
381 | event.SetClientData( GetClientData(n) ); | |
382 | event.SetString( GetString(n) ); | |
383 | event.SetInt( n ); | |
384 | event.SetExtraLong( 1 ); | |
385 | HandleWindowEvent(event); | |
386 | } | |
387 | ||
524c47aa | 388 | #endif // wxUSE_LISTBOX |