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"
27 if ( HasClientObjectData() )
29 unsigned int i
, max
= GetCount();
31 for ( i
= 0; i
< max
; ++i
)
32 delete GetClientObject( i
);
37 bool wxChoice::Create(wxWindow
*parent
,
41 const wxArrayString
& choices
,
43 const wxValidator
& validator
,
44 const wxString
& name
)
46 if ( !Create( parent
, id
, pos
, size
, 0, NULL
, style
, validator
, name
) )
51 if ( !choices
.empty() )
54 SetInitialSize( size
);
59 bool wxChoice::Create(wxWindow
*parent
,
64 const wxString choices
[],
66 const wxValidator
& validator
,
67 const wxString
& name
)
69 m_macIsUserPane
= false;
71 if ( !wxChoiceBase::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
74 m_popUpMenu
= new wxMenu();
75 m_popUpMenu
->SetNoEventsMode(true);
77 m_peer
= wxWidgetImpl::CreateChoice( this, parent
, id
, m_popUpMenu
, pos
, size
, style
, GetExtraStyle() );
79 MacPostControlCreate( pos
, size
);
82 if ( style
& wxCB_SORT
)
84 m_strings
= wxArrayString( 1 );
89 // Set the first item as being selected
93 // Needed because it is a wxControlWithItems
94 SetInitialSize( size
);
99 // ----------------------------------------------------------------------------
100 // adding/deleting items to/from the list
101 // ----------------------------------------------------------------------------
103 int wxChoice::DoInsertItems(const wxArrayStringsAdapter
& items
,
105 void **clientData
, wxClientDataType type
)
107 const unsigned int numItems
= items
.GetCount();
108 for( unsigned int i
= 0; i
< numItems
; ++i
, ++pos
)
115 wxArrayString::iterator
116 insertPoint
= std::lower_bound( m_strings
.begin(), m_strings
.end(), items
[i
] );
117 idx
= insertPoint
- m_strings
.begin();
118 m_strings
.insert( insertPoint
, items
[i
] );
124 m_strings
.Insert( items
[i
], idx
);
127 wxString text
= items
[i
];
128 if (text
== wxEmptyString
)
129 text
= " "; // menu items can't have empty labels
130 m_popUpMenu
->Insert( idx
, i
+1, text
);
131 m_datas
.Insert( NULL
, idx
);
132 AssignNewItemClientData(idx
, clientData
, i
, type
);
135 m_peer
->SetMaximum( GetCount() );
140 void wxChoice::DoDeleteOneItem(unsigned int n
)
142 wxCHECK_RET( IsValid(n
) , wxT("wxChoice::Delete: invalid index") );
144 if ( HasClientObjectData() )
145 delete GetClientObject( n
);
147 m_popUpMenu
->Delete( m_popUpMenu
->FindItemByPosition( n
) );
149 m_strings
.RemoveAt( n
) ;
150 m_datas
.RemoveAt( n
) ;
151 m_peer
->SetMaximum( GetCount() ) ;
155 void wxChoice::DoClear()
157 for ( unsigned int i
= 0 ; i
< GetCount() ; i
++ )
159 m_popUpMenu
->Delete( m_popUpMenu
->FindItemByPosition( 0 ) );
165 m_peer
->SetMaximum( 0 ) ;
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
171 int wxChoice::GetSelection() const
173 return m_peer
->GetValue();
176 void wxChoice::SetSelection( int n
)
178 m_peer
->SetValue( n
);
181 // ----------------------------------------------------------------------------
182 // string list functions
183 // ----------------------------------------------------------------------------
185 unsigned int wxChoice::GetCount() const
187 return m_strings
.GetCount() ;
190 int wxChoice::FindString( const wxString
& s
, bool bCase
) const
193 // Avoid assert for non-default args passed to sorted array Index
198 return m_strings
.Index( s
, bCase
) ;
201 void wxChoice::SetString(unsigned int n
, const wxString
& s
)
203 wxCHECK_RET( IsValid(n
), wxT("wxChoice::SetString(): invalid index") );
207 m_popUpMenu
->FindItemByPosition( n
)->SetItemLabel( s
) ;
210 wxString
wxChoice::GetString(unsigned int n
) const
212 wxCHECK_MSG( IsValid(n
), wxEmptyString
, wxT("wxChoice::GetString(): invalid index") );
214 return m_strings
[n
] ;
217 // ----------------------------------------------------------------------------
219 // ----------------------------------------------------------------------------
220 void wxChoice::DoSetItemClientData(unsigned int n
, void* clientData
)
222 m_datas
[n
] = (char*)clientData
;
225 void * wxChoice::DoGetItemClientData(unsigned int n
) const
227 return (void *)m_datas
[n
];
230 bool wxChoice::OSXHandleClicked( double WXUNUSED(timestampsec
) )
232 wxCommandEvent
event( wxEVT_COMMAND_CHOICE_SELECTED
, m_windowId
);
234 // actually n should be made sure by the os to be a valid selection, but ...
235 int n
= GetSelection();
239 event
.SetString( GetStringSelection() );
240 event
.SetEventObject( this );
242 if ( HasClientObjectData() )
243 event
.SetClientObject( GetClientObject( n
) );
244 else if ( HasClientUntypedData() )
245 event
.SetClientData( GetClientData( n
) );
247 ProcessCommand( event
);
253 wxSize
wxChoice::DoGetBestSize() const
255 int lbWidth
= GetCount() > 0 ? 20 : 100; // some defaults
256 wxSize baseSize
= wxWindow::DoGetBestSize();
257 int lbHeight
= baseSize
.y
;
261 wxClientDC
dc(const_cast<wxChoice
*>(this));
263 // Find the widest line
264 for(unsigned int i
= 0; i
< GetCount(); i
++)
266 wxString
str(GetString(i
));
268 wxCoord width
, height
;
269 dc
.GetTextExtent( str
, &width
, &height
);
272 lbWidth
= wxMax( lbWidth
, wLine
) ;
275 // Add room for the popup arrow
276 lbWidth
+= 2 * lbHeight
;
278 wxCoord width
, height
;
279 dc
.GetTextExtent( wxT("X"), &width
, &height
);
285 return wxSize( lbWidth
, lbHeight
);
288 #endif // wxUSE_CHOICE