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