]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/listbox.cpp
moved drawing to isvisible branch only
[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 {
939fba6c 140 wxCharBuffer text = linetext.mb_str( wxConvLocal) ;
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{
b657d5db 205 if ( !wxListBoxBase::Create(parent, id, pos, size, style & ~(wxHSCROLL|wxVSCROLL), validator, name) )
b45ed7a2
VZ
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
939fba6c 636 wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ;
2c1a3312 637 #endif
e40298d5
JS
638 lbWidth = wxMax(lbWidth, wLine);
639 }
640
641 // Add room for the scrollbar
642 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
643
644 // And just a bit more
645 int cy = 12 ;
646 int cx = ::TextWidth( "X" , 0 , 1 ) ;
647 lbWidth += cx ;
648
649 // don't make the listbox too tall (limit height to around 10 items) but don't
650 // make it too small neither
651 lbHeight = (cy+4) * wxMin(wxMax(GetCount(), 3), 10);
652 }
2b5f62a0 653 return wxSize(lbWidth, lbHeight);
e9576ca5
SC
654}
655
51abe921
SC
656int wxListBox::GetCount() const
657{
658 return m_noItems;
659}
660
661void wxListBox::SetupColours()
662{
a756f210 663 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
51abe921
SC
664 SetForegroundColour(GetParent()->GetForegroundColour());
665}
666
60149370
GD
667void wxListBox::Refresh(bool eraseBack, const wxRect *rect)
668{
de043984 669 wxControl::Refresh( eraseBack , rect ) ;
e40298d5 670 // MacRedrawControl() ;
60149370
GD
671}
672
51abe921
SC
673#if wxUSE_OWNER_DRAWN
674
675class wxListBoxItem : public wxOwnerDrawn
676{
677public:
678 wxListBoxItem(const wxString& str = "");
679};
680
681wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE)
682{
683 // no bitmaps/checkmarks
684 SetMarginWidth(0);
685}
686
687wxOwnerDrawn *wxListBox::CreateItem(size_t n)
688{
689 return new wxListBoxItem();
690}
691
692#endif //USE_OWNER_DRAWN
e9576ca5 693
519cb848
SC
694// ============================================================================
695// list box control implementation
696// ============================================================================
697
2b5f62a0 698/*
519cb848
SC
699void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon)
700{
e40298d5
JS
701wxListBox* list;
702// typecast our refCon
703list = (wxListBox*)refCon;
704
705 MoveTo(cellRect->left + 4 , cellRect->top + 10 );
706 const wxString text = list->m_stringArray[lCell.v] ;
707 ::TextFont( kFontIDMonaco ) ;
708 ::TextSize( 9 );
709 ::TextFace( 0 ) ;
710 DrawText(text, 0 , text.Length());
711
712 }
2b5f62a0 713*/
519cb848
SC
714void wxListBox::MacDelete( int N )
715{
76a5e5d2 716 LDelRow( 1 , N , (ListHandle)m_macList) ;
60149370 717 Refresh();
519cb848
SC
718}
719
427ff662 720void wxListBox::MacInsert( int n , const wxString& text)
519cb848 721{
60149370
GD
722 Cell cell = { 0 , 0 } ;
723 cell.v = n ;
76a5e5d2 724 LAddRow( 1 , cell.v , (ListHandle)m_macList ) ;
e40298d5 725 // LSetCell(text, strlen(text), cell, m_macList);
60149370 726 Refresh();
519cb848
SC
727}
728
427ff662 729void wxListBox::MacAppend( const wxString& text)
519cb848 730{
60149370 731 Cell cell = { 0 , 0 } ;
76a5e5d2
SC
732 cell.v = (**(ListHandle)m_macList).dataBounds.bottom ;
733 LAddRow( 1 , cell.v , (ListHandle)m_macList ) ;
e40298d5 734 // LSetCell(text, strlen(text), cell, m_macList);
60149370 735 Refresh();
519cb848
SC
736}
737
dc0ace7c 738void wxListBox::MacClear()
519cb848 739{
76a5e5d2 740 LDelRow( (**(ListHandle)m_macList).dataBounds.bottom , 0 ,(ListHandle) m_macList ) ;
60149370 741 Refresh();
519cb848
SC
742}
743
744void wxListBox::MacSetSelection( int n , bool select )
745{
a8e6bf8a
RR
746 Cell cell = { 0 , 0 } ;
747 if ( ! (m_windowStyle & wxLB_MULTIPLE) )
748 {
9f081f02
GD
749 if ( LGetSelect( true , &cell , (ListHandle)m_macList ) )
750 {
751 LSetSelect( false , cell , (ListHandle)m_macList ) ;
752 }
a8e6bf8a 753 }
e40298d5 754
a8e6bf8a 755 cell.v = n ;
76a5e5d2
SC
756 LSetSelect( select , cell , (ListHandle)m_macList ) ;
757 LAutoScroll( (ListHandle)m_macList ) ;
a8e6bf8a 758 Refresh();
519cb848
SC
759}
760
761bool wxListBox::MacIsSelected( int n ) const
762{
a8e6bf8a
RR
763 Cell cell = { 0 , 0 } ;
764 cell.v = n ;
76a5e5d2 765 return LGetSelect( false , &cell , (ListHandle)m_macList ) ;
519cb848
SC
766}
767
768void wxListBox::MacDestroy()
769{
e40298d5 770 // DisposeExtLDEFInfo( m_macList ) ;
519cb848
SC
771}
772
773int wxListBox::MacGetSelection() const
774{
a8e6bf8a 775 Cell cell = { 0 , 0 } ;
76a5e5d2 776 if ( LGetSelect( true , &cell , (ListHandle)m_macList ) )
a8e6bf8a
RR
777 return cell.v ;
778 else
779 return -1 ;
519cb848
SC
780}
781
782int wxListBox::MacGetSelections( wxArrayInt& aSelections ) const
783{
a8e6bf8a 784 int no_sel = 0 ;
e40298d5 785
519cb848 786 aSelections.Empty();
e40298d5 787
a8e6bf8a
RR
788 Cell cell = { 0 , 0 } ;
789 cell.v = 0 ;
e40298d5 790
76a5e5d2 791 while ( LGetSelect( true , &cell ,(ListHandle) m_macList ) )
a8e6bf8a
RR
792 {
793 aSelections.Add( cell.v ) ;
794 no_sel++ ;
795 cell.v++ ;
796 }
797 return no_sel ;
519cb848
SC
798}
799
427ff662 800void wxListBox::MacSet( int n , const wxString& text )
519cb848 801{
a8e6bf8a
RR
802 // our implementation does not store anything in the list
803 // so we just have to redraw
804 Cell cell = { 0 , 0 } ;
805 cell.v = n ;
e40298d5 806 // LSetCell(text, strlen(text), cell, m_macList);
a8e6bf8a 807 Refresh();
519cb848
SC
808}
809
810void wxListBox::MacScrollTo( int n )
811{
a8e6bf8a 812 // TODO implement scrolling
519cb848
SC
813}
814
815void wxListBox::OnSize( const wxSizeEvent &event)
816{
60149370 817 Point pt;
e40298d5 818
60149370 819#if TARGET_CARBON
962cbf2e 820 GetListCellSize((ListHandle)m_macList, &pt);
60149370 821#else
76a5e5d2 822 pt = (**(ListHandle)m_macList).cellSize ;
60149370
GD
823#endif
824 pt.h = m_width - 15 ;
76a5e5d2 825 LCellSize( pt , (ListHandle)m_macList ) ;
519cb848
SC
826}
827
4b26b60f 828void wxListBox::MacHandleControlClick( WXWidget control , wxInt16 controlpart , bool WXUNUSED(mouseStillDown))
519cb848 829{
a8e6bf8a
RR
830 Boolean wasDoubleClick = false ;
831 long result ;
e40298d5 832
76a5e5d2 833 ::GetControlData( (ControlHandle) m_macControl , kControlNoPart , kControlListBoxDoubleClickTag , sizeof( wasDoubleClick ) , (char*) &wasDoubleClick , &result ) ;
a8e6bf8a
RR
834 if ( !wasDoubleClick )
835 {
836 MacDoClick() ;
837 }
838 else
839 {
840 MacDoDoubleClick() ;
841 }
519cb848
SC
842}
843
dc0ace7c 844void wxListBox::MacSetRedraw( bool doDraw )
519cb848 845{
76a5e5d2 846 LSetDrawingMode( doDraw , (ListHandle)m_macList ) ;
e40298d5 847
519cb848
SC
848}
849
850void wxListBox::MacDoClick()
851{
a8e6bf8a 852 wxArrayInt aSelections;
68a9d9d0
SC
853 int n ;
854 size_t count = GetSelections(aSelections);
e40298d5 855
a8e6bf8a
RR
856 if ( count == m_selectionPreImage.GetCount() )
857 {
858 bool hasChanged = false ;
68a9d9d0 859 for ( size_t i = 0 ; i < count ; ++i )
a8e6bf8a
RR
860 {
861 if ( aSelections[i] != m_selectionPreImage[i] )
862 {
863 hasChanged = true ;
864 break ;
865 }
866 }
867 if ( !hasChanged )
868 {
869 return ;
870 }
871 }
e40298d5 872
a8e6bf8a 873 m_selectionPreImage = aSelections;
e40298d5 874
a8e6bf8a
RR
875 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
876 event.SetEventObject( this );
e40298d5 877
a8e6bf8a
RR
878 if ( count > 0 )
879 {
880 n = aSelections[0];
881 if ( HasClientObjectData() )
882 event.SetClientObject( GetClientObject(n) );
883 else if ( HasClientUntypedData() )
884 event.SetClientData( GetClientData(n) );
885 event.SetString( GetString(n) );
886 }
887 else
888 {
e40298d5 889 n = -1;
a8e6bf8a 890 }
e40298d5 891
e7549107 892 event.m_commandInt = n;
e40298d5 893
e7549107 894 GetEventHandler()->ProcessEvent(event);
519cb848
SC
895}
896
897void wxListBox::MacDoDoubleClick()
898{
899 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId);
900 event.SetEventObject( this );
e40298d5 901 GetEventHandler()->ProcessEvent(event) ;
519cb848 902}
ecaf6c18 903
ecaf6c18
SC
904void wxListBox::OnChar(wxKeyEvent& event)
905{
eb22f2a6 906 if ( event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER)
92104223 907 {
e40298d5
JS
908 wxWindow* parent = GetParent() ;
909 while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL )
910 parent = parent->GetParent() ;
911
912 if ( parent && parent->GetDefaultItem() )
913 {
914 wxButton *def = wxDynamicCast(parent->GetDefaultItem(),
915 wxButton);
916 if ( def && def->IsEnabled() )
917 {
918 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
919 event.SetEventObject(def);
920 def->Command(event);
921 return ;
922 }
923 }
924 event.Skip() ;
92104223
SC
925 }
926 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
eb22f2a6 927 else if (event.GetKeyCode() == WXK_ESCAPE || (event.GetKeyCode() == '.' && event.MetaDown() ) )
92104223 928 {
44553322 929 // FIXME: look in ancestors, not just parent.
e40298d5 930 wxWindow* win = GetParent()->FindWindow( wxID_CANCEL ) ;
44553322
JS
931 if (win)
932 {
933 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
934 new_event.SetEventObject( win );
935 win->GetEventHandler()->ProcessEvent( new_event );
936 }
92104223 937 }
eb22f2a6 938 else if ( event.GetKeyCode() == WXK_TAB )
92104223
SC
939 {
940 wxNavigationKeyEvent new_event;
941 new_event.SetEventObject( this );
942 new_event.SetDirection( !event.ShiftDown() );
943 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
944 new_event.SetWindowChange( event.ControlDown() );
945 new_event.SetCurrentFocus( this );
946 if ( !GetEventHandler()->ProcessEvent( new_event ) )
e40298d5 947 event.Skip() ;
92104223 948 }
e40298d5
JS
949 else if ( event.GetKeyCode() == WXK_DOWN || event.GetKeyCode() == WXK_UP )
950 {
951 // perform the default key handling first
952 wxControl::OnKeyDown( event ) ;
953
ecaf6c18
SC
954 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
955 event.SetEventObject( this );
e40298d5 956
ecaf6c18
SC
957 wxArrayInt aSelections;
958 int n, count = GetSelections(aSelections);
959 if ( count > 0 )
960 {
e40298d5
JS
961 n = aSelections[0];
962 if ( HasClientObjectData() )
963 event.SetClientObject( GetClientObject(n) );
964 else if ( HasClientUntypedData() )
965 event.SetClientData( GetClientData(n) );
966 event.SetString( GetString(n) );
ecaf6c18
SC
967 }
968 else
969 {
e40298d5 970 n = -1;
ecaf6c18 971 }
e40298d5 972
ecaf6c18 973 event.m_commandInt = n;
e40298d5 974
ecaf6c18 975 GetEventHandler()->ProcessEvent(event);
e40298d5
JS
976 }
977 else
978 {
979 if ( event.GetTimestamp() > m_lastTypeIn + 60 )
980 {
427ff662 981 m_typeIn = wxEmptyString ;
e40298d5
JS
982 }
983 m_lastTypeIn = event.GetTimestamp() ;
984 m_typeIn += (char) event.GetKeyCode() ;
427ff662 985 int line = FindString(wxT("*")+m_typeIn+wxT("*")) ;
e40298d5
JS
986 if ( line >= 0 )
987 {
988 if ( GetSelection() != line )
989 {
990 SetSelection(line) ;
991 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
992 event.SetEventObject( this );
993
994 if ( HasClientObjectData() )
995 event.SetClientObject( GetClientObject( line ) );
996 else if ( HasClientUntypedData() )
997 event.SetClientData( GetClientData(line) );
998 event.SetString( GetString(line) );
999
1000 event.m_commandInt = line ;
1001
1002 GetEventHandler()->ProcessEvent(event);
1003 }
1004 }
1005 }
ecaf6c18
SC
1006}
1007