]>
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
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
13 #if wxUSE_COMBOBOX && wxOSX_USE_COCOA
15 #include "wx/combobox.h"
16 #include "wx/osx/private.h"
23 wxComboBox::~wxComboBox()
27 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
28 const wxString
& value
,
31 const wxArrayString
& choices
,
33 const wxValidator
& validator
,
36 wxCArrayString
chs( choices
);
38 return Create( parent
, id
, value
, pos
, size
, chs
.GetCount(),
39 chs
.GetStrings(), style
, validator
, name
);
42 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
43 const wxString
& value
,
46 int n
, const wxString choices
[],
48 const wxValidator
& validator
,
56 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
59 wxASSERT_MSG( !(style
& wxCB_SORT
),
60 "wxCB_SORT not currently supported by wxOSX/Cocoa");
62 SetPeer(wxWidgetImpl::CreateComboBox( this, parent
, id
, NULL
, pos
, size
, style
, GetExtraStyle() ));
64 MacPostControlCreate( pos
, size
);
68 // Set up the initial value, by default the first item is selected.
74 // Needed because it is a wxControlWithItems
75 SetInitialSize( size
);
80 void wxComboBox::DelegateTextChanged( const wxString
& value
)
82 SetStringSelection( value
);
85 void wxComboBox::DelegateChoice( const wxString
& value
)
87 SetStringSelection( value
);
90 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
92 void **clientData
, wxClientDataType type
)
94 const unsigned int numItems
= items
.GetCount();
95 for( unsigned int i
= 0; i
< numItems
; ++i
, ++pos
)
100 GetComboPeer()->InsertItem( idx
, items
[i
] );
102 if (idx
> m_datas
.GetCount())
103 m_datas
.SetCount(idx
);
104 m_datas
.Insert( NULL
, idx
);
105 AssignNewItemClientData(idx
, clientData
, i
, type
);
108 GetPeer()->SetMaximum( GetCount() );
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
116 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
118 m_datas
[n
] = (char*)clientData
;
121 void * wxComboBox::DoGetItemClientData(unsigned int n
) const
123 return (void *)m_datas
[n
];
126 unsigned int wxComboBox::GetCount() const
128 return GetComboPeer()->GetNumberOfItems();
131 void wxComboBox::DoDeleteOneItem(unsigned int n
)
134 GetComboPeer()->RemoveItem(n
);
137 void wxComboBox::DoClear()
140 GetComboPeer()->Clear();
143 void wxComboBox::GetSelection(long *from
, long *to
) const
145 wxTextEntry::GetSelection(from
, to
);
148 int wxComboBox::GetSelection() const
150 return GetComboPeer()->GetSelectedItem();
153 void wxComboBox::SetSelection(int n
)
155 GetComboPeer()->SetSelectedItem(n
);
158 void wxComboBox::SetSelection(long from
, long to
)
160 wxTextEntry::SetSelection(from
, to
);
163 int wxComboBox::FindString(const wxString
& s
, bool bCase
) const
167 for (unsigned i
= 0; i
< GetCount(); i
++)
169 if (s
.IsSameAs(GetString(i
), false))
175 return GetComboPeer()->FindString(s
);
178 wxString
wxComboBox::GetString(unsigned int n
) const
180 wxCHECK_MSG( n
< GetCount(), wxString(), "Invalid combobox index" );
182 return GetComboPeer()->GetStringAtIndex(n
);
185 wxString
wxComboBox::GetStringSelection() const
187 const int sel
= GetSelection();
188 return sel
== wxNOT_FOUND
? wxString() : GetString(sel
);
191 void wxComboBox::SetValue(const wxString
& value
)
193 if ( HasFlag(wxCB_READONLY
) )
194 SetStringSelection( value
) ;
196 wxTextEntry::SetValue( value
);
199 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
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
204 GetComboPeer()->RemoveItem(n
);
205 GetComboPeer()->InsertItem(n
, s
);
206 SetValue(s
); // changing the item in the list won't update the display item
209 void wxComboBox::EnableTextChangedEvents(bool WXUNUSED(enable
))
211 // nothing to do here, events are never generated when we change the
212 // control value programmatically anyhow by Cocoa
215 bool wxComboBox::OSXHandleClicked( double WXUNUSED(timestampsec
) )
217 wxCommandEvent
event(wxEVT_COMBOBOX
, m_windowId
);
218 event
.SetInt(GetSelection());
219 event
.SetEventObject(this);
220 event
.SetString(GetStringSelection());
221 ProcessCommand(event
);
225 wxComboWidgetImpl
* wxComboBox::GetComboPeer() const
227 return dynamic_cast<wxComboWidgetImpl
*> (GetPeer());
230 void wxComboBox::Popup()
232 GetComboPeer()->Popup();
235 void wxComboBox::Dismiss()
237 GetComboPeer()->Dismiss();
240 #endif // wxUSE_COMBOBOX && wxOSX_USE_COCOA