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