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