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