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