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