Implement case-insensitive find and read only support for Cocoa combobox.
[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: combobox_osx.cpp 58318 2009-01-23 08:36:16Z RR $
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 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
25
26 wxComboBox::~wxComboBox()
27 {
28 }
29
30 void wxComboBox::Init()
31 {
32 }
33
34 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
35 const wxString& value,
36 const wxPoint& pos,
37 const wxSize& size,
38 const wxArrayString& choices,
39 long style,
40 const wxValidator& validator,
41 const wxString& name)
42 {
43 wxCArrayString chs( choices );
44
45 return Create( parent, id, value, pos, size, chs.GetCount(),
46 chs.GetStrings(), style, validator, name );
47 }
48
49 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
50 const wxString& value,
51 const wxPoint& pos,
52 const wxSize& size,
53 int n, const wxString choices[],
54 long style,
55 const wxValidator& validator,
56 const wxString& name)
57 {
58 m_text = NULL;
59 m_choice = NULL;
60
61 m_macIsUserPane = false;
62
63 if ( !wxControl::Create( parent, id, pos, size, style, validator, name ) )
64 return false;
65
66 wxASSERT_MSG( !(style & wxCB_SORT),
67 "wxCB_SORT not currently supported by wxOSX/Cocoa");
68
69 m_peer = wxWidgetImpl::CreateComboBox( this, parent, id, NULL, pos, size, style, GetExtraStyle() );
70
71 MacPostControlCreate( pos, size );
72
73 Append(n, choices);
74
75 // Set the first item as being selected
76 if (n > 0)
77 SetSelection( 0 );
78
79 // Needed because it is a wxControlWithItems
80 SetInitialSize( size );
81
82 return true;
83 }
84
85 void wxComboBox::DelegateTextChanged( const wxString& value )
86 {
87 SetStringSelection( value );
88 }
89
90 void wxComboBox::DelegateChoice( const wxString& value )
91 {
92 SetStringSelection( value );
93 }
94
95 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter& items,
96 unsigned int pos,
97 void **clientData, wxClientDataType type)
98 {
99 const unsigned int numItems = items.GetCount();
100 for( unsigned int i = 0; i < numItems; ++i, ++pos )
101 {
102 unsigned int idx;
103
104 idx = pos;
105 GetComboPeer()->InsertItem( idx, items[i] );
106
107 if (idx > m_datas.GetCount())
108 m_datas.SetCount(idx);
109 m_datas.Insert( NULL, idx );
110 AssignNewItemClientData(idx, clientData, i, type);
111 }
112
113 m_peer->SetMaximum( GetCount() );
114
115 return pos - 1;
116 }
117
118 // ----------------------------------------------------------------------------
119 // client data
120 // ----------------------------------------------------------------------------
121 void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData)
122 {
123 wxCHECK_RET( IsValid(n), "invalid index" );
124
125 m_datas[n] = (char*)clientData ;
126 }
127
128 void * wxComboBox::DoGetItemClientData(unsigned int n) const
129 {
130 wxCHECK_MSG( IsValid(n), NULL, "invalid index" );
131
132 return (void *)m_datas[n];
133 }
134
135 unsigned int wxComboBox::GetCount() const
136 {
137 return GetComboPeer()->GetNumberOfItems();
138 }
139
140 void wxComboBox::DoDeleteOneItem(unsigned int n)
141 {
142 GetComboPeer()->RemoveItem(n);
143 }
144
145 void wxComboBox::DoClear()
146 {
147 GetComboPeer()->Clear();
148 SetValue(wxEmptyString);
149 }
150
151 void wxComboBox::GetSelection(long *from, long *to) const
152 {
153 wxTextEntry::GetSelection(from, to);
154 }
155
156 int wxComboBox::GetSelection() const
157 {
158 return GetComboPeer()->GetSelectedItem();
159 }
160
161 void wxComboBox::SetSelection(int n)
162 {
163 GetComboPeer()->SetSelectedItem(n);
164 }
165
166 void wxComboBox::SetSelection(long from, long to)
167 {
168 wxTextEntry::SetSelection(from, to);
169 }
170
171 int wxComboBox::FindString(const wxString& s, bool bCase) const
172 {
173 if (!bCase)
174 {
175 for (int i = 0; i < GetCount(); i++)
176 {
177 if (s.IsSameAs(GetString(i), false))
178 return i;
179 }
180 return wxNOT_FOUND;
181 }
182
183 return GetComboPeer()->FindString(s);
184 }
185
186 wxString wxComboBox::GetString(unsigned int n) const
187 {
188 return GetComboPeer()->GetStringAtIndex(n);
189 }
190
191 wxString wxComboBox::GetStringSelection() const
192 {
193 return GetString(GetSelection());
194 }
195
196 void wxComboBox::SetString(unsigned int n, const wxString& s)
197 {
198 Delete(n);
199 Insert(s, n);
200 SetValue(s); // changing the item in the list won't update the display item
201 }
202
203 void wxComboBox::EnableTextChangedEvents(bool enable)
204 {
205 wxFAIL_MSG("Method Not Implemented.");
206 }
207
208 bool wxComboBox::OSXHandleClicked( double timestampsec )
209 {
210 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
211 event.SetInt(GetSelection());
212 event.SetEventObject(this);
213 event.SetString(GetStringSelection());
214 ProcessCommand(event);
215 return true;
216 }
217
218 wxTextWidgetImpl* wxComboBox::GetTextPeer() const
219 {
220 return dynamic_cast<wxTextWidgetImpl*> (m_peer);
221 }
222
223 wxComboWidgetImpl* wxComboBox::GetComboPeer() const
224 {
225 return dynamic_cast<wxComboWidgetImpl*> (m_peer);
226 }
227
228 #endif // wxUSE_COMBOBOX && wxOSX_USE_COCOA