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