]> git.saurik.com Git - wxWidgets.git/blame - src/mac/listbox.cpp
oops, typo leading to compilation problem fixed
[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
SC
17#include "wx/listbox.h"
18#include "wx/settings.h"
19#include "wx/dynarray.h"
20#include "wx/log.h"
21
519cb848 22#include "wx/utils.h"
03e11df5
GD
23#ifdef __UNIX__
24 #include "ldef/extldef.h"
25#else
26 #include "extldef.h"
27#endif
519cb848 28
2f1ae414 29#if !USE_SHARED_LIBRARY
e9576ca5 30 IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
519cb848
SC
31
32BEGIN_EVENT_TABLE(wxListBox, wxControl)
33 EVT_SIZE( wxListBox::OnSize )
34END_EVENT_TABLE()
2f1ae414 35#endif
e9576ca5 36
519cb848
SC
37#include <wx/mac/uma.h>
38
39extern "C" void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon) ;
40const short kwxMacListWithVerticalScrollbar = 128 ;
41
e9576ca5
SC
42// ============================================================================
43// list box control implementation
44// ============================================================================
45
46// Listbox item
47wxListBox::wxListBox()
48{
49 m_noItems = 0;
50 m_selected = 0;
2f1ae414 51 m_macList = NULL ;
e9576ca5
SC
52}
53
54bool wxListBox::Create(wxWindow *parent, wxWindowID id,
55 const wxPoint& pos,
56 const wxSize& size,
57 int n, const wxString choices[],
58 long style,
59 const wxValidator& validator,
60 const wxString& name)
61{
519cb848 62 m_noItems = 0 ; // this will be increased by our append command
e9576ca5
SC
63 m_selected = 0;
64
519cb848
SC
65 Rect bounds ;
66 Str255 title ;
519cb848
SC
67
68 MacPreControlCreate( parent , id , "" , pos , size ,style, validator , name , &bounds , title ) ;
69
70 m_macControl = UMANewControl( parent->GetMacRootWindow() , &bounds , title , true , kwxMacListWithVerticalScrollbar , 0 , 0,
71 kControlListBoxProc , (long) this ) ;
72
73 long result ;
74 UMAGetControlData( m_macControl , kControlNoPart , kControlListBoxListHandleTag , sizeof( ListHandle ) , (char*) &m_macList , &result ) ;
75
2f1ae414 76 HLock( (Handle) m_macList ) ;
519cb848 77 NewExtLDEFInfo( m_macList , MacDrawStringCell , (long) this ) ;
8208e181
SC
78 (**m_macList).selFlags = 0 ;
79 if ( style & wxLB_MULTIPLE )
519cb848
SC
80 {
81 (**m_macList).selFlags += lNoExtend ;
82 }
83 else if ( style & wxLB_EXTENDED )
84 {
85 (**m_macList).selFlags += lExtendDrag ;
86 }
8208e181
SC
87 else
88 {
89 (**m_macList).selFlags = lOnlyOne ;
90 }
519cb848
SC
91 Point pt = (**m_macList).cellSize ;
92 pt.v = 14 ;
93 LCellSize( pt , m_macList ) ;
94
95 LAddColumn( 1 , 0 , m_macList ) ;
96
97 MacPostControlCreate() ;
98
99 ControlFontStyleRec controlstyle ;
100 controlstyle.flags = kControlUseFontMask + kControlUseSizeMask ;
101 //controlstyle.font = kControlFontSmallSystemFont ;
102 controlstyle.font = kFontIDMonaco ;
103 controlstyle.size = 9 ;
104 ::UMASetControlFontStyle( m_macControl , &controlstyle ) ;
105
106 for ( int i = 0 ; i < n ; i++ )
107 {
108 Append( choices[i] ) ;
109 }
110
111 LSetDrawingMode( true , m_macList ) ;
112
113 return TRUE;
e9576ca5
SC
114}
115
116wxListBox::~wxListBox()
117{
e7549107 118 Free() ;
2f1ae414
SC
119 if ( m_macList )
120 {
121 DisposeExtLDEFInfo( m_macList ) ;
122 m_macList = NULL ;
123 }
e9576ca5
SC
124}
125
e7549107 126void wxListBox::Free()
e9576ca5 127{
e7549107
SC
128#if wxUSE_OWNER_DRAWN
129 if ( m_windowStyle & wxLB_OWNERDRAW )
130 {
131 size_t uiCount = m_aItems.Count();
132 while ( uiCount-- != 0 ) {
133 delete m_aItems[uiCount];
134 }
135
136 m_aItems.Clear();
137 }
138 else
139#endif // wxUSE_OWNER_DRAWN
140 if ( HasClientObjectData() )
141 {
142 for ( size_t n = 0; n < (size_t)m_noItems; n++ )
143 {
144 delete GetClientObject(n);
145 }
146 }
e9576ca5
SC
147}
148
e7549107 149void wxListBox::DoSetFirstItem(int N)
e9576ca5 150{
e7549107 151 MacScrollTo( N ) ;
e9576ca5
SC
152}
153
154void wxListBox::Delete(int N)
155{
e7549107
SC
156 wxCHECK_RET( N >= 0 && N < m_noItems,
157 wxT("invalid index in wxListBox::Delete") );
158
159#if wxUSE_OWNER_DRAWN
160 delete m_aItems[N];
161 m_aItems.Remove(N);
162#else // !wxUSE_OWNER_DRAWN
163 if ( HasClientObjectData() )
164 {
165 delete GetClientObject(N);
166 }
167#endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
519cb848 168 m_stringArray.Remove( N ) ;
8208e181 169 m_dataArray.Remove( N ) ;
2f1ae414 170 m_noItems --;
519cb848
SC
171
172 MacDelete( N ) ;
e9576ca5
SC
173}
174
e7549107 175int wxListBox::DoAppend(const wxString& item)
e9576ca5 176{
e7549107 177 int index = m_noItems ;
519cb848
SC
178 if( wxApp::s_macDefaultEncodingIsPC )
179 {
180 m_stringArray.Add( wxMacMakeMacStringFromPC( item ) ) ;
b81abd0d 181 m_dataArray.Add( NULL );
519cb848 182 }
b81abd0d 183 else {
519cb848 184 m_stringArray.Add( item ) ;
b81abd0d
GD
185 m_dataArray.Add( NULL );
186 }
e7549107 187 m_noItems ++;
5b781a67 188 DoSetItemClientData( index , NULL ) ;
519cb848 189 MacAppend( item ) ;
e7549107 190
e7549107 191 return index ;
e9576ca5
SC
192}
193
e7549107
SC
194void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
195{
196 MacSetRedraw( false ) ;
519cb848 197 Clear() ;
e7549107
SC
198 int n = choices.GetCount();
199
519cb848
SC
200 for( int i = 0 ; i < n ; ++i )
201 {
202 if ( clientData )
e7549107
SC
203 {
204#if wxUSE_OWNER_DRAWN
205 wxASSERT_MSG(clientData[i] == NULL,
206 wxT("Can't use client data with owner-drawn listboxes"));
207#else // !wxUSE_OWNER_DRAWN
8208e181 208 Append( choices[i] , clientData[i] ) ;
e7549107
SC
209 #endif
210 }
519cb848
SC
211 else
212 Append( choices[i] ) ;
213 }
e7549107
SC
214
215#if wxUSE_OWNER_DRAWN
216 if ( m_windowStyle & wxLB_OWNERDRAW ) {
217 // first delete old items
218 size_t ui = m_aItems.Count();
219 while ( ui-- != 0 ) {
220 delete m_aItems[ui];
221 }
222 m_aItems.Empty();
223
224 // then create new ones
225 for ( ui = 0; ui < (size_t)m_noItems; ui++ ) {
226 wxOwnerDrawn *pNewItem = CreateItem(ui);
227 pNewItem->SetName(choices[ui]);
228 m_aItems.Add(pNewItem);
229 }
230 }
231#endif // wxUSE_OWNER_DRAWN
232 MacSetRedraw( true ) ;
233}
234
235bool wxListBox::HasMultipleSelection() const
236{
237 return (m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED);
e9576ca5
SC
238}
239
519cb848 240int wxListBox::FindString(const wxString& st) const
e9576ca5 241{
519cb848
SC
242 wxString s ;
243 if( wxApp::s_macDefaultEncodingIsPC )
244 {
245 s = wxMacMakeMacStringFromPC( st ) ;
246 }
247 else
248 s = st ;
249
250 if ( s.Right(1) == "*" )
251 {
252 wxString search = s.Left( s.Length() - 1 ) ;
253 int len = search.Length() ;
03e11df5
GD
254 Str255 s1 , s2 ;
255
256#if TARGET_CARBON
257 c2pstrcpy( (StringPtr) s2 , search.c_str() ) ;
258#else
259 strcpy( (char *) s2 , search.c_str() ) ;
260 c2pstr( (char *) s2 ) ;
261#endif
262
2f1ae414
SC
263 for ( int i = 0 ; i < m_noItems ; ++ i )
264 {
03e11df5
GD
265#if TARGET_CARBON
266 c2pstrcpy( (StringPtr) s1 , m_stringArray[i].Left( len ).c_str() ) ;
267#else
268 strcpy( (char *) s1 , m_stringArray[i].Left( len ).c_str() ) ;
269 c2pstr( (char *) s1 ) ;
270#endif
2f1ae414
SC
271 if ( EqualString( s1 , s2 , false , false ) )
272 return i ;
273 }
5b781a67
SC
274 if ( s.Left(1) = "*" && s.Length() > 1 )
275 {
276 s.MakeLower() ;
277 for ( int i = 0 ; i < m_noItems ; ++i )
278 {
279 if ( GetString(i).Lower().Matches(s) )
280 return i ;
281 }
282 }
283
519cb848
SC
284 }
285 else
286 {
2f1ae414 287 Str255 s1 , s2 ;
03e11df5
GD
288
289#if TARGET_CARBON
290 c2pstrcpy( (StringPtr) s2 , s.c_str() ) ;
291#else
292 strcpy( (char *) s2 , s.c_str() ) ;
293 c2pstr( (char *) s2 ) ;
294#endif
295
2f1ae414
SC
296 for ( int i = 0 ; i < m_noItems ; ++ i )
297 {
03e11df5
GD
298#if TARGET_CARBON
299 c2pstrcpy( (StringPtr) s1 , m_stringArray[i].c_str() ) ;
300#else
301 strcpy( (char *) s1 , m_stringArray[i].c_str() ) ;
302 c2pstr( (char *) s1 ) ;
303#endif
2f1ae414
SC
304 if ( EqualString( s1 , s2 , false , false ) )
305 return i ;
306 }
519cb848
SC
307 }
308 return -1;
e9576ca5
SC
309}
310
311void wxListBox::Clear()
312{
e7549107 313 Free();
e9576ca5 314 m_noItems = 0;
519cb848
SC
315 m_stringArray.Empty() ;
316 m_dataArray.Empty() ;
317 MacClear() ;
e9576ca5
SC
318}
319
320void wxListBox::SetSelection(int N, bool select)
321{
519cb848
SC
322 wxCHECK_RET( N >= 0 && N < m_noItems,
323 "invalid index in wxListBox::SetSelection" );
324 MacSetSelection( N , select ) ;
e9576ca5
SC
325}
326
e7549107 327bool wxListBox::IsSelected(int N) const
e9576ca5 328{
519cb848
SC
329 wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE,
330 "invalid index in wxListBox::Selected" );
331
332 return MacIsSelected( N ) ;
e9576ca5
SC
333}
334
e7549107 335void *wxListBox::DoGetItemClientData(int N) const
e9576ca5 336{
519cb848
SC
337 wxCHECK_MSG( N >= 0 && N < m_noItems, NULL,
338 "invalid index in wxListBox::GetClientData" );
339
e7549107 340 return (void *)m_dataArray[N];
e9576ca5
SC
341}
342
51abe921
SC
343wxClientData *wxListBox::DoGetItemClientObject(int N) const
344{
345 return (wxClientData *) DoGetItemClientData( N ) ;
346}
347
e7549107 348void wxListBox::DoSetItemClientData(int N, void *Client_data)
e9576ca5 349{
519cb848
SC
350 wxCHECK_RET( N >= 0 && N < m_noItems,
351 "invalid index in wxListBox::SetClientData" );
352
e7549107
SC
353#if wxUSE_OWNER_DRAWN
354 if ( m_windowStyle & wxLB_OWNERDRAW )
355 {
356 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
357 // in OnMeasure/OnDraw.
358 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
359 }
360#endif // wxUSE_OWNER_DRAWN
8208e181
SC
361 wxASSERT_MSG( m_dataArray.GetCount() >= N , "invalid client_data array" ) ;
362
363 if ( m_dataArray.GetCount() > N )
364 {
365 m_dataArray[N] = (char*) Client_data ;
2f1ae414 366 }
8208e181
SC
367 else
368 {
369 m_dataArray.Add( (char*) Client_data ) ;
370 }
e7549107
SC
371}
372
373void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
374{
375 DoSetItemClientData(n, clientData);
e9576ca5
SC
376}
377
378// Return number of selections and an array of selected integers
379int wxListBox::GetSelections(wxArrayInt& aSelections) const
380{
519cb848 381 return MacGetSelections( aSelections ) ;
e9576ca5
SC
382
383/* TODO
519cb848 384 if ((m_windowStyle & wxLB_MULTIMacE) || (m_windowStyle & wxLB_EXTENDED))
e9576ca5
SC
385 {
386 int no_sel = ??
387 for ( int n = 0; n < no_sel; n++ )
388 aSelections.Add(??);
389
390 return no_sel;
391 }
392 else // single-selection listbox
393 {
394 aSelections.Add(??);
395
396 return 1;
397 }
398*/
e9576ca5
SC
399}
400
401// Get single selection, for single choice list items
402int wxListBox::GetSelection() const
403{
519cb848 404 return MacGetSelection() ;
e9576ca5
SC
405}
406
407// Find string for position
408wxString wxListBox::GetString(int N) const
409{
519cb848
SC
410 if( wxApp::s_macDefaultEncodingIsPC )
411 {
412 return wxMacMakePCStringFromMac( m_stringArray[N] ) ;
413 }
414 else
415 return m_stringArray[N] ;
e9576ca5
SC
416}
417
e7549107 418void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
e9576ca5 419{
e7549107
SC
420 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
421 wxT("invalid index in wxListBox::InsertItems") );
422
423 int nItems = items.GetCount();
424
519cb848
SC
425 for ( int i = 0 ; i < nItems ; i++ )
426 {
427 m_stringArray.Insert( items[i] , pos + i ) ;
428 m_dataArray.Insert( NULL , pos + i ) ;
429 MacInsert( pos + i , items[i] ) ;
430 }
e9576ca5 431
519cb848 432 m_noItems += nItems;
e9576ca5
SC
433}
434
435void wxListBox::SetString(int N, const wxString& s)
436{
2f1ae414
SC
437 wxString str ;
438 if( wxApp::s_macDefaultEncodingIsPC )
439 {
440 str = wxMacMakeMacStringFromPC( s ) ;
441 }
442 else
443 str = s ;
444 m_stringArray[N] = str ;
519cb848 445 MacSet( N , s ) ;
e9576ca5
SC
446}
447
37e2cb08 448wxSize wxListBox::DoGetBestSize() const
e9576ca5 449{
e7549107 450 return wxSize(100, 100);
e9576ca5
SC
451}
452
51abe921
SC
453int wxListBox::GetCount() const
454{
455 return m_noItems;
456}
457
458void wxListBox::SetupColours()
459{
460 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
461 SetForegroundColour(GetParent()->GetForegroundColour());
462}
463
464#if wxUSE_OWNER_DRAWN
465
466class wxListBoxItem : public wxOwnerDrawn
467{
468public:
469 wxListBoxItem(const wxString& str = "");
470};
471
472wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE)
473{
474 // no bitmaps/checkmarks
475 SetMarginWidth(0);
476}
477
478wxOwnerDrawn *wxListBox::CreateItem(size_t n)
479{
480 return new wxListBoxItem();
481}
482
483#endif //USE_OWNER_DRAWN
e9576ca5 484
519cb848
SC
485// ============================================================================
486// list box control implementation
487// ============================================================================
488
489void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon)
490{
491 wxListBox* list;
492 // typecast our refCon
493 list = (wxListBox*)refCon;
494
495 MoveTo(cellRect->left + 4 , cellRect->top + 10 );
496 const wxString text = list->m_stringArray[lCell.v] ;
497 ::TextFont( kFontIDMonaco ) ;
498 ::TextSize( 9 );
499 ::TextFace( 0 ) ;
500 DrawText(text, 0 , text.Length());
501
502}
503
504void wxListBox::MacDelete( int N )
505{
506 ListHandle list ;
507 long result ;
519cb848
SC
508 UMAGetControlData( m_macControl , kControlNoPart , kControlListBoxListHandleTag , sizeof( ListHandle ) , (char*) &list , &result ) ;
509 LDelRow( 1 , N , list ) ;
510}
511
512void wxListBox::MacInsert( int n , const char * text)
513{
514 Cell cell ;
515
516 cell.h = 0 ;
517 cell.v = n ;
518
519 LAddRow( 1 , cell.v , m_macList ) ;
520}
521
522void wxListBox::MacAppend( const char * text)
523{
524 Cell cell = { 0 , 0 } ;
525 cell.v = (**m_macList).dataBounds.bottom ;
526 LAddRow( 1 , cell.v , m_macList ) ;
527}
528
529void wxListBox::MacClear()
530{
531 LDelRow( (**m_macList).dataBounds.bottom , 0 , m_macList ) ;
532}
533
534void wxListBox::MacSetSelection( int n , bool select )
535{
536 Cell cell = { 0 , 0 } ;
537 if ( LGetSelect( TRUE , &cell , m_macList ) )
538 {
539 LSetSelect( false , cell , m_macList ) ;
540 }
541
542 cell.v = n ;
543 LSetSelect( select , cell , m_macList ) ;
544 LAutoScroll( m_macList ) ;
545}
546
547bool wxListBox::MacIsSelected( int n ) const
548{
549 Cell cell = { 0 , 0 } ;
550 cell.v = n ;
551 return LGetSelect( false , &cell , m_macList ) ;
552}
553
554void wxListBox::MacDestroy()
555{
556// DisposeExtLDEFInfo( m_macList ) ;
557}
558
559int wxListBox::MacGetSelection() const
560{
561 Cell cell = { 0 , 0 } ;
562 if ( LGetSelect( true , &cell , m_macList ) )
563 return cell.v ;
564 else
565 return -1 ;
566}
567
568int wxListBox::MacGetSelections( wxArrayInt& aSelections ) const
569{
570 int no_sel = 0 ;
571
572 aSelections.Empty();
573
574 Cell cell = { 0 , 0 } ;
575 cell.v = 0 ;
576
577 while ( LGetSelect( true , &cell , m_macList ) )
578 {
579 aSelections.Add( cell.v ) ;
580 no_sel++ ;
581 cell.v++ ;
582 }
583 return no_sel ;
584}
585
586void wxListBox::MacSet( int n , const char * text )
587{
588 // our implementation does not store anything in the list
589 // so we just have to redraw
590 Cell cell = { 0 , 0 } ;
591 cell.v = n ;
592 LDraw( cell , m_macList ) ;
593}
594
595void wxListBox::MacScrollTo( int n )
596{
597 // TODO implement scrolling
598}
599
600void wxListBox::OnSize( const wxSizeEvent &event)
601{
602 Point pt = (**m_macList).cellSize ;
2f1ae414 603 pt.h = m_width - 15 ;
519cb848
SC
604 LCellSize( pt , m_macList ) ;
605}
606
607void wxListBox::MacHandleControlClick( ControlHandle control , SInt16 controlpart )
608{
609 Boolean wasDoubleClick = false ;
610 long result ;
611
612 UMAGetControlData( m_macControl , kControlNoPart , kControlListBoxDoubleClickTag , sizeof( wasDoubleClick ) , (char*) &wasDoubleClick , &result ) ;
613 if ( !wasDoubleClick )
614 {
615 MacDoClick() ;
616 }
617 else
618 {
619 MacDoDoubleClick() ;
620 }
621}
622
623void wxListBox::MacSetRedraw( bool doDraw )
624{
625 LSetDrawingMode( doDraw , m_macList ) ;
626
627}
628
629void wxListBox::MacDoClick()
630{
631 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
e7549107
SC
632 event.SetEventObject( this );
633
519cb848 634 wxArrayInt aSelections;
e7549107 635 int n, count = GetSelections(aSelections);
519cb848
SC
636 if ( count > 0 )
637 {
7c74e7fe
SC
638 n = aSelections[0];
639 if ( HasClientObjectData() )
e7549107
SC
640 event.SetClientObject( GetClientObject(n) );
641 else if ( HasClientUntypedData() )
642 event.SetClientData( GetClientData(n) );
643 event.SetString( GetString(n) );
519cb848
SC
644 }
645 else
646 {
e7549107 647 n = -1;
519cb848
SC
648 }
649
e7549107
SC
650 event.m_commandInt = n;
651
652 GetEventHandler()->ProcessEvent(event);
519cb848
SC
653}
654
655void wxListBox::MacDoDoubleClick()
656{
657 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId);
658 event.SetEventObject( this );
659 GetEventHandler()->ProcessEvent(event) ;
660}