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