]> git.saurik.com Git - wxWidgets.git/blob - src/osx/choice_osx.cpp
Use correct direction in wxDataObject::IsSupported() in wxOSX dnd code.
[wxWidgets.git] / src / osx / choice_osx.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/choice_osx.cpp
3 // Purpose: wxChoice
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxUSE_CHOICE
15
16 #include "wx/choice.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/menu.h"
20 #include "wx/dcclient.h"
21 #endif
22
23 #include "wx/osx/private.h"
24
25 wxChoice::~wxChoice()
26 {
27 if ( HasClientObjectData() )
28 {
29 unsigned int i, max = GetCount();
30
31 for ( i = 0; i < max; ++i )
32 delete GetClientObject( i );
33 }
34 delete m_popUpMenu;
35 }
36
37 bool wxChoice::Create(wxWindow *parent,
38 wxWindowID id,
39 const wxPoint& pos,
40 const wxSize& size,
41 const wxArrayString& choices,
42 long style,
43 const wxValidator& validator,
44 const wxString& name )
45 {
46 if ( !Create( parent, id, pos, size, 0, NULL, style, validator, name ) )
47 return false;
48
49 Append( choices );
50
51 if ( !choices.empty() )
52 SetSelection( 0 );
53
54 SetInitialSize( size );
55
56 return true;
57 }
58
59 bool wxChoice::Create(wxWindow *parent,
60 wxWindowID id,
61 const wxPoint& pos,
62 const wxSize& size,
63 int n,
64 const wxString choices[],
65 long style,
66 const wxValidator& validator,
67 const wxString& name )
68 {
69 DontCreatePeer();
70
71 if ( !wxChoiceBase::Create( parent, id, pos, size, style, validator, name ) )
72 return false;
73
74 m_popUpMenu = new wxMenu();
75 m_popUpMenu->SetNoEventsMode(true);
76
77 SetPeer(wxWidgetImpl::CreateChoice( this, parent, id, m_popUpMenu, pos, size, style, GetExtraStyle() ));
78
79 MacPostControlCreate( pos, size );
80
81 #if !wxUSE_STD_CONTAINERS
82 if ( style & wxCB_SORT )
83 // autosort
84 m_strings = wxArrayString( 1 );
85 #endif
86
87 Append(n, choices);
88
89 // Set the first item as being selected
90 if (n > 0)
91 SetSelection( 0 );
92
93 // Needed because it is a wxControlWithItems
94 SetInitialSize( size );
95
96 return true;
97 }
98
99 // ----------------------------------------------------------------------------
100 // adding/deleting items to/from the list
101 // ----------------------------------------------------------------------------
102
103 int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items,
104 unsigned int pos,
105 void **clientData, wxClientDataType type)
106 {
107 const unsigned int numItems = items.GetCount();
108 for( unsigned int i = 0; i < numItems; ++i, ++pos )
109 {
110 unsigned int idx;
111
112 #if wxUSE_STD_CONTAINERS
113 if ( IsSorted() )
114 {
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] );
119 }
120 else
121 #endif // wxUSE_STD_CONTAINERS
122 {
123 idx = pos;
124 m_strings.Insert( items[i], idx );
125 }
126
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);
133 }
134
135 GetPeer()->SetMaximum( GetCount() );
136
137 return pos - 1;
138 }
139
140 void wxChoice::DoDeleteOneItem(unsigned int n)
141 {
142 wxCHECK_RET( IsValid(n) , wxT("wxChoice::Delete: invalid index") );
143
144 if ( HasClientObjectData() )
145 delete GetClientObject( n );
146
147 m_popUpMenu->Delete( m_popUpMenu->FindItemByPosition( n ) );
148
149 m_strings.RemoveAt( n ) ;
150 m_datas.RemoveAt( n ) ;
151 GetPeer()->SetMaximum( GetCount() ) ;
152
153 }
154
155 void wxChoice::DoClear()
156 {
157 for ( unsigned int i = 0 ; i < GetCount() ; i++ )
158 {
159 m_popUpMenu->Delete( m_popUpMenu->FindItemByPosition( 0 ) );
160 }
161
162 m_strings.Empty() ;
163 m_datas.Empty() ;
164
165 GetPeer()->SetMaximum( 0 ) ;
166 }
167
168 // ----------------------------------------------------------------------------
169 // selection
170 // ----------------------------------------------------------------------------
171 int wxChoice::GetSelection() const
172 {
173 return GetPeer()->GetValue();
174 }
175
176 void wxChoice::SetSelection( int n )
177 {
178 GetPeer()->SetValue( n );
179 }
180
181 // ----------------------------------------------------------------------------
182 // string list functions
183 // ----------------------------------------------------------------------------
184
185 unsigned int wxChoice::GetCount() const
186 {
187 return m_strings.GetCount() ;
188 }
189
190 int wxChoice::FindString( const wxString& s, bool bCase ) const
191 {
192 #if !wxUSE_STD_CONTAINERS
193 // Avoid assert for non-default args passed to sorted array Index
194 if ( IsSorted() )
195 bCase = true;
196 #endif
197
198 return m_strings.Index( s , bCase ) ;
199 }
200
201 void wxChoice::SetString(unsigned int n, const wxString& s)
202 {
203 wxCHECK_RET( IsValid(n), wxT("wxChoice::SetString(): invalid index") );
204
205 m_strings[n] = s ;
206
207 m_popUpMenu->FindItemByPosition( n )->SetItemLabel( s ) ;
208 }
209
210 wxString wxChoice::GetString(unsigned int n) const
211 {
212 wxCHECK_MSG( IsValid(n), wxEmptyString, wxT("wxChoice::GetString(): invalid index") );
213
214 return m_strings[n] ;
215 }
216
217 // ----------------------------------------------------------------------------
218 // client data
219 // ----------------------------------------------------------------------------
220 void wxChoice::DoSetItemClientData(unsigned int n, void* clientData)
221 {
222 m_datas[n] = (char*)clientData ;
223 }
224
225 void * wxChoice::DoGetItemClientData(unsigned int n) const
226 {
227 return (void *)m_datas[n];
228 }
229
230 bool wxChoice::OSXHandleClicked( double WXUNUSED(timestampsec) )
231 {
232 wxCommandEvent event( wxEVT_COMMAND_CHOICE_SELECTED, m_windowId );
233
234 // actually n should be made sure by the os to be a valid selection, but ...
235 int n = GetSelection();
236 if ( n > -1 )
237 {
238 event.SetInt( n );
239 event.SetString( GetStringSelection() );
240 event.SetEventObject( this );
241
242 if ( HasClientObjectData() )
243 event.SetClientObject( GetClientObject( n ) );
244 else if ( HasClientUntypedData() )
245 event.SetClientData( GetClientData( n ) );
246
247 ProcessCommand( event );
248 }
249
250 return true ;
251 }
252
253 wxSize wxChoice::DoGetBestSize() const
254 {
255 int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults
256 wxSize baseSize = wxWindow::DoGetBestSize();
257 int lbHeight = baseSize.y;
258 int wLine;
259
260 {
261 wxClientDC dc(const_cast<wxChoice*>(this));
262
263 // Find the widest line
264 for(unsigned int i = 0; i < GetCount(); i++)
265 {
266 wxString str(GetString(i));
267
268 wxCoord width, height ;
269 dc.GetTextExtent( str , &width, &height);
270 wLine = width ;
271
272 lbWidth = wxMax( lbWidth, wLine ) ;
273 }
274
275 // Add room for the popup arrow
276 lbWidth += 2 * lbHeight ;
277
278 wxCoord width, height ;
279 dc.GetTextExtent( wxT("X"), &width, &height);
280 int cx = width ;
281
282 lbWidth += cx ;
283 }
284
285 return wxSize( lbWidth, lbHeight );
286 }
287
288 #endif // wxUSE_CHOICE