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 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControl
)
26 BEGIN_EVENT_TABLE(wxListBox
, wxControl
)
27 EVT_SIZE( wxListBox::OnSize
)
30 #include <wx/mac/uma.h>
32 extern "C" void MacDrawStringCell(Rect
*cellRect
, Cell lCell
, ListHandle theList
, long refCon
) ;
33 const short kwxMacListWithVerticalScrollbar
= 128 ;
35 // ============================================================================
36 // list box control implementation
37 // ============================================================================
40 wxListBox::wxListBox()
46 bool wxListBox::Create(wxWindow
*parent
, wxWindowID id
,
49 int n
, const wxString choices
[],
51 const wxValidator
& validator
,
54 m_noItems
= 0 ; // this will be increased by our append command
59 m_macHorizontalBorder
= 5 ; // additional pixels around the real control
60 m_macVerticalBorder
= 5 ;
62 MacPreControlCreate( parent
, id
, "" , pos
, size
,style
, validator
, name
, &bounds
, title
) ;
64 m_macControl
= UMANewControl( parent
->GetMacRootWindow() , &bounds
, title
, true , kwxMacListWithVerticalScrollbar
, 0 , 0,
65 kControlListBoxProc
, (long) this ) ;
68 UMAGetControlData( m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
, sizeof( ListHandle
) , (char*) &m_macList
, &result
) ;
70 NewExtLDEFInfo( m_macList
, MacDrawStringCell
, (long) this ) ;
71 (**m_macList
).selFlags
= 0 ;
72 if ( style
& wxLB_MULTIPLE
)
74 (**m_macList
).selFlags
+= lNoExtend
;
76 else if ( style
& wxLB_EXTENDED
)
78 (**m_macList
).selFlags
+= lExtendDrag
;
82 (**m_macList
).selFlags
= lOnlyOne
;
84 Point pt
= (**m_macList
).cellSize
;
86 LCellSize( pt
, m_macList
) ;
88 LAddColumn( 1 , 0 , m_macList
) ;
90 MacPostControlCreate() ;
92 ControlFontStyleRec controlstyle
;
93 controlstyle
.flags
= kControlUseFontMask
+ kControlUseSizeMask
;
94 //controlstyle.font = kControlFontSmallSystemFont ;
95 controlstyle
.font
= kFontIDMonaco
;
96 controlstyle
.size
= 9 ;
97 ::UMASetControlFontStyle( m_macControl
, &controlstyle
) ;
99 for ( int i
= 0 ; i
< n
; i
++ )
101 Append( choices
[i
] ) ;
104 LSetDrawingMode( true , m_macList
) ;
109 wxListBox::~wxListBox()
112 DisposeExtLDEFInfo( m_macList
) ;
115 void wxListBox::Free()
117 #if wxUSE_OWNER_DRAWN
118 if ( m_windowStyle
& wxLB_OWNERDRAW
)
120 size_t uiCount
= m_aItems
.Count();
121 while ( uiCount
-- != 0 ) {
122 delete m_aItems
[uiCount
];
128 #endif // wxUSE_OWNER_DRAWN
129 if ( HasClientObjectData() )
131 for ( size_t n
= 0; n
< (size_t)m_noItems
; n
++ )
133 delete GetClientObject(n
);
138 void wxListBox::DoSetFirstItem(int N
)
143 void wxListBox::Delete(int N
)
145 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
146 wxT("invalid index in wxListBox::Delete") );
148 #if wxUSE_OWNER_DRAWN
151 #else // !wxUSE_OWNER_DRAWN
152 if ( HasClientObjectData() )
154 delete GetClientObject(N
);
156 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
157 m_stringArray
.Remove( N
) ;
158 m_dataArray
.Remove( N
) ;
164 int wxListBox::DoAppend(const wxString
& item
)
166 int index
= m_noItems
;
167 if( wxApp::s_macDefaultEncodingIsPC
)
169 m_stringArray
.Add( wxMacMakeMacStringFromPC( item
) ) ;
172 m_stringArray
.Add( item
) ;
179 void wxListBox::DoSetItems(const wxArrayString
& choices
, void** clientData
)
181 MacSetRedraw( false ) ;
183 int n
= choices
.GetCount();
185 for( int i
= 0 ; i
< n
; ++i
)
189 #if wxUSE_OWNER_DRAWN
190 wxASSERT_MSG(clientData
[i
] == NULL
,
191 wxT("Can't use client data with owner-drawn listboxes"));
192 #else // !wxUSE_OWNER_DRAWN
193 Append( choices
[i
] , clientData
[i
] ) ;
197 Append( choices
[i
] ) ;
200 #if wxUSE_OWNER_DRAWN
201 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
202 // first delete old items
203 size_t ui
= m_aItems
.Count();
204 while ( ui
-- != 0 ) {
209 // then create new ones
210 for ( ui
= 0; ui
< (size_t)m_noItems
; ui
++ ) {
211 wxOwnerDrawn
*pNewItem
= CreateItem(ui
);
212 pNewItem
->SetName(choices
[ui
]);
213 m_aItems
.Add(pNewItem
);
216 #endif // wxUSE_OWNER_DRAWN
217 MacSetRedraw( true ) ;
220 bool wxListBox::HasMultipleSelection() const
222 return (m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
);
225 int wxListBox::FindString(const wxString
& st
) const
228 if( wxApp::s_macDefaultEncodingIsPC
)
230 s
= wxMacMakeMacStringFromPC( st
) ;
235 if ( s
.Right(1) == "*" )
237 wxString search
= s
.Left( s
.Length() - 1 ) ;
238 int len
= search
.Length() ;
239 for ( int i
= 0 ; i
< m_noItems
; ++ i
)
241 if ( equalstring( m_stringArray
[i
].Left( len
) , search
, false , false ) )
247 for ( int i
= 0 ; i
< m_noItems
; ++ i
)
249 if ( equalstring( m_stringArray
[i
] , s
, false , false ) )
256 void wxListBox::Clear()
260 m_stringArray
.Empty() ;
261 m_dataArray
.Empty() ;
265 void wxListBox::SetSelection(int N
, bool select
)
267 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
268 "invalid index in wxListBox::SetSelection" );
269 MacSetSelection( N
, select
) ;
272 bool wxListBox::IsSelected(int N
) const
274 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, FALSE
,
275 "invalid index in wxListBox::Selected" );
277 return MacIsSelected( N
) ;
280 void *wxListBox::DoGetItemClientData(int N
) const
282 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, NULL
,
283 "invalid index in wxListBox::GetClientData" );
285 return (void *)m_dataArray
[N
];
288 wxClientData
*wxListBox::DoGetItemClientObject(int N
) const
290 return (wxClientData
*) DoGetItemClientData( N
) ;
293 void wxListBox::DoSetItemClientData(int N
, void *Client_data
)
295 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
296 "invalid index in wxListBox::SetClientData" );
298 #if wxUSE_OWNER_DRAWN
299 if ( m_windowStyle
& wxLB_OWNERDRAW
)
301 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
302 // in OnMeasure/OnDraw.
303 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
305 #endif // wxUSE_OWNER_DRAWN
306 wxASSERT_MSG( m_dataArray
.GetCount() >= N
, "invalid client_data array" ) ;
308 if ( m_dataArray
.GetCount() > N
)
310 m_dataArray
[N
] = (char*) Client_data
;
314 m_dataArray
.Add( (char*) Client_data
) ;
318 void wxListBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
320 DoSetItemClientData(n
, clientData
);
323 // Return number of selections and an array of selected integers
324 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
326 return MacGetSelections( aSelections
) ;
329 if ((m_windowStyle & wxLB_MULTIMacE) || (m_windowStyle & wxLB_EXTENDED))
332 for ( int n = 0; n < no_sel; n++ )
337 else // single-selection listbox
346 // Get single selection, for single choice list items
347 int wxListBox::GetSelection() const
349 return MacGetSelection() ;
352 // Find string for position
353 wxString
wxListBox::GetString(int N
) const
355 if( wxApp::s_macDefaultEncodingIsPC
)
357 return wxMacMakePCStringFromMac( m_stringArray
[N
] ) ;
360 return m_stringArray
[N
] ;
363 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
365 wxCHECK_RET( pos
>= 0 && pos
<= m_noItems
,
366 wxT("invalid index in wxListBox::InsertItems") );
368 int nItems
= items
.GetCount();
370 for ( int i
= 0 ; i
< nItems
; i
++ )
372 m_stringArray
.Insert( items
[i
] , pos
+ i
) ;
373 m_dataArray
.Insert( NULL
, pos
+ i
) ;
374 MacInsert( pos
+ i
, items
[i
] ) ;
380 void wxListBox::SetString(int N
, const wxString
& s
)
382 m_stringArray
[N
] = s
;
386 wxSize
wxListBox::DoGetBestSize() const
388 return wxSize(100, 100);
391 int wxListBox::GetCount() const
396 void wxListBox::SetupColours()
398 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
399 SetForegroundColour(GetParent()->GetForegroundColour());
402 #if wxUSE_OWNER_DRAWN
404 class wxListBoxItem
: public wxOwnerDrawn
407 wxListBoxItem(const wxString
& str
= "");
410 wxListBoxItem::wxListBoxItem(const wxString
& str
) : wxOwnerDrawn(str
, FALSE
)
412 // no bitmaps/checkmarks
416 wxOwnerDrawn
*wxListBox::CreateItem(size_t n
)
418 return new wxListBoxItem();
421 #endif //USE_OWNER_DRAWN
423 // ============================================================================
424 // list box control implementation
425 // ============================================================================
427 void MacDrawStringCell(Rect
*cellRect
, Cell lCell
, ListHandle theList
, long refCon
)
430 // typecast our refCon
431 list
= (wxListBox
*)refCon
;
433 MoveTo(cellRect
->left
+ 4 , cellRect
->top
+ 10 );
434 const wxString text
= list
->m_stringArray
[lCell
.v
] ;
435 ::TextFont( kFontIDMonaco
) ;
438 DrawText(text
, 0 , text
.Length());
442 void wxListBox::MacDelete( int N
)
446 Cell cell
= { 0 , 0 } ;
447 UMAGetControlData( m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
, sizeof( ListHandle
) , (char*) &list
, &result
) ;
448 LDelRow( 1 , N
, list
) ;
451 void wxListBox::MacInsert( int n
, const char * text
)
458 LAddRow( 1 , cell
.v
, m_macList
) ;
461 void wxListBox::MacAppend( const char * text
)
463 Cell cell
= { 0 , 0 } ;
464 cell
.v
= (**m_macList
).dataBounds
.bottom
;
465 LAddRow( 1 , cell
.v
, m_macList
) ;
468 void wxListBox::MacClear()
470 LDelRow( (**m_macList
).dataBounds
.bottom
, 0 , m_macList
) ;
473 void wxListBox::MacSetSelection( int n
, bool select
)
475 Cell cell
= { 0 , 0 } ;
476 if ( LGetSelect( TRUE
, &cell
, m_macList
) )
478 LSetSelect( false , cell
, m_macList
) ;
482 LSetSelect( select
, cell
, m_macList
) ;
483 LAutoScroll( m_macList
) ;
486 bool wxListBox::MacIsSelected( int n
) const
488 Cell cell
= { 0 , 0 } ;
490 return LGetSelect( false , &cell
, m_macList
) ;
493 void wxListBox::MacDestroy()
495 // DisposeExtLDEFInfo( m_macList ) ;
498 int wxListBox::MacGetSelection() const
500 Cell cell
= { 0 , 0 } ;
501 if ( LGetSelect( true , &cell
, m_macList
) )
507 int wxListBox::MacGetSelections( wxArrayInt
& aSelections
) const
513 Cell cell
= { 0 , 0 } ;
516 while ( LGetSelect( true , &cell
, m_macList
) )
518 aSelections
.Add( cell
.v
) ;
525 void wxListBox::MacSet( int n
, const char * text
)
527 // our implementation does not store anything in the list
528 // so we just have to redraw
529 Cell cell
= { 0 , 0 } ;
531 LDraw( cell
, m_macList
) ;
534 void wxListBox::MacScrollTo( int n
)
536 // TODO implement scrolling
539 void wxListBox::OnSize( const wxSizeEvent
&event
)
541 Point pt
= (**m_macList
).cellSize
;
542 pt
.h
= m_width
- 15 /* scrollbar */ - m_macHorizontalBorder
* 2 ;
543 LCellSize( pt
, m_macList
) ;
546 void wxListBox::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
548 Boolean wasDoubleClick
= false ;
551 UMAGetControlData( m_macControl
, kControlNoPart
, kControlListBoxDoubleClickTag
, sizeof( wasDoubleClick
) , (char*) &wasDoubleClick
, &result
) ;
552 if ( !wasDoubleClick
)
562 void wxListBox::MacSetRedraw( bool doDraw
)
564 LSetDrawingMode( doDraw
, m_macList
) ;
568 void wxListBox::MacDoClick()
570 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, m_windowId
);
571 event
.SetEventObject( this );
573 wxArrayInt aSelections
;
574 int n
, count
= GetSelections(aSelections
);
578 if ( HasClientObjectData() )
579 event
.SetClientObject( GetClientObject(n
) );
580 else if ( HasClientUntypedData() )
581 event
.SetClientData( GetClientData(n
) );
582 event
.SetString( GetString(n
) );
589 event
.m_commandInt
= n
;
591 GetEventHandler()->ProcessEvent(event
);
594 void wxListBox::MacDoDoubleClick()
596 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, m_windowId
);
597 event
.SetEventObject( this );
598 GetEventHandler()->ProcessEvent(event
) ;