]>
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 | |
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 | ||
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 | ||
24 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl) | |
25 | ||
26 | wxComboBox::~wxComboBox() | |
27 | { | |
28 | } | |
29 | ||
f941a30b KO |
30 | void wxComboBox::Init() |
31 | { | |
32 | } | |
33 | ||
4ddfa282 SC |
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; | |
8e7262fc | 65 | |
8e7262fc VZ |
66 | wxASSERT_MSG( !(style & wxCB_SORT), |
67 | "wxCB_SORT not currently supported by wxOSX/Cocoa"); | |
68 | ||
f941a30b | 69 | m_peer = wxWidgetImpl::CreateComboBox( this, parent, id, NULL, pos, size, style, GetExtraStyle() ); |
4ddfa282 SC |
70 | |
71 | MacPostControlCreate( pos, size ); | |
72 | ||
4ddfa282 SC |
73 | Append(n, choices); |
74 | ||
f25cb0af VZ |
75 | // Set up the initial value, by default the first item is selected. |
76 | if ( !value.empty() ) | |
77 | SetValue(value); | |
78 | else if (n > 0) | |
4ddfa282 SC |
79 | SetSelection( 0 ); |
80 | ||
81 | // Needed because it is a wxControlWithItems | |
82 | SetInitialSize( size ); | |
83 | ||
84 | return true; | |
85 | } | |
86 | ||
4ddfa282 SC |
87 | void wxComboBox::DelegateTextChanged( const wxString& value ) |
88 | { | |
89 | SetStringSelection( value ); | |
90 | } | |
91 | ||
4ddfa282 SC |
92 | void wxComboBox::DelegateChoice( const wxString& value ) |
93 | { | |
94 | SetStringSelection( value ); | |
95 | } | |
96 | ||
c84030e0 KO |
97 | int wxComboBox::DoInsertItems(const wxArrayStringsAdapter& items, |
98 | unsigned int pos, | |
99 | void **clientData, wxClientDataType type) | |
4ddfa282 | 100 | { |
c84030e0 KO |
101 | const unsigned int numItems = items.GetCount(); |
102 | for( unsigned int i = 0; i < numItems; ++i, ++pos ) | |
103 | { | |
104 | unsigned int idx; | |
4ddfa282 | 105 | |
c84030e0 KO |
106 | idx = pos; |
107 | GetComboPeer()->InsertItem( idx, items[i] ); | |
4ddfa282 | 108 | |
c84030e0 KO |
109 | if (idx > m_datas.GetCount()) |
110 | m_datas.SetCount(idx); | |
111 | m_datas.Insert( NULL, idx ); | |
112 | AssignNewItemClientData(idx, clientData, i, type); | |
113 | } | |
4ddfa282 | 114 | |
c84030e0 | 115 | m_peer->SetMaximum( GetCount() ); |
4ddfa282 | 116 | |
c84030e0 | 117 | return pos - 1; |
4ddfa282 SC |
118 | } |
119 | ||
c84030e0 KO |
120 | // ---------------------------------------------------------------------------- |
121 | // client data | |
122 | // ---------------------------------------------------------------------------- | |
123 | void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData) | |
4ddfa282 | 124 | { |
8e7262fc | 125 | wxCHECK_RET( IsValid(n), "invalid index" ); |
4ddfa282 | 126 | |
c84030e0 | 127 | m_datas[n] = (char*)clientData ; |
4ddfa282 SC |
128 | } |
129 | ||
c84030e0 | 130 | void * wxComboBox::DoGetItemClientData(unsigned int n) const |
4ddfa282 | 131 | { |
8e7262fc | 132 | wxCHECK_MSG( IsValid(n), NULL, "invalid index" ); |
4ddfa282 | 133 | |
c84030e0 | 134 | return (void *)m_datas[n]; |
4ddfa282 SC |
135 | } |
136 | ||
8e7262fc | 137 | unsigned int wxComboBox::GetCount() const |
f941a30b | 138 | { |
c84030e0 | 139 | return GetComboPeer()->GetNumberOfItems(); |
4ddfa282 SC |
140 | } |
141 | ||
142 | void wxComboBox::DoDeleteOneItem(unsigned int n) | |
143 | { | |
c84030e0 | 144 | GetComboPeer()->RemoveItem(n); |
4ddfa282 SC |
145 | } |
146 | ||
147 | void wxComboBox::DoClear() | |
148 | { | |
c84030e0 | 149 | GetComboPeer()->Clear(); |
4ddfa282 SC |
150 | } |
151 | ||
c84030e0 KO |
152 | void wxComboBox::GetSelection(long *from, long *to) const |
153 | { | |
154 | wxTextEntry::GetSelection(from, to); | |
155 | } | |
8e7262fc | 156 | |
4ddfa282 SC |
157 | int wxComboBox::GetSelection() const |
158 | { | |
c84030e0 | 159 | return GetComboPeer()->GetSelectedItem(); |
4ddfa282 SC |
160 | } |
161 | ||
c84030e0 | 162 | void wxComboBox::SetSelection(int n) |
4ddfa282 | 163 | { |
c84030e0 | 164 | GetComboPeer()->SetSelectedItem(n); |
f941a30b | 165 | } |
4ddfa282 | 166 | |
c84030e0 | 167 | void wxComboBox::SetSelection(long from, long to) |
f941a30b | 168 | { |
c84030e0 | 169 | wxTextEntry::SetSelection(from, to); |
4ddfa282 SC |
170 | } |
171 | ||
172 | int wxComboBox::FindString(const wxString& s, bool bCase) const | |
173 | { | |
ec073e73 KO |
174 | if (!bCase) |
175 | { | |
96dfe757 | 176 | for (unsigned i = 0; i < GetCount(); i++) |
ec073e73 KO |
177 | { |
178 | if (s.IsSameAs(GetString(i), false)) | |
179 | return i; | |
180 | } | |
181 | return wxNOT_FOUND; | |
182 | } | |
8e7262fc | 183 | |
c84030e0 | 184 | return GetComboPeer()->FindString(s); |
4ddfa282 SC |
185 | } |
186 | ||
187 | wxString wxComboBox::GetString(unsigned int n) const | |
188 | { | |
6f07c007 VZ |
189 | wxCHECK_MSG( n < GetCount(), wxString(), "Invalid combobox index" ); |
190 | ||
c84030e0 | 191 | return GetComboPeer()->GetStringAtIndex(n); |
4ddfa282 SC |
192 | } |
193 | ||
194 | wxString wxComboBox::GetStringSelection() const | |
195 | { | |
6f07c007 VZ |
196 | const int sel = GetSelection(); |
197 | return sel == wxNOT_FOUND ? wxString() : GetString(sel); | |
4ddfa282 SC |
198 | } |
199 | ||
200 | void wxComboBox::SetString(unsigned int n, const wxString& s) | |
201 | { | |
c84030e0 KO |
202 | Delete(n); |
203 | Insert(s, n); | |
204 | SetValue(s); // changing the item in the list won't update the display item | |
f941a30b KO |
205 | } |
206 | ||
de5361fa | 207 | void wxComboBox::EnableTextChangedEvents(bool WXUNUSED(enable)) |
f941a30b | 208 | { |
e567904a VZ |
209 | // nothing to do here, events are never generated when we change the |
210 | // control value programmatically anyhow by Cocoa | |
f941a30b KO |
211 | } |
212 | ||
de5361fa | 213 | bool wxComboBox::OSXHandleClicked( double WXUNUSED(timestampsec) ) |
4ddfa282 SC |
214 | { |
215 | wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId ); | |
216 | event.SetInt(GetSelection()); | |
217 | event.SetEventObject(this); | |
218 | event.SetString(GetStringSelection()); | |
219 | ProcessCommand(event); | |
220 | return true; | |
221 | } | |
222 | ||
c84030e0 KO |
223 | wxComboWidgetImpl* wxComboBox::GetComboPeer() const |
224 | { | |
225 | return dynamic_cast<wxComboWidgetImpl*> (m_peer); | |
226 | } | |
227 | ||
228 | #endif // wxUSE_COMBOBOX && wxOSX_USE_COCOA |