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