]>
git.saurik.com Git - wxWidgets.git/blob - src/osx/combobox_osx.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/combobox_osx.cpp
3 // Purpose: wxComboBox class using HIView ComboBox
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #if wxUSE_COMBOBOX && wxOSX_USE_COCOA
16 #include "wx/combobox.h"
17 #include "wx/osx/private.h"
24 wxComboBox::~wxComboBox()
28 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
29 const wxString
& value
,
32 const wxArrayString
& choices
,
34 const wxValidator
& validator
,
37 wxCArrayString
chs( choices
);
39 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
40 chs
.GetStrings(), style
, validator
, name
);
43 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
44 const wxString
& value
,
47 int n
, const wxString choices
[],
49 const wxValidator
& validator
,
57 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
60 wxASSERT_MSG( !(style
& wxCB_SORT
),
61 "wxCB_SORT not currently supported by wxOSX/Cocoa");
63 SetPeer(wxWidgetImpl::CreateComboBox( this, parent
, id
, NULL
, pos
, size
, style
, GetExtraStyle() ));
65 MacPostControlCreate( pos
, size
);
69 // Set up the initial value, by default the first item is selected.
75 // Needed because it is a wxControlWithItems
76 SetInitialSize( size
);
81 void wxComboBox::DelegateTextChanged( const wxString
& value
)
83 SetStringSelection( value
);
86 void wxComboBox::DelegateChoice( const wxString
& value
)
88 SetStringSelection( value
);
91 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
93 void **clientData
, wxClientDataType type
)
95 const unsigned int numItems
= items
.GetCount();
96 for( unsigned int i
= 0; i
< numItems
; ++i
, ++pos
)
101 GetComboPeer()->InsertItem( idx
, items
[i
] );
103 if (idx
> m_datas
.GetCount())
104 m_datas
.SetCount(idx
);
105 m_datas
.Insert( NULL
, idx
);
106 AssignNewItemClientData(idx
, clientData
, i
, type
);
109 GetPeer()->SetMaximum( GetCount() );
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
117 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
119 m_datas
[n
] = (char*)clientData
;
122 void * wxComboBox::DoGetItemClientData(unsigned int n
) const
124 return (void *)m_datas
[n
];
127 unsigned int wxComboBox::GetCount() const
129 return GetComboPeer()->GetNumberOfItems();
132 void wxComboBox::DoDeleteOneItem(unsigned int n
)
135 GetComboPeer()->RemoveItem(n
);
138 void wxComboBox::DoClear()
141 GetComboPeer()->Clear();
144 void wxComboBox::GetSelection(long *from
, long *to
) const
146 wxTextEntry::GetSelection(from
, to
);
149 int wxComboBox::GetSelection() const
151 return GetComboPeer()->GetSelectedItem();
154 void wxComboBox::SetSelection(int n
)
156 GetComboPeer()->SetSelectedItem(n
);
159 void wxComboBox::SetSelection(long from
, long to
)
161 wxTextEntry::SetSelection(from
, to
);
164 int wxComboBox::FindString(const wxString
& s
, bool bCase
) const
168 for (unsigned i
= 0; i
< GetCount(); i
++)
170 if (s
.IsSameAs(GetString(i
), false))
176 return GetComboPeer()->FindString(s
);
179 wxString
wxComboBox::GetString(unsigned int n
) const
181 wxCHECK_MSG( n
< GetCount(), wxString(), "Invalid combobox index" );
183 return GetComboPeer()->GetStringAtIndex(n
);
186 wxString
wxComboBox::GetStringSelection() const
188 const int sel
= GetSelection();
189 return sel
== wxNOT_FOUND
? wxString() : GetString(sel
);
192 void wxComboBox::SetValue(const wxString
& value
)
194 if ( HasFlag(wxCB_READONLY
) )
195 SetStringSelection( value
) ;
197 wxTextEntry::SetValue( value
);
200 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
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
205 GetComboPeer()->RemoveItem(n
);
206 GetComboPeer()->InsertItem(n
, s
);
207 SetValue(s
); // changing the item in the list won't update the display item
210 void wxComboBox::EnableTextChangedEvents(bool WXUNUSED(enable
))
212 // nothing to do here, events are never generated when we change the
213 // control value programmatically anyhow by Cocoa
216 bool wxComboBox::OSXHandleClicked( double WXUNUSED(timestampsec
) )
218 wxCommandEvent
event(wxEVT_COMBOBOX
, m_windowId
);
219 event
.SetInt(GetSelection());
220 event
.SetEventObject(this);
221 event
.SetString(GetStringSelection());
222 ProcessCommand(event
);
226 wxComboWidgetImpl
* wxComboBox::GetComboPeer() const
228 return dynamic_cast<wxComboWidgetImpl
*> (GetPeer());
231 void wxComboBox::Popup()
233 GetComboPeer()->Popup();
236 void wxComboBox::Dismiss()
238 GetComboPeer()->Dismiss();
241 #endif // wxUSE_COMBOBOX && wxOSX_USE_COCOA