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