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