]> git.saurik.com Git - wxWidgets.git/blame - src/osx/choice_osx.cpp
adapting to new event version
[wxWidgets.git] / src / osx / choice_osx.cpp
CommitLineData
524c47aa 1/////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/osx/choice_osx.cpp
524c47aa
SC
3// Purpose: wxChoice
4// Author: Stefan Csomor
5// Modified by:
6// Created: 1998-01-01
524c47aa
SC
7// Copyright: (c) Stefan Csomor
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11#include "wx/wxprec.h"
12
13#if wxUSE_CHOICE
14
15#include "wx/choice.h"
16
17#ifndef WX_PRECOMP
18 #include "wx/menu.h"
19 #include "wx/dcclient.h"
20#endif
21
22#include "wx/osx/private.h"
23
524c47aa
SC
24wxChoice::~wxChoice()
25{
26 if ( HasClientObjectData() )
27 {
28 unsigned int i, max = GetCount();
29
30 for ( i = 0; i < max; ++i )
31 delete GetClientObject( i );
32 }
33 delete m_popUpMenu;
34}
35
36bool wxChoice::Create(wxWindow *parent,
37 wxWindowID id,
38 const wxPoint& pos,
39 const wxSize& size,
40 const wxArrayString& choices,
41 long style,
42 const wxValidator& validator,
43 const wxString& name )
44{
45 if ( !Create( parent, id, pos, size, 0, NULL, style, validator, name ) )
46 return false;
47
48 Append( choices );
49
50 if ( !choices.empty() )
51 SetSelection( 0 );
52
53 SetInitialSize( size );
54
55 return true;
56}
57
58bool wxChoice::Create(wxWindow *parent,
59 wxWindowID id,
60 const wxPoint& pos,
61 const wxSize& size,
62 int n,
63 const wxString choices[],
64 long style,
65 const wxValidator& validator,
66 const wxString& name )
d15694e8
SC
67{
68 DontCreatePeer();
69
524c47aa
SC
70 if ( !wxChoiceBase::Create( parent, id, pos, size, style, validator, name ) )
71 return false;
03647350 72
524c47aa
SC
73 m_popUpMenu = new wxMenu();
74 m_popUpMenu->SetNoEventsMode(true);
03647350 75
22756322 76 SetPeer(wxWidgetImpl::CreateChoice( this, parent, id, m_popUpMenu, pos, size, style, GetExtraStyle() ));
524c47aa
SC
77
78 MacPostControlCreate( pos, size );
79
01871bf6 80#if !wxUSE_STD_CONTAINERS
524c47aa
SC
81 if ( style & wxCB_SORT )
82 // autosort
83 m_strings = wxArrayString( 1 );
84#endif
85
86 Append(n, choices);
87
88 // Set the first item as being selected
89 if (n > 0)
90 SetSelection( 0 );
91
92 // Needed because it is a wxControlWithItems
93 SetInitialSize( size );
94
95 return true;
96}
97
98// ----------------------------------------------------------------------------
99// adding/deleting items to/from the list
100// ----------------------------------------------------------------------------
101
c236dbae
VZ
102void wxChoice::DoAfterItemCountChange()
103{
104 InvalidateBestSize();
105
106 GetPeer()->SetMaximum( GetCount() );
107}
108
524c47aa
SC
109int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items,
110 unsigned int pos,
111 void **clientData, wxClientDataType type)
112{
113 const unsigned int numItems = items.GetCount();
114 for( unsigned int i = 0; i < numItems; ++i, ++pos )
115 {
116 unsigned int idx;
117
01871bf6 118#if wxUSE_STD_CONTAINERS
524c47aa
SC
119 if ( IsSorted() )
120 {
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] );
125 }
126 else
01871bf6 127#endif // wxUSE_STD_CONTAINERS
524c47aa
SC
128 {
129 idx = pos;
130 m_strings.Insert( items[i], idx );
131 }
132
708cc394
RD
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 );
524c47aa
SC
137 m_datas.Insert( NULL, idx );
138 AssignNewItemClientData(idx, clientData, i, type);
139 }
140
c236dbae 141 DoAfterItemCountChange();
524c47aa
SC
142
143 return pos - 1;
144}
145
146void wxChoice::DoDeleteOneItem(unsigned int n)
147{
148 wxCHECK_RET( IsValid(n) , wxT("wxChoice::Delete: invalid index") );
149
150 if ( HasClientObjectData() )
151 delete GetClientObject( n );
03647350 152
524c47aa
SC
153 m_popUpMenu->Delete( m_popUpMenu->FindItemByPosition( n ) );
154
155 m_strings.RemoveAt( n ) ;
156 m_datas.RemoveAt( n ) ;
524c47aa 157
c236dbae 158 DoAfterItemCountChange();
524c47aa
SC
159}
160
161void wxChoice::DoClear()
162{
163 for ( unsigned int i = 0 ; i < GetCount() ; i++ )
164 {
165 m_popUpMenu->Delete( m_popUpMenu->FindItemByPosition( 0 ) );
166 }
167
168 m_strings.Empty() ;
169 m_datas.Empty() ;
170
c236dbae 171 DoAfterItemCountChange();
524c47aa
SC
172}
173
174// ----------------------------------------------------------------------------
175// selection
176// ----------------------------------------------------------------------------
177int wxChoice::GetSelection() const
178{
22756322 179 return GetPeer()->GetValue();
524c47aa
SC
180}
181
182void wxChoice::SetSelection( int n )
183{
22756322 184 GetPeer()->SetValue( n );
524c47aa
SC
185}
186
187// ----------------------------------------------------------------------------
188// string list functions
189// ----------------------------------------------------------------------------
190
191unsigned int wxChoice::GetCount() const
192{
193 return m_strings.GetCount() ;
194}
195
196int wxChoice::FindString( const wxString& s, bool bCase ) const
197{
01871bf6 198#if !wxUSE_STD_CONTAINERS
524c47aa
SC
199 // Avoid assert for non-default args passed to sorted array Index
200 if ( IsSorted() )
201 bCase = true;
202#endif
203
204 return m_strings.Index( s , bCase ) ;
205}
206
207void wxChoice::SetString(unsigned int n, const wxString& s)
208{
209 wxCHECK_RET( IsValid(n), wxT("wxChoice::SetString(): invalid index") );
210
211 m_strings[n] = s ;
212
213 m_popUpMenu->FindItemByPosition( n )->SetItemLabel( s ) ;
214}
215
216wxString wxChoice::GetString(unsigned int n) const
217{
218 wxCHECK_MSG( IsValid(n), wxEmptyString, wxT("wxChoice::GetString(): invalid index") );
219
220 return m_strings[n] ;
221}
222
223// ----------------------------------------------------------------------------
224// client data
225// ----------------------------------------------------------------------------
226void wxChoice::DoSetItemClientData(unsigned int n, void* clientData)
227{
524c47aa
SC
228 m_datas[n] = (char*)clientData ;
229}
230
231void * wxChoice::DoGetItemClientData(unsigned int n) const
232{
524c47aa
SC
233 return (void *)m_datas[n];
234}
235
0faf03bf 236bool wxChoice::OSXHandleClicked( double WXUNUSED(timestampsec) )
524c47aa 237{
ce7fe42e 238 SendSelectionChangedEvent(wxEVT_CHOICE);
524c47aa
SC
239
240 return true ;
241}
242
243wxSize wxChoice::DoGetBestSize() const
244{
7eb0acef
VZ
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;
524c47aa 249
7eb0acef
VZ
250 return wxSize(wxChoiceBase::DoGetBestSize().x + 2*lbHeight + GetCharWidth(),
251 lbHeight);
524c47aa
SC
252}
253
254#endif // wxUSE_CHOICE