]> git.saurik.com Git - wxWidgets.git/blob - src/osx/combobox_osx.cpp
merging back XTI branch part 2
[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$
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 wxComboBox::~wxComboBox()
25 {
26 }
27
28 void wxComboBox::Init()
29 {
30 }
31
32 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
33 const wxString& value,
34 const wxPoint& pos,
35 const wxSize& size,
36 const wxArrayString& choices,
37 long style,
38 const wxValidator& validator,
39 const wxString& name)
40 {
41 wxCArrayString chs( choices );
42
43 return Create( parent, id, value, pos, size, chs.GetCount(),
44 chs.GetStrings(), style, validator, name );
45 }
46
47 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
48 const wxString& value,
49 const wxPoint& pos,
50 const wxSize& size,
51 int n, const wxString choices[],
52 long style,
53 const wxValidator& validator,
54 const wxString& name)
55 {
56 m_text = NULL;
57 m_choice = NULL;
58
59 m_macIsUserPane = false;
60
61 if ( !wxControl::Create( parent, id, pos, size, style, validator, name ) )
62 return false;
63
64 wxASSERT_MSG( !(style & wxCB_SORT),
65 "wxCB_SORT not currently supported by wxOSX/Cocoa");
66
67 m_peer = wxWidgetImpl::CreateComboBox( this, parent, id, NULL, pos, size, style, GetExtraStyle() );
68
69 MacPostControlCreate( pos, size );
70
71 Append(n, choices);
72
73 // Set up the initial value, by default the first item is selected.
74 if ( !value.empty() )
75 SetValue(value);
76 else 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 }
149
150 void wxComboBox::GetSelection(long *from, long *to) const
151 {
152 wxTextEntry::GetSelection(from, to);
153 }
154
155 int wxComboBox::GetSelection() const
156 {
157 return GetComboPeer()->GetSelectedItem();
158 }
159
160 void wxComboBox::SetSelection(int n)
161 {
162 GetComboPeer()->SetSelectedItem(n);
163 }
164
165 void wxComboBox::SetSelection(long from, long to)
166 {
167 wxTextEntry::SetSelection(from, to);
168 }
169
170 int wxComboBox::FindString(const wxString& s, bool bCase) const
171 {
172 if (!bCase)
173 {
174 for (unsigned i = 0; i < GetCount(); i++)
175 {
176 if (s.IsSameAs(GetString(i), false))
177 return i;
178 }
179 return wxNOT_FOUND;
180 }
181
182 return GetComboPeer()->FindString(s);
183 }
184
185 wxString wxComboBox::GetString(unsigned int n) const
186 {
187 wxCHECK_MSG( n < GetCount(), wxString(), "Invalid combobox index" );
188
189 return GetComboPeer()->GetStringAtIndex(n);
190 }
191
192 wxString wxComboBox::GetStringSelection() const
193 {
194 const int sel = GetSelection();
195 return sel == wxNOT_FOUND ? wxString() : GetString(sel);
196 }
197
198 void wxComboBox::SetString(unsigned int n, const wxString& s)
199 {
200 Delete(n);
201 Insert(s, n);
202 SetValue(s); // changing the item in the list won't update the display item
203 }
204
205 void wxComboBox::EnableTextChangedEvents(bool WXUNUSED(enable))
206 {
207 // nothing to do here, events are never generated when we change the
208 // control value programmatically anyhow by Cocoa
209 }
210
211 bool wxComboBox::OSXHandleClicked( double WXUNUSED(timestampsec) )
212 {
213 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
214 event.SetInt(GetSelection());
215 event.SetEventObject(this);
216 event.SetString(GetStringSelection());
217 ProcessCommand(event);
218 return true;
219 }
220
221 wxComboWidgetImpl* wxComboBox::GetComboPeer() const
222 {
223 return dynamic_cast<wxComboWidgetImpl*> (m_peer);
224 }
225
226 #endif // wxUSE_COMBOBOX && wxOSX_USE_COCOA