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