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