]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/listbox.cpp
Adding GetPartialTextExtents implementation.
[wxWidgets.git] / src / mac / carbon / listbox.cpp
CommitLineData
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 31IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
519cb848
SC
32
33BEGIN_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 38END_EVENT_TABLE()
2f1ae414 39#endif
e9576ca5 40
facd6764
SC
41#include "wx/mac/uma.h"
42
a9fc5eec
SC
43const 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 52static pascal void DataBrowserItemNotificationProc(ControlRef browser, DataBrowserItemID itemID,
83ce5634
SC
53 DataBrowserItemNotification message, DataBrowserItemDataRef itemData)
54#else
c6179a84 55static 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
103static 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
142wxListBox::wxListBox()
143{
144 m_noItems = 0;
145 m_selected = 0;
146 m_macList = NULL ;
147 m_suppressSelection = false ;
148}
149
150bool 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
164bool 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
21fd5529 185 m_peer = new wxMacControl() ;
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
268wxListBox::~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
279void 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
303void 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
310void wxListBox::DoSetFirstItem(int N)
311{
312 MacScrollTo( N ) ;
313}
314
315void 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
336int 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
350void 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
390int 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
436void wxListBox::Clear()
437{
438 FreeData();
439 m_noItems = 0;
440 m_stringArray.Empty() ;
441 m_dataArray.Empty() ;
442 MacClear() ;
443}
444
c6179a84 445void 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
456bool 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
464void *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
472wxClientData *wxListBox::DoGetItemClientObject(int N) const
473{
474 return (wxClientData *) DoGetItemClientData( N ) ;
475}
476
477void 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
502void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
503{
504 DoSetItemClientData(n, clientData);
505}
506
507// Return number of selections and an array of selected integers
508int wxListBox::GetSelections(wxArrayInt& aSelections) const
509{
510 return MacGetSelections( aSelections ) ;
511}
512
513// Get single selection, for single choice list items
514int wxListBox::GetSelection() const
515{
516 return MacGetSelection() ;
517}
518
519// Find string for position
520wxString wxListBox::GetString(int N) const
521{
522 return m_stringArray[N] ;
523}
524
525void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
526{
527 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
528 wxT("invalid index in wxListBox::InsertItems") );
c6179a84 529
9f884528
RD
530 InvalidateBestSize();
531
facd6764 532 int nItems = items.GetCount();
c6179a84 533
facd6764
SC
534 for ( int i = 0 ; i < nItems ; i++ )
535 {
536 m_stringArray.Insert( items[i] , pos + i ) ;
537 m_dataArray.Insert( NULL , pos + i ) ;
8e0f22c0 538 m_noItems++ ;
facd6764
SC
539 MacInsert( pos + i , items[i] ) ;
540 }
facd6764
SC
541}
542
543void wxListBox::SetString(int N, const wxString& s)
544{
545 m_stringArray[N] = s ;
546 MacSet( N , s ) ;
547}
548
549wxSize wxListBox::DoGetBestSize() const
550{
551 int lbWidth = 100; // some defaults
552 int lbHeight = 110;
553 int wLine;
554
555 {
c6179a84
VZ
556 wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetTopLevelWindowRef() ) ) ;
557
facd6764
SC
558 if ( m_font.Ok() )
559 {
560 ::TextFont( m_font.MacGetFontNum() ) ;
561 ::TextSize( m_font.MacGetFontSize() ) ;
562 ::TextFace( m_font.MacGetFontStyle() ) ;
563 }
564 else
565 {
566 ::TextFont( kFontIDMonaco ) ;
567 ::TextSize( 9 );
568 ::TextFace( 0 ) ;
569 }
c6179a84 570
facd6764
SC
571 // Find the widest line
572 for(int i = 0; i < GetCount(); i++) {
573 wxString str(GetString(i));
574 #if wxUSE_UNICODE
575 Point bounds={0,0} ;
576 SInt16 baseline ;
577 ::GetThemeTextDimensions( wxMacCFStringHolder( str , m_font.GetEncoding() ) ,
578 kThemeCurrentPortFont,
579 kThemeStateActive,
580 false,
581 &bounds,
582 &baseline );
583 wLine = bounds.h ;
584 #else
585 wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ;
586 #endif
587 lbWidth = wxMax(lbWidth, wLine);
588 }
c6179a84 589
facd6764
SC
590 // Add room for the scrollbar
591 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
c6179a84 592
facd6764
SC
593 // And just a bit more
594 int cy = 12 ;
595 int cx = ::TextWidth( "X" , 0 , 1 ) ;
596 lbWidth += cx ;
c6179a84 597
facd6764
SC
598 // don't make the listbox too tall (limit height to around 10 items) but don't
599 // make it too small neither
600 lbHeight = (cy+4) * wxMin(wxMax(GetCount(), 3), 10);
601 }
602
603 return wxSize(lbWidth, lbHeight);
604}
605
606int wxListBox::GetCount() const
607{
608 return m_noItems;
609}
610
611void wxListBox::Refresh(bool eraseBack, const wxRect *rect)
612{
613 wxControl::Refresh( eraseBack , rect ) ;
facd6764
SC
614}
615
616#if wxUSE_OWNER_DRAWN
617
618class wxListBoxItem : public wxOwnerDrawn
619{
620public:
621 wxListBoxItem(const wxString& str = "");
622};
623
624wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE)
625{
626 // no bitmaps/checkmarks
627 SetMarginWidth(0);
628}
629
630wxOwnerDrawn *wxListBox::CreateItem(size_t n)
631{
632 return new wxListBoxItem();
633}
634
635#endif //USE_OWNER_DRAWN
636
b6a20a20
RD
637
638// Some custom controls depend on this
639/* static */ wxVisualAttributes
640wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
641{
642 wxVisualAttributes attr;
643 attr.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
644 attr.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX);
645 attr.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
646 return attr;
647}
648
facd6764
SC
649// ============================================================================
650// list box control implementation
651// ============================================================================
652
fe3dc505 653void wxListBox::MacDelete( int n )
facd6764 654{
fe3dc505
SC
655 wxArrayInt selectionBefore ;
656 MacGetSelections( selectionBefore ) ;
657
8e0f22c0 658 UInt32 id = m_noItems+1 ;
5ca0d812 659 verify_noerr( m_peer->RemoveItems( kDataBrowserNoItem , 1 , (UInt32*) &id , kDataBrowserItemNoProperty ) ) ;
0b9cd93c 660 for ( size_t i = 0 ; i < selectionBefore.GetCount() ; ++i )
fe3dc505
SC
661 {
662 int current = selectionBefore[i] ;
663 if ( current == n )
664 {
665 // selection was deleted
666 MacSetSelection( current , false ) ;
667 }
668 else if ( current > n )
669 {
670 // something behind the deleted item was selected -> move up
c6179a84 671 MacSetSelection( current - 1 , true ) ;
fe3dc505
SC
672 MacSetSelection( current , false ) ;
673 }
674 }
675 // refresh all
8e0f22c0 676 verify_noerr( m_peer->UpdateItems( kDataBrowserNoItem , 1 , (UInt32*) kDataBrowserNoItem , kDataBrowserItemNoProperty , kDataBrowserItemNoProperty ) ) ;
facd6764
SC
677}
678
679void wxListBox::MacInsert( int n , const wxString& text)
680{
fe3dc505
SC
681 wxArrayInt selectionBefore ;
682 MacGetSelections( selectionBefore ) ;
683
8e0f22c0
SC
684 UInt32 id = m_noItems ; // this has already been increased
685 verify_noerr( m_peer->AddItems( kDataBrowserNoItem , 1 , (UInt32*) &id , kDataBrowserItemNoProperty ) ) ;
fe3dc505
SC
686
687 for ( int i = selectionBefore.GetCount()-1 ; i >= 0 ; --i )
688 {
689 int current = selectionBefore[i] ;
690 if ( current >= n )
691 {
c6179a84 692 MacSetSelection( current + 1 , true ) ;
fe3dc505
SC
693 MacSetSelection( current , false ) ;
694 }
695 }
696
697 // refresh all
8e0f22c0 698 verify_noerr( m_peer->UpdateItems( kDataBrowserNoItem , 1 , (UInt32*) kDataBrowserNoItem , kDataBrowserItemNoProperty , kDataBrowserItemNoProperty ) ) ;
facd6764
SC
699}
700
701void wxListBox::MacAppend( const wxString& text)
702{
8e0f22c0
SC
703 UInt32 id = m_noItems ; // this has already been increased
704 verify_noerr( m_peer->AddItems( kDataBrowserNoItem , 1 , (UInt32*) &id , kDataBrowserItemNoProperty ) ) ;
fe3dc505 705 // no need to deal with selections nor refreshed, as we have appended
facd6764
SC
706}
707
708void wxListBox::MacClear()
709{
5ca0d812 710 verify_noerr( m_peer->RemoveItems( kDataBrowserNoItem , 0 , NULL , kDataBrowserItemNoProperty ) ) ;
facd6764
SC
711}
712
c6179a84 713void wxListBox::MacDeselectAll()
fe3dc505
SC
714{
715 bool former = MacSuppressSelection( true ) ;
716 verify_noerr(m_peer->SetSelectedItems( 0 , NULL , kDataBrowserItemsRemove ) ) ;
717 MacSuppressSelection( former ) ;
718}
719
facd6764
SC
720void wxListBox::MacSetSelection( int n , bool select )
721{
fe3dc505 722 bool former = MacSuppressSelection( true ) ;
8e0f22c0 723 UInt32 id = n + 1 ;
fe3dc505 724
5ca0d812 725 if ( m_peer->IsItemSelected( id ) != select )
facd6764 726 {
fe3dc505
SC
727 if ( select )
728 verify_noerr(m_peer->SetSelectedItems( 1 , & id , HasMultipleSelection() ? kDataBrowserItemsAdd : kDataBrowserItemsAssign ) ) ;
729 else
730 verify_noerr(m_peer->SetSelectedItems( 1 , & id , kDataBrowserItemsRemove ) ) ;
facd6764
SC
731 }
732 MacScrollTo( n ) ;
fe3dc505
SC
733 MacSuppressSelection( former ) ;
734}
735
c6179a84 736bool wxListBox::MacSuppressSelection( bool suppress )
fe3dc505
SC
737{
738 bool former = m_suppressSelection ;
739 m_suppressSelection = suppress ;
740 return former ;
facd6764
SC
741}
742
743bool wxListBox::MacIsSelected( int n ) const
744{
8e0f22c0 745 return m_peer->IsItemSelected( n + 1 ) ;
facd6764
SC
746}
747
748int wxListBox::MacGetSelection() const
749{
45285a62 750 for ( int i = 0 ; i < GetCount() ; ++i )
facd6764 751 {
8e0f22c0 752 if ( m_peer->IsItemSelected( i + 1 ) )
facd6764
SC
753 {
754 return i ;
755 }
756 }
fe3dc505 757 return -1 ;
facd6764
SC
758}
759
760int wxListBox::MacGetSelections( wxArrayInt& aSelections ) const
761{
762 int no_sel = 0 ;
c6179a84 763
facd6764 764 aSelections.Empty();
c6179a84 765
fe3dc505
SC
766 UInt32 first , last ;
767 m_peer->GetSelectionAnchor( &first , &last ) ;
768 if ( first != kDataBrowserNoItem )
facd6764 769 {
0b9cd93c 770 for ( size_t i = first ; i <= last ; ++i )
facd6764 771 {
fe3dc505
SC
772 if ( m_peer->IsItemSelected( i ) )
773 {
774 aSelections.Add( i - 1 ) ;
775 no_sel++ ;
776 }
facd6764
SC
777 }
778 }
779 return no_sel ;
780}
519cb848 781
facd6764
SC
782void wxListBox::MacSet( int n , const wxString& text )
783{
784 // as we don't store the strings we only have to issue a redraw
8e0f22c0 785 UInt32 id = n + 1 ;
5ca0d812 786 verify_noerr( m_peer->UpdateItems( kDataBrowserNoItem , 1 , &id , kDataBrowserItemNoProperty , kDataBrowserItemNoProperty ) ) ;
facd6764 787}
e42e45a9 788
facd6764
SC
789void wxListBox::MacScrollTo( int n )
790{
8e0f22c0 791 UInt32 id = n + 1 ;
5ca0d812 792 verify_noerr( m_peer->RevealItem( id , kTextColumnId , kDataBrowserRevealWithoutSelecting ) ) ;
facd6764
SC
793}
794
5ecae0b7
SC
795#if !TARGET_API_MAC_OSX
796
facd6764
SC
797void wxListBox::OnChar(wxKeyEvent& event)
798{
799 // todo trigger proper events here
800 event.Skip() ;
801 return ;
c6179a84 802
facd6764
SC
803 if ( event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER)
804 {
805 wxWindow* parent = GetParent() ;
806 while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL )
807 parent = parent->GetParent() ;
c6179a84 808
facd6764
SC
809 if ( parent && parent->GetDefaultItem() )
810 {
811 wxButton *def = wxDynamicCast(parent->GetDefaultItem(),
812 wxButton);
813 if ( def && def->IsEnabled() )
814 {
815 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
816 event.SetEventObject(def);
817 def->Command(event);
818 return ;
819 }
820 }
821 event.Skip() ;
822 }
823 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
824 else if (event.GetKeyCode() == WXK_ESCAPE || (event.GetKeyCode() == '.' && event.MetaDown() ) )
825 {
826 // FIXME: look in ancestors, not just parent.
827 wxWindow* win = GetParent()->FindWindow( wxID_CANCEL ) ;
828 if (win)
829 {
830 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
831 new_event.SetEventObject( win );
832 win->GetEventHandler()->ProcessEvent( new_event );
833 }
834 }
835 else if ( event.GetKeyCode() == WXK_TAB )
836 {
837 wxNavigationKeyEvent new_event;
838 new_event.SetEventObject( this );
839 new_event.SetDirection( !event.ShiftDown() );
840 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
841 new_event.SetWindowChange( event.ControlDown() );
842 new_event.SetCurrentFocus( this );
843 if ( !GetEventHandler()->ProcessEvent( new_event ) )
844 event.Skip() ;
845 }
846 else if ( event.GetKeyCode() == WXK_DOWN || event.GetKeyCode() == WXK_UP )
847 {
848 // perform the default key handling first
849 wxControl::OnKeyDown( event ) ;
c6179a84 850
facd6764
SC
851 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
852 event.SetEventObject( this );
c6179a84 853
facd6764
SC
854 wxArrayInt aSelections;
855 int n, count = GetSelections(aSelections);
856 if ( count > 0 )
857 {
858 n = aSelections[0];
859 if ( HasClientObjectData() )
860 event.SetClientObject( GetClientObject(n) );
861 else if ( HasClientUntypedData() )
862 event.SetClientData( GetClientData(n) );
863 event.SetString( GetString(n) );
864 }
865 else
866 {
867 n = -1;
868 }
c6179a84 869
687706f5 870 event.SetInt(n);
c6179a84 871
facd6764
SC
872 GetEventHandler()->ProcessEvent(event);
873 }
874 else
875 {
876 if ( event.GetTimestamp() > m_lastTypeIn + 60 )
877 {
878 m_typeIn = wxEmptyString ;
879 }
880 m_lastTypeIn = event.GetTimestamp() ;
881 m_typeIn += (char) event.GetKeyCode() ;
882 int line = FindString(wxT("*")+m_typeIn+wxT("*")) ;
883 if ( line >= 0 )
884 {
885 if ( GetSelection() != line )
886 {
887 SetSelection(line) ;
888 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
889 event.SetEventObject( this );
c6179a84 890
facd6764
SC
891 if ( HasClientObjectData() )
892 event.SetClientObject( GetClientObject( line ) );
893 else if ( HasClientUntypedData() )
894 event.SetClientData( GetClientData(line) );
895 event.SetString( GetString(line) );
c6179a84 896
687706f5 897 event.SetInt(line);
c6179a84 898
facd6764
SC
899 GetEventHandler()->ProcessEvent(event);
900 }
901 }
902 }
903}
573ac9dc 904
c6179a84 905#endif // !TARGET_API_MAC_OSX
5ecae0b7 906
179e085f
RN
907#endif
908