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