]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/listbox.cpp
correction to maintain data array in synch with string array
[wxWidgets.git] / src / mac / carbon / listbox.cpp
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
21 #include "wx/utils.h"
22 #include "extldef.h"
23
24 #if !USE_SHARED_LIBRARY
25 IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
26
27 BEGIN_EVENT_TABLE(wxListBox, wxControl)
28 EVT_SIZE( wxListBox::OnSize )
29 END_EVENT_TABLE()
30 #endif
31
32 #include <wx/mac/uma.h>
33
34 extern "C" void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon) ;
35 const short kwxMacListWithVerticalScrollbar = 128 ;
36
37 // ============================================================================
38 // list box control implementation
39 // ============================================================================
40
41 // Listbox item
42 wxListBox::wxListBox()
43 {
44 m_noItems = 0;
45 m_selected = 0;
46 m_macList = NULL ;
47 }
48
49 bool 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 {
57 m_noItems = 0 ; // this will be increased by our append command
58 m_selected = 0;
59
60 Rect bounds ;
61 Str255 title ;
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
71 HLock( (Handle) m_macList ) ;
72 NewExtLDEFInfo( m_macList , MacDrawStringCell , (long) this ) ;
73 (**m_macList).selFlags = 0 ;
74 if ( style & wxLB_MULTIPLE )
75 {
76 (**m_macList).selFlags += lNoExtend ;
77 }
78 else if ( style & wxLB_EXTENDED )
79 {
80 (**m_macList).selFlags += lExtendDrag ;
81 }
82 else
83 {
84 (**m_macList).selFlags = lOnlyOne ;
85 }
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;
109 }
110
111 wxListBox::~wxListBox()
112 {
113 Free() ;
114 if ( m_macList )
115 {
116 DisposeExtLDEFInfo( m_macList ) ;
117 m_macList = NULL ;
118 }
119 }
120
121 void wxListBox::Free()
122 {
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 }
142 }
143
144 void wxListBox::DoSetFirstItem(int N)
145 {
146 MacScrollTo( N ) ;
147 }
148
149 void wxListBox::Delete(int N)
150 {
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
163 m_stringArray.Remove( N ) ;
164 m_dataArray.Remove( N ) ;
165 m_noItems --;
166
167 MacDelete( N ) ;
168 }
169
170 int wxListBox::DoAppend(const wxString& item)
171 {
172 int index = m_noItems ;
173 if( wxApp::s_macDefaultEncodingIsPC )
174 {
175 m_stringArray.Add( wxMacMakeMacStringFromPC( item ) ) ;
176 m_dataArray.Add( NULL );
177 }
178 else {
179 m_stringArray.Add( item ) ;
180 m_dataArray.Add( NULL );
181 }
182 m_noItems ++;
183 MacAppend( item ) ;
184
185 return index ;
186 }
187
188 void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
189 {
190 MacSetRedraw( false ) ;
191 Clear() ;
192 int n = choices.GetCount();
193
194 for( int i = 0 ; i < n ; ++i )
195 {
196 if ( clientData )
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
202 Append( choices[i] , clientData[i] ) ;
203 #endif
204 }
205 else
206 Append( choices[i] ) ;
207 }
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
229 bool wxListBox::HasMultipleSelection() const
230 {
231 return (m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED);
232 }
233
234 int wxListBox::FindString(const wxString& st) const
235 {
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() ;
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 }
258 }
259 else
260 {
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 }
271 }
272 return -1;
273 }
274
275 void wxListBox::Clear()
276 {
277 Free();
278 m_noItems = 0;
279 m_stringArray.Empty() ;
280 m_dataArray.Empty() ;
281 MacClear() ;
282 }
283
284 void wxListBox::SetSelection(int N, bool select)
285 {
286 wxCHECK_RET( N >= 0 && N < m_noItems,
287 "invalid index in wxListBox::SetSelection" );
288 MacSetSelection( N , select ) ;
289 }
290
291 bool wxListBox::IsSelected(int N) const
292 {
293 wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE,
294 "invalid index in wxListBox::Selected" );
295
296 return MacIsSelected( N ) ;
297 }
298
299 void *wxListBox::DoGetItemClientData(int N) const
300 {
301 wxCHECK_MSG( N >= 0 && N < m_noItems, NULL,
302 "invalid index in wxListBox::GetClientData" );
303
304 return (void *)m_dataArray[N];
305 }
306
307 wxClientData *wxListBox::DoGetItemClientObject(int N) const
308 {
309 return (wxClientData *) DoGetItemClientData( N ) ;
310 }
311
312 void wxListBox::DoSetItemClientData(int N, void *Client_data)
313 {
314 wxCHECK_RET( N >= 0 && N < m_noItems,
315 "invalid index in wxListBox::SetClientData" );
316
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
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 ;
330 }
331 else
332 {
333 m_dataArray.Add( (char*) Client_data ) ;
334 }
335 }
336
337 void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
338 {
339 DoSetItemClientData(n, clientData);
340 }
341
342 // Return number of selections and an array of selected integers
343 int wxListBox::GetSelections(wxArrayInt& aSelections) const
344 {
345 return MacGetSelections( aSelections ) ;
346
347 /* TODO
348 if ((m_windowStyle & wxLB_MULTIMacE) || (m_windowStyle & wxLB_EXTENDED))
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 */
363 }
364
365 // Get single selection, for single choice list items
366 int wxListBox::GetSelection() const
367 {
368 return MacGetSelection() ;
369 }
370
371 // Find string for position
372 wxString wxListBox::GetString(int N) const
373 {
374 if( wxApp::s_macDefaultEncodingIsPC )
375 {
376 return wxMacMakePCStringFromMac( m_stringArray[N] ) ;
377 }
378 else
379 return m_stringArray[N] ;
380 }
381
382 void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
383 {
384 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
385 wxT("invalid index in wxListBox::InsertItems") );
386
387 int nItems = items.GetCount();
388
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 }
395
396 m_noItems += nItems;
397 }
398
399 void wxListBox::SetString(int N, const wxString& s)
400 {
401 wxString str ;
402 if( wxApp::s_macDefaultEncodingIsPC )
403 {
404 str = wxMacMakeMacStringFromPC( s ) ;
405 }
406 else
407 str = s ;
408 m_stringArray[N] = str ;
409 MacSet( N , s ) ;
410 }
411
412 wxSize wxListBox::DoGetBestSize() const
413 {
414 return wxSize(100, 100);
415 }
416
417 int wxListBox::GetCount() const
418 {
419 return m_noItems;
420 }
421
422 void wxListBox::SetupColours()
423 {
424 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
425 SetForegroundColour(GetParent()->GetForegroundColour());
426 }
427
428 #if wxUSE_OWNER_DRAWN
429
430 class wxListBoxItem : public wxOwnerDrawn
431 {
432 public:
433 wxListBoxItem(const wxString& str = "");
434 };
435
436 wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE)
437 {
438 // no bitmaps/checkmarks
439 SetMarginWidth(0);
440 }
441
442 wxOwnerDrawn *wxListBox::CreateItem(size_t n)
443 {
444 return new wxListBoxItem();
445 }
446
447 #endif //USE_OWNER_DRAWN
448
449 // ============================================================================
450 // list box control implementation
451 // ============================================================================
452
453 void 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
468 void 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
477 void 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
487 void 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
494 void wxListBox::MacClear()
495 {
496 LDelRow( (**m_macList).dataBounds.bottom , 0 , m_macList ) ;
497 }
498
499 void 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
512 bool wxListBox::MacIsSelected( int n ) const
513 {
514 Cell cell = { 0 , 0 } ;
515 cell.v = n ;
516 return LGetSelect( false , &cell , m_macList ) ;
517 }
518
519 void wxListBox::MacDestroy()
520 {
521 // DisposeExtLDEFInfo( m_macList ) ;
522 }
523
524 int 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
533 int 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
551 void 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
560 void wxListBox::MacScrollTo( int n )
561 {
562 // TODO implement scrolling
563 }
564
565 void wxListBox::OnSize( const wxSizeEvent &event)
566 {
567 Point pt = (**m_macList).cellSize ;
568 pt.h = m_width - 15 ;
569 LCellSize( pt , m_macList ) ;
570 }
571
572 void 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
588 void wxListBox::MacSetRedraw( bool doDraw )
589 {
590 LSetDrawingMode( doDraw , m_macList ) ;
591
592 }
593
594 void wxListBox::MacDoClick()
595 {
596 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
597 event.SetEventObject( this );
598
599 wxArrayInt aSelections;
600 int n, count = GetSelections(aSelections);
601 if ( count > 0 )
602 {
603 n = aSelections[0];
604 if ( HasClientObjectData() )
605 event.SetClientObject( GetClientObject(n) );
606 else if ( HasClientUntypedData() )
607 event.SetClientData( GetClientData(n) );
608 event.SetString( GetString(n) );
609 }
610 else
611 {
612 n = -1;
613 }
614
615 event.m_commandInt = n;
616
617 GetEventHandler()->ProcessEvent(event);
618 }
619
620 void wxListBox::MacDoDoubleClick()
621 {
622 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId);
623 event.SetEventObject( this );
624 GetEventHandler()->ProcessEvent(event) ;
625 }