]> git.saurik.com Git - wxWidgets.git/blob - src/mac/listbox.cpp
Mac-ify wxTreeCtrl further.
[wxWidgets.git] / src / mac / 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/app.h"
17 #include "wx/listbox.h"
18 #include "wx/settings.h"
19 #include "wx/dynarray.h"
20 #include "wx/log.h"
21
22 #include "wx/utils.h"
23 #ifdef __UNIX__
24 #include "ldef/extldef.h"
25 #else
26 #include "extldef.h"
27 #endif
28
29 #if !USE_SHARED_LIBRARY
30 IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
31
32 BEGIN_EVENT_TABLE(wxListBox, wxControl)
33 EVT_SIZE( wxListBox::OnSize )
34 END_EVENT_TABLE()
35 #endif
36
37 #include <wx/mac/uma.h>
38
39 extern "C" void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon) ;
40 const short kwxMacListWithVerticalScrollbar = 128 ;
41
42 // ============================================================================
43 // list box control implementation
44 // ============================================================================
45
46 // Listbox item
47 wxListBox::wxListBox()
48 {
49 m_noItems = 0;
50 m_selected = 0;
51 m_macList = NULL ;
52 }
53
54 bool 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 {
62 m_noItems = 0 ; // this will be increased by our append command
63 m_selected = 0;
64
65 Rect bounds ;
66 Str255 title ;
67
68 MacPreControlCreate( parent , id , "" , pos , size ,style, validator , name , &bounds , title ) ;
69
70 m_macControl = UMANewControl( parent->GetMacRootWindow() , &bounds , title , false , kwxMacListWithVerticalScrollbar , 0 , 0,
71 kControlListBoxProc , (long) this ) ;
72
73 long result ;
74 UMAGetControlData( m_macControl , kControlNoPart , kControlListBoxListHandleTag , sizeof( ListHandle ) , (char*) &m_macList , &result ) ;
75
76 HLock( (Handle) m_macList ) ;
77 NewExtLDEFInfo( m_macList , MacDrawStringCell , (long) this ) ;
78 (**m_macList).selFlags = 0 ;
79 if ( style & wxLB_MULTIPLE )
80 {
81 (**m_macList).selFlags += lNoExtend ;
82 }
83 else if ( style & wxLB_EXTENDED )
84 {
85 (**m_macList).selFlags += lExtendDrag ;
86 }
87 else
88 {
89 (**m_macList).selFlags = lOnlyOne ;
90 }
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;
114 }
115
116 wxListBox::~wxListBox()
117 {
118 Free() ;
119 if ( m_macList )
120 {
121 DisposeExtLDEFInfo( m_macList ) ;
122 m_macList = NULL ;
123 }
124 }
125
126 void wxListBox::Free()
127 {
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 }
147 }
148
149 void wxListBox::DoSetFirstItem(int N)
150 {
151 MacScrollTo( N ) ;
152 }
153
154 void wxListBox::Delete(int N)
155 {
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
168 m_stringArray.Remove( N ) ;
169 m_dataArray.RemoveAt( N ) ;
170 m_noItems --;
171
172 MacDelete( N ) ;
173 }
174
175 int wxListBox::DoAppend(const wxString& item)
176 {
177 int index = m_noItems ;
178 if( wxApp::s_macDefaultEncodingIsPC )
179 {
180 m_stringArray.Add( wxMacMakeMacStringFromPC( item ) ) ;
181 m_dataArray.Add( NULL );
182 }
183 else {
184 m_stringArray.Add( item ) ;
185 m_dataArray.Add( NULL );
186 }
187 m_noItems ++;
188 DoSetItemClientData( index , NULL ) ;
189 MacAppend( item ) ;
190
191 return index ;
192 }
193
194 void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
195 {
196 MacSetRedraw( false ) ;
197 Clear() ;
198 int n = choices.GetCount();
199
200 for( int i = 0 ; i < n ; ++i )
201 {
202 if ( clientData )
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
208 Append( choices[i] , clientData[i] ) ;
209 #endif
210 }
211 else
212 Append( choices[i] ) ;
213 }
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
235 bool wxListBox::HasMultipleSelection() const
236 {
237 return (m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED);
238 }
239
240 int wxListBox::FindString(const wxString& st) const
241 {
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() ;
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
263 for ( int i = 0 ; i < m_noItems ; ++ i )
264 {
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
271 if ( EqualString( s1 , s2 , false , false ) )
272 return i ;
273 }
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
284 }
285 else
286 {
287 Str255 s1 , s2 ;
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
296 for ( int i = 0 ; i < m_noItems ; ++ i )
297 {
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
304 if ( EqualString( s1 , s2 , false , false ) )
305 return i ;
306 }
307 }
308 return -1;
309 }
310
311 void wxListBox::Clear()
312 {
313 Free();
314 m_noItems = 0;
315 m_stringArray.Empty() ;
316 m_dataArray.Empty() ;
317 MacClear() ;
318 }
319
320 void wxListBox::SetSelection(int N, bool select)
321 {
322 wxCHECK_RET( N >= 0 && N < m_noItems,
323 "invalid index in wxListBox::SetSelection" );
324 MacSetSelection( N , select ) ;
325 }
326
327 bool wxListBox::IsSelected(int N) const
328 {
329 wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE,
330 "invalid index in wxListBox::Selected" );
331
332 return MacIsSelected( N ) ;
333 }
334
335 void *wxListBox::DoGetItemClientData(int N) const
336 {
337 wxCHECK_MSG( N >= 0 && N < m_noItems, NULL,
338 "invalid index in wxListBox::GetClientData" );
339
340 return (void *)m_dataArray[N];
341 }
342
343 wxClientData *wxListBox::DoGetItemClientObject(int N) const
344 {
345 return (wxClientData *) DoGetItemClientData( N ) ;
346 }
347
348 void wxListBox::DoSetItemClientData(int N, void *Client_data)
349 {
350 wxCHECK_RET( N >= 0 && N < m_noItems,
351 "invalid index in wxListBox::SetClientData" );
352
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
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 ;
366 }
367 else
368 {
369 m_dataArray.Add( (char*) Client_data ) ;
370 }
371 }
372
373 void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
374 {
375 DoSetItemClientData(n, clientData);
376 }
377
378 // Return number of selections and an array of selected integers
379 int wxListBox::GetSelections(wxArrayInt& aSelections) const
380 {
381 return MacGetSelections( aSelections ) ;
382
383 /* TODO
384 if ((m_windowStyle & wxLB_MULTIMacE) || (m_windowStyle & wxLB_EXTENDED))
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 */
399 }
400
401 // Get single selection, for single choice list items
402 int wxListBox::GetSelection() const
403 {
404 return MacGetSelection() ;
405 }
406
407 // Find string for position
408 wxString wxListBox::GetString(int N) const
409 {
410 if( wxApp::s_macDefaultEncodingIsPC )
411 {
412 return wxMacMakePCStringFromMac( m_stringArray[N] ) ;
413 }
414 else
415 return m_stringArray[N] ;
416 }
417
418 void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
419 {
420 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
421 wxT("invalid index in wxListBox::InsertItems") );
422
423 int nItems = items.GetCount();
424
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 }
431
432 m_noItems += nItems;
433 }
434
435 void wxListBox::SetString(int N, const wxString& s)
436 {
437 wxString str ;
438 if( wxApp::s_macDefaultEncodingIsPC )
439 {
440 str = wxMacMakeMacStringFromPC( s ) ;
441 }
442 else
443 str = s ;
444 m_stringArray[N] = str ;
445 MacSet( N , s ) ;
446 }
447
448 wxSize wxListBox::DoGetBestSize() const
449 {
450 return wxSize(100, 100);
451 }
452
453 int wxListBox::GetCount() const
454 {
455 return m_noItems;
456 }
457
458 void wxListBox::SetupColours()
459 {
460 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
461 SetForegroundColour(GetParent()->GetForegroundColour());
462 }
463
464 #if wxUSE_OWNER_DRAWN
465
466 class wxListBoxItem : public wxOwnerDrawn
467 {
468 public:
469 wxListBoxItem(const wxString& str = "");
470 };
471
472 wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE)
473 {
474 // no bitmaps/checkmarks
475 SetMarginWidth(0);
476 }
477
478 wxOwnerDrawn *wxListBox::CreateItem(size_t n)
479 {
480 return new wxListBoxItem();
481 }
482
483 #endif //USE_OWNER_DRAWN
484
485 // ============================================================================
486 // list box control implementation
487 // ============================================================================
488
489 void 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
504 void wxListBox::MacDelete( int N )
505 {
506 ListHandle list ;
507 long result ;
508 UMAGetControlData( m_macControl , kControlNoPart , kControlListBoxListHandleTag , sizeof( ListHandle ) , (char*) &list , &result ) ;
509 LDelRow( 1 , N , list ) ;
510 }
511
512 void 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
522 void 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
529 void wxListBox::MacClear()
530 {
531 LDelRow( (**m_macList).dataBounds.bottom , 0 , m_macList ) ;
532 }
533
534 void 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
547 bool wxListBox::MacIsSelected( int n ) const
548 {
549 Cell cell = { 0 , 0 } ;
550 cell.v = n ;
551 return LGetSelect( false , &cell , m_macList ) ;
552 }
553
554 void wxListBox::MacDestroy()
555 {
556 // DisposeExtLDEFInfo( m_macList ) ;
557 }
558
559 int 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
568 int 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
586 void 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
595 void wxListBox::MacScrollTo( int n )
596 {
597 // TODO implement scrolling
598 }
599
600 void wxListBox::OnSize( const wxSizeEvent &event)
601 {
602 Point pt = (**m_macList).cellSize ;
603 pt.h = m_width - 15 ;
604 LCellSize( pt , m_macList ) ;
605 }
606
607 void 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
623 void wxListBox::MacSetRedraw( bool doDraw )
624 {
625 LSetDrawingMode( doDraw , m_macList ) ;
626
627 }
628
629 void wxListBox::MacDoClick()
630 {
631 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
632 event.SetEventObject( this );
633
634 wxArrayInt aSelections;
635 int n, count = GetSelections(aSelections);
636 if ( count > 0 )
637 {
638 n = aSelections[0];
639 if ( HasClientObjectData() )
640 event.SetClientObject( GetClientObject(n) );
641 else if ( HasClientUntypedData() )
642 event.SetClientData( GetClientData(n) );
643 event.SetString( GetString(n) );
644 }
645 else
646 {
647 n = -1;
648 }
649
650 event.m_commandInt = n;
651
652 GetEventHandler()->ProcessEvent(event);
653 }
654
655 void wxListBox::MacDoDoubleClick()
656 {
657 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId);
658 event.SetEventObject( this );
659 GetEventHandler()->ProcessEvent(event) ;
660 }