]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/listbox.cpp
fix for preserving the clip rgn (control redraws missing after switch to faster redra...
[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 = linetext.mb_str( wxConvLocal) ;
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 if ( !wxListBoxBase::Create(parent, id, pos, size, style & ~(wxHSCROLL|wxVSCROLL), validator, name) )
206 return false;
207
208 m_noItems = 0 ; // this will be increased by our append command
209 m_selected = 0;
210
211 Rect bounds ;
212 Str255 title ;
213
214 MacPreControlCreate( parent , id , wxEmptyString , pos , size ,style, validator , name , &bounds , title ) ;
215
216 ListDefSpec listDef;
217 listDef.defType = kListDefUserProcType;
218 if ( macListDefUPP == NULL )
219 {
220 macListDefUPP = NewListDefUPP( wxMacListDefinition );
221 }
222 listDef.u.userProc = macListDefUPP ;
223
224 Str255 fontName ;
225 SInt16 fontSize ;
226 Style fontStyle ;
227 #if TARGET_CARBON
228 GetThemeFont(kThemeViewsFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ;
229 #else
230 GetFontName( kFontIDMonaco , fontName ) ;
231 fontSize = 9 ;
232 fontStyle = normal ;
233 #endif
234 SetFont( wxFont (fontSize, wxSWISS, wxNORMAL, wxNORMAL , false , wxMacMakeStringFromPascal( fontName ) ) ) ;
235 #if TARGET_CARBON
236 Size asize;
237
238
239 CreateListBoxControl( MAC_WXHWND(parent->MacGetRootWindow()), &bounds, false, 0, 1, (style & wxLB_HSCROLL), true,
240 kwxMacListItemHeight, kwxMacListItemHeight, false, &listDef, (ControlRef *)&m_macControl );
241
242 GetControlData( (ControlHandle) m_macControl, kControlNoPart, kControlListBoxListHandleTag,
243 sizeof(ListHandle), (Ptr) &m_macList, &asize);
244
245 SetControlReference( (ControlHandle) m_macControl, (long) this);
246 SetControlVisibility( (ControlHandle) m_macControl, false, false);
247
248 #else
249
250 long result ;
251 wxStAppResource resload ;
252 m_macControl = ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , title , false ,
253 (style & wxLB_HSCROLL) ? kwxMacListWithVerticalAndHorizontalScrollbar : kwxMacListWithVerticalScrollbar ,
254 0 , 0, kControlListBoxProc , (long) this ) ;
255 ::GetControlData( (ControlHandle) m_macControl , kControlNoPart , kControlListBoxListHandleTag ,
256 sizeof( ListHandle ) , (char*) &m_macList , &result ) ;
257
258 HLock( (Handle) m_macList ) ;
259 ldefHandle ldef ;
260 ldef = (ldefHandle) NewHandle( sizeof(ldefRec) ) ;
261 if ( (**(ListHandle)m_macList).listDefProc != NULL )
262 {
263 (**ldef).instruction = 0x4EF9; /* JMP instruction */
264 (**ldef).function = (void(*)()) listDef.u.userProc;
265 (**(ListHandle)m_macList).listDefProc = (Handle) ldef ;
266 }
267
268 Point pt = (**(ListHandle)m_macList).cellSize ;
269 pt.v = kwxMacListItemHeight ;
270 LCellSize( pt , (ListHandle)m_macList ) ;
271 LAddColumn( 1 , 0 , (ListHandle)m_macList ) ;
272 #endif
273 OptionBits options = 0;
274 if ( style & wxLB_MULTIPLE )
275 {
276 options += lNoExtend ;
277 }
278 else if ( style & wxLB_EXTENDED )
279 {
280 options += lExtendDrag ;
281 }
282 else
283 {
284 options = (OptionBits) lOnlyOne ;
285 }
286 SetListSelectionFlags((ListHandle)m_macList, options);
287
288 for ( int i = 0 ; i < n ; i++ )
289 {
290 Append( choices[i] ) ;
291 }
292
293 MacPostControlCreate() ;
294
295 LSetDrawingMode( true , (ListHandle)m_macList ) ;
296
297 return TRUE;
298 }
299
300 wxListBox::~wxListBox()
301 {
302 FreeData() ;
303 if ( m_macList )
304 {
305 #if !TARGET_CARBON
306 DisposeHandle( (**(ListHandle)m_macList).listDefProc ) ;
307 (**(ListHandle)m_macList).listDefProc = NULL ;
308 #endif
309 m_macList = NULL ;
310 }
311 }
312
313 void wxListBox::FreeData()
314 {
315 #if wxUSE_OWNER_DRAWN
316 if ( m_windowStyle & wxLB_OWNERDRAW )
317 {
318 size_t uiCount = m_aItems.Count();
319 while ( uiCount-- != 0 ) {
320 delete m_aItems[uiCount];
321 m_aItems[uiCount] = NULL;
322 }
323
324 m_aItems.Clear();
325 }
326 else
327 #endif // wxUSE_OWNER_DRAWN
328 if ( HasClientObjectData() )
329 {
330 for ( size_t n = 0; n < (size_t)m_noItems; n++ )
331 {
332 delete GetClientObject(n);
333 }
334 }
335 }
336
337 void wxListBox::DoSetSize(int x, int y,
338 int width, int height,
339 int sizeFlags )
340 {
341 wxControl::DoSetSize( x , y , width , height , sizeFlags ) ;
342 #if TARGET_CARBON
343 Rect bounds ;
344 GetControlBounds( (ControlHandle) m_macControl , &bounds ) ;
345 ControlRef control = GetListVerticalScrollBar( (ListHandle)m_macList ) ;
346 if ( control )
347 {
348 Rect scrollbounds ;
349 GetControlBounds( control , &scrollbounds ) ;
350 if( scrollbounds.right != bounds.right + 1 )
351 {
352 UMAMoveControl( control , bounds.right - (scrollbounds.right - scrollbounds.left) + 1 ,
353 scrollbounds.top ) ;
354 }
355 }
356 #endif
357 }
358 void wxListBox::DoSetFirstItem(int N)
359 {
360 MacScrollTo( N ) ;
361 }
362
363 void wxListBox::Delete(int N)
364 {
365 wxCHECK_RET( N >= 0 && N < m_noItems,
366 wxT("invalid index in wxListBox::Delete") );
367
368 #if wxUSE_OWNER_DRAWN
369 delete m_aItems[N];
370 m_aItems.RemoveAt(N);
371 #else // !wxUSE_OWNER_DRAWN
372 if ( HasClientObjectData() )
373 {
374 delete GetClientObject(N);
375 }
376 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
377 m_stringArray.RemoveAt( N ) ;
378 m_dataArray.RemoveAt( N ) ;
379 m_noItems --;
380
381 MacDelete( N ) ;
382 }
383
384 int wxListBox::DoAppend(const wxString& item)
385 {
386 int index = m_noItems ;
387 m_stringArray.Add( item ) ;
388 m_dataArray.Add( NULL );
389 m_noItems ++;
390 DoSetItemClientData( index , NULL ) ;
391 MacAppend( item ) ;
392
393 return index ;
394 }
395
396 void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
397 {
398 MacSetRedraw( false ) ;
399 Clear() ;
400 int n = choices.GetCount();
401
402 for( int i = 0 ; i < n ; ++i )
403 {
404 if ( clientData )
405 {
406 #if wxUSE_OWNER_DRAWN
407 wxASSERT_MSG(clientData[i] == NULL,
408 wxT("Can't use client data with owner-drawn listboxes"));
409 #else // !wxUSE_OWNER_DRAWN
410 Append( choices[i] , clientData[i] ) ;
411 #endif
412 }
413 else
414 Append( choices[i] ) ;
415 }
416
417 #if wxUSE_OWNER_DRAWN
418 if ( m_windowStyle & wxLB_OWNERDRAW ) {
419 // first delete old items
420 size_t ui = m_aItems.Count();
421 while ( ui-- != 0 ) {
422 delete m_aItems[ui];
423 m_aItems[ui] = NULL;
424 }
425 m_aItems.Empty();
426
427 // then create new ones
428 for ( ui = 0; ui < (size_t)m_noItems; ui++ ) {
429 wxOwnerDrawn *pNewItem = CreateItem(ui);
430 pNewItem->SetName(choices[ui]);
431 m_aItems.Add(pNewItem);
432 }
433 }
434 #endif // wxUSE_OWNER_DRAWN
435 MacSetRedraw( true ) ;
436 }
437
438 bool wxListBox::HasMultipleSelection() const
439 {
440 return (m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED);
441 }
442
443 int wxListBox::FindString(const wxString& s) const
444 {
445
446 if ( s.Right(1) == wxT("*") )
447 {
448 wxString search = s.Left( s.Length() - 1 ) ;
449 int len = search.Length() ;
450 Str255 s1 , s2 ;
451 wxMacStringToPascal( search , s2 ) ;
452
453 for ( int i = 0 ; i < m_noItems ; ++ i )
454 {
455 wxMacStringToPascal( m_stringArray[i].Left( len ) , s1 ) ;
456
457 if ( EqualString( s1 , s2 , false , false ) )
458 return i ;
459 }
460 if ( s.Left(1) == wxT("*") && s.Length() > 1 )
461 {
462 wxString st = s ;
463 st.MakeLower() ;
464 for ( int i = 0 ; i < m_noItems ; ++i )
465 {
466 if ( GetString(i).Lower().Matches(st) )
467 return i ;
468 }
469 }
470
471 }
472 else
473 {
474 Str255 s1 , s2 ;
475
476 wxMacStringToPascal( s , s2 ) ;
477
478 for ( int i = 0 ; i < m_noItems ; ++ i )
479 {
480 wxMacStringToPascal( m_stringArray[i] , s1 ) ;
481
482 if ( EqualString( s1 , s2 , false , false ) )
483 return i ;
484 }
485 }
486 return -1;
487 }
488
489 void wxListBox::Clear()
490 {
491 FreeData();
492 m_noItems = 0;
493 m_stringArray.Empty() ;
494 m_dataArray.Empty() ;
495 MacClear() ;
496 }
497
498 void wxListBox::SetSelection(int N, bool select)
499 {
500 wxCHECK_RET( N >= 0 && N < m_noItems,
501 wxT("invalid index in wxListBox::SetSelection") );
502 MacSetSelection( N , select ) ;
503 GetSelections( m_selectionPreImage ) ;
504 }
505
506 bool wxListBox::IsSelected(int N) const
507 {
508 wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE,
509 wxT("invalid index in wxListBox::Selected") );
510
511 return MacIsSelected( N ) ;
512 }
513
514 void *wxListBox::DoGetItemClientData(int N) const
515 {
516 wxCHECK_MSG( N >= 0 && N < m_noItems, NULL,
517 wxT("invalid index in wxListBox::GetClientData"));
518
519 return (void *)m_dataArray[N];
520 }
521
522 wxClientData *wxListBox::DoGetItemClientObject(int N) const
523 {
524 return (wxClientData *) DoGetItemClientData( N ) ;
525 }
526
527 void wxListBox::DoSetItemClientData(int N, void *Client_data)
528 {
529 wxCHECK_RET( N >= 0 && N < m_noItems,
530 wxT("invalid index in wxListBox::SetClientData") );
531
532 #if wxUSE_OWNER_DRAWN
533 if ( m_windowStyle & wxLB_OWNERDRAW )
534 {
535 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
536 // in OnMeasure/OnDraw.
537 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
538 }
539 #endif // wxUSE_OWNER_DRAWN
540 wxASSERT_MSG( m_dataArray.GetCount() >= (size_t) N , wxT("invalid client_data array") ) ;
541
542 if ( m_dataArray.GetCount() > (size_t) N )
543 {
544 m_dataArray[N] = (char*) Client_data ;
545 }
546 else
547 {
548 m_dataArray.Add( (char*) Client_data ) ;
549 }
550 }
551
552 void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
553 {
554 DoSetItemClientData(n, clientData);
555 }
556
557 // Return number of selections and an array of selected integers
558 int wxListBox::GetSelections(wxArrayInt& aSelections) const
559 {
560 return MacGetSelections( aSelections ) ;
561 }
562
563 // Get single selection, for single choice list items
564 int wxListBox::GetSelection() const
565 {
566 return MacGetSelection() ;
567 }
568
569 // Find string for position
570 wxString wxListBox::GetString(int N) const
571 {
572 return m_stringArray[N] ;
573 }
574
575 void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
576 {
577 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
578 wxT("invalid index in wxListBox::InsertItems") );
579
580 int nItems = items.GetCount();
581
582 for ( int i = 0 ; i < nItems ; i++ )
583 {
584 m_stringArray.Insert( items[i] , pos + i ) ;
585 m_dataArray.Insert( NULL , pos + i ) ;
586 MacInsert( pos + i , items[i] ) ;
587 }
588
589 m_noItems += nItems;
590 }
591
592 void wxListBox::SetString(int N, const wxString& s)
593 {
594 m_stringArray[N] = s ;
595 MacSet( N , s ) ;
596 }
597
598 wxSize wxListBox::DoGetBestSize() const
599 {
600 int lbWidth = 100; // some defaults
601 int lbHeight = 110;
602 int wLine;
603
604 {
605 wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetRootWindow() ) ) ;
606
607 wxFontRefData * font = (wxFontRefData*) m_font.GetRefData() ;
608
609 if ( font )
610 {
611 ::TextFont( font->m_macFontNum ) ;
612 ::TextSize( short(font->m_macFontSize) ) ;
613 ::TextFace( font->m_macFontStyle ) ;
614 }
615 else
616 {
617 ::TextFont( kFontIDMonaco ) ;
618 ::TextSize( 9 );
619 ::TextFace( 0 ) ;
620 }
621
622 // Find the widest line
623 for(int i = 0; i < GetCount(); i++) {
624 wxString str(GetString(i));
625 #if wxUSE_UNICODE
626 Point bounds={0,0} ;
627 SInt16 baseline ;
628 ::GetThemeTextDimensions( wxMacCFStringHolder( str ) ,
629 kThemeCurrentPortFont,
630 kThemeStateActive,
631 false,
632 &bounds,
633 &baseline );
634 wLine = bounds.h ;
635 #else
636 wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ;
637 #endif
638 lbWidth = wxMax(lbWidth, wLine);
639 }
640
641 // Add room for the scrollbar
642 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
643
644 // And just a bit more
645 int cy = 12 ;
646 int cx = ::TextWidth( "X" , 0 , 1 ) ;
647 lbWidth += cx ;
648
649 // don't make the listbox too tall (limit height to around 10 items) but don't
650 // make it too small neither
651 lbHeight = (cy+4) * wxMin(wxMax(GetCount(), 3), 10);
652 }
653 return wxSize(lbWidth, lbHeight);
654 }
655
656 int wxListBox::GetCount() const
657 {
658 return m_noItems;
659 }
660
661 void wxListBox::SetupColours()
662 {
663 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
664 SetForegroundColour(GetParent()->GetForegroundColour());
665 }
666
667 void wxListBox::Refresh(bool eraseBack, const wxRect *rect)
668 {
669 wxControl::Refresh( eraseBack , rect ) ;
670 // MacRedrawControl() ;
671 }
672
673 #if wxUSE_OWNER_DRAWN
674
675 class wxListBoxItem : public wxOwnerDrawn
676 {
677 public:
678 wxListBoxItem(const wxString& str = "");
679 };
680
681 wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE)
682 {
683 // no bitmaps/checkmarks
684 SetMarginWidth(0);
685 }
686
687 wxOwnerDrawn *wxListBox::CreateItem(size_t n)
688 {
689 return new wxListBoxItem();
690 }
691
692 #endif //USE_OWNER_DRAWN
693
694 // ============================================================================
695 // list box control implementation
696 // ============================================================================
697
698 /*
699 void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon)
700 {
701 wxListBox* list;
702 // typecast our refCon
703 list = (wxListBox*)refCon;
704
705 MoveTo(cellRect->left + 4 , cellRect->top + 10 );
706 const wxString text = list->m_stringArray[lCell.v] ;
707 ::TextFont( kFontIDMonaco ) ;
708 ::TextSize( 9 );
709 ::TextFace( 0 ) ;
710 DrawText(text, 0 , text.Length());
711
712 }
713 */
714 void wxListBox::MacDelete( int N )
715 {
716 LDelRow( 1 , N , (ListHandle)m_macList) ;
717 Refresh();
718 }
719
720 void wxListBox::MacInsert( int n , const wxString& text)
721 {
722 Cell cell = { 0 , 0 } ;
723 cell.v = n ;
724 LAddRow( 1 , cell.v , (ListHandle)m_macList ) ;
725 // LSetCell(text, strlen(text), cell, m_macList);
726 Refresh();
727 }
728
729 void wxListBox::MacAppend( const wxString& text)
730 {
731 Cell cell = { 0 , 0 } ;
732 cell.v = (**(ListHandle)m_macList).dataBounds.bottom ;
733 LAddRow( 1 , cell.v , (ListHandle)m_macList ) ;
734 // LSetCell(text, strlen(text), cell, m_macList);
735 Refresh();
736 }
737
738 void wxListBox::MacClear()
739 {
740 LDelRow( (**(ListHandle)m_macList).dataBounds.bottom , 0 ,(ListHandle) m_macList ) ;
741 Refresh();
742 }
743
744 void wxListBox::MacSetSelection( int n , bool select )
745 {
746 Cell cell = { 0 , 0 } ;
747 if ( ! (m_windowStyle & wxLB_MULTIPLE) )
748 {
749 if ( LGetSelect( true , &cell , (ListHandle)m_macList ) )
750 {
751 LSetSelect( false , cell , (ListHandle)m_macList ) ;
752 }
753 }
754
755 cell.v = n ;
756 LSetSelect( select , cell , (ListHandle)m_macList ) ;
757 LAutoScroll( (ListHandle)m_macList ) ;
758 Refresh();
759 }
760
761 bool wxListBox::MacIsSelected( int n ) const
762 {
763 Cell cell = { 0 , 0 } ;
764 cell.v = n ;
765 return LGetSelect( false , &cell , (ListHandle)m_macList ) ;
766 }
767
768 void wxListBox::MacDestroy()
769 {
770 // DisposeExtLDEFInfo( m_macList ) ;
771 }
772
773 int wxListBox::MacGetSelection() const
774 {
775 Cell cell = { 0 , 0 } ;
776 if ( LGetSelect( true , &cell , (ListHandle)m_macList ) )
777 return cell.v ;
778 else
779 return -1 ;
780 }
781
782 int wxListBox::MacGetSelections( wxArrayInt& aSelections ) const
783 {
784 int no_sel = 0 ;
785
786 aSelections.Empty();
787
788 Cell cell = { 0 , 0 } ;
789 cell.v = 0 ;
790
791 while ( LGetSelect( true , &cell ,(ListHandle) m_macList ) )
792 {
793 aSelections.Add( cell.v ) ;
794 no_sel++ ;
795 cell.v++ ;
796 }
797 return no_sel ;
798 }
799
800 void wxListBox::MacSet( int n , const wxString& text )
801 {
802 // our implementation does not store anything in the list
803 // so we just have to redraw
804 Cell cell = { 0 , 0 } ;
805 cell.v = n ;
806 // LSetCell(text, strlen(text), cell, m_macList);
807 Refresh();
808 }
809
810 void wxListBox::MacScrollTo( int n )
811 {
812 // TODO implement scrolling
813 }
814
815 void wxListBox::OnSize( const wxSizeEvent &event)
816 {
817 Point pt;
818
819 #if TARGET_CARBON
820 GetListCellSize((ListHandle)m_macList, &pt);
821 #else
822 pt = (**(ListHandle)m_macList).cellSize ;
823 #endif
824 pt.h = m_width - 15 ;
825 LCellSize( pt , (ListHandle)m_macList ) ;
826 }
827
828 void wxListBox::MacHandleControlClick( WXWidget control , wxInt16 controlpart , bool WXUNUSED(mouseStillDown))
829 {
830 Boolean wasDoubleClick = false ;
831 long result ;
832
833 ::GetControlData( (ControlHandle) m_macControl , kControlNoPart , kControlListBoxDoubleClickTag , sizeof( wasDoubleClick ) , (char*) &wasDoubleClick , &result ) ;
834 if ( !wasDoubleClick )
835 {
836 MacDoClick() ;
837 }
838 else
839 {
840 MacDoDoubleClick() ;
841 }
842 }
843
844 void wxListBox::MacSetRedraw( bool doDraw )
845 {
846 LSetDrawingMode( doDraw , (ListHandle)m_macList ) ;
847
848 }
849
850 void wxListBox::MacDoClick()
851 {
852 wxArrayInt aSelections;
853 int n ;
854 size_t count = GetSelections(aSelections);
855
856 if ( count == m_selectionPreImage.GetCount() )
857 {
858 bool hasChanged = false ;
859 for ( size_t i = 0 ; i < count ; ++i )
860 {
861 if ( aSelections[i] != m_selectionPreImage[i] )
862 {
863 hasChanged = true ;
864 break ;
865 }
866 }
867 if ( !hasChanged )
868 {
869 return ;
870 }
871 }
872
873 m_selectionPreImage = aSelections;
874
875 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
876 event.SetEventObject( this );
877
878 if ( count > 0 )
879 {
880 n = aSelections[0];
881 if ( HasClientObjectData() )
882 event.SetClientObject( GetClientObject(n) );
883 else if ( HasClientUntypedData() )
884 event.SetClientData( GetClientData(n) );
885 event.SetString( GetString(n) );
886 }
887 else
888 {
889 n = -1;
890 }
891
892 event.m_commandInt = n;
893
894 GetEventHandler()->ProcessEvent(event);
895 }
896
897 void wxListBox::MacDoDoubleClick()
898 {
899 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId);
900 event.SetEventObject( this );
901 GetEventHandler()->ProcessEvent(event) ;
902 }
903
904 void wxListBox::OnChar(wxKeyEvent& event)
905 {
906 if ( event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER)
907 {
908 wxWindow* parent = GetParent() ;
909 while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL )
910 parent = parent->GetParent() ;
911
912 if ( parent && parent->GetDefaultItem() )
913 {
914 wxButton *def = wxDynamicCast(parent->GetDefaultItem(),
915 wxButton);
916 if ( def && def->IsEnabled() )
917 {
918 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
919 event.SetEventObject(def);
920 def->Command(event);
921 return ;
922 }
923 }
924 event.Skip() ;
925 }
926 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
927 else if (event.GetKeyCode() == WXK_ESCAPE || (event.GetKeyCode() == '.' && event.MetaDown() ) )
928 {
929 // FIXME: look in ancestors, not just parent.
930 wxWindow* win = GetParent()->FindWindow( wxID_CANCEL ) ;
931 if (win)
932 {
933 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
934 new_event.SetEventObject( win );
935 win->GetEventHandler()->ProcessEvent( new_event );
936 }
937 }
938 else if ( event.GetKeyCode() == WXK_TAB )
939 {
940 wxNavigationKeyEvent new_event;
941 new_event.SetEventObject( this );
942 new_event.SetDirection( !event.ShiftDown() );
943 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
944 new_event.SetWindowChange( event.ControlDown() );
945 new_event.SetCurrentFocus( this );
946 if ( !GetEventHandler()->ProcessEvent( new_event ) )
947 event.Skip() ;
948 }
949 else if ( event.GetKeyCode() == WXK_DOWN || event.GetKeyCode() == WXK_UP )
950 {
951 // perform the default key handling first
952 wxControl::OnKeyDown( event ) ;
953
954 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
955 event.SetEventObject( this );
956
957 wxArrayInt aSelections;
958 int n, count = GetSelections(aSelections);
959 if ( count > 0 )
960 {
961 n = aSelections[0];
962 if ( HasClientObjectData() )
963 event.SetClientObject( GetClientObject(n) );
964 else if ( HasClientUntypedData() )
965 event.SetClientData( GetClientData(n) );
966 event.SetString( GetString(n) );
967 }
968 else
969 {
970 n = -1;
971 }
972
973 event.m_commandInt = n;
974
975 GetEventHandler()->ProcessEvent(event);
976 }
977 else
978 {
979 if ( event.GetTimestamp() > m_lastTypeIn + 60 )
980 {
981 m_typeIn = wxEmptyString ;
982 }
983 m_lastTypeIn = event.GetTimestamp() ;
984 m_typeIn += (char) event.GetKeyCode() ;
985 int line = FindString(wxT("*")+m_typeIn+wxT("*")) ;
986 if ( line >= 0 )
987 {
988 if ( GetSelection() != line )
989 {
990 SetSelection(line) ;
991 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
992 event.SetEventObject( this );
993
994 if ( HasClientObjectData() )
995 event.SetClientObject( GetClientObject( line ) );
996 else if ( HasClientUntypedData() )
997 event.SetClientData( GetClientData(line) );
998 event.SetString( GetString(line) );
999
1000 event.m_commandInt = line ;
1001
1002 GetEventHandler()->ProcessEvent(event);
1003 }
1004 }
1005 }
1006 }
1007