1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/choice.cpp
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/choice.h"
20 #include "wx/dcclient.h"
23 #include "wx/osx/private.h"
25 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControlWithItems
)
29 if ( HasClientObjectData() )
31 unsigned int i
, max
= GetCount();
33 for ( i
= 0; i
< max
; ++i
)
34 delete GetClientObject( i
);
39 bool wxChoice::Create(wxWindow
*parent
,
43 const wxArrayString
& choices
,
45 const wxValidator
& validator
,
46 const wxString
& name
)
48 if ( !Create( parent
, id
, pos
, size
, 0, NULL
, style
, validator
, name
) )
53 if ( !choices
.empty() )
56 SetInitialSize( size
);
61 bool wxChoice::Create(wxWindow
*parent
,
66 const wxString choices
[],
68 const wxValidator
& validator
,
69 const wxString
& name
)
71 m_macIsUserPane
= false;
73 if ( !wxChoiceBase::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
76 m_popUpMenu
= new wxMenu();
77 m_popUpMenu
->SetNoEventsMode(true);
79 m_peer
= wxWidgetImpl::CreateChoice( this, parent
, id
, m_popUpMenu
, pos
, size
, style
, GetExtraStyle() );
81 MacPostControlCreate( pos
, size
);
84 if ( style
& wxCB_SORT
)
86 m_strings
= wxArrayString( 1 );
91 // Set the first item as being selected
95 // Needed because it is a wxControlWithItems
96 SetInitialSize( size
);
101 // ----------------------------------------------------------------------------
102 // adding/deleting items to/from the list
103 // ----------------------------------------------------------------------------
105 int wxChoice::DoInsertItems(const wxArrayStringsAdapter
& items
,
107 void **clientData
, wxClientDataType type
)
109 const unsigned int numItems
= items
.GetCount();
110 for( unsigned int i
= 0; i
< numItems
; ++i
, ++pos
)
117 wxArrayString::iterator
118 insertPoint
= std::lower_bound( m_strings
.begin(), m_strings
.end(), items
[i
] );
119 idx
= insertPoint
- m_strings
.begin();
120 m_strings
.insert( insertPoint
, items
[i
] );
126 m_strings
.Insert( items
[i
], idx
);
129 m_popUpMenu
->Insert( idx
, i
+1, items
[i
] );
130 m_datas
.Insert( NULL
, idx
);
131 AssignNewItemClientData(idx
, clientData
, i
, type
);
134 m_peer
->SetMaximum( GetCount() );
139 void wxChoice::DoDeleteOneItem(unsigned int n
)
141 wxCHECK_RET( IsValid(n
) , wxT("wxChoice::Delete: invalid index") );
143 if ( HasClientObjectData() )
144 delete GetClientObject( n
);
146 m_popUpMenu
->Delete( m_popUpMenu
->FindItemByPosition( n
) );
148 m_strings
.RemoveAt( n
) ;
149 m_datas
.RemoveAt( n
) ;
150 m_peer
->SetMaximum( GetCount() ) ;
154 void wxChoice::DoClear()
156 for ( unsigned int i
= 0 ; i
< GetCount() ; i
++ )
158 m_popUpMenu
->Delete( m_popUpMenu
->FindItemByPosition( 0 ) );
164 m_peer
->SetMaximum( 0 ) ;
167 // ----------------------------------------------------------------------------
169 // ----------------------------------------------------------------------------
170 int wxChoice::GetSelection() const
172 return m_peer
->GetValue();
175 void wxChoice::SetSelection( int n
)
177 m_peer
->SetValue( n
);
180 // ----------------------------------------------------------------------------
181 // string list functions
182 // ----------------------------------------------------------------------------
184 unsigned int wxChoice::GetCount() const
186 return m_strings
.GetCount() ;
189 int wxChoice::FindString( const wxString
& s
, bool bCase
) const
192 // Avoid assert for non-default args passed to sorted array Index
197 return m_strings
.Index( s
, bCase
) ;
200 void wxChoice::SetString(unsigned int n
, const wxString
& s
)
202 wxCHECK_RET( IsValid(n
), wxT("wxChoice::SetString(): invalid index") );
206 m_popUpMenu
->FindItemByPosition( n
)->SetItemLabel( s
) ;
209 wxString
wxChoice::GetString(unsigned int n
) const
211 wxCHECK_MSG( IsValid(n
), wxEmptyString
, wxT("wxChoice::GetString(): invalid index") );
213 return m_strings
[n
] ;
216 // ----------------------------------------------------------------------------
218 // ----------------------------------------------------------------------------
219 void wxChoice::DoSetItemClientData(unsigned int n
, void* clientData
)
221 wxCHECK_RET( IsValid(n
), wxT("wxChoice::DoSetItemClientData: invalid index") );
223 m_datas
[n
] = (char*)clientData
;
226 void * wxChoice::DoGetItemClientData(unsigned int n
) const
228 wxCHECK_MSG( IsValid(n
), NULL
, wxT("wxChoice::DoGetClientData: invalid index") );
230 return (void *)m_datas
[n
];
233 bool wxChoice::OSXHandleClicked( double WXUNUSED(timestampsec
) )
235 wxCommandEvent
event( wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
237 // actually n should be made sure by the os to be a valid selection, but ...
238 int n
= GetSelection();
242 event
.SetString( GetStringSelection() );
243 event
.SetEventObject( this );
245 if ( HasClientObjectData() )
246 event
.SetClientObject( GetClientObject( n
) );
247 else if ( HasClientUntypedData() )
248 event
.SetClientData( GetClientData( n
) );
250 ProcessCommand( event
);
256 wxSize
wxChoice::DoGetBestSize() const
258 int lbWidth
= GetCount() > 0 ? 20 : 100; // some defaults
259 wxSize baseSize
= wxWindow::DoGetBestSize();
260 int lbHeight
= baseSize
.y
;
264 wxClientDC
dc(const_cast<wxChoice
*>(this));
266 // Find the widest line
267 for(unsigned int i
= 0; i
< GetCount(); i
++)
269 wxString
str(GetString(i
));
271 wxCoord width
, height
;
272 dc
.GetTextExtent( str
, &width
, &height
);
275 lbWidth
= wxMax( lbWidth
, wLine
) ;
278 // Add room for the popup arrow
279 lbWidth
+= 2 * lbHeight
;
281 wxCoord width
, height
;
282 dc
.GetTextExtent( wxT("X"), &width
, &height
);
288 return wxSize( lbWidth
, lbHeight
);
291 #endif // wxUSE_CHOICE