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