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