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