Preserve client data in wxComboBox::SetString() in wxOSX/Cocoa.
[wxWidgets.git] / src / osx / combobox_osx.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/combobox_osx.cpp
3 // Purpose: wxComboBox class using HIView ComboBox
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_COMBOBOX && wxOSX_USE_COCOA
15
16 #include "wx/combobox.h"
17 #include "wx/osx/private.h"
18
19 #ifndef WX_PRECOMP
20 #endif
21
22 // work in progress
23
24 wxComboBox::~wxComboBox()
25 {
26 }
27
28 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
29 const wxString& value,
30 const wxPoint& pos,
31 const wxSize& size,
32 const wxArrayString& choices,
33 long style,
34 const wxValidator& validator,
35 const wxString& name)
36 {
37 wxCArrayString chs( choices );
38
39 return Create( parent, id, value, pos, size, chs.GetCount(),
40 chs.GetStrings(), style, validator, name );
41 }
42
43 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
44 const wxString& value,
45 const wxPoint& pos,
46 const wxSize& size,
47 int n, const wxString choices[],
48 long style,
49 const wxValidator& validator,
50 const wxString& name)
51 {
52 DontCreatePeer();
53
54 m_text = NULL;
55 m_choice = NULL;
56
57 if ( !wxControl::Create( parent, id, pos, size, style, validator, name ) )
58 return false;
59
60 wxASSERT_MSG( !(style & wxCB_SORT),
61 "wxCB_SORT not currently supported by wxOSX/Cocoa");
62
63 SetPeer(wxWidgetImpl::CreateComboBox( this, parent, id, NULL, pos, size, style, GetExtraStyle() ));
64
65 MacPostControlCreate( pos, size );
66
67 Append(n, choices);
68
69 // Set up the initial value, by default the first item is selected.
70 if ( !value.empty() )
71 SetValue(value);
72 else if (n > 0)
73 SetSelection( 0 );
74
75 // Needed because it is a wxControlWithItems
76 SetInitialSize( size );
77
78 return true;
79 }
80
81 void wxComboBox::DelegateTextChanged( const wxString& value )
82 {
83 SetStringSelection( value );
84 }
85
86 void wxComboBox::DelegateChoice( const wxString& value )
87 {
88 SetStringSelection( value );
89 }
90
91 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter& items,
92 unsigned int pos,
93 void **clientData, wxClientDataType type)
94 {
95 const unsigned int numItems = items.GetCount();
96 for( unsigned int i = 0; i < numItems; ++i, ++pos )
97 {
98 unsigned int idx;
99
100 idx = pos;
101 GetComboPeer()->InsertItem( idx, items[i] );
102
103 if (idx > m_datas.GetCount())
104 m_datas.SetCount(idx);
105 m_datas.Insert( NULL, idx );
106 AssignNewItemClientData(idx, clientData, i, type);
107 }
108
109 GetPeer()->SetMaximum( GetCount() );
110
111 return pos - 1;
112 }
113
114 // ----------------------------------------------------------------------------
115 // client data
116 // ----------------------------------------------------------------------------
117 void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData)
118 {
119 m_datas[n] = (char*)clientData ;
120 }
121
122 void * wxComboBox::DoGetItemClientData(unsigned int n) const
123 {
124 return (void *)m_datas[n];
125 }
126
127 unsigned int wxComboBox::GetCount() const
128 {
129 return GetComboPeer()->GetNumberOfItems();
130 }
131
132 void wxComboBox::DoDeleteOneItem(unsigned int n)
133 {
134 GetComboPeer()->RemoveItem(n);
135 }
136
137 void wxComboBox::DoClear()
138 {
139 GetComboPeer()->Clear();
140 }
141
142 void wxComboBox::GetSelection(long *from, long *to) const
143 {
144 wxTextEntry::GetSelection(from, to);
145 }
146
147 int wxComboBox::GetSelection() const
148 {
149 return GetComboPeer()->GetSelectedItem();
150 }
151
152 void wxComboBox::SetSelection(int n)
153 {
154 GetComboPeer()->SetSelectedItem(n);
155 }
156
157 void wxComboBox::SetSelection(long from, long to)
158 {
159 wxTextEntry::SetSelection(from, to);
160 }
161
162 int wxComboBox::FindString(const wxString& s, bool bCase) const
163 {
164 if (!bCase)
165 {
166 for (unsigned i = 0; i < GetCount(); i++)
167 {
168 if (s.IsSameAs(GetString(i), false))
169 return i;
170 }
171 return wxNOT_FOUND;
172 }
173
174 return GetComboPeer()->FindString(s);
175 }
176
177 wxString wxComboBox::GetString(unsigned int n) const
178 {
179 wxCHECK_MSG( n < GetCount(), wxString(), "Invalid combobox index" );
180
181 return GetComboPeer()->GetStringAtIndex(n);
182 }
183
184 wxString wxComboBox::GetStringSelection() const
185 {
186 const int sel = GetSelection();
187 return sel == wxNOT_FOUND ? wxString() : GetString(sel);
188 }
189
190 void wxComboBox::SetString(unsigned int n, const wxString& s)
191 {
192 // Notice that we shouldn't delete and insert the item in this control
193 // itself as this would also affect the client data which we need to
194 // preserve here.
195 GetComboPeer()->RemoveItem(n);
196 GetComboPeer()->InsertItem(n, s);
197 SetValue(s); // changing the item in the list won't update the display item
198 }
199
200 void wxComboBox::EnableTextChangedEvents(bool WXUNUSED(enable))
201 {
202 // nothing to do here, events are never generated when we change the
203 // control value programmatically anyhow by Cocoa
204 }
205
206 bool wxComboBox::OSXHandleClicked( double WXUNUSED(timestampsec) )
207 {
208 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
209 event.SetInt(GetSelection());
210 event.SetEventObject(this);
211 event.SetString(GetStringSelection());
212 ProcessCommand(event);
213 return true;
214 }
215
216 wxComboWidgetImpl* wxComboBox::GetComboPeer() const
217 {
218 return dynamic_cast<wxComboWidgetImpl*> (GetPeer());
219 }
220
221 void wxComboBox::Popup()
222 {
223 GetComboPeer()->Popup();
224 }
225
226 void wxComboBox::Dismiss()
227 {
228 GetComboPeer()->Dismiss();
229 }
230
231 #endif // wxUSE_COMBOBOX && wxOSX_USE_COCOA