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