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
);
188 void wxListBox::DoSetItems(const wxArrayString
& choices
, void** clientData
)
190 MacSetRedraw( false ) ;
192 int n
= choices
.GetCount();
194 for( int i
= 0 ; i
< n
; ++i
)
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
] ) ;
206 Append( choices
[i
] ) ;
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 ) {
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
);
225 #endif // wxUSE_OWNER_DRAWN
226 MacSetRedraw( true ) ;
229 bool wxListBox::HasMultipleSelection() const
231 return (m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
);
234 int wxListBox::FindString(const wxString
& st
) const
237 if( wxApp::s_macDefaultEncodingIsPC
)
239 s
= wxMacMakeMacStringFromPC( st
) ;
244 if ( s
.Right(1) == "*" )
246 wxString search
= s
.Left( s
.Length() - 1 ) ;
247 int len
= search
.Length() ;
249 strcpy( (char*) s2
, search
.c_str() ) ;
250 c2pstr( (char*) s2
) ;
251 for ( int i
= 0 ; i
< m_noItems
; ++ i
)
253 strcpy( (char*) s1
, m_stringArray
[i
].Left( len
).c_str() ) ;
254 c2pstr( (char*) s1
) ;
255 if ( EqualString( s1
, s2
, false , false ) )
262 strcpy( (char*) s2
, s
.c_str() ) ;
263 c2pstr( (char*) s2
) ;
264 for ( int i
= 0 ; i
< m_noItems
; ++ i
)
266 strcpy( (char*) s1
, m_stringArray
[i
].c_str() ) ;
267 c2pstr( (char*) s1
) ;
268 if ( EqualString( s1
, s2
, false , false ) )
275 void wxListBox::Clear()
279 m_stringArray
.Empty() ;
280 m_dataArray
.Empty() ;
284 void wxListBox::SetSelection(int N
, bool select
)
286 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
287 "invalid index in wxListBox::SetSelection" );
288 MacSetSelection( N
, select
) ;
291 bool wxListBox::IsSelected(int N
) const
293 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, FALSE
,
294 "invalid index in wxListBox::Selected" );
296 return MacIsSelected( N
) ;
299 void *wxListBox::DoGetItemClientData(int N
) const
301 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, NULL
,
302 "invalid index in wxListBox::GetClientData" );
304 return (void *)m_dataArray
[N
];
307 wxClientData
*wxListBox::DoGetItemClientObject(int N
) const
309 return (wxClientData
*) DoGetItemClientData( N
) ;
312 void wxListBox::DoSetItemClientData(int N
, void *Client_data
)
314 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
315 "invalid index in wxListBox::SetClientData" );
317 #if wxUSE_OWNER_DRAWN
318 if ( m_windowStyle
& wxLB_OWNERDRAW
)
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"));
324 #endif // wxUSE_OWNER_DRAWN
325 wxASSERT_MSG( m_dataArray
.GetCount() >= N
, "invalid client_data array" ) ;
327 if ( m_dataArray
.GetCount() > N
)
329 m_dataArray
[N
] = (char*) Client_data
;
333 m_dataArray
.Add( (char*) Client_data
) ;
337 void wxListBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
339 DoSetItemClientData(n
, clientData
);
342 // Return number of selections and an array of selected integers
343 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
345 return MacGetSelections( aSelections
) ;
348 if ((m_windowStyle & wxLB_MULTIMacE) || (m_windowStyle & wxLB_EXTENDED))
351 for ( int n = 0; n < no_sel; n++ )
356 else // single-selection listbox
365 // Get single selection, for single choice list items
366 int wxListBox::GetSelection() const
368 return MacGetSelection() ;
371 // Find string for position
372 wxString
wxListBox::GetString(int N
) const
374 if( wxApp::s_macDefaultEncodingIsPC
)
376 return wxMacMakePCStringFromMac( m_stringArray
[N
] ) ;
379 return m_stringArray
[N
] ;
382 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
384 wxCHECK_RET( pos
>= 0 && pos
<= m_noItems
,
385 wxT("invalid index in wxListBox::InsertItems") );
387 int nItems
= items
.GetCount();
389 for ( int i
= 0 ; i
< nItems
; i
++ )
391 m_stringArray
.Insert( items
[i
] , pos
+ i
) ;
392 m_dataArray
.Insert( NULL
, pos
+ i
) ;
393 MacInsert( pos
+ i
, items
[i
] ) ;
399 void wxListBox::SetString(int N
, const wxString
& s
)
402 if( wxApp::s_macDefaultEncodingIsPC
)
404 str
= wxMacMakeMacStringFromPC( s
) ;
408 m_stringArray
[N
] = str
;
412 wxSize
wxListBox::DoGetBestSize() const
414 return wxSize(100, 100);
417 int wxListBox::GetCount() const
422 void wxListBox::SetupColours()
424 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
425 SetForegroundColour(GetParent()->GetForegroundColour());
428 #if wxUSE_OWNER_DRAWN
430 class wxListBoxItem
: public wxOwnerDrawn
433 wxListBoxItem(const wxString
& str
= "");
436 wxListBoxItem::wxListBoxItem(const wxString
& str
) : wxOwnerDrawn(str
, FALSE
)
438 // no bitmaps/checkmarks
442 wxOwnerDrawn
*wxListBox::CreateItem(size_t n
)
444 return new wxListBoxItem();
447 #endif //USE_OWNER_DRAWN
449 // ============================================================================
450 // list box control implementation
451 // ============================================================================
453 void MacDrawStringCell(Rect
*cellRect
, Cell lCell
, ListHandle theList
, long refCon
)
456 // typecast our refCon
457 list
= (wxListBox
*)refCon
;
459 MoveTo(cellRect
->left
+ 4 , cellRect
->top
+ 10 );
460 const wxString text
= list
->m_stringArray
[lCell
.v
] ;
461 ::TextFont( kFontIDMonaco
) ;
464 DrawText(text
, 0 , text
.Length());
468 void wxListBox::MacDelete( int N
)
472 Cell cell
= { 0 , 0 } ;
473 UMAGetControlData( m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
, sizeof( ListHandle
) , (char*) &list
, &result
) ;
474 LDelRow( 1 , N
, list
) ;
477 void wxListBox::MacInsert( int n
, const char * text
)
484 LAddRow( 1 , cell
.v
, m_macList
) ;
487 void wxListBox::MacAppend( const char * text
)
489 Cell cell
= { 0 , 0 } ;
490 cell
.v
= (**m_macList
).dataBounds
.bottom
;
491 LAddRow( 1 , cell
.v
, m_macList
) ;
494 void wxListBox::MacClear()
496 LDelRow( (**m_macList
).dataBounds
.bottom
, 0 , m_macList
) ;
499 void wxListBox::MacSetSelection( int n
, bool select
)
501 Cell cell
= { 0 , 0 } ;
502 if ( LGetSelect( TRUE
, &cell
, m_macList
) )
504 LSetSelect( false , cell
, m_macList
) ;
508 LSetSelect( select
, cell
, m_macList
) ;
509 LAutoScroll( m_macList
) ;
512 bool wxListBox::MacIsSelected( int n
) const
514 Cell cell
= { 0 , 0 } ;
516 return LGetSelect( false , &cell
, m_macList
) ;
519 void wxListBox::MacDestroy()
521 // DisposeExtLDEFInfo( m_macList ) ;
524 int wxListBox::MacGetSelection() const
526 Cell cell
= { 0 , 0 } ;
527 if ( LGetSelect( true , &cell
, m_macList
) )
533 int wxListBox::MacGetSelections( wxArrayInt
& aSelections
) const
539 Cell cell
= { 0 , 0 } ;
542 while ( LGetSelect( true , &cell
, m_macList
) )
544 aSelections
.Add( cell
.v
) ;
551 void wxListBox::MacSet( int n
, const char * text
)
553 // our implementation does not store anything in the list
554 // so we just have to redraw
555 Cell cell
= { 0 , 0 } ;
557 LDraw( cell
, m_macList
) ;
560 void wxListBox::MacScrollTo( int n
)
562 // TODO implement scrolling
565 void wxListBox::OnSize( const wxSizeEvent
&event
)
567 Point pt
= (**m_macList
).cellSize
;
568 pt
.h
= m_width
- 15 ;
569 LCellSize( pt
, m_macList
) ;
572 void wxListBox::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
574 Boolean wasDoubleClick
= false ;
577 UMAGetControlData( m_macControl
, kControlNoPart
, kControlListBoxDoubleClickTag
, sizeof( wasDoubleClick
) , (char*) &wasDoubleClick
, &result
) ;
578 if ( !wasDoubleClick
)
588 void wxListBox::MacSetRedraw( bool doDraw
)
590 LSetDrawingMode( doDraw
, m_macList
) ;
594 void wxListBox::MacDoClick()
596 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, m_windowId
);
597 event
.SetEventObject( this );
599 wxArrayInt aSelections
;
600 int n
, count
= GetSelections(aSelections
);
604 if ( HasClientObjectData() )
605 event
.SetClientObject( GetClientObject(n
) );
606 else if ( HasClientUntypedData() )
607 event
.SetClientData( GetClientData(n
) );
608 event
.SetString( GetString(n
) );
615 event
.m_commandInt
= n
;
617 GetEventHandler()->ProcessEvent(event
);
620 void wxListBox::MacDoDoubleClick()
622 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, m_windowId
);
623 event
.SetEventObject( this );
624 GetEventHandler()->ProcessEvent(event
) ;