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