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
= lOnlyOne
;
72 if ( style
& wxLB_MULTIPLE
)
74 (**m_macList
).selFlags
+= lNoExtend
;
76 else if ( style
& wxLB_EXTENDED
)
78 (**m_macList
).selFlags
+= lExtendDrag
;
80 Point pt
= (**m_macList
).cellSize
;
82 LCellSize( pt
, m_macList
) ;
84 LAddColumn( 1 , 0 , m_macList
) ;
86 MacPostControlCreate() ;
88 ControlFontStyleRec controlstyle
;
89 controlstyle
.flags
= kControlUseFontMask
+ kControlUseSizeMask
;
90 //controlstyle.font = kControlFontSmallSystemFont ;
91 controlstyle
.font
= kFontIDMonaco
;
92 controlstyle
.size
= 9 ;
93 ::UMASetControlFontStyle( m_macControl
, &controlstyle
) ;
95 for ( int i
= 0 ; i
< n
; i
++ )
97 Append( choices
[i
] ) ;
100 LSetDrawingMode( true , m_macList
) ;
105 wxListBox::~wxListBox()
108 DisposeExtLDEFInfo( m_macList
) ;
111 void wxListBox::Free()
113 #if wxUSE_OWNER_DRAWN
114 if ( m_windowStyle
& wxLB_OWNERDRAW
)
116 size_t uiCount
= m_aItems
.Count();
117 while ( uiCount
-- != 0 ) {
118 delete m_aItems
[uiCount
];
124 #endif // wxUSE_OWNER_DRAWN
125 if ( HasClientObjectData() )
127 for ( size_t n
= 0; n
< (size_t)m_noItems
; n
++ )
129 delete GetClientObject(n
);
134 void wxListBox::DoSetFirstItem(int N
)
139 void wxListBox::Delete(int N
)
141 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
142 wxT("invalid index in wxListBox::Delete") );
144 #if wxUSE_OWNER_DRAWN
147 #else // !wxUSE_OWNER_DRAWN
148 if ( HasClientObjectData() )
150 delete GetClientObject(N
);
152 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
153 m_stringArray
.Remove( N
) ;
159 int wxListBox::DoAppend(const wxString
& item
)
161 int index
= m_noItems
;
162 if( wxApp::s_macDefaultEncodingIsPC
)
164 m_stringArray
.Add( wxMacMakeMacStringFromPC( item
) ) ;
167 m_stringArray
.Add( item
) ;
174 void wxListBox::DoSetItems(const wxArrayString
& choices
, void** clientData
)
176 MacSetRedraw( false ) ;
178 int n
= choices
.GetCount();
180 for( int i
= 0 ; i
< n
; ++i
)
184 #if wxUSE_OWNER_DRAWN
185 wxASSERT_MSG(clientData
[i
] == NULL
,
186 wxT("Can't use client data with owner-drawn listboxes"));
187 #else // !wxUSE_OWNER_DRAWN
188 Append( choices
[i
] , clientData
[0] ) ;
192 Append( choices
[i
] ) ;
195 #if wxUSE_OWNER_DRAWN
196 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
197 // first delete old items
198 size_t ui
= m_aItems
.Count();
199 while ( ui
-- != 0 ) {
204 // then create new ones
205 for ( ui
= 0; ui
< (size_t)m_noItems
; ui
++ ) {
206 wxOwnerDrawn
*pNewItem
= CreateItem(ui
);
207 pNewItem
->SetName(choices
[ui
]);
208 m_aItems
.Add(pNewItem
);
211 #endif // wxUSE_OWNER_DRAWN
212 MacSetRedraw( true ) ;
215 bool wxListBox::HasMultipleSelection() const
217 return (m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
);
220 int wxListBox::FindString(const wxString
& st
) const
223 if( wxApp::s_macDefaultEncodingIsPC
)
225 s
= wxMacMakeMacStringFromPC( st
) ;
230 if ( s
.Right(1) == "*" )
232 wxString search
= s
.Left( s
.Length() - 1 ) ;
233 int len
= search
.Length() ;
234 for ( int i
= 0 ; i
< m_noItems
; ++ i
)
236 if ( equalstring( m_stringArray
[i
].Left( len
) , search
, false , false ) )
242 for ( int i
= 0 ; i
< m_noItems
; ++ i
)
244 if ( equalstring( m_stringArray
[i
] , s
, false , false ) )
251 void wxListBox::Clear()
255 m_stringArray
.Empty() ;
256 m_dataArray
.Empty() ;
260 void wxListBox::SetSelection(int N
, bool select
)
262 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
263 "invalid index in wxListBox::SetSelection" );
264 MacSetSelection( N
, select
) ;
267 bool wxListBox::IsSelected(int N
) const
269 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, FALSE
,
270 "invalid index in wxListBox::Selected" );
272 return MacIsSelected( N
) ;
275 void *wxListBox::DoGetItemClientData(int N
) const
277 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, NULL
,
278 "invalid index in wxListBox::GetClientData" );
280 return (void *)m_dataArray
[N
];
283 wxClientData
*wxListBox::DoGetItemClientObject(int N
) const
285 return (wxClientData
*) DoGetItemClientData( N
) ;
288 void wxListBox::DoSetItemClientData(int N
, void *Client_data
)
290 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
291 "invalid index in wxListBox::SetClientData" );
293 #if wxUSE_OWNER_DRAWN
294 if ( m_windowStyle
& wxLB_OWNERDRAW
)
296 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
297 // in OnMeasure/OnDraw.
298 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
300 #endif // wxUSE_OWNER_DRAWN
301 m_dataArray
[N
] = (char*) Client_data
;
304 void wxListBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
306 DoSetItemClientData(n
, clientData
);
309 // Return number of selections and an array of selected integers
310 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
312 return MacGetSelections( aSelections
) ;
315 if ((m_windowStyle & wxLB_MULTIMacE) || (m_windowStyle & wxLB_EXTENDED))
318 for ( int n = 0; n < no_sel; n++ )
323 else // single-selection listbox
332 // Get single selection, for single choice list items
333 int wxListBox::GetSelection() const
335 return MacGetSelection() ;
338 // Find string for position
339 wxString
wxListBox::GetString(int N
) const
341 if( wxApp::s_macDefaultEncodingIsPC
)
343 return wxMacMakePCStringFromMac( m_stringArray
[N
] ) ;
346 return m_stringArray
[N
] ;
349 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
351 wxCHECK_RET( pos
>= 0 && pos
<= m_noItems
,
352 wxT("invalid index in wxListBox::InsertItems") );
354 int nItems
= items
.GetCount();
356 for ( int i
= 0 ; i
< nItems
; i
++ )
358 m_stringArray
.Insert( items
[i
] , pos
+ i
) ;
359 m_dataArray
.Insert( NULL
, pos
+ i
) ;
360 MacInsert( pos
+ i
, items
[i
] ) ;
366 void wxListBox::SetString(int N
, const wxString
& s
)
368 m_stringArray
[N
] = s
;
372 wxSize
wxListBox::DoGetBestSize()
374 return wxSize(100, 100);
377 int wxListBox::GetCount() const
382 void wxListBox::SetupColours()
384 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
385 SetForegroundColour(GetParent()->GetForegroundColour());
388 #if wxUSE_OWNER_DRAWN
390 class wxListBoxItem
: public wxOwnerDrawn
393 wxListBoxItem(const wxString
& str
= "");
396 wxListBoxItem::wxListBoxItem(const wxString
& str
) : wxOwnerDrawn(str
, FALSE
)
398 // no bitmaps/checkmarks
402 wxOwnerDrawn
*wxListBox::CreateItem(size_t n
)
404 return new wxListBoxItem();
407 #endif //USE_OWNER_DRAWN
409 // ============================================================================
410 // list box control implementation
411 // ============================================================================
413 void MacDrawStringCell(Rect
*cellRect
, Cell lCell
, ListHandle theList
, long refCon
)
416 // typecast our refCon
417 list
= (wxListBox
*)refCon
;
419 MoveTo(cellRect
->left
+ 4 , cellRect
->top
+ 10 );
420 const wxString text
= list
->m_stringArray
[lCell
.v
] ;
421 ::TextFont( kFontIDMonaco
) ;
424 DrawText(text
, 0 , text
.Length());
428 void wxListBox::MacDelete( int N
)
432 Cell cell
= { 0 , 0 } ;
433 UMAGetControlData( m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
, sizeof( ListHandle
) , (char*) &list
, &result
) ;
434 LDelRow( 1 , N
, list
) ;
437 void wxListBox::MacInsert( int n
, const char * text
)
444 LAddRow( 1 , cell
.v
, m_macList
) ;
447 void wxListBox::MacAppend( const char * text
)
449 Cell cell
= { 0 , 0 } ;
450 cell
.v
= (**m_macList
).dataBounds
.bottom
;
451 LAddRow( 1 , cell
.v
, m_macList
) ;
454 void wxListBox::MacClear()
456 LDelRow( (**m_macList
).dataBounds
.bottom
, 0 , m_macList
) ;
459 void wxListBox::MacSetSelection( int n
, bool select
)
461 Cell cell
= { 0 , 0 } ;
462 if ( LGetSelect( TRUE
, &cell
, m_macList
) )
464 LSetSelect( false , cell
, m_macList
) ;
468 LSetSelect( select
, cell
, m_macList
) ;
469 LAutoScroll( m_macList
) ;
472 bool wxListBox::MacIsSelected( int n
) const
474 Cell cell
= { 0 , 0 } ;
476 return LGetSelect( false , &cell
, m_macList
) ;
479 void wxListBox::MacDestroy()
481 // DisposeExtLDEFInfo( m_macList ) ;
484 int wxListBox::MacGetSelection() const
486 Cell cell
= { 0 , 0 } ;
487 if ( LGetSelect( true , &cell
, m_macList
) )
493 int wxListBox::MacGetSelections( wxArrayInt
& aSelections
) const
499 Cell cell
= { 0 , 0 } ;
502 while ( LGetSelect( true , &cell
, m_macList
) )
504 aSelections
.Add( cell
.v
) ;
511 void wxListBox::MacSet( int n
, const char * text
)
513 // our implementation does not store anything in the list
514 // so we just have to redraw
515 Cell cell
= { 0 , 0 } ;
517 LDraw( cell
, m_macList
) ;
520 void wxListBox::MacScrollTo( int n
)
522 // TODO implement scrolling
525 void wxListBox::OnSize( const wxSizeEvent
&event
)
527 Point pt
= (**m_macList
).cellSize
;
528 pt
.h
= m_width
- 15 /* scrollbar */ - m_macHorizontalBorder
* 2 ;
529 LCellSize( pt
, m_macList
) ;
532 void wxListBox::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
534 Boolean wasDoubleClick
= false ;
537 UMAGetControlData( m_macControl
, kControlNoPart
, kControlListBoxDoubleClickTag
, sizeof( wasDoubleClick
) , (char*) &wasDoubleClick
, &result
) ;
538 if ( !wasDoubleClick
)
548 void wxListBox::MacSetRedraw( bool doDraw
)
550 LSetDrawingMode( doDraw
, m_macList
) ;
554 void wxListBox::MacDoClick()
556 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, m_windowId
);
557 event
.SetEventObject( this );
559 wxArrayInt aSelections
;
560 int n
, count
= GetSelections(aSelections
);
564 if ( HasClientObjectData() )
565 event
.SetClientObject( GetClientObject(n
) );
566 else if ( HasClientUntypedData() )
567 event
.SetClientData( GetClientData(n
) );
568 event
.SetString( GetString(n
) );
575 event
.m_commandInt
= n
;
577 GetEventHandler()->ProcessEvent(event
);
580 void wxListBox::MacDoDoubleClick()
582 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, m_windowId
);
583 event
.SetEventObject( this );
584 GetEventHandler()->ProcessEvent(event
) ;