1 ///////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "listbox.h"
17 #include "wx/listbox.h"
18 #include "wx/settings.h"
19 #include "wx/dynarray.h"
24 #include "ldef/extldef.h"
29 #if !USE_SHARED_LIBRARY
30 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControl
)
32 BEGIN_EVENT_TABLE(wxListBox
, wxControl
)
33 EVT_SIZE( wxListBox::OnSize
)
37 #include <wx/mac/uma.h>
39 extern "C" void MacDrawStringCell(Rect
*cellRect
, Cell lCell
, ListHandle theList
, long refCon
) ;
40 const short kwxMacListWithVerticalScrollbar
= 128 ;
42 // ============================================================================
43 // list box control implementation
44 // ============================================================================
47 wxListBox::wxListBox()
54 bool wxListBox::Create(wxWindow
*parent
, wxWindowID id
,
57 int n
, const wxString choices
[],
59 const wxValidator
& validator
,
62 m_noItems
= 0 ; // this will be increased by our append command
68 MacPreControlCreate( parent
, id
, "" , pos
, size
,style
, validator
, name
, &bounds
, title
) ;
70 m_macControl
= UMANewControl( parent
->GetMacRootWindow() , &bounds
, title
, false , kwxMacListWithVerticalScrollbar
, 0 , 0,
71 kControlListBoxProc
, (long) this ) ;
74 UMAGetControlData( m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
, sizeof( ListHandle
) , (char*) &m_macList
, &result
) ;
76 HLock( (Handle
) m_macList
) ;
77 NewExtLDEFInfo( m_macList
, MacDrawStringCell
, (long) this ) ;
78 (**m_macList
).selFlags
= 0 ;
79 if ( style
& wxLB_MULTIPLE
)
81 (**m_macList
).selFlags
+= lNoExtend
;
83 else if ( style
& wxLB_EXTENDED
)
85 (**m_macList
).selFlags
+= lExtendDrag
;
89 (**m_macList
).selFlags
= lOnlyOne
;
91 Point pt
= (**m_macList
).cellSize
;
93 LCellSize( pt
, m_macList
) ;
95 LAddColumn( 1 , 0 , m_macList
) ;
97 MacPostControlCreate() ;
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
) ;
106 for ( int i
= 0 ; i
< n
; i
++ )
108 Append( choices
[i
] ) ;
111 LSetDrawingMode( true , m_macList
) ;
116 wxListBox::~wxListBox()
121 DisposeExtLDEFInfo( m_macList
) ;
126 void wxListBox::Free()
128 #if wxUSE_OWNER_DRAWN
129 if ( m_windowStyle
& wxLB_OWNERDRAW
)
131 size_t uiCount
= m_aItems
.Count();
132 while ( uiCount
-- != 0 ) {
133 delete m_aItems
[uiCount
];
139 #endif // wxUSE_OWNER_DRAWN
140 if ( HasClientObjectData() )
142 for ( size_t n
= 0; n
< (size_t)m_noItems
; n
++ )
144 delete GetClientObject(n
);
149 void wxListBox::DoSetFirstItem(int N
)
154 void wxListBox::Delete(int N
)
156 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
157 wxT("invalid index in wxListBox::Delete") );
159 #if wxUSE_OWNER_DRAWN
162 #else // !wxUSE_OWNER_DRAWN
163 if ( HasClientObjectData() )
165 delete GetClientObject(N
);
167 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
168 m_stringArray
.Remove( N
) ;
169 m_dataArray
.Remove( N
) ;
175 int wxListBox::DoAppend(const wxString
& item
)
177 int index
= m_noItems
;
178 if( wxApp::s_macDefaultEncodingIsPC
)
180 m_stringArray
.Add( wxMacMakeMacStringFromPC( item
) ) ;
181 m_dataArray
.Add( NULL
);
184 m_stringArray
.Add( item
) ;
185 m_dataArray
.Add( NULL
);
188 DoSetItemClientData( index
, NULL
) ;
194 void wxListBox::DoSetItems(const wxArrayString
& choices
, void** clientData
)
196 MacSetRedraw( false ) ;
198 int n
= choices
.GetCount();
200 for( int i
= 0 ; i
< n
; ++i
)
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
] ) ;
212 Append( choices
[i
] ) ;
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 ) {
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
);
231 #endif // wxUSE_OWNER_DRAWN
232 MacSetRedraw( true ) ;
235 bool wxListBox::HasMultipleSelection() const
237 return (m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
);
240 int wxListBox::FindString(const wxString
& st
) const
243 if( wxApp::s_macDefaultEncodingIsPC
)
245 s
= wxMacMakeMacStringFromPC( st
) ;
250 if ( s
.Right(1) == "*" )
252 wxString search
= s
.Left( s
.Length() - 1 ) ;
253 int len
= search
.Length() ;
257 c2pstrcpy( (StringPtr
) s2
, search
.c_str() ) ;
259 strcpy( (char *) s2
, search
.c_str() ) ;
260 c2pstr( (char *) s2
) ;
263 for ( int i
= 0 ; i
< m_noItems
; ++ i
)
266 c2pstrcpy( (StringPtr
) s1
, m_stringArray
[i
].Left( len
).c_str() ) ;
268 strcpy( (char *) s1
, m_stringArray
[i
].Left( len
).c_str() ) ;
269 c2pstr( (char *) s1
) ;
271 if ( EqualString( s1
, s2
, false , false ) )
274 if ( s
.Left(1) = "*" && s
.Length() > 1 )
277 for ( int i
= 0 ; i
< m_noItems
; ++i
)
279 if ( GetString(i
).Lower().Matches(s
) )
290 c2pstrcpy( (StringPtr
) s2
, s
.c_str() ) ;
292 strcpy( (char *) s2
, s
.c_str() ) ;
293 c2pstr( (char *) s2
) ;
296 for ( int i
= 0 ; i
< m_noItems
; ++ i
)
299 c2pstrcpy( (StringPtr
) s1
, m_stringArray
[i
].c_str() ) ;
301 strcpy( (char *) s1
, m_stringArray
[i
].c_str() ) ;
302 c2pstr( (char *) s1
) ;
304 if ( EqualString( s1
, s2
, false , false ) )
311 void wxListBox::Clear()
315 m_stringArray
.Empty() ;
316 m_dataArray
.Empty() ;
320 void wxListBox::SetSelection(int N
, bool select
)
322 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
323 "invalid index in wxListBox::SetSelection" );
324 MacSetSelection( N
, select
) ;
327 bool wxListBox::IsSelected(int N
) const
329 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, FALSE
,
330 "invalid index in wxListBox::Selected" );
332 return MacIsSelected( N
) ;
335 void *wxListBox::DoGetItemClientData(int N
) const
337 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, NULL
,
338 "invalid index in wxListBox::GetClientData" );
340 return (void *)m_dataArray
[N
];
343 wxClientData
*wxListBox::DoGetItemClientObject(int N
) const
345 return (wxClientData
*) DoGetItemClientData( N
) ;
348 void wxListBox::DoSetItemClientData(int N
, void *Client_data
)
350 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
351 "invalid index in wxListBox::SetClientData" );
353 #if wxUSE_OWNER_DRAWN
354 if ( m_windowStyle
& wxLB_OWNERDRAW
)
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"));
360 #endif // wxUSE_OWNER_DRAWN
361 wxASSERT_MSG( m_dataArray
.GetCount() >= N
, "invalid client_data array" ) ;
363 if ( m_dataArray
.GetCount() > N
)
365 m_dataArray
[N
] = (char*) Client_data
;
369 m_dataArray
.Add( (char*) Client_data
) ;
373 void wxListBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
375 DoSetItemClientData(n
, clientData
);
378 // Return number of selections and an array of selected integers
379 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
381 return MacGetSelections( aSelections
) ;
384 if ((m_windowStyle & wxLB_MULTIMacE) || (m_windowStyle & wxLB_EXTENDED))
387 for ( int n = 0; n < no_sel; n++ )
392 else // single-selection listbox
401 // Get single selection, for single choice list items
402 int wxListBox::GetSelection() const
404 return MacGetSelection() ;
407 // Find string for position
408 wxString
wxListBox::GetString(int N
) const
410 if( wxApp::s_macDefaultEncodingIsPC
)
412 return wxMacMakePCStringFromMac( m_stringArray
[N
] ) ;
415 return m_stringArray
[N
] ;
418 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
420 wxCHECK_RET( pos
>= 0 && pos
<= m_noItems
,
421 wxT("invalid index in wxListBox::InsertItems") );
423 int nItems
= items
.GetCount();
425 for ( int i
= 0 ; i
< nItems
; i
++ )
427 m_stringArray
.Insert( items
[i
] , pos
+ i
) ;
428 m_dataArray
.Insert( NULL
, pos
+ i
) ;
429 MacInsert( pos
+ i
, items
[i
] ) ;
435 void wxListBox::SetString(int N
, const wxString
& s
)
438 if( wxApp::s_macDefaultEncodingIsPC
)
440 str
= wxMacMakeMacStringFromPC( s
) ;
444 m_stringArray
[N
] = str
;
448 wxSize
wxListBox::DoGetBestSize() const
450 return wxSize(100, 100);
453 int wxListBox::GetCount() const
458 void wxListBox::SetupColours()
460 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
461 SetForegroundColour(GetParent()->GetForegroundColour());
464 #if wxUSE_OWNER_DRAWN
466 class wxListBoxItem
: public wxOwnerDrawn
469 wxListBoxItem(const wxString
& str
= "");
472 wxListBoxItem::wxListBoxItem(const wxString
& str
) : wxOwnerDrawn(str
, FALSE
)
474 // no bitmaps/checkmarks
478 wxOwnerDrawn
*wxListBox::CreateItem(size_t n
)
480 return new wxListBoxItem();
483 #endif //USE_OWNER_DRAWN
485 // ============================================================================
486 // list box control implementation
487 // ============================================================================
489 void MacDrawStringCell(Rect
*cellRect
, Cell lCell
, ListHandle theList
, long refCon
)
492 // typecast our refCon
493 list
= (wxListBox
*)refCon
;
495 MoveTo(cellRect
->left
+ 4 , cellRect
->top
+ 10 );
496 const wxString text
= list
->m_stringArray
[lCell
.v
] ;
497 ::TextFont( kFontIDMonaco
) ;
500 DrawText(text
, 0 , text
.Length());
504 void wxListBox::MacDelete( int N
)
508 UMAGetControlData( m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
, sizeof( ListHandle
) , (char*) &list
, &result
) ;
509 LDelRow( 1 , N
, list
) ;
512 void wxListBox::MacInsert( int n
, const char * text
)
519 LAddRow( 1 , cell
.v
, m_macList
) ;
522 void wxListBox::MacAppend( const char * text
)
524 Cell cell
= { 0 , 0 } ;
525 cell
.v
= (**m_macList
).dataBounds
.bottom
;
526 LAddRow( 1 , cell
.v
, m_macList
) ;
529 void wxListBox::MacClear()
531 LDelRow( (**m_macList
).dataBounds
.bottom
, 0 , m_macList
) ;
534 void wxListBox::MacSetSelection( int n
, bool select
)
536 Cell cell
= { 0 , 0 } ;
537 if ( LGetSelect( TRUE
, &cell
, m_macList
) )
539 LSetSelect( false , cell
, m_macList
) ;
543 LSetSelect( select
, cell
, m_macList
) ;
544 LAutoScroll( m_macList
) ;
547 bool wxListBox::MacIsSelected( int n
) const
549 Cell cell
= { 0 , 0 } ;
551 return LGetSelect( false , &cell
, m_macList
) ;
554 void wxListBox::MacDestroy()
556 // DisposeExtLDEFInfo( m_macList ) ;
559 int wxListBox::MacGetSelection() const
561 Cell cell
= { 0 , 0 } ;
562 if ( LGetSelect( true , &cell
, m_macList
) )
568 int wxListBox::MacGetSelections( wxArrayInt
& aSelections
) const
574 Cell cell
= { 0 , 0 } ;
577 while ( LGetSelect( true , &cell
, m_macList
) )
579 aSelections
.Add( cell
.v
) ;
586 void wxListBox::MacSet( int n
, const char * text
)
588 // our implementation does not store anything in the list
589 // so we just have to redraw
590 Cell cell
= { 0 , 0 } ;
592 LDraw( cell
, m_macList
) ;
595 void wxListBox::MacScrollTo( int n
)
597 // TODO implement scrolling
600 void wxListBox::OnSize( const wxSizeEvent
&event
)
602 Point pt
= (**m_macList
).cellSize
;
603 pt
.h
= m_width
- 15 ;
604 LCellSize( pt
, m_macList
) ;
607 void wxListBox::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
609 Boolean wasDoubleClick
= false ;
612 UMAGetControlData( m_macControl
, kControlNoPart
, kControlListBoxDoubleClickTag
, sizeof( wasDoubleClick
) , (char*) &wasDoubleClick
, &result
) ;
613 if ( !wasDoubleClick
)
623 void wxListBox::MacSetRedraw( bool doDraw
)
625 LSetDrawingMode( doDraw
, m_macList
) ;
629 void wxListBox::MacDoClick()
631 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, m_windowId
);
632 event
.SetEventObject( this );
634 wxArrayInt aSelections
;
635 int n
, count
= GetSelections(aSelections
);
639 if ( HasClientObjectData() )
640 event
.SetClientObject( GetClientObject(n
) );
641 else if ( HasClientUntypedData() )
642 event
.SetClientData( GetClientData(n
) );
643 event
.SetString( GetString(n
) );
650 event
.m_commandInt
= n
;
652 GetEventHandler()->ProcessEvent(event
);
655 void wxListBox::MacDoDoubleClick()
657 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, m_windowId
);
658 event
.SetEventObject( this );
659 GetEventHandler()->ProcessEvent(event
) ;