wxMac Unicode 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 EVT_SIZE( wxListBox::OnSize )
31 EVT_CHAR( wxListBox::OnChar )
32 END_EVENT_TABLE()
33 #endif
34
35 #include "wx/mac/uma.h"
36
37 #if PRAGMA_STRUCT_ALIGN
38 #pragma options align=mac68k
39 #elif PRAGMA_STRUCT_PACKPUSH
40 #pragma pack(push, 2)
41 #elif PRAGMA_STRUCT_PACK
42 #pragma pack(2)
43 #endif
44
45 typedef struct {
46 unsigned short instruction;
47 void (*function)();
48 } ldefRec, *ldefPtr, **ldefHandle;
49
50 #if PRAGMA_STRUCT_ALIGN
51 #pragma options align=reset
52 #elif PRAGMA_STRUCT_PACKPUSH
53 #pragma pack(pop)
54 #elif PRAGMA_STRUCT_PACK
55 #pragma pack()
56 #endif
57
58 #if TARGET_CARBON
59 const short kwxMacListItemHeight = 19 ;
60 #else
61 const short kwxMacListItemHeight = 14 ;
62 #endif
63
64 extern "C"
65 {
66 static pascal void wxMacListDefinition( short message, Boolean isSelected, Rect *drawRect,
67 Cell cell, short dataOffset, short dataLength,
68 ListHandle listHandle ) ;
69 }
70
71 static pascal void wxMacListDefinition( short message, Boolean isSelected, Rect *drawRect,
72 Cell cell, short dataOffset, short dataLength,
73 ListHandle listHandle )
74 {
75 GrafPtr savePort;
76 GrafPtr grafPtr;
77 RgnHandle savedClipRegion;
78 SInt32 savedPenMode;
79 wxListBox* list;
80 GetPort(&savePort);
81 SetPort((**listHandle).port);
82 grafPtr = (**listHandle).port ;
83 // typecast our refCon
84 list = (wxListBox*) GetControlReference( (ControlHandle) GetListRefCon(listHandle) );
85
86 // Calculate the cell rect.
87
88 switch( message ) {
89 case lInitMsg:
90 break;
91
92 case lCloseMsg:
93 break;
94
95 case lDrawMsg:
96 {
97 const wxString linetext = list->m_stringArray[cell.v] ;
98
99 // Save the current clip region, and set the clip region to the area we are about
100 // to draw.
101
102 savedClipRegion = NewRgn();
103 GetClip( savedClipRegion );
104
105 ClipRect( drawRect );
106 EraseRect( drawRect );
107
108 wxFontRefData * font = (wxFontRefData*) list->GetFont().GetRefData() ;
109
110 if ( font )
111 {
112 ::TextFont( font->m_macFontNum ) ;
113 ::TextSize( short(font->m_macFontSize) ) ;
114 ::TextFace( font->m_macFontStyle ) ;
115 }
116 else
117 {
118 ::TextFont( kFontIDMonaco ) ;
119 ::TextSize( 9 );
120 ::TextFace( 0 ) ;
121 }
122
123 #if TARGET_CARBON
124 {
125 Rect frame = { drawRect->top, drawRect->left + 4,
126 drawRect->top + kwxMacListItemHeight, drawRect->right + 10000 } ;
127 CFMutableStringRef mString = CFStringCreateMutableCopy( NULL , 0 , wxMacCFStringHolder(linetext) ) ;
128 ::TruncateThemeText( mString , kThemeCurrentPortFont, kThemeStateActive, drawRect->right - drawRect->left , truncEnd , NULL ) ;
129 ::DrawThemeTextBox( mString,
130 kThemeCurrentPortFont,
131 kThemeStateActive,
132 false,
133 &frame,
134 teJustLeft,
135 nil );
136 CFRelease( mString ) ;
137 }
138 #else
139 {
140 wxCharBuffer text = wxMacStringToCString( linetext ) ;
141 MoveTo(drawRect->left + 4 , drawRect->top + 10 );
142 DrawText(text, 0 , strlen(text) );
143 }
144 #endif
145 // If the cell is hilited, do the hilite now. Paint the cell contents with the
146 // appropriate QuickDraw transform mode.
147
148 if( isSelected ) {
149 savedPenMode = GetPortPenMode( (CGrafPtr) grafPtr );
150 SetPortPenMode( (CGrafPtr)grafPtr, hilitetransfermode );
151 PaintRect( drawRect );
152 SetPortPenMode( (CGrafPtr)grafPtr, savedPenMode );
153 }
154
155 // Restore the saved clip region.
156
157 SetClip( savedClipRegion );
158 DisposeRgn( savedClipRegion );
159 }
160 break;
161 case lHiliteMsg:
162
163 // Hilite or unhilite the cell. Paint the cell contents with the
164 // appropriate QuickDraw transform mode.
165
166 GetPort( &grafPtr );
167 savedPenMode = GetPortPenMode( (CGrafPtr)grafPtr );
168 SetPortPenMode( (CGrafPtr)grafPtr, hilitetransfermode );
169 PaintRect( drawRect );
170 SetPortPenMode( (CGrafPtr)grafPtr, savedPenMode );
171 break;
172 default :
173 break ;
174 }
175 SetPort(savePort);
176 }
177
178 extern "C" void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon) ;
179 // resources ldef ids
180 const short kwxMacListWithVerticalScrollbar = 128 ;
181 const short kwxMacListWithVerticalAndHorizontalScrollbar = 129 ;
182
183 // ============================================================================
184 // list box control implementation
185 // ============================================================================
186
187 // Listbox item
188 wxListBox::wxListBox()
189 {
190 m_noItems = 0;
191 m_selected = 0;
192 m_macList = NULL ;
193 }
194
195 static ListDefUPP macListDefUPP = NULL ;
196
197 bool wxListBox::Create(wxWindow *parent, wxWindowID id,
198 const wxPoint& pos,
199 const wxSize& size,
200 int n, const wxString choices[],
201 long style,
202 const wxValidator& validator,
203 const wxString& name)
204 {
205 m_noItems = 0 ; // this will be increased by our append command
206 m_selected = 0;
207
208 Rect bounds ;
209 Str255 title ;
210
211 MacPreControlCreate( parent , id , wxEmptyString , pos , size ,style, validator , name , &bounds , title ) ;
212
213 ListDefSpec listDef;
214 listDef.defType = kListDefUserProcType;
215 if ( macListDefUPP == NULL )
216 {
217 macListDefUPP = NewListDefUPP( wxMacListDefinition );
218 }
219 listDef.u.userProc = macListDefUPP ;
220
221 Str255 fontName ;
222 SInt16 fontSize ;
223 Style fontStyle ;
224 SInt16 fontNum ;
225 #if TARGET_CARBON
226 GetThemeFont(kThemeViewsFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
227 #else
228 GetFontName( kFontIDMonaco , fontName ) ;
229 fontSize = 9 ;
230 fontStyle = normal ;
231 #endif
232 SetFont( wxFont (fontSize, wxSWISS, wxNORMAL, wxNORMAL , false , wxMacMakeStringFromPascal( fontName ) ) ) ;
233 #if TARGET_CARBON
234 Size asize;
235
236
237 CreateListBoxControl( MAC_WXHWND(parent->MacGetRootWindow()), &bounds, false, 0, 1, (style & wxLB_HSCROLL), true,
238 kwxMacListItemHeight, kwxMacListItemHeight, false, &listDef, (ControlRef *)&m_macControl );
239
240 GetControlData( (ControlHandle) m_macControl, kControlNoPart, kControlListBoxListHandleTag,
241 sizeof(ListHandle), (Ptr) &m_macList, &asize);
242
243 SetControlReference( (ControlHandle) m_macControl, (long) this);
244 SetControlVisibility( (ControlHandle) m_macControl, false, false);
245
246 #else
247
248 long result ;
249 wxStAppResource resload ;
250 m_macControl = ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , title , false ,
251 (style & wxLB_HSCROLL) ? kwxMacListWithVerticalAndHorizontalScrollbar : kwxMacListWithVerticalScrollbar ,
252 0 , 0, kControlListBoxProc , (long) this ) ;
253 ::GetControlData( (ControlHandle) m_macControl , kControlNoPart , kControlListBoxListHandleTag ,
254 sizeof( ListHandle ) , (char*) &m_macList , &result ) ;
255
256 HLock( (Handle) m_macList ) ;
257 ldefHandle ldef ;
258 ldef = (ldefHandle) NewHandle( sizeof(ldefRec) ) ;
259 if ( (**(ListHandle)m_macList).listDefProc != NULL )
260 {
261 (**ldef).instruction = 0x4EF9; /* JMP instruction */
262 (**ldef).function = (void(*)()) listDef.u.userProc;
263 (**(ListHandle)m_macList).listDefProc = (Handle) ldef ;
264 }
265
266 Point pt = (**(ListHandle)m_macList).cellSize ;
267 pt.v = kwxMacListItemHeight ;
268 LCellSize( pt , (ListHandle)m_macList ) ;
269 LAddColumn( 1 , 0 , (ListHandle)m_macList ) ;
270 #endif
271 OptionBits options = 0;
272 if ( style & wxLB_MULTIPLE )
273 {
274 options += lNoExtend ;
275 }
276 else if ( style & wxLB_EXTENDED )
277 {
278 options += lExtendDrag ;
279 }
280 else
281 {
282 options = (OptionBits) lOnlyOne ;
283 }
284 SetListSelectionFlags((ListHandle)m_macList, options);
285
286 for ( int i = 0 ; i < n ; i++ )
287 {
288 Append( choices[i] ) ;
289 }
290
291 MacPostControlCreate() ;
292
293 LSetDrawingMode( true , (ListHandle)m_macList ) ;
294
295 return TRUE;
296 }
297
298 wxListBox::~wxListBox()
299 {
300 FreeData() ;
301 if ( m_macList )
302 {
303 #if !TARGET_CARBON
304 DisposeHandle( (**(ListHandle)m_macList).listDefProc ) ;
305 (**(ListHandle)m_macList).listDefProc = NULL ;
306 #endif
307 m_macList = NULL ;
308 }
309 }
310
311 void wxListBox::FreeData()
312 {
313 #if wxUSE_OWNER_DRAWN
314 if ( m_windowStyle & wxLB_OWNERDRAW )
315 {
316 size_t uiCount = m_aItems.Count();
317 while ( uiCount-- != 0 ) {
318 delete m_aItems[uiCount];
319 m_aItems[uiCount] = NULL;
320 }
321
322 m_aItems.Clear();
323 }
324 else
325 #endif // wxUSE_OWNER_DRAWN
326 if ( HasClientObjectData() )
327 {
328 for ( size_t n = 0; n < (size_t)m_noItems; n++ )
329 {
330 delete GetClientObject(n);
331 }
332 }
333 }
334
335 void wxListBox::DoSetSize(int x, int y,
336 int width, int height,
337 int sizeFlags )
338 {
339 wxControl::DoSetSize( x , y , width , height , sizeFlags ) ;
340 #if TARGET_CARBON
341 Rect bounds ;
342 GetControlBounds( (ControlHandle) m_macControl , &bounds ) ;
343 ControlRef control = GetListVerticalScrollBar( (ListHandle)m_macList ) ;
344 if ( control )
345 {
346 Rect scrollbounds ;
347 GetControlBounds( control , &scrollbounds ) ;
348 if( scrollbounds.right != bounds.right + 1 )
349 {
350 UMAMoveControl( control , bounds.right - (scrollbounds.right - scrollbounds.left) + 1 ,
351 scrollbounds.top ) ;
352 }
353 }
354 #endif
355 }
356 void wxListBox::DoSetFirstItem(int N)
357 {
358 MacScrollTo( N ) ;
359 }
360
361 void wxListBox::Delete(int N)
362 {
363 wxCHECK_RET( N >= 0 && N < m_noItems,
364 wxT("invalid index in wxListBox::Delete") );
365
366 #if wxUSE_OWNER_DRAWN
367 delete m_aItems[N];
368 m_aItems.RemoveAt(N);
369 #else // !wxUSE_OWNER_DRAWN
370 if ( HasClientObjectData() )
371 {
372 delete GetClientObject(N);
373 }
374 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
375 m_stringArray.RemoveAt( N ) ;
376 m_dataArray.RemoveAt( N ) ;
377 m_noItems --;
378
379 MacDelete( N ) ;
380 }
381
382 int wxListBox::DoAppend(const wxString& item)
383 {
384 int index = m_noItems ;
385 m_stringArray.Add( item ) ;
386 m_dataArray.Add( NULL );
387 m_noItems ++;
388 DoSetItemClientData( index , NULL ) ;
389 MacAppend( item ) ;
390
391 return index ;
392 }
393
394 void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
395 {
396 MacSetRedraw( false ) ;
397 Clear() ;
398 int n = choices.GetCount();
399
400 for( int i = 0 ; i < n ; ++i )
401 {
402 if ( clientData )
403 {
404 #if wxUSE_OWNER_DRAWN
405 wxASSERT_MSG(clientData[i] == NULL,
406 wxT("Can't use client data with owner-drawn listboxes"));
407 #else // !wxUSE_OWNER_DRAWN
408 Append( choices[i] , clientData[i] ) ;
409 #endif
410 }
411 else
412 Append( choices[i] ) ;
413 }
414
415 #if wxUSE_OWNER_DRAWN
416 if ( m_windowStyle & wxLB_OWNERDRAW ) {
417 // first delete old items
418 size_t ui = m_aItems.Count();
419 while ( ui-- != 0 ) {
420 delete m_aItems[ui];
421 m_aItems[ui] = NULL;
422 }
423 m_aItems.Empty();
424
425 // then create new ones
426 for ( ui = 0; ui < (size_t)m_noItems; ui++ ) {
427 wxOwnerDrawn *pNewItem = CreateItem(ui);
428 pNewItem->SetName(choices[ui]);
429 m_aItems.Add(pNewItem);
430 }
431 }
432 #endif // wxUSE_OWNER_DRAWN
433 MacSetRedraw( true ) ;
434 }
435
436 bool wxListBox::HasMultipleSelection() const
437 {
438 return (m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED);
439 }
440
441 int wxListBox::FindString(const wxString& s) const
442 {
443
444 if ( s.Right(1) == wxT("*") )
445 {
446 wxString search = s.Left( s.Length() - 1 ) ;
447 int len = search.Length() ;
448 Str255 s1 , s2 ;
449 wxMacStringToPascal( search , s2 ) ;
450
451 for ( int i = 0 ; i < m_noItems ; ++ i )
452 {
453 wxMacStringToPascal( m_stringArray[i].Left( len ) , s1 ) ;
454
455 if ( EqualString( s1 , s2 , false , false ) )
456 return i ;
457 }
458 if ( s.Left(1) == wxT("*") && s.Length() > 1 )
459 {
460 wxString st = s ;
461 st.MakeLower() ;
462 for ( int i = 0 ; i < m_noItems ; ++i )
463 {
464 if ( GetString(i).Lower().Matches(st) )
465 return i ;
466 }
467 }
468
469 }
470 else
471 {
472 Str255 s1 , s2 ;
473
474 wxMacStringToPascal( s , s2 ) ;
475
476 for ( int i = 0 ; i < m_noItems ; ++ i )
477 {
478 wxMacStringToPascal( m_stringArray[i] , s1 ) ;
479
480 if ( EqualString( s1 , s2 , false , false ) )
481 return i ;
482 }
483 }
484 return -1;
485 }
486
487 void wxListBox::Clear()
488 {
489 FreeData();
490 m_noItems = 0;
491 m_stringArray.Empty() ;
492 m_dataArray.Empty() ;
493 MacClear() ;
494 }
495
496 void wxListBox::SetSelection(int N, bool select)
497 {
498 wxCHECK_RET( N >= 0 && N < m_noItems,
499 wxT("invalid index in wxListBox::SetSelection") );
500 MacSetSelection( N , select ) ;
501 GetSelections( m_selectionPreImage ) ;
502 }
503
504 bool wxListBox::IsSelected(int N) const
505 {
506 wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE,
507 wxT("invalid index in wxListBox::Selected") );
508
509 return MacIsSelected( N ) ;
510 }
511
512 void *wxListBox::DoGetItemClientData(int N) const
513 {
514 wxCHECK_MSG( N >= 0 && N < m_noItems, NULL,
515 wxT("invalid index in wxListBox::GetClientData"));
516
517 return (void *)m_dataArray[N];
518 }
519
520 wxClientData *wxListBox::DoGetItemClientObject(int N) const
521 {
522 return (wxClientData *) DoGetItemClientData( N ) ;
523 }
524
525 void wxListBox::DoSetItemClientData(int N, void *Client_data)
526 {
527 wxCHECK_RET( N >= 0 && N < m_noItems,
528 wxT("invalid index in wxListBox::SetClientData") );
529
530 #if wxUSE_OWNER_DRAWN
531 if ( m_windowStyle & wxLB_OWNERDRAW )
532 {
533 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
534 // in OnMeasure/OnDraw.
535 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
536 }
537 #endif // wxUSE_OWNER_DRAWN
538 wxASSERT_MSG( m_dataArray.GetCount() >= (size_t) N , wxT("invalid client_data array") ) ;
539
540 if ( m_dataArray.GetCount() > (size_t) N )
541 {
542 m_dataArray[N] = (char*) Client_data ;
543 }
544 else
545 {
546 m_dataArray.Add( (char*) Client_data ) ;
547 }
548 }
549
550 void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
551 {
552 DoSetItemClientData(n, clientData);
553 }
554
555 // Return number of selections and an array of selected integers
556 int wxListBox::GetSelections(wxArrayInt& aSelections) const
557 {
558 return MacGetSelections( aSelections ) ;
559 }
560
561 // Get single selection, for single choice list items
562 int wxListBox::GetSelection() const
563 {
564 return MacGetSelection() ;
565 }
566
567 // Find string for position
568 wxString wxListBox::GetString(int N) const
569 {
570 return m_stringArray[N] ;
571 }
572
573 void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
574 {
575 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
576 wxT("invalid index in wxListBox::InsertItems") );
577
578 int nItems = items.GetCount();
579
580 for ( int i = 0 ; i < nItems ; i++ )
581 {
582 m_stringArray.Insert( items[i] , pos + i ) ;
583 m_dataArray.Insert( NULL , pos + i ) ;
584 MacInsert( pos + i , items[i] ) ;
585 }
586
587 m_noItems += nItems;
588 }
589
590 void wxListBox::SetString(int N, const wxString& s)
591 {
592 m_stringArray[N] = s ;
593 MacSet( N , s ) ;
594 }
595
596 wxSize wxListBox::DoGetBestSize() const
597 {
598 int lbWidth = 100; // some defaults
599 int lbHeight = 110;
600 int wLine;
601
602 {
603 wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetRootWindow() ) ) ;
604
605 wxFontRefData * font = (wxFontRefData*) m_font.GetRefData() ;
606
607 if ( font )
608 {
609 ::TextFont( font->m_macFontNum ) ;
610 ::TextSize( short(font->m_macFontSize) ) ;
611 ::TextFace( font->m_macFontStyle ) ;
612 }
613 else
614 {
615 ::TextFont( kFontIDMonaco ) ;
616 ::TextSize( 9 );
617 ::TextFace( 0 ) ;
618 }
619
620 // Find the widest line
621 for(int i = 0; i < GetCount(); i++) {
622 wxString str(GetString(i));
623 wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ;
624 lbWidth = wxMax(lbWidth, wLine);
625 }
626
627 // Add room for the scrollbar
628 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
629
630 // And just a bit more
631 int cy = 12 ;
632 int cx = ::TextWidth( "X" , 0 , 1 ) ;
633 lbWidth += cx ;
634
635 // don't make the listbox too tall (limit height to around 10 items) but don't
636 // make it too small neither
637 lbHeight = (cy+4) * wxMin(wxMax(GetCount(), 3), 10);
638 }
639 return wxSize(lbWidth, lbHeight);
640 }
641
642 int wxListBox::GetCount() const
643 {
644 return m_noItems;
645 }
646
647 void wxListBox::SetupColours()
648 {
649 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
650 SetForegroundColour(GetParent()->GetForegroundColour());
651 }
652
653 void wxListBox::Refresh(bool eraseBack, const wxRect *rect)
654 {
655 wxControl::Refresh( eraseBack , rect ) ;
656 // MacRedrawControl() ;
657 }
658
659 #if wxUSE_OWNER_DRAWN
660
661 class wxListBoxItem : public wxOwnerDrawn
662 {
663 public:
664 wxListBoxItem(const wxString& str = "");
665 };
666
667 wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE)
668 {
669 // no bitmaps/checkmarks
670 SetMarginWidth(0);
671 }
672
673 wxOwnerDrawn *wxListBox::CreateItem(size_t n)
674 {
675 return new wxListBoxItem();
676 }
677
678 #endif //USE_OWNER_DRAWN
679
680 // ============================================================================
681 // list box control implementation
682 // ============================================================================
683
684 /*
685 void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon)
686 {
687 wxListBox* list;
688 // typecast our refCon
689 list = (wxListBox*)refCon;
690
691 MoveTo(cellRect->left + 4 , cellRect->top + 10 );
692 const wxString text = list->m_stringArray[lCell.v] ;
693 ::TextFont( kFontIDMonaco ) ;
694 ::TextSize( 9 );
695 ::TextFace( 0 ) ;
696 DrawText(text, 0 , text.Length());
697
698 }
699 */
700 void wxListBox::MacDelete( int N )
701 {
702 LDelRow( 1 , N , (ListHandle)m_macList) ;
703 Refresh();
704 }
705
706 void wxListBox::MacInsert( int n , const wxString& text)
707 {
708 Cell cell = { 0 , 0 } ;
709 cell.v = n ;
710 LAddRow( 1 , cell.v , (ListHandle)m_macList ) ;
711 // LSetCell(text, strlen(text), cell, m_macList);
712 Refresh();
713 }
714
715 void wxListBox::MacAppend( const wxString& text)
716 {
717 Cell cell = { 0 , 0 } ;
718 cell.v = (**(ListHandle)m_macList).dataBounds.bottom ;
719 LAddRow( 1 , cell.v , (ListHandle)m_macList ) ;
720 // LSetCell(text, strlen(text), cell, m_macList);
721 Refresh();
722 }
723
724 void wxListBox::MacClear()
725 {
726 LDelRow( (**(ListHandle)m_macList).dataBounds.bottom , 0 ,(ListHandle) m_macList ) ;
727 Refresh();
728 }
729
730 void wxListBox::MacSetSelection( int n , bool select )
731 {
732 Cell cell = { 0 , 0 } ;
733 if ( ! (m_windowStyle & wxLB_MULTIPLE) )
734 {
735 if ( LGetSelect( true , &cell , (ListHandle)m_macList ) )
736 {
737 LSetSelect( false , cell , (ListHandle)m_macList ) ;
738 }
739 }
740
741 cell.v = n ;
742 LSetSelect( select , cell , (ListHandle)m_macList ) ;
743 LAutoScroll( (ListHandle)m_macList ) ;
744 Refresh();
745 }
746
747 bool wxListBox::MacIsSelected( int n ) const
748 {
749 Cell cell = { 0 , 0 } ;
750 cell.v = n ;
751 return LGetSelect( false , &cell , (ListHandle)m_macList ) ;
752 }
753
754 void wxListBox::MacDestroy()
755 {
756 // DisposeExtLDEFInfo( m_macList ) ;
757 }
758
759 int wxListBox::MacGetSelection() const
760 {
761 Cell cell = { 0 , 0 } ;
762 if ( LGetSelect( true , &cell , (ListHandle)m_macList ) )
763 return cell.v ;
764 else
765 return -1 ;
766 }
767
768 int wxListBox::MacGetSelections( wxArrayInt& aSelections ) const
769 {
770 int no_sel = 0 ;
771
772 aSelections.Empty();
773
774 Cell cell = { 0 , 0 } ;
775 cell.v = 0 ;
776
777 while ( LGetSelect( true , &cell ,(ListHandle) m_macList ) )
778 {
779 aSelections.Add( cell.v ) ;
780 no_sel++ ;
781 cell.v++ ;
782 }
783 return no_sel ;
784 }
785
786 void wxListBox::MacSet( int n , const wxString& text )
787 {
788 // our implementation does not store anything in the list
789 // so we just have to redraw
790 Cell cell = { 0 , 0 } ;
791 cell.v = n ;
792 // LSetCell(text, strlen(text), cell, m_macList);
793 Refresh();
794 }
795
796 void wxListBox::MacScrollTo( int n )
797 {
798 // TODO implement scrolling
799 }
800
801 void wxListBox::OnSize( const wxSizeEvent &event)
802 {
803 Point pt;
804
805 #if TARGET_CARBON
806 GetListCellSize((ListHandle)m_macList, &pt);
807 #else
808 pt = (**(ListHandle)m_macList).cellSize ;
809 #endif
810 pt.h = m_width - 15 ;
811 LCellSize( pt , (ListHandle)m_macList ) ;
812 }
813
814 void wxListBox::MacHandleControlClick( WXWidget control , wxInt16 controlpart )
815 {
816 Boolean wasDoubleClick = false ;
817 long result ;
818
819 ::GetControlData( (ControlHandle) m_macControl , kControlNoPart , kControlListBoxDoubleClickTag , sizeof( wasDoubleClick ) , (char*) &wasDoubleClick , &result ) ;
820 if ( !wasDoubleClick )
821 {
822 MacDoClick() ;
823 }
824 else
825 {
826 MacDoDoubleClick() ;
827 }
828 }
829
830 void wxListBox::MacSetRedraw( bool doDraw )
831 {
832 LSetDrawingMode( doDraw , (ListHandle)m_macList ) ;
833
834 }
835
836 void wxListBox::MacDoClick()
837 {
838 wxArrayInt aSelections;
839 int n ;
840 size_t count = GetSelections(aSelections);
841
842 if ( count == m_selectionPreImage.GetCount() )
843 {
844 bool hasChanged = false ;
845 for ( size_t i = 0 ; i < count ; ++i )
846 {
847 if ( aSelections[i] != m_selectionPreImage[i] )
848 {
849 hasChanged = true ;
850 break ;
851 }
852 }
853 if ( !hasChanged )
854 {
855 return ;
856 }
857 }
858
859 m_selectionPreImage = aSelections;
860
861 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
862 event.SetEventObject( this );
863
864 if ( count > 0 )
865 {
866 n = aSelections[0];
867 if ( HasClientObjectData() )
868 event.SetClientObject( GetClientObject(n) );
869 else if ( HasClientUntypedData() )
870 event.SetClientData( GetClientData(n) );
871 event.SetString( GetString(n) );
872 }
873 else
874 {
875 n = -1;
876 }
877
878 event.m_commandInt = n;
879
880 GetEventHandler()->ProcessEvent(event);
881 }
882
883 void wxListBox::MacDoDoubleClick()
884 {
885 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId);
886 event.SetEventObject( this );
887 GetEventHandler()->ProcessEvent(event) ;
888 }
889
890 void wxListBox::OnChar(wxKeyEvent& event)
891 {
892 if ( event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER)
893 {
894 wxWindow* parent = GetParent() ;
895 while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL )
896 parent = parent->GetParent() ;
897
898 if ( parent && parent->GetDefaultItem() )
899 {
900 wxButton *def = wxDynamicCast(parent->GetDefaultItem(),
901 wxButton);
902 if ( def && def->IsEnabled() )
903 {
904 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
905 event.SetEventObject(def);
906 def->Command(event);
907 return ;
908 }
909 }
910 event.Skip() ;
911 }
912 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
913 else if (event.GetKeyCode() == WXK_ESCAPE || (event.GetKeyCode() == '.' && event.MetaDown() ) )
914 {
915 wxWindow* win = GetParent()->FindWindow( wxID_CANCEL ) ;
916 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
917 new_event.SetEventObject( win );
918 win->GetEventHandler()->ProcessEvent( new_event );
919 }
920 else if ( event.GetKeyCode() == WXK_TAB )
921 {
922 wxNavigationKeyEvent new_event;
923 new_event.SetEventObject( this );
924 new_event.SetDirection( !event.ShiftDown() );
925 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
926 new_event.SetWindowChange( event.ControlDown() );
927 new_event.SetCurrentFocus( this );
928 if ( !GetEventHandler()->ProcessEvent( new_event ) )
929 event.Skip() ;
930 }
931 else if ( event.GetKeyCode() == WXK_DOWN || event.GetKeyCode() == WXK_UP )
932 {
933 // perform the default key handling first
934 wxControl::OnKeyDown( event ) ;
935
936 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
937 event.SetEventObject( this );
938
939 wxArrayInt aSelections;
940 int n, count = GetSelections(aSelections);
941 if ( count > 0 )
942 {
943 n = aSelections[0];
944 if ( HasClientObjectData() )
945 event.SetClientObject( GetClientObject(n) );
946 else if ( HasClientUntypedData() )
947 event.SetClientData( GetClientData(n) );
948 event.SetString( GetString(n) );
949 }
950 else
951 {
952 n = -1;
953 }
954
955 event.m_commandInt = n;
956
957 GetEventHandler()->ProcessEvent(event);
958 }
959 else
960 {
961 if ( event.GetTimestamp() > m_lastTypeIn + 60 )
962 {
963 m_typeIn = wxEmptyString ;
964 }
965 m_lastTypeIn = event.GetTimestamp() ;
966 m_typeIn += (char) event.GetKeyCode() ;
967 int line = FindString(wxT("*")+m_typeIn+wxT("*")) ;
968 if ( line >= 0 )
969 {
970 if ( GetSelection() != line )
971 {
972 SetSelection(line) ;
973 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
974 event.SetEventObject( this );
975
976 if ( HasClientObjectData() )
977 event.SetClientObject( GetClientObject( line ) );
978 else if ( HasClientUntypedData() )
979 event.SetClientData( GetClientData(line) );
980 event.SetString( GetString(line) );
981
982 event.m_commandInt = line ;
983
984 GetEventHandler()->ProcessEvent(event);
985 }
986 }
987 }
988 }
989