]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/listbox.cpp
default to striped background
[wxWidgets.git] / src / mac / carbon / listbox.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: listbox.cpp
3 // Purpose: wxListBox
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "listbox.h"
14 #endif
15
16 #include "wx/app.h"
17 #include "wx/listbox.h"
18 #include "wx/button.h"
19 #include "wx/settings.h"
20 #include "wx/toplevel.h"
21 #include "wx/dynarray.h"
22 #include "wx/log.h"
23
24 #include "wx/utils.h"
25
26 #if !USE_SHARED_LIBRARY
27 IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
28
29 BEGIN_EVENT_TABLE(wxListBox, wxControl)
30 #if !TARGET_API_MAC_OSX
31 EVT_SIZE( wxListBox::OnSize )
32 EVT_CHAR( wxListBox::OnChar )
33 #endif
34 END_EVENT_TABLE()
35 #endif
36
37 #include "wx/mac/uma.h"
38
39 #if TARGET_API_MAC_OSX
40
41 // new databrowserbased version
42
43 // Listbox item
44 wxListBox::wxListBox()
45 {
46 m_noItems = 0;
47 m_selected = 0;
48 m_macList = NULL ;
49 }
50
51 bool wxListBox::Create(wxWindow *parent, wxWindowID id,
52 const wxPoint& pos,
53 const wxSize& size,
54 const wxArrayString& choices,
55 long style,
56 const wxValidator& validator,
57 const wxString& name)
58 {
59 wxCArrayString chs(choices);
60
61 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
62 style, validator, name);
63 }
64
65 #if TARGET_API_MAC_OSX
66 static pascal void DataBrowserItemNotificationProc(ControlRef browser, DataBrowserItemID itemID,
67 DataBrowserItemNotification message, DataBrowserItemDataRef itemData)
68 #else
69 static pascal void DataBrowserItemNotificationProc(ControlRef browser, DataBrowserItemID itemID,
70 DataBrowserItemNotification message)
71 #endif
72 {
73 long ref = GetControlReference( browser ) ;
74 if ( ref )
75 {
76 wxListBox* list = wxDynamicCast( ref , wxListBox ) ;
77 for ( size_t i = 0 ; i < list->m_idArray.GetCount() ; ++i )
78 if ( list->m_idArray[i] == (long) itemID )
79 {
80 bool trigger = false ;
81 wxCommandEvent event(
82 wxEVT_COMMAND_LISTBOX_SELECTED, list->GetId() );
83 switch( message )
84 {
85 case kDataBrowserItemDeselected :
86 if ( list->HasMultipleSelection() )
87 trigger = true ;
88 break ;
89 case kDataBrowserItemSelected :
90 trigger = true ;
91 break ;
92 case kDataBrowserItemDoubleClicked :
93 event.SetEventType(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED) ;
94 trigger = true ;
95 break ;
96 default :
97 break ;
98 }
99 if ( trigger )
100 {
101 event.SetEventObject( list );
102 if ( list->HasClientObjectData() )
103 event.SetClientObject( list->GetClientObject(i) );
104 else if ( list->HasClientUntypedData() )
105 event.SetClientData( list->GetClientData(i) );
106 event.SetString( list->GetString(i) );
107 event.SetInt(i) ;
108 event.SetExtraLong( list->HasMultipleSelection() ? message == kDataBrowserItemSelected : TRUE );
109 list->GetEventHandler()->ProcessEvent(event) ;
110 }
111 break ;
112 }
113 }
114 }
115
116
117 static pascal OSStatus ListBoxGetSetItemData(ControlRef browser,
118 DataBrowserItemID itemID, DataBrowserPropertyID property,
119 DataBrowserItemDataRef itemData, Boolean changeValue)
120 {
121 OSStatus err = errDataBrowserPropertyNotSupported;
122
123 if ( ! changeValue )
124 {
125 switch (property)
126 {
127
128 case 1024:
129 {
130 long ref = GetControlReference( browser ) ;
131 if ( ref )
132 {
133 wxListBox* list = wxDynamicCast( ref , wxListBox ) ;
134 for ( size_t i = 0 ; i < list->m_idArray.GetCount() ; ++i )
135 if ( list->m_idArray[i] == (long) itemID )
136 {
137 wxMacCFStringHolder cf( list->GetString(i) , list->GetFont().GetEncoding() ) ;
138 verify_noerr( ::SetDataBrowserItemDataText( itemData , cf ) ) ;
139 err = noErr ;
140 break ;
141 }
142 }
143 }
144 break;
145
146 default:
147
148 break;
149 }
150 }
151
152 return err;
153 }
154 bool wxListBox::Create(wxWindow *parent, wxWindowID id,
155 const wxPoint& pos,
156 const wxSize& size,
157 int n, const wxString choices[],
158 long style,
159 const wxValidator& validator,
160 const wxString& name)
161 {
162 m_macIsUserPane = FALSE ;
163
164 if ( !wxListBoxBase::Create(parent, id, pos, size, style & ~(wxHSCROLL|wxVSCROLL), validator, name) )
165 return false;
166
167 m_noItems = 0 ; // this will be increased by our append command
168 m_selected = 0;
169 m_nextId = 1 ;
170
171
172 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
173 ControlRef browser ;
174
175 verify_noerr( ::CreateDataBrowserControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds, kDataBrowserListView , (ControlRef *)&m_macControl ) );
176 browser = (ControlRef) m_macControl ;
177
178 DataBrowserSelectionFlags options = kDataBrowserDragSelect ;
179 if ( style & wxLB_MULTIPLE )
180 {
181 options += kDataBrowserAlwaysExtendSelection + kDataBrowserCmdTogglesSelection ;
182 }
183 else if ( style & wxLB_EXTENDED )
184 {
185 // default behaviour
186 }
187 else
188 {
189 options += kDataBrowserSelectOnlyOne ;
190 }
191 verify_noerr(SetDataBrowserSelectionFlags (browser, options ) );
192
193 DataBrowserListViewColumnDesc columnDesc ;
194 columnDesc.headerBtnDesc.titleOffset = 0;
195 columnDesc.headerBtnDesc.version = kDataBrowserListViewLatestHeaderDesc;
196
197 columnDesc.headerBtnDesc.btnFontStyle.flags =
198 kControlUseFontMask | kControlUseJustMask;
199
200 columnDesc.headerBtnDesc.btnContentInfo.contentType = kControlNoContent;
201 columnDesc.propertyDesc.propertyType = kDataBrowserTextType;
202 columnDesc.headerBtnDesc.btnFontStyle.just = teFlushDefault;
203 columnDesc.headerBtnDesc.minimumWidth = 0;
204 columnDesc.headerBtnDesc.maximumWidth = 10000;
205
206 columnDesc.headerBtnDesc.btnFontStyle.font = kControlFontViewSystemFont;
207 columnDesc.headerBtnDesc.btnFontStyle.style = normal;
208 columnDesc.headerBtnDesc.titleString = NULL ; // CFSTR( "" );
209
210 columnDesc.propertyDesc.propertyID = 1024;
211 columnDesc.propertyDesc.propertyType = kDataBrowserTextType;
212 columnDesc.propertyDesc.propertyFlags =
213 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
214 kDataBrowserListViewTypeSelectColumn |
215 #endif
216 kDataBrowserTableViewSelectionColumn ;
217
218
219 verify_noerr(::AddDataBrowserListViewColumn(browser, &columnDesc, kDataBrowserListViewAppendColumn) ) ;
220 verify_noerr(::AutoSizeDataBrowserListViewColumns( browser ) ) ;
221 verify_noerr(::SetDataBrowserHasScrollBars( browser , false , true ) ) ;
222 verify_noerr(::SetDataBrowserTableViewHiliteStyle( browser, kDataBrowserTableViewFillHilite ) ) ;
223 verify_noerr(::SetDataBrowserListViewHeaderBtnHeight( browser , 0 ) ) ;
224 DataBrowserCallbacks callbacks ;
225
226 callbacks.version = kDataBrowserLatestCallbacks;
227
228 InitDataBrowserCallbacks(&callbacks);
229
230 callbacks.u.v1.itemDataCallback =
231 NewDataBrowserItemDataUPP(ListBoxGetSetItemData);
232
233 callbacks.u.v1.itemNotificationCallback =
234 #if TARGET_API_MAC_OSX
235 (DataBrowserItemNotificationUPP) NewDataBrowserItemNotificationWithItemUPP(DataBrowserItemNotificationProc) ;
236 #else
237 NewDataBrowserItemNotificationUPP(DataBrowserItemNotificationProc) ;
238 #endif
239 SetDataBrowserCallbacks(browser, &callbacks);
240
241 MacPostControlCreate(pos,size) ;
242
243 for ( int i = 0 ; i < n ; i++ )
244 {
245 Append( choices[i] ) ;
246 }
247
248 return TRUE;
249 }
250
251 wxListBox::~wxListBox()
252 {
253 SetControlReference( (ControlRef) m_macControl , NULL ) ;
254 FreeData() ;
255 // avoid access during destruction
256 if ( m_macList )
257 {
258 m_macList = NULL ;
259 }
260 }
261
262 void wxListBox::FreeData()
263 {
264 #if wxUSE_OWNER_DRAWN
265 if ( m_windowStyle & wxLB_OWNERDRAW )
266 {
267 size_t uiCount = m_aItems.Count();
268 while ( uiCount-- != 0 ) {
269 delete m_aItems[uiCount];
270 m_aItems[uiCount] = NULL;
271 }
272
273 m_aItems.Clear();
274 }
275 else
276 #endif // wxUSE_OWNER_DRAWN
277 if ( HasClientObjectData() )
278 {
279 for ( size_t n = 0; n < (size_t)m_noItems; n++ )
280 {
281 delete GetClientObject(n);
282 }
283 }
284 }
285
286 void wxListBox::DoSetSize(int x, int y,
287 int width, int height,
288 int sizeFlags )
289 {
290 wxControl::DoSetSize( x , y , width , height , sizeFlags ) ;
291 }
292
293 void wxListBox::DoSetFirstItem(int N)
294 {
295 MacScrollTo( N ) ;
296 }
297
298 void wxListBox::Delete(int N)
299 {
300 wxCHECK_RET( N >= 0 && N < m_noItems,
301 wxT("invalid index in wxListBox::Delete") );
302
303 #if wxUSE_OWNER_DRAWN
304 delete m_aItems[N];
305 m_aItems.RemoveAt(N);
306 #else // !wxUSE_OWNER_DRAWN
307 if ( HasClientObjectData() )
308 {
309 delete GetClientObject(N);
310 }
311 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
312 m_stringArray.RemoveAt( N ) ;
313 m_dataArray.RemoveAt( N ) ;
314 m_noItems --;
315
316 MacDelete( N ) ;
317 }
318
319 int wxListBox::DoAppend(const wxString& item)
320 {
321 int index = m_noItems ;
322 m_stringArray.Add( item ) ;
323 m_dataArray.Add( NULL );
324 m_noItems ++;
325 DoSetItemClientData( index , NULL ) ;
326 MacAppend( item ) ;
327
328 return index ;
329 }
330
331 void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
332 {
333 Clear() ;
334 int n = choices.GetCount();
335
336 for( int i = 0 ; i < n ; ++i )
337 {
338 if ( clientData )
339 {
340 #if wxUSE_OWNER_DRAWN
341 wxASSERT_MSG(clientData[i] == NULL,
342 wxT("Can't use client data with owner-drawn listboxes"));
343 #else // !wxUSE_OWNER_DRAWN
344 Append( choices[i] , clientData[i] ) ;
345 #endif
346 }
347 else
348 Append( choices[i] ) ;
349 }
350
351 #if wxUSE_OWNER_DRAWN
352 if ( m_windowStyle & wxLB_OWNERDRAW ) {
353 // first delete old items
354 size_t ui = m_aItems.Count();
355 while ( ui-- != 0 ) {
356 delete m_aItems[ui];
357 m_aItems[ui] = NULL;
358 }
359 m_aItems.Empty();
360
361 // then create new ones
362 for ( ui = 0; ui < (size_t)m_noItems; ui++ ) {
363 wxOwnerDrawn *pNewItem = CreateItem(ui);
364 pNewItem->SetName(choices[ui]);
365 m_aItems.Add(pNewItem);
366 }
367 }
368 #endif // wxUSE_OWNER_DRAWN
369 }
370
371 int wxListBox::FindString(const wxString& s) const
372 {
373
374 if ( s.Right(1) == wxT("*") )
375 {
376 wxString search = s.Left( s.Length() - 1 ) ;
377 int len = search.Length() ;
378 Str255 s1 , s2 ;
379 wxMacStringToPascal( search , s2 ) ;
380
381 for ( int i = 0 ; i < m_noItems ; ++ i )
382 {
383 wxMacStringToPascal( m_stringArray[i].Left( len ) , s1 ) ;
384
385 if ( EqualString( s1 , s2 , false , false ) )
386 return i ;
387 }
388 if ( s.Left(1) == wxT("*") && s.Length() > 1 )
389 {
390 wxString st = s ;
391 st.MakeLower() ;
392 for ( int i = 0 ; i < m_noItems ; ++i )
393 {
394 if ( GetString(i).Lower().Matches(st) )
395 return i ;
396 }
397 }
398
399 }
400 else
401 {
402 Str255 s1 , s2 ;
403
404 wxMacStringToPascal( s , s2 ) ;
405
406 for ( int i = 0 ; i < m_noItems ; ++ i )
407 {
408 wxMacStringToPascal( m_stringArray[i] , s1 ) ;
409
410 if ( EqualString( s1 , s2 , false , false ) )
411 return i ;
412 }
413 }
414 return -1;
415 }
416
417 void wxListBox::Clear()
418 {
419 FreeData();
420 m_noItems = 0;
421 m_stringArray.Empty() ;
422 m_dataArray.Empty() ;
423 MacClear() ;
424 }
425
426 void wxListBox::SetSelection(int N, bool select)
427 {
428 wxCHECK_RET( N >= 0 && N < m_noItems,
429 wxT("invalid index in wxListBox::SetSelection") );
430 MacSetSelection( N , select ) ;
431 GetSelections( m_selectionPreImage ) ;
432 }
433
434 bool wxListBox::IsSelected(int N) const
435 {
436 wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE,
437 wxT("invalid index in wxListBox::Selected") );
438
439 return MacIsSelected( N ) ;
440 }
441
442 void *wxListBox::DoGetItemClientData(int N) const
443 {
444 wxCHECK_MSG( N >= 0 && N < m_noItems, NULL,
445 wxT("invalid index in wxListBox::GetClientData"));
446
447 return (void *)m_dataArray[N];
448 }
449
450 wxClientData *wxListBox::DoGetItemClientObject(int N) const
451 {
452 return (wxClientData *) DoGetItemClientData( N ) ;
453 }
454
455 void wxListBox::DoSetItemClientData(int N, void *Client_data)
456 {
457 wxCHECK_RET( N >= 0 && N < m_noItems,
458 wxT("invalid index in wxListBox::SetClientData") );
459
460 #if wxUSE_OWNER_DRAWN
461 if ( m_windowStyle & wxLB_OWNERDRAW )
462 {
463 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
464 // in OnMeasure/OnDraw.
465 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
466 }
467 #endif // wxUSE_OWNER_DRAWN
468 wxASSERT_MSG( m_dataArray.GetCount() >= (size_t) N , wxT("invalid client_data array") ) ;
469
470 if ( m_dataArray.GetCount() > (size_t) N )
471 {
472 m_dataArray[N] = (char*) Client_data ;
473 }
474 else
475 {
476 m_dataArray.Add( (char*) Client_data ) ;
477 }
478 }
479
480 void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
481 {
482 DoSetItemClientData(n, clientData);
483 }
484
485 // Return number of selections and an array of selected integers
486 int wxListBox::GetSelections(wxArrayInt& aSelections) const
487 {
488 return MacGetSelections( aSelections ) ;
489 }
490
491 // Get single selection, for single choice list items
492 int wxListBox::GetSelection() const
493 {
494 return MacGetSelection() ;
495 }
496
497 // Find string for position
498 wxString wxListBox::GetString(int N) const
499 {
500 return m_stringArray[N] ;
501 }
502
503 void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
504 {
505 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
506 wxT("invalid index in wxListBox::InsertItems") );
507
508 int nItems = items.GetCount();
509
510 for ( int i = 0 ; i < nItems ; i++ )
511 {
512 m_stringArray.Insert( items[i] , pos + i ) ;
513 m_dataArray.Insert( NULL , pos + i ) ;
514 MacInsert( pos + i , items[i] ) ;
515 }
516
517 m_noItems += nItems;
518 }
519
520 void wxListBox::SetString(int N, const wxString& s)
521 {
522 m_stringArray[N] = s ;
523 MacSet( N , s ) ;
524 }
525
526 wxSize wxListBox::DoGetBestSize() const
527 {
528 int lbWidth = 100; // some defaults
529 int lbHeight = 110;
530 int wLine;
531
532 {
533 wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetTopLevelWindowRef() ) ) ;
534
535 if ( m_font.Ok() )
536 {
537 ::TextFont( m_font.MacGetFontNum() ) ;
538 ::TextSize( m_font.MacGetFontSize() ) ;
539 ::TextFace( m_font.MacGetFontStyle() ) ;
540 }
541 else
542 {
543 ::TextFont( kFontIDMonaco ) ;
544 ::TextSize( 9 );
545 ::TextFace( 0 ) ;
546 }
547
548 // Find the widest line
549 for(int i = 0; i < GetCount(); i++) {
550 wxString str(GetString(i));
551 #if wxUSE_UNICODE
552 Point bounds={0,0} ;
553 SInt16 baseline ;
554 ::GetThemeTextDimensions( wxMacCFStringHolder( str , m_font.GetEncoding() ) ,
555 kThemeCurrentPortFont,
556 kThemeStateActive,
557 false,
558 &bounds,
559 &baseline );
560 wLine = bounds.h ;
561 #else
562 wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ;
563 #endif
564 lbWidth = wxMax(lbWidth, wLine);
565 }
566
567 // Add room for the scrollbar
568 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
569
570 // And just a bit more
571 int cy = 12 ;
572 int cx = ::TextWidth( "X" , 0 , 1 ) ;
573 lbWidth += cx ;
574
575 // don't make the listbox too tall (limit height to around 10 items) but don't
576 // make it too small neither
577 lbHeight = (cy+4) * wxMin(wxMax(GetCount(), 3), 10);
578 }
579
580 return wxSize(lbWidth, lbHeight);
581 }
582
583 int wxListBox::GetCount() const
584 {
585 return m_noItems;
586 }
587
588 void wxListBox::Refresh(bool eraseBack, const wxRect *rect)
589 {
590 wxControl::Refresh( eraseBack , rect ) ;
591 // MacRedrawControl() ;
592 }
593
594 #if wxUSE_OWNER_DRAWN
595
596 class wxListBoxItem : public wxOwnerDrawn
597 {
598 public:
599 wxListBoxItem(const wxString& str = "");
600 };
601
602 wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE)
603 {
604 // no bitmaps/checkmarks
605 SetMarginWidth(0);
606 }
607
608 wxOwnerDrawn *wxListBox::CreateItem(size_t n)
609 {
610 return new wxListBoxItem();
611 }
612
613 #endif //USE_OWNER_DRAWN
614
615 // ============================================================================
616 // list box control implementation
617 // ============================================================================
618
619 void wxListBox::MacDelete( int N )
620 {
621 UInt32 id = m_idArray[N] ;
622 verify_noerr(::RemoveDataBrowserItems((ControlRef) m_macControl , kDataBrowserNoItem , 1 , (UInt32*) &id , kDataBrowserItemNoProperty ) ) ;
623 m_idArray.RemoveAt( N ) ;
624 }
625
626 void wxListBox::MacInsert( int n , const wxString& text)
627 {
628 verify_noerr(::AddDataBrowserItems( (ControlRef) m_macControl , kDataBrowserNoItem , 1 , (UInt32*) &m_nextId , kDataBrowserItemNoProperty ) ) ;
629 m_idArray.Insert( m_nextId , n ) ;
630 ++m_nextId ;
631 }
632
633 void wxListBox::MacAppend( const wxString& text)
634 {
635 verify_noerr(::AddDataBrowserItems( (ControlRef) m_macControl , kDataBrowserNoItem , 1 , (UInt32*) &m_nextId , kDataBrowserItemNoProperty ) ) ;
636 m_idArray.Add( m_nextId ) ;
637 ++m_nextId ;
638 }
639
640 void wxListBox::MacClear()
641 {
642 verify_noerr(::RemoveDataBrowserItems((ControlRef) m_macControl , kDataBrowserNoItem , 0 , NULL , kDataBrowserItemNoProperty ) ) ;
643 m_idArray.Empty() ;
644 }
645
646 void wxListBox::MacSetSelection( int n , bool select )
647 {
648 UInt32 id = m_idArray[n] ;
649 if ( ::IsDataBrowserItemSelected( (ControlRef) m_macControl , id ) != select )
650 {
651 verify_noerr(::SetDataBrowserSelectedItems((ControlRef) m_macControl , 1 , & id , kDataBrowserItemsToggle ) ) ;
652 }
653 MacScrollTo( n ) ;
654 }
655
656 bool wxListBox::MacIsSelected( int n ) const
657 {
658 return ::IsDataBrowserItemSelected( (ControlRef) m_macControl , m_idArray[n] ) ;
659 }
660
661 int wxListBox::MacGetSelection() const
662 {
663 for ( size_t i = 0 ; i < m_idArray.GetCount() ; ++i )
664 {
665 if ( ::IsDataBrowserItemSelected((ControlRef) m_macControl , m_idArray[i] ) )
666 {
667 return i ;
668 }
669 }
670 return -1 ;
671 }
672
673 int wxListBox::MacGetSelections( wxArrayInt& aSelections ) const
674 {
675 int no_sel = 0 ;
676
677 aSelections.Empty();
678 for ( size_t i = 0 ; i < m_idArray.GetCount() ; ++i )
679 {
680 if ( ::IsDataBrowserItemSelected((ControlRef) m_macControl , m_idArray[i] ) )
681 {
682 aSelections.Add( i ) ;
683 no_sel++ ;
684 }
685 }
686 return no_sel ;
687 }
688
689 void wxListBox::MacSet( int n , const wxString& text )
690 {
691 // as we don't store the strings we only have to issue a redraw
692 UInt32 id = m_idArray[n] ;
693 verify_noerr( ::UpdateDataBrowserItems( (ControlRef) m_macControl , kDataBrowserNoItem , 1 , &id , kDataBrowserItemNoProperty , kDataBrowserItemNoProperty ) ) ;
694 }
695
696 void wxListBox::MacScrollTo( int n )
697 {
698 // TODO implement scrolling
699 }
700
701 void wxListBox::OnSize( wxSizeEvent &event)
702 {
703 }
704
705 void wxListBox::MacHandleControlClick( WXWidget control , wxInt16 controlpart , bool WXUNUSED(mouseStillDown))
706 {
707 /*
708 Boolean wasDoubleClick = false ;
709 long result ;
710
711 ::GetControlData( (ControlRef) m_macControl , kControlNoPart , kControlListBoxDoubleClickTag , sizeof( wasDoubleClick ) , (char*) &wasDoubleClick , &result ) ;
712 if ( !wasDoubleClick )
713 {
714 MacDoClick() ;
715 }
716 else
717 {
718 MacDoDoubleClick() ;
719 }
720 */
721 }
722
723 void wxListBox::MacSetRedraw( bool doDraw )
724 {
725 // nothing to do in compositing mode
726 }
727
728 void wxListBox::MacDoClick()
729 {/*
730 wxArrayInt aSelections;
731 int n ;
732 size_t count = GetSelections(aSelections);
733
734 if ( count == m_selectionPreImage.GetCount() )
735 {
736 bool hasChanged = false ;
737 for ( size_t i = 0 ; i < count ; ++i )
738 {
739 if ( aSelections[i] != m_selectionPreImage[i] )
740 {
741 hasChanged = true ;
742 break ;
743 }
744 }
745 if ( !hasChanged )
746 {
747 return ;
748 }
749 }
750
751 m_selectionPreImage = aSelections;
752
753 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
754 event.SetEventObject( this );
755
756 if ( count > 0 )
757 {
758 n = aSelections[0];
759 if ( HasClientObjectData() )
760 event.SetClientObject( GetClientObject(n) );
761 else if ( HasClientUntypedData() )
762 event.SetClientData( GetClientData(n) );
763 event.SetString( GetString(n) );
764 }
765 else
766 {
767 n = -1;
768 }
769
770 event.m_commandInt = n;
771
772 GetEventHandler()->ProcessEvent(event);
773 */
774 }
775
776 void wxListBox::MacDoDoubleClick()
777 {
778 /*
779 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId);
780 event.SetEventObject( this );
781 GetEventHandler()->ProcessEvent(event) ;
782 */
783 }
784
785 void wxListBox::OnChar(wxKeyEvent& event)
786 {
787 // todo trigger proper events here
788 event.Skip() ;
789 return ;
790
791 if ( event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER)
792 {
793 wxWindow* parent = GetParent() ;
794 while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL )
795 parent = parent->GetParent() ;
796
797 if ( parent && parent->GetDefaultItem() )
798 {
799 wxButton *def = wxDynamicCast(parent->GetDefaultItem(),
800 wxButton);
801 if ( def && def->IsEnabled() )
802 {
803 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
804 event.SetEventObject(def);
805 def->Command(event);
806 return ;
807 }
808 }
809 event.Skip() ;
810 }
811 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
812 else if (event.GetKeyCode() == WXK_ESCAPE || (event.GetKeyCode() == '.' && event.MetaDown() ) )
813 {
814 // FIXME: look in ancestors, not just parent.
815 wxWindow* win = GetParent()->FindWindow( wxID_CANCEL ) ;
816 if (win)
817 {
818 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
819 new_event.SetEventObject( win );
820 win->GetEventHandler()->ProcessEvent( new_event );
821 }
822 }
823 else if ( event.GetKeyCode() == WXK_TAB )
824 {
825 wxNavigationKeyEvent new_event;
826 new_event.SetEventObject( this );
827 new_event.SetDirection( !event.ShiftDown() );
828 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
829 new_event.SetWindowChange( event.ControlDown() );
830 new_event.SetCurrentFocus( this );
831 if ( !GetEventHandler()->ProcessEvent( new_event ) )
832 event.Skip() ;
833 }
834 else if ( event.GetKeyCode() == WXK_DOWN || event.GetKeyCode() == WXK_UP )
835 {
836 // perform the default key handling first
837 wxControl::OnKeyDown( event ) ;
838
839 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
840 event.SetEventObject( this );
841
842 wxArrayInt aSelections;
843 int n, count = GetSelections(aSelections);
844 if ( count > 0 )
845 {
846 n = aSelections[0];
847 if ( HasClientObjectData() )
848 event.SetClientObject( GetClientObject(n) );
849 else if ( HasClientUntypedData() )
850 event.SetClientData( GetClientData(n) );
851 event.SetString( GetString(n) );
852 }
853 else
854 {
855 n = -1;
856 }
857
858 event.m_commandInt = n;
859
860 GetEventHandler()->ProcessEvent(event);
861 }
862 else
863 {
864 if ( event.GetTimestamp() > m_lastTypeIn + 60 )
865 {
866 m_typeIn = wxEmptyString ;
867 }
868 m_lastTypeIn = event.GetTimestamp() ;
869 m_typeIn += (char) event.GetKeyCode() ;
870 int line = FindString(wxT("*")+m_typeIn+wxT("*")) ;
871 if ( line >= 0 )
872 {
873 if ( GetSelection() != line )
874 {
875 SetSelection(line) ;
876 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
877 event.SetEventObject( this );
878
879 if ( HasClientObjectData() )
880 event.SetClientObject( GetClientObject( line ) );
881 else if ( HasClientUntypedData() )
882 event.SetClientData( GetClientData(line) );
883 event.SetString( GetString(line) );
884
885 event.m_commandInt = line ;
886
887 GetEventHandler()->ProcessEvent(event);
888 }
889 }
890 }
891 }
892
893 #else
894
895 // old carbon version
896
897 const short kwxMacListItemHeight = 19 ;
898
899 extern "C"
900 {
901 static pascal void wxMacListDefinition( short message, Boolean isSelected, Rect *drawRect,
902 Cell cell, short dataOffset, short dataLength,
903 ListHandle listHandle ) ;
904 }
905
906 static pascal void wxMacListDefinition( short message, Boolean isSelected, Rect *d,
907 Cell cell, short dataOffset, short dataLength,
908 ListHandle listHandle )
909 {
910 wxListBox* list;
911 Rect r = *d ;
912 Rect* drawRect = &r ;
913
914 list = (wxListBox*) GetControlReference( (ControlRef) GetListRefCon(listHandle) );
915 if ( list == NULL || list->GetHandle() == NULL || GetControlReference( ( ControlRef )list->GetHandle() ) == NULL )
916 return ;
917
918 // the bounds passed in are not correct, adjust the right border
919 int x = 0 , y = 0 ;
920 Rect bounds ;
921 GetControlBounds( (ControlRef) list->GetHandle() , &bounds ) ;
922 r.right = r.left + (bounds.right - bounds.left ) - 16 ;
923
924 GrafPtr savePort;
925 GrafPtr grafPtr;
926 RgnHandle savedClipRegion;
927 SInt32 savedPenMode;
928 GetPort(&savePort);
929 SetPort((**listHandle).port);
930 grafPtr = (**listHandle).port ;
931 // typecast our refCon
932
933 // Calculate the cell rect.
934
935 switch( message ) {
936 case lInitMsg:
937 break;
938
939 case lCloseMsg:
940 break;
941
942 case lDrawMsg:
943 {
944 const wxString linetext = list->m_stringArray[cell.v] ;
945
946 // Save the current clip region, and set the clip region to the area we are about
947 // to draw.
948
949 savedClipRegion = NewRgn();
950 GetClip( savedClipRegion );
951
952 ClipRect( drawRect );
953 EraseRect( drawRect );
954
955 const wxFont& font = list->GetFont();
956 if ( font.Ok() )
957 {
958 ::TextFont( font.MacGetFontNum() ) ;
959 ::TextSize( font.MacGetFontSize() ) ;
960 ::TextFace( font.MacGetFontStyle() ) ;
961 }
962 else
963 {
964 ::TextFont( kFontIDMonaco ) ;
965 ::TextSize( 9 );
966 ::TextFace( 0 ) ;
967 }
968
969 {
970 Rect frame = { drawRect->top, drawRect->left + 4,
971 drawRect->top + kwxMacListItemHeight, drawRect->right + 10000 } ;
972 CFMutableStringRef mString = CFStringCreateMutableCopy( NULL , 0 , wxMacCFStringHolder(linetext , list->GetFont().GetEncoding()) ) ;
973 ::TruncateThemeText( mString , kThemeCurrentPortFont, kThemeStateActive, drawRect->right - drawRect->left , truncEnd , NULL ) ;
974
975 ::DrawThemeTextBox( mString,
976 kThemeCurrentPortFont,
977 kThemeStateActive,
978 false,
979 &frame,
980 teJustLeft,
981 nil );
982
983 CFRelease( mString ) ;
984 }
985
986 // If the cell is hilited, do the hilite now. Paint the cell contents with the
987 // appropriate QuickDraw transform mode.
988
989 if( isSelected ) {
990 savedPenMode = GetPortPenMode( (CGrafPtr) grafPtr );
991 SetPortPenMode( (CGrafPtr)grafPtr, hilitetransfermode );
992 PaintRect( drawRect );
993 SetPortPenMode( (CGrafPtr)grafPtr, savedPenMode );
994 }
995
996 // Restore the saved clip region.
997
998 SetClip( savedClipRegion );
999 DisposeRgn( savedClipRegion );
1000 }
1001 break;
1002 case lHiliteMsg:
1003
1004 // Hilite or unhilite the cell. Paint the cell contents with the
1005 // appropriate QuickDraw transform mode.
1006
1007 GetPort( &grafPtr );
1008 savedPenMode = GetPortPenMode( (CGrafPtr)grafPtr );
1009 SetPortPenMode( (CGrafPtr)grafPtr, hilitetransfermode );
1010 PaintRect( drawRect );
1011 SetPortPenMode( (CGrafPtr)grafPtr, savedPenMode );
1012 break;
1013 default :
1014 break ;
1015 }
1016 SetPort(savePort);
1017 }
1018
1019 extern "C" void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon) ;
1020 // resources ldef ids
1021 const short kwxMacListWithVerticalScrollbar = 128 ;
1022 const short kwxMacListWithVerticalAndHorizontalScrollbar = 129 ;
1023
1024 // ============================================================================
1025 // list box control implementation
1026 // ============================================================================
1027
1028 // Listbox item
1029 wxListBox::wxListBox()
1030 {
1031 m_noItems = 0;
1032 m_selected = 0;
1033 m_macList = NULL ;
1034 }
1035
1036 static ListDefUPP macListDefUPP = NULL ;
1037
1038 bool wxListBox::Create(wxWindow *parent, wxWindowID id,
1039 const wxPoint& pos,
1040 const wxSize& size,
1041 const wxArrayString& choices,
1042 long style,
1043 const wxValidator& validator,
1044 const wxString& name)
1045 {
1046 wxCArrayString chs(choices);
1047
1048 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
1049 style, validator, name);
1050 }
1051
1052 bool wxListBox::Create(wxWindow *parent, wxWindowID id,
1053 const wxPoint& pos,
1054 const wxSize& size,
1055 int n, const wxString choices[],
1056 long style,
1057 const wxValidator& validator,
1058 const wxString& name)
1059 {
1060 m_macIsUserPane = FALSE ;
1061
1062 if ( !wxListBoxBase::Create(parent, id, pos, size, style & ~(wxHSCROLL|wxVSCROLL), validator, name) )
1063 return false;
1064
1065 m_noItems = 0 ; // this will be increased by our append command
1066 m_selected = 0;
1067
1068 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
1069
1070 ListDefSpec listDef;
1071 listDef.defType = kListDefUserProcType;
1072 if ( macListDefUPP == NULL )
1073 {
1074 macListDefUPP = NewListDefUPP( wxMacListDefinition );
1075 }
1076 listDef.u.userProc = macListDefUPP ;
1077
1078 Str255 fontName ;
1079 SInt16 fontSize ;
1080 Style fontStyle ;
1081
1082 GetThemeFont(kThemeViewsFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
1083
1084 SetFont( wxFont (fontSize, wxSWISS, wxNORMAL, wxNORMAL , false , wxMacMakeStringFromPascal( fontName ) ) ) ;
1085
1086 Size asize;
1087
1088
1089 CreateListBoxControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds, false, 0, 1, (style & wxLB_HSCROLL), true,
1090 kwxMacListItemHeight, kwxMacListItemHeight, false, &listDef, (ControlRef *)&m_macControl );
1091
1092 GetControlData( (ControlRef) m_macControl, kControlNoPart, kControlListBoxListHandleTag,
1093 sizeof(ListHandle), (Ptr) &m_macList, &asize);
1094
1095 SetControlReference( (ControlRef) m_macControl, (long) this);
1096
1097
1098 OptionBits options = 0;
1099 if ( style & wxLB_MULTIPLE )
1100 {
1101 options += lExtendDrag + lUseSense ;
1102 }
1103 else if ( style & wxLB_EXTENDED )
1104 {
1105 // default behaviour
1106 }
1107 else
1108 {
1109 options = (OptionBits) lOnlyOne ;
1110 }
1111 SetListSelectionFlags((ListHandle)m_macList, options);
1112
1113 for ( int i = 0 ; i < n ; i++ )
1114 {
1115 Append( choices[i] ) ;
1116 }
1117
1118 MacPostControlCreate(pos,size) ;
1119
1120 LSetDrawingMode( true , (ListHandle)m_macList ) ;
1121
1122 return TRUE;
1123 }
1124
1125 wxListBox::~wxListBox()
1126 {
1127 SetControlReference( (ControlRef) m_macControl , NULL ) ;
1128 FreeData() ;
1129 // avoid access during destruction
1130 if ( m_macList )
1131 {
1132 m_macList = NULL ;
1133 }
1134 }
1135
1136 void wxListBox::FreeData()
1137 {
1138 #if wxUSE_OWNER_DRAWN
1139 if ( m_windowStyle & wxLB_OWNERDRAW )
1140 {
1141 size_t uiCount = m_aItems.Count();
1142 while ( uiCount-- != 0 ) {
1143 delete m_aItems[uiCount];
1144 m_aItems[uiCount] = NULL;
1145 }
1146
1147 m_aItems.Clear();
1148 }
1149 else
1150 #endif // wxUSE_OWNER_DRAWN
1151 if ( HasClientObjectData() )
1152 {
1153 for ( size_t n = 0; n < (size_t)m_noItems; n++ )
1154 {
1155 delete GetClientObject(n);
1156 }
1157 }
1158 }
1159
1160 void wxListBox::DoSetSize(int x, int y,
1161 int width, int height,
1162 int sizeFlags )
1163 {
1164 wxControl::DoSetSize( x , y , width , height , sizeFlags ) ;
1165 #if TARGET_CARBON
1166 Rect bounds ;
1167 GetControlBounds( (ControlRef) m_macControl , &bounds ) ;
1168 ControlRef control = GetListVerticalScrollBar( (ListHandle)m_macList ) ;
1169 if ( control )
1170 {
1171 Rect scrollbounds ;
1172 GetControlBounds( control , &scrollbounds ) ;
1173 if( scrollbounds.right != bounds.right + 1 )
1174 {
1175 UMAMoveControl( control , bounds.right - (scrollbounds.right - scrollbounds.left) + 1 ,
1176 scrollbounds.top ) ;
1177 }
1178 }
1179 #endif
1180 }
1181 void wxListBox::DoSetFirstItem(int N)
1182 {
1183 MacScrollTo( N ) ;
1184 }
1185
1186 void wxListBox::Delete(int N)
1187 {
1188 wxCHECK_RET( N >= 0 && N < m_noItems,
1189 wxT("invalid index in wxListBox::Delete") );
1190
1191 #if wxUSE_OWNER_DRAWN
1192 delete m_aItems[N];
1193 m_aItems.RemoveAt(N);
1194 #else // !wxUSE_OWNER_DRAWN
1195 if ( HasClientObjectData() )
1196 {
1197 delete GetClientObject(N);
1198 }
1199 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
1200 m_stringArray.RemoveAt( N ) ;
1201 m_dataArray.RemoveAt( N ) ;
1202 m_noItems --;
1203
1204 MacDelete( N ) ;
1205 }
1206
1207 int wxListBox::DoAppend(const wxString& item)
1208 {
1209 int index = m_noItems ;
1210 m_stringArray.Add( item ) ;
1211 m_dataArray.Add( NULL );
1212 m_noItems ++;
1213 DoSetItemClientData( index , NULL ) ;
1214 MacAppend( item ) ;
1215
1216 return index ;
1217 }
1218
1219 void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
1220 {
1221 MacSetRedraw( false ) ;
1222 Clear() ;
1223 int n = choices.GetCount();
1224
1225 for( int i = 0 ; i < n ; ++i )
1226 {
1227 if ( clientData )
1228 {
1229 #if wxUSE_OWNER_DRAWN
1230 wxASSERT_MSG(clientData[i] == NULL,
1231 wxT("Can't use client data with owner-drawn listboxes"));
1232 #else // !wxUSE_OWNER_DRAWN
1233 Append( choices[i] , clientData[i] ) ;
1234 #endif
1235 }
1236 else
1237 Append( choices[i] ) ;
1238 }
1239
1240 #if wxUSE_OWNER_DRAWN
1241 if ( m_windowStyle & wxLB_OWNERDRAW ) {
1242 // first delete old items
1243 size_t ui = m_aItems.Count();
1244 while ( ui-- != 0 ) {
1245 delete m_aItems[ui];
1246 m_aItems[ui] = NULL;
1247 }
1248 m_aItems.Empty();
1249
1250 // then create new ones
1251 for ( ui = 0; ui < (size_t)m_noItems; ui++ ) {
1252 wxOwnerDrawn *pNewItem = CreateItem(ui);
1253 pNewItem->SetName(choices[ui]);
1254 m_aItems.Add(pNewItem);
1255 }
1256 }
1257 #endif // wxUSE_OWNER_DRAWN
1258 MacSetRedraw( true ) ;
1259 }
1260
1261 bool wxListBox::HasMultipleSelection() const
1262 {
1263 return (m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED);
1264 }
1265
1266 int wxListBox::FindString(const wxString& s) const
1267 {
1268
1269 if ( s.Right(1) == wxT("*") )
1270 {
1271 wxString search = s.Left( s.Length() - 1 ) ;
1272 int len = search.Length() ;
1273 Str255 s1 , s2 ;
1274 wxMacStringToPascal( search , s2 ) ;
1275
1276 for ( int i = 0 ; i < m_noItems ; ++ i )
1277 {
1278 wxMacStringToPascal( m_stringArray[i].Left( len ) , s1 ) ;
1279
1280 if ( EqualString( s1 , s2 , false , false ) )
1281 return i ;
1282 }
1283 if ( s.Left(1) == wxT("*") && s.Length() > 1 )
1284 {
1285 wxString st = s ;
1286 st.MakeLower() ;
1287 for ( int i = 0 ; i < m_noItems ; ++i )
1288 {
1289 if ( GetString(i).Lower().Matches(st) )
1290 return i ;
1291 }
1292 }
1293
1294 }
1295 else
1296 {
1297 Str255 s1 , s2 ;
1298
1299 wxMacStringToPascal( s , s2 ) ;
1300
1301 for ( int i = 0 ; i < m_noItems ; ++ i )
1302 {
1303 wxMacStringToPascal( m_stringArray[i] , s1 ) ;
1304
1305 if ( EqualString( s1 , s2 , false , false ) )
1306 return i ;
1307 }
1308 }
1309 return -1;
1310 }
1311
1312 void wxListBox::Clear()
1313 {
1314 FreeData();
1315 m_noItems = 0;
1316 m_stringArray.Empty() ;
1317 m_dataArray.Empty() ;
1318 MacClear() ;
1319 }
1320
1321 void wxListBox::SetSelection(int N, bool select)
1322 {
1323 wxCHECK_RET( N >= 0 && N < m_noItems,
1324 wxT("invalid index in wxListBox::SetSelection") );
1325 MacSetSelection( N , select ) ;
1326 GetSelections( m_selectionPreImage ) ;
1327 }
1328
1329 bool wxListBox::IsSelected(int N) const
1330 {
1331 wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE,
1332 wxT("invalid index in wxListBox::Selected") );
1333
1334 return MacIsSelected( N ) ;
1335 }
1336
1337 void *wxListBox::DoGetItemClientData(int N) const
1338 {
1339 wxCHECK_MSG( N >= 0 && N < m_noItems, NULL,
1340 wxT("invalid index in wxListBox::GetClientData"));
1341
1342 return (void *)m_dataArray[N];
1343 }
1344
1345 wxClientData *wxListBox::DoGetItemClientObject(int N) const
1346 {
1347 return (wxClientData *) DoGetItemClientData( N ) ;
1348 }
1349
1350 void wxListBox::DoSetItemClientData(int N, void *Client_data)
1351 {
1352 wxCHECK_RET( N >= 0 && N < m_noItems,
1353 wxT("invalid index in wxListBox::SetClientData") );
1354
1355 #if wxUSE_OWNER_DRAWN
1356 if ( m_windowStyle & wxLB_OWNERDRAW )
1357 {
1358 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
1359 // in OnMeasure/OnDraw.
1360 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
1361 }
1362 #endif // wxUSE_OWNER_DRAWN
1363 wxASSERT_MSG( m_dataArray.GetCount() >= (size_t) N , wxT("invalid client_data array") ) ;
1364
1365 if ( m_dataArray.GetCount() > (size_t) N )
1366 {
1367 m_dataArray[N] = (char*) Client_data ;
1368 }
1369 else
1370 {
1371 m_dataArray.Add( (char*) Client_data ) ;
1372 }
1373 }
1374
1375 void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
1376 {
1377 DoSetItemClientData(n, clientData);
1378 }
1379
1380 // Return number of selections and an array of selected integers
1381 int wxListBox::GetSelections(wxArrayInt& aSelections) const
1382 {
1383 return MacGetSelections( aSelections ) ;
1384 }
1385
1386 // Get single selection, for single choice list items
1387 int wxListBox::GetSelection() const
1388 {
1389 return MacGetSelection() ;
1390 }
1391
1392 // Find string for position
1393 wxString wxListBox::GetString(int N) const
1394 {
1395 return m_stringArray[N] ;
1396 }
1397
1398 void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
1399 {
1400 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
1401 wxT("invalid index in wxListBox::InsertItems") );
1402
1403 int nItems = items.GetCount();
1404
1405 for ( int i = 0 ; i < nItems ; i++ )
1406 {
1407 m_stringArray.Insert( items[i] , pos + i ) ;
1408 m_dataArray.Insert( NULL , pos + i ) ;
1409 MacInsert( pos + i , items[i] ) ;
1410 }
1411
1412 m_noItems += nItems;
1413 }
1414
1415 void wxListBox::SetString(int N, const wxString& s)
1416 {
1417 m_stringArray[N] = s ;
1418 MacSet( N , s ) ;
1419 }
1420
1421 wxSize wxListBox::DoGetBestSize() const
1422 {
1423 int lbWidth = 100; // some defaults
1424 int lbHeight = 110;
1425 int wLine;
1426
1427 {
1428 wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetTopLevelWindowRef() ) ) ;
1429
1430 if ( m_font.Ok() )
1431 {
1432 ::TextFont( m_font.MacGetFontNum() ) ;
1433 ::TextSize( m_font.MacGetFontSize() ) ;
1434 ::TextFace( m_font.MacGetFontStyle() ) ;
1435 }
1436 else
1437 {
1438 ::TextFont( kFontIDMonaco ) ;
1439 ::TextSize( 9 );
1440 ::TextFace( 0 ) ;
1441 }
1442
1443 // Find the widest line
1444 for(int i = 0; i < GetCount(); i++) {
1445 wxString str(GetString(i));
1446 #if wxUSE_UNICODE
1447 Point bounds={0,0} ;
1448 SInt16 baseline ;
1449 ::GetThemeTextDimensions( wxMacCFStringHolder( str , m_font.GetEncoding() ) ,
1450 kThemeCurrentPortFont,
1451 kThemeStateActive,
1452 false,
1453 &bounds,
1454 &baseline );
1455 wLine = bounds.h ;
1456 #else
1457 wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ;
1458 #endif
1459 lbWidth = wxMax(lbWidth, wLine);
1460 }
1461
1462 // Add room for the scrollbar
1463 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
1464
1465 // And just a bit more
1466 int cy = 12 ;
1467 int cx = ::TextWidth( "X" , 0 , 1 ) ;
1468 lbWidth += cx ;
1469
1470 // don't make the listbox too tall (limit height to around 10 items) but don't
1471 // make it too small neither
1472 lbHeight = (cy+4) * wxMin(wxMax(GetCount(), 3), 10);
1473 }
1474
1475 return wxSize(lbWidth, lbHeight);
1476 }
1477
1478 int wxListBox::GetCount() const
1479 {
1480 return m_noItems;
1481 }
1482
1483 void wxListBox::Refresh(bool eraseBack, const wxRect *rect)
1484 {
1485 wxControl::Refresh( eraseBack , rect ) ;
1486 // MacRedrawControl() ;
1487 }
1488
1489 #if wxUSE_OWNER_DRAWN
1490
1491 class wxListBoxItem : public wxOwnerDrawn
1492 {
1493 public:
1494 wxListBoxItem(const wxString& str = "");
1495 };
1496
1497 wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE)
1498 {
1499 // no bitmaps/checkmarks
1500 SetMarginWidth(0);
1501 }
1502
1503 wxOwnerDrawn *wxListBox::CreateItem(size_t n)
1504 {
1505 return new wxListBoxItem();
1506 }
1507
1508 #endif //USE_OWNER_DRAWN
1509
1510 // ============================================================================
1511 // list box control implementation
1512 // ============================================================================
1513
1514 /*
1515 void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon)
1516 {
1517 wxListBox* list;
1518 // typecast our refCon
1519 list = (wxListBox*)refCon;
1520
1521 MoveTo(cellRect->left + 4 , cellRect->top + 10 );
1522 const wxString text = list->m_stringArray[lCell.v] ;
1523 ::TextFont( kFontIDMonaco ) ;
1524 ::TextSize( 9 );
1525 ::TextFace( 0 ) ;
1526 DrawText(text, 0 , text.Length());
1527
1528 }
1529 */
1530 void wxListBox::MacDelete( int N )
1531 {
1532 LDelRow( 1 , N , (ListHandle)m_macList) ;
1533 Refresh();
1534 }
1535
1536 void wxListBox::MacInsert( int n , const wxString& text)
1537 {
1538 Cell cell = { 0 , 0 } ;
1539 cell.v = n ;
1540 LAddRow( 1 , cell.v , (ListHandle)m_macList ) ;
1541 // LSetCell(text, strlen(text), cell, m_macList);
1542 Refresh();
1543 }
1544
1545 void wxListBox::MacAppend( const wxString& text)
1546 {
1547 Cell cell = { 0 , 0 } ;
1548 cell.v = (**(ListHandle)m_macList).dataBounds.bottom ;
1549 LAddRow( 1 , cell.v , (ListHandle)m_macList ) ;
1550 // LSetCell(text, strlen(text), cell, m_macList);
1551 Refresh();
1552 }
1553
1554 void wxListBox::MacClear()
1555 {
1556 LDelRow( (**(ListHandle)m_macList).dataBounds.bottom , 0 ,(ListHandle) m_macList ) ;
1557 Refresh();
1558 }
1559
1560 void wxListBox::MacSetSelection( int n , bool select )
1561 {
1562 Cell cell = { 0 , 0 } ;
1563 if ( ! (m_windowStyle & wxLB_MULTIPLE) )
1564 {
1565 if ( LGetSelect( true , &cell , (ListHandle)m_macList ) )
1566 {
1567 LSetSelect( false , cell , (ListHandle)m_macList ) ;
1568 }
1569 }
1570
1571 cell.v = n ;
1572 LSetSelect( select , cell , (ListHandle)m_macList ) ;
1573 LAutoScroll( (ListHandle)m_macList ) ;
1574 Refresh();
1575 }
1576
1577 bool wxListBox::MacIsSelected( int n ) const
1578 {
1579 Cell cell = { 0 , 0 } ;
1580 cell.v = n ;
1581 return LGetSelect( false , &cell , (ListHandle)m_macList ) ;
1582 }
1583
1584 int wxListBox::MacGetSelection() const
1585 {
1586 Cell cell = { 0 , 0 } ;
1587 if ( LGetSelect( true , &cell , (ListHandle)m_macList ) )
1588 return cell.v ;
1589 else
1590 return -1 ;
1591 }
1592
1593 int wxListBox::MacGetSelections( wxArrayInt& aSelections ) const
1594 {
1595 int no_sel = 0 ;
1596
1597 aSelections.Empty();
1598
1599 Cell cell = { 0 , 0 } ;
1600 cell.v = 0 ;
1601
1602 while ( LGetSelect( true , &cell ,(ListHandle) m_macList ) )
1603 {
1604 aSelections.Add( cell.v ) ;
1605 no_sel++ ;
1606 cell.v++ ;
1607 }
1608 return no_sel ;
1609 }
1610
1611 void wxListBox::MacSet( int n , const wxString& text )
1612 {
1613 // our implementation does not store anything in the list
1614 // so we just have to redraw
1615 Cell cell = { 0 , 0 } ;
1616 cell.v = n ;
1617 // LSetCell(text, strlen(text), cell, m_macList);
1618 Refresh();
1619 }
1620
1621 void wxListBox::MacScrollTo( int n )
1622 {
1623 // TODO implement scrolling
1624 }
1625
1626 void wxListBox::OnSize( wxSizeEvent &event)
1627 {
1628 Point pt;
1629
1630 #if TARGET_CARBON
1631 GetListCellSize((ListHandle)m_macList, &pt);
1632 #else
1633 pt = (**(ListHandle)m_macList).cellSize ;
1634 #endif
1635 int w, h ;
1636 GetSize( &w , &h ) ;
1637 pt.h = w - 15 ;
1638 LCellSize( pt , (ListHandle)m_macList ) ;
1639 }
1640
1641 void wxListBox::MacHandleControlClick( WXWidget control , wxInt16 controlpart , bool WXUNUSED(mouseStillDown))
1642 {
1643 Boolean wasDoubleClick = false ;
1644 long result ;
1645
1646 ::GetControlData( (ControlRef) m_macControl , kControlNoPart , kControlListBoxDoubleClickTag , sizeof( wasDoubleClick ) , (char*) &wasDoubleClick , &result ) ;
1647 if ( !wasDoubleClick )
1648 {
1649 MacDoClick() ;
1650 }
1651 else
1652 {
1653 MacDoDoubleClick() ;
1654 }
1655 }
1656
1657 void wxListBox::MacSetRedraw( bool doDraw )
1658 {
1659 LSetDrawingMode( doDraw , (ListHandle)m_macList ) ;
1660
1661 }
1662
1663 void wxListBox::MacDoClick()
1664 {
1665 wxArrayInt aSelections;
1666 int n ;
1667 size_t count = GetSelections(aSelections);
1668
1669 if ( count == m_selectionPreImage.GetCount() )
1670 {
1671 bool hasChanged = false ;
1672 for ( size_t i = 0 ; i < count ; ++i )
1673 {
1674 if ( aSelections[i] != m_selectionPreImage[i] )
1675 {
1676 hasChanged = true ;
1677 break ;
1678 }
1679 }
1680 if ( !hasChanged )
1681 {
1682 return ;
1683 }
1684 }
1685
1686 m_selectionPreImage = aSelections;
1687
1688 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
1689 event.SetEventObject( this );
1690
1691 if ( count > 0 )
1692 {
1693 n = aSelections[0];
1694 if ( HasClientObjectData() )
1695 event.SetClientObject( GetClientObject(n) );
1696 else if ( HasClientUntypedData() )
1697 event.SetClientData( GetClientData(n) );
1698 event.SetString( GetString(n) );
1699 }
1700 else
1701 {
1702 n = -1;
1703 }
1704
1705 event.m_commandInt = n;
1706
1707 GetEventHandler()->ProcessEvent(event);
1708 }
1709
1710 void wxListBox::MacDoDoubleClick()
1711 {
1712 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId);
1713 event.SetEventObject( this );
1714 GetEventHandler()->ProcessEvent(event) ;
1715 }
1716
1717 void wxListBox::OnChar(wxKeyEvent& event)
1718 {
1719 if ( event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER)
1720 {
1721 wxWindow* parent = GetParent() ;
1722 while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL )
1723 parent = parent->GetParent() ;
1724
1725 if ( parent && parent->GetDefaultItem() )
1726 {
1727 wxButton *def = wxDynamicCast(parent->GetDefaultItem(),
1728 wxButton);
1729 if ( def && def->IsEnabled() )
1730 {
1731 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1732 event.SetEventObject(def);
1733 def->Command(event);
1734 return ;
1735 }
1736 }
1737 event.Skip() ;
1738 }
1739 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
1740 else if (event.GetKeyCode() == WXK_ESCAPE || (event.GetKeyCode() == '.' && event.MetaDown() ) )
1741 {
1742 // FIXME: look in ancestors, not just parent.
1743 wxWindow* win = GetParent()->FindWindow( wxID_CANCEL ) ;
1744 if (win)
1745 {
1746 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1747 new_event.SetEventObject( win );
1748 win->GetEventHandler()->ProcessEvent( new_event );
1749 }
1750 }
1751 else if ( event.GetKeyCode() == WXK_TAB )
1752 {
1753 wxNavigationKeyEvent new_event;
1754 new_event.SetEventObject( this );
1755 new_event.SetDirection( !event.ShiftDown() );
1756 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1757 new_event.SetWindowChange( event.ControlDown() );
1758 new_event.SetCurrentFocus( this );
1759 if ( !GetEventHandler()->ProcessEvent( new_event ) )
1760 event.Skip() ;
1761 }
1762 else if ( event.GetKeyCode() == WXK_DOWN || event.GetKeyCode() == WXK_UP )
1763 {
1764 // perform the default key handling first
1765 wxControl::OnKeyDown( event ) ;
1766
1767 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
1768 event.SetEventObject( this );
1769
1770 wxArrayInt aSelections;
1771 int n, count = GetSelections(aSelections);
1772 if ( count > 0 )
1773 {
1774 n = aSelections[0];
1775 if ( HasClientObjectData() )
1776 event.SetClientObject( GetClientObject(n) );
1777 else if ( HasClientUntypedData() )
1778 event.SetClientData( GetClientData(n) );
1779 event.SetString( GetString(n) );
1780 }
1781 else
1782 {
1783 n = -1;
1784 }
1785
1786 event.m_commandInt = n;
1787
1788 GetEventHandler()->ProcessEvent(event);
1789 }
1790 else
1791 {
1792 if ( event.GetTimestamp() > m_lastTypeIn + 60 )
1793 {
1794 m_typeIn = wxEmptyString ;
1795 }
1796 m_lastTypeIn = event.GetTimestamp() ;
1797 m_typeIn += (char) event.GetKeyCode() ;
1798 int line = FindString(wxT("*")+m_typeIn+wxT("*")) ;
1799 if ( line >= 0 )
1800 {
1801 if ( GetSelection() != line )
1802 {
1803 SetSelection(line) ;
1804 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
1805 event.SetEventObject( this );
1806
1807 if ( HasClientObjectData() )
1808 event.SetClientObject( GetClientObject( line ) );
1809 else if ( HasClientUntypedData() )
1810 event.SetClientData( GetClientData(line) );
1811 event.SetString( GetString(line) );
1812
1813 event.m_commandInt = line ;
1814
1815 GetEventHandler()->ProcessEvent(event);
1816 }
1817 }
1818 }
1819 }
1820
1821 #endif