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