]> git.saurik.com Git - wxWidgets.git/blame - src/osx/choice_osx.cpp
removing old codewarrior files
[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
b5b208a1 7// RCS-ID: $Id$
524c47aa
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
524c47aa
SC
25wxChoice::~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
37bool 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
59bool 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 )
d15694e8
SC
68{
69 DontCreatePeer();
70
524c47aa
SC
71 if ( !wxChoiceBase::Create( parent, id, pos, size, style, validator, name ) )
72 return false;
03647350 73
524c47aa
SC
74 m_popUpMenu = new wxMenu();
75 m_popUpMenu->SetNoEventsMode(true);
03647350 76
22756322 77 SetPeer(wxWidgetImpl::CreateChoice( this, parent, id, m_popUpMenu, pos, size, style, GetExtraStyle() ));
524c47aa
SC
78
79 MacPostControlCreate( pos, size );
80
01871bf6 81#if !wxUSE_STD_CONTAINERS
524c47aa
SC
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
103int 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
01871bf6 112#if wxUSE_STD_CONTAINERS
524c47aa
SC
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
01871bf6 121#endif // wxUSE_STD_CONTAINERS
524c47aa
SC
122 {
123 idx = pos;
124 m_strings.Insert( items[i], idx );
125 }
126
708cc394
RD
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 );
524c47aa
SC
131 m_datas.Insert( NULL, idx );
132 AssignNewItemClientData(idx, clientData, i, type);
133 }
134
22756322 135 GetPeer()->SetMaximum( GetCount() );
524c47aa
SC
136
137 return pos - 1;
138}
139
140void wxChoice::DoDeleteOneItem(unsigned int n)
141{
142 wxCHECK_RET( IsValid(n) , wxT("wxChoice::Delete: invalid index") );
143
144 if ( HasClientObjectData() )
145 delete GetClientObject( n );
03647350 146
524c47aa
SC
147 m_popUpMenu->Delete( m_popUpMenu->FindItemByPosition( n ) );
148
149 m_strings.RemoveAt( n ) ;
150 m_datas.RemoveAt( n ) ;
22756322 151 GetPeer()->SetMaximum( GetCount() ) ;
524c47aa
SC
152
153}
154
155void 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
22756322 165 GetPeer()->SetMaximum( 0 ) ;
524c47aa
SC
166}
167
168// ----------------------------------------------------------------------------
169// selection
170// ----------------------------------------------------------------------------
171int wxChoice::GetSelection() const
172{
22756322 173 return GetPeer()->GetValue();
524c47aa
SC
174}
175
176void wxChoice::SetSelection( int n )
177{
22756322 178 GetPeer()->SetValue( n );
524c47aa
SC
179}
180
181// ----------------------------------------------------------------------------
182// string list functions
183// ----------------------------------------------------------------------------
184
185unsigned int wxChoice::GetCount() const
186{
187 return m_strings.GetCount() ;
188}
189
190int wxChoice::FindString( const wxString& s, bool bCase ) const
191{
01871bf6 192#if !wxUSE_STD_CONTAINERS
524c47aa
SC
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
201void 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
210wxString 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// ----------------------------------------------------------------------------
220void wxChoice::DoSetItemClientData(unsigned int n, void* clientData)
221{
524c47aa
SC
222 m_datas[n] = (char*)clientData ;
223}
224
225void * wxChoice::DoGetItemClientData(unsigned int n) const
226{
524c47aa
SC
227 return (void *)m_datas[n];
228}
229
0faf03bf 230bool wxChoice::OSXHandleClicked( double WXUNUSED(timestampsec) )
524c47aa
SC
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 );
03647350 241
524c47aa
SC
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
253wxSize wxChoice::DoGetBestSize() const
254{
255 int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults
d33e45f1
SC
256 wxSize baseSize = wxWindow::DoGetBestSize();
257 int lbHeight = baseSize.y;
524c47aa
SC
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 ;
524c47aa
SC
281
282 lbWidth += cx ;
283 }
284
285 return wxSize( lbWidth, lbHeight );
286}
287
288#endif // wxUSE_CHOICE