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