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