]>
git.saurik.com Git - wxWidgets.git/blob - src/osx/choice_osx.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/choice_osx.cpp
4 // Author: Stefan Csomor
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
15 #include "wx/choice.h"
19 #include "wx/dcclient.h"
22 #include "wx/osx/private.h"
26 if ( HasClientObjectData() )
28 unsigned int i
, max
= GetCount();
30 for ( i
= 0; i
< max
; ++i
)
31 delete GetClientObject( i
);
36 bool wxChoice::Create(wxWindow
*parent
,
40 const wxArrayString
& choices
,
42 const wxValidator
& validator
,
43 const wxString
& name
)
45 if ( !Create( parent
, id
, pos
, size
, 0, NULL
, style
, validator
, name
) )
50 if ( !choices
.empty() )
53 SetInitialSize( size
);
58 bool wxChoice::Create(wxWindow
*parent
,
63 const wxString choices
[],
65 const wxValidator
& validator
,
66 const wxString
& name
)
70 if ( !wxChoiceBase::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
73 m_popUpMenu
= new wxMenu();
74 m_popUpMenu
->SetNoEventsMode(true);
76 SetPeer(wxWidgetImpl::CreateChoice( this, parent
, id
, m_popUpMenu
, pos
, size
, style
, GetExtraStyle() ));
78 MacPostControlCreate( pos
, size
);
80 #if !wxUSE_STD_CONTAINERS
81 if ( style
& wxCB_SORT
)
83 m_strings
= wxArrayString( 1 );
88 // Set the first item as being selected
92 // Needed because it is a wxControlWithItems
93 SetInitialSize( size
);
98 // ----------------------------------------------------------------------------
99 // adding/deleting items to/from the list
100 // ----------------------------------------------------------------------------
102 void wxChoice::DoAfterItemCountChange()
104 InvalidateBestSize();
106 GetPeer()->SetMaximum( GetCount() );
109 int wxChoice::DoInsertItems(const wxArrayStringsAdapter
& items
,
111 void **clientData
, wxClientDataType type
)
113 const unsigned int numItems
= items
.GetCount();
114 for( unsigned int i
= 0; i
< numItems
; ++i
, ++pos
)
118 #if wxUSE_STD_CONTAINERS
121 wxArrayString::iterator
122 insertPoint
= std::lower_bound( m_strings
.begin(), m_strings
.end(), items
[i
] );
123 idx
= insertPoint
- m_strings
.begin();
124 m_strings
.insert( insertPoint
, items
[i
] );
127 #endif // wxUSE_STD_CONTAINERS
130 m_strings
.Insert( items
[i
], idx
);
133 wxString text
= items
[i
];
134 if (text
== wxEmptyString
)
135 text
= " "; // menu items can't have empty labels
136 m_popUpMenu
->Insert( idx
, i
+1, text
);
137 m_datas
.Insert( NULL
, idx
);
138 AssignNewItemClientData(idx
, clientData
, i
, type
);
141 DoAfterItemCountChange();
146 void wxChoice::DoDeleteOneItem(unsigned int n
)
148 wxCHECK_RET( IsValid(n
) , wxT("wxChoice::Delete: invalid index") );
150 if ( HasClientObjectData() )
151 delete GetClientObject( n
);
153 m_popUpMenu
->Delete( m_popUpMenu
->FindItemByPosition( n
) );
155 m_strings
.RemoveAt( n
) ;
156 m_datas
.RemoveAt( n
) ;
158 DoAfterItemCountChange();
161 void wxChoice::DoClear()
163 for ( unsigned int i
= 0 ; i
< GetCount() ; i
++ )
165 m_popUpMenu
->Delete( m_popUpMenu
->FindItemByPosition( 0 ) );
171 DoAfterItemCountChange();
174 // ----------------------------------------------------------------------------
176 // ----------------------------------------------------------------------------
177 int wxChoice::GetSelection() const
179 return GetPeer()->GetValue();
182 void wxChoice::SetSelection( int n
)
184 GetPeer()->SetValue( n
);
187 // ----------------------------------------------------------------------------
188 // string list functions
189 // ----------------------------------------------------------------------------
191 unsigned int wxChoice::GetCount() const
193 return m_strings
.GetCount() ;
196 int wxChoice::FindString( const wxString
& s
, bool bCase
) const
198 #if !wxUSE_STD_CONTAINERS
199 // Avoid assert for non-default args passed to sorted array Index
204 return m_strings
.Index( s
, bCase
) ;
207 void wxChoice::SetString(unsigned int n
, const wxString
& s
)
209 wxCHECK_RET( IsValid(n
), wxT("wxChoice::SetString(): invalid index") );
213 m_popUpMenu
->FindItemByPosition( n
)->SetItemLabel( s
) ;
216 wxString
wxChoice::GetString(unsigned int n
) const
218 wxCHECK_MSG( IsValid(n
), wxEmptyString
, wxT("wxChoice::GetString(): invalid index") );
220 return m_strings
[n
] ;
223 // ----------------------------------------------------------------------------
225 // ----------------------------------------------------------------------------
226 void wxChoice::DoSetItemClientData(unsigned int n
, void* clientData
)
228 m_datas
[n
] = (char*)clientData
;
231 void * wxChoice::DoGetItemClientData(unsigned int n
) const
233 return (void *)m_datas
[n
];
236 bool wxChoice::OSXHandleClicked( double WXUNUSED(timestampsec
) )
238 SendSelectionChangedEvent(wxEVT_CHOICE
);
243 wxSize
wxChoice::DoGetBestSize() const
245 // We use the base window size for the height (which is wrong as it doesn't
246 // take the font into account -- TODO) and add some margins to the width
247 // computed by the base class method to account for the arrow.
248 const int lbHeight
= wxWindow::DoGetBestSize().y
;
250 return wxSize(wxChoiceBase::DoGetBestSize().x
+ 2*lbHeight
+ GetCharWidth(),
254 #endif // wxUSE_CHOICE