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