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