1 ///////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "listbox.h"
16 #include "wx/listbox.h"
17 #include "wx/settings.h"
18 #include "wx/dynarray.h"
24 #if !USE_SHARED_LIBRARY
25 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControl
)
27 BEGIN_EVENT_TABLE(wxListBox
, wxControl
)
28 EVT_SIZE( wxListBox::OnSize
)
32 #include <wx/mac/uma.h>
34 extern "C" void MacDrawStringCell(Rect
*cellRect
, Cell lCell
, ListHandle theList
, long refCon
) ;
35 const short kwxMacListWithVerticalScrollbar
= 128 ;
37 // ============================================================================
38 // list box control implementation
39 // ============================================================================
42 wxListBox::wxListBox()
49 bool wxListBox::Create(wxWindow
*parent
, wxWindowID id
,
52 int n
, const wxString choices
[],
54 const wxValidator
& validator
,
57 m_noItems
= 0 ; // this will be increased by our append command
63 MacPreControlCreate( parent
, id
, "" , pos
, size
,style
, validator
, name
, &bounds
, title
) ;
65 m_macControl
= UMANewControl( parent
->GetMacRootWindow() , &bounds
, title
, true , kwxMacListWithVerticalScrollbar
, 0 , 0,
66 kControlListBoxProc
, (long) this ) ;
69 UMAGetControlData( m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
, sizeof( ListHandle
) , (char*) &m_macList
, &result
) ;
71 HLock( (Handle
) m_macList
) ;
72 NewExtLDEFInfo( m_macList
, MacDrawStringCell
, (long) this ) ;
73 (**m_macList
).selFlags
= 0 ;
74 if ( style
& wxLB_MULTIPLE
)
76 (**m_macList
).selFlags
+= lNoExtend
;
78 else if ( style
& wxLB_EXTENDED
)
80 (**m_macList
).selFlags
+= lExtendDrag
;
84 (**m_macList
).selFlags
= lOnlyOne
;
86 Point pt
= (**m_macList
).cellSize
;
88 LCellSize( pt
, m_macList
) ;
90 LAddColumn( 1 , 0 , m_macList
) ;
92 MacPostControlCreate() ;
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
) ;
101 for ( int i
= 0 ; i
< n
; i
++ )
103 Append( choices
[i
] ) ;
106 LSetDrawingMode( true , m_macList
) ;
111 wxListBox::~wxListBox()
116 DisposeExtLDEFInfo( m_macList
) ;
121 void wxListBox::Free()
123 #if wxUSE_OWNER_DRAWN
124 if ( m_windowStyle
& wxLB_OWNERDRAW
)
126 size_t uiCount
= m_aItems
.Count();
127 while ( uiCount
-- != 0 ) {
128 delete m_aItems
[uiCount
];
134 #endif // wxUSE_OWNER_DRAWN
135 if ( HasClientObjectData() )
137 for ( size_t n
= 0; n
< (size_t)m_noItems
; n
++ )
139 delete GetClientObject(n
);
144 void wxListBox::DoSetFirstItem(int N
)
149 void wxListBox::Delete(int N
)
151 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
152 wxT("invalid index in wxListBox::Delete") );
154 #if wxUSE_OWNER_DRAWN
157 #else // !wxUSE_OWNER_DRAWN
158 if ( HasClientObjectData() )
160 delete GetClientObject(N
);
162 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
163 m_stringArray
.Remove( N
) ;
164 m_dataArray
.Remove( N
) ;
170 int wxListBox::DoAppend(const wxString
& item
)
172 int index
= m_noItems
;
173 if( wxApp::s_macDefaultEncodingIsPC
)
175 m_stringArray
.Add( wxMacMakeMacStringFromPC( item
) ) ;
176 m_dataArray
.Add( NULL
);
179 m_stringArray
.Add( item
) ;
180 m_dataArray
.Add( NULL
);
183 DoSetItemClientData( index
, NULL
) ;
189 void wxListBox::DoSetItems(const wxArrayString
& choices
, void** clientData
)
191 MacSetRedraw( false ) ;
193 int n
= choices
.GetCount();
195 for( int i
= 0 ; i
< n
; ++i
)
199 #if wxUSE_OWNER_DRAWN
200 wxASSERT_MSG(clientData
[i
] == NULL
,
201 wxT("Can't use client data with owner-drawn listboxes"));
202 #else // !wxUSE_OWNER_DRAWN
203 Append( choices
[i
] , clientData
[i
] ) ;
207 Append( choices
[i
] ) ;
210 #if wxUSE_OWNER_DRAWN
211 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
212 // first delete old items
213 size_t ui
= m_aItems
.Count();
214 while ( ui
-- != 0 ) {
219 // then create new ones
220 for ( ui
= 0; ui
< (size_t)m_noItems
; ui
++ ) {
221 wxOwnerDrawn
*pNewItem
= CreateItem(ui
);
222 pNewItem
->SetName(choices
[ui
]);
223 m_aItems
.Add(pNewItem
);
226 #endif // wxUSE_OWNER_DRAWN
227 MacSetRedraw( true ) ;
230 bool wxListBox::HasMultipleSelection() const
232 return (m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
);
235 int wxListBox::FindString(const wxString
& st
) const
238 if( wxApp::s_macDefaultEncodingIsPC
)
240 s
= wxMacMakeMacStringFromPC( st
) ;
245 if ( s
.Right(1) == "*" )
247 wxString search
= s
.Left( s
.Length() - 1 ) ;
248 int len
= search
.Length() ;
250 strcpy( (char*) s2
, search
.c_str() ) ;
251 c2pstr( (char*) s2
) ;
252 for ( int i
= 0 ; i
< m_noItems
; ++ i
)
254 strcpy( (char*) s1
, m_stringArray
[i
].Left( len
).c_str() ) ;
255 c2pstr( (char*) s1
) ;
256 if ( EqualString( s1
, s2
, false , false ) )
259 if ( s
.Left(1) = "*" && s
.Length() > 1 )
262 for ( int i
= 0 ; i
< m_noItems
; ++i
)
264 if ( GetString(i
).Lower().Matches(s
) )
273 strcpy( (char*) s2
, s
.c_str() ) ;
274 c2pstr( (char*) s2
) ;
275 for ( int i
= 0 ; i
< m_noItems
; ++ i
)
277 strcpy( (char*) s1
, m_stringArray
[i
].c_str() ) ;
278 c2pstr( (char*) s1
) ;
279 if ( EqualString( s1
, s2
, false , false ) )
286 void wxListBox::Clear()
290 m_stringArray
.Empty() ;
291 m_dataArray
.Empty() ;
295 void wxListBox::SetSelection(int N
, bool select
)
297 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
298 "invalid index in wxListBox::SetSelection" );
299 MacSetSelection( N
, select
) ;
302 bool wxListBox::IsSelected(int N
) const
304 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, FALSE
,
305 "invalid index in wxListBox::Selected" );
307 return MacIsSelected( N
) ;
310 void *wxListBox::DoGetItemClientData(int N
) const
312 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, NULL
,
313 "invalid index in wxListBox::GetClientData" );
315 return (void *)m_dataArray
[N
];
318 wxClientData
*wxListBox::DoGetItemClientObject(int N
) const
320 return (wxClientData
*) DoGetItemClientData( N
) ;
323 void wxListBox::DoSetItemClientData(int N
, void *Client_data
)
325 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
326 "invalid index in wxListBox::SetClientData" );
328 #if wxUSE_OWNER_DRAWN
329 if ( m_windowStyle
& wxLB_OWNERDRAW
)
331 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
332 // in OnMeasure/OnDraw.
333 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
335 #endif // wxUSE_OWNER_DRAWN
336 wxASSERT_MSG( m_dataArray
.GetCount() >= N
, "invalid client_data array" ) ;
338 if ( m_dataArray
.GetCount() > N
)
340 m_dataArray
[N
] = (char*) Client_data
;
344 m_dataArray
.Add( (char*) Client_data
) ;
348 void wxListBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
350 DoSetItemClientData(n
, clientData
);
353 // Return number of selections and an array of selected integers
354 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
356 return MacGetSelections( aSelections
) ;
359 if ((m_windowStyle & wxLB_MULTIMacE) || (m_windowStyle & wxLB_EXTENDED))
362 for ( int n = 0; n < no_sel; n++ )
367 else // single-selection listbox
376 // Get single selection, for single choice list items
377 int wxListBox::GetSelection() const
379 return MacGetSelection() ;
382 // Find string for position
383 wxString
wxListBox::GetString(int N
) const
385 if( wxApp::s_macDefaultEncodingIsPC
)
387 return wxMacMakePCStringFromMac( m_stringArray
[N
] ) ;
390 return m_stringArray
[N
] ;
393 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
395 wxCHECK_RET( pos
>= 0 && pos
<= m_noItems
,
396 wxT("invalid index in wxListBox::InsertItems") );
398 int nItems
= items
.GetCount();
400 for ( int i
= 0 ; i
< nItems
; i
++ )
402 m_stringArray
.Insert( items
[i
] , pos
+ i
) ;
403 m_dataArray
.Insert( NULL
, pos
+ i
) ;
404 MacInsert( pos
+ i
, items
[i
] ) ;
410 void wxListBox::SetString(int N
, const wxString
& s
)
413 if( wxApp::s_macDefaultEncodingIsPC
)
415 str
= wxMacMakeMacStringFromPC( s
) ;
419 m_stringArray
[N
] = str
;
423 wxSize
wxListBox::DoGetBestSize() const
425 return wxSize(100, 100);
428 int wxListBox::GetCount() const
433 void wxListBox::SetupColours()
435 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
436 SetForegroundColour(GetParent()->GetForegroundColour());
439 #if wxUSE_OWNER_DRAWN
441 class wxListBoxItem
: public wxOwnerDrawn
444 wxListBoxItem(const wxString
& str
= "");
447 wxListBoxItem::wxListBoxItem(const wxString
& str
) : wxOwnerDrawn(str
, FALSE
)
449 // no bitmaps/checkmarks
453 wxOwnerDrawn
*wxListBox::CreateItem(size_t n
)
455 return new wxListBoxItem();
458 #endif //USE_OWNER_DRAWN
460 // ============================================================================
461 // list box control implementation
462 // ============================================================================
464 void MacDrawStringCell(Rect
*cellRect
, Cell lCell
, ListHandle theList
, long refCon
)
467 // typecast our refCon
468 list
= (wxListBox
*)refCon
;
470 MoveTo(cellRect
->left
+ 4 , cellRect
->top
+ 10 );
471 const wxString text
= list
->m_stringArray
[lCell
.v
] ;
472 ::TextFont( kFontIDMonaco
) ;
475 DrawText(text
, 0 , text
.Length());
479 void wxListBox::MacDelete( int N
)
483 Cell cell
= { 0 , 0 } ;
484 UMAGetControlData( m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
, sizeof( ListHandle
) , (char*) &list
, &result
) ;
485 LDelRow( 1 , N
, list
) ;
488 void wxListBox::MacInsert( int n
, const char * text
)
495 LAddRow( 1 , cell
.v
, m_macList
) ;
498 void wxListBox::MacAppend( const char * text
)
500 Cell cell
= { 0 , 0 } ;
501 cell
.v
= (**m_macList
).dataBounds
.bottom
;
502 LAddRow( 1 , cell
.v
, m_macList
) ;
505 void wxListBox::MacClear()
507 LDelRow( (**m_macList
).dataBounds
.bottom
, 0 , m_macList
) ;
510 void wxListBox::MacSetSelection( int n
, bool select
)
512 Cell cell
= { 0 , 0 } ;
513 if ( LGetSelect( TRUE
, &cell
, m_macList
) )
515 LSetSelect( false , cell
, m_macList
) ;
519 LSetSelect( select
, cell
, m_macList
) ;
520 LAutoScroll( m_macList
) ;
523 bool wxListBox::MacIsSelected( int n
) const
525 Cell cell
= { 0 , 0 } ;
527 return LGetSelect( false , &cell
, m_macList
) ;
530 void wxListBox::MacDestroy()
532 // DisposeExtLDEFInfo( m_macList ) ;
535 int wxListBox::MacGetSelection() const
537 Cell cell
= { 0 , 0 } ;
538 if ( LGetSelect( true , &cell
, m_macList
) )
544 int wxListBox::MacGetSelections( wxArrayInt
& aSelections
) const
550 Cell cell
= { 0 , 0 } ;
553 while ( LGetSelect( true , &cell
, m_macList
) )
555 aSelections
.Add( cell
.v
) ;
562 void wxListBox::MacSet( int n
, const char * text
)
564 // our implementation does not store anything in the list
565 // so we just have to redraw
566 Cell cell
= { 0 , 0 } ;
568 LDraw( cell
, m_macList
) ;
571 void wxListBox::MacScrollTo( int n
)
573 // TODO implement scrolling
576 void wxListBox::OnSize( const wxSizeEvent
&event
)
578 Point pt
= (**m_macList
).cellSize
;
579 pt
.h
= m_width
- 15 ;
580 LCellSize( pt
, m_macList
) ;
583 void wxListBox::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
585 Boolean wasDoubleClick
= false ;
588 UMAGetControlData( m_macControl
, kControlNoPart
, kControlListBoxDoubleClickTag
, sizeof( wasDoubleClick
) , (char*) &wasDoubleClick
, &result
) ;
589 if ( !wasDoubleClick
)
599 void wxListBox::MacSetRedraw( bool doDraw
)
601 LSetDrawingMode( doDraw
, m_macList
) ;
605 void wxListBox::MacDoClick()
607 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, m_windowId
);
608 event
.SetEventObject( this );
610 wxArrayInt aSelections
;
611 int n
, count
= GetSelections(aSelections
);
615 if ( HasClientObjectData() )
616 event
.SetClientObject( GetClientObject(n
) );
617 else if ( HasClientUntypedData() )
618 event
.SetClientData( GetClientData(n
) );
619 event
.SetString( GetString(n
) );
626 event
.m_commandInt
= n
;
628 GetEventHandler()->ProcessEvent(event
);
631 void wxListBox::MacDoDoubleClick()
633 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, m_windowId
);
634 event
.SetEventObject( this );
635 GetEventHandler()->ProcessEvent(event
) ;