]>
Commit | Line | Data |
---|---|---|
e9576ca5 SC |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: listbox.cpp | |
3 | // Purpose: wxListBox | |
a31a5f85 | 4 | // Author: Stefan Csomor |
e9576ca5 | 5 | // Modified by: |
a31a5f85 | 6 | // Created: 1998-01-01 |
e9576ca5 | 7 | // RCS-ID: $Id$ |
a31a5f85 | 8 | // Copyright: (c) Stefan Csomor |
65571936 | 9 | // Licence: wxWindows licence |
e9576ca5 SC |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
3d1a4878 | 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
e9576ca5 SC |
13 | #pragma implementation "listbox.h" |
14 | #endif | |
15 | ||
3d1a4878 SC |
16 | #include "wx/wxprec.h" |
17 | ||
179e085f RN |
18 | #if wxUSE_LISTBOX |
19 | ||
03e11df5 | 20 | #include "wx/app.h" |
e9576ca5 | 21 | #include "wx/listbox.h" |
dc0ace7c | 22 | #include "wx/button.h" |
e9576ca5 | 23 | #include "wx/settings.h" |
422644a3 | 24 | #include "wx/toplevel.h" |
e9576ca5 SC |
25 | #include "wx/dynarray.h" |
26 | #include "wx/log.h" | |
27 | ||
519cb848 | 28 | #include "wx/utils.h" |
519cb848 | 29 | |
2f1ae414 | 30 | #if !USE_SHARED_LIBRARY |
e40298d5 | 31 | IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl) |
519cb848 SC |
32 | |
33 | BEGIN_EVENT_TABLE(wxListBox, wxControl) | |
684e0b31 | 34 | #ifndef __WXMAC_OSX__ |
393b27ad | 35 | // EVT_SIZE( wxListBox::OnSize ) |
e40298d5 | 36 | EVT_CHAR( wxListBox::OnChar ) |
facd6764 | 37 | #endif |
519cb848 | 38 | END_EVENT_TABLE() |
2f1ae414 | 39 | #endif |
e9576ca5 | 40 | |
facd6764 SC |
41 | #include "wx/mac/uma.h" |
42 | ||
a9fc5eec SC |
43 | const short kTextColumnId = 1024 ; |
44 | ||
facd6764 | 45 | // new databrowserbased version |
8e0f22c0 | 46 | // because of the limited insert |
c6179a84 | 47 | // functionality of DataBrowser, |
8e0f22c0 SC |
48 | // we just introduce id s corresponding |
49 | // to the line number | |
facd6764 | 50 | |
83ce5634 | 51 | #if TARGET_API_MAC_OSX |
c6179a84 | 52 | static pascal void DataBrowserItemNotificationProc(ControlRef browser, DataBrowserItemID itemID, |
83ce5634 SC |
53 | DataBrowserItemNotification message, DataBrowserItemDataRef itemData) |
54 | #else | |
c6179a84 | 55 | static pascal void DataBrowserItemNotificationProc(ControlRef browser, DataBrowserItemID itemID, |
83ce5634 SC |
56 | DataBrowserItemNotification message) |
57 | #endif | |
58 | { | |
59 | long ref = GetControlReference( browser ) ; | |
60 | if ( ref ) | |
61 | { | |
469d8d5d | 62 | wxListBox* list = wxDynamicCast( (wxObject*) ref , wxListBox ) ; |
45285a62 | 63 | int i = itemID - 1 ; |
8e0f22c0 SC |
64 | if (i >= 0 && i < list->GetCount() ) |
65 | { | |
66 | bool trigger = false ; | |
67 | wxCommandEvent event( | |
68 | wxEVT_COMMAND_LISTBOX_SELECTED, list->GetId() ); | |
69 | switch( message ) | |
83ce5634 | 70 | { |
8e0f22c0 SC |
71 | case kDataBrowserItemDeselected : |
72 | if ( list->HasMultipleSelection() ) | |
fe3dc505 | 73 | trigger = !list->MacIsSelectionSuppressed() ; |
8e0f22c0 SC |
74 | break ; |
75 | case kDataBrowserItemSelected : | |
fe3dc505 | 76 | trigger = !list->MacIsSelectionSuppressed() ; |
8e0f22c0 SC |
77 | break ; |
78 | case kDataBrowserItemDoubleClicked : | |
79 | event.SetEventType(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED) ; | |
80 | trigger = true ; | |
81 | break ; | |
82 | default : | |
83 | break ; | |
83ce5634 | 84 | } |
8e0f22c0 SC |
85 | if ( trigger ) |
86 | { | |
87 | event.SetEventObject( list ); | |
88 | if ( list->HasClientObjectData() ) | |
89 | event.SetClientObject( list->GetClientObject(i) ); | |
90 | else if ( list->HasClientUntypedData() ) | |
91 | event.SetClientData( list->GetClientData(i) ); | |
92 | event.SetString( list->GetString(i) ); | |
93 | event.SetInt(i) ; | |
94 | event.SetExtraLong( list->HasMultipleSelection() ? message == kDataBrowserItemSelected : TRUE ); | |
95 | wxPostEvent( list->GetEventHandler() , event ) ; | |
96 | // direct notification is not always having the listbox GetSelection() having in synch with event | |
c6179a84 VZ |
97 | // list->GetEventHandler()->ProcessEvent(event) ; |
98 | } | |
8e0f22c0 | 99 | } |
83ce5634 SC |
100 | } |
101 | } | |
102 | ||
c6179a84 VZ |
103 | static pascal OSStatus ListBoxGetSetItemData(ControlRef browser, |
104 | DataBrowserItemID itemID, DataBrowserPropertyID property, | |
facd6764 SC |
105 | DataBrowserItemDataRef itemData, Boolean changeValue) |
106 | { | |
107 | OSStatus err = errDataBrowserPropertyNotSupported; | |
c6179a84 | 108 | |
facd6764 | 109 | if ( ! changeValue ) |
c6179a84 | 110 | { |
facd6764 SC |
111 | switch (property) |
112 | { | |
c6179a84 | 113 | |
a9fc5eec | 114 | case kTextColumnId: |
c6179a84 | 115 | { |
facd6764 SC |
116 | long ref = GetControlReference( browser ) ; |
117 | if ( ref ) | |
118 | { | |
469d8d5d | 119 | wxListBox* list = wxDynamicCast( (wxObject*) ref , wxListBox ) ; |
45285a62 | 120 | int i = itemID - 1 ; |
8e0f22c0 SC |
121 | if (i >= 0 && i < list->GetCount() ) |
122 | { | |
123 | wxMacCFStringHolder cf( list->GetString(i) , list->GetFont().GetEncoding() ) ; | |
124 | verify_noerr( ::SetDataBrowserItemDataText( itemData , cf ) ) ; | |
125 | err = noErr ; | |
126 | } | |
facd6764 | 127 | } |
c6179a84 | 128 | } |
facd6764 | 129 | break; |
c6179a84 | 130 | |
facd6764 | 131 | default: |
c6179a84 | 132 | |
facd6764 SC |
133 | break; |
134 | } | |
135 | } | |
c6179a84 | 136 | |
facd6764 SC |
137 | return err; |
138 | } | |
fe3dc505 SC |
139 | |
140 | ||
141 | // Listbox item | |
142 | wxListBox::wxListBox() | |
143 | { | |
144 | m_noItems = 0; | |
145 | m_selected = 0; | |
146 | m_macList = NULL ; | |
147 | m_suppressSelection = false ; | |
148 | } | |
149 | ||
150 | bool wxListBox::Create(wxWindow *parent, wxWindowID id, | |
151 | const wxPoint& pos, | |
152 | const wxSize& size, | |
153 | const wxArrayString& choices, | |
154 | long style, | |
155 | const wxValidator& validator, | |
156 | const wxString& name) | |
157 | { | |
158 | wxCArrayString chs(choices); | |
159 | ||
160 | return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(), | |
161 | style, validator, name); | |
162 | } | |
163 | ||
facd6764 SC |
164 | bool wxListBox::Create(wxWindow *parent, wxWindowID id, |
165 | const wxPoint& pos, | |
166 | const wxSize& size, | |
167 | int n, const wxString choices[], | |
168 | long style, | |
169 | const wxValidator& validator, | |
170 | const wxString& name) | |
171 | { | |
172 | m_macIsUserPane = FALSE ; | |
5e6f42cd SC |
173 | |
174 | wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED), | |
175 | _T("only one of listbox selection modes can be specified") ); | |
c6179a84 | 176 | |
facd6764 SC |
177 | if ( !wxListBoxBase::Create(parent, id, pos, size, style & ~(wxHSCROLL|wxVSCROLL), validator, name) ) |
178 | return false; | |
179 | ||
180 | m_noItems = 0 ; // this will be increased by our append command | |
181 | m_selected = 0; | |
facd6764 SC |
182 | |
183 | Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ; | |
facd6764 | 184 | |
b905d6cc | 185 | m_peer = new wxMacControl(this) ; |
5ca0d812 | 186 | verify_noerr( ::CreateDataBrowserControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds, kDataBrowserListView , m_peer->GetControlRefAddr() ) ); |
facd6764 SC |
187 | |
188 | DataBrowserSelectionFlags options = kDataBrowserDragSelect ; | |
189 | if ( style & wxLB_MULTIPLE ) | |
190 | { | |
191 | options += kDataBrowserAlwaysExtendSelection + kDataBrowserCmdTogglesSelection ; | |
192 | } | |
193 | else if ( style & wxLB_EXTENDED ) | |
194 | { | |
195 | // default behaviour | |
196 | } | |
197 | else | |
198 | { | |
199 | options += kDataBrowserSelectOnlyOne ; | |
200 | } | |
c6179a84 | 201 | verify_noerr(m_peer->SetSelectionFlags( options ) ); |
facd6764 SC |
202 | |
203 | DataBrowserListViewColumnDesc columnDesc ; | |
204 | columnDesc.headerBtnDesc.titleOffset = 0; | |
205 | columnDesc.headerBtnDesc.version = kDataBrowserListViewLatestHeaderDesc; | |
c6179a84 VZ |
206 | |
207 | columnDesc.headerBtnDesc.btnFontStyle.flags = | |
facd6764 | 208 | kControlUseFontMask | kControlUseJustMask; |
c6179a84 | 209 | |
facd6764 SC |
210 | columnDesc.headerBtnDesc.btnContentInfo.contentType = kControlNoContent; |
211 | columnDesc.propertyDesc.propertyType = kDataBrowserTextType; | |
212 | columnDesc.headerBtnDesc.btnFontStyle.just = teFlushDefault; | |
213 | columnDesc.headerBtnDesc.minimumWidth = 0; | |
214 | columnDesc.headerBtnDesc.maximumWidth = 10000; | |
c6179a84 | 215 | |
facd6764 SC |
216 | columnDesc.headerBtnDesc.btnFontStyle.font = kControlFontViewSystemFont; |
217 | columnDesc.headerBtnDesc.btnFontStyle.style = normal; | |
218 | columnDesc.headerBtnDesc.titleString = NULL ; // CFSTR( "" ); | |
219 | ||
a9fc5eec | 220 | columnDesc.propertyDesc.propertyID = kTextColumnId; |
facd6764 | 221 | columnDesc.propertyDesc.propertyType = kDataBrowserTextType; |
c2697b87 | 222 | columnDesc.propertyDesc.propertyFlags = |
9bd2d050 | 223 | #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2 |
c6179a84 | 224 | kDataBrowserListViewTypeSelectColumn | |
c2697b87 SC |
225 | #endif |
226 | kDataBrowserTableViewSelectionColumn ; | |
facd6764 | 227 | |
c6179a84 | 228 | |
5ca0d812 SC |
229 | verify_noerr(m_peer->AddListViewColumn( &columnDesc, kDataBrowserListViewAppendColumn) ) ; |
230 | verify_noerr(m_peer->AutoSizeListViewColumns() ) ; | |
231 | verify_noerr(m_peer->SetHasScrollBars(false , true ) ) ; | |
232 | verify_noerr(m_peer->SetTableViewHiliteStyle(kDataBrowserTableViewFillHilite ) ) ; | |
233 | verify_noerr(m_peer->SetListViewHeaderBtnHeight( 0 ) ) ; | |
facd6764 | 234 | DataBrowserCallbacks callbacks ; |
c6179a84 | 235 | |
facd6764 | 236 | callbacks.version = kDataBrowserLatestCallbacks; |
c6179a84 | 237 | |
facd6764 | 238 | InitDataBrowserCallbacks(&callbacks); |
c6179a84 VZ |
239 | |
240 | callbacks.u.v1.itemDataCallback = | |
facd6764 | 241 | NewDataBrowserItemDataUPP(ListBoxGetSetItemData); |
c6179a84 | 242 | |
83ce5634 SC |
243 | callbacks.u.v1.itemNotificationCallback = |
244 | #if TARGET_API_MAC_OSX | |
245 | (DataBrowserItemNotificationUPP) NewDataBrowserItemNotificationWithItemUPP(DataBrowserItemNotificationProc) ; | |
246 | #else | |
247 | NewDataBrowserItemNotificationUPP(DataBrowserItemNotificationProc) ; | |
248 | #endif | |
5ca0d812 | 249 | m_peer->SetCallbacks( &callbacks); |
facd6764 | 250 | |
8e0f22c0 SC |
251 | #if TARGET_API_MAC_OSX |
252 | // there is a redraw bug in 10.2.X | |
253 | if ( UMAGetSystemVersion() < 0x1030 ) | |
254 | m_peer->SetData( kControlNoPart, kControlDataBrowserIncludesFrameAndFocusTag, (Boolean) false ) ; | |
255 | #endif | |
facd6764 SC |
256 | MacPostControlCreate(pos,size) ; |
257 | ||
258 | for ( int i = 0 ; i < n ; i++ ) | |
259 | { | |
260 | Append( choices[i] ) ; | |
261 | } | |
262 | ||
d3b5db4b | 263 | SetBestSize(size); // Needed because it is a wxControlWithItems |
c6179a84 | 264 | |
facd6764 SC |
265 | return TRUE; |
266 | } | |
267 | ||
268 | wxListBox::~wxListBox() | |
269 | { | |
77eddfb7 | 270 | m_peer->SetReference( 0 ) ; |
facd6764 SC |
271 | FreeData() ; |
272 | // avoid access during destruction | |
273 | if ( m_macList ) | |
274 | { | |
275 | m_macList = NULL ; | |
276 | } | |
277 | } | |
278 | ||
279 | void wxListBox::FreeData() | |
280 | { | |
281 | #if wxUSE_OWNER_DRAWN | |
282 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
283 | { | |
284 | size_t uiCount = m_aItems.Count(); | |
285 | while ( uiCount-- != 0 ) { | |
286 | delete m_aItems[uiCount]; | |
287 | m_aItems[uiCount] = NULL; | |
288 | } | |
289 | ||
290 | m_aItems.Clear(); | |
291 | } | |
292 | else | |
293 | #endif // wxUSE_OWNER_DRAWN | |
294 | if ( HasClientObjectData() ) | |
295 | { | |
296 | for ( size_t n = 0; n < (size_t)m_noItems; n++ ) | |
297 | { | |
298 | delete GetClientObject(n); | |
299 | } | |
300 | } | |
301 | } | |
302 | ||
303 | void wxListBox::DoSetSize(int x, int y, | |
304 | int width, int height, | |
305 | int sizeFlags ) | |
306 | { | |
307 | wxControl::DoSetSize( x , y , width , height , sizeFlags ) ; | |
308 | } | |
309 | ||
310 | void wxListBox::DoSetFirstItem(int N) | |
311 | { | |
312 | MacScrollTo( N ) ; | |
313 | } | |
314 | ||
315 | void wxListBox::Delete(int N) | |
316 | { | |
317 | wxCHECK_RET( N >= 0 && N < m_noItems, | |
318 | wxT("invalid index in wxListBox::Delete") ); | |
319 | ||
320 | #if wxUSE_OWNER_DRAWN | |
321 | delete m_aItems[N]; | |
322 | m_aItems.RemoveAt(N); | |
323 | #else // !wxUSE_OWNER_DRAWN | |
324 | if ( HasClientObjectData() ) | |
325 | { | |
326 | delete GetClientObject(N); | |
327 | } | |
328 | #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN | |
329 | m_stringArray.RemoveAt( N ) ; | |
330 | m_dataArray.RemoveAt( N ) ; | |
331 | m_noItems --; | |
332 | ||
333 | MacDelete( N ) ; | |
334 | } | |
335 | ||
336 | int wxListBox::DoAppend(const wxString& item) | |
337 | { | |
9f884528 RD |
338 | InvalidateBestSize(); |
339 | ||
facd6764 SC |
340 | int index = m_noItems ; |
341 | m_stringArray.Add( item ) ; | |
342 | m_dataArray.Add( NULL ); | |
343 | m_noItems ++; | |
344 | DoSetItemClientData( index , NULL ) ; | |
345 | MacAppend( item ) ; | |
346 | ||
347 | return index ; | |
348 | } | |
349 | ||
350 | void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData) | |
351 | { | |
352 | Clear() ; | |
353 | int n = choices.GetCount(); | |
c6179a84 | 354 | |
facd6764 SC |
355 | for( int i = 0 ; i < n ; ++i ) |
356 | { | |
357 | if ( clientData ) | |
358 | { | |
359 | #if wxUSE_OWNER_DRAWN | |
360 | wxASSERT_MSG(clientData[i] == NULL, | |
361 | wxT("Can't use client data with owner-drawn listboxes")); | |
362 | #else // !wxUSE_OWNER_DRAWN | |
363 | Append( choices[i] , clientData[i] ) ; | |
364 | #endif | |
365 | } | |
366 | else | |
367 | Append( choices[i] ) ; | |
368 | } | |
c6179a84 | 369 | |
facd6764 SC |
370 | #if wxUSE_OWNER_DRAWN |
371 | if ( m_windowStyle & wxLB_OWNERDRAW ) { | |
372 | // first delete old items | |
373 | size_t ui = m_aItems.Count(); | |
374 | while ( ui-- != 0 ) { | |
375 | delete m_aItems[ui]; | |
376 | m_aItems[ui] = NULL; | |
377 | } | |
378 | m_aItems.Empty(); | |
c6179a84 | 379 | |
facd6764 SC |
380 | // then create new ones |
381 | for ( ui = 0; ui < (size_t)m_noItems; ui++ ) { | |
382 | wxOwnerDrawn *pNewItem = CreateItem(ui); | |
383 | pNewItem->SetName(choices[ui]); | |
384 | m_aItems.Add(pNewItem); | |
385 | } | |
386 | } | |
387 | #endif // wxUSE_OWNER_DRAWN | |
388 | } | |
389 | ||
facd6764 SC |
390 | int wxListBox::FindString(const wxString& s) const |
391 | { | |
c6179a84 | 392 | |
facd6764 SC |
393 | if ( s.Right(1) == wxT("*") ) |
394 | { | |
395 | wxString search = s.Left( s.Length() - 1 ) ; | |
396 | int len = search.Length() ; | |
397 | Str255 s1 , s2 ; | |
398 | wxMacStringToPascal( search , s2 ) ; | |
c6179a84 | 399 | |
facd6764 SC |
400 | for ( int i = 0 ; i < m_noItems ; ++ i ) |
401 | { | |
402 | wxMacStringToPascal( m_stringArray[i].Left( len ) , s1 ) ; | |
403 | ||
404 | if ( EqualString( s1 , s2 , false , false ) ) | |
405 | return i ; | |
406 | } | |
407 | if ( s.Left(1) == wxT("*") && s.Length() > 1 ) | |
408 | { | |
409 | wxString st = s ; | |
410 | st.MakeLower() ; | |
411 | for ( int i = 0 ; i < m_noItems ; ++i ) | |
412 | { | |
413 | if ( GetString(i).Lower().Matches(st) ) | |
414 | return i ; | |
415 | } | |
416 | } | |
c6179a84 | 417 | |
facd6764 SC |
418 | } |
419 | else | |
420 | { | |
421 | Str255 s1 , s2 ; | |
c6179a84 | 422 | |
facd6764 | 423 | wxMacStringToPascal( s , s2 ) ; |
c6179a84 | 424 | |
facd6764 SC |
425 | for ( int i = 0 ; i < m_noItems ; ++ i ) |
426 | { | |
427 | wxMacStringToPascal( m_stringArray[i] , s1 ) ; | |
428 | ||
429 | if ( EqualString( s1 , s2 , false , false ) ) | |
430 | return i ; | |
431 | } | |
432 | } | |
433 | return -1; | |
434 | } | |
435 | ||
436 | void wxListBox::Clear() | |
437 | { | |
438 | FreeData(); | |
439 | m_noItems = 0; | |
440 | m_stringArray.Empty() ; | |
441 | m_dataArray.Empty() ; | |
442 | MacClear() ; | |
443 | } | |
444 | ||
c6179a84 | 445 | void wxListBox::DoSetSelection(int N, bool select) |
facd6764 | 446 | { |
13220cca | 447 | wxCHECK_RET( N == wxNOT_FOUND || (N >= 0 && N < m_noItems) , |
facd6764 | 448 | wxT("invalid index in wxListBox::SetSelection") ); |
c6179a84 | 449 | |
fe3dc505 SC |
450 | if ( N == wxNOT_FOUND ) |
451 | MacDeselectAll() ; | |
452 | else | |
453 | MacSetSelection( N , select ) ; | |
facd6764 SC |
454 | } |
455 | ||
456 | bool wxListBox::IsSelected(int N) const | |
457 | { | |
458 | wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE, | |
459 | wxT("invalid index in wxListBox::Selected") ); | |
c6179a84 | 460 | |
facd6764 SC |
461 | return MacIsSelected( N ) ; |
462 | } | |
463 | ||
464 | void *wxListBox::DoGetItemClientData(int N) const | |
465 | { | |
466 | wxCHECK_MSG( N >= 0 && N < m_noItems, NULL, | |
467 | wxT("invalid index in wxListBox::GetClientData")); | |
c6179a84 | 468 | |
facd6764 SC |
469 | return (void *)m_dataArray[N]; |
470 | } | |
471 | ||
472 | wxClientData *wxListBox::DoGetItemClientObject(int N) const | |
473 | { | |
474 | return (wxClientData *) DoGetItemClientData( N ) ; | |
475 | } | |
476 | ||
477 | void wxListBox::DoSetItemClientData(int N, void *Client_data) | |
478 | { | |
479 | wxCHECK_RET( N >= 0 && N < m_noItems, | |
480 | wxT("invalid index in wxListBox::SetClientData") ); | |
c6179a84 | 481 | |
facd6764 SC |
482 | #if wxUSE_OWNER_DRAWN |
483 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
484 | { | |
485 | // client data must be pointer to wxOwnerDrawn, otherwise we would crash | |
486 | // in OnMeasure/OnDraw. | |
487 | wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes")); | |
488 | } | |
489 | #endif // wxUSE_OWNER_DRAWN | |
490 | wxASSERT_MSG( m_dataArray.GetCount() >= (size_t) N , wxT("invalid client_data array") ) ; | |
c6179a84 | 491 | |
facd6764 SC |
492 | if ( m_dataArray.GetCount() > (size_t) N ) |
493 | { | |
494 | m_dataArray[N] = (char*) Client_data ; | |
495 | } | |
496 | else | |
497 | { | |
498 | m_dataArray.Add( (char*) Client_data ) ; | |
499 | } | |
500 | } | |
501 | ||
502 | void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData) | |
503 | { | |
504 | DoSetItemClientData(n, clientData); | |
505 | } | |
506 | ||
507 | // Return number of selections and an array of selected integers | |
508 | int wxListBox::GetSelections(wxArrayInt& aSelections) const | |
509 | { | |
510 | return MacGetSelections( aSelections ) ; | |
511 | } | |
512 | ||
513 | // Get single selection, for single choice list items | |
514 | int wxListBox::GetSelection() const | |
515 | { | |
516 | return MacGetSelection() ; | |
517 | } | |
518 | ||
519 | // Find string for position | |
520 | wxString wxListBox::GetString(int N) const | |
521 | { | |
55ae2833 RD |
522 | wxCHECK_MSG( N >= 0 && N < m_noItems, wxEmptyString, |
523 | wxT("invalid index in wxListBox::GetString") ); | |
524 | ||
525 | return m_stringArray[N] ; | |
facd6764 SC |
526 | } |
527 | ||
528 | void wxListBox::DoInsertItems(const wxArrayString& items, int pos) | |
529 | { | |
530 | wxCHECK_RET( pos >= 0 && pos <= m_noItems, | |
531 | wxT("invalid index in wxListBox::InsertItems") ); | |
c6179a84 | 532 | |
9f884528 RD |
533 | InvalidateBestSize(); |
534 | ||
facd6764 | 535 | int nItems = items.GetCount(); |
c6179a84 | 536 | |
facd6764 SC |
537 | for ( int i = 0 ; i < nItems ; i++ ) |
538 | { | |
539 | m_stringArray.Insert( items[i] , pos + i ) ; | |
540 | m_dataArray.Insert( NULL , pos + i ) ; | |
8e0f22c0 | 541 | m_noItems++ ; |
facd6764 SC |
542 | MacInsert( pos + i , items[i] ) ; |
543 | } | |
facd6764 SC |
544 | } |
545 | ||
546 | void wxListBox::SetString(int N, const wxString& s) | |
547 | { | |
548 | m_stringArray[N] = s ; | |
549 | MacSet( N , s ) ; | |
550 | } | |
551 | ||
552 | wxSize wxListBox::DoGetBestSize() const | |
553 | { | |
554 | int lbWidth = 100; // some defaults | |
555 | int lbHeight = 110; | |
556 | int wLine; | |
557 | ||
558 | { | |
c6179a84 VZ |
559 | wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetTopLevelWindowRef() ) ) ; |
560 | ||
facd6764 SC |
561 | if ( m_font.Ok() ) |
562 | { | |
563 | ::TextFont( m_font.MacGetFontNum() ) ; | |
564 | ::TextSize( m_font.MacGetFontSize() ) ; | |
565 | ::TextFace( m_font.MacGetFontStyle() ) ; | |
566 | } | |
567 | else | |
568 | { | |
569 | ::TextFont( kFontIDMonaco ) ; | |
570 | ::TextSize( 9 ); | |
571 | ::TextFace( 0 ) ; | |
572 | } | |
c6179a84 | 573 | |
facd6764 SC |
574 | // Find the widest line |
575 | for(int i = 0; i < GetCount(); i++) { | |
576 | wxString str(GetString(i)); | |
577 | #if wxUSE_UNICODE | |
578 | Point bounds={0,0} ; | |
579 | SInt16 baseline ; | |
580 | ::GetThemeTextDimensions( wxMacCFStringHolder( str , m_font.GetEncoding() ) , | |
581 | kThemeCurrentPortFont, | |
582 | kThemeStateActive, | |
583 | false, | |
584 | &bounds, | |
585 | &baseline ); | |
586 | wLine = bounds.h ; | |
587 | #else | |
588 | wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ; | |
589 | #endif | |
590 | lbWidth = wxMax(lbWidth, wLine); | |
591 | } | |
c6179a84 | 592 | |
facd6764 SC |
593 | // Add room for the scrollbar |
594 | lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X); | |
c6179a84 | 595 | |
facd6764 SC |
596 | // And just a bit more |
597 | int cy = 12 ; | |
598 | int cx = ::TextWidth( "X" , 0 , 1 ) ; | |
599 | lbWidth += cx ; | |
c6179a84 | 600 | |
facd6764 SC |
601 | // don't make the listbox too tall (limit height to around 10 items) but don't |
602 | // make it too small neither | |
603 | lbHeight = (cy+4) * wxMin(wxMax(GetCount(), 3), 10); | |
604 | } | |
605 | ||
606 | return wxSize(lbWidth, lbHeight); | |
607 | } | |
608 | ||
609 | int wxListBox::GetCount() const | |
610 | { | |
611 | return m_noItems; | |
612 | } | |
613 | ||
614 | void wxListBox::Refresh(bool eraseBack, const wxRect *rect) | |
615 | { | |
616 | wxControl::Refresh( eraseBack , rect ) ; | |
facd6764 SC |
617 | } |
618 | ||
619 | #if wxUSE_OWNER_DRAWN | |
620 | ||
621 | class wxListBoxItem : public wxOwnerDrawn | |
622 | { | |
623 | public: | |
624 | wxListBoxItem(const wxString& str = ""); | |
625 | }; | |
626 | ||
627 | wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE) | |
628 | { | |
629 | // no bitmaps/checkmarks | |
630 | SetMarginWidth(0); | |
631 | } | |
632 | ||
633 | wxOwnerDrawn *wxListBox::CreateItem(size_t n) | |
634 | { | |
635 | return new wxListBoxItem(); | |
636 | } | |
637 | ||
638 | #endif //USE_OWNER_DRAWN | |
639 | ||
b6a20a20 RD |
640 | |
641 | // Some custom controls depend on this | |
642 | /* static */ wxVisualAttributes | |
643 | wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
644 | { | |
645 | wxVisualAttributes attr; | |
646 | attr.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT); | |
647 | attr.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX); | |
648 | attr.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); | |
649 | return attr; | |
650 | } | |
651 | ||
facd6764 SC |
652 | // ============================================================================ |
653 | // list box control implementation | |
654 | // ============================================================================ | |
655 | ||
fe3dc505 | 656 | void wxListBox::MacDelete( int n ) |
facd6764 | 657 | { |
fe3dc505 SC |
658 | wxArrayInt selectionBefore ; |
659 | MacGetSelections( selectionBefore ) ; | |
660 | ||
8e0f22c0 | 661 | UInt32 id = m_noItems+1 ; |
5ca0d812 | 662 | verify_noerr( m_peer->RemoveItems( kDataBrowserNoItem , 1 , (UInt32*) &id , kDataBrowserItemNoProperty ) ) ; |
0b9cd93c | 663 | for ( size_t i = 0 ; i < selectionBefore.GetCount() ; ++i ) |
fe3dc505 SC |
664 | { |
665 | int current = selectionBefore[i] ; | |
666 | if ( current == n ) | |
667 | { | |
668 | // selection was deleted | |
669 | MacSetSelection( current , false ) ; | |
670 | } | |
671 | else if ( current > n ) | |
672 | { | |
673 | // something behind the deleted item was selected -> move up | |
c6179a84 | 674 | MacSetSelection( current - 1 , true ) ; |
fe3dc505 SC |
675 | MacSetSelection( current , false ) ; |
676 | } | |
677 | } | |
678 | // refresh all | |
8e0f22c0 | 679 | verify_noerr( m_peer->UpdateItems( kDataBrowserNoItem , 1 , (UInt32*) kDataBrowserNoItem , kDataBrowserItemNoProperty , kDataBrowserItemNoProperty ) ) ; |
facd6764 SC |
680 | } |
681 | ||
682 | void wxListBox::MacInsert( int n , const wxString& text) | |
683 | { | |
fe3dc505 SC |
684 | wxArrayInt selectionBefore ; |
685 | MacGetSelections( selectionBefore ) ; | |
686 | ||
8e0f22c0 SC |
687 | UInt32 id = m_noItems ; // this has already been increased |
688 | verify_noerr( m_peer->AddItems( kDataBrowserNoItem , 1 , (UInt32*) &id , kDataBrowserItemNoProperty ) ) ; | |
fe3dc505 SC |
689 | |
690 | for ( int i = selectionBefore.GetCount()-1 ; i >= 0 ; --i ) | |
691 | { | |
692 | int current = selectionBefore[i] ; | |
693 | if ( current >= n ) | |
694 | { | |
c6179a84 | 695 | MacSetSelection( current + 1 , true ) ; |
fe3dc505 SC |
696 | MacSetSelection( current , false ) ; |
697 | } | |
698 | } | |
699 | ||
700 | // refresh all | |
8e0f22c0 | 701 | verify_noerr( m_peer->UpdateItems( kDataBrowserNoItem , 1 , (UInt32*) kDataBrowserNoItem , kDataBrowserItemNoProperty , kDataBrowserItemNoProperty ) ) ; |
facd6764 SC |
702 | } |
703 | ||
704 | void wxListBox::MacAppend( const wxString& text) | |
705 | { | |
8e0f22c0 SC |
706 | UInt32 id = m_noItems ; // this has already been increased |
707 | verify_noerr( m_peer->AddItems( kDataBrowserNoItem , 1 , (UInt32*) &id , kDataBrowserItemNoProperty ) ) ; | |
fe3dc505 | 708 | // no need to deal with selections nor refreshed, as we have appended |
facd6764 SC |
709 | } |
710 | ||
711 | void wxListBox::MacClear() | |
712 | { | |
5ca0d812 | 713 | verify_noerr( m_peer->RemoveItems( kDataBrowserNoItem , 0 , NULL , kDataBrowserItemNoProperty ) ) ; |
facd6764 SC |
714 | } |
715 | ||
c6179a84 | 716 | void wxListBox::MacDeselectAll() |
fe3dc505 SC |
717 | { |
718 | bool former = MacSuppressSelection( true ) ; | |
719 | verify_noerr(m_peer->SetSelectedItems( 0 , NULL , kDataBrowserItemsRemove ) ) ; | |
720 | MacSuppressSelection( former ) ; | |
721 | } | |
722 | ||
facd6764 SC |
723 | void wxListBox::MacSetSelection( int n , bool select ) |
724 | { | |
fe3dc505 | 725 | bool former = MacSuppressSelection( true ) ; |
8e0f22c0 | 726 | UInt32 id = n + 1 ; |
fe3dc505 | 727 | |
5ca0d812 | 728 | if ( m_peer->IsItemSelected( id ) != select ) |
facd6764 | 729 | { |
fe3dc505 SC |
730 | if ( select ) |
731 | verify_noerr(m_peer->SetSelectedItems( 1 , & id , HasMultipleSelection() ? kDataBrowserItemsAdd : kDataBrowserItemsAssign ) ) ; | |
732 | else | |
733 | verify_noerr(m_peer->SetSelectedItems( 1 , & id , kDataBrowserItemsRemove ) ) ; | |
facd6764 SC |
734 | } |
735 | MacScrollTo( n ) ; | |
fe3dc505 SC |
736 | MacSuppressSelection( former ) ; |
737 | } | |
738 | ||
c6179a84 | 739 | bool wxListBox::MacSuppressSelection( bool suppress ) |
fe3dc505 SC |
740 | { |
741 | bool former = m_suppressSelection ; | |
742 | m_suppressSelection = suppress ; | |
743 | return former ; | |
facd6764 SC |
744 | } |
745 | ||
746 | bool wxListBox::MacIsSelected( int n ) const | |
747 | { | |
8e0f22c0 | 748 | return m_peer->IsItemSelected( n + 1 ) ; |
facd6764 SC |
749 | } |
750 | ||
751 | int wxListBox::MacGetSelection() const | |
752 | { | |
45285a62 | 753 | for ( int i = 0 ; i < GetCount() ; ++i ) |
facd6764 | 754 | { |
8e0f22c0 | 755 | if ( m_peer->IsItemSelected( i + 1 ) ) |
facd6764 SC |
756 | { |
757 | return i ; | |
758 | } | |
759 | } | |
fe3dc505 | 760 | return -1 ; |
facd6764 SC |
761 | } |
762 | ||
763 | int wxListBox::MacGetSelections( wxArrayInt& aSelections ) const | |
764 | { | |
765 | int no_sel = 0 ; | |
c6179a84 | 766 | |
facd6764 | 767 | aSelections.Empty(); |
c6179a84 | 768 | |
fe3dc505 SC |
769 | UInt32 first , last ; |
770 | m_peer->GetSelectionAnchor( &first , &last ) ; | |
771 | if ( first != kDataBrowserNoItem ) | |
facd6764 | 772 | { |
0b9cd93c | 773 | for ( size_t i = first ; i <= last ; ++i ) |
facd6764 | 774 | { |
fe3dc505 SC |
775 | if ( m_peer->IsItemSelected( i ) ) |
776 | { | |
777 | aSelections.Add( i - 1 ) ; | |
778 | no_sel++ ; | |
779 | } | |
facd6764 SC |
780 | } |
781 | } | |
782 | return no_sel ; | |
783 | } | |
519cb848 | 784 | |
facd6764 SC |
785 | void wxListBox::MacSet( int n , const wxString& text ) |
786 | { | |
787 | // as we don't store the strings we only have to issue a redraw | |
8e0f22c0 | 788 | UInt32 id = n + 1 ; |
5ca0d812 | 789 | verify_noerr( m_peer->UpdateItems( kDataBrowserNoItem , 1 , &id , kDataBrowserItemNoProperty , kDataBrowserItemNoProperty ) ) ; |
facd6764 | 790 | } |
e42e45a9 | 791 | |
facd6764 SC |
792 | void wxListBox::MacScrollTo( int n ) |
793 | { | |
8e0f22c0 | 794 | UInt32 id = n + 1 ; |
5ca0d812 | 795 | verify_noerr( m_peer->RevealItem( id , kTextColumnId , kDataBrowserRevealWithoutSelecting ) ) ; |
facd6764 SC |
796 | } |
797 | ||
5ecae0b7 SC |
798 | #if !TARGET_API_MAC_OSX |
799 | ||
facd6764 SC |
800 | void wxListBox::OnChar(wxKeyEvent& event) |
801 | { | |
802 | // todo trigger proper events here | |
803 | event.Skip() ; | |
804 | return ; | |
c6179a84 | 805 | |
facd6764 SC |
806 | if ( event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER) |
807 | { | |
808 | wxWindow* parent = GetParent() ; | |
809 | while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL ) | |
810 | parent = parent->GetParent() ; | |
c6179a84 | 811 | |
facd6764 SC |
812 | if ( parent && parent->GetDefaultItem() ) |
813 | { | |
814 | wxButton *def = wxDynamicCast(parent->GetDefaultItem(), | |
815 | wxButton); | |
816 | if ( def && def->IsEnabled() ) | |
817 | { | |
818 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() ); | |
819 | event.SetEventObject(def); | |
820 | def->Command(event); | |
821 | return ; | |
822 | } | |
823 | } | |
824 | event.Skip() ; | |
825 | } | |
826 | /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */ | |
827 | else if (event.GetKeyCode() == WXK_ESCAPE || (event.GetKeyCode() == '.' && event.MetaDown() ) ) | |
828 | { | |
829 | // FIXME: look in ancestors, not just parent. | |
830 | wxWindow* win = GetParent()->FindWindow( wxID_CANCEL ) ; | |
831 | if (win) | |
832 | { | |
833 | wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL); | |
834 | new_event.SetEventObject( win ); | |
835 | win->GetEventHandler()->ProcessEvent( new_event ); | |
836 | } | |
837 | } | |
838 | else if ( event.GetKeyCode() == WXK_TAB ) | |
839 | { | |
840 | wxNavigationKeyEvent new_event; | |
841 | new_event.SetEventObject( this ); | |
842 | new_event.SetDirection( !event.ShiftDown() ); | |
843 | /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */ | |
844 | new_event.SetWindowChange( event.ControlDown() ); | |
845 | new_event.SetCurrentFocus( this ); | |
846 | if ( !GetEventHandler()->ProcessEvent( new_event ) ) | |
847 | event.Skip() ; | |
848 | } | |
849 | else if ( event.GetKeyCode() == WXK_DOWN || event.GetKeyCode() == WXK_UP ) | |
850 | { | |
851 | // perform the default key handling first | |
852 | wxControl::OnKeyDown( event ) ; | |
c6179a84 | 853 | |
facd6764 SC |
854 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId); |
855 | event.SetEventObject( this ); | |
c6179a84 | 856 | |
facd6764 SC |
857 | wxArrayInt aSelections; |
858 | int n, count = GetSelections(aSelections); | |
859 | if ( count > 0 ) | |
860 | { | |
861 | n = aSelections[0]; | |
862 | if ( HasClientObjectData() ) | |
863 | event.SetClientObject( GetClientObject(n) ); | |
864 | else if ( HasClientUntypedData() ) | |
865 | event.SetClientData( GetClientData(n) ); | |
866 | event.SetString( GetString(n) ); | |
867 | } | |
868 | else | |
869 | { | |
870 | n = -1; | |
871 | } | |
c6179a84 | 872 | |
687706f5 | 873 | event.SetInt(n); |
c6179a84 | 874 | |
facd6764 SC |
875 | GetEventHandler()->ProcessEvent(event); |
876 | } | |
877 | else | |
878 | { | |
879 | if ( event.GetTimestamp() > m_lastTypeIn + 60 ) | |
880 | { | |
881 | m_typeIn = wxEmptyString ; | |
882 | } | |
883 | m_lastTypeIn = event.GetTimestamp() ; | |
884 | m_typeIn += (char) event.GetKeyCode() ; | |
885 | int line = FindString(wxT("*")+m_typeIn+wxT("*")) ; | |
886 | if ( line >= 0 ) | |
887 | { | |
888 | if ( GetSelection() != line ) | |
889 | { | |
890 | SetSelection(line) ; | |
891 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId); | |
892 | event.SetEventObject( this ); | |
c6179a84 | 893 | |
facd6764 SC |
894 | if ( HasClientObjectData() ) |
895 | event.SetClientObject( GetClientObject( line ) ); | |
896 | else if ( HasClientUntypedData() ) | |
897 | event.SetClientData( GetClientData(line) ); | |
898 | event.SetString( GetString(line) ); | |
c6179a84 | 899 | |
687706f5 | 900 | event.SetInt(line); |
c6179a84 | 901 | |
facd6764 SC |
902 | GetEventHandler()->ProcessEvent(event); |
903 | } | |
904 | } | |
905 | } | |
906 | } | |
573ac9dc | 907 | |
c6179a84 | 908 | #endif // !TARGET_API_MAC_OSX |
5ecae0b7 | 909 | |
179e085f RN |
910 | #endif |
911 |